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)
262 if ((ldsym.l_smtype & L_WEAK) != 0)
263 symbuf->symbol.flags |= BSF_WEAK;
265 symbuf->symbol.flags |= BSF_GLOBAL;
268 /* FIXME: We have no way to record the other information stored
269 with the loader symbol. */
270 *psyms = (asymbol *) symbuf;
275 return ldhdr.l_nsyms;
278 /* Get the size required to hold the dynamic relocs. */
281 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
285 struct internal_ldhdr ldhdr;
287 if ((abfd->flags & DYNAMIC) == 0)
289 bfd_set_error (bfd_error_invalid_operation);
293 lsec = bfd_get_section_by_name (abfd, ".loader");
296 bfd_set_error (bfd_error_no_symbols);
300 if (! xcoff_get_section_contents (abfd, lsec))
302 contents = coff_section_data (abfd, lsec)->contents;
304 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
306 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
309 /* Get the dynamic relocs. */
312 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
318 struct internal_ldhdr ldhdr;
320 bfd_byte *elrel, *elrelend;
322 if ((abfd->flags & DYNAMIC) == 0)
324 bfd_set_error (bfd_error_invalid_operation);
328 lsec = bfd_get_section_by_name (abfd, ".loader");
331 bfd_set_error (bfd_error_no_symbols);
335 if (! xcoff_get_section_contents (abfd, lsec))
337 contents = coff_section_data (abfd, lsec)->contents;
339 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
341 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
345 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
347 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
348 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
351 struct internal_ldrel ldrel;
353 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
355 if (ldrel.l_symndx >= 3)
356 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
362 switch (ldrel.l_symndx)
378 sec = bfd_get_section_by_name (abfd, name);
381 bfd_set_error (bfd_error_bad_value);
385 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
388 relbuf->address = ldrel.l_vaddr;
391 /* Most dynamic relocs have the same type. FIXME: This is only
392 correct if ldrel.l_rtype == 0. In other cases, we should use
393 a different howto. */
394 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
396 /* FIXME: We have no way to record the l_rsecnm field. */
403 return ldhdr.l_nreloc;
406 /* Routine to create an entry in an XCOFF link hash table. */
408 static struct bfd_hash_entry *
409 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
410 struct bfd_hash_table *table,
413 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
415 /* Allocate the structure if it has not already been allocated by a
418 ret = bfd_hash_allocate (table, sizeof (* ret));
422 /* Call the allocation method of the superclass. */
423 ret = ((struct xcoff_link_hash_entry *)
424 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
428 /* Set local fields. */
430 ret->toc_section = NULL;
431 ret->u.toc_indx = -1;
432 ret->descriptor = NULL;
436 ret->smclas = XMC_UA;
439 return (struct bfd_hash_entry *) ret;
442 /* Create a XCOFF link hash table. */
444 struct bfd_link_hash_table *
445 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
447 struct xcoff_link_hash_table *ret;
448 bfd_size_type amt = sizeof (* ret);
450 ret = bfd_malloc (amt);
453 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
454 sizeof (struct xcoff_link_hash_entry)))
460 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
461 ret->debug_section = NULL;
462 ret->loader_section = NULL;
463 ret->ldrel_count = 0;
464 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
465 ret->linkage_section = NULL;
466 ret->toc_section = NULL;
467 ret->descriptor_section = NULL;
472 memset (ret->special_sections, 0, sizeof ret->special_sections);
474 /* The linker will always generate a full a.out header. We need to
475 record that fact now, before the sizeof_headers routine could be
477 xcoff_data (abfd)->full_aouthdr = TRUE;
482 /* Free a XCOFF link hash table. */
485 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
487 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
489 _bfd_stringtab_free (ret->debug_strtab);
490 bfd_hash_table_free (&ret->root.table);
494 /* Read internal relocs for an XCOFF csect. This is a wrapper around
495 _bfd_coff_read_internal_relocs which tries to take advantage of any
496 relocs which may have been cached for the enclosing section. */
498 static struct internal_reloc *
499 xcoff_read_internal_relocs (bfd *abfd,
502 bfd_byte *external_relocs,
503 bfd_boolean require_internal,
504 struct internal_reloc *internal_relocs)
506 if (coff_section_data (abfd, sec) != NULL
507 && coff_section_data (abfd, sec)->relocs == NULL
508 && xcoff_section_data (abfd, sec) != NULL)
512 enclosing = xcoff_section_data (abfd, sec)->enclosing;
514 if (enclosing != NULL
515 && (coff_section_data (abfd, enclosing) == NULL
516 || coff_section_data (abfd, enclosing)->relocs == NULL)
518 && enclosing->reloc_count > 0)
520 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
521 external_relocs, FALSE, NULL)
526 if (enclosing != NULL
527 && coff_section_data (abfd, enclosing) != NULL
528 && coff_section_data (abfd, enclosing)->relocs != NULL)
532 off = ((sec->rel_filepos - enclosing->rel_filepos)
533 / bfd_coff_relsz (abfd));
535 if (! require_internal)
536 return coff_section_data (abfd, enclosing)->relocs + off;
537 memcpy (internal_relocs,
538 coff_section_data (abfd, enclosing)->relocs + off,
539 sec->reloc_count * sizeof (struct internal_reloc));
540 return internal_relocs;
544 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
545 require_internal, internal_relocs);
548 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
549 Return true if LDSYM defines H. */
552 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
553 struct internal_ldsym *ldsym)
555 /* If we didn't know about H before processing LDSYM, LDSYM
556 definitely defines H. */
557 if (h->root.type == bfd_link_hash_new)
560 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
561 dynamic symbol, LDSYM trumps the current definition of H. */
562 if ((ldsym->l_smtype & L_WEAK) == 0
563 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
564 && (h->flags & XCOFF_DEF_REGULAR) == 0
565 && (h->root.type == bfd_link_hash_defweak
566 || h->root.type == bfd_link_hash_undefweak))
569 /* If H is currently undefined, LDSYM defines it. */
570 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
571 && (h->root.type == bfd_link_hash_undefined
572 || h->root.type == bfd_link_hash_undefweak))
578 /* This function is used to add symbols from a dynamic object to the
579 global symbol table. */
582 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
586 struct internal_ldhdr ldhdr;
588 bfd_byte *elsym, *elsymend;
589 struct xcoff_import_file *n;
594 struct xcoff_import_file **pp;
596 /* We can only handle a dynamic object if we are generating an XCOFF
598 if (info->output_bfd->xvec != abfd->xvec)
600 (*_bfd_error_handler)
601 (_("%s: XCOFF shared object when not producing XCOFF output"),
602 bfd_get_filename (abfd));
603 bfd_set_error (bfd_error_invalid_operation);
607 /* The symbols we use from a dynamic object are not the symbols in
608 the normal symbol table, but, rather, the symbols in the export
609 table. If there is a global symbol in a dynamic object which is
610 not in the export table, the loader will not be able to find it,
611 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
612 libc.a has symbols in the export table which are not in the
615 /* Read in the .loader section. FIXME: We should really use the
616 o_snloader field in the a.out header, rather than grabbing the
618 lsec = bfd_get_section_by_name (abfd, ".loader");
621 (*_bfd_error_handler)
622 (_("%s: dynamic object with no .loader section"),
623 bfd_get_filename (abfd));
624 bfd_set_error (bfd_error_no_symbols);
628 if (! xcoff_get_section_contents (abfd, lsec))
630 contents = coff_section_data (abfd, lsec)->contents;
632 /* Remove the sections from this object, so that they do not get
633 included in the link. */
634 bfd_section_list_clear (abfd);
636 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
638 strings = (char *) contents + ldhdr.l_stoff;
640 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
642 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
644 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
646 struct internal_ldsym ldsym;
647 char nambuf[SYMNMLEN + 1];
649 struct xcoff_link_hash_entry *h;
651 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
653 /* We are only interested in exported symbols. */
654 if ((ldsym.l_smtype & L_EXPORT) == 0)
657 if (ldsym._l._l_l._l_zeroes == 0)
658 name = strings + ldsym._l._l_l._l_offset;
661 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
662 nambuf[SYMNMLEN] = '\0';
666 /* Normally we could not call xcoff_link_hash_lookup in an add
667 symbols routine, since we might not be using an XCOFF hash
668 table. However, we verified above that we are using an XCOFF
671 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
676 if (!xcoff_dynamic_definition_p (h, &ldsym))
679 h->flags |= XCOFF_DEF_DYNAMIC;
680 h->smclas = ldsym.l_smclas;
681 if (h->smclas == XMC_XO)
683 /* This symbol has an absolute value. */
684 if ((ldsym.l_smtype & L_WEAK) != 0)
685 h->root.type = bfd_link_hash_defweak;
687 h->root.type = bfd_link_hash_defined;
688 h->root.u.def.section = bfd_abs_section_ptr;
689 h->root.u.def.value = ldsym.l_value;
693 /* Otherwise, we don't bother to actually define the symbol,
694 since we don't have a section to put it in anyhow.
695 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
696 should be imported from the symbol's undef.abfd. */
697 if ((ldsym.l_smtype & L_WEAK) != 0)
698 h->root.type = bfd_link_hash_undefweak;
700 h->root.type = bfd_link_hash_undefined;
701 h->root.u.undef.abfd = abfd;
704 /* If this symbol defines a function descriptor, then it
705 implicitly defines the function code as well. */
706 if (h->smclas == XMC_DS
707 || (h->smclas == XMC_XO && name[0] != '.'))
708 h->flags |= XCOFF_DESCRIPTOR;
709 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
711 struct xcoff_link_hash_entry *hds;
718 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
722 strcpy (dsnm + 1, name);
723 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
733 if (xcoff_dynamic_definition_p (hds, &ldsym))
735 hds->root.type = h->root.type;
736 hds->flags |= XCOFF_DEF_DYNAMIC;
737 if (h->smclas == XMC_XO)
739 /* An absolute symbol appears to actually define code, not a
740 function descriptor. This is how some math functions are
741 implemented on AIX 4.1. */
742 hds->smclas = XMC_XO;
743 hds->root.u.def.section = bfd_abs_section_ptr;
744 hds->root.u.def.value = ldsym.l_value;
748 hds->smclas = XMC_PR;
749 hds->root.u.undef.abfd = abfd;
750 /* We do not want to add this to the undefined
757 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
759 free (coff_section_data (abfd, lsec)->contents);
760 coff_section_data (abfd, lsec)->contents = NULL;
763 /* Record this file in the import files. */
764 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
769 /* For some reason, the path entry in the import file list for a
770 shared object appears to always be empty. The file name is the
773 if (abfd->my_archive == NULL)
775 bname = bfd_get_filename (abfd);
780 bname = bfd_get_filename (abfd->my_archive);
781 mname = bfd_get_filename (abfd);
783 s = strrchr (bname, '/');
789 /* We start c at 1 because the first import file number is reserved
791 for (pp = &xcoff_hash_table (info)->imports, c = 1;
793 pp = &(*pp)->next, ++c)
797 xcoff_data (abfd)->import_file_id = c;
802 /* xcoff_link_create_extra_sections
804 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
807 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
809 bfd_boolean return_value = FALSE;
811 if (info->output_bfd->xvec == abfd->xvec)
813 /* We need to build a .loader section, so we do it here. This
814 won't work if we're producing an XCOFF output file with no
815 XCOFF input files. FIXME. */
817 if (xcoff_hash_table (info)->loader_section == NULL)
820 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
822 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
826 xcoff_hash_table (info)->loader_section = lsec;
829 /* Likewise for the linkage section. */
830 if (xcoff_hash_table (info)->linkage_section == NULL)
833 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
836 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
840 xcoff_hash_table (info)->linkage_section = lsec;
841 lsec->alignment_power = 2;
844 /* Likewise for the TOC section. */
845 if (xcoff_hash_table (info)->toc_section == NULL)
848 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
851 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
855 xcoff_hash_table (info)->toc_section = tsec;
856 tsec->alignment_power = 2;
859 /* Likewise for the descriptor section. */
860 if (xcoff_hash_table (info)->descriptor_section == NULL)
863 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
866 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
870 xcoff_hash_table (info)->descriptor_section = dsec;
871 dsec->alignment_power = 2;
874 /* Likewise for the .debug section. */
875 if (xcoff_hash_table (info)->debug_section == NULL
876 && info->strip != strip_all)
879 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
881 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
885 xcoff_hash_table (info)->debug_section = dsec;
896 /* Returns the index of reloc in RELOCS with the least address greater
897 than or equal to ADDRESS. The relocs are sorted by address. */
900 xcoff_find_reloc (struct internal_reloc *relocs,
904 bfd_size_type min, max, this;
908 if (count == 1 && relocs[0].r_vaddr < address)
917 /* Do a binary search over (min,max]. */
918 while (min + 1 < max)
922 this = (max + min) / 2;
923 raddr = relocs[this].r_vaddr;
926 else if (raddr < address)
935 if (relocs[min].r_vaddr < address)
939 && relocs[min - 1].r_vaddr == address)
945 /* Add all the symbols from an object file to the hash table.
947 XCOFF is a weird format. A normal XCOFF .o files will have three
948 COFF sections--.text, .data, and .bss--but each COFF section will
949 contain many csects. These csects are described in the symbol
950 table. From the linker's point of view, each csect must be
951 considered a section in its own right. For example, a TOC entry is
952 handled as a small XMC_TC csect. The linker must be able to merge
953 different TOC entries together, which means that it must be able to
954 extract the XMC_TC csects from the .data section of the input .o
957 From the point of view of our linker, this is, of course, a hideous
958 nightmare. We cope by actually creating sections for each csect,
959 and discarding the original sections. We then have to handle the
960 relocation entries carefully, since the only way to tell which
961 csect they belong to is to examine the address. */
964 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
966 unsigned int n_tmask;
967 unsigned int n_btshft;
968 bfd_boolean default_copy;
969 bfd_size_type symcount;
970 struct xcoff_link_hash_entry **sym_hash;
971 asection **csect_cache;
972 unsigned int *lineno_counts;
973 bfd_size_type linesz;
976 bfd_boolean keep_syms;
978 unsigned int csect_index;
979 asection *first_csect;
980 bfd_size_type symesz;
983 struct reloc_info_struct
985 struct internal_reloc *relocs;
988 } *reloc_info = NULL;
991 keep_syms = obj_coff_keep_syms (abfd);
993 if ((abfd->flags & DYNAMIC) != 0
994 && ! info->static_link)
996 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1000 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1001 if (! xcoff_link_create_extra_sections (abfd, info))
1004 if ((abfd->flags & DYNAMIC) != 0
1005 && ! info->static_link)
1008 n_tmask = coff_data (abfd)->local_n_tmask;
1009 n_btshft = coff_data (abfd)->local_n_btshft;
1011 /* Define macros so that ISFCN, et. al., macros work correctly. */
1012 #define N_TMASK n_tmask
1013 #define N_BTSHFT n_btshft
1015 if (info->keep_memory)
1016 default_copy = FALSE;
1018 default_copy = TRUE;
1020 symcount = obj_raw_syment_count (abfd);
1022 /* We keep a list of the linker hash table entries that correspond
1023 to each external symbol. */
1024 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1025 sym_hash = bfd_zalloc (abfd, amt);
1026 if (sym_hash == NULL && symcount != 0)
1028 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1030 /* Because of the weird stuff we are doing with XCOFF csects, we can
1031 not easily determine which section a symbol is in, so we store
1032 the information in the tdata for the input file. */
1033 amt = symcount * sizeof (asection *);
1034 csect_cache = bfd_zalloc (abfd, amt);
1035 if (csect_cache == NULL && symcount != 0)
1037 xcoff_data (abfd)->csects = csect_cache;
1039 /* We garbage-collect line-number information on a symbol-by-symbol
1040 basis, so we need to have quick access to the number of entries
1042 amt = symcount * sizeof (unsigned int);
1043 lineno_counts = bfd_zalloc (abfd, amt);
1044 if (lineno_counts == NULL && symcount != 0)
1046 xcoff_data (abfd)->lineno_counts = lineno_counts;
1048 /* While splitting sections into csects, we need to assign the
1049 relocs correctly. The relocs and the csects must both be in
1050 order by VMA within a given section, so we handle this by
1051 scanning along the relocs as we process the csects. We index
1052 into reloc_info using the section target_index. */
1053 amt = abfd->section_count + 1;
1054 amt *= sizeof (struct reloc_info_struct);
1055 reloc_info = bfd_zmalloc (amt);
1056 if (reloc_info == NULL)
1059 /* Read in the relocs and line numbers for each section. */
1060 linesz = bfd_coff_linesz (abfd);
1062 for (o = abfd->sections; o != NULL; o = o->next)
1066 if ((o->flags & SEC_RELOC) != 0)
1068 reloc_info[o->target_index].relocs =
1069 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1070 amt = o->reloc_count;
1071 amt *= sizeof (asection *);
1072 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1073 if (reloc_info[o->target_index].csects == NULL)
1077 if ((info->strip == strip_none || info->strip == strip_some)
1078 && o->lineno_count > 0)
1082 amt = linesz * o->lineno_count;
1083 linenos = bfd_malloc (amt);
1084 if (linenos == NULL)
1086 reloc_info[o->target_index].linenos = linenos;
1087 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1088 || bfd_bread (linenos, amt, abfd) != amt)
1093 /* Don't let the linker relocation routines discard the symbols. */
1094 obj_coff_keep_syms (abfd) = TRUE;
1100 symesz = bfd_coff_symesz (abfd);
1101 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1102 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1103 esym_end = esym + symcount * symesz;
1105 while (esym < esym_end)
1107 struct internal_syment sym;
1108 union internal_auxent aux;
1110 char buf[SYMNMLEN + 1];
1114 struct xcoff_link_hash_entry *set_toc;
1116 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1118 /* In this pass we are only interested in symbols with csect
1120 if (!CSECT_SYM_P (sym.n_sclass))
1123 Normally csect is a .pr, .rw etc. created in the loop
1124 If C_FILE or first time, handle special
1126 Advance esym, sym_hash, csect_hash ptrs. */
1127 if (sym.n_sclass == C_FILE)
1130 *csect_cache = csect;
1131 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1132 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1134 *csect_cache = NULL;
1135 esym += (sym.n_numaux + 1) * symesz;
1136 sym_hash += sym.n_numaux + 1;
1137 csect_cache += sym.n_numaux + 1;
1138 lineno_counts += sym.n_numaux + 1;
1143 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1148 /* If this symbol has line number information attached to it,
1149 and we're not stripping it, count the number of entries and
1150 add them to the count for this csect. In the final link pass
1151 we are going to attach line number information by symbol,
1152 rather than by section, in order to more easily handle
1153 garbage collection. */
1154 if ((info->strip == strip_none || info->strip == strip_some)
1157 && ISFCN (sym.n_type))
1159 union internal_auxent auxlin;
1161 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1162 sym.n_type, sym.n_sclass,
1163 0, sym.n_numaux, (void *) &auxlin);
1165 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1167 asection *enclosing;
1168 bfd_signed_vma linoff;
1170 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1171 if (enclosing == NULL)
1173 (*_bfd_error_handler)
1174 (_("%B: `%s' has line numbers but no enclosing section"),
1176 bfd_set_error (bfd_error_bad_value);
1179 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1180 - enclosing->line_filepos);
1181 /* Explicit cast to bfd_signed_vma for compiler. */
1182 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1184 struct internal_lineno lin;
1185 bfd_byte *linpstart;
1187 linpstart = (reloc_info[enclosing->target_index].linenos
1189 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1191 && ((bfd_size_type) lin.l_addr.l_symndx
1193 - (bfd_byte *) obj_coff_external_syms (abfd))
1196 bfd_byte *linpend, *linp;
1198 linpend = (reloc_info[enclosing->target_index].linenos
1199 + enclosing->lineno_count * linesz);
1200 for (linp = linpstart + linesz;
1204 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1206 if (lin.l_lnno == 0)
1209 *lineno_counts = (linp - linpstart) / linesz;
1210 /* The setting of line_filepos will only be
1211 useful if all the line number entries for a
1212 csect are contiguous; this only matters for
1214 if (csect->line_filepos == 0)
1215 csect->line_filepos =
1216 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1222 /* Pick up the csect auxiliary information. */
1223 if (sym.n_numaux == 0)
1225 (*_bfd_error_handler)
1226 (_("%B: class %d symbol `%s' has no aux entries"),
1227 abfd, sym.n_sclass, name);
1228 bfd_set_error (bfd_error_bad_value);
1232 bfd_coff_swap_aux_in (abfd,
1233 (void *) (esym + symesz * sym.n_numaux),
1234 sym.n_type, sym.n_sclass,
1235 sym.n_numaux - 1, sym.n_numaux,
1238 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1247 (*_bfd_error_handler)
1248 (_("%B: symbol `%s' has unrecognized csect type %d"),
1250 bfd_set_error (bfd_error_bad_value);
1254 /* This is an external reference. */
1255 if (sym.n_sclass == C_HIDEXT
1256 || sym.n_scnum != N_UNDEF
1257 || aux.x_csect.x_scnlen.l != 0)
1259 (*_bfd_error_handler)
1260 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1261 abfd, name, sym.n_sclass, sym.n_scnum,
1262 aux.x_csect.x_scnlen.l);
1263 bfd_set_error (bfd_error_bad_value);
1267 /* An XMC_XO external reference is actually a reference to
1268 an absolute location. */
1269 if (aux.x_csect.x_smclas != XMC_XO)
1270 section = bfd_und_section_ptr;
1273 section = bfd_abs_section_ptr;
1274 value = sym.n_value;
1280 csect_index = -(unsigned) 1;
1282 /* When we see a TOC anchor, we record the TOC value. */
1283 if (aux.x_csect.x_smclas == XMC_TC0)
1285 if (sym.n_sclass != C_HIDEXT
1286 || aux.x_csect.x_scnlen.l != 0)
1288 (*_bfd_error_handler)
1289 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1290 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1291 bfd_set_error (bfd_error_bad_value);
1294 xcoff_data (abfd)->toc = sym.n_value;
1297 /* We must merge TOC entries for the same symbol. We can
1298 merge two TOC entries if they are both C_HIDEXT, they
1299 both have the same name, they are both 4 or 8 bytes long, and
1300 they both have a relocation table entry for an external
1301 symbol with the same name. Unfortunately, this means
1302 that we must look through the relocations. Ick.
1304 Logic for 32 bit vs 64 bit.
1305 32 bit has a csect length of 4 for TOC
1306 64 bit has a csect length of 8 for TOC
1308 The conditions to get past the if-check are not that bad.
1309 They are what is used to create the TOC csects in the first
1311 if (aux.x_csect.x_smclas == XMC_TC
1312 && sym.n_sclass == C_HIDEXT
1313 && info->output_bfd->xvec == abfd->xvec
1314 && ((bfd_xcoff_is_xcoff32 (abfd)
1315 && aux.x_csect.x_scnlen.l == 4)
1316 || (bfd_xcoff_is_xcoff64 (abfd)
1317 && aux.x_csect.x_scnlen.l == 8)))
1319 asection *enclosing;
1320 struct internal_reloc *relocs;
1321 bfd_size_type relindx;
1322 struct internal_reloc *rel;
1324 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1325 if (enclosing == NULL)
1328 relocs = reloc_info[enclosing->target_index].relocs;
1329 amt = enclosing->reloc_count;
1330 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1331 rel = relocs + relindx;
1333 /* 32 bit R_POS r_size is 31
1334 64 bit R_POS r_size is 63 */
1335 if (relindx < enclosing->reloc_count
1336 && rel->r_vaddr == (bfd_vma) sym.n_value
1337 && rel->r_type == R_POS
1338 && ((bfd_xcoff_is_xcoff32 (abfd)
1339 && rel->r_size == 31)
1340 || (bfd_xcoff_is_xcoff64 (abfd)
1341 && rel->r_size == 63)))
1345 struct internal_syment relsym;
1347 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1348 + rel->r_symndx * symesz);
1349 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1350 if (EXTERN_SYM_P (relsym.n_sclass))
1352 const char *relname;
1353 char relbuf[SYMNMLEN + 1];
1355 struct xcoff_link_hash_entry *h;
1357 /* At this point we know that the TOC entry is
1358 for an externally visible symbol. */
1359 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1361 if (relname == NULL)
1364 /* We only merge TOC entries if the TC name is
1365 the same as the symbol name. This handles
1366 the normal case, but not common cases like
1367 SYM.P4 which gcc generates to store SYM + 4
1368 in the TOC. FIXME. */
1369 if (strcmp (name, relname) == 0)
1371 copy = (! info->keep_memory
1372 || relsym._n._n_n._n_zeroes != 0
1373 || relsym._n._n_n._n_offset == 0);
1374 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1375 relname, TRUE, copy,
1380 /* At this point h->root.type could be
1381 bfd_link_hash_new. That should be OK,
1382 since we know for sure that we will come
1383 across this symbol as we step through the
1386 /* We store h in *sym_hash for the
1387 convenience of the relocate_section
1391 if (h->toc_section != NULL)
1393 asection **rel_csects;
1395 /* We already have a TOC entry for this
1396 symbol, so we can just ignore this
1399 reloc_info[enclosing->target_index].csects;
1400 rel_csects[relindx] = bfd_und_section_ptr;
1404 /* We are about to create a TOC entry for
1413 asection *enclosing;
1415 /* We need to create a new section. We get the name from
1416 the csect storage mapping class, so that the linker can
1417 accumulate similar csects together. */
1419 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1423 /* The enclosing section is the main section : .data, .text
1424 or .bss that the csect is coming from. */
1425 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1426 if (enclosing == NULL)
1429 if (! bfd_is_abs_section (enclosing)
1430 && ((bfd_vma) sym.n_value < enclosing->vma
1431 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1432 > enclosing->vma + enclosing->size)))
1434 (*_bfd_error_handler)
1435 (_("%B: csect `%s' not in enclosing section"),
1437 bfd_set_error (bfd_error_bad_value);
1440 csect->vma = sym.n_value;
1441 csect->filepos = (enclosing->filepos
1444 csect->size = aux.x_csect.x_scnlen.l;
1445 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1446 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1448 /* Record the enclosing section in the tdata for this new
1450 amt = sizeof (struct coff_section_tdata);
1451 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1452 if (csect->used_by_bfd == NULL)
1454 amt = sizeof (struct xcoff_section_tdata);
1455 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1456 if (coff_section_data (abfd, csect)->tdata == NULL)
1458 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1459 xcoff_section_data (abfd, csect)->lineno_count =
1460 enclosing->lineno_count;
1462 if (enclosing->owner == abfd)
1464 struct internal_reloc *relocs;
1465 bfd_size_type relindx;
1466 struct internal_reloc *rel;
1467 asection **rel_csect;
1469 relocs = reloc_info[enclosing->target_index].relocs;
1470 amt = enclosing->reloc_count;
1471 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1473 rel = relocs + relindx;
1474 rel_csect = (reloc_info[enclosing->target_index].csects
1477 csect->rel_filepos = (enclosing->rel_filepos
1478 + relindx * bfd_coff_relsz (abfd));
1479 while (relindx < enclosing->reloc_count
1480 && *rel_csect == NULL
1481 && rel->r_vaddr < csect->vma + csect->size)
1485 csect->flags |= SEC_RELOC;
1486 ++csect->reloc_count;
1493 /* There are a number of other fields and section flags
1494 which we do not bother to set. */
1496 csect_index = ((esym
1497 - (bfd_byte *) obj_coff_external_syms (abfd))
1500 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1502 if (first_csect == NULL)
1503 first_csect = csect;
1505 /* If this symbol is external, we treat it as starting at the
1506 beginning of the newly created section. */
1507 if (EXTERN_SYM_P (sym.n_sclass))
1513 /* If this is a TOC section for a symbol, record it. */
1514 if (set_toc != NULL)
1515 set_toc->toc_section = csect;
1520 /* This is a label definition. The x_scnlen field is the
1521 symbol index of the csect. Usually the XTY_LD symbol will
1522 follow its appropriate XTY_SD symbol. The .set pseudo op can
1523 cause the XTY_LD to not follow the XTY_SD symbol. */
1528 if (aux.x_csect.x_scnlen.l < 0
1529 || (aux.x_csect.x_scnlen.l
1530 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1534 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1536 || (section->flags & SEC_HAS_CONTENTS) == 0)
1541 (*_bfd_error_handler)
1542 (_("%B: misplaced XTY_LD `%s'"),
1544 bfd_set_error (bfd_error_bad_value);
1548 value = sym.n_value - csect->vma;
1553 /* This is an unitialized csect. We could base the name on
1554 the storage mapping class, but we don't bother except for
1555 an XMC_TD symbol. If this csect is externally visible,
1556 it is a common symbol. We put XMC_TD symbols in sections
1557 named .tocbss, and rely on the linker script to put that
1560 if (aux.x_csect.x_smclas == XMC_TD)
1562 /* The linker script puts the .td section in the data
1563 section after the .tc section. */
1564 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1568 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1573 csect->vma = sym.n_value;
1574 csect->size = aux.x_csect.x_scnlen.l;
1575 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1576 /* There are a number of other fields and section flags
1577 which we do not bother to set. */
1579 csect_index = ((esym
1580 - (bfd_byte *) obj_coff_external_syms (abfd))
1583 amt = sizeof (struct coff_section_tdata);
1584 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1585 if (csect->used_by_bfd == NULL)
1587 amt = sizeof (struct xcoff_section_tdata);
1588 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1589 if (coff_section_data (abfd, csect)->tdata == NULL)
1591 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1593 if (first_csect == NULL)
1594 first_csect = csect;
1596 if (EXTERN_SYM_P (sym.n_sclass))
1598 csect->flags |= SEC_IS_COMMON;
1601 value = aux.x_csect.x_scnlen.l;
1607 /* Check for magic symbol names. */
1608 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1609 && aux.x_csect.x_smclas != XMC_TC
1610 && aux.x_csect.x_smclas != XMC_TD)
1616 if (strcmp (name, "_text") == 0)
1617 i = XCOFF_SPECIAL_SECTION_TEXT;
1618 else if (strcmp (name, "_etext") == 0)
1619 i = XCOFF_SPECIAL_SECTION_ETEXT;
1620 else if (strcmp (name, "_data") == 0)
1621 i = XCOFF_SPECIAL_SECTION_DATA;
1622 else if (strcmp (name, "_edata") == 0)
1623 i = XCOFF_SPECIAL_SECTION_EDATA;
1624 else if (strcmp (name, "_end") == 0)
1625 i = XCOFF_SPECIAL_SECTION_END;
1627 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1628 i = XCOFF_SPECIAL_SECTION_END2;
1631 xcoff_hash_table (info)->special_sections[i] = csect;
1634 /* Now we have enough information to add the symbol to the
1635 linker hash table. */
1637 if (EXTERN_SYM_P (sym.n_sclass))
1642 BFD_ASSERT (section != NULL);
1644 /* We must copy the name into memory if we got it from the
1645 syment itself, rather than the string table. */
1646 copy = default_copy;
1647 if (sym._n._n_n._n_zeroes != 0
1648 || sym._n._n_n._n_offset == 0)
1651 /* The AIX linker appears to only detect multiple symbol
1652 definitions when there is a reference to the symbol. If
1653 a symbol is defined multiple times, and the only
1654 references are from the same object file, the AIX linker
1655 appears to permit it. It does not merge the different
1656 definitions, but handles them independently. On the
1657 other hand, if there is a reference, the linker reports
1660 This matters because the AIX <net/net_globals.h> header
1661 file actually defines an initialized array, so we have to
1662 actually permit that to work.
1664 Just to make matters even more confusing, the AIX linker
1665 appears to permit multiple symbol definitions whenever
1666 the second definition is in an archive rather than an
1667 object file. This may be a consequence of the manner in
1668 which it handles archives: I think it may load the entire
1669 archive in as separate csects, and then let garbage
1670 collection discard symbols.
1672 We also have to handle the case of statically linking a
1673 shared object, which will cause symbol redefinitions,
1674 although this is an easier case to detect. */
1676 if (info->output_bfd->xvec == abfd->xvec)
1678 if (! bfd_is_und_section (section))
1679 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1680 name, TRUE, copy, FALSE);
1682 /* Make a copy of the symbol name to prevent problems with
1684 *sym_hash = ((struct xcoff_link_hash_entry *)
1685 bfd_wrapped_link_hash_lookup (abfd, info, name,
1686 TRUE, TRUE, FALSE));
1688 if (*sym_hash == NULL)
1690 if (((*sym_hash)->root.type == bfd_link_hash_defined
1691 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1692 && ! bfd_is_und_section (section)
1693 && ! bfd_is_com_section (section))
1695 /* This is a second definition of a defined symbol. */
1696 if ((abfd->flags & DYNAMIC) != 0
1697 && ((*sym_hash)->smclas != XMC_GL
1698 || aux.x_csect.x_smclas == XMC_GL
1699 || ((*sym_hash)->root.u.def.section->owner->flags
1702 /* The new symbol is from a shared library, and
1703 either the existing symbol is not global
1704 linkage code or this symbol is global linkage
1705 code. If the existing symbol is global
1706 linkage code and the new symbol is not, then
1707 we want to use the new symbol. */
1708 section = bfd_und_section_ptr;
1711 else if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1712 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1714 /* The existing symbol is from a shared library.
1716 (*sym_hash)->root.type = bfd_link_hash_undefined;
1717 (*sym_hash)->root.u.undef.abfd =
1718 (*sym_hash)->root.u.def.section->owner;
1720 else if (abfd->my_archive != NULL)
1722 /* This is a redefinition in an object contained
1723 in an archive. Just ignore it. See the
1725 section = bfd_und_section_ptr;
1728 else if (sym.n_sclass == C_AIX_WEAKEXT
1729 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1731 /* At least one of the definitions is weak.
1732 Allow the normal rules to take effect. */
1734 else if ((*sym_hash)->root.u.undef.next != NULL
1735 || info->hash->undefs_tail == &(*sym_hash)->root)
1737 /* This symbol has been referenced. In this
1738 case, we just continue and permit the
1739 multiple definition error. See the comment
1740 above about the behaviour of the AIX linker. */
1742 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1744 /* The symbols are both csects of the same
1745 class. There is at least a chance that this
1746 is a semi-legitimate redefinition. */
1747 section = bfd_und_section_ptr;
1749 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1752 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1753 && (*sym_hash)->root.type == bfd_link_hash_defined
1754 && (bfd_is_und_section (section)
1755 || bfd_is_com_section (section)))
1757 /* This is a reference to a multiply defined symbol.
1758 Report the error now. See the comment above
1759 about the behaviour of the AIX linker. We could
1760 also do this with warning symbols, but I'm not
1761 sure the XCOFF linker is wholly prepared to
1762 handle them, and that would only be a warning,
1764 if (! ((*info->callbacks->multiple_definition)
1765 (info, (*sym_hash)->root.root.string,
1766 NULL, NULL, (bfd_vma) 0,
1767 (*sym_hash)->root.u.def.section->owner,
1768 (*sym_hash)->root.u.def.section,
1769 (*sym_hash)->root.u.def.value)))
1771 /* Try not to give this error too many times. */
1772 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1776 /* _bfd_generic_link_add_one_symbol may call the linker to
1777 generate an error message, and the linker may try to read
1778 the symbol table to give a good error. Right now, the
1779 line numbers are in an inconsistent state, since they are
1780 counted both in the real sections and in the new csects.
1781 We need to leave the count in the real sections so that
1782 the linker can report the line number of the error
1783 correctly, so temporarily clobber the link to the csects
1784 so that the linker will not try to read the line numbers
1785 a second time from the csects. */
1786 BFD_ASSERT (last_real->next == first_csect);
1787 last_real->next = NULL;
1788 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
1789 if (! (_bfd_generic_link_add_one_symbol
1790 (info, abfd, name, flags, section, value,
1792 (struct bfd_link_hash_entry **) sym_hash)))
1794 last_real->next = first_csect;
1796 if (smtyp == XTY_CM)
1798 if ((*sym_hash)->root.type != bfd_link_hash_common
1799 || (*sym_hash)->root.u.c.p->section != csect)
1800 /* We don't need the common csect we just created. */
1803 (*sym_hash)->root.u.c.p->alignment_power
1804 = csect->alignment_power;
1807 if (info->output_bfd->xvec == abfd->xvec)
1811 if (smtyp == XTY_ER || smtyp == XTY_CM)
1812 flag = XCOFF_REF_REGULAR;
1814 flag = XCOFF_DEF_REGULAR;
1815 (*sym_hash)->flags |= flag;
1817 if ((*sym_hash)->smclas == XMC_UA
1818 || flag == XCOFF_DEF_REGULAR)
1819 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1823 if (smtyp == XTY_ER)
1824 *csect_cache = section;
1827 *csect_cache = csect;
1829 xcoff_section_data (abfd, csect)->last_symndx
1830 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
1833 esym += (sym.n_numaux + 1) * symesz;
1834 sym_hash += sym.n_numaux + 1;
1835 csect_cache += sym.n_numaux + 1;
1836 lineno_counts += sym.n_numaux + 1;
1839 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1841 /* Make sure that we have seen all the relocs. */
1842 for (o = abfd->sections; o != first_csect; o = o->next)
1844 /* Reset the section size and the line number count, since the
1845 data is now attached to the csects. Don't reset the size of
1846 the .debug section, since we need to read it below in
1847 bfd_xcoff_size_dynamic_sections. */
1848 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1850 o->lineno_count = 0;
1852 if ((o->flags & SEC_RELOC) != 0)
1855 struct internal_reloc *rel;
1856 asection **rel_csect;
1858 rel = reloc_info[o->target_index].relocs;
1859 rel_csect = reloc_info[o->target_index].csects;
1861 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1863 if (*rel_csect == NULL)
1865 (*_bfd_error_handler)
1866 (_("%B: reloc %s:%d not in csect"),
1868 bfd_set_error (bfd_error_bad_value);
1872 /* We identify all function symbols that are the target
1873 of a relocation, so that we can create glue code for
1874 functions imported from dynamic objects. */
1875 if (info->output_bfd->xvec == abfd->xvec
1876 && *rel_csect != bfd_und_section_ptr
1877 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1879 struct xcoff_link_hash_entry *h;
1881 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1882 /* If the symbol name starts with a period, it is
1883 the code of a function. If the symbol is
1884 currently undefined, then add an undefined symbol
1885 for the function descriptor. This should do no
1886 harm, because any regular object that defines the
1887 function should also define the function
1888 descriptor. It helps, because it means that we
1889 will identify the function descriptor with a
1890 dynamic object if a dynamic object defines it. */
1891 if (h->root.root.string[0] == '.'
1892 && h->descriptor == NULL)
1894 struct xcoff_link_hash_entry *hds;
1895 struct bfd_link_hash_entry *bh;
1897 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1898 h->root.root.string + 1,
1902 if (hds->root.type == bfd_link_hash_new)
1905 if (! (_bfd_generic_link_add_one_symbol
1906 (info, abfd, hds->root.root.string,
1907 (flagword) 0, bfd_und_section_ptr,
1908 (bfd_vma) 0, NULL, FALSE,
1911 hds = (struct xcoff_link_hash_entry *) bh;
1913 hds->flags |= XCOFF_DESCRIPTOR;
1914 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
1915 hds->descriptor = h;
1916 h->descriptor = hds;
1918 if (h->root.root.string[0] == '.')
1919 h->flags |= XCOFF_CALLED;
1923 free (reloc_info[o->target_index].csects);
1924 reloc_info[o->target_index].csects = NULL;
1926 /* Reset SEC_RELOC and the reloc_count, since the reloc
1927 information is now attached to the csects. */
1928 o->flags &=~ SEC_RELOC;
1931 /* If we are not keeping memory, free the reloc information. */
1932 if (! info->keep_memory
1933 && coff_section_data (abfd, o) != NULL
1934 && coff_section_data (abfd, o)->relocs != NULL
1935 && ! coff_section_data (abfd, o)->keep_relocs)
1937 free (coff_section_data (abfd, o)->relocs);
1938 coff_section_data (abfd, o)->relocs = NULL;
1942 /* Free up the line numbers. FIXME: We could cache these
1943 somewhere for the final link, to avoid reading them again. */
1944 if (reloc_info[o->target_index].linenos != NULL)
1946 free (reloc_info[o->target_index].linenos);
1947 reloc_info[o->target_index].linenos = NULL;
1953 obj_coff_keep_syms (abfd) = keep_syms;
1958 if (reloc_info != NULL)
1960 for (o = abfd->sections; o != NULL; o = o->next)
1962 if (reloc_info[o->target_index].csects != NULL)
1963 free (reloc_info[o->target_index].csects);
1964 if (reloc_info[o->target_index].linenos != NULL)
1965 free (reloc_info[o->target_index].linenos);
1969 obj_coff_keep_syms (abfd) = keep_syms;
1976 /* Add symbols from an XCOFF object file. */
1979 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1981 if (! _bfd_coff_get_external_symbols (abfd))
1983 if (! xcoff_link_add_symbols (abfd, info))
1985 if (! info->keep_memory)
1987 if (! _bfd_coff_free_symbols (abfd))
1993 /* Look through the loader symbols to see if this dynamic object
1994 should be included in the link. The native linker uses the loader
1995 symbols, not the normal symbol table, so we do too. */
1998 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1999 struct bfd_link_info *info,
2000 bfd_boolean *pneeded)
2004 struct internal_ldhdr ldhdr;
2005 const char *strings;
2006 bfd_byte *elsym, *elsymend;
2010 lsec = bfd_get_section_by_name (abfd, ".loader");
2012 /* There are no symbols, so don't try to include it. */
2015 if (! xcoff_get_section_contents (abfd, lsec))
2017 contents = coff_section_data (abfd, lsec)->contents;
2019 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2021 strings = (char *) contents + ldhdr.l_stoff;
2023 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2025 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2026 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2028 struct internal_ldsym ldsym;
2029 char nambuf[SYMNMLEN + 1];
2031 struct bfd_link_hash_entry *h;
2033 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2035 /* We are only interested in exported symbols. */
2036 if ((ldsym.l_smtype & L_EXPORT) == 0)
2039 if (ldsym._l._l_l._l_zeroes == 0)
2040 name = strings + ldsym._l._l_l._l_offset;
2043 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2044 nambuf[SYMNMLEN] = '\0';
2048 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2050 /* We are only interested in symbols that are currently
2051 undefined. At this point we know that we are using an XCOFF
2054 && h->type == bfd_link_hash_undefined
2055 && (((struct xcoff_link_hash_entry *) h)->flags
2056 & XCOFF_DEF_DYNAMIC) == 0)
2058 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2065 /* We do not need this shared object. */
2066 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2068 free (coff_section_data (abfd, lsec)->contents);
2069 coff_section_data (abfd, lsec)->contents = NULL;
2075 /* Look through the symbols to see if this object file should be
2076 included in the link. */
2079 xcoff_link_check_ar_symbols (bfd *abfd,
2080 struct bfd_link_info *info,
2081 bfd_boolean *pneeded)
2083 bfd_size_type symesz;
2089 if ((abfd->flags & DYNAMIC) != 0
2090 && ! info->static_link
2091 && info->output_bfd->xvec == abfd->xvec)
2092 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2094 symesz = bfd_coff_symesz (abfd);
2095 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2096 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2097 while (esym < esym_end)
2099 struct internal_syment sym;
2101 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2103 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2106 char buf[SYMNMLEN + 1];
2107 struct bfd_link_hash_entry *h;
2109 /* This symbol is externally visible, and is defined by this
2111 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2115 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2117 /* We are only interested in symbols that are currently
2118 undefined. If a symbol is currently known to be common,
2119 XCOFF linkers do not bring in an object file which
2120 defines it. We also don't bring in symbols to satisfy
2121 undefined references in shared objects. */
2123 && h->type == bfd_link_hash_undefined
2124 && (info->output_bfd->xvec != abfd->xvec
2125 || (((struct xcoff_link_hash_entry *) h)->flags
2126 & XCOFF_DEF_DYNAMIC) == 0))
2128 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2135 esym += (sym.n_numaux + 1) * symesz;
2138 /* We do not need this object file. */
2142 /* Check a single archive element to see if we need to include it in
2143 the link. *PNEEDED is set according to whether this element is
2144 needed in the link or not. This is called via
2145 _bfd_generic_link_add_archive_symbols. */
2148 xcoff_link_check_archive_element (bfd *abfd,
2149 struct bfd_link_info *info,
2150 bfd_boolean *pneeded)
2152 bfd_boolean keep_syms_p;
2154 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2155 if (! _bfd_coff_get_external_symbols (abfd))
2158 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2163 if (! xcoff_link_add_symbols (abfd, info))
2165 if (info->keep_memory)
2171 if (! _bfd_coff_free_symbols (abfd))
2178 /* Given an XCOFF BFD, add symbols to the global hash table as
2182 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2184 switch (bfd_get_format (abfd))
2187 return xcoff_link_add_object_symbols (abfd, info);
2190 /* If the archive has a map, do the usual search. We then need
2191 to check the archive for dynamic objects, because they may not
2192 appear in the archive map even though they should, perhaps, be
2193 included. If the archive has no map, we just consider each object
2194 file in turn, since that apparently is what the AIX native linker
2196 if (bfd_has_map (abfd))
2198 if (! (_bfd_generic_link_add_archive_symbols
2199 (abfd, info, xcoff_link_check_archive_element)))
2206 member = bfd_openr_next_archived_file (abfd, NULL);
2207 while (member != NULL)
2209 if (bfd_check_format (member, bfd_object)
2210 && (info->output_bfd->xvec == member->xvec)
2211 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2215 if (! xcoff_link_check_archive_element (member, info,
2219 member->archive_pass = -1;
2221 member = bfd_openr_next_archived_file (abfd, member);
2228 bfd_set_error (bfd_error_wrong_format);
2233 /* If symbol H has not been interpreted as a function descriptor,
2234 see whether it should be. Set up its descriptor information if so. */
2237 xcoff_find_function (struct bfd_link_info *info,
2238 struct xcoff_link_hash_entry *h)
2240 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2241 && h->root.root.string[0] != '.')
2244 struct xcoff_link_hash_entry *hfn;
2247 amt = strlen (h->root.root.string) + 2;
2248 fnname = bfd_malloc (amt);
2252 strcpy (fnname + 1, h->root.root.string);
2253 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2254 fnname, FALSE, FALSE, TRUE);
2257 && hfn->smclas == XMC_PR
2258 && (hfn->root.type == bfd_link_hash_defined
2259 || hfn->root.type == bfd_link_hash_defweak))
2261 h->flags |= XCOFF_DESCRIPTOR;
2262 h->descriptor = hfn;
2263 hfn->descriptor = h;
2269 /* H is an imported symbol. Set the import module's path, file and member
2270 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
2271 no specific import module is specified. */
2274 xcoff_set_import_path (struct bfd_link_info *info,
2275 struct xcoff_link_hash_entry *h,
2276 const char *imppath, const char *impfile,
2277 const char *impmember)
2280 struct xcoff_import_file **pp;
2282 /* We overload the ldindx field to hold the l_ifile value for this
2284 BFD_ASSERT (h->ldsym == NULL);
2285 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2286 if (imppath == NULL)
2290 /* We start c at 1 because the first entry in the import list is
2291 reserved for the library search path. */
2292 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2294 pp = &(*pp)->next, ++c)
2296 if (strcmp ((*pp)->path, imppath) == 0
2297 && strcmp ((*pp)->file, impfile) == 0
2298 && strcmp ((*pp)->member, impmember) == 0)
2304 struct xcoff_import_file *n;
2305 bfd_size_type amt = sizeof (* n);
2307 n = bfd_alloc (info->output_bfd, amt);
2313 n->member = impmember;
2321 /* Return true if the given bfd contains at least one shared object. */
2324 xcoff_archive_contains_shared_object_p (bfd *archive)
2328 member = bfd_openr_next_archived_file (archive, NULL);
2329 while (member != NULL && (member->flags & DYNAMIC) == 0)
2330 member = bfd_openr_next_archived_file (archive, member);
2331 return member != NULL;
2334 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2335 qualifies for export by -bexpall. */
2338 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2340 /* Exclude symbols beginning with '_'. */
2341 if (h->root.root.string[0] == '_')
2344 /* Exclude archive members that would otherwise be unreferenced. */
2345 if ((h->flags & XCOFF_MARK) == 0
2346 && (h->root.type == bfd_link_hash_defined
2347 || h->root.type == bfd_link_hash_defweak)
2348 && h->root.u.def.section->owner != NULL
2349 && h->root.u.def.section->owner->my_archive != NULL)
2355 /* Return true if symbol H qualifies for the forms of automatic export
2356 specified by AUTO_EXPORT_FLAGS. */
2359 xcoff_auto_export_p (struct xcoff_link_hash_entry *h,
2360 unsigned int auto_export_flags)
2362 /* Don't automatically export things that were explicitly exported. */
2363 if ((h->flags & XCOFF_EXPORT) != 0)
2366 /* Don't export things that we don't define. */
2367 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2370 /* Don't export functions; export their descriptors instead. */
2371 if (h->root.root.string[0] == '.')
2374 /* We don't export a symbol which is being defined by an object
2375 included from an archive which contains a shared object. The
2376 rationale is that if an archive contains both an unshared and
2377 a shared object, then there must be some reason that the
2378 unshared object is unshared, and we don't want to start
2379 providing a shared version of it. In particular, this solves
2380 a bug involving the _savefNN set of functions. gcc will call
2381 those functions without providing a slot to restore the TOC,
2382 so it is essential that these functions be linked in directly
2383 and not from a shared object, which means that a shared
2384 object which also happens to link them in must not export
2385 them. This is confusing, but I haven't been able to think of
2386 a different approach. Note that the symbols can, of course,
2387 be exported explicitly. */
2388 if (h->root.type == bfd_link_hash_defined
2389 || h->root.type == bfd_link_hash_defweak)
2393 owner = h->root.u.def.section->owner;
2395 && owner->my_archive != NULL
2396 && xcoff_archive_contains_shared_object_p (owner->my_archive))
2400 /* Otherwise, all symbols are exported by -bexpfull. */
2401 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2404 /* Despite its name, -bexpall exports most but not all symbols. */
2405 if ((auto_export_flags & XCOFF_EXPALL) != 0
2406 && xcoff_covered_by_expall_p (h))
2412 /* Mark a symbol as not being garbage, including the section in which
2415 static inline bfd_boolean
2416 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2418 if ((h->flags & XCOFF_MARK) != 0)
2421 h->flags |= XCOFF_MARK;
2423 /* If we're marking an undefined symbol, try find some way of
2425 if (!info->relocatable
2426 && (h->flags & XCOFF_IMPORT) == 0
2427 && (h->flags & XCOFF_DEF_REGULAR) == 0
2428 && (h->root.type == bfd_link_hash_undefined
2429 || h->root.type == bfd_link_hash_undefweak))
2431 /* First check whether this symbol can be interpreted as an
2432 undefined function descriptor for a defined function symbol. */
2433 if (!xcoff_find_function (info, h))
2436 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2437 && (h->descriptor->root.type == bfd_link_hash_defined
2438 || h->descriptor->root.type == bfd_link_hash_defweak))
2440 /* This is a descriptor for a defined symbol, but the input
2441 objects have not defined the descriptor itself. Fill in
2442 the definition automatically.
2444 Note that we do this even if we found a dynamic definition
2445 of H. The local function definition logically overrides
2449 sec = xcoff_hash_table (info)->descriptor_section;
2450 h->root.type = bfd_link_hash_defined;
2451 h->root.u.def.section = sec;
2452 h->root.u.def.value = sec->size;
2454 h->flags |= XCOFF_DEF_REGULAR;
2456 /* The size of the function descriptor depends on whether this
2457 is xcoff32 (12) or xcoff64 (24). */
2458 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2460 /* A function descriptor uses two relocs: one for the
2461 associated code, and one for the TOC address. */
2462 xcoff_hash_table (info)->ldrel_count += 2;
2463 sec->reloc_count += 2;
2465 /* Mark the function itself. */
2466 if (!xcoff_mark_symbol (info, h->descriptor))
2469 /* Mark the TOC section, so that we get an anchor
2470 to relocate against. */
2471 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2474 /* We handle writing out the contents of the descriptor in
2475 xcoff_write_global_symbol. */
2477 else if ((h->flags & XCOFF_CALLED) != 0)
2479 /* This is a function symbol for which we need to create
2482 struct xcoff_link_hash_entry *hds;
2484 /* Mark the descriptor (and its TOC section). */
2485 hds = h->descriptor;
2486 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2487 || hds->root.type == bfd_link_hash_undefweak)
2488 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2489 if (!xcoff_mark_symbol (info, hds))
2492 /* Treat this symbol as undefined if the descriptor was. */
2493 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2494 h->flags |= XCOFF_WAS_UNDEFINED;
2496 /* Allocate room for the global linkage code itself. */
2497 sec = xcoff_hash_table (info)->linkage_section;
2498 h->root.type = bfd_link_hash_defined;
2499 h->root.u.def.section = sec;
2500 h->root.u.def.value = sec->size;
2502 h->flags |= XCOFF_DEF_REGULAR;
2503 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2505 /* The global linkage code requires a TOC entry for the
2507 if (hds->toc_section == NULL)
2512 xcoff32 uses 4 bytes in the toc.
2513 xcoff64 uses 8 bytes in the toc. */
2514 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2516 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2521 /* Allocate room in the fallback TOC section. */
2522 hds->toc_section = xcoff_hash_table (info)->toc_section;
2523 hds->u.toc_offset = hds->toc_section->size;
2524 hds->toc_section->size += byte_size;
2525 if (!xcoff_mark (info, hds->toc_section))
2528 /* Allocate room for a static and dynamic R_TOC
2530 ++xcoff_hash_table (info)->ldrel_count;
2531 ++hds->toc_section->reloc_count;
2533 /* Set the index to -2 to force this symbol to
2536 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2539 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2541 /* Record that the symbol was undefined, then import it.
2542 -brtl links use a special fake import file. */
2543 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2544 if (xcoff_hash_table (info)->rtld)
2546 if (!xcoff_set_import_path (info, h, "", "..", ""))
2551 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2557 if (h->root.type == bfd_link_hash_defined
2558 || h->root.type == bfd_link_hash_defweak)
2562 hsec = h->root.u.def.section;
2563 if (! bfd_is_abs_section (hsec)
2564 && (hsec->flags & SEC_MARK) == 0)
2566 if (! xcoff_mark (info, hsec))
2571 if (h->toc_section != NULL
2572 && (h->toc_section->flags & SEC_MARK) == 0)
2574 if (! xcoff_mark (info, h->toc_section))
2581 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2582 If the symbol exists, set FLAGS. */
2585 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2586 const char *name, unsigned int flags)
2588 struct xcoff_link_hash_entry *h;
2590 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2591 FALSE, FALSE, TRUE);
2595 if (h->root.type == bfd_link_hash_defined
2596 || h->root.type == bfd_link_hash_defweak)
2598 if (!xcoff_mark (info, h->root.u.def.section))
2605 /* The mark phase of garbage collection. For a given section, mark
2606 it, and all the sections which define symbols to which it refers.
2607 Because this function needs to look at the relocs, we also count
2608 the number of relocs which need to be copied into the .loader
2612 xcoff_mark (struct bfd_link_info *info, asection *sec)
2614 if (bfd_is_abs_section (sec)
2615 || (sec->flags & SEC_MARK) != 0)
2618 sec->flags |= SEC_MARK;
2620 if (sec->owner->xvec == info->output_bfd->xvec
2621 && coff_section_data (sec->owner, sec) != NULL
2622 && xcoff_section_data (sec->owner, sec) != NULL)
2624 struct xcoff_link_hash_entry **syms;
2625 struct internal_reloc *rel, *relend;
2627 unsigned long i, first, last;
2629 /* Mark all the symbols in this section. */
2630 syms = obj_xcoff_sym_hashes (sec->owner);
2631 csects = xcoff_data (sec->owner)->csects;
2632 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2633 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2634 for (i = first; i <= last; i++)
2635 if (csects[i] == sec
2637 && (syms[i]->flags & XCOFF_MARK) == 0)
2639 if (!xcoff_mark_symbol (info, syms[i]))
2643 /* Look through the section relocs. */
2644 if ((sec->flags & SEC_RELOC) != 0
2645 && sec->reloc_count > 0)
2647 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2651 relend = rel + sec->reloc_count;
2652 for (; rel < relend; rel++)
2654 struct xcoff_link_hash_entry *h;
2656 if ((unsigned int) rel->r_symndx
2657 > obj_raw_syment_count (sec->owner))
2660 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2663 if ((h->flags & XCOFF_MARK) == 0)
2665 if (!xcoff_mark_symbol (info, h))
2673 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2675 && (rsec->flags & SEC_MARK) == 0)
2677 if (!xcoff_mark (info, rsec))
2682 /* See if this reloc needs to be copied into the .loader
2684 switch (rel->r_type)
2688 || h->root.type == bfd_link_hash_defined
2689 || h->root.type == bfd_link_hash_defweak
2690 || h->root.type == bfd_link_hash_common
2691 /* We will always provide a local definition of
2692 function symbols. */
2693 || (h->flags & XCOFF_CALLED) != 0)
2701 && (h->root.type == bfd_link_hash_defined
2702 || h->root.type == bfd_link_hash_defweak)
2703 && bfd_is_abs_section (h->root.u.def.section))
2705 ++xcoff_hash_table (info)->ldrel_count;
2707 h->flags |= XCOFF_LDREL;
2714 /* We should never need a .loader reloc for a TOC
2720 if (! info->keep_memory
2721 && coff_section_data (sec->owner, sec) != NULL
2722 && coff_section_data (sec->owner, sec)->relocs != NULL
2723 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2725 free (coff_section_data (sec->owner, sec)->relocs);
2726 coff_section_data (sec->owner, sec)->relocs = NULL;
2734 /* Routines that are called after all the input files have been
2735 handled, but before the sections are laid out in memory. */
2737 /* The sweep phase of garbage collection. Remove all garbage
2741 xcoff_sweep (struct bfd_link_info *info)
2745 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2749 for (o = sub->sections; o != NULL; o = o->next)
2751 if ((o->flags & SEC_MARK) == 0)
2753 /* Keep all sections from non-XCOFF input files. Keep
2754 special sections. Keep .debug sections for the
2756 if (sub->xvec != info->output_bfd->xvec
2757 || o == xcoff_hash_table (info)->debug_section
2758 || o == xcoff_hash_table (info)->loader_section
2759 || o == xcoff_hash_table (info)->linkage_section
2760 || o == xcoff_hash_table (info)->descriptor_section
2761 || strcmp (o->name, ".debug") == 0)
2762 o->flags |= SEC_MARK;
2773 /* Record the number of elements in a set. This is used to output the
2774 correct csect length. */
2777 bfd_xcoff_link_record_set (bfd *output_bfd,
2778 struct bfd_link_info *info,
2779 struct bfd_link_hash_entry *harg,
2782 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2783 struct xcoff_link_size_list *n;
2786 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2789 /* This will hardly ever be called. I don't want to burn four bytes
2790 per global symbol, so instead the size is kept on a linked list
2791 attached to the hash table. */
2793 n = bfd_alloc (output_bfd, amt);
2796 n->next = xcoff_hash_table (info)->size_list;
2799 xcoff_hash_table (info)->size_list = n;
2801 h->flags |= XCOFF_HAS_SIZE;
2806 /* Import a symbol. */
2809 bfd_xcoff_import_symbol (bfd *output_bfd,
2810 struct bfd_link_info *info,
2811 struct bfd_link_hash_entry *harg,
2813 const char *imppath,
2814 const char *impfile,
2815 const char *impmember,
2816 unsigned int syscall_flag)
2818 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2820 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2823 /* A symbol name which starts with a period is the code for a
2824 function. If the symbol is undefined, then add an undefined
2825 symbol for the function descriptor, and import that instead. */
2826 if (h->root.root.string[0] == '.'
2827 && h->root.type == bfd_link_hash_undefined
2828 && val == (bfd_vma) -1)
2830 struct xcoff_link_hash_entry *hds;
2832 hds = h->descriptor;
2835 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2836 h->root.root.string + 1,
2840 if (hds->root.type == bfd_link_hash_new)
2842 hds->root.type = bfd_link_hash_undefined;
2843 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2845 hds->flags |= XCOFF_DESCRIPTOR;
2846 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2847 hds->descriptor = h;
2848 h->descriptor = hds;
2851 /* Now, if the descriptor is undefined, import the descriptor
2852 rather than the symbol we were told to import. FIXME: Is
2853 this correct in all cases? */
2854 if (hds->root.type == bfd_link_hash_undefined)
2858 h->flags |= (XCOFF_IMPORT | syscall_flag);
2860 if (val != (bfd_vma) -1)
2862 if (h->root.type == bfd_link_hash_defined
2863 && (! bfd_is_abs_section (h->root.u.def.section)
2864 || h->root.u.def.value != val))
2866 if (! ((*info->callbacks->multiple_definition)
2867 (info, h->root.root.string, h->root.u.def.section->owner,
2868 h->root.u.def.section, h->root.u.def.value,
2869 output_bfd, bfd_abs_section_ptr, val)))
2873 h->root.type = bfd_link_hash_defined;
2874 h->root.u.def.section = bfd_abs_section_ptr;
2875 h->root.u.def.value = val;
2879 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
2885 /* Export a symbol. */
2888 bfd_xcoff_export_symbol (bfd *output_bfd,
2889 struct bfd_link_info *info,
2890 struct bfd_link_hash_entry *harg)
2892 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2894 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2897 h->flags |= XCOFF_EXPORT;
2899 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2900 I'm just going to ignore it until somebody explains it. */
2902 /* Make sure we don't garbage collect this symbol. */
2903 if (! xcoff_mark_symbol (info, h))
2906 /* If this is a function descriptor, make sure we don't garbage
2907 collect the associated function code. We normally don't have to
2908 worry about this, because the descriptor will be attached to a
2909 section with relocs, but if we are creating the descriptor
2910 ourselves those relocs will not be visible to the mark code. */
2911 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2913 if (! xcoff_mark_symbol (info, h->descriptor))
2920 /* Count a reloc against a symbol. This is called for relocs
2921 generated by the linker script, typically for global constructors
2925 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2926 struct bfd_link_info *info,
2929 struct xcoff_link_hash_entry *h;
2931 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2934 h = ((struct xcoff_link_hash_entry *)
2935 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2939 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2940 bfd_set_error (bfd_error_no_symbols);
2944 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2945 ++xcoff_hash_table (info)->ldrel_count;
2947 /* Mark the symbol to avoid garbage collection. */
2948 if (! xcoff_mark_symbol (info, h))
2954 /* This function is called for each symbol to which the linker script
2958 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2959 struct bfd_link_info *info,
2962 struct xcoff_link_hash_entry *h;
2964 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2967 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2972 h->flags |= XCOFF_DEF_REGULAR;
2977 /* An xcoff_link_hash_traverse callback for which DATA points to an
2978 xcoff_loader_info. Mark all symbols that should be automatically
2982 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
2984 struct xcoff_loader_info *ldinfo;
2986 ldinfo = (struct xcoff_loader_info *) data;
2987 if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
2989 if (!xcoff_mark_symbol (ldinfo->info, h))
2990 ldinfo->failed = TRUE;
2995 /* Add a symbol to the .loader symbols, if necessary. */
2997 /* INPUT_BFD has an external symbol associated with hash table entry H
2998 and csect CSECT. Return true if INPUT_BFD defines H. */
3001 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3004 switch (h->root.type)
3006 case bfd_link_hash_defined:
3007 case bfd_link_hash_defweak:
3008 /* No input bfd owns absolute symbols. They are written by
3009 xcoff_write_global_symbol instead. */
3010 return (!bfd_is_abs_section (csect)
3011 && h->root.u.def.section == csect);
3013 case bfd_link_hash_common:
3014 return h->root.u.c.p->section->owner == input_bfd;
3016 case bfd_link_hash_undefined:
3017 case bfd_link_hash_undefweak:
3018 /* We can't treat undef.abfd as the owner because that bfd
3019 might be a dynamic object. Allow any bfd to claim it. */
3028 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
3030 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3033 if (h->root.type == bfd_link_hash_warning)
3034 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3036 /* __rtinit, this symbol has special handling. */
3037 if (h->flags & XCOFF_RTINIT)
3040 /* If this is a final link, and the symbol was defined as a common
3041 symbol in a regular object file, and there was no definition in
3042 any dynamic object, then the linker will have allocated space for
3043 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3044 will not have been set. */
3045 if (h->root.type == bfd_link_hash_defined
3046 && (h->flags & XCOFF_DEF_REGULAR) == 0
3047 && (h->flags & XCOFF_REF_REGULAR) != 0
3048 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3049 && (bfd_is_abs_section (h->root.u.def.section)
3050 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3051 h->flags |= XCOFF_DEF_REGULAR;
3053 /* If all defined symbols should be exported, mark them now. We
3054 don't want to export the actual functions, just the function
3056 if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
3057 h->flags |= XCOFF_EXPORT;
3059 /* We don't want to garbage collect symbols which are not defined in
3060 XCOFF files. This is a convenient place to mark them. */
3061 if (xcoff_hash_table (ldinfo->info)->gc
3062 && (h->flags & XCOFF_MARK) == 0
3063 && (h->root.type == bfd_link_hash_defined
3064 || h->root.type == bfd_link_hash_defweak)
3065 && (h->root.u.def.section->owner == NULL
3066 || (h->root.u.def.section->owner->xvec
3067 != ldinfo->info->output_bfd->xvec)))
3068 h->flags |= XCOFF_MARK;
3070 /* If this symbol is exported, but not defined, we need to try to
3072 if ((h->flags & XCOFF_EXPORT) != 0
3073 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3075 (*_bfd_error_handler)
3076 (_("warning: attempt to export undefined symbol `%s'"),
3077 h->root.root.string);
3082 /* If this is still a common symbol, and it wasn't garbage
3083 collected, we need to actually allocate space for it in the .bss
3085 if (h->root.type == bfd_link_hash_common
3086 && (! xcoff_hash_table (ldinfo->info)->gc
3087 || (h->flags & XCOFF_MARK) != 0)
3088 && h->root.u.c.p->section->size == 0)
3090 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3091 h->root.u.c.p->section->size = h->root.u.c.size;
3094 /* We need to add a symbol to the .loader section if it is mentioned
3095 in a reloc which we are copying to the .loader section and it was
3096 not defined or common, or if it is the entry point, or if it is
3099 if (((h->flags & XCOFF_LDREL) == 0
3100 || h->root.type == bfd_link_hash_defined
3101 || h->root.type == bfd_link_hash_defweak
3102 || h->root.type == bfd_link_hash_common)
3103 && (h->flags & XCOFF_ENTRY) == 0
3104 && (h->flags & XCOFF_EXPORT) == 0)
3110 /* We don't need to add this symbol if we did garbage collection and
3111 we did not mark this symbol. */
3112 if (xcoff_hash_table (ldinfo->info)->gc
3113 && (h->flags & XCOFF_MARK) == 0)
3119 /* We may have already processed this symbol due to the recursive
3121 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3124 /* We need to add this symbol to the .loader symbols. */
3126 BFD_ASSERT (h->ldsym == NULL);
3127 amt = sizeof (struct internal_ldsym);
3128 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3129 if (h->ldsym == NULL)
3131 ldinfo->failed = TRUE;
3135 if ((h->flags & XCOFF_IMPORT) != 0)
3137 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3138 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3140 h->ldsym->l_ifile = h->ldindx;
3143 /* The first 3 symbol table indices are reserved to indicate the
3144 data, text and bss sections. */
3145 h->ldindx = ldinfo->ldsym_count + 3;
3147 ++ldinfo->ldsym_count;
3149 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3150 h->ldsym, h->root.root.string))
3153 h->flags |= XCOFF_BUILT_LDSYM;
3158 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3159 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3160 csect information, if any. NAME is the function's name if the name
3161 is stored in the .debug section, otherwise it is null.
3163 Return 1 if we should include an appropriately-adjusted ISYM
3164 in the output file, 0 if we should discard ISYM, or -1 if an
3168 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3169 struct internal_syment *isym,
3170 union internal_auxent *aux,
3171 struct xcoff_link_hash_entry *h,
3172 asection *csect, const char *name)
3176 /* If we are skipping this csect, we want to strip the symbol too. */
3180 /* Likewise if we garbage-collected the csect. */
3181 if (xcoff_hash_table (info)->gc
3182 && !bfd_is_abs_section (csect)
3183 && !bfd_is_und_section (csect)
3184 && (csect->flags & SEC_MARK) == 0)
3187 /* An XCOFF linker always removes C_STAT symbols. */
3188 if (isym->n_sclass == C_STAT)
3191 /* We generate the TOC anchor separately. */
3192 if (isym->n_sclass == C_HIDEXT
3193 && aux->x_csect.x_smclas == XMC_TC0)
3196 /* If we are stripping all symbols, we want to discard this one. */
3197 if (info->strip == strip_all)
3200 /* Discard symbols that are defined elsewhere. */
3201 if (EXTERN_SYM_P (isym->n_sclass))
3203 if ((h->flags & XCOFF_ALLOCATED) != 0)
3205 if (!xcoff_final_definition_p (input_bfd, h, csect))
3209 /* If we're discarding local symbols, check whether ISYM is local. */
3210 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3211 if (info->discard == discard_all
3212 && !EXTERN_SYM_P (isym->n_sclass)
3213 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3216 /* If we're stripping debugging symbols, check whether ISYM is one. */
3217 if (info->strip == strip_debugger
3218 && isym->n_scnum == N_DEBUG)
3221 /* If we are stripping symbols based on name, check how ISYM's
3222 name should be handled. */
3223 if (info->strip == strip_some
3224 || info->discard == discard_l)
3226 char buf[SYMNMLEN + 1];
3230 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3235 if (info->strip == strip_some
3236 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3239 if (info->discard == discard_l
3240 && !EXTERN_SYM_P (isym->n_sclass)
3241 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3242 && bfd_is_local_label_name (input_bfd, name))
3249 /* Build the .loader section. This is called by the XCOFF linker
3250 emulation before_allocation routine. We must set the size of the
3251 .loader section before the linker lays out the output file.
3252 LIBPATH is the library path to search for shared objects; this is
3253 normally built from the -L arguments passed to the linker. ENTRY
3254 is the name of the entry point symbol (the -e linker option).
3255 FILE_ALIGN is the alignment to use for sections within the file
3256 (the -H linker option). MAXSTACK is the maximum stack size (the
3257 -bmaxstack linker option). MAXDATA is the maximum data size (the
3258 -bmaxdata linker option). GC is whether to do garbage collection
3259 (the -bgc linker option). MODTYPE is the module type (the
3260 -bmodtype linker option). TEXTRO is whether the text section must
3261 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3262 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3263 is set by this routine to csects with magic names like _end. */
3266 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3267 struct bfd_link_info *info,
3268 const char *libpath,
3270 unsigned long file_align,
3271 unsigned long maxstack,
3272 unsigned long maxdata,
3276 unsigned int auto_export_flags,
3277 asection **special_sections,
3281 struct xcoff_loader_info ldinfo;
3283 size_t impsize, impcount;
3284 struct xcoff_import_file *fl;
3285 struct internal_ldhdr *ldhdr;
3286 bfd_size_type stoff;
3290 struct bfd_strtab_hash *debug_strtab;
3291 bfd_byte *debug_contents = NULL;
3294 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3296 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3297 special_sections[i] = NULL;
3301 ldinfo.failed = FALSE;
3302 ldinfo.output_bfd = output_bfd;
3304 ldinfo.auto_export_flags = auto_export_flags;
3305 ldinfo.ldsym_count = 0;
3306 ldinfo.string_size = 0;
3307 ldinfo.strings = NULL;
3308 ldinfo.string_alc = 0;
3310 xcoff_data (output_bfd)->maxstack = maxstack;
3311 xcoff_data (output_bfd)->maxdata = maxdata;
3312 xcoff_data (output_bfd)->modtype = modtype;
3314 xcoff_hash_table (info)->file_align = file_align;
3315 xcoff_hash_table (info)->textro = textro;
3316 xcoff_hash_table (info)->rtld = rtld;
3319 if (info->init_function || info->fini_function || rtld)
3321 struct xcoff_link_hash_entry *hsym;
3322 struct internal_ldsym *ldsym;
3324 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3325 "__rtinit", FALSE, FALSE, TRUE);
3328 (*_bfd_error_handler)
3329 (_("error: undefined symbol __rtinit"));
3333 xcoff_mark_symbol (info, hsym);
3334 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3336 /* __rtinit initialized. */
3337 amt = sizeof (* ldsym);
3338 ldsym = bfd_malloc (amt);
3340 ldsym->l_value = 0; /* Will be filled in later. */
3341 ldsym->l_scnum = 2; /* Data section. */
3342 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3343 ldsym->l_smclas = 5; /* .rw. */
3344 ldsym->l_ifile = 0; /* Special system loader symbol. */
3345 ldsym->l_parm = 0; /* NA. */
3347 /* Force __rtinit to be the first symbol in the loader symbol table
3348 See xcoff_build_ldsyms
3350 The first 3 symbol table indices are reserved to indicate the data,
3351 text and bss sections. */
3352 BFD_ASSERT (0 == ldinfo.ldsym_count);
3355 ldinfo.ldsym_count = 1;
3356 hsym->ldsym = ldsym;
3358 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3359 hsym->ldsym, hsym->root.root.string))
3362 /* This symbol is written out by xcoff_write_global_symbol
3363 Set stuff up so xcoff_write_global_symbol logic works. */
3364 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3365 hsym->root.type = bfd_link_hash_defined;
3366 hsym->root.u.def.value = 0;
3369 /* Garbage collect unused sections. */
3370 if (info->relocatable || !gc)
3373 xcoff_hash_table (info)->gc = FALSE;
3375 /* We still need to call xcoff_mark, in order to set ldrel_count
3377 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3381 for (o = sub->sections; o != NULL; o = o->next)
3383 /* We shouldn't unconditionaly mark the TOC section.
3384 The output file should only have a TOC if either
3385 (a) one of the input files did or (b) we end up
3386 creating TOC references as part of the link process. */
3387 if (o != xcoff_hash_table (info)->toc_section
3388 && (o->flags & SEC_MARK) == 0)
3390 if (! xcoff_mark (info, o))
3399 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3401 if (info->init_function != NULL
3402 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3404 if (info->fini_function != NULL
3405 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3407 if (auto_export_flags != 0)
3409 xcoff_link_hash_traverse (xcoff_hash_table (info),
3410 xcoff_mark_auto_exports, &ldinfo);
3415 xcoff_hash_table (info)->gc = TRUE;
3418 /* Return special sections to the caller. */
3419 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3421 sec = xcoff_hash_table (info)->special_sections[i];
3425 && (sec->flags & SEC_MARK) == 0)
3428 special_sections[i] = sec;
3431 if (info->input_bfds == NULL)
3432 /* I'm not sure what to do in this bizarre case. */
3435 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3440 /* Work out the size of the import file names. Each import file ID
3441 consists of three null terminated strings: the path, the file
3442 name, and the archive member name. The first entry in the list
3443 of names is the path to use to find objects, which the linker has
3444 passed in as the libpath argument. For some reason, the path
3445 entry in the other import file names appears to always be empty. */
3446 impsize = strlen (libpath) + 3;
3448 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3451 impsize += (strlen (fl->path)
3453 + strlen (fl->member)
3457 /* Set up the .loader section header. */
3458 ldhdr = &xcoff_hash_table (info)->ldhdr;
3459 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3460 ldhdr->l_nsyms = ldinfo.ldsym_count;
3461 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3462 ldhdr->l_istlen = impsize;
3463 ldhdr->l_nimpid = impcount;
3464 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3465 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3466 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3467 ldhdr->l_stlen = ldinfo.string_size;
3468 stoff = ldhdr->l_impoff + impsize;
3469 if (ldinfo.string_size == 0)
3472 ldhdr->l_stoff = stoff;
3474 /* 64 bit elements to ldhdr
3475 The swap out routine for 32 bit will ignore them.
3476 Nothing fancy, symbols come after the header and relocs come
3478 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3479 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3480 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3482 /* We now know the final size of the .loader section. Allocate
3484 lsec = xcoff_hash_table (info)->loader_section;
3485 lsec->size = stoff + ldhdr->l_stlen;
3486 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3487 if (lsec->contents == NULL)
3490 /* Set up the header. */
3491 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3493 /* Set up the import file names. */
3494 out = (char *) lsec->contents + ldhdr->l_impoff;
3495 strcpy (out, libpath);
3496 out += strlen (libpath) + 1;
3499 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3504 while ((*out++ = *s++) != '\0')
3507 while ((*out++ = *s++) != '\0')
3510 while ((*out++ = *s++) != '\0')
3514 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3516 /* Set up the symbol string table. */
3517 if (ldinfo.string_size > 0)
3519 memcpy (out, ldinfo.strings, ldinfo.string_size);
3520 free (ldinfo.strings);
3521 ldinfo.strings = NULL;
3524 /* We can't set up the symbol table or the relocs yet, because we
3525 don't yet know the final position of the various sections. The
3526 .loader symbols are written out when the corresponding normal
3527 symbols are written out in xcoff_link_input_bfd or
3528 xcoff_write_global_symbol. The .loader relocs are written out
3529 when the corresponding normal relocs are handled in
3530 xcoff_link_input_bfd. */
3532 /* Allocate space for the magic sections. */
3533 sec = xcoff_hash_table (info)->linkage_section;
3536 sec->contents = bfd_zalloc (output_bfd, sec->size);
3537 if (sec->contents == NULL)
3540 sec = xcoff_hash_table (info)->toc_section;
3543 sec->contents = bfd_zalloc (output_bfd, sec->size);
3544 if (sec->contents == NULL)
3547 sec = xcoff_hash_table (info)->descriptor_section;
3550 sec->contents = bfd_zalloc (output_bfd, sec->size);
3551 if (sec->contents == NULL)
3555 /* Now that we've done garbage collection, decide which symbols to keep,
3556 and figure out the contents of the .debug section. */
3557 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3559 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3562 bfd_size_type symcount;
3565 unsigned int *lineno_counts;
3566 struct xcoff_link_hash_entry **sym_hash;
3567 bfd_byte *esym, *esymend;
3568 bfd_size_type symesz;
3570 if (sub->xvec != info->output_bfd->xvec)
3573 if ((sub->flags & DYNAMIC) != 0
3574 && !info->static_link)
3577 if (! _bfd_coff_get_external_symbols (sub))
3580 symcount = obj_raw_syment_count (sub);
3581 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3582 if (debug_index == NULL)
3584 xcoff_data (sub)->debug_indices = debug_index;
3586 if (info->strip == strip_all
3587 || info->strip == strip_debugger
3588 || info->discard == discard_all)
3589 /* We're stripping all debugging information, so there's no need
3590 to read SUB's .debug section. */
3594 /* Grab the contents of SUB's .debug section, if any. */
3595 subdeb = bfd_get_section_by_name (sub, ".debug");
3596 if (subdeb != NULL && subdeb->size > 0)
3598 /* We use malloc and copy the names into the debug
3599 stringtab, rather than bfd_alloc, because I expect
3600 that, when linking many files together, many of the
3601 strings will be the same. Storing the strings in the
3602 hash table should save space in this case. */
3603 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3608 csectpp = xcoff_data (sub)->csects;
3609 lineno_counts = xcoff_data (sub)->lineno_counts;
3610 sym_hash = obj_xcoff_sym_hashes (sub);
3611 symesz = bfd_coff_symesz (sub);
3612 esym = (bfd_byte *) obj_coff_external_syms (sub);
3613 esymend = esym + symcount * symesz;
3615 while (esym < esymend)
3617 struct internal_syment sym;
3618 union internal_auxent aux;
3623 bfd_coff_swap_sym_in (sub, esym, &sym);
3625 /* Read in the csect information, if any. */
3626 if (CSECT_SYM_P (sym.n_sclass))
3628 BFD_ASSERT (sym.n_numaux > 0);
3629 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3630 sym.n_type, sym.n_sclass,
3631 sym.n_numaux - 1, sym.n_numaux, &aux);
3634 /* If this symbol's name is stored in the debug section,
3635 get a pointer to it. */
3636 if (debug_contents != NULL
3637 && sym._n._n_n._n_zeroes == 0
3638 && bfd_coff_symname_in_debug (sub, &sym))
3639 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3643 /* Decide whether to copy this symbol to the output file. */
3645 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3646 *sym_hash, csect, name);
3651 /* Use a debug_index of -2 to record that a symbol should
3656 /* See whether we should store the symbol name in the
3657 output .debug section. */
3662 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3663 if (indx == (bfd_size_type) -1)
3665 *debug_index = indx;
3670 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3671 if (*lineno_counts > 0)
3672 csect->output_section->lineno_count += *lineno_counts;
3675 esym += (sym.n_numaux + 1) * symesz;
3676 csectpp += sym.n_numaux + 1;
3677 sym_hash += sym.n_numaux + 1;
3678 lineno_counts += sym.n_numaux + 1;
3679 debug_index += sym.n_numaux + 1;
3684 free (debug_contents);
3685 debug_contents = NULL;
3687 /* Clear the size of subdeb, so that it is not included directly
3688 in the output file. */
3692 if (! info->keep_memory)
3694 if (! _bfd_coff_free_symbols (sub))
3699 if (info->strip != strip_all)
3700 xcoff_hash_table (info)->debug_section->size =
3701 _bfd_stringtab_size (debug_strtab);
3706 if (ldinfo.strings != NULL)
3707 free (ldinfo.strings);
3708 if (debug_contents != NULL)
3709 free (debug_contents);
3714 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3719 struct bfd_in_memory *bim;
3721 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3728 abfd->link_next = 0;
3729 abfd->format = bfd_object;
3730 abfd->iostream = (void *) bim;
3731 abfd->flags = BFD_IN_MEMORY;
3732 abfd->direction = write_direction;
3735 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3738 /* need to reset to unknown or it will not be read back in correctly */
3739 abfd->format = bfd_unknown;
3740 abfd->direction = read_direction;
3746 /* Link an input file into the linker output file. This function
3747 handles all the sections and relocations of the input file at once. */
3750 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3754 const char *strings;
3755 bfd_size_type syment_base;
3756 unsigned int n_tmask;
3757 unsigned int n_btshft;
3758 bfd_boolean copy, hash;
3759 bfd_size_type isymesz;
3760 bfd_size_type osymesz;
3761 bfd_size_type linesz;
3764 struct xcoff_link_hash_entry **sym_hash;
3765 struct internal_syment *isymp;
3767 unsigned int *lineno_counts;
3770 unsigned long output_index;
3774 bfd_boolean keep_syms;
3777 /* We can just skip DYNAMIC files, unless this is a static link. */
3778 if ((input_bfd->flags & DYNAMIC) != 0
3779 && ! finfo->info->static_link)
3782 /* Move all the symbols to the output file. */
3783 output_bfd = finfo->output_bfd;
3785 syment_base = obj_raw_syment_count (output_bfd);
3786 isymesz = bfd_coff_symesz (input_bfd);
3787 osymesz = bfd_coff_symesz (output_bfd);
3788 linesz = bfd_coff_linesz (input_bfd);
3789 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3791 n_tmask = coff_data (input_bfd)->local_n_tmask;
3792 n_btshft = coff_data (input_bfd)->local_n_btshft;
3794 /* Define macros so that ISFCN, et. al., macros work correctly. */
3795 #define N_TMASK n_tmask
3796 #define N_BTSHFT n_btshft
3799 if (! finfo->info->keep_memory)
3802 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3805 if (! _bfd_coff_get_external_symbols (input_bfd))
3808 /* Make one pass over the symbols and assign indices to symbols that
3809 we have decided to keep. Also use create .loader symbol information
3810 and update information in hash table entries. */
3811 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3812 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3813 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3814 csectpp = xcoff_data (input_bfd)->csects;
3815 debug_index = xcoff_data (input_bfd)->debug_indices;
3816 isymp = finfo->internal_syms;
3817 indexp = finfo->sym_indices;
3818 output_index = syment_base;
3819 while (esym < esym_end)
3821 union internal_auxent aux;
3825 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3827 /* Read in the csect information, if any. */
3828 if (CSECT_SYM_P (isymp->n_sclass))
3830 BFD_ASSERT (isymp->n_numaux > 0);
3831 bfd_coff_swap_aux_in (input_bfd,
3832 (void *) (esym + isymesz * isymp->n_numaux),
3833 isymp->n_type, isymp->n_sclass,
3834 isymp->n_numaux - 1, isymp->n_numaux,
3837 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3840 /* If this symbol is in the .loader section, swap out the
3841 .loader symbol information. If this is an external symbol
3842 reference to a defined symbol, though, then wait until we get
3843 to the definition. */
3844 if (EXTERN_SYM_P (isymp->n_sclass)
3845 && *sym_hash != NULL
3846 && (*sym_hash)->ldsym != NULL
3847 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
3849 struct xcoff_link_hash_entry *h;
3850 struct internal_ldsym *ldsym;
3854 if (isymp->n_scnum > 0)
3856 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3857 ldsym->l_value = (isymp->n_value
3858 + (*csectpp)->output_section->vma
3859 + (*csectpp)->output_offset
3864 ldsym->l_scnum = isymp->n_scnum;
3865 ldsym->l_value = isymp->n_value;
3868 ldsym->l_smtype = smtyp;
3869 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3870 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3871 || (h->flags & XCOFF_IMPORT) != 0)
3872 ldsym->l_smtype |= L_IMPORT;
3873 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3874 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3875 || (h->flags & XCOFF_EXPORT) != 0)
3876 ldsym->l_smtype |= L_EXPORT;
3877 if ((h->flags & XCOFF_ENTRY) != 0)
3878 ldsym->l_smtype |= L_ENTRY;
3879 if (isymp->n_sclass == C_AIX_WEAKEXT)
3880 ldsym->l_smtype |= L_WEAK;
3882 ldsym->l_smclas = aux.x_csect.x_smclas;
3884 if (ldsym->l_ifile == (bfd_size_type) -1)
3886 else if (ldsym->l_ifile == 0)
3888 if ((ldsym->l_smtype & L_IMPORT) == 0)
3894 if (h->root.type == bfd_link_hash_defined
3895 || h->root.type == bfd_link_hash_defweak)
3896 impbfd = h->root.u.def.section->owner;
3897 else if (h->root.type == bfd_link_hash_undefined
3898 || h->root.type == bfd_link_hash_undefweak)
3899 impbfd = h->root.u.undef.abfd;
3907 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3908 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3915 BFD_ASSERT (h->ldindx >= 0);
3916 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3919 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3922 /* Fill in snentry now that we know the target_index. */
3923 if ((h->flags & XCOFF_ENTRY) != 0
3924 && (h->root.type == bfd_link_hash_defined
3925 || h->root.type == bfd_link_hash_defweak))
3927 xcoff_data (output_bfd)->snentry =
3928 h->root.u.def.section->output_section->target_index;
3932 add = 1 + isymp->n_numaux;
3934 if (*debug_index == -2)
3935 /* We've decided to strip this symbol. */
3939 /* Assign the next unused index to this symbol. */
3940 *indexp = output_index;
3942 if (EXTERN_SYM_P (isymp->n_sclass))
3944 BFD_ASSERT (*sym_hash != NULL);
3945 (*sym_hash)->indx = output_index;
3948 /* If this is a symbol in the TOC which we may have merged
3949 (class XMC_TC), remember the symbol index of the TOC
3951 if (isymp->n_sclass == C_HIDEXT
3952 && aux.x_csect.x_smclas == XMC_TC
3953 && *sym_hash != NULL)
3955 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3956 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3957 (*sym_hash)->u.toc_indx = output_index;
3960 output_index += add;
3963 esym += add * isymesz;
3969 for (--add; add > 0; --add)
3973 /* Now write out the symbols that we decided to keep. */
3975 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3976 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3977 isymp = finfo->internal_syms;
3978 indexp = finfo->sym_indices;
3979 csectpp = xcoff_data (input_bfd)->csects;
3980 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
3981 debug_index = xcoff_data (input_bfd)->debug_indices;
3982 outsym = finfo->outsyms;
3985 while (esym < esym_end)
3989 add = 1 + isymp->n_numaux;
3992 esym += add * isymesz;
3995 struct internal_syment isym;
3998 /* Adjust the symbol in order to output it. */
4000 if (isym._n._n_n._n_zeroes == 0
4001 && isym._n._n_n._n_offset != 0)
4003 /* This symbol has a long name. Enter it in the string
4004 table we are building. If *debug_index != -1, the
4005 name has already been entered in the .debug section. */
4006 if (*debug_index >= 0)
4007 isym._n._n_n._n_offset = *debug_index;
4013 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4017 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4018 if (indx == (bfd_size_type) -1)
4020 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4024 /* The value of a C_FILE symbol is the symbol index of the
4025 next C_FILE symbol. The value of the last C_FILE symbol
4026 is -1. We try to get this right, below, just before we
4027 write the symbols out, but in the general case we may
4028 have to write the symbol out twice. */
4029 if (isym.n_sclass == C_FILE)
4031 if (finfo->last_file_index != -1
4032 && finfo->last_file.n_value != (bfd_vma) *indexp)
4034 /* We must correct the value of the last C_FILE entry. */
4035 finfo->last_file.n_value = *indexp;
4036 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4038 /* The last C_FILE symbol is in this input file. */
4039 bfd_coff_swap_sym_out (output_bfd,
4040 (void *) &finfo->last_file,
4041 (void *) (finfo->outsyms
4042 + ((finfo->last_file_index
4048 /* We have already written out the last C_FILE
4049 symbol. We need to write it out again. We
4050 borrow *outsym temporarily. */
4053 bfd_coff_swap_sym_out (output_bfd,
4054 (void *) &finfo->last_file,
4057 pos = obj_sym_filepos (output_bfd);
4058 pos += finfo->last_file_index * osymesz;
4059 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4060 || (bfd_bwrite (outsym, osymesz, output_bfd)
4066 finfo->last_file_index = *indexp;
4067 finfo->last_file = isym;
4070 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4071 into the line numbers. We update the symbol values when
4072 we handle the line numbers. */
4073 if (isym.n_sclass == C_BINCL
4074 || isym.n_sclass == C_EINCL)
4076 isym.n_value = finfo->line_filepos;
4079 /* The value of a C_BSTAT symbol is the symbol table
4080 index of the containing csect. */
4081 else if (isym.n_sclass == C_BSTAT)
4085 indx = isym.n_value;
4086 if (indx < obj_raw_syment_count (input_bfd))
4090 symindx = finfo->sym_indices[indx];
4094 isym.n_value = symindx;
4097 else if (isym.n_sclass != C_ESTAT
4098 && isym.n_sclass != C_DECL
4099 && isym.n_scnum > 0)
4101 isym.n_scnum = (*csectpp)->output_section->target_index;
4102 isym.n_value += ((*csectpp)->output_section->vma
4103 + (*csectpp)->output_offset
4107 /* Output the symbol. */
4108 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4113 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4115 union internal_auxent aux;
4117 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4118 isymp->n_sclass, i, isymp->n_numaux,
4121 if (isymp->n_sclass == C_FILE)
4123 /* This is the file name (or some comment put in by
4124 the compiler). If it is long, we must put it in
4125 the string table. */
4126 if (aux.x_file.x_n.x_zeroes == 0
4127 && aux.x_file.x_n.x_offset != 0)
4129 const char *filename;
4132 BFD_ASSERT (aux.x_file.x_n.x_offset
4133 >= STRING_SIZE_SIZE);
4134 if (strings == NULL)
4136 strings = _bfd_coff_read_string_table (input_bfd);
4137 if (strings == NULL)
4140 filename = strings + aux.x_file.x_n.x_offset;
4141 indx = _bfd_stringtab_add (finfo->strtab, filename,
4143 if (indx == (bfd_size_type) -1)
4145 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4148 else if (CSECT_SYM_P (isymp->n_sclass)
4149 && i + 1 == isymp->n_numaux)
4152 /* We don't support type checking. I don't know if
4154 aux.x_csect.x_parmhash = 0;
4155 /* I don't think anybody uses these fields, but we'd
4156 better clobber them just in case. */
4157 aux.x_csect.x_stab = 0;
4158 aux.x_csect.x_snstab = 0;
4160 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4164 indx = aux.x_csect.x_scnlen.l;
4165 if (indx < obj_raw_syment_count (input_bfd))
4169 symindx = finfo->sym_indices[indx];
4172 aux.x_csect.x_scnlen.l = 0;
4176 aux.x_csect.x_scnlen.l = symindx;
4181 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4185 if (ISFCN (isymp->n_type)
4186 || ISTAG (isymp->n_sclass)
4187 || isymp->n_sclass == C_BLOCK
4188 || isymp->n_sclass == C_FCN)
4190 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4192 && indx < obj_raw_syment_count (input_bfd))
4194 /* We look forward through the symbol for
4195 the index of the next symbol we are going
4196 to include. I don't know if this is
4198 while (finfo->sym_indices[indx] < 0
4199 && indx < obj_raw_syment_count (input_bfd))
4201 if (indx >= obj_raw_syment_count (input_bfd))
4202 indx = output_index;
4204 indx = finfo->sym_indices[indx];
4205 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4210 indx = aux.x_sym.x_tagndx.l;
4211 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4215 symindx = finfo->sym_indices[indx];
4217 aux.x_sym.x_tagndx.l = 0;
4219 aux.x_sym.x_tagndx.l = symindx;
4224 /* Copy over the line numbers, unless we are stripping
4225 them. We do this on a symbol by symbol basis in
4226 order to more easily handle garbage collection. */
4227 if (CSECT_SYM_P (isymp->n_sclass)
4229 && isymp->n_numaux > 1
4230 && ISFCN (isymp->n_type)
4231 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4233 if (*lineno_counts == 0)
4234 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4237 asection *enclosing;
4238 unsigned int enc_count;
4239 bfd_signed_vma linoff;
4240 struct internal_lineno lin;
4247 /* Read in the enclosing section's line-number
4248 information, if we haven't already. */
4250 enclosing = xcoff_section_data (abfd, o)->enclosing;
4251 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4252 if (oline != enclosing)
4254 pos = enclosing->line_filepos;
4255 amt = linesz * enc_count;
4256 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4257 || (bfd_bread (finfo->linenos, amt, input_bfd)
4263 /* Copy across the first entry, adjusting its
4265 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4266 - enclosing->line_filepos);
4267 linp = finfo->linenos + linoff;
4268 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4269 lin.l_addr.l_symndx = *indexp;
4270 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4272 /* Copy the other entries, adjusting their addresses. */
4273 linpend = linp + *lineno_counts * linesz;
4274 offset = (o->output_section->vma
4277 for (linp += linesz; linp < linpend; linp += linesz)
4279 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4280 lin.l_addr.l_paddr += offset;
4281 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4284 /* Write out the entries we've just processed. */
4285 pos = (o->output_section->line_filepos
4286 + o->output_section->lineno_count * linesz);
4287 amt = linesz * *lineno_counts;
4288 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4289 || bfd_bwrite (finfo->linenos + linoff,
4290 amt, output_bfd) != amt)
4292 o->output_section->lineno_count += *lineno_counts;
4294 /* Record the offset of the symbol's line numbers
4295 in the output file. */
4296 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4300 struct internal_syment *iisp, *iispend;
4303 bfd_vma range_start, range_end;
4306 /* Update any C_BINCL or C_EINCL symbols
4307 that refer to a line number in the
4308 range we just output. */
4309 iisp = finfo->internal_syms;
4310 iispend = iisp + obj_raw_syment_count (input_bfd);
4311 iindp = finfo->sym_indices;
4312 oos = finfo->outsyms;
4313 range_start = enclosing->line_filepos + linoff;
4314 range_end = range_start + *lineno_counts * linesz;
4315 while (iisp < iispend)
4318 && (iisp->n_sclass == C_BINCL
4319 || iisp->n_sclass == C_EINCL)
4320 && iisp->n_value >= range_start
4321 && iisp->n_value < range_end)
4323 struct internal_syment iis;
4325 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4326 iis.n_value = (iisp->n_value
4329 bfd_coff_swap_sym_out (output_bfd,
4334 iiadd = 1 + iisp->n_numaux;
4336 oos += iiadd * osymesz;
4344 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4345 isymp->n_sclass, i, isymp->n_numaux,
4355 lineno_counts += add;
4359 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4360 symbol will be the first symbol in the next input file. In the
4361 normal case, this will save us from writing out the C_FILE symbol
4363 if (finfo->last_file_index != -1
4364 && (bfd_size_type) finfo->last_file_index >= syment_base)
4366 finfo->last_file.n_value = output_index;
4367 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4368 (void *) (finfo->outsyms
4369 + ((finfo->last_file_index - syment_base)
4373 /* Write the modified symbols to the output file. */
4374 if (outsym > finfo->outsyms)
4376 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4377 bfd_size_type amt = outsym - finfo->outsyms;
4378 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4379 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4382 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4383 + (outsym - finfo->outsyms) / osymesz)
4386 obj_raw_syment_count (output_bfd) = output_index;
4389 /* Don't let the linker relocation routines discard the symbols. */
4390 keep_syms = obj_coff_keep_syms (input_bfd);
4391 obj_coff_keep_syms (input_bfd) = TRUE;
4393 /* Relocate the contents of each section. */
4394 for (o = input_bfd->sections; o != NULL; o = o->next)
4398 if (! o->linker_mark)
4399 /* This section was omitted from the link. */
4402 if ((o->flags & SEC_HAS_CONTENTS) == 0
4404 || (o->flags & SEC_IN_MEMORY) != 0)
4407 /* We have set filepos correctly for the sections we created to
4408 represent csects, so bfd_get_section_contents should work. */
4409 if (coff_section_data (input_bfd, o) != NULL
4410 && coff_section_data (input_bfd, o)->contents != NULL)
4411 contents = coff_section_data (input_bfd, o)->contents;
4414 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4415 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4417 contents = finfo->contents;
4420 if ((o->flags & SEC_RELOC) != 0)
4423 struct internal_reloc *internal_relocs;
4424 struct internal_reloc *irel;
4426 struct internal_reloc *irelend;
4427 struct xcoff_link_hash_entry **rel_hash;
4430 /* Read in the relocs. */
4431 target_index = o->output_section->target_index;
4432 internal_relocs = (xcoff_read_internal_relocs
4433 (input_bfd, o, FALSE, finfo->external_relocs,
4435 (finfo->section_info[target_index].relocs
4436 + o->output_section->reloc_count)));
4437 if (internal_relocs == NULL)
4440 /* Call processor specific code to relocate the section
4442 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4446 finfo->internal_syms,
4447 xcoff_data (input_bfd)->csects))
4450 offset = o->output_section->vma + o->output_offset - o->vma;
4451 irel = internal_relocs;
4452 irelend = irel + o->reloc_count;
4453 rel_hash = (finfo->section_info[target_index].rel_hashes
4454 + o->output_section->reloc_count);
4455 for (; irel < irelend; irel++, rel_hash++)
4457 struct xcoff_link_hash_entry *h = NULL;
4458 struct internal_ldrel ldrel;
4462 /* Adjust the reloc address and symbol index. */
4464 irel->r_vaddr += offset;
4466 r_symndx = irel->r_symndx;
4471 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4473 if (r_symndx != -1 && finfo->info->strip != strip_all)
4476 && h->smclas != XMC_TD
4477 && (irel->r_type == R_TOC
4478 || irel->r_type == R_GL
4479 || irel->r_type == R_TCL
4480 || irel->r_type == R_TRL
4481 || irel->r_type == R_TRLA))
4483 /* This is a TOC relative reloc with a symbol
4484 attached. The symbol should be the one which
4485 this reloc is for. We want to make this
4486 reloc against the TOC address of the symbol,
4487 not the symbol itself. */
4488 BFD_ASSERT (h->toc_section != NULL);
4489 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4490 if (h->u.toc_indx != -1)
4491 irel->r_symndx = h->u.toc_indx;
4494 struct xcoff_toc_rel_hash *n;
4495 struct xcoff_link_section_info *si;
4499 n = bfd_alloc (finfo->output_bfd, amt);
4502 si = finfo->section_info + target_index;
4503 n->next = si->toc_rel_hashes;
4506 si->toc_rel_hashes = n;
4511 /* This is a global symbol. */
4513 irel->r_symndx = h->indx;
4516 /* This symbol is being written at the end
4517 of the file, and we do not yet know the
4518 symbol index. We save the pointer to the
4519 hash table entry in the rel_hash list.
4520 We set the indx field to -2 to indicate
4521 that this symbol must not be stripped. */
4530 indx = finfo->sym_indices[r_symndx];
4534 struct internal_syment *is;
4536 /* Relocations against a TC0 TOC anchor are
4537 automatically transformed to be against
4538 the TOC anchor in the output file. */
4539 is = finfo->internal_syms + r_symndx;
4540 if (is->n_sclass == C_HIDEXT
4541 && is->n_numaux > 0)
4544 union internal_auxent aux;
4548 obj_coff_external_syms (input_bfd))
4549 + ((r_symndx + is->n_numaux)
4551 bfd_coff_swap_aux_in (input_bfd, auxptr,
4552 is->n_type, is->n_sclass,
4556 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4557 && aux.x_csect.x_smclas == XMC_TC0)
4558 indx = finfo->toc_symindx;
4563 irel->r_symndx = indx;
4567 struct internal_syment *is;
4570 char buf[SYMNMLEN + 1];
4572 /* This reloc is against a symbol we are
4573 stripping. It would be possible to handle
4574 this case, but I don't think it's worth it. */
4575 is = finfo->internal_syms + r_symndx;
4577 name = (_bfd_coff_internal_syment_name
4578 (input_bfd, is, buf));
4583 if (! ((*finfo->info->callbacks->unattached_reloc)
4584 (finfo->info, name, input_bfd, o,
4591 switch (irel->r_type)
4595 || h->root.type == bfd_link_hash_defined
4596 || h->root.type == bfd_link_hash_defweak
4597 || h->root.type == bfd_link_hash_common)
4605 && (h->root.type == bfd_link_hash_defined
4606 || h->root.type == bfd_link_hash_defweak)
4607 && bfd_is_abs_section (h->root.u.def.section))
4609 /* This reloc needs to be copied into the .loader
4611 ldrel.l_vaddr = irel->r_vaddr;
4613 ldrel.l_symndx = -(bfd_size_type ) 1;
4615 || (h->root.type == bfd_link_hash_defined
4616 || h->root.type == bfd_link_hash_defweak
4617 || h->root.type == bfd_link_hash_common))
4622 sec = xcoff_data (input_bfd)->csects[r_symndx];
4623 else if (h->root.type == bfd_link_hash_common)
4624 sec = h->root.u.c.p->section;
4626 sec = h->root.u.def.section;
4627 sec = sec->output_section;
4629 if (strcmp (sec->name, ".text") == 0)
4631 else if (strcmp (sec->name, ".data") == 0)
4633 else if (strcmp (sec->name, ".bss") == 0)
4637 (*_bfd_error_handler)
4638 (_("%B: loader reloc in unrecognized section `%A'"),
4640 bfd_set_error (bfd_error_nonrepresentable_section);
4648 (*_bfd_error_handler)
4649 (_("%B: `%s' in loader reloc but not loader sym"),
4651 h->root.root.string);
4652 bfd_set_error (bfd_error_bad_value);
4655 ldrel.l_symndx = h->ldindx;
4657 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4658 ldrel.l_rsecnm = o->output_section->target_index;
4659 if (xcoff_hash_table (finfo->info)->textro
4660 && strcmp (o->output_section->name, ".text") == 0)
4662 (*_bfd_error_handler)
4663 (_("%B: loader reloc in read-only section %A"),
4664 input_bfd, o->output_section);
4665 bfd_set_error (bfd_error_invalid_operation);
4668 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4671 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4679 /* We should never need a .loader reloc for a TOC
4685 o->output_section->reloc_count += o->reloc_count;
4688 /* Write out the modified section contents. */
4689 if (! bfd_set_section_contents (output_bfd, o->output_section,
4690 contents, (file_ptr) o->output_offset,
4695 obj_coff_keep_syms (input_bfd) = keep_syms;
4697 if (! finfo->info->keep_memory)
4699 if (! _bfd_coff_free_symbols (input_bfd))
4709 /* Sort relocs by VMA. This is called via qsort. */
4712 xcoff_sort_relocs (const void * p1, const void * p2)
4714 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4715 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4717 if (r1->r_vaddr > r2->r_vaddr)
4719 else if (r1->r_vaddr < r2->r_vaddr)
4725 /* Return true if section SEC is a TOC section. */
4727 static inline bfd_boolean
4728 xcoff_toc_section_p (asection *sec)
4733 if (name[0] == '.' && name[1] == 't')
4737 if (name[3] == '0' && name[4] == 0)
4742 if (name[2] == 'd' && name[3] == 0)
4748 /* See if the link requires a TOC (it usually does!). If so, find a
4749 good place to put the TOC anchor csect, and write out the associated
4753 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *finfo)
4755 bfd_vma toc_start, toc_end, start, end, best_address;
4759 struct internal_syment irsym;
4760 union internal_auxent iraux;
4764 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
4765 index of a csect at the beginning of the TOC. */
4766 toc_start = ~(bfd_vma) 0;
4769 for (input_bfd = finfo->info->input_bfds;
4771 input_bfd = input_bfd->link_next)
4772 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4773 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4775 start = sec->output_section->vma + sec->output_offset;
4776 if (toc_start > start)
4779 section_index = sec->output_section->target_index;
4782 end = start + sec->size;
4787 /* There's no need for a TC0 symbol if we don't have a TOC. */
4788 if (toc_end < toc_start)
4790 xcoff_data (output_bfd)->toc = toc_start;
4794 if (toc_end - toc_start < 0x8000)
4795 /* Every TOC csect can be accessed from TOC_START. */
4796 best_address = toc_start;
4799 /* Find the lowest TOC csect that is still within range of TOC_END. */
4800 best_address = toc_end;
4801 for (input_bfd = finfo->info->input_bfds;
4803 input_bfd = input_bfd->link_next)
4804 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4805 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4807 start = sec->output_section->vma + sec->output_offset;
4808 if (start < best_address
4809 && start + 0x8000 >= toc_end)
4811 best_address = start;
4812 section_index = sec->output_section->target_index;
4816 /* Make sure that the start of the TOC is also within range. */
4817 if (best_address > toc_start + 0x8000)
4819 (*_bfd_error_handler)
4820 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
4822 (unsigned long) (toc_end - toc_start));
4823 bfd_set_error (bfd_error_file_too_big);
4828 /* Record the chosen TOC value. */
4829 finfo->toc_symindx = obj_raw_syment_count (output_bfd);
4830 xcoff_data (output_bfd)->toc = best_address;
4831 xcoff_data (output_bfd)->sntoc = section_index;
4833 /* Fill out the TC0 symbol. */
4834 if (!bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &irsym, "TOC"))
4836 irsym.n_value = best_address;
4837 irsym.n_scnum = section_index;
4838 irsym.n_sclass = C_HIDEXT;
4839 irsym.n_type = T_NULL;
4841 bfd_coff_swap_sym_out (output_bfd, &irsym, finfo->outsyms);
4843 /* Fill out the auxillary csect information. */
4844 memset (&iraux, 0, sizeof iraux);
4845 iraux.x_csect.x_smtyp = XTY_SD;
4846 iraux.x_csect.x_smclas = XMC_TC0;
4847 iraux.x_csect.x_scnlen.l = 0;
4848 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
4849 finfo->outsyms + bfd_coff_symesz (output_bfd));
4851 /* Write the contents to the file. */
4852 pos = obj_sym_filepos (output_bfd);
4853 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
4854 size = 2 * bfd_coff_symesz (output_bfd);
4855 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4856 || bfd_bwrite (finfo->outsyms, size, output_bfd) != size)
4858 obj_raw_syment_count (output_bfd) += 2;
4863 /* Write out a non-XCOFF global symbol. */
4866 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4868 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4871 struct internal_syment isym;
4872 union internal_auxent aux;
4877 output_bfd = finfo->output_bfd;
4878 outsym = finfo->outsyms;
4880 if (h->root.type == bfd_link_hash_warning)
4882 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4883 if (h->root.type == bfd_link_hash_new)
4887 /* If this symbol was garbage collected, just skip it. */
4888 if (xcoff_hash_table (finfo->info)->gc
4889 && (h->flags & XCOFF_MARK) == 0)
4892 /* If we need a .loader section entry, write it out. */
4893 if (h->ldsym != NULL)
4895 struct internal_ldsym *ldsym;
4900 if (h->root.type == bfd_link_hash_undefined
4901 || h->root.type == bfd_link_hash_undefweak)
4905 ldsym->l_scnum = N_UNDEF;
4906 ldsym->l_smtype = XTY_ER;
4907 impbfd = h->root.u.undef.abfd;
4910 else if (h->root.type == bfd_link_hash_defined
4911 || h->root.type == bfd_link_hash_defweak)
4915 sec = h->root.u.def.section;
4916 ldsym->l_value = (sec->output_section->vma
4917 + sec->output_offset
4918 + h->root.u.def.value);
4919 ldsym->l_scnum = sec->output_section->target_index;
4920 ldsym->l_smtype = XTY_SD;
4921 impbfd = sec->owner;
4927 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4928 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4929 || (h->flags & XCOFF_IMPORT) != 0)
4931 Import symbols are defined so the check above will make
4932 the l_smtype XTY_SD. But this is not correct, it should
4934 ldsym->l_smtype |= L_IMPORT;
4936 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4937 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4938 || (h->flags & XCOFF_EXPORT) != 0)
4939 ldsym->l_smtype |= L_EXPORT;
4941 if ((h->flags & XCOFF_ENTRY) != 0)
4942 ldsym->l_smtype |= L_ENTRY;
4944 if ((h->flags & XCOFF_RTINIT) != 0)
4945 ldsym->l_smtype = XTY_SD;
4947 ldsym->l_smclas = h->smclas;
4949 if (ldsym->l_smtype & L_IMPORT)
4951 if ((h->root.type == bfd_link_hash_defined
4952 || h->root.type == bfd_link_hash_defweak)
4953 && (h->root.u.def.value != 0))
4954 ldsym->l_smclas = XMC_XO;
4956 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4957 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4958 ldsym->l_smclas = XMC_SV3264;
4960 else if (h->flags & XCOFF_SYSCALL32)
4961 ldsym->l_smclas = XMC_SV;
4963 else if (h->flags & XCOFF_SYSCALL64)
4964 ldsym->l_smclas = XMC_SV64;
4967 if (ldsym->l_ifile == -(bfd_size_type) 1)
4971 else if (ldsym->l_ifile == 0)
4973 if ((ldsym->l_smtype & L_IMPORT) == 0)
4975 else if (impbfd == NULL)
4979 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4980 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4986 BFD_ASSERT (h->ldindx >= 0);
4988 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4991 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4995 /* If this symbol needs global linkage code, write it out. */
4996 if (h->root.type == bfd_link_hash_defined
4997 && (h->root.u.def.section
4998 == xcoff_hash_table (finfo->info)->linkage_section))
5004 p = h->root.u.def.section->contents + h->root.u.def.value;
5006 /* The first instruction in the global linkage code loads a
5007 specific TOC element. */
5008 tocoff = (h->descriptor->toc_section->output_section->vma
5009 + h->descriptor->toc_section->output_offset
5010 - xcoff_data (output_bfd)->toc);
5012 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5013 tocoff += h->descriptor->u.toc_offset;
5015 /* The first instruction in the glink code needs to be
5016 cooked to to hold the correct offset in the toc. The
5017 rest are just output raw. */
5018 bfd_put_32 (output_bfd,
5019 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5021 /* Start with i == 1 to get past the first instruction done above
5022 The /4 is because the glink code is in bytes and we are going
5024 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5025 bfd_put_32 (output_bfd,
5026 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5030 /* If we created a TOC entry for this symbol, write out the required
5032 if ((h->flags & XCOFF_SET_TOC) != 0)
5037 struct internal_reloc *irel;
5038 struct internal_ldrel ldrel;
5039 struct internal_syment irsym;
5040 union internal_auxent iraux;
5042 tocsec = h->toc_section;
5043 osec = tocsec->output_section;
5044 oindx = osec->target_index;
5045 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5046 irel->r_vaddr = (osec->vma
5047 + tocsec->output_offset
5051 irel->r_symndx = h->indx;
5055 irel->r_symndx = obj_raw_syment_count (output_bfd);
5058 BFD_ASSERT (h->ldindx >= 0);
5060 /* Initialize the aux union here instead of closer to when it is
5061 written out below because the length of the csect depends on
5062 whether the output is 32 or 64 bit. */
5063 memset (&iraux, 0, sizeof iraux);
5064 iraux.x_csect.x_smtyp = XTY_SD;
5065 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5066 iraux.x_csect.x_smclas = XMC_TC;
5068 /* 32 bit uses a 32 bit R_POS to do the relocations
5069 64 bit uses a 64 bit R_POS to do the relocations
5071 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5073 Which one is determined by the backend. */
5074 if (bfd_xcoff_is_xcoff64 (output_bfd))
5077 iraux.x_csect.x_scnlen.l = 8;
5079 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5082 iraux.x_csect.x_scnlen.l = 4;
5087 irel->r_type = R_POS;
5088 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5089 ++osec->reloc_count;
5091 ldrel.l_vaddr = irel->r_vaddr;
5092 ldrel.l_symndx = h->ldindx;
5093 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
5094 ldrel.l_rsecnm = oindx;
5095 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5096 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5098 /* We need to emit a symbol to define a csect which holds
5100 if (finfo->info->strip != strip_all)
5102 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
5103 &irsym, h->root.root.string);
5107 irsym.n_value = irel->r_vaddr;
5108 irsym.n_scnum = osec->target_index;
5109 irsym.n_sclass = C_HIDEXT;
5110 irsym.n_type = T_NULL;
5113 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5114 outsym += bfd_coff_symesz (output_bfd);
5116 /* Note : iraux is initialized above. */
5117 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5118 0, 1, (void *) outsym);
5119 outsym += bfd_coff_auxesz (output_bfd);
5123 /* We aren't going to write out the symbols below, so we
5124 need to write them out now. */
5125 pos = obj_sym_filepos (output_bfd);
5126 pos += (obj_raw_syment_count (output_bfd)
5127 * bfd_coff_symesz (output_bfd));
5128 amt = outsym - finfo->outsyms;
5129 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5130 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5132 obj_raw_syment_count (output_bfd) +=
5133 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5135 outsym = finfo->outsyms;
5140 /* If this symbol is a specially defined function descriptor, write
5141 it out. The first word is the address of the function code
5142 itself, the second word is the address of the TOC, and the third
5146 The addresses for the 32 bit will take 4 bytes and the addresses
5147 for 64 bit will take 8 bytes. Similar for the relocs. This type
5148 of logic was also done above to create a TOC entry in
5149 xcoff_write_global_symbol. */
5150 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5151 && h->root.type == bfd_link_hash_defined
5152 && (h->root.u.def.section
5153 == xcoff_hash_table (finfo->info)->descriptor_section))
5159 struct xcoff_link_hash_entry *hentry;
5161 struct internal_reloc *irel;
5162 struct internal_ldrel ldrel;
5164 unsigned int reloc_size, byte_size;
5166 if (bfd_xcoff_is_xcoff64 (output_bfd))
5171 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5179 sec = h->root.u.def.section;
5180 osec = sec->output_section;
5181 oindx = osec->target_index;
5182 p = sec->contents + h->root.u.def.value;
5184 hentry = h->descriptor;
5185 BFD_ASSERT (hentry != NULL
5186 && (hentry->root.type == bfd_link_hash_defined
5187 || hentry->root.type == bfd_link_hash_defweak));
5188 esec = hentry->root.u.def.section;
5190 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5191 irel->r_vaddr = (osec->vma
5192 + sec->output_offset
5193 + h->root.u.def.value);
5194 irel->r_symndx = esec->output_section->target_index;
5195 irel->r_type = R_POS;
5196 irel->r_size = reloc_size;
5197 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5198 ++osec->reloc_count;
5200 ldrel.l_vaddr = irel->r_vaddr;
5201 if (strcmp (esec->output_section->name, ".text") == 0)
5203 else if (strcmp (esec->output_section->name, ".data") == 0)
5205 else if (strcmp (esec->output_section->name, ".bss") == 0)
5209 (*_bfd_error_handler)
5210 (_("%s: loader reloc in unrecognized section `%s'"),
5211 bfd_get_filename (output_bfd),
5212 esec->output_section->name);
5213 bfd_set_error (bfd_error_nonrepresentable_section);
5216 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5217 ldrel.l_rsecnm = oindx;
5218 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5219 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5221 /* There are three items to write out,
5222 the address of the code
5223 the address of the toc anchor
5224 the environment pointer.
5225 We are ignoring the environment pointer. So set it to zero. */
5226 if (bfd_xcoff_is_xcoff64 (output_bfd))
5228 bfd_put_64 (output_bfd,
5229 (esec->output_section->vma + esec->output_offset
5230 + hentry->root.u.def.value),
5232 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5233 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5238 This logic was already called above so the error case where
5239 the backend is neither has already been checked. */
5240 bfd_put_32 (output_bfd,
5241 (esec->output_section->vma + esec->output_offset
5242 + hentry->root.u.def.value),
5244 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5245 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5248 tsec = coff_section_from_bfd_index (output_bfd,
5249 xcoff_data (output_bfd)->sntoc);
5252 irel->r_vaddr = (osec->vma
5253 + sec->output_offset
5254 + h->root.u.def.value
5256 irel->r_symndx = tsec->output_section->target_index;
5257 irel->r_type = R_POS;
5258 irel->r_size = reloc_size;
5259 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5260 ++osec->reloc_count;
5262 ldrel.l_vaddr = irel->r_vaddr;
5263 if (strcmp (tsec->output_section->name, ".text") == 0)
5265 else if (strcmp (tsec->output_section->name, ".data") == 0)
5267 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5271 (*_bfd_error_handler)
5272 (_("%s: loader reloc in unrecognized section `%s'"),
5273 bfd_get_filename (output_bfd),
5274 tsec->output_section->name);
5275 bfd_set_error (bfd_error_nonrepresentable_section);
5278 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5279 ldrel.l_rsecnm = oindx;
5280 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5281 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5284 if (h->indx >= 0 || finfo->info->strip == strip_all)
5286 BFD_ASSERT (outsym == finfo->outsyms);
5291 && (finfo->info->strip == strip_all
5292 || (finfo->info->strip == strip_some
5293 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5294 FALSE, FALSE) == NULL)))
5296 BFD_ASSERT (outsym == finfo->outsyms);
5301 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5303 BFD_ASSERT (outsym == finfo->outsyms);
5307 memset (&aux, 0, sizeof aux);
5309 h->indx = obj_raw_syment_count (output_bfd);
5311 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5312 h->root.root.string);
5316 if (h->root.type == bfd_link_hash_undefined
5317 || h->root.type == bfd_link_hash_undefweak)
5320 isym.n_scnum = N_UNDEF;
5321 if (h->root.type == bfd_link_hash_undefweak
5322 && C_WEAKEXT == C_AIX_WEAKEXT)
5323 isym.n_sclass = C_WEAKEXT;
5325 isym.n_sclass = C_EXT;
5326 aux.x_csect.x_smtyp = XTY_ER;
5328 else if ((h->root.type == bfd_link_hash_defined
5329 || h->root.type == bfd_link_hash_defweak)
5330 && h->smclas == XMC_XO)
5332 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5333 isym.n_value = h->root.u.def.value;
5334 isym.n_scnum = N_UNDEF;
5335 if (h->root.type == bfd_link_hash_undefweak
5336 && C_WEAKEXT == C_AIX_WEAKEXT)
5337 isym.n_sclass = C_WEAKEXT;
5339 isym.n_sclass = C_EXT;
5340 aux.x_csect.x_smtyp = XTY_ER;
5342 else if (h->root.type == bfd_link_hash_defined
5343 || h->root.type == bfd_link_hash_defweak)
5345 struct xcoff_link_size_list *l;
5347 isym.n_value = (h->root.u.def.section->output_section->vma
5348 + h->root.u.def.section->output_offset
5349 + h->root.u.def.value);
5350 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5351 isym.n_scnum = N_ABS;
5353 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5354 isym.n_sclass = C_HIDEXT;
5355 aux.x_csect.x_smtyp = XTY_SD;
5357 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5359 for (l = xcoff_hash_table (finfo->info)->size_list;
5365 aux.x_csect.x_scnlen.l = l->size;
5371 else if (h->root.type == bfd_link_hash_common)
5373 isym.n_value = (h->root.u.c.p->section->output_section->vma
5374 + h->root.u.c.p->section->output_offset);
5375 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5376 isym.n_sclass = C_EXT;
5377 aux.x_csect.x_smtyp = XTY_CM;
5378 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5383 isym.n_type = T_NULL;
5386 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5387 outsym += bfd_coff_symesz (output_bfd);
5389 aux.x_csect.x_smclas = h->smclas;
5390 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5392 outsym += bfd_coff_auxesz (output_bfd);
5394 if ((h->root.type == bfd_link_hash_defined
5395 || h->root.type == bfd_link_hash_defweak)
5396 && h->smclas != XMC_XO)
5398 /* We just output an SD symbol. Now output an LD symbol. */
5401 if (h->root.type == bfd_link_hash_undefweak
5402 && C_WEAKEXT == C_AIX_WEAKEXT)
5403 isym.n_sclass = C_WEAKEXT;
5405 isym.n_sclass = C_EXT;
5406 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5407 outsym += bfd_coff_symesz (output_bfd);
5409 aux.x_csect.x_smtyp = XTY_LD;
5410 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5411 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5413 outsym += bfd_coff_auxesz (output_bfd);
5416 pos = obj_sym_filepos (output_bfd);
5417 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5418 amt = outsym - finfo->outsyms;
5419 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5420 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5422 obj_raw_syment_count (output_bfd) +=
5423 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5428 /* Handle a link order which is supposed to generate a reloc. */
5431 xcoff_reloc_link_order (bfd *output_bfd,
5432 struct xcoff_final_link_info *finfo,
5433 asection *output_section,
5434 struct bfd_link_order *link_order)
5436 reloc_howto_type *howto;
5437 struct xcoff_link_hash_entry *h;
5441 struct internal_reloc *irel;
5442 struct xcoff_link_hash_entry **rel_hash_ptr;
5443 struct internal_ldrel ldrel;
5445 if (link_order->type == bfd_section_reloc_link_order)
5446 /* We need to somehow locate a symbol in the right section. The
5447 symbol must either have a value of zero, or we must adjust
5448 the addend by the value of the symbol. FIXME: Write this
5449 when we need it. The old linker couldn't handle this anyhow. */
5452 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5455 bfd_set_error (bfd_error_bad_value);
5459 h = ((struct xcoff_link_hash_entry *)
5460 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5461 link_order->u.reloc.p->u.name,
5462 FALSE, FALSE, TRUE));
5465 if (! ((*finfo->info->callbacks->unattached_reloc)
5466 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5471 if (h->root.type == bfd_link_hash_common)
5473 hsec = h->root.u.c.p->section;
5476 else if (h->root.type == bfd_link_hash_defined
5477 || h->root.type == bfd_link_hash_defweak)
5479 hsec = h->root.u.def.section;
5480 hval = h->root.u.def.value;
5488 addend = link_order->u.reloc.p->addend;
5490 addend += (hsec->output_section->vma
5491 + hsec->output_offset
5498 bfd_reloc_status_type rstat;
5501 size = bfd_get_reloc_size (howto);
5502 buf = bfd_zmalloc (size);
5506 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5512 case bfd_reloc_outofrange:
5514 case bfd_reloc_overflow:
5515 if (! ((*finfo->info->callbacks->reloc_overflow)
5516 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5517 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5524 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5525 (file_ptr) link_order->offset, size);
5531 /* Store the reloc information in the right place. It will get
5532 swapped and written out at the end of the final_link routine. */
5533 irel = (finfo->section_info[output_section->target_index].relocs
5534 + output_section->reloc_count);
5535 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5536 + output_section->reloc_count);
5538 memset (irel, 0, sizeof (struct internal_reloc));
5539 *rel_hash_ptr = NULL;
5541 irel->r_vaddr = output_section->vma + link_order->offset;
5544 irel->r_symndx = h->indx;
5547 /* Set the index to -2 to force this symbol to get written out. */
5553 irel->r_type = howto->type;
5554 irel->r_size = howto->bitsize - 1;
5555 if (howto->complain_on_overflow == complain_overflow_signed)
5556 irel->r_size |= 0x80;
5558 ++output_section->reloc_count;
5560 /* Now output the reloc to the .loader section. */
5562 ldrel.l_vaddr = irel->r_vaddr;
5566 const char *secname;
5568 secname = hsec->output_section->name;
5570 if (strcmp (secname, ".text") == 0)
5572 else if (strcmp (secname, ".data") == 0)
5574 else if (strcmp (secname, ".bss") == 0)
5578 (*_bfd_error_handler)
5579 (_("%s: loader reloc in unrecognized section `%s'"),
5580 bfd_get_filename (output_bfd), secname);
5581 bfd_set_error (bfd_error_nonrepresentable_section);
5589 (*_bfd_error_handler)
5590 (_("%s: `%s' in loader reloc but not loader sym"),
5591 bfd_get_filename (output_bfd),
5592 h->root.root.string);
5593 bfd_set_error (bfd_error_bad_value);
5596 ldrel.l_symndx = h->ldindx;
5599 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5600 ldrel.l_rsecnm = output_section->target_index;
5601 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5602 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5607 /* Do the final link step. */
5610 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5612 bfd_size_type symesz;
5613 struct xcoff_final_link_info finfo;
5615 struct bfd_link_order *p;
5616 bfd_size_type max_contents_size;
5617 bfd_size_type max_sym_count;
5618 bfd_size_type max_lineno_count;
5619 bfd_size_type max_reloc_count;
5620 bfd_size_type max_output_reloc_count;
5621 file_ptr rel_filepos;
5623 file_ptr line_filepos;
5624 unsigned int linesz;
5626 bfd_byte *external_relocs = NULL;
5627 char strbuf[STRING_SIZE_SIZE];
5632 abfd->flags |= DYNAMIC;
5634 symesz = bfd_coff_symesz (abfd);
5637 finfo.output_bfd = abfd;
5638 finfo.strtab = NULL;
5639 finfo.section_info = NULL;
5640 finfo.last_file_index = -1;
5641 finfo.toc_symindx = -1;
5642 finfo.internal_syms = NULL;
5643 finfo.sym_indices = NULL;
5644 finfo.outsyms = NULL;
5645 finfo.linenos = NULL;
5646 finfo.contents = NULL;
5647 finfo.external_relocs = NULL;
5649 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5650 + bfd_xcoff_ldhdrsz (abfd));
5651 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5652 + bfd_xcoff_ldhdrsz(abfd)
5653 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5654 * bfd_xcoff_ldsymsz(abfd)));
5656 xcoff_data (abfd)->coff.link_info = info;
5658 finfo.strtab = _bfd_stringtab_init ();
5659 if (finfo.strtab == NULL)
5662 /* Count the relocation entries required for the output file.
5663 (We've already counted the line numbers.) Determine a few
5665 max_contents_size = 0;
5666 max_lineno_count = 0;
5667 max_reloc_count = 0;
5668 for (o = abfd->sections; o != NULL; o = o->next)
5671 for (p = o->map_head.link_order; p != NULL; p = p->next)
5673 if (p->type == bfd_indirect_link_order)
5677 sec = p->u.indirect.section;
5679 /* Mark all sections which are to be included in the
5680 link. This will normally be every section. We need
5681 to do this so that we can identify any sections which
5682 the linker has decided to not include. */
5683 sec->linker_mark = TRUE;
5685 o->reloc_count += sec->reloc_count;
5687 if (sec->rawsize > max_contents_size)
5688 max_contents_size = sec->rawsize;
5689 if (sec->size > max_contents_size)
5690 max_contents_size = sec->size;
5691 if (coff_section_data (sec->owner, sec) != NULL
5692 && xcoff_section_data (sec->owner, sec) != NULL
5693 && (xcoff_section_data (sec->owner, sec)->lineno_count
5694 > max_lineno_count))
5696 xcoff_section_data (sec->owner, sec)->lineno_count;
5697 if (sec->reloc_count > max_reloc_count)
5698 max_reloc_count = sec->reloc_count;
5700 else if (p->type == bfd_section_reloc_link_order
5701 || p->type == bfd_symbol_reloc_link_order)
5706 /* Compute the file positions for all the sections. */
5707 if (abfd->output_has_begun)
5709 if (xcoff_hash_table (info)->file_align != 0)
5716 file_align = xcoff_hash_table (info)->file_align;
5717 if (file_align != 0)
5719 bfd_boolean saw_contents;
5723 /* Insert .pad sections before every section which has
5724 contents and is loaded, if it is preceded by some other
5725 section which has contents and is loaded. */
5726 saw_contents = TRUE;
5727 for (o = abfd->sections; o != NULL; o = o->next)
5729 if (strcmp (o->name, ".pad") == 0)
5730 saw_contents = FALSE;
5731 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5732 && (o->flags & SEC_LOAD) != 0)
5735 saw_contents = TRUE;
5740 /* Create a pad section and place it before the section
5741 that needs padding. This requires unlinking and
5742 relinking the bfd's section list. */
5744 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5746 n->alignment_power = 0;
5748 bfd_section_list_remove (abfd, n);
5749 bfd_section_list_insert_before (abfd, o, n);
5750 saw_contents = FALSE;
5755 /* Reset the section indices after inserting the new
5758 for (o = abfd->sections; o != NULL; o = o->next)
5761 o->target_index = indx;
5763 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5765 /* Work out appropriate sizes for the .pad sections to force
5766 each section to land on a page boundary. This bit of
5767 code knows what compute_section_file_positions is going
5769 sofar = bfd_coff_filhsz (abfd);
5770 sofar += bfd_coff_aoutsz (abfd);
5771 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5772 for (o = abfd->sections; o != NULL; o = o->next)
5773 if ((bfd_xcoff_is_reloc_count_overflow
5774 (abfd, (bfd_vma) o->reloc_count))
5775 || (bfd_xcoff_is_lineno_count_overflow
5776 (abfd, (bfd_vma) o->lineno_count)))
5777 /* 64 does not overflow, need to check if 32 does */
5778 sofar += bfd_coff_scnhsz (abfd);
5780 for (o = abfd->sections; o != NULL; o = o->next)
5782 if (strcmp (o->name, ".pad") == 0)
5786 BFD_ASSERT (o->size == 0);
5787 pageoff = sofar & (file_align - 1);
5790 o->size = file_align - pageoff;
5791 sofar += file_align - pageoff;
5792 o->flags |= SEC_HAS_CONTENTS;
5797 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5798 sofar += BFD_ALIGN (o->size,
5799 1 << o->alignment_power);
5804 if (! bfd_coff_compute_section_file_positions (abfd))
5808 /* Allocate space for the pointers we need to keep for the relocs. */
5812 /* We use section_count + 1, rather than section_count, because
5813 the target_index fields are 1 based. */
5814 amt = abfd->section_count + 1;
5815 amt *= sizeof (struct xcoff_link_section_info);
5816 finfo.section_info = bfd_malloc (amt);
5817 if (finfo.section_info == NULL)
5819 for (i = 0; i <= abfd->section_count; i++)
5821 finfo.section_info[i].relocs = NULL;
5822 finfo.section_info[i].rel_hashes = NULL;
5823 finfo.section_info[i].toc_rel_hashes = NULL;
5827 /* Set the file positions for the relocs. */
5828 rel_filepos = obj_relocbase (abfd);
5829 relsz = bfd_coff_relsz (abfd);
5830 max_output_reloc_count = 0;
5831 for (o = abfd->sections; o != NULL; o = o->next)
5833 if (o->reloc_count == 0)
5837 /* A stripped file has no relocs. However, we still
5838 allocate the buffers, so that later code doesn't have to
5839 worry about whether we are stripping or not. */
5840 if (info->strip == strip_all)
5844 o->flags |= SEC_RELOC;
5845 o->rel_filepos = rel_filepos;
5846 rel_filepos += o->reloc_count * relsz;
5849 /* We don't know the indices of global symbols until we have
5850 written out all the local symbols. For each section in
5851 the output file, we keep an array of pointers to hash
5852 table entries. Each entry in the array corresponds to a
5853 reloc. When we find a reloc against a global symbol, we
5854 set the corresponding entry in this array so that we can
5855 fix up the symbol index after we have written out all the
5858 Because of this problem, we also keep the relocs in
5859 memory until the end of the link. This wastes memory.
5860 We could backpatch the file later, I suppose, although it
5862 amt = o->reloc_count;
5863 amt *= sizeof (struct internal_reloc);
5864 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5866 amt = o->reloc_count;
5867 amt *= sizeof (struct xcoff_link_hash_entry *);
5868 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5870 if (finfo.section_info[o->target_index].relocs == NULL
5871 || finfo.section_info[o->target_index].rel_hashes == NULL)
5874 if (o->reloc_count > max_output_reloc_count)
5875 max_output_reloc_count = o->reloc_count;
5879 /* We now know the size of the relocs, so we can determine the file
5880 positions of the line numbers. */
5881 line_filepos = rel_filepos;
5882 finfo.line_filepos = line_filepos;
5883 linesz = bfd_coff_linesz (abfd);
5884 for (o = abfd->sections; o != NULL; o = o->next)
5886 if (o->lineno_count == 0)
5887 o->line_filepos = 0;
5890 o->line_filepos = line_filepos;
5891 line_filepos += o->lineno_count * linesz;
5894 /* Reset the reloc and lineno counts, so that we can use them to
5895 count the number of entries we have output so far. */
5897 o->lineno_count = 0;
5900 obj_sym_filepos (abfd) = line_filepos;
5902 /* Figure out the largest number of symbols in an input BFD. Take
5903 the opportunity to clear the output_has_begun fields of all the
5904 input BFD's. We want at least 6 symbols, since that is the
5905 number which xcoff_write_global_symbol may need. */
5907 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5911 sub->output_has_begun = FALSE;
5912 sz = obj_raw_syment_count (sub);
5913 if (sz > max_sym_count)
5917 /* Allocate some buffers used while linking. */
5918 amt = max_sym_count * sizeof (struct internal_syment);
5919 finfo.internal_syms = bfd_malloc (amt);
5921 amt = max_sym_count * sizeof (long);
5922 finfo.sym_indices = bfd_malloc (amt);
5924 amt = (max_sym_count + 1) * symesz;
5925 finfo.outsyms = bfd_malloc (amt);
5927 amt = max_lineno_count * bfd_coff_linesz (abfd);
5928 finfo.linenos = bfd_malloc (amt);
5930 amt = max_contents_size;
5931 finfo.contents = bfd_malloc (amt);
5933 amt = max_reloc_count * relsz;
5934 finfo.external_relocs = bfd_malloc (amt);
5936 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5937 || (finfo.sym_indices == NULL && max_sym_count > 0)
5938 || finfo.outsyms == NULL
5939 || (finfo.linenos == NULL && max_lineno_count > 0)
5940 || (finfo.contents == NULL && max_contents_size > 0)
5941 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5944 obj_raw_syment_count (abfd) = 0;
5946 /* Find a TOC symbol, if we need one. */
5947 if (!xcoff_find_tc0 (abfd, &finfo))
5950 /* We now know the position of everything in the file, except that
5951 we don't know the size of the symbol table and therefore we don't
5952 know where the string table starts. We just build the string
5953 table in memory as we go along. We process all the relocations
5954 for a single input file at once. */
5955 for (o = abfd->sections; o != NULL; o = o->next)
5957 for (p = o->map_head.link_order; p != NULL; p = p->next)
5959 if (p->type == bfd_indirect_link_order
5960 && p->u.indirect.section->owner->xvec == abfd->xvec)
5962 sub = p->u.indirect.section->owner;
5963 if (! sub->output_has_begun)
5965 if (! xcoff_link_input_bfd (&finfo, sub))
5967 sub->output_has_begun = TRUE;
5970 else if (p->type == bfd_section_reloc_link_order
5971 || p->type == bfd_symbol_reloc_link_order)
5973 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5978 if (! _bfd_default_link_order (abfd, info, o, p))
5984 /* Free up the buffers used by xcoff_link_input_bfd. */
5985 if (finfo.internal_syms != NULL)
5987 free (finfo.internal_syms);
5988 finfo.internal_syms = NULL;
5990 if (finfo.sym_indices != NULL)
5992 free (finfo.sym_indices);
5993 finfo.sym_indices = NULL;
5995 if (finfo.linenos != NULL)
5997 free (finfo.linenos);
5998 finfo.linenos = NULL;
6000 if (finfo.contents != NULL)
6002 free (finfo.contents);
6003 finfo.contents = NULL;
6005 if (finfo.external_relocs != NULL)
6007 free (finfo.external_relocs);
6008 finfo.external_relocs = NULL;
6011 /* The value of the last C_FILE symbol is supposed to be -1. Write
6013 if (finfo.last_file_index != -1)
6015 finfo.last_file.n_value = -(bfd_vma) 1;
6016 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
6017 (void *) finfo.outsyms);
6018 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
6019 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6020 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
6024 /* Write out all the global symbols which do not come from XCOFF
6026 xcoff_link_hash_traverse (xcoff_hash_table (info),
6027 xcoff_write_global_symbol,
6030 if (finfo.outsyms != NULL)
6032 free (finfo.outsyms);
6033 finfo.outsyms = NULL;
6036 /* Now that we have written out all the global symbols, we know the
6037 symbol indices to use for relocs against them, and we can finally
6038 write out the relocs. */
6039 amt = max_output_reloc_count * relsz;
6040 external_relocs = bfd_malloc (amt);
6041 if (external_relocs == NULL && max_output_reloc_count != 0)
6044 for (o = abfd->sections; o != NULL; o = o->next)
6046 struct internal_reloc *irel;
6047 struct internal_reloc *irelend;
6048 struct xcoff_link_hash_entry **rel_hash;
6049 struct xcoff_toc_rel_hash *toc_rel_hash;
6051 bfd_size_type rel_size;
6053 /* A stripped file has no relocs. */
6054 if (info->strip == strip_all)
6060 if (o->reloc_count == 0)
6063 irel = finfo.section_info[o->target_index].relocs;
6064 irelend = irel + o->reloc_count;
6065 rel_hash = finfo.section_info[o->target_index].rel_hashes;
6066 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6068 if (*rel_hash != NULL)
6070 if ((*rel_hash)->indx < 0)
6072 if (! ((*info->callbacks->unattached_reloc)
6073 (info, (*rel_hash)->root.root.string,
6074 NULL, o, irel->r_vaddr)))
6076 (*rel_hash)->indx = 0;
6078 irel->r_symndx = (*rel_hash)->indx;
6082 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
6083 toc_rel_hash != NULL;
6084 toc_rel_hash = toc_rel_hash->next)
6086 if (toc_rel_hash->h->u.toc_indx < 0)
6088 if (! ((*info->callbacks->unattached_reloc)
6089 (info, toc_rel_hash->h->root.root.string,
6090 NULL, o, toc_rel_hash->rel->r_vaddr)))
6092 toc_rel_hash->h->u.toc_indx = 0;
6094 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6097 /* XCOFF requires that the relocs be sorted by address. We tend
6098 to produce them in the order in which their containing csects
6099 appear in the symbol table, which is not necessarily by
6100 address. So we sort them here. There may be a better way to
6102 qsort ((void *) finfo.section_info[o->target_index].relocs,
6103 o->reloc_count, sizeof (struct internal_reloc),
6106 irel = finfo.section_info[o->target_index].relocs;
6107 irelend = irel + o->reloc_count;
6108 erel = external_relocs;
6109 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6110 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6112 rel_size = relsz * o->reloc_count;
6113 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6114 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6118 if (external_relocs != NULL)
6120 free (external_relocs);
6121 external_relocs = NULL;
6124 /* Free up the section information. */
6125 if (finfo.section_info != NULL)
6129 for (i = 0; i < abfd->section_count; i++)
6131 if (finfo.section_info[i].relocs != NULL)
6132 free (finfo.section_info[i].relocs);
6133 if (finfo.section_info[i].rel_hashes != NULL)
6134 free (finfo.section_info[i].rel_hashes);
6136 free (finfo.section_info);
6137 finfo.section_info = NULL;
6140 /* Write out the loader section contents. */
6141 BFD_ASSERT ((bfd_byte *) finfo.ldrel
6142 == (xcoff_hash_table (info)->loader_section->contents
6143 + xcoff_hash_table (info)->ldhdr.l_impoff));
6144 o = xcoff_hash_table (info)->loader_section;
6145 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
6146 (file_ptr) o->output_offset, o->size))
6149 /* Write out the magic sections. */
6150 o = xcoff_hash_table (info)->linkage_section;
6152 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6153 (file_ptr) o->output_offset,
6156 o = xcoff_hash_table (info)->toc_section;
6158 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6159 (file_ptr) o->output_offset,
6162 o = xcoff_hash_table (info)->descriptor_section;
6164 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6165 (file_ptr) o->output_offset,
6169 /* Write out the string table. */
6170 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6171 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6174 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
6176 amt = STRING_SIZE_SIZE;
6177 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6179 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
6182 _bfd_stringtab_free (finfo.strtab);
6184 /* Write out the debugging string table. */
6185 o = xcoff_hash_table (info)->debug_section;
6188 struct bfd_strtab_hash *debug_strtab;
6190 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6191 BFD_ASSERT (o->output_section->size - o->output_offset
6192 >= _bfd_stringtab_size (debug_strtab));
6193 pos = o->output_section->filepos + o->output_offset;
6194 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6196 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6200 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6201 not try to write out the symbols. */
6202 bfd_get_symcount (abfd) = 0;
6207 if (finfo.strtab != NULL)
6208 _bfd_stringtab_free (finfo.strtab);
6210 if (finfo.section_info != NULL)
6214 for (i = 0; i < abfd->section_count; i++)
6216 if (finfo.section_info[i].relocs != NULL)
6217 free (finfo.section_info[i].relocs);
6218 if (finfo.section_info[i].rel_hashes != NULL)
6219 free (finfo.section_info[i].rel_hashes);
6221 free (finfo.section_info);
6224 if (finfo.internal_syms != NULL)
6225 free (finfo.internal_syms);
6226 if (finfo.sym_indices != NULL)
6227 free (finfo.sym_indices);
6228 if (finfo.outsyms != NULL)
6229 free (finfo.outsyms);
6230 if (finfo.linenos != NULL)
6231 free (finfo.linenos);
6232 if (finfo.contents != NULL)
6233 free (finfo.contents);
6234 if (finfo.external_relocs != NULL)
6235 free (finfo.external_relocs);
6236 if (external_relocs != NULL)
6237 free (external_relocs);