1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. A lot of it is very similar
29 to the COFF linker code. However, it is different enough that I
30 chose to avoid trying to hack up the COFF code to support XCOFF.
31 That leads to a certain amount of duplicated code, alas. */
33 #define STRING_SIZE_SIZE (4)
35 /* Get the XCOFF hash table entries for a BFD. */
36 #define obj_xcoff_sym_hashes(bfd) \
37 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
39 /* XCOFF relocation types. These probably belong in a header file
40 somewhere. The relocations are described in the function
41 _bfd_ppc_xcoff_relocate_section in this file. */
57 #define R_RRTBI (0x14)
58 #define R_RRTBA (0x15)
66 /* The first word of global linkage code. This must be modified by
67 filling in the correct TOC offset. */
69 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
71 /* The remaining words of global linkage code. */
73 static unsigned long xcoff_glink_code[] =
75 0x90410014, /* stw r2,20(r1) */
76 0x800c0000, /* lwz r0,0(r12) */
77 0x804c0004, /* lwz r2,4(r12) */
78 0x7c0903a6, /* mtctr r0 */
79 0x4e800420, /* bctr */
80 0x0, /* start of traceback table */
81 0x000c8000, /* traceback table */
82 0x0 /* traceback table */
85 #define XCOFF_GLINK_SIZE \
86 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
88 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
89 This flag will only be used on input sections. */
91 #define SEC_MARK (SEC_ROM)
93 /* The ldhdr structure. This appears at the start of the .loader
98 /* The version number: currently always 1. */
99 unsigned long l_version;
100 /* The number of symbol table entries. */
101 bfd_size_type l_nsyms;
102 /* The number of relocation table entries. */
103 bfd_size_type l_nreloc;
104 /* The length of the import file string table. */
105 bfd_size_type l_istlen;
106 /* The number of import files. */
107 bfd_size_type l_nimpid;
108 /* The offset from the start of the .loader section to the first
109 entry in the import file table. */
110 bfd_size_type l_impoff;
111 /* The length of the string table. */
112 bfd_size_type l_stlen;
113 /* The offset from the start of the .loader section to the first
114 entry in the string table. */
115 bfd_size_type l_stoff;
118 struct external_ldhdr
120 bfd_byte l_version[4];
122 bfd_byte l_nreloc[4];
123 bfd_byte l_istlen[4];
124 bfd_byte l_nimpid[4];
125 bfd_byte l_impoff[4];
130 #define LDHDRSZ (8 * 4)
132 /* The ldsym structure. This is used to represent a symbol in the
135 struct internal_ldsym
139 /* The symbol name if <= SYMNMLEN characters. */
140 char _l_name[SYMNMLEN];
143 /* Zero if the symbol name is more than SYMNMLEN characters. */
145 /* The offset in the string table if the symbol name is more
146 than SYMNMLEN characters. */
150 /* The symbol value. */
152 /* The symbol section number. */
154 /* The symbol type and flags. */
156 /* The symbol storage class. */
158 /* The import file ID. */
159 bfd_size_type l_ifile;
160 /* Offset to the parameter type check string. */
161 bfd_size_type l_parm;
164 struct external_ldsym
168 bfd_byte _l_name[SYMNMLEN];
171 bfd_byte _l_zeroes[4];
172 bfd_byte _l_offset[4];
177 bfd_byte l_smtype[1];
178 bfd_byte l_smclas[1];
183 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
185 /* These flags are for the l_smtype field (the lower three bits are an
188 /* Imported symbol. */
189 #define L_IMPORT (0x40)
191 #define L_ENTRY (0x20)
192 /* Exported symbol. */
193 #define L_EXPORT (0x10)
195 /* The ldrel structure. This is used to represent a reloc in the
198 struct internal_ldrel
200 /* The reloc address. */
202 /* The symbol table index in the .loader section symbol table. */
203 bfd_size_type l_symndx;
204 /* The relocation type and size. */
206 /* The section number this relocation applies to. */
210 struct external_ldrel
213 bfd_byte l_symndx[4];
215 bfd_byte l_rsecnm[2];
218 #define LDRELSZ (2 * 4 + 2 * 2)
220 /* The list of import files. */
222 struct xcoff_import_file
224 /* The next entry in the list. */
225 struct xcoff_import_file *next;
230 /* The member name. */
234 /* An entry in the XCOFF linker hash table. */
236 struct xcoff_link_hash_entry
238 struct bfd_link_hash_entry root;
240 /* Symbol index in output file. Set to -1 initially. Set to -2 if
241 there is a reloc against this symbol. */
244 /* If we have created a TOC entry for this symbol, this is the .tc
245 section which holds it. */
246 asection *toc_section;
248 /* If we have created a TOC entry, this is the offset in
252 /* If this symbol is a function entry point which is called, this
253 field holds a pointer to the function descriptor. */
254 struct xcoff_link_hash_entry *descriptor;
256 /* The .loader symbol table entry, if there is one. */
257 struct internal_ldsym *ldsym;
259 /* The .loader symbol table index. */
262 /* Some linker flags. */
263 unsigned short flags;
264 /* Symbol is referenced by a regular object. */
265 #define XCOFF_REF_REGULAR (01)
266 /* Symbol is defined by a regular object. */
267 #define XCOFF_DEF_REGULAR (02)
268 /* Symbol is referenced by a dynamic object. */
269 #define XCOFF_REF_DYNAMIC (04)
270 /* Symbol is used in a reloc being copied into the .loader section. */
271 #define XCOFF_LDREL (010)
272 /* Symbol is the entry point. */
273 #define XCOFF_ENTRY (020)
274 /* Symbol is called; this is, it appears in a R_BR reloc. */
275 #define XCOFF_CALLED (040)
276 /* Symbol needs the TOC entry filled in. */
277 #define XCOFF_SET_TOC (0100)
278 /* Symbol is explicitly imported. */
279 #define XCOFF_IMPORT (0200)
280 /* Symbol is explicitly exported. */
281 #define XCOFF_EXPORT (0400)
282 /* Symbol has been processed by xcoff_build_ldsyms. */
283 #define XCOFF_BUILT_LDSYM (01000)
284 /* Symbol is mentioned by a section which was not garbage collected. */
285 #define XCOFF_MARK (02000)
287 /* The storage mapping class. */
288 unsigned char smclas;
291 /* The XCOFF linker hash table. */
293 struct xcoff_link_hash_table
295 struct bfd_link_hash_table root;
297 /* The .debug string hash table. We need to compute this while
298 reading the input files, so that we know how large the .debug
299 section will be before we assign section positions. */
300 struct bfd_strtab_hash *debug_strtab;
302 /* The .debug section we will use for the final output. */
303 asection *debug_section;
305 /* The .loader section we will use for the final output. */
306 asection *loader_section;
308 /* A count of non TOC relative relocs which will need to be
309 allocated in the .loader section. */
312 /* The .loader section header. */
313 struct internal_ldhdr ldhdr;
315 /* The .gl section we use to hold global linkage code. */
316 asection *linkage_section;
318 /* The .tc section we use to hold toc entries we build for global
320 asection *toc_section;
322 /* The list of import files. */
323 struct xcoff_import_file *imports;
325 /* Required alignment of sections within the output file. */
326 unsigned long file_align;
328 /* Whether the .text section must be read-only. */
331 /* Whether garbage collection was done. */
335 /* Information we keep for each section in the output file during the
338 struct xcoff_link_section_info
340 /* The relocs to be output. */
341 struct internal_reloc *relocs;
342 /* For each reloc against a global symbol whose index was not known
343 when the reloc was handled, the global hash table entry. */
344 struct xcoff_link_hash_entry **rel_hashes;
347 /* Information that we pass around while doing the final link step. */
349 struct xcoff_final_link_info
351 /* General link information. */
352 struct bfd_link_info *info;
355 /* Hash table for long symbol names. */
356 struct bfd_strtab_hash *strtab;
357 /* Array of information kept for each output section, indexed by the
358 target_index field. */
359 struct xcoff_link_section_info *section_info;
360 /* Symbol index of last C_FILE symbol (-1 if none). */
361 long last_file_index;
362 /* Contents of last C_FILE symbol. */
363 struct internal_syment last_file;
364 /* Symbol index of TOC symbol. */
366 /* Start of .loader symbols. */
367 struct external_ldsym *ldsym;
368 /* Next .loader reloc to swap out. */
369 struct external_ldrel *ldrel;
370 /* Buffer large enough to hold swapped symbols of any input file. */
371 struct internal_syment *internal_syms;
372 /* Buffer large enough to hold output indices of symbols of any
375 /* Buffer large enough to hold output symbols for any input file. */
377 /* Buffer large enough to hold external line numbers for any input
380 /* Buffer large enough to hold any input section. */
382 /* Buffer large enough to hold external relocs of any input section. */
383 bfd_byte *external_relocs;
386 static void xcoff_swap_ldhdr_out
387 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
388 static void xcoff_swap_ldsym_out
389 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
390 static void xcoff_swap_ldrel_out
391 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
392 static struct bfd_hash_entry *xcoff_link_hash_newfunc
393 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
394 static struct internal_reloc *xcoff_read_internal_relocs
395 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
396 struct internal_reloc *));
397 static boolean xcoff_link_add_object_symbols
398 PARAMS ((bfd *, struct bfd_link_info *));
399 static boolean xcoff_link_check_archive_element
400 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
401 static boolean xcoff_link_check_ar_symbols
402 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
403 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
404 static boolean xcoff_link_add_dynamic_symbols
405 PARAMS ((bfd *, struct bfd_link_info *));
406 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
407 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
408 static boolean xcoff_build_ldsyms
409 PARAMS ((struct xcoff_link_hash_entry *, PTR));
410 static boolean xcoff_link_input_bfd
411 PARAMS ((struct xcoff_final_link_info *, bfd *));
412 static boolean xcoff_write_global_symbol
413 PARAMS ((struct xcoff_link_hash_entry *, PTR));
414 static boolean xcoff_reloc_link_order
415 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
416 struct bfd_link_order *));
417 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
419 /* Routines to swap information in the XCOFF .loader section. We only
420 need to swap this information out, not in. I believe that only the
421 loader needs to swap this information in. If we ever need to write
422 an XCOFF loader, this stuff will need to be moved to another file
423 shared by the linker (which XCOFF calls the ``binder'') and the
426 /* Swap out the ldhdr structure. */
429 xcoff_swap_ldhdr_out (abfd, src, dst)
431 const struct internal_ldhdr *src;
432 struct external_ldhdr *dst;
434 bfd_put_32 (abfd, src->l_version, dst->l_version);
435 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
436 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
437 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
438 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
439 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
440 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
441 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
444 /* Swap out the ldsym structure. */
447 xcoff_swap_ldsym_out (abfd, src, dst)
449 const struct internal_ldsym *src;
450 struct external_ldsym *dst;
452 if (src->_l._l_l._l_zeroes != 0)
453 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
456 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
457 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
459 bfd_put_32 (abfd, src->l_value, dst->l_value);
460 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
461 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
462 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
463 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
464 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
467 /* Swap out the ldrel structure. */
470 xcoff_swap_ldrel_out (abfd, src, dst)
472 const struct internal_ldrel *src;
473 struct external_ldrel *dst;
475 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
476 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
477 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
478 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
481 /* Routine to create an entry in an XCOFF link hash table. */
483 static struct bfd_hash_entry *
484 xcoff_link_hash_newfunc (entry, table, string)
485 struct bfd_hash_entry *entry;
486 struct bfd_hash_table *table;
489 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
491 /* Allocate the structure if it has not already been allocated by a
493 if (ret == (struct xcoff_link_hash_entry *) NULL)
494 ret = ((struct xcoff_link_hash_entry *)
495 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
496 if (ret == (struct xcoff_link_hash_entry *) NULL)
498 bfd_set_error (bfd_error_no_memory);
499 return (struct bfd_hash_entry *) ret;
502 /* Call the allocation method of the superclass. */
503 ret = ((struct xcoff_link_hash_entry *)
504 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
508 /* Set local fields. */
510 ret->toc_section = NULL;
512 ret->descriptor = NULL;
516 ret->smclas = XMC_UA;
519 return (struct bfd_hash_entry *) ret;
522 /* Create a XCOFF link hash table. */
524 struct bfd_link_hash_table *
525 _bfd_xcoff_bfd_link_hash_table_create (abfd)
528 struct xcoff_link_hash_table *ret;
530 ret = ((struct xcoff_link_hash_table *)
531 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
532 if (ret == (struct xcoff_link_hash_table *) NULL)
534 bfd_set_error (bfd_error_no_memory);
535 return (struct bfd_link_hash_table *) NULL;
537 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
539 bfd_release (abfd, ret);
540 return (struct bfd_link_hash_table *) NULL;
543 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
544 ret->debug_section = NULL;
545 ret->loader_section = NULL;
546 ret->ldrel_count = 0;
547 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
548 ret->linkage_section = NULL;
549 ret->toc_section = NULL;
558 /* Look up an entry in an XCOFF link hash table. */
560 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
561 ((struct xcoff_link_hash_entry *) \
562 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
565 /* Traverse an XCOFF link hash table. */
567 #define xcoff_link_hash_traverse(table, func, info) \
568 (bfd_link_hash_traverse \
570 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
573 /* Get the XCOFF link hash table from the info structure. This is
576 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
578 /* Read internal relocs for an XCOFF csect. This is a wrapper around
579 _bfd_coff_read_internal_relocs which tries to take advantage of any
580 relocs which may have been cached for the enclosing section. */
582 static struct internal_reloc *
583 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
584 require_internal, internal_relocs)
588 bfd_byte *external_relocs;
589 boolean require_internal;
590 struct internal_reloc *internal_relocs;
592 if (coff_section_data (abfd, sec) != NULL
593 && coff_section_data (abfd, sec)->relocs == NULL
594 && xcoff_section_data (abfd, sec) != NULL)
598 enclosing = xcoff_section_data (abfd, sec)->enclosing;
600 if (enclosing != NULL
601 && (coff_section_data (abfd, enclosing) == NULL
602 || coff_section_data (abfd, enclosing)->relocs == NULL)
605 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
606 external_relocs, false,
607 (struct internal_reloc *) NULL)
612 if (enclosing != NULL
613 && coff_section_data (abfd, enclosing) != NULL
614 && coff_section_data (abfd, enclosing)->relocs != NULL)
618 off = ((sec->rel_filepos - enclosing->rel_filepos)
619 / bfd_coff_relsz (abfd));
620 if (! require_internal)
621 return coff_section_data (abfd, enclosing)->relocs + off;
622 memcpy (internal_relocs,
623 coff_section_data (abfd, enclosing)->relocs + off,
624 sec->reloc_count * sizeof (struct internal_reloc));
625 return internal_relocs;
629 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
630 require_internal, internal_relocs);
633 /* Given an XCOFF BFD, add symbols to the global hash table as
637 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
639 struct bfd_link_info *info;
641 switch (bfd_get_format (abfd))
644 return xcoff_link_add_object_symbols (abfd, info);
646 return (_bfd_generic_link_add_archive_symbols
647 (abfd, info, xcoff_link_check_archive_element));
649 bfd_set_error (bfd_error_wrong_format);
654 /* Add symbols from an XCOFF object file. */
657 xcoff_link_add_object_symbols (abfd, info)
659 struct bfd_link_info *info;
661 if (! _bfd_coff_get_external_symbols (abfd))
663 if (! xcoff_link_add_symbols (abfd, info))
665 if (! info->keep_memory)
667 if (! _bfd_coff_free_symbols (abfd))
673 /* Check a single archive element to see if we need to include it in
674 the link. *PNEEDED is set according to whether this element is
675 needed in the link or not. This is called via
676 _bfd_generic_link_add_archive_symbols. */
679 xcoff_link_check_archive_element (abfd, info, pneeded)
681 struct bfd_link_info *info;
684 if (! _bfd_coff_get_external_symbols (abfd))
687 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
692 if (! xcoff_link_add_symbols (abfd, info))
696 if (! info->keep_memory || ! *pneeded)
698 if (! _bfd_coff_free_symbols (abfd))
705 /* Look through the symbols to see if this object file should be
706 included in the link. */
709 xcoff_link_check_ar_symbols (abfd, info, pneeded)
711 struct bfd_link_info *info;
714 bfd_size_type symesz;
720 symesz = bfd_coff_symesz (abfd);
721 esym = (bfd_byte *) obj_coff_external_syms (abfd);
722 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
723 while (esym < esym_end)
725 struct internal_syment sym;
727 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
729 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
732 char buf[SYMNMLEN + 1];
733 struct bfd_link_hash_entry *h;
735 /* This symbol is externally visible, and is defined by this
738 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
741 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
743 /* We are only interested in symbols that are currently
744 undefined. If a symbol is currently known to be common,
745 XCOFF linkers do not bring in an object file which
746 defines it. We also don't bring in symbols to satisfy
747 undefined references in shared objects. */
748 if (h != (struct bfd_link_hash_entry *) NULL
749 && h->type == bfd_link_hash_undefined)
751 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
758 esym += (sym.n_numaux + 1) * symesz;
761 /* We do not need this object file. */
765 /* Add all the symbols from an object file to the hash table.
767 XCOFF is a weird format. A normal XCOFF .o files will have three
768 COFF sections--.text, .data, and .bss--but each COFF section will
769 contain many csects. These csects are described in the symbol
770 table. From the linker's point of view, each csect must be
771 considered a section in its own right. For example, a TOC entry is
772 handled as a small XMC_TC csect. The linker must be able to merge
773 different TOC entries together, which means that it must be able to
774 extract the XMC_TC csects from the .data section of the input .o
777 From the point of view of our linker, this is, of course, a hideous
778 nightmare. We cope by actually creating sections for each csect,
779 and discarding the original sections. We then have to handle the
780 relocation entries carefully, since the only way to tell which
781 csect they belong to is to examine the address. */
784 xcoff_link_add_symbols (abfd, info)
786 struct bfd_link_info *info;
788 unsigned int n_tmask;
789 unsigned int n_btshft;
790 boolean default_copy;
791 bfd_size_type symcount;
792 struct xcoff_link_hash_entry **sym_hash;
793 asection **csect_cache;
794 bfd_size_type linesz;
798 unsigned int csect_index;
799 asection *first_csect;
800 bfd_size_type symesz;
803 struct reloc_info_struct
805 struct internal_reloc *relocs;
808 } *reloc_info = NULL;
810 if ((abfd->flags & DYNAMIC) != 0
811 && ! info->static_link)
812 return xcoff_link_add_dynamic_symbols (abfd, info);
814 n_tmask = coff_data (abfd)->local_n_tmask;
815 n_btshft = coff_data (abfd)->local_n_btshft;
817 /* Define macros so that ISFCN, et. al., macros work correctly. */
818 #define N_TMASK n_tmask
819 #define N_BTSHFT n_btshft
821 /* We need to build a .loader section, so we do it here. This won't
822 work if we're producing an XCOFF output file with no non dynamic
823 XCOFF input files. FIXME. */
824 if (xcoff_hash_table (info)->loader_section == NULL)
828 lsec = bfd_make_section_anyway (abfd, ".loader");
831 xcoff_hash_table (info)->loader_section = lsec;
832 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
834 /* Likewise for the linkage section. */
835 if (xcoff_hash_table (info)->linkage_section == NULL)
839 lsec = bfd_make_section_anyway (abfd, ".gl");
842 xcoff_hash_table (info)->linkage_section = lsec;
843 lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
845 /* Likewise for the TOC section. */
846 if (xcoff_hash_table (info)->toc_section == NULL)
850 tsec = bfd_make_section_anyway (abfd, ".tc");
853 xcoff_hash_table (info)->toc_section = tsec;
854 tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
856 /* Likewise for the .debug section. */
857 if (xcoff_hash_table (info)->debug_section == NULL)
861 dsec = bfd_make_section_anyway (abfd, ".debug");
864 xcoff_hash_table (info)->debug_section = dsec;
865 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
868 if (info->keep_memory)
869 default_copy = false;
873 symcount = obj_raw_syment_count (abfd);
875 /* We keep a list of the linker hash table entries that correspond
876 to each external symbol. */
877 sym_hash = ((struct xcoff_link_hash_entry **)
880 * sizeof (struct xcoff_link_hash_entry *))));
881 if (sym_hash == NULL && symcount != 0)
883 bfd_set_error (bfd_error_no_memory);
886 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
888 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
890 /* Because of the weird stuff we are doing with XCOFF csects, we can
891 not easily determine which section a symbol is in, so we store
892 the information in the tdata for the input file. */
893 csect_cache = ((asection **)
894 bfd_alloc (abfd, symcount * sizeof (asection *)));
895 if (csect_cache == NULL && symcount != 0)
897 bfd_set_error (bfd_error_no_memory);
900 xcoff_data (abfd)->csects = csect_cache;
901 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
903 /* While splitting sections into csects, we need to assign the
904 relocs correctly. The relocs and the csects must both be in
905 order by VMA within a given section, so we handle this by
906 scanning along the relocs as we process the csects. We index
907 into reloc_info using the section target_index. */
908 reloc_info = ((struct reloc_info_struct *)
909 malloc ((abfd->section_count + 1)
910 * sizeof (struct reloc_info_struct)));
911 if (reloc_info == NULL)
913 bfd_set_error (bfd_error_no_memory);
916 memset ((PTR) reloc_info, 0,
917 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
919 /* Read in the relocs and line numbers for each section. */
920 linesz = bfd_coff_linesz (abfd);
921 for (sub = abfd->sections; sub != NULL; sub = sub->next)
923 if ((sub->flags & SEC_RELOC) != 0)
925 reloc_info[sub->target_index].relocs =
926 xcoff_read_internal_relocs (abfd, sub, true, (bfd_byte *) NULL,
927 false, (struct internal_reloc *) NULL);
928 reloc_info[sub->target_index].csects =
929 (asection **) malloc (sub->reloc_count * sizeof (asection *));
930 if (reloc_info[sub->target_index].csects == NULL)
932 bfd_set_error (bfd_error_no_memory);
935 memset (reloc_info[sub->target_index].csects, 0,
936 sub->reloc_count * sizeof (asection *));
939 if ((info->strip == strip_none || info->strip == strip_some)
940 && sub->lineno_count > 0)
944 linenos = (bfd_byte *) malloc (sub->lineno_count * linesz);
947 bfd_set_error (bfd_error_no_memory);
950 reloc_info[sub->target_index].linenos = linenos;
951 if (bfd_seek (abfd, sub->line_filepos, SEEK_SET) != 0
952 || (bfd_read (linenos, linesz, sub->lineno_count, abfd)
953 != linesz * sub->lineno_count))
958 /* Don't let the linker relocation routines discard the symbols. */
959 keep_syms = obj_coff_keep_syms (abfd);
960 obj_coff_keep_syms (abfd) = true;
966 symesz = bfd_coff_symesz (abfd);
967 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
968 esym = (bfd_byte *) obj_coff_external_syms (abfd);
969 esym_end = esym + symcount * symesz;
970 while (esym < esym_end)
972 struct internal_syment sym;
973 union internal_auxent aux;
975 char buf[SYMNMLEN + 1];
980 struct xcoff_link_hash_entry *set_toc;
982 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
984 /* In this pass we are only interested in symbols with csect
986 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
988 if (sym.n_sclass == C_FILE && csect != NULL)
990 xcoff_section_data (abfd, csect)->last_symndx =
992 - (bfd_byte *) obj_coff_external_syms (abfd))
998 *csect_cache = csect;
999 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1000 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1002 *csect_cache = NULL;
1003 esym += (sym.n_numaux + 1) * symesz;
1004 sym_hash += sym.n_numaux + 1;
1005 csect_cache += sym.n_numaux + 1;
1009 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1013 /* If this symbol has line number information attached to it,
1014 and we're not stripping it, count the number of entries and
1015 add them to the count for this csect. In the final link pass
1016 we are going to attach line number information by symbol,
1017 rather than by section, in order to more easily handle
1018 garbage collection. */
1019 if ((info->strip == strip_none || info->strip == strip_some)
1022 && ISFCN (sym.n_type))
1024 union internal_auxent auxlin;
1026 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1027 sym.n_type, sym.n_sclass,
1028 0, sym.n_numaux, (PTR) &auxlin);
1029 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1031 asection *enclosing;
1032 bfd_size_type linoff;
1034 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1035 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1036 - enclosing->line_filepos);
1037 if (linoff < enclosing->lineno_count * linesz)
1039 struct internal_lineno lin;
1040 bfd_byte *linpstart;
1042 linpstart = (reloc_info[enclosing->target_index].linenos
1044 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1046 && ((bfd_size_type) lin.l_addr.l_symndx
1048 - (bfd_byte *) obj_coff_external_syms (abfd))
1051 bfd_byte *linpend, *linp;
1053 linpend = (reloc_info[enclosing->target_index].linenos
1054 + enclosing->lineno_count * linesz);
1055 for (linp = linpstart + linesz;
1059 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1061 if (lin.l_lnno == 0)
1064 csect->lineno_count += (linp - linpstart) / linesz;
1065 /* The setting of line_filepos will only be
1066 useful if all the line number entries for a
1067 csect are contiguous; this only matters for
1069 if (csect->line_filepos == 0)
1070 csect->line_filepos =
1071 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1077 /* Pick up the csect auxiliary information. */
1079 if (sym.n_numaux == 0)
1081 (*_bfd_error_handler)
1082 ("%s: class %d symbol `%s' has no aux entries",
1083 bfd_get_filename (abfd), sym.n_sclass, name);
1084 bfd_set_error (bfd_error_bad_value);
1088 bfd_coff_swap_aux_in (abfd,
1089 (PTR) (esym + symesz * sym.n_numaux),
1090 sym.n_type, sym.n_sclass,
1091 sym.n_numaux - 1, sym.n_numaux,
1094 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1104 (*_bfd_error_handler)
1105 ("%s: symbol `%s' has unrecognized csect type %d",
1106 bfd_get_filename (abfd), name, smtyp);
1107 bfd_set_error (bfd_error_bad_value);
1111 /* This is an external reference. */
1112 if (sym.n_sclass == C_HIDEXT
1113 || sym.n_scnum != N_UNDEF
1114 || aux.x_csect.x_scnlen.l != 0)
1116 (*_bfd_error_handler)
1117 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1118 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1119 aux.x_csect.x_scnlen.l);
1120 bfd_set_error (bfd_error_bad_value);
1123 section = bfd_und_section_ptr;
1127 /* This is a csect definition. */
1131 xcoff_section_data (abfd, csect)->last_symndx =
1133 - (bfd_byte *) obj_coff_external_syms (abfd))
1140 /* When we see a TOC anchor, we record the TOC value. */
1141 if (aux.x_csect.x_smclas == XMC_TC0)
1143 if (sym.n_sclass != C_HIDEXT
1144 || aux.x_csect.x_scnlen.l != 0)
1146 (*_bfd_error_handler)
1147 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1148 bfd_get_filename (abfd), name, sym.n_sclass,
1149 aux.x_csect.x_scnlen.l);
1150 bfd_set_error (bfd_error_bad_value);
1153 xcoff_data (abfd)->toc = sym.n_value;
1156 /* We must merge TOC entries for the same symbol. We can
1157 merge two TOC entries if they are both C_HIDEXT, they
1158 both have the same name, they are both 4 bytes long, and
1159 they both have a relocation table entry for an external
1160 symbol with the same name. Unfortunately, this means
1161 that we must look through the relocations. Ick. */
1162 if (aux.x_csect.x_smclas == XMC_TC
1163 && sym.n_sclass == C_HIDEXT
1164 && aux.x_csect.x_scnlen.l == 4
1165 && info->hash->creator == abfd->xvec)
1167 asection *enclosing;
1168 bfd_size_type relindx;
1169 struct internal_reloc *rel;
1170 asection **rel_csect;
1172 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1173 if (enclosing == NULL)
1176 /* XCOFF requires that relocs be sorted by address, so
1177 we could do a binary search here. FIXME. */
1178 rel = reloc_info[enclosing->target_index].relocs;
1179 rel_csect = reloc_info[enclosing->target_index].csects;
1181 relindx < enclosing->reloc_count;
1182 relindx++, rel++, rel_csect++)
1184 if (*rel_csect == NULL
1185 && rel->r_vaddr == (bfd_vma) sym.n_value
1186 && rel->r_size == 31
1187 && rel->r_type == R_POS)
1190 if (relindx < enclosing->reloc_count)
1193 struct internal_syment relsym;
1195 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1196 + rel->r_symndx * symesz);
1197 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1198 if (relsym.n_sclass == C_EXT)
1200 const char *relname;
1201 char relbuf[SYMNMLEN + 1];
1203 struct xcoff_link_hash_entry *h;
1205 /* At this point we know that the TOC entry is
1206 for an externally visible symbol. */
1207 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1209 if (relname == NULL)
1211 copy = (! info->keep_memory
1212 || relsym._n._n_n._n_zeroes != 0
1213 || relsym._n._n_n._n_offset == 0);
1214 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1215 relname, true, copy, false);
1219 /* At this point h->root.type could be
1220 bfd_link_hash_new. That should be OK, since
1221 we know for sure that we will come across
1222 this symbol as we step through the file. */
1224 /* We store h in *sym_hash for the convenience
1225 of the relocate_section function. */
1228 if (h->toc_section != NULL)
1230 /* We already have a TOC entry for this
1231 symbol, so we can just ignore this one. */
1232 *rel_csect = bfd_und_section_ptr;
1236 /* We are about to create a TOC entry for this
1243 /* We need to create a new section. We get the name from
1244 the csect storage mapping class, so that the linker can
1245 accumulate similar csects together. */
1247 static const char *csect_name_by_class[] =
1249 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1250 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1253 const char *csect_name;
1254 asection *enclosing;
1255 struct internal_reloc *rel;
1256 bfd_size_type relindx;
1257 asection **rel_csect;
1259 if ((aux.x_csect.x_smclas >=
1260 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1261 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1263 (*_bfd_error_handler)
1264 ("%s: symbol `%s' has unrecognized smclas %d",
1265 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1266 bfd_set_error (bfd_error_bad_value);
1270 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1271 csect = bfd_make_section_anyway (abfd, csect_name);
1274 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1275 if (enclosing == NULL)
1277 if ((bfd_vma) sym.n_value < enclosing->vma
1278 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1279 > enclosing->vma + enclosing->_raw_size))
1281 (*_bfd_error_handler)
1282 ("%s: csect `%s' not in enclosing section",
1283 bfd_get_filename (abfd), name);
1284 bfd_set_error (bfd_error_bad_value);
1287 csect->vma = sym.n_value;
1288 csect->filepos = (enclosing->filepos
1291 csect->_raw_size = aux.x_csect.x_scnlen.l;
1292 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1293 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1295 /* Record the enclosing section in the tdata for this new
1297 csect->used_by_bfd =
1298 ((struct coff_section_tdata *)
1299 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1300 if (csect->used_by_bfd == NULL)
1302 bfd_set_error (bfd_error_no_memory);
1305 coff_section_data (abfd, csect)->tdata =
1306 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1307 if (coff_section_data (abfd, csect)->tdata == NULL)
1309 bfd_set_error (bfd_error_no_memory);
1312 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1314 /* XCOFF requires that relocs be sorted by address, so we
1315 could do a binary search here. FIXME. (XCOFF
1316 unfortunately does not require that symbols be sorted
1317 by address, or this would be a simple merge). */
1318 rel = reloc_info[enclosing->target_index].relocs;
1319 rel_csect = reloc_info[enclosing->target_index].csects;
1321 relindx < enclosing->reloc_count;
1322 relindx++, rel++, rel_csect++)
1324 if (*rel_csect == NULL
1325 && rel->r_vaddr >= csect->vma
1326 && rel->r_vaddr < csect->vma + csect->_raw_size)
1328 csect->rel_filepos = (enclosing->rel_filepos
1329 + relindx * bfd_coff_relsz (abfd));
1333 while (relindx < enclosing->reloc_count
1334 && *rel_csect == NULL
1335 && rel->r_vaddr >= csect->vma
1336 && rel->r_vaddr < csect->vma + csect->_raw_size)
1339 csect->flags |= SEC_RELOC;
1340 ++csect->reloc_count;
1346 /* There are a number of other fields and section flags
1347 which we do not bother to set. */
1349 csect_index = ((esym
1350 - (bfd_byte *) obj_coff_external_syms (abfd))
1353 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1355 if (first_csect == NULL)
1356 first_csect = csect;
1358 /* If this symbol is C_EXT, we treat it as starting at the
1359 beginning of the newly created section. */
1360 if (sym.n_sclass == C_EXT)
1366 /* If this is a TOC section for a symbol, record it. */
1367 if (set_toc != NULL)
1369 set_toc->toc_section = csect;
1370 set_toc->toc_offset = 0;
1376 /* This is a label definition. The x_scnlen field is the
1377 symbol index of the csect. I believe that this must
1378 always follow the appropriate XTY_SD symbol, so I will
1384 if (aux.x_csect.x_scnlen.l < 0
1385 || (aux.x_csect.x_scnlen.l
1386 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1390 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1392 || (section->flags & SEC_HAS_CONTENTS) == 0)
1397 (*_bfd_error_handler)
1398 ("%s: misplaced XTY_LD `%s'",
1399 bfd_get_filename (abfd), name);
1400 bfd_set_error (bfd_error_bad_value);
1404 value = sym.n_value - csect->vma;
1409 /* This is an unitialized csect. We could base the name on
1410 the storage mapping class, but we don't bother. If this
1411 csect is externally visible, it is a common symbol. */
1415 xcoff_section_data (abfd, csect)->last_symndx =
1417 - (bfd_byte *) obj_coff_external_syms (abfd))
1421 csect = bfd_make_section_anyway (abfd, ".bss");
1425 csect->_raw_size = aux.x_csect.x_scnlen.l;
1426 csect->flags |= SEC_ALLOC;
1427 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1428 /* There are a number of other fields and section flags
1429 which we do not bother to set. */
1431 csect_index = ((esym
1432 - (bfd_byte *) obj_coff_external_syms (abfd))
1435 csect->used_by_bfd =
1436 ((struct coff_section_tdata *)
1437 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1438 if (csect->used_by_bfd == NULL)
1440 bfd_set_error (bfd_error_no_memory);
1443 coff_section_data (abfd, csect)->tdata =
1444 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1445 if (coff_section_data (abfd, csect)->tdata == NULL)
1447 bfd_set_error (bfd_error_no_memory);
1450 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1452 if (first_csect == NULL)
1453 first_csect = csect;
1455 if (sym.n_sclass == C_EXT)
1457 csect->flags |= SEC_IS_COMMON;
1459 value = aux.x_csect.x_scnlen.l;
1465 /* Now we have enough information to add the symbol to the
1466 linker hash table. */
1468 if (sym.n_sclass == C_EXT)
1472 BFD_ASSERT (section != NULL);
1474 /* We must copy the name into memory if we got it from the
1475 syment itself, rather than the string table. */
1476 copy = default_copy;
1477 if (sym._n._n_n._n_zeroes != 0
1478 || sym._n._n_n._n_offset == 0)
1481 if (info->hash->creator == abfd->xvec)
1483 /* If we are statically linking a shared object, it is
1484 OK for symbol redefinitions to occur. I can't figure
1485 out just what the XCOFF linker is doing, but
1486 something like this is required for -bnso to work. */
1487 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1488 name, true, copy, false);
1489 if (*sym_hash == NULL)
1491 if (((*sym_hash)->root.type == bfd_link_hash_defined
1492 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1493 && ! bfd_is_und_section (section)
1494 && ! bfd_is_com_section (section))
1496 if ((abfd->flags & DYNAMIC) != 0)
1498 section = bfd_und_section_ptr;
1501 else if (((*sym_hash)->root.u.def.section->owner->flags
1504 (*sym_hash)->root.type = bfd_link_hash_undefined;
1505 (*sym_hash)->root.u.undef.abfd =
1506 (*sym_hash)->root.u.def.section->owner;
1511 if (! (_bfd_generic_link_add_one_symbol
1512 (info, abfd, name, flags, section, value,
1513 (const char *) NULL, copy, false,
1514 (struct bfd_link_hash_entry **) sym_hash)))
1517 if (info->hash->creator == abfd->xvec)
1521 if (smtyp == XTY_ER || smtyp == XTY_CM)
1522 flag = XCOFF_REF_REGULAR;
1524 flag = XCOFF_DEF_REGULAR;
1525 (*sym_hash)->flags |= flag;
1527 if ((*sym_hash)->smclas == XMC_UA)
1528 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1532 *csect_cache = csect;
1534 esym += (sym.n_numaux + 1) * symesz;
1535 sym_hash += sym.n_numaux + 1;
1536 csect_cache += sym.n_numaux + 1;
1539 /* Make sure that we have seen all the relocs. */
1540 for (sub = abfd->sections; sub != first_csect; sub = sub->next)
1542 /* Reset the section size, since the data is now attached to the
1543 csects. Don't reset the size of the .debug section, since we
1544 need to read it below in bfd_xcoff_size_dynamic_sections. */
1545 if (strcmp (bfd_get_section_name (abfd, sub), ".debug") != 0)
1548 if ((sub->flags & SEC_RELOC) != 0)
1551 struct internal_reloc *rel;
1552 asection **rel_csect;
1554 rel = reloc_info[sub->target_index].relocs;
1555 rel_csect = reloc_info[sub->target_index].csects;
1556 for (i = 0; i < sub->reloc_count; i++, rel++, rel_csect++)
1558 if (*rel_csect == NULL)
1560 (*_bfd_error_handler)
1561 ("%s: reloc %s:%d not in csect",
1562 bfd_get_filename (abfd), sub->name, i);
1563 bfd_set_error (bfd_error_bad_value);
1567 /* We need to copy all relocs which are not PC relative
1568 and not TOC relative into the .loader section.
1570 We also identify all symbols which are called, so
1571 that we can create glue code for calls to functions
1572 imported from dynamic objects. */
1574 if (info->hash->creator == abfd->xvec
1575 && *rel_csect != bfd_und_section_ptr)
1577 struct xcoff_link_hash_entry *h;
1579 switch (rel->r_type)
1587 ++xcoff_hash_table (info)->ldrel_count;
1588 ++xcoff_section_data (abfd, *rel_csect)->ldrel_count;
1589 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1591 h->flags |= XCOFF_LDREL;
1595 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1598 h->flags |= XCOFF_CALLED;
1599 /* If the symbol name starts with a period,
1600 it is the code of a function. If the
1601 symbol is currently undefined, then add
1602 an undefined symbol for the function
1603 descriptor. This should do no harm,
1604 because any regular object that defines
1605 the function should also define the
1606 function descriptor. It helps, because
1607 it means that we will identify the
1608 function descriptor with a dynamic object
1609 if a dynamic object defines it. */
1610 if (h->root.root.string[0] == '.'
1611 && h->descriptor == NULL)
1613 struct xcoff_link_hash_entry *hds;
1615 hds = (xcoff_link_hash_lookup
1616 (xcoff_hash_table (info),
1617 h->root.root.string + 1, true, false,
1621 if (hds->root.type == bfd_link_hash_new)
1623 if (! (_bfd_generic_link_add_one_symbol
1624 (info, abfd, hds->root.root.string,
1625 (flagword) 0, bfd_und_section_ptr,
1626 (bfd_vma) 0, (const char *) NULL,
1628 ((struct bfd_link_hash_entry **)
1632 h->descriptor = hds;
1640 free (reloc_info[sub->target_index].csects);
1641 reloc_info[sub->target_index].csects = NULL;
1643 /* Reset SEC_RELOC, the reloc_count, and the lineno_count,
1644 since the reloc and lineno information is now attached to
1646 sub->flags &=~ SEC_RELOC;
1647 sub->reloc_count = 0;
1648 sub->lineno_count = 0;
1650 /* If we are not keeping memory, free the reloc information. */
1651 if (! info->keep_memory
1652 && coff_section_data (abfd, sub) != NULL
1653 && coff_section_data (abfd, sub)->relocs != NULL
1654 && ! coff_section_data (abfd, sub)->keep_relocs)
1656 free (coff_section_data (abfd, sub)->relocs);
1657 coff_section_data (abfd, sub)->relocs = NULL;
1661 /* Free up the line numbers. FIXME: We could cache these
1662 somewhere for the final link, to avoid reading them again. */
1663 if (reloc_info[sub->target_index].linenos != NULL)
1665 free (reloc_info[sub->target_index].linenos);
1666 reloc_info[sub->target_index].linenos = NULL;
1672 obj_coff_keep_syms (abfd) = keep_syms;
1677 if (reloc_info != NULL)
1679 for (sub = abfd->sections; sub != NULL; sub = sub->next)
1681 if (reloc_info[sub->target_index].csects != NULL)
1682 free (reloc_info[sub->target_index].csects);
1683 if (reloc_info[sub->target_index].linenos != NULL)
1684 free (reloc_info[sub->target_index].linenos);
1688 obj_coff_keep_syms (abfd) = keep_syms;
1695 /* This function is used to add symbols from a dynamic object to the
1696 global symbol table. */
1699 xcoff_link_add_dynamic_symbols (abfd, info)
1701 struct bfd_link_info *info;
1703 bfd_size_type symesz;
1706 struct xcoff_import_file *n;
1711 struct xcoff_import_file **pp;
1713 /* We can only handle a dynamic object if we are generating an XCOFF
1715 if (info->hash->creator != abfd->xvec)
1717 (*_bfd_error_handler)
1718 ("%s: XCOFF shared object when not producing XCOFF output",
1719 bfd_get_filename (abfd));
1720 bfd_set_error (bfd_error_invalid_operation);
1724 /* Remove the sections from this object, so that they do not get
1725 included in the link. */
1726 abfd->sections = NULL;
1728 symesz = bfd_coff_symesz (abfd);
1729 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1730 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1731 while (esym < esym_end)
1733 struct internal_syment sym;
1735 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1737 /* I think that every symbol mentioned in a dynamic object must
1738 be defined by that object, perhaps by importing it from
1739 another dynamic object. All we have to do is look up each
1740 external symbol. If we have already put it in the hash
1741 table, we simply set a flag indicating that it appears in a
1744 if (sym.n_sclass == C_EXT)
1747 char buf[SYMNMLEN + 1];
1748 struct xcoff_link_hash_entry *h;
1750 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1754 /* Normally we could not xcoff_link_hash_lookup in an add
1755 symbols routine, since we might not be using an XCOFF
1756 hash table. However, we verified above that we are using
1757 an XCOFF hash table. */
1758 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
1759 false, false, true);
1762 h->flags |= XCOFF_REF_DYNAMIC;
1764 /* If the symbol is undefined, and the current BFD is
1765 not a dynamic object, change the BFD to this dynamic
1766 object, so that we can get the import file ID
1768 if (h->root.u.undef.abfd == NULL
1769 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0)
1770 h->root.u.undef.abfd = abfd;
1772 if (h->smclas == XMC_UA
1773 && sym.n_numaux > 0)
1775 union internal_auxent aux;
1777 bfd_coff_swap_aux_in (abfd,
1778 (PTR) (esym + symesz * sym.n_numaux),
1779 sym.n_type, sym.n_sclass,
1780 sym.n_numaux - 1, sym.n_numaux,
1782 h->smclas = aux.x_csect.x_smclas;
1787 esym += (sym.n_numaux + 1) * symesz;
1790 /* Record this file in the import files. */
1792 n = ((struct xcoff_import_file *)
1793 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
1796 bfd_set_error (bfd_error_no_memory);
1801 /* For some reason, the path entry in the import file list for a
1802 shared object appears to always be empty. The file name is the
1805 if (abfd->my_archive == NULL)
1807 bname = bfd_get_filename (abfd);
1812 bname = bfd_get_filename (abfd->my_archive);
1813 mname = bfd_get_filename (abfd);
1815 s = strrchr (bname, '/');
1821 /* We start c at 1 because the first import file number is reserved
1823 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1825 pp = &(*pp)->next, ++c)
1829 xcoff_data (abfd)->import_file_id = c;
1834 /* Routines that are called after all the input files have been
1835 handled, but before the sections are laid out in memory. */
1837 /* Import a symbol. */
1840 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
1843 struct bfd_link_info *info;
1844 struct bfd_link_hash_entry *harg;
1846 const char *imppath;
1847 const char *impfile;
1848 const char *impmember;
1850 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
1852 h->flags |= XCOFF_IMPORT;
1854 if (val != (bfd_vma) -1)
1856 if (h->root.type == bfd_link_hash_defined)
1858 if (! ((*info->callbacks->multiple_definition)
1859 (info, h->root.root.string, h->root.u.def.section->owner,
1860 h->root.u.def.section, h->root.u.def.value,
1861 output_bfd, bfd_abs_section_ptr, val)))
1865 h->root.type = bfd_link_hash_defined;
1866 h->root.u.def.section = bfd_abs_section_ptr;
1867 h->root.u.def.value = val;
1870 if (h->ldsym == NULL)
1872 h->ldsym = ((struct internal_ldsym *)
1873 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
1874 if (h->ldsym == NULL)
1876 bfd_set_error (bfd_error_no_memory);
1881 if (imppath == NULL)
1882 h->ldsym->l_ifile = (bfd_size_type) -1;
1886 struct xcoff_import_file **pp;
1888 /* We start c at 1 because the first entry in the import list is
1889 reserved for the library search path. */
1890 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1892 pp = &(*pp)->next, ++c)
1894 if (strcmp ((*pp)->path, imppath) == 0
1895 && strcmp ((*pp)->file, impfile) == 0
1896 && strcmp ((*pp)->member, impmember) == 0)
1902 struct xcoff_import_file *n;
1904 n = ((struct xcoff_import_file *)
1905 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
1908 bfd_set_error (bfd_error_no_memory);
1914 n->member = impmember;
1918 h->ldsym->l_ifile = c;
1924 /* Export a symbol. */
1927 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
1929 struct bfd_link_info *info;
1930 struct bfd_link_hash_entry *harg;
1933 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
1935 h->flags |= XCOFF_EXPORT;
1937 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
1938 I'm just going to ignore it until somebody explains it. */
1943 /* This structure is used to pass information through
1944 xcoff_link_hash_traverse. */
1946 struct xcoff_loader_info
1948 /* Set if a problem occurred. */
1952 /* Link information structure. */
1953 struct bfd_link_info *info;
1954 /* Number of ldsym structures. */
1956 /* Size of string table. */
1960 /* Allocated size of string table. */
1964 /* Build the .loader section. This is called by the XCOFF linker
1965 emulation before_allocation routine. We must set the size of the
1966 .loader section before the linker lays out the output file.
1967 LIBPATH is the library path to search for shared objects; this is
1968 normally built from the -L arguments passed to the linker. ENTRY
1969 is the name of the entry point symbol. */
1972 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
1973 file_align, maxstack, maxdata, gc,
1976 struct bfd_link_info *info;
1977 const char *libpath;
1979 unsigned long file_align;
1980 unsigned long maxstack;
1981 unsigned long maxdata;
1986 struct xcoff_link_hash_entry *hentry;
1988 struct xcoff_loader_info ldinfo;
1989 size_t impsize, impcount;
1990 struct xcoff_import_file *fl;
1991 struct internal_ldhdr *ldhdr;
1995 struct bfd_strtab_hash *debug_strtab;
1996 bfd_byte *debug_contents = NULL;
1998 ldinfo.failed = false;
1999 ldinfo.output_bfd = output_bfd;
2001 ldinfo.ldsym_count = 0;
2002 ldinfo.string_size = 0;
2003 ldinfo.strings = NULL;
2004 ldinfo.string_alc = 0;
2006 xcoff_data (output_bfd)->maxstack = maxstack;
2007 xcoff_data (output_bfd)->maxdata = maxdata;
2008 xcoff_data (output_bfd)->modtype = modtype;
2010 xcoff_hash_table (info)->file_align = file_align;
2011 xcoff_hash_table (info)->textro = textro;
2013 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2014 false, false, true);
2016 hentry->flags |= XCOFF_ENTRY;
2018 /* Garbage collect unused sections. */
2019 if (info->relocateable
2022 || (hentry->root.type != bfd_link_hash_defined
2023 && hentry->root.type != bfd_link_hash_defweak))
2026 xcoff_hash_table (info)->gc = false;
2030 if (! xcoff_mark (info, hentry->root.u.def.section))
2033 xcoff_hash_table (info)->gc = true;
2036 if (info->input_bfds == NULL)
2038 /* I'm not sure what to do in this bizarre case. */
2042 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2047 /* Work out the size of the import file names. Each import file ID
2048 consists of three null terminated strings: the path, the file
2049 name, and the archive member name. The first entry in the list
2050 of names is the path to use to find objects, which the linker has
2051 passed in as the libpath argument. For some reason, the path
2052 entry in the other import file names appears to always be empty. */
2053 impsize = strlen (libpath) + 3;
2055 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2058 impsize += (strlen (fl->path)
2060 + strlen (fl->member)
2064 /* Set up the .loader section header. */
2065 ldhdr = &xcoff_hash_table (info)->ldhdr;
2066 ldhdr->l_version = 1;
2067 ldhdr->l_nsyms = ldinfo.ldsym_count;
2068 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2069 ldhdr->l_istlen = impsize;
2070 ldhdr->l_nimpid = impcount;
2071 ldhdr->l_impoff = (LDHDRSZ
2072 + ldhdr->l_nsyms * LDSYMSZ
2073 + ldhdr->l_nreloc * LDRELSZ);
2074 ldhdr->l_stlen = ldinfo.string_size;
2075 ldhdr->l_stoff = ldhdr->l_impoff + impsize;
2077 /* We now know the final size of the .loader section. Allocate
2079 lsec = xcoff_hash_table (info)->loader_section;
2080 lsec->_raw_size = ldhdr->l_stoff + ldhdr->l_stlen;
2081 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2082 if (lsec->contents == NULL)
2084 bfd_set_error (bfd_error_no_memory);
2088 /* Set up the header. */
2089 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2090 (struct external_ldhdr *) lsec->contents);
2092 /* Set up the import file names. */
2093 out = (char *) lsec->contents + ldhdr->l_impoff;
2094 strcpy (out, libpath);
2095 out += strlen (libpath) + 1;
2098 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2100 register const char *s;
2103 while ((*out++ = *s++) != '\0')
2106 while ((*out++ = *s++) != '\0')
2109 while ((*out++ = *s++) != '\0')
2113 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents)
2116 /* Set up the symbol string table. */
2117 if (ldinfo.string_size > 0)
2119 memcpy (out, ldinfo.strings, ldinfo.string_size);
2120 free (ldinfo.strings);
2121 ldinfo.strings = NULL;
2124 /* We can't set up the symbol table or the relocs yet, because we
2125 don't yet know the final position of the various sections. The
2126 .loader symbols are written out when the corresponding normal
2127 symbols are written out in xcoff_link_input_bfd or
2128 xcoff_write_global_symbol. The .loader relocs are written out
2129 when the corresponding normal relocs are handled in
2130 xcoff_link_input_bfd. */
2132 /* Allocate space for the global linkage section and the global toc
2134 sec = xcoff_hash_table (info)->linkage_section;
2135 if (sec->_raw_size > 0)
2137 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2138 if (sec->contents == NULL)
2140 bfd_set_error (bfd_error_no_memory);
2144 sec = xcoff_hash_table (info)->toc_section;
2145 if (sec->_raw_size > 0)
2147 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2148 if (sec->contents == NULL)
2150 bfd_set_error (bfd_error_no_memory);
2155 /* Now that we've done garbage collection, figure out the contents
2156 of the .debug section. */
2157 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2159 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2162 bfd_size_type symcount;
2163 unsigned long *debug_index;
2165 bfd_byte *esym, *esymend;
2166 bfd_size_type symesz;
2168 if (sub->xvec != info->hash->creator)
2170 subdeb = bfd_get_section_by_name (sub, ".debug");
2171 if (subdeb == NULL || subdeb->_raw_size == 0)
2174 if (info->strip == strip_all
2175 || info->strip == strip_debugger
2176 || info->discard == discard_all)
2178 subdeb->_raw_size = 0;
2182 if (! _bfd_coff_get_external_symbols (sub))
2185 symcount = obj_raw_syment_count (sub);
2186 debug_index = ((unsigned long *)
2187 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2188 if (debug_index == NULL)
2190 bfd_set_error (bfd_error_no_memory);
2193 xcoff_data (sub)->debug_indices = debug_index;
2195 /* Grab the contents of the .debug section. We use malloc and
2196 copy the neams into the debug stringtab, rather than
2197 bfd_alloc, because I expect that, when linking many files
2198 together, many of the strings will be the same. Storing the
2199 strings in the hash table should save space in this case. */
2200 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2201 if (debug_contents == NULL)
2203 bfd_set_error (bfd_error_no_memory);
2206 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2207 (file_ptr) 0, subdeb->_raw_size))
2210 csectpp = xcoff_data (sub)->csects;
2212 symesz = bfd_coff_symesz (sub);
2213 esym = (bfd_byte *) obj_coff_external_syms (sub);
2214 esymend = esym + symcount * symesz;
2215 while (esym < esymend)
2217 struct internal_syment sym;
2219 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2221 *debug_index = (unsigned long) -1;
2223 if (sym._n._n_n._n_zeroes == 0
2226 || ((*csectpp)->flags & SEC_MARK) != 0
2227 || *csectpp == bfd_abs_section_ptr)
2228 && bfd_coff_symname_in_debug (sub, &sym))
2233 name = (char *) debug_contents + sym._n._n_n._n_offset;
2234 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2235 if (indx == (bfd_size_type) -1)
2237 *debug_index = indx;
2240 esym += (sym.n_numaux + 1) * symesz;
2241 csectpp += sym.n_numaux + 1;
2242 debug_index += sym.n_numaux + 1;
2245 free (debug_contents);
2246 debug_contents = NULL;
2248 /* Clear the size of subdeb, so that it is not included directly
2249 in the output file. */
2250 subdeb->_raw_size = 0;
2252 if (! info->keep_memory)
2254 if (! _bfd_coff_free_symbols (sub))
2259 xcoff_hash_table (info)->debug_section->_raw_size =
2260 _bfd_stringtab_size (debug_strtab);
2265 if (ldinfo.strings != NULL)
2266 free (ldinfo.strings);
2267 if (debug_contents != NULL)
2268 free (debug_contents);
2272 /* The mark phase of garbage collection. For a given section, mark
2273 it, and all the sections which define symbols to which it refers. */
2276 xcoff_mark (info, sec)
2277 struct bfd_link_info *info;
2280 if ((sec->flags & SEC_MARK) != 0)
2283 sec->flags |= SEC_MARK;
2285 if (sec->owner->xvec == info->hash->creator
2286 && coff_section_data (sec->owner, sec) != NULL
2287 && xcoff_section_data (sec->owner, sec) != NULL)
2289 register struct xcoff_link_hash_entry **hp, **hpend;
2290 struct internal_reloc *rel, *relend;
2292 /* Mark all the symbols in this section. */
2294 hp = (obj_xcoff_sym_hashes (sec->owner)
2295 + xcoff_section_data (sec->owner, sec)->first_symndx);
2296 hpend = (obj_xcoff_sym_hashes (sec->owner)
2297 + xcoff_section_data (sec->owner, sec)->last_symndx);
2298 for (; hp < hpend; hp++)
2300 register struct xcoff_link_hash_entry *h;
2304 && (h->flags & XCOFF_MARK) == 0)
2306 h->flags |= XCOFF_MARK;
2307 if (h->root.type == bfd_link_hash_defined
2308 || h->root.type == bfd_link_hash_defweak)
2312 hsec = h->root.u.def.section;
2313 if ((hsec->flags & SEC_MARK) == 0)
2315 if (! xcoff_mark (info, hsec))
2320 if (h->toc_section != NULL
2321 && (h->toc_section->flags & SEC_MARK) == 0)
2323 if (! xcoff_mark (info, h->toc_section))
2329 /* Look through the section relocs. */
2331 if ((sec->flags & SEC_RELOC) != 0
2332 && sec->reloc_count > 0)
2334 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2335 (bfd_byte *) NULL, false,
2336 (struct internal_reloc *) NULL);
2339 relend = rel + sec->reloc_count;
2340 for (; rel < relend; rel++)
2343 struct xcoff_link_hash_entry *h;
2345 if ((unsigned int) rel->r_symndx
2346 > obj_raw_syment_count (sec->owner))
2349 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2351 && (h->flags & XCOFF_MARK) == 0)
2353 h->flags |= XCOFF_MARK;
2354 if (h->root.type == bfd_link_hash_defined
2355 || h->root.type == bfd_link_hash_defweak)
2359 hsec = h->root.u.def.section;
2360 if ((hsec->flags & SEC_MARK) == 0)
2362 if (! xcoff_mark (info, hsec))
2367 if (h->toc_section != NULL
2368 && (h->toc_section->flags & SEC_MARK) == 0)
2370 if (! xcoff_mark (info, h->toc_section))
2375 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2377 && (rsec->flags & SEC_MARK) == 0)
2379 if (! xcoff_mark (info, rsec))
2384 if (! info->keep_memory
2385 && coff_section_data (sec->owner, sec) != NULL
2386 && coff_section_data (sec->owner, sec)->relocs != NULL
2387 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2389 free (coff_section_data (sec->owner, sec)->relocs);
2390 coff_section_data (sec->owner, sec)->relocs = NULL;
2398 /* The sweep phase of garbage collection. Remove all garbage
2403 struct bfd_link_info *info;
2407 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2411 for (o = sub->sections; o != NULL; o = o->next)
2413 if ((o->flags & SEC_MARK) == 0)
2415 /* Keep all sections from non-XCOFF input files. Keep
2416 special sections. Keep .debug sections for the
2418 if (sub->xvec != info->hash->creator
2419 || o == xcoff_hash_table (info)->debug_section
2420 || o == xcoff_hash_table (info)->loader_section
2421 || o == xcoff_hash_table (info)->linkage_section
2422 || o == xcoff_hash_table (info)->toc_section
2423 || strcmp (o->name, ".debug") == 0)
2424 o->flags |= SEC_MARK;
2429 o->lineno_count = 0;
2430 if (coff_section_data (sub, o) != NULL
2431 && xcoff_section_data (sub, o) != NULL)
2432 xcoff_hash_table (info)->ldrel_count -=
2433 xcoff_section_data (sub, o)->ldrel_count;
2440 /* Add a symbol to the .loader symbols, if necessary. */
2443 xcoff_build_ldsyms (h, p)
2444 struct xcoff_link_hash_entry *h;
2447 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2450 /* We don't want to garbage collect symbols which are not defined in
2451 XCOFF files. This is a convenient place to mark them. */
2452 if (xcoff_hash_table (ldinfo->info)->gc
2453 && (h->flags & XCOFF_MARK) == 0
2454 && (h->root.type == bfd_link_hash_defined
2455 || h->root.type == bfd_link_hash_defweak)
2456 && (h->root.u.def.section->owner == NULL
2457 || (h->root.u.def.section->owner->xvec
2458 != ldinfo->info->hash->creator)))
2459 h->flags |= XCOFF_MARK;
2461 /* If this symbol is called, and it is defined in a dynamic object,
2462 then we need to set up global linkage code for it. (Unless we
2463 did garbage collection and we didn't need this symbol.) */
2464 if ((h->flags & XCOFF_CALLED) != 0
2465 && (h->flags & XCOFF_DEF_REGULAR) == 0
2466 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2467 && (h->root.type == bfd_link_hash_undefined
2468 || h->root.type == bfd_link_hash_undefweak)
2469 && h->root.root.string[0] == '.'
2470 && (! xcoff_hash_table (ldinfo->info)->gc
2471 || (h->flags & XCOFF_MARK) != 0))
2474 struct xcoff_link_hash_entry *hds;
2476 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2477 h->root.type = bfd_link_hash_defined;
2478 h->root.u.def.section = sec;
2479 h->root.u.def.value = sec->_raw_size;
2481 sec->_raw_size += XCOFF_GLINK_SIZE;
2483 /* The global linkage code requires a TOC entry for the
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 && (hds->flags & XCOFF_REF_DYNAMIC) != 0);
2490 hds->flags |= XCOFF_MARK;
2491 if (hds->toc_section == NULL)
2493 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2494 hds->toc_offset = hds->toc_section->_raw_size;
2495 hds->toc_section->_raw_size += 4;
2496 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2497 ++hds->toc_section->reloc_count;
2499 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2501 /* We need to call xcoff_build_ldsyms recursively here,
2502 because we may already have passed hds on the traversal. */
2503 xcoff_build_ldsyms (hds, p);
2507 /* We need to add a symbol to the .loader section if it is mentioned
2508 in a reloc which we are copying to the .loader section and it was
2509 not defined, or if it is the entry point. */
2511 if (((h->flags & XCOFF_LDREL) == 0
2512 || h->root.type == bfd_link_hash_defined
2513 || h->root.type == bfd_link_hash_defweak)
2514 && (h->flags & XCOFF_ENTRY) == 0)
2520 /* We don't need to add this symbol if we did garbage collection and
2521 we did not mark this symbol. */
2522 if (xcoff_hash_table (ldinfo->info)->gc
2523 && (h->flags & XCOFF_MARK) == 0)
2529 /* We may have already processed this symbol due to the recursive
2531 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2534 /* We need to add this symbol to the .loader symbols. */
2536 /* h->ldsym will already have been allocated for an explicitly
2538 if (h->ldsym == NULL)
2540 h->ldsym = ((struct internal_ldsym *)
2541 bfd_zalloc (ldinfo->output_bfd,
2542 sizeof (struct internal_ldsym)));
2543 if (h->ldsym == NULL)
2545 ldinfo->failed = true;
2546 bfd_set_error (bfd_error_no_memory);
2551 /* The first 3 symbol table indices are reserved to indicate the
2553 h->ldindx = ldinfo->ldsym_count + 3;
2555 ++ldinfo->ldsym_count;
2557 len = strlen (h->root.root.string);
2558 if (len <= SYMNMLEN)
2559 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
2562 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2565 bfd_byte *newstrings;
2567 newalc = ldinfo->string_alc * 2;
2570 while (ldinfo->string_size + len + 3 > newalc)
2573 if (ldinfo->strings == NULL)
2574 newstrings = (bfd_byte *) malloc (newalc);
2576 newstrings = ((bfd_byte *)
2577 realloc ((PTR) ldinfo->strings, newalc));
2578 if (newstrings == NULL)
2580 ldinfo->failed = true;
2581 bfd_set_error (bfd_error_no_memory);
2584 ldinfo->string_alc = newalc;
2585 ldinfo->strings = newstrings;
2588 bfd_put_16 (ldinfo->output_bfd, len + 1,
2589 ldinfo->strings + ldinfo->string_size);
2590 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
2591 h->ldsym->_l._l_l._l_zeroes = 0;
2592 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
2593 ldinfo->string_size += len + 3;
2596 h->flags |= XCOFF_BUILT_LDSYM;
2601 /* Do the final link step. */
2604 _bfd_xcoff_bfd_final_link (abfd, info)
2606 struct bfd_link_info *info;
2608 bfd_size_type symesz;
2609 struct xcoff_final_link_info finfo;
2611 struct bfd_link_order *p;
2612 size_t max_contents_size;
2613 size_t max_sym_count;
2614 size_t max_lineno_count;
2615 size_t max_reloc_count;
2616 size_t max_output_reloc_count;
2617 file_ptr rel_filepos;
2619 file_ptr line_filepos;
2620 unsigned int linesz;
2622 bfd_byte *external_relocs = NULL;
2623 char strbuf[STRING_SIZE_SIZE];
2625 symesz = bfd_coff_symesz (abfd);
2628 finfo.output_bfd = abfd;
2629 finfo.strtab = NULL;
2630 finfo.section_info = NULL;
2631 finfo.last_file_index = -1;
2632 finfo.toc_symindx = -1;
2633 finfo.internal_syms = NULL;
2634 finfo.sym_indices = NULL;
2635 finfo.outsyms = NULL;
2636 finfo.linenos = NULL;
2637 finfo.contents = NULL;
2638 finfo.external_relocs = NULL;
2640 finfo.ldsym = ((struct external_ldsym *)
2641 (xcoff_hash_table (info)->loader_section->contents
2643 finfo.ldrel = ((struct external_ldrel *)
2644 (xcoff_hash_table (info)->loader_section->contents
2646 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
2648 xcoff_data (abfd)->coff.link_info = info;
2650 finfo.strtab = _bfd_stringtab_init ();
2651 if (finfo.strtab == NULL)
2654 /* Compute the file positions for all the sections. */
2655 if (abfd->output_has_begun)
2657 if (xcoff_hash_table (info)->file_align != 0)
2664 file_align = xcoff_hash_table (info)->file_align;
2665 if (file_align != 0)
2667 boolean saw_contents;
2672 /* Insert .pad sections before every section which has
2673 contents and is loaded, if it is preceded by some other
2674 section which has contents and is loaded. */
2675 saw_contents = true;
2676 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
2678 (*op)->target_index = indx;
2679 if (strcmp ((*op)->name, ".pad") == 0)
2680 saw_contents = false;
2681 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
2682 && ((*op)->flags & SEC_LOAD) != 0)
2685 saw_contents = true;
2692 n = bfd_make_section_anyway (abfd, ".pad");
2693 BFD_ASSERT (*op == n);
2695 n->flags = SEC_HAS_CONTENTS;
2696 n->alignment_power = 0;
2697 saw_contents = false;
2702 /* Reset the section indices after inserting the new
2705 for (o = abfd->sections; o != NULL; o = o->next)
2708 o->target_index = indx;
2710 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
2712 /* Work out appropriate sizes for the .pad sections to force
2713 each section to land on a page boundary. This bit of
2714 code knows what compute_section_file_positions is going
2716 sofar = bfd_coff_filhsz (abfd);
2717 if ((abfd->flags & EXEC_P) != 0)
2718 sofar += bfd_coff_aoutsz (abfd);
2724 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
2726 for (o = abfd->sections; o != NULL; o = o->next)
2728 if (strcmp (o->name, ".pad") == 0)
2732 BFD_ASSERT (o->_raw_size == 0);
2733 pageoff = sofar & (file_align - 1);
2736 o->_raw_size = file_align - pageoff;
2737 sofar += file_align - pageoff;
2738 o->flags |= SEC_HAS_CONTENTS;
2743 if ((o->flags & SEC_HAS_CONTENTS) != 0)
2744 sofar += BFD_ALIGN (o->_raw_size,
2745 1 << o->alignment_power);
2750 bfd_coff_compute_section_file_positions (abfd);
2753 /* Count the line numbers and relocation entries required for the
2754 output file. Set the file positions for the relocs. */
2755 rel_filepos = obj_relocbase (abfd);
2756 relsz = bfd_coff_relsz (abfd);
2757 max_contents_size = 0;
2758 max_lineno_count = 0;
2759 max_reloc_count = 0;
2760 for (o = abfd->sections; o != NULL; o = o->next)
2763 o->lineno_count = 0;
2764 for (p = o->link_order_head; p != NULL; p = p->next)
2766 if (p->type == bfd_indirect_link_order)
2770 sec = p->u.indirect.section;
2772 if (info->strip == strip_none
2773 || info->strip == strip_some)
2774 o->lineno_count += sec->lineno_count;
2776 o->reloc_count += sec->reloc_count;
2778 if (sec->_raw_size > max_contents_size)
2779 max_contents_size = sec->_raw_size;
2780 if (sec->lineno_count > max_lineno_count)
2781 max_lineno_count = sec->lineno_count;
2782 if (sec->reloc_count > max_reloc_count)
2783 max_reloc_count = sec->reloc_count;
2785 else if (p->type == bfd_section_reloc_link_order
2786 || p->type == bfd_symbol_reloc_link_order)
2789 if (o->reloc_count == 0)
2793 o->flags |= SEC_RELOC;
2794 o->rel_filepos = rel_filepos;
2795 rel_filepos += o->reloc_count * relsz;
2799 /* Allocate space for the pointers we need to keep for the relocs. */
2803 /* We use section_count + 1, rather than section_count, because
2804 the target_index fields are 1 based. */
2805 finfo.section_info = ((struct xcoff_link_section_info *)
2806 malloc ((abfd->section_count + 1)
2807 * sizeof (struct xcoff_link_section_info)));
2808 if (finfo.section_info == NULL)
2810 bfd_set_error (bfd_error_no_memory);
2813 for (i = 0; i <= abfd->section_count; i++)
2815 finfo.section_info[i].relocs = NULL;
2816 finfo.section_info[i].rel_hashes = NULL;
2820 /* We now know the size of the relocs, so we can determine the file
2821 positions of the line numbers. */
2822 line_filepos = rel_filepos;
2823 linesz = bfd_coff_linesz (abfd);
2824 max_output_reloc_count = 0;
2825 for (o = abfd->sections; o != NULL; o = o->next)
2827 if (o->lineno_count == 0)
2828 o->line_filepos = 0;
2831 o->line_filepos = line_filepos;
2832 line_filepos += o->lineno_count * linesz;
2835 if (o->reloc_count != 0)
2837 /* We don't know the indices of global symbols until we have
2838 written out all the local symbols. For each section in
2839 the output file, we keep an array of pointers to hash
2840 table entries. Each entry in the array corresponds to a
2841 reloc. When we find a reloc against a global symbol, we
2842 set the corresponding entry in this array so that we can
2843 fix up the symbol index after we have written out all the
2846 Because of this problem, we also keep the relocs in
2847 memory until the end of the link. This wastes memory.
2848 We could backpatch the file later, I suppose, although it
2850 finfo.section_info[o->target_index].relocs =
2851 ((struct internal_reloc *)
2852 malloc (o->reloc_count * sizeof (struct internal_reloc)));
2853 finfo.section_info[o->target_index].rel_hashes =
2854 ((struct xcoff_link_hash_entry **)
2855 malloc (o->reloc_count
2856 * sizeof (struct xcoff_link_hash_entry *)));
2857 if (finfo.section_info[o->target_index].relocs == NULL
2858 || finfo.section_info[o->target_index].rel_hashes == NULL)
2860 bfd_set_error (bfd_error_no_memory);
2864 if (o->reloc_count > max_output_reloc_count)
2865 max_output_reloc_count = o->reloc_count;
2868 /* Reset the reloc and lineno counts, so that we can use them to
2869 count the number of entries we have output so far. */
2871 o->lineno_count = 0;
2874 obj_sym_filepos (abfd) = line_filepos;
2876 /* Figure out the largest number of symbols in an input BFD. Take
2877 the opportunity to clear the output_has_begun fields of all the
2878 input BFD's. We want at least 4 symbols, since that is the
2879 number which xcoff_write_global_symbol may need. */
2881 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2885 sub->output_has_begun = false;
2886 sz = obj_raw_syment_count (sub);
2887 if (sz > max_sym_count)
2891 /* Allocate some buffers used while linking. */
2892 finfo.internal_syms = ((struct internal_syment *)
2893 malloc (max_sym_count
2894 * sizeof (struct internal_syment)));
2895 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
2896 finfo.outsyms = ((bfd_byte *)
2897 malloc ((size_t) ((max_sym_count + 1) * symesz)));
2898 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
2899 * bfd_coff_linesz (abfd));
2900 finfo.contents = (bfd_byte *) malloc (max_contents_size);
2901 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
2902 if ((finfo.internal_syms == NULL && max_sym_count > 0)
2903 || (finfo.sym_indices == NULL && max_sym_count > 0)
2904 || finfo.outsyms == NULL
2905 || (finfo.linenos == NULL && max_lineno_count > 0)
2906 || (finfo.contents == NULL && max_contents_size > 0)
2907 || (finfo.external_relocs == NULL && max_reloc_count > 0))
2909 bfd_set_error (bfd_error_no_memory);
2913 obj_raw_syment_count (abfd) = 0;
2914 xcoff_data (abfd)->toc = (bfd_vma) -1;
2916 /* We now know the position of everything in the file, except that
2917 we don't know the size of the symbol table and therefore we don't
2918 know where the string table starts. We just build the string
2919 table in memory as we go along. We process all the relocations
2920 for a single input file at once. */
2921 for (o = abfd->sections; o != NULL; o = o->next)
2923 for (p = o->link_order_head; p != NULL; p = p->next)
2925 if (p->type == bfd_indirect_link_order
2926 && p->u.indirect.section->owner->xvec == abfd->xvec)
2928 sub = p->u.indirect.section->owner;
2929 if (! sub->output_has_begun)
2931 if (! xcoff_link_input_bfd (&finfo, sub))
2933 sub->output_has_begun = true;
2936 else if (p->type == bfd_section_reloc_link_order
2937 || p->type == bfd_symbol_reloc_link_order)
2939 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
2944 if (! _bfd_default_link_order (abfd, info, o, p))
2950 /* Free up the buffers used by xcoff_link_input_bfd. */
2952 if (finfo.internal_syms != NULL)
2954 free (finfo.internal_syms);
2955 finfo.internal_syms = NULL;
2957 if (finfo.sym_indices != NULL)
2959 free (finfo.sym_indices);
2960 finfo.sym_indices = NULL;
2962 if (finfo.linenos != NULL)
2964 free (finfo.linenos);
2965 finfo.linenos = NULL;
2967 if (finfo.contents != NULL)
2969 free (finfo.contents);
2970 finfo.contents = NULL;
2972 if (finfo.external_relocs != NULL)
2974 free (finfo.external_relocs);
2975 finfo.external_relocs = NULL;
2978 /* The value of the last C_FILE symbol is supposed to be -1. Write
2980 if (finfo.last_file_index != -1)
2982 finfo.last_file.n_value = -1;
2983 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
2984 (PTR) finfo.outsyms);
2986 (obj_sym_filepos (abfd)
2987 + finfo.last_file_index * symesz),
2989 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
2993 /* Write out all the global symbols which do not come from XCOFF
2995 xcoff_link_hash_traverse (xcoff_hash_table (info),
2996 xcoff_write_global_symbol,
2999 if (finfo.outsyms != NULL)
3001 free (finfo.outsyms);
3002 finfo.outsyms = NULL;
3005 /* Now that we have written out all the global symbols, we know the
3006 symbol indices to use for relocs against them, and we can finally
3007 write out the relocs. */
3008 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3009 if (external_relocs == NULL && max_output_reloc_count != 0)
3011 bfd_set_error (bfd_error_no_memory);
3015 for (o = abfd->sections; o != NULL; o = o->next)
3017 struct internal_reloc *irel;
3018 struct internal_reloc *irelend;
3019 struct xcoff_link_hash_entry **rel_hash;
3022 if (o->reloc_count == 0)
3025 irel = finfo.section_info[o->target_index].relocs;
3026 irelend = irel + o->reloc_count;
3027 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3028 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3030 if (*rel_hash != NULL)
3032 if ((*rel_hash)->indx < 0)
3034 if (! ((*info->callbacks->unattached_reloc)
3035 (info, (*rel_hash)->root.root.string,
3036 (bfd *) NULL, o, irel->r_vaddr)))
3038 (*rel_hash)->indx = 0;
3040 irel->r_symndx = (*rel_hash)->indx;
3044 /* XCOFF requires that the relocs be sorted by address. We tend
3045 to produce them in the order in which their containing csects
3046 appear in the symbol table, which is not necessarily by
3047 address. So we sort them here. There may be a better way to
3049 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3050 o->reloc_count, sizeof (struct internal_reloc),
3053 irel = finfo.section_info[o->target_index].relocs;
3054 irelend = irel + o->reloc_count;
3055 erel = external_relocs;
3056 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3057 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3059 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3060 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3061 abfd) != relsz * o->reloc_count)
3065 if (external_relocs != NULL)
3067 free (external_relocs);
3068 external_relocs = NULL;
3071 /* Free up the section information. */
3072 if (finfo.section_info != NULL)
3076 for (i = 0; i < abfd->section_count; i++)
3078 if (finfo.section_info[i].relocs != NULL)
3079 free (finfo.section_info[i].relocs);
3080 if (finfo.section_info[i].rel_hashes != NULL)
3081 free (finfo.section_info[i].rel_hashes);
3083 free (finfo.section_info);
3084 finfo.section_info = NULL;
3087 /* Write out the loader section contents. */
3088 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3089 == (xcoff_hash_table (info)->loader_section->contents
3090 + xcoff_hash_table (info)->ldhdr.l_impoff));
3091 o = xcoff_hash_table (info)->loader_section;
3092 if (! bfd_set_section_contents (abfd, o->output_section,
3093 o->contents, o->output_offset,
3097 /* Write out the global linkage section and the toc section. */
3098 o = xcoff_hash_table (info)->linkage_section;
3099 if (o->_raw_size > 0
3100 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3101 o->output_offset, o->_raw_size))
3103 o = xcoff_hash_table (info)->toc_section;
3104 if (o->_raw_size > 0
3105 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3106 o->output_offset, o->_raw_size))
3109 /* Write out the string table. */
3111 (obj_sym_filepos (abfd)
3112 + obj_raw_syment_count (abfd) * symesz),
3116 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3117 (bfd_byte *) strbuf);
3118 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3120 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3123 _bfd_stringtab_free (finfo.strtab);
3125 /* Write out the debugging string table. */
3126 o = xcoff_hash_table (info)->debug_section;
3129 struct bfd_strtab_hash *debug_strtab;
3131 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3132 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3133 >= _bfd_stringtab_size (debug_strtab));
3135 o->output_section->filepos + o->output_offset,
3138 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3142 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3143 not try to write out the symbols. */
3144 bfd_get_symcount (abfd) = 0;
3149 if (finfo.strtab != NULL)
3150 _bfd_stringtab_free (finfo.strtab);
3151 if (finfo.section_info != NULL)
3155 for (i = 0; i < abfd->section_count; i++)
3157 if (finfo.section_info[i].relocs != NULL)
3158 free (finfo.section_info[i].relocs);
3159 if (finfo.section_info[i].rel_hashes != NULL)
3160 free (finfo.section_info[i].rel_hashes);
3162 free (finfo.section_info);
3164 if (finfo.internal_syms != NULL)
3165 free (finfo.internal_syms);
3166 if (finfo.sym_indices != NULL)
3167 free (finfo.sym_indices);
3168 if (finfo.outsyms != NULL)
3169 free (finfo.outsyms);
3170 if (finfo.linenos != NULL)
3171 free (finfo.linenos);
3172 if (finfo.contents != NULL)
3173 free (finfo.contents);
3174 if (finfo.external_relocs != NULL)
3175 free (finfo.external_relocs);
3176 if (external_relocs != NULL)
3177 free (external_relocs);
3181 /* Link an input file into the linker output file. This function
3182 handles all the sections and relocations of the input file at once. */
3185 xcoff_link_input_bfd (finfo, input_bfd)
3186 struct xcoff_final_link_info *finfo;
3190 const char *strings;
3191 bfd_size_type syment_base;
3192 unsigned int n_tmask;
3193 unsigned int n_btshft;
3195 bfd_size_type isymesz;
3196 bfd_size_type osymesz;
3197 bfd_size_type linesz;
3200 struct internal_syment *isymp;
3202 unsigned long *debug_index;
3204 unsigned long output_index;
3206 struct xcoff_link_hash_entry **sym_hash;
3210 /* We can just skip DYNAMIC files, unless this is a static link. */
3211 if ((input_bfd->flags & DYNAMIC) != 0
3212 && ! finfo->info->static_link)
3215 /* Move all the symbols to the output file. */
3217 output_bfd = finfo->output_bfd;
3219 syment_base = obj_raw_syment_count (output_bfd);
3220 isymesz = bfd_coff_symesz (input_bfd);
3221 osymesz = bfd_coff_symesz (output_bfd);
3222 linesz = bfd_coff_linesz (input_bfd);
3223 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3225 n_tmask = coff_data (input_bfd)->local_n_tmask;
3226 n_btshft = coff_data (input_bfd)->local_n_btshft;
3228 /* Define macros so that ISFCN, et. al., macros work correctly. */
3229 #define N_TMASK n_tmask
3230 #define N_BTSHFT n_btshft
3233 if (! finfo->info->keep_memory)
3236 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3239 if (! _bfd_coff_get_external_symbols (input_bfd))
3242 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3243 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3244 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3245 csectpp = xcoff_data (input_bfd)->csects;
3246 debug_index = xcoff_data (input_bfd)->debug_indices;
3247 isymp = finfo->internal_syms;
3248 indexp = finfo->sym_indices;
3249 output_index = syment_base;
3250 outsym = finfo->outsyms;
3252 while (esym < esym_end)
3254 struct internal_syment isym;
3255 union internal_auxent aux;
3261 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3263 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3265 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3267 BFD_ASSERT (isymp->n_numaux > 0);
3268 bfd_coff_swap_aux_in (input_bfd,
3269 (PTR) (esym + isymesz * isymp->n_numaux),
3270 isymp->n_type, isymp->n_sclass,
3271 isymp->n_numaux - 1, isymp->n_numaux,
3273 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3276 /* Make a copy of *isymp so that the relocate_section function
3277 always sees the original values. This is more reliable than
3278 always recomputing the symbol value even if we are stripping
3282 /* If this symbol is in the .loader section, swap out the
3283 .loader symbol information. If this is an external symbol
3284 reference to a defined symbol, though, then wait until we get
3285 to the definition. */
3286 if (isym.n_sclass == C_EXT
3287 && *sym_hash != NULL
3288 && (*sym_hash)->ldsym != NULL
3290 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3292 struct xcoff_link_hash_entry *h;
3293 struct internal_ldsym *ldsym;
3297 if (isym.n_scnum > 0)
3299 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3300 ldsym->l_value = (isym.n_value
3301 + (*csectpp)->output_section->vma
3302 + (*csectpp)->output_offset
3307 ldsym->l_scnum = isym.n_scnum;
3308 ldsym->l_value = isym.n_value;
3311 ldsym->l_smtype = smtyp;
3312 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3313 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3314 || (h->flags & XCOFF_IMPORT) != 0)
3315 ldsym->l_smtype |= L_IMPORT;
3316 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3317 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3318 || (h->flags & XCOFF_EXPORT) != 0)
3319 ldsym->l_smtype |= L_EXPORT;
3320 if ((h->flags & XCOFF_ENTRY) != 0)
3321 ldsym->l_smtype |= L_ENTRY;
3323 ldsym->l_smclas = aux.x_csect.x_smclas;
3325 if (ldsym->l_ifile == (bfd_size_type) -1)
3327 else if (ldsym->l_ifile == 0)
3329 if ((ldsym->l_smtype & L_IMPORT) == 0)
3335 if (h->root.type == bfd_link_hash_defined
3336 || h->root.type == bfd_link_hash_defweak)
3337 impbfd = h->root.u.def.section->owner;
3338 else if (h->root.type == bfd_link_hash_undefined
3339 || h->root.type == bfd_link_hash_undefweak)
3340 impbfd = h->root.u.undef.abfd;
3348 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3349 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3356 BFD_ASSERT (h->ldindx >= 0);
3357 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3358 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3359 finfo->ldsym + h->ldindx - 3);
3367 add = 1 + isym.n_numaux;
3369 /* If we are skipping this csect, we want to skip this symbol. */
3370 if (*csectpp == NULL)
3373 /* If we garbage collected this csect, we want to skip this
3376 && xcoff_hash_table (finfo->info)->gc
3377 && ((*csectpp)->flags & SEC_MARK) == 0
3378 && *csectpp != bfd_abs_section_ptr)
3381 /* An XCOFF linker always skips C_STAT symbols. */
3383 && isymp->n_sclass == C_STAT)
3386 /* We skip all but the first TOC anchor. */
3388 && isymp->n_sclass == C_HIDEXT
3389 && aux.x_csect.x_smclas == XMC_TC0)
3391 if (finfo->toc_symindx != -1)
3395 finfo->toc_symindx = output_index;
3396 xcoff_data (finfo->output_bfd)->toc =
3397 ((*csectpp)->output_section->vma
3398 + (*csectpp)->output_offset
3405 /* If we are stripping all symbols, we want to skip this one. */
3407 && finfo->info->strip == strip_all)
3410 /* We can skip resolved external references. */
3412 && isym.n_sclass == C_EXT
3414 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3417 /* We can skip common symbols if they got defined somewhere
3420 && isym.n_sclass == C_EXT
3422 && ((*sym_hash)->flags & XCOFF_DEF_REGULAR) != 0)
3425 /* Skip local symbols if we are discarding them. */
3427 && finfo->info->discard == discard_all
3428 && isym.n_sclass != C_EXT
3429 && (isym.n_sclass != C_HIDEXT
3430 || smtyp != XTY_SD))
3433 /* If we stripping debugging symbols, and this is a debugging
3434 symbol, then skip it. */
3436 && finfo->info->strip == strip_debugger
3437 && isym.n_scnum == N_DEBUG)
3440 /* If some symbols are stripped based on the name, work out the
3441 name and decide whether to skip this symbol. We don't handle
3442 this correctly for symbols whose names are in the .debug
3443 section; to get it right we would need a new bfd_strtab_hash
3444 function to return the string given the index. */
3446 && (finfo->info->strip == strip_some
3447 || finfo->info->discard == discard_l)
3448 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3451 char buf[SYMNMLEN + 1];
3453 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3457 if ((finfo->info->strip == strip_some
3458 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
3460 || (finfo->info->discard == discard_l
3461 && (isym.n_sclass != C_EXT
3462 && (isym.n_sclass != C_HIDEXT
3463 || smtyp != XTY_SD))
3464 && strncmp (name, finfo->info->lprefix,
3465 finfo->info->lprefix_len) == 0))
3469 /* On the other hand, we can't skip global symbols which have
3470 relocs against them. */
3472 && isym.n_sclass == C_EXT
3473 && (*sym_hash)->indx == -2
3474 && finfo->info->strip != strip_all)
3477 /* We can not skip the first TOC anchor. */
3480 && finfo->info->strip != strip_all)
3483 /* We now know whether we are to skip this symbol or not. */
3486 /* Adjust the symbol in order to output it. */
3488 if (isym._n._n_n._n_zeroes == 0
3489 && isym._n._n_n._n_offset != 0)
3491 /* This symbol has a long name. Enter it in the string
3492 table we are building. If *debug_index != -1, the
3493 name has already been entered in the .debug section. */
3494 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3495 isym._n._n_n._n_offset = *debug_index;
3501 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
3505 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3506 if (indx == (bfd_size_type) -1)
3508 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3512 if (isym.n_sclass == C_BSTAT)
3516 /* The value of a C_BSTAT symbol is the symbol table
3517 index of the containing csect. */
3519 indx = isym.n_value;
3520 if (indx < obj_raw_syment_count (input_bfd))
3524 symindx = finfo->sym_indices[indx];
3528 isym.n_value = symindx;
3531 else if (isym.n_scnum > 0)
3533 isym.n_scnum = (*csectpp)->output_section->target_index;
3534 isym.n_value += ((*csectpp)->output_section->vma
3535 + (*csectpp)->output_offset
3539 /* The value of a C_FILE symbol is the symbol index of the
3540 next C_FILE symbol. The value of the last C_FILE symbol
3541 is -1. We try to get this right, below, just before we
3542 write the symbols out, but in the general case we may
3543 have to write the symbol out twice. */
3544 if (isym.n_sclass == C_FILE)
3546 if (finfo->last_file_index != -1
3547 && finfo->last_file.n_value != (long) output_index)
3549 /* We must correct the value of the last C_FILE entry. */
3550 finfo->last_file.n_value = output_index;
3551 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3553 /* The last C_FILE symbol is in this input file. */
3554 bfd_coff_swap_sym_out (output_bfd,
3555 (PTR) &finfo->last_file,
3556 (PTR) (finfo->outsyms
3557 + ((finfo->last_file_index
3563 /* We have already written out the last C_FILE
3564 symbol. We need to write it out again. We
3565 borrow *outsym temporarily. */
3566 bfd_coff_swap_sym_out (output_bfd,
3567 (PTR) &finfo->last_file,
3569 if (bfd_seek (output_bfd,
3570 (obj_sym_filepos (output_bfd)
3571 + finfo->last_file_index * osymesz),
3573 || (bfd_write (outsym, osymesz, 1, output_bfd)
3579 finfo->last_file_index = output_index;
3580 finfo->last_file = isym;
3583 /* Output the symbol. */
3585 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
3587 *indexp = output_index;
3589 if (isym.n_sclass == C_EXT)
3592 struct xcoff_link_hash_entry *h;
3594 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3596 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3597 BFD_ASSERT (h != NULL);
3598 h->indx = output_index;
3601 output_index += add;
3602 outsym += add * osymesz;
3605 esym += add * isymesz;
3609 if (debug_index != NULL)
3612 for (--add; add > 0; --add)
3616 /* Fix up the aux entries. This must be done in a separate pass,
3617 because we don't know the correct symbol indices until we have
3618 already decided which symbols we are going to keep. */
3620 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3621 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3622 isymp = finfo->internal_syms;
3623 indexp = finfo->sym_indices;
3624 csectpp = xcoff_data (input_bfd)->csects;
3625 outsym = finfo->outsyms;
3626 while (esym < esym_end)
3630 add = 1 + isymp->n_numaux;
3633 esym += add * isymesz;
3641 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3643 union internal_auxent aux;
3645 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
3646 isymp->n_sclass, i, isymp->n_numaux,
3649 if (isymp->n_sclass == C_FILE)
3651 /* This is the file name (or some comment put in by
3652 the compiler). If it is long, we must put it in
3653 the string table. */
3654 if (aux.x_file.x_n.x_zeroes == 0
3655 && aux.x_file.x_n.x_offset != 0)
3657 const char *filename;
3660 BFD_ASSERT (aux.x_file.x_n.x_offset
3661 >= STRING_SIZE_SIZE);
3662 if (strings == NULL)
3664 strings = _bfd_coff_read_string_table (input_bfd);
3665 if (strings == NULL)
3668 filename = strings + aux.x_file.x_n.x_offset;
3669 indx = _bfd_stringtab_add (finfo->strtab, filename,
3671 if (indx == (bfd_size_type) -1)
3673 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3676 else if ((isymp->n_sclass == C_EXT
3677 || isymp->n_sclass == C_HIDEXT)
3678 && i + 1 == isymp->n_numaux)
3680 /* We don't support type checking. I don't know if
3682 aux.x_csect.x_parmhash = 0;
3683 /* I don't think anybody uses these fields, but we'd
3684 better clobber them just in case. */
3685 aux.x_csect.x_stab = 0;
3686 aux.x_csect.x_snstab = 0;
3687 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
3691 indx = aux.x_csect.x_scnlen.l;
3692 if (indx < obj_raw_syment_count (input_bfd))
3696 symindx = finfo->sym_indices[indx];
3698 aux.x_sym.x_tagndx.l = 0;
3700 aux.x_sym.x_tagndx.l = symindx;
3704 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
3708 if (ISFCN (isymp->n_type)
3709 || ISTAG (isymp->n_sclass)
3710 || isymp->n_sclass == C_BLOCK)
3712 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
3714 && indx < obj_raw_syment_count (input_bfd))
3716 /* We look forward through the symbol for
3717 the index of the next symbol we are going
3718 to include. I don't know if this is
3720 while (finfo->sym_indices[indx] < 0
3721 && indx < obj_raw_syment_count (input_bfd))
3723 if (indx >= obj_raw_syment_count (input_bfd))
3724 indx = output_index;
3726 indx = finfo->sym_indices[indx];
3727 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
3731 indx = aux.x_sym.x_tagndx.l;
3732 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
3736 symindx = finfo->sym_indices[indx];
3738 aux.x_sym.x_tagndx.l = 0;
3740 aux.x_sym.x_tagndx.l = symindx;
3744 /* Copy over the line numbers, unless we are stripping
3745 them. We do this on a symbol by symbol basis in
3746 order to more easily handle garbage collection. */
3747 if ((isymp->n_sclass == C_EXT
3748 || isymp->n_sclass == C_HIDEXT)
3750 && isymp->n_numaux > 1
3751 && ISFCN (isymp->n_type)
3752 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
3754 if (finfo->info->strip != strip_none
3755 && finfo->info->strip != strip_some)
3756 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
3759 asection *enclosing;
3760 bfd_size_type linoff;
3761 struct internal_lineno lin;
3764 enclosing = xcoff_section_data (abfd, o)->enclosing;
3765 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
3766 - enclosing->line_filepos);
3768 if (bfd_seek (input_bfd,
3769 enclosing->line_filepos + linoff,
3771 || (bfd_read (finfo->linenos, linesz,
3772 o->lineno_count, input_bfd)
3773 != linesz * o->lineno_count))
3776 bfd_coff_swap_lineno_in (input_bfd,
3777 (PTR) finfo->linenos,
3780 || ((bfd_size_type) lin.l_addr.l_symndx
3784 obj_coff_external_syms (input_bfd)))
3786 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
3789 bfd_byte *linpend, *linp;
3791 bfd_size_type count;
3793 lin.l_addr.l_symndx = *indexp;
3794 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
3795 (PTR) finfo->linenos);
3797 linpend = (finfo->linenos
3798 + o->lineno_count * linesz);
3799 offset = (o->output_section->vma
3802 for (linp = finfo->linenos + linesz;
3806 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
3808 if (lin.l_lnno == 0)
3810 lin.l_addr.l_paddr += offset;
3811 bfd_coff_swap_lineno_out (output_bfd,
3816 count = (linp - finfo->linenos) / linesz;
3818 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
3819 (o->output_section->line_filepos
3820 + o->output_section->lineno_count * linesz);
3822 if (bfd_seek (output_bfd,
3823 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
3825 || (bfd_write (finfo->linenos, linesz, count,
3830 o->output_section->lineno_count += count;
3835 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
3836 isymp->n_sclass, i, isymp->n_numaux,
3848 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
3849 symbol will be the first symbol in the next input file. In the
3850 normal case, this will save us from writing out the C_FILE symbol
3852 if (finfo->last_file_index != -1
3853 && (bfd_size_type) finfo->last_file_index >= syment_base)
3855 finfo->last_file.n_value = output_index;
3856 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
3857 (PTR) (finfo->outsyms
3858 + ((finfo->last_file_index - syment_base)
3862 /* Write the modified symbols to the output file. */
3863 if (outsym > finfo->outsyms)
3865 if (bfd_seek (output_bfd,
3866 obj_sym_filepos (output_bfd) + syment_base * osymesz,
3868 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
3870 != (bfd_size_type) (outsym - finfo->outsyms)))
3873 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
3874 + (outsym - finfo->outsyms) / osymesz)
3877 obj_raw_syment_count (output_bfd) = output_index;
3880 /* Don't let the linker relocation routines discard the symbols. */
3881 keep_syms = obj_coff_keep_syms (input_bfd);
3882 obj_coff_keep_syms (input_bfd) = true;
3884 /* Relocate the contents of each section. */
3885 for (o = input_bfd->sections; o != NULL; o = o->next)
3889 if ((o->flags & SEC_HAS_CONTENTS) == 0
3890 || o->_raw_size == 0
3891 || (o->flags & SEC_IN_MEMORY) != 0)
3894 /* We have set filepos correctly for the sections we created to
3895 represent csects, so bfd_get_section_contents should work. */
3896 if (coff_section_data (input_bfd, o) != NULL
3897 && coff_section_data (input_bfd, o)->contents != NULL)
3898 contents = coff_section_data (input_bfd, o)->contents;
3901 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
3902 (file_ptr) 0, o->_raw_size))
3904 contents = finfo->contents;
3907 if ((o->flags & SEC_RELOC) != 0)
3910 struct internal_reloc *internal_relocs;
3911 struct internal_reloc *irel;
3913 struct internal_reloc *irelend;
3914 struct xcoff_link_hash_entry **rel_hash;
3917 /* Read in the relocs. */
3918 target_index = o->output_section->target_index;
3919 internal_relocs = (xcoff_read_internal_relocs
3920 (input_bfd, o, false, finfo->external_relocs,
3922 (finfo->section_info[target_index].relocs
3923 + o->output_section->reloc_count)));
3924 if (internal_relocs == NULL)
3927 /* Call processor specific code to relocate the section
3929 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
3933 finfo->internal_syms,
3934 xcoff_data (input_bfd)->csects))
3937 offset = o->output_section->vma + o->output_offset - o->vma;
3938 irel = internal_relocs;
3939 irelend = irel + o->reloc_count;
3940 rel_hash = (finfo->section_info[target_index].rel_hashes
3941 + o->output_section->reloc_count);
3942 for (; irel < irelend; irel++, rel_hash++)
3944 struct xcoff_link_hash_entry *h = NULL;
3945 struct internal_ldrel ldrel;
3949 /* Adjust the reloc address and symbol index. */
3951 irel->r_vaddr += offset;
3953 r_symndx = irel->r_symndx;
3957 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
3960 /* This is a global symbol. */
3962 irel->r_symndx = h->indx;
3965 /* This symbol is being written at the end
3966 of the file, and we do not yet know the
3967 symbol index. We save the pointer to the
3968 hash table entry in the rel_hash list.
3969 We set the indx field to -2 to indicate
3970 that this symbol must not be stripped. */
3979 indx = finfo->sym_indices[r_symndx];
3983 struct internal_syment *is;
3985 /* Relocations against a TC0 TOC anchor are
3986 automatically transformed to be against
3987 the TOC anchor in the output file. */
3988 is = finfo->internal_syms + r_symndx;
3989 if (is->n_sclass == C_HIDEXT
3990 && is->n_numaux > 0)
3993 union internal_auxent aux;
3997 obj_coff_external_syms (input_bfd))
3998 + ((r_symndx + is->n_numaux)
4000 bfd_coff_swap_aux_in (input_bfd, auxptr,
4001 is->n_type, is->n_sclass,
4005 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4006 && aux.x_csect.x_smclas == XMC_TC0)
4007 indx = finfo->toc_symindx;
4012 irel->r_symndx = indx;
4015 struct internal_syment *is;
4017 char buf[SYMNMLEN + 1];
4019 /* This reloc is against a symbol we are
4020 stripping. It would be possible to handle
4021 this case, but I don't think it's worth it. */
4022 is = finfo->internal_syms + r_symndx;
4024 name = (_bfd_coff_internal_syment_name
4025 (input_bfd, is, buf));
4029 if (! ((*finfo->info->callbacks->unattached_reloc)
4030 (finfo->info, name, input_bfd, o,
4037 switch (irel->r_type)
4045 /* This reloc needs to be copied into the .loader
4047 ldrel.l_vaddr = irel->r_vaddr;
4049 ldrel.l_symndx = -1;
4054 sec = xcoff_data (input_bfd)->csects[r_symndx];
4055 if ((sec->flags & SEC_CODE) != 0)
4057 else if ((sec->flags & SEC_HAS_CONTENTS) != 0)
4062 else if (h->root.type == bfd_link_hash_defined
4063 || h->root.type == bfd_link_hash_defweak)
4067 sec = h->root.u.def.section->output_section;
4068 if ((sec->flags & SEC_CODE) != 0)
4070 else if ((sec->flags & SEC_HAS_CONTENTS) != 0)
4079 (*_bfd_error_handler)
4080 ("%s: `%s' in loader reloc but not loader sym",
4081 bfd_get_filename (input_bfd),
4082 h->root.root.string);
4083 bfd_set_error (bfd_error_bad_value);
4086 ldrel.l_symndx = h->ldindx;
4088 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4089 ldrel.l_rsecnm = o->output_section->target_index;
4090 if (xcoff_hash_table (finfo->info)->textro
4091 && (o->output_section->flags & SEC_CODE) != 0)
4093 (*_bfd_error_handler)
4094 ("%s: loader reloc in read-only section %s",
4095 bfd_get_filename (input_bfd),
4096 bfd_get_section_name (finfo->output_bfd,
4097 o->output_section));
4098 bfd_set_error (bfd_error_invalid_operation);
4101 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4103 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4108 o->output_section->reloc_count += o->reloc_count;
4111 /* Write out the modified section contents. */
4112 if (! bfd_set_section_contents (output_bfd, o->output_section,
4113 contents, o->output_offset,
4114 (o->_cooked_size != 0
4120 obj_coff_keep_syms (input_bfd) = keep_syms;
4122 if (! finfo->info->keep_memory)
4124 if (! _bfd_coff_free_symbols (input_bfd))
4134 /* Write out a non-XCOFF global symbol. */
4137 xcoff_write_global_symbol (h, p)
4138 struct xcoff_link_hash_entry *h;
4141 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4144 struct internal_syment isym;
4145 union internal_auxent aux;
4147 output_bfd = finfo->output_bfd;
4149 /* If this symbol was garbage collected, just skip it. */
4150 if (xcoff_hash_table (finfo->info)->gc
4151 && (h->flags & XCOFF_MARK) == 0)
4154 /* If we need a .loader section entry, write it out. */
4155 if (h->ldsym != NULL)
4157 struct internal_ldsym *ldsym;
4162 if (h->root.type == bfd_link_hash_undefined
4163 || h->root.type == bfd_link_hash_undefweak)
4166 ldsym->l_scnum = N_UNDEF;
4167 ldsym->l_smtype = XTY_ER;
4168 impbfd = h->root.u.undef.abfd;
4170 else if (h->root.type == bfd_link_hash_defined
4171 || h->root.type == bfd_link_hash_defweak)
4175 sec = h->root.u.def.section;
4176 ldsym->l_value = (sec->output_section->vma
4177 + sec->output_offset
4178 + h->root.u.def.value);
4179 ldsym->l_scnum = sec->output_section->target_index;
4180 ldsym->l_smtype = XTY_SD;
4181 impbfd = sec->owner;
4186 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4187 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4188 || (h->flags & XCOFF_IMPORT) != 0)
4189 ldsym->l_smtype |= L_IMPORT;
4190 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4191 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4192 || (h->flags & XCOFF_EXPORT) != 0)
4193 ldsym->l_smtype |= L_EXPORT;
4194 if ((h->flags & XCOFF_ENTRY) != 0)
4195 ldsym->l_smtype |= L_ENTRY;
4197 ldsym->l_smclas = h->smclas;
4199 if (ldsym->l_ifile == (bfd_size_type) -1)
4201 else if (ldsym->l_ifile == 0)
4203 if ((ldsym->l_smtype & L_IMPORT) == 0)
4205 else if (impbfd == NULL)
4209 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4210 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4216 BFD_ASSERT (h->ldindx >= 0);
4217 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4218 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4222 /* If this symbol needs global linkage code, write it out. */
4223 if (h->root.type == bfd_link_hash_defined
4224 && (h->root.u.def.section
4225 == xcoff_hash_table (finfo->info)->linkage_section))
4231 p = h->root.u.def.section->contents + h->root.u.def.value;
4233 /* The first instruction in the global linkage code loads a
4234 specific TOC element. */
4235 tocoff = (h->descriptor->toc_section->output_section->vma
4236 + h->descriptor->toc_section->output_offset
4237 + h->descriptor->toc_offset
4238 - xcoff_data (output_bfd)->toc);
4239 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4241 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
4243 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
4246 /* If we created a TOC entry for this symbol, write out the required
4248 if ((h->flags & XCOFF_SET_TOC) != 0)
4253 struct internal_reloc *irel;
4254 struct internal_ldrel ldrel;
4256 tocsec = h->toc_section;
4257 osec = tocsec->output_section;
4258 oindx = osec->target_index;
4259 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4260 irel->r_vaddr = (osec->vma
4261 + tocsec->output_offset
4264 irel->r_symndx = h->indx;
4268 irel->r_symndx = obj_raw_syment_count (output_bfd);
4270 irel->r_type = R_POS;
4272 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4273 ++osec->reloc_count;
4275 BFD_ASSERT (h->ldindx >= 0);
4276 ldrel.l_vaddr = irel->r_vaddr;
4277 ldrel.l_symndx = h->ldindx;
4278 ldrel.l_rtype = (31 << 8) | R_POS;
4279 ldrel.l_rsecnm = oindx;
4280 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4288 && (finfo->info->strip == strip_all
4289 || (finfo->info->strip == strip_some
4290 && (bfd_hash_lookup (finfo->info->keep_hash,
4291 h->root.root.string, false, false)
4296 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
4299 outsym = finfo->outsyms;
4301 memset (&aux, 0, sizeof aux);
4303 h->indx = obj_raw_syment_count (output_bfd);
4305 if (strlen (h->root.root.string) <= SYMNMLEN)
4306 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
4313 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4315 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
4317 if (indx == (bfd_size_type) -1)
4319 isym._n._n_n._n_zeroes = 0;
4320 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4323 if (h->root.type == bfd_link_hash_undefined
4324 || h->root.type == bfd_link_hash_undefweak)
4327 isym.n_scnum = N_UNDEF;
4328 isym.n_sclass = C_EXT;
4329 aux.x_csect.x_smtyp = XTY_ER;
4331 else if (h->root.type == bfd_link_hash_defined
4332 || h->root.type == bfd_link_hash_defweak)
4334 isym.n_value = (h->root.u.def.section->output_section->vma
4335 + h->root.u.def.section->output_offset
4336 + h->root.u.def.value);
4337 isym.n_scnum = h->root.u.def.section->output_section->target_index;
4338 isym.n_sclass = C_HIDEXT;
4339 aux.x_csect.x_smtyp = XTY_SD;
4340 /* I don't know what the csect length should be in this case. */
4345 isym.n_type = T_NULL;
4348 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4349 outsym += bfd_coff_symesz (output_bfd);
4351 aux.x_csect.x_smclas = h->smclas;
4353 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
4355 outsym += bfd_coff_auxesz (output_bfd);
4357 if (h->root.type == bfd_link_hash_defined
4358 || h->root.type == bfd_link_hash_defweak)
4360 /* We just output an SD symbol. Now output an LD symbol. */
4364 isym.n_sclass = C_EXT;
4365 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4366 outsym += bfd_coff_symesz (output_bfd);
4368 aux.x_csect.x_smtyp = XTY_LD;
4369 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
4371 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
4373 outsym += bfd_coff_auxesz (output_bfd);
4376 if (bfd_seek (output_bfd,
4377 (obj_sym_filepos (output_bfd)
4378 + (obj_raw_syment_count (output_bfd)
4379 * bfd_coff_symesz (output_bfd))),
4381 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
4382 != (bfd_size_type) (outsym - finfo->outsyms)))
4384 obj_raw_syment_count (output_bfd) +=
4385 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4390 /* Handle a link order which is supposed to generate a reloc. */
4393 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
4395 struct xcoff_final_link_info *finfo;
4396 asection *output_section;
4397 struct bfd_link_order *link_order;
4399 reloc_howto_type *howto;
4400 struct internal_reloc *irel;
4401 struct xcoff_link_hash_entry **rel_hash_ptr;
4403 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4406 bfd_set_error (bfd_error_bad_value);
4410 if (link_order->u.reloc.p->addend != 0)
4414 bfd_reloc_status_type rstat;
4417 size = bfd_get_reloc_size (howto);
4418 buf = (bfd_byte *) bfd_zmalloc (size);
4421 bfd_set_error (bfd_error_no_memory);
4425 rstat = _bfd_relocate_contents (howto, output_bfd,
4426 link_order->u.reloc.p->addend, buf);
4432 case bfd_reloc_outofrange:
4434 case bfd_reloc_overflow:
4435 if (! ((*finfo->info->callbacks->reloc_overflow)
4437 (link_order->type == bfd_section_reloc_link_order
4438 ? bfd_section_name (output_bfd,
4439 link_order->u.reloc.p->u.section)
4440 : link_order->u.reloc.p->u.name),
4441 howto->name, link_order->u.reloc.p->addend,
4442 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
4449 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4450 (file_ptr) link_order->offset, size);
4456 /* Store the reloc information in the right place. It will get
4457 swapped and written out at the end of the final_link routine. */
4459 irel = (finfo->section_info[output_section->target_index].relocs
4460 + output_section->reloc_count);
4461 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
4462 + output_section->reloc_count);
4464 memset (irel, 0, sizeof (struct internal_reloc));
4465 *rel_hash_ptr = NULL;
4467 irel->r_vaddr = output_section->vma + link_order->offset;
4469 if (link_order->type == bfd_section_reloc_link_order)
4471 /* We need to somehow locate a symbol in the right section. The
4472 symbol must either have a value of zero, or we must adjust
4473 the addend by the value of the symbol. FIXME: Write this
4474 when we need it. The old linker couldn't handle this anyhow. */
4476 *rel_hash_ptr = NULL;
4481 struct xcoff_link_hash_entry *h;
4483 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
4484 link_order->u.reloc.p->u.name,
4485 false, false, true);
4489 irel->r_symndx = h->indx;
4492 /* Set the index to -2 to force this symbol to get
4501 if (! ((*finfo->info->callbacks->unattached_reloc)
4502 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4503 (asection *) NULL, (bfd_vma) 0)))
4509 irel->r_type = howto->type;
4510 irel->r_size = howto->bitsize - 1;
4511 if (howto->complain_on_overflow == complain_overflow_signed)
4512 irel->r_size |= 0x80;
4514 ++output_section->reloc_count;
4519 /* Sort relocs by VMA. This is called via qsort. */
4522 xcoff_sort_relocs (p1, p2)
4526 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4527 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4529 if (r1->r_vaddr > r2->r_vaddr)
4531 else if (r1->r_vaddr < r2->r_vaddr)
4537 /* This is the relocation function for the RS/6000/POWER/PowerPC.
4538 This is currently the only processor which uses XCOFF; I hope that
4539 will never change. */
4542 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
4543 input_section, contents, relocs, syms,
4546 struct bfd_link_info *info;
4548 asection *input_section;
4550 struct internal_reloc *relocs;
4551 struct internal_syment *syms;
4552 asection **sections;
4554 struct internal_reloc *rel;
4555 struct internal_reloc *relend;
4558 relend = rel + input_section->reloc_count;
4559 for (; rel < relend; rel++)
4562 struct xcoff_link_hash_entry *h;
4563 struct internal_syment *sym;
4566 struct reloc_howto_struct howto;
4567 bfd_reloc_status_type rstat;
4569 /* Relocation type R_REF is a special relocation type which is
4570 merely used to prevent garbage collection from occurring for
4571 the csect including the symbol which it references. */
4572 if (rel->r_type == R_REF)
4575 symndx = rel->r_symndx;
4585 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
4586 sym = syms + symndx;
4587 addend = - sym->n_value;
4590 /* We build the howto information on the fly. */
4592 howto.type = rel->r_type;
4593 howto.rightshift = 0;
4595 howto.bitsize = (rel->r_size & 0x1f) + 1;
4596 howto.pc_relative = false;
4598 if ((rel->r_size & 0x80) != 0)
4599 howto.complain_on_overflow = complain_overflow_signed;
4601 howto.complain_on_overflow = complain_overflow_bitfield;
4602 howto.special_function = NULL;
4603 howto.name = "internal";
4604 howto.partial_inplace = true;
4605 if (howto.bitsize == 32)
4606 howto.src_mask = howto.dst_mask = 0xffffffff;
4609 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
4610 if (howto.bitsize == 16)
4613 howto.pcrel_offset = false;
4623 sec = bfd_abs_section_ptr;
4628 sec = sections[symndx];
4629 val = (sec->output_section->vma
4630 + sec->output_offset
4637 if (h->root.type == bfd_link_hash_defined
4638 || h->root.type == bfd_link_hash_defweak)
4642 sec = h->root.u.def.section;
4643 val = (h->root.u.def.value
4644 + sec->output_section->vma
4645 + sec->output_offset);
4647 else if ((h->flags & XCOFF_REF_DYNAMIC) != 0
4648 || (h->flags & XCOFF_IMPORT) != 0)
4650 /* Every symbol in a shared object is defined somewhere. */
4653 else if (! info->relocateable)
4655 if (! ((*info->callbacks->undefined_symbol)
4656 (info, h->root.root.string, input_bfd, input_section,
4657 rel->r_vaddr - input_section->vma)))
4662 /* I took the relocation type definitions from two documents:
4663 the PowerPC AIX Version 4 Application Binary Interface, First
4664 Edition (April 1992), and the PowerOpen ABI, Big-Endian
4665 32-Bit Hardware Implementation (June 30, 1994). Differences
4666 between the documents are noted below. */
4668 switch (rel->r_type)
4673 /* These relocs are defined by the PowerPC ABI to be
4674 relative branches which use half of the difference
4675 between the symbol and the program counter. I can't
4676 quite figure out when this is useful. These relocs are
4677 not defined by the PowerOpen ABI. */
4679 (*_bfd_error_handler)
4680 ("%s: unsupported relocation type 0x%02x",
4681 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
4682 bfd_set_error (bfd_error_bad_value);
4685 /* Simple positive relocation. */
4688 /* Simple negative relocation. */
4692 /* Simple PC relative relocation. */
4693 howto.pc_relative = true;
4696 /* TOC relative relocation. The value in the instruction in
4697 the input file is the offset from the input file TOC to
4698 the desired location. We want the offset from the final
4699 TOC to the desired location. We have:
4704 so we must change insn by on - in.
4707 /* Global linkage relocation. The value of this relocation
4708 is the address of the entry in the TOC section. */
4710 /* Local object TOC address. I can't figure out the
4711 difference between this and case R_GL. */
4713 /* TOC relative relocation. A TOC relative load instruction
4714 which may be changed to a load address instruction.
4715 FIXME: We don't currently implement this optimization. */
4717 /* TOC relative relocation. This is a TOC relative load
4718 address instruction which may be changed to a load
4719 instruction. FIXME: I don't know if this is the correct
4721 if (h != NULL && h->toc_section == NULL)
4723 (*_bfd_error_handler)
4724 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
4725 bfd_get_filename (input_bfd), rel->r_vaddr,
4726 h->root.root.string);
4727 bfd_set_error (bfd_error_bad_value);
4731 val = (h->toc_section->output_section->vma
4732 + h->toc_section->output_offset
4734 val = ((val - xcoff_data (output_bfd)->toc)
4735 - (sym->n_value - xcoff_data (input_bfd)->toc));
4739 /* Absolute branch. We don't want to mess with the lower
4740 two bits of the instruction. */
4742 /* The PowerPC ABI defines this as an absolute call which
4743 may be modified to become a relative call. The PowerOpen
4744 ABI does not define this relocation type. */
4746 /* Absolute branch which may be modified to become a
4749 /* The PowerPC ABI defines this as an absolute branch to a
4750 fixed address which may be modified to an absolute branch
4751 to a symbol. The PowerOpen ABI does not define this
4754 /* The PowerPC ABI defines this as an absolute branch to a
4755 fixed address which may be modified to a relative branch.
4756 The PowerOpen ABI does not define this relocation type. */
4757 howto.src_mask &= ~3;
4758 howto.dst_mask = howto.src_mask;
4761 /* Relative branch. We don't want to mess with the lower
4762 two bits of the instruction. */
4764 /* The PowerPC ABI defines this as a relative call which may
4765 be modified to become an absolute call. The PowerOpen
4766 ABI does not define this relocation type. */
4768 /* A relative branch which may be modified to become an
4769 absolute branch. FIXME: We don't implement this,
4770 although we should for symbols of storage mapping class
4772 howto.pc_relative = true;
4773 howto.src_mask &= ~3;
4774 howto.dst_mask = howto.src_mask;
4777 /* The PowerPC AIX ABI describes this as a load which may be
4778 changed to a load address. The PowerOpen ABI says this
4779 is the same as case R_POS. */
4782 /* The PowerPC AIX ABI describes this as a load address
4783 which may be changed to a load. The PowerOpen ABI says
4784 this is the same as R_POS. */
4788 /* If we see an R_BR or R_RBR reloc which is jumping to global
4789 linkage code, and it is followed by an appropriate cror nop
4790 instruction, we replace the cror with lwz r2,20(r1). This
4791 restores the TOC after the glink code. Contrariwise, if the
4792 call is followed by a lwz r2,20(r1), but the call is not
4793 going to global linkage code, we can replace the load with a
4795 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
4797 && h->root.type == bfd_link_hash_defined
4798 && (rel->r_vaddr - input_section->vma + 8
4799 <= input_section->_cooked_size))
4804 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
4805 next = bfd_get_32 (input_bfd, pnext);
4806 if (h->smclas == XMC_GL)
4808 if (next == 0x4def7b82 /* cror 15,15,15 */
4809 || next == 0x4ffffb82) /* cror 31,31,31 */
4810 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
4814 if (next == 0x80410014) /* lwz r1,20(r1) */
4815 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
4819 /* A PC relative reloc includes the section address. */
4820 if (howto.pc_relative)
4821 addend += input_section->vma;
4823 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
4825 rel->r_vaddr - input_section->vma,
4834 case bfd_reloc_overflow:
4837 char buf[SYMNMLEN + 1];
4838 char howto_name[10];
4843 name = h->root.root.string;
4846 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
4850 sprintf (howto_name, "0x%02x", rel->r_type);
4852 if (! ((*info->callbacks->reloc_overflow)
4853 (info, name, howto_name, (bfd_vma) 0, input_bfd,
4854 input_section, rel->r_vaddr - input_section->vma)))