1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996 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 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "xcoff-powermac") == 0)
40 /* Get the XCOFF hash table entries for a BFD. */
41 #define obj_xcoff_sym_hashes(bfd) \
42 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
44 /* XCOFF relocation types. These probably belong in a header file
45 somewhere. The relocations are described in the function
46 _bfd_ppc_xcoff_relocate_section in this file. */
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
71 /* The first word of global linkage code. This must be modified by
72 filling in the correct TOC offset. */
74 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
76 /* The remaining words of global linkage code. */
78 static unsigned long xcoff_glink_code[] =
80 0x90410014, /* stw r2,20(r1) */
81 0x800c0000, /* lwz r0,0(r12) */
82 0x804c0004, /* lwz r2,4(r12) */
83 0x7c0903a6, /* mtctr r0 */
84 0x4e800420, /* bctr */
85 0x0, /* start of traceback table */
86 0x000c8000, /* traceback table */
87 0x0 /* traceback table */
90 #define XCOFF_GLINK_SIZE \
91 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
93 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
94 This flag will only be used on input sections. */
96 #define SEC_MARK (SEC_ROM)
98 /* The ldhdr structure. This appears at the start of the .loader
101 struct internal_ldhdr
103 /* The version number: currently always 1. */
104 unsigned long l_version;
105 /* The number of symbol table entries. */
106 bfd_size_type l_nsyms;
107 /* The number of relocation table entries. */
108 bfd_size_type l_nreloc;
109 /* The length of the import file string table. */
110 bfd_size_type l_istlen;
111 /* The number of import files. */
112 bfd_size_type l_nimpid;
113 /* The offset from the start of the .loader section to the first
114 entry in the import file table. */
115 bfd_size_type l_impoff;
116 /* The length of the string table. */
117 bfd_size_type l_stlen;
118 /* The offset from the start of the .loader section to the first
119 entry in the string table. */
120 bfd_size_type l_stoff;
123 struct external_ldhdr
125 bfd_byte l_version[4];
127 bfd_byte l_nreloc[4];
128 bfd_byte l_istlen[4];
129 bfd_byte l_nimpid[4];
130 bfd_byte l_impoff[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
140 struct internal_ldsym
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name[SYMNMLEN];
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
155 /* The symbol value. */
157 /* The symbol section number. */
159 /* The symbol type and flags. */
161 /* The symbol storage class. */
163 /* The import file ID. */
164 bfd_size_type l_ifile;
165 /* Offset to the parameter type check string. */
166 bfd_size_type l_parm;
169 struct external_ldsym
173 bfd_byte _l_name[SYMNMLEN];
176 bfd_byte _l_zeroes[4];
177 bfd_byte _l_offset[4];
182 bfd_byte l_smtype[1];
183 bfd_byte l_smclas[1];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
196 #define L_ENTRY (0x20)
197 /* Exported symbol. */
198 #define L_EXPORT (0x10)
200 /* The ldrel structure. This is used to represent a reloc in the
203 struct internal_ldrel
205 /* The reloc address. */
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx;
209 /* The relocation type and size. */
211 /* The section number this relocation applies to. */
215 struct external_ldrel
218 bfd_byte l_symndx[4];
220 bfd_byte l_rsecnm[2];
223 #define LDRELSZ (2 * 4 + 2 * 2)
225 /* The list of import files. */
227 struct xcoff_import_file
229 /* The next entry in the list. */
230 struct xcoff_import_file *next;
235 /* The member name. */
239 /* An entry in the XCOFF linker hash table. */
241 struct xcoff_link_hash_entry
243 struct bfd_link_hash_entry root;
245 /* Symbol index in output file. Set to -1 initially. Set to -2 if
246 there is a reloc against this symbol. */
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection *toc_section;
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
258 /* If the TOC entry comes from an input file, this is set to the
259 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
263 /* If this symbol is a function entry point which is called, this
264 field holds a pointer to the function descriptor. If this symbol
265 is a function descriptor, this field holds a pointer to the
266 function entry point. */
267 struct xcoff_link_hash_entry *descriptor;
269 /* The .loader symbol table entry, if there is one. */
270 struct internal_ldsym *ldsym;
272 /* The .loader symbol table index. */
275 /* Some linker flags. */
276 unsigned short flags;
277 /* Symbol is referenced by a regular object. */
278 #define XCOFF_REF_REGULAR (01)
279 /* Symbol is defined by a regular object. */
280 #define XCOFF_DEF_REGULAR (02)
281 /* Symbol is defined by a dynamic object. */
282 #define XCOFF_DEF_DYNAMIC (04)
283 /* Symbol is used in a reloc being copied into the .loader section. */
284 #define XCOFF_LDREL (010)
285 /* Symbol is the entry point. */
286 #define XCOFF_ENTRY (020)
287 /* Symbol is called; this is, it appears in a R_BR reloc. */
288 #define XCOFF_CALLED (040)
289 /* Symbol needs the TOC entry filled in. */
290 #define XCOFF_SET_TOC (0100)
291 /* Symbol is explicitly imported. */
292 #define XCOFF_IMPORT (0200)
293 /* Symbol is explicitly exported. */
294 #define XCOFF_EXPORT (0400)
295 /* Symbol has been processed by xcoff_build_ldsyms. */
296 #define XCOFF_BUILT_LDSYM (01000)
297 /* Symbol is mentioned by a section which was not garbage collected. */
298 #define XCOFF_MARK (02000)
299 /* Symbol size is recorded in size_list list from hash table. */
300 #define XCOFF_HAS_SIZE (04000)
301 /* Symbol is a function descriptor. */
302 #define XCOFF_DESCRIPTOR (010000)
303 /* Multiple definitions have been for the symbol. */
304 #define XCOFF_MULTIPLY_DEFINED (020000)
306 /* The storage mapping class. */
307 unsigned char smclas;
310 /* The XCOFF linker hash table. */
312 struct xcoff_link_hash_table
314 struct bfd_link_hash_table root;
316 /* The .debug string hash table. We need to compute this while
317 reading the input files, so that we know how large the .debug
318 section will be before we assign section positions. */
319 struct bfd_strtab_hash *debug_strtab;
321 /* The .debug section we will use for the final output. */
322 asection *debug_section;
324 /* The .loader section we will use for the final output. */
325 asection *loader_section;
327 /* A count of non TOC relative relocs which will need to be
328 allocated in the .loader section. */
331 /* The .loader section header. */
332 struct internal_ldhdr ldhdr;
334 /* The .gl section we use to hold global linkage code. */
335 asection *linkage_section;
337 /* The .tc section we use to hold toc entries we build for global
339 asection *toc_section;
341 /* The .ds section we use to hold function descriptors which we
342 create for exported symbols. */
343 asection *descriptor_section;
345 /* The list of import files. */
346 struct xcoff_import_file *imports;
348 /* Required alignment of sections within the output file. */
349 unsigned long file_align;
351 /* Whether the .text section must be read-only. */
354 /* Whether garbage collection was done. */
357 /* A linked list of symbols for which we have size information. */
358 struct xcoff_link_size_list
360 struct xcoff_link_size_list *next;
361 struct xcoff_link_hash_entry *h;
365 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
366 asection *special_sections[6];
369 /* Information we keep for each section in the output file during the
372 struct xcoff_link_section_info
374 /* The relocs to be output. */
375 struct internal_reloc *relocs;
376 /* For each reloc against a global symbol whose index was not known
377 when the reloc was handled, the global hash table entry. */
378 struct xcoff_link_hash_entry **rel_hashes;
379 /* If there is a TOC relative reloc against a global symbol, and the
380 index of the TOC symbol is not known when the reloc was handled,
381 an entry is added to this linked list. This is not an array,
382 like rel_hashes, because this case is quite uncommon. */
383 struct xcoff_toc_rel_hash
385 struct xcoff_toc_rel_hash *next;
386 struct xcoff_link_hash_entry *h;
387 struct internal_reloc *rel;
391 /* Information that we pass around while doing the final link step. */
393 struct xcoff_final_link_info
395 /* General link information. */
396 struct bfd_link_info *info;
399 /* Hash table for long symbol names. */
400 struct bfd_strtab_hash *strtab;
401 /* Array of information kept for each output section, indexed by the
402 target_index field. */
403 struct xcoff_link_section_info *section_info;
404 /* Symbol index of last C_FILE symbol (-1 if none). */
405 long last_file_index;
406 /* Contents of last C_FILE symbol. */
407 struct internal_syment last_file;
408 /* Symbol index of TOC symbol. */
410 /* Start of .loader symbols. */
411 struct external_ldsym *ldsym;
412 /* Next .loader reloc to swap out. */
413 struct external_ldrel *ldrel;
414 /* File position of start of line numbers. */
415 file_ptr line_filepos;
416 /* Buffer large enough to hold swapped symbols of any input file. */
417 struct internal_syment *internal_syms;
418 /* Buffer large enough to hold output indices of symbols of any
421 /* Buffer large enough to hold output symbols for any input file. */
423 /* Buffer large enough to hold external line numbers for any input
426 /* Buffer large enough to hold any input section. */
428 /* Buffer large enough to hold external relocs of any input section. */
429 bfd_byte *external_relocs;
432 static void xcoff_swap_ldhdr_in
433 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
434 static void xcoff_swap_ldhdr_out
435 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
436 static void xcoff_swap_ldsym_in
437 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
438 static void xcoff_swap_ldsym_out
439 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
440 static void xcoff_swap_ldrel_in
441 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
442 static void xcoff_swap_ldrel_out
443 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
444 static struct bfd_hash_entry *xcoff_link_hash_newfunc
445 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
446 static struct internal_reloc *xcoff_read_internal_relocs
447 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
448 struct internal_reloc *));
449 static boolean xcoff_link_add_object_symbols
450 PARAMS ((bfd *, struct bfd_link_info *));
451 static boolean xcoff_link_check_archive_element
452 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
453 static boolean xcoff_link_check_ar_symbols
454 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
455 static boolean xcoff_link_check_dynamic_ar_symbols
456 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
457 static bfd_size_type xcoff_find_reloc
458 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
459 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
460 static boolean xcoff_link_add_dynamic_symbols
461 PARAMS ((bfd *, struct bfd_link_info *));
462 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
463 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
464 static boolean xcoff_build_ldsyms
465 PARAMS ((struct xcoff_link_hash_entry *, PTR));
466 static boolean xcoff_link_input_bfd
467 PARAMS ((struct xcoff_final_link_info *, bfd *));
468 static boolean xcoff_write_global_symbol
469 PARAMS ((struct xcoff_link_hash_entry *, PTR));
470 static boolean xcoff_reloc_link_order
471 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
472 struct bfd_link_order *));
473 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
475 /* Routines to swap information in the XCOFF .loader section. If we
476 ever need to write an XCOFF loader, this stuff will need to be
477 moved to another file shared by the linker (which XCOFF calls the
478 ``binder'') and the loader. */
480 /* Swap in the ldhdr structure. */
483 xcoff_swap_ldhdr_in (abfd, src, dst)
485 const struct external_ldhdr *src;
486 struct internal_ldhdr *dst;
488 dst->l_version = bfd_get_32 (abfd, src->l_version);
489 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
490 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
491 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
492 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
493 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
494 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
495 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
498 /* Swap out the ldhdr structure. */
501 xcoff_swap_ldhdr_out (abfd, src, dst)
503 const struct internal_ldhdr *src;
504 struct external_ldhdr *dst;
506 bfd_put_32 (abfd, src->l_version, dst->l_version);
507 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
508 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
509 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
510 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
511 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
512 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
513 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
516 /* Swap in the ldsym structure. */
519 xcoff_swap_ldsym_in (abfd, src, dst)
521 const struct external_ldsym *src;
522 struct internal_ldsym *dst;
524 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
525 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
528 dst->_l._l_l._l_zeroes = 0;
529 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
531 dst->l_value = bfd_get_32 (abfd, src->l_value);
532 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
533 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
534 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
535 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
536 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
539 /* Swap out the ldsym structure. */
542 xcoff_swap_ldsym_out (abfd, src, dst)
544 const struct internal_ldsym *src;
545 struct external_ldsym *dst;
547 if (src->_l._l_l._l_zeroes != 0)
548 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
551 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
552 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
554 bfd_put_32 (abfd, src->l_value, dst->l_value);
555 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
556 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
557 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
558 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
559 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
562 /* Swap in the ldrel structure. */
565 xcoff_swap_ldrel_in (abfd, src, dst)
567 const struct external_ldrel *src;
568 struct internal_ldrel *dst;
570 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
571 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
572 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
573 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
576 /* Swap out the ldrel structure. */
579 xcoff_swap_ldrel_out (abfd, src, dst)
581 const struct internal_ldrel *src;
582 struct external_ldrel *dst;
584 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
585 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
586 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
587 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
590 /* Routines to read XCOFF dynamic information. This don't really
591 belong here, but we already have the ldsym manipulation routines
594 /* Read the contents of a section. */
597 xcoff_get_section_contents (abfd, sec)
601 if (coff_section_data (abfd, sec) == NULL)
603 sec->used_by_bfd = bfd_zalloc (abfd,
604 sizeof (struct coff_section_tdata));
605 if (sec->used_by_bfd == NULL)
609 if (coff_section_data (abfd, sec)->contents == NULL)
611 coff_section_data (abfd, sec)->contents = bfd_malloc (sec->_raw_size);
612 if (coff_section_data (abfd, sec)->contents == NULL)
615 if (! bfd_get_section_contents (abfd, sec,
616 coff_section_data (abfd, sec)->contents,
617 (file_ptr) 0, sec->_raw_size))
624 /* Get the size required to hold the dynamic symbols. */
627 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
632 struct internal_ldhdr ldhdr;
634 if ((abfd->flags & DYNAMIC) == 0)
636 bfd_set_error (bfd_error_invalid_operation);
640 lsec = bfd_get_section_by_name (abfd, ".loader");
643 bfd_set_error (bfd_error_no_symbols);
647 if (! xcoff_get_section_contents (abfd, lsec))
649 contents = coff_section_data (abfd, lsec)->contents;
651 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
653 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
656 /* Get the dynamic symbols. */
659 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
665 struct internal_ldhdr ldhdr;
667 struct external_ldsym *elsym, *elsymend;
668 coff_symbol_type *symbuf;
670 if ((abfd->flags & DYNAMIC) == 0)
672 bfd_set_error (bfd_error_invalid_operation);
676 lsec = bfd_get_section_by_name (abfd, ".loader");
679 bfd_set_error (bfd_error_no_symbols);
683 if (! xcoff_get_section_contents (abfd, lsec))
685 contents = coff_section_data (abfd, lsec)->contents;
687 coff_section_data (abfd, lsec)->keep_contents = true;
689 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
691 strings = (char *) contents + ldhdr.l_stoff;
693 symbuf = ((coff_symbol_type *)
694 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
698 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
699 elsymend = elsym + ldhdr.l_nsyms;
700 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
702 struct internal_ldsym ldsym;
704 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
706 symbuf->symbol.the_bfd = abfd;
708 if (ldsym._l._l_l._l_zeroes == 0)
709 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
714 for (i = 0; i < SYMNMLEN; i++)
715 if (ldsym._l._l_name[i] == '\0')
718 symbuf->symbol.name = elsym->_l._l_name;
723 c = bfd_alloc (abfd, SYMNMLEN + 1);
726 memcpy (c, ldsym._l._l_name, SYMNMLEN);
728 symbuf->symbol.name = c;
732 if (ldsym.l_smclas == XMC_XO)
733 symbuf->symbol.section = bfd_abs_section_ptr;
735 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
737 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
739 symbuf->symbol.flags = BSF_NO_FLAGS;
740 if ((ldsym.l_smtype & L_EXPORT) != 0)
741 symbuf->symbol.flags |= BSF_GLOBAL;
743 /* FIXME: We have no way to record the other information stored
744 with the loader symbol. */
746 *psyms = (asymbol *) symbuf;
751 return ldhdr.l_nsyms;
754 /* Get the size required to hold the dynamic relocs. */
757 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
762 struct internal_ldhdr ldhdr;
764 if ((abfd->flags & DYNAMIC) == 0)
766 bfd_set_error (bfd_error_invalid_operation);
770 lsec = bfd_get_section_by_name (abfd, ".loader");
773 bfd_set_error (bfd_error_no_symbols);
777 if (! xcoff_get_section_contents (abfd, lsec))
779 contents = coff_section_data (abfd, lsec)->contents;
781 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
783 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
786 /* The typical dynamic reloc. */
788 static reloc_howto_type xcoff_dynamic_reloc =
791 2, /* size (0 = byte, 1 = short, 2 = long) */
793 false, /* pc_relative */
795 complain_overflow_bitfield, /* complain_on_overflow */
796 0, /* special_function */
798 true, /* partial_inplace */
799 0xffffffff, /* src_mask */
800 0xffffffff, /* dst_mask */
801 false); /* pcrel_offset */
803 /* Get the dynamic relocs. */
806 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
813 struct internal_ldhdr ldhdr;
815 struct external_ldrel *elrel, *elrelend;
817 if ((abfd->flags & DYNAMIC) == 0)
819 bfd_set_error (bfd_error_invalid_operation);
823 lsec = bfd_get_section_by_name (abfd, ".loader");
826 bfd_set_error (bfd_error_no_symbols);
830 if (! xcoff_get_section_contents (abfd, lsec))
832 contents = coff_section_data (abfd, lsec)->contents;
834 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
836 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
840 elrel = ((struct external_ldrel *)
841 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
842 elrelend = elrel + ldhdr.l_nreloc;
843 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
845 struct internal_ldrel ldrel;
847 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
849 if (ldrel.l_symndx >= 3)
850 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
856 switch (ldrel.l_symndx)
872 sec = bfd_get_section_by_name (abfd, name);
875 bfd_set_error (bfd_error_bad_value);
879 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
882 relbuf->address = ldrel.l_vaddr;
885 /* Most dynamic relocs have the same type. FIXME: This is only
886 correct if ldrel.l_rtype == 0. In other cases, we should use
887 a different howto. */
888 relbuf->howto = &xcoff_dynamic_reloc;
890 /* FIXME: We have no way to record the l_rsecnm field. */
897 return ldhdr.l_nreloc;
900 /* Routine to create an entry in an XCOFF link hash table. */
902 static struct bfd_hash_entry *
903 xcoff_link_hash_newfunc (entry, table, string)
904 struct bfd_hash_entry *entry;
905 struct bfd_hash_table *table;
908 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
910 /* Allocate the structure if it has not already been allocated by a
912 if (ret == (struct xcoff_link_hash_entry *) NULL)
913 ret = ((struct xcoff_link_hash_entry *)
914 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
915 if (ret == (struct xcoff_link_hash_entry *) NULL)
916 return (struct bfd_hash_entry *) ret;
918 /* Call the allocation method of the superclass. */
919 ret = ((struct xcoff_link_hash_entry *)
920 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
924 /* Set local fields. */
926 ret->toc_section = NULL;
927 ret->u.toc_indx = -1;
928 ret->descriptor = NULL;
932 ret->smclas = XMC_UA;
935 return (struct bfd_hash_entry *) ret;
938 /* Create a XCOFF link hash table. */
940 struct bfd_link_hash_table *
941 _bfd_xcoff_bfd_link_hash_table_create (abfd)
944 struct xcoff_link_hash_table *ret;
946 ret = ((struct xcoff_link_hash_table *)
947 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
948 if (ret == (struct xcoff_link_hash_table *) NULL)
949 return (struct bfd_link_hash_table *) NULL;
950 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
952 bfd_release (abfd, ret);
953 return (struct bfd_link_hash_table *) NULL;
956 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
957 ret->debug_section = NULL;
958 ret->loader_section = NULL;
959 ret->ldrel_count = 0;
960 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
961 ret->linkage_section = NULL;
962 ret->toc_section = NULL;
963 ret->descriptor_section = NULL;
968 memset (ret->special_sections, 0, sizeof ret->special_sections);
970 /* The linker will always generate a full a.out header. We need to
971 record that fact now, before the sizeof_headers routine could be
973 xcoff_data (abfd)->full_aouthdr = true;
978 /* Look up an entry in an XCOFF link hash table. */
980 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
981 ((struct xcoff_link_hash_entry *) \
982 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
985 /* Traverse an XCOFF link hash table. */
987 #define xcoff_link_hash_traverse(table, func, info) \
988 (bfd_link_hash_traverse \
990 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
993 /* Get the XCOFF link hash table from the info structure. This is
996 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
998 /* Read internal relocs for an XCOFF csect. This is a wrapper around
999 _bfd_coff_read_internal_relocs which tries to take advantage of any
1000 relocs which may have been cached for the enclosing section. */
1002 static struct internal_reloc *
1003 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
1004 require_internal, internal_relocs)
1008 bfd_byte *external_relocs;
1009 boolean require_internal;
1010 struct internal_reloc *internal_relocs;
1012 if (coff_section_data (abfd, sec) != NULL
1013 && coff_section_data (abfd, sec)->relocs == NULL
1014 && xcoff_section_data (abfd, sec) != NULL)
1016 asection *enclosing;
1018 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1020 if (enclosing != NULL
1021 && (coff_section_data (abfd, enclosing) == NULL
1022 || coff_section_data (abfd, enclosing)->relocs == NULL)
1024 && enclosing->reloc_count > 0)
1026 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1027 external_relocs, false,
1028 (struct internal_reloc *) NULL)
1033 if (enclosing != NULL
1034 && coff_section_data (abfd, enclosing) != NULL
1035 && coff_section_data (abfd, enclosing)->relocs != NULL)
1039 off = ((sec->rel_filepos - enclosing->rel_filepos)
1040 / bfd_coff_relsz (abfd));
1041 if (! require_internal)
1042 return coff_section_data (abfd, enclosing)->relocs + off;
1043 memcpy (internal_relocs,
1044 coff_section_data (abfd, enclosing)->relocs + off,
1045 sec->reloc_count * sizeof (struct internal_reloc));
1046 return internal_relocs;
1050 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1051 require_internal, internal_relocs);
1054 /* Given an XCOFF BFD, add symbols to the global hash table as
1058 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
1060 struct bfd_link_info *info;
1062 switch (bfd_get_format (abfd))
1065 return xcoff_link_add_object_symbols (abfd, info);
1068 /* If the archive has a map, do the usual search. We then need
1069 to check the archive for stripped dynamic objects, because
1070 they will not appear in the archive map even though they
1071 should, perhaps, be included. If the archive has no map, we
1072 just consider each object file in turn, since that apparently
1073 is what the AIX native linker does. */
1074 if (bfd_has_map (abfd))
1076 if (! (_bfd_generic_link_add_archive_symbols
1077 (abfd, info, xcoff_link_check_archive_element)))
1084 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1085 while (member != NULL)
1087 if (bfd_check_format (member, bfd_object)
1088 && (! bfd_has_map (abfd)
1089 || ((member->flags & DYNAMIC) != 0
1090 && (member->flags & HAS_SYMS) == 0)))
1094 if (! xcoff_link_check_archive_element (member, info, &needed))
1097 member->archive_pass = -1;
1099 member = bfd_openr_next_archived_file (abfd, member);
1106 bfd_set_error (bfd_error_wrong_format);
1111 /* Add symbols from an XCOFF object file. */
1114 xcoff_link_add_object_symbols (abfd, info)
1116 struct bfd_link_info *info;
1118 if (! _bfd_coff_get_external_symbols (abfd))
1120 if (! xcoff_link_add_symbols (abfd, info))
1122 if (! info->keep_memory)
1124 if (! _bfd_coff_free_symbols (abfd))
1130 /* Check a single archive element to see if we need to include it in
1131 the link. *PNEEDED is set according to whether this element is
1132 needed in the link or not. This is called via
1133 _bfd_generic_link_add_archive_symbols. */
1136 xcoff_link_check_archive_element (abfd, info, pneeded)
1138 struct bfd_link_info *info;
1141 if (! _bfd_coff_get_external_symbols (abfd))
1144 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1149 if (! xcoff_link_add_symbols (abfd, info))
1153 if (! info->keep_memory || ! *pneeded)
1155 if (! _bfd_coff_free_symbols (abfd))
1162 /* Look through the symbols to see if this object file should be
1163 included in the link. */
1166 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1168 struct bfd_link_info *info;
1171 bfd_size_type symesz;
1177 if ((abfd->flags & DYNAMIC) != 0
1178 && ! info->static_link
1179 && info->hash->creator == abfd->xvec)
1180 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1182 symesz = bfd_coff_symesz (abfd);
1183 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1184 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1185 while (esym < esym_end)
1187 struct internal_syment sym;
1189 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1191 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1194 char buf[SYMNMLEN + 1];
1195 struct bfd_link_hash_entry *h;
1197 /* This symbol is externally visible, and is defined by this
1200 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1203 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1205 /* We are only interested in symbols that are currently
1206 undefined. If a symbol is currently known to be common,
1207 XCOFF linkers do not bring in an object file which
1208 defines it. We also don't bring in symbols to satisfy
1209 undefined references in shared objects. */
1210 if (h != (struct bfd_link_hash_entry *) NULL
1211 && h->type == bfd_link_hash_undefined
1212 && (info->hash->creator != abfd->xvec
1213 || (((struct xcoff_link_hash_entry *) h)->flags
1214 & XCOFF_DEF_DYNAMIC) == 0))
1216 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1223 esym += (sym.n_numaux + 1) * symesz;
1226 /* We do not need this object file. */
1230 /* Look through the loader symbols to see if this dynamic object
1231 should be included in the link. The native linker uses the loader
1232 symbols, not the normal symbol table, so we do too. */
1235 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1237 struct bfd_link_info *info;
1242 struct internal_ldhdr ldhdr;
1243 const char *strings;
1244 struct external_ldsym *elsym, *elsymend;
1248 lsec = bfd_get_section_by_name (abfd, ".loader");
1251 /* There are no symbols, so don't try to include it. */
1255 if (! xcoff_get_section_contents (abfd, lsec))
1257 buf = coff_section_data (abfd, lsec)->contents;
1259 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1261 strings = (char *) buf + ldhdr.l_stoff;
1263 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1264 elsymend = elsym + ldhdr.l_nsyms;
1265 for (; elsym < elsymend; elsym++)
1267 struct internal_ldsym ldsym;
1268 char nambuf[SYMNMLEN + 1];
1270 struct bfd_link_hash_entry *h;
1272 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1274 /* We are only interested in exported symbols. */
1275 if ((ldsym.l_smtype & L_EXPORT) == 0)
1278 if (ldsym._l._l_l._l_zeroes == 0)
1279 name = strings + ldsym._l._l_l._l_offset;
1282 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1283 nambuf[SYMNMLEN] = '\0';
1287 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1289 /* We are only interested in symbols that are currently
1290 undefined. At this point we know that we are using an XCOFF
1293 && h->type == bfd_link_hash_undefined
1294 && (((struct xcoff_link_hash_entry *) h)->flags
1295 & XCOFF_DEF_DYNAMIC) == 0)
1297 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1304 /* We do not need this shared object. */
1306 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1308 free (coff_section_data (abfd, lsec)->contents);
1309 coff_section_data (abfd, lsec)->contents = NULL;
1315 /* Returns the index of reloc in RELOCS with the least address greater
1316 than or equal to ADDRESS. The relocs are sorted by address. */
1318 static bfd_size_type
1319 xcoff_find_reloc (relocs, count, address)
1320 struct internal_reloc *relocs;
1321 bfd_size_type count;
1324 bfd_size_type min, max, this;
1328 if (count == 1 && relocs[0].r_vaddr < address)
1337 /* Do a binary search over (min,max]. */
1338 while (min + 1 < max)
1342 this = (max + min) / 2;
1343 raddr = relocs[this].r_vaddr;
1344 if (raddr > address)
1346 else if (raddr < address)
1355 if (relocs[min].r_vaddr < address)
1359 && relocs[min - 1].r_vaddr == address)
1365 /* Add all the symbols from an object file to the hash table.
1367 XCOFF is a weird format. A normal XCOFF .o files will have three
1368 COFF sections--.text, .data, and .bss--but each COFF section will
1369 contain many csects. These csects are described in the symbol
1370 table. From the linker's point of view, each csect must be
1371 considered a section in its own right. For example, a TOC entry is
1372 handled as a small XMC_TC csect. The linker must be able to merge
1373 different TOC entries together, which means that it must be able to
1374 extract the XMC_TC csects from the .data section of the input .o
1377 From the point of view of our linker, this is, of course, a hideous
1378 nightmare. We cope by actually creating sections for each csect,
1379 and discarding the original sections. We then have to handle the
1380 relocation entries carefully, since the only way to tell which
1381 csect they belong to is to examine the address. */
1384 xcoff_link_add_symbols (abfd, info)
1386 struct bfd_link_info *info;
1388 unsigned int n_tmask;
1389 unsigned int n_btshft;
1390 boolean default_copy;
1391 bfd_size_type symcount;
1392 struct xcoff_link_hash_entry **sym_hash;
1393 asection **csect_cache;
1394 bfd_size_type linesz;
1396 asection *last_real;
1399 unsigned int csect_index;
1400 asection *first_csect;
1401 bfd_size_type symesz;
1404 struct reloc_info_struct
1406 struct internal_reloc *relocs;
1409 } *reloc_info = NULL;
1411 if ((abfd->flags & DYNAMIC) != 0
1412 && ! info->static_link)
1414 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1418 if (info->hash->creator == abfd->xvec)
1420 /* We need to build a .loader section, so we do it here. This
1421 won't work if we're producing an XCOFF output file with no
1422 XCOFF input files. FIXME. */
1423 if (xcoff_hash_table (info)->loader_section == NULL)
1427 lsec = bfd_make_section_anyway (abfd, ".loader");
1430 xcoff_hash_table (info)->loader_section = lsec;
1431 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1433 /* Likewise for the linkage section. */
1434 if (xcoff_hash_table (info)->linkage_section == NULL)
1438 lsec = bfd_make_section_anyway (abfd, ".gl");
1441 xcoff_hash_table (info)->linkage_section = lsec;
1442 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1444 lsec->alignment_power = 2;
1446 /* Likewise for the TOC section. */
1447 if (xcoff_hash_table (info)->toc_section == NULL)
1451 tsec = bfd_make_section_anyway (abfd, ".tc");
1454 xcoff_hash_table (info)->toc_section = tsec;
1455 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1457 tsec->alignment_power = 2;
1459 /* Likewise for the descriptor section. */
1460 if (xcoff_hash_table (info)->descriptor_section == NULL)
1464 dsec = bfd_make_section_anyway (abfd, ".ds");
1467 xcoff_hash_table (info)->descriptor_section = dsec;
1468 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1470 dsec->alignment_power = 2;
1472 /* Likewise for the .debug section. */
1473 if (xcoff_hash_table (info)->debug_section == NULL)
1477 dsec = bfd_make_section_anyway (abfd, ".debug");
1480 xcoff_hash_table (info)->debug_section = dsec;
1481 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1485 if ((abfd->flags & DYNAMIC) != 0
1486 && ! info->static_link)
1489 n_tmask = coff_data (abfd)->local_n_tmask;
1490 n_btshft = coff_data (abfd)->local_n_btshft;
1492 /* Define macros so that ISFCN, et. al., macros work correctly. */
1493 #define N_TMASK n_tmask
1494 #define N_BTSHFT n_btshft
1496 if (info->keep_memory)
1497 default_copy = false;
1499 default_copy = true;
1501 symcount = obj_raw_syment_count (abfd);
1503 /* We keep a list of the linker hash table entries that correspond
1504 to each external symbol. */
1505 sym_hash = ((struct xcoff_link_hash_entry **)
1508 * sizeof (struct xcoff_link_hash_entry *))));
1509 if (sym_hash == NULL && symcount != 0)
1511 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1512 memset (sym_hash, 0,
1513 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1515 /* Because of the weird stuff we are doing with XCOFF csects, we can
1516 not easily determine which section a symbol is in, so we store
1517 the information in the tdata for the input file. */
1518 csect_cache = ((asection **)
1519 bfd_alloc (abfd, symcount * sizeof (asection *)));
1520 if (csect_cache == NULL && symcount != 0)
1522 xcoff_data (abfd)->csects = csect_cache;
1523 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1525 /* While splitting sections into csects, we need to assign the
1526 relocs correctly. The relocs and the csects must both be in
1527 order by VMA within a given section, so we handle this by
1528 scanning along the relocs as we process the csects. We index
1529 into reloc_info using the section target_index. */
1530 reloc_info = ((struct reloc_info_struct *)
1531 bfd_malloc ((abfd->section_count + 1)
1532 * sizeof (struct reloc_info_struct)));
1533 if (reloc_info == NULL)
1535 memset ((PTR) reloc_info, 0,
1536 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1538 /* Read in the relocs and line numbers for each section. */
1539 linesz = bfd_coff_linesz (abfd);
1541 for (o = abfd->sections; o != NULL; o = o->next)
1544 if ((o->flags & SEC_RELOC) != 0)
1546 reloc_info[o->target_index].relocs =
1547 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1548 false, (struct internal_reloc *) NULL);
1549 reloc_info[o->target_index].csects =
1550 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1551 if (reloc_info[o->target_index].csects == NULL)
1553 memset (reloc_info[o->target_index].csects, 0,
1554 o->reloc_count * sizeof (asection *));
1557 if ((info->strip == strip_none || info->strip == strip_some)
1558 && o->lineno_count > 0)
1562 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1563 if (linenos == NULL)
1565 reloc_info[o->target_index].linenos = linenos;
1566 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1567 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1568 != linesz * o->lineno_count))
1573 /* Don't let the linker relocation routines discard the symbols. */
1574 keep_syms = obj_coff_keep_syms (abfd);
1575 obj_coff_keep_syms (abfd) = true;
1581 symesz = bfd_coff_symesz (abfd);
1582 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1583 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1584 esym_end = esym + symcount * symesz;
1585 while (esym < esym_end)
1587 struct internal_syment sym;
1588 union internal_auxent aux;
1590 char buf[SYMNMLEN + 1];
1595 struct xcoff_link_hash_entry *set_toc;
1597 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1599 /* In this pass we are only interested in symbols with csect
1601 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1603 if (sym.n_sclass == C_FILE && csect != NULL)
1605 xcoff_section_data (abfd, csect)->last_symndx =
1607 - (bfd_byte *) obj_coff_external_syms (abfd))
1613 *csect_cache = csect;
1614 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1615 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1617 *csect_cache = NULL;
1618 esym += (sym.n_numaux + 1) * symesz;
1619 sym_hash += sym.n_numaux + 1;
1620 csect_cache += sym.n_numaux + 1;
1624 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1628 /* If this symbol has line number information attached to it,
1629 and we're not stripping it, count the number of entries and
1630 add them to the count for this csect. In the final link pass
1631 we are going to attach line number information by symbol,
1632 rather than by section, in order to more easily handle
1633 garbage collection. */
1634 if ((info->strip == strip_none || info->strip == strip_some)
1637 && ISFCN (sym.n_type))
1639 union internal_auxent auxlin;
1641 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1642 sym.n_type, sym.n_sclass,
1643 0, sym.n_numaux, (PTR) &auxlin);
1644 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1646 asection *enclosing;
1647 bfd_size_type linoff;
1649 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1650 if (enclosing == NULL)
1652 (*_bfd_error_handler)
1653 ("%s: `%s' has line numbers but no enclosing section",
1654 bfd_get_filename (abfd), name);
1655 bfd_set_error (bfd_error_bad_value);
1658 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1659 - enclosing->line_filepos);
1660 if (linoff < enclosing->lineno_count * linesz)
1662 struct internal_lineno lin;
1663 bfd_byte *linpstart;
1665 linpstart = (reloc_info[enclosing->target_index].linenos
1667 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1669 && ((bfd_size_type) lin.l_addr.l_symndx
1671 - (bfd_byte *) obj_coff_external_syms (abfd))
1674 bfd_byte *linpend, *linp;
1676 linpend = (reloc_info[enclosing->target_index].linenos
1677 + enclosing->lineno_count * linesz);
1678 for (linp = linpstart + linesz;
1682 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1684 if (lin.l_lnno == 0)
1687 csect->lineno_count += (linp - linpstart) / linesz;
1688 /* The setting of line_filepos will only be
1689 useful if all the line number entries for a
1690 csect are contiguous; this only matters for
1692 if (csect->line_filepos == 0)
1693 csect->line_filepos =
1694 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1700 /* Pick up the csect auxiliary information. */
1702 if (sym.n_numaux == 0)
1704 (*_bfd_error_handler)
1705 ("%s: class %d symbol `%s' has no aux entries",
1706 bfd_get_filename (abfd), sym.n_sclass, name);
1707 bfd_set_error (bfd_error_bad_value);
1711 bfd_coff_swap_aux_in (abfd,
1712 (PTR) (esym + symesz * sym.n_numaux),
1713 sym.n_type, sym.n_sclass,
1714 sym.n_numaux - 1, sym.n_numaux,
1717 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1727 (*_bfd_error_handler)
1728 ("%s: symbol `%s' has unrecognized csect type %d",
1729 bfd_get_filename (abfd), name, smtyp);
1730 bfd_set_error (bfd_error_bad_value);
1734 /* This is an external reference. */
1735 if (sym.n_sclass == C_HIDEXT
1736 || sym.n_scnum != N_UNDEF
1737 || aux.x_csect.x_scnlen.l != 0)
1739 (*_bfd_error_handler)
1740 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1741 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1742 aux.x_csect.x_scnlen.l);
1743 bfd_set_error (bfd_error_bad_value);
1747 /* An XMC_XO external reference is actually a reference to
1748 an absolute location. */
1749 if (aux.x_csect.x_smclas != XMC_XO)
1750 section = bfd_und_section_ptr;
1753 section = bfd_abs_section_ptr;
1754 value = sym.n_value;
1759 /* This is a csect definition. */
1763 xcoff_section_data (abfd, csect)->last_symndx =
1765 - (bfd_byte *) obj_coff_external_syms (abfd))
1772 /* When we see a TOC anchor, we record the TOC value. */
1773 if (aux.x_csect.x_smclas == XMC_TC0)
1775 if (sym.n_sclass != C_HIDEXT
1776 || aux.x_csect.x_scnlen.l != 0)
1778 (*_bfd_error_handler)
1779 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1780 bfd_get_filename (abfd), name, sym.n_sclass,
1781 aux.x_csect.x_scnlen.l);
1782 bfd_set_error (bfd_error_bad_value);
1785 xcoff_data (abfd)->toc = sym.n_value;
1788 /* We must merge TOC entries for the same symbol. We can
1789 merge two TOC entries if they are both C_HIDEXT, they
1790 both have the same name, they are both 4 bytes long, and
1791 they both have a relocation table entry for an external
1792 symbol with the same name. Unfortunately, this means
1793 that we must look through the relocations. Ick. */
1794 if (aux.x_csect.x_smclas == XMC_TC
1795 && sym.n_sclass == C_HIDEXT
1796 && aux.x_csect.x_scnlen.l == 4
1797 && info->hash->creator == abfd->xvec)
1799 asection *enclosing;
1800 struct internal_reloc *relocs;
1801 bfd_size_type relindx;
1802 struct internal_reloc *rel;
1804 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1805 if (enclosing == NULL)
1808 relocs = reloc_info[enclosing->target_index].relocs;
1809 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1811 rel = relocs + relindx;
1812 if (relindx < enclosing->reloc_count
1813 && rel->r_vaddr == (bfd_vma) sym.n_value
1814 && rel->r_size == 31
1815 && rel->r_type == R_POS)
1818 struct internal_syment relsym;
1820 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1821 + rel->r_symndx * symesz);
1822 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1823 if (relsym.n_sclass == C_EXT)
1825 const char *relname;
1826 char relbuf[SYMNMLEN + 1];
1828 struct xcoff_link_hash_entry *h;
1830 /* At this point we know that the TOC entry is
1831 for an externally visible symbol. */
1832 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1834 if (relname == NULL)
1837 /* We only merge TOC entries if the TC name is
1838 the same as the symbol name. This handles
1839 the normal case, but not common cases like
1840 SYM.P4 which gcc generates to store SYM + 4
1841 in the TOC. FIXME. */
1842 if (strcmp (name, relname) == 0)
1844 copy = (! info->keep_memory
1845 || relsym._n._n_n._n_zeroes != 0
1846 || relsym._n._n_n._n_offset == 0);
1847 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1848 relname, true, copy,
1853 /* At this point h->root.type could be
1854 bfd_link_hash_new. That should be OK,
1855 since we know for sure that we will come
1856 across this symbol as we step through the
1859 /* We store h in *sym_hash for the
1860 convenience of the relocate_section
1864 if (h->toc_section != NULL)
1866 asection **rel_csects;
1868 /* We already have a TOC entry for this
1869 symbol, so we can just ignore this
1872 reloc_info[enclosing->target_index].csects;
1873 rel_csects[relindx] = bfd_und_section_ptr;
1877 /* We are about to create a TOC entry for
1885 /* We need to create a new section. We get the name from
1886 the csect storage mapping class, so that the linker can
1887 accumulate similar csects together. */
1889 static const char *csect_name_by_class[] =
1891 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1892 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1895 const char *csect_name;
1896 asection *enclosing;
1898 if ((aux.x_csect.x_smclas >=
1899 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1900 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1902 (*_bfd_error_handler)
1903 ("%s: symbol `%s' has unrecognized smclas %d",
1904 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1905 bfd_set_error (bfd_error_bad_value);
1909 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1910 csect = bfd_make_section_anyway (abfd, csect_name);
1913 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1914 if (enclosing == NULL)
1916 if (! bfd_is_abs_section (enclosing)
1917 && ((bfd_vma) sym.n_value < enclosing->vma
1918 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1919 > enclosing->vma + enclosing->_raw_size)))
1921 (*_bfd_error_handler)
1922 ("%s: csect `%s' not in enclosing section",
1923 bfd_get_filename (abfd), name);
1924 bfd_set_error (bfd_error_bad_value);
1927 csect->vma = sym.n_value;
1928 csect->filepos = (enclosing->filepos
1931 csect->_raw_size = aux.x_csect.x_scnlen.l;
1932 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1933 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1935 /* Record the enclosing section in the tdata for this new
1937 csect->used_by_bfd =
1938 ((struct coff_section_tdata *)
1939 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1940 if (csect->used_by_bfd == NULL)
1942 coff_section_data (abfd, csect)->tdata =
1943 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1944 if (coff_section_data (abfd, csect)->tdata == NULL)
1946 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1947 xcoff_section_data (abfd, csect)->lineno_count =
1948 enclosing->lineno_count;
1950 if (enclosing->owner == abfd)
1952 struct internal_reloc *relocs;
1953 bfd_size_type relindx;
1954 struct internal_reloc *rel;
1955 asection **rel_csect;
1957 relocs = reloc_info[enclosing->target_index].relocs;
1958 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1960 rel = relocs + relindx;
1961 rel_csect = (reloc_info[enclosing->target_index].csects
1963 csect->rel_filepos = (enclosing->rel_filepos
1964 + relindx * bfd_coff_relsz (abfd));
1965 while (relindx < enclosing->reloc_count
1966 && *rel_csect == NULL
1967 && rel->r_vaddr < csect->vma + csect->_raw_size)
1970 csect->flags |= SEC_RELOC;
1971 ++csect->reloc_count;
1978 /* There are a number of other fields and section flags
1979 which we do not bother to set. */
1981 csect_index = ((esym
1982 - (bfd_byte *) obj_coff_external_syms (abfd))
1985 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1987 if (first_csect == NULL)
1988 first_csect = csect;
1990 /* If this symbol is C_EXT, we treat it as starting at the
1991 beginning of the newly created section. */
1992 if (sym.n_sclass == C_EXT)
1998 /* If this is a TOC section for a symbol, record it. */
1999 if (set_toc != NULL)
2000 set_toc->toc_section = csect;
2005 /* This is a label definition. The x_scnlen field is the
2006 symbol index of the csect. I believe that this must
2007 always follow the appropriate XTY_SD symbol, so I will
2013 if (aux.x_csect.x_scnlen.l < 0
2014 || (aux.x_csect.x_scnlen.l
2015 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2019 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2021 || (section->flags & SEC_HAS_CONTENTS) == 0)
2026 (*_bfd_error_handler)
2027 ("%s: misplaced XTY_LD `%s'",
2028 bfd_get_filename (abfd), name);
2029 bfd_set_error (bfd_error_bad_value);
2033 value = sym.n_value - csect->vma;
2038 /* This is an unitialized csect. We could base the name on
2039 the storage mapping class, but we don't bother. If this
2040 csect is externally visible, it is a common symbol. */
2044 xcoff_section_data (abfd, csect)->last_symndx =
2046 - (bfd_byte *) obj_coff_external_syms (abfd))
2050 csect = bfd_make_section_anyway (abfd, ".bss");
2053 csect->vma = sym.n_value;
2054 csect->_raw_size = aux.x_csect.x_scnlen.l;
2055 csect->flags |= SEC_ALLOC;
2056 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2057 /* There are a number of other fields and section flags
2058 which we do not bother to set. */
2060 csect_index = ((esym
2061 - (bfd_byte *) obj_coff_external_syms (abfd))
2064 csect->used_by_bfd =
2065 ((struct coff_section_tdata *)
2066 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
2067 if (csect->used_by_bfd == NULL)
2069 coff_section_data (abfd, csect)->tdata =
2070 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2071 if (coff_section_data (abfd, csect)->tdata == NULL)
2073 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2075 if (first_csect == NULL)
2076 first_csect = csect;
2078 if (sym.n_sclass == C_EXT)
2080 csect->flags |= SEC_IS_COMMON;
2081 csect->_raw_size = 0;
2083 value = aux.x_csect.x_scnlen.l;
2089 /* Check for magic symbol names. */
2090 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2091 && aux.x_csect.x_smclas != XMC_TC
2092 && aux.x_csect.x_smclas != XMC_TD)
2099 if (strcmp (name, "_text") == 0)
2101 else if (strcmp (name, "_etext") == 0)
2103 else if (strcmp (name, "_data") == 0)
2105 else if (strcmp (name, "_edata") == 0)
2107 else if (strcmp (name, "_end") == 0)
2110 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2114 xcoff_hash_table (info)->special_sections[i] = csect;
2117 /* Now we have enough information to add the symbol to the
2118 linker hash table. */
2120 if (sym.n_sclass == C_EXT)
2124 BFD_ASSERT (section != NULL);
2126 /* We must copy the name into memory if we got it from the
2127 syment itself, rather than the string table. */
2128 copy = default_copy;
2129 if (sym._n._n_n._n_zeroes != 0
2130 || sym._n._n_n._n_offset == 0)
2133 /* The AIX linker appears to only detect multiple symbol
2134 definitions when there is a reference to the symbol. If
2135 a symbol is defined multiple times, and the only
2136 references are from the same object file, the AIX linker
2137 appears to permit it. It does not merge the different
2138 definitions, but handles them independently. On the
2139 other hand, if there is a reference, the linker reports
2142 This matters because the AIX <net/net_globals.h> header
2143 file actually defines an initialized array, so we have to
2144 actually permit that to work.
2146 We also have to handle the case of statically linking a
2147 shared object, which will cause symbol redefinitions,
2148 although this is an easier case to detect. */
2150 if (info->hash->creator == abfd->xvec)
2152 if (! bfd_is_und_section (section))
2153 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2154 name, true, copy, false);
2156 *sym_hash = ((struct xcoff_link_hash_entry *)
2157 bfd_wrapped_link_hash_lookup (abfd, info, name,
2158 true, copy, false));
2159 if (*sym_hash == NULL)
2161 if (((*sym_hash)->root.type == bfd_link_hash_defined
2162 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2163 && ! bfd_is_und_section (section)
2164 && ! bfd_is_com_section (section))
2166 /* This is a second definition of a defined symbol. */
2167 if ((abfd->flags & DYNAMIC) != 0
2168 && ((*sym_hash)->smclas != XMC_GL
2169 || aux.x_csect.x_smclas == XMC_GL
2170 || ((*sym_hash)->root.u.def.section->owner->flags
2173 /* The new symbol is from a shared library, and
2174 either the existing symbol is not global
2175 linkage code or this symbol is global linkage
2176 code. If the existing symbol is global
2177 linkage code and the new symbol is not, then
2178 we want to use the new symbol. */
2179 section = bfd_und_section_ptr;
2182 else if (((*sym_hash)->root.u.def.section->owner->flags
2185 /* The existing symbol is from a shared library.
2187 (*sym_hash)->root.type = bfd_link_hash_undefined;
2188 (*sym_hash)->root.u.undef.abfd =
2189 (*sym_hash)->root.u.def.section->owner;
2191 else if ((*sym_hash)->root.next != NULL
2192 || info->hash->undefs_tail == &(*sym_hash)->root)
2194 /* This symbol has been referenced. In this
2195 case, we just continue and permit the
2196 multiple definition error. See the comment
2197 above about the behaviour of the AIX linker. */
2199 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2201 /* The symbols are both csects of the same
2202 class. There is at least a chance that this
2203 is a semi-legitimate redefinition. */
2204 section = bfd_und_section_ptr;
2206 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2209 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2210 && ((*sym_hash)->root.type == bfd_link_hash_defined
2211 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2212 && (bfd_is_und_section (section)
2213 || bfd_is_com_section (section)))
2215 /* This is a reference to a multiply defined symbol.
2216 Report the error now. See the comment above
2217 about the behaviour of the AIX linker. We could
2218 also do this with warning symbols, but I'm not
2219 sure the XCOFF linker is wholly prepared to
2220 handle them, and that would only be a warning,
2222 if (! ((*info->callbacks->multiple_definition)
2223 (info, (*sym_hash)->root.root.string,
2224 (bfd *) NULL, (asection *) NULL, 0,
2225 (*sym_hash)->root.u.def.section->owner,
2226 (*sym_hash)->root.u.def.section,
2227 (*sym_hash)->root.u.def.value)))
2229 /* Try not to give this error too many times. */
2230 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2234 /* _bfd_generic_link_add_one_symbol may call the linker to
2235 generate an error message, and the linker may try to read
2236 the symbol table to give a good error. Right now, the
2237 line numbers are in an inconsistent state, since they are
2238 counted both in the real sections and in the new csects.
2239 We need to leave the count in the real sections so that
2240 the linker can report the line number of the error
2241 correctly, so temporarily clobber the link to the csects
2242 so that the linker will not try to read the line numbers
2243 a second time from the csects. */
2244 BFD_ASSERT (last_real->next == first_csect);
2245 last_real->next = NULL;
2246 if (! (_bfd_generic_link_add_one_symbol
2247 (info, abfd, name, flags, section, value,
2248 (const char *) NULL, copy, true,
2249 (struct bfd_link_hash_entry **) sym_hash)))
2251 last_real->next = first_csect;
2253 if (smtyp == XTY_CM)
2255 if ((*sym_hash)->root.type != bfd_link_hash_common
2256 || (*sym_hash)->root.u.c.p->section != csect)
2258 /* We don't need the common csect we just created. */
2259 csect->_raw_size = 0;
2263 (*sym_hash)->root.u.c.p->alignment_power
2264 = csect->alignment_power;
2268 if (info->hash->creator == abfd->xvec)
2272 if (smtyp == XTY_ER || smtyp == XTY_CM)
2273 flag = XCOFF_REF_REGULAR;
2275 flag = XCOFF_DEF_REGULAR;
2276 (*sym_hash)->flags |= flag;
2278 if ((*sym_hash)->smclas == XMC_UA
2279 || flag == XCOFF_DEF_REGULAR)
2280 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2284 *csect_cache = csect;
2286 esym += (sym.n_numaux + 1) * symesz;
2287 sym_hash += sym.n_numaux + 1;
2288 csect_cache += sym.n_numaux + 1;
2291 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2293 /* Make sure that we have seen all the relocs. */
2294 for (o = abfd->sections; o != first_csect; o = o->next)
2296 /* Reset the section size and the line number count, since the
2297 data is now attached to the csects. Don't reset the size of
2298 the .debug section, since we need to read it below in
2299 bfd_xcoff_size_dynamic_sections. */
2300 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2302 o->lineno_count = 0;
2304 if ((o->flags & SEC_RELOC) != 0)
2307 struct internal_reloc *rel;
2308 asection **rel_csect;
2310 rel = reloc_info[o->target_index].relocs;
2311 rel_csect = reloc_info[o->target_index].csects;
2312 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2314 if (*rel_csect == NULL)
2316 (*_bfd_error_handler)
2317 ("%s: reloc %s:%d not in csect",
2318 bfd_get_filename (abfd), o->name, i);
2319 bfd_set_error (bfd_error_bad_value);
2323 /* We identify all symbols which are called, so that we
2324 can create glue code for calls to functions imported
2325 from dynamic objects. */
2326 if (info->hash->creator == abfd->xvec
2327 && *rel_csect != bfd_und_section_ptr
2328 && (rel->r_type == R_BR
2329 || rel->r_type == R_RBR)
2330 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2332 struct xcoff_link_hash_entry *h;
2334 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2335 h->flags |= XCOFF_CALLED;
2336 /* If the symbol name starts with a period, it is
2337 the code of a function. If the symbol is
2338 currently undefined, then add an undefined symbol
2339 for the function descriptor. This should do no
2340 harm, because any regular object that defines the
2341 function should also define the function
2342 descriptor. It helps, because it means that we
2343 will identify the function descriptor with a
2344 dynamic object if a dynamic object defines it. */
2345 if (h->root.root.string[0] == '.'
2346 && h->descriptor == NULL)
2348 struct xcoff_link_hash_entry *hds;
2350 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2351 h->root.root.string + 1,
2355 if (hds->root.type == bfd_link_hash_new)
2357 if (! (_bfd_generic_link_add_one_symbol
2358 (info, abfd, hds->root.root.string,
2359 (flagword) 0, bfd_und_section_ptr,
2360 (bfd_vma) 0, (const char *) NULL, false,
2362 (struct bfd_link_hash_entry **) &hds)))
2365 hds->flags |= XCOFF_DESCRIPTOR;
2366 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2367 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2368 hds->descriptor = h;
2369 h->descriptor = hds;
2374 free (reloc_info[o->target_index].csects);
2375 reloc_info[o->target_index].csects = NULL;
2377 /* Reset SEC_RELOC and the reloc_count, since the reloc
2378 information is now attached to the csects. */
2379 o->flags &=~ SEC_RELOC;
2382 /* If we are not keeping memory, free the reloc information. */
2383 if (! info->keep_memory
2384 && coff_section_data (abfd, o) != NULL
2385 && coff_section_data (abfd, o)->relocs != NULL
2386 && ! coff_section_data (abfd, o)->keep_relocs)
2388 free (coff_section_data (abfd, o)->relocs);
2389 coff_section_data (abfd, o)->relocs = NULL;
2393 /* Free up the line numbers. FIXME: We could cache these
2394 somewhere for the final link, to avoid reading them again. */
2395 if (reloc_info[o->target_index].linenos != NULL)
2397 free (reloc_info[o->target_index].linenos);
2398 reloc_info[o->target_index].linenos = NULL;
2404 obj_coff_keep_syms (abfd) = keep_syms;
2409 if (reloc_info != NULL)
2411 for (o = abfd->sections; o != NULL; o = o->next)
2413 if (reloc_info[o->target_index].csects != NULL)
2414 free (reloc_info[o->target_index].csects);
2415 if (reloc_info[o->target_index].linenos != NULL)
2416 free (reloc_info[o->target_index].linenos);
2420 obj_coff_keep_syms (abfd) = keep_syms;
2427 /* This function is used to add symbols from a dynamic object to the
2428 global symbol table. */
2431 xcoff_link_add_dynamic_symbols (abfd, info)
2433 struct bfd_link_info *info;
2437 struct internal_ldhdr ldhdr;
2438 const char *strings;
2439 struct external_ldsym *elsym, *elsymend;
2440 struct xcoff_import_file *n;
2445 struct xcoff_import_file **pp;
2447 /* We can only handle a dynamic object if we are generating an XCOFF
2449 if (info->hash->creator != abfd->xvec)
2451 (*_bfd_error_handler)
2452 ("%s: XCOFF shared object when not producing XCOFF output",
2453 bfd_get_filename (abfd));
2454 bfd_set_error (bfd_error_invalid_operation);
2458 /* The symbols we use from a dynamic object are not the symbols in
2459 the normal symbol table, but, rather, the symbols in the export
2460 table. If there is a global symbol in a dynamic object which is
2461 not in the export table, the loader will not be able to find it,
2462 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2463 libc.a has symbols in the export table which are not in the
2466 /* Read in the .loader section. FIXME: We should really use the
2467 o_snloader field in the a.out header, rather than grabbing the
2469 lsec = bfd_get_section_by_name (abfd, ".loader");
2472 (*_bfd_error_handler)
2473 ("%s: dynamic object with no .loader section",
2474 bfd_get_filename (abfd));
2475 bfd_set_error (bfd_error_no_symbols);
2479 if (! xcoff_get_section_contents (abfd, lsec))
2481 buf = coff_section_data (abfd, lsec)->contents;
2483 /* Remove the sections from this object, so that they do not get
2484 included in the link. */
2485 abfd->sections = NULL;
2487 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2489 strings = (char *) buf + ldhdr.l_stoff;
2491 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2492 elsymend = elsym + ldhdr.l_nsyms;
2493 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2494 for (; elsym < elsymend; elsym++)
2496 struct internal_ldsym ldsym;
2497 char nambuf[SYMNMLEN + 1];
2499 struct xcoff_link_hash_entry *h;
2501 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2503 /* We are only interested in exported symbols. */
2504 if ((ldsym.l_smtype & L_EXPORT) == 0)
2507 if (ldsym._l._l_l._l_zeroes == 0)
2508 name = strings + ldsym._l._l_l._l_offset;
2511 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2512 nambuf[SYMNMLEN] = '\0';
2516 /* Normally we could not call xcoff_link_hash_lookup in an add
2517 symbols routine, since we might not be using an XCOFF hash
2518 table. However, we verified above that we are using an XCOFF
2521 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2526 h->flags |= XCOFF_DEF_DYNAMIC;
2528 /* If the symbol is undefined, and the BFD it was found in is
2529 not a dynamic object, change the BFD to this dynamic object,
2530 so that we can get the correct import file ID. */
2531 if ((h->root.type == bfd_link_hash_undefined
2532 || h->root.type == bfd_link_hash_undefweak)
2533 && (h->root.u.undef.abfd == NULL
2534 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2535 h->root.u.undef.abfd = abfd;
2537 if (h->root.type == bfd_link_hash_new)
2539 h->root.type = bfd_link_hash_undefined;
2540 h->root.u.undef.abfd = abfd;
2541 /* We do not want to add this to the undefined symbol list. */
2544 if (h->smclas == XMC_UA
2545 || h->root.type == bfd_link_hash_undefined
2546 || h->root.type == bfd_link_hash_undefweak)
2547 h->smclas = ldsym.l_smclas;
2549 /* Unless this is an XMC_XO symbol, we don't bother to actually
2550 define it, since we don't have a section to put it in anyhow.
2551 Instead, the relocation routines handle the DEF_DYNAMIC flag
2554 if (h->smclas == XMC_XO
2555 && (h->root.type == bfd_link_hash_undefined
2556 || h->root.type == bfd_link_hash_undefweak))
2558 /* This symbol has an absolute value. */
2559 h->root.type = bfd_link_hash_defined;
2560 h->root.u.def.section = bfd_abs_section_ptr;
2561 h->root.u.def.value = ldsym.l_value;
2565 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2567 free (coff_section_data (abfd, lsec)->contents);
2568 coff_section_data (abfd, lsec)->contents = NULL;
2571 /* Record this file in the import files. */
2573 n = ((struct xcoff_import_file *)
2574 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2579 /* For some reason, the path entry in the import file list for a
2580 shared object appears to always be empty. The file name is the
2583 if (abfd->my_archive == NULL)
2585 bname = bfd_get_filename (abfd);
2590 bname = bfd_get_filename (abfd->my_archive);
2591 mname = bfd_get_filename (abfd);
2593 s = strrchr (bname, '/');
2599 /* We start c at 1 because the first import file number is reserved
2601 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2603 pp = &(*pp)->next, ++c)
2607 xcoff_data (abfd)->import_file_id = c;
2612 /* Routines that are called after all the input files have been
2613 handled, but before the sections are laid out in memory. */
2615 /* Mark a symbol as not being garbage, including the section in which
2618 static INLINE boolean
2619 xcoff_mark_symbol (info, h)
2620 struct bfd_link_info *info;
2621 struct xcoff_link_hash_entry *h;
2623 if ((h->flags & XCOFF_MARK) != 0)
2626 h->flags |= XCOFF_MARK;
2627 if (h->root.type == bfd_link_hash_defined
2628 || h->root.type == bfd_link_hash_defweak)
2632 hsec = h->root.u.def.section;
2633 if ((hsec->flags & SEC_MARK) == 0)
2635 if (! xcoff_mark (info, hsec))
2640 if (h->toc_section != NULL
2641 && (h->toc_section->flags & SEC_MARK) == 0)
2643 if (! xcoff_mark (info, h->toc_section))
2650 /* The mark phase of garbage collection. For a given section, mark
2651 it, and all the sections which define symbols to which it refers.
2652 Because this function needs to look at the relocs, we also count
2653 the number of relocs which need to be copied into the .loader
2657 xcoff_mark (info, sec)
2658 struct bfd_link_info *info;
2661 if ((sec->flags & SEC_MARK) != 0)
2664 sec->flags |= SEC_MARK;
2666 if (sec->owner->xvec == info->hash->creator
2667 && coff_section_data (sec->owner, sec) != NULL
2668 && xcoff_section_data (sec->owner, sec) != NULL)
2670 register struct xcoff_link_hash_entry **hp, **hpend;
2671 struct internal_reloc *rel, *relend;
2673 /* Mark all the symbols in this section. */
2675 hp = (obj_xcoff_sym_hashes (sec->owner)
2676 + xcoff_section_data (sec->owner, sec)->first_symndx);
2677 hpend = (obj_xcoff_sym_hashes (sec->owner)
2678 + xcoff_section_data (sec->owner, sec)->last_symndx);
2679 for (; hp < hpend; hp++)
2681 register struct xcoff_link_hash_entry *h;
2685 && (h->flags & XCOFF_MARK) == 0)
2687 if (! xcoff_mark_symbol (info, h))
2692 /* Look through the section relocs. */
2694 if ((sec->flags & SEC_RELOC) != 0
2695 && sec->reloc_count > 0)
2697 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2698 (bfd_byte *) NULL, false,
2699 (struct internal_reloc *) NULL);
2702 relend = rel + sec->reloc_count;
2703 for (; rel < relend; rel++)
2706 struct xcoff_link_hash_entry *h;
2708 if ((unsigned int) rel->r_symndx
2709 > obj_raw_syment_count (sec->owner))
2712 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2714 && (h->flags & XCOFF_MARK) == 0)
2716 if (! xcoff_mark_symbol (info, h))
2720 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2722 && (rsec->flags & SEC_MARK) == 0)
2724 if (! xcoff_mark (info, rsec))
2728 /* See if this reloc needs to be copied into the .loader
2730 switch (rel->r_type)
2734 || h->root.type == bfd_link_hash_defined
2735 || h->root.type == bfd_link_hash_defweak
2736 || h->root.type == bfd_link_hash_common
2737 || ((h->flags & XCOFF_CALLED) != 0
2738 && (h->root.type == bfd_link_hash_undefined
2739 || h->root.type == bfd_link_hash_undefweak)
2740 && h->root.root.string[0] == '.'
2741 && h->descriptor != NULL
2742 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2744 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2745 && (h->descriptor->flags
2746 & XCOFF_DEF_REGULAR) == 0))))
2753 ++xcoff_hash_table (info)->ldrel_count;
2755 h->flags |= XCOFF_LDREL;
2762 /* We should never need a .loader reloc for a TOC
2768 if (! info->keep_memory
2769 && coff_section_data (sec->owner, sec) != NULL
2770 && coff_section_data (sec->owner, sec)->relocs != NULL
2771 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2773 free (coff_section_data (sec->owner, sec)->relocs);
2774 coff_section_data (sec->owner, sec)->relocs = NULL;
2782 /* The sweep phase of garbage collection. Remove all garbage
2787 struct bfd_link_info *info;
2791 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2795 for (o = sub->sections; o != NULL; o = o->next)
2797 if ((o->flags & SEC_MARK) == 0)
2799 /* Keep all sections from non-XCOFF input files. Keep
2800 special sections. Keep .debug sections for the
2802 if (sub->xvec != info->hash->creator
2803 || o == xcoff_hash_table (info)->debug_section
2804 || o == xcoff_hash_table (info)->loader_section
2805 || o == xcoff_hash_table (info)->linkage_section
2806 || o == xcoff_hash_table (info)->toc_section
2807 || o == xcoff_hash_table (info)->descriptor_section
2808 || strcmp (o->name, ".debug") == 0)
2809 o->flags |= SEC_MARK;
2814 o->lineno_count = 0;
2821 /* Record the number of elements in a set. This is used to output the
2822 correct csect length. */
2825 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2827 struct bfd_link_info *info;
2828 struct bfd_link_hash_entry *harg;
2831 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2832 struct xcoff_link_size_list *n;
2834 if (! XCOFF_XVECP (output_bfd->xvec))
2837 /* This will hardly ever be called. I don't want to burn four bytes
2838 per global symbol, so instead the size is kept on a linked list
2839 attached to the hash table. */
2841 n = ((struct xcoff_link_size_list *)
2842 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2845 n->next = xcoff_hash_table (info)->size_list;
2848 xcoff_hash_table (info)->size_list = n;
2850 h->flags |= XCOFF_HAS_SIZE;
2855 /* Import a symbol. */
2858 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2861 struct bfd_link_info *info;
2862 struct bfd_link_hash_entry *harg;
2864 const char *imppath;
2865 const char *impfile;
2866 const char *impmember;
2868 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2870 if (! XCOFF_XVECP (output_bfd->xvec))
2873 h->flags |= XCOFF_IMPORT;
2875 if (val != (bfd_vma) -1)
2877 if (h->root.type == bfd_link_hash_defined
2878 && (! bfd_is_abs_section (h->root.u.def.section)
2879 || h->root.u.def.value != val))
2881 if (! ((*info->callbacks->multiple_definition)
2882 (info, h->root.root.string, h->root.u.def.section->owner,
2883 h->root.u.def.section, h->root.u.def.value,
2884 output_bfd, bfd_abs_section_ptr, val)))
2888 h->root.type = bfd_link_hash_defined;
2889 h->root.u.def.section = bfd_abs_section_ptr;
2890 h->root.u.def.value = val;
2893 if (h->ldsym == NULL)
2895 h->ldsym = ((struct internal_ldsym *)
2896 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2897 if (h->ldsym == NULL)
2901 if (imppath == NULL)
2902 h->ldsym->l_ifile = (bfd_size_type) -1;
2906 struct xcoff_import_file **pp;
2908 /* We start c at 1 because the first entry in the import list is
2909 reserved for the library search path. */
2910 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2912 pp = &(*pp)->next, ++c)
2914 if (strcmp ((*pp)->path, imppath) == 0
2915 && strcmp ((*pp)->file, impfile) == 0
2916 && strcmp ((*pp)->member, impmember) == 0)
2922 struct xcoff_import_file *n;
2924 n = ((struct xcoff_import_file *)
2925 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2931 n->member = impmember;
2935 h->ldsym->l_ifile = c;
2941 /* Export a symbol. */
2944 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2946 struct bfd_link_info *info;
2947 struct bfd_link_hash_entry *harg;
2950 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2952 if (! XCOFF_XVECP (output_bfd->xvec))
2955 h->flags |= XCOFF_EXPORT;
2957 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2958 I'm just going to ignore it until somebody explains it. */
2960 /* See if this is a function descriptor. It may be one even though
2961 it is not so marked. */
2962 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2963 && h->root.root.string[0] != '.')
2966 struct xcoff_link_hash_entry *hfn;
2968 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
2972 strcpy (fnname + 1, h->root.root.string);
2973 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2974 fnname, false, false, true);
2977 && hfn->smclas == XMC_PR
2978 && (hfn->root.type == bfd_link_hash_defined
2979 || hfn->root.type == bfd_link_hash_defweak))
2981 h->flags |= XCOFF_DESCRIPTOR;
2982 h->descriptor = hfn;
2983 hfn->descriptor = h;
2987 /* Make sure we don't garbage collect this symbol. */
2988 if (! xcoff_mark_symbol (info, h))
2991 /* If this is a function descriptor, make sure we don't garbage
2992 collect the associated function code. We normally don't have to
2993 worry about this, because the descriptor will be attached to a
2994 section with relocs, but if we are creating the descriptor
2995 ourselves those relocs will not be visible to the mark code. */
2996 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2998 if (! xcoff_mark_symbol (info, h->descriptor))
3005 /* Count a reloc against a symbol. This is called for relocs
3006 generated by the linker script, typically for global constructors
3010 bfd_xcoff_link_count_reloc (output_bfd, info, name)
3012 struct bfd_link_info *info;
3015 struct xcoff_link_hash_entry *h;
3017 if (! XCOFF_XVECP (output_bfd->xvec))
3020 h = ((struct xcoff_link_hash_entry *)
3021 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3025 (*_bfd_error_handler) ("%s: no such symbol", name);
3026 bfd_set_error (bfd_error_no_symbols);
3030 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3031 ++xcoff_hash_table (info)->ldrel_count;
3033 /* Mark the symbol to avoid garbage collection. */
3034 if (! xcoff_mark_symbol (info, h))
3040 /* This function is called for each symbol to which the linker script
3044 bfd_xcoff_record_link_assignment (output_bfd, info, name)
3046 struct bfd_link_info *info;
3049 struct xcoff_link_hash_entry *h;
3051 if (! XCOFF_XVECP (output_bfd->xvec))
3054 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3059 h->flags |= XCOFF_DEF_REGULAR;
3064 /* This structure is used to pass information through
3065 xcoff_link_hash_traverse. */
3067 struct xcoff_loader_info
3069 /* Set if a problem occurred. */
3073 /* Link information structure. */
3074 struct bfd_link_info *info;
3075 /* Whether all defined symbols should be exported. */
3076 boolean export_defineds;
3077 /* Number of ldsym structures. */
3079 /* Size of string table. */
3083 /* Allocated size of string table. */
3087 /* Build the .loader section. This is called by the XCOFF linker
3088 emulation before_allocation routine. We must set the size of the
3089 .loader section before the linker lays out the output file.
3090 LIBPATH is the library path to search for shared objects; this is
3091 normally built from the -L arguments passed to the linker. ENTRY
3092 is the name of the entry point symbol (the -e linker option).
3093 FILE_ALIGN is the alignment to use for sections within the file
3094 (the -H linker option). MAXSTACK is the maximum stack size (the
3095 -bmaxstack linker option). MAXDATA is the maximum data size (the
3096 -bmaxdata linker option). GC is whether to do garbage collection
3097 (the -bgc linker option). MODTYPE is the module type (the
3098 -bmodtype linker option). TEXTRO is whether the text section must
3099 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3100 whether all defined symbols should be exported (the -unix linker
3101 option). SPECIAL_SECTIONS is set by this routine to csects with
3102 magic names like _end. */
3105 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3106 file_align, maxstack, maxdata, gc,
3107 modtype, textro, export_defineds,
3110 struct bfd_link_info *info;
3111 const char *libpath;
3113 unsigned long file_align;
3114 unsigned long maxstack;
3115 unsigned long maxdata;
3119 boolean export_defineds;
3120 asection **special_sections;
3122 struct xcoff_link_hash_entry *hentry;
3124 struct xcoff_loader_info ldinfo;
3126 size_t impsize, impcount;
3127 struct xcoff_import_file *fl;
3128 struct internal_ldhdr *ldhdr;
3129 bfd_size_type stoff;
3133 struct bfd_strtab_hash *debug_strtab;
3134 bfd_byte *debug_contents = NULL;
3136 if (! XCOFF_XVECP (output_bfd->xvec))
3138 for (i = 0; i < 6; i++)
3139 special_sections[i] = NULL;
3143 ldinfo.failed = false;
3144 ldinfo.output_bfd = output_bfd;
3146 ldinfo.export_defineds = export_defineds;
3147 ldinfo.ldsym_count = 0;
3148 ldinfo.string_size = 0;
3149 ldinfo.strings = NULL;
3150 ldinfo.string_alc = 0;
3152 xcoff_data (output_bfd)->maxstack = maxstack;
3153 xcoff_data (output_bfd)->maxdata = maxdata;
3154 xcoff_data (output_bfd)->modtype = modtype;
3156 xcoff_hash_table (info)->file_align = file_align;
3157 xcoff_hash_table (info)->textro = textro;
3159 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3160 false, false, true);
3162 hentry->flags |= XCOFF_ENTRY;
3164 /* Garbage collect unused sections. */
3165 if (info->relocateable
3168 || (hentry->root.type != bfd_link_hash_defined
3169 && hentry->root.type != bfd_link_hash_defweak))
3172 xcoff_hash_table (info)->gc = false;
3174 /* We still need to call xcoff_mark, in order to set ldrel_count
3176 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3180 for (o = sub->sections; o != NULL; o = o->next)
3182 if ((o->flags & SEC_MARK) == 0)
3184 if (! xcoff_mark (info, o))
3192 if (! xcoff_mark (info, hentry->root.u.def.section))
3195 xcoff_hash_table (info)->gc = true;
3198 /* Return special sections to the caller. */
3199 for (i = 0; i < 6; i++)
3203 sec = xcoff_hash_table (info)->special_sections[i];
3206 && (sec->flags & SEC_MARK) == 0)
3208 special_sections[i] = sec;
3211 if (info->input_bfds == NULL)
3213 /* I'm not sure what to do in this bizarre case. */
3217 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3222 /* Work out the size of the import file names. Each import file ID
3223 consists of three null terminated strings: the path, the file
3224 name, and the archive member name. The first entry in the list
3225 of names is the path to use to find objects, which the linker has
3226 passed in as the libpath argument. For some reason, the path
3227 entry in the other import file names appears to always be empty. */
3228 impsize = strlen (libpath) + 3;
3230 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3233 impsize += (strlen (fl->path)
3235 + strlen (fl->member)
3239 /* Set up the .loader section header. */
3240 ldhdr = &xcoff_hash_table (info)->ldhdr;
3241 ldhdr->l_version = 1;
3242 ldhdr->l_nsyms = ldinfo.ldsym_count;
3243 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3244 ldhdr->l_istlen = impsize;
3245 ldhdr->l_nimpid = impcount;
3246 ldhdr->l_impoff = (LDHDRSZ
3247 + ldhdr->l_nsyms * LDSYMSZ
3248 + ldhdr->l_nreloc * LDRELSZ);
3249 ldhdr->l_stlen = ldinfo.string_size;
3250 stoff = ldhdr->l_impoff + impsize;
3251 if (ldinfo.string_size == 0)
3254 ldhdr->l_stoff = stoff;
3256 /* We now know the final size of the .loader section. Allocate
3258 lsec = xcoff_hash_table (info)->loader_section;
3259 lsec->_raw_size = stoff + ldhdr->l_stlen;
3260 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3261 if (lsec->contents == NULL)
3264 /* Set up the header. */
3265 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3266 (struct external_ldhdr *) lsec->contents);
3268 /* Set up the import file names. */
3269 out = (char *) lsec->contents + ldhdr->l_impoff;
3270 strcpy (out, libpath);
3271 out += strlen (libpath) + 1;
3274 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3276 register const char *s;
3279 while ((*out++ = *s++) != '\0')
3282 while ((*out++ = *s++) != '\0')
3285 while ((*out++ = *s++) != '\0')
3289 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3291 /* Set up the symbol string table. */
3292 if (ldinfo.string_size > 0)
3294 memcpy (out, ldinfo.strings, ldinfo.string_size);
3295 free (ldinfo.strings);
3296 ldinfo.strings = NULL;
3299 /* We can't set up the symbol table or the relocs yet, because we
3300 don't yet know the final position of the various sections. The
3301 .loader symbols are written out when the corresponding normal
3302 symbols are written out in xcoff_link_input_bfd or
3303 xcoff_write_global_symbol. The .loader relocs are written out
3304 when the corresponding normal relocs are handled in
3305 xcoff_link_input_bfd. */
3307 /* Allocate space for the magic sections. */
3308 sec = xcoff_hash_table (info)->linkage_section;
3309 if (sec->_raw_size > 0)
3311 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3312 if (sec->contents == NULL)
3315 sec = xcoff_hash_table (info)->toc_section;
3316 if (sec->_raw_size > 0)
3318 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3319 if (sec->contents == NULL)
3322 sec = xcoff_hash_table (info)->descriptor_section;
3323 if (sec->_raw_size > 0)
3325 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3326 if (sec->contents == NULL)
3330 /* Now that we've done garbage collection, figure out the contents
3331 of the .debug section. */
3332 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3334 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3337 bfd_size_type symcount;
3338 unsigned long *debug_index;
3340 bfd_byte *esym, *esymend;
3341 bfd_size_type symesz;
3343 if (sub->xvec != info->hash->creator)
3345 subdeb = bfd_get_section_by_name (sub, ".debug");
3346 if (subdeb == NULL || subdeb->_raw_size == 0)
3349 if (info->strip == strip_all
3350 || info->strip == strip_debugger
3351 || info->discard == discard_all)
3353 subdeb->_raw_size = 0;
3357 if (! _bfd_coff_get_external_symbols (sub))
3360 symcount = obj_raw_syment_count (sub);
3361 debug_index = ((unsigned long *)
3362 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3363 if (debug_index == NULL)
3365 xcoff_data (sub)->debug_indices = debug_index;
3367 /* Grab the contents of the .debug section. We use malloc and
3368 copy the neams into the debug stringtab, rather than
3369 bfd_alloc, because I expect that, when linking many files
3370 together, many of the strings will be the same. Storing the
3371 strings in the hash table should save space in this case. */
3372 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3373 if (debug_contents == NULL)
3375 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3376 (file_ptr) 0, subdeb->_raw_size))
3379 csectpp = xcoff_data (sub)->csects;
3381 symesz = bfd_coff_symesz (sub);
3382 esym = (bfd_byte *) obj_coff_external_syms (sub);
3383 esymend = esym + symcount * symesz;
3384 while (esym < esymend)
3386 struct internal_syment sym;
3388 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3390 *debug_index = (unsigned long) -1;
3392 if (sym._n._n_n._n_zeroes == 0
3395 || ((*csectpp)->flags & SEC_MARK) != 0
3396 || *csectpp == bfd_abs_section_ptr)
3397 && bfd_coff_symname_in_debug (sub, &sym))
3402 name = (char *) debug_contents + sym._n._n_n._n_offset;
3403 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3404 if (indx == (bfd_size_type) -1)
3406 *debug_index = indx;
3409 esym += (sym.n_numaux + 1) * symesz;
3410 csectpp += sym.n_numaux + 1;
3411 debug_index += sym.n_numaux + 1;
3414 free (debug_contents);
3415 debug_contents = NULL;
3417 /* Clear the size of subdeb, so that it is not included directly
3418 in the output file. */
3419 subdeb->_raw_size = 0;
3421 if (! info->keep_memory)
3423 if (! _bfd_coff_free_symbols (sub))
3428 xcoff_hash_table (info)->debug_section->_raw_size =
3429 _bfd_stringtab_size (debug_strtab);
3434 if (ldinfo.strings != NULL)
3435 free (ldinfo.strings);
3436 if (debug_contents != NULL)
3437 free (debug_contents);
3441 /* Add a symbol to the .loader symbols, if necessary. */
3444 xcoff_build_ldsyms (h, p)
3445 struct xcoff_link_hash_entry *h;
3448 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3451 /* If this is a final link, and the symbol was defined as a common
3452 symbol in a regular object file, and there was no definition in
3453 any dynamic object, then the linker will have allocated space for
3454 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3455 will not have been set. */
3456 if (h->root.type == bfd_link_hash_defined
3457 && (h->flags & XCOFF_DEF_REGULAR) == 0
3458 && (h->flags & XCOFF_REF_REGULAR) != 0
3459 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3460 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3461 h->flags |= XCOFF_DEF_REGULAR;
3463 /* If all defined symbols should be exported, mark them now. We
3464 don't want to export the actual functions, just the function
3466 if (ldinfo->export_defineds
3467 && (h->flags & XCOFF_DEF_REGULAR) != 0
3468 && h->root.root.string[0] != '.')
3469 h->flags |= XCOFF_EXPORT;
3471 /* We don't want to garbage collect symbols which are not defined in
3472 XCOFF files. This is a convenient place to mark them. */
3473 if (xcoff_hash_table (ldinfo->info)->gc
3474 && (h->flags & XCOFF_MARK) == 0
3475 && (h->root.type == bfd_link_hash_defined
3476 || h->root.type == bfd_link_hash_defweak)
3477 && (h->root.u.def.section->owner == NULL
3478 || (h->root.u.def.section->owner->xvec
3479 != ldinfo->info->hash->creator)))
3480 h->flags |= XCOFF_MARK;
3482 /* If this symbol is called and defined in a dynamic object, or not
3483 defined at all when building a shared object, or imported, then
3484 we need to set up global linkage code for it. (Unless we did
3485 garbage collection and we didn't need this symbol.) */
3486 if ((h->flags & XCOFF_CALLED) != 0
3487 && (h->root.type == bfd_link_hash_undefined
3488 || h->root.type == bfd_link_hash_undefweak)
3489 && h->root.root.string[0] == '.'
3490 && h->descriptor != NULL
3491 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3492 || ldinfo->info->shared
3493 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3494 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3495 && (! xcoff_hash_table (ldinfo->info)->gc
3496 || (h->flags & XCOFF_MARK) != 0))
3499 struct xcoff_link_hash_entry *hds;
3501 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3502 h->root.type = bfd_link_hash_defined;
3503 h->root.u.def.section = sec;
3504 h->root.u.def.value = sec->_raw_size;
3506 h->flags |= XCOFF_DEF_REGULAR;
3507 sec->_raw_size += XCOFF_GLINK_SIZE;
3509 /* The global linkage code requires a TOC entry for the
3511 hds = h->descriptor;
3512 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3513 || hds->root.type == bfd_link_hash_undefweak)
3514 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3515 hds->flags |= XCOFF_MARK;
3516 if (hds->toc_section == NULL)
3518 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3519 hds->u.toc_offset = hds->toc_section->_raw_size;
3520 hds->toc_section->_raw_size += 4;
3521 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3522 ++hds->toc_section->reloc_count;
3524 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3526 /* We need to call xcoff_build_ldsyms recursively here,
3527 because we may already have passed hds on the traversal. */
3528 xcoff_build_ldsyms (hds, p);
3532 /* If this symbol is exported, but not defined, we need to try to
3534 if ((h->flags & XCOFF_EXPORT) != 0
3535 && (h->flags & XCOFF_IMPORT) == 0
3536 && (h->flags & XCOFF_DEF_REGULAR) == 0
3537 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3538 && (h->root.type == bfd_link_hash_undefined
3539 || h->root.type == bfd_link_hash_undefweak))
3541 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3542 && (h->descriptor->root.type == bfd_link_hash_defined
3543 || h->descriptor->root.type == bfd_link_hash_defweak))
3547 /* This is an undefined function descriptor associated with
3548 a defined entry point. We can build up a function
3549 descriptor ourselves. Believe it or not, the AIX linker
3550 actually does this, and there are cases where we need to
3552 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3553 h->root.type = bfd_link_hash_defined;
3554 h->root.u.def.section = sec;
3555 h->root.u.def.value = sec->_raw_size;
3557 h->flags |= XCOFF_DEF_REGULAR;
3558 sec->_raw_size += 12;
3560 /* A function descriptor uses two relocs: one for the
3561 associated code, and one for the TOC address. */
3562 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3563 sec->reloc_count += 2;
3565 /* We handle writing out the contents of the descriptor in
3566 xcoff_write_global_symbol. */
3570 (*_bfd_error_handler)
3571 ("warning: attempt to export undefined symbol `%s'",
3572 h->root.root.string);
3578 /* If this is still a common symbol, and it wasn't garbage
3579 collected, we need to actually allocate space for it in the .bss
3581 if (h->root.type == bfd_link_hash_common
3582 && (! xcoff_hash_table (ldinfo->info)->gc
3583 || (h->flags & XCOFF_MARK) != 0)
3584 && h->root.u.c.p->section->_raw_size == 0)
3586 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3587 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3590 /* We need to add a symbol to the .loader section if it is mentioned
3591 in a reloc which we are copying to the .loader section and it was
3592 not defined or common, or if it is the entry point, or if it is
3595 if (((h->flags & XCOFF_LDREL) == 0
3596 || h->root.type == bfd_link_hash_defined
3597 || h->root.type == bfd_link_hash_defweak
3598 || h->root.type == bfd_link_hash_common)
3599 && (h->flags & XCOFF_ENTRY) == 0
3600 && (h->flags & XCOFF_EXPORT) == 0)
3606 /* We don't need to add this symbol if we did garbage collection and
3607 we did not mark this symbol. */
3608 if (xcoff_hash_table (ldinfo->info)->gc
3609 && (h->flags & XCOFF_MARK) == 0)
3615 /* We may have already processed this symbol due to the recursive
3617 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3620 /* We need to add this symbol to the .loader symbols. */
3622 /* h->ldsym will already have been allocated for an explicitly
3624 if (h->ldsym == NULL)
3626 h->ldsym = ((struct internal_ldsym *)
3627 bfd_zalloc (ldinfo->output_bfd,
3628 sizeof (struct internal_ldsym)));
3629 if (h->ldsym == NULL)
3631 ldinfo->failed = true;
3636 /* The first 3 symbol table indices are reserved to indicate the
3638 h->ldindx = ldinfo->ldsym_count + 3;
3640 ++ldinfo->ldsym_count;
3642 len = strlen (h->root.root.string);
3643 if (len <= SYMNMLEN)
3644 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3647 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3650 bfd_byte *newstrings;
3652 newalc = ldinfo->string_alc * 2;
3655 while (ldinfo->string_size + len + 3 > newalc)
3658 newstrings = ((bfd_byte *)
3659 bfd_realloc ((PTR) ldinfo->strings, newalc));
3660 if (newstrings == NULL)
3662 ldinfo->failed = true;
3665 ldinfo->string_alc = newalc;
3666 ldinfo->strings = newstrings;
3669 bfd_put_16 (ldinfo->output_bfd, len + 1,
3670 ldinfo->strings + ldinfo->string_size);
3671 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3672 h->ldsym->_l._l_l._l_zeroes = 0;
3673 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3674 ldinfo->string_size += len + 3;
3677 h->flags |= XCOFF_BUILT_LDSYM;
3682 /* Do the final link step. */
3685 _bfd_xcoff_bfd_final_link (abfd, info)
3687 struct bfd_link_info *info;
3689 bfd_size_type symesz;
3690 struct xcoff_final_link_info finfo;
3692 struct bfd_link_order *p;
3693 size_t max_contents_size;
3694 size_t max_sym_count;
3695 size_t max_lineno_count;
3696 size_t max_reloc_count;
3697 size_t max_output_reloc_count;
3698 file_ptr rel_filepos;
3700 file_ptr line_filepos;
3701 unsigned int linesz;
3703 bfd_byte *external_relocs = NULL;
3704 char strbuf[STRING_SIZE_SIZE];
3707 abfd->flags |= DYNAMIC;
3709 symesz = bfd_coff_symesz (abfd);
3712 finfo.output_bfd = abfd;
3713 finfo.strtab = NULL;
3714 finfo.section_info = NULL;
3715 finfo.last_file_index = -1;
3716 finfo.toc_symindx = -1;
3717 finfo.internal_syms = NULL;
3718 finfo.sym_indices = NULL;
3719 finfo.outsyms = NULL;
3720 finfo.linenos = NULL;
3721 finfo.contents = NULL;
3722 finfo.external_relocs = NULL;
3724 finfo.ldsym = ((struct external_ldsym *)
3725 (xcoff_hash_table (info)->loader_section->contents
3727 finfo.ldrel = ((struct external_ldrel *)
3728 (xcoff_hash_table (info)->loader_section->contents
3730 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3732 xcoff_data (abfd)->coff.link_info = info;
3734 finfo.strtab = _bfd_stringtab_init ();
3735 if (finfo.strtab == NULL)
3738 /* Count the line number and relocation entries required for the
3739 output file. Determine a few maximum sizes. */
3740 max_contents_size = 0;
3741 max_lineno_count = 0;
3742 max_reloc_count = 0;
3743 for (o = abfd->sections; o != NULL; o = o->next)
3746 o->lineno_count = 0;
3747 for (p = o->link_order_head; p != NULL; p = p->next)
3749 if (p->type == bfd_indirect_link_order)
3753 sec = p->u.indirect.section;
3755 /* Mark all sections which are to be included in the
3756 link. This will normally be every section. We need
3757 to do this so that we can identify any sections which
3758 the linker has decided to not include. */
3759 sec->linker_mark = true;
3761 if (info->strip == strip_none
3762 || info->strip == strip_some)
3763 o->lineno_count += sec->lineno_count;
3765 o->reloc_count += sec->reloc_count;
3767 if (sec->_raw_size > max_contents_size)
3768 max_contents_size = sec->_raw_size;
3769 if (sec->lineno_count > max_lineno_count)
3770 max_lineno_count = sec->lineno_count;
3771 if (coff_section_data (sec->owner, sec) != NULL
3772 && xcoff_section_data (sec->owner, sec) != NULL
3773 && (xcoff_section_data (sec->owner, sec)->lineno_count
3774 > max_lineno_count))
3776 xcoff_section_data (sec->owner, sec)->lineno_count;
3777 if (sec->reloc_count > max_reloc_count)
3778 max_reloc_count = sec->reloc_count;
3780 else if (p->type == bfd_section_reloc_link_order
3781 || p->type == bfd_symbol_reloc_link_order)
3786 /* Compute the file positions for all the sections. */
3787 if (abfd->output_has_begun)
3789 if (xcoff_hash_table (info)->file_align != 0)
3796 file_align = xcoff_hash_table (info)->file_align;
3797 if (file_align != 0)
3799 boolean saw_contents;
3804 /* Insert .pad sections before every section which has
3805 contents and is loaded, if it is preceded by some other
3806 section which has contents and is loaded. */
3807 saw_contents = true;
3808 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3810 (*op)->target_index = indx;
3811 if (strcmp ((*op)->name, ".pad") == 0)
3812 saw_contents = false;
3813 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3814 && ((*op)->flags & SEC_LOAD) != 0)
3817 saw_contents = true;
3824 n = bfd_make_section_anyway (abfd, ".pad");
3825 BFD_ASSERT (*op == n);
3827 n->flags = SEC_HAS_CONTENTS;
3828 n->alignment_power = 0;
3829 saw_contents = false;
3834 /* Reset the section indices after inserting the new
3837 for (o = abfd->sections; o != NULL; o = o->next)
3840 o->target_index = indx;
3842 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3844 /* Work out appropriate sizes for the .pad sections to force
3845 each section to land on a page boundary. This bit of
3846 code knows what compute_section_file_positions is going
3848 sofar = bfd_coff_filhsz (abfd);
3849 sofar += bfd_coff_aoutsz (abfd);
3850 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3851 for (o = abfd->sections; o != NULL; o = o->next)
3852 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
3853 sofar += bfd_coff_scnhsz (abfd);
3855 for (o = abfd->sections; o != NULL; o = o->next)
3857 if (strcmp (o->name, ".pad") == 0)
3861 BFD_ASSERT (o->_raw_size == 0);
3862 pageoff = sofar & (file_align - 1);
3865 o->_raw_size = file_align - pageoff;
3866 sofar += file_align - pageoff;
3867 o->flags |= SEC_HAS_CONTENTS;
3872 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3873 sofar += BFD_ALIGN (o->_raw_size,
3874 1 << o->alignment_power);
3879 bfd_coff_compute_section_file_positions (abfd);
3882 /* Allocate space for the pointers we need to keep for the relocs. */
3886 /* We use section_count + 1, rather than section_count, because
3887 the target_index fields are 1 based. */
3888 finfo.section_info =
3889 ((struct xcoff_link_section_info *)
3890 bfd_malloc ((abfd->section_count + 1)
3891 * sizeof (struct xcoff_link_section_info)));
3892 if (finfo.section_info == NULL)
3894 for (i = 0; i <= abfd->section_count; i++)
3896 finfo.section_info[i].relocs = NULL;
3897 finfo.section_info[i].rel_hashes = NULL;
3898 finfo.section_info[i].toc_rel_hashes = NULL;
3902 /* Set the file positions for the relocs. */
3903 rel_filepos = obj_relocbase (abfd);
3904 relsz = bfd_coff_relsz (abfd);
3905 max_output_reloc_count = 0;
3906 for (o = abfd->sections; o != NULL; o = o->next)
3908 if (o->reloc_count == 0)
3912 o->flags |= SEC_RELOC;
3913 o->rel_filepos = rel_filepos;
3914 rel_filepos += o->reloc_count * relsz;
3916 /* We don't know the indices of global symbols until we have
3917 written out all the local symbols. For each section in
3918 the output file, we keep an array of pointers to hash
3919 table entries. Each entry in the array corresponds to a
3920 reloc. When we find a reloc against a global symbol, we
3921 set the corresponding entry in this array so that we can
3922 fix up the symbol index after we have written out all the
3925 Because of this problem, we also keep the relocs in
3926 memory until the end of the link. This wastes memory.
3927 We could backpatch the file later, I suppose, although it
3929 finfo.section_info[o->target_index].relocs =
3930 ((struct internal_reloc *)
3931 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
3932 finfo.section_info[o->target_index].rel_hashes =
3933 ((struct xcoff_link_hash_entry **)
3934 bfd_malloc (o->reloc_count
3935 * sizeof (struct xcoff_link_hash_entry *)));
3936 if (finfo.section_info[o->target_index].relocs == NULL
3937 || finfo.section_info[o->target_index].rel_hashes == NULL)
3940 if (o->reloc_count > max_output_reloc_count)
3941 max_output_reloc_count = o->reloc_count;
3945 /* We now know the size of the relocs, so we can determine the file
3946 positions of the line numbers. */
3947 line_filepos = rel_filepos;
3948 finfo.line_filepos = line_filepos;
3949 linesz = bfd_coff_linesz (abfd);
3950 for (o = abfd->sections; o != NULL; o = o->next)
3952 if (o->lineno_count == 0)
3953 o->line_filepos = 0;
3956 o->line_filepos = line_filepos;
3957 line_filepos += o->lineno_count * linesz;
3960 /* Reset the reloc and lineno counts, so that we can use them to
3961 count the number of entries we have output so far. */
3963 o->lineno_count = 0;
3966 obj_sym_filepos (abfd) = line_filepos;
3968 /* Figure out the largest number of symbols in an input BFD. Take
3969 the opportunity to clear the output_has_begun fields of all the
3970 input BFD's. We want at least 4 symbols, since that is the
3971 number which xcoff_write_global_symbol may need. */
3973 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3977 sub->output_has_begun = false;
3978 sz = obj_raw_syment_count (sub);
3979 if (sz > max_sym_count)
3983 /* Allocate some buffers used while linking. */
3984 finfo.internal_syms = ((struct internal_syment *)
3985 bfd_malloc (max_sym_count
3986 * sizeof (struct internal_syment)));
3987 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
3988 finfo.outsyms = ((bfd_byte *)
3989 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
3990 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
3991 * bfd_coff_linesz (abfd));
3992 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3993 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
3994 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3995 || (finfo.sym_indices == NULL && max_sym_count > 0)
3996 || finfo.outsyms == NULL
3997 || (finfo.linenos == NULL && max_lineno_count > 0)
3998 || (finfo.contents == NULL && max_contents_size > 0)
3999 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4002 obj_raw_syment_count (abfd) = 0;
4003 xcoff_data (abfd)->toc = (bfd_vma) -1;
4005 /* We now know the position of everything in the file, except that
4006 we don't know the size of the symbol table and therefore we don't
4007 know where the string table starts. We just build the string
4008 table in memory as we go along. We process all the relocations
4009 for a single input file at once. */
4010 for (o = abfd->sections; o != NULL; o = o->next)
4012 for (p = o->link_order_head; p != NULL; p = p->next)
4014 if (p->type == bfd_indirect_link_order
4015 && p->u.indirect.section->owner->xvec == abfd->xvec)
4017 sub = p->u.indirect.section->owner;
4018 if (! sub->output_has_begun)
4020 if (! xcoff_link_input_bfd (&finfo, sub))
4022 sub->output_has_begun = true;
4025 else if (p->type == bfd_section_reloc_link_order
4026 || p->type == bfd_symbol_reloc_link_order)
4028 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4033 if (! _bfd_default_link_order (abfd, info, o, p))
4039 /* Free up the buffers used by xcoff_link_input_bfd. */
4041 if (finfo.internal_syms != NULL)
4043 free (finfo.internal_syms);
4044 finfo.internal_syms = NULL;
4046 if (finfo.sym_indices != NULL)
4048 free (finfo.sym_indices);
4049 finfo.sym_indices = NULL;
4051 if (finfo.linenos != NULL)
4053 free (finfo.linenos);
4054 finfo.linenos = NULL;
4056 if (finfo.contents != NULL)
4058 free (finfo.contents);
4059 finfo.contents = NULL;
4061 if (finfo.external_relocs != NULL)
4063 free (finfo.external_relocs);
4064 finfo.external_relocs = NULL;
4067 /* The value of the last C_FILE symbol is supposed to be -1. Write
4069 if (finfo.last_file_index != -1)
4071 finfo.last_file.n_value = -1;
4072 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4073 (PTR) finfo.outsyms);
4075 (obj_sym_filepos (abfd)
4076 + finfo.last_file_index * symesz),
4078 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4082 /* Write out all the global symbols which do not come from XCOFF
4084 xcoff_link_hash_traverse (xcoff_hash_table (info),
4085 xcoff_write_global_symbol,
4088 if (finfo.outsyms != NULL)
4090 free (finfo.outsyms);
4091 finfo.outsyms = NULL;
4094 /* Now that we have written out all the global symbols, we know the
4095 symbol indices to use for relocs against them, and we can finally
4096 write out the relocs. */
4097 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
4098 if (external_relocs == NULL && max_output_reloc_count != 0)
4100 bfd_set_error (bfd_error_no_memory);
4104 for (o = abfd->sections; o != NULL; o = o->next)
4106 struct internal_reloc *irel;
4107 struct internal_reloc *irelend;
4108 struct xcoff_link_hash_entry **rel_hash;
4109 struct xcoff_toc_rel_hash *toc_rel_hash;
4112 if (o->reloc_count == 0)
4115 irel = finfo.section_info[o->target_index].relocs;
4116 irelend = irel + o->reloc_count;
4117 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4118 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4120 if (*rel_hash != NULL)
4122 if ((*rel_hash)->indx < 0)
4124 if (! ((*info->callbacks->unattached_reloc)
4125 (info, (*rel_hash)->root.root.string,
4126 (bfd *) NULL, o, irel->r_vaddr)))
4128 (*rel_hash)->indx = 0;
4130 irel->r_symndx = (*rel_hash)->indx;
4134 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4135 toc_rel_hash != NULL;
4136 toc_rel_hash = toc_rel_hash->next)
4138 if (toc_rel_hash->h->u.toc_indx < 0)
4140 if (! ((*info->callbacks->unattached_reloc)
4141 (info, toc_rel_hash->h->root.root.string,
4142 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4144 toc_rel_hash->h->u.toc_indx = 0;
4146 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4149 /* XCOFF requires that the relocs be sorted by address. We tend
4150 to produce them in the order in which their containing csects
4151 appear in the symbol table, which is not necessarily by
4152 address. So we sort them here. There may be a better way to
4154 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4155 o->reloc_count, sizeof (struct internal_reloc),
4158 irel = finfo.section_info[o->target_index].relocs;
4159 irelend = irel + o->reloc_count;
4160 erel = external_relocs;
4161 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4162 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4164 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4165 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4166 abfd) != relsz * o->reloc_count)
4170 if (external_relocs != NULL)
4172 free (external_relocs);
4173 external_relocs = NULL;
4176 /* Free up the section information. */
4177 if (finfo.section_info != NULL)
4181 for (i = 0; i < abfd->section_count; i++)
4183 if (finfo.section_info[i].relocs != NULL)
4184 free (finfo.section_info[i].relocs);
4185 if (finfo.section_info[i].rel_hashes != NULL)
4186 free (finfo.section_info[i].rel_hashes);
4188 free (finfo.section_info);
4189 finfo.section_info = NULL;
4192 /* Write out the loader section contents. */
4193 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4194 == (xcoff_hash_table (info)->loader_section->contents
4195 + xcoff_hash_table (info)->ldhdr.l_impoff));
4196 o = xcoff_hash_table (info)->loader_section;
4197 if (! bfd_set_section_contents (abfd, o->output_section,
4198 o->contents, o->output_offset,
4202 /* Write out the magic sections. */
4203 o = xcoff_hash_table (info)->linkage_section;
4204 if (o->_raw_size > 0
4205 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4206 o->output_offset, o->_raw_size))
4208 o = xcoff_hash_table (info)->toc_section;
4209 if (o->_raw_size > 0
4210 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4211 o->output_offset, o->_raw_size))
4213 o = xcoff_hash_table (info)->descriptor_section;
4214 if (o->_raw_size > 0
4215 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4216 o->output_offset, o->_raw_size))
4219 /* Write out the string table. */
4221 (obj_sym_filepos (abfd)
4222 + obj_raw_syment_count (abfd) * symesz),
4226 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4227 (bfd_byte *) strbuf);
4228 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4230 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4233 _bfd_stringtab_free (finfo.strtab);
4235 /* Write out the debugging string table. */
4236 o = xcoff_hash_table (info)->debug_section;
4239 struct bfd_strtab_hash *debug_strtab;
4241 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4242 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4243 >= _bfd_stringtab_size (debug_strtab));
4245 o->output_section->filepos + o->output_offset,
4248 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4252 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4253 not try to write out the symbols. */
4254 bfd_get_symcount (abfd) = 0;
4259 if (finfo.strtab != NULL)
4260 _bfd_stringtab_free (finfo.strtab);
4261 if (finfo.section_info != NULL)
4265 for (i = 0; i < abfd->section_count; i++)
4267 if (finfo.section_info[i].relocs != NULL)
4268 free (finfo.section_info[i].relocs);
4269 if (finfo.section_info[i].rel_hashes != NULL)
4270 free (finfo.section_info[i].rel_hashes);
4272 free (finfo.section_info);
4274 if (finfo.internal_syms != NULL)
4275 free (finfo.internal_syms);
4276 if (finfo.sym_indices != NULL)
4277 free (finfo.sym_indices);
4278 if (finfo.outsyms != NULL)
4279 free (finfo.outsyms);
4280 if (finfo.linenos != NULL)
4281 free (finfo.linenos);
4282 if (finfo.contents != NULL)
4283 free (finfo.contents);
4284 if (finfo.external_relocs != NULL)
4285 free (finfo.external_relocs);
4286 if (external_relocs != NULL)
4287 free (external_relocs);
4291 /* Link an input file into the linker output file. This function
4292 handles all the sections and relocations of the input file at once. */
4295 xcoff_link_input_bfd (finfo, input_bfd)
4296 struct xcoff_final_link_info *finfo;
4300 const char *strings;
4301 bfd_size_type syment_base;
4302 unsigned int n_tmask;
4303 unsigned int n_btshft;
4305 bfd_size_type isymesz;
4306 bfd_size_type osymesz;
4307 bfd_size_type linesz;
4310 struct xcoff_link_hash_entry **sym_hash;
4311 struct internal_syment *isymp;
4313 unsigned long *debug_index;
4315 unsigned long output_index;
4322 /* We can just skip DYNAMIC files, unless this is a static link. */
4323 if ((input_bfd->flags & DYNAMIC) != 0
4324 && ! finfo->info->static_link)
4327 /* Move all the symbols to the output file. */
4329 output_bfd = finfo->output_bfd;
4331 syment_base = obj_raw_syment_count (output_bfd);
4332 isymesz = bfd_coff_symesz (input_bfd);
4333 osymesz = bfd_coff_symesz (output_bfd);
4334 linesz = bfd_coff_linesz (input_bfd);
4335 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4337 n_tmask = coff_data (input_bfd)->local_n_tmask;
4338 n_btshft = coff_data (input_bfd)->local_n_btshft;
4340 /* Define macros so that ISFCN, et. al., macros work correctly. */
4341 #define N_TMASK n_tmask
4342 #define N_BTSHFT n_btshft
4345 if (! finfo->info->keep_memory)
4348 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4351 if (! _bfd_coff_get_external_symbols (input_bfd))
4354 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4355 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4356 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4357 csectpp = xcoff_data (input_bfd)->csects;
4358 debug_index = xcoff_data (input_bfd)->debug_indices;
4359 isymp = finfo->internal_syms;
4360 indexp = finfo->sym_indices;
4361 output_index = syment_base;
4362 outsym = finfo->outsyms;
4366 while (esym < esym_end)
4368 struct internal_syment isym;
4369 union internal_auxent aux;
4375 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4377 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4379 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4381 BFD_ASSERT (isymp->n_numaux > 0);
4382 bfd_coff_swap_aux_in (input_bfd,
4383 (PTR) (esym + isymesz * isymp->n_numaux),
4384 isymp->n_type, isymp->n_sclass,
4385 isymp->n_numaux - 1, isymp->n_numaux,
4387 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4390 /* Make a copy of *isymp so that the relocate_section function
4391 always sees the original values. This is more reliable than
4392 always recomputing the symbol value even if we are stripping
4396 /* If this symbol is in the .loader section, swap out the
4397 .loader symbol information. If this is an external symbol
4398 reference to a defined symbol, though, then wait until we get
4399 to the definition. */
4400 if (isym.n_sclass == C_EXT
4401 && *sym_hash != NULL
4402 && (*sym_hash)->ldsym != NULL
4404 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4406 struct xcoff_link_hash_entry *h;
4407 struct internal_ldsym *ldsym;
4411 if (isym.n_scnum > 0)
4413 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4414 ldsym->l_value = (isym.n_value
4415 + (*csectpp)->output_section->vma
4416 + (*csectpp)->output_offset
4421 ldsym->l_scnum = isym.n_scnum;
4422 ldsym->l_value = isym.n_value;
4425 ldsym->l_smtype = smtyp;
4426 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4427 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4428 || (h->flags & XCOFF_IMPORT) != 0)
4429 ldsym->l_smtype |= L_IMPORT;
4430 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4431 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4432 || (h->flags & XCOFF_EXPORT) != 0)
4433 ldsym->l_smtype |= L_EXPORT;
4434 if ((h->flags & XCOFF_ENTRY) != 0)
4435 ldsym->l_smtype |= L_ENTRY;
4437 ldsym->l_smclas = aux.x_csect.x_smclas;
4439 if (ldsym->l_ifile == (bfd_size_type) -1)
4441 else if (ldsym->l_ifile == 0)
4443 if ((ldsym->l_smtype & L_IMPORT) == 0)
4449 if (h->root.type == bfd_link_hash_defined
4450 || h->root.type == bfd_link_hash_defweak)
4451 impbfd = h->root.u.def.section->owner;
4452 else if (h->root.type == bfd_link_hash_undefined
4453 || h->root.type == bfd_link_hash_undefweak)
4454 impbfd = h->root.u.undef.abfd;
4462 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4463 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4470 BFD_ASSERT (h->ldindx >= 0);
4471 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4472 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4473 finfo->ldsym + h->ldindx - 3);
4476 /* Fill in snentry now that we know the target_index. */
4477 if ((h->flags & XCOFF_ENTRY) != 0
4478 && (h->root.type == bfd_link_hash_defined
4479 || h->root.type == bfd_link_hash_defweak))
4480 xcoff_data (output_bfd)->snentry =
4481 h->root.u.def.section->output_section->target_index;
4488 add = 1 + isym.n_numaux;
4490 /* If we are skipping this csect, we want to skip this symbol. */
4491 if (*csectpp == NULL)
4494 /* If we garbage collected this csect, we want to skip this
4497 && xcoff_hash_table (finfo->info)->gc
4498 && ((*csectpp)->flags & SEC_MARK) == 0
4499 && *csectpp != bfd_abs_section_ptr)
4502 /* An XCOFF linker always skips C_STAT symbols. */
4504 && isymp->n_sclass == C_STAT)
4507 /* We skip all but the first TOC anchor. */
4509 && isymp->n_sclass == C_HIDEXT
4510 && aux.x_csect.x_smclas == XMC_TC0)
4512 if (finfo->toc_symindx != -1)
4516 bfd_vma tocval, tocend;
4518 tocval = ((*csectpp)->output_section->vma
4519 + (*csectpp)->output_offset
4522 /* We want to find out if tocval is a good value to use
4523 as the TOC anchor--that is, whether we can access all
4524 of the TOC using a 16 bit offset from tocval. This
4525 test assumes that the TOC comes at the end of the
4526 output section, as it does in the default linker
4529 tocend = ((*csectpp)->output_section->vma
4530 + (*csectpp)->output_section->_raw_size);
4532 if (tocval + 0x10000 < tocend)
4534 (*_bfd_error_handler)
4535 ("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling",
4536 (unsigned long) (tocend - tocval));
4537 bfd_set_error (bfd_error_file_too_big);
4541 if (tocval + 0x8000 < tocend)
4545 tocadd = tocend - (tocval + 0x8000);
4547 isym.n_value += tocadd;
4550 finfo->toc_symindx = output_index;
4551 xcoff_data (finfo->output_bfd)->toc = tocval;
4552 xcoff_data (finfo->output_bfd)->sntoc =
4553 (*csectpp)->output_section->target_index;
4558 /* If we are stripping all symbols, we want to skip this one. */
4560 && finfo->info->strip == strip_all)
4563 /* We can skip resolved external references. */
4565 && isym.n_sclass == C_EXT
4567 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4570 /* We can skip common symbols if they got defined somewhere
4573 && isym.n_sclass == C_EXT
4575 && ((*sym_hash)->root.type != bfd_link_hash_common
4576 || (*sym_hash)->root.u.c.p->section != *csectpp)
4577 && ((*sym_hash)->root.type != bfd_link_hash_defined
4578 || (*sym_hash)->root.u.def.section != *csectpp))
4581 /* Skip local symbols if we are discarding them. */
4583 && finfo->info->discard == discard_all
4584 && isym.n_sclass != C_EXT
4585 && (isym.n_sclass != C_HIDEXT
4586 || smtyp != XTY_SD))
4589 /* If we stripping debugging symbols, and this is a debugging
4590 symbol, then skip it. */
4592 && finfo->info->strip == strip_debugger
4593 && isym.n_scnum == N_DEBUG)
4596 /* If some symbols are stripped based on the name, work out the
4597 name and decide whether to skip this symbol. We don't handle
4598 this correctly for symbols whose names are in the .debug
4599 section; to get it right we would need a new bfd_strtab_hash
4600 function to return the string given the index. */
4602 && (finfo->info->strip == strip_some
4603 || finfo->info->discard == discard_l)
4604 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4607 char buf[SYMNMLEN + 1];
4609 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4613 if ((finfo->info->strip == strip_some
4614 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4616 || (finfo->info->discard == discard_l
4617 && (isym.n_sclass != C_EXT
4618 && (isym.n_sclass != C_HIDEXT
4619 || smtyp != XTY_SD))
4620 && strncmp (name, finfo->info->lprefix,
4621 finfo->info->lprefix_len) == 0))
4625 /* We can not skip the first TOC anchor. */
4628 && finfo->info->strip != strip_all)
4631 /* We now know whether we are to skip this symbol or not. */
4634 /* Adjust the symbol in order to output it. */
4636 if (isym._n._n_n._n_zeroes == 0
4637 && isym._n._n_n._n_offset != 0)
4639 /* This symbol has a long name. Enter it in the string
4640 table we are building. If *debug_index != -1, the
4641 name has already been entered in the .debug section. */
4642 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4643 isym._n._n_n._n_offset = *debug_index;
4649 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4653 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4654 if (indx == (bfd_size_type) -1)
4656 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4660 if (isym.n_sclass != C_BSTAT
4661 && isym.n_sclass != C_ESTAT
4662 && isym.n_sclass != C_DECL
4663 && isym.n_scnum > 0)
4665 isym.n_scnum = (*csectpp)->output_section->target_index;
4666 isym.n_value += ((*csectpp)->output_section->vma
4667 + (*csectpp)->output_offset
4671 /* The value of a C_FILE symbol is the symbol index of the
4672 next C_FILE symbol. The value of the last C_FILE symbol
4673 is -1. We try to get this right, below, just before we
4674 write the symbols out, but in the general case we may
4675 have to write the symbol out twice. */
4676 if (isym.n_sclass == C_FILE)
4678 if (finfo->last_file_index != -1
4679 && finfo->last_file.n_value != (long) output_index)
4681 /* We must correct the value of the last C_FILE entry. */
4682 finfo->last_file.n_value = output_index;
4683 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4685 /* The last C_FILE symbol is in this input file. */
4686 bfd_coff_swap_sym_out (output_bfd,
4687 (PTR) &finfo->last_file,
4688 (PTR) (finfo->outsyms
4689 + ((finfo->last_file_index
4695 /* We have already written out the last C_FILE
4696 symbol. We need to write it out again. We
4697 borrow *outsym temporarily. */
4698 bfd_coff_swap_sym_out (output_bfd,
4699 (PTR) &finfo->last_file,
4701 if (bfd_seek (output_bfd,
4702 (obj_sym_filepos (output_bfd)
4703 + finfo->last_file_index * osymesz),
4705 || (bfd_write (outsym, osymesz, 1, output_bfd)
4711 finfo->last_file_index = output_index;
4712 finfo->last_file = isym;
4715 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4716 into the line numbers. We update the symbol values when
4717 we handle the line numbers. */
4718 if (isym.n_sclass == C_BINCL
4719 || isym.n_sclass == C_EINCL)
4721 isym.n_value = finfo->line_filepos;
4725 /* Output the symbol. */
4727 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4729 *indexp = output_index;
4731 if (isym.n_sclass == C_EXT)
4734 struct xcoff_link_hash_entry *h;
4736 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4738 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4739 BFD_ASSERT (h != NULL);
4740 h->indx = output_index;
4743 /* If this is a symbol in the TOC which we may have merged
4744 (class XMC_TC), remember the symbol index of the TOC
4746 if (isym.n_sclass == C_HIDEXT
4747 && aux.x_csect.x_smclas == XMC_TC
4748 && *sym_hash != NULL)
4750 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4751 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4752 (*sym_hash)->u.toc_indx = output_index;
4755 output_index += add;
4756 outsym += add * osymesz;
4759 esym += add * isymesz;
4763 if (debug_index != NULL)
4766 for (--add; add > 0; --add)
4770 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4771 done in a separate pass, because we don't know the correct symbol
4772 indices until we have already decided which symbols we are going
4775 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4776 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4777 isymp = finfo->internal_syms;
4778 indexp = finfo->sym_indices;
4779 csectpp = xcoff_data (input_bfd)->csects;
4780 outsym = finfo->outsyms;
4781 while (esym < esym_end)
4785 add = 1 + isymp->n_numaux;
4788 esym += add * isymesz;
4793 if (isymp->n_sclass == C_BSTAT)
4795 struct internal_syment isym;
4798 /* The value of a C_BSTAT symbol is the symbol table
4799 index of the containing csect. */
4800 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4801 indx = isym.n_value;
4802 if (indx < obj_raw_syment_count (input_bfd))
4806 symindx = finfo->sym_indices[indx];
4810 isym.n_value = symindx;
4811 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4819 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4821 union internal_auxent aux;
4823 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4824 isymp->n_sclass, i, isymp->n_numaux,
4827 if (isymp->n_sclass == C_FILE)
4829 /* This is the file name (or some comment put in by
4830 the compiler). If it is long, we must put it in
4831 the string table. */
4832 if (aux.x_file.x_n.x_zeroes == 0
4833 && aux.x_file.x_n.x_offset != 0)
4835 const char *filename;
4838 BFD_ASSERT (aux.x_file.x_n.x_offset
4839 >= STRING_SIZE_SIZE);
4840 if (strings == NULL)
4842 strings = _bfd_coff_read_string_table (input_bfd);
4843 if (strings == NULL)
4846 filename = strings + aux.x_file.x_n.x_offset;
4847 indx = _bfd_stringtab_add (finfo->strtab, filename,
4849 if (indx == (bfd_size_type) -1)
4851 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4854 else if ((isymp->n_sclass == C_EXT
4855 || isymp->n_sclass == C_HIDEXT)
4856 && i + 1 == isymp->n_numaux)
4858 /* We don't support type checking. I don't know if
4860 aux.x_csect.x_parmhash = 0;
4861 /* I don't think anybody uses these fields, but we'd
4862 better clobber them just in case. */
4863 aux.x_csect.x_stab = 0;
4864 aux.x_csect.x_snstab = 0;
4865 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4869 indx = aux.x_csect.x_scnlen.l;
4870 if (indx < obj_raw_syment_count (input_bfd))
4874 symindx = finfo->sym_indices[indx];
4876 aux.x_sym.x_tagndx.l = 0;
4878 aux.x_sym.x_tagndx.l = symindx;
4882 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4886 if (ISFCN (isymp->n_type)
4887 || ISTAG (isymp->n_sclass)
4888 || isymp->n_sclass == C_BLOCK
4889 || isymp->n_sclass == C_FCN)
4891 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4893 && indx < obj_raw_syment_count (input_bfd))
4895 /* We look forward through the symbol for
4896 the index of the next symbol we are going
4897 to include. I don't know if this is
4899 while (finfo->sym_indices[indx] < 0
4900 && indx < obj_raw_syment_count (input_bfd))
4902 if (indx >= obj_raw_syment_count (input_bfd))
4903 indx = output_index;
4905 indx = finfo->sym_indices[indx];
4906 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4910 indx = aux.x_sym.x_tagndx.l;
4911 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4915 symindx = finfo->sym_indices[indx];
4917 aux.x_sym.x_tagndx.l = 0;
4919 aux.x_sym.x_tagndx.l = symindx;
4923 /* Copy over the line numbers, unless we are stripping
4924 them. We do this on a symbol by symbol basis in
4925 order to more easily handle garbage collection. */
4926 if ((isymp->n_sclass == C_EXT
4927 || isymp->n_sclass == C_HIDEXT)
4929 && isymp->n_numaux > 1
4930 && ISFCN (isymp->n_type)
4931 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4933 if (finfo->info->strip != strip_none
4934 && finfo->info->strip != strip_some)
4935 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4938 asection *enclosing;
4939 unsigned int enc_count;
4940 bfd_size_type linoff;
4941 struct internal_lineno lin;
4944 enclosing = xcoff_section_data (abfd, o)->enclosing;
4945 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4946 if (oline != enclosing)
4948 if (bfd_seek (input_bfd,
4949 enclosing->line_filepos,
4951 || (bfd_read (finfo->linenos, linesz,
4952 enc_count, input_bfd)
4953 != linesz * enc_count))
4958 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4959 - enclosing->line_filepos);
4961 bfd_coff_swap_lineno_in (input_bfd,
4962 (PTR) (finfo->linenos + linoff),
4965 || ((bfd_size_type) lin.l_addr.l_symndx
4969 obj_coff_external_syms (input_bfd)))
4971 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4974 bfd_byte *linpend, *linp;
4976 bfd_size_type count;
4978 lin.l_addr.l_symndx = *indexp;
4979 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4980 (PTR) (finfo->linenos
4983 linpend = (finfo->linenos
4984 + enc_count * linesz);
4985 offset = (o->output_section->vma
4988 for (linp = finfo->linenos + linoff + linesz;
4992 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4994 if (lin.l_lnno == 0)
4996 lin.l_addr.l_paddr += offset;
4997 bfd_coff_swap_lineno_out (output_bfd,
5002 count = (linp - (finfo->linenos + linoff)) / linesz;
5004 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5005 (o->output_section->line_filepos
5006 + o->output_section->lineno_count * linesz);
5008 if (bfd_seek (output_bfd,
5009 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5011 || (bfd_write (finfo->linenos + linoff,
5012 linesz, count, output_bfd)
5016 o->output_section->lineno_count += count;
5020 struct internal_syment *iisp, *iispend;
5025 /* Update any C_BINCL or C_EINCL symbols
5026 that refer to a line number in the
5027 range we just output. */
5028 iisp = finfo->internal_syms;
5030 + obj_raw_syment_count (input_bfd));
5031 iindp = finfo->sym_indices;
5032 oos = finfo->outsyms;
5033 while (iisp < iispend)
5036 && (iisp->n_sclass == C_BINCL
5037 || iisp->n_sclass == C_EINCL)
5038 && ((bfd_size_type) iisp->n_value
5039 >= enclosing->line_filepos + linoff)
5040 && ((bfd_size_type) iisp->n_value
5041 < (enclosing->line_filepos
5042 + enc_count * linesz)))
5044 struct internal_syment iis;
5046 bfd_coff_swap_sym_in (output_bfd,
5051 - enclosing->line_filepos
5053 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5054 bfd_coff_swap_sym_out (output_bfd,
5060 iiadd = 1 + iisp->n_numaux;
5062 oos += iiadd * osymesz;
5071 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5072 isymp->n_sclass, i, isymp->n_numaux,
5084 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5085 symbol will be the first symbol in the next input file. In the
5086 normal case, this will save us from writing out the C_FILE symbol
5088 if (finfo->last_file_index != -1
5089 && (bfd_size_type) finfo->last_file_index >= syment_base)
5091 finfo->last_file.n_value = output_index;
5092 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5093 (PTR) (finfo->outsyms
5094 + ((finfo->last_file_index - syment_base)
5098 /* Write the modified symbols to the output file. */
5099 if (outsym > finfo->outsyms)
5101 if (bfd_seek (output_bfd,
5102 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5104 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5106 != (bfd_size_type) (outsym - finfo->outsyms)))
5109 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5110 + (outsym - finfo->outsyms) / osymesz)
5113 obj_raw_syment_count (output_bfd) = output_index;
5116 /* Don't let the linker relocation routines discard the symbols. */
5117 keep_syms = obj_coff_keep_syms (input_bfd);
5118 obj_coff_keep_syms (input_bfd) = true;
5120 /* Relocate the contents of each section. */
5121 for (o = input_bfd->sections; o != NULL; o = o->next)
5125 if (! o->linker_mark)
5127 /* This section was omitted from the link. */
5131 if ((o->flags & SEC_HAS_CONTENTS) == 0
5132 || o->_raw_size == 0
5133 || (o->flags & SEC_IN_MEMORY) != 0)
5136 /* We have set filepos correctly for the sections we created to
5137 represent csects, so bfd_get_section_contents should work. */
5138 if (coff_section_data (input_bfd, o) != NULL
5139 && coff_section_data (input_bfd, o)->contents != NULL)
5140 contents = coff_section_data (input_bfd, o)->contents;
5143 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5144 (file_ptr) 0, o->_raw_size))
5146 contents = finfo->contents;
5149 if ((o->flags & SEC_RELOC) != 0)
5152 struct internal_reloc *internal_relocs;
5153 struct internal_reloc *irel;
5155 struct internal_reloc *irelend;
5156 struct xcoff_link_hash_entry **rel_hash;
5159 /* Read in the relocs. */
5160 target_index = o->output_section->target_index;
5161 internal_relocs = (xcoff_read_internal_relocs
5162 (input_bfd, o, false, finfo->external_relocs,
5164 (finfo->section_info[target_index].relocs
5165 + o->output_section->reloc_count)));
5166 if (internal_relocs == NULL)
5169 /* Call processor specific code to relocate the section
5171 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5175 finfo->internal_syms,
5176 xcoff_data (input_bfd)->csects))
5179 offset = o->output_section->vma + o->output_offset - o->vma;
5180 irel = internal_relocs;
5181 irelend = irel + o->reloc_count;
5182 rel_hash = (finfo->section_info[target_index].rel_hashes
5183 + o->output_section->reloc_count);
5184 for (; irel < irelend; irel++, rel_hash++)
5186 struct xcoff_link_hash_entry *h = NULL;
5187 struct internal_ldrel ldrel;
5192 /* Adjust the reloc address and symbol index. */
5194 irel->r_vaddr += offset;
5196 r_symndx = irel->r_symndx;
5200 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5202 && h->smclas != XMC_TD
5203 && (irel->r_type == R_TOC
5204 || irel->r_type == R_GL
5205 || irel->r_type == R_TCL
5206 || irel->r_type == R_TRL
5207 || irel->r_type == R_TRLA))
5209 /* This is a TOC relative reloc with a symbol
5210 attached. The symbol should be the one which
5211 this reloc is for. We want to make this
5212 reloc against the TOC address of the symbol,
5213 not the symbol itself. */
5214 BFD_ASSERT (h->toc_section != NULL);
5215 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5216 if (h->u.toc_indx != -1)
5217 irel->r_symndx = h->u.toc_indx;
5220 struct xcoff_toc_rel_hash *n;
5221 struct xcoff_link_section_info *si;
5223 n = ((struct xcoff_toc_rel_hash *)
5224 bfd_alloc (finfo->output_bfd,
5225 sizeof (struct xcoff_toc_rel_hash)));
5228 si = finfo->section_info + target_index;
5229 n->next = si->toc_rel_hashes;
5232 si->toc_rel_hashes = n;
5237 /* This is a global symbol. */
5239 irel->r_symndx = h->indx;
5242 /* This symbol is being written at the end
5243 of the file, and we do not yet know the
5244 symbol index. We save the pointer to the
5245 hash table entry in the rel_hash list.
5246 We set the indx field to -2 to indicate
5247 that this symbol must not be stripped. */
5256 indx = finfo->sym_indices[r_symndx];
5260 struct internal_syment *is;
5262 /* Relocations against a TC0 TOC anchor are
5263 automatically transformed to be against
5264 the TOC anchor in the output file. */
5265 is = finfo->internal_syms + r_symndx;
5266 if (is->n_sclass == C_HIDEXT
5267 && is->n_numaux > 0)
5270 union internal_auxent aux;
5274 obj_coff_external_syms (input_bfd))
5275 + ((r_symndx + is->n_numaux)
5277 bfd_coff_swap_aux_in (input_bfd, auxptr,
5278 is->n_type, is->n_sclass,
5282 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5283 && aux.x_csect.x_smclas == XMC_TC0)
5284 indx = finfo->toc_symindx;
5289 irel->r_symndx = indx;
5292 struct internal_syment *is;
5294 char buf[SYMNMLEN + 1];
5296 /* This reloc is against a symbol we are
5297 stripping. It would be possible to handle
5298 this case, but I don't think it's worth it. */
5299 is = finfo->internal_syms + r_symndx;
5301 name = (_bfd_coff_internal_syment_name
5302 (input_bfd, is, buf));
5306 if (! ((*finfo->info->callbacks->unattached_reloc)
5307 (finfo->info, name, input_bfd, o,
5315 switch (irel->r_type)
5319 || h->root.type == bfd_link_hash_defined
5320 || h->root.type == bfd_link_hash_defweak
5321 || h->root.type == bfd_link_hash_common)
5328 /* This reloc needs to be copied into the .loader
5330 ldrel.l_vaddr = irel->r_vaddr;
5332 ldrel.l_symndx = -1;
5334 || (h->root.type == bfd_link_hash_defined
5335 || h->root.type == bfd_link_hash_defweak
5336 || h->root.type == bfd_link_hash_common))
5341 sec = xcoff_data (input_bfd)->csects[r_symndx];
5342 else if (h->root.type == bfd_link_hash_common)
5343 sec = h->root.u.c.p->section;
5345 sec = h->root.u.def.section;
5346 sec = sec->output_section;
5348 if (strcmp (sec->name, ".text") == 0)
5350 else if (strcmp (sec->name, ".data") == 0)
5352 else if (strcmp (sec->name, ".bss") == 0)
5356 (*_bfd_error_handler)
5357 ("%s: loader reloc in unrecognized section `%s'",
5358 bfd_get_filename (input_bfd),
5360 bfd_set_error (bfd_error_nonrepresentable_section);
5366 if (! finfo->info->relocateable
5367 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5368 && (h->flags & XCOFF_IMPORT) == 0)
5370 /* We already called the undefined_symbol
5371 callback for this relocation, in
5372 _bfd_ppc_xcoff_relocate_section. Don't
5373 issue any more warnings. */
5376 if (h->ldindx < 0 && ! quiet)
5378 (*_bfd_error_handler)
5379 ("%s: `%s' in loader reloc but not loader sym",
5380 bfd_get_filename (input_bfd),
5381 h->root.root.string);
5382 bfd_set_error (bfd_error_bad_value);
5385 ldrel.l_symndx = h->ldindx;
5387 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5388 ldrel.l_rsecnm = o->output_section->target_index;
5389 if (xcoff_hash_table (finfo->info)->textro
5390 && strcmp (o->output_section->name, ".text") == 0
5393 (*_bfd_error_handler)
5394 ("%s: loader reloc in read-only section %s",
5395 bfd_get_filename (input_bfd),
5396 bfd_get_section_name (finfo->output_bfd,
5397 o->output_section));
5398 bfd_set_error (bfd_error_invalid_operation);
5401 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5403 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5412 /* We should never need a .loader reloc for a TOC
5418 o->output_section->reloc_count += o->reloc_count;
5421 /* Write out the modified section contents. */
5422 if (! bfd_set_section_contents (output_bfd, o->output_section,
5423 contents, o->output_offset,
5424 (o->_cooked_size != 0
5430 obj_coff_keep_syms (input_bfd) = keep_syms;
5432 if (! finfo->info->keep_memory)
5434 if (! _bfd_coff_free_symbols (input_bfd))
5444 /* Write out a non-XCOFF global symbol. */
5447 xcoff_write_global_symbol (h, p)
5448 struct xcoff_link_hash_entry *h;
5451 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5454 struct internal_syment isym;
5455 union internal_auxent aux;
5457 output_bfd = finfo->output_bfd;
5459 /* If this symbol was garbage collected, just skip it. */
5460 if (xcoff_hash_table (finfo->info)->gc
5461 && (h->flags & XCOFF_MARK) == 0)
5464 /* If we need a .loader section entry, write it out. */
5465 if (h->ldsym != NULL)
5467 struct internal_ldsym *ldsym;
5472 if (h->root.type == bfd_link_hash_undefined
5473 || h->root.type == bfd_link_hash_undefweak)
5476 ldsym->l_scnum = N_UNDEF;
5477 ldsym->l_smtype = XTY_ER;
5478 impbfd = h->root.u.undef.abfd;
5480 else if (h->root.type == bfd_link_hash_defined
5481 || h->root.type == bfd_link_hash_defweak)
5485 sec = h->root.u.def.section;
5486 ldsym->l_value = (sec->output_section->vma
5487 + sec->output_offset
5488 + h->root.u.def.value);
5489 ldsym->l_scnum = sec->output_section->target_index;
5490 ldsym->l_smtype = XTY_SD;
5491 impbfd = sec->owner;
5496 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5497 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5498 || (h->flags & XCOFF_IMPORT) != 0)
5499 ldsym->l_smtype |= L_IMPORT;
5500 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5501 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5502 || (h->flags & XCOFF_EXPORT) != 0)
5503 ldsym->l_smtype |= L_EXPORT;
5504 if ((h->flags & XCOFF_ENTRY) != 0)
5505 ldsym->l_smtype |= L_ENTRY;
5507 ldsym->l_smclas = h->smclas;
5509 if (ldsym->l_ifile == (bfd_size_type) -1)
5511 else if (ldsym->l_ifile == 0)
5513 if ((ldsym->l_smtype & L_IMPORT) == 0)
5515 else if (impbfd == NULL)
5519 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5520 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5526 BFD_ASSERT (h->ldindx >= 0);
5527 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5528 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5532 /* If this symbol needs global linkage code, write it out. */
5533 if (h->root.type == bfd_link_hash_defined
5534 && (h->root.u.def.section
5535 == xcoff_hash_table (finfo->info)->linkage_section))
5541 p = h->root.u.def.section->contents + h->root.u.def.value;
5543 /* The first instruction in the global linkage code loads a
5544 specific TOC element. */
5545 tocoff = (h->descriptor->toc_section->output_section->vma
5546 + h->descriptor->toc_section->output_offset
5547 - xcoff_data (output_bfd)->toc);
5548 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5549 tocoff += h->descriptor->u.toc_offset;
5550 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5552 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5554 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5557 /* If we created a TOC entry for this symbol, write out the required
5559 if ((h->flags & XCOFF_SET_TOC) != 0)
5564 struct internal_reloc *irel;
5565 struct internal_ldrel ldrel;
5567 tocsec = h->toc_section;
5568 osec = tocsec->output_section;
5569 oindx = osec->target_index;
5570 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5571 irel->r_vaddr = (osec->vma
5572 + tocsec->output_offset
5575 irel->r_symndx = h->indx;
5579 irel->r_symndx = obj_raw_syment_count (output_bfd);
5581 irel->r_type = R_POS;
5583 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5584 ++osec->reloc_count;
5586 BFD_ASSERT (h->ldindx >= 0);
5587 ldrel.l_vaddr = irel->r_vaddr;
5588 ldrel.l_symndx = h->ldindx;
5589 ldrel.l_rtype = (31 << 8) | R_POS;
5590 ldrel.l_rsecnm = oindx;
5591 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5595 /* If this symbol is a specially defined function descriptor, write
5596 it out. The first word is the address of the function code
5597 itself, the second word is the address of the TOC, and the third
5599 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5600 && h->root.type == bfd_link_hash_defined
5601 && (h->root.u.def.section
5602 == xcoff_hash_table (finfo->info)->descriptor_section))
5608 struct xcoff_link_hash_entry *hentry;
5610 struct internal_reloc *irel;
5611 struct internal_ldrel ldrel;
5614 sec = h->root.u.def.section;
5615 osec = sec->output_section;
5616 oindx = osec->target_index;
5617 p = sec->contents + h->root.u.def.value;
5619 hentry = h->descriptor;
5620 BFD_ASSERT (hentry != NULL
5621 && (hentry->root.type == bfd_link_hash_defined
5622 || hentry->root.type == bfd_link_hash_defweak));
5623 esec = hentry->root.u.def.section;
5624 bfd_put_32 (output_bfd,
5625 (esec->output_section->vma
5626 + esec->output_offset
5627 + hentry->root.u.def.value),
5630 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5631 irel->r_vaddr = (osec->vma
5632 + sec->output_offset
5633 + h->root.u.def.value);
5634 irel->r_symndx = esec->output_section->target_index;
5635 irel->r_type = R_POS;
5637 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5638 ++osec->reloc_count;
5640 ldrel.l_vaddr = irel->r_vaddr;
5641 if (strcmp (esec->output_section->name, ".text") == 0)
5643 else if (strcmp (esec->output_section->name, ".data") == 0)
5645 else if (strcmp (esec->output_section->name, ".bss") == 0)
5649 (*_bfd_error_handler)
5650 ("%s: loader reloc in unrecognized section `%s'",
5651 bfd_get_filename (output_bfd),
5652 esec->output_section->name);
5653 bfd_set_error (bfd_error_nonrepresentable_section);
5656 ldrel.l_rtype = (31 << 8) | R_POS;
5657 ldrel.l_rsecnm = oindx;
5658 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5661 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5663 tsec = coff_section_from_bfd_index (output_bfd,
5664 xcoff_data (output_bfd)->sntoc);
5667 irel->r_vaddr = (osec->vma
5668 + sec->output_offset
5669 + h->root.u.def.value
5671 irel->r_symndx = tsec->output_section->target_index;
5672 irel->r_type = R_POS;
5674 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5675 ++osec->reloc_count;
5677 ldrel.l_vaddr = irel->r_vaddr;
5678 if (strcmp (tsec->output_section->name, ".text") == 0)
5680 else if (strcmp (tsec->output_section->name, ".data") == 0)
5682 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5686 (*_bfd_error_handler)
5687 ("%s: loader reloc in unrecognized section `%s'",
5688 bfd_get_filename (output_bfd),
5689 tsec->output_section->name);
5690 bfd_set_error (bfd_error_nonrepresentable_section);
5693 ldrel.l_rtype = (31 << 8) | R_POS;
5694 ldrel.l_rsecnm = oindx;
5695 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5703 && (finfo->info->strip == strip_all
5704 || (finfo->info->strip == strip_some
5705 && (bfd_hash_lookup (finfo->info->keep_hash,
5706 h->root.root.string, false, false)
5711 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5714 outsym = finfo->outsyms;
5716 memset (&aux, 0, sizeof aux);
5718 h->indx = obj_raw_syment_count (output_bfd);
5720 if (strlen (h->root.root.string) <= SYMNMLEN)
5721 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5728 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5730 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5732 if (indx == (bfd_size_type) -1)
5734 isym._n._n_n._n_zeroes = 0;
5735 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5738 if (h->root.type == bfd_link_hash_undefined
5739 || h->root.type == bfd_link_hash_undefweak)
5742 isym.n_scnum = N_UNDEF;
5743 isym.n_sclass = C_EXT;
5744 aux.x_csect.x_smtyp = XTY_ER;
5746 else if (h->root.type == bfd_link_hash_defined
5747 || h->root.type == bfd_link_hash_defweak)
5749 struct xcoff_link_size_list *l;
5751 isym.n_value = (h->root.u.def.section->output_section->vma
5752 + h->root.u.def.section->output_offset
5753 + h->root.u.def.value);
5754 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5755 isym.n_sclass = C_HIDEXT;
5756 aux.x_csect.x_smtyp = XTY_SD;
5758 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5760 for (l = xcoff_hash_table (finfo->info)->size_list;
5766 aux.x_csect.x_scnlen.l = l->size;
5772 else if (h->root.type == bfd_link_hash_common)
5774 isym.n_value = (h->root.u.c.p->section->output_section->vma
5775 + h->root.u.c.p->section->output_offset);
5776 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5777 isym.n_sclass = C_EXT;
5778 aux.x_csect.x_smtyp = XTY_CM;
5779 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5784 isym.n_type = T_NULL;
5787 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5788 outsym += bfd_coff_symesz (output_bfd);
5790 aux.x_csect.x_smclas = h->smclas;
5792 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5794 outsym += bfd_coff_auxesz (output_bfd);
5796 if (h->root.type == bfd_link_hash_defined
5797 || h->root.type == bfd_link_hash_defweak)
5799 /* We just output an SD symbol. Now output an LD symbol. */
5803 isym.n_sclass = C_EXT;
5804 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5805 outsym += bfd_coff_symesz (output_bfd);
5807 aux.x_csect.x_smtyp = XTY_LD;
5808 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5810 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5812 outsym += bfd_coff_auxesz (output_bfd);
5815 if (bfd_seek (output_bfd,
5816 (obj_sym_filepos (output_bfd)
5817 + (obj_raw_syment_count (output_bfd)
5818 * bfd_coff_symesz (output_bfd))),
5820 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
5821 != (bfd_size_type) (outsym - finfo->outsyms)))
5823 obj_raw_syment_count (output_bfd) +=
5824 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5829 /* Handle a link order which is supposed to generate a reloc. */
5832 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5834 struct xcoff_final_link_info *finfo;
5835 asection *output_section;
5836 struct bfd_link_order *link_order;
5838 reloc_howto_type *howto;
5839 struct xcoff_link_hash_entry *h;
5843 struct internal_reloc *irel;
5844 struct xcoff_link_hash_entry **rel_hash_ptr;
5845 struct internal_ldrel ldrel;
5847 if (link_order->type == bfd_section_reloc_link_order)
5849 /* We need to somehow locate a symbol in the right section. The
5850 symbol must either have a value of zero, or we must adjust
5851 the addend by the value of the symbol. FIXME: Write this
5852 when we need it. The old linker couldn't handle this anyhow. */
5856 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5859 bfd_set_error (bfd_error_bad_value);
5863 h = ((struct xcoff_link_hash_entry *)
5864 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5865 link_order->u.reloc.p->u.name,
5866 false, false, true));
5869 if (! ((*finfo->info->callbacks->unattached_reloc)
5870 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5871 (asection *) NULL, (bfd_vma) 0)))
5876 if (h->root.type == bfd_link_hash_common)
5878 hsec = h->root.u.c.p->section;
5881 else if (h->root.type == bfd_link_hash_defined
5882 || h->root.type == bfd_link_hash_defweak)
5884 hsec = h->root.u.def.section;
5885 hval = h->root.u.def.value;
5893 addend = link_order->u.reloc.p->addend;
5895 addend += (hsec->output_section->vma
5896 + hsec->output_offset
5903 bfd_reloc_status_type rstat;
5906 size = bfd_get_reloc_size (howto);
5907 buf = (bfd_byte *) bfd_zmalloc (size);
5911 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5917 case bfd_reloc_outofrange:
5919 case bfd_reloc_overflow:
5920 if (! ((*finfo->info->callbacks->reloc_overflow)
5921 (finfo->info, link_order->u.reloc.p->u.name,
5922 howto->name, addend, (bfd *) NULL, (asection *) NULL,
5930 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5931 (file_ptr) link_order->offset, size);
5937 /* Store the reloc information in the right place. It will get
5938 swapped and written out at the end of the final_link routine. */
5940 irel = (finfo->section_info[output_section->target_index].relocs
5941 + output_section->reloc_count);
5942 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5943 + output_section->reloc_count);
5945 memset (irel, 0, sizeof (struct internal_reloc));
5946 *rel_hash_ptr = NULL;
5948 irel->r_vaddr = output_section->vma + link_order->offset;
5951 irel->r_symndx = h->indx;
5954 /* Set the index to -2 to force this symbol to get written out. */
5960 irel->r_type = howto->type;
5961 irel->r_size = howto->bitsize - 1;
5962 if (howto->complain_on_overflow == complain_overflow_signed)
5963 irel->r_size |= 0x80;
5965 ++output_section->reloc_count;
5967 /* Now output the reloc to the .loader section. */
5969 ldrel.l_vaddr = irel->r_vaddr;
5973 const char *secname;
5975 secname = hsec->output_section->name;
5977 if (strcmp (secname, ".text") == 0)
5979 else if (strcmp (secname, ".data") == 0)
5981 else if (strcmp (secname, ".bss") == 0)
5985 (*_bfd_error_handler)
5986 ("%s: loader reloc in unrecognized section `%s'",
5987 bfd_get_filename (output_bfd), secname);
5988 bfd_set_error (bfd_error_nonrepresentable_section);
5996 (*_bfd_error_handler)
5997 ("%s: `%s' in loader reloc but not loader sym",
5998 bfd_get_filename (output_bfd),
5999 h->root.root.string);
6000 bfd_set_error (bfd_error_bad_value);
6003 ldrel.l_symndx = h->ldindx;
6006 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6007 ldrel.l_rsecnm = output_section->target_index;
6008 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6014 /* Sort relocs by VMA. This is called via qsort. */
6017 xcoff_sort_relocs (p1, p2)
6021 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6022 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6024 if (r1->r_vaddr > r2->r_vaddr)
6026 else if (r1->r_vaddr < r2->r_vaddr)
6032 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6033 This is currently the only processor which uses XCOFF; I hope that
6034 will never change. */
6037 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6038 input_section, contents, relocs, syms,
6041 struct bfd_link_info *info;
6043 asection *input_section;
6045 struct internal_reloc *relocs;
6046 struct internal_syment *syms;
6047 asection **sections;
6049 struct internal_reloc *rel;
6050 struct internal_reloc *relend;
6053 relend = rel + input_section->reloc_count;
6054 for (; rel < relend; rel++)
6057 struct xcoff_link_hash_entry *h;
6058 struct internal_syment *sym;
6061 struct reloc_howto_struct howto;
6062 bfd_reloc_status_type rstat;
6064 /* Relocation type R_REF is a special relocation type which is
6065 merely used to prevent garbage collection from occurring for
6066 the csect including the symbol which it references. */
6067 if (rel->r_type == R_REF)
6070 symndx = rel->r_symndx;
6080 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6081 sym = syms + symndx;
6082 addend = - sym->n_value;
6085 /* We build the howto information on the fly. */
6087 howto.type = rel->r_type;
6088 howto.rightshift = 0;
6090 howto.bitsize = (rel->r_size & 0x1f) + 1;
6091 howto.pc_relative = false;
6093 if ((rel->r_size & 0x80) != 0)
6094 howto.complain_on_overflow = complain_overflow_signed;
6096 howto.complain_on_overflow = complain_overflow_bitfield;
6097 howto.special_function = NULL;
6098 howto.name = "internal";
6099 howto.partial_inplace = true;
6100 if (howto.bitsize == 32)
6101 howto.src_mask = howto.dst_mask = 0xffffffff;
6104 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6105 if (howto.bitsize == 16)
6108 howto.pcrel_offset = false;
6118 sec = bfd_abs_section_ptr;
6123 sec = sections[symndx];
6124 /* Hack to make sure we use the right TOC anchor value
6125 if this reloc is against the TOC anchor. */
6126 if (sec->name[3] == '0'
6127 && strcmp (sec->name, ".tc0") == 0)
6128 val = xcoff_data (output_bfd)->toc;
6130 val = (sec->output_section->vma
6131 + sec->output_offset
6138 if (h->root.type == bfd_link_hash_defined
6139 || h->root.type == bfd_link_hash_defweak)
6143 sec = h->root.u.def.section;
6144 val = (h->root.u.def.value
6145 + sec->output_section->vma
6146 + sec->output_offset);
6148 else if (h->root.type == bfd_link_hash_common)
6152 sec = h->root.u.c.p->section;
6153 val = (sec->output_section->vma
6154 + sec->output_offset);
6156 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6157 || (h->flags & XCOFF_IMPORT) != 0)
6159 /* Every symbol in a shared object is defined somewhere. */
6162 else if (! info->relocateable)
6164 if (! ((*info->callbacks->undefined_symbol)
6165 (info, h->root.root.string, input_bfd, input_section,
6166 rel->r_vaddr - input_section->vma)))
6169 /* Don't try to process the reloc. It can't help, and
6170 it may generate another error. */
6175 /* I took the relocation type definitions from two documents:
6176 the PowerPC AIX Version 4 Application Binary Interface, First
6177 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6178 32-Bit Hardware Implementation (June 30, 1994). Differences
6179 between the documents are noted below. */
6181 switch (rel->r_type)
6186 /* These relocs are defined by the PowerPC ABI to be
6187 relative branches which use half of the difference
6188 between the symbol and the program counter. I can't
6189 quite figure out when this is useful. These relocs are
6190 not defined by the PowerOpen ABI. */
6192 (*_bfd_error_handler)
6193 ("%s: unsupported relocation type 0x%02x",
6194 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6195 bfd_set_error (bfd_error_bad_value);
6198 /* Simple positive relocation. */
6201 /* Simple negative relocation. */
6205 /* Simple PC relative relocation. */
6206 howto.pc_relative = true;
6209 /* TOC relative relocation. The value in the instruction in
6210 the input file is the offset from the input file TOC to
6211 the desired location. We want the offset from the final
6212 TOC to the desired location. We have:
6217 so we must change insn by on - in.
6220 /* Global linkage relocation. The value of this relocation
6221 is the address of the entry in the TOC section. */
6223 /* Local object TOC address. I can't figure out the
6224 difference between this and case R_GL. */
6226 /* TOC relative relocation. A TOC relative load instruction
6227 which may be changed to a load address instruction.
6228 FIXME: We don't currently implement this optimization. */
6230 /* TOC relative relocation. This is a TOC relative load
6231 address instruction which may be changed to a load
6232 instruction. FIXME: I don't know if this is the correct
6234 if (h != NULL && h->smclas != XMC_TD)
6236 if (h->toc_section == NULL)
6238 (*_bfd_error_handler)
6239 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
6240 bfd_get_filename (input_bfd), rel->r_vaddr,
6241 h->root.root.string);
6242 bfd_set_error (bfd_error_bad_value);
6246 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6247 val = (h->toc_section->output_section->vma
6248 + h->toc_section->output_offset);
6251 val = ((val - xcoff_data (output_bfd)->toc)
6252 - (sym->n_value - xcoff_data (input_bfd)->toc));
6256 /* Absolute branch. We don't want to mess with the lower
6257 two bits of the instruction. */
6259 /* The PowerPC ABI defines this as an absolute call which
6260 may be modified to become a relative call. The PowerOpen
6261 ABI does not define this relocation type. */
6263 /* Absolute branch which may be modified to become a
6266 /* The PowerPC ABI defines this as an absolute branch to a
6267 fixed address which may be modified to an absolute branch
6268 to a symbol. The PowerOpen ABI does not define this
6271 /* The PowerPC ABI defines this as an absolute branch to a
6272 fixed address which may be modified to a relative branch.
6273 The PowerOpen ABI does not define this relocation type. */
6274 howto.src_mask &= ~3;
6275 howto.dst_mask = howto.src_mask;
6278 /* Relative branch. We don't want to mess with the lower
6279 two bits of the instruction. */
6281 /* The PowerPC ABI defines this as a relative call which may
6282 be modified to become an absolute call. The PowerOpen
6283 ABI does not define this relocation type. */
6285 /* A relative branch which may be modified to become an
6286 absolute branch. FIXME: We don't implement this,
6287 although we should for symbols of storage mapping class
6289 howto.pc_relative = true;
6290 howto.src_mask &= ~3;
6291 howto.dst_mask = howto.src_mask;
6294 /* The PowerPC AIX ABI describes this as a load which may be
6295 changed to a load address. The PowerOpen ABI says this
6296 is the same as case R_POS. */
6299 /* The PowerPC AIX ABI describes this as a load address
6300 which may be changed to a load. The PowerOpen ABI says
6301 this is the same as R_POS. */
6305 /* If we see an R_BR or R_RBR reloc which is jumping to global
6306 linkage code, and it is followed by an appropriate cror nop
6307 instruction, we replace the cror with lwz r2,20(r1). This
6308 restores the TOC after the glink code. Contrariwise, if the
6309 call is followed by a lwz r2,20(r1), but the call is not
6310 going to global linkage code, we can replace the load with a
6312 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6314 && h->root.type == bfd_link_hash_defined
6315 && (rel->r_vaddr - input_section->vma + 8
6316 <= input_section->_cooked_size))
6321 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6322 next = bfd_get_32 (input_bfd, pnext);
6324 /* The _ptrgl function is magic. It is used by the AIX
6325 compiler to call a function through a pointer. */
6326 if (h->smclas == XMC_GL
6327 || strcmp (h->root.root.string, "._ptrgl") == 0)
6329 if (next == 0x4def7b82 /* cror 15,15,15 */
6330 || next == 0x4ffffb82) /* cror 31,31,31 */
6331 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6335 if (next == 0x80410014) /* lwz r1,20(r1) */
6336 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
6340 /* A PC relative reloc includes the section address. */
6341 if (howto.pc_relative)
6342 addend += input_section->vma;
6344 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6346 rel->r_vaddr - input_section->vma,
6355 case bfd_reloc_overflow:
6358 char buf[SYMNMLEN + 1];
6359 char howto_name[10];
6364 name = h->root.root.string;
6367 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6371 sprintf (howto_name, "0x%02x", rel->r_type);
6373 if (! ((*info->callbacks->reloc_overflow)
6374 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6375 input_section, rel->r_vaddr - input_section->vma)))