1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* 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 symbo lindex of the C_HIDEXT XMC_TC symbol. */
263 /* If this symbol is a function entry point which is called, this
264 field holds a pointer to the function descriptor. */
265 struct xcoff_link_hash_entry *descriptor;
267 /* The .loader symbol table entry, if there is one. */
268 struct internal_ldsym *ldsym;
270 /* The .loader symbol table index. */
273 /* Some linker flags. */
274 unsigned short flags;
275 /* Symbol is referenced by a regular object. */
276 #define XCOFF_REF_REGULAR (01)
277 /* Symbol is defined by a regular object. */
278 #define XCOFF_DEF_REGULAR (02)
279 /* Symbol is defined by a dynamic object. */
280 #define XCOFF_DEF_DYNAMIC (04)
281 /* Symbol is used in a reloc being copied into the .loader section. */
282 #define XCOFF_LDREL (010)
283 /* Symbol is the entry point. */
284 #define XCOFF_ENTRY (020)
285 /* Symbol is called; this is, it appears in a R_BR reloc. */
286 #define XCOFF_CALLED (040)
287 /* Symbol needs the TOC entry filled in. */
288 #define XCOFF_SET_TOC (0100)
289 /* Symbol is explicitly imported. */
290 #define XCOFF_IMPORT (0200)
291 /* Symbol is explicitly exported. */
292 #define XCOFF_EXPORT (0400)
293 /* Symbol has been processed by xcoff_build_ldsyms. */
294 #define XCOFF_BUILT_LDSYM (01000)
295 /* Symbol is mentioned by a section which was not garbage collected. */
296 #define XCOFF_MARK (02000)
297 /* Symbol size is recorded in size_list list from hash table. */
298 #define XCOFF_HAS_SIZE (04000)
300 /* The storage mapping class. */
301 unsigned char smclas;
304 /* The XCOFF linker hash table. */
306 struct xcoff_link_hash_table
308 struct bfd_link_hash_table root;
310 /* The .debug string hash table. We need to compute this while
311 reading the input files, so that we know how large the .debug
312 section will be before we assign section positions. */
313 struct bfd_strtab_hash *debug_strtab;
315 /* The .debug section we will use for the final output. */
316 asection *debug_section;
318 /* The .loader section we will use for the final output. */
319 asection *loader_section;
321 /* A count of non TOC relative relocs which will need to be
322 allocated in the .loader section. */
325 /* The .loader section header. */
326 struct internal_ldhdr ldhdr;
328 /* The .gl section we use to hold global linkage code. */
329 asection *linkage_section;
331 /* The .tc section we use to hold toc entries we build for global
333 asection *toc_section;
335 /* The list of import files. */
336 struct xcoff_import_file *imports;
338 /* Required alignment of sections within the output file. */
339 unsigned long file_align;
341 /* Whether the .text section must be read-only. */
344 /* Whether garbage collection was done. */
347 /* A linked list of symbols for which we have size information. */
348 struct xcoff_link_size_list
350 struct xcoff_link_size_list *next;
351 struct xcoff_link_hash_entry *h;
356 /* Information we keep for each section in the output file during the
359 struct xcoff_link_section_info
361 /* The relocs to be output. */
362 struct internal_reloc *relocs;
363 /* For each reloc against a global symbol whose index was not known
364 when the reloc was handled, the global hash table entry. */
365 struct xcoff_link_hash_entry **rel_hashes;
366 /* If there is a TOC relative reloc against a global symbol, and the
367 index of the TOC symbol is not known when the reloc was handled,
368 an entry is added to this linked list. This is not an array,
369 like rel_hashes, because this case is quite uncommon. */
370 struct xcoff_toc_rel_hash
372 struct xcoff_toc_rel_hash *next;
373 struct xcoff_link_hash_entry *h;
374 struct internal_reloc *rel;
378 /* Information that we pass around while doing the final link step. */
380 struct xcoff_final_link_info
382 /* General link information. */
383 struct bfd_link_info *info;
386 /* Hash table for long symbol names. */
387 struct bfd_strtab_hash *strtab;
388 /* Array of information kept for each output section, indexed by the
389 target_index field. */
390 struct xcoff_link_section_info *section_info;
391 /* Symbol index of last C_FILE symbol (-1 if none). */
392 long last_file_index;
393 /* Contents of last C_FILE symbol. */
394 struct internal_syment last_file;
395 /* Symbol index of TOC symbol. */
397 /* Start of .loader symbols. */
398 struct external_ldsym *ldsym;
399 /* Next .loader reloc to swap out. */
400 struct external_ldrel *ldrel;
401 /* File position of start of line numbers. */
402 file_ptr line_filepos;
403 /* Buffer large enough to hold swapped symbols of any input file. */
404 struct internal_syment *internal_syms;
405 /* Buffer large enough to hold output indices of symbols of any
408 /* Buffer large enough to hold output symbols for any input file. */
410 /* Buffer large enough to hold external line numbers for any input
413 /* Buffer large enough to hold any input section. */
415 /* Buffer large enough to hold external relocs of any input section. */
416 bfd_byte *external_relocs;
419 static void xcoff_swap_ldhdr_in
420 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
421 static void xcoff_swap_ldhdr_out
422 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
423 static void xcoff_swap_ldsym_in
424 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
425 static void xcoff_swap_ldsym_out
426 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
427 static void xcoff_swap_ldrel_out
428 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
429 static struct bfd_hash_entry *xcoff_link_hash_newfunc
430 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
431 static struct internal_reloc *xcoff_read_internal_relocs
432 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
433 struct internal_reloc *));
434 static boolean xcoff_link_add_object_symbols
435 PARAMS ((bfd *, struct bfd_link_info *));
436 static boolean xcoff_link_check_archive_element
437 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
438 static boolean xcoff_link_check_ar_symbols
439 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
440 static bfd_size_type xcoff_find_reloc
441 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
442 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
443 static boolean xcoff_link_add_dynamic_symbols
444 PARAMS ((bfd *, struct bfd_link_info *));
445 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
446 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
447 static boolean xcoff_build_ldsyms
448 PARAMS ((struct xcoff_link_hash_entry *, PTR));
449 static boolean xcoff_link_input_bfd
450 PARAMS ((struct xcoff_final_link_info *, bfd *));
451 static boolean xcoff_write_global_symbol
452 PARAMS ((struct xcoff_link_hash_entry *, PTR));
453 static boolean xcoff_reloc_link_order
454 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
455 struct bfd_link_order *));
456 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
458 /* Routines to swap information in the XCOFF .loader section. If we
459 ever need to write an XCOFF loader, this stuff will need to be
460 moved to another file shared by the linker (which XCOFF calls the
461 ``binder'') and the loader. */
463 /* Swap in the ldhdr structure. */
466 xcoff_swap_ldhdr_in (abfd, src, dst)
468 const struct external_ldhdr *src;
469 struct internal_ldhdr *dst;
471 dst->l_version = bfd_get_32 (abfd, src->l_version);
472 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
473 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
474 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
475 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
476 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
477 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
478 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
481 /* Swap out the ldhdr structure. */
484 xcoff_swap_ldhdr_out (abfd, src, dst)
486 const struct internal_ldhdr *src;
487 struct external_ldhdr *dst;
489 bfd_put_32 (abfd, src->l_version, dst->l_version);
490 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
491 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
492 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
493 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
494 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
495 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
496 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
499 /* Swap in the ldsym structure. */
502 xcoff_swap_ldsym_in (abfd, src, dst)
504 const struct external_ldsym *src;
505 struct internal_ldsym *dst;
507 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
508 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
511 dst->_l._l_l._l_zeroes = 0;
512 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
514 dst->l_value = bfd_get_32 (abfd, src->l_value);
515 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
516 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
517 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
518 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
519 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
522 /* Swap out the ldsym structure. */
525 xcoff_swap_ldsym_out (abfd, src, dst)
527 const struct internal_ldsym *src;
528 struct external_ldsym *dst;
530 if (src->_l._l_l._l_zeroes != 0)
531 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
534 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
535 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
537 bfd_put_32 (abfd, src->l_value, dst->l_value);
538 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
539 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
540 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
541 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
542 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
545 /* As it happens, we never need to swap in the ldrel structure. */
547 /* Swap out the ldrel structure. */
550 xcoff_swap_ldrel_out (abfd, src, dst)
552 const struct internal_ldrel *src;
553 struct external_ldrel *dst;
555 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
556 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
557 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
558 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
561 /* Routine to create an entry in an XCOFF link hash table. */
563 static struct bfd_hash_entry *
564 xcoff_link_hash_newfunc (entry, table, string)
565 struct bfd_hash_entry *entry;
566 struct bfd_hash_table *table;
569 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
571 /* Allocate the structure if it has not already been allocated by a
573 if (ret == (struct xcoff_link_hash_entry *) NULL)
574 ret = ((struct xcoff_link_hash_entry *)
575 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
576 if (ret == (struct xcoff_link_hash_entry *) NULL)
578 bfd_set_error (bfd_error_no_memory);
579 return (struct bfd_hash_entry *) ret;
582 /* Call the allocation method of the superclass. */
583 ret = ((struct xcoff_link_hash_entry *)
584 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
588 /* Set local fields. */
590 ret->toc_section = NULL;
591 ret->u.toc_indx = -1;
592 ret->descriptor = NULL;
596 ret->smclas = XMC_UA;
599 return (struct bfd_hash_entry *) ret;
602 /* Create a XCOFF link hash table. */
604 struct bfd_link_hash_table *
605 _bfd_xcoff_bfd_link_hash_table_create (abfd)
608 struct xcoff_link_hash_table *ret;
610 ret = ((struct xcoff_link_hash_table *)
611 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
612 if (ret == (struct xcoff_link_hash_table *) NULL)
614 bfd_set_error (bfd_error_no_memory);
615 return (struct bfd_link_hash_table *) NULL;
617 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
619 bfd_release (abfd, ret);
620 return (struct bfd_link_hash_table *) NULL;
623 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
624 ret->debug_section = NULL;
625 ret->loader_section = NULL;
626 ret->ldrel_count = 0;
627 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
628 ret->linkage_section = NULL;
629 ret->toc_section = NULL;
635 /* The linker will always generate a full a.out header. We need to
636 record that fact now, before the sizeof_headers routine could be
638 xcoff_data (abfd)->full_aouthdr = true;
643 /* Look up an entry in an XCOFF link hash table. */
645 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
646 ((struct xcoff_link_hash_entry *) \
647 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
650 /* Traverse an XCOFF link hash table. */
652 #define xcoff_link_hash_traverse(table, func, info) \
653 (bfd_link_hash_traverse \
655 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
658 /* Get the XCOFF link hash table from the info structure. This is
661 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
663 /* Read internal relocs for an XCOFF csect. This is a wrapper around
664 _bfd_coff_read_internal_relocs which tries to take advantage of any
665 relocs which may have been cached for the enclosing section. */
667 static struct internal_reloc *
668 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
669 require_internal, internal_relocs)
673 bfd_byte *external_relocs;
674 boolean require_internal;
675 struct internal_reloc *internal_relocs;
677 if (coff_section_data (abfd, sec) != NULL
678 && coff_section_data (abfd, sec)->relocs == NULL
679 && xcoff_section_data (abfd, sec) != NULL)
683 enclosing = xcoff_section_data (abfd, sec)->enclosing;
685 if (enclosing != NULL
686 && (coff_section_data (abfd, enclosing) == NULL
687 || coff_section_data (abfd, enclosing)->relocs == NULL)
689 && enclosing->reloc_count > 0)
691 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
692 external_relocs, false,
693 (struct internal_reloc *) NULL)
698 if (enclosing != NULL
699 && coff_section_data (abfd, enclosing) != NULL
700 && coff_section_data (abfd, enclosing)->relocs != NULL)
704 off = ((sec->rel_filepos - enclosing->rel_filepos)
705 / bfd_coff_relsz (abfd));
706 if (! require_internal)
707 return coff_section_data (abfd, enclosing)->relocs + off;
708 memcpy (internal_relocs,
709 coff_section_data (abfd, enclosing)->relocs + off,
710 sec->reloc_count * sizeof (struct internal_reloc));
711 return internal_relocs;
715 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
716 require_internal, internal_relocs);
719 /* Given an XCOFF BFD, add symbols to the global hash table as
723 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
725 struct bfd_link_info *info;
727 switch (bfd_get_format (abfd))
730 return xcoff_link_add_object_symbols (abfd, info);
732 return (_bfd_generic_link_add_archive_symbols
733 (abfd, info, xcoff_link_check_archive_element));
735 bfd_set_error (bfd_error_wrong_format);
740 /* Add symbols from an XCOFF object file. */
743 xcoff_link_add_object_symbols (abfd, info)
745 struct bfd_link_info *info;
747 if (! _bfd_coff_get_external_symbols (abfd))
749 if (! xcoff_link_add_symbols (abfd, info))
751 if (! info->keep_memory)
753 if (! _bfd_coff_free_symbols (abfd))
759 /* Check a single archive element to see if we need to include it in
760 the link. *PNEEDED is set according to whether this element is
761 needed in the link or not. This is called via
762 _bfd_generic_link_add_archive_symbols. */
765 xcoff_link_check_archive_element (abfd, info, pneeded)
767 struct bfd_link_info *info;
770 if (! _bfd_coff_get_external_symbols (abfd))
773 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
778 if (! xcoff_link_add_symbols (abfd, info))
782 if (! info->keep_memory || ! *pneeded)
784 if (! _bfd_coff_free_symbols (abfd))
791 /* Look through the symbols to see if this object file should be
792 included in the link. */
795 xcoff_link_check_ar_symbols (abfd, info, pneeded)
797 struct bfd_link_info *info;
800 bfd_size_type symesz;
806 symesz = bfd_coff_symesz (abfd);
807 esym = (bfd_byte *) obj_coff_external_syms (abfd);
808 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
809 while (esym < esym_end)
811 struct internal_syment sym;
813 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
815 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
818 char buf[SYMNMLEN + 1];
819 struct bfd_link_hash_entry *h;
821 /* This symbol is externally visible, and is defined by this
824 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
827 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
829 /* We are only interested in symbols that are currently
830 undefined. If a symbol is currently known to be common,
831 XCOFF linkers do not bring in an object file which
832 defines it. We also don't bring in symbols to satisfy
833 undefined references in shared objects. */
834 if (h != (struct bfd_link_hash_entry *) NULL
835 && h->type == bfd_link_hash_undefined)
837 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
844 esym += (sym.n_numaux + 1) * symesz;
847 /* We do not need this object file. */
851 /* Returns the index of reloc in RELOCS with the least address greater
852 than or equal to ADDRESS. The relocs are sorted by address. */
855 xcoff_find_reloc (relocs, count, address)
856 struct internal_reloc *relocs;
860 bfd_size_type min, max, this;
864 if (count == 1 && relocs[0].r_vaddr < address)
873 /* Do a binary search over (min,max]. */
874 while (min + 1 < max)
878 this = (max + min) / 2;
879 raddr = relocs[this].r_vaddr;
882 else if (raddr < address)
891 if (relocs[min].r_vaddr < address)
895 && relocs[min - 1].r_vaddr == address)
901 /* Add all the symbols from an object file to the hash table.
903 XCOFF is a weird format. A normal XCOFF .o files will have three
904 COFF sections--.text, .data, and .bss--but each COFF section will
905 contain many csects. These csects are described in the symbol
906 table. From the linker's point of view, each csect must be
907 considered a section in its own right. For example, a TOC entry is
908 handled as a small XMC_TC csect. The linker must be able to merge
909 different TOC entries together, which means that it must be able to
910 extract the XMC_TC csects from the .data section of the input .o
913 From the point of view of our linker, this is, of course, a hideous
914 nightmare. We cope by actually creating sections for each csect,
915 and discarding the original sections. We then have to handle the
916 relocation entries carefully, since the only way to tell which
917 csect they belong to is to examine the address. */
920 xcoff_link_add_symbols (abfd, info)
922 struct bfd_link_info *info;
924 unsigned int n_tmask;
925 unsigned int n_btshft;
926 boolean default_copy;
927 bfd_size_type symcount;
928 struct xcoff_link_hash_entry **sym_hash;
929 asection **csect_cache;
930 bfd_size_type linesz;
935 unsigned int csect_index;
936 asection *first_csect;
937 bfd_size_type symesz;
940 struct reloc_info_struct
942 struct internal_reloc *relocs;
945 } *reloc_info = NULL;
947 if ((abfd->flags & DYNAMIC) != 0
948 && ! info->static_link)
949 return xcoff_link_add_dynamic_symbols (abfd, info);
951 n_tmask = coff_data (abfd)->local_n_tmask;
952 n_btshft = coff_data (abfd)->local_n_btshft;
954 /* Define macros so that ISFCN, et. al., macros work correctly. */
955 #define N_TMASK n_tmask
956 #define N_BTSHFT n_btshft
958 /* We need to build a .loader section, so we do it here. This won't
959 work if we're producing an XCOFF output file with no non dynamic
960 XCOFF input files. FIXME. */
961 if (xcoff_hash_table (info)->loader_section == NULL)
965 lsec = bfd_make_section_anyway (abfd, ".loader");
968 xcoff_hash_table (info)->loader_section = lsec;
969 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
971 /* Likewise for the linkage section. */
972 if (xcoff_hash_table (info)->linkage_section == NULL)
976 lsec = bfd_make_section_anyway (abfd, ".gl");
979 xcoff_hash_table (info)->linkage_section = lsec;
980 lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
982 /* Likewise for the TOC section. */
983 if (xcoff_hash_table (info)->toc_section == NULL)
987 tsec = bfd_make_section_anyway (abfd, ".tc");
990 xcoff_hash_table (info)->toc_section = tsec;
991 tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
992 tsec->alignment_power = 2;
994 /* Likewise for the .debug section. */
995 if (xcoff_hash_table (info)->debug_section == NULL)
999 dsec = bfd_make_section_anyway (abfd, ".debug");
1002 xcoff_hash_table (info)->debug_section = dsec;
1003 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1006 if (info->keep_memory)
1007 default_copy = false;
1009 default_copy = true;
1011 symcount = obj_raw_syment_count (abfd);
1013 /* We keep a list of the linker hash table entries that correspond
1014 to each external symbol. */
1015 sym_hash = ((struct xcoff_link_hash_entry **)
1018 * sizeof (struct xcoff_link_hash_entry *))));
1019 if (sym_hash == NULL && symcount != 0)
1021 bfd_set_error (bfd_error_no_memory);
1024 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1025 memset (sym_hash, 0,
1026 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1028 /* Because of the weird stuff we are doing with XCOFF csects, we can
1029 not easily determine which section a symbol is in, so we store
1030 the information in the tdata for the input file. */
1031 csect_cache = ((asection **)
1032 bfd_alloc (abfd, symcount * sizeof (asection *)));
1033 if (csect_cache == NULL && symcount != 0)
1035 bfd_set_error (bfd_error_no_memory);
1038 xcoff_data (abfd)->csects = csect_cache;
1039 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1041 /* While splitting sections into csects, we need to assign the
1042 relocs correctly. The relocs and the csects must both be in
1043 order by VMA within a given section, so we handle this by
1044 scanning along the relocs as we process the csects. We index
1045 into reloc_info using the section target_index. */
1046 reloc_info = ((struct reloc_info_struct *)
1047 malloc ((abfd->section_count + 1)
1048 * sizeof (struct reloc_info_struct)));
1049 if (reloc_info == NULL)
1051 bfd_set_error (bfd_error_no_memory);
1054 memset ((PTR) reloc_info, 0,
1055 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1057 /* Read in the relocs and line numbers for each section. */
1058 linesz = bfd_coff_linesz (abfd);
1060 for (o = abfd->sections; o != NULL; o = o->next)
1063 if ((o->flags & SEC_RELOC) != 0)
1065 reloc_info[o->target_index].relocs =
1066 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1067 false, (struct internal_reloc *) NULL);
1068 reloc_info[o->target_index].csects =
1069 (asection **) malloc (o->reloc_count * sizeof (asection *));
1070 if (reloc_info[o->target_index].csects == NULL)
1072 bfd_set_error (bfd_error_no_memory);
1075 memset (reloc_info[o->target_index].csects, 0,
1076 o->reloc_count * sizeof (asection *));
1079 if ((info->strip == strip_none || info->strip == strip_some)
1080 && o->lineno_count > 0)
1084 linenos = (bfd_byte *) malloc (o->lineno_count * linesz);
1085 if (linenos == NULL)
1087 bfd_set_error (bfd_error_no_memory);
1090 reloc_info[o->target_index].linenos = linenos;
1091 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1092 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1093 != linesz * o->lineno_count))
1098 /* Don't let the linker relocation routines discard the symbols. */
1099 keep_syms = obj_coff_keep_syms (abfd);
1100 obj_coff_keep_syms (abfd) = true;
1106 symesz = bfd_coff_symesz (abfd);
1107 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1108 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1109 esym_end = esym + symcount * symesz;
1110 while (esym < esym_end)
1112 struct internal_syment sym;
1113 union internal_auxent aux;
1115 char buf[SYMNMLEN + 1];
1120 struct xcoff_link_hash_entry *set_toc;
1122 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1124 /* In this pass we are only interested in symbols with csect
1126 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1128 if (sym.n_sclass == C_FILE && csect != NULL)
1130 xcoff_section_data (abfd, csect)->last_symndx =
1132 - (bfd_byte *) obj_coff_external_syms (abfd))
1138 *csect_cache = csect;
1139 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1140 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1142 *csect_cache = NULL;
1143 esym += (sym.n_numaux + 1) * symesz;
1144 sym_hash += sym.n_numaux + 1;
1145 csect_cache += sym.n_numaux + 1;
1149 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1153 /* If this symbol has line number information attached to it,
1154 and we're not stripping it, count the number of entries and
1155 add them to the count for this csect. In the final link pass
1156 we are going to attach line number information by symbol,
1157 rather than by section, in order to more easily handle
1158 garbage collection. */
1159 if ((info->strip == strip_none || info->strip == strip_some)
1162 && ISFCN (sym.n_type))
1164 union internal_auxent auxlin;
1166 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1167 sym.n_type, sym.n_sclass,
1168 0, sym.n_numaux, (PTR) &auxlin);
1169 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1171 asection *enclosing;
1172 bfd_size_type linoff;
1174 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1175 if (enclosing == NULL)
1177 (*_bfd_error_handler)
1178 ("%s: `%s' has line numbers but no enclosing section",
1179 bfd_get_filename (abfd), name);
1180 bfd_set_error (bfd_error_bad_value);
1183 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1184 - enclosing->line_filepos);
1185 if (linoff < enclosing->lineno_count * linesz)
1187 struct internal_lineno lin;
1188 bfd_byte *linpstart;
1190 linpstart = (reloc_info[enclosing->target_index].linenos
1192 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1194 && ((bfd_size_type) lin.l_addr.l_symndx
1196 - (bfd_byte *) obj_coff_external_syms (abfd))
1199 bfd_byte *linpend, *linp;
1201 linpend = (reloc_info[enclosing->target_index].linenos
1202 + enclosing->lineno_count * linesz);
1203 for (linp = linpstart + linesz;
1207 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1209 if (lin.l_lnno == 0)
1212 csect->lineno_count += (linp - linpstart) / linesz;
1213 /* The setting of line_filepos will only be
1214 useful if all the line number entries for a
1215 csect are contiguous; this only matters for
1217 if (csect->line_filepos == 0)
1218 csect->line_filepos =
1219 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1225 /* Pick up the csect auxiliary information. */
1227 if (sym.n_numaux == 0)
1229 (*_bfd_error_handler)
1230 ("%s: class %d symbol `%s' has no aux entries",
1231 bfd_get_filename (abfd), sym.n_sclass, name);
1232 bfd_set_error (bfd_error_bad_value);
1236 bfd_coff_swap_aux_in (abfd,
1237 (PTR) (esym + symesz * sym.n_numaux),
1238 sym.n_type, sym.n_sclass,
1239 sym.n_numaux - 1, sym.n_numaux,
1242 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1252 (*_bfd_error_handler)
1253 ("%s: symbol `%s' has unrecognized csect type %d",
1254 bfd_get_filename (abfd), name, smtyp);
1255 bfd_set_error (bfd_error_bad_value);
1259 /* This is an external reference. */
1260 if (sym.n_sclass == C_HIDEXT
1261 || sym.n_scnum != N_UNDEF
1262 || aux.x_csect.x_scnlen.l != 0)
1264 (*_bfd_error_handler)
1265 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1266 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1267 aux.x_csect.x_scnlen.l);
1268 bfd_set_error (bfd_error_bad_value);
1272 /* An XMC_XO external reference is actually a reference to
1273 an absolute location. */
1274 if (aux.x_csect.x_smclas != XMC_XO)
1275 section = bfd_und_section_ptr;
1278 section = bfd_abs_section_ptr;
1279 value = sym.n_value;
1284 /* This is a csect definition. */
1288 xcoff_section_data (abfd, csect)->last_symndx =
1290 - (bfd_byte *) obj_coff_external_syms (abfd))
1297 /* When we see a TOC anchor, we record the TOC value. */
1298 if (aux.x_csect.x_smclas == XMC_TC0)
1300 if (sym.n_sclass != C_HIDEXT
1301 || aux.x_csect.x_scnlen.l != 0)
1303 (*_bfd_error_handler)
1304 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1305 bfd_get_filename (abfd), name, sym.n_sclass,
1306 aux.x_csect.x_scnlen.l);
1307 bfd_set_error (bfd_error_bad_value);
1310 xcoff_data (abfd)->toc = sym.n_value;
1313 /* We must merge TOC entries for the same symbol. We can
1314 merge two TOC entries if they are both C_HIDEXT, they
1315 both have the same name, they are both 4 bytes long, and
1316 they both have a relocation table entry for an external
1317 symbol with the same name. Unfortunately, this means
1318 that we must look through the relocations. Ick. */
1319 if (aux.x_csect.x_smclas == XMC_TC
1320 && sym.n_sclass == C_HIDEXT
1321 && aux.x_csect.x_scnlen.l == 4
1322 && info->hash->creator == abfd->xvec)
1324 asection *enclosing;
1325 struct internal_reloc *relocs;
1326 bfd_size_type relindx;
1327 struct internal_reloc *rel;
1329 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1330 if (enclosing == NULL)
1333 relocs = reloc_info[enclosing->target_index].relocs;
1334 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1336 rel = relocs + relindx;
1337 if (relindx < enclosing->reloc_count
1338 && rel->r_vaddr == (bfd_vma) sym.n_value
1339 && rel->r_size == 31
1340 && rel->r_type == R_POS)
1343 struct internal_syment relsym;
1345 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1346 + rel->r_symndx * symesz);
1347 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1348 if (relsym.n_sclass == C_EXT)
1350 const char *relname;
1351 char relbuf[SYMNMLEN + 1];
1353 struct xcoff_link_hash_entry *h;
1355 /* At this point we know that the TOC entry is
1356 for an externally visible symbol. */
1357 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1359 if (relname == NULL)
1362 /* We only merge TOC entries if the TC name is
1363 the same as the symbol name. This handles
1364 the normal case, but not common cases like
1365 SYM.P4 which gcc generates to store SYM + 4
1366 in the TOC. FIXME. */
1367 if (strcmp (name, relname) == 0)
1369 copy = (! info->keep_memory
1370 || relsym._n._n_n._n_zeroes != 0
1371 || relsym._n._n_n._n_offset == 0);
1372 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1373 relname, true, copy,
1378 /* At this point h->root.type could be
1379 bfd_link_hash_new. That should be OK,
1380 since we know for sure that we will come
1381 across this symbol as we step through the
1384 /* We store h in *sym_hash for the
1385 convenience of the relocate_section
1389 if (h->toc_section != NULL)
1391 asection **rel_csects;
1393 /* We already have a TOC entry for this
1394 symbol, so we can just ignore this
1397 reloc_info[enclosing->target_index].csects;
1398 rel_csects[relindx] = bfd_und_section_ptr;
1402 /* We are about to create a TOC entry for
1410 /* We need to create a new section. We get the name from
1411 the csect storage mapping class, so that the linker can
1412 accumulate similar csects together. */
1414 static const char *csect_name_by_class[] =
1416 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1417 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1420 const char *csect_name;
1421 asection *enclosing;
1423 if ((aux.x_csect.x_smclas >=
1424 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1425 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1427 (*_bfd_error_handler)
1428 ("%s: symbol `%s' has unrecognized smclas %d",
1429 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1430 bfd_set_error (bfd_error_bad_value);
1434 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1435 csect = bfd_make_section_anyway (abfd, csect_name);
1438 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1439 if (enclosing == NULL)
1441 if (! bfd_is_abs_section (enclosing)
1442 && ((bfd_vma) sym.n_value < enclosing->vma
1443 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1444 > enclosing->vma + enclosing->_raw_size)))
1446 (*_bfd_error_handler)
1447 ("%s: csect `%s' not in enclosing section",
1448 bfd_get_filename (abfd), name);
1449 bfd_set_error (bfd_error_bad_value);
1452 csect->vma = sym.n_value;
1453 csect->filepos = (enclosing->filepos
1456 csect->_raw_size = aux.x_csect.x_scnlen.l;
1457 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1458 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1460 /* Record the enclosing section in the tdata for this new
1462 csect->used_by_bfd =
1463 ((struct coff_section_tdata *)
1464 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1465 if (csect->used_by_bfd == NULL)
1467 bfd_set_error (bfd_error_no_memory);
1470 coff_section_data (abfd, csect)->tdata =
1471 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1472 if (coff_section_data (abfd, csect)->tdata == NULL)
1474 bfd_set_error (bfd_error_no_memory);
1477 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1478 xcoff_section_data (abfd, csect)->lineno_count =
1479 enclosing->lineno_count;
1481 if (enclosing->owner == abfd)
1483 struct internal_reloc *relocs;
1484 bfd_size_type relindx;
1485 struct internal_reloc *rel;
1486 asection **rel_csect;
1488 relocs = reloc_info[enclosing->target_index].relocs;
1489 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1491 rel = relocs + relindx;
1492 rel_csect = (reloc_info[enclosing->target_index].csects
1494 csect->rel_filepos = (enclosing->rel_filepos
1495 + relindx * bfd_coff_relsz (abfd));
1496 while (relindx < enclosing->reloc_count
1497 && *rel_csect == NULL
1498 && rel->r_vaddr < csect->vma + csect->_raw_size)
1501 csect->flags |= SEC_RELOC;
1502 ++csect->reloc_count;
1509 /* There are a number of other fields and section flags
1510 which we do not bother to set. */
1512 csect_index = ((esym
1513 - (bfd_byte *) obj_coff_external_syms (abfd))
1516 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1518 if (first_csect == NULL)
1519 first_csect = csect;
1521 /* If this symbol is C_EXT, we treat it as starting at the
1522 beginning of the newly created section. */
1523 if (sym.n_sclass == C_EXT)
1529 /* If this is a TOC section for a symbol, record it. */
1530 if (set_toc != NULL)
1531 set_toc->toc_section = csect;
1536 /* This is a label definition. The x_scnlen field is the
1537 symbol index of the csect. I believe that this must
1538 always follow the appropriate XTY_SD symbol, so I will
1544 if (aux.x_csect.x_scnlen.l < 0
1545 || (aux.x_csect.x_scnlen.l
1546 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1550 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1552 || (section->flags & SEC_HAS_CONTENTS) == 0)
1557 (*_bfd_error_handler)
1558 ("%s: misplaced XTY_LD `%s'",
1559 bfd_get_filename (abfd), name);
1560 bfd_set_error (bfd_error_bad_value);
1564 value = sym.n_value - csect->vma;
1569 /* This is an unitialized csect. We could base the name on
1570 the storage mapping class, but we don't bother. If this
1571 csect is externally visible, it is a common symbol. */
1575 xcoff_section_data (abfd, csect)->last_symndx =
1577 - (bfd_byte *) obj_coff_external_syms (abfd))
1581 csect = bfd_make_section_anyway (abfd, ".bss");
1584 csect->vma = sym.n_value;
1585 csect->_raw_size = aux.x_csect.x_scnlen.l;
1586 csect->flags |= SEC_ALLOC;
1587 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1588 /* There are a number of other fields and section flags
1589 which we do not bother to set. */
1591 csect_index = ((esym
1592 - (bfd_byte *) obj_coff_external_syms (abfd))
1595 csect->used_by_bfd =
1596 ((struct coff_section_tdata *)
1597 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1598 if (csect->used_by_bfd == NULL)
1600 bfd_set_error (bfd_error_no_memory);
1603 coff_section_data (abfd, csect)->tdata =
1604 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1605 if (coff_section_data (abfd, csect)->tdata == NULL)
1607 bfd_set_error (bfd_error_no_memory);
1610 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1612 if (first_csect == NULL)
1613 first_csect = csect;
1615 if (sym.n_sclass == C_EXT)
1617 csect->flags |= SEC_IS_COMMON;
1618 csect->_raw_size = 0;
1620 value = aux.x_csect.x_scnlen.l;
1626 /* Now we have enough information to add the symbol to the
1627 linker hash table. */
1629 if (sym.n_sclass == C_EXT)
1633 BFD_ASSERT (section != NULL);
1635 /* We must copy the name into memory if we got it from the
1636 syment itself, rather than the string table. */
1637 copy = default_copy;
1638 if (sym._n._n_n._n_zeroes != 0
1639 || sym._n._n_n._n_offset == 0)
1642 if (info->hash->creator == abfd->xvec)
1644 /* If we are statically linking a shared object, it is
1645 OK for symbol redefinitions to occur. I can't figure
1646 out just what the XCOFF linker is doing, but
1647 something like this is required for -bnso to work. */
1648 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1649 name, true, copy, false);
1650 if (*sym_hash == NULL)
1652 if (((*sym_hash)->root.type == bfd_link_hash_defined
1653 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1654 && ! bfd_is_und_section (section)
1655 && ! bfd_is_com_section (section))
1657 if ((abfd->flags & DYNAMIC) != 0)
1659 section = bfd_und_section_ptr;
1662 else if (((*sym_hash)->root.u.def.section->owner->flags
1665 (*sym_hash)->root.type = bfd_link_hash_undefined;
1666 (*sym_hash)->root.u.undef.abfd =
1667 (*sym_hash)->root.u.def.section->owner;
1672 /* _bfd_generic_link_add_one_symbol may call the linker to
1673 generate an error message, and the linker may try to read
1674 the symbol table to give a good error. Right now, the
1675 line numbers are in an inconsistent state, since they are
1676 counted both in the real sections and in the new csects.
1677 We need to leave the count in the real sections so that
1678 the linker can report the line number of the error
1679 correctly, so temporarily clobber the link to the csects
1680 so that the linker will not try to read the line numbers
1681 a second time from the csects. */
1682 BFD_ASSERT (last_real->next == first_csect);
1683 last_real->next = NULL;
1684 if (! (_bfd_generic_link_add_one_symbol
1685 (info, abfd, name, flags, section, value,
1686 (const char *) NULL, copy, true,
1687 (struct bfd_link_hash_entry **) sym_hash)))
1689 last_real->next = first_csect;
1691 if (smtyp == XTY_CM)
1693 if ((*sym_hash)->root.type != bfd_link_hash_common
1694 || (*sym_hash)->root.u.c.p->section != csect)
1696 /* We don't need the common csect we just created. */
1697 csect->_raw_size = 0;
1701 (*sym_hash)->root.u.c.p->alignment_power
1702 = csect->alignment_power;
1706 if (info->hash->creator == abfd->xvec)
1710 if (smtyp == XTY_ER || smtyp == XTY_CM)
1711 flag = XCOFF_REF_REGULAR;
1713 flag = XCOFF_DEF_REGULAR;
1714 (*sym_hash)->flags |= flag;
1716 if ((*sym_hash)->smclas == XMC_UA
1717 || flag == XCOFF_DEF_REGULAR)
1718 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1722 *csect_cache = csect;
1724 esym += (sym.n_numaux + 1) * symesz;
1725 sym_hash += sym.n_numaux + 1;
1726 csect_cache += sym.n_numaux + 1;
1729 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1731 /* Make sure that we have seen all the relocs. */
1732 for (o = abfd->sections; o != first_csect; o = o->next)
1734 /* Reset the section size and the line numebr count, since the
1735 data is now attached to the csects. Don't reset the size of
1736 the .debug section, since we need to read it below in
1737 bfd_xcoff_size_dynamic_sections. */
1738 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1740 o->lineno_count = 0;
1742 if ((o->flags & SEC_RELOC) != 0)
1745 struct internal_reloc *rel;
1746 asection **rel_csect;
1748 rel = reloc_info[o->target_index].relocs;
1749 rel_csect = reloc_info[o->target_index].csects;
1750 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1752 if (*rel_csect == NULL)
1754 (*_bfd_error_handler)
1755 ("%s: reloc %s:%d not in csect",
1756 bfd_get_filename (abfd), o->name, i);
1757 bfd_set_error (bfd_error_bad_value);
1761 /* We identify all symbols which are called, so that we
1762 can create glue code for calls to functions imported
1763 from dynamic objects. */
1764 if (info->hash->creator == abfd->xvec
1765 && *rel_csect != bfd_und_section_ptr
1766 && (rel->r_type == R_BR
1767 || rel->r_type == R_RBR)
1768 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1770 struct xcoff_link_hash_entry *h;
1772 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1773 h->flags |= XCOFF_CALLED;
1774 /* If the symbol name starts with a period, it is
1775 the code of a function. If the symbol is
1776 currently undefined, then add an undefined symbol
1777 for the function descriptor. This should do no
1778 harm, because any regular object that defines the
1779 function should also define the function
1780 descriptor. It helps, because it means that we
1781 will identify the function descriptor with a
1782 dynamic object if a dynamic object defines it. */
1783 if (h->root.root.string[0] == '.'
1784 && h->descriptor == NULL)
1786 struct xcoff_link_hash_entry *hds;
1788 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1789 h->root.root.string + 1,
1793 if (hds->root.type == bfd_link_hash_new)
1795 if (! (_bfd_generic_link_add_one_symbol
1796 (info, abfd, hds->root.root.string,
1797 (flagword) 0, bfd_und_section_ptr,
1798 (bfd_vma) 0, (const char *) NULL, false,
1800 (struct bfd_link_hash_entry **) &hds)))
1803 h->descriptor = hds;
1808 free (reloc_info[o->target_index].csects);
1809 reloc_info[o->target_index].csects = NULL;
1811 /* Reset SEC_RELOC and the reloc_count, since the reloc
1812 information is now attached to the csects. */
1813 o->flags &=~ SEC_RELOC;
1816 /* If we are not keeping memory, free the reloc information. */
1817 if (! info->keep_memory
1818 && coff_section_data (abfd, o) != NULL
1819 && coff_section_data (abfd, o)->relocs != NULL
1820 && ! coff_section_data (abfd, o)->keep_relocs)
1822 free (coff_section_data (abfd, o)->relocs);
1823 coff_section_data (abfd, o)->relocs = NULL;
1827 /* Free up the line numbers. FIXME: We could cache these
1828 somewhere for the final link, to avoid reading them again. */
1829 if (reloc_info[o->target_index].linenos != NULL)
1831 free (reloc_info[o->target_index].linenos);
1832 reloc_info[o->target_index].linenos = NULL;
1838 obj_coff_keep_syms (abfd) = keep_syms;
1843 if (reloc_info != NULL)
1845 for (o = abfd->sections; o != NULL; o = o->next)
1847 if (reloc_info[o->target_index].csects != NULL)
1848 free (reloc_info[o->target_index].csects);
1849 if (reloc_info[o->target_index].linenos != NULL)
1850 free (reloc_info[o->target_index].linenos);
1854 obj_coff_keep_syms (abfd) = keep_syms;
1861 /* This function is used to add symbols from a dynamic object to the
1862 global symbol table. */
1865 xcoff_link_add_dynamic_symbols (abfd, info)
1867 struct bfd_link_info *info;
1870 bfd_byte *buf = NULL;
1871 struct internal_ldhdr ldhdr;
1872 const char *strings;
1873 struct external_ldsym *elsym, *elsymend;
1874 struct xcoff_import_file *n;
1879 struct xcoff_import_file **pp;
1881 /* We can only handle a dynamic object if we are generating an XCOFF
1883 if (info->hash->creator != abfd->xvec)
1885 (*_bfd_error_handler)
1886 ("%s: XCOFF shared object when not producing XCOFF output",
1887 bfd_get_filename (abfd));
1888 bfd_set_error (bfd_error_invalid_operation);
1892 /* The symbols we use from a dynamic object are not the symbols in
1893 the normal symbol table, but, rather, the symbols in the export
1894 table. If there is a global symbol in a dynamic object which is
1895 not in the export table, the loader will not be able to find it,
1896 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
1897 libc.a has symbols in the export table which are not in the
1900 /* Read in the .loader section. FIXME: We should really use the
1901 o_snloader field in the a.out header, rather than grabbing the
1903 lsec = bfd_get_section_by_name (abfd, ".loader");
1906 (*_bfd_error_handler)
1907 ("%s: dynamic object with no .loader section",
1908 bfd_get_filename (abfd));
1909 bfd_set_error (bfd_error_no_symbols);
1913 buf = (bfd_byte *) malloc (lsec->_raw_size);
1914 if (buf == NULL && lsec->_raw_size > 0)
1916 bfd_set_error (bfd_error_no_memory);
1920 if (! bfd_get_section_contents (abfd, lsec, (PTR) buf, (file_ptr) 0,
1924 /* Remove the sections from this object, so that they do not get
1925 included in the link. */
1926 abfd->sections = NULL;
1928 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1930 strings = (char *) buf + ldhdr.l_stoff;
1932 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1933 elsymend = elsym + ldhdr.l_nsyms;
1934 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
1935 for (; elsym < elsymend; elsym++)
1937 struct internal_ldsym ldsym;
1938 char nambuf[SYMNMLEN + 1];
1940 struct xcoff_link_hash_entry *h;
1942 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1944 /* We are only interested in exported symbols. */
1945 if ((ldsym.l_smtype & L_EXPORT) == 0)
1948 if (ldsym._l._l_l._l_zeroes == 0)
1949 name = strings + ldsym._l._l_l._l_offset;
1952 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1953 nambuf[SYMNMLEN] = '\0';
1957 /* Normally we could not xcoff_link_hash_lookup in an add
1958 symbols routine, since we might not be using an XCOFF hash
1959 table. However, we verified above that we are using an XCOFF
1962 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
1967 h->flags |= XCOFF_DEF_DYNAMIC;
1969 /* If the symbol is undefined, and the BFD it was found in is
1970 not a dynamic object, change the BFD to this dynamic object,
1971 so that we can get the correct import file ID. */
1972 if ((h->root.type == bfd_link_hash_undefined
1973 || h->root.type == bfd_link_hash_undefweak)
1974 && (h->root.u.undef.abfd == NULL
1975 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
1976 h->root.u.undef.abfd = abfd;
1978 if (h->root.type == bfd_link_hash_new)
1980 h->root.type = bfd_link_hash_undefined;
1981 h->root.u.undef.abfd = abfd;
1982 /* We do not want to add this to the undefined symbol list. */
1985 if (h->smclas == XMC_UA
1986 || h->root.type == bfd_link_hash_undefined
1987 || h->root.type == bfd_link_hash_undefweak)
1988 h->smclas = ldsym.l_smclas;
1990 /* Unless this is an XMC_XO symbol, we don't bother to actually
1991 define it, since we don't have a section to put it in anyhow.
1992 Instead, the relocation routines handle the DEF_DYNAMIC flag
1995 if (h->smclas == XMC_XO
1996 && (h->root.type == bfd_link_hash_undefined
1997 || h->root.type == bfd_link_hash_undefweak))
1999 /* This symbol has an absolute value. */
2000 h->root.type = bfd_link_hash_defined;
2001 h->root.u.def.section = bfd_abs_section_ptr;
2002 h->root.u.def.value = ldsym.l_value;
2012 /* Record this file in the import files. */
2014 n = ((struct xcoff_import_file *)
2015 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2018 bfd_set_error (bfd_error_no_memory);
2023 /* For some reason, the path entry in the import file list for a
2024 shared object appears to always be empty. The file name is the
2027 if (abfd->my_archive == NULL)
2029 bname = bfd_get_filename (abfd);
2034 bname = bfd_get_filename (abfd->my_archive);
2035 mname = bfd_get_filename (abfd);
2037 s = strrchr (bname, '/');
2043 /* We start c at 1 because the first import file number is reserved
2045 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2047 pp = &(*pp)->next, ++c)
2051 xcoff_data (abfd)->import_file_id = c;
2061 /* Routines that are called after all the input files have been
2062 handled, but before the sections are laid out in memory. */
2064 /* Mark a symbol as not being garbage, including the section in which
2067 static INLINE boolean
2068 xcoff_mark_symbol (info, h)
2069 struct bfd_link_info *info;
2070 struct xcoff_link_hash_entry *h;
2072 if ((h->flags & XCOFF_MARK) != 0)
2075 h->flags |= XCOFF_MARK;
2076 if (h->root.type == bfd_link_hash_defined
2077 || h->root.type == bfd_link_hash_defweak)
2081 hsec = h->root.u.def.section;
2082 if ((hsec->flags & SEC_MARK) == 0)
2084 if (! xcoff_mark (info, hsec))
2089 if (h->toc_section != NULL
2090 && (h->toc_section->flags & SEC_MARK) == 0)
2092 if (! xcoff_mark (info, h->toc_section))
2099 /* The mark phase of garbage collection. For a given section, mark
2100 it, and all the sections which define symbols to which it refers.
2101 Because this function needs to look at the relocs, we also count
2102 the number of relocs which need to be copied into the .loader
2106 xcoff_mark (info, sec)
2107 struct bfd_link_info *info;
2110 if ((sec->flags & SEC_MARK) != 0)
2113 sec->flags |= SEC_MARK;
2115 if (sec->owner->xvec == info->hash->creator
2116 && coff_section_data (sec->owner, sec) != NULL
2117 && xcoff_section_data (sec->owner, sec) != NULL)
2119 register struct xcoff_link_hash_entry **hp, **hpend;
2120 struct internal_reloc *rel, *relend;
2122 /* Mark all the symbols in this section. */
2124 hp = (obj_xcoff_sym_hashes (sec->owner)
2125 + xcoff_section_data (sec->owner, sec)->first_symndx);
2126 hpend = (obj_xcoff_sym_hashes (sec->owner)
2127 + xcoff_section_data (sec->owner, sec)->last_symndx);
2128 for (; hp < hpend; hp++)
2130 register struct xcoff_link_hash_entry *h;
2134 && (h->flags & XCOFF_MARK) == 0)
2136 if (! xcoff_mark_symbol (info, h))
2141 /* Look through the section relocs. */
2143 if ((sec->flags & SEC_RELOC) != 0
2144 && sec->reloc_count > 0)
2146 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2147 (bfd_byte *) NULL, false,
2148 (struct internal_reloc *) NULL);
2151 relend = rel + sec->reloc_count;
2152 for (; rel < relend; rel++)
2155 struct xcoff_link_hash_entry *h;
2157 if ((unsigned int) rel->r_symndx
2158 > obj_raw_syment_count (sec->owner))
2161 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2163 && (h->flags & XCOFF_MARK) == 0)
2165 if (! xcoff_mark_symbol (info, h))
2169 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2171 && (rsec->flags & SEC_MARK) == 0)
2173 if (! xcoff_mark (info, rsec))
2177 /* See if this reloc needs to be copied into the .loader
2179 switch (rel->r_type)
2183 || h->root.type == bfd_link_hash_defined
2184 || h->root.type == bfd_link_hash_defweak
2185 || h->root.type == bfd_link_hash_common
2186 || ((h->flags & XCOFF_CALLED) != 0
2187 && (h->root.type == bfd_link_hash_undefined
2188 || h->root.type == bfd_link_hash_undefweak)
2189 && h->root.root.string[0] == '.'
2190 && h->descriptor != NULL
2191 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2199 ++xcoff_hash_table (info)->ldrel_count;
2201 h->flags |= XCOFF_LDREL;
2208 /* We should never need a .loader reloc for a TOC
2214 if (! info->keep_memory
2215 && coff_section_data (sec->owner, sec) != NULL
2216 && coff_section_data (sec->owner, sec)->relocs != NULL
2217 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2219 free (coff_section_data (sec->owner, sec)->relocs);
2220 coff_section_data (sec->owner, sec)->relocs = NULL;
2228 /* The sweep phase of garbage collection. Remove all garbage
2233 struct bfd_link_info *info;
2237 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2241 for (o = sub->sections; o != NULL; o = o->next)
2243 if ((o->flags & SEC_MARK) == 0)
2245 /* Keep all sections from non-XCOFF input files. Keep
2246 special sections. Keep .debug sections for the
2248 if (sub->xvec != info->hash->creator
2249 || o == xcoff_hash_table (info)->debug_section
2250 || o == xcoff_hash_table (info)->loader_section
2251 || o == xcoff_hash_table (info)->linkage_section
2252 || o == xcoff_hash_table (info)->toc_section
2253 || strcmp (o->name, ".debug") == 0)
2254 o->flags |= SEC_MARK;
2259 o->lineno_count = 0;
2266 /* Record the number of elements in a set. This is used to output the
2267 correct csect length. */
2270 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2272 struct bfd_link_info *info;
2273 struct bfd_link_hash_entry *harg;
2276 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2277 struct xcoff_link_size_list *n;
2279 if (! XCOFF_XVECP (output_bfd->xvec))
2282 /* This will hardly ever be called. I don't want to burn four bytes
2283 per global symbol, so instead the size is kept on a linked list
2284 attached to the hash table. */
2286 n = ((struct xcoff_link_size_list *)
2287 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2290 bfd_set_error (bfd_error_no_memory);
2293 n->next = xcoff_hash_table (info)->size_list;
2296 xcoff_hash_table (info)->size_list = n;
2298 h->flags |= XCOFF_HAS_SIZE;
2303 /* Import a symbol. */
2306 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2309 struct bfd_link_info *info;
2310 struct bfd_link_hash_entry *harg;
2312 const char *imppath;
2313 const char *impfile;
2314 const char *impmember;
2316 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2318 if (! XCOFF_XVECP (output_bfd->xvec))
2321 h->flags |= XCOFF_IMPORT;
2323 if (val != (bfd_vma) -1)
2325 if (h->root.type == bfd_link_hash_defined
2326 && (! bfd_is_abs_section (h->root.u.def.section)
2327 || h->root.u.def.value != val))
2329 if (! ((*info->callbacks->multiple_definition)
2330 (info, h->root.root.string, h->root.u.def.section->owner,
2331 h->root.u.def.section, h->root.u.def.value,
2332 output_bfd, bfd_abs_section_ptr, val)))
2336 h->root.type = bfd_link_hash_defined;
2337 h->root.u.def.section = bfd_abs_section_ptr;
2338 h->root.u.def.value = val;
2341 if (h->ldsym == NULL)
2343 h->ldsym = ((struct internal_ldsym *)
2344 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2345 if (h->ldsym == NULL)
2347 bfd_set_error (bfd_error_no_memory);
2352 if (imppath == NULL)
2353 h->ldsym->l_ifile = (bfd_size_type) -1;
2357 struct xcoff_import_file **pp;
2359 /* We start c at 1 because the first entry in the import list is
2360 reserved for the library search path. */
2361 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2363 pp = &(*pp)->next, ++c)
2365 if (strcmp ((*pp)->path, imppath) == 0
2366 && strcmp ((*pp)->file, impfile) == 0
2367 && strcmp ((*pp)->member, impmember) == 0)
2373 struct xcoff_import_file *n;
2375 n = ((struct xcoff_import_file *)
2376 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2379 bfd_set_error (bfd_error_no_memory);
2385 n->member = impmember;
2389 h->ldsym->l_ifile = c;
2395 /* Export a symbol. */
2398 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2400 struct bfd_link_info *info;
2401 struct bfd_link_hash_entry *harg;
2404 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2406 if (! XCOFF_XVECP (output_bfd->xvec))
2409 h->flags |= XCOFF_EXPORT;
2411 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2412 I'm just going to ignore it until somebody explains it. */
2414 /* Make sure we don't garbage collect this symbol. */
2415 if (! xcoff_mark_symbol (info, h))
2421 /* Count a reloc against a symbol. This is called for relocs
2422 generated by the linker script, typically for global constructors
2426 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2428 struct bfd_link_info *info;
2431 struct xcoff_link_hash_entry *h;
2433 if (! XCOFF_XVECP (output_bfd->xvec))
2436 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, false, false,
2440 (*_bfd_error_handler) ("%s: no such symbol", name);
2441 bfd_set_error (bfd_error_no_symbols);
2445 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2446 ++xcoff_hash_table (info)->ldrel_count;
2448 /* Mark the symbol to avoid garbage collection. */
2449 if (! xcoff_mark_symbol (info, h))
2455 /* This function is called for each symbol to which the linker script
2459 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2461 struct bfd_link_info *info;
2464 struct xcoff_link_hash_entry *h;
2466 if (! XCOFF_XVECP (output_bfd->xvec))
2469 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2474 h->flags |= XCOFF_DEF_REGULAR;
2479 /* This structure is used to pass information through
2480 xcoff_link_hash_traverse. */
2482 struct xcoff_loader_info
2484 /* Set if a problem occurred. */
2488 /* Link information structure. */
2489 struct bfd_link_info *info;
2490 /* Number of ldsym structures. */
2492 /* Size of string table. */
2496 /* Allocated size of string table. */
2500 /* Build the .loader section. This is called by the XCOFF linker
2501 emulation before_allocation routine. We must set the size of the
2502 .loader section before the linker lays out the output file.
2503 LIBPATH is the library path to search for shared objects; this is
2504 normally built from the -L arguments passed to the linker. ENTRY
2505 is the name of the entry point symbol. */
2508 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2509 file_align, maxstack, maxdata, gc,
2512 struct bfd_link_info *info;
2513 const char *libpath;
2515 unsigned long file_align;
2516 unsigned long maxstack;
2517 unsigned long maxdata;
2522 struct xcoff_link_hash_entry *hentry;
2524 struct xcoff_loader_info ldinfo;
2525 size_t impsize, impcount;
2526 struct xcoff_import_file *fl;
2527 struct internal_ldhdr *ldhdr;
2528 bfd_size_type stoff;
2532 struct bfd_strtab_hash *debug_strtab;
2533 bfd_byte *debug_contents = NULL;
2535 if (! XCOFF_XVECP (output_bfd->xvec))
2538 ldinfo.failed = false;
2539 ldinfo.output_bfd = output_bfd;
2541 ldinfo.ldsym_count = 0;
2542 ldinfo.string_size = 0;
2543 ldinfo.strings = NULL;
2544 ldinfo.string_alc = 0;
2546 xcoff_data (output_bfd)->maxstack = maxstack;
2547 xcoff_data (output_bfd)->maxdata = maxdata;
2548 xcoff_data (output_bfd)->modtype = modtype;
2550 xcoff_hash_table (info)->file_align = file_align;
2551 xcoff_hash_table (info)->textro = textro;
2553 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2554 false, false, true);
2557 hentry->flags |= XCOFF_ENTRY;
2558 if (hentry->root.type == bfd_link_hash_defined
2559 || hentry->root.type == bfd_link_hash_defweak)
2560 xcoff_data (output_bfd)->entry_section =
2561 hentry->root.u.def.section->output_section;
2564 /* Garbage collect unused sections. */
2565 if (info->relocateable
2568 || (hentry->root.type != bfd_link_hash_defined
2569 && hentry->root.type != bfd_link_hash_defweak))
2572 xcoff_hash_table (info)->gc = false;
2574 /* We still need to call xcoff_mark, in order to set ldrel_count
2576 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2580 for (o = sub->sections; o != NULL; o = o->next)
2582 if ((o->flags & SEC_MARK) == 0)
2584 if (! xcoff_mark (info, o))
2592 if (! xcoff_mark (info, hentry->root.u.def.section))
2595 xcoff_hash_table (info)->gc = true;
2598 if (info->input_bfds == NULL)
2600 /* I'm not sure what to do in this bizarre case. */
2604 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2609 /* Work out the size of the import file names. Each import file ID
2610 consists of three null terminated strings: the path, the file
2611 name, and the archive member name. The first entry in the list
2612 of names is the path to use to find objects, which the linker has
2613 passed in as the libpath argument. For some reason, the path
2614 entry in the other import file names appears to always be empty. */
2615 impsize = strlen (libpath) + 3;
2617 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2620 impsize += (strlen (fl->path)
2622 + strlen (fl->member)
2626 /* Set up the .loader section header. */
2627 ldhdr = &xcoff_hash_table (info)->ldhdr;
2628 ldhdr->l_version = 1;
2629 ldhdr->l_nsyms = ldinfo.ldsym_count;
2630 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2631 ldhdr->l_istlen = impsize;
2632 ldhdr->l_nimpid = impcount;
2633 ldhdr->l_impoff = (LDHDRSZ
2634 + ldhdr->l_nsyms * LDSYMSZ
2635 + ldhdr->l_nreloc * LDRELSZ);
2636 ldhdr->l_stlen = ldinfo.string_size;
2637 stoff = ldhdr->l_impoff + impsize;
2638 if (ldinfo.string_size == 0)
2641 ldhdr->l_stoff = stoff;
2643 /* We now know the final size of the .loader section. Allocate
2645 lsec = xcoff_hash_table (info)->loader_section;
2646 lsec->_raw_size = stoff + ldhdr->l_stlen;
2647 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2648 if (lsec->contents == NULL)
2650 bfd_set_error (bfd_error_no_memory);
2654 /* Set up the header. */
2655 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2656 (struct external_ldhdr *) lsec->contents);
2658 /* Set up the import file names. */
2659 out = (char *) lsec->contents + ldhdr->l_impoff;
2660 strcpy (out, libpath);
2661 out += strlen (libpath) + 1;
2664 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2666 register const char *s;
2669 while ((*out++ = *s++) != '\0')
2672 while ((*out++ = *s++) != '\0')
2675 while ((*out++ = *s++) != '\0')
2679 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
2681 /* Set up the symbol string table. */
2682 if (ldinfo.string_size > 0)
2684 memcpy (out, ldinfo.strings, ldinfo.string_size);
2685 free (ldinfo.strings);
2686 ldinfo.strings = NULL;
2689 /* We can't set up the symbol table or the relocs yet, because we
2690 don't yet know the final position of the various sections. The
2691 .loader symbols are written out when the corresponding normal
2692 symbols are written out in xcoff_link_input_bfd or
2693 xcoff_write_global_symbol. The .loader relocs are written out
2694 when the corresponding normal relocs are handled in
2695 xcoff_link_input_bfd. */
2697 /* Allocate space for the global linkage section and the global toc
2699 sec = xcoff_hash_table (info)->linkage_section;
2700 if (sec->_raw_size > 0)
2702 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2703 if (sec->contents == NULL)
2705 bfd_set_error (bfd_error_no_memory);
2709 sec = xcoff_hash_table (info)->toc_section;
2710 if (sec->_raw_size > 0)
2712 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2713 if (sec->contents == NULL)
2715 bfd_set_error (bfd_error_no_memory);
2720 /* Now that we've done garbage collection, figure out the contents
2721 of the .debug section. */
2722 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2724 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2727 bfd_size_type symcount;
2728 unsigned long *debug_index;
2730 bfd_byte *esym, *esymend;
2731 bfd_size_type symesz;
2733 if (sub->xvec != info->hash->creator)
2735 subdeb = bfd_get_section_by_name (sub, ".debug");
2736 if (subdeb == NULL || subdeb->_raw_size == 0)
2739 if (info->strip == strip_all
2740 || info->strip == strip_debugger
2741 || info->discard == discard_all)
2743 subdeb->_raw_size = 0;
2747 if (! _bfd_coff_get_external_symbols (sub))
2750 symcount = obj_raw_syment_count (sub);
2751 debug_index = ((unsigned long *)
2752 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2753 if (debug_index == NULL)
2755 bfd_set_error (bfd_error_no_memory);
2758 xcoff_data (sub)->debug_indices = debug_index;
2760 /* Grab the contents of the .debug section. We use malloc and
2761 copy the neams into the debug stringtab, rather than
2762 bfd_alloc, because I expect that, when linking many files
2763 together, many of the strings will be the same. Storing the
2764 strings in the hash table should save space in this case. */
2765 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2766 if (debug_contents == NULL)
2768 bfd_set_error (bfd_error_no_memory);
2771 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2772 (file_ptr) 0, subdeb->_raw_size))
2775 csectpp = xcoff_data (sub)->csects;
2777 symesz = bfd_coff_symesz (sub);
2778 esym = (bfd_byte *) obj_coff_external_syms (sub);
2779 esymend = esym + symcount * symesz;
2780 while (esym < esymend)
2782 struct internal_syment sym;
2784 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2786 *debug_index = (unsigned long) -1;
2788 if (sym._n._n_n._n_zeroes == 0
2791 || ((*csectpp)->flags & SEC_MARK) != 0
2792 || *csectpp == bfd_abs_section_ptr)
2793 && bfd_coff_symname_in_debug (sub, &sym))
2798 name = (char *) debug_contents + sym._n._n_n._n_offset;
2799 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2800 if (indx == (bfd_size_type) -1)
2802 *debug_index = indx;
2805 esym += (sym.n_numaux + 1) * symesz;
2806 csectpp += sym.n_numaux + 1;
2807 debug_index += sym.n_numaux + 1;
2810 free (debug_contents);
2811 debug_contents = NULL;
2813 /* Clear the size of subdeb, so that it is not included directly
2814 in the output file. */
2815 subdeb->_raw_size = 0;
2817 if (! info->keep_memory)
2819 if (! _bfd_coff_free_symbols (sub))
2824 xcoff_hash_table (info)->debug_section->_raw_size =
2825 _bfd_stringtab_size (debug_strtab);
2830 if (ldinfo.strings != NULL)
2831 free (ldinfo.strings);
2832 if (debug_contents != NULL)
2833 free (debug_contents);
2837 /* Add a symbol to the .loader symbols, if necessary. */
2840 xcoff_build_ldsyms (h, p)
2841 struct xcoff_link_hash_entry *h;
2844 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2847 /* We don't want to garbage collect symbols which are not defined in
2848 XCOFF files. This is a convenient place to mark them. */
2849 if (xcoff_hash_table (ldinfo->info)->gc
2850 && (h->flags & XCOFF_MARK) == 0
2851 && (h->root.type == bfd_link_hash_defined
2852 || h->root.type == bfd_link_hash_defweak)
2853 && (h->root.u.def.section->owner == NULL
2854 || (h->root.u.def.section->owner->xvec
2855 != ldinfo->info->hash->creator)))
2856 h->flags |= XCOFF_MARK;
2858 /* If this symbol is called and defined in a dynamic object, or not
2859 defined at all when building a shared object, then we need to set
2860 up global linkage code for it. (Unless we did garbage collection
2861 and we didn't need this symbol.) */
2862 if ((h->flags & XCOFF_CALLED) != 0
2863 && (h->root.type == bfd_link_hash_undefined
2864 || h->root.type == bfd_link_hash_undefweak)
2865 && h->root.root.string[0] == '.'
2866 && h->descriptor != NULL
2867 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2868 || ldinfo->info->shared)
2869 && (! xcoff_hash_table (ldinfo->info)->gc
2870 || (h->flags & XCOFF_MARK) != 0))
2873 struct xcoff_link_hash_entry *hds;
2875 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2876 h->root.type = bfd_link_hash_defined;
2877 h->root.u.def.section = sec;
2878 h->root.u.def.value = sec->_raw_size;
2880 sec->_raw_size += XCOFF_GLINK_SIZE;
2882 /* The global linkage code requires a TOC entry for the
2884 hds = h->descriptor;
2885 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2886 || hds->root.type == bfd_link_hash_undefweak)
2887 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2888 hds->flags |= XCOFF_MARK;
2889 if (hds->toc_section == NULL)
2891 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2892 hds->u.toc_offset = hds->toc_section->_raw_size;
2893 hds->toc_section->_raw_size += 4;
2894 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2895 ++hds->toc_section->reloc_count;
2897 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2899 /* We need to call xcoff_build_ldsyms recursively here,
2900 because we may already have passed hds on the traversal. */
2901 xcoff_build_ldsyms (hds, p);
2905 /* If this is still a common symbol, and it wasn't garbage
2906 collected, we need to actually allocate space for it in the .bss
2908 if (h->root.type == bfd_link_hash_common
2909 && (! xcoff_hash_table (ldinfo->info)->gc
2910 || (h->flags & XCOFF_MARK) != 0)
2911 && h->root.u.c.p->section->_raw_size == 0)
2913 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2914 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
2917 /* We need to add a symbol to the .loader section if it is mentioned
2918 in a reloc which we are copying to the .loader section and it was
2919 not defined or common, or if it is the entry point, or if it is
2922 if (((h->flags & XCOFF_LDREL) == 0
2923 || h->root.type == bfd_link_hash_defined
2924 || h->root.type == bfd_link_hash_defweak
2925 || h->root.type == bfd_link_hash_common)
2926 && (h->flags & XCOFF_ENTRY) == 0
2927 && (h->flags & XCOFF_EXPORT) == 0)
2933 /* We don't need to add this symbol if we did garbage collection and
2934 we did not mark this symbol. */
2935 if (xcoff_hash_table (ldinfo->info)->gc
2936 && (h->flags & XCOFF_MARK) == 0)
2942 /* We may have already processed this symbol due to the recursive
2944 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2947 /* We need to add this symbol to the .loader symbols. */
2949 /* h->ldsym will already have been allocated for an explicitly
2951 if (h->ldsym == NULL)
2953 h->ldsym = ((struct internal_ldsym *)
2954 bfd_zalloc (ldinfo->output_bfd,
2955 sizeof (struct internal_ldsym)));
2956 if (h->ldsym == NULL)
2958 ldinfo->failed = true;
2959 bfd_set_error (bfd_error_no_memory);
2964 /* The first 3 symbol table indices are reserved to indicate the
2966 h->ldindx = ldinfo->ldsym_count + 3;
2968 ++ldinfo->ldsym_count;
2970 len = strlen (h->root.root.string);
2971 if (len <= SYMNMLEN)
2972 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
2975 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2978 bfd_byte *newstrings;
2980 newalc = ldinfo->string_alc * 2;
2983 while (ldinfo->string_size + len + 3 > newalc)
2986 if (ldinfo->strings == NULL)
2987 newstrings = (bfd_byte *) malloc (newalc);
2989 newstrings = ((bfd_byte *)
2990 realloc ((PTR) ldinfo->strings, newalc));
2991 if (newstrings == NULL)
2993 ldinfo->failed = true;
2994 bfd_set_error (bfd_error_no_memory);
2997 ldinfo->string_alc = newalc;
2998 ldinfo->strings = newstrings;
3001 bfd_put_16 (ldinfo->output_bfd, len + 1,
3002 ldinfo->strings + ldinfo->string_size);
3003 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3004 h->ldsym->_l._l_l._l_zeroes = 0;
3005 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3006 ldinfo->string_size += len + 3;
3009 h->flags |= XCOFF_BUILT_LDSYM;
3014 /* Do the final link step. */
3017 _bfd_xcoff_bfd_final_link (abfd, info)
3019 struct bfd_link_info *info;
3021 bfd_size_type symesz;
3022 struct xcoff_final_link_info finfo;
3024 struct bfd_link_order *p;
3025 size_t max_contents_size;
3026 size_t max_sym_count;
3027 size_t max_lineno_count;
3028 size_t max_reloc_count;
3029 size_t max_output_reloc_count;
3030 file_ptr rel_filepos;
3032 file_ptr line_filepos;
3033 unsigned int linesz;
3035 bfd_byte *external_relocs = NULL;
3036 char strbuf[STRING_SIZE_SIZE];
3039 abfd->flags |= DYNAMIC;
3041 symesz = bfd_coff_symesz (abfd);
3044 finfo.output_bfd = abfd;
3045 finfo.strtab = NULL;
3046 finfo.section_info = NULL;
3047 finfo.last_file_index = -1;
3048 finfo.toc_symindx = -1;
3049 finfo.internal_syms = NULL;
3050 finfo.sym_indices = NULL;
3051 finfo.outsyms = NULL;
3052 finfo.linenos = NULL;
3053 finfo.contents = NULL;
3054 finfo.external_relocs = NULL;
3056 finfo.ldsym = ((struct external_ldsym *)
3057 (xcoff_hash_table (info)->loader_section->contents
3059 finfo.ldrel = ((struct external_ldrel *)
3060 (xcoff_hash_table (info)->loader_section->contents
3062 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3064 xcoff_data (abfd)->coff.link_info = info;
3066 finfo.strtab = _bfd_stringtab_init ();
3067 if (finfo.strtab == NULL)
3070 /* Compute the file positions for all the sections. */
3071 if (abfd->output_has_begun)
3073 if (xcoff_hash_table (info)->file_align != 0)
3080 file_align = xcoff_hash_table (info)->file_align;
3081 if (file_align != 0)
3083 boolean saw_contents;
3088 /* Insert .pad sections before every section which has
3089 contents and is loaded, if it is preceded by some other
3090 section which has contents and is loaded. */
3091 saw_contents = true;
3092 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3094 (*op)->target_index = indx;
3095 if (strcmp ((*op)->name, ".pad") == 0)
3096 saw_contents = false;
3097 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3098 && ((*op)->flags & SEC_LOAD) != 0)
3101 saw_contents = true;
3108 n = bfd_make_section_anyway (abfd, ".pad");
3109 BFD_ASSERT (*op == n);
3111 n->flags = SEC_HAS_CONTENTS;
3112 n->alignment_power = 0;
3113 saw_contents = false;
3118 /* Reset the section indices after inserting the new
3121 for (o = abfd->sections; o != NULL; o = o->next)
3124 o->target_index = indx;
3126 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3128 /* Work out appropriate sizes for the .pad sections to force
3129 each section to land on a page boundary. This bit of
3130 code knows what compute_section_file_positions is going
3132 sofar = bfd_coff_filhsz (abfd);
3133 sofar += bfd_coff_aoutsz (abfd);
3134 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3136 for (o = abfd->sections; o != NULL; o = o->next)
3138 if (strcmp (o->name, ".pad") == 0)
3142 BFD_ASSERT (o->_raw_size == 0);
3143 pageoff = sofar & (file_align - 1);
3146 o->_raw_size = file_align - pageoff;
3147 sofar += file_align - pageoff;
3148 o->flags |= SEC_HAS_CONTENTS;
3153 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3154 sofar += BFD_ALIGN (o->_raw_size,
3155 1 << o->alignment_power);
3160 bfd_coff_compute_section_file_positions (abfd);
3163 /* Count the line numbers and relocation entries required for the
3164 output file. Set the file positions for the relocs. */
3165 rel_filepos = obj_relocbase (abfd);
3166 relsz = bfd_coff_relsz (abfd);
3167 max_contents_size = 0;
3168 max_lineno_count = 0;
3169 max_reloc_count = 0;
3170 for (o = abfd->sections; o != NULL; o = o->next)
3173 o->lineno_count = 0;
3174 for (p = o->link_order_head; p != NULL; p = p->next)
3176 if (p->type == bfd_indirect_link_order)
3180 sec = p->u.indirect.section;
3182 if (info->strip == strip_none
3183 || info->strip == strip_some)
3184 o->lineno_count += sec->lineno_count;
3186 o->reloc_count += sec->reloc_count;
3188 if (sec->_raw_size > max_contents_size)
3189 max_contents_size = sec->_raw_size;
3190 if (sec->lineno_count > max_lineno_count)
3191 max_lineno_count = sec->lineno_count;
3192 if (coff_section_data (sec->owner, sec) != NULL
3193 && xcoff_section_data (sec->owner, sec) != NULL
3194 && (xcoff_section_data (sec->owner, sec)->lineno_count
3195 > max_lineno_count))
3197 xcoff_section_data (sec->owner, sec)->lineno_count;
3198 if (sec->reloc_count > max_reloc_count)
3199 max_reloc_count = sec->reloc_count;
3201 else if (p->type == bfd_section_reloc_link_order
3202 || p->type == bfd_symbol_reloc_link_order)
3205 if (o->reloc_count == 0)
3209 o->flags |= SEC_RELOC;
3210 o->rel_filepos = rel_filepos;
3211 rel_filepos += o->reloc_count * relsz;
3215 /* Allocate space for the pointers we need to keep for the relocs. */
3219 /* We use section_count + 1, rather than section_count, because
3220 the target_index fields are 1 based. */
3221 finfo.section_info = ((struct xcoff_link_section_info *)
3222 malloc ((abfd->section_count + 1)
3223 * sizeof (struct xcoff_link_section_info)));
3224 if (finfo.section_info == NULL)
3226 bfd_set_error (bfd_error_no_memory);
3229 for (i = 0; i <= abfd->section_count; i++)
3231 finfo.section_info[i].relocs = NULL;
3232 finfo.section_info[i].rel_hashes = NULL;
3233 finfo.section_info[i].toc_rel_hashes = NULL;
3237 /* We now know the size of the relocs, so we can determine the file
3238 positions of the line numbers. */
3239 line_filepos = rel_filepos;
3240 finfo.line_filepos = line_filepos;
3241 linesz = bfd_coff_linesz (abfd);
3242 max_output_reloc_count = 0;
3243 for (o = abfd->sections; o != NULL; o = o->next)
3245 if (o->lineno_count == 0)
3246 o->line_filepos = 0;
3249 o->line_filepos = line_filepos;
3250 line_filepos += o->lineno_count * linesz;
3253 if (o->reloc_count != 0)
3255 /* We don't know the indices of global symbols until we have
3256 written out all the local symbols. For each section in
3257 the output file, we keep an array of pointers to hash
3258 table entries. Each entry in the array corresponds to a
3259 reloc. When we find a reloc against a global symbol, we
3260 set the corresponding entry in this array so that we can
3261 fix up the symbol index after we have written out all the
3264 Because of this problem, we also keep the relocs in
3265 memory until the end of the link. This wastes memory.
3266 We could backpatch the file later, I suppose, although it
3268 finfo.section_info[o->target_index].relocs =
3269 ((struct internal_reloc *)
3270 malloc (o->reloc_count * sizeof (struct internal_reloc)));
3271 finfo.section_info[o->target_index].rel_hashes =
3272 ((struct xcoff_link_hash_entry **)
3273 malloc (o->reloc_count
3274 * sizeof (struct xcoff_link_hash_entry *)));
3275 if (finfo.section_info[o->target_index].relocs == NULL
3276 || finfo.section_info[o->target_index].rel_hashes == NULL)
3278 bfd_set_error (bfd_error_no_memory);
3282 if (o->reloc_count > max_output_reloc_count)
3283 max_output_reloc_count = o->reloc_count;
3286 /* Reset the reloc and lineno counts, so that we can use them to
3287 count the number of entries we have output so far. */
3289 o->lineno_count = 0;
3292 obj_sym_filepos (abfd) = line_filepos;
3294 /* Figure out the largest number of symbols in an input BFD. Take
3295 the opportunity to clear the output_has_begun fields of all the
3296 input BFD's. We want at least 4 symbols, since that is the
3297 number which xcoff_write_global_symbol may need. */
3299 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3303 sub->output_has_begun = false;
3304 sz = obj_raw_syment_count (sub);
3305 if (sz > max_sym_count)
3309 /* Allocate some buffers used while linking. */
3310 finfo.internal_syms = ((struct internal_syment *)
3311 malloc (max_sym_count
3312 * sizeof (struct internal_syment)));
3313 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
3314 finfo.outsyms = ((bfd_byte *)
3315 malloc ((size_t) ((max_sym_count + 1) * symesz)));
3316 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
3317 * bfd_coff_linesz (abfd));
3318 finfo.contents = (bfd_byte *) malloc (max_contents_size);
3319 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
3320 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3321 || (finfo.sym_indices == NULL && max_sym_count > 0)
3322 || finfo.outsyms == NULL
3323 || (finfo.linenos == NULL && max_lineno_count > 0)
3324 || (finfo.contents == NULL && max_contents_size > 0)
3325 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3327 bfd_set_error (bfd_error_no_memory);
3331 obj_raw_syment_count (abfd) = 0;
3332 xcoff_data (abfd)->toc = (bfd_vma) -1;
3334 /* We now know the position of everything in the file, except that
3335 we don't know the size of the symbol table and therefore we don't
3336 know where the string table starts. We just build the string
3337 table in memory as we go along. We process all the relocations
3338 for a single input file at once. */
3339 for (o = abfd->sections; o != NULL; o = o->next)
3341 for (p = o->link_order_head; p != NULL; p = p->next)
3343 if (p->type == bfd_indirect_link_order
3344 && p->u.indirect.section->owner->xvec == abfd->xvec)
3346 sub = p->u.indirect.section->owner;
3347 if (! sub->output_has_begun)
3349 if (! xcoff_link_input_bfd (&finfo, sub))
3351 sub->output_has_begun = true;
3354 else if (p->type == bfd_section_reloc_link_order
3355 || p->type == bfd_symbol_reloc_link_order)
3357 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3362 if (! _bfd_default_link_order (abfd, info, o, p))
3368 /* Free up the buffers used by xcoff_link_input_bfd. */
3370 if (finfo.internal_syms != NULL)
3372 free (finfo.internal_syms);
3373 finfo.internal_syms = NULL;
3375 if (finfo.sym_indices != NULL)
3377 free (finfo.sym_indices);
3378 finfo.sym_indices = NULL;
3380 if (finfo.linenos != NULL)
3382 free (finfo.linenos);
3383 finfo.linenos = NULL;
3385 if (finfo.contents != NULL)
3387 free (finfo.contents);
3388 finfo.contents = NULL;
3390 if (finfo.external_relocs != NULL)
3392 free (finfo.external_relocs);
3393 finfo.external_relocs = NULL;
3396 /* The value of the last C_FILE symbol is supposed to be -1. Write
3398 if (finfo.last_file_index != -1)
3400 finfo.last_file.n_value = -1;
3401 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3402 (PTR) finfo.outsyms);
3404 (obj_sym_filepos (abfd)
3405 + finfo.last_file_index * symesz),
3407 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3411 /* Write out all the global symbols which do not come from XCOFF
3413 xcoff_link_hash_traverse (xcoff_hash_table (info),
3414 xcoff_write_global_symbol,
3417 if (finfo.outsyms != NULL)
3419 free (finfo.outsyms);
3420 finfo.outsyms = NULL;
3423 /* Now that we have written out all the global symbols, we know the
3424 symbol indices to use for relocs against them, and we can finally
3425 write out the relocs. */
3426 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3427 if (external_relocs == NULL && max_output_reloc_count != 0)
3429 bfd_set_error (bfd_error_no_memory);
3433 for (o = abfd->sections; o != NULL; o = o->next)
3435 struct internal_reloc *irel;
3436 struct internal_reloc *irelend;
3437 struct xcoff_link_hash_entry **rel_hash;
3438 struct xcoff_toc_rel_hash *toc_rel_hash;
3441 if (o->reloc_count == 0)
3444 irel = finfo.section_info[o->target_index].relocs;
3445 irelend = irel + o->reloc_count;
3446 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3447 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3449 if (*rel_hash != NULL)
3451 if ((*rel_hash)->indx < 0)
3453 if (! ((*info->callbacks->unattached_reloc)
3454 (info, (*rel_hash)->root.root.string,
3455 (bfd *) NULL, o, irel->r_vaddr)))
3457 (*rel_hash)->indx = 0;
3459 irel->r_symndx = (*rel_hash)->indx;
3463 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
3464 toc_rel_hash != NULL;
3465 toc_rel_hash = toc_rel_hash->next)
3467 if (toc_rel_hash->h->u.toc_indx < 0)
3469 if (! ((*info->callbacks->unattached_reloc)
3470 (info, toc_rel_hash->h->root.root.string,
3471 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
3473 toc_rel_hash->h->u.toc_indx = 0;
3475 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
3478 /* XCOFF requires that the relocs be sorted by address. We tend
3479 to produce them in the order in which their containing csects
3480 appear in the symbol table, which is not necessarily by
3481 address. So we sort them here. There may be a better way to
3483 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3484 o->reloc_count, sizeof (struct internal_reloc),
3487 irel = finfo.section_info[o->target_index].relocs;
3488 irelend = irel + o->reloc_count;
3489 erel = external_relocs;
3490 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3491 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3493 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3494 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3495 abfd) != relsz * o->reloc_count)
3499 if (external_relocs != NULL)
3501 free (external_relocs);
3502 external_relocs = NULL;
3505 /* Free up the section information. */
3506 if (finfo.section_info != NULL)
3510 for (i = 0; i < abfd->section_count; i++)
3512 if (finfo.section_info[i].relocs != NULL)
3513 free (finfo.section_info[i].relocs);
3514 if (finfo.section_info[i].rel_hashes != NULL)
3515 free (finfo.section_info[i].rel_hashes);
3517 free (finfo.section_info);
3518 finfo.section_info = NULL;
3521 /* Write out the loader section contents. */
3522 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3523 == (xcoff_hash_table (info)->loader_section->contents
3524 + xcoff_hash_table (info)->ldhdr.l_impoff));
3525 o = xcoff_hash_table (info)->loader_section;
3526 if (! bfd_set_section_contents (abfd, o->output_section,
3527 o->contents, o->output_offset,
3531 /* Write out the global linkage section and the toc section. */
3532 o = xcoff_hash_table (info)->linkage_section;
3533 if (o->_raw_size > 0
3534 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3535 o->output_offset, o->_raw_size))
3537 o = xcoff_hash_table (info)->toc_section;
3538 if (o->_raw_size > 0
3539 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3540 o->output_offset, o->_raw_size))
3543 /* Write out the string table. */
3545 (obj_sym_filepos (abfd)
3546 + obj_raw_syment_count (abfd) * symesz),
3550 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3551 (bfd_byte *) strbuf);
3552 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3554 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3557 _bfd_stringtab_free (finfo.strtab);
3559 /* Write out the debugging string table. */
3560 o = xcoff_hash_table (info)->debug_section;
3563 struct bfd_strtab_hash *debug_strtab;
3565 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3566 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3567 >= _bfd_stringtab_size (debug_strtab));
3569 o->output_section->filepos + o->output_offset,
3572 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3576 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3577 not try to write out the symbols. */
3578 bfd_get_symcount (abfd) = 0;
3583 if (finfo.strtab != NULL)
3584 _bfd_stringtab_free (finfo.strtab);
3585 if (finfo.section_info != NULL)
3589 for (i = 0; i < abfd->section_count; i++)
3591 if (finfo.section_info[i].relocs != NULL)
3592 free (finfo.section_info[i].relocs);
3593 if (finfo.section_info[i].rel_hashes != NULL)
3594 free (finfo.section_info[i].rel_hashes);
3596 free (finfo.section_info);
3598 if (finfo.internal_syms != NULL)
3599 free (finfo.internal_syms);
3600 if (finfo.sym_indices != NULL)
3601 free (finfo.sym_indices);
3602 if (finfo.outsyms != NULL)
3603 free (finfo.outsyms);
3604 if (finfo.linenos != NULL)
3605 free (finfo.linenos);
3606 if (finfo.contents != NULL)
3607 free (finfo.contents);
3608 if (finfo.external_relocs != NULL)
3609 free (finfo.external_relocs);
3610 if (external_relocs != NULL)
3611 free (external_relocs);
3615 /* Link an input file into the linker output file. This function
3616 handles all the sections and relocations of the input file at once. */
3619 xcoff_link_input_bfd (finfo, input_bfd)
3620 struct xcoff_final_link_info *finfo;
3624 const char *strings;
3625 bfd_size_type syment_base;
3626 unsigned int n_tmask;
3627 unsigned int n_btshft;
3629 bfd_size_type isymesz;
3630 bfd_size_type osymesz;
3631 bfd_size_type linesz;
3634 struct xcoff_link_hash_entry **sym_hash;
3635 struct internal_syment *isymp;
3637 unsigned long *debug_index;
3639 unsigned long output_index;
3646 /* We can just skip DYNAMIC files, unless this is a static link. */
3647 if ((input_bfd->flags & DYNAMIC) != 0
3648 && ! finfo->info->static_link)
3651 /* Move all the symbols to the output file. */
3653 output_bfd = finfo->output_bfd;
3655 syment_base = obj_raw_syment_count (output_bfd);
3656 isymesz = bfd_coff_symesz (input_bfd);
3657 osymesz = bfd_coff_symesz (output_bfd);
3658 linesz = bfd_coff_linesz (input_bfd);
3659 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3661 n_tmask = coff_data (input_bfd)->local_n_tmask;
3662 n_btshft = coff_data (input_bfd)->local_n_btshft;
3664 /* Define macros so that ISFCN, et. al., macros work correctly. */
3665 #define N_TMASK n_tmask
3666 #define N_BTSHFT n_btshft
3669 if (! finfo->info->keep_memory)
3672 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3675 if (! _bfd_coff_get_external_symbols (input_bfd))
3678 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3679 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3680 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3681 csectpp = xcoff_data (input_bfd)->csects;
3682 debug_index = xcoff_data (input_bfd)->debug_indices;
3683 isymp = finfo->internal_syms;
3684 indexp = finfo->sym_indices;
3685 output_index = syment_base;
3686 outsym = finfo->outsyms;
3690 while (esym < esym_end)
3692 struct internal_syment isym;
3693 union internal_auxent aux;
3699 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3701 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3703 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3705 BFD_ASSERT (isymp->n_numaux > 0);
3706 bfd_coff_swap_aux_in (input_bfd,
3707 (PTR) (esym + isymesz * isymp->n_numaux),
3708 isymp->n_type, isymp->n_sclass,
3709 isymp->n_numaux - 1, isymp->n_numaux,
3711 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3714 /* Make a copy of *isymp so that the relocate_section function
3715 always sees the original values. This is more reliable than
3716 always recomputing the symbol value even if we are stripping
3720 /* If this symbol is in the .loader section, swap out the
3721 .loader symbol information. If this is an external symbol
3722 reference to a defined symbol, though, then wait until we get
3723 to the definition. */
3724 if (isym.n_sclass == C_EXT
3725 && *sym_hash != NULL
3726 && (*sym_hash)->ldsym != NULL
3728 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3730 struct xcoff_link_hash_entry *h;
3731 struct internal_ldsym *ldsym;
3735 if (isym.n_scnum > 0)
3737 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3738 ldsym->l_value = (isym.n_value
3739 + (*csectpp)->output_section->vma
3740 + (*csectpp)->output_offset
3745 ldsym->l_scnum = isym.n_scnum;
3746 ldsym->l_value = isym.n_value;
3749 ldsym->l_smtype = smtyp;
3750 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3751 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3752 || (h->flags & XCOFF_IMPORT) != 0)
3753 ldsym->l_smtype |= L_IMPORT;
3754 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3755 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3756 || (h->flags & XCOFF_EXPORT) != 0)
3757 ldsym->l_smtype |= L_EXPORT;
3758 if ((h->flags & XCOFF_ENTRY) != 0)
3759 ldsym->l_smtype |= L_ENTRY;
3761 ldsym->l_smclas = aux.x_csect.x_smclas;
3763 if (ldsym->l_ifile == (bfd_size_type) -1)
3765 else if (ldsym->l_ifile == 0)
3767 if ((ldsym->l_smtype & L_IMPORT) == 0)
3773 if (h->root.type == bfd_link_hash_defined
3774 || h->root.type == bfd_link_hash_defweak)
3775 impbfd = h->root.u.def.section->owner;
3776 else if (h->root.type == bfd_link_hash_undefined
3777 || h->root.type == bfd_link_hash_undefweak)
3778 impbfd = h->root.u.undef.abfd;
3786 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3787 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3794 BFD_ASSERT (h->ldindx >= 0);
3795 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3796 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3797 finfo->ldsym + h->ldindx - 3);
3805 add = 1 + isym.n_numaux;
3807 /* If we are skipping this csect, we want to skip this symbol. */
3808 if (*csectpp == NULL)
3811 /* If we garbage collected this csect, we want to skip this
3814 && xcoff_hash_table (finfo->info)->gc
3815 && ((*csectpp)->flags & SEC_MARK) == 0
3816 && *csectpp != bfd_abs_section_ptr)
3819 /* An XCOFF linker always skips C_STAT symbols. */
3821 && isymp->n_sclass == C_STAT)
3824 /* We skip all but the first TOC anchor. */
3826 && isymp->n_sclass == C_HIDEXT
3827 && aux.x_csect.x_smclas == XMC_TC0)
3829 if (finfo->toc_symindx != -1)
3833 finfo->toc_symindx = output_index;
3834 xcoff_data (finfo->output_bfd)->toc =
3835 ((*csectpp)->output_section->vma
3836 + (*csectpp)->output_offset
3839 xcoff_data (finfo->output_bfd)->toc_section =
3840 (*csectpp)->output_section;
3845 /* If we are stripping all symbols, we want to skip this one. */
3847 && finfo->info->strip == strip_all)
3850 /* We can skip resolved external references. */
3852 && isym.n_sclass == C_EXT
3854 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3857 /* We can skip common symbols if they got defined somewhere
3860 && isym.n_sclass == C_EXT
3862 && ((*sym_hash)->root.type != bfd_link_hash_common
3863 || (*sym_hash)->root.u.c.p->section != *csectpp)
3864 && ((*sym_hash)->root.type != bfd_link_hash_defined
3865 || (*sym_hash)->root.u.def.section != *csectpp))
3868 /* Skip local symbols if we are discarding them. */
3870 && finfo->info->discard == discard_all
3871 && isym.n_sclass != C_EXT
3872 && (isym.n_sclass != C_HIDEXT
3873 || smtyp != XTY_SD))
3876 /* If we stripping debugging symbols, and this is a debugging
3877 symbol, then skip it. */
3879 && finfo->info->strip == strip_debugger
3880 && isym.n_scnum == N_DEBUG)
3883 /* If some symbols are stripped based on the name, work out the
3884 name and decide whether to skip this symbol. We don't handle
3885 this correctly for symbols whose names are in the .debug
3886 section; to get it right we would need a new bfd_strtab_hash
3887 function to return the string given the index. */
3889 && (finfo->info->strip == strip_some
3890 || finfo->info->discard == discard_l)
3891 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3894 char buf[SYMNMLEN + 1];
3896 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3900 if ((finfo->info->strip == strip_some
3901 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
3903 || (finfo->info->discard == discard_l
3904 && (isym.n_sclass != C_EXT
3905 && (isym.n_sclass != C_HIDEXT
3906 || smtyp != XTY_SD))
3907 && strncmp (name, finfo->info->lprefix,
3908 finfo->info->lprefix_len) == 0))
3912 /* We can not skip the first TOC anchor. */
3915 && finfo->info->strip != strip_all)
3918 /* We now know whether we are to skip this symbol or not. */
3921 /* Adjust the symbol in order to output it. */
3923 if (isym._n._n_n._n_zeroes == 0
3924 && isym._n._n_n._n_offset != 0)
3926 /* This symbol has a long name. Enter it in the string
3927 table we are building. If *debug_index != -1, the
3928 name has already been entered in the .debug section. */
3929 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3930 isym._n._n_n._n_offset = *debug_index;
3936 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
3940 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3941 if (indx == (bfd_size_type) -1)
3943 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3947 if (isym.n_sclass != C_BSTAT
3948 && isym.n_sclass != C_ESTAT
3949 && isym.n_sclass != C_DECL
3950 && isym.n_scnum > 0)
3952 isym.n_scnum = (*csectpp)->output_section->target_index;
3953 isym.n_value += ((*csectpp)->output_section->vma
3954 + (*csectpp)->output_offset
3958 /* The value of a C_FILE symbol is the symbol index of the
3959 next C_FILE symbol. The value of the last C_FILE symbol
3960 is -1. We try to get this right, below, just before we
3961 write the symbols out, but in the general case we may
3962 have to write the symbol out twice. */
3963 if (isym.n_sclass == C_FILE)
3965 if (finfo->last_file_index != -1
3966 && finfo->last_file.n_value != (long) output_index)
3968 /* We must correct the value of the last C_FILE entry. */
3969 finfo->last_file.n_value = output_index;
3970 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3972 /* The last C_FILE symbol is in this input file. */
3973 bfd_coff_swap_sym_out (output_bfd,
3974 (PTR) &finfo->last_file,
3975 (PTR) (finfo->outsyms
3976 + ((finfo->last_file_index
3982 /* We have already written out the last C_FILE
3983 symbol. We need to write it out again. We
3984 borrow *outsym temporarily. */
3985 bfd_coff_swap_sym_out (output_bfd,
3986 (PTR) &finfo->last_file,
3988 if (bfd_seek (output_bfd,
3989 (obj_sym_filepos (output_bfd)
3990 + finfo->last_file_index * osymesz),
3992 || (bfd_write (outsym, osymesz, 1, output_bfd)
3998 finfo->last_file_index = output_index;
3999 finfo->last_file = isym;
4002 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4003 into the line numbers. We update the symbol values when
4004 we handle the line numbers. */
4005 if (isym.n_sclass == C_BINCL
4006 || isym.n_sclass == C_EINCL)
4008 isym.n_value = finfo->line_filepos;
4012 /* Output the symbol. */
4014 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4016 *indexp = output_index;
4018 if (isym.n_sclass == C_EXT)
4021 struct xcoff_link_hash_entry *h;
4023 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4025 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4026 BFD_ASSERT (h != NULL);
4027 h->indx = output_index;
4030 /* If this is a symbol in the TOC which we may have merged
4031 (class XMC_TC), remember the symbol index of the TOC
4033 if (isym.n_sclass == C_HIDEXT
4034 && aux.x_csect.x_smclas == XMC_TC
4035 && *sym_hash != NULL)
4037 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4038 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4039 (*sym_hash)->u.toc_indx = output_index;
4042 output_index += add;
4043 outsym += add * osymesz;
4046 esym += add * isymesz;
4050 if (debug_index != NULL)
4053 for (--add; add > 0; --add)
4057 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4058 done in a separate pass, because we don't know the correct symbol
4059 indices until we have already decided which symbols we are going
4062 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4063 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4064 isymp = finfo->internal_syms;
4065 indexp = finfo->sym_indices;
4066 csectpp = xcoff_data (input_bfd)->csects;
4067 outsym = finfo->outsyms;
4068 while (esym < esym_end)
4072 add = 1 + isymp->n_numaux;
4075 esym += add * isymesz;
4080 if (isymp->n_sclass == C_BSTAT)
4082 struct internal_syment isym;
4085 /* The value of a C_BSTAT symbol is the symbol table
4086 index of the containing csect. */
4087 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4088 indx = isym.n_value;
4089 if (indx < obj_raw_syment_count (input_bfd))
4093 symindx = finfo->sym_indices[indx];
4097 isym.n_value = symindx;
4098 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4106 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4108 union internal_auxent aux;
4110 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4111 isymp->n_sclass, i, isymp->n_numaux,
4114 if (isymp->n_sclass == C_FILE)
4116 /* This is the file name (or some comment put in by
4117 the compiler). If it is long, we must put it in
4118 the string table. */
4119 if (aux.x_file.x_n.x_zeroes == 0
4120 && aux.x_file.x_n.x_offset != 0)
4122 const char *filename;
4125 BFD_ASSERT (aux.x_file.x_n.x_offset
4126 >= STRING_SIZE_SIZE);
4127 if (strings == NULL)
4129 strings = _bfd_coff_read_string_table (input_bfd);
4130 if (strings == NULL)
4133 filename = strings + aux.x_file.x_n.x_offset;
4134 indx = _bfd_stringtab_add (finfo->strtab, filename,
4136 if (indx == (bfd_size_type) -1)
4138 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4141 else if ((isymp->n_sclass == C_EXT
4142 || isymp->n_sclass == C_HIDEXT)
4143 && i + 1 == isymp->n_numaux)
4145 /* We don't support type checking. I don't know if
4147 aux.x_csect.x_parmhash = 0;
4148 /* I don't think anybody uses these fields, but we'd
4149 better clobber them just in case. */
4150 aux.x_csect.x_stab = 0;
4151 aux.x_csect.x_snstab = 0;
4152 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4156 indx = aux.x_csect.x_scnlen.l;
4157 if (indx < obj_raw_syment_count (input_bfd))
4161 symindx = finfo->sym_indices[indx];
4163 aux.x_sym.x_tagndx.l = 0;
4165 aux.x_sym.x_tagndx.l = symindx;
4169 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4173 if (ISFCN (isymp->n_type)
4174 || ISTAG (isymp->n_sclass)
4175 || isymp->n_sclass == C_BLOCK)
4177 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4179 && indx < obj_raw_syment_count (input_bfd))
4181 /* We look forward through the symbol for
4182 the index of the next symbol we are going
4183 to include. I don't know if this is
4185 while (finfo->sym_indices[indx] < 0
4186 && indx < obj_raw_syment_count (input_bfd))
4188 if (indx >= obj_raw_syment_count (input_bfd))
4189 indx = output_index;
4191 indx = finfo->sym_indices[indx];
4192 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4196 indx = aux.x_sym.x_tagndx.l;
4197 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4201 symindx = finfo->sym_indices[indx];
4203 aux.x_sym.x_tagndx.l = 0;
4205 aux.x_sym.x_tagndx.l = symindx;
4209 /* Copy over the line numbers, unless we are stripping
4210 them. We do this on a symbol by symbol basis in
4211 order to more easily handle garbage collection. */
4212 if ((isymp->n_sclass == C_EXT
4213 || isymp->n_sclass == C_HIDEXT)
4215 && isymp->n_numaux > 1
4216 && ISFCN (isymp->n_type)
4217 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4219 if (finfo->info->strip != strip_none
4220 && finfo->info->strip != strip_some)
4221 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4224 asection *enclosing;
4225 unsigned int enc_count;
4226 bfd_size_type linoff;
4227 struct internal_lineno lin;
4230 enclosing = xcoff_section_data (abfd, o)->enclosing;
4231 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4232 if (oline != enclosing)
4234 if (bfd_seek (input_bfd,
4235 enclosing->line_filepos,
4237 || (bfd_read (finfo->linenos, linesz,
4238 enc_count, input_bfd)
4239 != linesz * enc_count))
4244 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4245 - enclosing->line_filepos);
4247 bfd_coff_swap_lineno_in (input_bfd,
4248 (PTR) (finfo->linenos + linoff),
4251 || ((bfd_size_type) lin.l_addr.l_symndx
4255 obj_coff_external_syms (input_bfd)))
4257 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4260 bfd_byte *linpend, *linp;
4262 bfd_size_type count;
4264 lin.l_addr.l_symndx = *indexp;
4265 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4266 (PTR) (finfo->linenos
4269 linpend = (finfo->linenos
4270 + enc_count * linesz);
4271 offset = (o->output_section->vma
4274 for (linp = finfo->linenos + linoff + linesz;
4278 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4280 if (lin.l_lnno == 0)
4282 lin.l_addr.l_paddr += offset;
4283 bfd_coff_swap_lineno_out (output_bfd,
4288 count = (linp - (finfo->linenos + linoff)) / linesz;
4290 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4291 (o->output_section->line_filepos
4292 + o->output_section->lineno_count * linesz);
4294 if (bfd_seek (output_bfd,
4295 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4297 || (bfd_write (finfo->linenos + linoff,
4298 linesz, count, output_bfd)
4302 o->output_section->lineno_count += count;
4306 struct internal_syment *iisp, *iispend;
4310 /* Update any C_BINCL or C_EINCL symbols
4311 that refer to a line number in the
4312 range we just output. */
4313 iisp = finfo->internal_syms;
4315 + obj_raw_syment_count (input_bfd));
4316 iindp = finfo->sym_indices;
4317 oos = finfo->outsyms;
4318 while (iisp < iispend)
4320 if ((iisp->n_sclass == C_BINCL
4321 || iisp->n_sclass == C_EINCL)
4322 && ((bfd_size_type) iisp->n_value
4323 >= enclosing->line_filepos + linoff)
4324 && ((bfd_size_type) iisp->n_value
4325 < (enclosing->line_filepos
4326 + enc_count * linesz)))
4328 struct internal_syment iis;
4330 bfd_coff_swap_sym_in (output_bfd,
4335 - enclosing->line_filepos
4337 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4338 bfd_coff_swap_sym_out (output_bfd,
4344 iisp += iisp->n_numaux + 1;
4345 iindp += iisp->n_numaux + 1;
4346 oos += (iisp->n_numaux + 1) * osymesz;
4353 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4354 isymp->n_sclass, i, isymp->n_numaux,
4366 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4367 symbol will be the first symbol in the next input file. In the
4368 normal case, this will save us from writing out the C_FILE symbol
4370 if (finfo->last_file_index != -1
4371 && (bfd_size_type) finfo->last_file_index >= syment_base)
4373 finfo->last_file.n_value = output_index;
4374 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
4375 (PTR) (finfo->outsyms
4376 + ((finfo->last_file_index - syment_base)
4380 /* Write the modified symbols to the output file. */
4381 if (outsym > finfo->outsyms)
4383 if (bfd_seek (output_bfd,
4384 obj_sym_filepos (output_bfd) + syment_base * osymesz,
4386 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
4388 != (bfd_size_type) (outsym - finfo->outsyms)))
4391 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4392 + (outsym - finfo->outsyms) / osymesz)
4395 obj_raw_syment_count (output_bfd) = output_index;
4398 /* Don't let the linker relocation routines discard the symbols. */
4399 keep_syms = obj_coff_keep_syms (input_bfd);
4400 obj_coff_keep_syms (input_bfd) = true;
4402 /* Relocate the contents of each section. */
4403 for (o = input_bfd->sections; o != NULL; o = o->next)
4407 if ((o->flags & SEC_HAS_CONTENTS) == 0
4408 || o->_raw_size == 0
4409 || (o->flags & SEC_IN_MEMORY) != 0)
4412 /* We have set filepos correctly for the sections we created to
4413 represent csects, so bfd_get_section_contents should work. */
4414 if (coff_section_data (input_bfd, o) != NULL
4415 && coff_section_data (input_bfd, o)->contents != NULL)
4416 contents = coff_section_data (input_bfd, o)->contents;
4419 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4420 (file_ptr) 0, o->_raw_size))
4422 contents = finfo->contents;
4425 if ((o->flags & SEC_RELOC) != 0)
4428 struct internal_reloc *internal_relocs;
4429 struct internal_reloc *irel;
4431 struct internal_reloc *irelend;
4432 struct xcoff_link_hash_entry **rel_hash;
4435 /* Read in the relocs. */
4436 target_index = o->output_section->target_index;
4437 internal_relocs = (xcoff_read_internal_relocs
4438 (input_bfd, o, false, finfo->external_relocs,
4440 (finfo->section_info[target_index].relocs
4441 + o->output_section->reloc_count)));
4442 if (internal_relocs == NULL)
4445 /* Call processor specific code to relocate the section
4447 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4451 finfo->internal_syms,
4452 xcoff_data (input_bfd)->csects))
4455 offset = o->output_section->vma + o->output_offset - o->vma;
4456 irel = internal_relocs;
4457 irelend = irel + o->reloc_count;
4458 rel_hash = (finfo->section_info[target_index].rel_hashes
4459 + o->output_section->reloc_count);
4460 for (; irel < irelend; irel++, rel_hash++)
4462 struct xcoff_link_hash_entry *h = NULL;
4463 struct internal_ldrel ldrel;
4467 /* Adjust the reloc address and symbol index. */
4469 irel->r_vaddr += offset;
4471 r_symndx = irel->r_symndx;
4475 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4477 && (irel->r_type == R_TOC
4478 || irel->r_type == R_GL
4479 || irel->r_type == R_TCL
4480 || irel->r_type == R_TRL
4481 || irel->r_type == R_TRLA))
4483 /* This is a TOC relative reloc with a symbol
4484 attached. The symbol should be the one which
4485 this reloc is for. We want to make this
4486 reloc against the TOC address of the symbol,
4487 not the symbol itself. */
4488 BFD_ASSERT (h->toc_section != NULL);
4489 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4490 if (h->u.toc_indx != -1)
4491 irel->r_symndx = h->u.toc_indx;
4494 struct xcoff_toc_rel_hash *n;
4495 struct xcoff_link_section_info *si;
4497 n = ((struct xcoff_toc_rel_hash *)
4498 bfd_alloc (finfo->output_bfd,
4499 sizeof (struct xcoff_toc_rel_hash)));
4502 bfd_set_error (bfd_error_no_memory);
4505 si = finfo->section_info + target_index;
4506 n->next = si->toc_rel_hashes;
4509 si->toc_rel_hashes = n;
4514 /* This is a global symbol. */
4516 irel->r_symndx = h->indx;
4519 /* This symbol is being written at the end
4520 of the file, and we do not yet know the
4521 symbol index. We save the pointer to the
4522 hash table entry in the rel_hash list.
4523 We set the indx field to -2 to indicate
4524 that this symbol must not be stripped. */
4533 indx = finfo->sym_indices[r_symndx];
4537 struct internal_syment *is;
4539 /* Relocations against a TC0 TOC anchor are
4540 automatically transformed to be against
4541 the TOC anchor in the output file. */
4542 is = finfo->internal_syms + r_symndx;
4543 if (is->n_sclass == C_HIDEXT
4544 && is->n_numaux > 0)
4547 union internal_auxent aux;
4551 obj_coff_external_syms (input_bfd))
4552 + ((r_symndx + is->n_numaux)
4554 bfd_coff_swap_aux_in (input_bfd, auxptr,
4555 is->n_type, is->n_sclass,
4559 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4560 && aux.x_csect.x_smclas == XMC_TC0)
4561 indx = finfo->toc_symindx;
4566 irel->r_symndx = indx;
4569 struct internal_syment *is;
4571 char buf[SYMNMLEN + 1];
4573 /* This reloc is against a symbol we are
4574 stripping. It would be possible to handle
4575 this case, but I don't think it's worth it. */
4576 is = finfo->internal_syms + r_symndx;
4578 name = (_bfd_coff_internal_syment_name
4579 (input_bfd, is, buf));
4583 if (! ((*finfo->info->callbacks->unattached_reloc)
4584 (finfo->info, name, input_bfd, o,
4591 switch (irel->r_type)
4595 || h->root.type == bfd_link_hash_defined
4596 || h->root.type == bfd_link_hash_defweak
4597 || h->root.type == bfd_link_hash_common)
4604 /* This reloc needs to be copied into the .loader
4606 ldrel.l_vaddr = irel->r_vaddr;
4608 ldrel.l_symndx = -1;
4610 || (h->root.type == bfd_link_hash_defined
4611 || h->root.type == bfd_link_hash_defweak
4612 || h->root.type == bfd_link_hash_common))
4617 sec = xcoff_data (input_bfd)->csects[r_symndx];
4618 else if (h->root.type == bfd_link_hash_common)
4619 sec = h->root.u.c.p->section;
4621 sec = h->root.u.def.section;
4622 sec = sec->output_section;
4624 if (strcmp (sec->name, ".text") == 0)
4626 else if (strcmp (sec->name, ".data") == 0)
4628 else if (strcmp (sec->name, ".bss") == 0)
4632 (*_bfd_error_handler)
4633 ("%s: loader reloc in unrecognized section `%s'",
4634 bfd_get_filename (input_bfd),
4636 bfd_set_error (bfd_error_nonrepresentable_section);
4644 (*_bfd_error_handler)
4645 ("%s: `%s' in loader reloc but not loader sym",
4646 bfd_get_filename (input_bfd),
4647 h->root.root.string);
4648 bfd_set_error (bfd_error_bad_value);
4651 ldrel.l_symndx = h->ldindx;
4653 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4654 ldrel.l_rsecnm = o->output_section->target_index;
4655 if (xcoff_hash_table (finfo->info)->textro
4656 && strcmp (o->output_section->name, ".text") == 0)
4658 (*_bfd_error_handler)
4659 ("%s: loader reloc in read-only section %s",
4660 bfd_get_filename (input_bfd),
4661 bfd_get_section_name (finfo->output_bfd,
4662 o->output_section));
4663 bfd_set_error (bfd_error_invalid_operation);
4666 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4668 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4677 /* We should never need a .loader reloc for a TOC
4683 o->output_section->reloc_count += o->reloc_count;
4686 /* Write out the modified section contents. */
4687 if (! bfd_set_section_contents (output_bfd, o->output_section,
4688 contents, o->output_offset,
4689 (o->_cooked_size != 0
4695 obj_coff_keep_syms (input_bfd) = keep_syms;
4697 if (! finfo->info->keep_memory)
4699 if (! _bfd_coff_free_symbols (input_bfd))
4709 /* Write out a non-XCOFF global symbol. */
4712 xcoff_write_global_symbol (h, p)
4713 struct xcoff_link_hash_entry *h;
4716 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4719 struct internal_syment isym;
4720 union internal_auxent aux;
4722 output_bfd = finfo->output_bfd;
4724 /* If this symbol was garbage collected, just skip it. */
4725 if (xcoff_hash_table (finfo->info)->gc
4726 && (h->flags & XCOFF_MARK) == 0)
4729 /* If we need a .loader section entry, write it out. */
4730 if (h->ldsym != NULL)
4732 struct internal_ldsym *ldsym;
4737 if (h->root.type == bfd_link_hash_undefined
4738 || h->root.type == bfd_link_hash_undefweak)
4741 ldsym->l_scnum = N_UNDEF;
4742 ldsym->l_smtype = XTY_ER;
4743 impbfd = h->root.u.undef.abfd;
4745 else if (h->root.type == bfd_link_hash_defined
4746 || h->root.type == bfd_link_hash_defweak)
4750 sec = h->root.u.def.section;
4751 ldsym->l_value = (sec->output_section->vma
4752 + sec->output_offset
4753 + h->root.u.def.value);
4754 ldsym->l_scnum = sec->output_section->target_index;
4755 ldsym->l_smtype = XTY_SD;
4756 impbfd = sec->owner;
4761 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4762 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4763 || (h->flags & XCOFF_IMPORT) != 0)
4764 ldsym->l_smtype |= L_IMPORT;
4765 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4766 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4767 || (h->flags & XCOFF_EXPORT) != 0)
4768 ldsym->l_smtype |= L_EXPORT;
4769 if ((h->flags & XCOFF_ENTRY) != 0)
4770 ldsym->l_smtype |= L_ENTRY;
4772 ldsym->l_smclas = h->smclas;
4774 if (ldsym->l_ifile == (bfd_size_type) -1)
4776 else if (ldsym->l_ifile == 0)
4778 if ((ldsym->l_smtype & L_IMPORT) == 0)
4780 else if (impbfd == NULL)
4784 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4785 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4791 BFD_ASSERT (h->ldindx >= 0);
4792 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4793 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4797 /* If this symbol needs global linkage code, write it out. */
4798 if (h->root.type == bfd_link_hash_defined
4799 && (h->root.u.def.section
4800 == xcoff_hash_table (finfo->info)->linkage_section))
4806 p = h->root.u.def.section->contents + h->root.u.def.value;
4808 /* The first instruction in the global linkage code loads a
4809 specific TOC element. */
4810 tocoff = (h->descriptor->toc_section->output_section->vma
4811 + h->descriptor->toc_section->output_offset
4812 - xcoff_data (output_bfd)->toc);
4813 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4814 tocoff += h->descriptor->u.toc_offset;
4815 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4817 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
4819 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
4822 /* If we created a TOC entry for this symbol, write out the required
4824 if ((h->flags & XCOFF_SET_TOC) != 0)
4829 struct internal_reloc *irel;
4830 struct internal_ldrel ldrel;
4832 tocsec = h->toc_section;
4833 osec = tocsec->output_section;
4834 oindx = osec->target_index;
4835 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4836 irel->r_vaddr = (osec->vma
4837 + tocsec->output_offset
4840 irel->r_symndx = h->indx;
4844 irel->r_symndx = obj_raw_syment_count (output_bfd);
4846 irel->r_type = R_POS;
4848 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4849 ++osec->reloc_count;
4851 BFD_ASSERT (h->ldindx >= 0);
4852 ldrel.l_vaddr = irel->r_vaddr;
4853 ldrel.l_symndx = h->ldindx;
4854 ldrel.l_rtype = (31 << 8) | R_POS;
4855 ldrel.l_rsecnm = oindx;
4856 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4864 && (finfo->info->strip == strip_all
4865 || (finfo->info->strip == strip_some
4866 && (bfd_hash_lookup (finfo->info->keep_hash,
4867 h->root.root.string, false, false)
4872 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
4875 outsym = finfo->outsyms;
4877 memset (&aux, 0, sizeof aux);
4879 h->indx = obj_raw_syment_count (output_bfd);
4881 if (strlen (h->root.root.string) <= SYMNMLEN)
4882 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
4889 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4891 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
4893 if (indx == (bfd_size_type) -1)
4895 isym._n._n_n._n_zeroes = 0;
4896 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4899 if (h->root.type == bfd_link_hash_undefined
4900 || h->root.type == bfd_link_hash_undefweak)
4903 isym.n_scnum = N_UNDEF;
4904 isym.n_sclass = C_EXT;
4905 aux.x_csect.x_smtyp = XTY_ER;
4907 else if (h->root.type == bfd_link_hash_defined
4908 || h->root.type == bfd_link_hash_defweak)
4910 struct xcoff_link_size_list *l;
4912 isym.n_value = (h->root.u.def.section->output_section->vma
4913 + h->root.u.def.section->output_offset
4914 + h->root.u.def.value);
4915 isym.n_scnum = h->root.u.def.section->output_section->target_index;
4916 isym.n_sclass = C_HIDEXT;
4917 aux.x_csect.x_smtyp = XTY_SD;
4919 if ((h->flags & XCOFF_HAS_SIZE) != 0)
4921 for (l = xcoff_hash_table (finfo->info)->size_list;
4927 aux.x_csect.x_scnlen.l = l->size;
4933 else if (h->root.type == bfd_link_hash_common)
4935 isym.n_value = (h->root.u.c.p->section->output_section->vma
4936 + h->root.u.c.p->section->output_offset);
4937 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
4938 isym.n_sclass = C_EXT;
4939 aux.x_csect.x_smtyp = XTY_CM;
4940 aux.x_csect.x_scnlen.l = h->root.u.c.size;
4945 isym.n_type = T_NULL;
4948 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4949 outsym += bfd_coff_symesz (output_bfd);
4951 aux.x_csect.x_smclas = h->smclas;
4953 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
4955 outsym += bfd_coff_auxesz (output_bfd);
4957 if (h->root.type == bfd_link_hash_defined
4958 || h->root.type == bfd_link_hash_defweak)
4960 /* We just output an SD symbol. Now output an LD symbol. */
4964 isym.n_sclass = C_EXT;
4965 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4966 outsym += bfd_coff_symesz (output_bfd);
4968 aux.x_csect.x_smtyp = XTY_LD;
4969 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
4971 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
4973 outsym += bfd_coff_auxesz (output_bfd);
4976 if (bfd_seek (output_bfd,
4977 (obj_sym_filepos (output_bfd)
4978 + (obj_raw_syment_count (output_bfd)
4979 * bfd_coff_symesz (output_bfd))),
4981 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
4982 != (bfd_size_type) (outsym - finfo->outsyms)))
4984 obj_raw_syment_count (output_bfd) +=
4985 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4990 /* Handle a link order which is supposed to generate a reloc. */
4993 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
4995 struct xcoff_final_link_info *finfo;
4996 asection *output_section;
4997 struct bfd_link_order *link_order;
4999 reloc_howto_type *howto;
5000 struct xcoff_link_hash_entry *h;
5004 struct internal_reloc *irel;
5005 struct xcoff_link_hash_entry **rel_hash_ptr;
5006 struct internal_ldrel ldrel;
5008 if (link_order->type == bfd_section_reloc_link_order)
5010 /* We need to somehow locate a symbol in the right section. The
5011 symbol must either have a value of zero, or we must adjust
5012 the addend by the value of the symbol. FIXME: Write this
5013 when we need it. The old linker couldn't handle this anyhow. */
5017 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5020 bfd_set_error (bfd_error_bad_value);
5024 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
5025 link_order->u.reloc.p->u.name,
5026 false, false, true);
5029 if (! ((*finfo->info->callbacks->unattached_reloc)
5030 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5031 (asection *) NULL, (bfd_vma) 0)))
5036 if (h->root.type == bfd_link_hash_common)
5038 hsec = h->root.u.c.p->section;
5041 else if (h->root.type == bfd_link_hash_defined
5042 || h->root.type == bfd_link_hash_defweak)
5044 hsec = h->root.u.def.section;
5045 hval = h->root.u.def.value;
5053 addend = link_order->u.reloc.p->addend;
5055 addend += (hsec->output_section->vma
5056 + hsec->output_offset
5063 bfd_reloc_status_type rstat;
5066 size = bfd_get_reloc_size (howto);
5067 buf = (bfd_byte *) bfd_zmalloc (size);
5070 bfd_set_error (bfd_error_no_memory);
5074 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5080 case bfd_reloc_outofrange:
5082 case bfd_reloc_overflow:
5083 if (! ((*finfo->info->callbacks->reloc_overflow)
5084 (finfo->info, link_order->u.reloc.p->u.name,
5085 howto->name, addend, (bfd *) NULL, (asection *) NULL,
5093 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5094 (file_ptr) link_order->offset, size);
5100 /* Store the reloc information in the right place. It will get
5101 swapped and written out at the end of the final_link routine. */
5103 irel = (finfo->section_info[output_section->target_index].relocs
5104 + output_section->reloc_count);
5105 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5106 + output_section->reloc_count);
5108 memset (irel, 0, sizeof (struct internal_reloc));
5109 *rel_hash_ptr = NULL;
5111 irel->r_vaddr = output_section->vma + link_order->offset;
5114 irel->r_symndx = h->indx;
5117 /* Set the index to -2 to force this symbol to get written out. */
5123 irel->r_type = howto->type;
5124 irel->r_size = howto->bitsize - 1;
5125 if (howto->complain_on_overflow == complain_overflow_signed)
5126 irel->r_size |= 0x80;
5128 ++output_section->reloc_count;
5130 /* Now output the reloc to the .loader section. */
5132 ldrel.l_vaddr = irel->r_vaddr;
5136 const char *secname;
5138 secname = hsec->output_section->name;
5140 if (strcmp (secname, ".text") == 0)
5142 else if (strcmp (secname, ".data") == 0)
5144 else if (strcmp (secname, ".bss") == 0)
5148 (*_bfd_error_handler)
5149 ("%s: loader reloc in unrecognized section `%s'",
5150 bfd_get_filename (output_bfd), secname);
5151 bfd_set_error (bfd_error_nonrepresentable_section);
5159 (*_bfd_error_handler)
5160 ("%s: `%s' in loader reloc but not loader sym",
5161 bfd_get_filename (output_bfd),
5162 h->root.root.string);
5163 bfd_set_error (bfd_error_bad_value);
5166 ldrel.l_symndx = h->ldindx;
5169 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5170 ldrel.l_rsecnm = output_section->target_index;
5171 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5177 /* Sort relocs by VMA. This is called via qsort. */
5180 xcoff_sort_relocs (p1, p2)
5184 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5185 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5187 if (r1->r_vaddr > r2->r_vaddr)
5189 else if (r1->r_vaddr < r2->r_vaddr)
5195 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5196 This is currently the only processor which uses XCOFF; I hope that
5197 will never change. */
5200 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
5201 input_section, contents, relocs, syms,
5204 struct bfd_link_info *info;
5206 asection *input_section;
5208 struct internal_reloc *relocs;
5209 struct internal_syment *syms;
5210 asection **sections;
5212 struct internal_reloc *rel;
5213 struct internal_reloc *relend;
5216 relend = rel + input_section->reloc_count;
5217 for (; rel < relend; rel++)
5220 struct xcoff_link_hash_entry *h;
5221 struct internal_syment *sym;
5224 struct reloc_howto_struct howto;
5225 bfd_reloc_status_type rstat;
5227 /* Relocation type R_REF is a special relocation type which is
5228 merely used to prevent garbage collection from occurring for
5229 the csect including the symbol which it references. */
5230 if (rel->r_type == R_REF)
5233 symndx = rel->r_symndx;
5243 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
5244 sym = syms + symndx;
5245 addend = - sym->n_value;
5248 /* We build the howto information on the fly. */
5250 howto.type = rel->r_type;
5251 howto.rightshift = 0;
5253 howto.bitsize = (rel->r_size & 0x1f) + 1;
5254 howto.pc_relative = false;
5256 if ((rel->r_size & 0x80) != 0)
5257 howto.complain_on_overflow = complain_overflow_signed;
5259 howto.complain_on_overflow = complain_overflow_bitfield;
5260 howto.special_function = NULL;
5261 howto.name = "internal";
5262 howto.partial_inplace = true;
5263 if (howto.bitsize == 32)
5264 howto.src_mask = howto.dst_mask = 0xffffffff;
5267 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
5268 if (howto.bitsize == 16)
5271 howto.pcrel_offset = false;
5281 sec = bfd_abs_section_ptr;
5286 sec = sections[symndx];
5287 val = (sec->output_section->vma
5288 + sec->output_offset
5295 if (h->root.type == bfd_link_hash_defined
5296 || h->root.type == bfd_link_hash_defweak)
5300 sec = h->root.u.def.section;
5301 val = (h->root.u.def.value
5302 + sec->output_section->vma
5303 + sec->output_offset);
5305 else if (h->root.type == bfd_link_hash_common)
5309 sec = h->root.u.c.p->section;
5310 val = (sec->output_section->vma
5311 + sec->output_offset);
5313 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
5314 || (h->flags & XCOFF_IMPORT) != 0)
5316 /* Every symbol in a shared object is defined somewhere. */
5319 else if (! info->relocateable
5322 if (! ((*info->callbacks->undefined_symbol)
5323 (info, h->root.root.string, input_bfd, input_section,
5324 rel->r_vaddr - input_section->vma)))
5329 /* I took the relocation type definitions from two documents:
5330 the PowerPC AIX Version 4 Application Binary Interface, First
5331 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5332 32-Bit Hardware Implementation (June 30, 1994). Differences
5333 between the documents are noted below. */
5335 switch (rel->r_type)
5340 /* These relocs are defined by the PowerPC ABI to be
5341 relative branches which use half of the difference
5342 between the symbol and the program counter. I can't
5343 quite figure out when this is useful. These relocs are
5344 not defined by the PowerOpen ABI. */
5346 (*_bfd_error_handler)
5347 ("%s: unsupported relocation type 0x%02x",
5348 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
5349 bfd_set_error (bfd_error_bad_value);
5352 /* Simple positive relocation. */
5355 /* Simple negative relocation. */
5359 /* Simple PC relative relocation. */
5360 howto.pc_relative = true;
5363 /* TOC relative relocation. The value in the instruction in
5364 the input file is the offset from the input file TOC to
5365 the desired location. We want the offset from the final
5366 TOC to the desired location. We have:
5371 so we must change insn by on - in.
5374 /* Global linkage relocation. The value of this relocation
5375 is the address of the entry in the TOC section. */
5377 /* Local object TOC address. I can't figure out the
5378 difference between this and case R_GL. */
5380 /* TOC relative relocation. A TOC relative load instruction
5381 which may be changed to a load address instruction.
5382 FIXME: We don't currently implement this optimization. */
5384 /* TOC relative relocation. This is a TOC relative load
5385 address instruction which may be changed to a load
5386 instruction. FIXME: I don't know if this is the correct
5388 if (h != NULL && h->toc_section == NULL)
5390 (*_bfd_error_handler)
5391 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5392 bfd_get_filename (input_bfd), rel->r_vaddr,
5393 h->root.root.string);
5394 bfd_set_error (bfd_error_bad_value);
5399 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5400 val = (h->toc_section->output_section->vma
5401 + h->toc_section->output_offset);
5403 val = ((val - xcoff_data (output_bfd)->toc)
5404 - (sym->n_value - xcoff_data (input_bfd)->toc));
5408 /* Absolute branch. We don't want to mess with the lower
5409 two bits of the instruction. */
5411 /* The PowerPC ABI defines this as an absolute call which
5412 may be modified to become a relative call. The PowerOpen
5413 ABI does not define this relocation type. */
5415 /* Absolute branch which may be modified to become a
5418 /* The PowerPC ABI defines this as an absolute branch to a
5419 fixed address which may be modified to an absolute branch
5420 to a symbol. The PowerOpen ABI does not define this
5423 /* The PowerPC ABI defines this as an absolute branch to a
5424 fixed address which may be modified to a relative branch.
5425 The PowerOpen ABI does not define this relocation type. */
5426 howto.src_mask &= ~3;
5427 howto.dst_mask = howto.src_mask;
5430 /* Relative branch. We don't want to mess with the lower
5431 two bits of the instruction. */
5433 /* The PowerPC ABI defines this as a relative call which may
5434 be modified to become an absolute call. The PowerOpen
5435 ABI does not define this relocation type. */
5437 /* A relative branch which may be modified to become an
5438 absolute branch. FIXME: We don't implement this,
5439 although we should for symbols of storage mapping class
5441 howto.pc_relative = true;
5442 howto.src_mask &= ~3;
5443 howto.dst_mask = howto.src_mask;
5446 /* The PowerPC AIX ABI describes this as a load which may be
5447 changed to a load address. The PowerOpen ABI says this
5448 is the same as case R_POS. */
5451 /* The PowerPC AIX ABI describes this as a load address
5452 which may be changed to a load. The PowerOpen ABI says
5453 this is the same as R_POS. */
5457 /* If we see an R_BR or R_RBR reloc which is jumping to global
5458 linkage code, and it is followed by an appropriate cror nop
5459 instruction, we replace the cror with lwz r2,20(r1). This
5460 restores the TOC after the glink code. Contrariwise, if the
5461 call is followed by a lwz r2,20(r1), but the call is not
5462 going to global linkage code, we can replace the load with a
5464 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
5466 && h->root.type == bfd_link_hash_defined
5467 && (rel->r_vaddr - input_section->vma + 8
5468 <= input_section->_cooked_size))
5473 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
5474 next = bfd_get_32 (input_bfd, pnext);
5475 if (h->smclas == XMC_GL)
5477 if (next == 0x4def7b82 /* cror 15,15,15 */
5478 || next == 0x4ffffb82) /* cror 31,31,31 */
5479 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
5483 if (next == 0x80410014) /* lwz r1,20(r1) */
5484 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
5488 /* A PC relative reloc includes the section address. */
5489 if (howto.pc_relative)
5490 addend += input_section->vma;
5492 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
5494 rel->r_vaddr - input_section->vma,
5503 case bfd_reloc_overflow:
5506 char buf[SYMNMLEN + 1];
5507 char howto_name[10];
5512 name = h->root.root.string;
5515 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
5519 sprintf (howto_name, "0x%02x", rel->r_type);
5521 if (! ((*info->callbacks->reloc_overflow)
5522 (info, name, howto_name, (bfd_vma) 0, input_bfd,
5523 input_section, rel->r_vaddr - input_section->vma)))