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. */
30 #define STRING_SIZE_SIZE (4)
32 /* Get the XCOFF hash table entries for a BFD. */
33 #define obj_xcoff_sym_hashes(bfd) \
34 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
36 /* XCOFF relocation types. These probably belong in a header file
37 somewhere. The relocations are described in the function
38 _bfd_ppc_xcoff_relocate_section in this file. */
54 #define R_RRTBI (0x14)
55 #define R_RRTBA (0x15)
63 /* The first word of global linkage code. This must be modified by
64 filling in the correct TOC offset. */
66 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
68 /* The remaining words of global linkage code. */
70 static unsigned long xcoff_glink_code[] =
72 0x90410014, /* stw r2,20(r1) */
73 0x800c0000, /* lwz r0,0(r12) */
74 0x804c0004, /* lwz r2,4(r12) */
75 0x7c0903a6, /* mtctr r0 */
76 0x4e800420, /* bctr */
77 0x0, /* start of traceback table */
78 0x000c8000, /* traceback table */
79 0x0 /* traceback table */
82 #define XCOFF_GLINK_SIZE \
83 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
85 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
86 This flag will only be used on input sections. */
88 #define SEC_MARK (SEC_ROM)
90 /* The ldhdr structure. This appears at the start of the .loader
95 /* The version number: currently always 1. */
96 unsigned long l_version;
97 /* The number of symbol table entries. */
98 bfd_size_type l_nsyms;
99 /* The number of relocation table entries. */
100 bfd_size_type l_nreloc;
101 /* The length of the import file string table. */
102 bfd_size_type l_istlen;
103 /* The number of import files. */
104 bfd_size_type l_nimpid;
105 /* The offset from the start of the .loader section to the first
106 entry in the import file table. */
107 bfd_size_type l_impoff;
108 /* The length of the string table. */
109 bfd_size_type l_stlen;
110 /* The offset from the start of the .loader section to the first
111 entry in the string table. */
112 bfd_size_type l_stoff;
115 struct external_ldhdr
117 bfd_byte l_version[4];
119 bfd_byte l_nreloc[4];
120 bfd_byte l_istlen[4];
121 bfd_byte l_nimpid[4];
122 bfd_byte l_impoff[4];
127 #define LDHDRSZ (8 * 4)
129 /* The ldsym structure. This is used to represent a symbol in the
132 struct internal_ldsym
136 /* The symbol name if <= SYMNMLEN characters. */
137 char _l_name[SYMNMLEN];
140 /* Zero if the symbol name is more than SYMNMLEN characters. */
142 /* The offset in the string table if the symbol name is more
143 than SYMNMLEN characters. */
147 /* The symbol value. */
149 /* The symbol section number. */
151 /* The symbol type and flags. */
153 /* The symbol storage class. */
155 /* The import file ID. */
156 bfd_size_type l_ifile;
157 /* Offset to the parameter type check string. */
158 bfd_size_type l_parm;
161 struct external_ldsym
165 bfd_byte _l_name[SYMNMLEN];
168 bfd_byte _l_zeroes[4];
169 bfd_byte _l_offset[4];
174 bfd_byte l_smtype[1];
175 bfd_byte l_smclas[1];
180 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
182 /* These flags are for the l_smtype field (the lower three bits are an
185 /* Imported symbol. */
186 #define L_IMPORT (0x40)
188 #define L_ENTRY (0x20)
189 /* Exported symbol. */
190 #define L_EXPORT (0x10)
192 /* The ldrel structure. This is used to represent a reloc in the
195 struct internal_ldrel
197 /* The reloc address. */
199 /* The symbol table index in the .loader section symbol table. */
200 bfd_size_type l_symndx;
201 /* The relocation type and size. */
203 /* The section number this relocation applies to. */
207 struct external_ldrel
210 bfd_byte l_symndx[4];
212 bfd_byte l_rsecnm[2];
215 #define LDRELSZ (2 * 4 + 2 * 2)
217 /* The list of import files. */
219 struct xcoff_import_file
221 /* The next entry in the list. */
222 struct xcoff_import_file *next;
227 /* The member name. */
231 /* An entry in the XCOFF linker hash table. */
233 struct xcoff_link_hash_entry
235 struct bfd_link_hash_entry root;
237 /* Symbol index in output file. Set to -1 initially. Set to -2 if
238 there is a reloc against this symbol. */
241 /* If we have created a TOC entry for this symbol, this is the .tc
242 section which holds it. */
243 asection *toc_section;
247 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
248 set), this is the offset in toc_section. */
250 /* If the TOC entry comes from an input file, this is set to the
251 symbo lindex of the C_HIDEXT XMC_TC symbol. */
255 /* If this symbol is a function entry point which is called, this
256 field holds a pointer to the function descriptor. */
257 struct xcoff_link_hash_entry *descriptor;
259 /* The .loader symbol table entry, if there is one. */
260 struct internal_ldsym *ldsym;
262 /* The .loader symbol table index. */
265 /* Some linker flags. */
266 unsigned short flags;
267 /* Symbol is referenced by a regular object. */
268 #define XCOFF_REF_REGULAR (01)
269 /* Symbol is defined by a regular object. */
270 #define XCOFF_DEF_REGULAR (02)
271 /* Symbol is referenced by a dynamic object. */
272 #define XCOFF_REF_DYNAMIC (04)
273 /* Symbol is used in a reloc being copied into the .loader section. */
274 #define XCOFF_LDREL (010)
275 /* Symbol is the entry point. */
276 #define XCOFF_ENTRY (020)
277 /* Symbol is called; this is, it appears in a R_BR reloc. */
278 #define XCOFF_CALLED (040)
279 /* Symbol needs the TOC entry filled in. */
280 #define XCOFF_SET_TOC (0100)
281 /* Symbol is explicitly imported. */
282 #define XCOFF_IMPORT (0200)
283 /* Symbol is explicitly exported. */
284 #define XCOFF_EXPORT (0400)
285 /* Symbol has been processed by xcoff_build_ldsyms. */
286 #define XCOFF_BUILT_LDSYM (01000)
287 /* Symbol is mentioned by a section which was not garbage collected. */
288 #define XCOFF_MARK (02000)
290 /* The storage mapping class. */
291 unsigned char smclas;
294 /* The XCOFF linker hash table. */
296 struct xcoff_link_hash_table
298 struct bfd_link_hash_table root;
300 /* The .debug string hash table. We need to compute this while
301 reading the input files, so that we know how large the .debug
302 section will be before we assign section positions. */
303 struct bfd_strtab_hash *debug_strtab;
305 /* The .debug section we will use for the final output. */
306 asection *debug_section;
308 /* The .loader section we will use for the final output. */
309 asection *loader_section;
311 /* A count of non TOC relative relocs which will need to be
312 allocated in the .loader section. */
315 /* The .loader section header. */
316 struct internal_ldhdr ldhdr;
318 /* The .gl section we use to hold global linkage code. */
319 asection *linkage_section;
321 /* The .tc section we use to hold toc entries we build for global
323 asection *toc_section;
325 /* The list of import files. */
326 struct xcoff_import_file *imports;
328 /* Required alignment of sections within the output file. */
329 unsigned long file_align;
331 /* Whether the .text section must be read-only. */
334 /* Whether garbage collection was done. */
338 /* Information we keep for each section in the output file during the
341 struct xcoff_link_section_info
343 /* The relocs to be output. */
344 struct internal_reloc *relocs;
345 /* For each reloc against a global symbol whose index was not known
346 when the reloc was handled, the global hash table entry. */
347 struct xcoff_link_hash_entry **rel_hashes;
350 /* Information that we pass around while doing the final link step. */
352 struct xcoff_final_link_info
354 /* General link information. */
355 struct bfd_link_info *info;
358 /* Hash table for long symbol names. */
359 struct bfd_strtab_hash *strtab;
360 /* Array of information kept for each output section, indexed by the
361 target_index field. */
362 struct xcoff_link_section_info *section_info;
363 /* Symbol index of last C_FILE symbol (-1 if none). */
364 long last_file_index;
365 /* Contents of last C_FILE symbol. */
366 struct internal_syment last_file;
367 /* Symbol index of TOC symbol. */
369 /* Start of .loader symbols. */
370 struct external_ldsym *ldsym;
371 /* Next .loader reloc to swap out. */
372 struct external_ldrel *ldrel;
373 /* Buffer large enough to hold swapped symbols of any input file. */
374 struct internal_syment *internal_syms;
375 /* Buffer large enough to hold output indices of symbols of any
378 /* Buffer large enough to hold output symbols for any input file. */
380 /* Buffer large enough to hold external line numbers for any input
383 /* Buffer large enough to hold any input section. */
385 /* Buffer large enough to hold external relocs of any input section. */
386 bfd_byte *external_relocs;
389 static void xcoff_swap_ldhdr_out
390 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
391 static void xcoff_swap_ldsym_out
392 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
393 static void xcoff_swap_ldrel_out
394 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
395 static struct bfd_hash_entry *xcoff_link_hash_newfunc
396 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
397 static struct internal_reloc *xcoff_read_internal_relocs
398 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
399 struct internal_reloc *));
400 static boolean xcoff_link_add_object_symbols
401 PARAMS ((bfd *, struct bfd_link_info *));
402 static boolean xcoff_link_check_archive_element
403 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
404 static boolean xcoff_link_check_ar_symbols
405 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
406 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
407 static boolean xcoff_link_add_dynamic_symbols
408 PARAMS ((bfd *, struct bfd_link_info *));
409 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
410 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
411 static boolean xcoff_build_ldsyms
412 PARAMS ((struct xcoff_link_hash_entry *, PTR));
413 static boolean xcoff_link_input_bfd
414 PARAMS ((struct xcoff_final_link_info *, bfd *));
415 static boolean xcoff_write_global_symbol
416 PARAMS ((struct xcoff_link_hash_entry *, PTR));
417 static boolean xcoff_reloc_link_order
418 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
419 struct bfd_link_order *));
420 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
422 /* Routines to swap information in the XCOFF .loader section. We only
423 need to swap this information out, not in. I believe that only the
424 loader needs to swap this information in. If we ever need to write
425 an XCOFF loader, this stuff will need to be moved to another file
426 shared by the linker (which XCOFF calls the ``binder'') and the
429 /* Swap out the ldhdr structure. */
432 xcoff_swap_ldhdr_out (abfd, src, dst)
434 const struct internal_ldhdr *src;
435 struct external_ldhdr *dst;
437 bfd_put_32 (abfd, src->l_version, dst->l_version);
438 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
439 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
440 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
441 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
442 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
443 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
444 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
447 /* Swap out the ldsym structure. */
450 xcoff_swap_ldsym_out (abfd, src, dst)
452 const struct internal_ldsym *src;
453 struct external_ldsym *dst;
455 if (src->_l._l_l._l_zeroes != 0)
456 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
459 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
460 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
462 bfd_put_32 (abfd, src->l_value, dst->l_value);
463 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
464 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
465 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
466 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
467 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
470 /* Swap out the ldrel structure. */
473 xcoff_swap_ldrel_out (abfd, src, dst)
475 const struct internal_ldrel *src;
476 struct external_ldrel *dst;
478 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
479 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
480 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
481 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
484 /* Routine to create an entry in an XCOFF link hash table. */
486 static struct bfd_hash_entry *
487 xcoff_link_hash_newfunc (entry, table, string)
488 struct bfd_hash_entry *entry;
489 struct bfd_hash_table *table;
492 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
494 /* Allocate the structure if it has not already been allocated by a
496 if (ret == (struct xcoff_link_hash_entry *) NULL)
497 ret = ((struct xcoff_link_hash_entry *)
498 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
499 if (ret == (struct xcoff_link_hash_entry *) NULL)
501 bfd_set_error (bfd_error_no_memory);
502 return (struct bfd_hash_entry *) ret;
505 /* Call the allocation method of the superclass. */
506 ret = ((struct xcoff_link_hash_entry *)
507 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
511 /* Set local fields. */
513 ret->toc_section = NULL;
514 ret->u.toc_indx = -1;
515 ret->descriptor = NULL;
519 ret->smclas = XMC_UA;
522 return (struct bfd_hash_entry *) ret;
525 /* Create a XCOFF link hash table. */
527 struct bfd_link_hash_table *
528 _bfd_xcoff_bfd_link_hash_table_create (abfd)
531 struct xcoff_link_hash_table *ret;
533 ret = ((struct xcoff_link_hash_table *)
534 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
535 if (ret == (struct xcoff_link_hash_table *) NULL)
537 bfd_set_error (bfd_error_no_memory);
538 return (struct bfd_link_hash_table *) NULL;
540 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
542 bfd_release (abfd, ret);
543 return (struct bfd_link_hash_table *) NULL;
546 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
547 ret->debug_section = NULL;
548 ret->loader_section = NULL;
549 ret->ldrel_count = 0;
550 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
551 ret->linkage_section = NULL;
552 ret->toc_section = NULL;
561 /* Look up an entry in an XCOFF link hash table. */
563 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
564 ((struct xcoff_link_hash_entry *) \
565 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
568 /* Traverse an XCOFF link hash table. */
570 #define xcoff_link_hash_traverse(table, func, info) \
571 (bfd_link_hash_traverse \
573 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
576 /* Get the XCOFF link hash table from the info structure. This is
579 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
581 /* Read internal relocs for an XCOFF csect. This is a wrapper around
582 _bfd_coff_read_internal_relocs which tries to take advantage of any
583 relocs which may have been cached for the enclosing section. */
585 static struct internal_reloc *
586 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
587 require_internal, internal_relocs)
591 bfd_byte *external_relocs;
592 boolean require_internal;
593 struct internal_reloc *internal_relocs;
595 if (coff_section_data (abfd, sec) != NULL
596 && coff_section_data (abfd, sec)->relocs == NULL
597 && xcoff_section_data (abfd, sec) != NULL)
601 enclosing = xcoff_section_data (abfd, sec)->enclosing;
603 if (enclosing != NULL
604 && (coff_section_data (abfd, enclosing) == NULL
605 || coff_section_data (abfd, enclosing)->relocs == NULL)
607 && enclosing->reloc_count > 0)
609 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
610 external_relocs, false,
611 (struct internal_reloc *) NULL)
616 if (enclosing != NULL
617 && coff_section_data (abfd, enclosing) != NULL
618 && coff_section_data (abfd, enclosing)->relocs != NULL)
622 off = ((sec->rel_filepos - enclosing->rel_filepos)
623 / bfd_coff_relsz (abfd));
624 if (! require_internal)
625 return coff_section_data (abfd, enclosing)->relocs + off;
626 memcpy (internal_relocs,
627 coff_section_data (abfd, enclosing)->relocs + off,
628 sec->reloc_count * sizeof (struct internal_reloc));
629 return internal_relocs;
633 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
634 require_internal, internal_relocs);
637 /* Given an XCOFF BFD, add symbols to the global hash table as
641 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
643 struct bfd_link_info *info;
645 switch (bfd_get_format (abfd))
648 return xcoff_link_add_object_symbols (abfd, info);
650 return (_bfd_generic_link_add_archive_symbols
651 (abfd, info, xcoff_link_check_archive_element));
653 bfd_set_error (bfd_error_wrong_format);
658 /* Add symbols from an XCOFF object file. */
661 xcoff_link_add_object_symbols (abfd, info)
663 struct bfd_link_info *info;
665 if (! _bfd_coff_get_external_symbols (abfd))
667 if (! xcoff_link_add_symbols (abfd, info))
669 if (! info->keep_memory)
671 if (! _bfd_coff_free_symbols (abfd))
677 /* Check a single archive element to see if we need to include it in
678 the link. *PNEEDED is set according to whether this element is
679 needed in the link or not. This is called via
680 _bfd_generic_link_add_archive_symbols. */
683 xcoff_link_check_archive_element (abfd, info, pneeded)
685 struct bfd_link_info *info;
688 if (! _bfd_coff_get_external_symbols (abfd))
691 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
696 if (! xcoff_link_add_symbols (abfd, info))
700 if (! info->keep_memory || ! *pneeded)
702 if (! _bfd_coff_free_symbols (abfd))
709 /* Look through the symbols to see if this object file should be
710 included in the link. */
713 xcoff_link_check_ar_symbols (abfd, info, pneeded)
715 struct bfd_link_info *info;
718 bfd_size_type symesz;
724 symesz = bfd_coff_symesz (abfd);
725 esym = (bfd_byte *) obj_coff_external_syms (abfd);
726 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
727 while (esym < esym_end)
729 struct internal_syment sym;
731 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
733 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
736 char buf[SYMNMLEN + 1];
737 struct bfd_link_hash_entry *h;
739 /* This symbol is externally visible, and is defined by this
742 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
745 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
747 /* We are only interested in symbols that are currently
748 undefined. If a symbol is currently known to be common,
749 XCOFF linkers do not bring in an object file which
750 defines it. We also don't bring in symbols to satisfy
751 undefined references in shared objects. */
752 if (h != (struct bfd_link_hash_entry *) NULL
753 && h->type == bfd_link_hash_undefined)
755 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
762 esym += (sym.n_numaux + 1) * symesz;
765 /* We do not need this object file. */
769 /* Add all the symbols from an object file to the hash table.
771 XCOFF is a weird format. A normal XCOFF .o files will have three
772 COFF sections--.text, .data, and .bss--but each COFF section will
773 contain many csects. These csects are described in the symbol
774 table. From the linker's point of view, each csect must be
775 considered a section in its own right. For example, a TOC entry is
776 handled as a small XMC_TC csect. The linker must be able to merge
777 different TOC entries together, which means that it must be able to
778 extract the XMC_TC csects from the .data section of the input .o
781 From the point of view of our linker, this is, of course, a hideous
782 nightmare. We cope by actually creating sections for each csect,
783 and discarding the original sections. We then have to handle the
784 relocation entries carefully, since the only way to tell which
785 csect they belong to is to examine the address. */
788 xcoff_link_add_symbols (abfd, info)
790 struct bfd_link_info *info;
792 unsigned int n_tmask;
793 unsigned int n_btshft;
794 boolean default_copy;
795 bfd_size_type symcount;
796 struct xcoff_link_hash_entry **sym_hash;
797 asection **csect_cache;
798 bfd_size_type linesz;
802 unsigned int csect_index;
803 asection *first_csect;
804 bfd_size_type symesz;
807 struct reloc_info_struct
809 struct internal_reloc *relocs;
812 } *reloc_info = NULL;
814 if ((abfd->flags & DYNAMIC) != 0
815 && ! info->static_link)
816 return xcoff_link_add_dynamic_symbols (abfd, info);
818 n_tmask = coff_data (abfd)->local_n_tmask;
819 n_btshft = coff_data (abfd)->local_n_btshft;
821 /* Define macros so that ISFCN, et. al., macros work correctly. */
822 #define N_TMASK n_tmask
823 #define N_BTSHFT n_btshft
825 /* We need to build a .loader section, so we do it here. This won't
826 work if we're producing an XCOFF output file with no non dynamic
827 XCOFF input files. FIXME. */
828 if (xcoff_hash_table (info)->loader_section == NULL)
832 lsec = bfd_make_section_anyway (abfd, ".loader");
835 xcoff_hash_table (info)->loader_section = lsec;
836 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
838 /* Likewise for the linkage section. */
839 if (xcoff_hash_table (info)->linkage_section == NULL)
843 lsec = bfd_make_section_anyway (abfd, ".gl");
846 xcoff_hash_table (info)->linkage_section = lsec;
847 lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
849 /* Likewise for the TOC section. */
850 if (xcoff_hash_table (info)->toc_section == NULL)
854 tsec = bfd_make_section_anyway (abfd, ".tc");
857 xcoff_hash_table (info)->toc_section = tsec;
858 tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
860 /* Likewise for the .debug section. */
861 if (xcoff_hash_table (info)->debug_section == NULL)
865 dsec = bfd_make_section_anyway (abfd, ".debug");
868 xcoff_hash_table (info)->debug_section = dsec;
869 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
872 if (info->keep_memory)
873 default_copy = false;
877 symcount = obj_raw_syment_count (abfd);
879 /* We keep a list of the linker hash table entries that correspond
880 to each external symbol. */
881 sym_hash = ((struct xcoff_link_hash_entry **)
884 * sizeof (struct xcoff_link_hash_entry *))));
885 if (sym_hash == NULL && symcount != 0)
887 bfd_set_error (bfd_error_no_memory);
890 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
892 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
894 /* Because of the weird stuff we are doing with XCOFF csects, we can
895 not easily determine which section a symbol is in, so we store
896 the information in the tdata for the input file. */
897 csect_cache = ((asection **)
898 bfd_alloc (abfd, symcount * sizeof (asection *)));
899 if (csect_cache == NULL && symcount != 0)
901 bfd_set_error (bfd_error_no_memory);
904 xcoff_data (abfd)->csects = csect_cache;
905 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
907 /* While splitting sections into csects, we need to assign the
908 relocs correctly. The relocs and the csects must both be in
909 order by VMA within a given section, so we handle this by
910 scanning along the relocs as we process the csects. We index
911 into reloc_info using the section target_index. */
912 reloc_info = ((struct reloc_info_struct *)
913 malloc ((abfd->section_count + 1)
914 * sizeof (struct reloc_info_struct)));
915 if (reloc_info == NULL)
917 bfd_set_error (bfd_error_no_memory);
920 memset ((PTR) reloc_info, 0,
921 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
923 /* Read in the relocs and line numbers for each section. */
924 linesz = bfd_coff_linesz (abfd);
925 for (sub = abfd->sections; sub != NULL; sub = sub->next)
927 if ((sub->flags & SEC_RELOC) != 0)
929 reloc_info[sub->target_index].relocs =
930 xcoff_read_internal_relocs (abfd, sub, true, (bfd_byte *) NULL,
931 false, (struct internal_reloc *) NULL);
932 reloc_info[sub->target_index].csects =
933 (asection **) malloc (sub->reloc_count * sizeof (asection *));
934 if (reloc_info[sub->target_index].csects == NULL)
936 bfd_set_error (bfd_error_no_memory);
939 memset (reloc_info[sub->target_index].csects, 0,
940 sub->reloc_count * sizeof (asection *));
943 if ((info->strip == strip_none || info->strip == strip_some)
944 && sub->lineno_count > 0)
948 linenos = (bfd_byte *) malloc (sub->lineno_count * linesz);
951 bfd_set_error (bfd_error_no_memory);
954 reloc_info[sub->target_index].linenos = linenos;
955 if (bfd_seek (abfd, sub->line_filepos, SEEK_SET) != 0
956 || (bfd_read (linenos, linesz, sub->lineno_count, abfd)
957 != linesz * sub->lineno_count))
962 /* Don't let the linker relocation routines discard the symbols. */
963 keep_syms = obj_coff_keep_syms (abfd);
964 obj_coff_keep_syms (abfd) = true;
970 symesz = bfd_coff_symesz (abfd);
971 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
972 esym = (bfd_byte *) obj_coff_external_syms (abfd);
973 esym_end = esym + symcount * symesz;
974 while (esym < esym_end)
976 struct internal_syment sym;
977 union internal_auxent aux;
979 char buf[SYMNMLEN + 1];
984 struct xcoff_link_hash_entry *set_toc;
986 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
988 /* In this pass we are only interested in symbols with csect
990 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
992 if (sym.n_sclass == C_FILE && csect != NULL)
994 xcoff_section_data (abfd, csect)->last_symndx =
996 - (bfd_byte *) obj_coff_external_syms (abfd))
1002 *csect_cache = csect;
1003 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1004 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1006 *csect_cache = NULL;
1007 esym += (sym.n_numaux + 1) * symesz;
1008 sym_hash += sym.n_numaux + 1;
1009 csect_cache += sym.n_numaux + 1;
1013 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1017 /* If this symbol has line number information attached to it,
1018 and we're not stripping it, count the number of entries and
1019 add them to the count for this csect. In the final link pass
1020 we are going to attach line number information by symbol,
1021 rather than by section, in order to more easily handle
1022 garbage collection. */
1023 if ((info->strip == strip_none || info->strip == strip_some)
1026 && ISFCN (sym.n_type))
1028 union internal_auxent auxlin;
1030 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1031 sym.n_type, sym.n_sclass,
1032 0, sym.n_numaux, (PTR) &auxlin);
1033 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1035 asection *enclosing;
1036 bfd_size_type linoff;
1038 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1039 if (enclosing == NULL)
1041 (*_bfd_error_handler)
1042 ("%s: `%s' has line numbers but no enclosing section",
1043 bfd_get_filename (abfd), name);
1044 bfd_set_error (bfd_error_bad_value);
1047 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1048 - enclosing->line_filepos);
1049 if (linoff < enclosing->lineno_count * linesz)
1051 struct internal_lineno lin;
1052 bfd_byte *linpstart;
1054 linpstart = (reloc_info[enclosing->target_index].linenos
1056 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1058 && ((bfd_size_type) lin.l_addr.l_symndx
1060 - (bfd_byte *) obj_coff_external_syms (abfd))
1063 bfd_byte *linpend, *linp;
1065 linpend = (reloc_info[enclosing->target_index].linenos
1066 + enclosing->lineno_count * linesz);
1067 for (linp = linpstart + linesz;
1071 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1073 if (lin.l_lnno == 0)
1076 csect->lineno_count += (linp - linpstart) / linesz;
1077 /* The setting of line_filepos will only be
1078 useful if all the line number entries for a
1079 csect are contiguous; this only matters for
1081 if (csect->line_filepos == 0)
1082 csect->line_filepos =
1083 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1089 /* Pick up the csect auxiliary information. */
1091 if (sym.n_numaux == 0)
1093 (*_bfd_error_handler)
1094 ("%s: class %d symbol `%s' has no aux entries",
1095 bfd_get_filename (abfd), sym.n_sclass, name);
1096 bfd_set_error (bfd_error_bad_value);
1100 bfd_coff_swap_aux_in (abfd,
1101 (PTR) (esym + symesz * sym.n_numaux),
1102 sym.n_type, sym.n_sclass,
1103 sym.n_numaux - 1, sym.n_numaux,
1106 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1116 (*_bfd_error_handler)
1117 ("%s: symbol `%s' has unrecognized csect type %d",
1118 bfd_get_filename (abfd), name, smtyp);
1119 bfd_set_error (bfd_error_bad_value);
1123 /* This is an external reference. */
1124 if (sym.n_sclass == C_HIDEXT
1125 || sym.n_scnum != N_UNDEF
1126 || aux.x_csect.x_scnlen.l != 0)
1128 (*_bfd_error_handler)
1129 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1130 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1131 aux.x_csect.x_scnlen.l);
1132 bfd_set_error (bfd_error_bad_value);
1135 section = bfd_und_section_ptr;
1139 /* This is a csect definition. */
1143 xcoff_section_data (abfd, csect)->last_symndx =
1145 - (bfd_byte *) obj_coff_external_syms (abfd))
1152 /* When we see a TOC anchor, we record the TOC value. */
1153 if (aux.x_csect.x_smclas == XMC_TC0)
1155 if (sym.n_sclass != C_HIDEXT
1156 || aux.x_csect.x_scnlen.l != 0)
1158 (*_bfd_error_handler)
1159 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1160 bfd_get_filename (abfd), name, sym.n_sclass,
1161 aux.x_csect.x_scnlen.l);
1162 bfd_set_error (bfd_error_bad_value);
1165 xcoff_data (abfd)->toc = sym.n_value;
1168 /* We must merge TOC entries for the same symbol. We can
1169 merge two TOC entries if they are both C_HIDEXT, they
1170 both have the same name, they are both 4 bytes long, and
1171 they both have a relocation table entry for an external
1172 symbol with the same name. Unfortunately, this means
1173 that we must look through the relocations. Ick. */
1174 if (aux.x_csect.x_smclas == XMC_TC
1175 && sym.n_sclass == C_HIDEXT
1176 && aux.x_csect.x_scnlen.l == 4
1177 && info->hash->creator == abfd->xvec)
1179 asection *enclosing;
1180 bfd_size_type relindx;
1181 struct internal_reloc *rel;
1182 asection **rel_csect;
1184 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1185 if (enclosing == NULL)
1188 /* XCOFF requires that relocs be sorted by address, so
1189 we could do a binary search here. FIXME. */
1190 rel = reloc_info[enclosing->target_index].relocs;
1191 rel_csect = reloc_info[enclosing->target_index].csects;
1193 relindx < enclosing->reloc_count;
1194 relindx++, rel++, rel_csect++)
1196 if (*rel_csect == NULL
1197 && rel->r_vaddr == (bfd_vma) sym.n_value
1198 && rel->r_size == 31
1199 && rel->r_type == R_POS)
1202 if (relindx < enclosing->reloc_count)
1205 struct internal_syment relsym;
1207 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1208 + rel->r_symndx * symesz);
1209 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1210 if (relsym.n_sclass == C_EXT)
1212 const char *relname;
1213 char relbuf[SYMNMLEN + 1];
1215 struct xcoff_link_hash_entry *h;
1217 /* At this point we know that the TOC entry is
1218 for an externally visible symbol. */
1219 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1221 if (relname == NULL)
1224 /* We only merge TOC entries if the TC name is
1225 the same as the symbol name. This handles
1226 the normal case, but not common cases like
1227 SYM.P4 which gcc generates to store SYM + 4
1228 in the TOC. FIXME. */
1229 if (strcmp (name, relname) == 0)
1231 copy = (! info->keep_memory
1232 || relsym._n._n_n._n_zeroes != 0
1233 || relsym._n._n_n._n_offset == 0);
1234 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1235 relname, true, copy,
1240 /* At this point h->root.type could be
1241 bfd_link_hash_new. That should be OK,
1242 since we know for sure that we will come
1243 across this symbol as we step through the
1246 /* We store h in *sym_hash for the
1247 convenience of the relocate_section
1251 if (h->toc_section != NULL)
1253 /* We already have a TOC entry for this
1254 symbol, so we can just ignore this
1256 *rel_csect = bfd_und_section_ptr;
1260 /* We are about to create a TOC entry for
1268 /* We need to create a new section. We get the name from
1269 the csect storage mapping class, so that the linker can
1270 accumulate similar csects together. */
1272 static const char *csect_name_by_class[] =
1274 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1275 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1278 const char *csect_name;
1279 asection *enclosing;
1280 struct internal_reloc *rel;
1281 bfd_size_type relindx;
1282 asection **rel_csect;
1284 if ((aux.x_csect.x_smclas >=
1285 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1286 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1288 (*_bfd_error_handler)
1289 ("%s: symbol `%s' has unrecognized smclas %d",
1290 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1291 bfd_set_error (bfd_error_bad_value);
1295 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1296 csect = bfd_make_section_anyway (abfd, csect_name);
1299 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1300 if (enclosing == NULL)
1302 if ((bfd_vma) sym.n_value < enclosing->vma
1303 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1304 > enclosing->vma + enclosing->_raw_size))
1306 (*_bfd_error_handler)
1307 ("%s: csect `%s' not in enclosing section",
1308 bfd_get_filename (abfd), name);
1309 bfd_set_error (bfd_error_bad_value);
1312 csect->vma = sym.n_value;
1313 csect->filepos = (enclosing->filepos
1316 csect->_raw_size = aux.x_csect.x_scnlen.l;
1317 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1318 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1320 /* Record the enclosing section in the tdata for this new
1322 csect->used_by_bfd =
1323 ((struct coff_section_tdata *)
1324 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1325 if (csect->used_by_bfd == NULL)
1327 bfd_set_error (bfd_error_no_memory);
1330 coff_section_data (abfd, csect)->tdata =
1331 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1332 if (coff_section_data (abfd, csect)->tdata == NULL)
1334 bfd_set_error (bfd_error_no_memory);
1337 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1338 xcoff_section_data (abfd, csect)->lineno_count =
1339 enclosing->lineno_count;
1341 /* XCOFF requires that relocs be sorted by address, so we
1342 could do a binary search here. FIXME. (XCOFF
1343 unfortunately does not require that symbols be sorted
1344 by address, or this would be a simple merge). */
1345 rel = reloc_info[enclosing->target_index].relocs;
1346 rel_csect = reloc_info[enclosing->target_index].csects;
1348 relindx < enclosing->reloc_count;
1349 relindx++, rel++, rel_csect++)
1351 if (*rel_csect == NULL
1352 && rel->r_vaddr >= csect->vma
1353 && rel->r_vaddr < csect->vma + csect->_raw_size)
1355 csect->rel_filepos = (enclosing->rel_filepos
1356 + relindx * bfd_coff_relsz (abfd));
1360 while (relindx < enclosing->reloc_count
1361 && *rel_csect == NULL
1362 && rel->r_vaddr >= csect->vma
1363 && rel->r_vaddr < csect->vma + csect->_raw_size)
1366 csect->flags |= SEC_RELOC;
1367 ++csect->reloc_count;
1373 /* There are a number of other fields and section flags
1374 which we do not bother to set. */
1376 csect_index = ((esym
1377 - (bfd_byte *) obj_coff_external_syms (abfd))
1380 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1382 if (first_csect == NULL)
1383 first_csect = csect;
1385 /* If this symbol is C_EXT, we treat it as starting at the
1386 beginning of the newly created section. */
1387 if (sym.n_sclass == C_EXT)
1393 /* If this is a TOC section for a symbol, record it. */
1394 if (set_toc != NULL)
1395 set_toc->toc_section = csect;
1400 /* This is a label definition. The x_scnlen field is the
1401 symbol index of the csect. I believe that this must
1402 always follow the appropriate XTY_SD symbol, so I will
1408 if (aux.x_csect.x_scnlen.l < 0
1409 || (aux.x_csect.x_scnlen.l
1410 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1414 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1416 || (section->flags & SEC_HAS_CONTENTS) == 0)
1421 (*_bfd_error_handler)
1422 ("%s: misplaced XTY_LD `%s'",
1423 bfd_get_filename (abfd), name);
1424 bfd_set_error (bfd_error_bad_value);
1428 value = sym.n_value - csect->vma;
1433 /* This is an unitialized csect. We could base the name on
1434 the storage mapping class, but we don't bother. If this
1435 csect is externally visible, it is a common symbol. */
1439 xcoff_section_data (abfd, csect)->last_symndx =
1441 - (bfd_byte *) obj_coff_external_syms (abfd))
1445 csect = bfd_make_section_anyway (abfd, ".bss");
1448 csect->vma = sym.n_value;
1449 csect->_raw_size = aux.x_csect.x_scnlen.l;
1450 csect->flags |= SEC_ALLOC;
1451 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1452 /* There are a number of other fields and section flags
1453 which we do not bother to set. */
1455 csect_index = ((esym
1456 - (bfd_byte *) obj_coff_external_syms (abfd))
1459 csect->used_by_bfd =
1460 ((struct coff_section_tdata *)
1461 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1462 if (csect->used_by_bfd == NULL)
1464 bfd_set_error (bfd_error_no_memory);
1467 coff_section_data (abfd, csect)->tdata =
1468 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1469 if (coff_section_data (abfd, csect)->tdata == NULL)
1471 bfd_set_error (bfd_error_no_memory);
1474 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1476 if (first_csect == NULL)
1477 first_csect = csect;
1479 if (sym.n_sclass == C_EXT)
1481 csect->flags |= SEC_IS_COMMON;
1482 csect->_raw_size = 0;
1484 value = aux.x_csect.x_scnlen.l;
1490 /* Now we have enough information to add the symbol to the
1491 linker hash table. */
1493 if (sym.n_sclass == C_EXT)
1497 BFD_ASSERT (section != NULL);
1499 /* We must copy the name into memory if we got it from the
1500 syment itself, rather than the string table. */
1501 copy = default_copy;
1502 if (sym._n._n_n._n_zeroes != 0
1503 || sym._n._n_n._n_offset == 0)
1506 if (info->hash->creator == abfd->xvec)
1508 /* If we are statically linking a shared object, it is
1509 OK for symbol redefinitions to occur. I can't figure
1510 out just what the XCOFF linker is doing, but
1511 something like this is required for -bnso to work. */
1512 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1513 name, true, copy, false);
1514 if (*sym_hash == NULL)
1516 if (((*sym_hash)->root.type == bfd_link_hash_defined
1517 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1518 && ! bfd_is_und_section (section)
1519 && ! bfd_is_com_section (section))
1521 if ((abfd->flags & DYNAMIC) != 0)
1523 section = bfd_und_section_ptr;
1526 else if (((*sym_hash)->root.u.def.section->owner->flags
1529 (*sym_hash)->root.type = bfd_link_hash_undefined;
1530 (*sym_hash)->root.u.undef.abfd =
1531 (*sym_hash)->root.u.def.section->owner;
1536 if (! (_bfd_generic_link_add_one_symbol
1537 (info, abfd, name, flags, section, value,
1538 (const char *) NULL, copy, true,
1539 (struct bfd_link_hash_entry **) sym_hash)))
1542 if (smtyp == XTY_CM)
1544 if ((*sym_hash)->root.type != bfd_link_hash_common
1545 || (*sym_hash)->root.u.c.p->section != csect)
1547 /* We don't need the common csect we just created. */
1548 csect->_raw_size = 0;
1552 (*sym_hash)->root.u.c.p->alignment_power
1553 = csect->alignment_power;
1557 if (info->hash->creator == abfd->xvec)
1561 if (smtyp == XTY_ER || smtyp == XTY_CM)
1562 flag = XCOFF_REF_REGULAR;
1564 flag = XCOFF_DEF_REGULAR;
1565 (*sym_hash)->flags |= flag;
1567 if ((*sym_hash)->smclas == XMC_UA)
1568 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1572 *csect_cache = csect;
1574 esym += (sym.n_numaux + 1) * symesz;
1575 sym_hash += sym.n_numaux + 1;
1576 csect_cache += sym.n_numaux + 1;
1579 /* Make sure that we have seen all the relocs. */
1580 for (sub = abfd->sections; sub != first_csect; sub = sub->next)
1582 /* Reset the section size, since the data is now attached to the
1583 csects. Don't reset the size of the .debug section, since we
1584 need to read it below in bfd_xcoff_size_dynamic_sections. */
1585 if (strcmp (bfd_get_section_name (abfd, sub), ".debug") != 0)
1588 if ((sub->flags & SEC_RELOC) != 0)
1591 struct internal_reloc *rel;
1592 asection **rel_csect;
1594 rel = reloc_info[sub->target_index].relocs;
1595 rel_csect = reloc_info[sub->target_index].csects;
1596 for (i = 0; i < sub->reloc_count; i++, rel++, rel_csect++)
1598 if (*rel_csect == NULL)
1600 (*_bfd_error_handler)
1601 ("%s: reloc %s:%d not in csect",
1602 bfd_get_filename (abfd), sub->name, i);
1603 bfd_set_error (bfd_error_bad_value);
1607 /* We identify all symbols which are called, so that we
1608 can create glue code for calls to functions imported
1609 from dynamic objects. */
1610 if (info->hash->creator == abfd->xvec
1611 && *rel_csect != bfd_und_section_ptr
1612 && (rel->r_type == R_BR
1613 || rel->r_type == R_RBR)
1614 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1616 struct xcoff_link_hash_entry *h;
1618 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1619 h->flags |= XCOFF_CALLED;
1620 /* If the symbol name starts with a period, it is
1621 the code of a function. If the symbol is
1622 currently undefined, then add an undefined symbol
1623 for the function descriptor. This should do no
1624 harm, because any regular object that defines the
1625 function should also define the function
1626 descriptor. It helps, because it means that we
1627 will identify the function descriptor with a
1628 dynamic object if a dynamic object defines it. */
1629 if (h->root.root.string[0] == '.'
1630 && h->descriptor == NULL)
1632 struct xcoff_link_hash_entry *hds;
1634 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1635 h->root.root.string + 1,
1639 if (hds->root.type == bfd_link_hash_new)
1641 if (! (_bfd_generic_link_add_one_symbol
1642 (info, abfd, hds->root.root.string,
1643 (flagword) 0, bfd_und_section_ptr,
1644 (bfd_vma) 0, (const char *) NULL, false,
1646 (struct bfd_link_hash_entry **) NULL)))
1649 h->descriptor = hds;
1654 free (reloc_info[sub->target_index].csects);
1655 reloc_info[sub->target_index].csects = NULL;
1657 /* Reset SEC_RELOC, the reloc_count, and the lineno_count,
1658 since the reloc and lineno information is now attached to
1660 sub->flags &=~ SEC_RELOC;
1661 sub->reloc_count = 0;
1662 sub->lineno_count = 0;
1664 /* If we are not keeping memory, free the reloc information. */
1665 if (! info->keep_memory
1666 && coff_section_data (abfd, sub) != NULL
1667 && coff_section_data (abfd, sub)->relocs != NULL
1668 && ! coff_section_data (abfd, sub)->keep_relocs)
1670 free (coff_section_data (abfd, sub)->relocs);
1671 coff_section_data (abfd, sub)->relocs = NULL;
1675 /* Free up the line numbers. FIXME: We could cache these
1676 somewhere for the final link, to avoid reading them again. */
1677 if (reloc_info[sub->target_index].linenos != NULL)
1679 free (reloc_info[sub->target_index].linenos);
1680 reloc_info[sub->target_index].linenos = NULL;
1686 obj_coff_keep_syms (abfd) = keep_syms;
1691 if (reloc_info != NULL)
1693 for (sub = abfd->sections; sub != NULL; sub = sub->next)
1695 if (reloc_info[sub->target_index].csects != NULL)
1696 free (reloc_info[sub->target_index].csects);
1697 if (reloc_info[sub->target_index].linenos != NULL)
1698 free (reloc_info[sub->target_index].linenos);
1702 obj_coff_keep_syms (abfd) = keep_syms;
1709 /* This function is used to add symbols from a dynamic object to the
1710 global symbol table. */
1713 xcoff_link_add_dynamic_symbols (abfd, info)
1715 struct bfd_link_info *info;
1717 bfd_size_type symesz;
1720 struct xcoff_import_file *n;
1725 struct xcoff_import_file **pp;
1727 /* We can only handle a dynamic object if we are generating an XCOFF
1729 if (info->hash->creator != abfd->xvec)
1731 (*_bfd_error_handler)
1732 ("%s: XCOFF shared object when not producing XCOFF output",
1733 bfd_get_filename (abfd));
1734 bfd_set_error (bfd_error_invalid_operation);
1738 /* Remove the sections from this object, so that they do not get
1739 included in the link. */
1740 abfd->sections = NULL;
1742 symesz = bfd_coff_symesz (abfd);
1743 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1744 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1745 while (esym < esym_end)
1747 struct internal_syment sym;
1749 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1751 /* I think that every symbol mentioned in a dynamic object must
1752 be defined by that object, perhaps by importing it from
1753 another dynamic object. All we have to do is look up each
1754 external symbol. If we have already put it in the hash
1755 table, we simply set a flag indicating that it appears in a
1758 if (sym.n_sclass == C_EXT)
1761 char buf[SYMNMLEN + 1];
1762 struct xcoff_link_hash_entry *h;
1764 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1768 /* Normally we could not xcoff_link_hash_lookup in an add
1769 symbols routine, since we might not be using an XCOFF
1770 hash table. However, we verified above that we are using
1771 an XCOFF hash table. */
1772 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
1773 false, false, true);
1776 h->flags |= XCOFF_REF_DYNAMIC;
1778 /* If the symbol is undefined, and the current BFD is
1779 not a dynamic object, change the BFD to this dynamic
1780 object, so that we can get the correct import file
1782 if ((h->root.type == bfd_link_hash_undefined
1783 || h->root.type == bfd_link_hash_undefweak)
1784 && (h->root.u.undef.abfd == NULL
1785 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
1786 h->root.u.undef.abfd = abfd;
1788 if (h->smclas == XMC_UA
1789 && sym.n_numaux > 0)
1791 union internal_auxent aux;
1793 bfd_coff_swap_aux_in (abfd,
1794 (PTR) (esym + symesz * sym.n_numaux),
1795 sym.n_type, sym.n_sclass,
1796 sym.n_numaux - 1, sym.n_numaux,
1798 h->smclas = aux.x_csect.x_smclas;
1803 esym += (sym.n_numaux + 1) * symesz;
1806 /* Record this file in the import files. */
1808 n = ((struct xcoff_import_file *)
1809 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
1812 bfd_set_error (bfd_error_no_memory);
1817 /* For some reason, the path entry in the import file list for a
1818 shared object appears to always be empty. The file name is the
1821 if (abfd->my_archive == NULL)
1823 bname = bfd_get_filename (abfd);
1828 bname = bfd_get_filename (abfd->my_archive);
1829 mname = bfd_get_filename (abfd);
1831 s = strrchr (bname, '/');
1837 /* We start c at 1 because the first import file number is reserved
1839 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1841 pp = &(*pp)->next, ++c)
1845 xcoff_data (abfd)->import_file_id = c;
1850 /* Routines that are called after all the input files have been
1851 handled, but before the sections are laid out in memory. */
1853 /* Import a symbol. */
1856 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
1859 struct bfd_link_info *info;
1860 struct bfd_link_hash_entry *harg;
1862 const char *imppath;
1863 const char *impfile;
1864 const char *impmember;
1866 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
1868 h->flags |= XCOFF_IMPORT;
1870 if (val != (bfd_vma) -1)
1872 if (h->root.type == bfd_link_hash_defined)
1874 if (! ((*info->callbacks->multiple_definition)
1875 (info, h->root.root.string, h->root.u.def.section->owner,
1876 h->root.u.def.section, h->root.u.def.value,
1877 output_bfd, bfd_abs_section_ptr, val)))
1881 h->root.type = bfd_link_hash_defined;
1882 h->root.u.def.section = bfd_abs_section_ptr;
1883 h->root.u.def.value = val;
1886 if (h->ldsym == NULL)
1888 h->ldsym = ((struct internal_ldsym *)
1889 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
1890 if (h->ldsym == NULL)
1892 bfd_set_error (bfd_error_no_memory);
1897 if (imppath == NULL)
1898 h->ldsym->l_ifile = (bfd_size_type) -1;
1902 struct xcoff_import_file **pp;
1904 /* We start c at 1 because the first entry in the import list is
1905 reserved for the library search path. */
1906 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1908 pp = &(*pp)->next, ++c)
1910 if (strcmp ((*pp)->path, imppath) == 0
1911 && strcmp ((*pp)->file, impfile) == 0
1912 && strcmp ((*pp)->member, impmember) == 0)
1918 struct xcoff_import_file *n;
1920 n = ((struct xcoff_import_file *)
1921 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
1924 bfd_set_error (bfd_error_no_memory);
1930 n->member = impmember;
1934 h->ldsym->l_ifile = c;
1940 /* Export a symbol. */
1943 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
1945 struct bfd_link_info *info;
1946 struct bfd_link_hash_entry *harg;
1949 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
1951 h->flags |= XCOFF_EXPORT;
1953 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
1954 I'm just going to ignore it until somebody explains it. */
1959 /* This structure is used to pass information through
1960 xcoff_link_hash_traverse. */
1962 struct xcoff_loader_info
1964 /* Set if a problem occurred. */
1968 /* Link information structure. */
1969 struct bfd_link_info *info;
1970 /* Number of ldsym structures. */
1972 /* Size of string table. */
1976 /* Allocated size of string table. */
1980 /* Build the .loader section. This is called by the XCOFF linker
1981 emulation before_allocation routine. We must set the size of the
1982 .loader section before the linker lays out the output file.
1983 LIBPATH is the library path to search for shared objects; this is
1984 normally built from the -L arguments passed to the linker. ENTRY
1985 is the name of the entry point symbol. */
1988 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
1989 file_align, maxstack, maxdata, gc,
1992 struct bfd_link_info *info;
1993 const char *libpath;
1995 unsigned long file_align;
1996 unsigned long maxstack;
1997 unsigned long maxdata;
2002 struct xcoff_link_hash_entry *hentry;
2004 struct xcoff_loader_info ldinfo;
2005 size_t impsize, impcount;
2006 struct xcoff_import_file *fl;
2007 struct internal_ldhdr *ldhdr;
2008 bfd_size_type stoff;
2012 struct bfd_strtab_hash *debug_strtab;
2013 bfd_byte *debug_contents = NULL;
2015 ldinfo.failed = false;
2016 ldinfo.output_bfd = output_bfd;
2018 ldinfo.ldsym_count = 0;
2019 ldinfo.string_size = 0;
2020 ldinfo.strings = NULL;
2021 ldinfo.string_alc = 0;
2023 xcoff_data (output_bfd)->maxstack = maxstack;
2024 xcoff_data (output_bfd)->maxdata = maxdata;
2025 xcoff_data (output_bfd)->modtype = modtype;
2027 xcoff_hash_table (info)->file_align = file_align;
2028 xcoff_hash_table (info)->textro = textro;
2030 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2031 false, false, true);
2034 hentry->flags |= XCOFF_ENTRY;
2035 if (hentry->root.type == bfd_link_hash_defined
2036 || hentry->root.type == bfd_link_hash_defweak)
2037 xcoff_data (output_bfd)->entry_section =
2038 hentry->root.u.def.section->output_section;
2041 /* Garbage collect unused sections. */
2042 if (info->relocateable
2045 || (hentry->root.type != bfd_link_hash_defined
2046 && hentry->root.type != bfd_link_hash_defweak))
2049 xcoff_hash_table (info)->gc = false;
2051 /* We still need to call xcoff_mark, in order to set ldrel_count
2053 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2057 for (o = sub->sections; o != NULL; o = o->next)
2059 if ((o->flags & SEC_MARK) == 0)
2061 if (! xcoff_mark (info, o))
2069 if (! xcoff_mark (info, hentry->root.u.def.section))
2072 xcoff_hash_table (info)->gc = true;
2075 if (info->input_bfds == NULL)
2077 /* I'm not sure what to do in this bizarre case. */
2081 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2086 /* Work out the size of the import file names. Each import file ID
2087 consists of three null terminated strings: the path, the file
2088 name, and the archive member name. The first entry in the list
2089 of names is the path to use to find objects, which the linker has
2090 passed in as the libpath argument. For some reason, the path
2091 entry in the other import file names appears to always be empty. */
2092 impsize = strlen (libpath) + 3;
2094 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2097 impsize += (strlen (fl->path)
2099 + strlen (fl->member)
2103 /* Set up the .loader section header. */
2104 ldhdr = &xcoff_hash_table (info)->ldhdr;
2105 ldhdr->l_version = 1;
2106 ldhdr->l_nsyms = ldinfo.ldsym_count;
2107 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2108 ldhdr->l_istlen = impsize;
2109 ldhdr->l_nimpid = impcount;
2110 ldhdr->l_impoff = (LDHDRSZ
2111 + ldhdr->l_nsyms * LDSYMSZ
2112 + ldhdr->l_nreloc * LDRELSZ);
2113 ldhdr->l_stlen = ldinfo.string_size;
2114 stoff = ldhdr->l_impoff + impsize;
2115 if (ldinfo.string_size == 0)
2118 ldhdr->l_stoff = stoff;
2120 /* We now know the final size of the .loader section. Allocate
2122 lsec = xcoff_hash_table (info)->loader_section;
2123 lsec->_raw_size = stoff + ldhdr->l_stlen;
2124 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2125 if (lsec->contents == NULL)
2127 bfd_set_error (bfd_error_no_memory);
2131 /* Set up the header. */
2132 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2133 (struct external_ldhdr *) lsec->contents);
2135 /* Set up the import file names. */
2136 out = (char *) lsec->contents + ldhdr->l_impoff;
2137 strcpy (out, libpath);
2138 out += strlen (libpath) + 1;
2141 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2143 register const char *s;
2146 while ((*out++ = *s++) != '\0')
2149 while ((*out++ = *s++) != '\0')
2152 while ((*out++ = *s++) != '\0')
2156 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
2158 /* Set up the symbol string table. */
2159 if (ldinfo.string_size > 0)
2161 memcpy (out, ldinfo.strings, ldinfo.string_size);
2162 free (ldinfo.strings);
2163 ldinfo.strings = NULL;
2166 /* We can't set up the symbol table or the relocs yet, because we
2167 don't yet know the final position of the various sections. The
2168 .loader symbols are written out when the corresponding normal
2169 symbols are written out in xcoff_link_input_bfd or
2170 xcoff_write_global_symbol. The .loader relocs are written out
2171 when the corresponding normal relocs are handled in
2172 xcoff_link_input_bfd. */
2174 /* Allocate space for the global linkage section and the global toc
2176 sec = xcoff_hash_table (info)->linkage_section;
2177 if (sec->_raw_size > 0)
2179 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2180 if (sec->contents == NULL)
2182 bfd_set_error (bfd_error_no_memory);
2186 sec = xcoff_hash_table (info)->toc_section;
2187 if (sec->_raw_size > 0)
2189 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2190 if (sec->contents == NULL)
2192 bfd_set_error (bfd_error_no_memory);
2197 /* Now that we've done garbage collection, figure out the contents
2198 of the .debug section. */
2199 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2201 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2204 bfd_size_type symcount;
2205 unsigned long *debug_index;
2207 bfd_byte *esym, *esymend;
2208 bfd_size_type symesz;
2210 if (sub->xvec != info->hash->creator)
2212 subdeb = bfd_get_section_by_name (sub, ".debug");
2213 if (subdeb == NULL || subdeb->_raw_size == 0)
2216 if (info->strip == strip_all
2217 || info->strip == strip_debugger
2218 || info->discard == discard_all)
2220 subdeb->_raw_size = 0;
2224 if (! _bfd_coff_get_external_symbols (sub))
2227 symcount = obj_raw_syment_count (sub);
2228 debug_index = ((unsigned long *)
2229 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2230 if (debug_index == NULL)
2232 bfd_set_error (bfd_error_no_memory);
2235 xcoff_data (sub)->debug_indices = debug_index;
2237 /* Grab the contents of the .debug section. We use malloc and
2238 copy the neams into the debug stringtab, rather than
2239 bfd_alloc, because I expect that, when linking many files
2240 together, many of the strings will be the same. Storing the
2241 strings in the hash table should save space in this case. */
2242 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2243 if (debug_contents == NULL)
2245 bfd_set_error (bfd_error_no_memory);
2248 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2249 (file_ptr) 0, subdeb->_raw_size))
2252 csectpp = xcoff_data (sub)->csects;
2254 symesz = bfd_coff_symesz (sub);
2255 esym = (bfd_byte *) obj_coff_external_syms (sub);
2256 esymend = esym + symcount * symesz;
2257 while (esym < esymend)
2259 struct internal_syment sym;
2261 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2263 *debug_index = (unsigned long) -1;
2265 if (sym._n._n_n._n_zeroes == 0
2268 || ((*csectpp)->flags & SEC_MARK) != 0
2269 || *csectpp == bfd_abs_section_ptr)
2270 && bfd_coff_symname_in_debug (sub, &sym))
2275 name = (char *) debug_contents + sym._n._n_n._n_offset;
2276 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2277 if (indx == (bfd_size_type) -1)
2279 *debug_index = indx;
2282 esym += (sym.n_numaux + 1) * symesz;
2283 csectpp += sym.n_numaux + 1;
2284 debug_index += sym.n_numaux + 1;
2287 free (debug_contents);
2288 debug_contents = NULL;
2290 /* Clear the size of subdeb, so that it is not included directly
2291 in the output file. */
2292 subdeb->_raw_size = 0;
2294 if (! info->keep_memory)
2296 if (! _bfd_coff_free_symbols (sub))
2301 xcoff_hash_table (info)->debug_section->_raw_size =
2302 _bfd_stringtab_size (debug_strtab);
2307 if (ldinfo.strings != NULL)
2308 free (ldinfo.strings);
2309 if (debug_contents != NULL)
2310 free (debug_contents);
2314 /* The mark phase of garbage collection. For a given section, mark
2315 it, and all the sections which define symbols to which it refers.
2316 Because this function needs to look at the relocs, we also count
2317 the number of relocs which need to be copied into the .loader
2321 xcoff_mark (info, sec)
2322 struct bfd_link_info *info;
2325 if ((sec->flags & SEC_MARK) != 0)
2328 sec->flags |= SEC_MARK;
2330 if (sec->owner->xvec == info->hash->creator
2331 && coff_section_data (sec->owner, sec) != NULL
2332 && xcoff_section_data (sec->owner, sec) != NULL)
2334 register struct xcoff_link_hash_entry **hp, **hpend;
2335 struct internal_reloc *rel, *relend;
2337 /* Mark all the symbols in this section. */
2339 hp = (obj_xcoff_sym_hashes (sec->owner)
2340 + xcoff_section_data (sec->owner, sec)->first_symndx);
2341 hpend = (obj_xcoff_sym_hashes (sec->owner)
2342 + xcoff_section_data (sec->owner, sec)->last_symndx);
2343 for (; hp < hpend; hp++)
2345 register struct xcoff_link_hash_entry *h;
2349 && (h->flags & XCOFF_MARK) == 0)
2351 h->flags |= XCOFF_MARK;
2352 if (h->root.type == bfd_link_hash_defined
2353 || h->root.type == bfd_link_hash_defweak)
2357 hsec = h->root.u.def.section;
2358 if ((hsec->flags & SEC_MARK) == 0)
2360 if (! xcoff_mark (info, hsec))
2365 if (h->toc_section != NULL
2366 && (h->toc_section->flags & SEC_MARK) == 0)
2368 if (! xcoff_mark (info, h->toc_section))
2374 /* Look through the section relocs. */
2376 if ((sec->flags & SEC_RELOC) != 0
2377 && sec->reloc_count > 0)
2379 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2380 (bfd_byte *) NULL, false,
2381 (struct internal_reloc *) NULL);
2384 relend = rel + sec->reloc_count;
2385 for (; rel < relend; rel++)
2388 struct xcoff_link_hash_entry *h;
2390 if ((unsigned int) rel->r_symndx
2391 > obj_raw_syment_count (sec->owner))
2394 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2396 && (h->flags & XCOFF_MARK) == 0)
2398 h->flags |= XCOFF_MARK;
2399 if (h->root.type == bfd_link_hash_defined
2400 || h->root.type == bfd_link_hash_defweak)
2404 hsec = h->root.u.def.section;
2405 if ((hsec->flags & SEC_MARK) == 0)
2407 if (! xcoff_mark (info, hsec))
2412 if (h->toc_section != NULL
2413 && (h->toc_section->flags & SEC_MARK) == 0)
2415 if (! xcoff_mark (info, h->toc_section))
2420 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2422 && (rsec->flags & SEC_MARK) == 0)
2424 if (! xcoff_mark (info, rsec))
2428 /* See if this reloc needs to be copied into the .loader
2430 switch (rel->r_type)
2434 || h->root.type == bfd_link_hash_defined
2435 || h->root.type == bfd_link_hash_defweak
2436 || h->root.type == bfd_link_hash_common
2437 || ((h->flags & XCOFF_CALLED) != 0
2438 && (h->flags & XCOFF_DEF_REGULAR) == 0
2439 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2440 && (h->root.type == bfd_link_hash_undefined
2441 || h->root.type == bfd_link_hash_undefweak)
2442 && h->root.root.string[0] == '.'))
2449 ++xcoff_hash_table (info)->ldrel_count;
2451 h->flags |= XCOFF_LDREL;
2458 /* We should never need a .loader reloc for a TOC
2464 if (! info->keep_memory
2465 && coff_section_data (sec->owner, sec) != NULL
2466 && coff_section_data (sec->owner, sec)->relocs != NULL
2467 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2469 free (coff_section_data (sec->owner, sec)->relocs);
2470 coff_section_data (sec->owner, sec)->relocs = NULL;
2478 /* The sweep phase of garbage collection. Remove all garbage
2483 struct bfd_link_info *info;
2487 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2491 for (o = sub->sections; o != NULL; o = o->next)
2493 if ((o->flags & SEC_MARK) == 0)
2495 /* Keep all sections from non-XCOFF input files. Keep
2496 special sections. Keep .debug sections for the
2498 if (sub->xvec != info->hash->creator
2499 || o == xcoff_hash_table (info)->debug_section
2500 || o == xcoff_hash_table (info)->loader_section
2501 || o == xcoff_hash_table (info)->linkage_section
2502 || o == xcoff_hash_table (info)->toc_section
2503 || strcmp (o->name, ".debug") == 0)
2504 o->flags |= SEC_MARK;
2509 o->lineno_count = 0;
2516 /* Add a symbol to the .loader symbols, if necessary. */
2519 xcoff_build_ldsyms (h, p)
2520 struct xcoff_link_hash_entry *h;
2523 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2526 /* We don't want to garbage collect symbols which are not defined in
2527 XCOFF files. This is a convenient place to mark them. */
2528 if (xcoff_hash_table (ldinfo->info)->gc
2529 && (h->flags & XCOFF_MARK) == 0
2530 && (h->root.type == bfd_link_hash_defined
2531 || h->root.type == bfd_link_hash_defweak)
2532 && (h->root.u.def.section->owner == NULL
2533 || (h->root.u.def.section->owner->xvec
2534 != ldinfo->info->hash->creator)))
2535 h->flags |= XCOFF_MARK;
2537 /* If this symbol is called, and it is defined in a dynamic object,
2538 then we need to set up global linkage code for it. (Unless we
2539 did garbage collection and we didn't need this symbol.) */
2540 if ((h->flags & XCOFF_CALLED) != 0
2541 && (h->flags & XCOFF_DEF_REGULAR) == 0
2542 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2543 && (h->root.type == bfd_link_hash_undefined
2544 || h->root.type == bfd_link_hash_undefweak)
2545 && h->root.root.string[0] == '.'
2546 && (! xcoff_hash_table (ldinfo->info)->gc
2547 || (h->flags & XCOFF_MARK) != 0))
2550 struct xcoff_link_hash_entry *hds;
2552 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2553 h->root.type = bfd_link_hash_defined;
2554 h->root.u.def.section = sec;
2555 h->root.u.def.value = sec->_raw_size;
2557 sec->_raw_size += XCOFF_GLINK_SIZE;
2559 /* The global linkage code requires a TOC entry for the
2561 hds = h->descriptor;
2562 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2563 || hds->root.type == bfd_link_hash_undefweak)
2564 && (hds->flags & XCOFF_DEF_REGULAR) == 0
2565 && (hds->flags & XCOFF_REF_DYNAMIC) != 0);
2566 hds->flags |= XCOFF_MARK;
2567 if (hds->toc_section == NULL)
2569 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2570 hds->u.toc_offset = hds->toc_section->_raw_size;
2571 hds->toc_section->_raw_size += 4;
2572 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2573 ++hds->toc_section->reloc_count;
2575 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2577 /* We need to call xcoff_build_ldsyms recursively here,
2578 because we may already have passed hds on the traversal. */
2579 xcoff_build_ldsyms (hds, p);
2583 /* If this is still a common symbol, and it wasn't garbage
2584 collected, we need to actually allocate space for it in the .bss
2586 if (h->root.type == bfd_link_hash_common
2587 && (! xcoff_hash_table (ldinfo->info)->gc
2588 || (h->flags & XCOFF_MARK) != 0)
2589 && h->root.u.c.p->section->_raw_size == 0)
2591 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2592 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
2595 /* We need to add a symbol to the .loader section if it is mentioned
2596 in a reloc which we are copying to the .loader section and it was
2597 not defined or common, or if it is the entry point. */
2599 if (((h->flags & XCOFF_LDREL) == 0
2600 || h->root.type == bfd_link_hash_defined
2601 || h->root.type == bfd_link_hash_defweak
2602 || h->root.type == bfd_link_hash_common)
2603 && (h->flags & XCOFF_ENTRY) == 0)
2609 /* We don't need to add this symbol if we did garbage collection and
2610 we did not mark this symbol. */
2611 if (xcoff_hash_table (ldinfo->info)->gc
2612 && (h->flags & XCOFF_MARK) == 0)
2618 /* We may have already processed this symbol due to the recursive
2620 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2623 /* We need to add this symbol to the .loader symbols. */
2625 /* h->ldsym will already have been allocated for an explicitly
2627 if (h->ldsym == NULL)
2629 h->ldsym = ((struct internal_ldsym *)
2630 bfd_zalloc (ldinfo->output_bfd,
2631 sizeof (struct internal_ldsym)));
2632 if (h->ldsym == NULL)
2634 ldinfo->failed = true;
2635 bfd_set_error (bfd_error_no_memory);
2640 /* The first 3 symbol table indices are reserved to indicate the
2642 h->ldindx = ldinfo->ldsym_count + 3;
2644 ++ldinfo->ldsym_count;
2646 len = strlen (h->root.root.string);
2647 if (len <= SYMNMLEN)
2648 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
2651 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2654 bfd_byte *newstrings;
2656 newalc = ldinfo->string_alc * 2;
2659 while (ldinfo->string_size + len + 3 > newalc)
2662 if (ldinfo->strings == NULL)
2663 newstrings = (bfd_byte *) malloc (newalc);
2665 newstrings = ((bfd_byte *)
2666 realloc ((PTR) ldinfo->strings, newalc));
2667 if (newstrings == NULL)
2669 ldinfo->failed = true;
2670 bfd_set_error (bfd_error_no_memory);
2673 ldinfo->string_alc = newalc;
2674 ldinfo->strings = newstrings;
2677 bfd_put_16 (ldinfo->output_bfd, len + 1,
2678 ldinfo->strings + ldinfo->string_size);
2679 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
2680 h->ldsym->_l._l_l._l_zeroes = 0;
2681 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
2682 ldinfo->string_size += len + 3;
2685 h->flags |= XCOFF_BUILT_LDSYM;
2690 /* Do the final link step. */
2693 _bfd_xcoff_bfd_final_link (abfd, info)
2695 struct bfd_link_info *info;
2697 bfd_size_type symesz;
2698 struct xcoff_final_link_info finfo;
2700 struct bfd_link_order *p;
2701 size_t max_contents_size;
2702 size_t max_sym_count;
2703 size_t max_lineno_count;
2704 size_t max_reloc_count;
2705 size_t max_output_reloc_count;
2706 file_ptr rel_filepos;
2708 file_ptr line_filepos;
2709 unsigned int linesz;
2711 bfd_byte *external_relocs = NULL;
2712 char strbuf[STRING_SIZE_SIZE];
2714 symesz = bfd_coff_symesz (abfd);
2717 finfo.output_bfd = abfd;
2718 finfo.strtab = NULL;
2719 finfo.section_info = NULL;
2720 finfo.last_file_index = -1;
2721 finfo.toc_symindx = -1;
2722 finfo.internal_syms = NULL;
2723 finfo.sym_indices = NULL;
2724 finfo.outsyms = NULL;
2725 finfo.linenos = NULL;
2726 finfo.contents = NULL;
2727 finfo.external_relocs = NULL;
2729 finfo.ldsym = ((struct external_ldsym *)
2730 (xcoff_hash_table (info)->loader_section->contents
2732 finfo.ldrel = ((struct external_ldrel *)
2733 (xcoff_hash_table (info)->loader_section->contents
2735 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
2737 xcoff_data (abfd)->coff.link_info = info;
2738 xcoff_data (abfd)->full_aouthdr = true;
2740 finfo.strtab = _bfd_stringtab_init ();
2741 if (finfo.strtab == NULL)
2744 /* Compute the file positions for all the sections. */
2745 if (abfd->output_has_begun)
2747 if (xcoff_hash_table (info)->file_align != 0)
2754 file_align = xcoff_hash_table (info)->file_align;
2755 if (file_align != 0)
2757 boolean saw_contents;
2762 /* Insert .pad sections before every section which has
2763 contents and is loaded, if it is preceded by some other
2764 section which has contents and is loaded. */
2765 saw_contents = true;
2766 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
2768 (*op)->target_index = indx;
2769 if (strcmp ((*op)->name, ".pad") == 0)
2770 saw_contents = false;
2771 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
2772 && ((*op)->flags & SEC_LOAD) != 0)
2775 saw_contents = true;
2782 n = bfd_make_section_anyway (abfd, ".pad");
2783 BFD_ASSERT (*op == n);
2785 n->flags = SEC_HAS_CONTENTS;
2786 n->alignment_power = 0;
2787 saw_contents = false;
2792 /* Reset the section indices after inserting the new
2795 for (o = abfd->sections; o != NULL; o = o->next)
2798 o->target_index = indx;
2800 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
2802 /* Work out appropriate sizes for the .pad sections to force
2803 each section to land on a page boundary. This bit of
2804 code knows what compute_section_file_positions is going
2806 sofar = bfd_coff_filhsz (abfd);
2807 sofar += bfd_coff_aoutsz (abfd);
2808 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
2810 for (o = abfd->sections; o != NULL; o = o->next)
2812 if (strcmp (o->name, ".pad") == 0)
2816 BFD_ASSERT (o->_raw_size == 0);
2817 pageoff = sofar & (file_align - 1);
2820 o->_raw_size = file_align - pageoff;
2821 sofar += file_align - pageoff;
2822 o->flags |= SEC_HAS_CONTENTS;
2827 if ((o->flags & SEC_HAS_CONTENTS) != 0)
2828 sofar += BFD_ALIGN (o->_raw_size,
2829 1 << o->alignment_power);
2834 bfd_coff_compute_section_file_positions (abfd);
2837 /* Count the line numbers and relocation entries required for the
2838 output file. Set the file positions for the relocs. */
2839 rel_filepos = obj_relocbase (abfd);
2840 relsz = bfd_coff_relsz (abfd);
2841 max_contents_size = 0;
2842 max_lineno_count = 0;
2843 max_reloc_count = 0;
2844 for (o = abfd->sections; o != NULL; o = o->next)
2847 o->lineno_count = 0;
2848 for (p = o->link_order_head; p != NULL; p = p->next)
2850 if (p->type == bfd_indirect_link_order)
2854 sec = p->u.indirect.section;
2856 if (info->strip == strip_none
2857 || info->strip == strip_some)
2858 o->lineno_count += sec->lineno_count;
2860 o->reloc_count += sec->reloc_count;
2862 if (sec->_raw_size > max_contents_size)
2863 max_contents_size = sec->_raw_size;
2864 if (sec->lineno_count > max_lineno_count)
2865 max_lineno_count = sec->lineno_count;
2866 if (coff_section_data (sec->owner, sec) != NULL
2867 && xcoff_section_data (sec->owner, sec) != NULL
2868 && (xcoff_section_data (sec->owner, sec)->lineno_count
2869 > max_lineno_count))
2871 xcoff_section_data (sec->owner, sec)->lineno_count;
2872 if (sec->reloc_count > max_reloc_count)
2873 max_reloc_count = sec->reloc_count;
2875 else if (p->type == bfd_section_reloc_link_order
2876 || p->type == bfd_symbol_reloc_link_order)
2879 if (o->reloc_count == 0)
2883 o->flags |= SEC_RELOC;
2884 o->rel_filepos = rel_filepos;
2885 rel_filepos += o->reloc_count * relsz;
2889 /* Allocate space for the pointers we need to keep for the relocs. */
2893 /* We use section_count + 1, rather than section_count, because
2894 the target_index fields are 1 based. */
2895 finfo.section_info = ((struct xcoff_link_section_info *)
2896 malloc ((abfd->section_count + 1)
2897 * sizeof (struct xcoff_link_section_info)));
2898 if (finfo.section_info == NULL)
2900 bfd_set_error (bfd_error_no_memory);
2903 for (i = 0; i <= abfd->section_count; i++)
2905 finfo.section_info[i].relocs = NULL;
2906 finfo.section_info[i].rel_hashes = NULL;
2910 /* We now know the size of the relocs, so we can determine the file
2911 positions of the line numbers. */
2912 line_filepos = rel_filepos;
2913 linesz = bfd_coff_linesz (abfd);
2914 max_output_reloc_count = 0;
2915 for (o = abfd->sections; o != NULL; o = o->next)
2917 if (o->lineno_count == 0)
2918 o->line_filepos = 0;
2921 o->line_filepos = line_filepos;
2922 line_filepos += o->lineno_count * linesz;
2925 if (o->reloc_count != 0)
2927 /* We don't know the indices of global symbols until we have
2928 written out all the local symbols. For each section in
2929 the output file, we keep an array of pointers to hash
2930 table entries. Each entry in the array corresponds to a
2931 reloc. When we find a reloc against a global symbol, we
2932 set the corresponding entry in this array so that we can
2933 fix up the symbol index after we have written out all the
2936 Because of this problem, we also keep the relocs in
2937 memory until the end of the link. This wastes memory.
2938 We could backpatch the file later, I suppose, although it
2940 finfo.section_info[o->target_index].relocs =
2941 ((struct internal_reloc *)
2942 malloc (o->reloc_count * sizeof (struct internal_reloc)));
2943 finfo.section_info[o->target_index].rel_hashes =
2944 ((struct xcoff_link_hash_entry **)
2945 malloc (o->reloc_count
2946 * sizeof (struct xcoff_link_hash_entry *)));
2947 if (finfo.section_info[o->target_index].relocs == NULL
2948 || finfo.section_info[o->target_index].rel_hashes == NULL)
2950 bfd_set_error (bfd_error_no_memory);
2954 if (o->reloc_count > max_output_reloc_count)
2955 max_output_reloc_count = o->reloc_count;
2958 /* Reset the reloc and lineno counts, so that we can use them to
2959 count the number of entries we have output so far. */
2961 o->lineno_count = 0;
2964 obj_sym_filepos (abfd) = line_filepos;
2966 /* Figure out the largest number of symbols in an input BFD. Take
2967 the opportunity to clear the output_has_begun fields of all the
2968 input BFD's. We want at least 4 symbols, since that is the
2969 number which xcoff_write_global_symbol may need. */
2971 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2975 sub->output_has_begun = false;
2976 sz = obj_raw_syment_count (sub);
2977 if (sz > max_sym_count)
2981 /* Allocate some buffers used while linking. */
2982 finfo.internal_syms = ((struct internal_syment *)
2983 malloc (max_sym_count
2984 * sizeof (struct internal_syment)));
2985 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
2986 finfo.outsyms = ((bfd_byte *)
2987 malloc ((size_t) ((max_sym_count + 1) * symesz)));
2988 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
2989 * bfd_coff_linesz (abfd));
2990 finfo.contents = (bfd_byte *) malloc (max_contents_size);
2991 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
2992 if ((finfo.internal_syms == NULL && max_sym_count > 0)
2993 || (finfo.sym_indices == NULL && max_sym_count > 0)
2994 || finfo.outsyms == NULL
2995 || (finfo.linenos == NULL && max_lineno_count > 0)
2996 || (finfo.contents == NULL && max_contents_size > 0)
2997 || (finfo.external_relocs == NULL && max_reloc_count > 0))
2999 bfd_set_error (bfd_error_no_memory);
3003 obj_raw_syment_count (abfd) = 0;
3004 xcoff_data (abfd)->toc = (bfd_vma) -1;
3006 /* We now know the position of everything in the file, except that
3007 we don't know the size of the symbol table and therefore we don't
3008 know where the string table starts. We just build the string
3009 table in memory as we go along. We process all the relocations
3010 for a single input file at once. */
3011 for (o = abfd->sections; o != NULL; o = o->next)
3013 for (p = o->link_order_head; p != NULL; p = p->next)
3015 if (p->type == bfd_indirect_link_order
3016 && p->u.indirect.section->owner->xvec == abfd->xvec)
3018 sub = p->u.indirect.section->owner;
3019 if (! sub->output_has_begun)
3021 if (! xcoff_link_input_bfd (&finfo, sub))
3023 sub->output_has_begun = true;
3026 else if (p->type == bfd_section_reloc_link_order
3027 || p->type == bfd_symbol_reloc_link_order)
3029 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3034 if (! _bfd_default_link_order (abfd, info, o, p))
3040 /* Free up the buffers used by xcoff_link_input_bfd. */
3042 if (finfo.internal_syms != NULL)
3044 free (finfo.internal_syms);
3045 finfo.internal_syms = NULL;
3047 if (finfo.sym_indices != NULL)
3049 free (finfo.sym_indices);
3050 finfo.sym_indices = NULL;
3052 if (finfo.linenos != NULL)
3054 free (finfo.linenos);
3055 finfo.linenos = NULL;
3057 if (finfo.contents != NULL)
3059 free (finfo.contents);
3060 finfo.contents = NULL;
3062 if (finfo.external_relocs != NULL)
3064 free (finfo.external_relocs);
3065 finfo.external_relocs = NULL;
3068 /* The value of the last C_FILE symbol is supposed to be -1. Write
3070 if (finfo.last_file_index != -1)
3072 finfo.last_file.n_value = -1;
3073 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3074 (PTR) finfo.outsyms);
3076 (obj_sym_filepos (abfd)
3077 + finfo.last_file_index * symesz),
3079 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3083 /* Write out all the global symbols which do not come from XCOFF
3085 xcoff_link_hash_traverse (xcoff_hash_table (info),
3086 xcoff_write_global_symbol,
3089 if (finfo.outsyms != NULL)
3091 free (finfo.outsyms);
3092 finfo.outsyms = NULL;
3095 /* Now that we have written out all the global symbols, we know the
3096 symbol indices to use for relocs against them, and we can finally
3097 write out the relocs. */
3098 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3099 if (external_relocs == NULL && max_output_reloc_count != 0)
3101 bfd_set_error (bfd_error_no_memory);
3105 for (o = abfd->sections; o != NULL; o = o->next)
3107 struct internal_reloc *irel;
3108 struct internal_reloc *irelend;
3109 struct xcoff_link_hash_entry **rel_hash;
3112 if (o->reloc_count == 0)
3115 irel = finfo.section_info[o->target_index].relocs;
3116 irelend = irel + o->reloc_count;
3117 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3118 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3120 if (*rel_hash != NULL)
3122 if ((*rel_hash)->indx < 0)
3124 if (! ((*info->callbacks->unattached_reloc)
3125 (info, (*rel_hash)->root.root.string,
3126 (bfd *) NULL, o, irel->r_vaddr)))
3128 (*rel_hash)->indx = 0;
3130 irel->r_symndx = (*rel_hash)->indx;
3134 /* XCOFF requires that the relocs be sorted by address. We tend
3135 to produce them in the order in which their containing csects
3136 appear in the symbol table, which is not necessarily by
3137 address. So we sort them here. There may be a better way to
3139 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3140 o->reloc_count, sizeof (struct internal_reloc),
3143 irel = finfo.section_info[o->target_index].relocs;
3144 irelend = irel + o->reloc_count;
3145 erel = external_relocs;
3146 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3147 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3149 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3150 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3151 abfd) != relsz * o->reloc_count)
3155 if (external_relocs != NULL)
3157 free (external_relocs);
3158 external_relocs = NULL;
3161 /* Free up the section information. */
3162 if (finfo.section_info != NULL)
3166 for (i = 0; i < abfd->section_count; i++)
3168 if (finfo.section_info[i].relocs != NULL)
3169 free (finfo.section_info[i].relocs);
3170 if (finfo.section_info[i].rel_hashes != NULL)
3171 free (finfo.section_info[i].rel_hashes);
3173 free (finfo.section_info);
3174 finfo.section_info = NULL;
3177 /* Write out the loader section contents. */
3178 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3179 == (xcoff_hash_table (info)->loader_section->contents
3180 + xcoff_hash_table (info)->ldhdr.l_impoff));
3181 o = xcoff_hash_table (info)->loader_section;
3182 if (! bfd_set_section_contents (abfd, o->output_section,
3183 o->contents, o->output_offset,
3187 /* Write out the global linkage section and the toc section. */
3188 o = xcoff_hash_table (info)->linkage_section;
3189 if (o->_raw_size > 0
3190 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3191 o->output_offset, o->_raw_size))
3193 o = xcoff_hash_table (info)->toc_section;
3194 if (o->_raw_size > 0
3195 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3196 o->output_offset, o->_raw_size))
3199 /* Write out the string table. */
3201 (obj_sym_filepos (abfd)
3202 + obj_raw_syment_count (abfd) * symesz),
3206 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3207 (bfd_byte *) strbuf);
3208 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3210 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3213 _bfd_stringtab_free (finfo.strtab);
3215 /* Write out the debugging string table. */
3216 o = xcoff_hash_table (info)->debug_section;
3219 struct bfd_strtab_hash *debug_strtab;
3221 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3222 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3223 >= _bfd_stringtab_size (debug_strtab));
3225 o->output_section->filepos + o->output_offset,
3228 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3232 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3233 not try to write out the symbols. */
3234 bfd_get_symcount (abfd) = 0;
3239 if (finfo.strtab != NULL)
3240 _bfd_stringtab_free (finfo.strtab);
3241 if (finfo.section_info != NULL)
3245 for (i = 0; i < abfd->section_count; i++)
3247 if (finfo.section_info[i].relocs != NULL)
3248 free (finfo.section_info[i].relocs);
3249 if (finfo.section_info[i].rel_hashes != NULL)
3250 free (finfo.section_info[i].rel_hashes);
3252 free (finfo.section_info);
3254 if (finfo.internal_syms != NULL)
3255 free (finfo.internal_syms);
3256 if (finfo.sym_indices != NULL)
3257 free (finfo.sym_indices);
3258 if (finfo.outsyms != NULL)
3259 free (finfo.outsyms);
3260 if (finfo.linenos != NULL)
3261 free (finfo.linenos);
3262 if (finfo.contents != NULL)
3263 free (finfo.contents);
3264 if (finfo.external_relocs != NULL)
3265 free (finfo.external_relocs);
3266 if (external_relocs != NULL)
3267 free (external_relocs);
3271 /* Link an input file into the linker output file. This function
3272 handles all the sections and relocations of the input file at once. */
3275 xcoff_link_input_bfd (finfo, input_bfd)
3276 struct xcoff_final_link_info *finfo;
3280 const char *strings;
3281 bfd_size_type syment_base;
3282 unsigned int n_tmask;
3283 unsigned int n_btshft;
3285 bfd_size_type isymesz;
3286 bfd_size_type osymesz;
3287 bfd_size_type linesz;
3290 struct xcoff_link_hash_entry **sym_hash;
3291 struct internal_syment *isymp;
3293 unsigned long *debug_index;
3295 unsigned long output_index;
3301 /* We can just skip DYNAMIC files, unless this is a static link. */
3302 if ((input_bfd->flags & DYNAMIC) != 0
3303 && ! finfo->info->static_link)
3306 /* Move all the symbols to the output file. */
3308 output_bfd = finfo->output_bfd;
3310 syment_base = obj_raw_syment_count (output_bfd);
3311 isymesz = bfd_coff_symesz (input_bfd);
3312 osymesz = bfd_coff_symesz (output_bfd);
3313 linesz = bfd_coff_linesz (input_bfd);
3314 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3316 n_tmask = coff_data (input_bfd)->local_n_tmask;
3317 n_btshft = coff_data (input_bfd)->local_n_btshft;
3319 /* Define macros so that ISFCN, et. al., macros work correctly. */
3320 #define N_TMASK n_tmask
3321 #define N_BTSHFT n_btshft
3324 if (! finfo->info->keep_memory)
3327 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3330 if (! _bfd_coff_get_external_symbols (input_bfd))
3333 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3334 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3335 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3336 csectpp = xcoff_data (input_bfd)->csects;
3337 debug_index = xcoff_data (input_bfd)->debug_indices;
3338 isymp = finfo->internal_syms;
3339 indexp = finfo->sym_indices;
3340 output_index = syment_base;
3341 outsym = finfo->outsyms;
3344 while (esym < esym_end)
3346 struct internal_syment isym;
3347 union internal_auxent aux;
3353 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3355 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3357 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3359 BFD_ASSERT (isymp->n_numaux > 0);
3360 bfd_coff_swap_aux_in (input_bfd,
3361 (PTR) (esym + isymesz * isymp->n_numaux),
3362 isymp->n_type, isymp->n_sclass,
3363 isymp->n_numaux - 1, isymp->n_numaux,
3365 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3368 /* Make a copy of *isymp so that the relocate_section function
3369 always sees the original values. This is more reliable than
3370 always recomputing the symbol value even if we are stripping
3374 /* If this symbol is in the .loader section, swap out the
3375 .loader symbol information. If this is an external symbol
3376 reference to a defined symbol, though, then wait until we get
3377 to the definition. */
3378 if (isym.n_sclass == C_EXT
3379 && *sym_hash != NULL
3380 && (*sym_hash)->ldsym != NULL
3382 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3384 struct xcoff_link_hash_entry *h;
3385 struct internal_ldsym *ldsym;
3389 if (isym.n_scnum > 0)
3391 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3392 ldsym->l_value = (isym.n_value
3393 + (*csectpp)->output_section->vma
3394 + (*csectpp)->output_offset
3399 ldsym->l_scnum = isym.n_scnum;
3400 ldsym->l_value = isym.n_value;
3403 ldsym->l_smtype = smtyp;
3404 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3405 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3406 || (h->flags & XCOFF_IMPORT) != 0)
3407 ldsym->l_smtype |= L_IMPORT;
3408 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3409 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3410 || (h->flags & XCOFF_EXPORT) != 0)
3411 ldsym->l_smtype |= L_EXPORT;
3412 if ((h->flags & XCOFF_ENTRY) != 0)
3413 ldsym->l_smtype |= L_ENTRY;
3415 ldsym->l_smclas = aux.x_csect.x_smclas;
3417 if (ldsym->l_ifile == (bfd_size_type) -1)
3419 else if (ldsym->l_ifile == 0)
3421 if ((ldsym->l_smtype & L_IMPORT) == 0)
3427 if (h->root.type == bfd_link_hash_defined
3428 || h->root.type == bfd_link_hash_defweak)
3429 impbfd = h->root.u.def.section->owner;
3430 else if (h->root.type == bfd_link_hash_undefined
3431 || h->root.type == bfd_link_hash_undefweak)
3432 impbfd = h->root.u.undef.abfd;
3440 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3441 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3448 BFD_ASSERT (h->ldindx >= 0);
3449 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3450 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3451 finfo->ldsym + h->ldindx - 3);
3459 add = 1 + isym.n_numaux;
3461 /* If we are skipping this csect, we want to skip this symbol. */
3462 if (*csectpp == NULL)
3465 /* If we garbage collected this csect, we want to skip this
3468 && xcoff_hash_table (finfo->info)->gc
3469 && ((*csectpp)->flags & SEC_MARK) == 0
3470 && *csectpp != bfd_abs_section_ptr)
3473 /* An XCOFF linker always skips C_STAT symbols. */
3475 && isymp->n_sclass == C_STAT)
3478 /* We skip all but the first TOC anchor. */
3480 && isymp->n_sclass == C_HIDEXT
3481 && aux.x_csect.x_smclas == XMC_TC0)
3483 if (finfo->toc_symindx != -1)
3487 finfo->toc_symindx = output_index;
3488 xcoff_data (finfo->output_bfd)->toc =
3489 ((*csectpp)->output_section->vma
3490 + (*csectpp)->output_offset
3493 xcoff_data (finfo->output_bfd)->toc_section =
3494 (*csectpp)->output_section;
3499 /* If we are stripping all symbols, we want to skip this one. */
3501 && finfo->info->strip == strip_all)
3504 /* We can skip resolved external references. */
3506 && isym.n_sclass == C_EXT
3508 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3511 /* We can skip common symbols if they got defined somewhere
3514 && isym.n_sclass == C_EXT
3516 && ((*sym_hash)->root.type != bfd_link_hash_common
3517 || (*sym_hash)->root.u.c.p->section != *csectpp)
3518 && ((*sym_hash)->root.type != bfd_link_hash_defined
3519 || (*sym_hash)->root.u.def.section != *csectpp))
3522 /* Skip local symbols if we are discarding them. */
3524 && finfo->info->discard == discard_all
3525 && isym.n_sclass != C_EXT
3526 && (isym.n_sclass != C_HIDEXT
3527 || smtyp != XTY_SD))
3530 /* If we stripping debugging symbols, and this is a debugging
3531 symbol, then skip it. */
3533 && finfo->info->strip == strip_debugger
3534 && isym.n_scnum == N_DEBUG)
3537 /* If some symbols are stripped based on the name, work out the
3538 name and decide whether to skip this symbol. We don't handle
3539 this correctly for symbols whose names are in the .debug
3540 section; to get it right we would need a new bfd_strtab_hash
3541 function to return the string given the index. */
3543 && (finfo->info->strip == strip_some
3544 || finfo->info->discard == discard_l)
3545 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3548 char buf[SYMNMLEN + 1];
3550 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3554 if ((finfo->info->strip == strip_some
3555 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
3557 || (finfo->info->discard == discard_l
3558 && (isym.n_sclass != C_EXT
3559 && (isym.n_sclass != C_HIDEXT
3560 || smtyp != XTY_SD))
3561 && strncmp (name, finfo->info->lprefix,
3562 finfo->info->lprefix_len) == 0))
3566 /* We can not skip the first TOC anchor. */
3569 && finfo->info->strip != strip_all)
3572 /* We now know whether we are to skip this symbol or not. */
3575 /* Adjust the symbol in order to output it. */
3577 if (isym._n._n_n._n_zeroes == 0
3578 && isym._n._n_n._n_offset != 0)
3580 /* This symbol has a long name. Enter it in the string
3581 table we are building. If *debug_index != -1, the
3582 name has already been entered in the .debug section. */
3583 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3584 isym._n._n_n._n_offset = *debug_index;
3590 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
3594 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3595 if (indx == (bfd_size_type) -1)
3597 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3601 if (isym.n_sclass != C_BSTAT
3602 && isym.n_sclass != C_ESTAT
3603 && isym.n_scnum > 0)
3605 isym.n_scnum = (*csectpp)->output_section->target_index;
3606 isym.n_value += ((*csectpp)->output_section->vma
3607 + (*csectpp)->output_offset
3611 /* The value of a C_FILE symbol is the symbol index of the
3612 next C_FILE symbol. The value of the last C_FILE symbol
3613 is -1. We try to get this right, below, just before we
3614 write the symbols out, but in the general case we may
3615 have to write the symbol out twice. */
3616 if (isym.n_sclass == C_FILE)
3618 if (finfo->last_file_index != -1
3619 && finfo->last_file.n_value != (long) output_index)
3621 /* We must correct the value of the last C_FILE entry. */
3622 finfo->last_file.n_value = output_index;
3623 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3625 /* The last C_FILE symbol is in this input file. */
3626 bfd_coff_swap_sym_out (output_bfd,
3627 (PTR) &finfo->last_file,
3628 (PTR) (finfo->outsyms
3629 + ((finfo->last_file_index
3635 /* We have already written out the last C_FILE
3636 symbol. We need to write it out again. We
3637 borrow *outsym temporarily. */
3638 bfd_coff_swap_sym_out (output_bfd,
3639 (PTR) &finfo->last_file,
3641 if (bfd_seek (output_bfd,
3642 (obj_sym_filepos (output_bfd)
3643 + finfo->last_file_index * osymesz),
3645 || (bfd_write (outsym, osymesz, 1, output_bfd)
3651 finfo->last_file_index = output_index;
3652 finfo->last_file = isym;
3655 /* Output the symbol. */
3657 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
3659 *indexp = output_index;
3661 if (isym.n_sclass == C_EXT)
3664 struct xcoff_link_hash_entry *h;
3666 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3668 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3669 BFD_ASSERT (h != NULL);
3670 h->indx = output_index;
3673 /* If this is a symbol in the TOC which we may have merged
3674 (class XMC_TC), remember the symbol index of the TOC
3676 if (isym.n_sclass == C_HIDEXT
3677 && aux.x_csect.x_smclas == XMC_TC
3678 && *sym_hash != NULL)
3680 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3681 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3682 (*sym_hash)->u.toc_indx = output_index;
3685 output_index += add;
3686 outsym += add * osymesz;
3689 esym += add * isymesz;
3693 if (debug_index != NULL)
3696 for (--add; add > 0; --add)
3700 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3701 done in a separate pass, because we don't know the correct symbol
3702 indices until we have already decided which symbols we are going
3705 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3706 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3707 isymp = finfo->internal_syms;
3708 indexp = finfo->sym_indices;
3709 csectpp = xcoff_data (input_bfd)->csects;
3710 outsym = finfo->outsyms;
3711 while (esym < esym_end)
3715 add = 1 + isymp->n_numaux;
3718 esym += add * isymesz;
3723 if (isymp->n_sclass == C_BSTAT)
3727 /* The value of a C_BSTAT symbol is the symbol table
3728 index of the containing csect. */
3729 indx = isymp->n_value;
3730 if (indx < obj_raw_syment_count (input_bfd))
3734 symindx = finfo->sym_indices[indx];
3738 isymp->n_value = symindx;
3739 bfd_coff_swap_sym_out (output_bfd, (PTR) isymp,
3747 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3749 union internal_auxent aux;
3751 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
3752 isymp->n_sclass, i, isymp->n_numaux,
3755 if (isymp->n_sclass == C_FILE)
3757 /* This is the file name (or some comment put in by
3758 the compiler). If it is long, we must put it in
3759 the string table. */
3760 if (aux.x_file.x_n.x_zeroes == 0
3761 && aux.x_file.x_n.x_offset != 0)
3763 const char *filename;
3766 BFD_ASSERT (aux.x_file.x_n.x_offset
3767 >= STRING_SIZE_SIZE);
3768 if (strings == NULL)
3770 strings = _bfd_coff_read_string_table (input_bfd);
3771 if (strings == NULL)
3774 filename = strings + aux.x_file.x_n.x_offset;
3775 indx = _bfd_stringtab_add (finfo->strtab, filename,
3777 if (indx == (bfd_size_type) -1)
3779 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3782 else if ((isymp->n_sclass == C_EXT
3783 || isymp->n_sclass == C_HIDEXT)
3784 && i + 1 == isymp->n_numaux)
3786 /* We don't support type checking. I don't know if
3788 aux.x_csect.x_parmhash = 0;
3789 /* I don't think anybody uses these fields, but we'd
3790 better clobber them just in case. */
3791 aux.x_csect.x_stab = 0;
3792 aux.x_csect.x_snstab = 0;
3793 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
3797 indx = aux.x_csect.x_scnlen.l;
3798 if (indx < obj_raw_syment_count (input_bfd))
3802 symindx = finfo->sym_indices[indx];
3804 aux.x_sym.x_tagndx.l = 0;
3806 aux.x_sym.x_tagndx.l = symindx;
3810 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
3814 if (ISFCN (isymp->n_type)
3815 || ISTAG (isymp->n_sclass)
3816 || isymp->n_sclass == C_BLOCK)
3818 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
3820 && indx < obj_raw_syment_count (input_bfd))
3822 /* We look forward through the symbol for
3823 the index of the next symbol we are going
3824 to include. I don't know if this is
3826 while (finfo->sym_indices[indx] < 0
3827 && indx < obj_raw_syment_count (input_bfd))
3829 if (indx >= obj_raw_syment_count (input_bfd))
3830 indx = output_index;
3832 indx = finfo->sym_indices[indx];
3833 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
3837 indx = aux.x_sym.x_tagndx.l;
3838 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
3842 symindx = finfo->sym_indices[indx];
3844 aux.x_sym.x_tagndx.l = 0;
3846 aux.x_sym.x_tagndx.l = symindx;
3850 /* Copy over the line numbers, unless we are stripping
3851 them. We do this on a symbol by symbol basis in
3852 order to more easily handle garbage collection. */
3853 if ((isymp->n_sclass == C_EXT
3854 || isymp->n_sclass == C_HIDEXT)
3856 && isymp->n_numaux > 1
3857 && ISFCN (isymp->n_type)
3858 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
3860 if (finfo->info->strip != strip_none
3861 && finfo->info->strip != strip_some)
3862 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
3865 asection *enclosing;
3866 unsigned int enc_count;
3867 bfd_size_type linoff;
3868 struct internal_lineno lin;
3871 enclosing = xcoff_section_data (abfd, o)->enclosing;
3872 enc_count = xcoff_section_data (abfd, o)->lineno_count;
3873 if (oline != enclosing)
3875 if (bfd_seek (input_bfd,
3876 enclosing->line_filepos,
3878 || (bfd_read (finfo->linenos, linesz,
3879 enc_count, input_bfd)
3880 != linesz * enc_count))
3885 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
3886 - enclosing->line_filepos);
3888 bfd_coff_swap_lineno_in (input_bfd,
3889 (PTR) (finfo->linenos + linoff),
3892 || ((bfd_size_type) lin.l_addr.l_symndx
3896 obj_coff_external_syms (input_bfd)))
3898 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
3901 bfd_byte *linpend, *linp;
3903 bfd_size_type count;
3905 lin.l_addr.l_symndx = *indexp;
3906 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
3907 (PTR) (finfo->linenos
3910 linpend = (finfo->linenos
3911 + enc_count * linesz);
3912 offset = (o->output_section->vma
3915 for (linp = finfo->linenos + linoff + linesz;
3919 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
3921 if (lin.l_lnno == 0)
3923 lin.l_addr.l_paddr += offset;
3924 bfd_coff_swap_lineno_out (output_bfd,
3929 count = (linp - (finfo->linenos + linoff)) / linesz;
3931 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
3932 (o->output_section->line_filepos
3933 + o->output_section->lineno_count * linesz);
3935 if (bfd_seek (output_bfd,
3936 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
3938 || (bfd_write (finfo->linenos + linoff,
3939 linesz, count, output_bfd)
3943 o->output_section->lineno_count += count;
3948 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
3949 isymp->n_sclass, i, isymp->n_numaux,
3961 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
3962 symbol will be the first symbol in the next input file. In the
3963 normal case, this will save us from writing out the C_FILE symbol
3965 if (finfo->last_file_index != -1
3966 && (bfd_size_type) finfo->last_file_index >= syment_base)
3968 finfo->last_file.n_value = output_index;
3969 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
3970 (PTR) (finfo->outsyms
3971 + ((finfo->last_file_index - syment_base)
3975 /* Write the modified symbols to the output file. */
3976 if (outsym > finfo->outsyms)
3978 if (bfd_seek (output_bfd,
3979 obj_sym_filepos (output_bfd) + syment_base * osymesz,
3981 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
3983 != (bfd_size_type) (outsym - finfo->outsyms)))
3986 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
3987 + (outsym - finfo->outsyms) / osymesz)
3990 obj_raw_syment_count (output_bfd) = output_index;
3993 /* Don't let the linker relocation routines discard the symbols. */
3994 keep_syms = obj_coff_keep_syms (input_bfd);
3995 obj_coff_keep_syms (input_bfd) = true;
3997 /* Relocate the contents of each section. */
3998 for (o = input_bfd->sections; o != NULL; o = o->next)
4002 if ((o->flags & SEC_HAS_CONTENTS) == 0
4003 || o->_raw_size == 0
4004 || (o->flags & SEC_IN_MEMORY) != 0)
4007 /* We have set filepos correctly for the sections we created to
4008 represent csects, so bfd_get_section_contents should work. */
4009 if (coff_section_data (input_bfd, o) != NULL
4010 && coff_section_data (input_bfd, o)->contents != NULL)
4011 contents = coff_section_data (input_bfd, o)->contents;
4014 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4015 (file_ptr) 0, o->_raw_size))
4017 contents = finfo->contents;
4020 if ((o->flags & SEC_RELOC) != 0)
4023 struct internal_reloc *internal_relocs;
4024 struct internal_reloc *irel;
4026 struct internal_reloc *irelend;
4027 struct xcoff_link_hash_entry **rel_hash;
4030 /* Read in the relocs. */
4031 target_index = o->output_section->target_index;
4032 internal_relocs = (xcoff_read_internal_relocs
4033 (input_bfd, o, false, finfo->external_relocs,
4035 (finfo->section_info[target_index].relocs
4036 + o->output_section->reloc_count)));
4037 if (internal_relocs == NULL)
4040 /* Call processor specific code to relocate the section
4042 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4046 finfo->internal_syms,
4047 xcoff_data (input_bfd)->csects))
4050 offset = o->output_section->vma + o->output_offset - o->vma;
4051 irel = internal_relocs;
4052 irelend = irel + o->reloc_count;
4053 rel_hash = (finfo->section_info[target_index].rel_hashes
4054 + o->output_section->reloc_count);
4055 for (; irel < irelend; irel++, rel_hash++)
4057 struct xcoff_link_hash_entry *h = NULL;
4058 struct internal_ldrel ldrel;
4062 /* Adjust the reloc address and symbol index. */
4064 irel->r_vaddr += offset;
4066 r_symndx = irel->r_symndx;
4070 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4072 && (irel->r_type == R_TOC
4073 || irel->r_type == R_GL
4074 || irel->r_type == R_TCL
4075 || irel->r_type == R_TRL
4076 || irel->r_type == R_TRLA))
4078 /* This is a TOC relative reloc with a symbol
4079 attached. The symbol should be the one which
4080 this reloc is for. We want to make this
4081 reloc against the TOC address of the symbol,
4082 not the symbol itself. */
4083 BFD_ASSERT (h->toc_section != NULL);
4084 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4085 if (h->u.toc_indx == -1)
4087 /* We could handle this case if we had to,
4088 but I don't think it can arise. */
4089 (*_bfd_error_handler)
4090 ("%s: unattached TOC reloc against `%s'",
4091 bfd_get_filename (input_bfd),
4092 h->root.root.string);
4093 bfd_set_error (bfd_error_bad_value);
4096 irel->r_symndx = h->u.toc_indx;
4100 /* This is a global symbol. */
4102 irel->r_symndx = h->indx;
4105 /* This symbol is being written at the end
4106 of the file, and we do not yet know the
4107 symbol index. We save the pointer to the
4108 hash table entry in the rel_hash list.
4109 We set the indx field to -2 to indicate
4110 that this symbol must not be stripped. */
4119 indx = finfo->sym_indices[r_symndx];
4123 struct internal_syment *is;
4125 /* Relocations against a TC0 TOC anchor are
4126 automatically transformed to be against
4127 the TOC anchor in the output file. */
4128 is = finfo->internal_syms + r_symndx;
4129 if (is->n_sclass == C_HIDEXT
4130 && is->n_numaux > 0)
4133 union internal_auxent aux;
4137 obj_coff_external_syms (input_bfd))
4138 + ((r_symndx + is->n_numaux)
4140 bfd_coff_swap_aux_in (input_bfd, auxptr,
4141 is->n_type, is->n_sclass,
4145 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4146 && aux.x_csect.x_smclas == XMC_TC0)
4147 indx = finfo->toc_symindx;
4152 irel->r_symndx = indx;
4155 struct internal_syment *is;
4157 char buf[SYMNMLEN + 1];
4159 /* This reloc is against a symbol we are
4160 stripping. It would be possible to handle
4161 this case, but I don't think it's worth it. */
4162 is = finfo->internal_syms + r_symndx;
4164 name = (_bfd_coff_internal_syment_name
4165 (input_bfd, is, buf));
4169 if (! ((*finfo->info->callbacks->unattached_reloc)
4170 (finfo->info, name, input_bfd, o,
4177 switch (irel->r_type)
4181 || h->root.type == bfd_link_hash_defined
4182 || h->root.type == bfd_link_hash_defweak
4183 || h->root.type == bfd_link_hash_common)
4190 /* This reloc needs to be copied into the .loader
4192 ldrel.l_vaddr = irel->r_vaddr;
4194 ldrel.l_symndx = -1;
4196 || (h->root.type == bfd_link_hash_defined
4197 || h->root.type == bfd_link_hash_defweak
4198 || h->root.type == bfd_link_hash_common))
4203 sec = xcoff_data (input_bfd)->csects[r_symndx];
4204 else if (h->root.type == bfd_link_hash_common)
4205 sec = h->root.u.c.p->section;
4207 sec = h->root.u.def.section;
4208 sec = sec->output_section;
4210 if (strcmp (sec->name, ".text") == 0)
4212 else if (strcmp (sec->name, ".data") == 0)
4214 else if (strcmp (sec->name, ".bss") == 0)
4218 (*_bfd_error_handler)
4219 ("%s: loader reloc in unrecognized section `%s'",
4220 bfd_get_filename (input_bfd),
4222 bfd_set_error (bfd_error_nonrepresentable_section);
4230 (*_bfd_error_handler)
4231 ("%s: `%s' in loader reloc but not loader sym",
4232 bfd_get_filename (input_bfd),
4233 h->root.root.string);
4234 bfd_set_error (bfd_error_bad_value);
4237 ldrel.l_symndx = h->ldindx;
4239 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4240 ldrel.l_rsecnm = o->output_section->target_index;
4241 if (xcoff_hash_table (finfo->info)->textro
4242 && strcmp (o->output_section->name, ".text") == 0)
4244 (*_bfd_error_handler)
4245 ("%s: loader reloc in read-only section %s",
4246 bfd_get_filename (input_bfd),
4247 bfd_get_section_name (finfo->output_bfd,
4248 o->output_section));
4249 bfd_set_error (bfd_error_invalid_operation);
4252 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4254 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4263 /* We should never need a .loader reloc for a TOC
4269 o->output_section->reloc_count += o->reloc_count;
4272 /* Write out the modified section contents. */
4273 if (! bfd_set_section_contents (output_bfd, o->output_section,
4274 contents, o->output_offset,
4275 (o->_cooked_size != 0
4281 obj_coff_keep_syms (input_bfd) = keep_syms;
4283 if (! finfo->info->keep_memory)
4285 if (! _bfd_coff_free_symbols (input_bfd))
4295 /* Write out a non-XCOFF global symbol. */
4298 xcoff_write_global_symbol (h, p)
4299 struct xcoff_link_hash_entry *h;
4302 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4305 struct internal_syment isym;
4306 union internal_auxent aux;
4308 output_bfd = finfo->output_bfd;
4310 /* If this symbol was garbage collected, just skip it. */
4311 if (xcoff_hash_table (finfo->info)->gc
4312 && (h->flags & XCOFF_MARK) == 0)
4315 /* If we need a .loader section entry, write it out. */
4316 if (h->ldsym != NULL)
4318 struct internal_ldsym *ldsym;
4323 if (h->root.type == bfd_link_hash_undefined
4324 || h->root.type == bfd_link_hash_undefweak)
4327 ldsym->l_scnum = N_UNDEF;
4328 ldsym->l_smtype = XTY_ER;
4329 impbfd = h->root.u.undef.abfd;
4331 else if (h->root.type == bfd_link_hash_defined
4332 || h->root.type == bfd_link_hash_defweak)
4336 sec = h->root.u.def.section;
4337 ldsym->l_value = (sec->output_section->vma
4338 + sec->output_offset
4339 + h->root.u.def.value);
4340 ldsym->l_scnum = sec->output_section->target_index;
4341 ldsym->l_smtype = XTY_SD;
4342 impbfd = sec->owner;
4347 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4348 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4349 || (h->flags & XCOFF_IMPORT) != 0)
4350 ldsym->l_smtype |= L_IMPORT;
4351 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4352 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4353 || (h->flags & XCOFF_EXPORT) != 0)
4354 ldsym->l_smtype |= L_EXPORT;
4355 if ((h->flags & XCOFF_ENTRY) != 0)
4356 ldsym->l_smtype |= L_ENTRY;
4358 ldsym->l_smclas = h->smclas;
4360 if (ldsym->l_ifile == (bfd_size_type) -1)
4362 else if (ldsym->l_ifile == 0)
4364 if ((ldsym->l_smtype & L_IMPORT) == 0)
4366 else if (impbfd == NULL)
4370 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4371 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4377 BFD_ASSERT (h->ldindx >= 0);
4378 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4379 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4383 /* If this symbol needs global linkage code, write it out. */
4384 if (h->root.type == bfd_link_hash_defined
4385 && (h->root.u.def.section
4386 == xcoff_hash_table (finfo->info)->linkage_section))
4392 p = h->root.u.def.section->contents + h->root.u.def.value;
4394 /* The first instruction in the global linkage code loads a
4395 specific TOC element. */
4396 tocoff = (h->descriptor->toc_section->output_section->vma
4397 + h->descriptor->toc_section->output_offset
4398 - xcoff_data (output_bfd)->toc);
4399 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4400 tocoff += h->descriptor->u.toc_offset;
4401 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4403 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
4405 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
4408 /* If we created a TOC entry for this symbol, write out the required
4410 if ((h->flags & XCOFF_SET_TOC) != 0)
4415 struct internal_reloc *irel;
4416 struct internal_ldrel ldrel;
4418 tocsec = h->toc_section;
4419 osec = tocsec->output_section;
4420 oindx = osec->target_index;
4421 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4422 irel->r_vaddr = (osec->vma
4423 + tocsec->output_offset
4426 irel->r_symndx = h->indx;
4430 irel->r_symndx = obj_raw_syment_count (output_bfd);
4432 irel->r_type = R_POS;
4434 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4435 ++osec->reloc_count;
4437 BFD_ASSERT (h->ldindx >= 0);
4438 ldrel.l_vaddr = irel->r_vaddr;
4439 ldrel.l_symndx = h->ldindx;
4440 ldrel.l_rtype = (31 << 8) | R_POS;
4441 ldrel.l_rsecnm = oindx;
4442 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4450 && (finfo->info->strip == strip_all
4451 || (finfo->info->strip == strip_some
4452 && (bfd_hash_lookup (finfo->info->keep_hash,
4453 h->root.root.string, false, false)
4458 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
4461 outsym = finfo->outsyms;
4463 memset (&aux, 0, sizeof aux);
4465 h->indx = obj_raw_syment_count (output_bfd);
4467 if (strlen (h->root.root.string) <= SYMNMLEN)
4468 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
4475 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4477 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
4479 if (indx == (bfd_size_type) -1)
4481 isym._n._n_n._n_zeroes = 0;
4482 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4485 if (h->root.type == bfd_link_hash_undefined
4486 || h->root.type == bfd_link_hash_undefweak)
4489 isym.n_scnum = N_UNDEF;
4490 isym.n_sclass = C_EXT;
4491 aux.x_csect.x_smtyp = XTY_ER;
4493 else if (h->root.type == bfd_link_hash_defined
4494 || h->root.type == bfd_link_hash_defweak)
4496 isym.n_value = (h->root.u.def.section->output_section->vma
4497 + h->root.u.def.section->output_offset
4498 + h->root.u.def.value);
4499 isym.n_scnum = h->root.u.def.section->output_section->target_index;
4500 isym.n_sclass = C_HIDEXT;
4501 aux.x_csect.x_smtyp = XTY_SD;
4502 /* I don't know what the csect length should be in this case. */
4507 isym.n_type = T_NULL;
4510 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4511 outsym += bfd_coff_symesz (output_bfd);
4513 aux.x_csect.x_smclas = h->smclas;
4515 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
4517 outsym += bfd_coff_auxesz (output_bfd);
4519 if (h->root.type == bfd_link_hash_defined
4520 || h->root.type == bfd_link_hash_defweak)
4522 /* We just output an SD symbol. Now output an LD symbol. */
4526 isym.n_sclass = C_EXT;
4527 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4528 outsym += bfd_coff_symesz (output_bfd);
4530 aux.x_csect.x_smtyp = XTY_LD;
4531 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
4533 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
4535 outsym += bfd_coff_auxesz (output_bfd);
4538 if (bfd_seek (output_bfd,
4539 (obj_sym_filepos (output_bfd)
4540 + (obj_raw_syment_count (output_bfd)
4541 * bfd_coff_symesz (output_bfd))),
4543 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
4544 != (bfd_size_type) (outsym - finfo->outsyms)))
4546 obj_raw_syment_count (output_bfd) +=
4547 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4552 /* Handle a link order which is supposed to generate a reloc. */
4555 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
4557 struct xcoff_final_link_info *finfo;
4558 asection *output_section;
4559 struct bfd_link_order *link_order;
4561 reloc_howto_type *howto;
4562 struct internal_reloc *irel;
4563 struct xcoff_link_hash_entry **rel_hash_ptr;
4565 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4568 bfd_set_error (bfd_error_bad_value);
4572 if (link_order->u.reloc.p->addend != 0)
4576 bfd_reloc_status_type rstat;
4579 size = bfd_get_reloc_size (howto);
4580 buf = (bfd_byte *) bfd_zmalloc (size);
4583 bfd_set_error (bfd_error_no_memory);
4587 rstat = _bfd_relocate_contents (howto, output_bfd,
4588 link_order->u.reloc.p->addend, buf);
4594 case bfd_reloc_outofrange:
4596 case bfd_reloc_overflow:
4597 if (! ((*finfo->info->callbacks->reloc_overflow)
4599 (link_order->type == bfd_section_reloc_link_order
4600 ? bfd_section_name (output_bfd,
4601 link_order->u.reloc.p->u.section)
4602 : link_order->u.reloc.p->u.name),
4603 howto->name, link_order->u.reloc.p->addend,
4604 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
4611 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4612 (file_ptr) link_order->offset, size);
4618 /* Store the reloc information in the right place. It will get
4619 swapped and written out at the end of the final_link routine. */
4621 irel = (finfo->section_info[output_section->target_index].relocs
4622 + output_section->reloc_count);
4623 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
4624 + output_section->reloc_count);
4626 memset (irel, 0, sizeof (struct internal_reloc));
4627 *rel_hash_ptr = NULL;
4629 irel->r_vaddr = output_section->vma + link_order->offset;
4631 if (link_order->type == bfd_section_reloc_link_order)
4633 /* We need to somehow locate a symbol in the right section. The
4634 symbol must either have a value of zero, or we must adjust
4635 the addend by the value of the symbol. FIXME: Write this
4636 when we need it. The old linker couldn't handle this anyhow. */
4638 *rel_hash_ptr = NULL;
4643 struct xcoff_link_hash_entry *h;
4645 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
4646 link_order->u.reloc.p->u.name,
4647 false, false, true);
4651 irel->r_symndx = h->indx;
4654 /* Set the index to -2 to force this symbol to get
4663 if (! ((*finfo->info->callbacks->unattached_reloc)
4664 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4665 (asection *) NULL, (bfd_vma) 0)))
4671 irel->r_type = howto->type;
4672 irel->r_size = howto->bitsize - 1;
4673 if (howto->complain_on_overflow == complain_overflow_signed)
4674 irel->r_size |= 0x80;
4676 ++output_section->reloc_count;
4681 /* Sort relocs by VMA. This is called via qsort. */
4684 xcoff_sort_relocs (p1, p2)
4688 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4689 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4691 if (r1->r_vaddr > r2->r_vaddr)
4693 else if (r1->r_vaddr < r2->r_vaddr)
4699 /* This is the relocation function for the RS/6000/POWER/PowerPC.
4700 This is currently the only processor which uses XCOFF; I hope that
4701 will never change. */
4704 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
4705 input_section, contents, relocs, syms,
4708 struct bfd_link_info *info;
4710 asection *input_section;
4712 struct internal_reloc *relocs;
4713 struct internal_syment *syms;
4714 asection **sections;
4716 struct internal_reloc *rel;
4717 struct internal_reloc *relend;
4720 relend = rel + input_section->reloc_count;
4721 for (; rel < relend; rel++)
4724 struct xcoff_link_hash_entry *h;
4725 struct internal_syment *sym;
4728 struct reloc_howto_struct howto;
4729 bfd_reloc_status_type rstat;
4731 /* Relocation type R_REF is a special relocation type which is
4732 merely used to prevent garbage collection from occurring for
4733 the csect including the symbol which it references. */
4734 if (rel->r_type == R_REF)
4737 symndx = rel->r_symndx;
4747 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
4748 sym = syms + symndx;
4749 addend = - sym->n_value;
4752 /* We build the howto information on the fly. */
4754 howto.type = rel->r_type;
4755 howto.rightshift = 0;
4757 howto.bitsize = (rel->r_size & 0x1f) + 1;
4758 howto.pc_relative = false;
4760 if ((rel->r_size & 0x80) != 0)
4761 howto.complain_on_overflow = complain_overflow_signed;
4763 howto.complain_on_overflow = complain_overflow_bitfield;
4764 howto.special_function = NULL;
4765 howto.name = "internal";
4766 howto.partial_inplace = true;
4767 if (howto.bitsize == 32)
4768 howto.src_mask = howto.dst_mask = 0xffffffff;
4771 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
4772 if (howto.bitsize == 16)
4775 howto.pcrel_offset = false;
4785 sec = bfd_abs_section_ptr;
4790 sec = sections[symndx];
4791 val = (sec->output_section->vma
4792 + sec->output_offset
4799 if (h->root.type == bfd_link_hash_defined
4800 || h->root.type == bfd_link_hash_defweak)
4804 sec = h->root.u.def.section;
4805 val = (h->root.u.def.value
4806 + sec->output_section->vma
4807 + sec->output_offset);
4809 else if (h->root.type == bfd_link_hash_common)
4813 sec = h->root.u.c.p->section;
4814 val = (sec->output_section->vma
4815 + sec->output_offset);
4817 else if ((h->flags & XCOFF_REF_DYNAMIC) != 0
4818 || (h->flags & XCOFF_IMPORT) != 0)
4820 /* Every symbol in a shared object is defined somewhere. */
4823 else if (! info->relocateable)
4825 if (! ((*info->callbacks->undefined_symbol)
4826 (info, h->root.root.string, input_bfd, input_section,
4827 rel->r_vaddr - input_section->vma)))
4832 /* I took the relocation type definitions from two documents:
4833 the PowerPC AIX Version 4 Application Binary Interface, First
4834 Edition (April 1992), and the PowerOpen ABI, Big-Endian
4835 32-Bit Hardware Implementation (June 30, 1994). Differences
4836 between the documents are noted below. */
4838 switch (rel->r_type)
4843 /* These relocs are defined by the PowerPC ABI to be
4844 relative branches which use half of the difference
4845 between the symbol and the program counter. I can't
4846 quite figure out when this is useful. These relocs are
4847 not defined by the PowerOpen ABI. */
4849 (*_bfd_error_handler)
4850 ("%s: unsupported relocation type 0x%02x",
4851 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
4852 bfd_set_error (bfd_error_bad_value);
4855 /* Simple positive relocation. */
4858 /* Simple negative relocation. */
4862 /* Simple PC relative relocation. */
4863 howto.pc_relative = true;
4866 /* TOC relative relocation. The value in the instruction in
4867 the input file is the offset from the input file TOC to
4868 the desired location. We want the offset from the final
4869 TOC to the desired location. We have:
4874 so we must change insn by on - in.
4877 /* Global linkage relocation. The value of this relocation
4878 is the address of the entry in the TOC section. */
4880 /* Local object TOC address. I can't figure out the
4881 difference between this and case R_GL. */
4883 /* TOC relative relocation. A TOC relative load instruction
4884 which may be changed to a load address instruction.
4885 FIXME: We don't currently implement this optimization. */
4887 /* TOC relative relocation. This is a TOC relative load
4888 address instruction which may be changed to a load
4889 instruction. FIXME: I don't know if this is the correct
4891 if (h != NULL && h->toc_section == NULL)
4893 (*_bfd_error_handler)
4894 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
4895 bfd_get_filename (input_bfd), rel->r_vaddr,
4896 h->root.root.string);
4897 bfd_set_error (bfd_error_bad_value);
4902 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4903 val = (h->toc_section->output_section->vma
4904 + h->toc_section->output_offset);
4906 val = ((val - xcoff_data (output_bfd)->toc)
4907 - (sym->n_value - xcoff_data (input_bfd)->toc));
4911 /* Absolute branch. We don't want to mess with the lower
4912 two bits of the instruction. */
4914 /* The PowerPC ABI defines this as an absolute call which
4915 may be modified to become a relative call. The PowerOpen
4916 ABI does not define this relocation type. */
4918 /* Absolute branch which may be modified to become a
4921 /* The PowerPC ABI defines this as an absolute branch to a
4922 fixed address which may be modified to an absolute branch
4923 to a symbol. The PowerOpen ABI does not define this
4926 /* The PowerPC ABI defines this as an absolute branch to a
4927 fixed address which may be modified to a relative branch.
4928 The PowerOpen ABI does not define this relocation type. */
4929 howto.src_mask &= ~3;
4930 howto.dst_mask = howto.src_mask;
4933 /* Relative branch. We don't want to mess with the lower
4934 two bits of the instruction. */
4936 /* The PowerPC ABI defines this as a relative call which may
4937 be modified to become an absolute call. The PowerOpen
4938 ABI does not define this relocation type. */
4940 /* A relative branch which may be modified to become an
4941 absolute branch. FIXME: We don't implement this,
4942 although we should for symbols of storage mapping class
4944 howto.pc_relative = true;
4945 howto.src_mask &= ~3;
4946 howto.dst_mask = howto.src_mask;
4949 /* The PowerPC AIX ABI describes this as a load which may be
4950 changed to a load address. The PowerOpen ABI says this
4951 is the same as case R_POS. */
4954 /* The PowerPC AIX ABI describes this as a load address
4955 which may be changed to a load. The PowerOpen ABI says
4956 this is the same as R_POS. */
4960 /* If we see an R_BR or R_RBR reloc which is jumping to global
4961 linkage code, and it is followed by an appropriate cror nop
4962 instruction, we replace the cror with lwz r2,20(r1). This
4963 restores the TOC after the glink code. Contrariwise, if the
4964 call is followed by a lwz r2,20(r1), but the call is not
4965 going to global linkage code, we can replace the load with a
4967 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
4969 && h->root.type == bfd_link_hash_defined
4970 && (rel->r_vaddr - input_section->vma + 8
4971 <= input_section->_cooked_size))
4976 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
4977 next = bfd_get_32 (input_bfd, pnext);
4978 if (h->smclas == XMC_GL)
4980 if (next == 0x4def7b82 /* cror 15,15,15 */
4981 || next == 0x4ffffb82) /* cror 31,31,31 */
4982 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
4986 if (next == 0x80410014) /* lwz r1,20(r1) */
4987 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
4991 /* A PC relative reloc includes the section address. */
4992 if (howto.pc_relative)
4993 addend += input_section->vma;
4995 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
4997 rel->r_vaddr - input_section->vma,
5006 case bfd_reloc_overflow:
5009 char buf[SYMNMLEN + 1];
5010 char howto_name[10];
5015 name = h->root.root.string;
5018 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
5022 sprintf (howto_name, "0x%02x", rel->r_type);
5024 if (! ((*info->callbacks->reloc_overflow)
5025 (info, name, howto_name, (bfd_vma) 0, input_bfd,
5026 input_section, rel->r_vaddr - input_section->vma)))