1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996 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 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "xcoff-powermac") == 0)
40 /* Get the XCOFF hash table entries for a BFD. */
41 #define obj_xcoff_sym_hashes(bfd) \
42 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
44 /* XCOFF relocation types. These probably belong in a header file
45 somewhere. The relocations are described in the function
46 _bfd_ppc_xcoff_relocate_section in this file. */
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
71 /* The first word of global linkage code. This must be modified by
72 filling in the correct TOC offset. */
74 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
76 /* The remaining words of global linkage code. */
78 static unsigned long xcoff_glink_code[] =
80 0x90410014, /* stw r2,20(r1) */
81 0x800c0000, /* lwz r0,0(r12) */
82 0x804c0004, /* lwz r2,4(r12) */
83 0x7c0903a6, /* mtctr r0 */
84 0x4e800420, /* bctr */
85 0x0, /* start of traceback table */
86 0x000c8000, /* traceback table */
87 0x0 /* traceback table */
90 #define XCOFF_GLINK_SIZE \
91 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
93 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
94 This flag will only be used on input sections. */
96 #define SEC_MARK (SEC_ROM)
98 /* The ldhdr structure. This appears at the start of the .loader
101 struct internal_ldhdr
103 /* The version number: currently always 1. */
104 unsigned long l_version;
105 /* The number of symbol table entries. */
106 bfd_size_type l_nsyms;
107 /* The number of relocation table entries. */
108 bfd_size_type l_nreloc;
109 /* The length of the import file string table. */
110 bfd_size_type l_istlen;
111 /* The number of import files. */
112 bfd_size_type l_nimpid;
113 /* The offset from the start of the .loader section to the first
114 entry in the import file table. */
115 bfd_size_type l_impoff;
116 /* The length of the string table. */
117 bfd_size_type l_stlen;
118 /* The offset from the start of the .loader section to the first
119 entry in the string table. */
120 bfd_size_type l_stoff;
123 struct external_ldhdr
125 bfd_byte l_version[4];
127 bfd_byte l_nreloc[4];
128 bfd_byte l_istlen[4];
129 bfd_byte l_nimpid[4];
130 bfd_byte l_impoff[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
140 struct internal_ldsym
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name[SYMNMLEN];
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
155 /* The symbol value. */
157 /* The symbol section number. */
159 /* The symbol type and flags. */
161 /* The symbol storage class. */
163 /* The import file ID. */
164 bfd_size_type l_ifile;
165 /* Offset to the parameter type check string. */
166 bfd_size_type l_parm;
169 struct external_ldsym
173 bfd_byte _l_name[SYMNMLEN];
176 bfd_byte _l_zeroes[4];
177 bfd_byte _l_offset[4];
182 bfd_byte l_smtype[1];
183 bfd_byte l_smclas[1];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
196 #define L_ENTRY (0x20)
197 /* Exported symbol. */
198 #define L_EXPORT (0x10)
200 /* The ldrel structure. This is used to represent a reloc in the
203 struct internal_ldrel
205 /* The reloc address. */
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx;
209 /* The relocation type and size. */
211 /* The section number this relocation applies to. */
215 struct external_ldrel
218 bfd_byte l_symndx[4];
220 bfd_byte l_rsecnm[2];
223 #define LDRELSZ (2 * 4 + 2 * 2)
225 /* The list of import files. */
227 struct xcoff_import_file
229 /* The next entry in the list. */
230 struct xcoff_import_file *next;
235 /* The member name. */
239 /* An entry in the XCOFF linker hash table. */
241 struct xcoff_link_hash_entry
243 struct bfd_link_hash_entry root;
245 /* Symbol index in output file. Set to -1 initially. Set to -2 if
246 there is a reloc against this symbol. */
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection *toc_section;
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
258 /* If the TOC entry comes from an input file, this is set to the
259 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
263 /* If this symbol is a function entry point which is called, this
264 field holds a pointer to the function descriptor. If this symbol
265 is a function descriptor, this field holds a pointer to the
266 function entry point. */
267 struct xcoff_link_hash_entry *descriptor;
269 /* The .loader symbol table entry, if there is one. */
270 struct internal_ldsym *ldsym;
272 /* The .loader symbol table index. */
275 /* Some linker flags. */
276 unsigned short flags;
277 /* Symbol is referenced by a regular object. */
278 #define XCOFF_REF_REGULAR (01)
279 /* Symbol is defined by a regular object. */
280 #define XCOFF_DEF_REGULAR (02)
281 /* Symbol is defined by a dynamic object. */
282 #define XCOFF_DEF_DYNAMIC (04)
283 /* Symbol is used in a reloc being copied into the .loader section. */
284 #define XCOFF_LDREL (010)
285 /* Symbol is the entry point. */
286 #define XCOFF_ENTRY (020)
287 /* Symbol is called; this is, it appears in a R_BR reloc. */
288 #define XCOFF_CALLED (040)
289 /* Symbol needs the TOC entry filled in. */
290 #define XCOFF_SET_TOC (0100)
291 /* Symbol is explicitly imported. */
292 #define XCOFF_IMPORT (0200)
293 /* Symbol is explicitly exported. */
294 #define XCOFF_EXPORT (0400)
295 /* Symbol has been processed by xcoff_build_ldsyms. */
296 #define XCOFF_BUILT_LDSYM (01000)
297 /* Symbol is mentioned by a section which was not garbage collected. */
298 #define XCOFF_MARK (02000)
299 /* Symbol size is recorded in size_list list from hash table. */
300 #define XCOFF_HAS_SIZE (04000)
301 /* Symbol is a function descriptor. */
302 #define XCOFF_DESCRIPTOR (010000)
304 /* The storage mapping class. */
305 unsigned char smclas;
308 /* The XCOFF linker hash table. */
310 struct xcoff_link_hash_table
312 struct bfd_link_hash_table root;
314 /* The .debug string hash table. We need to compute this while
315 reading the input files, so that we know how large the .debug
316 section will be before we assign section positions. */
317 struct bfd_strtab_hash *debug_strtab;
319 /* The .debug section we will use for the final output. */
320 asection *debug_section;
322 /* The .loader section we will use for the final output. */
323 asection *loader_section;
325 /* A count of non TOC relative relocs which will need to be
326 allocated in the .loader section. */
329 /* The .loader section header. */
330 struct internal_ldhdr ldhdr;
332 /* The .gl section we use to hold global linkage code. */
333 asection *linkage_section;
335 /* The .tc section we use to hold toc entries we build for global
337 asection *toc_section;
339 /* The .ds section we use to hold function descriptors which we
340 create for exported symbols. */
341 asection *descriptor_section;
343 /* The list of import files. */
344 struct xcoff_import_file *imports;
346 /* Required alignment of sections within the output file. */
347 unsigned long file_align;
349 /* Whether the .text section must be read-only. */
352 /* Whether garbage collection was done. */
355 /* A linked list of symbols for which we have size information. */
356 struct xcoff_link_size_list
358 struct xcoff_link_size_list *next;
359 struct xcoff_link_hash_entry *h;
363 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
364 asection *special_sections[6];
367 /* Information we keep for each section in the output file during the
370 struct xcoff_link_section_info
372 /* The relocs to be output. */
373 struct internal_reloc *relocs;
374 /* For each reloc against a global symbol whose index was not known
375 when the reloc was handled, the global hash table entry. */
376 struct xcoff_link_hash_entry **rel_hashes;
377 /* If there is a TOC relative reloc against a global symbol, and the
378 index of the TOC symbol is not known when the reloc was handled,
379 an entry is added to this linked list. This is not an array,
380 like rel_hashes, because this case is quite uncommon. */
381 struct xcoff_toc_rel_hash
383 struct xcoff_toc_rel_hash *next;
384 struct xcoff_link_hash_entry *h;
385 struct internal_reloc *rel;
389 /* Information that we pass around while doing the final link step. */
391 struct xcoff_final_link_info
393 /* General link information. */
394 struct bfd_link_info *info;
397 /* Hash table for long symbol names. */
398 struct bfd_strtab_hash *strtab;
399 /* Array of information kept for each output section, indexed by the
400 target_index field. */
401 struct xcoff_link_section_info *section_info;
402 /* Symbol index of last C_FILE symbol (-1 if none). */
403 long last_file_index;
404 /* Contents of last C_FILE symbol. */
405 struct internal_syment last_file;
406 /* Symbol index of TOC symbol. */
408 /* Start of .loader symbols. */
409 struct external_ldsym *ldsym;
410 /* Next .loader reloc to swap out. */
411 struct external_ldrel *ldrel;
412 /* File position of start of line numbers. */
413 file_ptr line_filepos;
414 /* Buffer large enough to hold swapped symbols of any input file. */
415 struct internal_syment *internal_syms;
416 /* Buffer large enough to hold output indices of symbols of any
419 /* Buffer large enough to hold output symbols for any input file. */
421 /* Buffer large enough to hold external line numbers for any input
424 /* Buffer large enough to hold any input section. */
426 /* Buffer large enough to hold external relocs of any input section. */
427 bfd_byte *external_relocs;
430 static void xcoff_swap_ldhdr_in
431 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
432 static void xcoff_swap_ldhdr_out
433 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
434 static void xcoff_swap_ldsym_in
435 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
436 static void xcoff_swap_ldsym_out
437 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
438 static void xcoff_swap_ldrel_in
439 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
440 static void xcoff_swap_ldrel_out
441 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
442 static struct bfd_hash_entry *xcoff_link_hash_newfunc
443 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
444 static struct internal_reloc *xcoff_read_internal_relocs
445 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
446 struct internal_reloc *));
447 static boolean xcoff_link_add_object_symbols
448 PARAMS ((bfd *, struct bfd_link_info *));
449 static boolean xcoff_link_check_archive_element
450 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
451 static boolean xcoff_link_check_ar_symbols
452 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
453 static boolean xcoff_link_check_dynamic_ar_symbols
454 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
455 static bfd_size_type xcoff_find_reloc
456 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
457 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
458 static boolean xcoff_link_add_dynamic_symbols
459 PARAMS ((bfd *, struct bfd_link_info *));
460 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
461 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
462 static boolean xcoff_build_ldsyms
463 PARAMS ((struct xcoff_link_hash_entry *, PTR));
464 static boolean xcoff_link_input_bfd
465 PARAMS ((struct xcoff_final_link_info *, bfd *));
466 static boolean xcoff_write_global_symbol
467 PARAMS ((struct xcoff_link_hash_entry *, PTR));
468 static boolean xcoff_reloc_link_order
469 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
470 struct bfd_link_order *));
471 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
473 /* Routines to swap information in the XCOFF .loader section. If we
474 ever need to write an XCOFF loader, this stuff will need to be
475 moved to another file shared by the linker (which XCOFF calls the
476 ``binder'') and the loader. */
478 /* Swap in the ldhdr structure. */
481 xcoff_swap_ldhdr_in (abfd, src, dst)
483 const struct external_ldhdr *src;
484 struct internal_ldhdr *dst;
486 dst->l_version = bfd_get_32 (abfd, src->l_version);
487 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
488 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
489 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
490 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
491 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
492 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
493 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
496 /* Swap out the ldhdr structure. */
499 xcoff_swap_ldhdr_out (abfd, src, dst)
501 const struct internal_ldhdr *src;
502 struct external_ldhdr *dst;
504 bfd_put_32 (abfd, src->l_version, dst->l_version);
505 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
506 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
507 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
508 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
509 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
510 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
511 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
514 /* Swap in the ldsym structure. */
517 xcoff_swap_ldsym_in (abfd, src, dst)
519 const struct external_ldsym *src;
520 struct internal_ldsym *dst;
522 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
523 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
526 dst->_l._l_l._l_zeroes = 0;
527 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
529 dst->l_value = bfd_get_32 (abfd, src->l_value);
530 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
531 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
532 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
533 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
534 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
537 /* Swap out the ldsym structure. */
540 xcoff_swap_ldsym_out (abfd, src, dst)
542 const struct internal_ldsym *src;
543 struct external_ldsym *dst;
545 if (src->_l._l_l._l_zeroes != 0)
546 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
549 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
550 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
552 bfd_put_32 (abfd, src->l_value, dst->l_value);
553 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
554 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
555 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
556 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
557 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
560 /* Swap in the ldrel structure. */
563 xcoff_swap_ldrel_in (abfd, src, dst)
565 const struct external_ldrel *src;
566 struct internal_ldrel *dst;
568 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
569 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
570 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
571 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
574 /* Swap out the ldrel structure. */
577 xcoff_swap_ldrel_out (abfd, src, dst)
579 const struct internal_ldrel *src;
580 struct external_ldrel *dst;
582 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
583 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
584 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
585 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
588 /* Routines to read XCOFF dynamic information. This don't really
589 belong here, but we already have the ldsym manipulation routines
592 /* Read the contents of a section. */
595 xcoff_get_section_contents (abfd, sec)
599 if (coff_section_data (abfd, sec) == NULL)
601 sec->used_by_bfd = bfd_zalloc (abfd,
602 sizeof (struct coff_section_tdata));
603 if (sec->used_by_bfd == NULL)
607 if (coff_section_data (abfd, sec)->contents == NULL)
609 coff_section_data (abfd, sec)->contents = 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 = 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);
1065 /* We need to look through the archive for stripped dynamic
1066 objects, because they will not appear in the archive map even
1067 though they should, perhaps, be included. Also, if the
1068 linker has no map, we just consider each object file in turn,
1069 since that apparently is what the AIX native linker does. */
1073 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1074 while (member != NULL)
1076 if (bfd_check_format (member, bfd_object)
1077 && (! bfd_has_map (abfd)
1078 || ((member->flags & DYNAMIC) != 0
1079 && (member->flags & HAS_SYMS) == 0)))
1083 if (! xcoff_link_check_archive_element (member, info, &needed))
1086 member->archive_pass = -1;
1088 member = bfd_openr_next_archived_file (abfd, member);
1091 if (! bfd_has_map (abfd))
1094 /* Now do the usual search. */
1095 return (_bfd_generic_link_add_archive_symbols
1096 (abfd, info, xcoff_link_check_archive_element));
1100 bfd_set_error (bfd_error_wrong_format);
1105 /* Add symbols from an XCOFF object file. */
1108 xcoff_link_add_object_symbols (abfd, info)
1110 struct bfd_link_info *info;
1112 if (! _bfd_coff_get_external_symbols (abfd))
1114 if (! xcoff_link_add_symbols (abfd, info))
1116 if (! info->keep_memory)
1118 if (! _bfd_coff_free_symbols (abfd))
1124 /* Check a single archive element to see if we need to include it in
1125 the link. *PNEEDED is set according to whether this element is
1126 needed in the link or not. This is called via
1127 _bfd_generic_link_add_archive_symbols. */
1130 xcoff_link_check_archive_element (abfd, info, pneeded)
1132 struct bfd_link_info *info;
1135 if (! _bfd_coff_get_external_symbols (abfd))
1138 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1143 if (! xcoff_link_add_symbols (abfd, info))
1147 if (! info->keep_memory || ! *pneeded)
1149 if (! _bfd_coff_free_symbols (abfd))
1156 /* Look through the symbols to see if this object file should be
1157 included in the link. */
1160 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1162 struct bfd_link_info *info;
1165 bfd_size_type symesz;
1171 if ((abfd->flags & DYNAMIC) != 0
1172 && ! info->static_link
1173 && info->hash->creator == abfd->xvec)
1174 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1176 symesz = bfd_coff_symesz (abfd);
1177 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1178 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1179 while (esym < esym_end)
1181 struct internal_syment sym;
1183 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1185 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1188 char buf[SYMNMLEN + 1];
1189 struct bfd_link_hash_entry *h;
1191 /* This symbol is externally visible, and is defined by this
1194 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1197 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1199 /* We are only interested in symbols that are currently
1200 undefined. If a symbol is currently known to be common,
1201 XCOFF linkers do not bring in an object file which
1202 defines it. We also don't bring in symbols to satisfy
1203 undefined references in shared objects. */
1204 if (h != (struct bfd_link_hash_entry *) NULL
1205 && h->type == bfd_link_hash_undefined
1206 && (info->hash->creator != abfd->xvec
1207 || (((struct xcoff_link_hash_entry *) h)->flags
1208 & XCOFF_DEF_DYNAMIC) == 0))
1210 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1217 esym += (sym.n_numaux + 1) * symesz;
1220 /* We do not need this object file. */
1224 /* Look through the loader symbols to see if this dynamic object
1225 should be included in the link. The native linker uses the loader
1226 symbols, not the normal symbol table, so we do too. */
1229 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1231 struct bfd_link_info *info;
1236 struct internal_ldhdr ldhdr;
1237 const char *strings;
1238 struct external_ldsym *elsym, *elsymend;
1242 lsec = bfd_get_section_by_name (abfd, ".loader");
1245 /* There are no symbols, so don't try to include it. */
1249 if (! xcoff_get_section_contents (abfd, lsec))
1251 buf = coff_section_data (abfd, lsec)->contents;
1253 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1255 strings = (char *) buf + ldhdr.l_stoff;
1257 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1258 elsymend = elsym + ldhdr.l_nsyms;
1259 for (; elsym < elsymend; elsym++)
1261 struct internal_ldsym ldsym;
1262 char nambuf[SYMNMLEN + 1];
1264 struct bfd_link_hash_entry *h;
1266 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1268 /* We are only interested in exported symbols. */
1269 if ((ldsym.l_smtype & L_EXPORT) == 0)
1272 if (ldsym._l._l_l._l_zeroes == 0)
1273 name = strings + ldsym._l._l_l._l_offset;
1276 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1277 nambuf[SYMNMLEN] = '\0';
1281 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1283 /* We are only interested in symbols that are currently
1284 undefined. At this point we know that we are using an XCOFF
1287 && h->type == bfd_link_hash_undefined
1288 && (((struct xcoff_link_hash_entry *) h)->flags
1289 & XCOFF_DEF_DYNAMIC) == 0)
1291 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1298 /* We do not need this shared object. */
1300 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1302 free (coff_section_data (abfd, lsec)->contents);
1303 coff_section_data (abfd, lsec)->contents = NULL;
1309 /* Returns the index of reloc in RELOCS with the least address greater
1310 than or equal to ADDRESS. The relocs are sorted by address. */
1312 static bfd_size_type
1313 xcoff_find_reloc (relocs, count, address)
1314 struct internal_reloc *relocs;
1315 bfd_size_type count;
1318 bfd_size_type min, max, this;
1322 if (count == 1 && relocs[0].r_vaddr < address)
1331 /* Do a binary search over (min,max]. */
1332 while (min + 1 < max)
1336 this = (max + min) / 2;
1337 raddr = relocs[this].r_vaddr;
1338 if (raddr > address)
1340 else if (raddr < address)
1349 if (relocs[min].r_vaddr < address)
1353 && relocs[min - 1].r_vaddr == address)
1359 /* Add all the symbols from an object file to the hash table.
1361 XCOFF is a weird format. A normal XCOFF .o files will have three
1362 COFF sections--.text, .data, and .bss--but each COFF section will
1363 contain many csects. These csects are described in the symbol
1364 table. From the linker's point of view, each csect must be
1365 considered a section in its own right. For example, a TOC entry is
1366 handled as a small XMC_TC csect. The linker must be able to merge
1367 different TOC entries together, which means that it must be able to
1368 extract the XMC_TC csects from the .data section of the input .o
1371 From the point of view of our linker, this is, of course, a hideous
1372 nightmare. We cope by actually creating sections for each csect,
1373 and discarding the original sections. We then have to handle the
1374 relocation entries carefully, since the only way to tell which
1375 csect they belong to is to examine the address. */
1378 xcoff_link_add_symbols (abfd, info)
1380 struct bfd_link_info *info;
1382 unsigned int n_tmask;
1383 unsigned int n_btshft;
1384 boolean default_copy;
1385 bfd_size_type symcount;
1386 struct xcoff_link_hash_entry **sym_hash;
1387 asection **csect_cache;
1388 bfd_size_type linesz;
1390 asection *last_real;
1393 unsigned int csect_index;
1394 asection *first_csect;
1395 bfd_size_type symesz;
1398 struct reloc_info_struct
1400 struct internal_reloc *relocs;
1403 } *reloc_info = NULL;
1405 if ((abfd->flags & DYNAMIC) != 0
1406 && ! info->static_link)
1408 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1412 if (info->hash->creator == abfd->xvec)
1414 /* We need to build a .loader section, so we do it here. This
1415 won't work if we're producing an XCOFF output file with no
1416 XCOFF input files. FIXME. */
1417 if (xcoff_hash_table (info)->loader_section == NULL)
1421 lsec = bfd_make_section_anyway (abfd, ".loader");
1424 xcoff_hash_table (info)->loader_section = lsec;
1425 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1427 /* Likewise for the linkage section. */
1428 if (xcoff_hash_table (info)->linkage_section == NULL)
1432 lsec = bfd_make_section_anyway (abfd, ".gl");
1435 xcoff_hash_table (info)->linkage_section = lsec;
1436 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1438 lsec->alignment_power = 2;
1440 /* Likewise for the TOC section. */
1441 if (xcoff_hash_table (info)->toc_section == NULL)
1445 tsec = bfd_make_section_anyway (abfd, ".tc");
1448 xcoff_hash_table (info)->toc_section = tsec;
1449 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1451 tsec->alignment_power = 2;
1453 /* Likewise for the descriptor section. */
1454 if (xcoff_hash_table (info)->descriptor_section == NULL)
1458 dsec = bfd_make_section_anyway (abfd, ".ds");
1461 xcoff_hash_table (info)->descriptor_section = dsec;
1462 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1464 dsec->alignment_power = 2;
1466 /* Likewise for the .debug section. */
1467 if (xcoff_hash_table (info)->debug_section == NULL)
1471 dsec = bfd_make_section_anyway (abfd, ".debug");
1474 xcoff_hash_table (info)->debug_section = dsec;
1475 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1479 if ((abfd->flags & DYNAMIC) != 0
1480 && ! info->static_link)
1483 n_tmask = coff_data (abfd)->local_n_tmask;
1484 n_btshft = coff_data (abfd)->local_n_btshft;
1486 /* Define macros so that ISFCN, et. al., macros work correctly. */
1487 #define N_TMASK n_tmask
1488 #define N_BTSHFT n_btshft
1490 if (info->keep_memory)
1491 default_copy = false;
1493 default_copy = true;
1495 symcount = obj_raw_syment_count (abfd);
1497 /* We keep a list of the linker hash table entries that correspond
1498 to each external symbol. */
1499 sym_hash = ((struct xcoff_link_hash_entry **)
1502 * sizeof (struct xcoff_link_hash_entry *))));
1503 if (sym_hash == NULL && symcount != 0)
1505 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1506 memset (sym_hash, 0,
1507 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1509 /* Because of the weird stuff we are doing with XCOFF csects, we can
1510 not easily determine which section a symbol is in, so we store
1511 the information in the tdata for the input file. */
1512 csect_cache = ((asection **)
1513 bfd_alloc (abfd, symcount * sizeof (asection *)));
1514 if (csect_cache == NULL && symcount != 0)
1516 xcoff_data (abfd)->csects = csect_cache;
1517 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1519 /* While splitting sections into csects, we need to assign the
1520 relocs correctly. The relocs and the csects must both be in
1521 order by VMA within a given section, so we handle this by
1522 scanning along the relocs as we process the csects. We index
1523 into reloc_info using the section target_index. */
1524 reloc_info = ((struct reloc_info_struct *)
1525 bfd_malloc ((abfd->section_count + 1)
1526 * sizeof (struct reloc_info_struct)));
1527 if (reloc_info == NULL)
1529 memset ((PTR) reloc_info, 0,
1530 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1532 /* Read in the relocs and line numbers for each section. */
1533 linesz = bfd_coff_linesz (abfd);
1535 for (o = abfd->sections; o != NULL; o = o->next)
1538 if ((o->flags & SEC_RELOC) != 0)
1540 reloc_info[o->target_index].relocs =
1541 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1542 false, (struct internal_reloc *) NULL);
1543 reloc_info[o->target_index].csects =
1544 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1545 if (reloc_info[o->target_index].csects == NULL)
1547 memset (reloc_info[o->target_index].csects, 0,
1548 o->reloc_count * sizeof (asection *));
1551 if ((info->strip == strip_none || info->strip == strip_some)
1552 && o->lineno_count > 0)
1556 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1557 if (linenos == NULL)
1559 reloc_info[o->target_index].linenos = linenos;
1560 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1561 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1562 != linesz * o->lineno_count))
1567 /* Don't let the linker relocation routines discard the symbols. */
1568 keep_syms = obj_coff_keep_syms (abfd);
1569 obj_coff_keep_syms (abfd) = true;
1575 symesz = bfd_coff_symesz (abfd);
1576 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1577 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1578 esym_end = esym + symcount * symesz;
1579 while (esym < esym_end)
1581 struct internal_syment sym;
1582 union internal_auxent aux;
1584 char buf[SYMNMLEN + 1];
1589 struct xcoff_link_hash_entry *set_toc;
1591 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1593 /* In this pass we are only interested in symbols with csect
1595 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1597 if (sym.n_sclass == C_FILE && csect != NULL)
1599 xcoff_section_data (abfd, csect)->last_symndx =
1601 - (bfd_byte *) obj_coff_external_syms (abfd))
1607 *csect_cache = csect;
1608 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1609 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1611 *csect_cache = NULL;
1612 esym += (sym.n_numaux + 1) * symesz;
1613 sym_hash += sym.n_numaux + 1;
1614 csect_cache += sym.n_numaux + 1;
1618 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1622 /* If this symbol has line number information attached to it,
1623 and we're not stripping it, count the number of entries and
1624 add them to the count for this csect. In the final link pass
1625 we are going to attach line number information by symbol,
1626 rather than by section, in order to more easily handle
1627 garbage collection. */
1628 if ((info->strip == strip_none || info->strip == strip_some)
1631 && ISFCN (sym.n_type))
1633 union internal_auxent auxlin;
1635 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1636 sym.n_type, sym.n_sclass,
1637 0, sym.n_numaux, (PTR) &auxlin);
1638 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1640 asection *enclosing;
1641 bfd_size_type linoff;
1643 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1644 if (enclosing == NULL)
1646 (*_bfd_error_handler)
1647 ("%s: `%s' has line numbers but no enclosing section",
1648 bfd_get_filename (abfd), name);
1649 bfd_set_error (bfd_error_bad_value);
1652 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1653 - enclosing->line_filepos);
1654 if (linoff < enclosing->lineno_count * linesz)
1656 struct internal_lineno lin;
1657 bfd_byte *linpstart;
1659 linpstart = (reloc_info[enclosing->target_index].linenos
1661 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1663 && ((bfd_size_type) lin.l_addr.l_symndx
1665 - (bfd_byte *) obj_coff_external_syms (abfd))
1668 bfd_byte *linpend, *linp;
1670 linpend = (reloc_info[enclosing->target_index].linenos
1671 + enclosing->lineno_count * linesz);
1672 for (linp = linpstart + linesz;
1676 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1678 if (lin.l_lnno == 0)
1681 csect->lineno_count += (linp - linpstart) / linesz;
1682 /* The setting of line_filepos will only be
1683 useful if all the line number entries for a
1684 csect are contiguous; this only matters for
1686 if (csect->line_filepos == 0)
1687 csect->line_filepos =
1688 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1694 /* Pick up the csect auxiliary information. */
1696 if (sym.n_numaux == 0)
1698 (*_bfd_error_handler)
1699 ("%s: class %d symbol `%s' has no aux entries",
1700 bfd_get_filename (abfd), sym.n_sclass, name);
1701 bfd_set_error (bfd_error_bad_value);
1705 bfd_coff_swap_aux_in (abfd,
1706 (PTR) (esym + symesz * sym.n_numaux),
1707 sym.n_type, sym.n_sclass,
1708 sym.n_numaux - 1, sym.n_numaux,
1711 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1721 (*_bfd_error_handler)
1722 ("%s: symbol `%s' has unrecognized csect type %d",
1723 bfd_get_filename (abfd), name, smtyp);
1724 bfd_set_error (bfd_error_bad_value);
1728 /* This is an external reference. */
1729 if (sym.n_sclass == C_HIDEXT
1730 || sym.n_scnum != N_UNDEF
1731 || aux.x_csect.x_scnlen.l != 0)
1733 (*_bfd_error_handler)
1734 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1735 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1736 aux.x_csect.x_scnlen.l);
1737 bfd_set_error (bfd_error_bad_value);
1741 /* An XMC_XO external reference is actually a reference to
1742 an absolute location. */
1743 if (aux.x_csect.x_smclas != XMC_XO)
1744 section = bfd_und_section_ptr;
1747 section = bfd_abs_section_ptr;
1748 value = sym.n_value;
1753 /* This is a csect definition. */
1757 xcoff_section_data (abfd, csect)->last_symndx =
1759 - (bfd_byte *) obj_coff_external_syms (abfd))
1766 /* When we see a TOC anchor, we record the TOC value. */
1767 if (aux.x_csect.x_smclas == XMC_TC0)
1769 if (sym.n_sclass != C_HIDEXT
1770 || aux.x_csect.x_scnlen.l != 0)
1772 (*_bfd_error_handler)
1773 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1774 bfd_get_filename (abfd), name, sym.n_sclass,
1775 aux.x_csect.x_scnlen.l);
1776 bfd_set_error (bfd_error_bad_value);
1779 xcoff_data (abfd)->toc = sym.n_value;
1782 /* We must merge TOC entries for the same symbol. We can
1783 merge two TOC entries if they are both C_HIDEXT, they
1784 both have the same name, they are both 4 bytes long, and
1785 they both have a relocation table entry for an external
1786 symbol with the same name. Unfortunately, this means
1787 that we must look through the relocations. Ick. */
1788 if (aux.x_csect.x_smclas == XMC_TC
1789 && sym.n_sclass == C_HIDEXT
1790 && aux.x_csect.x_scnlen.l == 4
1791 && info->hash->creator == abfd->xvec)
1793 asection *enclosing;
1794 struct internal_reloc *relocs;
1795 bfd_size_type relindx;
1796 struct internal_reloc *rel;
1798 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1799 if (enclosing == NULL)
1802 relocs = reloc_info[enclosing->target_index].relocs;
1803 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1805 rel = relocs + relindx;
1806 if (relindx < enclosing->reloc_count
1807 && rel->r_vaddr == (bfd_vma) sym.n_value
1808 && rel->r_size == 31
1809 && rel->r_type == R_POS)
1812 struct internal_syment relsym;
1814 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1815 + rel->r_symndx * symesz);
1816 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1817 if (relsym.n_sclass == C_EXT)
1819 const char *relname;
1820 char relbuf[SYMNMLEN + 1];
1822 struct xcoff_link_hash_entry *h;
1824 /* At this point we know that the TOC entry is
1825 for an externally visible symbol. */
1826 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1828 if (relname == NULL)
1831 /* We only merge TOC entries if the TC name is
1832 the same as the symbol name. This handles
1833 the normal case, but not common cases like
1834 SYM.P4 which gcc generates to store SYM + 4
1835 in the TOC. FIXME. */
1836 if (strcmp (name, relname) == 0)
1838 copy = (! info->keep_memory
1839 || relsym._n._n_n._n_zeroes != 0
1840 || relsym._n._n_n._n_offset == 0);
1841 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1842 relname, true, copy,
1847 /* At this point h->root.type could be
1848 bfd_link_hash_new. That should be OK,
1849 since we know for sure that we will come
1850 across this symbol as we step through the
1853 /* We store h in *sym_hash for the
1854 convenience of the relocate_section
1858 if (h->toc_section != NULL)
1860 asection **rel_csects;
1862 /* We already have a TOC entry for this
1863 symbol, so we can just ignore this
1866 reloc_info[enclosing->target_index].csects;
1867 rel_csects[relindx] = bfd_und_section_ptr;
1871 /* We are about to create a TOC entry for
1879 /* We need to create a new section. We get the name from
1880 the csect storage mapping class, so that the linker can
1881 accumulate similar csects together. */
1883 static const char *csect_name_by_class[] =
1885 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1886 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1889 const char *csect_name;
1890 asection *enclosing;
1892 if ((aux.x_csect.x_smclas >=
1893 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1894 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1896 (*_bfd_error_handler)
1897 ("%s: symbol `%s' has unrecognized smclas %d",
1898 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1899 bfd_set_error (bfd_error_bad_value);
1903 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1904 csect = bfd_make_section_anyway (abfd, csect_name);
1907 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1908 if (enclosing == NULL)
1910 if (! bfd_is_abs_section (enclosing)
1911 && ((bfd_vma) sym.n_value < enclosing->vma
1912 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1913 > enclosing->vma + enclosing->_raw_size)))
1915 (*_bfd_error_handler)
1916 ("%s: csect `%s' not in enclosing section",
1917 bfd_get_filename (abfd), name);
1918 bfd_set_error (bfd_error_bad_value);
1921 csect->vma = sym.n_value;
1922 csect->filepos = (enclosing->filepos
1925 csect->_raw_size = aux.x_csect.x_scnlen.l;
1926 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1927 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1929 /* Record the enclosing section in the tdata for this new
1931 csect->used_by_bfd =
1932 ((struct coff_section_tdata *)
1933 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1934 if (csect->used_by_bfd == NULL)
1936 coff_section_data (abfd, csect)->tdata =
1937 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1938 if (coff_section_data (abfd, csect)->tdata == NULL)
1940 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1941 xcoff_section_data (abfd, csect)->lineno_count =
1942 enclosing->lineno_count;
1944 if (enclosing->owner == abfd)
1946 struct internal_reloc *relocs;
1947 bfd_size_type relindx;
1948 struct internal_reloc *rel;
1949 asection **rel_csect;
1951 relocs = reloc_info[enclosing->target_index].relocs;
1952 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1954 rel = relocs + relindx;
1955 rel_csect = (reloc_info[enclosing->target_index].csects
1957 csect->rel_filepos = (enclosing->rel_filepos
1958 + relindx * bfd_coff_relsz (abfd));
1959 while (relindx < enclosing->reloc_count
1960 && *rel_csect == NULL
1961 && rel->r_vaddr < csect->vma + csect->_raw_size)
1964 csect->flags |= SEC_RELOC;
1965 ++csect->reloc_count;
1972 /* There are a number of other fields and section flags
1973 which we do not bother to set. */
1975 csect_index = ((esym
1976 - (bfd_byte *) obj_coff_external_syms (abfd))
1979 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1981 if (first_csect == NULL)
1982 first_csect = csect;
1984 /* If this symbol is C_EXT, we treat it as starting at the
1985 beginning of the newly created section. */
1986 if (sym.n_sclass == C_EXT)
1992 /* If this is a TOC section for a symbol, record it. */
1993 if (set_toc != NULL)
1994 set_toc->toc_section = csect;
1999 /* This is a label definition. The x_scnlen field is the
2000 symbol index of the csect. I believe that this must
2001 always follow the appropriate XTY_SD symbol, so I will
2007 if (aux.x_csect.x_scnlen.l < 0
2008 || (aux.x_csect.x_scnlen.l
2009 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2013 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2015 || (section->flags & SEC_HAS_CONTENTS) == 0)
2020 (*_bfd_error_handler)
2021 ("%s: misplaced XTY_LD `%s'",
2022 bfd_get_filename (abfd), name);
2023 bfd_set_error (bfd_error_bad_value);
2027 value = sym.n_value - csect->vma;
2032 /* This is an unitialized csect. We could base the name on
2033 the storage mapping class, but we don't bother. If this
2034 csect is externally visible, it is a common symbol. */
2038 xcoff_section_data (abfd, csect)->last_symndx =
2040 - (bfd_byte *) obj_coff_external_syms (abfd))
2044 csect = bfd_make_section_anyway (abfd, ".bss");
2047 csect->vma = sym.n_value;
2048 csect->_raw_size = aux.x_csect.x_scnlen.l;
2049 csect->flags |= SEC_ALLOC;
2050 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2051 /* There are a number of other fields and section flags
2052 which we do not bother to set. */
2054 csect_index = ((esym
2055 - (bfd_byte *) obj_coff_external_syms (abfd))
2058 csect->used_by_bfd =
2059 ((struct coff_section_tdata *)
2060 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
2061 if (csect->used_by_bfd == NULL)
2063 coff_section_data (abfd, csect)->tdata =
2064 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2065 if (coff_section_data (abfd, csect)->tdata == NULL)
2067 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2069 if (first_csect == NULL)
2070 first_csect = csect;
2072 if (sym.n_sclass == C_EXT)
2074 csect->flags |= SEC_IS_COMMON;
2075 csect->_raw_size = 0;
2077 value = aux.x_csect.x_scnlen.l;
2083 /* Check for magic symbol names. */
2084 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2085 && aux.x_csect.x_smclas != XMC_TC
2086 && aux.x_csect.x_smclas != XMC_TD)
2093 if (strcmp (name, "_text") == 0)
2095 else if (strcmp (name, "_etext") == 0)
2097 else if (strcmp (name, "_data") == 0)
2099 else if (strcmp (name, "_edata") == 0)
2101 else if (strcmp (name, "_end") == 0)
2104 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2108 xcoff_hash_table (info)->special_sections[i] = csect;
2111 /* Now we have enough information to add the symbol to the
2112 linker hash table. */
2114 if (sym.n_sclass == C_EXT)
2118 BFD_ASSERT (section != NULL);
2120 /* We must copy the name into memory if we got it from the
2121 syment itself, rather than the string table. */
2122 copy = default_copy;
2123 if (sym._n._n_n._n_zeroes != 0
2124 || sym._n._n_n._n_offset == 0)
2127 if (info->hash->creator == abfd->xvec)
2129 /* If we are statically linking a shared object, it is
2130 OK for symbol redefinitions to occur. I can't figure
2131 out just what the XCOFF linker is doing, but
2132 something like this is required for -bnso to work. */
2133 if (! bfd_is_und_section (section))
2134 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2135 name, true, copy, false);
2137 *sym_hash = ((struct xcoff_link_hash_entry *)
2138 bfd_wrapped_link_hash_lookup (abfd, info, name,
2139 true, copy, false));
2140 if (*sym_hash == NULL)
2142 if (((*sym_hash)->root.type == bfd_link_hash_defined
2143 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2144 && ! bfd_is_und_section (section)
2145 && ! bfd_is_com_section (section))
2147 /* If the existing symbol is to global linkage code,
2148 and this symbol is not global linkage code, then
2149 replace the existing symbol. */
2150 if ((abfd->flags & DYNAMIC) != 0
2151 && ((*sym_hash)->smclas != XMC_GL
2152 || aux.x_csect.x_smclas == XMC_GL
2153 || ((*sym_hash)->root.u.def.section->owner->flags
2156 section = bfd_und_section_ptr;
2159 else if (((*sym_hash)->root.u.def.section->owner->flags
2162 (*sym_hash)->root.type = bfd_link_hash_undefined;
2163 (*sym_hash)->root.u.undef.abfd =
2164 (*sym_hash)->root.u.def.section->owner;
2169 /* _bfd_generic_link_add_one_symbol may call the linker to
2170 generate an error message, and the linker may try to read
2171 the symbol table to give a good error. Right now, the
2172 line numbers are in an inconsistent state, since they are
2173 counted both in the real sections and in the new csects.
2174 We need to leave the count in the real sections so that
2175 the linker can report the line number of the error
2176 correctly, so temporarily clobber the link to the csects
2177 so that the linker will not try to read the line numbers
2178 a second time from the csects. */
2179 BFD_ASSERT (last_real->next == first_csect);
2180 last_real->next = NULL;
2181 if (! (_bfd_generic_link_add_one_symbol
2182 (info, abfd, name, flags, section, value,
2183 (const char *) NULL, copy, true,
2184 (struct bfd_link_hash_entry **) sym_hash)))
2186 last_real->next = first_csect;
2188 if (smtyp == XTY_CM)
2190 if ((*sym_hash)->root.type != bfd_link_hash_common
2191 || (*sym_hash)->root.u.c.p->section != csect)
2193 /* We don't need the common csect we just created. */
2194 csect->_raw_size = 0;
2198 (*sym_hash)->root.u.c.p->alignment_power
2199 = csect->alignment_power;
2203 if (info->hash->creator == abfd->xvec)
2207 if (smtyp == XTY_ER || smtyp == XTY_CM)
2208 flag = XCOFF_REF_REGULAR;
2210 flag = XCOFF_DEF_REGULAR;
2211 (*sym_hash)->flags |= flag;
2213 if ((*sym_hash)->smclas == XMC_UA
2214 || flag == XCOFF_DEF_REGULAR)
2215 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2219 *csect_cache = csect;
2221 esym += (sym.n_numaux + 1) * symesz;
2222 sym_hash += sym.n_numaux + 1;
2223 csect_cache += sym.n_numaux + 1;
2226 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2228 /* Make sure that we have seen all the relocs. */
2229 for (o = abfd->sections; o != first_csect; o = o->next)
2231 /* Reset the section size and the line number count, since the
2232 data is now attached to the csects. Don't reset the size of
2233 the .debug section, since we need to read it below in
2234 bfd_xcoff_size_dynamic_sections. */
2235 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2237 o->lineno_count = 0;
2239 if ((o->flags & SEC_RELOC) != 0)
2242 struct internal_reloc *rel;
2243 asection **rel_csect;
2245 rel = reloc_info[o->target_index].relocs;
2246 rel_csect = reloc_info[o->target_index].csects;
2247 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2249 if (*rel_csect == NULL)
2251 (*_bfd_error_handler)
2252 ("%s: reloc %s:%d not in csect",
2253 bfd_get_filename (abfd), o->name, i);
2254 bfd_set_error (bfd_error_bad_value);
2258 /* We identify all symbols which are called, so that we
2259 can create glue code for calls to functions imported
2260 from dynamic objects. */
2261 if (info->hash->creator == abfd->xvec
2262 && *rel_csect != bfd_und_section_ptr
2263 && (rel->r_type == R_BR
2264 || rel->r_type == R_RBR)
2265 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2267 struct xcoff_link_hash_entry *h;
2269 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2270 h->flags |= XCOFF_CALLED;
2271 /* If the symbol name starts with a period, it is
2272 the code of a function. If the symbol is
2273 currently undefined, then add an undefined symbol
2274 for the function descriptor. This should do no
2275 harm, because any regular object that defines the
2276 function should also define the function
2277 descriptor. It helps, because it means that we
2278 will identify the function descriptor with a
2279 dynamic object if a dynamic object defines it. */
2280 if (h->root.root.string[0] == '.'
2281 && h->descriptor == NULL)
2283 struct xcoff_link_hash_entry *hds;
2285 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2286 h->root.root.string + 1,
2290 if (hds->root.type == bfd_link_hash_new)
2292 if (! (_bfd_generic_link_add_one_symbol
2293 (info, abfd, hds->root.root.string,
2294 (flagword) 0, bfd_und_section_ptr,
2295 (bfd_vma) 0, (const char *) NULL, false,
2297 (struct bfd_link_hash_entry **) &hds)))
2300 hds->flags |= XCOFF_DESCRIPTOR;
2301 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2302 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2303 hds->descriptor = h;
2304 h->descriptor = hds;
2309 free (reloc_info[o->target_index].csects);
2310 reloc_info[o->target_index].csects = NULL;
2312 /* Reset SEC_RELOC and the reloc_count, since the reloc
2313 information is now attached to the csects. */
2314 o->flags &=~ SEC_RELOC;
2317 /* If we are not keeping memory, free the reloc information. */
2318 if (! info->keep_memory
2319 && coff_section_data (abfd, o) != NULL
2320 && coff_section_data (abfd, o)->relocs != NULL
2321 && ! coff_section_data (abfd, o)->keep_relocs)
2323 free (coff_section_data (abfd, o)->relocs);
2324 coff_section_data (abfd, o)->relocs = NULL;
2328 /* Free up the line numbers. FIXME: We could cache these
2329 somewhere for the final link, to avoid reading them again. */
2330 if (reloc_info[o->target_index].linenos != NULL)
2332 free (reloc_info[o->target_index].linenos);
2333 reloc_info[o->target_index].linenos = NULL;
2339 obj_coff_keep_syms (abfd) = keep_syms;
2344 if (reloc_info != NULL)
2346 for (o = abfd->sections; o != NULL; o = o->next)
2348 if (reloc_info[o->target_index].csects != NULL)
2349 free (reloc_info[o->target_index].csects);
2350 if (reloc_info[o->target_index].linenos != NULL)
2351 free (reloc_info[o->target_index].linenos);
2355 obj_coff_keep_syms (abfd) = keep_syms;
2362 /* This function is used to add symbols from a dynamic object to the
2363 global symbol table. */
2366 xcoff_link_add_dynamic_symbols (abfd, info)
2368 struct bfd_link_info *info;
2372 struct internal_ldhdr ldhdr;
2373 const char *strings;
2374 struct external_ldsym *elsym, *elsymend;
2375 struct xcoff_import_file *n;
2380 struct xcoff_import_file **pp;
2382 /* We can only handle a dynamic object if we are generating an XCOFF
2384 if (info->hash->creator != abfd->xvec)
2386 (*_bfd_error_handler)
2387 ("%s: XCOFF shared object when not producing XCOFF output",
2388 bfd_get_filename (abfd));
2389 bfd_set_error (bfd_error_invalid_operation);
2393 /* The symbols we use from a dynamic object are not the symbols in
2394 the normal symbol table, but, rather, the symbols in the export
2395 table. If there is a global symbol in a dynamic object which is
2396 not in the export table, the loader will not be able to find it,
2397 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2398 libc.a has symbols in the export table which are not in the
2401 /* Read in the .loader section. FIXME: We should really use the
2402 o_snloader field in the a.out header, rather than grabbing the
2404 lsec = bfd_get_section_by_name (abfd, ".loader");
2407 (*_bfd_error_handler)
2408 ("%s: dynamic object with no .loader section",
2409 bfd_get_filename (abfd));
2410 bfd_set_error (bfd_error_no_symbols);
2414 if (! xcoff_get_section_contents (abfd, lsec))
2416 buf = coff_section_data (abfd, lsec)->contents;
2418 /* Remove the sections from this object, so that they do not get
2419 included in the link. */
2420 abfd->sections = NULL;
2422 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2424 strings = (char *) buf + ldhdr.l_stoff;
2426 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2427 elsymend = elsym + ldhdr.l_nsyms;
2428 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2429 for (; elsym < elsymend; elsym++)
2431 struct internal_ldsym ldsym;
2432 char nambuf[SYMNMLEN + 1];
2434 struct xcoff_link_hash_entry *h;
2436 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2438 /* We are only interested in exported symbols. */
2439 if ((ldsym.l_smtype & L_EXPORT) == 0)
2442 if (ldsym._l._l_l._l_zeroes == 0)
2443 name = strings + ldsym._l._l_l._l_offset;
2446 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2447 nambuf[SYMNMLEN] = '\0';
2451 /* Normally we could not call xcoff_link_hash_lookup in an add
2452 symbols routine, since we might not be using an XCOFF hash
2453 table. However, we verified above that we are using an XCOFF
2456 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2461 h->flags |= XCOFF_DEF_DYNAMIC;
2463 /* If the symbol is undefined, and the BFD it was found in is
2464 not a dynamic object, change the BFD to this dynamic object,
2465 so that we can get the correct import file ID. */
2466 if ((h->root.type == bfd_link_hash_undefined
2467 || h->root.type == bfd_link_hash_undefweak)
2468 && (h->root.u.undef.abfd == NULL
2469 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2470 h->root.u.undef.abfd = abfd;
2472 if (h->root.type == bfd_link_hash_new)
2474 h->root.type = bfd_link_hash_undefined;
2475 h->root.u.undef.abfd = abfd;
2476 /* We do not want to add this to the undefined symbol list. */
2479 if (h->smclas == XMC_UA
2480 || h->root.type == bfd_link_hash_undefined
2481 || h->root.type == bfd_link_hash_undefweak)
2482 h->smclas = ldsym.l_smclas;
2484 /* Unless this is an XMC_XO symbol, we don't bother to actually
2485 define it, since we don't have a section to put it in anyhow.
2486 Instead, the relocation routines handle the DEF_DYNAMIC flag
2489 if (h->smclas == XMC_XO
2490 && (h->root.type == bfd_link_hash_undefined
2491 || h->root.type == bfd_link_hash_undefweak))
2493 /* This symbol has an absolute value. */
2494 h->root.type = bfd_link_hash_defined;
2495 h->root.u.def.section = bfd_abs_section_ptr;
2496 h->root.u.def.value = ldsym.l_value;
2500 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2502 free (coff_section_data (abfd, lsec)->contents);
2503 coff_section_data (abfd, lsec)->contents = NULL;
2506 /* Record this file in the import files. */
2508 n = ((struct xcoff_import_file *)
2509 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2514 /* For some reason, the path entry in the import file list for a
2515 shared object appears to always be empty. The file name is the
2518 if (abfd->my_archive == NULL)
2520 bname = bfd_get_filename (abfd);
2525 bname = bfd_get_filename (abfd->my_archive);
2526 mname = bfd_get_filename (abfd);
2528 s = strrchr (bname, '/');
2534 /* We start c at 1 because the first import file number is reserved
2536 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2538 pp = &(*pp)->next, ++c)
2542 xcoff_data (abfd)->import_file_id = c;
2547 /* Routines that are called after all the input files have been
2548 handled, but before the sections are laid out in memory. */
2550 /* Mark a symbol as not being garbage, including the section in which
2553 static INLINE boolean
2554 xcoff_mark_symbol (info, h)
2555 struct bfd_link_info *info;
2556 struct xcoff_link_hash_entry *h;
2558 if ((h->flags & XCOFF_MARK) != 0)
2561 h->flags |= XCOFF_MARK;
2562 if (h->root.type == bfd_link_hash_defined
2563 || h->root.type == bfd_link_hash_defweak)
2567 hsec = h->root.u.def.section;
2568 if ((hsec->flags & SEC_MARK) == 0)
2570 if (! xcoff_mark (info, hsec))
2575 if (h->toc_section != NULL
2576 && (h->toc_section->flags & SEC_MARK) == 0)
2578 if (! xcoff_mark (info, h->toc_section))
2585 /* The mark phase of garbage collection. For a given section, mark
2586 it, and all the sections which define symbols to which it refers.
2587 Because this function needs to look at the relocs, we also count
2588 the number of relocs which need to be copied into the .loader
2592 xcoff_mark (info, sec)
2593 struct bfd_link_info *info;
2596 if ((sec->flags & SEC_MARK) != 0)
2599 sec->flags |= SEC_MARK;
2601 if (sec->owner->xvec == info->hash->creator
2602 && coff_section_data (sec->owner, sec) != NULL
2603 && xcoff_section_data (sec->owner, sec) != NULL)
2605 register struct xcoff_link_hash_entry **hp, **hpend;
2606 struct internal_reloc *rel, *relend;
2608 /* Mark all the symbols in this section. */
2610 hp = (obj_xcoff_sym_hashes (sec->owner)
2611 + xcoff_section_data (sec->owner, sec)->first_symndx);
2612 hpend = (obj_xcoff_sym_hashes (sec->owner)
2613 + xcoff_section_data (sec->owner, sec)->last_symndx);
2614 for (; hp < hpend; hp++)
2616 register struct xcoff_link_hash_entry *h;
2620 && (h->flags & XCOFF_MARK) == 0)
2622 if (! xcoff_mark_symbol (info, h))
2627 /* Look through the section relocs. */
2629 if ((sec->flags & SEC_RELOC) != 0
2630 && sec->reloc_count > 0)
2632 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2633 (bfd_byte *) NULL, false,
2634 (struct internal_reloc *) NULL);
2637 relend = rel + sec->reloc_count;
2638 for (; rel < relend; rel++)
2641 struct xcoff_link_hash_entry *h;
2643 if ((unsigned int) rel->r_symndx
2644 > obj_raw_syment_count (sec->owner))
2647 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2649 && (h->flags & XCOFF_MARK) == 0)
2651 if (! xcoff_mark_symbol (info, h))
2655 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2657 && (rsec->flags & SEC_MARK) == 0)
2659 if (! xcoff_mark (info, rsec))
2663 /* See if this reloc needs to be copied into the .loader
2665 switch (rel->r_type)
2669 || h->root.type == bfd_link_hash_defined
2670 || h->root.type == bfd_link_hash_defweak
2671 || h->root.type == bfd_link_hash_common
2672 || ((h->flags & XCOFF_CALLED) != 0
2673 && (h->root.type == bfd_link_hash_undefined
2674 || h->root.type == bfd_link_hash_undefweak)
2675 && h->root.root.string[0] == '.'
2676 && h->descriptor != NULL
2677 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2679 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2680 && (h->descriptor->flags
2681 & XCOFF_DEF_REGULAR) == 0))))
2688 ++xcoff_hash_table (info)->ldrel_count;
2690 h->flags |= XCOFF_LDREL;
2697 /* We should never need a .loader reloc for a TOC
2703 if (! info->keep_memory
2704 && coff_section_data (sec->owner, sec) != NULL
2705 && coff_section_data (sec->owner, sec)->relocs != NULL
2706 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2708 free (coff_section_data (sec->owner, sec)->relocs);
2709 coff_section_data (sec->owner, sec)->relocs = NULL;
2717 /* The sweep phase of garbage collection. Remove all garbage
2722 struct bfd_link_info *info;
2726 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2730 for (o = sub->sections; o != NULL; o = o->next)
2732 if ((o->flags & SEC_MARK) == 0)
2734 /* Keep all sections from non-XCOFF input files. Keep
2735 special sections. Keep .debug sections for the
2737 if (sub->xvec != info->hash->creator
2738 || o == xcoff_hash_table (info)->debug_section
2739 || o == xcoff_hash_table (info)->loader_section
2740 || o == xcoff_hash_table (info)->linkage_section
2741 || o == xcoff_hash_table (info)->toc_section
2742 || o == xcoff_hash_table (info)->descriptor_section
2743 || strcmp (o->name, ".debug") == 0)
2744 o->flags |= SEC_MARK;
2749 o->lineno_count = 0;
2756 /* Record the number of elements in a set. This is used to output the
2757 correct csect length. */
2760 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2762 struct bfd_link_info *info;
2763 struct bfd_link_hash_entry *harg;
2766 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2767 struct xcoff_link_size_list *n;
2769 if (! XCOFF_XVECP (output_bfd->xvec))
2772 /* This will hardly ever be called. I don't want to burn four bytes
2773 per global symbol, so instead the size is kept on a linked list
2774 attached to the hash table. */
2776 n = ((struct xcoff_link_size_list *)
2777 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2780 n->next = xcoff_hash_table (info)->size_list;
2783 xcoff_hash_table (info)->size_list = n;
2785 h->flags |= XCOFF_HAS_SIZE;
2790 /* Import a symbol. */
2793 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2796 struct bfd_link_info *info;
2797 struct bfd_link_hash_entry *harg;
2799 const char *imppath;
2800 const char *impfile;
2801 const char *impmember;
2803 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2805 if (! XCOFF_XVECP (output_bfd->xvec))
2808 h->flags |= XCOFF_IMPORT;
2810 if (val != (bfd_vma) -1)
2812 if (h->root.type == bfd_link_hash_defined
2813 && (! bfd_is_abs_section (h->root.u.def.section)
2814 || h->root.u.def.value != val))
2816 if (! ((*info->callbacks->multiple_definition)
2817 (info, h->root.root.string, h->root.u.def.section->owner,
2818 h->root.u.def.section, h->root.u.def.value,
2819 output_bfd, bfd_abs_section_ptr, val)))
2823 h->root.type = bfd_link_hash_defined;
2824 h->root.u.def.section = bfd_abs_section_ptr;
2825 h->root.u.def.value = val;
2828 if (h->ldsym == NULL)
2830 h->ldsym = ((struct internal_ldsym *)
2831 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2832 if (h->ldsym == NULL)
2836 if (imppath == NULL)
2837 h->ldsym->l_ifile = (bfd_size_type) -1;
2841 struct xcoff_import_file **pp;
2843 /* We start c at 1 because the first entry in the import list is
2844 reserved for the library search path. */
2845 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2847 pp = &(*pp)->next, ++c)
2849 if (strcmp ((*pp)->path, imppath) == 0
2850 && strcmp ((*pp)->file, impfile) == 0
2851 && strcmp ((*pp)->member, impmember) == 0)
2857 struct xcoff_import_file *n;
2859 n = ((struct xcoff_import_file *)
2860 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2866 n->member = impmember;
2870 h->ldsym->l_ifile = c;
2876 /* Export a symbol. */
2879 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2881 struct bfd_link_info *info;
2882 struct bfd_link_hash_entry *harg;
2885 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2887 if (! XCOFF_XVECP (output_bfd->xvec))
2890 h->flags |= XCOFF_EXPORT;
2892 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2893 I'm just going to ignore it until somebody explains it. */
2895 /* See if this is a function descriptor. It may be one even though
2896 it is not so marked. */
2897 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2898 && h->root.root.string[0] != '.')
2901 struct xcoff_link_hash_entry *hfn;
2903 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
2907 strcpy (fnname + 1, h->root.root.string);
2908 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2909 fnname, false, false, true);
2912 && hfn->smclas == XMC_PR
2913 && (hfn->root.type == bfd_link_hash_defined
2914 || hfn->root.type == bfd_link_hash_defweak))
2916 h->flags |= XCOFF_DESCRIPTOR;
2917 h->descriptor = hfn;
2918 hfn->descriptor = h;
2922 /* Make sure we don't garbage collect this symbol. */
2923 if (! xcoff_mark_symbol (info, h))
2926 /* If this is a function descriptor, make sure we don't garbage
2927 collect the associated function code. We normally don't have to
2928 worry about this, because the descriptor will be attached to a
2929 section with relocs, but if we are creating the descriptor
2930 ourselves those relocs will not be visible to the mark code. */
2931 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2933 if (! xcoff_mark_symbol (info, h->descriptor))
2940 /* Count a reloc against a symbol. This is called for relocs
2941 generated by the linker script, typically for global constructors
2945 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2947 struct bfd_link_info *info;
2950 struct xcoff_link_hash_entry *h;
2952 if (! XCOFF_XVECP (output_bfd->xvec))
2955 h = ((struct xcoff_link_hash_entry *)
2956 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
2960 (*_bfd_error_handler) ("%s: no such symbol", name);
2961 bfd_set_error (bfd_error_no_symbols);
2965 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2966 ++xcoff_hash_table (info)->ldrel_count;
2968 /* Mark the symbol to avoid garbage collection. */
2969 if (! xcoff_mark_symbol (info, h))
2975 /* This function is called for each symbol to which the linker script
2979 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2981 struct bfd_link_info *info;
2984 struct xcoff_link_hash_entry *h;
2986 if (! XCOFF_XVECP (output_bfd->xvec))
2989 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2994 h->flags |= XCOFF_DEF_REGULAR;
2999 /* This structure is used to pass information through
3000 xcoff_link_hash_traverse. */
3002 struct xcoff_loader_info
3004 /* Set if a problem occurred. */
3008 /* Link information structure. */
3009 struct bfd_link_info *info;
3010 /* Whether all defined symbols should be exported. */
3011 boolean export_defineds;
3012 /* Number of ldsym structures. */
3014 /* Size of string table. */
3018 /* Allocated size of string table. */
3022 /* Build the .loader section. This is called by the XCOFF linker
3023 emulation before_allocation routine. We must set the size of the
3024 .loader section before the linker lays out the output file.
3025 LIBPATH is the library path to search for shared objects; this is
3026 normally built from the -L arguments passed to the linker. ENTRY
3027 is the name of the entry point symbol (the -e linker option).
3028 FILE_ALIGN is the alignment to use for sections within the file
3029 (the -H linker option). MAXSTACK is the maximum stack size (the
3030 -bmaxstack linker option). MAXDATA is the maximum data size (the
3031 -bmaxdata linker option). GC is whether to do garbage collection
3032 (the -bgc linker option). MODTYPE is the module type (the
3033 -bmodtype linker option). TEXTRO is whether the text section must
3034 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3035 whether all defined symbols should be exported (the -unix linker
3036 option). SPECIAL_SECTIONS is set by this routine to csects with
3037 magic names like _end. */
3040 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3041 file_align, maxstack, maxdata, gc,
3042 modtype, textro, export_defineds,
3045 struct bfd_link_info *info;
3046 const char *libpath;
3048 unsigned long file_align;
3049 unsigned long maxstack;
3050 unsigned long maxdata;
3054 boolean export_defineds;
3055 asection **special_sections;
3057 struct xcoff_link_hash_entry *hentry;
3059 struct xcoff_loader_info ldinfo;
3061 size_t impsize, impcount;
3062 struct xcoff_import_file *fl;
3063 struct internal_ldhdr *ldhdr;
3064 bfd_size_type stoff;
3068 struct bfd_strtab_hash *debug_strtab;
3069 bfd_byte *debug_contents = NULL;
3071 if (! XCOFF_XVECP (output_bfd->xvec))
3073 for (i = 0; i < 6; i++)
3074 special_sections[i] = NULL;
3078 ldinfo.failed = false;
3079 ldinfo.output_bfd = output_bfd;
3081 ldinfo.export_defineds = export_defineds;
3082 ldinfo.ldsym_count = 0;
3083 ldinfo.string_size = 0;
3084 ldinfo.strings = NULL;
3085 ldinfo.string_alc = 0;
3087 xcoff_data (output_bfd)->maxstack = maxstack;
3088 xcoff_data (output_bfd)->maxdata = maxdata;
3089 xcoff_data (output_bfd)->modtype = modtype;
3091 xcoff_hash_table (info)->file_align = file_align;
3092 xcoff_hash_table (info)->textro = textro;
3094 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3095 false, false, true);
3097 hentry->flags |= XCOFF_ENTRY;
3099 /* Garbage collect unused sections. */
3100 if (info->relocateable
3103 || (hentry->root.type != bfd_link_hash_defined
3104 && hentry->root.type != bfd_link_hash_defweak))
3107 xcoff_hash_table (info)->gc = false;
3109 /* We still need to call xcoff_mark, in order to set ldrel_count
3111 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3115 for (o = sub->sections; o != NULL; o = o->next)
3117 if ((o->flags & SEC_MARK) == 0)
3119 if (! xcoff_mark (info, o))
3127 if (! xcoff_mark (info, hentry->root.u.def.section))
3130 xcoff_hash_table (info)->gc = true;
3133 /* Return special sections to the caller. */
3134 for (i = 0; i < 6; i++)
3138 sec = xcoff_hash_table (info)->special_sections[i];
3141 && (sec->flags & SEC_MARK) == 0)
3143 special_sections[i] = sec;
3146 if (info->input_bfds == NULL)
3148 /* I'm not sure what to do in this bizarre case. */
3152 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3157 /* Work out the size of the import file names. Each import file ID
3158 consists of three null terminated strings: the path, the file
3159 name, and the archive member name. The first entry in the list
3160 of names is the path to use to find objects, which the linker has
3161 passed in as the libpath argument. For some reason, the path
3162 entry in the other import file names appears to always be empty. */
3163 impsize = strlen (libpath) + 3;
3165 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3168 impsize += (strlen (fl->path)
3170 + strlen (fl->member)
3174 /* Set up the .loader section header. */
3175 ldhdr = &xcoff_hash_table (info)->ldhdr;
3176 ldhdr->l_version = 1;
3177 ldhdr->l_nsyms = ldinfo.ldsym_count;
3178 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3179 ldhdr->l_istlen = impsize;
3180 ldhdr->l_nimpid = impcount;
3181 ldhdr->l_impoff = (LDHDRSZ
3182 + ldhdr->l_nsyms * LDSYMSZ
3183 + ldhdr->l_nreloc * LDRELSZ);
3184 ldhdr->l_stlen = ldinfo.string_size;
3185 stoff = ldhdr->l_impoff + impsize;
3186 if (ldinfo.string_size == 0)
3189 ldhdr->l_stoff = stoff;
3191 /* We now know the final size of the .loader section. Allocate
3193 lsec = xcoff_hash_table (info)->loader_section;
3194 lsec->_raw_size = stoff + ldhdr->l_stlen;
3195 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3196 if (lsec->contents == NULL)
3199 /* Set up the header. */
3200 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3201 (struct external_ldhdr *) lsec->contents);
3203 /* Set up the import file names. */
3204 out = (char *) lsec->contents + ldhdr->l_impoff;
3205 strcpy (out, libpath);
3206 out += strlen (libpath) + 1;
3209 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3211 register const char *s;
3214 while ((*out++ = *s++) != '\0')
3217 while ((*out++ = *s++) != '\0')
3220 while ((*out++ = *s++) != '\0')
3224 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3226 /* Set up the symbol string table. */
3227 if (ldinfo.string_size > 0)
3229 memcpy (out, ldinfo.strings, ldinfo.string_size);
3230 free (ldinfo.strings);
3231 ldinfo.strings = NULL;
3234 /* We can't set up the symbol table or the relocs yet, because we
3235 don't yet know the final position of the various sections. The
3236 .loader symbols are written out when the corresponding normal
3237 symbols are written out in xcoff_link_input_bfd or
3238 xcoff_write_global_symbol. The .loader relocs are written out
3239 when the corresponding normal relocs are handled in
3240 xcoff_link_input_bfd. */
3242 /* Allocate space for the magic sections. */
3243 sec = xcoff_hash_table (info)->linkage_section;
3244 if (sec->_raw_size > 0)
3246 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3247 if (sec->contents == NULL)
3250 sec = xcoff_hash_table (info)->toc_section;
3251 if (sec->_raw_size > 0)
3253 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3254 if (sec->contents == NULL)
3257 sec = xcoff_hash_table (info)->descriptor_section;
3258 if (sec->_raw_size > 0)
3260 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3261 if (sec->contents == NULL)
3265 /* Now that we've done garbage collection, figure out the contents
3266 of the .debug section. */
3267 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3269 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3272 bfd_size_type symcount;
3273 unsigned long *debug_index;
3275 bfd_byte *esym, *esymend;
3276 bfd_size_type symesz;
3278 if (sub->xvec != info->hash->creator)
3280 subdeb = bfd_get_section_by_name (sub, ".debug");
3281 if (subdeb == NULL || subdeb->_raw_size == 0)
3284 if (info->strip == strip_all
3285 || info->strip == strip_debugger
3286 || info->discard == discard_all)
3288 subdeb->_raw_size = 0;
3292 if (! _bfd_coff_get_external_symbols (sub))
3295 symcount = obj_raw_syment_count (sub);
3296 debug_index = ((unsigned long *)
3297 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3298 if (debug_index == NULL)
3300 xcoff_data (sub)->debug_indices = debug_index;
3302 /* Grab the contents of the .debug section. We use malloc and
3303 copy the neams into the debug stringtab, rather than
3304 bfd_alloc, because I expect that, when linking many files
3305 together, many of the strings will be the same. Storing the
3306 strings in the hash table should save space in this case. */
3307 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3308 if (debug_contents == NULL)
3310 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3311 (file_ptr) 0, subdeb->_raw_size))
3314 csectpp = xcoff_data (sub)->csects;
3316 symesz = bfd_coff_symesz (sub);
3317 esym = (bfd_byte *) obj_coff_external_syms (sub);
3318 esymend = esym + symcount * symesz;
3319 while (esym < esymend)
3321 struct internal_syment sym;
3323 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3325 *debug_index = (unsigned long) -1;
3327 if (sym._n._n_n._n_zeroes == 0
3330 || ((*csectpp)->flags & SEC_MARK) != 0
3331 || *csectpp == bfd_abs_section_ptr)
3332 && bfd_coff_symname_in_debug (sub, &sym))
3337 name = (char *) debug_contents + sym._n._n_n._n_offset;
3338 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3339 if (indx == (bfd_size_type) -1)
3341 *debug_index = indx;
3344 esym += (sym.n_numaux + 1) * symesz;
3345 csectpp += sym.n_numaux + 1;
3346 debug_index += sym.n_numaux + 1;
3349 free (debug_contents);
3350 debug_contents = NULL;
3352 /* Clear the size of subdeb, so that it is not included directly
3353 in the output file. */
3354 subdeb->_raw_size = 0;
3356 if (! info->keep_memory)
3358 if (! _bfd_coff_free_symbols (sub))
3363 xcoff_hash_table (info)->debug_section->_raw_size =
3364 _bfd_stringtab_size (debug_strtab);
3369 if (ldinfo.strings != NULL)
3370 free (ldinfo.strings);
3371 if (debug_contents != NULL)
3372 free (debug_contents);
3376 /* Add a symbol to the .loader symbols, if necessary. */
3379 xcoff_build_ldsyms (h, p)
3380 struct xcoff_link_hash_entry *h;
3383 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3386 /* If this is a final link, and the symbol was defined as a common
3387 symbol in a regular object file, and there was no definition in
3388 any dynamic object, then the linker will have allocated space for
3389 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3390 will not have been set. */
3391 if (h->root.type == bfd_link_hash_defined
3392 && (h->flags & XCOFF_DEF_REGULAR) == 0
3393 && (h->flags & XCOFF_REF_REGULAR) != 0
3394 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3395 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3396 h->flags |= XCOFF_DEF_REGULAR;
3398 /* If all defined symbols should be exported, mark them now. We
3399 don't want to export the actual functions, just the function
3401 if (ldinfo->export_defineds
3402 && (h->flags & XCOFF_DEF_REGULAR) != 0
3403 && h->root.root.string[0] != '.')
3404 h->flags |= XCOFF_EXPORT;
3406 /* We don't want to garbage collect symbols which are not defined in
3407 XCOFF files. This is a convenient place to mark them. */
3408 if (xcoff_hash_table (ldinfo->info)->gc
3409 && (h->flags & XCOFF_MARK) == 0
3410 && (h->root.type == bfd_link_hash_defined
3411 || h->root.type == bfd_link_hash_defweak)
3412 && (h->root.u.def.section->owner == NULL
3413 || (h->root.u.def.section->owner->xvec
3414 != ldinfo->info->hash->creator)))
3415 h->flags |= XCOFF_MARK;
3417 /* If this symbol is called and defined in a dynamic object, or not
3418 defined at all when building a shared object, or imported, then
3419 we need to set up global linkage code for it. (Unless we did
3420 garbage collection and we didn't need this symbol.) */
3421 if ((h->flags & XCOFF_CALLED) != 0
3422 && (h->root.type == bfd_link_hash_undefined
3423 || h->root.type == bfd_link_hash_undefweak)
3424 && h->root.root.string[0] == '.'
3425 && h->descriptor != NULL
3426 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3427 || ldinfo->info->shared
3428 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3429 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3430 && (! xcoff_hash_table (ldinfo->info)->gc
3431 || (h->flags & XCOFF_MARK) != 0))
3434 struct xcoff_link_hash_entry *hds;
3436 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3437 h->root.type = bfd_link_hash_defined;
3438 h->root.u.def.section = sec;
3439 h->root.u.def.value = sec->_raw_size;
3441 h->flags |= XCOFF_DEF_REGULAR;
3442 sec->_raw_size += XCOFF_GLINK_SIZE;
3444 /* The global linkage code requires a TOC entry for the
3446 hds = h->descriptor;
3447 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3448 || hds->root.type == bfd_link_hash_undefweak)
3449 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3450 hds->flags |= XCOFF_MARK;
3451 if (hds->toc_section == NULL)
3453 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3454 hds->u.toc_offset = hds->toc_section->_raw_size;
3455 hds->toc_section->_raw_size += 4;
3456 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3457 ++hds->toc_section->reloc_count;
3459 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3461 /* We need to call xcoff_build_ldsyms recursively here,
3462 because we may already have passed hds on the traversal. */
3463 xcoff_build_ldsyms (hds, p);
3467 /* If this symbol is exported, but not defined, we need to try to
3469 if ((h->flags & XCOFF_EXPORT) != 0
3470 && (h->flags & XCOFF_IMPORT) == 0
3471 && (h->flags & XCOFF_DEF_REGULAR) == 0
3472 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3473 && (h->root.type == bfd_link_hash_undefined
3474 || h->root.type == bfd_link_hash_undefweak))
3476 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3477 && (h->descriptor->root.type == bfd_link_hash_defined
3478 || h->descriptor->root.type == bfd_link_hash_defweak))
3482 /* This is an undefined function descriptor associated with
3483 a defined entry point. We can build up a function
3484 descriptor ourselves. Believe it or not, the AIX linker
3485 actually does this, and there are cases where we need to
3487 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3488 h->root.type = bfd_link_hash_defined;
3489 h->root.u.def.section = sec;
3490 h->root.u.def.value = sec->_raw_size;
3492 h->flags |= XCOFF_DEF_REGULAR;
3493 sec->_raw_size += 12;
3495 /* A function descriptor uses two relocs: one for the
3496 associated code, and one for the TOC address. */
3497 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3498 sec->reloc_count += 2;
3500 /* We handle writing out the contents of the descriptor in
3501 xcoff_write_global_symbol. */
3505 (*_bfd_error_handler)
3506 ("attempt to export undefined symbol `%s'",
3507 h->root.root.string);
3508 ldinfo->failed = true;
3509 bfd_set_error (bfd_error_invalid_operation);
3514 /* If this is still a common symbol, and it wasn't garbage
3515 collected, we need to actually allocate space for it in the .bss
3517 if (h->root.type == bfd_link_hash_common
3518 && (! xcoff_hash_table (ldinfo->info)->gc
3519 || (h->flags & XCOFF_MARK) != 0)
3520 && h->root.u.c.p->section->_raw_size == 0)
3522 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3523 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3526 /* We need to add a symbol to the .loader section if it is mentioned
3527 in a reloc which we are copying to the .loader section and it was
3528 not defined or common, or if it is the entry point, or if it is
3531 if (((h->flags & XCOFF_LDREL) == 0
3532 || h->root.type == bfd_link_hash_defined
3533 || h->root.type == bfd_link_hash_defweak
3534 || h->root.type == bfd_link_hash_common)
3535 && (h->flags & XCOFF_ENTRY) == 0
3536 && (h->flags & XCOFF_EXPORT) == 0)
3542 /* We don't need to add this symbol if we did garbage collection and
3543 we did not mark this symbol. */
3544 if (xcoff_hash_table (ldinfo->info)->gc
3545 && (h->flags & XCOFF_MARK) == 0)
3551 /* We may have already processed this symbol due to the recursive
3553 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3556 /* We need to add this symbol to the .loader symbols. */
3558 /* h->ldsym will already have been allocated for an explicitly
3560 if (h->ldsym == NULL)
3562 h->ldsym = ((struct internal_ldsym *)
3563 bfd_zalloc (ldinfo->output_bfd,
3564 sizeof (struct internal_ldsym)));
3565 if (h->ldsym == NULL)
3567 ldinfo->failed = true;
3572 /* The first 3 symbol table indices are reserved to indicate the
3574 h->ldindx = ldinfo->ldsym_count + 3;
3576 ++ldinfo->ldsym_count;
3578 len = strlen (h->root.root.string);
3579 if (len <= SYMNMLEN)
3580 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3583 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3586 bfd_byte *newstrings;
3588 newalc = ldinfo->string_alc * 2;
3591 while (ldinfo->string_size + len + 3 > newalc)
3594 newstrings = ((bfd_byte *)
3595 bfd_realloc ((PTR) ldinfo->strings, newalc));
3596 if (newstrings == NULL)
3598 ldinfo->failed = true;
3601 ldinfo->string_alc = newalc;
3602 ldinfo->strings = newstrings;
3605 bfd_put_16 (ldinfo->output_bfd, len + 1,
3606 ldinfo->strings + ldinfo->string_size);
3607 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3608 h->ldsym->_l._l_l._l_zeroes = 0;
3609 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3610 ldinfo->string_size += len + 3;
3613 h->flags |= XCOFF_BUILT_LDSYM;
3618 /* Do the final link step. */
3621 _bfd_xcoff_bfd_final_link (abfd, info)
3623 struct bfd_link_info *info;
3625 bfd_size_type symesz;
3626 struct xcoff_final_link_info finfo;
3628 struct bfd_link_order *p;
3629 size_t max_contents_size;
3630 size_t max_sym_count;
3631 size_t max_lineno_count;
3632 size_t max_reloc_count;
3633 size_t max_output_reloc_count;
3634 file_ptr rel_filepos;
3636 file_ptr line_filepos;
3637 unsigned int linesz;
3639 bfd_byte *external_relocs = NULL;
3640 char strbuf[STRING_SIZE_SIZE];
3643 abfd->flags |= DYNAMIC;
3645 symesz = bfd_coff_symesz (abfd);
3648 finfo.output_bfd = abfd;
3649 finfo.strtab = NULL;
3650 finfo.section_info = NULL;
3651 finfo.last_file_index = -1;
3652 finfo.toc_symindx = -1;
3653 finfo.internal_syms = NULL;
3654 finfo.sym_indices = NULL;
3655 finfo.outsyms = NULL;
3656 finfo.linenos = NULL;
3657 finfo.contents = NULL;
3658 finfo.external_relocs = NULL;
3660 finfo.ldsym = ((struct external_ldsym *)
3661 (xcoff_hash_table (info)->loader_section->contents
3663 finfo.ldrel = ((struct external_ldrel *)
3664 (xcoff_hash_table (info)->loader_section->contents
3666 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3668 xcoff_data (abfd)->coff.link_info = info;
3670 finfo.strtab = _bfd_stringtab_init ();
3671 if (finfo.strtab == NULL)
3674 /* Count the line number and relocation entries required for the
3675 output file. Determine a few maximum sizes. */
3676 max_contents_size = 0;
3677 max_lineno_count = 0;
3678 max_reloc_count = 0;
3679 for (o = abfd->sections; o != NULL; o = o->next)
3682 o->lineno_count = 0;
3683 for (p = o->link_order_head; p != NULL; p = p->next)
3685 if (p->type == bfd_indirect_link_order)
3689 sec = p->u.indirect.section;
3691 /* Mark all sections which are to be included in the
3692 link. This will normally be every section. We need
3693 to do this so that we can identify any sections which
3694 the linker has decided to not include. */
3695 sec->linker_mark = true;
3697 if (info->strip == strip_none
3698 || info->strip == strip_some)
3699 o->lineno_count += sec->lineno_count;
3701 o->reloc_count += sec->reloc_count;
3703 if (sec->_raw_size > max_contents_size)
3704 max_contents_size = sec->_raw_size;
3705 if (sec->lineno_count > max_lineno_count)
3706 max_lineno_count = sec->lineno_count;
3707 if (coff_section_data (sec->owner, sec) != NULL
3708 && xcoff_section_data (sec->owner, sec) != NULL
3709 && (xcoff_section_data (sec->owner, sec)->lineno_count
3710 > max_lineno_count))
3712 xcoff_section_data (sec->owner, sec)->lineno_count;
3713 if (sec->reloc_count > max_reloc_count)
3714 max_reloc_count = sec->reloc_count;
3716 else if (p->type == bfd_section_reloc_link_order
3717 || p->type == bfd_symbol_reloc_link_order)
3722 /* Compute the file positions for all the sections. */
3723 if (abfd->output_has_begun)
3725 if (xcoff_hash_table (info)->file_align != 0)
3732 file_align = xcoff_hash_table (info)->file_align;
3733 if (file_align != 0)
3735 boolean saw_contents;
3740 /* Insert .pad sections before every section which has
3741 contents and is loaded, if it is preceded by some other
3742 section which has contents and is loaded. */
3743 saw_contents = true;
3744 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3746 (*op)->target_index = indx;
3747 if (strcmp ((*op)->name, ".pad") == 0)
3748 saw_contents = false;
3749 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3750 && ((*op)->flags & SEC_LOAD) != 0)
3753 saw_contents = true;
3760 n = bfd_make_section_anyway (abfd, ".pad");
3761 BFD_ASSERT (*op == n);
3763 n->flags = SEC_HAS_CONTENTS;
3764 n->alignment_power = 0;
3765 saw_contents = false;
3770 /* Reset the section indices after inserting the new
3773 for (o = abfd->sections; o != NULL; o = o->next)
3776 o->target_index = indx;
3778 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3780 /* Work out appropriate sizes for the .pad sections to force
3781 each section to land on a page boundary. This bit of
3782 code knows what compute_section_file_positions is going
3784 sofar = bfd_coff_filhsz (abfd);
3785 sofar += bfd_coff_aoutsz (abfd);
3786 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3787 for (o = abfd->sections; o != NULL; o = o->next)
3788 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
3789 sofar += bfd_coff_scnhsz (abfd);
3791 for (o = abfd->sections; o != NULL; o = o->next)
3793 if (strcmp (o->name, ".pad") == 0)
3797 BFD_ASSERT (o->_raw_size == 0);
3798 pageoff = sofar & (file_align - 1);
3801 o->_raw_size = file_align - pageoff;
3802 sofar += file_align - pageoff;
3803 o->flags |= SEC_HAS_CONTENTS;
3808 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3809 sofar += BFD_ALIGN (o->_raw_size,
3810 1 << o->alignment_power);
3815 bfd_coff_compute_section_file_positions (abfd);
3818 /* Allocate space for the pointers we need to keep for the relocs. */
3822 /* We use section_count + 1, rather than section_count, because
3823 the target_index fields are 1 based. */
3824 finfo.section_info =
3825 ((struct xcoff_link_section_info *)
3826 bfd_malloc ((abfd->section_count + 1)
3827 * sizeof (struct xcoff_link_section_info)));
3828 if (finfo.section_info == NULL)
3830 for (i = 0; i <= abfd->section_count; i++)
3832 finfo.section_info[i].relocs = NULL;
3833 finfo.section_info[i].rel_hashes = NULL;
3834 finfo.section_info[i].toc_rel_hashes = NULL;
3838 /* Set the file positions for the relocs. */
3839 rel_filepos = obj_relocbase (abfd);
3840 relsz = bfd_coff_relsz (abfd);
3841 max_output_reloc_count = 0;
3842 for (o = abfd->sections; o != NULL; o = o->next)
3844 if (o->reloc_count == 0)
3848 o->flags |= SEC_RELOC;
3849 o->rel_filepos = rel_filepos;
3850 rel_filepos += o->reloc_count * relsz;
3852 /* We don't know the indices of global symbols until we have
3853 written out all the local symbols. For each section in
3854 the output file, we keep an array of pointers to hash
3855 table entries. Each entry in the array corresponds to a
3856 reloc. When we find a reloc against a global symbol, we
3857 set the corresponding entry in this array so that we can
3858 fix up the symbol index after we have written out all the
3861 Because of this problem, we also keep the relocs in
3862 memory until the end of the link. This wastes memory.
3863 We could backpatch the file later, I suppose, although it
3865 finfo.section_info[o->target_index].relocs =
3866 ((struct internal_reloc *)
3867 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
3868 finfo.section_info[o->target_index].rel_hashes =
3869 ((struct xcoff_link_hash_entry **)
3870 bfd_malloc (o->reloc_count
3871 * sizeof (struct xcoff_link_hash_entry *)));
3872 if (finfo.section_info[o->target_index].relocs == NULL
3873 || finfo.section_info[o->target_index].rel_hashes == NULL)
3876 if (o->reloc_count > max_output_reloc_count)
3877 max_output_reloc_count = o->reloc_count;
3881 /* We now know the size of the relocs, so we can determine the file
3882 positions of the line numbers. */
3883 line_filepos = rel_filepos;
3884 finfo.line_filepos = line_filepos;
3885 linesz = bfd_coff_linesz (abfd);
3886 for (o = abfd->sections; o != NULL; o = o->next)
3888 if (o->lineno_count == 0)
3889 o->line_filepos = 0;
3892 o->line_filepos = line_filepos;
3893 line_filepos += o->lineno_count * linesz;
3896 /* Reset the reloc and lineno counts, so that we can use them to
3897 count the number of entries we have output so far. */
3899 o->lineno_count = 0;
3902 obj_sym_filepos (abfd) = line_filepos;
3904 /* Figure out the largest number of symbols in an input BFD. Take
3905 the opportunity to clear the output_has_begun fields of all the
3906 input BFD's. We want at least 4 symbols, since that is the
3907 number which xcoff_write_global_symbol may need. */
3909 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3913 sub->output_has_begun = false;
3914 sz = obj_raw_syment_count (sub);
3915 if (sz > max_sym_count)
3919 /* Allocate some buffers used while linking. */
3920 finfo.internal_syms = ((struct internal_syment *)
3921 bfd_malloc (max_sym_count
3922 * sizeof (struct internal_syment)));
3923 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
3924 finfo.outsyms = ((bfd_byte *)
3925 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
3926 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
3927 * bfd_coff_linesz (abfd));
3928 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3929 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
3930 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3931 || (finfo.sym_indices == NULL && max_sym_count > 0)
3932 || finfo.outsyms == NULL
3933 || (finfo.linenos == NULL && max_lineno_count > 0)
3934 || (finfo.contents == NULL && max_contents_size > 0)
3935 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3938 obj_raw_syment_count (abfd) = 0;
3939 xcoff_data (abfd)->toc = (bfd_vma) -1;
3941 /* We now know the position of everything in the file, except that
3942 we don't know the size of the symbol table and therefore we don't
3943 know where the string table starts. We just build the string
3944 table in memory as we go along. We process all the relocations
3945 for a single input file at once. */
3946 for (o = abfd->sections; o != NULL; o = o->next)
3948 for (p = o->link_order_head; p != NULL; p = p->next)
3950 if (p->type == bfd_indirect_link_order
3951 && p->u.indirect.section->owner->xvec == abfd->xvec)
3953 sub = p->u.indirect.section->owner;
3954 if (! sub->output_has_begun)
3956 if (! xcoff_link_input_bfd (&finfo, sub))
3958 sub->output_has_begun = true;
3961 else if (p->type == bfd_section_reloc_link_order
3962 || p->type == bfd_symbol_reloc_link_order)
3964 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3969 if (! _bfd_default_link_order (abfd, info, o, p))
3975 /* Free up the buffers used by xcoff_link_input_bfd. */
3977 if (finfo.internal_syms != NULL)
3979 free (finfo.internal_syms);
3980 finfo.internal_syms = NULL;
3982 if (finfo.sym_indices != NULL)
3984 free (finfo.sym_indices);
3985 finfo.sym_indices = NULL;
3987 if (finfo.linenos != NULL)
3989 free (finfo.linenos);
3990 finfo.linenos = NULL;
3992 if (finfo.contents != NULL)
3994 free (finfo.contents);
3995 finfo.contents = NULL;
3997 if (finfo.external_relocs != NULL)
3999 free (finfo.external_relocs);
4000 finfo.external_relocs = NULL;
4003 /* The value of the last C_FILE symbol is supposed to be -1. Write
4005 if (finfo.last_file_index != -1)
4007 finfo.last_file.n_value = -1;
4008 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4009 (PTR) finfo.outsyms);
4011 (obj_sym_filepos (abfd)
4012 + finfo.last_file_index * symesz),
4014 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4018 /* Write out all the global symbols which do not come from XCOFF
4020 xcoff_link_hash_traverse (xcoff_hash_table (info),
4021 xcoff_write_global_symbol,
4024 if (finfo.outsyms != NULL)
4026 free (finfo.outsyms);
4027 finfo.outsyms = NULL;
4030 /* Now that we have written out all the global symbols, we know the
4031 symbol indices to use for relocs against them, and we can finally
4032 write out the relocs. */
4033 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
4034 if (external_relocs == NULL && max_output_reloc_count != 0)
4036 bfd_set_error (bfd_error_no_memory);
4040 for (o = abfd->sections; o != NULL; o = o->next)
4042 struct internal_reloc *irel;
4043 struct internal_reloc *irelend;
4044 struct xcoff_link_hash_entry **rel_hash;
4045 struct xcoff_toc_rel_hash *toc_rel_hash;
4048 if (o->reloc_count == 0)
4051 irel = finfo.section_info[o->target_index].relocs;
4052 irelend = irel + o->reloc_count;
4053 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4054 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4056 if (*rel_hash != NULL)
4058 if ((*rel_hash)->indx < 0)
4060 if (! ((*info->callbacks->unattached_reloc)
4061 (info, (*rel_hash)->root.root.string,
4062 (bfd *) NULL, o, irel->r_vaddr)))
4064 (*rel_hash)->indx = 0;
4066 irel->r_symndx = (*rel_hash)->indx;
4070 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4071 toc_rel_hash != NULL;
4072 toc_rel_hash = toc_rel_hash->next)
4074 if (toc_rel_hash->h->u.toc_indx < 0)
4076 if (! ((*info->callbacks->unattached_reloc)
4077 (info, toc_rel_hash->h->root.root.string,
4078 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4080 toc_rel_hash->h->u.toc_indx = 0;
4082 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4085 /* XCOFF requires that the relocs be sorted by address. We tend
4086 to produce them in the order in which their containing csects
4087 appear in the symbol table, which is not necessarily by
4088 address. So we sort them here. There may be a better way to
4090 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4091 o->reloc_count, sizeof (struct internal_reloc),
4094 irel = finfo.section_info[o->target_index].relocs;
4095 irelend = irel + o->reloc_count;
4096 erel = external_relocs;
4097 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4098 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4100 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4101 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4102 abfd) != relsz * o->reloc_count)
4106 if (external_relocs != NULL)
4108 free (external_relocs);
4109 external_relocs = NULL;
4112 /* Free up the section information. */
4113 if (finfo.section_info != NULL)
4117 for (i = 0; i < abfd->section_count; i++)
4119 if (finfo.section_info[i].relocs != NULL)
4120 free (finfo.section_info[i].relocs);
4121 if (finfo.section_info[i].rel_hashes != NULL)
4122 free (finfo.section_info[i].rel_hashes);
4124 free (finfo.section_info);
4125 finfo.section_info = NULL;
4128 /* Write out the loader section contents. */
4129 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4130 == (xcoff_hash_table (info)->loader_section->contents
4131 + xcoff_hash_table (info)->ldhdr.l_impoff));
4132 o = xcoff_hash_table (info)->loader_section;
4133 if (! bfd_set_section_contents (abfd, o->output_section,
4134 o->contents, o->output_offset,
4138 /* Write out the magic sections. */
4139 o = xcoff_hash_table (info)->linkage_section;
4140 if (o->_raw_size > 0
4141 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4142 o->output_offset, o->_raw_size))
4144 o = xcoff_hash_table (info)->toc_section;
4145 if (o->_raw_size > 0
4146 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4147 o->output_offset, o->_raw_size))
4149 o = xcoff_hash_table (info)->descriptor_section;
4150 if (o->_raw_size > 0
4151 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4152 o->output_offset, o->_raw_size))
4155 /* Write out the string table. */
4157 (obj_sym_filepos (abfd)
4158 + obj_raw_syment_count (abfd) * symesz),
4162 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4163 (bfd_byte *) strbuf);
4164 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4166 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4169 _bfd_stringtab_free (finfo.strtab);
4171 /* Write out the debugging string table. */
4172 o = xcoff_hash_table (info)->debug_section;
4175 struct bfd_strtab_hash *debug_strtab;
4177 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4178 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4179 >= _bfd_stringtab_size (debug_strtab));
4181 o->output_section->filepos + o->output_offset,
4184 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4188 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4189 not try to write out the symbols. */
4190 bfd_get_symcount (abfd) = 0;
4195 if (finfo.strtab != NULL)
4196 _bfd_stringtab_free (finfo.strtab);
4197 if (finfo.section_info != NULL)
4201 for (i = 0; i < abfd->section_count; i++)
4203 if (finfo.section_info[i].relocs != NULL)
4204 free (finfo.section_info[i].relocs);
4205 if (finfo.section_info[i].rel_hashes != NULL)
4206 free (finfo.section_info[i].rel_hashes);
4208 free (finfo.section_info);
4210 if (finfo.internal_syms != NULL)
4211 free (finfo.internal_syms);
4212 if (finfo.sym_indices != NULL)
4213 free (finfo.sym_indices);
4214 if (finfo.outsyms != NULL)
4215 free (finfo.outsyms);
4216 if (finfo.linenos != NULL)
4217 free (finfo.linenos);
4218 if (finfo.contents != NULL)
4219 free (finfo.contents);
4220 if (finfo.external_relocs != NULL)
4221 free (finfo.external_relocs);
4222 if (external_relocs != NULL)
4223 free (external_relocs);
4227 /* Link an input file into the linker output file. This function
4228 handles all the sections and relocations of the input file at once. */
4231 xcoff_link_input_bfd (finfo, input_bfd)
4232 struct xcoff_final_link_info *finfo;
4236 const char *strings;
4237 bfd_size_type syment_base;
4238 unsigned int n_tmask;
4239 unsigned int n_btshft;
4241 bfd_size_type isymesz;
4242 bfd_size_type osymesz;
4243 bfd_size_type linesz;
4246 struct xcoff_link_hash_entry **sym_hash;
4247 struct internal_syment *isymp;
4249 unsigned long *debug_index;
4251 unsigned long output_index;
4258 /* We can just skip DYNAMIC files, unless this is a static link. */
4259 if ((input_bfd->flags & DYNAMIC) != 0
4260 && ! finfo->info->static_link)
4263 /* Move all the symbols to the output file. */
4265 output_bfd = finfo->output_bfd;
4267 syment_base = obj_raw_syment_count (output_bfd);
4268 isymesz = bfd_coff_symesz (input_bfd);
4269 osymesz = bfd_coff_symesz (output_bfd);
4270 linesz = bfd_coff_linesz (input_bfd);
4271 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4273 n_tmask = coff_data (input_bfd)->local_n_tmask;
4274 n_btshft = coff_data (input_bfd)->local_n_btshft;
4276 /* Define macros so that ISFCN, et. al., macros work correctly. */
4277 #define N_TMASK n_tmask
4278 #define N_BTSHFT n_btshft
4281 if (! finfo->info->keep_memory)
4284 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4287 if (! _bfd_coff_get_external_symbols (input_bfd))
4290 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4291 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4292 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4293 csectpp = xcoff_data (input_bfd)->csects;
4294 debug_index = xcoff_data (input_bfd)->debug_indices;
4295 isymp = finfo->internal_syms;
4296 indexp = finfo->sym_indices;
4297 output_index = syment_base;
4298 outsym = finfo->outsyms;
4302 while (esym < esym_end)
4304 struct internal_syment isym;
4305 union internal_auxent aux;
4311 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4313 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4315 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4317 BFD_ASSERT (isymp->n_numaux > 0);
4318 bfd_coff_swap_aux_in (input_bfd,
4319 (PTR) (esym + isymesz * isymp->n_numaux),
4320 isymp->n_type, isymp->n_sclass,
4321 isymp->n_numaux - 1, isymp->n_numaux,
4323 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4326 /* Make a copy of *isymp so that the relocate_section function
4327 always sees the original values. This is more reliable than
4328 always recomputing the symbol value even if we are stripping
4332 /* If this symbol is in the .loader section, swap out the
4333 .loader symbol information. If this is an external symbol
4334 reference to a defined symbol, though, then wait until we get
4335 to the definition. */
4336 if (isym.n_sclass == C_EXT
4337 && *sym_hash != NULL
4338 && (*sym_hash)->ldsym != NULL
4340 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4342 struct xcoff_link_hash_entry *h;
4343 struct internal_ldsym *ldsym;
4347 if (isym.n_scnum > 0)
4349 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4350 ldsym->l_value = (isym.n_value
4351 + (*csectpp)->output_section->vma
4352 + (*csectpp)->output_offset
4357 ldsym->l_scnum = isym.n_scnum;
4358 ldsym->l_value = isym.n_value;
4361 ldsym->l_smtype = smtyp;
4362 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4363 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4364 || (h->flags & XCOFF_IMPORT) != 0)
4365 ldsym->l_smtype |= L_IMPORT;
4366 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4367 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4368 || (h->flags & XCOFF_EXPORT) != 0)
4369 ldsym->l_smtype |= L_EXPORT;
4370 if ((h->flags & XCOFF_ENTRY) != 0)
4371 ldsym->l_smtype |= L_ENTRY;
4373 ldsym->l_smclas = aux.x_csect.x_smclas;
4375 if (ldsym->l_ifile == (bfd_size_type) -1)
4377 else if (ldsym->l_ifile == 0)
4379 if ((ldsym->l_smtype & L_IMPORT) == 0)
4385 if (h->root.type == bfd_link_hash_defined
4386 || h->root.type == bfd_link_hash_defweak)
4387 impbfd = h->root.u.def.section->owner;
4388 else if (h->root.type == bfd_link_hash_undefined
4389 || h->root.type == bfd_link_hash_undefweak)
4390 impbfd = h->root.u.undef.abfd;
4398 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4399 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4406 BFD_ASSERT (h->ldindx >= 0);
4407 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4408 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4409 finfo->ldsym + h->ldindx - 3);
4412 /* Fill in snentry now that we know the target_index. */
4413 if ((h->flags & XCOFF_ENTRY) != 0
4414 && (h->root.type == bfd_link_hash_defined
4415 || h->root.type == bfd_link_hash_defweak))
4416 xcoff_data (output_bfd)->snentry =
4417 h->root.u.def.section->output_section->target_index;
4424 add = 1 + isym.n_numaux;
4426 /* If we are skipping this csect, we want to skip this symbol. */
4427 if (*csectpp == NULL)
4430 /* If we garbage collected this csect, we want to skip this
4433 && xcoff_hash_table (finfo->info)->gc
4434 && ((*csectpp)->flags & SEC_MARK) == 0
4435 && *csectpp != bfd_abs_section_ptr)
4438 /* An XCOFF linker always skips C_STAT symbols. */
4440 && isymp->n_sclass == C_STAT)
4443 /* We skip all but the first TOC anchor. */
4445 && isymp->n_sclass == C_HIDEXT
4446 && aux.x_csect.x_smclas == XMC_TC0)
4448 if (finfo->toc_symindx != -1)
4452 bfd_vma tocval, tocend;
4454 tocval = ((*csectpp)->output_section->vma
4455 + (*csectpp)->output_offset
4458 /* We want to find out if tocval is a good value to use
4459 as the TOC anchor--that is, whether we can access all
4460 of the TOC using a 16 bit offset from tocval. This
4461 test assumes that the TOC comes at the end of the
4462 output section, as it does in the default linker
4465 tocend = ((*csectpp)->output_section->vma
4466 + (*csectpp)->output_section->_raw_size);
4468 if (tocval + 0x10000 < tocend)
4470 (*_bfd_error_handler)
4471 ("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling",
4472 (unsigned long) (tocend - tocval));
4473 bfd_set_error (bfd_error_file_too_big);
4477 if (tocval + 0x8000 < tocend)
4481 tocadd = tocend - (tocval + 0x8000);
4483 isym.n_value += tocadd;
4486 finfo->toc_symindx = output_index;
4487 xcoff_data (finfo->output_bfd)->toc = tocval;
4488 xcoff_data (finfo->output_bfd)->sntoc =
4489 (*csectpp)->output_section->target_index;
4494 /* If we are stripping all symbols, we want to skip this one. */
4496 && finfo->info->strip == strip_all)
4499 /* We can skip resolved external references. */
4501 && isym.n_sclass == C_EXT
4503 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4506 /* We can skip common symbols if they got defined somewhere
4509 && isym.n_sclass == C_EXT
4511 && ((*sym_hash)->root.type != bfd_link_hash_common
4512 || (*sym_hash)->root.u.c.p->section != *csectpp)
4513 && ((*sym_hash)->root.type != bfd_link_hash_defined
4514 || (*sym_hash)->root.u.def.section != *csectpp))
4517 /* Skip local symbols if we are discarding them. */
4519 && finfo->info->discard == discard_all
4520 && isym.n_sclass != C_EXT
4521 && (isym.n_sclass != C_HIDEXT
4522 || smtyp != XTY_SD))
4525 /* If we stripping debugging symbols, and this is a debugging
4526 symbol, then skip it. */
4528 && finfo->info->strip == strip_debugger
4529 && isym.n_scnum == N_DEBUG)
4532 /* If some symbols are stripped based on the name, work out the
4533 name and decide whether to skip this symbol. We don't handle
4534 this correctly for symbols whose names are in the .debug
4535 section; to get it right we would need a new bfd_strtab_hash
4536 function to return the string given the index. */
4538 && (finfo->info->strip == strip_some
4539 || finfo->info->discard == discard_l)
4540 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4543 char buf[SYMNMLEN + 1];
4545 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4549 if ((finfo->info->strip == strip_some
4550 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4552 || (finfo->info->discard == discard_l
4553 && (isym.n_sclass != C_EXT
4554 && (isym.n_sclass != C_HIDEXT
4555 || smtyp != XTY_SD))
4556 && strncmp (name, finfo->info->lprefix,
4557 finfo->info->lprefix_len) == 0))
4561 /* We can not skip the first TOC anchor. */
4564 && finfo->info->strip != strip_all)
4567 /* We now know whether we are to skip this symbol or not. */
4570 /* Adjust the symbol in order to output it. */
4572 if (isym._n._n_n._n_zeroes == 0
4573 && isym._n._n_n._n_offset != 0)
4575 /* This symbol has a long name. Enter it in the string
4576 table we are building. If *debug_index != -1, the
4577 name has already been entered in the .debug section. */
4578 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4579 isym._n._n_n._n_offset = *debug_index;
4585 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4589 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4590 if (indx == (bfd_size_type) -1)
4592 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4596 if (isym.n_sclass != C_BSTAT
4597 && isym.n_sclass != C_ESTAT
4598 && isym.n_sclass != C_DECL
4599 && isym.n_scnum > 0)
4601 isym.n_scnum = (*csectpp)->output_section->target_index;
4602 isym.n_value += ((*csectpp)->output_section->vma
4603 + (*csectpp)->output_offset
4607 /* The value of a C_FILE symbol is the symbol index of the
4608 next C_FILE symbol. The value of the last C_FILE symbol
4609 is -1. We try to get this right, below, just before we
4610 write the symbols out, but in the general case we may
4611 have to write the symbol out twice. */
4612 if (isym.n_sclass == C_FILE)
4614 if (finfo->last_file_index != -1
4615 && finfo->last_file.n_value != (long) output_index)
4617 /* We must correct the value of the last C_FILE entry. */
4618 finfo->last_file.n_value = output_index;
4619 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4621 /* The last C_FILE symbol is in this input file. */
4622 bfd_coff_swap_sym_out (output_bfd,
4623 (PTR) &finfo->last_file,
4624 (PTR) (finfo->outsyms
4625 + ((finfo->last_file_index
4631 /* We have already written out the last C_FILE
4632 symbol. We need to write it out again. We
4633 borrow *outsym temporarily. */
4634 bfd_coff_swap_sym_out (output_bfd,
4635 (PTR) &finfo->last_file,
4637 if (bfd_seek (output_bfd,
4638 (obj_sym_filepos (output_bfd)
4639 + finfo->last_file_index * osymesz),
4641 || (bfd_write (outsym, osymesz, 1, output_bfd)
4647 finfo->last_file_index = output_index;
4648 finfo->last_file = isym;
4651 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4652 into the line numbers. We update the symbol values when
4653 we handle the line numbers. */
4654 if (isym.n_sclass == C_BINCL
4655 || isym.n_sclass == C_EINCL)
4657 isym.n_value = finfo->line_filepos;
4661 /* Output the symbol. */
4663 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4665 *indexp = output_index;
4667 if (isym.n_sclass == C_EXT)
4670 struct xcoff_link_hash_entry *h;
4672 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4674 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4675 BFD_ASSERT (h != NULL);
4676 h->indx = output_index;
4679 /* If this is a symbol in the TOC which we may have merged
4680 (class XMC_TC), remember the symbol index of the TOC
4682 if (isym.n_sclass == C_HIDEXT
4683 && aux.x_csect.x_smclas == XMC_TC
4684 && *sym_hash != NULL)
4686 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4687 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4688 (*sym_hash)->u.toc_indx = output_index;
4691 output_index += add;
4692 outsym += add * osymesz;
4695 esym += add * isymesz;
4699 if (debug_index != NULL)
4702 for (--add; add > 0; --add)
4706 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4707 done in a separate pass, because we don't know the correct symbol
4708 indices until we have already decided which symbols we are going
4711 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4712 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4713 isymp = finfo->internal_syms;
4714 indexp = finfo->sym_indices;
4715 csectpp = xcoff_data (input_bfd)->csects;
4716 outsym = finfo->outsyms;
4717 while (esym < esym_end)
4721 add = 1 + isymp->n_numaux;
4724 esym += add * isymesz;
4729 if (isymp->n_sclass == C_BSTAT)
4731 struct internal_syment isym;
4734 /* The value of a C_BSTAT symbol is the symbol table
4735 index of the containing csect. */
4736 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4737 indx = isym.n_value;
4738 if (indx < obj_raw_syment_count (input_bfd))
4742 symindx = finfo->sym_indices[indx];
4746 isym.n_value = symindx;
4747 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4755 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4757 union internal_auxent aux;
4759 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4760 isymp->n_sclass, i, isymp->n_numaux,
4763 if (isymp->n_sclass == C_FILE)
4765 /* This is the file name (or some comment put in by
4766 the compiler). If it is long, we must put it in
4767 the string table. */
4768 if (aux.x_file.x_n.x_zeroes == 0
4769 && aux.x_file.x_n.x_offset != 0)
4771 const char *filename;
4774 BFD_ASSERT (aux.x_file.x_n.x_offset
4775 >= STRING_SIZE_SIZE);
4776 if (strings == NULL)
4778 strings = _bfd_coff_read_string_table (input_bfd);
4779 if (strings == NULL)
4782 filename = strings + aux.x_file.x_n.x_offset;
4783 indx = _bfd_stringtab_add (finfo->strtab, filename,
4785 if (indx == (bfd_size_type) -1)
4787 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4790 else if ((isymp->n_sclass == C_EXT
4791 || isymp->n_sclass == C_HIDEXT)
4792 && i + 1 == isymp->n_numaux)
4794 /* We don't support type checking. I don't know if
4796 aux.x_csect.x_parmhash = 0;
4797 /* I don't think anybody uses these fields, but we'd
4798 better clobber them just in case. */
4799 aux.x_csect.x_stab = 0;
4800 aux.x_csect.x_snstab = 0;
4801 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4805 indx = aux.x_csect.x_scnlen.l;
4806 if (indx < obj_raw_syment_count (input_bfd))
4810 symindx = finfo->sym_indices[indx];
4812 aux.x_sym.x_tagndx.l = 0;
4814 aux.x_sym.x_tagndx.l = symindx;
4818 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4822 if (ISFCN (isymp->n_type)
4823 || ISTAG (isymp->n_sclass)
4824 || isymp->n_sclass == C_BLOCK
4825 || isymp->n_sclass == C_FCN)
4827 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4829 && indx < obj_raw_syment_count (input_bfd))
4831 /* We look forward through the symbol for
4832 the index of the next symbol we are going
4833 to include. I don't know if this is
4835 while (finfo->sym_indices[indx] < 0
4836 && indx < obj_raw_syment_count (input_bfd))
4838 if (indx >= obj_raw_syment_count (input_bfd))
4839 indx = output_index;
4841 indx = finfo->sym_indices[indx];
4842 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4846 indx = aux.x_sym.x_tagndx.l;
4847 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4851 symindx = finfo->sym_indices[indx];
4853 aux.x_sym.x_tagndx.l = 0;
4855 aux.x_sym.x_tagndx.l = symindx;
4859 /* Copy over the line numbers, unless we are stripping
4860 them. We do this on a symbol by symbol basis in
4861 order to more easily handle garbage collection. */
4862 if ((isymp->n_sclass == C_EXT
4863 || isymp->n_sclass == C_HIDEXT)
4865 && isymp->n_numaux > 1
4866 && ISFCN (isymp->n_type)
4867 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4869 if (finfo->info->strip != strip_none
4870 && finfo->info->strip != strip_some)
4871 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4874 asection *enclosing;
4875 unsigned int enc_count;
4876 bfd_size_type linoff;
4877 struct internal_lineno lin;
4880 enclosing = xcoff_section_data (abfd, o)->enclosing;
4881 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4882 if (oline != enclosing)
4884 if (bfd_seek (input_bfd,
4885 enclosing->line_filepos,
4887 || (bfd_read (finfo->linenos, linesz,
4888 enc_count, input_bfd)
4889 != linesz * enc_count))
4894 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4895 - enclosing->line_filepos);
4897 bfd_coff_swap_lineno_in (input_bfd,
4898 (PTR) (finfo->linenos + linoff),
4901 || ((bfd_size_type) lin.l_addr.l_symndx
4905 obj_coff_external_syms (input_bfd)))
4907 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4910 bfd_byte *linpend, *linp;
4912 bfd_size_type count;
4914 lin.l_addr.l_symndx = *indexp;
4915 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4916 (PTR) (finfo->linenos
4919 linpend = (finfo->linenos
4920 + enc_count * linesz);
4921 offset = (o->output_section->vma
4924 for (linp = finfo->linenos + linoff + linesz;
4928 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4930 if (lin.l_lnno == 0)
4932 lin.l_addr.l_paddr += offset;
4933 bfd_coff_swap_lineno_out (output_bfd,
4938 count = (linp - (finfo->linenos + linoff)) / linesz;
4940 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4941 (o->output_section->line_filepos
4942 + o->output_section->lineno_count * linesz);
4944 if (bfd_seek (output_bfd,
4945 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4947 || (bfd_write (finfo->linenos + linoff,
4948 linesz, count, output_bfd)
4952 o->output_section->lineno_count += count;
4956 struct internal_syment *iisp, *iispend;
4961 /* Update any C_BINCL or C_EINCL symbols
4962 that refer to a line number in the
4963 range we just output. */
4964 iisp = finfo->internal_syms;
4966 + obj_raw_syment_count (input_bfd));
4967 iindp = finfo->sym_indices;
4968 oos = finfo->outsyms;
4969 while (iisp < iispend)
4972 && (iisp->n_sclass == C_BINCL
4973 || iisp->n_sclass == C_EINCL)
4974 && ((bfd_size_type) iisp->n_value
4975 >= enclosing->line_filepos + linoff)
4976 && ((bfd_size_type) iisp->n_value
4977 < (enclosing->line_filepos
4978 + enc_count * linesz)))
4980 struct internal_syment iis;
4982 bfd_coff_swap_sym_in (output_bfd,
4987 - enclosing->line_filepos
4989 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4990 bfd_coff_swap_sym_out (output_bfd,
4996 iiadd = 1 + iisp->n_numaux;
4998 oos += iiadd * osymesz;
5007 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5008 isymp->n_sclass, i, isymp->n_numaux,
5020 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5021 symbol will be the first symbol in the next input file. In the
5022 normal case, this will save us from writing out the C_FILE symbol
5024 if (finfo->last_file_index != -1
5025 && (bfd_size_type) finfo->last_file_index >= syment_base)
5027 finfo->last_file.n_value = output_index;
5028 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5029 (PTR) (finfo->outsyms
5030 + ((finfo->last_file_index - syment_base)
5034 /* Write the modified symbols to the output file. */
5035 if (outsym > finfo->outsyms)
5037 if (bfd_seek (output_bfd,
5038 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5040 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5042 != (bfd_size_type) (outsym - finfo->outsyms)))
5045 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5046 + (outsym - finfo->outsyms) / osymesz)
5049 obj_raw_syment_count (output_bfd) = output_index;
5052 /* Don't let the linker relocation routines discard the symbols. */
5053 keep_syms = obj_coff_keep_syms (input_bfd);
5054 obj_coff_keep_syms (input_bfd) = true;
5056 /* Relocate the contents of each section. */
5057 for (o = input_bfd->sections; o != NULL; o = o->next)
5061 if (! o->linker_mark)
5063 /* This section was omitted from the link. */
5067 if ((o->flags & SEC_HAS_CONTENTS) == 0
5068 || o->_raw_size == 0
5069 || (o->flags & SEC_IN_MEMORY) != 0)
5072 /* We have set filepos correctly for the sections we created to
5073 represent csects, so bfd_get_section_contents should work. */
5074 if (coff_section_data (input_bfd, o) != NULL
5075 && coff_section_data (input_bfd, o)->contents != NULL)
5076 contents = coff_section_data (input_bfd, o)->contents;
5079 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5080 (file_ptr) 0, o->_raw_size))
5082 contents = finfo->contents;
5085 if ((o->flags & SEC_RELOC) != 0)
5088 struct internal_reloc *internal_relocs;
5089 struct internal_reloc *irel;
5091 struct internal_reloc *irelend;
5092 struct xcoff_link_hash_entry **rel_hash;
5095 /* Read in the relocs. */
5096 target_index = o->output_section->target_index;
5097 internal_relocs = (xcoff_read_internal_relocs
5098 (input_bfd, o, false, finfo->external_relocs,
5100 (finfo->section_info[target_index].relocs
5101 + o->output_section->reloc_count)));
5102 if (internal_relocs == NULL)
5105 /* Call processor specific code to relocate the section
5107 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5111 finfo->internal_syms,
5112 xcoff_data (input_bfd)->csects))
5115 offset = o->output_section->vma + o->output_offset - o->vma;
5116 irel = internal_relocs;
5117 irelend = irel + o->reloc_count;
5118 rel_hash = (finfo->section_info[target_index].rel_hashes
5119 + o->output_section->reloc_count);
5120 for (; irel < irelend; irel++, rel_hash++)
5122 struct xcoff_link_hash_entry *h = NULL;
5123 struct internal_ldrel ldrel;
5127 /* Adjust the reloc address and symbol index. */
5129 irel->r_vaddr += offset;
5131 r_symndx = irel->r_symndx;
5135 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5137 && h->smclas != XMC_TD
5138 && (irel->r_type == R_TOC
5139 || irel->r_type == R_GL
5140 || irel->r_type == R_TCL
5141 || irel->r_type == R_TRL
5142 || irel->r_type == R_TRLA))
5144 /* This is a TOC relative reloc with a symbol
5145 attached. The symbol should be the one which
5146 this reloc is for. We want to make this
5147 reloc against the TOC address of the symbol,
5148 not the symbol itself. */
5149 BFD_ASSERT (h->toc_section != NULL);
5150 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5151 if (h->u.toc_indx != -1)
5152 irel->r_symndx = h->u.toc_indx;
5155 struct xcoff_toc_rel_hash *n;
5156 struct xcoff_link_section_info *si;
5158 n = ((struct xcoff_toc_rel_hash *)
5159 bfd_alloc (finfo->output_bfd,
5160 sizeof (struct xcoff_toc_rel_hash)));
5163 si = finfo->section_info + target_index;
5164 n->next = si->toc_rel_hashes;
5167 si->toc_rel_hashes = n;
5172 /* This is a global symbol. */
5174 irel->r_symndx = h->indx;
5177 /* This symbol is being written at the end
5178 of the file, and we do not yet know the
5179 symbol index. We save the pointer to the
5180 hash table entry in the rel_hash list.
5181 We set the indx field to -2 to indicate
5182 that this symbol must not be stripped. */
5191 indx = finfo->sym_indices[r_symndx];
5195 struct internal_syment *is;
5197 /* Relocations against a TC0 TOC anchor are
5198 automatically transformed to be against
5199 the TOC anchor in the output file. */
5200 is = finfo->internal_syms + r_symndx;
5201 if (is->n_sclass == C_HIDEXT
5202 && is->n_numaux > 0)
5205 union internal_auxent aux;
5209 obj_coff_external_syms (input_bfd))
5210 + ((r_symndx + is->n_numaux)
5212 bfd_coff_swap_aux_in (input_bfd, auxptr,
5213 is->n_type, is->n_sclass,
5217 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5218 && aux.x_csect.x_smclas == XMC_TC0)
5219 indx = finfo->toc_symindx;
5224 irel->r_symndx = indx;
5227 struct internal_syment *is;
5229 char buf[SYMNMLEN + 1];
5231 /* This reloc is against a symbol we are
5232 stripping. It would be possible to handle
5233 this case, but I don't think it's worth it. */
5234 is = finfo->internal_syms + r_symndx;
5236 name = (_bfd_coff_internal_syment_name
5237 (input_bfd, is, buf));
5241 if (! ((*finfo->info->callbacks->unattached_reloc)
5242 (finfo->info, name, input_bfd, o,
5249 switch (irel->r_type)
5253 || h->root.type == bfd_link_hash_defined
5254 || h->root.type == bfd_link_hash_defweak
5255 || h->root.type == bfd_link_hash_common)
5262 /* This reloc needs to be copied into the .loader
5264 ldrel.l_vaddr = irel->r_vaddr;
5266 ldrel.l_symndx = -1;
5268 || (h->root.type == bfd_link_hash_defined
5269 || h->root.type == bfd_link_hash_defweak
5270 || h->root.type == bfd_link_hash_common))
5275 sec = xcoff_data (input_bfd)->csects[r_symndx];
5276 else if (h->root.type == bfd_link_hash_common)
5277 sec = h->root.u.c.p->section;
5279 sec = h->root.u.def.section;
5280 sec = sec->output_section;
5282 if (strcmp (sec->name, ".text") == 0)
5284 else if (strcmp (sec->name, ".data") == 0)
5286 else if (strcmp (sec->name, ".bss") == 0)
5290 (*_bfd_error_handler)
5291 ("%s: loader reloc in unrecognized section `%s'",
5292 bfd_get_filename (input_bfd),
5294 bfd_set_error (bfd_error_nonrepresentable_section);
5302 (*_bfd_error_handler)
5303 ("%s: `%s' in loader reloc but not loader sym",
5304 bfd_get_filename (input_bfd),
5305 h->root.root.string);
5306 bfd_set_error (bfd_error_bad_value);
5309 ldrel.l_symndx = h->ldindx;
5311 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5312 ldrel.l_rsecnm = o->output_section->target_index;
5313 if (xcoff_hash_table (finfo->info)->textro
5314 && strcmp (o->output_section->name, ".text") == 0)
5316 (*_bfd_error_handler)
5317 ("%s: loader reloc in read-only section %s",
5318 bfd_get_filename (input_bfd),
5319 bfd_get_section_name (finfo->output_bfd,
5320 o->output_section));
5321 bfd_set_error (bfd_error_invalid_operation);
5324 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5326 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5335 /* We should never need a .loader reloc for a TOC
5341 o->output_section->reloc_count += o->reloc_count;
5344 /* Write out the modified section contents. */
5345 if (! bfd_set_section_contents (output_bfd, o->output_section,
5346 contents, o->output_offset,
5347 (o->_cooked_size != 0
5353 obj_coff_keep_syms (input_bfd) = keep_syms;
5355 if (! finfo->info->keep_memory)
5357 if (! _bfd_coff_free_symbols (input_bfd))
5367 /* Write out a non-XCOFF global symbol. */
5370 xcoff_write_global_symbol (h, p)
5371 struct xcoff_link_hash_entry *h;
5374 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5377 struct internal_syment isym;
5378 union internal_auxent aux;
5380 output_bfd = finfo->output_bfd;
5382 /* If this symbol was garbage collected, just skip it. */
5383 if (xcoff_hash_table (finfo->info)->gc
5384 && (h->flags & XCOFF_MARK) == 0)
5387 /* If we need a .loader section entry, write it out. */
5388 if (h->ldsym != NULL)
5390 struct internal_ldsym *ldsym;
5395 if (h->root.type == bfd_link_hash_undefined
5396 || h->root.type == bfd_link_hash_undefweak)
5399 ldsym->l_scnum = N_UNDEF;
5400 ldsym->l_smtype = XTY_ER;
5401 impbfd = h->root.u.undef.abfd;
5403 else if (h->root.type == bfd_link_hash_defined
5404 || h->root.type == bfd_link_hash_defweak)
5408 sec = h->root.u.def.section;
5409 ldsym->l_value = (sec->output_section->vma
5410 + sec->output_offset
5411 + h->root.u.def.value);
5412 ldsym->l_scnum = sec->output_section->target_index;
5413 ldsym->l_smtype = XTY_SD;
5414 impbfd = sec->owner;
5419 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5420 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5421 || (h->flags & XCOFF_IMPORT) != 0)
5422 ldsym->l_smtype |= L_IMPORT;
5423 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5424 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5425 || (h->flags & XCOFF_EXPORT) != 0)
5426 ldsym->l_smtype |= L_EXPORT;
5427 if ((h->flags & XCOFF_ENTRY) != 0)
5428 ldsym->l_smtype |= L_ENTRY;
5430 ldsym->l_smclas = h->smclas;
5432 if (ldsym->l_ifile == (bfd_size_type) -1)
5434 else if (ldsym->l_ifile == 0)
5436 if ((ldsym->l_smtype & L_IMPORT) == 0)
5438 else if (impbfd == NULL)
5442 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5443 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5449 BFD_ASSERT (h->ldindx >= 0);
5450 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5451 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5455 /* If this symbol needs global linkage code, write it out. */
5456 if (h->root.type == bfd_link_hash_defined
5457 && (h->root.u.def.section
5458 == xcoff_hash_table (finfo->info)->linkage_section))
5464 p = h->root.u.def.section->contents + h->root.u.def.value;
5466 /* The first instruction in the global linkage code loads a
5467 specific TOC element. */
5468 tocoff = (h->descriptor->toc_section->output_section->vma
5469 + h->descriptor->toc_section->output_offset
5470 - xcoff_data (output_bfd)->toc);
5471 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5472 tocoff += h->descriptor->u.toc_offset;
5473 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5475 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5477 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5480 /* If we created a TOC entry for this symbol, write out the required
5482 if ((h->flags & XCOFF_SET_TOC) != 0)
5487 struct internal_reloc *irel;
5488 struct internal_ldrel ldrel;
5490 tocsec = h->toc_section;
5491 osec = tocsec->output_section;
5492 oindx = osec->target_index;
5493 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5494 irel->r_vaddr = (osec->vma
5495 + tocsec->output_offset
5498 irel->r_symndx = h->indx;
5502 irel->r_symndx = obj_raw_syment_count (output_bfd);
5504 irel->r_type = R_POS;
5506 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5507 ++osec->reloc_count;
5509 BFD_ASSERT (h->ldindx >= 0);
5510 ldrel.l_vaddr = irel->r_vaddr;
5511 ldrel.l_symndx = h->ldindx;
5512 ldrel.l_rtype = (31 << 8) | R_POS;
5513 ldrel.l_rsecnm = oindx;
5514 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5518 /* If this symbol is a specially defined function descriptor, write
5519 it out. The first word is the address of the function code
5520 itself, the second word is the address of the TOC, and the third
5522 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5523 && h->root.type == bfd_link_hash_defined
5524 && (h->root.u.def.section
5525 == xcoff_hash_table (finfo->info)->descriptor_section))
5531 struct xcoff_link_hash_entry *hentry;
5533 struct internal_reloc *irel;
5534 struct internal_ldrel ldrel;
5537 sec = h->root.u.def.section;
5538 osec = sec->output_section;
5539 oindx = osec->target_index;
5540 p = sec->contents + h->root.u.def.value;
5542 hentry = h->descriptor;
5543 BFD_ASSERT (hentry != NULL
5544 && (hentry->root.type == bfd_link_hash_defined
5545 || hentry->root.type == bfd_link_hash_defweak));
5546 esec = hentry->root.u.def.section;
5547 bfd_put_32 (output_bfd,
5548 (esec->output_section->vma
5549 + esec->output_offset
5550 + hentry->root.u.def.value),
5553 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5554 irel->r_vaddr = (osec->vma
5555 + sec->output_offset
5556 + h->root.u.def.value);
5557 irel->r_symndx = esec->output_section->target_index;
5558 irel->r_type = R_POS;
5560 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5561 ++osec->reloc_count;
5563 ldrel.l_vaddr = irel->r_vaddr;
5564 if (strcmp (esec->output_section->name, ".text") == 0)
5566 else if (strcmp (esec->output_section->name, ".data") == 0)
5568 else if (strcmp (esec->output_section->name, ".bss") == 0)
5572 (*_bfd_error_handler)
5573 ("%s: loader reloc in unrecognized section `%s'",
5574 bfd_get_filename (output_bfd),
5575 esec->output_section->name);
5576 bfd_set_error (bfd_error_nonrepresentable_section);
5579 ldrel.l_rtype = (31 << 8) | R_POS;
5580 ldrel.l_rsecnm = oindx;
5581 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5584 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5586 tsec = coff_section_from_bfd_index (output_bfd,
5587 xcoff_data (output_bfd)->sntoc);
5590 irel->r_vaddr = (osec->vma
5591 + sec->output_offset
5592 + h->root.u.def.value
5594 irel->r_symndx = tsec->output_section->target_index;
5595 irel->r_type = R_POS;
5597 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5598 ++osec->reloc_count;
5600 ldrel.l_vaddr = irel->r_vaddr;
5601 if (strcmp (tsec->output_section->name, ".text") == 0)
5603 else if (strcmp (tsec->output_section->name, ".data") == 0)
5605 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5609 (*_bfd_error_handler)
5610 ("%s: loader reloc in unrecognized section `%s'",
5611 bfd_get_filename (output_bfd),
5612 tsec->output_section->name);
5613 bfd_set_error (bfd_error_nonrepresentable_section);
5616 ldrel.l_rtype = (31 << 8) | R_POS;
5617 ldrel.l_rsecnm = oindx;
5618 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5626 && (finfo->info->strip == strip_all
5627 || (finfo->info->strip == strip_some
5628 && (bfd_hash_lookup (finfo->info->keep_hash,
5629 h->root.root.string, false, false)
5634 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5637 outsym = finfo->outsyms;
5639 memset (&aux, 0, sizeof aux);
5641 h->indx = obj_raw_syment_count (output_bfd);
5643 if (strlen (h->root.root.string) <= SYMNMLEN)
5644 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5651 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5653 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5655 if (indx == (bfd_size_type) -1)
5657 isym._n._n_n._n_zeroes = 0;
5658 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5661 if (h->root.type == bfd_link_hash_undefined
5662 || h->root.type == bfd_link_hash_undefweak)
5665 isym.n_scnum = N_UNDEF;
5666 isym.n_sclass = C_EXT;
5667 aux.x_csect.x_smtyp = XTY_ER;
5669 else if (h->root.type == bfd_link_hash_defined
5670 || h->root.type == bfd_link_hash_defweak)
5672 struct xcoff_link_size_list *l;
5674 isym.n_value = (h->root.u.def.section->output_section->vma
5675 + h->root.u.def.section->output_offset
5676 + h->root.u.def.value);
5677 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5678 isym.n_sclass = C_HIDEXT;
5679 aux.x_csect.x_smtyp = XTY_SD;
5681 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5683 for (l = xcoff_hash_table (finfo->info)->size_list;
5689 aux.x_csect.x_scnlen.l = l->size;
5695 else if (h->root.type == bfd_link_hash_common)
5697 isym.n_value = (h->root.u.c.p->section->output_section->vma
5698 + h->root.u.c.p->section->output_offset);
5699 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5700 isym.n_sclass = C_EXT;
5701 aux.x_csect.x_smtyp = XTY_CM;
5702 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5707 isym.n_type = T_NULL;
5710 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5711 outsym += bfd_coff_symesz (output_bfd);
5713 aux.x_csect.x_smclas = h->smclas;
5715 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5717 outsym += bfd_coff_auxesz (output_bfd);
5719 if (h->root.type == bfd_link_hash_defined
5720 || h->root.type == bfd_link_hash_defweak)
5722 /* We just output an SD symbol. Now output an LD symbol. */
5726 isym.n_sclass = C_EXT;
5727 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5728 outsym += bfd_coff_symesz (output_bfd);
5730 aux.x_csect.x_smtyp = XTY_LD;
5731 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5733 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5735 outsym += bfd_coff_auxesz (output_bfd);
5738 if (bfd_seek (output_bfd,
5739 (obj_sym_filepos (output_bfd)
5740 + (obj_raw_syment_count (output_bfd)
5741 * bfd_coff_symesz (output_bfd))),
5743 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
5744 != (bfd_size_type) (outsym - finfo->outsyms)))
5746 obj_raw_syment_count (output_bfd) +=
5747 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5752 /* Handle a link order which is supposed to generate a reloc. */
5755 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5757 struct xcoff_final_link_info *finfo;
5758 asection *output_section;
5759 struct bfd_link_order *link_order;
5761 reloc_howto_type *howto;
5762 struct xcoff_link_hash_entry *h;
5766 struct internal_reloc *irel;
5767 struct xcoff_link_hash_entry **rel_hash_ptr;
5768 struct internal_ldrel ldrel;
5770 if (link_order->type == bfd_section_reloc_link_order)
5772 /* We need to somehow locate a symbol in the right section. The
5773 symbol must either have a value of zero, or we must adjust
5774 the addend by the value of the symbol. FIXME: Write this
5775 when we need it. The old linker couldn't handle this anyhow. */
5779 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5782 bfd_set_error (bfd_error_bad_value);
5786 h = ((struct xcoff_link_hash_entry *)
5787 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5788 link_order->u.reloc.p->u.name,
5789 false, false, true));
5792 if (! ((*finfo->info->callbacks->unattached_reloc)
5793 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5794 (asection *) NULL, (bfd_vma) 0)))
5799 if (h->root.type == bfd_link_hash_common)
5801 hsec = h->root.u.c.p->section;
5804 else if (h->root.type == bfd_link_hash_defined
5805 || h->root.type == bfd_link_hash_defweak)
5807 hsec = h->root.u.def.section;
5808 hval = h->root.u.def.value;
5816 addend = link_order->u.reloc.p->addend;
5818 addend += (hsec->output_section->vma
5819 + hsec->output_offset
5826 bfd_reloc_status_type rstat;
5829 size = bfd_get_reloc_size (howto);
5830 buf = (bfd_byte *) bfd_zmalloc (size);
5834 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5840 case bfd_reloc_outofrange:
5842 case bfd_reloc_overflow:
5843 if (! ((*finfo->info->callbacks->reloc_overflow)
5844 (finfo->info, link_order->u.reloc.p->u.name,
5845 howto->name, addend, (bfd *) NULL, (asection *) NULL,
5853 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5854 (file_ptr) link_order->offset, size);
5860 /* Store the reloc information in the right place. It will get
5861 swapped and written out at the end of the final_link routine. */
5863 irel = (finfo->section_info[output_section->target_index].relocs
5864 + output_section->reloc_count);
5865 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5866 + output_section->reloc_count);
5868 memset (irel, 0, sizeof (struct internal_reloc));
5869 *rel_hash_ptr = NULL;
5871 irel->r_vaddr = output_section->vma + link_order->offset;
5874 irel->r_symndx = h->indx;
5877 /* Set the index to -2 to force this symbol to get written out. */
5883 irel->r_type = howto->type;
5884 irel->r_size = howto->bitsize - 1;
5885 if (howto->complain_on_overflow == complain_overflow_signed)
5886 irel->r_size |= 0x80;
5888 ++output_section->reloc_count;
5890 /* Now output the reloc to the .loader section. */
5892 ldrel.l_vaddr = irel->r_vaddr;
5896 const char *secname;
5898 secname = hsec->output_section->name;
5900 if (strcmp (secname, ".text") == 0)
5902 else if (strcmp (secname, ".data") == 0)
5904 else if (strcmp (secname, ".bss") == 0)
5908 (*_bfd_error_handler)
5909 ("%s: loader reloc in unrecognized section `%s'",
5910 bfd_get_filename (output_bfd), secname);
5911 bfd_set_error (bfd_error_nonrepresentable_section);
5919 (*_bfd_error_handler)
5920 ("%s: `%s' in loader reloc but not loader sym",
5921 bfd_get_filename (output_bfd),
5922 h->root.root.string);
5923 bfd_set_error (bfd_error_bad_value);
5926 ldrel.l_symndx = h->ldindx;
5929 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5930 ldrel.l_rsecnm = output_section->target_index;
5931 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5937 /* Sort relocs by VMA. This is called via qsort. */
5940 xcoff_sort_relocs (p1, p2)
5944 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5945 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5947 if (r1->r_vaddr > r2->r_vaddr)
5949 else if (r1->r_vaddr < r2->r_vaddr)
5955 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5956 This is currently the only processor which uses XCOFF; I hope that
5957 will never change. */
5960 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
5961 input_section, contents, relocs, syms,
5964 struct bfd_link_info *info;
5966 asection *input_section;
5968 struct internal_reloc *relocs;
5969 struct internal_syment *syms;
5970 asection **sections;
5972 struct internal_reloc *rel;
5973 struct internal_reloc *relend;
5976 relend = rel + input_section->reloc_count;
5977 for (; rel < relend; rel++)
5980 struct xcoff_link_hash_entry *h;
5981 struct internal_syment *sym;
5984 struct reloc_howto_struct howto;
5985 bfd_reloc_status_type rstat;
5987 /* Relocation type R_REF is a special relocation type which is
5988 merely used to prevent garbage collection from occurring for
5989 the csect including the symbol which it references. */
5990 if (rel->r_type == R_REF)
5993 symndx = rel->r_symndx;
6003 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6004 sym = syms + symndx;
6005 addend = - sym->n_value;
6008 /* We build the howto information on the fly. */
6010 howto.type = rel->r_type;
6011 howto.rightshift = 0;
6013 howto.bitsize = (rel->r_size & 0x1f) + 1;
6014 howto.pc_relative = false;
6016 if ((rel->r_size & 0x80) != 0)
6017 howto.complain_on_overflow = complain_overflow_signed;
6019 howto.complain_on_overflow = complain_overflow_bitfield;
6020 howto.special_function = NULL;
6021 howto.name = "internal";
6022 howto.partial_inplace = true;
6023 if (howto.bitsize == 32)
6024 howto.src_mask = howto.dst_mask = 0xffffffff;
6027 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6028 if (howto.bitsize == 16)
6031 howto.pcrel_offset = false;
6041 sec = bfd_abs_section_ptr;
6046 sec = sections[symndx];
6047 /* Hack to make sure we use the right TOC anchor value
6048 if this reloc is against the TOC anchor. */
6049 if (sec->name[3] == '0'
6050 && strcmp (sec->name, ".tc0") == 0)
6051 val = xcoff_data (output_bfd)->toc;
6053 val = (sec->output_section->vma
6054 + sec->output_offset
6061 if (h->root.type == bfd_link_hash_defined
6062 || h->root.type == bfd_link_hash_defweak)
6066 sec = h->root.u.def.section;
6067 val = (h->root.u.def.value
6068 + sec->output_section->vma
6069 + sec->output_offset);
6071 else if (h->root.type == bfd_link_hash_common)
6075 sec = h->root.u.c.p->section;
6076 val = (sec->output_section->vma
6077 + sec->output_offset);
6079 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6080 || (h->flags & XCOFF_IMPORT) != 0)
6082 /* Every symbol in a shared object is defined somewhere. */
6085 else if (! info->relocateable)
6087 if (! ((*info->callbacks->undefined_symbol)
6088 (info, h->root.root.string, input_bfd, input_section,
6089 rel->r_vaddr - input_section->vma)))
6094 /* I took the relocation type definitions from two documents:
6095 the PowerPC AIX Version 4 Application Binary Interface, First
6096 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6097 32-Bit Hardware Implementation (June 30, 1994). Differences
6098 between the documents are noted below. */
6100 switch (rel->r_type)
6105 /* These relocs are defined by the PowerPC ABI to be
6106 relative branches which use half of the difference
6107 between the symbol and the program counter. I can't
6108 quite figure out when this is useful. These relocs are
6109 not defined by the PowerOpen ABI. */
6111 (*_bfd_error_handler)
6112 ("%s: unsupported relocation type 0x%02x",
6113 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6114 bfd_set_error (bfd_error_bad_value);
6117 /* Simple positive relocation. */
6120 /* Simple negative relocation. */
6124 /* Simple PC relative relocation. */
6125 howto.pc_relative = true;
6128 /* TOC relative relocation. The value in the instruction in
6129 the input file is the offset from the input file TOC to
6130 the desired location. We want the offset from the final
6131 TOC to the desired location. We have:
6136 so we must change insn by on - in.
6139 /* Global linkage relocation. The value of this relocation
6140 is the address of the entry in the TOC section. */
6142 /* Local object TOC address. I can't figure out the
6143 difference between this and case R_GL. */
6145 /* TOC relative relocation. A TOC relative load instruction
6146 which may be changed to a load address instruction.
6147 FIXME: We don't currently implement this optimization. */
6149 /* TOC relative relocation. This is a TOC relative load
6150 address instruction which may be changed to a load
6151 instruction. FIXME: I don't know if this is the correct
6153 if (h != NULL && h->smclas != XMC_TD)
6155 if (h->toc_section == NULL)
6157 (*_bfd_error_handler)
6158 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
6159 bfd_get_filename (input_bfd), rel->r_vaddr,
6160 h->root.root.string);
6161 bfd_set_error (bfd_error_bad_value);
6165 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6166 val = (h->toc_section->output_section->vma
6167 + h->toc_section->output_offset);
6170 val = ((val - xcoff_data (output_bfd)->toc)
6171 - (sym->n_value - xcoff_data (input_bfd)->toc));
6175 /* Absolute branch. We don't want to mess with the lower
6176 two bits of the instruction. */
6178 /* The PowerPC ABI defines this as an absolute call which
6179 may be modified to become a relative call. The PowerOpen
6180 ABI does not define this relocation type. */
6182 /* Absolute branch which may be modified to become a
6185 /* The PowerPC ABI defines this as an absolute branch to a
6186 fixed address which may be modified to an absolute branch
6187 to a symbol. The PowerOpen ABI does not define this
6190 /* The PowerPC ABI defines this as an absolute branch to a
6191 fixed address which may be modified to a relative branch.
6192 The PowerOpen ABI does not define this relocation type. */
6193 howto.src_mask &= ~3;
6194 howto.dst_mask = howto.src_mask;
6197 /* Relative branch. We don't want to mess with the lower
6198 two bits of the instruction. */
6200 /* The PowerPC ABI defines this as a relative call which may
6201 be modified to become an absolute call. The PowerOpen
6202 ABI does not define this relocation type. */
6204 /* A relative branch which may be modified to become an
6205 absolute branch. FIXME: We don't implement this,
6206 although we should for symbols of storage mapping class
6208 howto.pc_relative = true;
6209 howto.src_mask &= ~3;
6210 howto.dst_mask = howto.src_mask;
6213 /* The PowerPC AIX ABI describes this as a load which may be
6214 changed to a load address. The PowerOpen ABI says this
6215 is the same as case R_POS. */
6218 /* The PowerPC AIX ABI describes this as a load address
6219 which may be changed to a load. The PowerOpen ABI says
6220 this is the same as R_POS. */
6224 /* If we see an R_BR or R_RBR reloc which is jumping to global
6225 linkage code, and it is followed by an appropriate cror nop
6226 instruction, we replace the cror with lwz r2,20(r1). This
6227 restores the TOC after the glink code. Contrariwise, if the
6228 call is followed by a lwz r2,20(r1), but the call is not
6229 going to global linkage code, we can replace the load with a
6231 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6233 && h->root.type == bfd_link_hash_defined
6234 && (rel->r_vaddr - input_section->vma + 8
6235 <= input_section->_cooked_size))
6240 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6241 next = bfd_get_32 (input_bfd, pnext);
6243 /* The _ptrgl function is magic. It is used by the AIX
6244 compiler to call a function through a pointer. */
6245 if (h->smclas == XMC_GL
6246 || strcmp (h->root.root.string, "._ptrgl") == 0)
6248 if (next == 0x4def7b82 /* cror 15,15,15 */
6249 || next == 0x4ffffb82) /* cror 31,31,31 */
6250 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6254 if (next == 0x80410014) /* lwz r1,20(r1) */
6255 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
6259 /* A PC relative reloc includes the section address. */
6260 if (howto.pc_relative)
6261 addend += input_section->vma;
6263 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6265 rel->r_vaddr - input_section->vma,
6274 case bfd_reloc_overflow:
6277 char buf[SYMNMLEN + 1];
6278 char howto_name[10];
6283 name = h->root.root.string;
6286 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6290 sprintf (howto_name, "0x%02x", rel->r_type);
6292 if (! ((*info->callbacks->reloc_overflow)
6293 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6294 input_section, rel->r_vaddr - input_section->vma)))