1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* Get the XCOFF hash table entries for a BFD. */
33 #define obj_xcoff_sym_hashes(bfd) \
34 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
36 /* XCOFF relocation types. These probably belong in a header file
37 somewhere. The relocations are described in the function
38 _bfd_ppc_xcoff_relocate_section in this file. */
54 #define R_RRTBI (0x14)
55 #define R_RRTBA (0x15)
63 /* The first word of global linkage code. This must be modified by
64 filling in the correct TOC offset. */
66 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
68 /* The remaining words of global linkage code. */
70 static unsigned long xcoff_glink_code[] =
72 0x90410014, /* stw r2,20(r1) */
73 0x800c0000, /* lwz r0,0(r12) */
74 0x804c0004, /* lwz r2,4(r12) */
75 0x7c0903a6, /* mtctr r0 */
76 0x4e800420, /* bctr */
77 0x0, /* start of traceback table */
78 0x000c8000, /* traceback table */
79 0x0 /* traceback table */
82 #define XCOFF_GLINK_SIZE \
83 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
85 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
86 This flag will only be used on input sections. */
88 #define SEC_MARK (SEC_ROM)
90 /* The ldhdr structure. This appears at the start of the .loader
95 /* The version number: currently always 1. */
96 unsigned long l_version;
97 /* The number of symbol table entries. */
98 bfd_size_type l_nsyms;
99 /* The number of relocation table entries. */
100 bfd_size_type l_nreloc;
101 /* The length of the import file string table. */
102 bfd_size_type l_istlen;
103 /* The number of import files. */
104 bfd_size_type l_nimpid;
105 /* The offset from the start of the .loader section to the first
106 entry in the import file table. */
107 bfd_size_type l_impoff;
108 /* The length of the string table. */
109 bfd_size_type l_stlen;
110 /* The offset from the start of the .loader section to the first
111 entry in the string table. */
112 bfd_size_type l_stoff;
115 struct external_ldhdr
117 bfd_byte l_version[4];
119 bfd_byte l_nreloc[4];
120 bfd_byte l_istlen[4];
121 bfd_byte l_nimpid[4];
122 bfd_byte l_impoff[4];
127 #define LDHDRSZ (8 * 4)
129 /* The ldsym structure. This is used to represent a symbol in the
132 struct internal_ldsym
136 /* The symbol name if <= SYMNMLEN characters. */
137 char _l_name[SYMNMLEN];
140 /* Zero if the symbol name is more than SYMNMLEN characters. */
142 /* The offset in the string table if the symbol name is more
143 than SYMNMLEN characters. */
147 /* The symbol value. */
149 /* The symbol section number. */
151 /* The symbol type and flags. */
153 /* The symbol storage class. */
155 /* The import file ID. */
156 bfd_size_type l_ifile;
157 /* Offset to the parameter type check string. */
158 bfd_size_type l_parm;
161 struct external_ldsym
165 bfd_byte _l_name[SYMNMLEN];
168 bfd_byte _l_zeroes[4];
169 bfd_byte _l_offset[4];
174 bfd_byte l_smtype[1];
175 bfd_byte l_smclas[1];
180 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
182 /* These flags are for the l_smtype field (the lower three bits are an
185 /* Imported symbol. */
186 #define L_IMPORT (0x40)
188 #define L_ENTRY (0x20)
189 /* Exported symbol. */
190 #define L_EXPORT (0x10)
192 /* The ldrel structure. This is used to represent a reloc in the
195 struct internal_ldrel
197 /* The reloc address. */
199 /* The symbol table index in the .loader section symbol table. */
200 bfd_size_type l_symndx;
201 /* The relocation type and size. */
203 /* The section number this relocation applies to. */
207 struct external_ldrel
210 bfd_byte l_symndx[4];
212 bfd_byte l_rsecnm[2];
215 #define LDRELSZ (2 * 4 + 2 * 2)
217 /* The list of import files. */
219 struct xcoff_import_file
221 /* The next entry in the list. */
222 struct xcoff_import_file *next;
227 /* The member name. */
231 /* An entry in the XCOFF linker hash table. */
233 struct xcoff_link_hash_entry
235 struct bfd_link_hash_entry root;
237 /* Symbol index in output file. Set to -1 initially. Set to -2 if
238 there is a reloc against this symbol. */
241 /* If we have created a TOC entry for this symbol, this is the .tc
242 section which holds it. */
243 asection *toc_section;
247 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
248 set), this is the offset in toc_section. */
250 /* If the TOC entry comes from an input file, this is set to the
251 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
255 /* If this symbol is a function entry point which is called, this
256 field holds a pointer to the function descriptor. If this symbol
257 is a function descriptor, this field holds a pointer to the
258 function entry point. */
259 struct xcoff_link_hash_entry *descriptor;
261 /* The .loader symbol table entry, if there is one. */
262 struct internal_ldsym *ldsym;
264 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
265 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
266 this is the l_ifile value. */
269 /* Some linker flags. */
270 unsigned short flags;
271 /* Symbol is referenced by a regular object. */
272 #define XCOFF_REF_REGULAR (01)
273 /* Symbol is defined by a regular object. */
274 #define XCOFF_DEF_REGULAR (02)
275 /* Symbol is defined by a dynamic object. */
276 #define XCOFF_DEF_DYNAMIC (04)
277 /* Symbol is used in a reloc being copied into the .loader section. */
278 #define XCOFF_LDREL (010)
279 /* Symbol is the entry point. */
280 #define XCOFF_ENTRY (020)
281 /* Symbol is called; this is, it appears in a R_BR reloc. */
282 #define XCOFF_CALLED (040)
283 /* Symbol needs the TOC entry filled in. */
284 #define XCOFF_SET_TOC (0100)
285 /* Symbol is explicitly imported. */
286 #define XCOFF_IMPORT (0200)
287 /* Symbol is explicitly exported. */
288 #define XCOFF_EXPORT (0400)
289 /* Symbol has been processed by xcoff_build_ldsyms. */
290 #define XCOFF_BUILT_LDSYM (01000)
291 /* Symbol is mentioned by a section which was not garbage collected. */
292 #define XCOFF_MARK (02000)
293 /* Symbol size is recorded in size_list list from hash table. */
294 #define XCOFF_HAS_SIZE (04000)
295 /* Symbol is a function descriptor. */
296 #define XCOFF_DESCRIPTOR (010000)
297 /* Multiple definitions have been for the symbol. */
298 #define XCOFF_MULTIPLY_DEFINED (020000)
300 /* The storage mapping class. */
301 unsigned char smclas;
304 /* The XCOFF linker hash table. */
306 struct xcoff_link_hash_table
308 struct bfd_link_hash_table root;
310 /* The .debug string hash table. We need to compute this while
311 reading the input files, so that we know how large the .debug
312 section will be before we assign section positions. */
313 struct bfd_strtab_hash *debug_strtab;
315 /* The .debug section we will use for the final output. */
316 asection *debug_section;
318 /* The .loader section we will use for the final output. */
319 asection *loader_section;
321 /* A count of non TOC relative relocs which will need to be
322 allocated in the .loader section. */
325 /* The .loader section header. */
326 struct internal_ldhdr ldhdr;
328 /* The .gl section we use to hold global linkage code. */
329 asection *linkage_section;
331 /* The .tc section we use to hold toc entries we build for global
333 asection *toc_section;
335 /* The .ds section we use to hold function descriptors which we
336 create for exported symbols. */
337 asection *descriptor_section;
339 /* The list of import files. */
340 struct xcoff_import_file *imports;
342 /* Required alignment of sections within the output file. */
343 unsigned long file_align;
345 /* Whether the .text section must be read-only. */
348 /* Whether garbage collection was done. */
351 /* A linked list of symbols for which we have size information. */
352 struct xcoff_link_size_list
354 struct xcoff_link_size_list *next;
355 struct xcoff_link_hash_entry *h;
359 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
360 asection *special_sections[6];
363 /* Information we keep for each section in the output file during the
366 struct xcoff_link_section_info
368 /* The relocs to be output. */
369 struct internal_reloc *relocs;
370 /* For each reloc against a global symbol whose index was not known
371 when the reloc was handled, the global hash table entry. */
372 struct xcoff_link_hash_entry **rel_hashes;
373 /* If there is a TOC relative reloc against a global symbol, and the
374 index of the TOC symbol is not known when the reloc was handled,
375 an entry is added to this linked list. This is not an array,
376 like rel_hashes, because this case is quite uncommon. */
377 struct xcoff_toc_rel_hash
379 struct xcoff_toc_rel_hash *next;
380 struct xcoff_link_hash_entry *h;
381 struct internal_reloc *rel;
385 /* Information that we pass around while doing the final link step. */
387 struct xcoff_final_link_info
389 /* General link information. */
390 struct bfd_link_info *info;
393 /* Hash table for long symbol names. */
394 struct bfd_strtab_hash *strtab;
395 /* Array of information kept for each output section, indexed by the
396 target_index field. */
397 struct xcoff_link_section_info *section_info;
398 /* Symbol index of last C_FILE symbol (-1 if none). */
399 long last_file_index;
400 /* Contents of last C_FILE symbol. */
401 struct internal_syment last_file;
402 /* Symbol index of TOC symbol. */
404 /* Start of .loader symbols. */
405 struct external_ldsym *ldsym;
406 /* Next .loader reloc to swap out. */
407 struct external_ldrel *ldrel;
408 /* File position of start of line numbers. */
409 file_ptr line_filepos;
410 /* Buffer large enough to hold swapped symbols of any input file. */
411 struct internal_syment *internal_syms;
412 /* Buffer large enough to hold output indices of symbols of any
415 /* Buffer large enough to hold output symbols for any input file. */
417 /* Buffer large enough to hold external line numbers for any input
420 /* Buffer large enough to hold any input section. */
422 /* Buffer large enough to hold external relocs of any input section. */
423 bfd_byte *external_relocs;
426 static void xcoff_swap_ldhdr_in
427 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
428 static void xcoff_swap_ldhdr_out
429 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
430 static void xcoff_swap_ldsym_in
431 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
432 static void xcoff_swap_ldsym_out
433 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
434 static void xcoff_swap_ldrel_in
435 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
436 static void xcoff_swap_ldrel_out
437 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
438 static struct bfd_hash_entry *xcoff_link_hash_newfunc
439 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
440 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
441 static struct internal_reloc *xcoff_read_internal_relocs
442 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
443 struct internal_reloc *));
444 static boolean xcoff_link_add_object_symbols
445 PARAMS ((bfd *, struct bfd_link_info *));
446 static boolean xcoff_link_check_archive_element
447 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
448 static boolean xcoff_link_check_ar_symbols
449 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
450 static boolean xcoff_link_check_dynamic_ar_symbols
451 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
452 static bfd_size_type xcoff_find_reloc
453 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
454 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
455 static boolean xcoff_link_add_dynamic_symbols
456 PARAMS ((bfd *, struct bfd_link_info *));
457 static boolean xcoff_mark_symbol
458 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
459 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
460 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
461 static boolean xcoff_build_ldsyms
462 PARAMS ((struct xcoff_link_hash_entry *, PTR));
463 static boolean xcoff_link_input_bfd
464 PARAMS ((struct xcoff_final_link_info *, bfd *));
465 static boolean xcoff_write_global_symbol
466 PARAMS ((struct xcoff_link_hash_entry *, PTR));
467 static boolean xcoff_reloc_link_order
468 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
469 struct bfd_link_order *));
470 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
472 /* Routines to swap information in the XCOFF .loader section. If we
473 ever need to write an XCOFF loader, this stuff will need to be
474 moved to another file shared by the linker (which XCOFF calls the
475 ``binder'') and the loader. */
477 /* Swap in the ldhdr structure. */
480 xcoff_swap_ldhdr_in (abfd, src, dst)
482 const struct external_ldhdr *src;
483 struct internal_ldhdr *dst;
485 dst->l_version = bfd_get_32 (abfd, src->l_version);
486 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
487 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
488 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
489 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
490 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
491 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
492 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
495 /* Swap out the ldhdr structure. */
498 xcoff_swap_ldhdr_out (abfd, src, dst)
500 const struct internal_ldhdr *src;
501 struct external_ldhdr *dst;
503 bfd_put_32 (abfd, src->l_version, dst->l_version);
504 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
505 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
506 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
507 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
508 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
509 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
510 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
513 /* Swap in the ldsym structure. */
516 xcoff_swap_ldsym_in (abfd, src, dst)
518 const struct external_ldsym *src;
519 struct internal_ldsym *dst;
521 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
522 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
525 dst->_l._l_l._l_zeroes = 0;
526 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
528 dst->l_value = bfd_get_32 (abfd, src->l_value);
529 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
530 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
531 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
532 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
533 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
536 /* Swap out the ldsym structure. */
539 xcoff_swap_ldsym_out (abfd, src, dst)
541 const struct internal_ldsym *src;
542 struct external_ldsym *dst;
544 if (src->_l._l_l._l_zeroes != 0)
545 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
548 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
549 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
551 bfd_put_32 (abfd, src->l_value, dst->l_value);
552 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
553 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
554 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
555 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
556 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
559 /* Swap in the ldrel structure. */
562 xcoff_swap_ldrel_in (abfd, src, dst)
564 const struct external_ldrel *src;
565 struct internal_ldrel *dst;
567 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
568 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
569 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
570 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
573 /* Swap out the ldrel structure. */
576 xcoff_swap_ldrel_out (abfd, src, dst)
578 const struct internal_ldrel *src;
579 struct external_ldrel *dst;
581 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
582 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
583 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
584 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
587 /* Routines to read XCOFF dynamic information. This don't really
588 belong here, but we already have the ldsym manipulation routines
591 /* Read the contents of a section. */
594 xcoff_get_section_contents (abfd, sec)
598 if (coff_section_data (abfd, sec) == NULL)
600 sec->used_by_bfd = bfd_zalloc (abfd,
601 sizeof (struct coff_section_tdata));
602 if (sec->used_by_bfd == NULL)
606 if (coff_section_data (abfd, sec)->contents == NULL)
608 coff_section_data (abfd, sec)->contents =
609 (bfd_byte *) bfd_malloc (sec->_raw_size);
610 if (coff_section_data (abfd, sec)->contents == NULL)
613 if (! bfd_get_section_contents (abfd, sec,
614 coff_section_data (abfd, sec)->contents,
615 (file_ptr) 0, sec->_raw_size))
622 /* Get the size required to hold the dynamic symbols. */
625 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
630 struct internal_ldhdr ldhdr;
632 if ((abfd->flags & DYNAMIC) == 0)
634 bfd_set_error (bfd_error_invalid_operation);
638 lsec = bfd_get_section_by_name (abfd, ".loader");
641 bfd_set_error (bfd_error_no_symbols);
645 if (! xcoff_get_section_contents (abfd, lsec))
647 contents = coff_section_data (abfd, lsec)->contents;
649 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
651 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
654 /* Get the dynamic symbols. */
657 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
663 struct internal_ldhdr ldhdr;
665 struct external_ldsym *elsym, *elsymend;
666 coff_symbol_type *symbuf;
668 if ((abfd->flags & DYNAMIC) == 0)
670 bfd_set_error (bfd_error_invalid_operation);
674 lsec = bfd_get_section_by_name (abfd, ".loader");
677 bfd_set_error (bfd_error_no_symbols);
681 if (! xcoff_get_section_contents (abfd, lsec))
683 contents = coff_section_data (abfd, lsec)->contents;
685 coff_section_data (abfd, lsec)->keep_contents = true;
687 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
689 strings = (char *) contents + ldhdr.l_stoff;
691 symbuf = ((coff_symbol_type *)
692 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
696 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
697 elsymend = elsym + ldhdr.l_nsyms;
698 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
700 struct internal_ldsym ldsym;
702 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
704 symbuf->symbol.the_bfd = abfd;
706 if (ldsym._l._l_l._l_zeroes == 0)
707 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
712 for (i = 0; i < SYMNMLEN; i++)
713 if (ldsym._l._l_name[i] == '\0')
716 symbuf->symbol.name = (char *) elsym->_l._l_name;
721 c = bfd_alloc (abfd, SYMNMLEN + 1);
724 memcpy (c, ldsym._l._l_name, SYMNMLEN);
726 symbuf->symbol.name = c;
730 if (ldsym.l_smclas == XMC_XO)
731 symbuf->symbol.section = bfd_abs_section_ptr;
733 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
735 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
737 symbuf->symbol.flags = BSF_NO_FLAGS;
738 if ((ldsym.l_smtype & L_EXPORT) != 0)
739 symbuf->symbol.flags |= BSF_GLOBAL;
741 /* FIXME: We have no way to record the other information stored
742 with the loader symbol. */
744 *psyms = (asymbol *) symbuf;
749 return ldhdr.l_nsyms;
752 /* Get the size required to hold the dynamic relocs. */
755 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
760 struct internal_ldhdr ldhdr;
762 if ((abfd->flags & DYNAMIC) == 0)
764 bfd_set_error (bfd_error_invalid_operation);
768 lsec = bfd_get_section_by_name (abfd, ".loader");
771 bfd_set_error (bfd_error_no_symbols);
775 if (! xcoff_get_section_contents (abfd, lsec))
777 contents = coff_section_data (abfd, lsec)->contents;
779 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
781 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
784 /* The typical dynamic reloc. */
786 static reloc_howto_type xcoff_dynamic_reloc =
789 2, /* size (0 = byte, 1 = short, 2 = long) */
791 false, /* pc_relative */
793 complain_overflow_bitfield, /* complain_on_overflow */
794 0, /* special_function */
796 true, /* partial_inplace */
797 0xffffffff, /* src_mask */
798 0xffffffff, /* dst_mask */
799 false); /* pcrel_offset */
801 /* Get the dynamic relocs. */
804 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
811 struct internal_ldhdr ldhdr;
813 struct external_ldrel *elrel, *elrelend;
815 if ((abfd->flags & DYNAMIC) == 0)
817 bfd_set_error (bfd_error_invalid_operation);
821 lsec = bfd_get_section_by_name (abfd, ".loader");
824 bfd_set_error (bfd_error_no_symbols);
828 if (! xcoff_get_section_contents (abfd, lsec))
830 contents = coff_section_data (abfd, lsec)->contents;
832 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
834 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
838 elrel = ((struct external_ldrel *)
839 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
840 elrelend = elrel + ldhdr.l_nreloc;
841 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
843 struct internal_ldrel ldrel;
845 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
847 if (ldrel.l_symndx >= 3)
848 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
854 switch (ldrel.l_symndx)
870 sec = bfd_get_section_by_name (abfd, name);
873 bfd_set_error (bfd_error_bad_value);
877 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
880 relbuf->address = ldrel.l_vaddr;
883 /* Most dynamic relocs have the same type. FIXME: This is only
884 correct if ldrel.l_rtype == 0. In other cases, we should use
885 a different howto. */
886 relbuf->howto = &xcoff_dynamic_reloc;
888 /* FIXME: We have no way to record the l_rsecnm field. */
895 return ldhdr.l_nreloc;
898 /* Routine to create an entry in an XCOFF link hash table. */
900 static struct bfd_hash_entry *
901 xcoff_link_hash_newfunc (entry, table, string)
902 struct bfd_hash_entry *entry;
903 struct bfd_hash_table *table;
906 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
908 /* Allocate the structure if it has not already been allocated by a
910 if (ret == (struct xcoff_link_hash_entry *) NULL)
911 ret = ((struct xcoff_link_hash_entry *)
912 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
913 if (ret == (struct xcoff_link_hash_entry *) NULL)
914 return (struct bfd_hash_entry *) ret;
916 /* Call the allocation method of the superclass. */
917 ret = ((struct xcoff_link_hash_entry *)
918 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
922 /* Set local fields. */
924 ret->toc_section = NULL;
925 ret->u.toc_indx = -1;
926 ret->descriptor = NULL;
930 ret->smclas = XMC_UA;
933 return (struct bfd_hash_entry *) ret;
936 /* Create a XCOFF link hash table. */
938 struct bfd_link_hash_table *
939 _bfd_xcoff_bfd_link_hash_table_create (abfd)
942 struct xcoff_link_hash_table *ret;
944 ret = ((struct xcoff_link_hash_table *)
945 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
946 if (ret == (struct xcoff_link_hash_table *) NULL)
947 return (struct bfd_link_hash_table *) NULL;
948 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
950 bfd_release (abfd, ret);
951 return (struct bfd_link_hash_table *) NULL;
954 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
955 ret->debug_section = NULL;
956 ret->loader_section = NULL;
957 ret->ldrel_count = 0;
958 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
959 ret->linkage_section = NULL;
960 ret->toc_section = NULL;
961 ret->descriptor_section = NULL;
966 memset (ret->special_sections, 0, sizeof ret->special_sections);
968 /* The linker will always generate a full a.out header. We need to
969 record that fact now, before the sizeof_headers routine could be
971 xcoff_data (abfd)->full_aouthdr = true;
976 /* Look up an entry in an XCOFF link hash table. */
978 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
979 ((struct xcoff_link_hash_entry *) \
980 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
983 /* Traverse an XCOFF link hash table. */
985 #define xcoff_link_hash_traverse(table, func, info) \
986 (bfd_link_hash_traverse \
988 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
991 /* Get the XCOFF link hash table from the info structure. This is
994 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
996 /* Read internal relocs for an XCOFF csect. This is a wrapper around
997 _bfd_coff_read_internal_relocs which tries to take advantage of any
998 relocs which may have been cached for the enclosing section. */
1000 static struct internal_reloc *
1001 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
1002 require_internal, internal_relocs)
1006 bfd_byte *external_relocs;
1007 boolean require_internal;
1008 struct internal_reloc *internal_relocs;
1010 if (coff_section_data (abfd, sec) != NULL
1011 && coff_section_data (abfd, sec)->relocs == NULL
1012 && xcoff_section_data (abfd, sec) != NULL)
1014 asection *enclosing;
1016 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1018 if (enclosing != NULL
1019 && (coff_section_data (abfd, enclosing) == NULL
1020 || coff_section_data (abfd, enclosing)->relocs == NULL)
1022 && enclosing->reloc_count > 0)
1024 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1025 external_relocs, false,
1026 (struct internal_reloc *) NULL)
1031 if (enclosing != NULL
1032 && coff_section_data (abfd, enclosing) != NULL
1033 && coff_section_data (abfd, enclosing)->relocs != NULL)
1037 off = ((sec->rel_filepos - enclosing->rel_filepos)
1038 / bfd_coff_relsz (abfd));
1039 if (! require_internal)
1040 return coff_section_data (abfd, enclosing)->relocs + off;
1041 memcpy (internal_relocs,
1042 coff_section_data (abfd, enclosing)->relocs + off,
1043 sec->reloc_count * sizeof (struct internal_reloc));
1044 return internal_relocs;
1048 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1049 require_internal, internal_relocs);
1052 /* Given an XCOFF BFD, add symbols to the global hash table as
1056 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
1058 struct bfd_link_info *info;
1060 switch (bfd_get_format (abfd))
1063 return xcoff_link_add_object_symbols (abfd, info);
1066 /* If the archive has a map, do the usual search. We then need
1067 to check the archive for stripped dynamic objects, because
1068 they will not appear in the archive map even though they
1069 should, perhaps, be included. If the archive has no map, we
1070 just consider each object file in turn, since that apparently
1071 is what the AIX native linker does. */
1072 if (bfd_has_map (abfd))
1074 if (! (_bfd_generic_link_add_archive_symbols
1075 (abfd, info, xcoff_link_check_archive_element)))
1082 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1083 while (member != NULL)
1085 if (bfd_check_format (member, bfd_object)
1086 && (! bfd_has_map (abfd)
1087 || ((member->flags & DYNAMIC) != 0
1088 && (member->flags & HAS_SYMS) == 0)))
1092 if (! xcoff_link_check_archive_element (member, info, &needed))
1095 member->archive_pass = -1;
1097 member = bfd_openr_next_archived_file (abfd, member);
1104 bfd_set_error (bfd_error_wrong_format);
1109 /* Add symbols from an XCOFF object file. */
1112 xcoff_link_add_object_symbols (abfd, info)
1114 struct bfd_link_info *info;
1116 if (! _bfd_coff_get_external_symbols (abfd))
1118 if (! xcoff_link_add_symbols (abfd, info))
1120 if (! info->keep_memory)
1122 if (! _bfd_coff_free_symbols (abfd))
1128 /* Check a single archive element to see if we need to include it in
1129 the link. *PNEEDED is set according to whether this element is
1130 needed in the link or not. This is called via
1131 _bfd_generic_link_add_archive_symbols. */
1134 xcoff_link_check_archive_element (abfd, info, pneeded)
1136 struct bfd_link_info *info;
1139 if (! _bfd_coff_get_external_symbols (abfd))
1142 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1147 if (! xcoff_link_add_symbols (abfd, info))
1151 if (! info->keep_memory || ! *pneeded)
1153 if (! _bfd_coff_free_symbols (abfd))
1160 /* Look through the symbols to see if this object file should be
1161 included in the link. */
1164 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1166 struct bfd_link_info *info;
1169 bfd_size_type symesz;
1175 if ((abfd->flags & DYNAMIC) != 0
1176 && ! info->static_link
1177 && info->hash->creator == abfd->xvec)
1178 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1180 symesz = bfd_coff_symesz (abfd);
1181 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1182 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1183 while (esym < esym_end)
1185 struct internal_syment sym;
1187 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1189 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1192 char buf[SYMNMLEN + 1];
1193 struct bfd_link_hash_entry *h;
1195 /* This symbol is externally visible, and is defined by this
1198 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1201 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1203 /* We are only interested in symbols that are currently
1204 undefined. If a symbol is currently known to be common,
1205 XCOFF linkers do not bring in an object file which
1206 defines it. We also don't bring in symbols to satisfy
1207 undefined references in shared objects. */
1208 if (h != (struct bfd_link_hash_entry *) NULL
1209 && h->type == bfd_link_hash_undefined
1210 && (info->hash->creator != abfd->xvec
1211 || (((struct xcoff_link_hash_entry *) h)->flags
1212 & XCOFF_DEF_DYNAMIC) == 0))
1214 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1221 esym += (sym.n_numaux + 1) * symesz;
1224 /* We do not need this object file. */
1228 /* Look through the loader symbols to see if this dynamic object
1229 should be included in the link. The native linker uses the loader
1230 symbols, not the normal symbol table, so we do too. */
1233 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1235 struct bfd_link_info *info;
1240 struct internal_ldhdr ldhdr;
1241 const char *strings;
1242 struct external_ldsym *elsym, *elsymend;
1246 lsec = bfd_get_section_by_name (abfd, ".loader");
1249 /* There are no symbols, so don't try to include it. */
1253 if (! xcoff_get_section_contents (abfd, lsec))
1255 buf = coff_section_data (abfd, lsec)->contents;
1257 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1259 strings = (char *) buf + ldhdr.l_stoff;
1261 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1262 elsymend = elsym + ldhdr.l_nsyms;
1263 for (; elsym < elsymend; elsym++)
1265 struct internal_ldsym ldsym;
1266 char nambuf[SYMNMLEN + 1];
1268 struct bfd_link_hash_entry *h;
1270 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1272 /* We are only interested in exported symbols. */
1273 if ((ldsym.l_smtype & L_EXPORT) == 0)
1276 if (ldsym._l._l_l._l_zeroes == 0)
1277 name = strings + ldsym._l._l_l._l_offset;
1280 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1281 nambuf[SYMNMLEN] = '\0';
1285 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1287 /* We are only interested in symbols that are currently
1288 undefined. At this point we know that we are using an XCOFF
1291 && h->type == bfd_link_hash_undefined
1292 && (((struct xcoff_link_hash_entry *) h)->flags
1293 & XCOFF_DEF_DYNAMIC) == 0)
1295 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1302 /* We do not need this shared object. */
1304 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1306 free (coff_section_data (abfd, lsec)->contents);
1307 coff_section_data (abfd, lsec)->contents = NULL;
1313 /* Returns the index of reloc in RELOCS with the least address greater
1314 than or equal to ADDRESS. The relocs are sorted by address. */
1316 static bfd_size_type
1317 xcoff_find_reloc (relocs, count, address)
1318 struct internal_reloc *relocs;
1319 bfd_size_type count;
1322 bfd_size_type min, max, this;
1326 if (count == 1 && relocs[0].r_vaddr < address)
1335 /* Do a binary search over (min,max]. */
1336 while (min + 1 < max)
1340 this = (max + min) / 2;
1341 raddr = relocs[this].r_vaddr;
1342 if (raddr > address)
1344 else if (raddr < address)
1353 if (relocs[min].r_vaddr < address)
1357 && relocs[min - 1].r_vaddr == address)
1363 /* Add all the symbols from an object file to the hash table.
1365 XCOFF is a weird format. A normal XCOFF .o files will have three
1366 COFF sections--.text, .data, and .bss--but each COFF section will
1367 contain many csects. These csects are described in the symbol
1368 table. From the linker's point of view, each csect must be
1369 considered a section in its own right. For example, a TOC entry is
1370 handled as a small XMC_TC csect. The linker must be able to merge
1371 different TOC entries together, which means that it must be able to
1372 extract the XMC_TC csects from the .data section of the input .o
1375 From the point of view of our linker, this is, of course, a hideous
1376 nightmare. We cope by actually creating sections for each csect,
1377 and discarding the original sections. We then have to handle the
1378 relocation entries carefully, since the only way to tell which
1379 csect they belong to is to examine the address. */
1382 xcoff_link_add_symbols (abfd, info)
1384 struct bfd_link_info *info;
1386 unsigned int n_tmask;
1387 unsigned int n_btshft;
1388 boolean default_copy;
1389 bfd_size_type symcount;
1390 struct xcoff_link_hash_entry **sym_hash;
1391 asection **csect_cache;
1392 bfd_size_type linesz;
1394 asection *last_real;
1397 unsigned int csect_index;
1398 asection *first_csect;
1399 bfd_size_type symesz;
1402 struct reloc_info_struct
1404 struct internal_reloc *relocs;
1407 } *reloc_info = NULL;
1409 keep_syms = obj_coff_keep_syms (abfd);
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
1474 && info->strip != strip_all)
1478 dsec = bfd_make_section_anyway (abfd, ".debug");
1481 xcoff_hash_table (info)->debug_section = dsec;
1482 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1486 if ((abfd->flags & DYNAMIC) != 0
1487 && ! info->static_link)
1490 n_tmask = coff_data (abfd)->local_n_tmask;
1491 n_btshft = coff_data (abfd)->local_n_btshft;
1493 /* Define macros so that ISFCN, et. al., macros work correctly. */
1494 #define N_TMASK n_tmask
1495 #define N_BTSHFT n_btshft
1497 if (info->keep_memory)
1498 default_copy = false;
1500 default_copy = true;
1502 symcount = obj_raw_syment_count (abfd);
1504 /* We keep a list of the linker hash table entries that correspond
1505 to each external symbol. */
1506 sym_hash = ((struct xcoff_link_hash_entry **)
1509 * sizeof (struct xcoff_link_hash_entry *))));
1510 if (sym_hash == NULL && symcount != 0)
1512 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1513 memset (sym_hash, 0,
1514 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1516 /* Because of the weird stuff we are doing with XCOFF csects, we can
1517 not easily determine which section a symbol is in, so we store
1518 the information in the tdata for the input file. */
1519 csect_cache = ((asection **)
1520 bfd_alloc (abfd, symcount * sizeof (asection *)));
1521 if (csect_cache == NULL && symcount != 0)
1523 xcoff_data (abfd)->csects = csect_cache;
1524 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1526 /* While splitting sections into csects, we need to assign the
1527 relocs correctly. The relocs and the csects must both be in
1528 order by VMA within a given section, so we handle this by
1529 scanning along the relocs as we process the csects. We index
1530 into reloc_info using the section target_index. */
1531 reloc_info = ((struct reloc_info_struct *)
1532 bfd_malloc ((abfd->section_count + 1)
1533 * sizeof (struct reloc_info_struct)));
1534 if (reloc_info == NULL)
1536 memset ((PTR) reloc_info, 0,
1537 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1539 /* Read in the relocs and line numbers for each section. */
1540 linesz = bfd_coff_linesz (abfd);
1542 for (o = abfd->sections; o != NULL; o = o->next)
1545 if ((o->flags & SEC_RELOC) != 0)
1547 reloc_info[o->target_index].relocs =
1548 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1549 false, (struct internal_reloc *) NULL);
1550 reloc_info[o->target_index].csects =
1551 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1552 if (reloc_info[o->target_index].csects == NULL)
1554 memset (reloc_info[o->target_index].csects, 0,
1555 o->reloc_count * sizeof (asection *));
1558 if ((info->strip == strip_none || info->strip == strip_some)
1559 && o->lineno_count > 0)
1563 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1564 if (linenos == NULL)
1566 reloc_info[o->target_index].linenos = linenos;
1567 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1568 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1569 != linesz * o->lineno_count))
1574 /* Don't let the linker relocation routines discard the symbols. */
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 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1939 if (csect->used_by_bfd == NULL)
1941 coff_section_data (abfd, csect)->tdata =
1942 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1943 if (coff_section_data (abfd, csect)->tdata == NULL)
1945 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1946 xcoff_section_data (abfd, csect)->lineno_count =
1947 enclosing->lineno_count;
1949 if (enclosing->owner == abfd)
1951 struct internal_reloc *relocs;
1952 bfd_size_type relindx;
1953 struct internal_reloc *rel;
1954 asection **rel_csect;
1956 relocs = reloc_info[enclosing->target_index].relocs;
1957 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1959 rel = relocs + relindx;
1960 rel_csect = (reloc_info[enclosing->target_index].csects
1962 csect->rel_filepos = (enclosing->rel_filepos
1963 + relindx * bfd_coff_relsz (abfd));
1964 while (relindx < enclosing->reloc_count
1965 && *rel_csect == NULL
1966 && rel->r_vaddr < csect->vma + csect->_raw_size)
1969 csect->flags |= SEC_RELOC;
1970 ++csect->reloc_count;
1977 /* There are a number of other fields and section flags
1978 which we do not bother to set. */
1980 csect_index = ((esym
1981 - (bfd_byte *) obj_coff_external_syms (abfd))
1984 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1986 if (first_csect == NULL)
1987 first_csect = csect;
1989 /* If this symbol is C_EXT, we treat it as starting at the
1990 beginning of the newly created section. */
1991 if (sym.n_sclass == C_EXT)
1997 /* If this is a TOC section for a symbol, record it. */
1998 if (set_toc != NULL)
1999 set_toc->toc_section = csect;
2004 /* This is a label definition. The x_scnlen field is the
2005 symbol index of the csect. I believe that this must
2006 always follow the appropriate XTY_SD symbol, so I will
2012 if (aux.x_csect.x_scnlen.l < 0
2013 || (aux.x_csect.x_scnlen.l
2014 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2018 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2020 || (section->flags & SEC_HAS_CONTENTS) == 0)
2025 (*_bfd_error_handler)
2026 (_("%s: misplaced XTY_LD `%s'"),
2027 bfd_get_filename (abfd), name);
2028 bfd_set_error (bfd_error_bad_value);
2032 value = sym.n_value - csect->vma;
2037 /* This is an unitialized csect. We could base the name on
2038 the storage mapping class, but we don't bother except for
2039 an XMC_TD symbol. If this csect is externally visible,
2040 it is a common symbol. We put XMC_TD symbols in sections
2041 named .tocbss, and rely on the linker script to put that
2046 xcoff_section_data (abfd, csect)->last_symndx =
2048 - (bfd_byte *) obj_coff_external_syms (abfd))
2052 if (aux.x_csect.x_smclas == XMC_TD)
2053 csect = bfd_make_section_anyway (abfd, ".tocbss");
2055 csect = bfd_make_section_anyway (abfd, ".bss");
2058 csect->vma = sym.n_value;
2059 csect->_raw_size = aux.x_csect.x_scnlen.l;
2060 csect->flags |= SEC_ALLOC;
2061 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2062 /* There are a number of other fields and section flags
2063 which we do not bother to set. */
2065 csect_index = ((esym
2066 - (bfd_byte *) obj_coff_external_syms (abfd))
2069 csect->used_by_bfd =
2070 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
2071 if (csect->used_by_bfd == NULL)
2073 coff_section_data (abfd, csect)->tdata =
2074 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2075 if (coff_section_data (abfd, csect)->tdata == NULL)
2077 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2079 if (first_csect == NULL)
2080 first_csect = csect;
2082 if (sym.n_sclass == C_EXT)
2084 csect->flags |= SEC_IS_COMMON;
2085 csect->_raw_size = 0;
2087 value = aux.x_csect.x_scnlen.l;
2093 /* Check for magic symbol names. */
2094 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2095 && aux.x_csect.x_smclas != XMC_TC
2096 && aux.x_csect.x_smclas != XMC_TD)
2103 if (strcmp (name, "_text") == 0)
2105 else if (strcmp (name, "_etext") == 0)
2107 else if (strcmp (name, "_data") == 0)
2109 else if (strcmp (name, "_edata") == 0)
2111 else if (strcmp (name, "_end") == 0)
2114 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2118 xcoff_hash_table (info)->special_sections[i] = csect;
2121 /* Now we have enough information to add the symbol to the
2122 linker hash table. */
2124 if (sym.n_sclass == C_EXT)
2128 BFD_ASSERT (section != NULL);
2130 /* We must copy the name into memory if we got it from the
2131 syment itself, rather than the string table. */
2132 copy = default_copy;
2133 if (sym._n._n_n._n_zeroes != 0
2134 || sym._n._n_n._n_offset == 0)
2137 /* The AIX linker appears to only detect multiple symbol
2138 definitions when there is a reference to the symbol. If
2139 a symbol is defined multiple times, and the only
2140 references are from the same object file, the AIX linker
2141 appears to permit it. It does not merge the different
2142 definitions, but handles them independently. On the
2143 other hand, if there is a reference, the linker reports
2146 This matters because the AIX <net/net_globals.h> header
2147 file actually defines an initialized array, so we have to
2148 actually permit that to work.
2150 Just to make matters even more confusing, the AIX linker
2151 appears to permit multiple symbol definitions whenever
2152 the second definition is in an archive rather than an
2153 object file. This may be a consequence of the manner in
2154 which it handles archives: I think it may load the entire
2155 archive in as separate csects, and then let garbage
2156 collection discard symbols.
2158 We also have to handle the case of statically linking a
2159 shared object, which will cause symbol redefinitions,
2160 although this is an easier case to detect. */
2162 if (info->hash->creator == abfd->xvec)
2164 if (! bfd_is_und_section (section))
2165 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2166 name, true, copy, false);
2168 *sym_hash = ((struct xcoff_link_hash_entry *)
2169 bfd_wrapped_link_hash_lookup (abfd, info, name,
2170 true, copy, false));
2171 if (*sym_hash == NULL)
2173 if (((*sym_hash)->root.type == bfd_link_hash_defined
2174 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2175 && ! bfd_is_und_section (section)
2176 && ! bfd_is_com_section (section))
2178 /* This is a second definition of a defined symbol. */
2179 if ((abfd->flags & DYNAMIC) != 0
2180 && ((*sym_hash)->smclas != XMC_GL
2181 || aux.x_csect.x_smclas == XMC_GL
2182 || ((*sym_hash)->root.u.def.section->owner->flags
2185 /* The new symbol is from a shared library, and
2186 either the existing symbol is not global
2187 linkage code or this symbol is global linkage
2188 code. If the existing symbol is global
2189 linkage code and the new symbol is not, then
2190 we want to use the new symbol. */
2191 section = bfd_und_section_ptr;
2194 else if (((*sym_hash)->root.u.def.section->owner->flags
2197 /* The existing symbol is from a shared library.
2199 (*sym_hash)->root.type = bfd_link_hash_undefined;
2200 (*sym_hash)->root.u.undef.abfd =
2201 (*sym_hash)->root.u.def.section->owner;
2203 else if (abfd->my_archive != NULL)
2205 /* This is a redefinition in an object contained
2206 in an archive. Just ignore it. See the
2208 section = bfd_und_section_ptr;
2211 else if ((*sym_hash)->root.next != NULL
2212 || info->hash->undefs_tail == &(*sym_hash)->root)
2214 /* This symbol has been referenced. In this
2215 case, we just continue and permit the
2216 multiple definition error. See the comment
2217 above about the behaviour of the AIX linker. */
2219 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2221 /* The symbols are both csects of the same
2222 class. There is at least a chance that this
2223 is a semi-legitimate redefinition. */
2224 section = bfd_und_section_ptr;
2226 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2229 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2230 && ((*sym_hash)->root.type == bfd_link_hash_defined
2231 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2232 && (bfd_is_und_section (section)
2233 || bfd_is_com_section (section)))
2235 /* This is a reference to a multiply defined symbol.
2236 Report the error now. See the comment above
2237 about the behaviour of the AIX linker. We could
2238 also do this with warning symbols, but I'm not
2239 sure the XCOFF linker is wholly prepared to
2240 handle them, and that would only be a warning,
2242 if (! ((*info->callbacks->multiple_definition)
2243 (info, (*sym_hash)->root.root.string,
2244 (bfd *) NULL, (asection *) NULL, 0,
2245 (*sym_hash)->root.u.def.section->owner,
2246 (*sym_hash)->root.u.def.section,
2247 (*sym_hash)->root.u.def.value)))
2249 /* Try not to give this error too many times. */
2250 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2254 /* _bfd_generic_link_add_one_symbol may call the linker to
2255 generate an error message, and the linker may try to read
2256 the symbol table to give a good error. Right now, the
2257 line numbers are in an inconsistent state, since they are
2258 counted both in the real sections and in the new csects.
2259 We need to leave the count in the real sections so that
2260 the linker can report the line number of the error
2261 correctly, so temporarily clobber the link to the csects
2262 so that the linker will not try to read the line numbers
2263 a second time from the csects. */
2264 BFD_ASSERT (last_real->next == first_csect);
2265 last_real->next = NULL;
2266 if (! (_bfd_generic_link_add_one_symbol
2267 (info, abfd, name, flags, section, value,
2268 (const char *) NULL, copy, true,
2269 (struct bfd_link_hash_entry **) sym_hash)))
2271 last_real->next = first_csect;
2273 if (smtyp == XTY_CM)
2275 if ((*sym_hash)->root.type != bfd_link_hash_common
2276 || (*sym_hash)->root.u.c.p->section != csect)
2278 /* We don't need the common csect we just created. */
2279 csect->_raw_size = 0;
2283 (*sym_hash)->root.u.c.p->alignment_power
2284 = csect->alignment_power;
2288 if (info->hash->creator == abfd->xvec)
2292 if (smtyp == XTY_ER || smtyp == XTY_CM)
2293 flag = XCOFF_REF_REGULAR;
2295 flag = XCOFF_DEF_REGULAR;
2296 (*sym_hash)->flags |= flag;
2298 if ((*sym_hash)->smclas == XMC_UA
2299 || flag == XCOFF_DEF_REGULAR)
2300 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2304 *csect_cache = csect;
2306 esym += (sym.n_numaux + 1) * symesz;
2307 sym_hash += sym.n_numaux + 1;
2308 csect_cache += sym.n_numaux + 1;
2311 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2313 /* Make sure that we have seen all the relocs. */
2314 for (o = abfd->sections; o != first_csect; o = o->next)
2316 /* Reset the section size and the line number count, since the
2317 data is now attached to the csects. Don't reset the size of
2318 the .debug section, since we need to read it below in
2319 bfd_xcoff_size_dynamic_sections. */
2320 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2322 o->lineno_count = 0;
2324 if ((o->flags & SEC_RELOC) != 0)
2327 struct internal_reloc *rel;
2328 asection **rel_csect;
2330 rel = reloc_info[o->target_index].relocs;
2331 rel_csect = reloc_info[o->target_index].csects;
2332 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2334 if (*rel_csect == NULL)
2336 (*_bfd_error_handler)
2337 (_("%s: reloc %s:%d not in csect"),
2338 bfd_get_filename (abfd), o->name, i);
2339 bfd_set_error (bfd_error_bad_value);
2343 /* We identify all symbols which are called, so that we
2344 can create glue code for calls to functions imported
2345 from dynamic objects. */
2346 if (info->hash->creator == abfd->xvec
2347 && *rel_csect != bfd_und_section_ptr
2348 && (rel->r_type == R_BR
2349 || rel->r_type == R_RBR)
2350 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2352 struct xcoff_link_hash_entry *h;
2354 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2355 h->flags |= XCOFF_CALLED;
2356 /* If the symbol name starts with a period, it is
2357 the code of a function. If the symbol is
2358 currently undefined, then add an undefined symbol
2359 for the function descriptor. This should do no
2360 harm, because any regular object that defines the
2361 function should also define the function
2362 descriptor. It helps, because it means that we
2363 will identify the function descriptor with a
2364 dynamic object if a dynamic object defines it. */
2365 if (h->root.root.string[0] == '.'
2366 && h->descriptor == NULL)
2368 struct xcoff_link_hash_entry *hds;
2370 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2371 h->root.root.string + 1,
2375 if (hds->root.type == bfd_link_hash_new)
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info, abfd, hds->root.root.string,
2379 (flagword) 0, bfd_und_section_ptr,
2380 (bfd_vma) 0, (const char *) NULL, false,
2382 (struct bfd_link_hash_entry **) &hds)))
2385 hds->flags |= XCOFF_DESCRIPTOR;
2386 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2387 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2388 hds->descriptor = h;
2389 h->descriptor = hds;
2394 free (reloc_info[o->target_index].csects);
2395 reloc_info[o->target_index].csects = NULL;
2397 /* Reset SEC_RELOC and the reloc_count, since the reloc
2398 information is now attached to the csects. */
2399 o->flags &=~ SEC_RELOC;
2402 /* If we are not keeping memory, free the reloc information. */
2403 if (! info->keep_memory
2404 && coff_section_data (abfd, o) != NULL
2405 && coff_section_data (abfd, o)->relocs != NULL
2406 && ! coff_section_data (abfd, o)->keep_relocs)
2408 free (coff_section_data (abfd, o)->relocs);
2409 coff_section_data (abfd, o)->relocs = NULL;
2413 /* Free up the line numbers. FIXME: We could cache these
2414 somewhere for the final link, to avoid reading them again. */
2415 if (reloc_info[o->target_index].linenos != NULL)
2417 free (reloc_info[o->target_index].linenos);
2418 reloc_info[o->target_index].linenos = NULL;
2424 obj_coff_keep_syms (abfd) = keep_syms;
2429 if (reloc_info != NULL)
2431 for (o = abfd->sections; o != NULL; o = o->next)
2433 if (reloc_info[o->target_index].csects != NULL)
2434 free (reloc_info[o->target_index].csects);
2435 if (reloc_info[o->target_index].linenos != NULL)
2436 free (reloc_info[o->target_index].linenos);
2440 obj_coff_keep_syms (abfd) = keep_syms;
2447 /* This function is used to add symbols from a dynamic object to the
2448 global symbol table. */
2451 xcoff_link_add_dynamic_symbols (abfd, info)
2453 struct bfd_link_info *info;
2457 struct internal_ldhdr ldhdr;
2458 const char *strings;
2459 struct external_ldsym *elsym, *elsymend;
2460 struct xcoff_import_file *n;
2465 struct xcoff_import_file **pp;
2467 /* We can only handle a dynamic object if we are generating an XCOFF
2469 if (info->hash->creator != abfd->xvec)
2471 (*_bfd_error_handler)
2472 (_("%s: XCOFF shared object when not producing XCOFF output"),
2473 bfd_get_filename (abfd));
2474 bfd_set_error (bfd_error_invalid_operation);
2478 /* The symbols we use from a dynamic object are not the symbols in
2479 the normal symbol table, but, rather, the symbols in the export
2480 table. If there is a global symbol in a dynamic object which is
2481 not in the export table, the loader will not be able to find it,
2482 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2483 libc.a has symbols in the export table which are not in the
2486 /* Read in the .loader section. FIXME: We should really use the
2487 o_snloader field in the a.out header, rather than grabbing the
2489 lsec = bfd_get_section_by_name (abfd, ".loader");
2492 (*_bfd_error_handler)
2493 (_("%s: dynamic object with no .loader section"),
2494 bfd_get_filename (abfd));
2495 bfd_set_error (bfd_error_no_symbols);
2499 if (! xcoff_get_section_contents (abfd, lsec))
2501 buf = coff_section_data (abfd, lsec)->contents;
2503 /* Remove the sections from this object, so that they do not get
2504 included in the link. */
2505 abfd->sections = NULL;
2507 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2509 strings = (char *) buf + ldhdr.l_stoff;
2511 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2512 elsymend = elsym + ldhdr.l_nsyms;
2513 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2514 for (; elsym < elsymend; elsym++)
2516 struct internal_ldsym ldsym;
2517 char nambuf[SYMNMLEN + 1];
2519 struct xcoff_link_hash_entry *h;
2521 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2523 /* We are only interested in exported symbols. */
2524 if ((ldsym.l_smtype & L_EXPORT) == 0)
2527 if (ldsym._l._l_l._l_zeroes == 0)
2528 name = strings + ldsym._l._l_l._l_offset;
2531 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2532 nambuf[SYMNMLEN] = '\0';
2536 /* Normally we could not call xcoff_link_hash_lookup in an add
2537 symbols routine, since we might not be using an XCOFF hash
2538 table. However, we verified above that we are using an XCOFF
2541 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2546 h->flags |= XCOFF_DEF_DYNAMIC;
2548 /* If the symbol is undefined, and the BFD it was found in is
2549 not a dynamic object, change the BFD to this dynamic object,
2550 so that we can get the correct import file ID. */
2551 if ((h->root.type == bfd_link_hash_undefined
2552 || h->root.type == bfd_link_hash_undefweak)
2553 && (h->root.u.undef.abfd == NULL
2554 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2555 h->root.u.undef.abfd = abfd;
2557 if (h->root.type == bfd_link_hash_new)
2559 h->root.type = bfd_link_hash_undefined;
2560 h->root.u.undef.abfd = abfd;
2561 /* We do not want to add this to the undefined symbol list. */
2564 if (h->smclas == XMC_UA
2565 || h->root.type == bfd_link_hash_undefined
2566 || h->root.type == bfd_link_hash_undefweak)
2567 h->smclas = ldsym.l_smclas;
2569 /* Unless this is an XMC_XO symbol, we don't bother to actually
2570 define it, since we don't have a section to put it in anyhow.
2571 Instead, the relocation routines handle the DEF_DYNAMIC flag
2574 if (h->smclas == XMC_XO
2575 && (h->root.type == bfd_link_hash_undefined
2576 || h->root.type == bfd_link_hash_undefweak))
2578 /* This symbol has an absolute value. */
2579 h->root.type = bfd_link_hash_defined;
2580 h->root.u.def.section = bfd_abs_section_ptr;
2581 h->root.u.def.value = ldsym.l_value;
2584 /* If this symbol defines a function descriptor, then it
2585 implicitly defines the function code as well. */
2586 if (h->smclas == XMC_DS
2587 || (h->smclas == XMC_XO && name[0] != '.'))
2588 h->flags |= XCOFF_DESCRIPTOR;
2589 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2591 struct xcoff_link_hash_entry *hds;
2593 hds = h->descriptor;
2598 dsnm = bfd_malloc (strlen (name) + 2);
2602 strcpy (dsnm + 1, name);
2603 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2609 if (hds->root.type == bfd_link_hash_new)
2611 hds->root.type = bfd_link_hash_undefined;
2612 hds->root.u.undef.abfd = abfd;
2613 /* We do not want to add this to the undefined
2617 hds->descriptor = h;
2618 h->descriptor = hds;
2621 hds->flags |= XCOFF_DEF_DYNAMIC;
2622 if (hds->smclas == XMC_UA)
2623 hds->smclas = XMC_PR;
2625 /* An absolute symbol appears to actually define code, not a
2626 function descriptor. This is how some math functions are
2627 implemented on AIX 4.1. */
2628 if (h->smclas == XMC_XO
2629 && (hds->root.type == bfd_link_hash_undefined
2630 || hds->root.type == bfd_link_hash_undefweak))
2632 hds->smclas = XMC_XO;
2633 hds->root.type = bfd_link_hash_defined;
2634 hds->root.u.def.section = bfd_abs_section_ptr;
2635 hds->root.u.def.value = ldsym.l_value;
2640 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2642 free (coff_section_data (abfd, lsec)->contents);
2643 coff_section_data (abfd, lsec)->contents = NULL;
2646 /* Record this file in the import files. */
2648 n = ((struct xcoff_import_file *)
2649 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2654 /* For some reason, the path entry in the import file list for a
2655 shared object appears to always be empty. The file name is the
2658 if (abfd->my_archive == NULL)
2660 bname = bfd_get_filename (abfd);
2665 bname = bfd_get_filename (abfd->my_archive);
2666 mname = bfd_get_filename (abfd);
2668 s = strrchr (bname, '/');
2674 /* We start c at 1 because the first import file number is reserved
2676 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2678 pp = &(*pp)->next, ++c)
2682 xcoff_data (abfd)->import_file_id = c;
2687 /* Routines that are called after all the input files have been
2688 handled, but before the sections are laid out in memory. */
2690 /* Mark a symbol as not being garbage, including the section in which
2693 static INLINE boolean
2694 xcoff_mark_symbol (info, h)
2695 struct bfd_link_info *info;
2696 struct xcoff_link_hash_entry *h;
2698 if ((h->flags & XCOFF_MARK) != 0)
2701 h->flags |= XCOFF_MARK;
2702 if (h->root.type == bfd_link_hash_defined
2703 || h->root.type == bfd_link_hash_defweak)
2707 hsec = h->root.u.def.section;
2708 if (! bfd_is_abs_section (hsec)
2709 && (hsec->flags & SEC_MARK) == 0)
2711 if (! xcoff_mark (info, hsec))
2716 if (h->toc_section != NULL
2717 && (h->toc_section->flags & SEC_MARK) == 0)
2719 if (! xcoff_mark (info, h->toc_section))
2726 /* The mark phase of garbage collection. For a given section, mark
2727 it, and all the sections which define symbols to which it refers.
2728 Because this function needs to look at the relocs, we also count
2729 the number of relocs which need to be copied into the .loader
2733 xcoff_mark (info, sec)
2734 struct bfd_link_info *info;
2737 if (bfd_is_abs_section (sec)
2738 || (sec->flags & SEC_MARK) != 0)
2741 sec->flags |= SEC_MARK;
2743 if (sec->owner->xvec == info->hash->creator
2744 && coff_section_data (sec->owner, sec) != NULL
2745 && xcoff_section_data (sec->owner, sec) != NULL)
2747 register struct xcoff_link_hash_entry **hp, **hpend;
2748 struct internal_reloc *rel, *relend;
2750 /* Mark all the symbols in this section. */
2752 hp = (obj_xcoff_sym_hashes (sec->owner)
2753 + xcoff_section_data (sec->owner, sec)->first_symndx);
2754 hpend = (obj_xcoff_sym_hashes (sec->owner)
2755 + xcoff_section_data (sec->owner, sec)->last_symndx);
2756 for (; hp < hpend; hp++)
2758 register struct xcoff_link_hash_entry *h;
2762 && (h->flags & XCOFF_MARK) == 0)
2764 if (! xcoff_mark_symbol (info, h))
2769 /* Look through the section relocs. */
2771 if ((sec->flags & SEC_RELOC) != 0
2772 && sec->reloc_count > 0)
2774 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2775 (bfd_byte *) NULL, false,
2776 (struct internal_reloc *) NULL);
2779 relend = rel + sec->reloc_count;
2780 for (; rel < relend; rel++)
2783 struct xcoff_link_hash_entry *h;
2785 if ((unsigned int) rel->r_symndx
2786 > obj_raw_syment_count (sec->owner))
2789 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2791 && (h->flags & XCOFF_MARK) == 0)
2793 if (! xcoff_mark_symbol (info, h))
2797 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2799 && (rsec->flags & SEC_MARK) == 0)
2801 if (! xcoff_mark (info, rsec))
2805 /* See if this reloc needs to be copied into the .loader
2807 switch (rel->r_type)
2811 || h->root.type == bfd_link_hash_defined
2812 || h->root.type == bfd_link_hash_defweak
2813 || h->root.type == bfd_link_hash_common
2814 || ((h->flags & XCOFF_CALLED) != 0
2815 && (h->root.type == bfd_link_hash_undefined
2816 || h->root.type == bfd_link_hash_undefweak)
2817 && h->root.root.string[0] == '.'
2818 && h->descriptor != NULL
2819 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2820 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2821 && (h->descriptor->flags
2822 & XCOFF_DEF_REGULAR) == 0))))
2829 ++xcoff_hash_table (info)->ldrel_count;
2831 h->flags |= XCOFF_LDREL;
2838 /* We should never need a .loader reloc for a TOC
2844 if (! info->keep_memory
2845 && coff_section_data (sec->owner, sec) != NULL
2846 && coff_section_data (sec->owner, sec)->relocs != NULL
2847 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2849 free (coff_section_data (sec->owner, sec)->relocs);
2850 coff_section_data (sec->owner, sec)->relocs = NULL;
2858 /* The sweep phase of garbage collection. Remove all garbage
2863 struct bfd_link_info *info;
2867 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2871 for (o = sub->sections; o != NULL; o = o->next)
2873 if ((o->flags & SEC_MARK) == 0)
2875 /* Keep all sections from non-XCOFF input files. Keep
2876 special sections. Keep .debug sections for the
2878 if (sub->xvec != info->hash->creator
2879 || o == xcoff_hash_table (info)->debug_section
2880 || o == xcoff_hash_table (info)->loader_section
2881 || o == xcoff_hash_table (info)->linkage_section
2882 || o == xcoff_hash_table (info)->toc_section
2883 || o == xcoff_hash_table (info)->descriptor_section
2884 || strcmp (o->name, ".debug") == 0)
2885 o->flags |= SEC_MARK;
2890 o->lineno_count = 0;
2897 /* Record the number of elements in a set. This is used to output the
2898 correct csect length. */
2901 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2903 struct bfd_link_info *info;
2904 struct bfd_link_hash_entry *harg;
2907 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2908 struct xcoff_link_size_list *n;
2910 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2913 /* This will hardly ever be called. I don't want to burn four bytes
2914 per global symbol, so instead the size is kept on a linked list
2915 attached to the hash table. */
2917 n = ((struct xcoff_link_size_list *)
2918 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2921 n->next = xcoff_hash_table (info)->size_list;
2924 xcoff_hash_table (info)->size_list = n;
2926 h->flags |= XCOFF_HAS_SIZE;
2931 /* Import a symbol. */
2934 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2937 struct bfd_link_info *info;
2938 struct bfd_link_hash_entry *harg;
2940 const char *imppath;
2941 const char *impfile;
2942 const char *impmember;
2944 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2946 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2949 /* A symbol name which starts with a period is the code for a
2950 function. If the symbol is undefined, then add an undefined
2951 symbol for the function descriptor, and import that instead. */
2952 if (h->root.root.string[0] == '.'
2953 && h->root.type == bfd_link_hash_undefined
2954 && val == (bfd_vma) -1)
2956 struct xcoff_link_hash_entry *hds;
2958 hds = h->descriptor;
2961 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2962 h->root.root.string + 1,
2966 if (hds->root.type == bfd_link_hash_new)
2968 hds->root.type = bfd_link_hash_undefined;
2969 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2971 hds->flags |= XCOFF_DESCRIPTOR;
2972 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2973 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2974 hds->descriptor = h;
2975 h->descriptor = hds;
2978 /* Now, if the descriptor is undefined, import the descriptor
2979 rather than the symbol we were told to import. FIXME: Is
2980 this correct in all cases? */
2981 if (hds->root.type == bfd_link_hash_undefined)
2985 h->flags |= XCOFF_IMPORT;
2987 if (val != (bfd_vma) -1)
2989 if (h->root.type == bfd_link_hash_defined
2990 && (! bfd_is_abs_section (h->root.u.def.section)
2991 || h->root.u.def.value != val))
2993 if (! ((*info->callbacks->multiple_definition)
2994 (info, h->root.root.string, h->root.u.def.section->owner,
2995 h->root.u.def.section, h->root.u.def.value,
2996 output_bfd, bfd_abs_section_ptr, val)))
3000 h->root.type = bfd_link_hash_defined;
3001 h->root.u.def.section = bfd_abs_section_ptr;
3002 h->root.u.def.value = val;
3005 /* We overload the ldindx field to hold the l_ifile value for this
3007 BFD_ASSERT (h->ldsym == NULL);
3008 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
3009 if (imppath == NULL)
3014 struct xcoff_import_file **pp;
3016 /* We start c at 1 because the first entry in the import list is
3017 reserved for the library search path. */
3018 for (pp = &xcoff_hash_table (info)->imports, c = 1;
3020 pp = &(*pp)->next, ++c)
3022 if (strcmp ((*pp)->path, imppath) == 0
3023 && strcmp ((*pp)->file, impfile) == 0
3024 && strcmp ((*pp)->member, impmember) == 0)
3030 struct xcoff_import_file *n;
3032 n = ((struct xcoff_import_file *)
3033 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
3039 n->member = impmember;
3049 /* Export a symbol. */
3052 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
3054 struct bfd_link_info *info;
3055 struct bfd_link_hash_entry *harg;
3056 boolean syscall ATTRIBUTE_UNUSED;
3058 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3060 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3063 h->flags |= XCOFF_EXPORT;
3065 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3066 I'm just going to ignore it until somebody explains it. */
3068 /* See if this is a function descriptor. It may be one even though
3069 it is not so marked. */
3070 if ((h->flags & XCOFF_DESCRIPTOR) == 0
3071 && h->root.root.string[0] != '.')
3074 struct xcoff_link_hash_entry *hfn;
3076 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
3080 strcpy (fnname + 1, h->root.root.string);
3081 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
3082 fnname, false, false, true);
3085 && hfn->smclas == XMC_PR
3086 && (hfn->root.type == bfd_link_hash_defined
3087 || hfn->root.type == bfd_link_hash_defweak))
3089 h->flags |= XCOFF_DESCRIPTOR;
3090 h->descriptor = hfn;
3091 hfn->descriptor = h;
3095 /* Make sure we don't garbage collect this symbol. */
3096 if (! xcoff_mark_symbol (info, h))
3099 /* If this is a function descriptor, make sure we don't garbage
3100 collect the associated function code. We normally don't have to
3101 worry about this, because the descriptor will be attached to a
3102 section with relocs, but if we are creating the descriptor
3103 ourselves those relocs will not be visible to the mark code. */
3104 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3106 if (! xcoff_mark_symbol (info, h->descriptor))
3113 /* Count a reloc against a symbol. This is called for relocs
3114 generated by the linker script, typically for global constructors
3118 bfd_xcoff_link_count_reloc (output_bfd, info, name)
3120 struct bfd_link_info *info;
3123 struct xcoff_link_hash_entry *h;
3125 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3128 h = ((struct xcoff_link_hash_entry *)
3129 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3133 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3134 bfd_set_error (bfd_error_no_symbols);
3138 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3139 ++xcoff_hash_table (info)->ldrel_count;
3141 /* Mark the symbol to avoid garbage collection. */
3142 if (! xcoff_mark_symbol (info, h))
3148 /* This function is called for each symbol to which the linker script
3152 bfd_xcoff_record_link_assignment (output_bfd, info, name)
3154 struct bfd_link_info *info;
3157 struct xcoff_link_hash_entry *h;
3159 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3162 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3167 h->flags |= XCOFF_DEF_REGULAR;
3172 /* This structure is used to pass information through
3173 xcoff_link_hash_traverse. */
3175 struct xcoff_loader_info
3177 /* Set if a problem occurred. */
3181 /* Link information structure. */
3182 struct bfd_link_info *info;
3183 /* Whether all defined symbols should be exported. */
3184 boolean export_defineds;
3185 /* Number of ldsym structures. */
3187 /* Size of string table. */
3191 /* Allocated size of string table. */
3195 /* Build the .loader section. This is called by the XCOFF linker
3196 emulation before_allocation routine. We must set the size of the
3197 .loader section before the linker lays out the output file.
3198 LIBPATH is the library path to search for shared objects; this is
3199 normally built from the -L arguments passed to the linker. ENTRY
3200 is the name of the entry point symbol (the -e linker option).
3201 FILE_ALIGN is the alignment to use for sections within the file
3202 (the -H linker option). MAXSTACK is the maximum stack size (the
3203 -bmaxstack linker option). MAXDATA is the maximum data size (the
3204 -bmaxdata linker option). GC is whether to do garbage collection
3205 (the -bgc linker option). MODTYPE is the module type (the
3206 -bmodtype linker option). TEXTRO is whether the text section must
3207 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3208 whether all defined symbols should be exported (the -unix linker
3209 option). SPECIAL_SECTIONS is set by this routine to csects with
3210 magic names like _end. */
3213 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3214 file_align, maxstack, maxdata, gc,
3215 modtype, textro, export_defineds,
3218 struct bfd_link_info *info;
3219 const char *libpath;
3221 unsigned long file_align;
3222 unsigned long maxstack;
3223 unsigned long maxdata;
3227 boolean export_defineds;
3228 asection **special_sections;
3230 struct xcoff_link_hash_entry *hentry;
3232 struct xcoff_loader_info ldinfo;
3234 size_t impsize, impcount;
3235 struct xcoff_import_file *fl;
3236 struct internal_ldhdr *ldhdr;
3237 bfd_size_type stoff;
3241 struct bfd_strtab_hash *debug_strtab;
3242 bfd_byte *debug_contents = NULL;
3244 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3246 for (i = 0; i < 6; i++)
3247 special_sections[i] = NULL;
3251 ldinfo.failed = false;
3252 ldinfo.output_bfd = output_bfd;
3254 ldinfo.export_defineds = export_defineds;
3255 ldinfo.ldsym_count = 0;
3256 ldinfo.string_size = 0;
3257 ldinfo.strings = NULL;
3258 ldinfo.string_alc = 0;
3260 xcoff_data (output_bfd)->maxstack = maxstack;
3261 xcoff_data (output_bfd)->maxdata = maxdata;
3262 xcoff_data (output_bfd)->modtype = modtype;
3264 xcoff_hash_table (info)->file_align = file_align;
3265 xcoff_hash_table (info)->textro = textro;
3271 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3272 false, false, true);
3274 hentry->flags |= XCOFF_ENTRY;
3277 /* Garbage collect unused sections. */
3278 if (info->relocateable
3281 || (hentry->root.type != bfd_link_hash_defined
3282 && hentry->root.type != bfd_link_hash_defweak))
3285 xcoff_hash_table (info)->gc = false;
3287 /* We still need to call xcoff_mark, in order to set ldrel_count
3289 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3293 for (o = sub->sections; o != NULL; o = o->next)
3295 if ((o->flags & SEC_MARK) == 0)
3297 if (! xcoff_mark (info, o))
3305 if (! xcoff_mark (info, hentry->root.u.def.section))
3308 xcoff_hash_table (info)->gc = true;
3311 /* Return special sections to the caller. */
3312 for (i = 0; i < 6; i++)
3316 sec = xcoff_hash_table (info)->special_sections[i];
3319 && (sec->flags & SEC_MARK) == 0)
3321 special_sections[i] = sec;
3324 if (info->input_bfds == NULL)
3326 /* I'm not sure what to do in this bizarre case. */
3330 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3335 /* Work out the size of the import file names. Each import file ID
3336 consists of three null terminated strings: the path, the file
3337 name, and the archive member name. The first entry in the list
3338 of names is the path to use to find objects, which the linker has
3339 passed in as the libpath argument. For some reason, the path
3340 entry in the other import file names appears to always be empty. */
3341 impsize = strlen (libpath) + 3;
3343 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3346 impsize += (strlen (fl->path)
3348 + strlen (fl->member)
3352 /* Set up the .loader section header. */
3353 ldhdr = &xcoff_hash_table (info)->ldhdr;
3354 ldhdr->l_version = 1;
3355 ldhdr->l_nsyms = ldinfo.ldsym_count;
3356 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3357 ldhdr->l_istlen = impsize;
3358 ldhdr->l_nimpid = impcount;
3359 ldhdr->l_impoff = (LDHDRSZ
3360 + ldhdr->l_nsyms * LDSYMSZ
3361 + ldhdr->l_nreloc * LDRELSZ);
3362 ldhdr->l_stlen = ldinfo.string_size;
3363 stoff = ldhdr->l_impoff + impsize;
3364 if (ldinfo.string_size == 0)
3367 ldhdr->l_stoff = stoff;
3369 /* We now know the final size of the .loader section. Allocate
3371 lsec = xcoff_hash_table (info)->loader_section;
3372 lsec->_raw_size = stoff + ldhdr->l_stlen;
3373 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3374 if (lsec->contents == NULL)
3377 /* Set up the header. */
3378 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3379 (struct external_ldhdr *) lsec->contents);
3381 /* Set up the import file names. */
3382 out = (char *) lsec->contents + ldhdr->l_impoff;
3383 strcpy (out, libpath);
3384 out += strlen (libpath) + 1;
3387 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3389 register const char *s;
3392 while ((*out++ = *s++) != '\0')
3395 while ((*out++ = *s++) != '\0')
3398 while ((*out++ = *s++) != '\0')
3402 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3404 /* Set up the symbol string table. */
3405 if (ldinfo.string_size > 0)
3407 memcpy (out, ldinfo.strings, ldinfo.string_size);
3408 free (ldinfo.strings);
3409 ldinfo.strings = NULL;
3412 /* We can't set up the symbol table or the relocs yet, because we
3413 don't yet know the final position of the various sections. The
3414 .loader symbols are written out when the corresponding normal
3415 symbols are written out in xcoff_link_input_bfd or
3416 xcoff_write_global_symbol. The .loader relocs are written out
3417 when the corresponding normal relocs are handled in
3418 xcoff_link_input_bfd. */
3420 /* Allocate space for the magic sections. */
3421 sec = xcoff_hash_table (info)->linkage_section;
3422 if (sec->_raw_size > 0)
3424 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3425 if (sec->contents == NULL)
3428 sec = xcoff_hash_table (info)->toc_section;
3429 if (sec->_raw_size > 0)
3431 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3432 if (sec->contents == NULL)
3435 sec = xcoff_hash_table (info)->descriptor_section;
3436 if (sec->_raw_size > 0)
3438 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3439 if (sec->contents == NULL)
3443 /* Now that we've done garbage collection, figure out the contents
3444 of the .debug section. */
3445 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3447 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3450 bfd_size_type symcount;
3451 unsigned long *debug_index;
3453 bfd_byte *esym, *esymend;
3454 bfd_size_type symesz;
3456 if (sub->xvec != info->hash->creator)
3458 subdeb = bfd_get_section_by_name (sub, ".debug");
3459 if (subdeb == NULL || subdeb->_raw_size == 0)
3462 if (info->strip == strip_all
3463 || info->strip == strip_debugger
3464 || info->discard == discard_all)
3466 subdeb->_raw_size = 0;
3470 if (! _bfd_coff_get_external_symbols (sub))
3473 symcount = obj_raw_syment_count (sub);
3474 debug_index = ((unsigned long *)
3475 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3476 if (debug_index == NULL)
3478 xcoff_data (sub)->debug_indices = debug_index;
3480 /* Grab the contents of the .debug section. We use malloc and
3481 copy the names into the debug stringtab, rather than
3482 bfd_alloc, because I expect that, when linking many files
3483 together, many of the strings will be the same. Storing the
3484 strings in the hash table should save space in this case. */
3485 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3486 if (debug_contents == NULL)
3488 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3489 (file_ptr) 0, subdeb->_raw_size))
3492 csectpp = xcoff_data (sub)->csects;
3494 symesz = bfd_coff_symesz (sub);
3495 esym = (bfd_byte *) obj_coff_external_syms (sub);
3496 esymend = esym + symcount * symesz;
3497 while (esym < esymend)
3499 struct internal_syment sym;
3501 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3503 *debug_index = (unsigned long) -1;
3505 if (sym._n._n_n._n_zeroes == 0
3508 || ((*csectpp)->flags & SEC_MARK) != 0
3509 || *csectpp == bfd_abs_section_ptr)
3510 && bfd_coff_symname_in_debug (sub, &sym))
3515 name = (char *) debug_contents + sym._n._n_n._n_offset;
3516 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3517 if (indx == (bfd_size_type) -1)
3519 *debug_index = indx;
3522 esym += (sym.n_numaux + 1) * symesz;
3523 csectpp += sym.n_numaux + 1;
3524 debug_index += sym.n_numaux + 1;
3527 free (debug_contents);
3528 debug_contents = NULL;
3530 /* Clear the size of subdeb, so that it is not included directly
3531 in the output file. */
3532 subdeb->_raw_size = 0;
3534 if (! info->keep_memory)
3536 if (! _bfd_coff_free_symbols (sub))
3541 if (info->strip != strip_all)
3542 xcoff_hash_table (info)->debug_section->_raw_size =
3543 _bfd_stringtab_size (debug_strtab);
3548 if (ldinfo.strings != NULL)
3549 free (ldinfo.strings);
3550 if (debug_contents != NULL)
3551 free (debug_contents);
3555 /* Add a symbol to the .loader symbols, if necessary. */
3558 xcoff_build_ldsyms (h, p)
3559 struct xcoff_link_hash_entry *h;
3562 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3565 /* If this is a final link, and the symbol was defined as a common
3566 symbol in a regular object file, and there was no definition in
3567 any dynamic object, then the linker will have allocated space for
3568 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3569 will not have been set. */
3570 if (h->root.type == bfd_link_hash_defined
3571 && (h->flags & XCOFF_DEF_REGULAR) == 0
3572 && (h->flags & XCOFF_REF_REGULAR) != 0
3573 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3574 && (bfd_is_abs_section (h->root.u.def.section)
3575 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3576 h->flags |= XCOFF_DEF_REGULAR;
3578 /* If all defined symbols should be exported, mark them now. We
3579 don't want to export the actual functions, just the function
3581 if (ldinfo->export_defineds
3582 && (h->flags & XCOFF_DEF_REGULAR) != 0
3583 && h->root.root.string[0] != '.')
3587 /* We don't export a symbol which is being defined by an object
3588 included from an archive which contains a shared object. The
3589 rationale is that if an archive contains both an unshared and
3590 a shared object, then there must be some reason that the
3591 unshared object is unshared, and we don't want to start
3592 providing a shared version of it. In particular, this solves
3593 a bug involving the _savefNN set of functions. gcc will call
3594 those functions without providing a slot to restore the TOC,
3595 so it is essential that these functions be linked in directly
3596 and not from a shared object, which means that a shared
3597 object which also happens to link them in must not export
3598 them. This is confusing, but I haven't been able to think of
3599 a different approach. Note that the symbols can, of course,
3600 be exported explicitly. */
3602 if ((h->root.type == bfd_link_hash_defined
3603 || h->root.type == bfd_link_hash_defweak)
3604 && h->root.u.def.section->owner != NULL
3605 && h->root.u.def.section->owner->my_archive != NULL)
3607 bfd *arbfd, *member;
3609 arbfd = h->root.u.def.section->owner->my_archive;
3610 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3611 while (member != NULL)
3613 if ((member->flags & DYNAMIC) != 0)
3618 member = bfd_openr_next_archived_file (arbfd, member);
3623 h->flags |= XCOFF_EXPORT;
3626 /* We don't want to garbage collect symbols which are not defined in
3627 XCOFF files. This is a convenient place to mark them. */
3628 if (xcoff_hash_table (ldinfo->info)->gc
3629 && (h->flags & XCOFF_MARK) == 0
3630 && (h->root.type == bfd_link_hash_defined
3631 || h->root.type == bfd_link_hash_defweak)
3632 && (h->root.u.def.section->owner == NULL
3633 || (h->root.u.def.section->owner->xvec
3634 != ldinfo->info->hash->creator)))
3635 h->flags |= XCOFF_MARK;
3637 /* If this symbol is called and defined in a dynamic object, or it
3638 is imported, then we need to set up global linkage code for it.
3639 (Unless we did garbage collection and we didn't need this
3641 if ((h->flags & XCOFF_CALLED) != 0
3642 && (h->root.type == bfd_link_hash_undefined
3643 || h->root.type == bfd_link_hash_undefweak)
3644 && h->root.root.string[0] == '.'
3645 && h->descriptor != NULL
3646 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3647 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3648 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3649 && (! xcoff_hash_table (ldinfo->info)->gc
3650 || (h->flags & XCOFF_MARK) != 0))
3653 struct xcoff_link_hash_entry *hds;
3655 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3656 h->root.type = bfd_link_hash_defined;
3657 h->root.u.def.section = sec;
3658 h->root.u.def.value = sec->_raw_size;
3660 h->flags |= XCOFF_DEF_REGULAR;
3661 sec->_raw_size += XCOFF_GLINK_SIZE;
3663 /* The global linkage code requires a TOC entry for the
3665 hds = h->descriptor;
3666 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3667 || hds->root.type == bfd_link_hash_undefweak)
3668 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3669 hds->flags |= XCOFF_MARK;
3670 if (hds->toc_section == NULL)
3672 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3673 hds->u.toc_offset = hds->toc_section->_raw_size;
3674 hds->toc_section->_raw_size += 4;
3675 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3676 ++hds->toc_section->reloc_count;
3678 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3680 /* We need to call xcoff_build_ldsyms recursively here,
3681 because we may already have passed hds on the traversal. */
3682 xcoff_build_ldsyms (hds, p);
3686 /* If this symbol is exported, but not defined, we need to try to
3688 if ((h->flags & XCOFF_EXPORT) != 0
3689 && (h->flags & XCOFF_IMPORT) == 0
3690 && (h->flags & XCOFF_DEF_REGULAR) == 0
3691 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3692 && (h->root.type == bfd_link_hash_undefined
3693 || h->root.type == bfd_link_hash_undefweak))
3695 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3696 && (h->descriptor->root.type == bfd_link_hash_defined
3697 || h->descriptor->root.type == bfd_link_hash_defweak))
3701 /* This is an undefined function descriptor associated with
3702 a defined entry point. We can build up a function
3703 descriptor ourselves. Believe it or not, the AIX linker
3704 actually does this, and there are cases where we need to
3706 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3707 h->root.type = bfd_link_hash_defined;
3708 h->root.u.def.section = sec;
3709 h->root.u.def.value = sec->_raw_size;
3711 h->flags |= XCOFF_DEF_REGULAR;
3712 sec->_raw_size += 12;
3714 /* A function descriptor uses two relocs: one for the
3715 associated code, and one for the TOC address. */
3716 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3717 sec->reloc_count += 2;
3719 /* We handle writing out the contents of the descriptor in
3720 xcoff_write_global_symbol. */
3724 (*_bfd_error_handler)
3725 (_("warning: attempt to export undefined symbol `%s'"),
3726 h->root.root.string);
3732 /* If this is still a common symbol, and it wasn't garbage
3733 collected, we need to actually allocate space for it in the .bss
3735 if (h->root.type == bfd_link_hash_common
3736 && (! xcoff_hash_table (ldinfo->info)->gc
3737 || (h->flags & XCOFF_MARK) != 0)
3738 && h->root.u.c.p->section->_raw_size == 0)
3740 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3741 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3744 /* We need to add a symbol to the .loader section if it is mentioned
3745 in a reloc which we are copying to the .loader section and it was
3746 not defined or common, or if it is the entry point, or if it is
3749 if (((h->flags & XCOFF_LDREL) == 0
3750 || h->root.type == bfd_link_hash_defined
3751 || h->root.type == bfd_link_hash_defweak
3752 || h->root.type == bfd_link_hash_common)
3753 && (h->flags & XCOFF_ENTRY) == 0
3754 && (h->flags & XCOFF_EXPORT) == 0)
3760 /* We don't need to add this symbol if we did garbage collection and
3761 we did not mark this symbol. */
3762 if (xcoff_hash_table (ldinfo->info)->gc
3763 && (h->flags & XCOFF_MARK) == 0)
3769 /* We may have already processed this symbol due to the recursive
3771 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3774 /* We need to add this symbol to the .loader symbols. */
3776 BFD_ASSERT (h->ldsym == NULL);
3777 h->ldsym = ((struct internal_ldsym *)
3778 bfd_zalloc (ldinfo->output_bfd,
3779 sizeof (struct internal_ldsym)));
3780 if (h->ldsym == NULL)
3782 ldinfo->failed = true;
3786 if ((h->flags & XCOFF_IMPORT) != 0)
3787 h->ldsym->l_ifile = h->ldindx;
3789 /* The first 3 symbol table indices are reserved to indicate the
3791 h->ldindx = ldinfo->ldsym_count + 3;
3793 ++ldinfo->ldsym_count;
3795 len = strlen (h->root.root.string);
3796 if (len <= SYMNMLEN)
3797 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3800 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3803 bfd_byte *newstrings;
3805 newalc = ldinfo->string_alc * 2;
3808 while (ldinfo->string_size + len + 3 > newalc)
3811 newstrings = ((bfd_byte *)
3812 bfd_realloc ((PTR) ldinfo->strings, newalc));
3813 if (newstrings == NULL)
3815 ldinfo->failed = true;
3818 ldinfo->string_alc = newalc;
3819 ldinfo->strings = newstrings;
3822 bfd_put_16 (ldinfo->output_bfd, len + 1,
3823 ldinfo->strings + ldinfo->string_size);
3824 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3825 h->ldsym->_l._l_l._l_zeroes = 0;
3826 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3827 ldinfo->string_size += len + 3;
3830 h->flags |= XCOFF_BUILT_LDSYM;
3835 /* Do the final link step. */
3838 _bfd_xcoff_bfd_final_link (abfd, info)
3840 struct bfd_link_info *info;
3842 bfd_size_type symesz;
3843 struct xcoff_final_link_info finfo;
3845 struct bfd_link_order *p;
3846 size_t max_contents_size;
3847 size_t max_sym_count;
3848 size_t max_lineno_count;
3849 size_t max_reloc_count;
3850 size_t max_output_reloc_count;
3851 file_ptr rel_filepos;
3853 file_ptr line_filepos;
3854 unsigned int linesz;
3856 bfd_byte *external_relocs = NULL;
3857 char strbuf[STRING_SIZE_SIZE];
3860 abfd->flags |= DYNAMIC;
3862 symesz = bfd_coff_symesz (abfd);
3865 finfo.output_bfd = abfd;
3866 finfo.strtab = NULL;
3867 finfo.section_info = NULL;
3868 finfo.last_file_index = -1;
3869 finfo.toc_symindx = -1;
3870 finfo.internal_syms = NULL;
3871 finfo.sym_indices = NULL;
3872 finfo.outsyms = NULL;
3873 finfo.linenos = NULL;
3874 finfo.contents = NULL;
3875 finfo.external_relocs = NULL;
3877 finfo.ldsym = ((struct external_ldsym *)
3878 (xcoff_hash_table (info)->loader_section->contents
3880 finfo.ldrel = ((struct external_ldrel *)
3881 (xcoff_hash_table (info)->loader_section->contents
3883 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3885 xcoff_data (abfd)->coff.link_info = info;
3887 finfo.strtab = _bfd_stringtab_init ();
3888 if (finfo.strtab == NULL)
3891 /* Count the line number and relocation entries required for the
3892 output file. Determine a few maximum sizes. */
3893 max_contents_size = 0;
3894 max_lineno_count = 0;
3895 max_reloc_count = 0;
3896 for (o = abfd->sections; o != NULL; o = o->next)
3899 o->lineno_count = 0;
3900 for (p = o->link_order_head; p != NULL; p = p->next)
3902 if (p->type == bfd_indirect_link_order)
3906 sec = p->u.indirect.section;
3908 /* Mark all sections which are to be included in the
3909 link. This will normally be every section. We need
3910 to do this so that we can identify any sections which
3911 the linker has decided to not include. */
3912 sec->linker_mark = true;
3914 if (info->strip == strip_none
3915 || info->strip == strip_some)
3916 o->lineno_count += sec->lineno_count;
3918 o->reloc_count += sec->reloc_count;
3920 if (sec->_raw_size > max_contents_size)
3921 max_contents_size = sec->_raw_size;
3922 if (sec->lineno_count > max_lineno_count)
3923 max_lineno_count = sec->lineno_count;
3924 if (coff_section_data (sec->owner, sec) != NULL
3925 && xcoff_section_data (sec->owner, sec) != NULL
3926 && (xcoff_section_data (sec->owner, sec)->lineno_count
3927 > max_lineno_count))
3929 xcoff_section_data (sec->owner, sec)->lineno_count;
3930 if (sec->reloc_count > max_reloc_count)
3931 max_reloc_count = sec->reloc_count;
3933 else if (p->type == bfd_section_reloc_link_order
3934 || p->type == bfd_symbol_reloc_link_order)
3939 /* Compute the file positions for all the sections. */
3940 if (abfd->output_has_begun)
3942 if (xcoff_hash_table (info)->file_align != 0)
3949 file_align = xcoff_hash_table (info)->file_align;
3950 if (file_align != 0)
3952 boolean saw_contents;
3957 /* Insert .pad sections before every section which has
3958 contents and is loaded, if it is preceded by some other
3959 section which has contents and is loaded. */
3960 saw_contents = true;
3961 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3963 if (strcmp ((*op)->name, ".pad") == 0)
3964 saw_contents = false;
3965 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3966 && ((*op)->flags & SEC_LOAD) != 0)
3969 saw_contents = true;
3976 n = bfd_make_section_anyway (abfd, ".pad");
3977 BFD_ASSERT (*op == n);
3979 n->flags = SEC_HAS_CONTENTS;
3980 n->alignment_power = 0;
3981 saw_contents = false;
3986 /* Reset the section indices after inserting the new
3989 for (o = abfd->sections; o != NULL; o = o->next)
3992 o->target_index = indx;
3994 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3996 /* Work out appropriate sizes for the .pad sections to force
3997 each section to land on a page boundary. This bit of
3998 code knows what compute_section_file_positions is going
4000 sofar = bfd_coff_filhsz (abfd);
4001 sofar += bfd_coff_aoutsz (abfd);
4002 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
4003 for (o = abfd->sections; o != NULL; o = o->next)
4004 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
4005 sofar += bfd_coff_scnhsz (abfd);
4007 for (o = abfd->sections; o != NULL; o = o->next)
4009 if (strcmp (o->name, ".pad") == 0)
4013 BFD_ASSERT (o->_raw_size == 0);
4014 pageoff = sofar & (file_align - 1);
4017 o->_raw_size = file_align - pageoff;
4018 sofar += file_align - pageoff;
4019 o->flags |= SEC_HAS_CONTENTS;
4024 if ((o->flags & SEC_HAS_CONTENTS) != 0)
4025 sofar += BFD_ALIGN (o->_raw_size,
4026 1 << o->alignment_power);
4031 if (! bfd_coff_compute_section_file_positions (abfd))
4035 /* Allocate space for the pointers we need to keep for the relocs. */
4039 /* We use section_count + 1, rather than section_count, because
4040 the target_index fields are 1 based. */
4041 finfo.section_info =
4042 ((struct xcoff_link_section_info *)
4043 bfd_malloc ((abfd->section_count + 1)
4044 * sizeof (struct xcoff_link_section_info)));
4045 if (finfo.section_info == NULL)
4047 for (i = 0; i <= abfd->section_count; i++)
4049 finfo.section_info[i].relocs = NULL;
4050 finfo.section_info[i].rel_hashes = NULL;
4051 finfo.section_info[i].toc_rel_hashes = NULL;
4055 /* Set the file positions for the relocs. */
4056 rel_filepos = obj_relocbase (abfd);
4057 relsz = bfd_coff_relsz (abfd);
4058 max_output_reloc_count = 0;
4059 for (o = abfd->sections; o != NULL; o = o->next)
4061 if (o->reloc_count == 0)
4065 /* A stripped file has no relocs. However, we still
4066 allocate the buffers, so that later code doesn't have to
4067 worry about whether we are stripping or not. */
4068 if (info->strip == strip_all)
4072 o->flags |= SEC_RELOC;
4073 o->rel_filepos = rel_filepos;
4074 rel_filepos += o->reloc_count * relsz;
4077 /* We don't know the indices of global symbols until we have
4078 written out all the local symbols. For each section in
4079 the output file, we keep an array of pointers to hash
4080 table entries. Each entry in the array corresponds to a
4081 reloc. When we find a reloc against a global symbol, we
4082 set the corresponding entry in this array so that we can
4083 fix up the symbol index after we have written out all the
4086 Because of this problem, we also keep the relocs in
4087 memory until the end of the link. This wastes memory.
4088 We could backpatch the file later, I suppose, although it
4090 finfo.section_info[o->target_index].relocs =
4091 ((struct internal_reloc *)
4092 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
4093 finfo.section_info[o->target_index].rel_hashes =
4094 ((struct xcoff_link_hash_entry **)
4095 bfd_malloc (o->reloc_count
4096 * sizeof (struct xcoff_link_hash_entry *)));
4097 if (finfo.section_info[o->target_index].relocs == NULL
4098 || finfo.section_info[o->target_index].rel_hashes == NULL)
4101 if (o->reloc_count > max_output_reloc_count)
4102 max_output_reloc_count = o->reloc_count;
4106 /* We now know the size of the relocs, so we can determine the file
4107 positions of the line numbers. */
4108 line_filepos = rel_filepos;
4109 finfo.line_filepos = line_filepos;
4110 linesz = bfd_coff_linesz (abfd);
4111 for (o = abfd->sections; o != NULL; o = o->next)
4113 if (o->lineno_count == 0)
4114 o->line_filepos = 0;
4117 o->line_filepos = line_filepos;
4118 line_filepos += o->lineno_count * linesz;
4121 /* Reset the reloc and lineno counts, so that we can use them to
4122 count the number of entries we have output so far. */
4124 o->lineno_count = 0;
4127 obj_sym_filepos (abfd) = line_filepos;
4129 /* Figure out the largest number of symbols in an input BFD. Take
4130 the opportunity to clear the output_has_begun fields of all the
4131 input BFD's. We want at least 6 symbols, since that is the
4132 number which xcoff_write_global_symbol may need. */
4134 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4138 sub->output_has_begun = false;
4139 sz = obj_raw_syment_count (sub);
4140 if (sz > max_sym_count)
4144 /* Allocate some buffers used while linking. */
4145 finfo.internal_syms = ((struct internal_syment *)
4146 bfd_malloc (max_sym_count
4147 * sizeof (struct internal_syment)));
4148 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4149 finfo.outsyms = ((bfd_byte *)
4150 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
4151 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
4152 * bfd_coff_linesz (abfd));
4153 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4154 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
4155 if ((finfo.internal_syms == NULL && max_sym_count > 0)
4156 || (finfo.sym_indices == NULL && max_sym_count > 0)
4157 || finfo.outsyms == NULL
4158 || (finfo.linenos == NULL && max_lineno_count > 0)
4159 || (finfo.contents == NULL && max_contents_size > 0)
4160 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4163 obj_raw_syment_count (abfd) = 0;
4164 xcoff_data (abfd)->toc = (bfd_vma) -1;
4166 /* We now know the position of everything in the file, except that
4167 we don't know the size of the symbol table and therefore we don't
4168 know where the string table starts. We just build the string
4169 table in memory as we go along. We process all the relocations
4170 for a single input file at once. */
4171 for (o = abfd->sections; o != NULL; o = o->next)
4173 for (p = o->link_order_head; p != NULL; p = p->next)
4175 if (p->type == bfd_indirect_link_order
4176 && p->u.indirect.section->owner->xvec == abfd->xvec)
4178 sub = p->u.indirect.section->owner;
4179 if (! sub->output_has_begun)
4181 if (! xcoff_link_input_bfd (&finfo, sub))
4183 sub->output_has_begun = true;
4186 else if (p->type == bfd_section_reloc_link_order
4187 || p->type == bfd_symbol_reloc_link_order)
4189 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4194 if (! _bfd_default_link_order (abfd, info, o, p))
4200 /* Free up the buffers used by xcoff_link_input_bfd. */
4202 if (finfo.internal_syms != NULL)
4204 free (finfo.internal_syms);
4205 finfo.internal_syms = NULL;
4207 if (finfo.sym_indices != NULL)
4209 free (finfo.sym_indices);
4210 finfo.sym_indices = NULL;
4212 if (finfo.linenos != NULL)
4214 free (finfo.linenos);
4215 finfo.linenos = NULL;
4217 if (finfo.contents != NULL)
4219 free (finfo.contents);
4220 finfo.contents = NULL;
4222 if (finfo.external_relocs != NULL)
4224 free (finfo.external_relocs);
4225 finfo.external_relocs = NULL;
4228 /* The value of the last C_FILE symbol is supposed to be -1. Write
4230 if (finfo.last_file_index != -1)
4232 finfo.last_file.n_value = -1;
4233 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4234 (PTR) finfo.outsyms);
4236 (obj_sym_filepos (abfd)
4237 + finfo.last_file_index * symesz),
4239 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4243 /* Write out all the global symbols which do not come from XCOFF
4245 xcoff_link_hash_traverse (xcoff_hash_table (info),
4246 xcoff_write_global_symbol,
4249 if (finfo.outsyms != NULL)
4251 free (finfo.outsyms);
4252 finfo.outsyms = NULL;
4255 /* Now that we have written out all the global symbols, we know the
4256 symbol indices to use for relocs against them, and we can finally
4257 write out the relocs. */
4258 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
4259 if (external_relocs == NULL && max_output_reloc_count != 0)
4262 for (o = abfd->sections; o != NULL; o = o->next)
4264 struct internal_reloc *irel;
4265 struct internal_reloc *irelend;
4266 struct xcoff_link_hash_entry **rel_hash;
4267 struct xcoff_toc_rel_hash *toc_rel_hash;
4270 /* A stripped file has no relocs. */
4271 if (info->strip == strip_all)
4277 if (o->reloc_count == 0)
4280 irel = finfo.section_info[o->target_index].relocs;
4281 irelend = irel + o->reloc_count;
4282 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4283 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4285 if (*rel_hash != NULL)
4287 if ((*rel_hash)->indx < 0)
4289 if (! ((*info->callbacks->unattached_reloc)
4290 (info, (*rel_hash)->root.root.string,
4291 (bfd *) NULL, o, irel->r_vaddr)))
4293 (*rel_hash)->indx = 0;
4295 irel->r_symndx = (*rel_hash)->indx;
4299 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4300 toc_rel_hash != NULL;
4301 toc_rel_hash = toc_rel_hash->next)
4303 if (toc_rel_hash->h->u.toc_indx < 0)
4305 if (! ((*info->callbacks->unattached_reloc)
4306 (info, toc_rel_hash->h->root.root.string,
4307 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4309 toc_rel_hash->h->u.toc_indx = 0;
4311 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4314 /* XCOFF requires that the relocs be sorted by address. We tend
4315 to produce them in the order in which their containing csects
4316 appear in the symbol table, which is not necessarily by
4317 address. So we sort them here. There may be a better way to
4319 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4320 o->reloc_count, sizeof (struct internal_reloc),
4323 irel = finfo.section_info[o->target_index].relocs;
4324 irelend = irel + o->reloc_count;
4325 erel = external_relocs;
4326 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4327 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4329 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4330 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4331 abfd) != relsz * o->reloc_count)
4335 if (external_relocs != NULL)
4337 free (external_relocs);
4338 external_relocs = NULL;
4341 /* Free up the section information. */
4342 if (finfo.section_info != NULL)
4346 for (i = 0; i < abfd->section_count; i++)
4348 if (finfo.section_info[i].relocs != NULL)
4349 free (finfo.section_info[i].relocs);
4350 if (finfo.section_info[i].rel_hashes != NULL)
4351 free (finfo.section_info[i].rel_hashes);
4353 free (finfo.section_info);
4354 finfo.section_info = NULL;
4357 /* Write out the loader section contents. */
4358 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4359 == (xcoff_hash_table (info)->loader_section->contents
4360 + xcoff_hash_table (info)->ldhdr.l_impoff));
4361 o = xcoff_hash_table (info)->loader_section;
4362 if (! bfd_set_section_contents (abfd, o->output_section,
4363 o->contents, o->output_offset,
4367 /* Write out the magic sections. */
4368 o = xcoff_hash_table (info)->linkage_section;
4369 if (o->_raw_size > 0
4370 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4371 o->output_offset, o->_raw_size))
4373 o = xcoff_hash_table (info)->toc_section;
4374 if (o->_raw_size > 0
4375 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4376 o->output_offset, o->_raw_size))
4378 o = xcoff_hash_table (info)->descriptor_section;
4379 if (o->_raw_size > 0
4380 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4381 o->output_offset, o->_raw_size))
4384 /* Write out the string table. */
4386 (obj_sym_filepos (abfd)
4387 + obj_raw_syment_count (abfd) * symesz),
4391 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4392 (bfd_byte *) strbuf);
4393 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4395 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4398 _bfd_stringtab_free (finfo.strtab);
4400 /* Write out the debugging string table. */
4401 o = xcoff_hash_table (info)->debug_section;
4404 struct bfd_strtab_hash *debug_strtab;
4406 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4407 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4408 >= _bfd_stringtab_size (debug_strtab));
4410 o->output_section->filepos + o->output_offset,
4413 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4417 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4418 not try to write out the symbols. */
4419 bfd_get_symcount (abfd) = 0;
4424 if (finfo.strtab != NULL)
4425 _bfd_stringtab_free (finfo.strtab);
4426 if (finfo.section_info != NULL)
4430 for (i = 0; i < abfd->section_count; i++)
4432 if (finfo.section_info[i].relocs != NULL)
4433 free (finfo.section_info[i].relocs);
4434 if (finfo.section_info[i].rel_hashes != NULL)
4435 free (finfo.section_info[i].rel_hashes);
4437 free (finfo.section_info);
4439 if (finfo.internal_syms != NULL)
4440 free (finfo.internal_syms);
4441 if (finfo.sym_indices != NULL)
4442 free (finfo.sym_indices);
4443 if (finfo.outsyms != NULL)
4444 free (finfo.outsyms);
4445 if (finfo.linenos != NULL)
4446 free (finfo.linenos);
4447 if (finfo.contents != NULL)
4448 free (finfo.contents);
4449 if (finfo.external_relocs != NULL)
4450 free (finfo.external_relocs);
4451 if (external_relocs != NULL)
4452 free (external_relocs);
4456 /* Link an input file into the linker output file. This function
4457 handles all the sections and relocations of the input file at once. */
4460 xcoff_link_input_bfd (finfo, input_bfd)
4461 struct xcoff_final_link_info *finfo;
4465 const char *strings;
4466 bfd_size_type syment_base;
4467 unsigned int n_tmask;
4468 unsigned int n_btshft;
4470 bfd_size_type isymesz;
4471 bfd_size_type osymesz;
4472 bfd_size_type linesz;
4475 struct xcoff_link_hash_entry **sym_hash;
4476 struct internal_syment *isymp;
4478 unsigned long *debug_index;
4480 unsigned long output_index;
4487 /* We can just skip DYNAMIC files, unless this is a static link. */
4488 if ((input_bfd->flags & DYNAMIC) != 0
4489 && ! finfo->info->static_link)
4492 /* Move all the symbols to the output file. */
4494 output_bfd = finfo->output_bfd;
4496 syment_base = obj_raw_syment_count (output_bfd);
4497 isymesz = bfd_coff_symesz (input_bfd);
4498 osymesz = bfd_coff_symesz (output_bfd);
4499 linesz = bfd_coff_linesz (input_bfd);
4500 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4502 n_tmask = coff_data (input_bfd)->local_n_tmask;
4503 n_btshft = coff_data (input_bfd)->local_n_btshft;
4505 /* Define macros so that ISFCN, et. al., macros work correctly. */
4506 #define N_TMASK n_tmask
4507 #define N_BTSHFT n_btshft
4510 if (! finfo->info->keep_memory)
4513 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4516 if (! _bfd_coff_get_external_symbols (input_bfd))
4519 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4520 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4521 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4522 csectpp = xcoff_data (input_bfd)->csects;
4523 debug_index = xcoff_data (input_bfd)->debug_indices;
4524 isymp = finfo->internal_syms;
4525 indexp = finfo->sym_indices;
4526 output_index = syment_base;
4527 outsym = finfo->outsyms;
4531 while (esym < esym_end)
4533 struct internal_syment isym;
4534 union internal_auxent aux;
4540 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4542 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4544 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4546 BFD_ASSERT (isymp->n_numaux > 0);
4547 bfd_coff_swap_aux_in (input_bfd,
4548 (PTR) (esym + isymesz * isymp->n_numaux),
4549 isymp->n_type, isymp->n_sclass,
4550 isymp->n_numaux - 1, isymp->n_numaux,
4552 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4555 /* Make a copy of *isymp so that the relocate_section function
4556 always sees the original values. This is more reliable than
4557 always recomputing the symbol value even if we are stripping
4561 /* If this symbol is in the .loader section, swap out the
4562 .loader symbol information. If this is an external symbol
4563 reference to a defined symbol, though, then wait until we get
4564 to the definition. */
4565 if (isym.n_sclass == C_EXT
4566 && *sym_hash != NULL
4567 && (*sym_hash)->ldsym != NULL
4569 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4571 struct xcoff_link_hash_entry *h;
4572 struct internal_ldsym *ldsym;
4576 if (isym.n_scnum > 0)
4578 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4579 ldsym->l_value = (isym.n_value
4580 + (*csectpp)->output_section->vma
4581 + (*csectpp)->output_offset
4586 ldsym->l_scnum = isym.n_scnum;
4587 ldsym->l_value = isym.n_value;
4590 ldsym->l_smtype = smtyp;
4591 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4592 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4593 || (h->flags & XCOFF_IMPORT) != 0)
4594 ldsym->l_smtype |= L_IMPORT;
4595 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4596 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4597 || (h->flags & XCOFF_EXPORT) != 0)
4598 ldsym->l_smtype |= L_EXPORT;
4599 if ((h->flags & XCOFF_ENTRY) != 0)
4600 ldsym->l_smtype |= L_ENTRY;
4602 ldsym->l_smclas = aux.x_csect.x_smclas;
4604 if (ldsym->l_ifile == (bfd_size_type) -1)
4606 else if (ldsym->l_ifile == 0)
4608 if ((ldsym->l_smtype & L_IMPORT) == 0)
4614 if (h->root.type == bfd_link_hash_defined
4615 || h->root.type == bfd_link_hash_defweak)
4616 impbfd = h->root.u.def.section->owner;
4617 else if (h->root.type == bfd_link_hash_undefined
4618 || h->root.type == bfd_link_hash_undefweak)
4619 impbfd = h->root.u.undef.abfd;
4627 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4628 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4635 BFD_ASSERT (h->ldindx >= 0);
4636 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4637 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4638 finfo->ldsym + h->ldindx - 3);
4641 /* Fill in snentry now that we know the target_index. */
4642 if ((h->flags & XCOFF_ENTRY) != 0
4643 && (h->root.type == bfd_link_hash_defined
4644 || h->root.type == bfd_link_hash_defweak))
4645 xcoff_data (output_bfd)->snentry =
4646 h->root.u.def.section->output_section->target_index;
4653 add = 1 + isym.n_numaux;
4655 /* If we are skipping this csect, we want to skip this symbol. */
4656 if (*csectpp == NULL)
4659 /* If we garbage collected this csect, we want to skip this
4662 && xcoff_hash_table (finfo->info)->gc
4663 && ((*csectpp)->flags & SEC_MARK) == 0
4664 && *csectpp != bfd_abs_section_ptr)
4667 /* An XCOFF linker always skips C_STAT symbols. */
4669 && isymp->n_sclass == C_STAT)
4672 /* We skip all but the first TOC anchor. */
4674 && isymp->n_sclass == C_HIDEXT
4675 && aux.x_csect.x_smclas == XMC_TC0)
4677 if (finfo->toc_symindx != -1)
4681 bfd_vma tocval, tocend;
4683 tocval = ((*csectpp)->output_section->vma
4684 + (*csectpp)->output_offset
4687 /* We want to find out if tocval is a good value to use
4688 as the TOC anchor--that is, whether we can access all
4689 of the TOC using a 16 bit offset from tocval. This
4690 test assumes that the TOC comes at the end of the
4691 output section, as it does in the default linker
4692 script. FIXME: This doesn't handle .tocbss sections
4693 created from XMC_TD common symbols correctly. */
4695 tocend = ((*csectpp)->output_section->vma
4696 + (*csectpp)->output_section->_raw_size);
4698 if (tocval + 0x10000 < tocend)
4700 (*_bfd_error_handler)
4701 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4702 (unsigned long) (tocend - tocval));
4703 bfd_set_error (bfd_error_file_too_big);
4707 if (tocval + 0x8000 < tocend)
4711 tocadd = tocend - (tocval + 0x8000);
4713 isym.n_value += tocadd;
4716 finfo->toc_symindx = output_index;
4717 xcoff_data (finfo->output_bfd)->toc = tocval;
4718 xcoff_data (finfo->output_bfd)->sntoc =
4719 (*csectpp)->output_section->target_index;
4724 /* If we are stripping all symbols, we want to skip this one. */
4726 && finfo->info->strip == strip_all)
4729 /* We can skip resolved external references. */
4731 && isym.n_sclass == C_EXT
4733 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4736 /* We can skip common symbols if they got defined somewhere
4739 && isym.n_sclass == C_EXT
4741 && ((*sym_hash)->root.type != bfd_link_hash_common
4742 || (*sym_hash)->root.u.c.p->section != *csectpp)
4743 && ((*sym_hash)->root.type != bfd_link_hash_defined
4744 || (*sym_hash)->root.u.def.section != *csectpp))
4747 /* Skip local symbols if we are discarding them. */
4749 && finfo->info->discard == discard_all
4750 && isym.n_sclass != C_EXT
4751 && (isym.n_sclass != C_HIDEXT
4752 || smtyp != XTY_SD))
4755 /* If we stripping debugging symbols, and this is a debugging
4756 symbol, then skip it. */
4758 && finfo->info->strip == strip_debugger
4759 && isym.n_scnum == N_DEBUG)
4762 /* If some symbols are stripped based on the name, work out the
4763 name and decide whether to skip this symbol. We don't handle
4764 this correctly for symbols whose names are in the .debug
4765 section; to get it right we would need a new bfd_strtab_hash
4766 function to return the string given the index. */
4768 && (finfo->info->strip == strip_some
4769 || finfo->info->discard == discard_l)
4770 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4773 char buf[SYMNMLEN + 1];
4775 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4779 if ((finfo->info->strip == strip_some
4780 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4782 || (finfo->info->discard == discard_l
4783 && (isym.n_sclass != C_EXT
4784 && (isym.n_sclass != C_HIDEXT
4785 || smtyp != XTY_SD))
4786 && bfd_is_local_label_name (input_bfd, name)))
4790 /* We can not skip the first TOC anchor. */
4793 && finfo->info->strip != strip_all)
4796 /* We now know whether we are to skip this symbol or not. */
4799 /* Adjust the symbol in order to output it. */
4801 if (isym._n._n_n._n_zeroes == 0
4802 && isym._n._n_n._n_offset != 0)
4804 /* This symbol has a long name. Enter it in the string
4805 table we are building. If *debug_index != -1, the
4806 name has already been entered in the .debug section. */
4807 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4808 isym._n._n_n._n_offset = *debug_index;
4814 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4818 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4819 if (indx == (bfd_size_type) -1)
4821 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4825 if (isym.n_sclass != C_BSTAT
4826 && isym.n_sclass != C_ESTAT
4827 && isym.n_sclass != C_DECL
4828 && isym.n_scnum > 0)
4830 isym.n_scnum = (*csectpp)->output_section->target_index;
4831 isym.n_value += ((*csectpp)->output_section->vma
4832 + (*csectpp)->output_offset
4836 /* The value of a C_FILE symbol is the symbol index of the
4837 next C_FILE symbol. The value of the last C_FILE symbol
4838 is -1. We try to get this right, below, just before we
4839 write the symbols out, but in the general case we may
4840 have to write the symbol out twice. */
4841 if (isym.n_sclass == C_FILE)
4843 if (finfo->last_file_index != -1
4844 && finfo->last_file.n_value != (long) output_index)
4846 /* We must correct the value of the last C_FILE entry. */
4847 finfo->last_file.n_value = output_index;
4848 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4850 /* The last C_FILE symbol is in this input file. */
4851 bfd_coff_swap_sym_out (output_bfd,
4852 (PTR) &finfo->last_file,
4853 (PTR) (finfo->outsyms
4854 + ((finfo->last_file_index
4860 /* We have already written out the last C_FILE
4861 symbol. We need to write it out again. We
4862 borrow *outsym temporarily. */
4863 bfd_coff_swap_sym_out (output_bfd,
4864 (PTR) &finfo->last_file,
4866 if (bfd_seek (output_bfd,
4867 (obj_sym_filepos (output_bfd)
4868 + finfo->last_file_index * osymesz),
4870 || (bfd_write (outsym, osymesz, 1, output_bfd)
4876 finfo->last_file_index = output_index;
4877 finfo->last_file = isym;
4880 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4881 into the line numbers. We update the symbol values when
4882 we handle the line numbers. */
4883 if (isym.n_sclass == C_BINCL
4884 || isym.n_sclass == C_EINCL)
4886 isym.n_value = finfo->line_filepos;
4890 /* Output the symbol. */
4892 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4894 *indexp = output_index;
4896 if (isym.n_sclass == C_EXT)
4899 struct xcoff_link_hash_entry *h;
4901 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4903 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4904 BFD_ASSERT (h != NULL);
4905 h->indx = output_index;
4908 /* If this is a symbol in the TOC which we may have merged
4909 (class XMC_TC), remember the symbol index of the TOC
4911 if (isym.n_sclass == C_HIDEXT
4912 && aux.x_csect.x_smclas == XMC_TC
4913 && *sym_hash != NULL)
4915 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4916 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4917 (*sym_hash)->u.toc_indx = output_index;
4920 output_index += add;
4921 outsym += add * osymesz;
4924 esym += add * isymesz;
4928 if (debug_index != NULL)
4931 for (--add; add > 0; --add)
4935 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4936 done in a separate pass, because we don't know the correct symbol
4937 indices until we have already decided which symbols we are going
4940 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4941 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4942 isymp = finfo->internal_syms;
4943 indexp = finfo->sym_indices;
4944 csectpp = xcoff_data (input_bfd)->csects;
4945 outsym = finfo->outsyms;
4946 while (esym < esym_end)
4950 add = 1 + isymp->n_numaux;
4953 esym += add * isymesz;
4958 if (isymp->n_sclass == C_BSTAT)
4960 struct internal_syment isym;
4963 /* The value of a C_BSTAT symbol is the symbol table
4964 index of the containing csect. */
4965 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4966 indx = isym.n_value;
4967 if (indx < obj_raw_syment_count (input_bfd))
4971 symindx = finfo->sym_indices[indx];
4975 isym.n_value = symindx;
4976 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4984 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4986 union internal_auxent aux;
4988 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4989 isymp->n_sclass, i, isymp->n_numaux,
4992 if (isymp->n_sclass == C_FILE)
4994 /* This is the file name (or some comment put in by
4995 the compiler). If it is long, we must put it in
4996 the string table. */
4997 if (aux.x_file.x_n.x_zeroes == 0
4998 && aux.x_file.x_n.x_offset != 0)
5000 const char *filename;
5003 BFD_ASSERT (aux.x_file.x_n.x_offset
5004 >= STRING_SIZE_SIZE);
5005 if (strings == NULL)
5007 strings = _bfd_coff_read_string_table (input_bfd);
5008 if (strings == NULL)
5011 filename = strings + aux.x_file.x_n.x_offset;
5012 indx = _bfd_stringtab_add (finfo->strtab, filename,
5014 if (indx == (bfd_size_type) -1)
5016 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
5019 else if ((isymp->n_sclass == C_EXT
5020 || isymp->n_sclass == C_HIDEXT)
5021 && i + 1 == isymp->n_numaux)
5023 /* We don't support type checking. I don't know if
5025 aux.x_csect.x_parmhash = 0;
5026 /* I don't think anybody uses these fields, but we'd
5027 better clobber them just in case. */
5028 aux.x_csect.x_stab = 0;
5029 aux.x_csect.x_snstab = 0;
5030 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5034 indx = aux.x_csect.x_scnlen.l;
5035 if (indx < obj_raw_syment_count (input_bfd))
5039 symindx = finfo->sym_indices[indx];
5041 aux.x_sym.x_tagndx.l = 0;
5043 aux.x_sym.x_tagndx.l = symindx;
5047 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5051 if (ISFCN (isymp->n_type)
5052 || ISTAG (isymp->n_sclass)
5053 || isymp->n_sclass == C_BLOCK
5054 || isymp->n_sclass == C_FCN)
5056 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5058 && indx < obj_raw_syment_count (input_bfd))
5060 /* We look forward through the symbol for
5061 the index of the next symbol we are going
5062 to include. I don't know if this is
5064 while (finfo->sym_indices[indx] < 0
5065 && indx < obj_raw_syment_count (input_bfd))
5067 if (indx >= obj_raw_syment_count (input_bfd))
5068 indx = output_index;
5070 indx = finfo->sym_indices[indx];
5071 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5075 indx = aux.x_sym.x_tagndx.l;
5076 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5080 symindx = finfo->sym_indices[indx];
5082 aux.x_sym.x_tagndx.l = 0;
5084 aux.x_sym.x_tagndx.l = symindx;
5088 /* Copy over the line numbers, unless we are stripping
5089 them. We do this on a symbol by symbol basis in
5090 order to more easily handle garbage collection. */
5091 if ((isymp->n_sclass == C_EXT
5092 || isymp->n_sclass == C_HIDEXT)
5094 && isymp->n_numaux > 1
5095 && ISFCN (isymp->n_type)
5096 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5098 if (finfo->info->strip != strip_none
5099 && finfo->info->strip != strip_some)
5100 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5103 asection *enclosing;
5104 unsigned int enc_count;
5105 bfd_size_type linoff;
5106 struct internal_lineno lin;
5109 enclosing = xcoff_section_data (abfd, o)->enclosing;
5110 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5111 if (oline != enclosing)
5113 if (bfd_seek (input_bfd,
5114 enclosing->line_filepos,
5116 || (bfd_read (finfo->linenos, linesz,
5117 enc_count, input_bfd)
5118 != linesz * enc_count))
5123 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5124 - enclosing->line_filepos);
5126 bfd_coff_swap_lineno_in (input_bfd,
5127 (PTR) (finfo->linenos + linoff),
5130 || ((bfd_size_type) lin.l_addr.l_symndx
5134 obj_coff_external_syms (input_bfd)))
5136 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5139 bfd_byte *linpend, *linp;
5141 bfd_size_type count;
5143 lin.l_addr.l_symndx = *indexp;
5144 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
5145 (PTR) (finfo->linenos
5148 linpend = (finfo->linenos
5149 + enc_count * linesz);
5150 offset = (o->output_section->vma
5153 for (linp = finfo->linenos + linoff + linesz;
5157 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
5159 if (lin.l_lnno == 0)
5161 lin.l_addr.l_paddr += offset;
5162 bfd_coff_swap_lineno_out (output_bfd,
5167 count = (linp - (finfo->linenos + linoff)) / linesz;
5169 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5170 (o->output_section->line_filepos
5171 + o->output_section->lineno_count * linesz);
5173 if (bfd_seek (output_bfd,
5174 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5176 || (bfd_write (finfo->linenos + linoff,
5177 linesz, count, output_bfd)
5181 o->output_section->lineno_count += count;
5185 struct internal_syment *iisp, *iispend;
5190 /* Update any C_BINCL or C_EINCL symbols
5191 that refer to a line number in the
5192 range we just output. */
5193 iisp = finfo->internal_syms;
5195 + obj_raw_syment_count (input_bfd));
5196 iindp = finfo->sym_indices;
5197 oos = finfo->outsyms;
5198 while (iisp < iispend)
5201 && (iisp->n_sclass == C_BINCL
5202 || iisp->n_sclass == C_EINCL)
5203 && ((bfd_size_type) iisp->n_value
5204 >= enclosing->line_filepos + linoff)
5205 && ((bfd_size_type) iisp->n_value
5206 < (enclosing->line_filepos
5207 + enc_count * linesz)))
5209 struct internal_syment iis;
5211 bfd_coff_swap_sym_in (output_bfd,
5216 - enclosing->line_filepos
5218 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5219 bfd_coff_swap_sym_out (output_bfd,
5225 iiadd = 1 + iisp->n_numaux;
5227 oos += iiadd * osymesz;
5236 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5237 isymp->n_sclass, i, isymp->n_numaux,
5249 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5250 symbol will be the first symbol in the next input file. In the
5251 normal case, this will save us from writing out the C_FILE symbol
5253 if (finfo->last_file_index != -1
5254 && (bfd_size_type) finfo->last_file_index >= syment_base)
5256 finfo->last_file.n_value = output_index;
5257 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5258 (PTR) (finfo->outsyms
5259 + ((finfo->last_file_index - syment_base)
5263 /* Write the modified symbols to the output file. */
5264 if (outsym > finfo->outsyms)
5266 if (bfd_seek (output_bfd,
5267 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5269 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5271 != (bfd_size_type) (outsym - finfo->outsyms)))
5274 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5275 + (outsym - finfo->outsyms) / osymesz)
5278 obj_raw_syment_count (output_bfd) = output_index;
5281 /* Don't let the linker relocation routines discard the symbols. */
5282 keep_syms = obj_coff_keep_syms (input_bfd);
5283 obj_coff_keep_syms (input_bfd) = true;
5285 /* Relocate the contents of each section. */
5286 for (o = input_bfd->sections; o != NULL; o = o->next)
5290 if (! o->linker_mark)
5292 /* This section was omitted from the link. */
5296 if ((o->flags & SEC_HAS_CONTENTS) == 0
5297 || o->_raw_size == 0
5298 || (o->flags & SEC_IN_MEMORY) != 0)
5301 /* We have set filepos correctly for the sections we created to
5302 represent csects, so bfd_get_section_contents should work. */
5303 if (coff_section_data (input_bfd, o) != NULL
5304 && coff_section_data (input_bfd, o)->contents != NULL)
5305 contents = coff_section_data (input_bfd, o)->contents;
5308 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5309 (file_ptr) 0, o->_raw_size))
5311 contents = finfo->contents;
5314 if ((o->flags & SEC_RELOC) != 0)
5317 struct internal_reloc *internal_relocs;
5318 struct internal_reloc *irel;
5320 struct internal_reloc *irelend;
5321 struct xcoff_link_hash_entry **rel_hash;
5324 /* Read in the relocs. */
5325 target_index = o->output_section->target_index;
5326 internal_relocs = (xcoff_read_internal_relocs
5327 (input_bfd, o, false, finfo->external_relocs,
5329 (finfo->section_info[target_index].relocs
5330 + o->output_section->reloc_count)));
5331 if (internal_relocs == NULL)
5334 /* Call processor specific code to relocate the section
5336 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5340 finfo->internal_syms,
5341 xcoff_data (input_bfd)->csects))
5344 offset = o->output_section->vma + o->output_offset - o->vma;
5345 irel = internal_relocs;
5346 irelend = irel + o->reloc_count;
5347 rel_hash = (finfo->section_info[target_index].rel_hashes
5348 + o->output_section->reloc_count);
5349 for (; irel < irelend; irel++, rel_hash++)
5351 struct xcoff_link_hash_entry *h = NULL;
5352 struct internal_ldrel ldrel;
5357 /* Adjust the reloc address and symbol index. */
5359 irel->r_vaddr += offset;
5361 r_symndx = irel->r_symndx;
5366 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5368 if (r_symndx != -1 && finfo->info->strip != strip_all)
5371 && h->smclas != XMC_TD
5372 && (irel->r_type == R_TOC
5373 || irel->r_type == R_GL
5374 || irel->r_type == R_TCL
5375 || irel->r_type == R_TRL
5376 || irel->r_type == R_TRLA))
5378 /* This is a TOC relative reloc with a symbol
5379 attached. The symbol should be the one which
5380 this reloc is for. We want to make this
5381 reloc against the TOC address of the symbol,
5382 not the symbol itself. */
5383 BFD_ASSERT (h->toc_section != NULL);
5384 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5385 if (h->u.toc_indx != -1)
5386 irel->r_symndx = h->u.toc_indx;
5389 struct xcoff_toc_rel_hash *n;
5390 struct xcoff_link_section_info *si;
5392 n = ((struct xcoff_toc_rel_hash *)
5393 bfd_alloc (finfo->output_bfd,
5394 sizeof (struct xcoff_toc_rel_hash)));
5397 si = finfo->section_info + target_index;
5398 n->next = si->toc_rel_hashes;
5401 si->toc_rel_hashes = n;
5406 /* This is a global symbol. */
5408 irel->r_symndx = h->indx;
5411 /* This symbol is being written at the end
5412 of the file, and we do not yet know the
5413 symbol index. We save the pointer to the
5414 hash table entry in the rel_hash list.
5415 We set the indx field to -2 to indicate
5416 that this symbol must not be stripped. */
5425 indx = finfo->sym_indices[r_symndx];
5429 struct internal_syment *is;
5431 /* Relocations against a TC0 TOC anchor are
5432 automatically transformed to be against
5433 the TOC anchor in the output file. */
5434 is = finfo->internal_syms + r_symndx;
5435 if (is->n_sclass == C_HIDEXT
5436 && is->n_numaux > 0)
5439 union internal_auxent aux;
5443 obj_coff_external_syms (input_bfd))
5444 + ((r_symndx + is->n_numaux)
5446 bfd_coff_swap_aux_in (input_bfd, auxptr,
5447 is->n_type, is->n_sclass,
5451 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5452 && aux.x_csect.x_smclas == XMC_TC0)
5453 indx = finfo->toc_symindx;
5458 irel->r_symndx = indx;
5461 struct internal_syment *is;
5463 char buf[SYMNMLEN + 1];
5465 /* This reloc is against a symbol we are
5466 stripping. It would be possible to handle
5467 this case, but I don't think it's worth it. */
5468 is = finfo->internal_syms + r_symndx;
5470 name = (_bfd_coff_internal_syment_name
5471 (input_bfd, is, buf));
5475 if (! ((*finfo->info->callbacks->unattached_reloc)
5476 (finfo->info, name, input_bfd, o,
5484 switch (irel->r_type)
5488 || h->root.type == bfd_link_hash_defined
5489 || h->root.type == bfd_link_hash_defweak
5490 || h->root.type == bfd_link_hash_common)
5497 /* This reloc needs to be copied into the .loader
5499 ldrel.l_vaddr = irel->r_vaddr;
5501 ldrel.l_symndx = -1;
5503 || (h->root.type == bfd_link_hash_defined
5504 || h->root.type == bfd_link_hash_defweak
5505 || h->root.type == bfd_link_hash_common))
5510 sec = xcoff_data (input_bfd)->csects[r_symndx];
5511 else if (h->root.type == bfd_link_hash_common)
5512 sec = h->root.u.c.p->section;
5514 sec = h->root.u.def.section;
5515 sec = sec->output_section;
5517 if (strcmp (sec->name, ".text") == 0)
5519 else if (strcmp (sec->name, ".data") == 0)
5521 else if (strcmp (sec->name, ".bss") == 0)
5525 (*_bfd_error_handler)
5526 (_("%s: loader reloc in unrecognized section `%s'"),
5527 bfd_get_filename (input_bfd),
5529 bfd_set_error (bfd_error_nonrepresentable_section);
5535 if (! finfo->info->relocateable
5536 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5537 && (h->flags & XCOFF_IMPORT) == 0)
5539 /* We already called the undefined_symbol
5540 callback for this relocation, in
5541 _bfd_ppc_xcoff_relocate_section. Don't
5542 issue any more warnings. */
5545 if (h->ldindx < 0 && ! quiet)
5547 (*_bfd_error_handler)
5548 (_("%s: `%s' in loader reloc but not loader sym"),
5549 bfd_get_filename (input_bfd),
5550 h->root.root.string);
5551 bfd_set_error (bfd_error_bad_value);
5554 ldrel.l_symndx = h->ldindx;
5556 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5557 ldrel.l_rsecnm = o->output_section->target_index;
5558 if (xcoff_hash_table (finfo->info)->textro
5559 && strcmp (o->output_section->name, ".text") == 0
5562 (*_bfd_error_handler)
5563 (_("%s: loader reloc in read-only section %s"),
5564 bfd_get_filename (input_bfd),
5565 bfd_get_section_name (finfo->output_bfd,
5566 o->output_section));
5567 bfd_set_error (bfd_error_invalid_operation);
5570 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5572 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5581 /* We should never need a .loader reloc for a TOC
5587 o->output_section->reloc_count += o->reloc_count;
5590 /* Write out the modified section contents. */
5591 if (! bfd_set_section_contents (output_bfd, o->output_section,
5592 contents, o->output_offset,
5593 (o->_cooked_size != 0
5599 obj_coff_keep_syms (input_bfd) = keep_syms;
5601 if (! finfo->info->keep_memory)
5603 if (! _bfd_coff_free_symbols (input_bfd))
5613 /* Write out a non-XCOFF global symbol. */
5616 xcoff_write_global_symbol (h, p)
5617 struct xcoff_link_hash_entry *h;
5620 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5623 struct internal_syment isym;
5624 union internal_auxent aux;
5626 output_bfd = finfo->output_bfd;
5627 outsym = finfo->outsyms;
5629 /* If this symbol was garbage collected, just skip it. */
5630 if (xcoff_hash_table (finfo->info)->gc
5631 && (h->flags & XCOFF_MARK) == 0)
5634 /* If we need a .loader section entry, write it out. */
5635 if (h->ldsym != NULL)
5637 struct internal_ldsym *ldsym;
5642 if (h->root.type == bfd_link_hash_undefined
5643 || h->root.type == bfd_link_hash_undefweak)
5646 ldsym->l_scnum = N_UNDEF;
5647 ldsym->l_smtype = XTY_ER;
5648 impbfd = h->root.u.undef.abfd;
5650 else if (h->root.type == bfd_link_hash_defined
5651 || h->root.type == bfd_link_hash_defweak)
5655 sec = h->root.u.def.section;
5656 ldsym->l_value = (sec->output_section->vma
5657 + sec->output_offset
5658 + h->root.u.def.value);
5659 ldsym->l_scnum = sec->output_section->target_index;
5660 ldsym->l_smtype = XTY_SD;
5661 impbfd = sec->owner;
5666 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5667 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5668 || (h->flags & XCOFF_IMPORT) != 0)
5669 ldsym->l_smtype |= L_IMPORT;
5670 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5671 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5672 || (h->flags & XCOFF_EXPORT) != 0)
5673 ldsym->l_smtype |= L_EXPORT;
5674 if ((h->flags & XCOFF_ENTRY) != 0)
5675 ldsym->l_smtype |= L_ENTRY;
5677 ldsym->l_smclas = h->smclas;
5679 if (ldsym->l_ifile == (bfd_size_type) -1)
5681 else if (ldsym->l_ifile == 0)
5683 if ((ldsym->l_smtype & L_IMPORT) == 0)
5685 else if (impbfd == NULL)
5689 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5690 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5696 BFD_ASSERT (h->ldindx >= 0);
5697 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5698 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5702 /* If this symbol needs global linkage code, write it out. */
5703 if (h->root.type == bfd_link_hash_defined
5704 && (h->root.u.def.section
5705 == xcoff_hash_table (finfo->info)->linkage_section))
5711 p = h->root.u.def.section->contents + h->root.u.def.value;
5713 /* The first instruction in the global linkage code loads a
5714 specific TOC element. */
5715 tocoff = (h->descriptor->toc_section->output_section->vma
5716 + h->descriptor->toc_section->output_offset
5717 - xcoff_data (output_bfd)->toc);
5718 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5719 tocoff += h->descriptor->u.toc_offset;
5720 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5722 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5724 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5727 /* If we created a TOC entry for this symbol, write out the required
5729 if ((h->flags & XCOFF_SET_TOC) != 0)
5734 struct internal_reloc *irel;
5735 struct internal_ldrel ldrel;
5736 struct internal_syment irsym;
5737 union internal_auxent iraux;
5739 tocsec = h->toc_section;
5740 osec = tocsec->output_section;
5741 oindx = osec->target_index;
5742 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5743 irel->r_vaddr = (osec->vma
5744 + tocsec->output_offset
5747 irel->r_symndx = h->indx;
5751 irel->r_symndx = obj_raw_syment_count (output_bfd);
5753 irel->r_type = R_POS;
5755 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5756 ++osec->reloc_count;
5758 BFD_ASSERT (h->ldindx >= 0);
5759 ldrel.l_vaddr = irel->r_vaddr;
5760 ldrel.l_symndx = h->ldindx;
5761 ldrel.l_rtype = (31 << 8) | R_POS;
5762 ldrel.l_rsecnm = oindx;
5763 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5766 /* We need to emit a symbol to define a csect which holds the
5768 if (finfo->info->strip != strip_all)
5770 if (strlen (h->root.root.string) <= SYMNMLEN)
5771 strncpy (irsym._n._n_name, h->root.root.string, SYMNMLEN);
5778 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5780 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string,
5782 if (indx == (bfd_size_type) -1)
5784 irsym._n._n_n._n_zeroes = 0;
5785 irsym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5788 irsym.n_value = irel->r_vaddr;
5789 irsym.n_scnum = osec->target_index;
5790 irsym.n_sclass = C_HIDEXT;
5791 irsym.n_type = T_NULL;
5794 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5795 outsym += bfd_coff_symesz (output_bfd);
5797 memset (&iraux, 0, sizeof iraux);
5798 iraux.x_csect.x_smtyp = XTY_SD;
5799 iraux.x_csect.x_scnlen.l = 4;
5800 iraux.x_csect.x_smclas = XMC_TC;
5802 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5803 0, 1, (PTR) outsym);
5804 outsym += bfd_coff_auxesz (output_bfd);
5808 /* We aren't going to write out the symbols below, so we
5809 need to write them out now. */
5810 if (bfd_seek (output_bfd,
5811 (obj_sym_filepos (output_bfd)
5812 + (obj_raw_syment_count (output_bfd)
5813 * bfd_coff_symesz (output_bfd))),
5815 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5817 != (bfd_size_type) (outsym - finfo->outsyms)))
5819 obj_raw_syment_count (output_bfd) +=
5820 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5822 outsym = finfo->outsyms;
5827 /* If this symbol is a specially defined function descriptor, write
5828 it out. The first word is the address of the function code
5829 itself, the second word is the address of the TOC, and the third
5831 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5832 && h->root.type == bfd_link_hash_defined
5833 && (h->root.u.def.section
5834 == xcoff_hash_table (finfo->info)->descriptor_section))
5840 struct xcoff_link_hash_entry *hentry;
5842 struct internal_reloc *irel;
5843 struct internal_ldrel ldrel;
5846 sec = h->root.u.def.section;
5847 osec = sec->output_section;
5848 oindx = osec->target_index;
5849 p = sec->contents + h->root.u.def.value;
5851 hentry = h->descriptor;
5852 BFD_ASSERT (hentry != NULL
5853 && (hentry->root.type == bfd_link_hash_defined
5854 || hentry->root.type == bfd_link_hash_defweak));
5855 esec = hentry->root.u.def.section;
5856 bfd_put_32 (output_bfd,
5857 (esec->output_section->vma
5858 + esec->output_offset
5859 + hentry->root.u.def.value),
5862 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5863 irel->r_vaddr = (osec->vma
5864 + sec->output_offset
5865 + h->root.u.def.value);
5866 irel->r_symndx = esec->output_section->target_index;
5867 irel->r_type = R_POS;
5869 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5870 ++osec->reloc_count;
5872 ldrel.l_vaddr = irel->r_vaddr;
5873 if (strcmp (esec->output_section->name, ".text") == 0)
5875 else if (strcmp (esec->output_section->name, ".data") == 0)
5877 else if (strcmp (esec->output_section->name, ".bss") == 0)
5881 (*_bfd_error_handler)
5882 (_("%s: loader reloc in unrecognized section `%s'"),
5883 bfd_get_filename (output_bfd),
5884 esec->output_section->name);
5885 bfd_set_error (bfd_error_nonrepresentable_section);
5888 ldrel.l_rtype = (31 << 8) | R_POS;
5889 ldrel.l_rsecnm = oindx;
5890 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5893 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5895 tsec = coff_section_from_bfd_index (output_bfd,
5896 xcoff_data (output_bfd)->sntoc);
5899 irel->r_vaddr = (osec->vma
5900 + sec->output_offset
5901 + h->root.u.def.value
5903 irel->r_symndx = tsec->output_section->target_index;
5904 irel->r_type = R_POS;
5906 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5907 ++osec->reloc_count;
5909 ldrel.l_vaddr = irel->r_vaddr;
5910 if (strcmp (tsec->output_section->name, ".text") == 0)
5912 else if (strcmp (tsec->output_section->name, ".data") == 0)
5914 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5918 (*_bfd_error_handler)
5919 (_("%s: loader reloc in unrecognized section `%s'"),
5920 bfd_get_filename (output_bfd),
5921 tsec->output_section->name);
5922 bfd_set_error (bfd_error_nonrepresentable_section);
5925 ldrel.l_rtype = (31 << 8) | R_POS;
5926 ldrel.l_rsecnm = oindx;
5927 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5931 if (h->indx >= 0 || finfo->info->strip == strip_all)
5933 BFD_ASSERT (outsym == finfo->outsyms);
5938 && (finfo->info->strip == strip_all
5939 || (finfo->info->strip == strip_some
5940 && (bfd_hash_lookup (finfo->info->keep_hash,
5941 h->root.root.string, false, false)
5944 BFD_ASSERT (outsym == finfo->outsyms);
5949 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5951 BFD_ASSERT (outsym == finfo->outsyms);
5955 memset (&aux, 0, sizeof aux);
5957 h->indx = obj_raw_syment_count (output_bfd);
5959 if (strlen (h->root.root.string) <= SYMNMLEN)
5960 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5967 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5969 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5971 if (indx == (bfd_size_type) -1)
5973 isym._n._n_n._n_zeroes = 0;
5974 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5977 if (h->root.type == bfd_link_hash_undefined
5978 || h->root.type == bfd_link_hash_undefweak)
5981 isym.n_scnum = N_UNDEF;
5982 isym.n_sclass = C_EXT;
5983 aux.x_csect.x_smtyp = XTY_ER;
5985 else if ((h->root.type == bfd_link_hash_defined
5986 || h->root.type == bfd_link_hash_defweak)
5987 && h->smclas == XMC_XO)
5989 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5990 isym.n_value = h->root.u.def.value;
5991 isym.n_scnum = N_UNDEF;
5992 isym.n_sclass = C_EXT;
5993 aux.x_csect.x_smtyp = XTY_ER;
5995 else if (h->root.type == bfd_link_hash_defined
5996 || h->root.type == bfd_link_hash_defweak)
5998 struct xcoff_link_size_list *l;
6000 isym.n_value = (h->root.u.def.section->output_section->vma
6001 + h->root.u.def.section->output_offset
6002 + h->root.u.def.value);
6003 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6004 isym.n_sclass = C_HIDEXT;
6005 aux.x_csect.x_smtyp = XTY_SD;
6007 if ((h->flags & XCOFF_HAS_SIZE) != 0)
6009 for (l = xcoff_hash_table (finfo->info)->size_list;
6015 aux.x_csect.x_scnlen.l = l->size;
6021 else if (h->root.type == bfd_link_hash_common)
6023 isym.n_value = (h->root.u.c.p->section->output_section->vma
6024 + h->root.u.c.p->section->output_offset);
6025 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6026 isym.n_sclass = C_EXT;
6027 aux.x_csect.x_smtyp = XTY_CM;
6028 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6033 isym.n_type = T_NULL;
6036 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6037 outsym += bfd_coff_symesz (output_bfd);
6039 aux.x_csect.x_smclas = h->smclas;
6041 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
6043 outsym += bfd_coff_auxesz (output_bfd);
6045 if ((h->root.type == bfd_link_hash_defined
6046 || h->root.type == bfd_link_hash_defweak)
6047 && h->smclas != XMC_XO)
6049 /* We just output an SD symbol. Now output an LD symbol. */
6053 isym.n_sclass = C_EXT;
6054 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6055 outsym += bfd_coff_symesz (output_bfd);
6057 aux.x_csect.x_smtyp = XTY_LD;
6058 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6060 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
6062 outsym += bfd_coff_auxesz (output_bfd);
6065 if (bfd_seek (output_bfd,
6066 (obj_sym_filepos (output_bfd)
6067 + (obj_raw_syment_count (output_bfd)
6068 * bfd_coff_symesz (output_bfd))),
6070 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
6071 != (bfd_size_type) (outsym - finfo->outsyms)))
6073 obj_raw_syment_count (output_bfd) +=
6074 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
6079 /* Handle a link order which is supposed to generate a reloc. */
6082 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
6084 struct xcoff_final_link_info *finfo;
6085 asection *output_section;
6086 struct bfd_link_order *link_order;
6088 reloc_howto_type *howto;
6089 struct xcoff_link_hash_entry *h;
6093 struct internal_reloc *irel;
6094 struct xcoff_link_hash_entry **rel_hash_ptr;
6095 struct internal_ldrel ldrel;
6097 if (link_order->type == bfd_section_reloc_link_order)
6099 /* We need to somehow locate a symbol in the right section. The
6100 symbol must either have a value of zero, or we must adjust
6101 the addend by the value of the symbol. FIXME: Write this
6102 when we need it. The old linker couldn't handle this anyhow. */
6106 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6109 bfd_set_error (bfd_error_bad_value);
6113 h = ((struct xcoff_link_hash_entry *)
6114 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
6115 link_order->u.reloc.p->u.name,
6116 false, false, true));
6119 if (! ((*finfo->info->callbacks->unattached_reloc)
6120 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6121 (asection *) NULL, (bfd_vma) 0)))
6126 if (h->root.type == bfd_link_hash_common)
6128 hsec = h->root.u.c.p->section;
6131 else if (h->root.type == bfd_link_hash_defined
6132 || h->root.type == bfd_link_hash_defweak)
6134 hsec = h->root.u.def.section;
6135 hval = h->root.u.def.value;
6143 addend = link_order->u.reloc.p->addend;
6145 addend += (hsec->output_section->vma
6146 + hsec->output_offset
6153 bfd_reloc_status_type rstat;
6156 size = bfd_get_reloc_size (howto);
6157 buf = (bfd_byte *) bfd_zmalloc (size);
6161 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6167 case bfd_reloc_outofrange:
6169 case bfd_reloc_overflow:
6170 if (! ((*finfo->info->callbacks->reloc_overflow)
6171 (finfo->info, link_order->u.reloc.p->u.name,
6172 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6180 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6181 (file_ptr) link_order->offset, size);
6187 /* Store the reloc information in the right place. It will get
6188 swapped and written out at the end of the final_link routine. */
6190 irel = (finfo->section_info[output_section->target_index].relocs
6191 + output_section->reloc_count);
6192 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6193 + output_section->reloc_count);
6195 memset (irel, 0, sizeof (struct internal_reloc));
6196 *rel_hash_ptr = NULL;
6198 irel->r_vaddr = output_section->vma + link_order->offset;
6201 irel->r_symndx = h->indx;
6204 /* Set the index to -2 to force this symbol to get written out. */
6210 irel->r_type = howto->type;
6211 irel->r_size = howto->bitsize - 1;
6212 if (howto->complain_on_overflow == complain_overflow_signed)
6213 irel->r_size |= 0x80;
6215 ++output_section->reloc_count;
6217 /* Now output the reloc to the .loader section. */
6219 ldrel.l_vaddr = irel->r_vaddr;
6223 const char *secname;
6225 secname = hsec->output_section->name;
6227 if (strcmp (secname, ".text") == 0)
6229 else if (strcmp (secname, ".data") == 0)
6231 else if (strcmp (secname, ".bss") == 0)
6235 (*_bfd_error_handler)
6236 (_("%s: loader reloc in unrecognized section `%s'"),
6237 bfd_get_filename (output_bfd), secname);
6238 bfd_set_error (bfd_error_nonrepresentable_section);
6246 (*_bfd_error_handler)
6247 (_("%s: `%s' in loader reloc but not loader sym"),
6248 bfd_get_filename (output_bfd),
6249 h->root.root.string);
6250 bfd_set_error (bfd_error_bad_value);
6253 ldrel.l_symndx = h->ldindx;
6256 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6257 ldrel.l_rsecnm = output_section->target_index;
6258 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6264 /* Sort relocs by VMA. This is called via qsort. */
6267 xcoff_sort_relocs (p1, p2)
6271 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6272 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6274 if (r1->r_vaddr > r2->r_vaddr)
6276 else if (r1->r_vaddr < r2->r_vaddr)
6282 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6283 This is currently the only processor which uses XCOFF; I hope that
6284 will never change. */
6287 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6288 input_section, contents, relocs, syms,
6291 struct bfd_link_info *info;
6293 asection *input_section;
6295 struct internal_reloc *relocs;
6296 struct internal_syment *syms;
6297 asection **sections;
6299 struct internal_reloc *rel;
6300 struct internal_reloc *relend;
6303 relend = rel + input_section->reloc_count;
6304 for (; rel < relend; rel++)
6307 struct xcoff_link_hash_entry *h;
6308 struct internal_syment *sym;
6311 struct reloc_howto_struct howto;
6312 bfd_reloc_status_type rstat;
6314 /* Relocation type R_REF is a special relocation type which is
6315 merely used to prevent garbage collection from occurring for
6316 the csect including the symbol which it references. */
6317 if (rel->r_type == R_REF)
6320 symndx = rel->r_symndx;
6330 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6331 sym = syms + symndx;
6332 addend = - sym->n_value;
6335 /* We build the howto information on the fly. */
6337 howto.type = rel->r_type;
6338 howto.rightshift = 0;
6340 howto.bitsize = (rel->r_size & 0x1f) + 1;
6341 howto.pc_relative = false;
6343 if ((rel->r_size & 0x80) != 0)
6344 howto.complain_on_overflow = complain_overflow_signed;
6346 howto.complain_on_overflow = complain_overflow_bitfield;
6347 howto.special_function = NULL;
6348 howto.name = "internal";
6349 howto.partial_inplace = true;
6350 if (howto.bitsize == 32)
6351 howto.src_mask = howto.dst_mask = 0xffffffff;
6354 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6355 if (howto.bitsize == 16)
6358 howto.pcrel_offset = false;
6368 sec = bfd_abs_section_ptr;
6373 sec = sections[symndx];
6374 /* Hack to make sure we use the right TOC anchor value
6375 if this reloc is against the TOC anchor. */
6376 if (sec->name[3] == '0'
6377 && strcmp (sec->name, ".tc0") == 0)
6378 val = xcoff_data (output_bfd)->toc;
6380 val = (sec->output_section->vma
6381 + sec->output_offset
6388 if (h->root.type == bfd_link_hash_defined
6389 || h->root.type == bfd_link_hash_defweak)
6393 sec = h->root.u.def.section;
6394 val = (h->root.u.def.value
6395 + sec->output_section->vma
6396 + sec->output_offset);
6398 else if (h->root.type == bfd_link_hash_common)
6402 sec = h->root.u.c.p->section;
6403 val = (sec->output_section->vma
6404 + sec->output_offset);
6406 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6407 || (h->flags & XCOFF_IMPORT) != 0)
6409 /* Every symbol in a shared object is defined somewhere. */
6412 else if (! info->relocateable)
6414 if (! ((*info->callbacks->undefined_symbol)
6415 (info, h->root.root.string, input_bfd, input_section,
6416 rel->r_vaddr - input_section->vma, true)))
6419 /* Don't try to process the reloc. It can't help, and
6420 it may generate another error. */
6425 /* I took the relocation type definitions from two documents:
6426 the PowerPC AIX Version 4 Application Binary Interface, First
6427 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6428 32-Bit Hardware Implementation (June 30, 1994). Differences
6429 between the documents are noted below. */
6431 switch (rel->r_type)
6436 /* These relocs are defined by the PowerPC ABI to be
6437 relative branches which use half of the difference
6438 between the symbol and the program counter. I can't
6439 quite figure out when this is useful. These relocs are
6440 not defined by the PowerOpen ABI. */
6442 (*_bfd_error_handler)
6443 (_("%s: unsupported relocation type 0x%02x"),
6444 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6445 bfd_set_error (bfd_error_bad_value);
6448 /* Simple positive relocation. */
6451 /* Simple negative relocation. */
6455 /* Simple PC relative relocation. */
6456 howto.pc_relative = true;
6459 /* TOC relative relocation. The value in the instruction in
6460 the input file is the offset from the input file TOC to
6461 the desired location. We want the offset from the final
6462 TOC to the desired location. We have:
6467 so we must change insn by on - in.
6470 /* Global linkage relocation. The value of this relocation
6471 is the address of the entry in the TOC section. */
6473 /* Local object TOC address. I can't figure out the
6474 difference between this and case R_GL. */
6476 /* TOC relative relocation. A TOC relative load instruction
6477 which may be changed to a load address instruction.
6478 FIXME: We don't currently implement this optimization. */
6480 /* TOC relative relocation. This is a TOC relative load
6481 address instruction which may be changed to a load
6482 instruction. FIXME: I don't know if this is the correct
6484 if (h != NULL && h->smclas != XMC_TD)
6486 if (h->toc_section == NULL)
6488 (*_bfd_error_handler)
6489 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6490 bfd_get_filename (input_bfd), rel->r_vaddr,
6491 h->root.root.string);
6492 bfd_set_error (bfd_error_bad_value);
6496 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6497 val = (h->toc_section->output_section->vma
6498 + h->toc_section->output_offset);
6501 val = ((val - xcoff_data (output_bfd)->toc)
6502 - (sym->n_value - xcoff_data (input_bfd)->toc));
6506 /* Absolute branch. We don't want to mess with the lower
6507 two bits of the instruction. */
6509 /* The PowerPC ABI defines this as an absolute call which
6510 may be modified to become a relative call. The PowerOpen
6511 ABI does not define this relocation type. */
6513 /* Absolute branch which may be modified to become a
6516 /* The PowerPC ABI defines this as an absolute branch to a
6517 fixed address which may be modified to an absolute branch
6518 to a symbol. The PowerOpen ABI does not define this
6521 /* The PowerPC ABI defines this as an absolute branch to a
6522 fixed address which may be modified to a relative branch.
6523 The PowerOpen ABI does not define this relocation type. */
6524 howto.src_mask &= ~3;
6525 howto.dst_mask = howto.src_mask;
6528 /* Relative branch. We don't want to mess with the lower
6529 two bits of the instruction. */
6531 /* The PowerPC ABI defines this as a relative call which may
6532 be modified to become an absolute call. The PowerOpen
6533 ABI does not define this relocation type. */
6535 /* A relative branch which may be modified to become an
6536 absolute branch. FIXME: We don't implement this,
6537 although we should for symbols of storage mapping class
6539 howto.pc_relative = true;
6540 howto.src_mask &= ~3;
6541 howto.dst_mask = howto.src_mask;
6544 /* The PowerPC AIX ABI describes this as a load which may be
6545 changed to a load address. The PowerOpen ABI says this
6546 is the same as case R_POS. */
6549 /* The PowerPC AIX ABI describes this as a load address
6550 which may be changed to a load. The PowerOpen ABI says
6551 this is the same as R_POS. */
6555 /* If we see an R_BR or R_RBR reloc which is jumping to global
6556 linkage code, and it is followed by an appropriate cror nop
6557 instruction, we replace the cror with lwz r2,20(r1). This
6558 restores the TOC after the glink code. Contrariwise, if the
6559 call is followed by a lwz r2,20(r1), but the call is not
6560 going to global linkage code, we can replace the load with a
6562 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6564 && h->root.type == bfd_link_hash_defined
6565 && (rel->r_vaddr - input_section->vma + 8
6566 <= input_section->_cooked_size))
6571 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6572 next = bfd_get_32 (input_bfd, pnext);
6574 /* The _ptrgl function is magic. It is used by the AIX
6575 compiler to call a function through a pointer. */
6576 if (h->smclas == XMC_GL
6577 || strcmp (h->root.root.string, "._ptrgl") == 0)
6579 if (next == 0x4def7b82 /* cror 15,15,15 */
6580 || next == 0x4ffffb82) /* cror 31,31,31 */
6581 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6585 if (next == 0x80410014) /* lwz r1,20(r1) */
6586 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
6590 /* A PC relative reloc includes the section address. */
6591 if (howto.pc_relative)
6592 addend += input_section->vma;
6594 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6596 rel->r_vaddr - input_section->vma,
6605 case bfd_reloc_overflow:
6608 char buf[SYMNMLEN + 1];
6609 char howto_name[10];
6614 name = h->root.root.string;
6617 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6621 sprintf (howto_name, "0x%02x", rel->r_type);
6623 if (! ((*info->callbacks->reloc_overflow)
6624 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6625 input_section, rel->r_vaddr - input_section->vma)))