1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "xcoff-powermac") == 0)
40 /* Get the XCOFF hash table entries for a BFD. */
41 #define obj_xcoff_sym_hashes(bfd) \
42 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
44 /* XCOFF relocation types. These probably belong in a header file
45 somewhere. The relocations are described in the function
46 _bfd_ppc_xcoff_relocate_section in this file. */
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
71 /* The first word of global linkage code. This must be modified by
72 filling in the correct TOC offset. */
74 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
76 /* The remaining words of global linkage code. */
78 static unsigned long xcoff_glink_code[] =
80 0x90410014, /* stw r2,20(r1) */
81 0x800c0000, /* lwz r0,0(r12) */
82 0x804c0004, /* lwz r2,4(r12) */
83 0x7c0903a6, /* mtctr r0 */
84 0x4e800420, /* bctr */
85 0x0, /* start of traceback table */
86 0x000c8000, /* traceback table */
87 0x0 /* traceback table */
90 #define XCOFF_GLINK_SIZE \
91 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
93 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
94 This flag will only be used on input sections. */
96 #define SEC_MARK (SEC_ROM)
98 /* The ldhdr structure. This appears at the start of the .loader
101 struct internal_ldhdr
103 /* The version number: currently always 1. */
104 unsigned long l_version;
105 /* The number of symbol table entries. */
106 bfd_size_type l_nsyms;
107 /* The number of relocation table entries. */
108 bfd_size_type l_nreloc;
109 /* The length of the import file string table. */
110 bfd_size_type l_istlen;
111 /* The number of import files. */
112 bfd_size_type l_nimpid;
113 /* The offset from the start of the .loader section to the first
114 entry in the import file table. */
115 bfd_size_type l_impoff;
116 /* The length of the string table. */
117 bfd_size_type l_stlen;
118 /* The offset from the start of the .loader section to the first
119 entry in the string table. */
120 bfd_size_type l_stoff;
123 struct external_ldhdr
125 bfd_byte l_version[4];
127 bfd_byte l_nreloc[4];
128 bfd_byte l_istlen[4];
129 bfd_byte l_nimpid[4];
130 bfd_byte l_impoff[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
140 struct internal_ldsym
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name[SYMNMLEN];
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
155 /* The symbol value. */
157 /* The symbol section number. */
159 /* The symbol type and flags. */
161 /* The symbol storage class. */
163 /* The import file ID. */
164 bfd_size_type l_ifile;
165 /* Offset to the parameter type check string. */
166 bfd_size_type l_parm;
169 struct external_ldsym
173 bfd_byte _l_name[SYMNMLEN];
176 bfd_byte _l_zeroes[4];
177 bfd_byte _l_offset[4];
182 bfd_byte l_smtype[1];
183 bfd_byte l_smclas[1];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
196 #define L_ENTRY (0x20)
197 /* Exported symbol. */
198 #define L_EXPORT (0x10)
200 /* The ldrel structure. This is used to represent a reloc in the
203 struct internal_ldrel
205 /* The reloc address. */
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx;
209 /* The relocation type and size. */
211 /* The section number this relocation applies to. */
215 struct external_ldrel
218 bfd_byte l_symndx[4];
220 bfd_byte l_rsecnm[2];
223 #define LDRELSZ (2 * 4 + 2 * 2)
225 /* The list of import files. */
227 struct xcoff_import_file
229 /* The next entry in the list. */
230 struct xcoff_import_file *next;
235 /* The member name. */
239 /* An entry in the XCOFF linker hash table. */
241 struct xcoff_link_hash_entry
243 struct bfd_link_hash_entry root;
245 /* Symbol index in output file. Set to -1 initially. Set to -2 if
246 there is a reloc against this symbol. */
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection *toc_section;
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
258 /* If the TOC entry comes from an input file, this is set to the
259 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 referenced by a dynamic object. */
280 #define XCOFF_REF_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_out
420 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
421 static void xcoff_swap_ldsym_out
422 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
423 static void xcoff_swap_ldrel_out
424 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
425 static struct bfd_hash_entry *xcoff_link_hash_newfunc
426 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
427 static struct internal_reloc *xcoff_read_internal_relocs
428 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
429 struct internal_reloc *));
430 static boolean xcoff_link_add_object_symbols
431 PARAMS ((bfd *, struct bfd_link_info *));
432 static boolean xcoff_link_check_archive_element
433 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
434 static boolean xcoff_link_check_ar_symbols
435 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
436 static bfd_size_type xcoff_find_reloc
437 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
438 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
439 static boolean xcoff_link_add_dynamic_symbols
440 PARAMS ((bfd *, struct bfd_link_info *));
441 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
442 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
443 static boolean xcoff_build_ldsyms
444 PARAMS ((struct xcoff_link_hash_entry *, PTR));
445 static boolean xcoff_link_input_bfd
446 PARAMS ((struct xcoff_final_link_info *, bfd *));
447 static boolean xcoff_write_global_symbol
448 PARAMS ((struct xcoff_link_hash_entry *, PTR));
449 static boolean xcoff_reloc_link_order
450 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
451 struct bfd_link_order *));
452 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
454 /* Routines to swap information in the XCOFF .loader section. We only
455 need to swap this information out, not in. I believe that only the
456 loader needs to swap this information in. If we ever need to write
457 an XCOFF loader, this stuff will need to be moved to another file
458 shared by the linker (which XCOFF calls the ``binder'') and the
461 /* Swap out the ldhdr structure. */
464 xcoff_swap_ldhdr_out (abfd, src, dst)
466 const struct internal_ldhdr *src;
467 struct external_ldhdr *dst;
469 bfd_put_32 (abfd, src->l_version, dst->l_version);
470 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
471 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
472 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
473 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
474 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
475 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
476 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
479 /* Swap out the ldsym structure. */
482 xcoff_swap_ldsym_out (abfd, src, dst)
484 const struct internal_ldsym *src;
485 struct external_ldsym *dst;
487 if (src->_l._l_l._l_zeroes != 0)
488 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
491 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
492 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
494 bfd_put_32 (abfd, src->l_value, dst->l_value);
495 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
496 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
497 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
498 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
499 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
502 /* Swap out the ldrel structure. */
505 xcoff_swap_ldrel_out (abfd, src, dst)
507 const struct internal_ldrel *src;
508 struct external_ldrel *dst;
510 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
511 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
512 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
513 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
516 /* Routine to create an entry in an XCOFF link hash table. */
518 static struct bfd_hash_entry *
519 xcoff_link_hash_newfunc (entry, table, string)
520 struct bfd_hash_entry *entry;
521 struct bfd_hash_table *table;
524 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
526 /* Allocate the structure if it has not already been allocated by a
528 if (ret == (struct xcoff_link_hash_entry *) NULL)
529 ret = ((struct xcoff_link_hash_entry *)
530 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
531 if (ret == (struct xcoff_link_hash_entry *) NULL)
533 bfd_set_error (bfd_error_no_memory);
534 return (struct bfd_hash_entry *) ret;
537 /* Call the allocation method of the superclass. */
538 ret = ((struct xcoff_link_hash_entry *)
539 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
543 /* Set local fields. */
545 ret->toc_section = NULL;
546 ret->u.toc_indx = -1;
547 ret->descriptor = NULL;
551 ret->smclas = XMC_UA;
554 return (struct bfd_hash_entry *) ret;
557 /* Create a XCOFF link hash table. */
559 struct bfd_link_hash_table *
560 _bfd_xcoff_bfd_link_hash_table_create (abfd)
563 struct xcoff_link_hash_table *ret;
565 ret = ((struct xcoff_link_hash_table *)
566 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
567 if (ret == (struct xcoff_link_hash_table *) NULL)
569 bfd_set_error (bfd_error_no_memory);
570 return (struct bfd_link_hash_table *) NULL;
572 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
574 bfd_release (abfd, ret);
575 return (struct bfd_link_hash_table *) NULL;
578 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
579 ret->debug_section = NULL;
580 ret->loader_section = NULL;
581 ret->ldrel_count = 0;
582 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
583 ret->linkage_section = NULL;
584 ret->toc_section = NULL;
590 /* The linker will always generate a full a.out header. We need to
591 record that fact now, before the sizeof_headers routine could be
593 xcoff_data (abfd)->full_aouthdr = true;
598 /* Look up an entry in an XCOFF link hash table. */
600 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
601 ((struct xcoff_link_hash_entry *) \
602 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
605 /* Traverse an XCOFF link hash table. */
607 #define xcoff_link_hash_traverse(table, func, info) \
608 (bfd_link_hash_traverse \
610 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
613 /* Get the XCOFF link hash table from the info structure. This is
616 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
618 /* Read internal relocs for an XCOFF csect. This is a wrapper around
619 _bfd_coff_read_internal_relocs which tries to take advantage of any
620 relocs which may have been cached for the enclosing section. */
622 static struct internal_reloc *
623 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
624 require_internal, internal_relocs)
628 bfd_byte *external_relocs;
629 boolean require_internal;
630 struct internal_reloc *internal_relocs;
632 if (coff_section_data (abfd, sec) != NULL
633 && coff_section_data (abfd, sec)->relocs == NULL
634 && xcoff_section_data (abfd, sec) != NULL)
638 enclosing = xcoff_section_data (abfd, sec)->enclosing;
640 if (enclosing != NULL
641 && (coff_section_data (abfd, enclosing) == NULL
642 || coff_section_data (abfd, enclosing)->relocs == NULL)
644 && enclosing->reloc_count > 0)
646 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
647 external_relocs, false,
648 (struct internal_reloc *) NULL)
653 if (enclosing != NULL
654 && coff_section_data (abfd, enclosing) != NULL
655 && coff_section_data (abfd, enclosing)->relocs != NULL)
659 off = ((sec->rel_filepos - enclosing->rel_filepos)
660 / bfd_coff_relsz (abfd));
661 if (! require_internal)
662 return coff_section_data (abfd, enclosing)->relocs + off;
663 memcpy (internal_relocs,
664 coff_section_data (abfd, enclosing)->relocs + off,
665 sec->reloc_count * sizeof (struct internal_reloc));
666 return internal_relocs;
670 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
671 require_internal, internal_relocs);
674 /* Given an XCOFF BFD, add symbols to the global hash table as
678 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
680 struct bfd_link_info *info;
682 switch (bfd_get_format (abfd))
685 return xcoff_link_add_object_symbols (abfd, info);
687 return (_bfd_generic_link_add_archive_symbols
688 (abfd, info, xcoff_link_check_archive_element));
690 bfd_set_error (bfd_error_wrong_format);
695 /* Add symbols from an XCOFF object file. */
698 xcoff_link_add_object_symbols (abfd, info)
700 struct bfd_link_info *info;
702 if (! _bfd_coff_get_external_symbols (abfd))
704 if (! xcoff_link_add_symbols (abfd, info))
706 if (! info->keep_memory)
708 if (! _bfd_coff_free_symbols (abfd))
714 /* Check a single archive element to see if we need to include it in
715 the link. *PNEEDED is set according to whether this element is
716 needed in the link or not. This is called via
717 _bfd_generic_link_add_archive_symbols. */
720 xcoff_link_check_archive_element (abfd, info, pneeded)
722 struct bfd_link_info *info;
725 if (! _bfd_coff_get_external_symbols (abfd))
728 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
733 if (! xcoff_link_add_symbols (abfd, info))
737 if (! info->keep_memory || ! *pneeded)
739 if (! _bfd_coff_free_symbols (abfd))
746 /* Look through the symbols to see if this object file should be
747 included in the link. */
750 xcoff_link_check_ar_symbols (abfd, info, pneeded)
752 struct bfd_link_info *info;
755 bfd_size_type symesz;
761 symesz = bfd_coff_symesz (abfd);
762 esym = (bfd_byte *) obj_coff_external_syms (abfd);
763 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
764 while (esym < esym_end)
766 struct internal_syment sym;
768 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
770 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
773 char buf[SYMNMLEN + 1];
774 struct bfd_link_hash_entry *h;
776 /* This symbol is externally visible, and is defined by this
779 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
782 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
784 /* We are only interested in symbols that are currently
785 undefined. If a symbol is currently known to be common,
786 XCOFF linkers do not bring in an object file which
787 defines it. We also don't bring in symbols to satisfy
788 undefined references in shared objects. */
789 if (h != (struct bfd_link_hash_entry *) NULL
790 && h->type == bfd_link_hash_undefined)
792 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
799 esym += (sym.n_numaux + 1) * symesz;
802 /* We do not need this object file. */
806 /* Returns the index of reloc in RELOCS with the least address greater
807 than or equal to ADDRESS. The relocs are sorted by address. */
810 xcoff_find_reloc (relocs, count, address)
811 struct internal_reloc *relocs;
815 bfd_size_type min, max, this;
823 /* Do a binary search over (min,max]. */
824 while (min + 1 < max)
828 this = (max + min) / 2;
829 raddr = relocs[this].r_vaddr;
832 else if (raddr < address)
841 if (relocs[min].r_vaddr < address)
845 && relocs[min - 1].r_vaddr == address)
851 /* Add all the symbols from an object file to the hash table.
853 XCOFF is a weird format. A normal XCOFF .o files will have three
854 COFF sections--.text, .data, and .bss--but each COFF section will
855 contain many csects. These csects are described in the symbol
856 table. From the linker's point of view, each csect must be
857 considered a section in its own right. For example, a TOC entry is
858 handled as a small XMC_TC csect. The linker must be able to merge
859 different TOC entries together, which means that it must be able to
860 extract the XMC_TC csects from the .data section of the input .o
863 From the point of view of our linker, this is, of course, a hideous
864 nightmare. We cope by actually creating sections for each csect,
865 and discarding the original sections. We then have to handle the
866 relocation entries carefully, since the only way to tell which
867 csect they belong to is to examine the address. */
870 xcoff_link_add_symbols (abfd, info)
872 struct bfd_link_info *info;
874 unsigned int n_tmask;
875 unsigned int n_btshft;
876 boolean default_copy;
877 bfd_size_type symcount;
878 struct xcoff_link_hash_entry **sym_hash;
879 asection **csect_cache;
880 bfd_size_type linesz;
884 unsigned int csect_index;
885 asection *first_csect;
886 bfd_size_type symesz;
889 struct reloc_info_struct
891 struct internal_reloc *relocs;
894 } *reloc_info = NULL;
896 if ((abfd->flags & DYNAMIC) != 0
897 && ! info->static_link)
898 return xcoff_link_add_dynamic_symbols (abfd, info);
900 n_tmask = coff_data (abfd)->local_n_tmask;
901 n_btshft = coff_data (abfd)->local_n_btshft;
903 /* Define macros so that ISFCN, et. al., macros work correctly. */
904 #define N_TMASK n_tmask
905 #define N_BTSHFT n_btshft
907 /* We need to build a .loader section, so we do it here. This won't
908 work if we're producing an XCOFF output file with no non dynamic
909 XCOFF input files. FIXME. */
910 if (xcoff_hash_table (info)->loader_section == NULL)
914 lsec = bfd_make_section_anyway (abfd, ".loader");
917 xcoff_hash_table (info)->loader_section = lsec;
918 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
920 /* Likewise for the linkage section. */
921 if (xcoff_hash_table (info)->linkage_section == NULL)
925 lsec = bfd_make_section_anyway (abfd, ".gl");
928 xcoff_hash_table (info)->linkage_section = lsec;
929 lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
931 /* Likewise for the TOC section. */
932 if (xcoff_hash_table (info)->toc_section == NULL)
936 tsec = bfd_make_section_anyway (abfd, ".tc");
939 xcoff_hash_table (info)->toc_section = tsec;
940 tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
942 /* Likewise for the .debug section. */
943 if (xcoff_hash_table (info)->debug_section == NULL)
947 dsec = bfd_make_section_anyway (abfd, ".debug");
950 xcoff_hash_table (info)->debug_section = dsec;
951 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
954 if (info->keep_memory)
955 default_copy = false;
959 symcount = obj_raw_syment_count (abfd);
961 /* We keep a list of the linker hash table entries that correspond
962 to each external symbol. */
963 sym_hash = ((struct xcoff_link_hash_entry **)
966 * sizeof (struct xcoff_link_hash_entry *))));
967 if (sym_hash == NULL && symcount != 0)
969 bfd_set_error (bfd_error_no_memory);
972 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
974 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
976 /* Because of the weird stuff we are doing with XCOFF csects, we can
977 not easily determine which section a symbol is in, so we store
978 the information in the tdata for the input file. */
979 csect_cache = ((asection **)
980 bfd_alloc (abfd, symcount * sizeof (asection *)));
981 if (csect_cache == NULL && symcount != 0)
983 bfd_set_error (bfd_error_no_memory);
986 xcoff_data (abfd)->csects = csect_cache;
987 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
989 /* While splitting sections into csects, we need to assign the
990 relocs correctly. The relocs and the csects must both be in
991 order by VMA within a given section, so we handle this by
992 scanning along the relocs as we process the csects. We index
993 into reloc_info using the section target_index. */
994 reloc_info = ((struct reloc_info_struct *)
995 malloc ((abfd->section_count + 1)
996 * sizeof (struct reloc_info_struct)));
997 if (reloc_info == NULL)
999 bfd_set_error (bfd_error_no_memory);
1002 memset ((PTR) reloc_info, 0,
1003 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1005 /* Read in the relocs and line numbers for each section. */
1006 linesz = bfd_coff_linesz (abfd);
1007 for (sub = abfd->sections; sub != NULL; sub = sub->next)
1009 if ((sub->flags & SEC_RELOC) != 0)
1011 reloc_info[sub->target_index].relocs =
1012 xcoff_read_internal_relocs (abfd, sub, true, (bfd_byte *) NULL,
1013 false, (struct internal_reloc *) NULL);
1014 reloc_info[sub->target_index].csects =
1015 (asection **) malloc (sub->reloc_count * sizeof (asection *));
1016 if (reloc_info[sub->target_index].csects == NULL)
1018 bfd_set_error (bfd_error_no_memory);
1021 memset (reloc_info[sub->target_index].csects, 0,
1022 sub->reloc_count * sizeof (asection *));
1025 if ((info->strip == strip_none || info->strip == strip_some)
1026 && sub->lineno_count > 0)
1030 linenos = (bfd_byte *) malloc (sub->lineno_count * linesz);
1031 if (linenos == NULL)
1033 bfd_set_error (bfd_error_no_memory);
1036 reloc_info[sub->target_index].linenos = linenos;
1037 if (bfd_seek (abfd, sub->line_filepos, SEEK_SET) != 0
1038 || (bfd_read (linenos, linesz, sub->lineno_count, abfd)
1039 != linesz * sub->lineno_count))
1044 /* Don't let the linker relocation routines discard the symbols. */
1045 keep_syms = obj_coff_keep_syms (abfd);
1046 obj_coff_keep_syms (abfd) = true;
1052 symesz = bfd_coff_symesz (abfd);
1053 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1054 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1055 esym_end = esym + symcount * symesz;
1056 while (esym < esym_end)
1058 struct internal_syment sym;
1059 union internal_auxent aux;
1061 char buf[SYMNMLEN + 1];
1066 struct xcoff_link_hash_entry *set_toc;
1068 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1070 /* In this pass we are only interested in symbols with csect
1072 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1074 if (sym.n_sclass == C_FILE && csect != NULL)
1076 xcoff_section_data (abfd, csect)->last_symndx =
1078 - (bfd_byte *) obj_coff_external_syms (abfd))
1084 *csect_cache = csect;
1085 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1086 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1088 *csect_cache = NULL;
1089 esym += (sym.n_numaux + 1) * symesz;
1090 sym_hash += sym.n_numaux + 1;
1091 csect_cache += sym.n_numaux + 1;
1095 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1099 /* If this symbol has line number information attached to it,
1100 and we're not stripping it, count the number of entries and
1101 add them to the count for this csect. In the final link pass
1102 we are going to attach line number information by symbol,
1103 rather than by section, in order to more easily handle
1104 garbage collection. */
1105 if ((info->strip == strip_none || info->strip == strip_some)
1108 && ISFCN (sym.n_type))
1110 union internal_auxent auxlin;
1112 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1113 sym.n_type, sym.n_sclass,
1114 0, sym.n_numaux, (PTR) &auxlin);
1115 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1117 asection *enclosing;
1118 bfd_size_type linoff;
1120 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1121 if (enclosing == NULL)
1123 (*_bfd_error_handler)
1124 ("%s: `%s' has line numbers but no enclosing section",
1125 bfd_get_filename (abfd), name);
1126 bfd_set_error (bfd_error_bad_value);
1129 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1130 - enclosing->line_filepos);
1131 if (linoff < enclosing->lineno_count * linesz)
1133 struct internal_lineno lin;
1134 bfd_byte *linpstart;
1136 linpstart = (reloc_info[enclosing->target_index].linenos
1138 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1140 && ((bfd_size_type) lin.l_addr.l_symndx
1142 - (bfd_byte *) obj_coff_external_syms (abfd))
1145 bfd_byte *linpend, *linp;
1147 linpend = (reloc_info[enclosing->target_index].linenos
1148 + enclosing->lineno_count * linesz);
1149 for (linp = linpstart + linesz;
1153 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1155 if (lin.l_lnno == 0)
1158 csect->lineno_count += (linp - linpstart) / linesz;
1159 /* The setting of line_filepos will only be
1160 useful if all the line number entries for a
1161 csect are contiguous; this only matters for
1163 if (csect->line_filepos == 0)
1164 csect->line_filepos =
1165 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1171 /* Pick up the csect auxiliary information. */
1173 if (sym.n_numaux == 0)
1175 (*_bfd_error_handler)
1176 ("%s: class %d symbol `%s' has no aux entries",
1177 bfd_get_filename (abfd), sym.n_sclass, name);
1178 bfd_set_error (bfd_error_bad_value);
1182 bfd_coff_swap_aux_in (abfd,
1183 (PTR) (esym + symesz * sym.n_numaux),
1184 sym.n_type, sym.n_sclass,
1185 sym.n_numaux - 1, sym.n_numaux,
1188 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1198 (*_bfd_error_handler)
1199 ("%s: symbol `%s' has unrecognized csect type %d",
1200 bfd_get_filename (abfd), name, smtyp);
1201 bfd_set_error (bfd_error_bad_value);
1205 /* This is an external reference. */
1206 if (sym.n_sclass == C_HIDEXT
1207 || sym.n_scnum != N_UNDEF
1208 || aux.x_csect.x_scnlen.l != 0)
1210 (*_bfd_error_handler)
1211 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1212 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1213 aux.x_csect.x_scnlen.l);
1214 bfd_set_error (bfd_error_bad_value);
1217 section = bfd_und_section_ptr;
1221 /* This is a csect definition. */
1225 xcoff_section_data (abfd, csect)->last_symndx =
1227 - (bfd_byte *) obj_coff_external_syms (abfd))
1234 /* When we see a TOC anchor, we record the TOC value. */
1235 if (aux.x_csect.x_smclas == XMC_TC0)
1237 if (sym.n_sclass != C_HIDEXT
1238 || aux.x_csect.x_scnlen.l != 0)
1240 (*_bfd_error_handler)
1241 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1242 bfd_get_filename (abfd), name, sym.n_sclass,
1243 aux.x_csect.x_scnlen.l);
1244 bfd_set_error (bfd_error_bad_value);
1247 xcoff_data (abfd)->toc = sym.n_value;
1250 /* We must merge TOC entries for the same symbol. We can
1251 merge two TOC entries if they are both C_HIDEXT, they
1252 both have the same name, they are both 4 bytes long, and
1253 they both have a relocation table entry for an external
1254 symbol with the same name. Unfortunately, this means
1255 that we must look through the relocations. Ick. */
1256 if (aux.x_csect.x_smclas == XMC_TC
1257 && sym.n_sclass == C_HIDEXT
1258 && aux.x_csect.x_scnlen.l == 4
1259 && info->hash->creator == abfd->xvec)
1261 asection *enclosing;
1262 struct internal_reloc *relocs;
1263 bfd_size_type relindx;
1264 struct internal_reloc *rel;
1266 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1267 if (enclosing == NULL)
1270 relocs = reloc_info[enclosing->target_index].relocs;
1271 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1273 rel = relocs + relindx;
1274 if (relindx < enclosing->reloc_count
1275 && rel->r_vaddr == (bfd_vma) sym.n_value
1276 && rel->r_size == 31
1277 && rel->r_type == R_POS)
1280 struct internal_syment relsym;
1282 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1283 + rel->r_symndx * symesz);
1284 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1285 if (relsym.n_sclass == C_EXT)
1287 const char *relname;
1288 char relbuf[SYMNMLEN + 1];
1290 struct xcoff_link_hash_entry *h;
1292 /* At this point we know that the TOC entry is
1293 for an externally visible symbol. */
1294 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1296 if (relname == NULL)
1299 /* We only merge TOC entries if the TC name is
1300 the same as the symbol name. This handles
1301 the normal case, but not common cases like
1302 SYM.P4 which gcc generates to store SYM + 4
1303 in the TOC. FIXME. */
1304 if (strcmp (name, relname) == 0)
1306 copy = (! info->keep_memory
1307 || relsym._n._n_n._n_zeroes != 0
1308 || relsym._n._n_n._n_offset == 0);
1309 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1310 relname, true, copy,
1315 /* At this point h->root.type could be
1316 bfd_link_hash_new. That should be OK,
1317 since we know for sure that we will come
1318 across this symbol as we step through the
1321 /* We store h in *sym_hash for the
1322 convenience of the relocate_section
1326 if (h->toc_section != NULL)
1328 asection **rel_csects;
1330 /* We already have a TOC entry for this
1331 symbol, so we can just ignore this
1334 reloc_info[enclosing->target_index].csects;
1335 rel_csects[relindx] = bfd_und_section_ptr;
1339 /* We are about to create a TOC entry for
1347 /* We need to create a new section. We get the name from
1348 the csect storage mapping class, so that the linker can
1349 accumulate similar csects together. */
1351 static const char *csect_name_by_class[] =
1353 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1354 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1357 const char *csect_name;
1358 asection *enclosing;
1360 if ((aux.x_csect.x_smclas >=
1361 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1362 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1364 (*_bfd_error_handler)
1365 ("%s: symbol `%s' has unrecognized smclas %d",
1366 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1367 bfd_set_error (bfd_error_bad_value);
1371 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1372 csect = bfd_make_section_anyway (abfd, csect_name);
1375 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1376 if (enclosing == NULL)
1378 if (! bfd_is_abs_section (enclosing)
1379 && ((bfd_vma) sym.n_value < enclosing->vma
1380 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1381 > enclosing->vma + enclosing->_raw_size)))
1383 (*_bfd_error_handler)
1384 ("%s: csect `%s' not in enclosing section",
1385 bfd_get_filename (abfd), name);
1386 bfd_set_error (bfd_error_bad_value);
1389 csect->vma = sym.n_value;
1390 csect->filepos = (enclosing->filepos
1393 csect->_raw_size = aux.x_csect.x_scnlen.l;
1394 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1395 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1397 /* Record the enclosing section in the tdata for this new
1399 csect->used_by_bfd =
1400 ((struct coff_section_tdata *)
1401 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1402 if (csect->used_by_bfd == NULL)
1404 bfd_set_error (bfd_error_no_memory);
1407 coff_section_data (abfd, csect)->tdata =
1408 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1409 if (coff_section_data (abfd, csect)->tdata == NULL)
1411 bfd_set_error (bfd_error_no_memory);
1414 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1415 xcoff_section_data (abfd, csect)->lineno_count =
1416 enclosing->lineno_count;
1418 if (enclosing->owner == abfd)
1420 struct internal_reloc *relocs;
1421 bfd_size_type relindx;
1422 struct internal_reloc *rel;
1423 asection **rel_csect;
1425 relocs = reloc_info[enclosing->target_index].relocs;
1426 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1428 rel = relocs + relindx;
1429 rel_csect = (reloc_info[enclosing->target_index].csects
1431 csect->rel_filepos = (enclosing->rel_filepos
1432 + relindx * bfd_coff_relsz (abfd));
1433 while (relindx < enclosing->reloc_count
1434 && *rel_csect == NULL
1435 && rel->r_vaddr < csect->vma + csect->_raw_size)
1438 csect->flags |= SEC_RELOC;
1439 ++csect->reloc_count;
1446 /* There are a number of other fields and section flags
1447 which we do not bother to set. */
1449 csect_index = ((esym
1450 - (bfd_byte *) obj_coff_external_syms (abfd))
1453 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1455 if (first_csect == NULL)
1456 first_csect = csect;
1458 /* If this symbol is C_EXT, we treat it as starting at the
1459 beginning of the newly created section. */
1460 if (sym.n_sclass == C_EXT)
1466 /* If this is a TOC section for a symbol, record it. */
1467 if (set_toc != NULL)
1468 set_toc->toc_section = csect;
1473 /* This is a label definition. The x_scnlen field is the
1474 symbol index of the csect. I believe that this must
1475 always follow the appropriate XTY_SD symbol, so I will
1481 if (aux.x_csect.x_scnlen.l < 0
1482 || (aux.x_csect.x_scnlen.l
1483 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1487 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1489 || (section->flags & SEC_HAS_CONTENTS) == 0)
1494 (*_bfd_error_handler)
1495 ("%s: misplaced XTY_LD `%s'",
1496 bfd_get_filename (abfd), name);
1497 bfd_set_error (bfd_error_bad_value);
1501 value = sym.n_value - csect->vma;
1506 /* This is an unitialized csect. We could base the name on
1507 the storage mapping class, but we don't bother. If this
1508 csect is externally visible, it is a common symbol. */
1512 xcoff_section_data (abfd, csect)->last_symndx =
1514 - (bfd_byte *) obj_coff_external_syms (abfd))
1518 csect = bfd_make_section_anyway (abfd, ".bss");
1521 csect->vma = sym.n_value;
1522 csect->_raw_size = aux.x_csect.x_scnlen.l;
1523 csect->flags |= SEC_ALLOC;
1524 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1525 /* There are a number of other fields and section flags
1526 which we do not bother to set. */
1528 csect_index = ((esym
1529 - (bfd_byte *) obj_coff_external_syms (abfd))
1532 csect->used_by_bfd =
1533 ((struct coff_section_tdata *)
1534 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1535 if (csect->used_by_bfd == NULL)
1537 bfd_set_error (bfd_error_no_memory);
1540 coff_section_data (abfd, csect)->tdata =
1541 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1542 if (coff_section_data (abfd, csect)->tdata == NULL)
1544 bfd_set_error (bfd_error_no_memory);
1547 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1549 if (first_csect == NULL)
1550 first_csect = csect;
1552 if (sym.n_sclass == C_EXT)
1554 csect->flags |= SEC_IS_COMMON;
1555 csect->_raw_size = 0;
1557 value = aux.x_csect.x_scnlen.l;
1563 /* Now we have enough information to add the symbol to the
1564 linker hash table. */
1566 if (sym.n_sclass == C_EXT)
1570 BFD_ASSERT (section != NULL);
1572 /* We must copy the name into memory if we got it from the
1573 syment itself, rather than the string table. */
1574 copy = default_copy;
1575 if (sym._n._n_n._n_zeroes != 0
1576 || sym._n._n_n._n_offset == 0)
1579 if (info->hash->creator == abfd->xvec)
1581 /* If we are statically linking a shared object, it is
1582 OK for symbol redefinitions to occur. I can't figure
1583 out just what the XCOFF linker is doing, but
1584 something like this is required for -bnso to work. */
1585 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1586 name, true, copy, false);
1587 if (*sym_hash == NULL)
1589 if (((*sym_hash)->root.type == bfd_link_hash_defined
1590 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1591 && ! bfd_is_und_section (section)
1592 && ! bfd_is_com_section (section))
1594 if ((abfd->flags & DYNAMIC) != 0)
1596 section = bfd_und_section_ptr;
1599 else if (((*sym_hash)->root.u.def.section->owner->flags
1602 (*sym_hash)->root.type = bfd_link_hash_undefined;
1603 (*sym_hash)->root.u.undef.abfd =
1604 (*sym_hash)->root.u.def.section->owner;
1609 if (! (_bfd_generic_link_add_one_symbol
1610 (info, abfd, name, flags, section, value,
1611 (const char *) NULL, copy, true,
1612 (struct bfd_link_hash_entry **) sym_hash)))
1615 if (smtyp == XTY_CM)
1617 if ((*sym_hash)->root.type != bfd_link_hash_common
1618 || (*sym_hash)->root.u.c.p->section != csect)
1620 /* We don't need the common csect we just created. */
1621 csect->_raw_size = 0;
1625 (*sym_hash)->root.u.c.p->alignment_power
1626 = csect->alignment_power;
1630 if (info->hash->creator == abfd->xvec)
1634 if (smtyp == XTY_ER || smtyp == XTY_CM)
1635 flag = XCOFF_REF_REGULAR;
1637 flag = XCOFF_DEF_REGULAR;
1638 (*sym_hash)->flags |= flag;
1640 if ((*sym_hash)->smclas == XMC_UA
1641 || flag == XCOFF_DEF_REGULAR)
1642 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1646 *csect_cache = csect;
1648 esym += (sym.n_numaux + 1) * symesz;
1649 sym_hash += sym.n_numaux + 1;
1650 csect_cache += sym.n_numaux + 1;
1653 /* Make sure that we have seen all the relocs. */
1654 for (sub = abfd->sections; sub != first_csect; sub = sub->next)
1656 /* Reset the section size, since the data is now attached to the
1657 csects. Don't reset the size of the .debug section, since we
1658 need to read it below in bfd_xcoff_size_dynamic_sections. */
1659 if (strcmp (bfd_get_section_name (abfd, sub), ".debug") != 0)
1662 if ((sub->flags & SEC_RELOC) != 0)
1665 struct internal_reloc *rel;
1666 asection **rel_csect;
1668 rel = reloc_info[sub->target_index].relocs;
1669 rel_csect = reloc_info[sub->target_index].csects;
1670 for (i = 0; i < sub->reloc_count; i++, rel++, rel_csect++)
1672 if (*rel_csect == NULL)
1674 (*_bfd_error_handler)
1675 ("%s: reloc %s:%d not in csect",
1676 bfd_get_filename (abfd), sub->name, i);
1677 bfd_set_error (bfd_error_bad_value);
1681 /* We identify all symbols which are called, so that we
1682 can create glue code for calls to functions imported
1683 from dynamic objects. */
1684 if (info->hash->creator == abfd->xvec
1685 && *rel_csect != bfd_und_section_ptr
1686 && (rel->r_type == R_BR
1687 || rel->r_type == R_RBR)
1688 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1690 struct xcoff_link_hash_entry *h;
1692 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1693 h->flags |= XCOFF_CALLED;
1694 /* If the symbol name starts with a period, it is
1695 the code of a function. If the symbol is
1696 currently undefined, then add an undefined symbol
1697 for the function descriptor. This should do no
1698 harm, because any regular object that defines the
1699 function should also define the function
1700 descriptor. It helps, because it means that we
1701 will identify the function descriptor with a
1702 dynamic object if a dynamic object defines it. */
1703 if (h->root.root.string[0] == '.'
1704 && h->descriptor == NULL)
1706 struct xcoff_link_hash_entry *hds;
1708 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1709 h->root.root.string + 1,
1713 if (hds->root.type == bfd_link_hash_new)
1715 if (! (_bfd_generic_link_add_one_symbol
1716 (info, abfd, hds->root.root.string,
1717 (flagword) 0, bfd_und_section_ptr,
1718 (bfd_vma) 0, (const char *) NULL, false,
1720 (struct bfd_link_hash_entry **) NULL)))
1723 h->descriptor = hds;
1728 free (reloc_info[sub->target_index].csects);
1729 reloc_info[sub->target_index].csects = NULL;
1731 /* Reset SEC_RELOC, the reloc_count, and the lineno_count,
1732 since the reloc and lineno information is now attached to
1734 sub->flags &=~ SEC_RELOC;
1735 sub->reloc_count = 0;
1736 sub->lineno_count = 0;
1738 /* If we are not keeping memory, free the reloc information. */
1739 if (! info->keep_memory
1740 && coff_section_data (abfd, sub) != NULL
1741 && coff_section_data (abfd, sub)->relocs != NULL
1742 && ! coff_section_data (abfd, sub)->keep_relocs)
1744 free (coff_section_data (abfd, sub)->relocs);
1745 coff_section_data (abfd, sub)->relocs = NULL;
1749 /* Free up the line numbers. FIXME: We could cache these
1750 somewhere for the final link, to avoid reading them again. */
1751 if (reloc_info[sub->target_index].linenos != NULL)
1753 free (reloc_info[sub->target_index].linenos);
1754 reloc_info[sub->target_index].linenos = NULL;
1760 obj_coff_keep_syms (abfd) = keep_syms;
1765 if (reloc_info != NULL)
1767 for (sub = abfd->sections; sub != NULL; sub = sub->next)
1769 if (reloc_info[sub->target_index].csects != NULL)
1770 free (reloc_info[sub->target_index].csects);
1771 if (reloc_info[sub->target_index].linenos != NULL)
1772 free (reloc_info[sub->target_index].linenos);
1776 obj_coff_keep_syms (abfd) = keep_syms;
1783 /* This function is used to add symbols from a dynamic object to the
1784 global symbol table. */
1787 xcoff_link_add_dynamic_symbols (abfd, info)
1789 struct bfd_link_info *info;
1791 bfd_size_type symesz;
1794 struct xcoff_import_file *n;
1799 struct xcoff_import_file **pp;
1801 /* We can only handle a dynamic object if we are generating an XCOFF
1803 if (info->hash->creator != abfd->xvec)
1805 (*_bfd_error_handler)
1806 ("%s: XCOFF shared object when not producing XCOFF output",
1807 bfd_get_filename (abfd));
1808 bfd_set_error (bfd_error_invalid_operation);
1812 /* Remove the sections from this object, so that they do not get
1813 included in the link. */
1814 abfd->sections = NULL;
1816 symesz = bfd_coff_symesz (abfd);
1817 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1818 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1819 while (esym < esym_end)
1821 struct internal_syment sym;
1823 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1825 /* I think that every symbol mentioned in a dynamic object must
1826 be defined by that object, perhaps by importing it from
1827 another dynamic object. All we have to do is look up each
1828 external symbol. If we have already put it in the hash
1829 table, we simply set a flag indicating that it appears in a
1832 if (sym.n_sclass == C_EXT)
1835 char buf[SYMNMLEN + 1];
1836 struct xcoff_link_hash_entry *h;
1838 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1842 /* Normally we could not xcoff_link_hash_lookup in an add
1843 symbols routine, since we might not be using an XCOFF
1844 hash table. However, we verified above that we are using
1845 an XCOFF hash table. */
1846 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
1847 false, false, true);
1850 h->flags |= XCOFF_REF_DYNAMIC;
1852 /* If the symbol is undefined, and the current BFD is
1853 not a dynamic object, change the BFD to this dynamic
1854 object, so that we can get the correct import file
1856 if ((h->root.type == bfd_link_hash_undefined
1857 || h->root.type == bfd_link_hash_undefweak)
1858 && (h->root.u.undef.abfd == NULL
1859 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
1860 h->root.u.undef.abfd = abfd;
1862 if (h->smclas == XMC_UA
1863 && sym.n_numaux > 0)
1865 union internal_auxent aux;
1867 bfd_coff_swap_aux_in (abfd,
1868 (PTR) (esym + symesz * sym.n_numaux),
1869 sym.n_type, sym.n_sclass,
1870 sym.n_numaux - 1, sym.n_numaux,
1872 h->smclas = aux.x_csect.x_smclas;
1877 esym += (sym.n_numaux + 1) * symesz;
1880 /* Record this file in the import files. */
1882 n = ((struct xcoff_import_file *)
1883 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
1886 bfd_set_error (bfd_error_no_memory);
1891 /* For some reason, the path entry in the import file list for a
1892 shared object appears to always be empty. The file name is the
1895 if (abfd->my_archive == NULL)
1897 bname = bfd_get_filename (abfd);
1902 bname = bfd_get_filename (abfd->my_archive);
1903 mname = bfd_get_filename (abfd);
1905 s = strrchr (bname, '/');
1911 /* We start c at 1 because the first import file number is reserved
1913 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1915 pp = &(*pp)->next, ++c)
1919 xcoff_data (abfd)->import_file_id = c;
1924 /* Routines that are called after all the input files have been
1925 handled, but before the sections are laid out in memory. */
1927 /* Mark a symbol as not being garbage, including the section in which
1930 static INLINE boolean
1931 xcoff_mark_symbol (info, h)
1932 struct bfd_link_info *info;
1933 struct xcoff_link_hash_entry *h;
1935 if ((h->flags & XCOFF_MARK) != 0)
1938 h->flags |= XCOFF_MARK;
1939 if (h->root.type == bfd_link_hash_defined
1940 || h->root.type == bfd_link_hash_defweak)
1944 hsec = h->root.u.def.section;
1945 if ((hsec->flags & SEC_MARK) == 0)
1947 if (! xcoff_mark (info, hsec))
1952 if (h->toc_section != NULL
1953 && (h->toc_section->flags & SEC_MARK) == 0)
1955 if (! xcoff_mark (info, h->toc_section))
1962 /* The mark phase of garbage collection. For a given section, mark
1963 it, and all the sections which define symbols to which it refers.
1964 Because this function needs to look at the relocs, we also count
1965 the number of relocs which need to be copied into the .loader
1969 xcoff_mark (info, sec)
1970 struct bfd_link_info *info;
1973 if ((sec->flags & SEC_MARK) != 0)
1976 sec->flags |= SEC_MARK;
1978 if (sec->owner->xvec == info->hash->creator
1979 && coff_section_data (sec->owner, sec) != NULL
1980 && xcoff_section_data (sec->owner, sec) != NULL)
1982 register struct xcoff_link_hash_entry **hp, **hpend;
1983 struct internal_reloc *rel, *relend;
1985 /* Mark all the symbols in this section. */
1987 hp = (obj_xcoff_sym_hashes (sec->owner)
1988 + xcoff_section_data (sec->owner, sec)->first_symndx);
1989 hpend = (obj_xcoff_sym_hashes (sec->owner)
1990 + xcoff_section_data (sec->owner, sec)->last_symndx);
1991 for (; hp < hpend; hp++)
1993 register struct xcoff_link_hash_entry *h;
1997 && (h->flags & XCOFF_MARK) == 0)
1999 if (! xcoff_mark_symbol (info, h))
2004 /* Look through the section relocs. */
2006 if ((sec->flags & SEC_RELOC) != 0
2007 && sec->reloc_count > 0)
2009 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2010 (bfd_byte *) NULL, false,
2011 (struct internal_reloc *) NULL);
2014 relend = rel + sec->reloc_count;
2015 for (; rel < relend; rel++)
2018 struct xcoff_link_hash_entry *h;
2020 if ((unsigned int) rel->r_symndx
2021 > obj_raw_syment_count (sec->owner))
2024 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2026 && (h->flags & XCOFF_MARK) == 0)
2028 if (! xcoff_mark_symbol (info, h))
2032 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2034 && (rsec->flags & SEC_MARK) == 0)
2036 if (! xcoff_mark (info, rsec))
2040 /* See if this reloc needs to be copied into the .loader
2042 switch (rel->r_type)
2046 || h->root.type == bfd_link_hash_defined
2047 || h->root.type == bfd_link_hash_defweak
2048 || h->root.type == bfd_link_hash_common
2049 || ((h->flags & XCOFF_CALLED) != 0
2050 && (h->flags & XCOFF_DEF_REGULAR) == 0
2051 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2052 && (h->root.type == bfd_link_hash_undefined
2053 || h->root.type == bfd_link_hash_undefweak)
2054 && h->root.root.string[0] == '.'))
2061 ++xcoff_hash_table (info)->ldrel_count;
2063 h->flags |= XCOFF_LDREL;
2070 /* We should never need a .loader reloc for a TOC
2076 if (! info->keep_memory
2077 && coff_section_data (sec->owner, sec) != NULL
2078 && coff_section_data (sec->owner, sec)->relocs != NULL
2079 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2081 free (coff_section_data (sec->owner, sec)->relocs);
2082 coff_section_data (sec->owner, sec)->relocs = NULL;
2090 /* The sweep phase of garbage collection. Remove all garbage
2095 struct bfd_link_info *info;
2099 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2103 for (o = sub->sections; o != NULL; o = o->next)
2105 if ((o->flags & SEC_MARK) == 0)
2107 /* Keep all sections from non-XCOFF input files. Keep
2108 special sections. Keep .debug sections for the
2110 if (sub->xvec != info->hash->creator
2111 || o == xcoff_hash_table (info)->debug_section
2112 || o == xcoff_hash_table (info)->loader_section
2113 || o == xcoff_hash_table (info)->linkage_section
2114 || o == xcoff_hash_table (info)->toc_section
2115 || strcmp (o->name, ".debug") == 0)
2116 o->flags |= SEC_MARK;
2121 o->lineno_count = 0;
2128 /* Record the number of elements in a set. This is used to output the
2129 correct csect length. */
2132 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2134 struct bfd_link_info *info;
2135 struct bfd_link_hash_entry *harg;
2138 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2139 struct xcoff_link_size_list *n;
2141 if (! XCOFF_XVECP (output_bfd->xvec))
2144 /* This will hardly ever be called. I don't want to burn four bytes
2145 per global symbol, so instead the size is kept on a linked list
2146 attached to the hash table. */
2148 n = ((struct xcoff_link_size_list *)
2149 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2152 bfd_set_error (bfd_error_no_memory);
2155 n->next = xcoff_hash_table (info)->size_list;
2158 xcoff_hash_table (info)->size_list = n;
2160 h->flags |= XCOFF_HAS_SIZE;
2165 /* Import a symbol. */
2168 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2171 struct bfd_link_info *info;
2172 struct bfd_link_hash_entry *harg;
2174 const char *imppath;
2175 const char *impfile;
2176 const char *impmember;
2178 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2180 if (! XCOFF_XVECP (output_bfd->xvec))
2183 h->flags |= XCOFF_IMPORT;
2185 if (val != (bfd_vma) -1)
2187 if (h->root.type == bfd_link_hash_defined)
2189 if (! ((*info->callbacks->multiple_definition)
2190 (info, h->root.root.string, h->root.u.def.section->owner,
2191 h->root.u.def.section, h->root.u.def.value,
2192 output_bfd, bfd_abs_section_ptr, val)))
2196 h->root.type = bfd_link_hash_defined;
2197 h->root.u.def.section = bfd_abs_section_ptr;
2198 h->root.u.def.value = val;
2201 if (h->ldsym == NULL)
2203 h->ldsym = ((struct internal_ldsym *)
2204 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2205 if (h->ldsym == NULL)
2207 bfd_set_error (bfd_error_no_memory);
2212 if (imppath == NULL)
2213 h->ldsym->l_ifile = (bfd_size_type) -1;
2217 struct xcoff_import_file **pp;
2219 /* We start c at 1 because the first entry in the import list is
2220 reserved for the library search path. */
2221 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2223 pp = &(*pp)->next, ++c)
2225 if (strcmp ((*pp)->path, imppath) == 0
2226 && strcmp ((*pp)->file, impfile) == 0
2227 && strcmp ((*pp)->member, impmember) == 0)
2233 struct xcoff_import_file *n;
2235 n = ((struct xcoff_import_file *)
2236 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2239 bfd_set_error (bfd_error_no_memory);
2245 n->member = impmember;
2249 h->ldsym->l_ifile = c;
2255 /* Export a symbol. */
2258 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2260 struct bfd_link_info *info;
2261 struct bfd_link_hash_entry *harg;
2264 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2266 if (! XCOFF_XVECP (output_bfd->xvec))
2269 h->flags |= XCOFF_EXPORT;
2271 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2272 I'm just going to ignore it until somebody explains it. */
2274 /* Make sure we don't garbage collect this symbol. */
2275 if (! xcoff_mark_symbol (info, h))
2281 /* Count a reloc against a symbol. This is called for relocs
2282 generated by the linker script, typically for global constructors
2286 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2288 struct bfd_link_info *info;
2291 struct xcoff_link_hash_entry *h;
2293 if (! XCOFF_XVECP (output_bfd->xvec))
2296 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, false, false,
2300 (*_bfd_error_handler) ("%s: no such symbol", name);
2301 bfd_set_error (bfd_error_no_symbols);
2305 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2306 ++xcoff_hash_table (info)->ldrel_count;
2308 /* Mark the symbol to avoid garbage collection. */
2309 if (! xcoff_mark_symbol (info, h))
2315 /* This function is called for each symbol to which the linker script
2319 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2321 struct bfd_link_info *info;
2324 struct xcoff_link_hash_entry *h;
2326 if (! XCOFF_XVECP (output_bfd->xvec))
2329 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2334 h->flags |= XCOFF_DEF_REGULAR;
2339 /* This structure is used to pass information through
2340 xcoff_link_hash_traverse. */
2342 struct xcoff_loader_info
2344 /* Set if a problem occurred. */
2348 /* Link information structure. */
2349 struct bfd_link_info *info;
2350 /* Number of ldsym structures. */
2352 /* Size of string table. */
2356 /* Allocated size of string table. */
2360 /* Build the .loader section. This is called by the XCOFF linker
2361 emulation before_allocation routine. We must set the size of the
2362 .loader section before the linker lays out the output file.
2363 LIBPATH is the library path to search for shared objects; this is
2364 normally built from the -L arguments passed to the linker. ENTRY
2365 is the name of the entry point symbol. */
2368 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2369 file_align, maxstack, maxdata, gc,
2372 struct bfd_link_info *info;
2373 const char *libpath;
2375 unsigned long file_align;
2376 unsigned long maxstack;
2377 unsigned long maxdata;
2382 struct xcoff_link_hash_entry *hentry;
2384 struct xcoff_loader_info ldinfo;
2385 size_t impsize, impcount;
2386 struct xcoff_import_file *fl;
2387 struct internal_ldhdr *ldhdr;
2388 bfd_size_type stoff;
2392 struct bfd_strtab_hash *debug_strtab;
2393 bfd_byte *debug_contents = NULL;
2395 if (! XCOFF_XVECP (output_bfd->xvec))
2398 ldinfo.failed = false;
2399 ldinfo.output_bfd = output_bfd;
2401 ldinfo.ldsym_count = 0;
2402 ldinfo.string_size = 0;
2403 ldinfo.strings = NULL;
2404 ldinfo.string_alc = 0;
2406 xcoff_data (output_bfd)->maxstack = maxstack;
2407 xcoff_data (output_bfd)->maxdata = maxdata;
2408 xcoff_data (output_bfd)->modtype = modtype;
2410 xcoff_hash_table (info)->file_align = file_align;
2411 xcoff_hash_table (info)->textro = textro;
2413 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2414 false, false, true);
2417 hentry->flags |= XCOFF_ENTRY;
2418 if (hentry->root.type == bfd_link_hash_defined
2419 || hentry->root.type == bfd_link_hash_defweak)
2420 xcoff_data (output_bfd)->entry_section =
2421 hentry->root.u.def.section->output_section;
2424 /* Garbage collect unused sections. */
2425 if (info->relocateable
2428 || (hentry->root.type != bfd_link_hash_defined
2429 && hentry->root.type != bfd_link_hash_defweak))
2432 xcoff_hash_table (info)->gc = false;
2434 /* We still need to call xcoff_mark, in order to set ldrel_count
2436 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2440 for (o = sub->sections; o != NULL; o = o->next)
2442 if ((o->flags & SEC_MARK) == 0)
2444 if (! xcoff_mark (info, o))
2452 if (! xcoff_mark (info, hentry->root.u.def.section))
2455 xcoff_hash_table (info)->gc = true;
2458 if (info->input_bfds == NULL)
2460 /* I'm not sure what to do in this bizarre case. */
2464 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2469 /* Work out the size of the import file names. Each import file ID
2470 consists of three null terminated strings: the path, the file
2471 name, and the archive member name. The first entry in the list
2472 of names is the path to use to find objects, which the linker has
2473 passed in as the libpath argument. For some reason, the path
2474 entry in the other import file names appears to always be empty. */
2475 impsize = strlen (libpath) + 3;
2477 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2480 impsize += (strlen (fl->path)
2482 + strlen (fl->member)
2486 /* Set up the .loader section header. */
2487 ldhdr = &xcoff_hash_table (info)->ldhdr;
2488 ldhdr->l_version = 1;
2489 ldhdr->l_nsyms = ldinfo.ldsym_count;
2490 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2491 ldhdr->l_istlen = impsize;
2492 ldhdr->l_nimpid = impcount;
2493 ldhdr->l_impoff = (LDHDRSZ
2494 + ldhdr->l_nsyms * LDSYMSZ
2495 + ldhdr->l_nreloc * LDRELSZ);
2496 ldhdr->l_stlen = ldinfo.string_size;
2497 stoff = ldhdr->l_impoff + impsize;
2498 if (ldinfo.string_size == 0)
2501 ldhdr->l_stoff = stoff;
2503 /* We now know the final size of the .loader section. Allocate
2505 lsec = xcoff_hash_table (info)->loader_section;
2506 lsec->_raw_size = stoff + ldhdr->l_stlen;
2507 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2508 if (lsec->contents == NULL)
2510 bfd_set_error (bfd_error_no_memory);
2514 /* Set up the header. */
2515 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2516 (struct external_ldhdr *) lsec->contents);
2518 /* Set up the import file names. */
2519 out = (char *) lsec->contents + ldhdr->l_impoff;
2520 strcpy (out, libpath);
2521 out += strlen (libpath) + 1;
2524 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2526 register const char *s;
2529 while ((*out++ = *s++) != '\0')
2532 while ((*out++ = *s++) != '\0')
2535 while ((*out++ = *s++) != '\0')
2539 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
2541 /* Set up the symbol string table. */
2542 if (ldinfo.string_size > 0)
2544 memcpy (out, ldinfo.strings, ldinfo.string_size);
2545 free (ldinfo.strings);
2546 ldinfo.strings = NULL;
2549 /* We can't set up the symbol table or the relocs yet, because we
2550 don't yet know the final position of the various sections. The
2551 .loader symbols are written out when the corresponding normal
2552 symbols are written out in xcoff_link_input_bfd or
2553 xcoff_write_global_symbol. The .loader relocs are written out
2554 when the corresponding normal relocs are handled in
2555 xcoff_link_input_bfd. */
2557 /* Allocate space for the global linkage section and the global toc
2559 sec = xcoff_hash_table (info)->linkage_section;
2560 if (sec->_raw_size > 0)
2562 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2563 if (sec->contents == NULL)
2565 bfd_set_error (bfd_error_no_memory);
2569 sec = xcoff_hash_table (info)->toc_section;
2570 if (sec->_raw_size > 0)
2572 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2573 if (sec->contents == NULL)
2575 bfd_set_error (bfd_error_no_memory);
2580 /* Now that we've done garbage collection, figure out the contents
2581 of the .debug section. */
2582 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2584 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2587 bfd_size_type symcount;
2588 unsigned long *debug_index;
2590 bfd_byte *esym, *esymend;
2591 bfd_size_type symesz;
2593 if (sub->xvec != info->hash->creator)
2595 subdeb = bfd_get_section_by_name (sub, ".debug");
2596 if (subdeb == NULL || subdeb->_raw_size == 0)
2599 if (info->strip == strip_all
2600 || info->strip == strip_debugger
2601 || info->discard == discard_all)
2603 subdeb->_raw_size = 0;
2607 if (! _bfd_coff_get_external_symbols (sub))
2610 symcount = obj_raw_syment_count (sub);
2611 debug_index = ((unsigned long *)
2612 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2613 if (debug_index == NULL)
2615 bfd_set_error (bfd_error_no_memory);
2618 xcoff_data (sub)->debug_indices = debug_index;
2620 /* Grab the contents of the .debug section. We use malloc and
2621 copy the neams into the debug stringtab, rather than
2622 bfd_alloc, because I expect that, when linking many files
2623 together, many of the strings will be the same. Storing the
2624 strings in the hash table should save space in this case. */
2625 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2626 if (debug_contents == NULL)
2628 bfd_set_error (bfd_error_no_memory);
2631 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2632 (file_ptr) 0, subdeb->_raw_size))
2635 csectpp = xcoff_data (sub)->csects;
2637 symesz = bfd_coff_symesz (sub);
2638 esym = (bfd_byte *) obj_coff_external_syms (sub);
2639 esymend = esym + symcount * symesz;
2640 while (esym < esymend)
2642 struct internal_syment sym;
2644 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2646 *debug_index = (unsigned long) -1;
2648 if (sym._n._n_n._n_zeroes == 0
2651 || ((*csectpp)->flags & SEC_MARK) != 0
2652 || *csectpp == bfd_abs_section_ptr)
2653 && bfd_coff_symname_in_debug (sub, &sym))
2658 name = (char *) debug_contents + sym._n._n_n._n_offset;
2659 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2660 if (indx == (bfd_size_type) -1)
2662 *debug_index = indx;
2665 esym += (sym.n_numaux + 1) * symesz;
2666 csectpp += sym.n_numaux + 1;
2667 debug_index += sym.n_numaux + 1;
2670 free (debug_contents);
2671 debug_contents = NULL;
2673 /* Clear the size of subdeb, so that it is not included directly
2674 in the output file. */
2675 subdeb->_raw_size = 0;
2677 if (! info->keep_memory)
2679 if (! _bfd_coff_free_symbols (sub))
2684 xcoff_hash_table (info)->debug_section->_raw_size =
2685 _bfd_stringtab_size (debug_strtab);
2690 if (ldinfo.strings != NULL)
2691 free (ldinfo.strings);
2692 if (debug_contents != NULL)
2693 free (debug_contents);
2697 /* Add a symbol to the .loader symbols, if necessary. */
2700 xcoff_build_ldsyms (h, p)
2701 struct xcoff_link_hash_entry *h;
2704 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2707 /* We don't want to garbage collect symbols which are not defined in
2708 XCOFF files. This is a convenient place to mark them. */
2709 if (xcoff_hash_table (ldinfo->info)->gc
2710 && (h->flags & XCOFF_MARK) == 0
2711 && (h->root.type == bfd_link_hash_defined
2712 || h->root.type == bfd_link_hash_defweak)
2713 && (h->root.u.def.section->owner == NULL
2714 || (h->root.u.def.section->owner->xvec
2715 != ldinfo->info->hash->creator)))
2716 h->flags |= XCOFF_MARK;
2718 /* If this symbol is called, and it is defined in a dynamic object,
2719 then we need to set up global linkage code for it. (Unless we
2720 did garbage collection and we didn't need this symbol.) */
2721 if ((h->flags & XCOFF_CALLED) != 0
2722 && (h->flags & XCOFF_DEF_REGULAR) == 0
2723 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2724 && (h->root.type == bfd_link_hash_undefined
2725 || h->root.type == bfd_link_hash_undefweak)
2726 && h->root.root.string[0] == '.'
2727 && (! xcoff_hash_table (ldinfo->info)->gc
2728 || (h->flags & XCOFF_MARK) != 0))
2731 struct xcoff_link_hash_entry *hds;
2733 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2734 h->root.type = bfd_link_hash_defined;
2735 h->root.u.def.section = sec;
2736 h->root.u.def.value = sec->_raw_size;
2738 sec->_raw_size += XCOFF_GLINK_SIZE;
2740 /* The global linkage code requires a TOC entry for the
2742 hds = h->descriptor;
2743 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2744 || hds->root.type == bfd_link_hash_undefweak)
2745 && (hds->flags & XCOFF_DEF_REGULAR) == 0
2746 && (hds->flags & XCOFF_REF_DYNAMIC) != 0);
2747 hds->flags |= XCOFF_MARK;
2748 if (hds->toc_section == NULL)
2750 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2751 hds->u.toc_offset = hds->toc_section->_raw_size;
2752 hds->toc_section->_raw_size += 4;
2753 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2754 ++hds->toc_section->reloc_count;
2756 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2758 /* We need to call xcoff_build_ldsyms recursively here,
2759 because we may already have passed hds on the traversal. */
2760 xcoff_build_ldsyms (hds, p);
2764 /* If this is still a common symbol, and it wasn't garbage
2765 collected, we need to actually allocate space for it in the .bss
2767 if (h->root.type == bfd_link_hash_common
2768 && (! xcoff_hash_table (ldinfo->info)->gc
2769 || (h->flags & XCOFF_MARK) != 0)
2770 && h->root.u.c.p->section->_raw_size == 0)
2772 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2773 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
2776 /* We need to add a symbol to the .loader section if it is mentioned
2777 in a reloc which we are copying to the .loader section and it was
2778 not defined or common, or if it is the entry point, or if it is
2781 if (((h->flags & XCOFF_LDREL) == 0
2782 || h->root.type == bfd_link_hash_defined
2783 || h->root.type == bfd_link_hash_defweak
2784 || h->root.type == bfd_link_hash_common)
2785 && (h->flags & XCOFF_ENTRY) == 0
2786 && (h->flags & XCOFF_EXPORT) == 0)
2792 /* We don't need to add this symbol if we did garbage collection and
2793 we did not mark this symbol. */
2794 if (xcoff_hash_table (ldinfo->info)->gc
2795 && (h->flags & XCOFF_MARK) == 0)
2801 /* We may have already processed this symbol due to the recursive
2803 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2806 /* We need to add this symbol to the .loader symbols. */
2808 /* h->ldsym will already have been allocated for an explicitly
2810 if (h->ldsym == NULL)
2812 h->ldsym = ((struct internal_ldsym *)
2813 bfd_zalloc (ldinfo->output_bfd,
2814 sizeof (struct internal_ldsym)));
2815 if (h->ldsym == NULL)
2817 ldinfo->failed = true;
2818 bfd_set_error (bfd_error_no_memory);
2823 /* The first 3 symbol table indices are reserved to indicate the
2825 h->ldindx = ldinfo->ldsym_count + 3;
2827 ++ldinfo->ldsym_count;
2829 len = strlen (h->root.root.string);
2830 if (len <= SYMNMLEN)
2831 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
2834 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2837 bfd_byte *newstrings;
2839 newalc = ldinfo->string_alc * 2;
2842 while (ldinfo->string_size + len + 3 > newalc)
2845 if (ldinfo->strings == NULL)
2846 newstrings = (bfd_byte *) malloc (newalc);
2848 newstrings = ((bfd_byte *)
2849 realloc ((PTR) ldinfo->strings, newalc));
2850 if (newstrings == NULL)
2852 ldinfo->failed = true;
2853 bfd_set_error (bfd_error_no_memory);
2856 ldinfo->string_alc = newalc;
2857 ldinfo->strings = newstrings;
2860 bfd_put_16 (ldinfo->output_bfd, len + 1,
2861 ldinfo->strings + ldinfo->string_size);
2862 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
2863 h->ldsym->_l._l_l._l_zeroes = 0;
2864 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
2865 ldinfo->string_size += len + 3;
2868 h->flags |= XCOFF_BUILT_LDSYM;
2873 /* Do the final link step. */
2876 _bfd_xcoff_bfd_final_link (abfd, info)
2878 struct bfd_link_info *info;
2880 bfd_size_type symesz;
2881 struct xcoff_final_link_info finfo;
2883 struct bfd_link_order *p;
2884 size_t max_contents_size;
2885 size_t max_sym_count;
2886 size_t max_lineno_count;
2887 size_t max_reloc_count;
2888 size_t max_output_reloc_count;
2889 file_ptr rel_filepos;
2891 file_ptr line_filepos;
2892 unsigned int linesz;
2894 bfd_byte *external_relocs = NULL;
2895 char strbuf[STRING_SIZE_SIZE];
2897 symesz = bfd_coff_symesz (abfd);
2900 finfo.output_bfd = abfd;
2901 finfo.strtab = NULL;
2902 finfo.section_info = NULL;
2903 finfo.last_file_index = -1;
2904 finfo.toc_symindx = -1;
2905 finfo.internal_syms = NULL;
2906 finfo.sym_indices = NULL;
2907 finfo.outsyms = NULL;
2908 finfo.linenos = NULL;
2909 finfo.contents = NULL;
2910 finfo.external_relocs = NULL;
2912 finfo.ldsym = ((struct external_ldsym *)
2913 (xcoff_hash_table (info)->loader_section->contents
2915 finfo.ldrel = ((struct external_ldrel *)
2916 (xcoff_hash_table (info)->loader_section->contents
2918 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
2920 xcoff_data (abfd)->coff.link_info = info;
2922 finfo.strtab = _bfd_stringtab_init ();
2923 if (finfo.strtab == NULL)
2926 /* Compute the file positions for all the sections. */
2927 if (abfd->output_has_begun)
2929 if (xcoff_hash_table (info)->file_align != 0)
2936 file_align = xcoff_hash_table (info)->file_align;
2937 if (file_align != 0)
2939 boolean saw_contents;
2944 /* Insert .pad sections before every section which has
2945 contents and is loaded, if it is preceded by some other
2946 section which has contents and is loaded. */
2947 saw_contents = true;
2948 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
2950 (*op)->target_index = indx;
2951 if (strcmp ((*op)->name, ".pad") == 0)
2952 saw_contents = false;
2953 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
2954 && ((*op)->flags & SEC_LOAD) != 0)
2957 saw_contents = true;
2964 n = bfd_make_section_anyway (abfd, ".pad");
2965 BFD_ASSERT (*op == n);
2967 n->flags = SEC_HAS_CONTENTS;
2968 n->alignment_power = 0;
2969 saw_contents = false;
2974 /* Reset the section indices after inserting the new
2977 for (o = abfd->sections; o != NULL; o = o->next)
2980 o->target_index = indx;
2982 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
2984 /* Work out appropriate sizes for the .pad sections to force
2985 each section to land on a page boundary. This bit of
2986 code knows what compute_section_file_positions is going
2988 sofar = bfd_coff_filhsz (abfd);
2989 sofar += bfd_coff_aoutsz (abfd);
2990 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
2992 for (o = abfd->sections; o != NULL; o = o->next)
2994 if (strcmp (o->name, ".pad") == 0)
2998 BFD_ASSERT (o->_raw_size == 0);
2999 pageoff = sofar & (file_align - 1);
3002 o->_raw_size = file_align - pageoff;
3003 sofar += file_align - pageoff;
3004 o->flags |= SEC_HAS_CONTENTS;
3009 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3010 sofar += BFD_ALIGN (o->_raw_size,
3011 1 << o->alignment_power);
3016 bfd_coff_compute_section_file_positions (abfd);
3019 /* Count the line numbers and relocation entries required for the
3020 output file. Set the file positions for the relocs. */
3021 rel_filepos = obj_relocbase (abfd);
3022 relsz = bfd_coff_relsz (abfd);
3023 max_contents_size = 0;
3024 max_lineno_count = 0;
3025 max_reloc_count = 0;
3026 for (o = abfd->sections; o != NULL; o = o->next)
3029 o->lineno_count = 0;
3030 for (p = o->link_order_head; p != NULL; p = p->next)
3032 if (p->type == bfd_indirect_link_order)
3036 sec = p->u.indirect.section;
3038 if (info->strip == strip_none
3039 || info->strip == strip_some)
3040 o->lineno_count += sec->lineno_count;
3042 o->reloc_count += sec->reloc_count;
3044 if (sec->_raw_size > max_contents_size)
3045 max_contents_size = sec->_raw_size;
3046 if (sec->lineno_count > max_lineno_count)
3047 max_lineno_count = sec->lineno_count;
3048 if (coff_section_data (sec->owner, sec) != NULL
3049 && xcoff_section_data (sec->owner, sec) != NULL
3050 && (xcoff_section_data (sec->owner, sec)->lineno_count
3051 > max_lineno_count))
3053 xcoff_section_data (sec->owner, sec)->lineno_count;
3054 if (sec->reloc_count > max_reloc_count)
3055 max_reloc_count = sec->reloc_count;
3057 else if (p->type == bfd_section_reloc_link_order
3058 || p->type == bfd_symbol_reloc_link_order)
3061 if (o->reloc_count == 0)
3065 o->flags |= SEC_RELOC;
3066 o->rel_filepos = rel_filepos;
3067 rel_filepos += o->reloc_count * relsz;
3071 /* Allocate space for the pointers we need to keep for the relocs. */
3075 /* We use section_count + 1, rather than section_count, because
3076 the target_index fields are 1 based. */
3077 finfo.section_info = ((struct xcoff_link_section_info *)
3078 malloc ((abfd->section_count + 1)
3079 * sizeof (struct xcoff_link_section_info)));
3080 if (finfo.section_info == NULL)
3082 bfd_set_error (bfd_error_no_memory);
3085 for (i = 0; i <= abfd->section_count; i++)
3087 finfo.section_info[i].relocs = NULL;
3088 finfo.section_info[i].rel_hashes = NULL;
3089 finfo.section_info[i].toc_rel_hashes = NULL;
3093 /* We now know the size of the relocs, so we can determine the file
3094 positions of the line numbers. */
3095 line_filepos = rel_filepos;
3096 finfo.line_filepos = line_filepos;
3097 linesz = bfd_coff_linesz (abfd);
3098 max_output_reloc_count = 0;
3099 for (o = abfd->sections; o != NULL; o = o->next)
3101 if (o->lineno_count == 0)
3102 o->line_filepos = 0;
3105 o->line_filepos = line_filepos;
3106 line_filepos += o->lineno_count * linesz;
3109 if (o->reloc_count != 0)
3111 /* We don't know the indices of global symbols until we have
3112 written out all the local symbols. For each section in
3113 the output file, we keep an array of pointers to hash
3114 table entries. Each entry in the array corresponds to a
3115 reloc. When we find a reloc against a global symbol, we
3116 set the corresponding entry in this array so that we can
3117 fix up the symbol index after we have written out all the
3120 Because of this problem, we also keep the relocs in
3121 memory until the end of the link. This wastes memory.
3122 We could backpatch the file later, I suppose, although it
3124 finfo.section_info[o->target_index].relocs =
3125 ((struct internal_reloc *)
3126 malloc (o->reloc_count * sizeof (struct internal_reloc)));
3127 finfo.section_info[o->target_index].rel_hashes =
3128 ((struct xcoff_link_hash_entry **)
3129 malloc (o->reloc_count
3130 * sizeof (struct xcoff_link_hash_entry *)));
3131 if (finfo.section_info[o->target_index].relocs == NULL
3132 || finfo.section_info[o->target_index].rel_hashes == NULL)
3134 bfd_set_error (bfd_error_no_memory);
3138 if (o->reloc_count > max_output_reloc_count)
3139 max_output_reloc_count = o->reloc_count;
3142 /* Reset the reloc and lineno counts, so that we can use them to
3143 count the number of entries we have output so far. */
3145 o->lineno_count = 0;
3148 obj_sym_filepos (abfd) = line_filepos;
3150 /* Figure out the largest number of symbols in an input BFD. Take
3151 the opportunity to clear the output_has_begun fields of all the
3152 input BFD's. We want at least 4 symbols, since that is the
3153 number which xcoff_write_global_symbol may need. */
3155 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3159 sub->output_has_begun = false;
3160 sz = obj_raw_syment_count (sub);
3161 if (sz > max_sym_count)
3165 /* Allocate some buffers used while linking. */
3166 finfo.internal_syms = ((struct internal_syment *)
3167 malloc (max_sym_count
3168 * sizeof (struct internal_syment)));
3169 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
3170 finfo.outsyms = ((bfd_byte *)
3171 malloc ((size_t) ((max_sym_count + 1) * symesz)));
3172 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
3173 * bfd_coff_linesz (abfd));
3174 finfo.contents = (bfd_byte *) malloc (max_contents_size);
3175 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
3176 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3177 || (finfo.sym_indices == NULL && max_sym_count > 0)
3178 || finfo.outsyms == NULL
3179 || (finfo.linenos == NULL && max_lineno_count > 0)
3180 || (finfo.contents == NULL && max_contents_size > 0)
3181 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3183 bfd_set_error (bfd_error_no_memory);
3187 obj_raw_syment_count (abfd) = 0;
3188 xcoff_data (abfd)->toc = (bfd_vma) -1;
3190 /* We now know the position of everything in the file, except that
3191 we don't know the size of the symbol table and therefore we don't
3192 know where the string table starts. We just build the string
3193 table in memory as we go along. We process all the relocations
3194 for a single input file at once. */
3195 for (o = abfd->sections; o != NULL; o = o->next)
3197 for (p = o->link_order_head; p != NULL; p = p->next)
3199 if (p->type == bfd_indirect_link_order
3200 && p->u.indirect.section->owner->xvec == abfd->xvec)
3202 sub = p->u.indirect.section->owner;
3203 if (! sub->output_has_begun)
3205 if (! xcoff_link_input_bfd (&finfo, sub))
3207 sub->output_has_begun = true;
3210 else if (p->type == bfd_section_reloc_link_order
3211 || p->type == bfd_symbol_reloc_link_order)
3213 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3218 if (! _bfd_default_link_order (abfd, info, o, p))
3224 /* Free up the buffers used by xcoff_link_input_bfd. */
3226 if (finfo.internal_syms != NULL)
3228 free (finfo.internal_syms);
3229 finfo.internal_syms = NULL;
3231 if (finfo.sym_indices != NULL)
3233 free (finfo.sym_indices);
3234 finfo.sym_indices = NULL;
3236 if (finfo.linenos != NULL)
3238 free (finfo.linenos);
3239 finfo.linenos = NULL;
3241 if (finfo.contents != NULL)
3243 free (finfo.contents);
3244 finfo.contents = NULL;
3246 if (finfo.external_relocs != NULL)
3248 free (finfo.external_relocs);
3249 finfo.external_relocs = NULL;
3252 /* The value of the last C_FILE symbol is supposed to be -1. Write
3254 if (finfo.last_file_index != -1)
3256 finfo.last_file.n_value = -1;
3257 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3258 (PTR) finfo.outsyms);
3260 (obj_sym_filepos (abfd)
3261 + finfo.last_file_index * symesz),
3263 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3267 /* Write out all the global symbols which do not come from XCOFF
3269 xcoff_link_hash_traverse (xcoff_hash_table (info),
3270 xcoff_write_global_symbol,
3273 if (finfo.outsyms != NULL)
3275 free (finfo.outsyms);
3276 finfo.outsyms = NULL;
3279 /* Now that we have written out all the global symbols, we know the
3280 symbol indices to use for relocs against them, and we can finally
3281 write out the relocs. */
3282 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3283 if (external_relocs == NULL && max_output_reloc_count != 0)
3285 bfd_set_error (bfd_error_no_memory);
3289 for (o = abfd->sections; o != NULL; o = o->next)
3291 struct internal_reloc *irel;
3292 struct internal_reloc *irelend;
3293 struct xcoff_link_hash_entry **rel_hash;
3294 struct xcoff_toc_rel_hash *toc_rel_hash;
3297 if (o->reloc_count == 0)
3300 irel = finfo.section_info[o->target_index].relocs;
3301 irelend = irel + o->reloc_count;
3302 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3303 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3305 if (*rel_hash != NULL)
3307 if ((*rel_hash)->indx < 0)
3309 if (! ((*info->callbacks->unattached_reloc)
3310 (info, (*rel_hash)->root.root.string,
3311 (bfd *) NULL, o, irel->r_vaddr)))
3313 (*rel_hash)->indx = 0;
3315 irel->r_symndx = (*rel_hash)->indx;
3319 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
3320 toc_rel_hash != NULL;
3321 toc_rel_hash = toc_rel_hash->next)
3323 if (toc_rel_hash->h->u.toc_indx < 0)
3325 if (! ((*info->callbacks->unattached_reloc)
3326 (info, toc_rel_hash->h->root.root.string,
3327 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
3329 toc_rel_hash->h->u.toc_indx = 0;
3331 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
3334 /* XCOFF requires that the relocs be sorted by address. We tend
3335 to produce them in the order in which their containing csects
3336 appear in the symbol table, which is not necessarily by
3337 address. So we sort them here. There may be a better way to
3339 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3340 o->reloc_count, sizeof (struct internal_reloc),
3343 irel = finfo.section_info[o->target_index].relocs;
3344 irelend = irel + o->reloc_count;
3345 erel = external_relocs;
3346 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3347 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3349 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3350 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3351 abfd) != relsz * o->reloc_count)
3355 if (external_relocs != NULL)
3357 free (external_relocs);
3358 external_relocs = NULL;
3361 /* Free up the section information. */
3362 if (finfo.section_info != NULL)
3366 for (i = 0; i < abfd->section_count; i++)
3368 if (finfo.section_info[i].relocs != NULL)
3369 free (finfo.section_info[i].relocs);
3370 if (finfo.section_info[i].rel_hashes != NULL)
3371 free (finfo.section_info[i].rel_hashes);
3373 free (finfo.section_info);
3374 finfo.section_info = NULL;
3377 /* Write out the loader section contents. */
3378 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3379 == (xcoff_hash_table (info)->loader_section->contents
3380 + xcoff_hash_table (info)->ldhdr.l_impoff));
3381 o = xcoff_hash_table (info)->loader_section;
3382 if (! bfd_set_section_contents (abfd, o->output_section,
3383 o->contents, o->output_offset,
3387 /* Write out the global linkage section and the toc section. */
3388 o = xcoff_hash_table (info)->linkage_section;
3389 if (o->_raw_size > 0
3390 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3391 o->output_offset, o->_raw_size))
3393 o = xcoff_hash_table (info)->toc_section;
3394 if (o->_raw_size > 0
3395 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3396 o->output_offset, o->_raw_size))
3399 /* Write out the string table. */
3401 (obj_sym_filepos (abfd)
3402 + obj_raw_syment_count (abfd) * symesz),
3406 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3407 (bfd_byte *) strbuf);
3408 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3410 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3413 _bfd_stringtab_free (finfo.strtab);
3415 /* Write out the debugging string table. */
3416 o = xcoff_hash_table (info)->debug_section;
3419 struct bfd_strtab_hash *debug_strtab;
3421 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3422 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3423 >= _bfd_stringtab_size (debug_strtab));
3425 o->output_section->filepos + o->output_offset,
3428 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3432 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3433 not try to write out the symbols. */
3434 bfd_get_symcount (abfd) = 0;
3439 if (finfo.strtab != NULL)
3440 _bfd_stringtab_free (finfo.strtab);
3441 if (finfo.section_info != NULL)
3445 for (i = 0; i < abfd->section_count; i++)
3447 if (finfo.section_info[i].relocs != NULL)
3448 free (finfo.section_info[i].relocs);
3449 if (finfo.section_info[i].rel_hashes != NULL)
3450 free (finfo.section_info[i].rel_hashes);
3452 free (finfo.section_info);
3454 if (finfo.internal_syms != NULL)
3455 free (finfo.internal_syms);
3456 if (finfo.sym_indices != NULL)
3457 free (finfo.sym_indices);
3458 if (finfo.outsyms != NULL)
3459 free (finfo.outsyms);
3460 if (finfo.linenos != NULL)
3461 free (finfo.linenos);
3462 if (finfo.contents != NULL)
3463 free (finfo.contents);
3464 if (finfo.external_relocs != NULL)
3465 free (finfo.external_relocs);
3466 if (external_relocs != NULL)
3467 free (external_relocs);
3471 /* Link an input file into the linker output file. This function
3472 handles all the sections and relocations of the input file at once. */
3475 xcoff_link_input_bfd (finfo, input_bfd)
3476 struct xcoff_final_link_info *finfo;
3480 const char *strings;
3481 bfd_size_type syment_base;
3482 unsigned int n_tmask;
3483 unsigned int n_btshft;
3485 bfd_size_type isymesz;
3486 bfd_size_type osymesz;
3487 bfd_size_type linesz;
3490 struct xcoff_link_hash_entry **sym_hash;
3491 struct internal_syment *isymp;
3493 unsigned long *debug_index;
3495 unsigned long output_index;
3502 /* We can just skip DYNAMIC files, unless this is a static link. */
3503 if ((input_bfd->flags & DYNAMIC) != 0
3504 && ! finfo->info->static_link)
3507 /* Move all the symbols to the output file. */
3509 output_bfd = finfo->output_bfd;
3511 syment_base = obj_raw_syment_count (output_bfd);
3512 isymesz = bfd_coff_symesz (input_bfd);
3513 osymesz = bfd_coff_symesz (output_bfd);
3514 linesz = bfd_coff_linesz (input_bfd);
3515 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3517 n_tmask = coff_data (input_bfd)->local_n_tmask;
3518 n_btshft = coff_data (input_bfd)->local_n_btshft;
3520 /* Define macros so that ISFCN, et. al., macros work correctly. */
3521 #define N_TMASK n_tmask
3522 #define N_BTSHFT n_btshft
3525 if (! finfo->info->keep_memory)
3528 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3531 if (! _bfd_coff_get_external_symbols (input_bfd))
3534 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3535 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3536 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3537 csectpp = xcoff_data (input_bfd)->csects;
3538 debug_index = xcoff_data (input_bfd)->debug_indices;
3539 isymp = finfo->internal_syms;
3540 indexp = finfo->sym_indices;
3541 output_index = syment_base;
3542 outsym = finfo->outsyms;
3546 while (esym < esym_end)
3548 struct internal_syment isym;
3549 union internal_auxent aux;
3555 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3557 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3559 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3561 BFD_ASSERT (isymp->n_numaux > 0);
3562 bfd_coff_swap_aux_in (input_bfd,
3563 (PTR) (esym + isymesz * isymp->n_numaux),
3564 isymp->n_type, isymp->n_sclass,
3565 isymp->n_numaux - 1, isymp->n_numaux,
3567 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3570 /* Make a copy of *isymp so that the relocate_section function
3571 always sees the original values. This is more reliable than
3572 always recomputing the symbol value even if we are stripping
3576 /* If this symbol is in the .loader section, swap out the
3577 .loader symbol information. If this is an external symbol
3578 reference to a defined symbol, though, then wait until we get
3579 to the definition. */
3580 if (isym.n_sclass == C_EXT
3581 && *sym_hash != NULL
3582 && (*sym_hash)->ldsym != NULL
3584 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3586 struct xcoff_link_hash_entry *h;
3587 struct internal_ldsym *ldsym;
3591 if (isym.n_scnum > 0)
3593 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3594 ldsym->l_value = (isym.n_value
3595 + (*csectpp)->output_section->vma
3596 + (*csectpp)->output_offset
3601 ldsym->l_scnum = isym.n_scnum;
3602 ldsym->l_value = isym.n_value;
3605 ldsym->l_smtype = smtyp;
3606 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3607 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3608 || (h->flags & XCOFF_IMPORT) != 0)
3609 ldsym->l_smtype |= L_IMPORT;
3610 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3611 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3612 || (h->flags & XCOFF_EXPORT) != 0)
3613 ldsym->l_smtype |= L_EXPORT;
3614 if ((h->flags & XCOFF_ENTRY) != 0)
3615 ldsym->l_smtype |= L_ENTRY;
3617 ldsym->l_smclas = aux.x_csect.x_smclas;
3619 if (ldsym->l_ifile == (bfd_size_type) -1)
3621 else if (ldsym->l_ifile == 0)
3623 if ((ldsym->l_smtype & L_IMPORT) == 0)
3629 if (h->root.type == bfd_link_hash_defined
3630 || h->root.type == bfd_link_hash_defweak)
3631 impbfd = h->root.u.def.section->owner;
3632 else if (h->root.type == bfd_link_hash_undefined
3633 || h->root.type == bfd_link_hash_undefweak)
3634 impbfd = h->root.u.undef.abfd;
3642 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3643 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3650 BFD_ASSERT (h->ldindx >= 0);
3651 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3652 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3653 finfo->ldsym + h->ldindx - 3);
3661 add = 1 + isym.n_numaux;
3663 /* If we are skipping this csect, we want to skip this symbol. */
3664 if (*csectpp == NULL)
3667 /* If we garbage collected this csect, we want to skip this
3670 && xcoff_hash_table (finfo->info)->gc
3671 && ((*csectpp)->flags & SEC_MARK) == 0
3672 && *csectpp != bfd_abs_section_ptr)
3675 /* An XCOFF linker always skips C_STAT symbols. */
3677 && isymp->n_sclass == C_STAT)
3680 /* We skip all but the first TOC anchor. */
3682 && isymp->n_sclass == C_HIDEXT
3683 && aux.x_csect.x_smclas == XMC_TC0)
3685 if (finfo->toc_symindx != -1)
3689 finfo->toc_symindx = output_index;
3690 xcoff_data (finfo->output_bfd)->toc =
3691 ((*csectpp)->output_section->vma
3692 + (*csectpp)->output_offset
3695 xcoff_data (finfo->output_bfd)->toc_section =
3696 (*csectpp)->output_section;
3701 /* If we are stripping all symbols, we want to skip this one. */
3703 && finfo->info->strip == strip_all)
3706 /* We can skip resolved external references. */
3708 && isym.n_sclass == C_EXT
3710 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3713 /* We can skip common symbols if they got defined somewhere
3716 && isym.n_sclass == C_EXT
3718 && ((*sym_hash)->root.type != bfd_link_hash_common
3719 || (*sym_hash)->root.u.c.p->section != *csectpp)
3720 && ((*sym_hash)->root.type != bfd_link_hash_defined
3721 || (*sym_hash)->root.u.def.section != *csectpp))
3724 /* Skip local symbols if we are discarding them. */
3726 && finfo->info->discard == discard_all
3727 && isym.n_sclass != C_EXT
3728 && (isym.n_sclass != C_HIDEXT
3729 || smtyp != XTY_SD))
3732 /* If we stripping debugging symbols, and this is a debugging
3733 symbol, then skip it. */
3735 && finfo->info->strip == strip_debugger
3736 && isym.n_scnum == N_DEBUG)
3739 /* If some symbols are stripped based on the name, work out the
3740 name and decide whether to skip this symbol. We don't handle
3741 this correctly for symbols whose names are in the .debug
3742 section; to get it right we would need a new bfd_strtab_hash
3743 function to return the string given the index. */
3745 && (finfo->info->strip == strip_some
3746 || finfo->info->discard == discard_l)
3747 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3750 char buf[SYMNMLEN + 1];
3752 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3756 if ((finfo->info->strip == strip_some
3757 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
3759 || (finfo->info->discard == discard_l
3760 && (isym.n_sclass != C_EXT
3761 && (isym.n_sclass != C_HIDEXT
3762 || smtyp != XTY_SD))
3763 && strncmp (name, finfo->info->lprefix,
3764 finfo->info->lprefix_len) == 0))
3768 /* We can not skip the first TOC anchor. */
3771 && finfo->info->strip != strip_all)
3774 /* We now know whether we are to skip this symbol or not. */
3777 /* Adjust the symbol in order to output it. */
3779 if (isym._n._n_n._n_zeroes == 0
3780 && isym._n._n_n._n_offset != 0)
3782 /* This symbol has a long name. Enter it in the string
3783 table we are building. If *debug_index != -1, the
3784 name has already been entered in the .debug section. */
3785 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3786 isym._n._n_n._n_offset = *debug_index;
3792 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
3796 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3797 if (indx == (bfd_size_type) -1)
3799 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3803 if (isym.n_sclass != C_BSTAT
3804 && isym.n_sclass != C_ESTAT
3805 && isym.n_sclass != C_DECL
3806 && isym.n_scnum > 0)
3808 isym.n_scnum = (*csectpp)->output_section->target_index;
3809 isym.n_value += ((*csectpp)->output_section->vma
3810 + (*csectpp)->output_offset
3814 /* The value of a C_FILE symbol is the symbol index of the
3815 next C_FILE symbol. The value of the last C_FILE symbol
3816 is -1. We try to get this right, below, just before we
3817 write the symbols out, but in the general case we may
3818 have to write the symbol out twice. */
3819 if (isym.n_sclass == C_FILE)
3821 if (finfo->last_file_index != -1
3822 && finfo->last_file.n_value != (long) output_index)
3824 /* We must correct the value of the last C_FILE entry. */
3825 finfo->last_file.n_value = output_index;
3826 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3828 /* The last C_FILE symbol is in this input file. */
3829 bfd_coff_swap_sym_out (output_bfd,
3830 (PTR) &finfo->last_file,
3831 (PTR) (finfo->outsyms
3832 + ((finfo->last_file_index
3838 /* We have already written out the last C_FILE
3839 symbol. We need to write it out again. We
3840 borrow *outsym temporarily. */
3841 bfd_coff_swap_sym_out (output_bfd,
3842 (PTR) &finfo->last_file,
3844 if (bfd_seek (output_bfd,
3845 (obj_sym_filepos (output_bfd)
3846 + finfo->last_file_index * osymesz),
3848 || (bfd_write (outsym, osymesz, 1, output_bfd)
3854 finfo->last_file_index = output_index;
3855 finfo->last_file = isym;
3858 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3859 into the line numbers. We update the symbol values when
3860 we handle the line numbers. */
3861 if (isym.n_sclass == C_BINCL
3862 || isym.n_sclass == C_EINCL)
3864 isym.n_value = finfo->line_filepos;
3868 /* Output the symbol. */
3870 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
3872 *indexp = output_index;
3874 if (isym.n_sclass == C_EXT)
3877 struct xcoff_link_hash_entry *h;
3879 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3881 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3882 BFD_ASSERT (h != NULL);
3883 h->indx = output_index;
3886 /* If this is a symbol in the TOC which we may have merged
3887 (class XMC_TC), remember the symbol index of the TOC
3889 if (isym.n_sclass == C_HIDEXT
3890 && aux.x_csect.x_smclas == XMC_TC
3891 && *sym_hash != NULL)
3893 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3894 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3895 (*sym_hash)->u.toc_indx = output_index;
3898 output_index += add;
3899 outsym += add * osymesz;
3902 esym += add * isymesz;
3906 if (debug_index != NULL)
3909 for (--add; add > 0; --add)
3913 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3914 done in a separate pass, because we don't know the correct symbol
3915 indices until we have already decided which symbols we are going
3918 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3919 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3920 isymp = finfo->internal_syms;
3921 indexp = finfo->sym_indices;
3922 csectpp = xcoff_data (input_bfd)->csects;
3923 outsym = finfo->outsyms;
3924 while (esym < esym_end)
3928 add = 1 + isymp->n_numaux;
3931 esym += add * isymesz;
3936 if (isymp->n_sclass == C_BSTAT)
3938 struct internal_syment isym;
3941 /* The value of a C_BSTAT symbol is the symbol table
3942 index of the containing csect. */
3943 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
3944 indx = isym.n_value;
3945 if (indx < obj_raw_syment_count (input_bfd))
3949 symindx = finfo->sym_indices[indx];
3953 isym.n_value = symindx;
3954 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
3962 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3964 union internal_auxent aux;
3966 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
3967 isymp->n_sclass, i, isymp->n_numaux,
3970 if (isymp->n_sclass == C_FILE)
3972 /* This is the file name (or some comment put in by
3973 the compiler). If it is long, we must put it in
3974 the string table. */
3975 if (aux.x_file.x_n.x_zeroes == 0
3976 && aux.x_file.x_n.x_offset != 0)
3978 const char *filename;
3981 BFD_ASSERT (aux.x_file.x_n.x_offset
3982 >= STRING_SIZE_SIZE);
3983 if (strings == NULL)
3985 strings = _bfd_coff_read_string_table (input_bfd);
3986 if (strings == NULL)
3989 filename = strings + aux.x_file.x_n.x_offset;
3990 indx = _bfd_stringtab_add (finfo->strtab, filename,
3992 if (indx == (bfd_size_type) -1)
3994 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3997 else if ((isymp->n_sclass == C_EXT
3998 || isymp->n_sclass == C_HIDEXT)
3999 && i + 1 == isymp->n_numaux)
4001 /* We don't support type checking. I don't know if
4003 aux.x_csect.x_parmhash = 0;
4004 /* I don't think anybody uses these fields, but we'd
4005 better clobber them just in case. */
4006 aux.x_csect.x_stab = 0;
4007 aux.x_csect.x_snstab = 0;
4008 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4012 indx = aux.x_csect.x_scnlen.l;
4013 if (indx < obj_raw_syment_count (input_bfd))
4017 symindx = finfo->sym_indices[indx];
4019 aux.x_sym.x_tagndx.l = 0;
4021 aux.x_sym.x_tagndx.l = symindx;
4025 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4029 if (ISFCN (isymp->n_type)
4030 || ISTAG (isymp->n_sclass)
4031 || isymp->n_sclass == C_BLOCK)
4033 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4035 && indx < obj_raw_syment_count (input_bfd))
4037 /* We look forward through the symbol for
4038 the index of the next symbol we are going
4039 to include. I don't know if this is
4041 while (finfo->sym_indices[indx] < 0
4042 && indx < obj_raw_syment_count (input_bfd))
4044 if (indx >= obj_raw_syment_count (input_bfd))
4045 indx = output_index;
4047 indx = finfo->sym_indices[indx];
4048 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4052 indx = aux.x_sym.x_tagndx.l;
4053 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4057 symindx = finfo->sym_indices[indx];
4059 aux.x_sym.x_tagndx.l = 0;
4061 aux.x_sym.x_tagndx.l = symindx;
4065 /* Copy over the line numbers, unless we are stripping
4066 them. We do this on a symbol by symbol basis in
4067 order to more easily handle garbage collection. */
4068 if ((isymp->n_sclass == C_EXT
4069 || isymp->n_sclass == C_HIDEXT)
4071 && isymp->n_numaux > 1
4072 && ISFCN (isymp->n_type)
4073 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4075 if (finfo->info->strip != strip_none
4076 && finfo->info->strip != strip_some)
4077 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4080 asection *enclosing;
4081 unsigned int enc_count;
4082 bfd_size_type linoff;
4083 struct internal_lineno lin;
4086 enclosing = xcoff_section_data (abfd, o)->enclosing;
4087 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4088 if (oline != enclosing)
4090 if (bfd_seek (input_bfd,
4091 enclosing->line_filepos,
4093 || (bfd_read (finfo->linenos, linesz,
4094 enc_count, input_bfd)
4095 != linesz * enc_count))
4100 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4101 - enclosing->line_filepos);
4103 bfd_coff_swap_lineno_in (input_bfd,
4104 (PTR) (finfo->linenos + linoff),
4107 || ((bfd_size_type) lin.l_addr.l_symndx
4111 obj_coff_external_syms (input_bfd)))
4113 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4116 bfd_byte *linpend, *linp;
4118 bfd_size_type count;
4120 lin.l_addr.l_symndx = *indexp;
4121 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4122 (PTR) (finfo->linenos
4125 linpend = (finfo->linenos
4126 + enc_count * linesz);
4127 offset = (o->output_section->vma
4130 for (linp = finfo->linenos + linoff + linesz;
4134 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4136 if (lin.l_lnno == 0)
4138 lin.l_addr.l_paddr += offset;
4139 bfd_coff_swap_lineno_out (output_bfd,
4144 count = (linp - (finfo->linenos + linoff)) / linesz;
4146 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4147 (o->output_section->line_filepos
4148 + o->output_section->lineno_count * linesz);
4150 if (bfd_seek (output_bfd,
4151 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4153 || (bfd_write (finfo->linenos + linoff,
4154 linesz, count, output_bfd)
4158 o->output_section->lineno_count += count;
4162 struct internal_syment *iisp, *iispend;
4166 /* Update any C_BINCL or C_EINCL symbols
4167 that refer to a line number in the
4168 range we just output. */
4169 iisp = finfo->internal_syms;
4171 + obj_raw_syment_count (input_bfd));
4172 iindp = finfo->sym_indices;
4173 oos = finfo->outsyms;
4174 while (iisp < iispend)
4176 if ((iisp->n_sclass == C_BINCL
4177 || iisp->n_sclass == C_EINCL)
4178 && ((bfd_size_type) iisp->n_value
4179 >= enclosing->line_filepos + linoff)
4180 && ((bfd_size_type) iisp->n_value
4181 < (enclosing->line_filepos
4182 + enc_count * linesz)))
4184 struct internal_syment iis;
4186 bfd_coff_swap_sym_in (output_bfd,
4191 - enclosing->line_filepos
4193 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4194 bfd_coff_swap_sym_out (output_bfd,
4200 iisp += iisp->n_numaux + 1;
4201 iindp += iisp->n_numaux + 1;
4202 oos += (iisp->n_numaux + 1) * osymesz;
4209 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4210 isymp->n_sclass, i, isymp->n_numaux,
4222 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4223 symbol will be the first symbol in the next input file. In the
4224 normal case, this will save us from writing out the C_FILE symbol
4226 if (finfo->last_file_index != -1
4227 && (bfd_size_type) finfo->last_file_index >= syment_base)
4229 finfo->last_file.n_value = output_index;
4230 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
4231 (PTR) (finfo->outsyms
4232 + ((finfo->last_file_index - syment_base)
4236 /* Write the modified symbols to the output file. */
4237 if (outsym > finfo->outsyms)
4239 if (bfd_seek (output_bfd,
4240 obj_sym_filepos (output_bfd) + syment_base * osymesz,
4242 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
4244 != (bfd_size_type) (outsym - finfo->outsyms)))
4247 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4248 + (outsym - finfo->outsyms) / osymesz)
4251 obj_raw_syment_count (output_bfd) = output_index;
4254 /* Don't let the linker relocation routines discard the symbols. */
4255 keep_syms = obj_coff_keep_syms (input_bfd);
4256 obj_coff_keep_syms (input_bfd) = true;
4258 /* Relocate the contents of each section. */
4259 for (o = input_bfd->sections; o != NULL; o = o->next)
4263 if ((o->flags & SEC_HAS_CONTENTS) == 0
4264 || o->_raw_size == 0
4265 || (o->flags & SEC_IN_MEMORY) != 0)
4268 /* We have set filepos correctly for the sections we created to
4269 represent csects, so bfd_get_section_contents should work. */
4270 if (coff_section_data (input_bfd, o) != NULL
4271 && coff_section_data (input_bfd, o)->contents != NULL)
4272 contents = coff_section_data (input_bfd, o)->contents;
4275 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4276 (file_ptr) 0, o->_raw_size))
4278 contents = finfo->contents;
4281 if ((o->flags & SEC_RELOC) != 0)
4284 struct internal_reloc *internal_relocs;
4285 struct internal_reloc *irel;
4287 struct internal_reloc *irelend;
4288 struct xcoff_link_hash_entry **rel_hash;
4291 /* Read in the relocs. */
4292 target_index = o->output_section->target_index;
4293 internal_relocs = (xcoff_read_internal_relocs
4294 (input_bfd, o, false, finfo->external_relocs,
4296 (finfo->section_info[target_index].relocs
4297 + o->output_section->reloc_count)));
4298 if (internal_relocs == NULL)
4301 /* Call processor specific code to relocate the section
4303 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4307 finfo->internal_syms,
4308 xcoff_data (input_bfd)->csects))
4311 offset = o->output_section->vma + o->output_offset - o->vma;
4312 irel = internal_relocs;
4313 irelend = irel + o->reloc_count;
4314 rel_hash = (finfo->section_info[target_index].rel_hashes
4315 + o->output_section->reloc_count);
4316 for (; irel < irelend; irel++, rel_hash++)
4318 struct xcoff_link_hash_entry *h = NULL;
4319 struct internal_ldrel ldrel;
4323 /* Adjust the reloc address and symbol index. */
4325 irel->r_vaddr += offset;
4327 r_symndx = irel->r_symndx;
4331 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4333 && (irel->r_type == R_TOC
4334 || irel->r_type == R_GL
4335 || irel->r_type == R_TCL
4336 || irel->r_type == R_TRL
4337 || irel->r_type == R_TRLA))
4339 /* This is a TOC relative reloc with a symbol
4340 attached. The symbol should be the one which
4341 this reloc is for. We want to make this
4342 reloc against the TOC address of the symbol,
4343 not the symbol itself. */
4344 BFD_ASSERT (h->toc_section != NULL);
4345 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4346 if (h->u.toc_indx != -1)
4347 irel->r_symndx = h->u.toc_indx;
4350 struct xcoff_toc_rel_hash *n;
4351 struct xcoff_link_section_info *si;
4353 n = ((struct xcoff_toc_rel_hash *)
4354 bfd_alloc (finfo->output_bfd,
4355 sizeof (struct xcoff_toc_rel_hash)));
4358 bfd_set_error (bfd_error_no_memory);
4361 si = finfo->section_info + target_index;
4362 n->next = si->toc_rel_hashes;
4365 si->toc_rel_hashes = n;
4370 /* This is a global symbol. */
4372 irel->r_symndx = h->indx;
4375 /* This symbol is being written at the end
4376 of the file, and we do not yet know the
4377 symbol index. We save the pointer to the
4378 hash table entry in the rel_hash list.
4379 We set the indx field to -2 to indicate
4380 that this symbol must not be stripped. */
4389 indx = finfo->sym_indices[r_symndx];
4393 struct internal_syment *is;
4395 /* Relocations against a TC0 TOC anchor are
4396 automatically transformed to be against
4397 the TOC anchor in the output file. */
4398 is = finfo->internal_syms + r_symndx;
4399 if (is->n_sclass == C_HIDEXT
4400 && is->n_numaux > 0)
4403 union internal_auxent aux;
4407 obj_coff_external_syms (input_bfd))
4408 + ((r_symndx + is->n_numaux)
4410 bfd_coff_swap_aux_in (input_bfd, auxptr,
4411 is->n_type, is->n_sclass,
4415 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4416 && aux.x_csect.x_smclas == XMC_TC0)
4417 indx = finfo->toc_symindx;
4422 irel->r_symndx = indx;
4425 struct internal_syment *is;
4427 char buf[SYMNMLEN + 1];
4429 /* This reloc is against a symbol we are
4430 stripping. It would be possible to handle
4431 this case, but I don't think it's worth it. */
4432 is = finfo->internal_syms + r_symndx;
4434 name = (_bfd_coff_internal_syment_name
4435 (input_bfd, is, buf));
4439 if (! ((*finfo->info->callbacks->unattached_reloc)
4440 (finfo->info, name, input_bfd, o,
4447 switch (irel->r_type)
4451 || h->root.type == bfd_link_hash_defined
4452 || h->root.type == bfd_link_hash_defweak
4453 || h->root.type == bfd_link_hash_common)
4460 /* This reloc needs to be copied into the .loader
4462 ldrel.l_vaddr = irel->r_vaddr;
4464 ldrel.l_symndx = -1;
4466 || (h->root.type == bfd_link_hash_defined
4467 || h->root.type == bfd_link_hash_defweak
4468 || h->root.type == bfd_link_hash_common))
4473 sec = xcoff_data (input_bfd)->csects[r_symndx];
4474 else if (h->root.type == bfd_link_hash_common)
4475 sec = h->root.u.c.p->section;
4477 sec = h->root.u.def.section;
4478 sec = sec->output_section;
4480 if (strcmp (sec->name, ".text") == 0)
4482 else if (strcmp (sec->name, ".data") == 0)
4484 else if (strcmp (sec->name, ".bss") == 0)
4488 (*_bfd_error_handler)
4489 ("%s: loader reloc in unrecognized section `%s'",
4490 bfd_get_filename (input_bfd),
4492 bfd_set_error (bfd_error_nonrepresentable_section);
4500 (*_bfd_error_handler)
4501 ("%s: `%s' in loader reloc but not loader sym",
4502 bfd_get_filename (input_bfd),
4503 h->root.root.string);
4504 bfd_set_error (bfd_error_bad_value);
4507 ldrel.l_symndx = h->ldindx;
4509 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4510 ldrel.l_rsecnm = o->output_section->target_index;
4511 if (xcoff_hash_table (finfo->info)->textro
4512 && strcmp (o->output_section->name, ".text") == 0)
4514 (*_bfd_error_handler)
4515 ("%s: loader reloc in read-only section %s",
4516 bfd_get_filename (input_bfd),
4517 bfd_get_section_name (finfo->output_bfd,
4518 o->output_section));
4519 bfd_set_error (bfd_error_invalid_operation);
4522 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4524 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4533 /* We should never need a .loader reloc for a TOC
4539 o->output_section->reloc_count += o->reloc_count;
4542 /* Write out the modified section contents. */
4543 if (! bfd_set_section_contents (output_bfd, o->output_section,
4544 contents, o->output_offset,
4545 (o->_cooked_size != 0
4551 obj_coff_keep_syms (input_bfd) = keep_syms;
4553 if (! finfo->info->keep_memory)
4555 if (! _bfd_coff_free_symbols (input_bfd))
4565 /* Write out a non-XCOFF global symbol. */
4568 xcoff_write_global_symbol (h, p)
4569 struct xcoff_link_hash_entry *h;
4572 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4575 struct internal_syment isym;
4576 union internal_auxent aux;
4578 output_bfd = finfo->output_bfd;
4580 /* If this symbol was garbage collected, just skip it. */
4581 if (xcoff_hash_table (finfo->info)->gc
4582 && (h->flags & XCOFF_MARK) == 0)
4585 /* If we need a .loader section entry, write it out. */
4586 if (h->ldsym != NULL)
4588 struct internal_ldsym *ldsym;
4593 if (h->root.type == bfd_link_hash_undefined
4594 || h->root.type == bfd_link_hash_undefweak)
4597 ldsym->l_scnum = N_UNDEF;
4598 ldsym->l_smtype = XTY_ER;
4599 impbfd = h->root.u.undef.abfd;
4601 else if (h->root.type == bfd_link_hash_defined
4602 || h->root.type == bfd_link_hash_defweak)
4606 sec = h->root.u.def.section;
4607 ldsym->l_value = (sec->output_section->vma
4608 + sec->output_offset
4609 + h->root.u.def.value);
4610 ldsym->l_scnum = sec->output_section->target_index;
4611 ldsym->l_smtype = XTY_SD;
4612 impbfd = sec->owner;
4617 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4618 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4619 || (h->flags & XCOFF_IMPORT) != 0)
4620 ldsym->l_smtype |= L_IMPORT;
4621 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4622 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4623 || (h->flags & XCOFF_EXPORT) != 0)
4624 ldsym->l_smtype |= L_EXPORT;
4625 if ((h->flags & XCOFF_ENTRY) != 0)
4626 ldsym->l_smtype |= L_ENTRY;
4628 ldsym->l_smclas = h->smclas;
4630 if (ldsym->l_ifile == (bfd_size_type) -1)
4632 else if (ldsym->l_ifile == 0)
4634 if ((ldsym->l_smtype & L_IMPORT) == 0)
4636 else if (impbfd == NULL)
4640 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4641 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4647 BFD_ASSERT (h->ldindx >= 0);
4648 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4649 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4653 /* If this symbol needs global linkage code, write it out. */
4654 if (h->root.type == bfd_link_hash_defined
4655 && (h->root.u.def.section
4656 == xcoff_hash_table (finfo->info)->linkage_section))
4662 p = h->root.u.def.section->contents + h->root.u.def.value;
4664 /* The first instruction in the global linkage code loads a
4665 specific TOC element. */
4666 tocoff = (h->descriptor->toc_section->output_section->vma
4667 + h->descriptor->toc_section->output_offset
4668 - xcoff_data (output_bfd)->toc);
4669 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4670 tocoff += h->descriptor->u.toc_offset;
4671 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4673 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
4675 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
4678 /* If we created a TOC entry for this symbol, write out the required
4680 if ((h->flags & XCOFF_SET_TOC) != 0)
4685 struct internal_reloc *irel;
4686 struct internal_ldrel ldrel;
4688 tocsec = h->toc_section;
4689 osec = tocsec->output_section;
4690 oindx = osec->target_index;
4691 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4692 irel->r_vaddr = (osec->vma
4693 + tocsec->output_offset
4696 irel->r_symndx = h->indx;
4700 irel->r_symndx = obj_raw_syment_count (output_bfd);
4702 irel->r_type = R_POS;
4704 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4705 ++osec->reloc_count;
4707 BFD_ASSERT (h->ldindx >= 0);
4708 ldrel.l_vaddr = irel->r_vaddr;
4709 ldrel.l_symndx = h->ldindx;
4710 ldrel.l_rtype = (31 << 8) | R_POS;
4711 ldrel.l_rsecnm = oindx;
4712 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4720 && (finfo->info->strip == strip_all
4721 || (finfo->info->strip == strip_some
4722 && (bfd_hash_lookup (finfo->info->keep_hash,
4723 h->root.root.string, false, false)
4728 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
4731 outsym = finfo->outsyms;
4733 memset (&aux, 0, sizeof aux);
4735 h->indx = obj_raw_syment_count (output_bfd);
4737 if (strlen (h->root.root.string) <= SYMNMLEN)
4738 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
4745 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4747 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
4749 if (indx == (bfd_size_type) -1)
4751 isym._n._n_n._n_zeroes = 0;
4752 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4755 if (h->root.type == bfd_link_hash_undefined
4756 || h->root.type == bfd_link_hash_undefweak)
4759 isym.n_scnum = N_UNDEF;
4760 isym.n_sclass = C_EXT;
4761 aux.x_csect.x_smtyp = XTY_ER;
4763 else if (h->root.type == bfd_link_hash_defined
4764 || h->root.type == bfd_link_hash_defweak)
4766 struct xcoff_link_size_list *l;
4768 isym.n_value = (h->root.u.def.section->output_section->vma
4769 + h->root.u.def.section->output_offset
4770 + h->root.u.def.value);
4771 isym.n_scnum = h->root.u.def.section->output_section->target_index;
4772 isym.n_sclass = C_HIDEXT;
4773 aux.x_csect.x_smtyp = XTY_SD;
4775 if ((h->flags & XCOFF_HAS_SIZE) != 0)
4777 for (l = xcoff_hash_table (finfo->info)->size_list;
4783 aux.x_csect.x_scnlen.l = l->size;
4789 else if (h->root.type == bfd_link_hash_common)
4791 isym.n_value = (h->root.u.c.p->section->output_section->vma
4792 + h->root.u.c.p->section->output_offset);
4793 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
4794 isym.n_sclass = C_EXT;
4795 aux.x_csect.x_smtyp = XTY_CM;
4796 aux.x_csect.x_scnlen.l = h->root.u.c.size;
4801 isym.n_type = T_NULL;
4804 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4805 outsym += bfd_coff_symesz (output_bfd);
4807 aux.x_csect.x_smclas = h->smclas;
4809 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
4811 outsym += bfd_coff_auxesz (output_bfd);
4813 if (h->root.type == bfd_link_hash_defined
4814 || h->root.type == bfd_link_hash_defweak)
4816 /* We just output an SD symbol. Now output an LD symbol. */
4820 isym.n_sclass = C_EXT;
4821 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4822 outsym += bfd_coff_symesz (output_bfd);
4824 aux.x_csect.x_smtyp = XTY_LD;
4825 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
4827 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
4829 outsym += bfd_coff_auxesz (output_bfd);
4832 if (bfd_seek (output_bfd,
4833 (obj_sym_filepos (output_bfd)
4834 + (obj_raw_syment_count (output_bfd)
4835 * bfd_coff_symesz (output_bfd))),
4837 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
4838 != (bfd_size_type) (outsym - finfo->outsyms)))
4840 obj_raw_syment_count (output_bfd) +=
4841 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4846 /* Handle a link order which is supposed to generate a reloc. */
4849 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
4851 struct xcoff_final_link_info *finfo;
4852 asection *output_section;
4853 struct bfd_link_order *link_order;
4855 reloc_howto_type *howto;
4856 struct xcoff_link_hash_entry *h;
4860 struct internal_reloc *irel;
4861 struct xcoff_link_hash_entry **rel_hash_ptr;
4862 struct internal_ldrel ldrel;
4864 if (link_order->type == bfd_section_reloc_link_order)
4866 /* We need to somehow locate a symbol in the right section. The
4867 symbol must either have a value of zero, or we must adjust
4868 the addend by the value of the symbol. FIXME: Write this
4869 when we need it. The old linker couldn't handle this anyhow. */
4873 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4876 bfd_set_error (bfd_error_bad_value);
4880 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
4881 link_order->u.reloc.p->u.name,
4882 false, false, true);
4885 if (! ((*finfo->info->callbacks->unattached_reloc)
4886 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4887 (asection *) NULL, (bfd_vma) 0)))
4892 if (h->root.type == bfd_link_hash_common)
4894 hsec = h->root.u.c.p->section;
4897 else if (h->root.type == bfd_link_hash_defined
4898 || h->root.type == bfd_link_hash_defweak)
4900 hsec = h->root.u.def.section;
4901 hval = h->root.u.def.value;
4909 addend = link_order->u.reloc.p->addend;
4911 addend += (hsec->output_section->vma
4912 + hsec->output_offset
4919 bfd_reloc_status_type rstat;
4922 size = bfd_get_reloc_size (howto);
4923 buf = (bfd_byte *) bfd_zmalloc (size);
4926 bfd_set_error (bfd_error_no_memory);
4930 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
4936 case bfd_reloc_outofrange:
4938 case bfd_reloc_overflow:
4939 if (! ((*finfo->info->callbacks->reloc_overflow)
4940 (finfo->info, link_order->u.reloc.p->u.name,
4941 howto->name, addend, (bfd *) NULL, (asection *) NULL,
4949 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4950 (file_ptr) link_order->offset, size);
4956 /* Store the reloc information in the right place. It will get
4957 swapped and written out at the end of the final_link routine. */
4959 irel = (finfo->section_info[output_section->target_index].relocs
4960 + output_section->reloc_count);
4961 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
4962 + output_section->reloc_count);
4964 memset (irel, 0, sizeof (struct internal_reloc));
4965 *rel_hash_ptr = NULL;
4967 irel->r_vaddr = output_section->vma + link_order->offset;
4970 irel->r_symndx = h->indx;
4973 /* Set the index to -2 to force this symbol to get written out. */
4979 irel->r_type = howto->type;
4980 irel->r_size = howto->bitsize - 1;
4981 if (howto->complain_on_overflow == complain_overflow_signed)
4982 irel->r_size |= 0x80;
4984 ++output_section->reloc_count;
4986 /* Now output the reloc to the .loader section. */
4988 ldrel.l_vaddr = irel->r_vaddr;
4992 const char *secname;
4994 secname = hsec->output_section->name;
4996 if (strcmp (secname, ".text") == 0)
4998 else if (strcmp (secname, ".data") == 0)
5000 else if (strcmp (secname, ".bss") == 0)
5004 (*_bfd_error_handler)
5005 ("%s: loader reloc in unrecognized section `%s'",
5006 bfd_get_filename (output_bfd), secname);
5007 bfd_set_error (bfd_error_nonrepresentable_section);
5015 (*_bfd_error_handler)
5016 ("%s: `%s' in loader reloc but not loader sym",
5017 bfd_get_filename (output_bfd),
5018 h->root.root.string);
5019 bfd_set_error (bfd_error_bad_value);
5022 ldrel.l_symndx = h->ldindx;
5025 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5026 ldrel.l_rsecnm = output_section->target_index;
5027 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5033 /* Sort relocs by VMA. This is called via qsort. */
5036 xcoff_sort_relocs (p1, p2)
5040 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5041 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5043 if (r1->r_vaddr > r2->r_vaddr)
5045 else if (r1->r_vaddr < r2->r_vaddr)
5051 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5052 This is currently the only processor which uses XCOFF; I hope that
5053 will never change. */
5056 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
5057 input_section, contents, relocs, syms,
5060 struct bfd_link_info *info;
5062 asection *input_section;
5064 struct internal_reloc *relocs;
5065 struct internal_syment *syms;
5066 asection **sections;
5068 struct internal_reloc *rel;
5069 struct internal_reloc *relend;
5072 relend = rel + input_section->reloc_count;
5073 for (; rel < relend; rel++)
5076 struct xcoff_link_hash_entry *h;
5077 struct internal_syment *sym;
5080 struct reloc_howto_struct howto;
5081 bfd_reloc_status_type rstat;
5083 /* Relocation type R_REF is a special relocation type which is
5084 merely used to prevent garbage collection from occurring for
5085 the csect including the symbol which it references. */
5086 if (rel->r_type == R_REF)
5089 symndx = rel->r_symndx;
5099 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
5100 sym = syms + symndx;
5101 addend = - sym->n_value;
5104 /* We build the howto information on the fly. */
5106 howto.type = rel->r_type;
5107 howto.rightshift = 0;
5109 howto.bitsize = (rel->r_size & 0x1f) + 1;
5110 howto.pc_relative = false;
5112 if ((rel->r_size & 0x80) != 0)
5113 howto.complain_on_overflow = complain_overflow_signed;
5115 howto.complain_on_overflow = complain_overflow_bitfield;
5116 howto.special_function = NULL;
5117 howto.name = "internal";
5118 howto.partial_inplace = true;
5119 if (howto.bitsize == 32)
5120 howto.src_mask = howto.dst_mask = 0xffffffff;
5123 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
5124 if (howto.bitsize == 16)
5127 howto.pcrel_offset = false;
5137 sec = bfd_abs_section_ptr;
5142 sec = sections[symndx];
5143 val = (sec->output_section->vma
5144 + sec->output_offset
5151 if (h->root.type == bfd_link_hash_defined
5152 || h->root.type == bfd_link_hash_defweak)
5156 sec = h->root.u.def.section;
5157 val = (h->root.u.def.value
5158 + sec->output_section->vma
5159 + sec->output_offset);
5161 else if (h->root.type == bfd_link_hash_common)
5165 sec = h->root.u.c.p->section;
5166 val = (sec->output_section->vma
5167 + sec->output_offset);
5169 else if ((h->flags & XCOFF_REF_DYNAMIC) != 0
5170 || (h->flags & XCOFF_IMPORT) != 0)
5172 /* Every symbol in a shared object is defined somewhere. */
5175 else if (! info->relocateable)
5177 if (! ((*info->callbacks->undefined_symbol)
5178 (info, h->root.root.string, input_bfd, input_section,
5179 rel->r_vaddr - input_section->vma)))
5184 /* I took the relocation type definitions from two documents:
5185 the PowerPC AIX Version 4 Application Binary Interface, First
5186 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5187 32-Bit Hardware Implementation (June 30, 1994). Differences
5188 between the documents are noted below. */
5190 switch (rel->r_type)
5195 /* These relocs are defined by the PowerPC ABI to be
5196 relative branches which use half of the difference
5197 between the symbol and the program counter. I can't
5198 quite figure out when this is useful. These relocs are
5199 not defined by the PowerOpen ABI. */
5201 (*_bfd_error_handler)
5202 ("%s: unsupported relocation type 0x%02x",
5203 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
5204 bfd_set_error (bfd_error_bad_value);
5207 /* Simple positive relocation. */
5210 /* Simple negative relocation. */
5214 /* Simple PC relative relocation. */
5215 howto.pc_relative = true;
5218 /* TOC relative relocation. The value in the instruction in
5219 the input file is the offset from the input file TOC to
5220 the desired location. We want the offset from the final
5221 TOC to the desired location. We have:
5226 so we must change insn by on - in.
5229 /* Global linkage relocation. The value of this relocation
5230 is the address of the entry in the TOC section. */
5232 /* Local object TOC address. I can't figure out the
5233 difference between this and case R_GL. */
5235 /* TOC relative relocation. A TOC relative load instruction
5236 which may be changed to a load address instruction.
5237 FIXME: We don't currently implement this optimization. */
5239 /* TOC relative relocation. This is a TOC relative load
5240 address instruction which may be changed to a load
5241 instruction. FIXME: I don't know if this is the correct
5243 if (h != NULL && h->toc_section == NULL)
5245 (*_bfd_error_handler)
5246 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5247 bfd_get_filename (input_bfd), rel->r_vaddr,
5248 h->root.root.string);
5249 bfd_set_error (bfd_error_bad_value);
5254 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5255 val = (h->toc_section->output_section->vma
5256 + h->toc_section->output_offset);
5258 val = ((val - xcoff_data (output_bfd)->toc)
5259 - (sym->n_value - xcoff_data (input_bfd)->toc));
5263 /* Absolute branch. We don't want to mess with the lower
5264 two bits of the instruction. */
5266 /* The PowerPC ABI defines this as an absolute call which
5267 may be modified to become a relative call. The PowerOpen
5268 ABI does not define this relocation type. */
5270 /* Absolute branch which may be modified to become a
5273 /* The PowerPC ABI defines this as an absolute branch to a
5274 fixed address which may be modified to an absolute branch
5275 to a symbol. The PowerOpen ABI does not define this
5278 /* The PowerPC ABI defines this as an absolute branch to a
5279 fixed address which may be modified to a relative branch.
5280 The PowerOpen ABI does not define this relocation type. */
5281 howto.src_mask &= ~3;
5282 howto.dst_mask = howto.src_mask;
5285 /* Relative branch. We don't want to mess with the lower
5286 two bits of the instruction. */
5288 /* The PowerPC ABI defines this as a relative call which may
5289 be modified to become an absolute call. The PowerOpen
5290 ABI does not define this relocation type. */
5292 /* A relative branch which may be modified to become an
5293 absolute branch. FIXME: We don't implement this,
5294 although we should for symbols of storage mapping class
5296 howto.pc_relative = true;
5297 howto.src_mask &= ~3;
5298 howto.dst_mask = howto.src_mask;
5301 /* The PowerPC AIX ABI describes this as a load which may be
5302 changed to a load address. The PowerOpen ABI says this
5303 is the same as case R_POS. */
5306 /* The PowerPC AIX ABI describes this as a load address
5307 which may be changed to a load. The PowerOpen ABI says
5308 this is the same as R_POS. */
5312 /* If we see an R_BR or R_RBR reloc which is jumping to global
5313 linkage code, and it is followed by an appropriate cror nop
5314 instruction, we replace the cror with lwz r2,20(r1). This
5315 restores the TOC after the glink code. Contrariwise, if the
5316 call is followed by a lwz r2,20(r1), but the call is not
5317 going to global linkage code, we can replace the load with a
5319 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
5321 && h->root.type == bfd_link_hash_defined
5322 && (rel->r_vaddr - input_section->vma + 8
5323 <= input_section->_cooked_size))
5328 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
5329 next = bfd_get_32 (input_bfd, pnext);
5330 if (h->smclas == XMC_GL)
5332 if (next == 0x4def7b82 /* cror 15,15,15 */
5333 || next == 0x4ffffb82) /* cror 31,31,31 */
5334 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
5338 if (next == 0x80410014) /* lwz r1,20(r1) */
5339 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
5343 /* A PC relative reloc includes the section address. */
5344 if (howto.pc_relative)
5345 addend += input_section->vma;
5347 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
5349 rel->r_vaddr - input_section->vma,
5358 case bfd_reloc_overflow:
5361 char buf[SYMNMLEN + 1];
5362 char howto_name[10];
5367 name = h->root.root.string;
5370 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
5374 sprintf (howto_name, "0x%02x", rel->r_type);
5376 if (! ((*info->callbacks->reloc_overflow)
5377 (info, name, howto_name, (bfd_vma) 0, input_bfd,
5378 input_section, rel->r_vaddr - input_section->vma)))