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 unsigned int *lineno_counts;
951 bfd_size_type linesz;
954 bfd_boolean keep_syms;
956 unsigned int csect_index;
957 asection *first_csect;
958 bfd_size_type symesz;
961 struct reloc_info_struct
963 struct internal_reloc *relocs;
966 } *reloc_info = NULL;
969 keep_syms = obj_coff_keep_syms (abfd);
971 if ((abfd->flags & DYNAMIC) != 0
972 && ! info->static_link)
974 if (! xcoff_link_add_dynamic_symbols (abfd, info))
978 /* Create the loader, toc, gl, ds and debug sections, if needed. */
979 if (! xcoff_link_create_extra_sections (abfd, info))
982 if ((abfd->flags & DYNAMIC) != 0
983 && ! info->static_link)
986 n_tmask = coff_data (abfd)->local_n_tmask;
987 n_btshft = coff_data (abfd)->local_n_btshft;
989 /* Define macros so that ISFCN, et. al., macros work correctly. */
990 #define N_TMASK n_tmask
991 #define N_BTSHFT n_btshft
993 if (info->keep_memory)
994 default_copy = FALSE;
998 symcount = obj_raw_syment_count (abfd);
1000 /* We keep a list of the linker hash table entries that correspond
1001 to each external symbol. */
1002 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1003 sym_hash = bfd_zalloc (abfd, amt);
1004 if (sym_hash == NULL && symcount != 0)
1006 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1008 /* Because of the weird stuff we are doing with XCOFF csects, we can
1009 not easily determine which section a symbol is in, so we store
1010 the information in the tdata for the input file. */
1011 amt = symcount * sizeof (asection *);
1012 csect_cache = bfd_zalloc (abfd, amt);
1013 if (csect_cache == NULL && symcount != 0)
1015 xcoff_data (abfd)->csects = csect_cache;
1017 /* We garbage-collect line-number information on a symbol-by-symbol
1018 basis, so we need to have quick access to the number of entries
1020 amt = symcount * sizeof (unsigned int);
1021 lineno_counts = bfd_zalloc (abfd, amt);
1022 if (lineno_counts == NULL && symcount != 0)
1024 xcoff_data (abfd)->lineno_counts = lineno_counts;
1026 /* While splitting sections into csects, we need to assign the
1027 relocs correctly. The relocs and the csects must both be in
1028 order by VMA within a given section, so we handle this by
1029 scanning along the relocs as we process the csects. We index
1030 into reloc_info using the section target_index. */
1031 amt = abfd->section_count + 1;
1032 amt *= sizeof (struct reloc_info_struct);
1033 reloc_info = bfd_zmalloc (amt);
1034 if (reloc_info == NULL)
1037 /* Read in the relocs and line numbers for each section. */
1038 linesz = bfd_coff_linesz (abfd);
1040 for (o = abfd->sections; o != NULL; o = o->next)
1044 if ((o->flags & SEC_RELOC) != 0)
1046 reloc_info[o->target_index].relocs =
1047 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1048 amt = o->reloc_count;
1049 amt *= sizeof (asection *);
1050 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1051 if (reloc_info[o->target_index].csects == NULL)
1055 if ((info->strip == strip_none || info->strip == strip_some)
1056 && o->lineno_count > 0)
1060 amt = linesz * o->lineno_count;
1061 linenos = bfd_malloc (amt);
1062 if (linenos == NULL)
1064 reloc_info[o->target_index].linenos = linenos;
1065 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1066 || bfd_bread (linenos, amt, abfd) != amt)
1071 /* Don't let the linker relocation routines discard the symbols. */
1072 obj_coff_keep_syms (abfd) = TRUE;
1078 symesz = bfd_coff_symesz (abfd);
1079 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1080 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1081 esym_end = esym + symcount * symesz;
1083 while (esym < esym_end)
1085 struct internal_syment sym;
1086 union internal_auxent aux;
1088 char buf[SYMNMLEN + 1];
1093 struct xcoff_link_hash_entry *set_toc;
1095 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1097 /* In this pass we are only interested in symbols with csect
1099 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1102 Normally csect is a .pr, .rw etc. created in the loop
1103 If C_FILE or first time, handle special
1105 Advance esym, sym_hash, csect_hash ptrs. */
1106 if (sym.n_sclass == C_FILE)
1109 *csect_cache = csect;
1110 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1111 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1113 *csect_cache = NULL;
1114 esym += (sym.n_numaux + 1) * symesz;
1115 sym_hash += sym.n_numaux + 1;
1116 csect_cache += sym.n_numaux + 1;
1117 lineno_counts += sym.n_numaux + 1;
1122 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1127 /* If this symbol has line number information attached to it,
1128 and we're not stripping it, count the number of entries and
1129 add them to the count for this csect. In the final link pass
1130 we are going to attach line number information by symbol,
1131 rather than by section, in order to more easily handle
1132 garbage collection. */
1133 if ((info->strip == strip_none || info->strip == strip_some)
1136 && ISFCN (sym.n_type))
1138 union internal_auxent auxlin;
1140 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1141 sym.n_type, sym.n_sclass,
1142 0, sym.n_numaux, (void *) &auxlin);
1144 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1146 asection *enclosing;
1147 bfd_signed_vma linoff;
1149 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1150 if (enclosing == NULL)
1152 (*_bfd_error_handler)
1153 (_("%B: `%s' has line numbers but no enclosing section"),
1155 bfd_set_error (bfd_error_bad_value);
1158 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1159 - enclosing->line_filepos);
1160 /* Explicit cast to bfd_signed_vma for compiler. */
1161 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1163 struct internal_lineno lin;
1164 bfd_byte *linpstart;
1166 linpstart = (reloc_info[enclosing->target_index].linenos
1168 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1170 && ((bfd_size_type) lin.l_addr.l_symndx
1172 - (bfd_byte *) obj_coff_external_syms (abfd))
1175 bfd_byte *linpend, *linp;
1177 linpend = (reloc_info[enclosing->target_index].linenos
1178 + enclosing->lineno_count * linesz);
1179 for (linp = linpstart + linesz;
1183 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1185 if (lin.l_lnno == 0)
1188 *lineno_counts = (linp - linpstart) / linesz;
1189 /* The setting of line_filepos will only be
1190 useful if all the line number entries for a
1191 csect are contiguous; this only matters for
1193 if (csect->line_filepos == 0)
1194 csect->line_filepos =
1195 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1201 /* Pick up the csect auxiliary information. */
1202 if (sym.n_numaux == 0)
1204 (*_bfd_error_handler)
1205 (_("%B: class %d symbol `%s' has no aux entries"),
1206 abfd, sym.n_sclass, name);
1207 bfd_set_error (bfd_error_bad_value);
1211 bfd_coff_swap_aux_in (abfd,
1212 (void *) (esym + symesz * sym.n_numaux),
1213 sym.n_type, sym.n_sclass,
1214 sym.n_numaux - 1, sym.n_numaux,
1217 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1227 (*_bfd_error_handler)
1228 (_("%B: symbol `%s' has unrecognized csect type %d"),
1230 bfd_set_error (bfd_error_bad_value);
1234 /* This is an external reference. */
1235 if (sym.n_sclass == C_HIDEXT
1236 || sym.n_scnum != N_UNDEF
1237 || aux.x_csect.x_scnlen.l != 0)
1239 (*_bfd_error_handler)
1240 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1241 abfd, name, sym.n_sclass, sym.n_scnum,
1242 aux.x_csect.x_scnlen.l);
1243 bfd_set_error (bfd_error_bad_value);
1247 /* An XMC_XO external reference is actually a reference to
1248 an absolute location. */
1249 if (aux.x_csect.x_smclas != XMC_XO)
1250 section = bfd_und_section_ptr;
1253 section = bfd_abs_section_ptr;
1254 value = sym.n_value;
1260 csect_index = -(unsigned) 1;
1262 /* When we see a TOC anchor, we record the TOC value. */
1263 if (aux.x_csect.x_smclas == XMC_TC0)
1265 if (sym.n_sclass != C_HIDEXT
1266 || aux.x_csect.x_scnlen.l != 0)
1268 (*_bfd_error_handler)
1269 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1270 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1271 bfd_set_error (bfd_error_bad_value);
1274 xcoff_data (abfd)->toc = sym.n_value;
1277 /* We must merge TOC entries for the same symbol. We can
1278 merge two TOC entries if they are both C_HIDEXT, they
1279 both have the same name, they are both 4 or 8 bytes long, and
1280 they both have a relocation table entry for an external
1281 symbol with the same name. Unfortunately, this means
1282 that we must look through the relocations. Ick.
1284 Logic for 32 bit vs 64 bit.
1285 32 bit has a csect length of 4 for TOC
1286 64 bit has a csect length of 8 for TOC
1288 The conditions to get past the if-check are not that bad.
1289 They are what is used to create the TOC csects in the first
1291 if (aux.x_csect.x_smclas == XMC_TC
1292 && sym.n_sclass == C_HIDEXT
1293 && info->output_bfd->xvec == abfd->xvec
1294 && ((bfd_xcoff_is_xcoff32 (abfd)
1295 && aux.x_csect.x_scnlen.l == 4)
1296 || (bfd_xcoff_is_xcoff64 (abfd)
1297 && aux.x_csect.x_scnlen.l == 8)))
1299 asection *enclosing;
1300 struct internal_reloc *relocs;
1301 bfd_size_type relindx;
1302 struct internal_reloc *rel;
1304 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1305 if (enclosing == NULL)
1308 relocs = reloc_info[enclosing->target_index].relocs;
1309 amt = enclosing->reloc_count;
1310 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1311 rel = relocs + relindx;
1313 /* 32 bit R_POS r_size is 31
1314 64 bit R_POS r_size is 63 */
1315 if (relindx < enclosing->reloc_count
1316 && rel->r_vaddr == (bfd_vma) sym.n_value
1317 && rel->r_type == R_POS
1318 && ((bfd_xcoff_is_xcoff32 (abfd)
1319 && rel->r_size == 31)
1320 || (bfd_xcoff_is_xcoff64 (abfd)
1321 && rel->r_size == 63)))
1325 struct internal_syment relsym;
1327 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1328 + rel->r_symndx * symesz);
1329 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1330 if (relsym.n_sclass == C_EXT)
1332 const char *relname;
1333 char relbuf[SYMNMLEN + 1];
1335 struct xcoff_link_hash_entry *h;
1337 /* At this point we know that the TOC entry is
1338 for an externally visible symbol. */
1339 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1341 if (relname == NULL)
1344 /* We only merge TOC entries if the TC name is
1345 the same as the symbol name. This handles
1346 the normal case, but not common cases like
1347 SYM.P4 which gcc generates to store SYM + 4
1348 in the TOC. FIXME. */
1349 if (strcmp (name, relname) == 0)
1351 copy = (! info->keep_memory
1352 || relsym._n._n_n._n_zeroes != 0
1353 || relsym._n._n_n._n_offset == 0);
1354 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1355 relname, TRUE, copy,
1360 /* At this point h->root.type could be
1361 bfd_link_hash_new. That should be OK,
1362 since we know for sure that we will come
1363 across this symbol as we step through the
1366 /* We store h in *sym_hash for the
1367 convenience of the relocate_section
1371 if (h->toc_section != NULL)
1373 asection **rel_csects;
1375 /* We already have a TOC entry for this
1376 symbol, so we can just ignore this
1379 reloc_info[enclosing->target_index].csects;
1380 rel_csects[relindx] = bfd_und_section_ptr;
1384 /* We are about to create a TOC entry for
1393 asection *enclosing;
1395 /* We need to create a new section. We get the name from
1396 the csect storage mapping class, so that the linker can
1397 accumulate similar csects together. */
1399 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1403 /* The enclosing section is the main section : .data, .text
1404 or .bss that the csect is coming from. */
1405 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1406 if (enclosing == NULL)
1409 if (! bfd_is_abs_section (enclosing)
1410 && ((bfd_vma) sym.n_value < enclosing->vma
1411 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1412 > enclosing->vma + enclosing->size)))
1414 (*_bfd_error_handler)
1415 (_("%B: csect `%s' not in enclosing section"),
1417 bfd_set_error (bfd_error_bad_value);
1420 csect->vma = sym.n_value;
1421 csect->filepos = (enclosing->filepos
1424 csect->size = aux.x_csect.x_scnlen.l;
1425 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1426 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1428 /* Record the enclosing section in the tdata for this new
1430 amt = sizeof (struct coff_section_tdata);
1431 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1432 if (csect->used_by_bfd == NULL)
1434 amt = sizeof (struct xcoff_section_tdata);
1435 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1436 if (coff_section_data (abfd, csect)->tdata == NULL)
1438 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1439 xcoff_section_data (abfd, csect)->lineno_count =
1440 enclosing->lineno_count;
1442 if (enclosing->owner == abfd)
1444 struct internal_reloc *relocs;
1445 bfd_size_type relindx;
1446 struct internal_reloc *rel;
1447 asection **rel_csect;
1449 relocs = reloc_info[enclosing->target_index].relocs;
1450 amt = enclosing->reloc_count;
1451 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1453 rel = relocs + relindx;
1454 rel_csect = (reloc_info[enclosing->target_index].csects
1457 csect->rel_filepos = (enclosing->rel_filepos
1458 + relindx * bfd_coff_relsz (abfd));
1459 while (relindx < enclosing->reloc_count
1460 && *rel_csect == NULL
1461 && rel->r_vaddr < csect->vma + csect->size)
1465 csect->flags |= SEC_RELOC;
1466 ++csect->reloc_count;
1473 /* There are a number of other fields and section flags
1474 which we do not bother to set. */
1476 csect_index = ((esym
1477 - (bfd_byte *) obj_coff_external_syms (abfd))
1480 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1482 if (first_csect == NULL)
1483 first_csect = csect;
1485 /* If this symbol is C_EXT, we treat it as starting at the
1486 beginning of the newly created section. */
1487 if (sym.n_sclass == C_EXT)
1493 /* If this is a TOC section for a symbol, record it. */
1494 if (set_toc != NULL)
1495 set_toc->toc_section = csect;
1500 /* This is a label definition. The x_scnlen field is the
1501 symbol index of the csect. Usually the XTY_LD symbol will
1502 follow its appropriate XTY_SD symbol. The .set pseudo op can
1503 cause the XTY_LD to not follow the XTY_SD symbol. */
1508 if (aux.x_csect.x_scnlen.l < 0
1509 || (aux.x_csect.x_scnlen.l
1510 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1514 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1516 || (section->flags & SEC_HAS_CONTENTS) == 0)
1521 (*_bfd_error_handler)
1522 (_("%B: misplaced XTY_LD `%s'"),
1524 bfd_set_error (bfd_error_bad_value);
1528 value = sym.n_value - csect->vma;
1533 /* This is an unitialized csect. We could base the name on
1534 the storage mapping class, but we don't bother except for
1535 an XMC_TD symbol. If this csect is externally visible,
1536 it is a common symbol. We put XMC_TD symbols in sections
1537 named .tocbss, and rely on the linker script to put that
1540 if (aux.x_csect.x_smclas == XMC_TD)
1542 /* The linker script puts the .td section in the data
1543 section after the .tc section. */
1544 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1548 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1553 csect->vma = sym.n_value;
1554 csect->size = aux.x_csect.x_scnlen.l;
1555 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1556 /* There are a number of other fields and section flags
1557 which we do not bother to set. */
1559 csect_index = ((esym
1560 - (bfd_byte *) obj_coff_external_syms (abfd))
1563 amt = sizeof (struct coff_section_tdata);
1564 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1565 if (csect->used_by_bfd == NULL)
1567 amt = sizeof (struct xcoff_section_tdata);
1568 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1569 if (coff_section_data (abfd, csect)->tdata == NULL)
1571 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1573 if (first_csect == NULL)
1574 first_csect = csect;
1576 if (sym.n_sclass == C_EXT)
1578 csect->flags |= SEC_IS_COMMON;
1581 value = aux.x_csect.x_scnlen.l;
1587 /* Check for magic symbol names. */
1588 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1589 && aux.x_csect.x_smclas != XMC_TC
1590 && aux.x_csect.x_smclas != XMC_TD)
1596 if (strcmp (name, "_text") == 0)
1597 i = XCOFF_SPECIAL_SECTION_TEXT;
1598 else if (strcmp (name, "_etext") == 0)
1599 i = XCOFF_SPECIAL_SECTION_ETEXT;
1600 else if (strcmp (name, "_data") == 0)
1601 i = XCOFF_SPECIAL_SECTION_DATA;
1602 else if (strcmp (name, "_edata") == 0)
1603 i = XCOFF_SPECIAL_SECTION_EDATA;
1604 else if (strcmp (name, "_end") == 0)
1605 i = XCOFF_SPECIAL_SECTION_END;
1607 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1608 i = XCOFF_SPECIAL_SECTION_END2;
1611 xcoff_hash_table (info)->special_sections[i] = csect;
1614 /* Now we have enough information to add the symbol to the
1615 linker hash table. */
1617 if (sym.n_sclass == C_EXT)
1621 BFD_ASSERT (section != NULL);
1623 /* We must copy the name into memory if we got it from the
1624 syment itself, rather than the string table. */
1625 copy = default_copy;
1626 if (sym._n._n_n._n_zeroes != 0
1627 || sym._n._n_n._n_offset == 0)
1630 /* The AIX linker appears to only detect multiple symbol
1631 definitions when there is a reference to the symbol. If
1632 a symbol is defined multiple times, and the only
1633 references are from the same object file, the AIX linker
1634 appears to permit it. It does not merge the different
1635 definitions, but handles them independently. On the
1636 other hand, if there is a reference, the linker reports
1639 This matters because the AIX <net/net_globals.h> header
1640 file actually defines an initialized array, so we have to
1641 actually permit that to work.
1643 Just to make matters even more confusing, the AIX linker
1644 appears to permit multiple symbol definitions whenever
1645 the second definition is in an archive rather than an
1646 object file. This may be a consequence of the manner in
1647 which it handles archives: I think it may load the entire
1648 archive in as separate csects, and then let garbage
1649 collection discard symbols.
1651 We also have to handle the case of statically linking a
1652 shared object, which will cause symbol redefinitions,
1653 although this is an easier case to detect. */
1655 if (info->output_bfd->xvec == abfd->xvec)
1657 if (! bfd_is_und_section (section))
1658 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1659 name, TRUE, copy, FALSE);
1661 /* Make a copy of the symbol name to prevent problems with
1663 *sym_hash = ((struct xcoff_link_hash_entry *)
1664 bfd_wrapped_link_hash_lookup (abfd, info, name,
1665 TRUE, TRUE, FALSE));
1667 if (*sym_hash == NULL)
1669 if (((*sym_hash)->root.type == bfd_link_hash_defined
1670 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1671 && ! bfd_is_und_section (section)
1672 && ! bfd_is_com_section (section))
1674 /* This is a second definition of a defined symbol. */
1675 if ((abfd->flags & DYNAMIC) != 0
1676 && ((*sym_hash)->smclas != XMC_GL
1677 || aux.x_csect.x_smclas == XMC_GL
1678 || ((*sym_hash)->root.u.def.section->owner->flags
1681 /* The new symbol is from a shared library, and
1682 either the existing symbol is not global
1683 linkage code or this symbol is global linkage
1684 code. If the existing symbol is global
1685 linkage code and the new symbol is not, then
1686 we want to use the new symbol. */
1687 section = bfd_und_section_ptr;
1690 else if (((*sym_hash)->root.u.def.section->owner->flags
1693 /* The existing symbol is from a shared library.
1695 (*sym_hash)->root.type = bfd_link_hash_undefined;
1696 (*sym_hash)->root.u.undef.abfd =
1697 (*sym_hash)->root.u.def.section->owner;
1699 else if (abfd->my_archive != NULL)
1701 /* This is a redefinition in an object contained
1702 in an archive. Just ignore it. See the
1704 section = bfd_und_section_ptr;
1707 else if ((*sym_hash)->root.u.undef.next != NULL
1708 || info->hash->undefs_tail == &(*sym_hash)->root)
1710 /* This symbol has been referenced. In this
1711 case, we just continue and permit the
1712 multiple definition error. See the comment
1713 above about the behaviour of the AIX linker. */
1715 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1717 /* The symbols are both csects of the same
1718 class. There is at least a chance that this
1719 is a semi-legitimate redefinition. */
1720 section = bfd_und_section_ptr;
1722 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1725 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1726 && ((*sym_hash)->root.type == bfd_link_hash_defined
1727 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1728 && (bfd_is_und_section (section)
1729 || bfd_is_com_section (section)))
1731 /* This is a reference to a multiply defined symbol.
1732 Report the error now. See the comment above
1733 about the behaviour of the AIX linker. We could
1734 also do this with warning symbols, but I'm not
1735 sure the XCOFF linker is wholly prepared to
1736 handle them, and that would only be a warning,
1738 if (! ((*info->callbacks->multiple_definition)
1739 (info, (*sym_hash)->root.root.string,
1740 NULL, NULL, (bfd_vma) 0,
1741 (*sym_hash)->root.u.def.section->owner,
1742 (*sym_hash)->root.u.def.section,
1743 (*sym_hash)->root.u.def.value)))
1745 /* Try not to give this error too many times. */
1746 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1750 /* _bfd_generic_link_add_one_symbol may call the linker to
1751 generate an error message, and the linker may try to read
1752 the symbol table to give a good error. Right now, the
1753 line numbers are in an inconsistent state, since they are
1754 counted both in the real sections and in the new csects.
1755 We need to leave the count in the real sections so that
1756 the linker can report the line number of the error
1757 correctly, so temporarily clobber the link to the csects
1758 so that the linker will not try to read the line numbers
1759 a second time from the csects. */
1760 BFD_ASSERT (last_real->next == first_csect);
1761 last_real->next = NULL;
1762 if (! (_bfd_generic_link_add_one_symbol
1763 (info, abfd, name, flags, section, value,
1765 (struct bfd_link_hash_entry **) sym_hash)))
1767 last_real->next = first_csect;
1769 if (smtyp == XTY_CM)
1771 if ((*sym_hash)->root.type != bfd_link_hash_common
1772 || (*sym_hash)->root.u.c.p->section != csect)
1773 /* We don't need the common csect we just created. */
1776 (*sym_hash)->root.u.c.p->alignment_power
1777 = csect->alignment_power;
1780 if (info->output_bfd->xvec == abfd->xvec)
1784 if (smtyp == XTY_ER || smtyp == XTY_CM)
1785 flag = XCOFF_REF_REGULAR;
1787 flag = XCOFF_DEF_REGULAR;
1788 (*sym_hash)->flags |= flag;
1790 if ((*sym_hash)->smclas == XMC_UA
1791 || flag == XCOFF_DEF_REGULAR)
1792 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1796 if (smtyp == XTY_ER)
1797 *csect_cache = section;
1800 *csect_cache = csect;
1802 xcoff_section_data (abfd, csect)->last_symndx
1803 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
1806 esym += (sym.n_numaux + 1) * symesz;
1807 sym_hash += sym.n_numaux + 1;
1808 csect_cache += sym.n_numaux + 1;
1809 lineno_counts += sym.n_numaux + 1;
1812 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1814 /* Make sure that we have seen all the relocs. */
1815 for (o = abfd->sections; o != first_csect; o = o->next)
1817 /* Reset the section size and the line number count, since the
1818 data is now attached to the csects. Don't reset the size of
1819 the .debug section, since we need to read it below in
1820 bfd_xcoff_size_dynamic_sections. */
1821 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1823 o->lineno_count = 0;
1825 if ((o->flags & SEC_RELOC) != 0)
1828 struct internal_reloc *rel;
1829 asection **rel_csect;
1831 rel = reloc_info[o->target_index].relocs;
1832 rel_csect = reloc_info[o->target_index].csects;
1834 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1836 if (*rel_csect == NULL)
1838 (*_bfd_error_handler)
1839 (_("%B: reloc %s:%d not in csect"),
1841 bfd_set_error (bfd_error_bad_value);
1845 /* We identify all function symbols that are the target
1846 of a relocation, so that we can create glue code for
1847 functions imported from dynamic objects. */
1848 if (info->output_bfd->xvec == abfd->xvec
1849 && *rel_csect != bfd_und_section_ptr
1850 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1852 struct xcoff_link_hash_entry *h;
1854 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1855 /* If the symbol name starts with a period, it is
1856 the code of a function. If the symbol is
1857 currently undefined, then add an undefined symbol
1858 for the function descriptor. This should do no
1859 harm, because any regular object that defines the
1860 function should also define the function
1861 descriptor. It helps, because it means that we
1862 will identify the function descriptor with a
1863 dynamic object if a dynamic object defines it. */
1864 if (h->root.root.string[0] == '.'
1865 && h->descriptor == NULL)
1867 struct xcoff_link_hash_entry *hds;
1868 struct bfd_link_hash_entry *bh;
1870 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1871 h->root.root.string + 1,
1875 if (hds->root.type == bfd_link_hash_new)
1878 if (! (_bfd_generic_link_add_one_symbol
1879 (info, abfd, hds->root.root.string,
1880 (flagword) 0, bfd_und_section_ptr,
1881 (bfd_vma) 0, NULL, FALSE,
1884 hds = (struct xcoff_link_hash_entry *) bh;
1886 hds->flags |= XCOFF_DESCRIPTOR;
1887 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
1888 hds->descriptor = h;
1889 h->descriptor = hds;
1891 if (h->root.root.string[0] == '.')
1892 h->flags |= XCOFF_CALLED;
1896 free (reloc_info[o->target_index].csects);
1897 reloc_info[o->target_index].csects = NULL;
1899 /* Reset SEC_RELOC and the reloc_count, since the reloc
1900 information is now attached to the csects. */
1901 o->flags &=~ SEC_RELOC;
1904 /* If we are not keeping memory, free the reloc information. */
1905 if (! info->keep_memory
1906 && coff_section_data (abfd, o) != NULL
1907 && coff_section_data (abfd, o)->relocs != NULL
1908 && ! coff_section_data (abfd, o)->keep_relocs)
1910 free (coff_section_data (abfd, o)->relocs);
1911 coff_section_data (abfd, o)->relocs = NULL;
1915 /* Free up the line numbers. FIXME: We could cache these
1916 somewhere for the final link, to avoid reading them again. */
1917 if (reloc_info[o->target_index].linenos != NULL)
1919 free (reloc_info[o->target_index].linenos);
1920 reloc_info[o->target_index].linenos = NULL;
1926 obj_coff_keep_syms (abfd) = keep_syms;
1931 if (reloc_info != NULL)
1933 for (o = abfd->sections; o != NULL; o = o->next)
1935 if (reloc_info[o->target_index].csects != NULL)
1936 free (reloc_info[o->target_index].csects);
1937 if (reloc_info[o->target_index].linenos != NULL)
1938 free (reloc_info[o->target_index].linenos);
1942 obj_coff_keep_syms (abfd) = keep_syms;
1949 /* Add symbols from an XCOFF object file. */
1952 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1954 if (! _bfd_coff_get_external_symbols (abfd))
1956 if (! xcoff_link_add_symbols (abfd, info))
1958 if (! info->keep_memory)
1960 if (! _bfd_coff_free_symbols (abfd))
1966 /* Look through the loader symbols to see if this dynamic object
1967 should be included in the link. The native linker uses the loader
1968 symbols, not the normal symbol table, so we do too. */
1971 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1972 struct bfd_link_info *info,
1973 bfd_boolean *pneeded)
1977 struct internal_ldhdr ldhdr;
1978 const char *strings;
1979 bfd_byte *elsym, *elsymend;
1983 lsec = bfd_get_section_by_name (abfd, ".loader");
1985 /* There are no symbols, so don't try to include it. */
1988 if (! xcoff_get_section_contents (abfd, lsec))
1990 contents = coff_section_data (abfd, lsec)->contents;
1992 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
1994 strings = (char *) contents + ldhdr.l_stoff;
1996 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
1998 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
1999 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2001 struct internal_ldsym ldsym;
2002 char nambuf[SYMNMLEN + 1];
2004 struct bfd_link_hash_entry *h;
2006 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2008 /* We are only interested in exported symbols. */
2009 if ((ldsym.l_smtype & L_EXPORT) == 0)
2012 if (ldsym._l._l_l._l_zeroes == 0)
2013 name = strings + ldsym._l._l_l._l_offset;
2016 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2017 nambuf[SYMNMLEN] = '\0';
2021 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2023 /* We are only interested in symbols that are currently
2024 undefined. At this point we know that we are using an XCOFF
2027 && h->type == bfd_link_hash_undefined
2028 && (((struct xcoff_link_hash_entry *) h)->flags
2029 & XCOFF_DEF_DYNAMIC) == 0)
2031 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2038 /* We do not need this shared object. */
2039 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2041 free (coff_section_data (abfd, lsec)->contents);
2042 coff_section_data (abfd, lsec)->contents = NULL;
2048 /* Look through the symbols to see if this object file should be
2049 included in the link. */
2052 xcoff_link_check_ar_symbols (bfd *abfd,
2053 struct bfd_link_info *info,
2054 bfd_boolean *pneeded)
2056 bfd_size_type symesz;
2062 if ((abfd->flags & DYNAMIC) != 0
2063 && ! info->static_link
2064 && info->output_bfd->xvec == abfd->xvec)
2065 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2067 symesz = bfd_coff_symesz (abfd);
2068 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2069 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2070 while (esym < esym_end)
2072 struct internal_syment sym;
2074 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2076 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
2079 char buf[SYMNMLEN + 1];
2080 struct bfd_link_hash_entry *h;
2082 /* This symbol is externally visible, and is defined by this
2084 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2088 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2090 /* We are only interested in symbols that are currently
2091 undefined. If a symbol is currently known to be common,
2092 XCOFF linkers do not bring in an object file which
2093 defines it. We also don't bring in symbols to satisfy
2094 undefined references in shared objects. */
2096 && h->type == bfd_link_hash_undefined
2097 && (info->output_bfd->xvec != abfd->xvec
2098 || (((struct xcoff_link_hash_entry *) h)->flags
2099 & XCOFF_DEF_DYNAMIC) == 0))
2101 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2108 esym += (sym.n_numaux + 1) * symesz;
2111 /* We do not need this object file. */
2115 /* Check a single archive element to see if we need to include it in
2116 the link. *PNEEDED is set according to whether this element is
2117 needed in the link or not. This is called via
2118 _bfd_generic_link_add_archive_symbols. */
2121 xcoff_link_check_archive_element (bfd *abfd,
2122 struct bfd_link_info *info,
2123 bfd_boolean *pneeded)
2125 if (! _bfd_coff_get_external_symbols (abfd))
2128 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2133 if (! xcoff_link_add_symbols (abfd, info))
2137 if (! info->keep_memory || ! *pneeded)
2139 if (! _bfd_coff_free_symbols (abfd))
2146 /* Given an XCOFF BFD, add symbols to the global hash table as
2150 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2152 switch (bfd_get_format (abfd))
2155 return xcoff_link_add_object_symbols (abfd, info);
2158 /* If the archive has a map, do the usual search. We then need
2159 to check the archive for dynamic objects, because they may not
2160 appear in the archive map even though they should, perhaps, be
2161 included. If the archive has no map, we just consider each object
2162 file in turn, since that apparently is what the AIX native linker
2164 if (bfd_has_map (abfd))
2166 if (! (_bfd_generic_link_add_archive_symbols
2167 (abfd, info, xcoff_link_check_archive_element)))
2174 member = bfd_openr_next_archived_file (abfd, NULL);
2175 while (member != NULL)
2177 if (bfd_check_format (member, bfd_object)
2178 && (info->output_bfd->xvec == member->xvec)
2179 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2183 if (! xcoff_link_check_archive_element (member, info,
2187 member->archive_pass = -1;
2189 member = bfd_openr_next_archived_file (abfd, member);
2196 bfd_set_error (bfd_error_wrong_format);
2201 /* If symbol H has not been interpreted as a function descriptor,
2202 see whether it should be. Set up its descriptor information if so. */
2205 xcoff_find_function (struct bfd_link_info *info,
2206 struct xcoff_link_hash_entry *h)
2208 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2209 && h->root.root.string[0] != '.')
2212 struct xcoff_link_hash_entry *hfn;
2215 amt = strlen (h->root.root.string) + 2;
2216 fnname = bfd_malloc (amt);
2220 strcpy (fnname + 1, h->root.root.string);
2221 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2222 fnname, FALSE, FALSE, TRUE);
2225 && hfn->smclas == XMC_PR
2226 && (hfn->root.type == bfd_link_hash_defined
2227 || hfn->root.type == bfd_link_hash_defweak))
2229 h->flags |= XCOFF_DESCRIPTOR;
2230 h->descriptor = hfn;
2231 hfn->descriptor = h;
2237 /* H is an imported symbol. Set the import module's path, file and member
2238 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
2239 no specific import module is specified. */
2242 xcoff_set_import_path (struct bfd_link_info *info,
2243 struct xcoff_link_hash_entry *h,
2244 const char *imppath, const char *impfile,
2245 const char *impmember)
2248 struct xcoff_import_file **pp;
2250 /* We overload the ldindx field to hold the l_ifile value for this
2252 BFD_ASSERT (h->ldsym == NULL);
2253 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2254 if (imppath == NULL)
2258 /* We start c at 1 because the first entry in the import list is
2259 reserved for the library search path. */
2260 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2262 pp = &(*pp)->next, ++c)
2264 if (strcmp ((*pp)->path, imppath) == 0
2265 && strcmp ((*pp)->file, impfile) == 0
2266 && strcmp ((*pp)->member, impmember) == 0)
2272 struct xcoff_import_file *n;
2273 bfd_size_type amt = sizeof (* n);
2275 n = bfd_alloc (info->output_bfd, amt);
2281 n->member = impmember;
2289 /* Mark a symbol as not being garbage, including the section in which
2292 static inline bfd_boolean
2293 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2295 if ((h->flags & XCOFF_MARK) != 0)
2298 h->flags |= XCOFF_MARK;
2300 /* If we're marking an undefined symbol, try find some way of
2302 if (!info->relocatable
2303 && (h->flags & XCOFF_IMPORT) == 0
2304 && (h->flags & XCOFF_DEF_REGULAR) == 0
2305 && (h->root.type == bfd_link_hash_undefined
2306 || h->root.type == bfd_link_hash_undefweak))
2308 /* First check whether this symbol can be interpreted as an
2309 undefined function descriptor for a defined function symbol. */
2310 if (!xcoff_find_function (info, h))
2313 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2314 && (h->descriptor->root.type == bfd_link_hash_defined
2315 || h->descriptor->root.type == bfd_link_hash_defweak))
2317 /* This is a descriptor for a defined symbol, but the input
2318 objects have not defined the descriptor itself. Fill in
2319 the definition automatically.
2321 Note that we do this even if we found a dynamic definition
2322 of H. The local function definition logically overrides
2326 sec = xcoff_hash_table (info)->descriptor_section;
2327 h->root.type = bfd_link_hash_defined;
2328 h->root.u.def.section = sec;
2329 h->root.u.def.value = sec->size;
2331 h->flags |= XCOFF_DEF_REGULAR;
2333 /* The size of the function descriptor depends on whether this
2334 is xcoff32 (12) or xcoff64 (24). */
2335 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2337 /* A function descriptor uses two relocs: one for the
2338 associated code, and one for the TOC address. */
2339 xcoff_hash_table (info)->ldrel_count += 2;
2340 sec->reloc_count += 2;
2342 /* Mark the function itself. */
2343 if (!xcoff_mark_symbol (info, h->descriptor))
2346 /* Mark the TOC section, so that we get an anchor
2347 to relocate against. */
2348 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2351 /* We handle writing out the contents of the descriptor in
2352 xcoff_write_global_symbol. */
2354 else if ((h->flags & XCOFF_CALLED) != 0)
2356 /* This is a function symbol for which we need to create
2359 struct xcoff_link_hash_entry *hds;
2361 /* Mark the descriptor (and its TOC section). */
2362 hds = h->descriptor;
2363 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2364 || hds->root.type == bfd_link_hash_undefweak)
2365 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2366 if (!xcoff_mark_symbol (info, hds))
2369 /* Treat this symbol as undefined if the descriptor was. */
2370 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2371 h->flags |= XCOFF_WAS_UNDEFINED;
2373 /* Allocate room for the global linkage code itself. */
2374 sec = xcoff_hash_table (info)->linkage_section;
2375 h->root.type = bfd_link_hash_defined;
2376 h->root.u.def.section = sec;
2377 h->root.u.def.value = sec->size;
2379 h->flags |= XCOFF_DEF_REGULAR;
2380 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2382 /* The global linkage code requires a TOC entry for the
2384 if (hds->toc_section == NULL)
2389 xcoff32 uses 4 bytes in the toc.
2390 xcoff64 uses 8 bytes in the toc. */
2391 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2393 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2398 /* Allocate room in the fallback TOC section. */
2399 hds->toc_section = xcoff_hash_table (info)->toc_section;
2400 hds->u.toc_offset = hds->toc_section->size;
2401 hds->toc_section->size += byte_size;
2402 if (!xcoff_mark (info, hds->toc_section))
2405 /* Allocate room for a static and dynamic R_TOC
2407 ++xcoff_hash_table (info)->ldrel_count;
2408 ++hds->toc_section->reloc_count;
2410 /* Set the index to -2 to force this symbol to
2413 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2416 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2418 /* Record that the symbol was undefined, then import it.
2419 -brtl links use a special fake import file. */
2420 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2421 if (xcoff_hash_table (info)->rtld)
2423 if (!xcoff_set_import_path (info, h, "", "..", ""))
2428 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2434 if (h->root.type == bfd_link_hash_defined
2435 || h->root.type == bfd_link_hash_defweak)
2439 hsec = h->root.u.def.section;
2440 if (! bfd_is_abs_section (hsec)
2441 && (hsec->flags & SEC_MARK) == 0)
2443 if (! xcoff_mark (info, hsec))
2448 if (h->toc_section != NULL
2449 && (h->toc_section->flags & SEC_MARK) == 0)
2451 if (! xcoff_mark (info, h->toc_section))
2458 /* The mark phase of garbage collection. For a given section, mark
2459 it, and all the sections which define symbols to which it refers.
2460 Because this function needs to look at the relocs, we also count
2461 the number of relocs which need to be copied into the .loader
2465 xcoff_mark (struct bfd_link_info *info, asection *sec)
2467 if (bfd_is_abs_section (sec)
2468 || (sec->flags & SEC_MARK) != 0)
2471 sec->flags |= SEC_MARK;
2473 if (sec->owner->xvec == info->output_bfd->xvec
2474 && coff_section_data (sec->owner, sec) != NULL
2475 && xcoff_section_data (sec->owner, sec) != NULL)
2477 struct xcoff_link_hash_entry **syms;
2478 struct internal_reloc *rel, *relend;
2480 unsigned long i, first, last;
2482 /* Mark all the symbols in this section. */
2483 syms = obj_xcoff_sym_hashes (sec->owner);
2484 csects = xcoff_data (sec->owner)->csects;
2485 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2486 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2487 for (i = first; i <= last; i++)
2488 if (csects[i] == sec
2490 && (syms[i]->flags & XCOFF_MARK) == 0)
2492 if (!xcoff_mark_symbol (info, syms[i]))
2496 /* Look through the section relocs. */
2497 if ((sec->flags & SEC_RELOC) != 0
2498 && sec->reloc_count > 0)
2500 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2504 relend = rel + sec->reloc_count;
2505 for (; rel < relend; rel++)
2507 struct xcoff_link_hash_entry *h;
2509 if ((unsigned int) rel->r_symndx
2510 > obj_raw_syment_count (sec->owner))
2513 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2516 if ((h->flags & XCOFF_MARK) == 0)
2518 if (!xcoff_mark_symbol (info, h))
2526 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2528 && (rsec->flags & SEC_MARK) == 0)
2530 if (!xcoff_mark (info, rsec))
2535 /* See if this reloc needs to be copied into the .loader
2537 switch (rel->r_type)
2541 || h->root.type == bfd_link_hash_defined
2542 || h->root.type == bfd_link_hash_defweak
2543 || h->root.type == bfd_link_hash_common
2544 /* We will always provide a local definition of
2545 function symbols. */
2546 || (h->flags & XCOFF_CALLED) != 0)
2554 && (h->root.type == bfd_link_hash_defined
2555 || h->root.type == bfd_link_hash_defweak)
2556 && bfd_is_abs_section (h->root.u.def.section))
2558 ++xcoff_hash_table (info)->ldrel_count;
2560 h->flags |= XCOFF_LDREL;
2567 /* We should never need a .loader reloc for a TOC
2573 if (! info->keep_memory
2574 && coff_section_data (sec->owner, sec) != NULL
2575 && coff_section_data (sec->owner, sec)->relocs != NULL
2576 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2578 free (coff_section_data (sec->owner, sec)->relocs);
2579 coff_section_data (sec->owner, sec)->relocs = NULL;
2587 /* Routines that are called after all the input files have been
2588 handled, but before the sections are laid out in memory. */
2590 /* The sweep phase of garbage collection. Remove all garbage
2594 xcoff_sweep (struct bfd_link_info *info)
2598 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2602 for (o = sub->sections; o != NULL; o = o->next)
2604 if ((o->flags & SEC_MARK) == 0)
2606 /* Keep all sections from non-XCOFF input files. Keep
2607 special sections. Keep .debug sections for the
2609 if (sub->xvec != info->output_bfd->xvec
2610 || o == xcoff_hash_table (info)->debug_section
2611 || o == xcoff_hash_table (info)->loader_section
2612 || o == xcoff_hash_table (info)->linkage_section
2613 || o == xcoff_hash_table (info)->descriptor_section
2614 || strcmp (o->name, ".debug") == 0)
2615 o->flags |= SEC_MARK;
2626 /* Record the number of elements in a set. This is used to output the
2627 correct csect length. */
2630 bfd_xcoff_link_record_set (bfd *output_bfd,
2631 struct bfd_link_info *info,
2632 struct bfd_link_hash_entry *harg,
2635 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2636 struct xcoff_link_size_list *n;
2639 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2642 /* This will hardly ever be called. I don't want to burn four bytes
2643 per global symbol, so instead the size is kept on a linked list
2644 attached to the hash table. */
2646 n = bfd_alloc (output_bfd, amt);
2649 n->next = xcoff_hash_table (info)->size_list;
2652 xcoff_hash_table (info)->size_list = n;
2654 h->flags |= XCOFF_HAS_SIZE;
2659 /* Import a symbol. */
2662 bfd_xcoff_import_symbol (bfd *output_bfd,
2663 struct bfd_link_info *info,
2664 struct bfd_link_hash_entry *harg,
2666 const char *imppath,
2667 const char *impfile,
2668 const char *impmember,
2669 unsigned int syscall_flag)
2671 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2673 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2676 /* A symbol name which starts with a period is the code for a
2677 function. If the symbol is undefined, then add an undefined
2678 symbol for the function descriptor, and import that instead. */
2679 if (h->root.root.string[0] == '.'
2680 && h->root.type == bfd_link_hash_undefined
2681 && val == (bfd_vma) -1)
2683 struct xcoff_link_hash_entry *hds;
2685 hds = h->descriptor;
2688 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2689 h->root.root.string + 1,
2693 if (hds->root.type == bfd_link_hash_new)
2695 hds->root.type = bfd_link_hash_undefined;
2696 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2698 hds->flags |= XCOFF_DESCRIPTOR;
2699 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2700 hds->descriptor = h;
2701 h->descriptor = hds;
2704 /* Now, if the descriptor is undefined, import the descriptor
2705 rather than the symbol we were told to import. FIXME: Is
2706 this correct in all cases? */
2707 if (hds->root.type == bfd_link_hash_undefined)
2711 h->flags |= (XCOFF_IMPORT | syscall_flag);
2713 if (val != (bfd_vma) -1)
2715 if (h->root.type == bfd_link_hash_defined
2716 && (! bfd_is_abs_section (h->root.u.def.section)
2717 || h->root.u.def.value != val))
2719 if (! ((*info->callbacks->multiple_definition)
2720 (info, h->root.root.string, h->root.u.def.section->owner,
2721 h->root.u.def.section, h->root.u.def.value,
2722 output_bfd, bfd_abs_section_ptr, val)))
2726 h->root.type = bfd_link_hash_defined;
2727 h->root.u.def.section = bfd_abs_section_ptr;
2728 h->root.u.def.value = val;
2732 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
2738 /* Export a symbol. */
2741 bfd_xcoff_export_symbol (bfd *output_bfd,
2742 struct bfd_link_info *info,
2743 struct bfd_link_hash_entry *harg)
2745 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2747 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2750 h->flags |= XCOFF_EXPORT;
2752 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2753 I'm just going to ignore it until somebody explains it. */
2755 /* Make sure we don't garbage collect this symbol. */
2756 if (! xcoff_mark_symbol (info, h))
2759 /* If this is a function descriptor, make sure we don't garbage
2760 collect the associated function code. We normally don't have to
2761 worry about this, because the descriptor will be attached to a
2762 section with relocs, but if we are creating the descriptor
2763 ourselves those relocs will not be visible to the mark code. */
2764 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2766 if (! xcoff_mark_symbol (info, h->descriptor))
2773 /* Count a reloc against a symbol. This is called for relocs
2774 generated by the linker script, typically for global constructors
2778 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2779 struct bfd_link_info *info,
2782 struct xcoff_link_hash_entry *h;
2784 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2787 h = ((struct xcoff_link_hash_entry *)
2788 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2792 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2793 bfd_set_error (bfd_error_no_symbols);
2797 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2798 ++xcoff_hash_table (info)->ldrel_count;
2800 /* Mark the symbol to avoid garbage collection. */
2801 if (! xcoff_mark_symbol (info, h))
2807 /* This function is called for each symbol to which the linker script
2811 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2812 struct bfd_link_info *info,
2815 struct xcoff_link_hash_entry *h;
2817 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2820 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2825 h->flags |= XCOFF_DEF_REGULAR;
2830 /* Add a symbol to the .loader symbols, if necessary. */
2832 /* INPUT_BFD has an external symbol associated with hash table entry H
2833 and csect CSECT. Return true if INPUT_BFD defines H. */
2836 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
2839 switch (h->root.type)
2841 case bfd_link_hash_defined:
2842 case bfd_link_hash_defweak:
2843 /* No input bfd owns absolute symbols. They are written by
2844 xcoff_write_global_symbol instead. */
2845 return (!bfd_is_abs_section (csect)
2846 && h->root.u.def.section == csect);
2848 case bfd_link_hash_common:
2849 return h->root.u.c.p->section->owner == input_bfd;
2851 case bfd_link_hash_undefined:
2852 case bfd_link_hash_undefweak:
2853 /* We can't treat undef.abfd as the owner because that bfd
2854 might be a dynamic object. Allow any bfd to claim it. */
2863 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
2865 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2868 if (h->root.type == bfd_link_hash_warning)
2869 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
2871 /* __rtinit, this symbol has special handling. */
2872 if (h->flags & XCOFF_RTINIT)
2875 /* If this is a final link, and the symbol was defined as a common
2876 symbol in a regular object file, and there was no definition in
2877 any dynamic object, then the linker will have allocated space for
2878 the symbol in a common section but the XCOFF_DEF_REGULAR flag
2879 will not have been set. */
2880 if (h->root.type == bfd_link_hash_defined
2881 && (h->flags & XCOFF_DEF_REGULAR) == 0
2882 && (h->flags & XCOFF_REF_REGULAR) != 0
2883 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2884 && (bfd_is_abs_section (h->root.u.def.section)
2885 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
2886 h->flags |= XCOFF_DEF_REGULAR;
2888 /* If all defined symbols should be exported, mark them now. We
2889 don't want to export the actual functions, just the function
2891 if (ldinfo->export_defineds
2892 && (h->flags & XCOFF_DEF_REGULAR) != 0
2893 && h->root.root.string[0] != '.')
2897 /* We don't export a symbol which is being defined by an object
2898 included from an archive which contains a shared object. The
2899 rationale is that if an archive contains both an unshared and
2900 a shared object, then there must be some reason that the
2901 unshared object is unshared, and we don't want to start
2902 providing a shared version of it. In particular, this solves
2903 a bug involving the _savefNN set of functions. gcc will call
2904 those functions without providing a slot to restore the TOC,
2905 so it is essential that these functions be linked in directly
2906 and not from a shared object, which means that a shared
2907 object which also happens to link them in must not export
2908 them. This is confusing, but I haven't been able to think of
2909 a different approach. Note that the symbols can, of course,
2910 be exported explicitly. */
2912 if ((h->root.type == bfd_link_hash_defined
2913 || h->root.type == bfd_link_hash_defweak)
2914 && h->root.u.def.section->owner != NULL
2915 && h->root.u.def.section->owner->my_archive != NULL)
2917 bfd *arbfd, *member;
2919 arbfd = h->root.u.def.section->owner->my_archive;
2920 member = bfd_openr_next_archived_file (arbfd, NULL);
2921 while (member != NULL)
2923 if ((member->flags & DYNAMIC) != 0)
2928 member = bfd_openr_next_archived_file (arbfd, member);
2933 h->flags |= XCOFF_EXPORT;
2936 /* We don't want to garbage collect symbols which are not defined in
2937 XCOFF files. This is a convenient place to mark them. */
2938 if (xcoff_hash_table (ldinfo->info)->gc
2939 && (h->flags & XCOFF_MARK) == 0
2940 && (h->root.type == bfd_link_hash_defined
2941 || h->root.type == bfd_link_hash_defweak)
2942 && (h->root.u.def.section->owner == NULL
2943 || (h->root.u.def.section->owner->xvec
2944 != ldinfo->info->output_bfd->xvec)))
2945 h->flags |= XCOFF_MARK;
2947 /* If this symbol is exported, but not defined, we need to try to
2949 if ((h->flags & XCOFF_EXPORT) != 0
2950 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
2952 (*_bfd_error_handler)
2953 (_("warning: attempt to export undefined symbol `%s'"),
2954 h->root.root.string);
2959 /* If this is still a common symbol, and it wasn't garbage
2960 collected, we need to actually allocate space for it in the .bss
2962 if (h->root.type == bfd_link_hash_common
2963 && (! xcoff_hash_table (ldinfo->info)->gc
2964 || (h->flags & XCOFF_MARK) != 0)
2965 && h->root.u.c.p->section->size == 0)
2967 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2968 h->root.u.c.p->section->size = h->root.u.c.size;
2971 /* We need to add a symbol to the .loader section if it is mentioned
2972 in a reloc which we are copying to the .loader section and it was
2973 not defined or common, or if it is the entry point, or if it is
2976 if (((h->flags & XCOFF_LDREL) == 0
2977 || h->root.type == bfd_link_hash_defined
2978 || h->root.type == bfd_link_hash_defweak
2979 || h->root.type == bfd_link_hash_common)
2980 && (h->flags & XCOFF_ENTRY) == 0
2981 && (h->flags & XCOFF_EXPORT) == 0)
2987 /* We don't need to add this symbol if we did garbage collection and
2988 we did not mark this symbol. */
2989 if (xcoff_hash_table (ldinfo->info)->gc
2990 && (h->flags & XCOFF_MARK) == 0)
2996 /* We may have already processed this symbol due to the recursive
2998 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3001 /* We need to add this symbol to the .loader symbols. */
3003 BFD_ASSERT (h->ldsym == NULL);
3004 amt = sizeof (struct internal_ldsym);
3005 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3006 if (h->ldsym == NULL)
3008 ldinfo->failed = TRUE;
3012 if ((h->flags & XCOFF_IMPORT) != 0)
3013 h->ldsym->l_ifile = h->ldindx;
3015 /* The first 3 symbol table indices are reserved to indicate the
3016 data, text and bss sections. */
3017 h->ldindx = ldinfo->ldsym_count + 3;
3019 ++ldinfo->ldsym_count;
3021 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3022 h->ldsym, h->root.root.string))
3025 h->flags |= XCOFF_BUILT_LDSYM;
3030 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3031 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3032 csect information, if any. NAME is the function's name if the name
3033 is stored in the .debug section, otherwise it is null.
3035 Return 1 if we should include an appropriately-adjusted ISYM
3036 in the output file, 0 if we should discard ISYM, or -1 if an
3040 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3041 struct internal_syment *isym,
3042 union internal_auxent *aux,
3043 struct xcoff_link_hash_entry *h,
3044 asection *csect, const char *name)
3048 /* If we are skipping this csect, we want to strip the symbol too. */
3052 /* Likewise if we garbage-collected the csect. */
3053 if (xcoff_hash_table (info)->gc
3054 && !bfd_is_abs_section (csect)
3055 && !bfd_is_und_section (csect)
3056 && (csect->flags & SEC_MARK) == 0)
3059 /* An XCOFF linker always removes C_STAT symbols. */
3060 if (isym->n_sclass == C_STAT)
3063 /* We generate the TOC anchor separately. */
3064 if (isym->n_sclass == C_HIDEXT
3065 && aux->x_csect.x_smclas == XMC_TC0)
3068 /* If we are stripping all symbols, we want to discard this one. */
3069 if (info->strip == strip_all)
3072 /* Discard symbols that are defined elsewhere. */
3073 if (isym->n_sclass == C_EXT)
3075 if ((h->flags & XCOFF_ALLOCATED) != 0)
3077 if (!xcoff_final_definition_p (input_bfd, h, csect))
3081 /* If we're discarding local symbols, check whether ISYM is local. */
3082 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3083 if (info->discard == discard_all
3084 && isym->n_sclass != C_EXT
3085 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3088 /* If we're stripping debugging symbols, check whether ISYM is one. */
3089 if (info->strip == strip_debugger
3090 && isym->n_scnum == N_DEBUG)
3093 /* If we are stripping symbols based on name, check how ISYM's
3094 name should be handled. */
3095 if (info->strip == strip_some
3096 || info->discard == discard_l)
3098 char buf[SYMNMLEN + 1];
3102 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3107 if (info->strip == strip_some
3108 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3111 if (info->discard == discard_l
3112 && isym->n_sclass != C_EXT
3113 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3114 && bfd_is_local_label_name (input_bfd, name))
3121 /* Build the .loader section. This is called by the XCOFF linker
3122 emulation before_allocation routine. We must set the size of the
3123 .loader section before the linker lays out the output file.
3124 LIBPATH is the library path to search for shared objects; this is
3125 normally built from the -L arguments passed to the linker. ENTRY
3126 is the name of the entry point symbol (the -e linker option).
3127 FILE_ALIGN is the alignment to use for sections within the file
3128 (the -H linker option). MAXSTACK is the maximum stack size (the
3129 -bmaxstack linker option). MAXDATA is the maximum data size (the
3130 -bmaxdata linker option). GC is whether to do garbage collection
3131 (the -bgc linker option). MODTYPE is the module type (the
3132 -bmodtype linker option). TEXTRO is whether the text section must
3133 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3134 whether all defined symbols should be exported (the -unix linker
3135 option). SPECIAL_SECTIONS is set by this routine to csects with
3136 magic names like _end. */
3139 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3140 struct bfd_link_info *info,
3141 const char *libpath,
3143 unsigned long file_align,
3144 unsigned long maxstack,
3145 unsigned long maxdata,
3149 bfd_boolean export_defineds,
3150 asection **special_sections,
3153 struct xcoff_link_hash_entry *hentry;
3155 struct xcoff_loader_info ldinfo;
3157 size_t impsize, impcount;
3158 struct xcoff_import_file *fl;
3159 struct internal_ldhdr *ldhdr;
3160 bfd_size_type stoff;
3164 struct bfd_strtab_hash *debug_strtab;
3165 bfd_byte *debug_contents = NULL;
3168 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3170 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3171 special_sections[i] = NULL;
3175 ldinfo.failed = FALSE;
3176 ldinfo.output_bfd = output_bfd;
3178 ldinfo.export_defineds = export_defineds;
3179 ldinfo.ldsym_count = 0;
3180 ldinfo.string_size = 0;
3181 ldinfo.strings = NULL;
3182 ldinfo.string_alc = 0;
3184 xcoff_data (output_bfd)->maxstack = maxstack;
3185 xcoff_data (output_bfd)->maxdata = maxdata;
3186 xcoff_data (output_bfd)->modtype = modtype;
3188 xcoff_hash_table (info)->file_align = file_align;
3189 xcoff_hash_table (info)->textro = textro;
3190 xcoff_hash_table (info)->rtld = rtld;
3195 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3196 FALSE, FALSE, TRUE);
3198 hentry->flags |= XCOFF_ENTRY;
3202 if (info->init_function || info->fini_function || rtld)
3204 struct xcoff_link_hash_entry *hsym;
3205 struct internal_ldsym *ldsym;
3207 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3208 "__rtinit", FALSE, FALSE, TRUE);
3211 (*_bfd_error_handler)
3212 (_("error: undefined symbol __rtinit"));
3216 xcoff_mark_symbol (info, hsym);
3217 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3219 /* __rtinit initialized. */
3220 amt = sizeof (* ldsym);
3221 ldsym = bfd_malloc (amt);
3223 ldsym->l_value = 0; /* Will be filled in later. */
3224 ldsym->l_scnum = 2; /* Data section. */
3225 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3226 ldsym->l_smclas = 5; /* .rw. */
3227 ldsym->l_ifile = 0; /* Special system loader symbol. */
3228 ldsym->l_parm = 0; /* NA. */
3230 /* Force __rtinit to be the first symbol in the loader symbol table
3231 See xcoff_build_ldsyms
3233 The first 3 symbol table indices are reserved to indicate the data,
3234 text and bss sections. */
3235 BFD_ASSERT (0 == ldinfo.ldsym_count);
3238 ldinfo.ldsym_count = 1;
3239 hsym->ldsym = ldsym;
3241 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3242 hsym->ldsym, hsym->root.root.string))
3245 /* This symbol is written out by xcoff_write_global_symbol
3246 Set stuff up so xcoff_write_global_symbol logic works. */
3247 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3248 hsym->root.type = bfd_link_hash_defined;
3249 hsym->root.u.def.value = 0;
3252 /* Garbage collect unused sections. */
3253 if (info->relocatable
3256 || (hentry->root.type != bfd_link_hash_defined
3257 && hentry->root.type != bfd_link_hash_defweak))
3260 xcoff_hash_table (info)->gc = FALSE;
3262 /* We still need to call xcoff_mark, in order to set ldrel_count
3264 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3268 for (o = sub->sections; o != NULL; o = o->next)
3270 /* We shouldn't unconditionaly mark the TOC section.
3271 The output file should only have a TOC if either
3272 (a) one of the input files did or (b) we end up
3273 creating TOC references as part of the link process. */
3274 if (o != xcoff_hash_table (info)->toc_section
3275 && (o->flags & SEC_MARK) == 0)
3277 if (! xcoff_mark (info, o))
3285 if (! xcoff_mark (info, hentry->root.u.def.section))
3288 xcoff_hash_table (info)->gc = TRUE;
3291 /* Return special sections to the caller. */
3292 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3294 sec = xcoff_hash_table (info)->special_sections[i];
3298 && (sec->flags & SEC_MARK) == 0)
3301 special_sections[i] = sec;
3304 if (info->input_bfds == NULL)
3305 /* I'm not sure what to do in this bizarre case. */
3308 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3313 /* Work out the size of the import file names. Each import file ID
3314 consists of three null terminated strings: the path, the file
3315 name, and the archive member name. The first entry in the list
3316 of names is the path to use to find objects, which the linker has
3317 passed in as the libpath argument. For some reason, the path
3318 entry in the other import file names appears to always be empty. */
3319 impsize = strlen (libpath) + 3;
3321 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3324 impsize += (strlen (fl->path)
3326 + strlen (fl->member)
3330 /* Set up the .loader section header. */
3331 ldhdr = &xcoff_hash_table (info)->ldhdr;
3332 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3333 ldhdr->l_nsyms = ldinfo.ldsym_count;
3334 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3335 ldhdr->l_istlen = impsize;
3336 ldhdr->l_nimpid = impcount;
3337 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3338 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3339 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3340 ldhdr->l_stlen = ldinfo.string_size;
3341 stoff = ldhdr->l_impoff + impsize;
3342 if (ldinfo.string_size == 0)
3345 ldhdr->l_stoff = stoff;
3347 /* 64 bit elements to ldhdr
3348 The swap out routine for 32 bit will ignore them.
3349 Nothing fancy, symbols come after the header and relocs come
3351 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3352 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3353 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3355 /* We now know the final size of the .loader section. Allocate
3357 lsec = xcoff_hash_table (info)->loader_section;
3358 lsec->size = stoff + ldhdr->l_stlen;
3359 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3360 if (lsec->contents == NULL)
3363 /* Set up the header. */
3364 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3366 /* Set up the import file names. */
3367 out = (char *) lsec->contents + ldhdr->l_impoff;
3368 strcpy (out, libpath);
3369 out += strlen (libpath) + 1;
3372 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3377 while ((*out++ = *s++) != '\0')
3380 while ((*out++ = *s++) != '\0')
3383 while ((*out++ = *s++) != '\0')
3387 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3389 /* Set up the symbol string table. */
3390 if (ldinfo.string_size > 0)
3392 memcpy (out, ldinfo.strings, ldinfo.string_size);
3393 free (ldinfo.strings);
3394 ldinfo.strings = NULL;
3397 /* We can't set up the symbol table or the relocs yet, because we
3398 don't yet know the final position of the various sections. The
3399 .loader symbols are written out when the corresponding normal
3400 symbols are written out in xcoff_link_input_bfd or
3401 xcoff_write_global_symbol. The .loader relocs are written out
3402 when the corresponding normal relocs are handled in
3403 xcoff_link_input_bfd. */
3405 /* Allocate space for the magic sections. */
3406 sec = xcoff_hash_table (info)->linkage_section;
3409 sec->contents = bfd_zalloc (output_bfd, sec->size);
3410 if (sec->contents == NULL)
3413 sec = xcoff_hash_table (info)->toc_section;
3416 sec->contents = bfd_zalloc (output_bfd, sec->size);
3417 if (sec->contents == NULL)
3420 sec = xcoff_hash_table (info)->descriptor_section;
3423 sec->contents = bfd_zalloc (output_bfd, sec->size);
3424 if (sec->contents == NULL)
3428 /* Now that we've done garbage collection, decide which symbols to keep,
3429 and figure out the contents of the .debug section. */
3430 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3432 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3435 bfd_size_type symcount;
3438 unsigned int *lineno_counts;
3439 struct xcoff_link_hash_entry **sym_hash;
3440 bfd_byte *esym, *esymend;
3441 bfd_size_type symesz;
3443 if (sub->xvec != info->output_bfd->xvec)
3446 if ((sub->flags & DYNAMIC) != 0
3447 && !info->static_link)
3450 if (! _bfd_coff_get_external_symbols (sub))
3453 symcount = obj_raw_syment_count (sub);
3454 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3455 if (debug_index == NULL)
3457 xcoff_data (sub)->debug_indices = debug_index;
3459 if (info->strip == strip_all
3460 || info->strip == strip_debugger
3461 || info->discard == discard_all)
3462 /* We're stripping all debugging information, so there's no need
3463 to read SUB's .debug section. */
3467 /* Grab the contents of SUB's .debug section, if any. */
3468 subdeb = bfd_get_section_by_name (sub, ".debug");
3469 if (subdeb != NULL && subdeb->size > 0)
3471 /* We use malloc and copy the names into the debug
3472 stringtab, rather than bfd_alloc, because I expect
3473 that, when linking many files together, many of the
3474 strings will be the same. Storing the strings in the
3475 hash table should save space in this case. */
3476 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3481 csectpp = xcoff_data (sub)->csects;
3482 lineno_counts = xcoff_data (sub)->lineno_counts;
3483 sym_hash = obj_xcoff_sym_hashes (sub);
3484 symesz = bfd_coff_symesz (sub);
3485 esym = (bfd_byte *) obj_coff_external_syms (sub);
3486 esymend = esym + symcount * symesz;
3488 while (esym < esymend)
3490 struct internal_syment sym;
3491 union internal_auxent aux;
3496 bfd_coff_swap_sym_in (sub, esym, &sym);
3498 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3500 if (sym.n_sclass == C_EXT || sym.n_sclass == C_HIDEXT)
3502 BFD_ASSERT (sym.n_numaux > 0);
3503 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3504 sym.n_type, sym.n_sclass,
3505 sym.n_numaux - 1, sym.n_numaux, &aux);
3508 /* If this symbol's name is stored in the debug section,
3509 get a pointer to it. */
3510 if (debug_contents != NULL
3511 && sym._n._n_n._n_zeroes == 0
3512 && bfd_coff_symname_in_debug (sub, &sym))
3513 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3517 /* Decide whether to copy this symbol to the output file. */
3519 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3520 *sym_hash, csect, name);
3525 /* Use a debug_index of -2 to record that a symbol should
3530 /* See whether we should store the symbol name in the
3531 output .debug section. */
3536 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3537 if (indx == (bfd_size_type) -1)
3539 *debug_index = indx;
3544 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3545 if (*lineno_counts > 0)
3546 csect->output_section->lineno_count += *lineno_counts;
3549 esym += (sym.n_numaux + 1) * symesz;
3550 csectpp += sym.n_numaux + 1;
3551 sym_hash += sym.n_numaux + 1;
3552 lineno_counts += sym.n_numaux + 1;
3553 debug_index += sym.n_numaux + 1;
3558 free (debug_contents);
3559 debug_contents = NULL;
3561 /* Clear the size of subdeb, so that it is not included directly
3562 in the output file. */
3566 if (! info->keep_memory)
3568 if (! _bfd_coff_free_symbols (sub))
3573 if (info->strip != strip_all)
3574 xcoff_hash_table (info)->debug_section->size =
3575 _bfd_stringtab_size (debug_strtab);
3580 if (ldinfo.strings != NULL)
3581 free (ldinfo.strings);
3582 if (debug_contents != NULL)
3583 free (debug_contents);
3588 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3593 struct bfd_in_memory *bim;
3595 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3602 abfd->link_next = 0;
3603 abfd->format = bfd_object;
3604 abfd->iostream = (void *) bim;
3605 abfd->flags = BFD_IN_MEMORY;
3606 abfd->direction = write_direction;
3609 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3612 /* need to reset to unknown or it will not be read back in correctly */
3613 abfd->format = bfd_unknown;
3614 abfd->direction = read_direction;
3620 /* Link an input file into the linker output file. This function
3621 handles all the sections and relocations of the input file at once. */
3624 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3628 const char *strings;
3629 bfd_size_type syment_base;
3630 unsigned int n_tmask;
3631 unsigned int n_btshft;
3632 bfd_boolean copy, hash;
3633 bfd_size_type isymesz;
3634 bfd_size_type osymesz;
3635 bfd_size_type linesz;
3638 struct xcoff_link_hash_entry **sym_hash;
3639 struct internal_syment *isymp;
3641 unsigned int *lineno_counts;
3644 unsigned long output_index;
3648 bfd_boolean keep_syms;
3651 /* We can just skip DYNAMIC files, unless this is a static link. */
3652 if ((input_bfd->flags & DYNAMIC) != 0
3653 && ! finfo->info->static_link)
3656 /* Move all the symbols to the output file. */
3657 output_bfd = finfo->output_bfd;
3659 syment_base = obj_raw_syment_count (output_bfd);
3660 isymesz = bfd_coff_symesz (input_bfd);
3661 osymesz = bfd_coff_symesz (output_bfd);
3662 linesz = bfd_coff_linesz (input_bfd);
3663 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3665 n_tmask = coff_data (input_bfd)->local_n_tmask;
3666 n_btshft = coff_data (input_bfd)->local_n_btshft;
3668 /* Define macros so that ISFCN, et. al., macros work correctly. */
3669 #define N_TMASK n_tmask
3670 #define N_BTSHFT n_btshft
3673 if (! finfo->info->keep_memory)
3676 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3679 if (! _bfd_coff_get_external_symbols (input_bfd))
3682 /* Make one pass over the symbols and assign indices to symbols that
3683 we have decided to keep. Also use create .loader symbol information
3684 and update information in hash table entries. */
3685 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3686 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3687 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3688 csectpp = xcoff_data (input_bfd)->csects;
3689 debug_index = xcoff_data (input_bfd)->debug_indices;
3690 isymp = finfo->internal_syms;
3691 indexp = finfo->sym_indices;
3692 output_index = syment_base;
3693 while (esym < esym_end)
3695 union internal_auxent aux;
3699 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3701 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3703 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3705 BFD_ASSERT (isymp->n_numaux > 0);
3706 bfd_coff_swap_aux_in (input_bfd,
3707 (void *) (esym + isymesz * isymp->n_numaux),
3708 isymp->n_type, isymp->n_sclass,
3709 isymp->n_numaux - 1, isymp->n_numaux,
3712 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3715 /* If this symbol is in the .loader section, swap out the
3716 .loader symbol information. If this is an external symbol
3717 reference to a defined symbol, though, then wait until we get
3718 to the definition. */
3719 if (isymp->n_sclass == C_EXT
3720 && *sym_hash != NULL
3721 && (*sym_hash)->ldsym != NULL
3722 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
3724 struct xcoff_link_hash_entry *h;
3725 struct internal_ldsym *ldsym;
3729 if (isymp->n_scnum > 0)
3731 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3732 ldsym->l_value = (isymp->n_value
3733 + (*csectpp)->output_section->vma
3734 + (*csectpp)->output_offset
3739 ldsym->l_scnum = isymp->n_scnum;
3740 ldsym->l_value = isymp->n_value;
3743 ldsym->l_smtype = smtyp;
3744 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3745 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3746 || (h->flags & XCOFF_IMPORT) != 0)
3747 ldsym->l_smtype |= L_IMPORT;
3748 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3749 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3750 || (h->flags & XCOFF_EXPORT) != 0)
3751 ldsym->l_smtype |= L_EXPORT;
3752 if ((h->flags & XCOFF_ENTRY) != 0)
3753 ldsym->l_smtype |= L_ENTRY;
3755 ldsym->l_smclas = aux.x_csect.x_smclas;
3757 if (ldsym->l_ifile == (bfd_size_type) -1)
3759 else if (ldsym->l_ifile == 0)
3761 if ((ldsym->l_smtype & L_IMPORT) == 0)
3767 if (h->root.type == bfd_link_hash_defined
3768 || h->root.type == bfd_link_hash_defweak)
3769 impbfd = h->root.u.def.section->owner;
3770 else if (h->root.type == bfd_link_hash_undefined
3771 || h->root.type == bfd_link_hash_undefweak)
3772 impbfd = h->root.u.undef.abfd;
3780 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3781 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3788 BFD_ASSERT (h->ldindx >= 0);
3789 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3792 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3795 /* Fill in snentry now that we know the target_index. */
3796 if ((h->flags & XCOFF_ENTRY) != 0
3797 && (h->root.type == bfd_link_hash_defined
3798 || h->root.type == bfd_link_hash_defweak))
3800 xcoff_data (output_bfd)->snentry =
3801 h->root.u.def.section->output_section->target_index;
3805 add = 1 + isymp->n_numaux;
3807 if (*debug_index == -2)
3808 /* We've decided to strip this symbol. */
3812 /* Assign the next unused index to this symbol. */
3813 *indexp = output_index;
3815 if (isymp->n_sclass == C_EXT)
3817 BFD_ASSERT (*sym_hash != NULL);
3818 (*sym_hash)->indx = output_index;
3821 /* If this is a symbol in the TOC which we may have merged
3822 (class XMC_TC), remember the symbol index of the TOC
3824 if (isymp->n_sclass == C_HIDEXT
3825 && aux.x_csect.x_smclas == XMC_TC
3826 && *sym_hash != NULL)
3828 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3829 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3830 (*sym_hash)->u.toc_indx = output_index;
3833 output_index += add;
3836 esym += add * isymesz;
3842 for (--add; add > 0; --add)
3846 /* Now write out the symbols that we decided to keep. */
3848 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3849 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3850 isymp = finfo->internal_syms;
3851 indexp = finfo->sym_indices;
3852 csectpp = xcoff_data (input_bfd)->csects;
3853 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
3854 debug_index = xcoff_data (input_bfd)->debug_indices;
3855 outsym = finfo->outsyms;
3858 while (esym < esym_end)
3862 add = 1 + isymp->n_numaux;
3865 esym += add * isymesz;
3868 struct internal_syment isym;
3871 /* Adjust the symbol in order to output it. */
3873 if (isym._n._n_n._n_zeroes == 0
3874 && isym._n._n_n._n_offset != 0)
3876 /* This symbol has a long name. Enter it in the string
3877 table we are building. If *debug_index != -1, the
3878 name has already been entered in the .debug section. */
3879 if (*debug_index >= 0)
3880 isym._n._n_n._n_offset = *debug_index;
3886 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
3890 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3891 if (indx == (bfd_size_type) -1)
3893 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3897 /* The value of a C_FILE symbol is the symbol index of the
3898 next C_FILE symbol. The value of the last C_FILE symbol
3899 is -1. We try to get this right, below, just before we
3900 write the symbols out, but in the general case we may
3901 have to write the symbol out twice. */
3902 if (isym.n_sclass == C_FILE)
3904 if (finfo->last_file_index != -1
3905 && finfo->last_file.n_value != (bfd_vma) *indexp)
3907 /* We must correct the value of the last C_FILE entry. */
3908 finfo->last_file.n_value = *indexp;
3909 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3911 /* The last C_FILE symbol is in this input file. */
3912 bfd_coff_swap_sym_out (output_bfd,
3913 (void *) &finfo->last_file,
3914 (void *) (finfo->outsyms
3915 + ((finfo->last_file_index
3921 /* We have already written out the last C_FILE
3922 symbol. We need to write it out again. We
3923 borrow *outsym temporarily. */
3926 bfd_coff_swap_sym_out (output_bfd,
3927 (void *) &finfo->last_file,
3930 pos = obj_sym_filepos (output_bfd);
3931 pos += finfo->last_file_index * osymesz;
3932 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3933 || (bfd_bwrite (outsym, osymesz, output_bfd)
3939 finfo->last_file_index = *indexp;
3940 finfo->last_file = isym;
3943 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3944 into the line numbers. We update the symbol values when
3945 we handle the line numbers. */
3946 if (isym.n_sclass == C_BINCL
3947 || isym.n_sclass == C_EINCL)
3949 isym.n_value = finfo->line_filepos;
3952 /* The value of a C_BSTAT symbol is the symbol table
3953 index of the containing csect. */
3954 else if (isym.n_sclass == C_BSTAT)
3958 indx = isym.n_value;
3959 if (indx < obj_raw_syment_count (input_bfd))
3963 symindx = finfo->sym_indices[indx];
3967 isym.n_value = symindx;
3970 else if (isym.n_sclass != C_ESTAT
3971 && isym.n_sclass != C_DECL
3972 && isym.n_scnum > 0)
3974 isym.n_scnum = (*csectpp)->output_section->target_index;
3975 isym.n_value += ((*csectpp)->output_section->vma
3976 + (*csectpp)->output_offset
3980 /* Output the symbol. */
3981 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
3986 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3988 union internal_auxent aux;
3990 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
3991 isymp->n_sclass, i, isymp->n_numaux,
3994 if (isymp->n_sclass == C_FILE)
3996 /* This is the file name (or some comment put in by
3997 the compiler). If it is long, we must put it in
3998 the string table. */
3999 if (aux.x_file.x_n.x_zeroes == 0
4000 && aux.x_file.x_n.x_offset != 0)
4002 const char *filename;
4005 BFD_ASSERT (aux.x_file.x_n.x_offset
4006 >= STRING_SIZE_SIZE);
4007 if (strings == NULL)
4009 strings = _bfd_coff_read_string_table (input_bfd);
4010 if (strings == NULL)
4013 filename = strings + aux.x_file.x_n.x_offset;
4014 indx = _bfd_stringtab_add (finfo->strtab, filename,
4016 if (indx == (bfd_size_type) -1)
4018 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4021 else if ((isymp->n_sclass == C_EXT
4022 || isymp->n_sclass == C_HIDEXT)
4023 && i + 1 == isymp->n_numaux)
4026 /* We don't support type checking. I don't know if
4028 aux.x_csect.x_parmhash = 0;
4029 /* I don't think anybody uses these fields, but we'd
4030 better clobber them just in case. */
4031 aux.x_csect.x_stab = 0;
4032 aux.x_csect.x_snstab = 0;
4034 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4038 indx = aux.x_csect.x_scnlen.l;
4039 if (indx < obj_raw_syment_count (input_bfd))
4043 symindx = finfo->sym_indices[indx];
4046 aux.x_csect.x_scnlen.l = 0;
4050 aux.x_csect.x_scnlen.l = symindx;
4055 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4059 if (ISFCN (isymp->n_type)
4060 || ISTAG (isymp->n_sclass)
4061 || isymp->n_sclass == C_BLOCK
4062 || isymp->n_sclass == C_FCN)
4064 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4066 && indx < obj_raw_syment_count (input_bfd))
4068 /* We look forward through the symbol for
4069 the index of the next symbol we are going
4070 to include. I don't know if this is
4072 while (finfo->sym_indices[indx] < 0
4073 && indx < obj_raw_syment_count (input_bfd))
4075 if (indx >= obj_raw_syment_count (input_bfd))
4076 indx = output_index;
4078 indx = finfo->sym_indices[indx];
4079 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4084 indx = aux.x_sym.x_tagndx.l;
4085 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4089 symindx = finfo->sym_indices[indx];
4091 aux.x_sym.x_tagndx.l = 0;
4093 aux.x_sym.x_tagndx.l = symindx;
4098 /* Copy over the line numbers, unless we are stripping
4099 them. We do this on a symbol by symbol basis in
4100 order to more easily handle garbage collection. */
4101 if ((isymp->n_sclass == C_EXT
4102 || isymp->n_sclass == C_HIDEXT)
4104 && isymp->n_numaux > 1
4105 && ISFCN (isymp->n_type)
4106 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4108 if (*lineno_counts == 0)
4109 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4112 asection *enclosing;
4113 unsigned int enc_count;
4114 bfd_signed_vma linoff;
4115 struct internal_lineno lin;
4122 /* Read in the enclosing section's line-number
4123 information, if we haven't already. */
4125 enclosing = xcoff_section_data (abfd, o)->enclosing;
4126 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4127 if (oline != enclosing)
4129 pos = enclosing->line_filepos;
4130 amt = linesz * enc_count;
4131 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4132 || (bfd_bread (finfo->linenos, amt, input_bfd)
4138 /* Copy across the first entry, adjusting its
4140 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4141 - enclosing->line_filepos);
4142 linp = finfo->linenos + linoff;
4143 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4144 lin.l_addr.l_symndx = *indexp;
4145 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4148 /* Copy the other entries, adjusting their addresses. */
4149 linpend = linp + *lineno_counts * linesz;
4150 offset = (o->output_section->vma
4153 for (; linp < linpend; linp += linesz)
4155 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4156 lin.l_addr.l_paddr += offset;
4157 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4160 /* Write out the entries we've just processed. */
4161 pos = (o->output_section->line_filepos
4162 + o->output_section->lineno_count * linesz);
4163 amt = linesz * *lineno_counts;
4164 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4165 || bfd_bwrite (finfo->linenos + linoff,
4166 amt, output_bfd) != amt)
4168 o->output_section->lineno_count += *lineno_counts;
4170 /* Record the offset of the symbol's line numbers
4171 in the output file. */
4172 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4176 struct internal_syment *iisp, *iispend;
4179 bfd_vma range_start, range_end;
4182 /* Update any C_BINCL or C_EINCL symbols
4183 that refer to a line number in the
4184 range we just output. */
4185 iisp = finfo->internal_syms;
4186 iispend = iisp + obj_raw_syment_count (input_bfd);
4187 iindp = finfo->sym_indices;
4188 oos = finfo->outsyms;
4189 range_start = enclosing->line_filepos + linoff;
4190 range_end = range_start + *lineno_counts * linesz;
4191 while (iisp < iispend)
4194 && (iisp->n_sclass == C_BINCL
4195 || iisp->n_sclass == C_EINCL)
4196 && iisp->n_value >= range_start
4197 && iisp->n_value < range_end)
4199 struct internal_syment iis;
4201 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4202 iis.n_value = (iisp->n_value
4205 bfd_coff_swap_sym_out (output_bfd,
4210 iiadd = 1 + iisp->n_numaux;
4212 oos += iiadd * osymesz;
4220 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4221 isymp->n_sclass, i, isymp->n_numaux,
4231 lineno_counts += add;
4235 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4236 symbol will be the first symbol in the next input file. In the
4237 normal case, this will save us from writing out the C_FILE symbol
4239 if (finfo->last_file_index != -1
4240 && (bfd_size_type) finfo->last_file_index >= syment_base)
4242 finfo->last_file.n_value = output_index;
4243 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4244 (void *) (finfo->outsyms
4245 + ((finfo->last_file_index - syment_base)
4249 /* Write the modified symbols to the output file. */
4250 if (outsym > finfo->outsyms)
4252 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4253 bfd_size_type amt = outsym - finfo->outsyms;
4254 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4255 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4258 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4259 + (outsym - finfo->outsyms) / osymesz)
4262 obj_raw_syment_count (output_bfd) = output_index;
4265 /* Don't let the linker relocation routines discard the symbols. */
4266 keep_syms = obj_coff_keep_syms (input_bfd);
4267 obj_coff_keep_syms (input_bfd) = TRUE;
4269 /* Relocate the contents of each section. */
4270 for (o = input_bfd->sections; o != NULL; o = o->next)
4274 if (! o->linker_mark)
4275 /* This section was omitted from the link. */
4278 if ((o->flags & SEC_HAS_CONTENTS) == 0
4280 || (o->flags & SEC_IN_MEMORY) != 0)
4283 /* We have set filepos correctly for the sections we created to
4284 represent csects, so bfd_get_section_contents should work. */
4285 if (coff_section_data (input_bfd, o) != NULL
4286 && coff_section_data (input_bfd, o)->contents != NULL)
4287 contents = coff_section_data (input_bfd, o)->contents;
4290 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4291 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4293 contents = finfo->contents;
4296 if ((o->flags & SEC_RELOC) != 0)
4299 struct internal_reloc *internal_relocs;
4300 struct internal_reloc *irel;
4302 struct internal_reloc *irelend;
4303 struct xcoff_link_hash_entry **rel_hash;
4306 /* Read in the relocs. */
4307 target_index = o->output_section->target_index;
4308 internal_relocs = (xcoff_read_internal_relocs
4309 (input_bfd, o, FALSE, finfo->external_relocs,
4311 (finfo->section_info[target_index].relocs
4312 + o->output_section->reloc_count)));
4313 if (internal_relocs == NULL)
4316 /* Call processor specific code to relocate the section
4318 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4322 finfo->internal_syms,
4323 xcoff_data (input_bfd)->csects))
4326 offset = o->output_section->vma + o->output_offset - o->vma;
4327 irel = internal_relocs;
4328 irelend = irel + o->reloc_count;
4329 rel_hash = (finfo->section_info[target_index].rel_hashes
4330 + o->output_section->reloc_count);
4331 for (; irel < irelend; irel++, rel_hash++)
4333 struct xcoff_link_hash_entry *h = NULL;
4334 struct internal_ldrel ldrel;
4338 /* Adjust the reloc address and symbol index. */
4340 irel->r_vaddr += offset;
4342 r_symndx = irel->r_symndx;
4347 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4349 if (r_symndx != -1 && finfo->info->strip != strip_all)
4352 && h->smclas != XMC_TD
4353 && (irel->r_type == R_TOC
4354 || irel->r_type == R_GL
4355 || irel->r_type == R_TCL
4356 || irel->r_type == R_TRL
4357 || irel->r_type == R_TRLA))
4359 /* This is a TOC relative reloc with a symbol
4360 attached. The symbol should be the one which
4361 this reloc is for. We want to make this
4362 reloc against the TOC address of the symbol,
4363 not the symbol itself. */
4364 BFD_ASSERT (h->toc_section != NULL);
4365 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4366 if (h->u.toc_indx != -1)
4367 irel->r_symndx = h->u.toc_indx;
4370 struct xcoff_toc_rel_hash *n;
4371 struct xcoff_link_section_info *si;
4375 n = bfd_alloc (finfo->output_bfd, amt);
4378 si = finfo->section_info + target_index;
4379 n->next = si->toc_rel_hashes;
4382 si->toc_rel_hashes = n;
4387 /* This is a global symbol. */
4389 irel->r_symndx = h->indx;
4392 /* This symbol is being written at the end
4393 of the file, and we do not yet know the
4394 symbol index. We save the pointer to the
4395 hash table entry in the rel_hash list.
4396 We set the indx field to -2 to indicate
4397 that this symbol must not be stripped. */
4406 indx = finfo->sym_indices[r_symndx];
4410 struct internal_syment *is;
4412 /* Relocations against a TC0 TOC anchor are
4413 automatically transformed to be against
4414 the TOC anchor in the output file. */
4415 is = finfo->internal_syms + r_symndx;
4416 if (is->n_sclass == C_HIDEXT
4417 && is->n_numaux > 0)
4420 union internal_auxent aux;
4424 obj_coff_external_syms (input_bfd))
4425 + ((r_symndx + is->n_numaux)
4427 bfd_coff_swap_aux_in (input_bfd, auxptr,
4428 is->n_type, is->n_sclass,
4432 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4433 && aux.x_csect.x_smclas == XMC_TC0)
4434 indx = finfo->toc_symindx;
4439 irel->r_symndx = indx;
4443 struct internal_syment *is;
4446 char buf[SYMNMLEN + 1];
4448 /* This reloc is against a symbol we are
4449 stripping. It would be possible to handle
4450 this case, but I don't think it's worth it. */
4451 is = finfo->internal_syms + r_symndx;
4453 name = (_bfd_coff_internal_syment_name
4454 (input_bfd, is, buf));
4459 if (! ((*finfo->info->callbacks->unattached_reloc)
4460 (finfo->info, name, input_bfd, o,
4467 switch (irel->r_type)
4471 || h->root.type == bfd_link_hash_defined
4472 || h->root.type == bfd_link_hash_defweak
4473 || h->root.type == bfd_link_hash_common)
4481 && (h->root.type == bfd_link_hash_defined
4482 || h->root.type == bfd_link_hash_defweak)
4483 && bfd_is_abs_section (h->root.u.def.section))
4485 /* This reloc needs to be copied into the .loader
4487 ldrel.l_vaddr = irel->r_vaddr;
4489 ldrel.l_symndx = -(bfd_size_type ) 1;
4491 || (h->root.type == bfd_link_hash_defined
4492 || h->root.type == bfd_link_hash_defweak
4493 || h->root.type == bfd_link_hash_common))
4498 sec = xcoff_data (input_bfd)->csects[r_symndx];
4499 else if (h->root.type == bfd_link_hash_common)
4500 sec = h->root.u.c.p->section;
4502 sec = h->root.u.def.section;
4503 sec = sec->output_section;
4505 if (strcmp (sec->name, ".text") == 0)
4507 else if (strcmp (sec->name, ".data") == 0)
4509 else if (strcmp (sec->name, ".bss") == 0)
4513 (*_bfd_error_handler)
4514 (_("%B: loader reloc in unrecognized section `%A'"),
4516 bfd_set_error (bfd_error_nonrepresentable_section);
4524 (*_bfd_error_handler)
4525 (_("%B: `%s' in loader reloc but not loader sym"),
4527 h->root.root.string);
4528 bfd_set_error (bfd_error_bad_value);
4531 ldrel.l_symndx = h->ldindx;
4533 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4534 ldrel.l_rsecnm = o->output_section->target_index;
4535 if (xcoff_hash_table (finfo->info)->textro
4536 && strcmp (o->output_section->name, ".text") == 0)
4538 (*_bfd_error_handler)
4539 (_("%B: loader reloc in read-only section %A"),
4540 input_bfd, o->output_section);
4541 bfd_set_error (bfd_error_invalid_operation);
4544 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4547 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4555 /* We should never need a .loader reloc for a TOC
4561 o->output_section->reloc_count += o->reloc_count;
4564 /* Write out the modified section contents. */
4565 if (! bfd_set_section_contents (output_bfd, o->output_section,
4566 contents, (file_ptr) o->output_offset,
4571 obj_coff_keep_syms (input_bfd) = keep_syms;
4573 if (! finfo->info->keep_memory)
4575 if (! _bfd_coff_free_symbols (input_bfd))
4585 /* Sort relocs by VMA. This is called via qsort. */
4588 xcoff_sort_relocs (const void * p1, const void * p2)
4590 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4591 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4593 if (r1->r_vaddr > r2->r_vaddr)
4595 else if (r1->r_vaddr < r2->r_vaddr)
4601 /* Return true if section SEC is a TOC section. */
4603 static inline bfd_boolean
4604 xcoff_toc_section_p (asection *sec)
4609 if (name[0] == '.' && name[1] == 't')
4613 if (name[3] == '0' && name[4] == 0)
4618 if (name[2] == 'd' && name[3] == 0)
4624 /* See if the link requires a TOC (it usually does!). If so, find a
4625 good place to put the TOC anchor csect, and write out the associated
4629 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *finfo)
4631 bfd_vma toc_start, toc_end, start, end, best_address;
4635 struct internal_syment irsym;
4636 union internal_auxent iraux;
4640 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
4641 index of a csect at the beginning of the TOC. */
4642 toc_start = ~(bfd_vma) 0;
4645 for (input_bfd = finfo->info->input_bfds;
4647 input_bfd = input_bfd->link_next)
4648 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4649 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4651 start = sec->output_section->vma + sec->output_offset;
4652 if (toc_start > start)
4655 section_index = sec->output_section->target_index;
4658 end = start + sec->size;
4663 /* There's no need for a TC0 symbol if we don't have a TOC. */
4664 if (toc_end < toc_start)
4666 xcoff_data (output_bfd)->toc = toc_start;
4670 if (toc_end - toc_start < 0x8000)
4671 /* Every TOC csect can be accessed from TOC_START. */
4672 best_address = toc_start;
4675 /* Find the lowest TOC csect that is still within range of TOC_END. */
4676 best_address = toc_end;
4677 for (input_bfd = finfo->info->input_bfds;
4679 input_bfd = input_bfd->link_next)
4680 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4681 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4683 start = sec->output_section->vma + sec->output_offset;
4684 if (start < best_address
4685 && start + 0x8000 >= toc_end)
4687 best_address = start;
4688 section_index = sec->output_section->target_index;
4692 /* Make sure that the start of the TOC is also within range. */
4693 if (best_address > toc_start + 0x8000)
4695 (*_bfd_error_handler)
4696 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
4698 (unsigned long) (toc_end - toc_start));
4699 bfd_set_error (bfd_error_file_too_big);
4704 /* Record the chosen TOC value. */
4705 finfo->toc_symindx = obj_raw_syment_count (output_bfd);
4706 xcoff_data (output_bfd)->toc = best_address;
4707 xcoff_data (output_bfd)->sntoc = section_index;
4709 /* Fill out the TC0 symbol. */
4710 if (!bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &irsym, "TOC"))
4712 irsym.n_value = best_address;
4713 irsym.n_scnum = section_index;
4714 irsym.n_sclass = C_HIDEXT;
4715 irsym.n_type = T_NULL;
4717 bfd_coff_swap_sym_out (output_bfd, &irsym, finfo->outsyms);
4719 /* Fill out the auxillary csect information. */
4720 memset (&iraux, 0, sizeof iraux);
4721 iraux.x_csect.x_smtyp = XTY_SD;
4722 iraux.x_csect.x_smclas = XMC_TC0;
4723 iraux.x_csect.x_scnlen.l = 0;
4724 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
4725 finfo->outsyms + bfd_coff_symesz (output_bfd));
4727 /* Write the contents to the file. */
4728 pos = obj_sym_filepos (output_bfd);
4729 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
4730 size = 2 * bfd_coff_symesz (output_bfd);
4731 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4732 || bfd_bwrite (finfo->outsyms, size, output_bfd) != size)
4734 obj_raw_syment_count (output_bfd) += 2;
4739 /* Write out a non-XCOFF global symbol. */
4742 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4744 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4747 struct internal_syment isym;
4748 union internal_auxent aux;
4753 output_bfd = finfo->output_bfd;
4754 outsym = finfo->outsyms;
4756 if (h->root.type == bfd_link_hash_warning)
4758 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4759 if (h->root.type == bfd_link_hash_new)
4763 /* If this symbol was garbage collected, just skip it. */
4764 if (xcoff_hash_table (finfo->info)->gc
4765 && (h->flags & XCOFF_MARK) == 0)
4768 /* If we need a .loader section entry, write it out. */
4769 if (h->ldsym != NULL)
4771 struct internal_ldsym *ldsym;
4776 if (h->root.type == bfd_link_hash_undefined
4777 || h->root.type == bfd_link_hash_undefweak)
4781 ldsym->l_scnum = N_UNDEF;
4782 ldsym->l_smtype = XTY_ER;
4783 impbfd = h->root.u.undef.abfd;
4786 else if (h->root.type == bfd_link_hash_defined
4787 || h->root.type == bfd_link_hash_defweak)
4791 sec = h->root.u.def.section;
4792 ldsym->l_value = (sec->output_section->vma
4793 + sec->output_offset
4794 + h->root.u.def.value);
4795 ldsym->l_scnum = sec->output_section->target_index;
4796 ldsym->l_smtype = XTY_SD;
4797 impbfd = sec->owner;
4803 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4804 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4805 || (h->flags & XCOFF_IMPORT) != 0)
4807 Import symbols are defined so the check above will make
4808 the l_smtype XTY_SD. But this is not correct, it should
4810 ldsym->l_smtype |= L_IMPORT;
4812 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4813 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4814 || (h->flags & XCOFF_EXPORT) != 0)
4815 ldsym->l_smtype |= L_EXPORT;
4817 if ((h->flags & XCOFF_ENTRY) != 0)
4818 ldsym->l_smtype |= L_ENTRY;
4820 if ((h->flags & XCOFF_RTINIT) != 0)
4821 ldsym->l_smtype = XTY_SD;
4823 ldsym->l_smclas = h->smclas;
4825 if (ldsym->l_smtype & L_IMPORT)
4827 if ((h->root.type == bfd_link_hash_defined
4828 || h->root.type == bfd_link_hash_defweak)
4829 && (h->root.u.def.value != 0))
4830 ldsym->l_smclas = XMC_XO;
4832 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4833 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4834 ldsym->l_smclas = XMC_SV3264;
4836 else if (h->flags & XCOFF_SYSCALL32)
4837 ldsym->l_smclas = XMC_SV;
4839 else if (h->flags & XCOFF_SYSCALL64)
4840 ldsym->l_smclas = XMC_SV64;
4843 if (ldsym->l_ifile == -(bfd_size_type) 1)
4847 else if (ldsym->l_ifile == 0)
4849 if ((ldsym->l_smtype & L_IMPORT) == 0)
4851 else if (impbfd == NULL)
4855 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4856 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4862 BFD_ASSERT (h->ldindx >= 0);
4864 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4867 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4871 /* If this symbol needs global linkage code, write it out. */
4872 if (h->root.type == bfd_link_hash_defined
4873 && (h->root.u.def.section
4874 == xcoff_hash_table (finfo->info)->linkage_section))
4880 p = h->root.u.def.section->contents + h->root.u.def.value;
4882 /* The first instruction in the global linkage code loads a
4883 specific TOC element. */
4884 tocoff = (h->descriptor->toc_section->output_section->vma
4885 + h->descriptor->toc_section->output_offset
4886 - xcoff_data (output_bfd)->toc);
4888 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4889 tocoff += h->descriptor->u.toc_offset;
4891 /* The first instruction in the glink code needs to be
4892 cooked to to hold the correct offset in the toc. The
4893 rest are just output raw. */
4894 bfd_put_32 (output_bfd,
4895 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
4897 /* Start with i == 1 to get past the first instruction done above
4898 The /4 is because the glink code is in bytes and we are going
4900 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
4901 bfd_put_32 (output_bfd,
4902 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
4906 /* If we created a TOC entry for this symbol, write out the required
4908 if ((h->flags & XCOFF_SET_TOC) != 0)
4913 struct internal_reloc *irel;
4914 struct internal_ldrel ldrel;
4915 struct internal_syment irsym;
4916 union internal_auxent iraux;
4918 tocsec = h->toc_section;
4919 osec = tocsec->output_section;
4920 oindx = osec->target_index;
4921 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4922 irel->r_vaddr = (osec->vma
4923 + tocsec->output_offset
4927 irel->r_symndx = h->indx;
4931 irel->r_symndx = obj_raw_syment_count (output_bfd);
4934 BFD_ASSERT (h->ldindx >= 0);
4936 /* Initialize the aux union here instead of closer to when it is
4937 written out below because the length of the csect depends on
4938 whether the output is 32 or 64 bit. */
4939 memset (&iraux, 0, sizeof iraux);
4940 iraux.x_csect.x_smtyp = XTY_SD;
4941 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
4942 iraux.x_csect.x_smclas = XMC_TC;
4944 /* 32 bit uses a 32 bit R_POS to do the relocations
4945 64 bit uses a 64 bit R_POS to do the relocations
4947 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4949 Which one is determined by the backend. */
4950 if (bfd_xcoff_is_xcoff64 (output_bfd))
4953 iraux.x_csect.x_scnlen.l = 8;
4955 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4958 iraux.x_csect.x_scnlen.l = 4;
4963 irel->r_type = R_POS;
4964 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4965 ++osec->reloc_count;
4967 ldrel.l_vaddr = irel->r_vaddr;
4968 ldrel.l_symndx = h->ldindx;
4969 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
4970 ldrel.l_rsecnm = oindx;
4971 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4972 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4974 /* We need to emit a symbol to define a csect which holds
4976 if (finfo->info->strip != strip_all)
4978 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
4979 &irsym, h->root.root.string);
4983 irsym.n_value = irel->r_vaddr;
4984 irsym.n_scnum = osec->target_index;
4985 irsym.n_sclass = C_HIDEXT;
4986 irsym.n_type = T_NULL;
4989 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
4990 outsym += bfd_coff_symesz (output_bfd);
4992 /* Note : iraux is initialized above. */
4993 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
4994 0, 1, (void *) outsym);
4995 outsym += bfd_coff_auxesz (output_bfd);
4999 /* We aren't going to write out the symbols below, so we
5000 need to write them out now. */
5001 pos = obj_sym_filepos (output_bfd);
5002 pos += (obj_raw_syment_count (output_bfd)
5003 * bfd_coff_symesz (output_bfd));
5004 amt = outsym - finfo->outsyms;
5005 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5006 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5008 obj_raw_syment_count (output_bfd) +=
5009 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5011 outsym = finfo->outsyms;
5016 /* If this symbol is a specially defined function descriptor, write
5017 it out. The first word is the address of the function code
5018 itself, the second word is the address of the TOC, and the third
5022 The addresses for the 32 bit will take 4 bytes and the addresses
5023 for 64 bit will take 8 bytes. Similar for the relocs. This type
5024 of logic was also done above to create a TOC entry in
5025 xcoff_write_global_symbol. */
5026 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5027 && h->root.type == bfd_link_hash_defined
5028 && (h->root.u.def.section
5029 == xcoff_hash_table (finfo->info)->descriptor_section))
5035 struct xcoff_link_hash_entry *hentry;
5037 struct internal_reloc *irel;
5038 struct internal_ldrel ldrel;
5040 unsigned int reloc_size, byte_size;
5042 if (bfd_xcoff_is_xcoff64 (output_bfd))
5047 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5055 sec = h->root.u.def.section;
5056 osec = sec->output_section;
5057 oindx = osec->target_index;
5058 p = sec->contents + h->root.u.def.value;
5060 hentry = h->descriptor;
5061 BFD_ASSERT (hentry != NULL
5062 && (hentry->root.type == bfd_link_hash_defined
5063 || hentry->root.type == bfd_link_hash_defweak));
5064 esec = hentry->root.u.def.section;
5066 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5067 irel->r_vaddr = (osec->vma
5068 + sec->output_offset
5069 + h->root.u.def.value);
5070 irel->r_symndx = esec->output_section->target_index;
5071 irel->r_type = R_POS;
5072 irel->r_size = reloc_size;
5073 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5074 ++osec->reloc_count;
5076 ldrel.l_vaddr = irel->r_vaddr;
5077 if (strcmp (esec->output_section->name, ".text") == 0)
5079 else if (strcmp (esec->output_section->name, ".data") == 0)
5081 else if (strcmp (esec->output_section->name, ".bss") == 0)
5085 (*_bfd_error_handler)
5086 (_("%s: loader reloc in unrecognized section `%s'"),
5087 bfd_get_filename (output_bfd),
5088 esec->output_section->name);
5089 bfd_set_error (bfd_error_nonrepresentable_section);
5092 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5093 ldrel.l_rsecnm = oindx;
5094 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5095 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5097 /* There are three items to write out,
5098 the address of the code
5099 the address of the toc anchor
5100 the environment pointer.
5101 We are ignoring the environment pointer. So set it to zero. */
5102 if (bfd_xcoff_is_xcoff64 (output_bfd))
5104 bfd_put_64 (output_bfd,
5105 (esec->output_section->vma + esec->output_offset
5106 + hentry->root.u.def.value),
5108 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5109 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5114 This logic was already called above so the error case where
5115 the backend is neither has already been checked. */
5116 bfd_put_32 (output_bfd,
5117 (esec->output_section->vma + esec->output_offset
5118 + hentry->root.u.def.value),
5120 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5121 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5124 tsec = coff_section_from_bfd_index (output_bfd,
5125 xcoff_data (output_bfd)->sntoc);
5128 irel->r_vaddr = (osec->vma
5129 + sec->output_offset
5130 + h->root.u.def.value
5132 irel->r_symndx = tsec->output_section->target_index;
5133 irel->r_type = R_POS;
5134 irel->r_size = reloc_size;
5135 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5136 ++osec->reloc_count;
5138 ldrel.l_vaddr = irel->r_vaddr;
5139 if (strcmp (tsec->output_section->name, ".text") == 0)
5141 else if (strcmp (tsec->output_section->name, ".data") == 0)
5143 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5147 (*_bfd_error_handler)
5148 (_("%s: loader reloc in unrecognized section `%s'"),
5149 bfd_get_filename (output_bfd),
5150 tsec->output_section->name);
5151 bfd_set_error (bfd_error_nonrepresentable_section);
5154 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5155 ldrel.l_rsecnm = oindx;
5156 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5157 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5160 if (h->indx >= 0 || finfo->info->strip == strip_all)
5162 BFD_ASSERT (outsym == finfo->outsyms);
5167 && (finfo->info->strip == strip_all
5168 || (finfo->info->strip == strip_some
5169 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5170 FALSE, FALSE) == NULL)))
5172 BFD_ASSERT (outsym == finfo->outsyms);
5177 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5179 BFD_ASSERT (outsym == finfo->outsyms);
5183 memset (&aux, 0, sizeof aux);
5185 h->indx = obj_raw_syment_count (output_bfd);
5187 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5188 h->root.root.string);
5192 if (h->root.type == bfd_link_hash_undefined
5193 || h->root.type == bfd_link_hash_undefweak)
5196 isym.n_scnum = N_UNDEF;
5197 isym.n_sclass = C_EXT;
5198 aux.x_csect.x_smtyp = XTY_ER;
5200 else if ((h->root.type == bfd_link_hash_defined
5201 || h->root.type == bfd_link_hash_defweak)
5202 && h->smclas == XMC_XO)
5204 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5205 isym.n_value = h->root.u.def.value;
5206 isym.n_scnum = N_UNDEF;
5207 isym.n_sclass = C_EXT;
5208 aux.x_csect.x_smtyp = XTY_ER;
5210 else if (h->root.type == bfd_link_hash_defined
5211 || h->root.type == bfd_link_hash_defweak)
5213 struct xcoff_link_size_list *l;
5215 isym.n_value = (h->root.u.def.section->output_section->vma
5216 + h->root.u.def.section->output_offset
5217 + h->root.u.def.value);
5218 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5219 isym.n_scnum = N_ABS;
5221 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5222 isym.n_sclass = C_HIDEXT;
5223 aux.x_csect.x_smtyp = XTY_SD;
5225 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5227 for (l = xcoff_hash_table (finfo->info)->size_list;
5233 aux.x_csect.x_scnlen.l = l->size;
5239 else if (h->root.type == bfd_link_hash_common)
5241 isym.n_value = (h->root.u.c.p->section->output_section->vma
5242 + h->root.u.c.p->section->output_offset);
5243 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5244 isym.n_sclass = C_EXT;
5245 aux.x_csect.x_smtyp = XTY_CM;
5246 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5251 isym.n_type = T_NULL;
5254 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5255 outsym += bfd_coff_symesz (output_bfd);
5257 aux.x_csect.x_smclas = h->smclas;
5258 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5260 outsym += bfd_coff_auxesz (output_bfd);
5262 if ((h->root.type == bfd_link_hash_defined
5263 || h->root.type == bfd_link_hash_defweak)
5264 && h->smclas != XMC_XO)
5266 /* We just output an SD symbol. Now output an LD symbol. */
5269 isym.n_sclass = C_EXT;
5270 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5271 outsym += bfd_coff_symesz (output_bfd);
5273 aux.x_csect.x_smtyp = XTY_LD;
5274 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5275 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5277 outsym += bfd_coff_auxesz (output_bfd);
5280 pos = obj_sym_filepos (output_bfd);
5281 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5282 amt = outsym - finfo->outsyms;
5283 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5284 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5286 obj_raw_syment_count (output_bfd) +=
5287 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5292 /* Handle a link order which is supposed to generate a reloc. */
5295 xcoff_reloc_link_order (bfd *output_bfd,
5296 struct xcoff_final_link_info *finfo,
5297 asection *output_section,
5298 struct bfd_link_order *link_order)
5300 reloc_howto_type *howto;
5301 struct xcoff_link_hash_entry *h;
5305 struct internal_reloc *irel;
5306 struct xcoff_link_hash_entry **rel_hash_ptr;
5307 struct internal_ldrel ldrel;
5309 if (link_order->type == bfd_section_reloc_link_order)
5310 /* We need to somehow locate a symbol in the right section. The
5311 symbol must either have a value of zero, or we must adjust
5312 the addend by the value of the symbol. FIXME: Write this
5313 when we need it. The old linker couldn't handle this anyhow. */
5316 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5319 bfd_set_error (bfd_error_bad_value);
5323 h = ((struct xcoff_link_hash_entry *)
5324 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5325 link_order->u.reloc.p->u.name,
5326 FALSE, FALSE, TRUE));
5329 if (! ((*finfo->info->callbacks->unattached_reloc)
5330 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5335 if (h->root.type == bfd_link_hash_common)
5337 hsec = h->root.u.c.p->section;
5340 else if (h->root.type == bfd_link_hash_defined
5341 || h->root.type == bfd_link_hash_defweak)
5343 hsec = h->root.u.def.section;
5344 hval = h->root.u.def.value;
5352 addend = link_order->u.reloc.p->addend;
5354 addend += (hsec->output_section->vma
5355 + hsec->output_offset
5362 bfd_reloc_status_type rstat;
5365 size = bfd_get_reloc_size (howto);
5366 buf = bfd_zmalloc (size);
5370 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5376 case bfd_reloc_outofrange:
5378 case bfd_reloc_overflow:
5379 if (! ((*finfo->info->callbacks->reloc_overflow)
5380 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5381 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5388 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5389 (file_ptr) link_order->offset, size);
5395 /* Store the reloc information in the right place. It will get
5396 swapped and written out at the end of the final_link routine. */
5397 irel = (finfo->section_info[output_section->target_index].relocs
5398 + output_section->reloc_count);
5399 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5400 + output_section->reloc_count);
5402 memset (irel, 0, sizeof (struct internal_reloc));
5403 *rel_hash_ptr = NULL;
5405 irel->r_vaddr = output_section->vma + link_order->offset;
5408 irel->r_symndx = h->indx;
5411 /* Set the index to -2 to force this symbol to get written out. */
5417 irel->r_type = howto->type;
5418 irel->r_size = howto->bitsize - 1;
5419 if (howto->complain_on_overflow == complain_overflow_signed)
5420 irel->r_size |= 0x80;
5422 ++output_section->reloc_count;
5424 /* Now output the reloc to the .loader section. */
5426 ldrel.l_vaddr = irel->r_vaddr;
5430 const char *secname;
5432 secname = hsec->output_section->name;
5434 if (strcmp (secname, ".text") == 0)
5436 else if (strcmp (secname, ".data") == 0)
5438 else if (strcmp (secname, ".bss") == 0)
5442 (*_bfd_error_handler)
5443 (_("%s: loader reloc in unrecognized section `%s'"),
5444 bfd_get_filename (output_bfd), secname);
5445 bfd_set_error (bfd_error_nonrepresentable_section);
5453 (*_bfd_error_handler)
5454 (_("%s: `%s' in loader reloc but not loader sym"),
5455 bfd_get_filename (output_bfd),
5456 h->root.root.string);
5457 bfd_set_error (bfd_error_bad_value);
5460 ldrel.l_symndx = h->ldindx;
5463 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5464 ldrel.l_rsecnm = output_section->target_index;
5465 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5466 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5471 /* Do the final link step. */
5474 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5476 bfd_size_type symesz;
5477 struct xcoff_final_link_info finfo;
5479 struct bfd_link_order *p;
5480 bfd_size_type max_contents_size;
5481 bfd_size_type max_sym_count;
5482 bfd_size_type max_lineno_count;
5483 bfd_size_type max_reloc_count;
5484 bfd_size_type max_output_reloc_count;
5485 file_ptr rel_filepos;
5487 file_ptr line_filepos;
5488 unsigned int linesz;
5490 bfd_byte *external_relocs = NULL;
5491 char strbuf[STRING_SIZE_SIZE];
5496 abfd->flags |= DYNAMIC;
5498 symesz = bfd_coff_symesz (abfd);
5501 finfo.output_bfd = abfd;
5502 finfo.strtab = NULL;
5503 finfo.section_info = NULL;
5504 finfo.last_file_index = -1;
5505 finfo.toc_symindx = -1;
5506 finfo.internal_syms = NULL;
5507 finfo.sym_indices = NULL;
5508 finfo.outsyms = NULL;
5509 finfo.linenos = NULL;
5510 finfo.contents = NULL;
5511 finfo.external_relocs = NULL;
5513 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5514 + bfd_xcoff_ldhdrsz (abfd));
5515 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5516 + bfd_xcoff_ldhdrsz(abfd)
5517 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5518 * bfd_xcoff_ldsymsz(abfd)));
5520 xcoff_data (abfd)->coff.link_info = info;
5522 finfo.strtab = _bfd_stringtab_init ();
5523 if (finfo.strtab == NULL)
5526 /* Count the relocation entries required for the output file.
5527 (We've already counted the line numbers.) Determine a few
5529 max_contents_size = 0;
5530 max_lineno_count = 0;
5531 max_reloc_count = 0;
5532 for (o = abfd->sections; o != NULL; o = o->next)
5535 for (p = o->map_head.link_order; p != NULL; p = p->next)
5537 if (p->type == bfd_indirect_link_order)
5541 sec = p->u.indirect.section;
5543 /* Mark all sections which are to be included in the
5544 link. This will normally be every section. We need
5545 to do this so that we can identify any sections which
5546 the linker has decided to not include. */
5547 sec->linker_mark = TRUE;
5549 o->reloc_count += sec->reloc_count;
5551 if (sec->rawsize > max_contents_size)
5552 max_contents_size = sec->rawsize;
5553 if (sec->size > max_contents_size)
5554 max_contents_size = sec->size;
5555 if (coff_section_data (sec->owner, sec) != NULL
5556 && xcoff_section_data (sec->owner, sec) != NULL
5557 && (xcoff_section_data (sec->owner, sec)->lineno_count
5558 > max_lineno_count))
5560 xcoff_section_data (sec->owner, sec)->lineno_count;
5561 if (sec->reloc_count > max_reloc_count)
5562 max_reloc_count = sec->reloc_count;
5564 else if (p->type == bfd_section_reloc_link_order
5565 || p->type == bfd_symbol_reloc_link_order)
5570 /* Compute the file positions for all the sections. */
5571 if (abfd->output_has_begun)
5573 if (xcoff_hash_table (info)->file_align != 0)
5580 file_align = xcoff_hash_table (info)->file_align;
5581 if (file_align != 0)
5583 bfd_boolean saw_contents;
5587 /* Insert .pad sections before every section which has
5588 contents and is loaded, if it is preceded by some other
5589 section which has contents and is loaded. */
5590 saw_contents = TRUE;
5591 for (o = abfd->sections; o != NULL; o = o->next)
5593 if (strcmp (o->name, ".pad") == 0)
5594 saw_contents = FALSE;
5595 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5596 && (o->flags & SEC_LOAD) != 0)
5599 saw_contents = TRUE;
5604 /* Create a pad section and place it before the section
5605 that needs padding. This requires unlinking and
5606 relinking the bfd's section list. */
5608 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5610 n->alignment_power = 0;
5612 bfd_section_list_remove (abfd, n);
5613 bfd_section_list_insert_before (abfd, o, n);
5614 saw_contents = FALSE;
5619 /* Reset the section indices after inserting the new
5622 for (o = abfd->sections; o != NULL; o = o->next)
5625 o->target_index = indx;
5627 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5629 /* Work out appropriate sizes for the .pad sections to force
5630 each section to land on a page boundary. This bit of
5631 code knows what compute_section_file_positions is going
5633 sofar = bfd_coff_filhsz (abfd);
5634 sofar += bfd_coff_aoutsz (abfd);
5635 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5636 for (o = abfd->sections; o != NULL; o = o->next)
5637 if ((bfd_xcoff_is_reloc_count_overflow
5638 (abfd, (bfd_vma) o->reloc_count))
5639 || (bfd_xcoff_is_lineno_count_overflow
5640 (abfd, (bfd_vma) o->lineno_count)))
5641 /* 64 does not overflow, need to check if 32 does */
5642 sofar += bfd_coff_scnhsz (abfd);
5644 for (o = abfd->sections; o != NULL; o = o->next)
5646 if (strcmp (o->name, ".pad") == 0)
5650 BFD_ASSERT (o->size == 0);
5651 pageoff = sofar & (file_align - 1);
5654 o->size = file_align - pageoff;
5655 sofar += file_align - pageoff;
5656 o->flags |= SEC_HAS_CONTENTS;
5661 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5662 sofar += BFD_ALIGN (o->size,
5663 1 << o->alignment_power);
5668 if (! bfd_coff_compute_section_file_positions (abfd))
5672 /* Allocate space for the pointers we need to keep for the relocs. */
5676 /* We use section_count + 1, rather than section_count, because
5677 the target_index fields are 1 based. */
5678 amt = abfd->section_count + 1;
5679 amt *= sizeof (struct xcoff_link_section_info);
5680 finfo.section_info = bfd_malloc (amt);
5681 if (finfo.section_info == NULL)
5683 for (i = 0; i <= abfd->section_count; i++)
5685 finfo.section_info[i].relocs = NULL;
5686 finfo.section_info[i].rel_hashes = NULL;
5687 finfo.section_info[i].toc_rel_hashes = NULL;
5691 /* Set the file positions for the relocs. */
5692 rel_filepos = obj_relocbase (abfd);
5693 relsz = bfd_coff_relsz (abfd);
5694 max_output_reloc_count = 0;
5695 for (o = abfd->sections; o != NULL; o = o->next)
5697 if (o->reloc_count == 0)
5701 /* A stripped file has no relocs. However, we still
5702 allocate the buffers, so that later code doesn't have to
5703 worry about whether we are stripping or not. */
5704 if (info->strip == strip_all)
5708 o->flags |= SEC_RELOC;
5709 o->rel_filepos = rel_filepos;
5710 rel_filepos += o->reloc_count * relsz;
5713 /* We don't know the indices of global symbols until we have
5714 written out all the local symbols. For each section in
5715 the output file, we keep an array of pointers to hash
5716 table entries. Each entry in the array corresponds to a
5717 reloc. When we find a reloc against a global symbol, we
5718 set the corresponding entry in this array so that we can
5719 fix up the symbol index after we have written out all the
5722 Because of this problem, we also keep the relocs in
5723 memory until the end of the link. This wastes memory.
5724 We could backpatch the file later, I suppose, although it
5726 amt = o->reloc_count;
5727 amt *= sizeof (struct internal_reloc);
5728 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5730 amt = o->reloc_count;
5731 amt *= sizeof (struct xcoff_link_hash_entry *);
5732 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5734 if (finfo.section_info[o->target_index].relocs == NULL
5735 || finfo.section_info[o->target_index].rel_hashes == NULL)
5738 if (o->reloc_count > max_output_reloc_count)
5739 max_output_reloc_count = o->reloc_count;
5743 /* We now know the size of the relocs, so we can determine the file
5744 positions of the line numbers. */
5745 line_filepos = rel_filepos;
5746 finfo.line_filepos = line_filepos;
5747 linesz = bfd_coff_linesz (abfd);
5748 for (o = abfd->sections; o != NULL; o = o->next)
5750 if (o->lineno_count == 0)
5751 o->line_filepos = 0;
5754 o->line_filepos = line_filepos;
5755 line_filepos += o->lineno_count * linesz;
5758 /* Reset the reloc and lineno counts, so that we can use them to
5759 count the number of entries we have output so far. */
5761 o->lineno_count = 0;
5764 obj_sym_filepos (abfd) = line_filepos;
5766 /* Figure out the largest number of symbols in an input BFD. Take
5767 the opportunity to clear the output_has_begun fields of all the
5768 input BFD's. We want at least 6 symbols, since that is the
5769 number which xcoff_write_global_symbol may need. */
5771 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5775 sub->output_has_begun = FALSE;
5776 sz = obj_raw_syment_count (sub);
5777 if (sz > max_sym_count)
5781 /* Allocate some buffers used while linking. */
5782 amt = max_sym_count * sizeof (struct internal_syment);
5783 finfo.internal_syms = bfd_malloc (amt);
5785 amt = max_sym_count * sizeof (long);
5786 finfo.sym_indices = bfd_malloc (amt);
5788 amt = (max_sym_count + 1) * symesz;
5789 finfo.outsyms = bfd_malloc (amt);
5791 amt = max_lineno_count * bfd_coff_linesz (abfd);
5792 finfo.linenos = bfd_malloc (amt);
5794 amt = max_contents_size;
5795 finfo.contents = bfd_malloc (amt);
5797 amt = max_reloc_count * relsz;
5798 finfo.external_relocs = bfd_malloc (amt);
5800 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5801 || (finfo.sym_indices == NULL && max_sym_count > 0)
5802 || finfo.outsyms == NULL
5803 || (finfo.linenos == NULL && max_lineno_count > 0)
5804 || (finfo.contents == NULL && max_contents_size > 0)
5805 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5808 obj_raw_syment_count (abfd) = 0;
5810 /* Find a TOC symbol, if we need one. */
5811 if (!xcoff_find_tc0 (abfd, &finfo))
5814 /* We now know the position of everything in the file, except that
5815 we don't know the size of the symbol table and therefore we don't
5816 know where the string table starts. We just build the string
5817 table in memory as we go along. We process all the relocations
5818 for a single input file at once. */
5819 for (o = abfd->sections; o != NULL; o = o->next)
5821 for (p = o->map_head.link_order; p != NULL; p = p->next)
5823 if (p->type == bfd_indirect_link_order
5824 && p->u.indirect.section->owner->xvec == abfd->xvec)
5826 sub = p->u.indirect.section->owner;
5827 if (! sub->output_has_begun)
5829 if (! xcoff_link_input_bfd (&finfo, sub))
5831 sub->output_has_begun = TRUE;
5834 else if (p->type == bfd_section_reloc_link_order
5835 || p->type == bfd_symbol_reloc_link_order)
5837 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5842 if (! _bfd_default_link_order (abfd, info, o, p))
5848 /* Free up the buffers used by xcoff_link_input_bfd. */
5849 if (finfo.internal_syms != NULL)
5851 free (finfo.internal_syms);
5852 finfo.internal_syms = NULL;
5854 if (finfo.sym_indices != NULL)
5856 free (finfo.sym_indices);
5857 finfo.sym_indices = NULL;
5859 if (finfo.linenos != NULL)
5861 free (finfo.linenos);
5862 finfo.linenos = NULL;
5864 if (finfo.contents != NULL)
5866 free (finfo.contents);
5867 finfo.contents = NULL;
5869 if (finfo.external_relocs != NULL)
5871 free (finfo.external_relocs);
5872 finfo.external_relocs = NULL;
5875 /* The value of the last C_FILE symbol is supposed to be -1. Write
5877 if (finfo.last_file_index != -1)
5879 finfo.last_file.n_value = -(bfd_vma) 1;
5880 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
5881 (void *) finfo.outsyms);
5882 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
5883 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5884 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
5888 /* Write out all the global symbols which do not come from XCOFF
5890 xcoff_link_hash_traverse (xcoff_hash_table (info),
5891 xcoff_write_global_symbol,
5894 if (finfo.outsyms != NULL)
5896 free (finfo.outsyms);
5897 finfo.outsyms = NULL;
5900 /* Now that we have written out all the global symbols, we know the
5901 symbol indices to use for relocs against them, and we can finally
5902 write out the relocs. */
5903 amt = max_output_reloc_count * relsz;
5904 external_relocs = bfd_malloc (amt);
5905 if (external_relocs == NULL && max_output_reloc_count != 0)
5908 for (o = abfd->sections; o != NULL; o = o->next)
5910 struct internal_reloc *irel;
5911 struct internal_reloc *irelend;
5912 struct xcoff_link_hash_entry **rel_hash;
5913 struct xcoff_toc_rel_hash *toc_rel_hash;
5915 bfd_size_type rel_size;
5917 /* A stripped file has no relocs. */
5918 if (info->strip == strip_all)
5924 if (o->reloc_count == 0)
5927 irel = finfo.section_info[o->target_index].relocs;
5928 irelend = irel + o->reloc_count;
5929 rel_hash = finfo.section_info[o->target_index].rel_hashes;
5930 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5932 if (*rel_hash != NULL)
5934 if ((*rel_hash)->indx < 0)
5936 if (! ((*info->callbacks->unattached_reloc)
5937 (info, (*rel_hash)->root.root.string,
5938 NULL, o, irel->r_vaddr)))
5940 (*rel_hash)->indx = 0;
5942 irel->r_symndx = (*rel_hash)->indx;
5946 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
5947 toc_rel_hash != NULL;
5948 toc_rel_hash = toc_rel_hash->next)
5950 if (toc_rel_hash->h->u.toc_indx < 0)
5952 if (! ((*info->callbacks->unattached_reloc)
5953 (info, toc_rel_hash->h->root.root.string,
5954 NULL, o, toc_rel_hash->rel->r_vaddr)))
5956 toc_rel_hash->h->u.toc_indx = 0;
5958 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
5961 /* XCOFF requires that the relocs be sorted by address. We tend
5962 to produce them in the order in which their containing csects
5963 appear in the symbol table, which is not necessarily by
5964 address. So we sort them here. There may be a better way to
5966 qsort ((void *) finfo.section_info[o->target_index].relocs,
5967 o->reloc_count, sizeof (struct internal_reloc),
5970 irel = finfo.section_info[o->target_index].relocs;
5971 irelend = irel + o->reloc_count;
5972 erel = external_relocs;
5973 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5974 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
5976 rel_size = relsz * o->reloc_count;
5977 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
5978 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
5982 if (external_relocs != NULL)
5984 free (external_relocs);
5985 external_relocs = NULL;
5988 /* Free up the section information. */
5989 if (finfo.section_info != NULL)
5993 for (i = 0; i < abfd->section_count; i++)
5995 if (finfo.section_info[i].relocs != NULL)
5996 free (finfo.section_info[i].relocs);
5997 if (finfo.section_info[i].rel_hashes != NULL)
5998 free (finfo.section_info[i].rel_hashes);
6000 free (finfo.section_info);
6001 finfo.section_info = NULL;
6004 /* Write out the loader section contents. */
6005 BFD_ASSERT ((bfd_byte *) finfo.ldrel
6006 == (xcoff_hash_table (info)->loader_section->contents
6007 + xcoff_hash_table (info)->ldhdr.l_impoff));
6008 o = xcoff_hash_table (info)->loader_section;
6009 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
6010 (file_ptr) o->output_offset, o->size))
6013 /* Write out the magic sections. */
6014 o = xcoff_hash_table (info)->linkage_section;
6016 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6017 (file_ptr) o->output_offset,
6020 o = xcoff_hash_table (info)->toc_section;
6022 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6023 (file_ptr) o->output_offset,
6026 o = xcoff_hash_table (info)->descriptor_section;
6028 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6029 (file_ptr) o->output_offset,
6033 /* Write out the string table. */
6034 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6035 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6038 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
6040 amt = STRING_SIZE_SIZE;
6041 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6043 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
6046 _bfd_stringtab_free (finfo.strtab);
6048 /* Write out the debugging string table. */
6049 o = xcoff_hash_table (info)->debug_section;
6052 struct bfd_strtab_hash *debug_strtab;
6054 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6055 BFD_ASSERT (o->output_section->size - o->output_offset
6056 >= _bfd_stringtab_size (debug_strtab));
6057 pos = o->output_section->filepos + o->output_offset;
6058 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6060 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6064 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6065 not try to write out the symbols. */
6066 bfd_get_symcount (abfd) = 0;
6071 if (finfo.strtab != NULL)
6072 _bfd_stringtab_free (finfo.strtab);
6074 if (finfo.section_info != NULL)
6078 for (i = 0; i < abfd->section_count; i++)
6080 if (finfo.section_info[i].relocs != NULL)
6081 free (finfo.section_info[i].relocs);
6082 if (finfo.section_info[i].rel_hashes != NULL)
6083 free (finfo.section_info[i].rel_hashes);
6085 free (finfo.section_info);
6088 if (finfo.internal_syms != NULL)
6089 free (finfo.internal_syms);
6090 if (finfo.sym_indices != NULL)
6091 free (finfo.sym_indices);
6092 if (finfo.outsyms != NULL)
6093 free (finfo.outsyms);
6094 if (finfo.linenos != NULL)
6095 free (finfo.linenos);
6096 if (finfo.contents != NULL)
6097 free (finfo.contents);
6098 if (finfo.external_relocs != NULL)
6099 free (finfo.external_relocs);
6100 if (external_relocs != NULL)
6101 free (external_relocs);