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;
885 unsigned int csect_index;
886 asection *first_csect;
887 bfd_size_type symesz;
890 struct reloc_info_struct
892 struct internal_reloc *relocs;
895 } *reloc_info = NULL;
897 if ((abfd->flags & DYNAMIC) != 0
898 && ! info->static_link)
899 return xcoff_link_add_dynamic_symbols (abfd, info);
901 n_tmask = coff_data (abfd)->local_n_tmask;
902 n_btshft = coff_data (abfd)->local_n_btshft;
904 /* Define macros so that ISFCN, et. al., macros work correctly. */
905 #define N_TMASK n_tmask
906 #define N_BTSHFT n_btshft
908 /* We need to build a .loader section, so we do it here. This won't
909 work if we're producing an XCOFF output file with no non dynamic
910 XCOFF input files. FIXME. */
911 if (xcoff_hash_table (info)->loader_section == NULL)
915 lsec = bfd_make_section_anyway (abfd, ".loader");
918 xcoff_hash_table (info)->loader_section = lsec;
919 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
921 /* Likewise for the linkage section. */
922 if (xcoff_hash_table (info)->linkage_section == NULL)
926 lsec = bfd_make_section_anyway (abfd, ".gl");
929 xcoff_hash_table (info)->linkage_section = lsec;
930 lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
932 /* Likewise for the TOC section. */
933 if (xcoff_hash_table (info)->toc_section == NULL)
937 tsec = bfd_make_section_anyway (abfd, ".tc");
940 xcoff_hash_table (info)->toc_section = tsec;
941 tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
942 tsec->alignment_power = 2;
944 /* Likewise for the .debug section. */
945 if (xcoff_hash_table (info)->debug_section == NULL)
949 dsec = bfd_make_section_anyway (abfd, ".debug");
952 xcoff_hash_table (info)->debug_section = dsec;
953 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
956 if (info->keep_memory)
957 default_copy = false;
961 symcount = obj_raw_syment_count (abfd);
963 /* We keep a list of the linker hash table entries that correspond
964 to each external symbol. */
965 sym_hash = ((struct xcoff_link_hash_entry **)
968 * sizeof (struct xcoff_link_hash_entry *))));
969 if (sym_hash == NULL && symcount != 0)
971 bfd_set_error (bfd_error_no_memory);
974 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
976 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
978 /* Because of the weird stuff we are doing with XCOFF csects, we can
979 not easily determine which section a symbol is in, so we store
980 the information in the tdata for the input file. */
981 csect_cache = ((asection **)
982 bfd_alloc (abfd, symcount * sizeof (asection *)));
983 if (csect_cache == NULL && symcount != 0)
985 bfd_set_error (bfd_error_no_memory);
988 xcoff_data (abfd)->csects = csect_cache;
989 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
991 /* While splitting sections into csects, we need to assign the
992 relocs correctly. The relocs and the csects must both be in
993 order by VMA within a given section, so we handle this by
994 scanning along the relocs as we process the csects. We index
995 into reloc_info using the section target_index. */
996 reloc_info = ((struct reloc_info_struct *)
997 malloc ((abfd->section_count + 1)
998 * sizeof (struct reloc_info_struct)));
999 if (reloc_info == NULL)
1001 bfd_set_error (bfd_error_no_memory);
1004 memset ((PTR) reloc_info, 0,
1005 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1007 /* Read in the relocs and line numbers for each section. */
1008 linesz = bfd_coff_linesz (abfd);
1010 for (o = abfd->sections; o != NULL; o = o->next)
1013 if ((o->flags & SEC_RELOC) != 0)
1015 reloc_info[o->target_index].relocs =
1016 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1017 false, (struct internal_reloc *) NULL);
1018 reloc_info[o->target_index].csects =
1019 (asection **) malloc (o->reloc_count * sizeof (asection *));
1020 if (reloc_info[o->target_index].csects == NULL)
1022 bfd_set_error (bfd_error_no_memory);
1025 memset (reloc_info[o->target_index].csects, 0,
1026 o->reloc_count * sizeof (asection *));
1029 if ((info->strip == strip_none || info->strip == strip_some)
1030 && o->lineno_count > 0)
1034 linenos = (bfd_byte *) malloc (o->lineno_count * linesz);
1035 if (linenos == NULL)
1037 bfd_set_error (bfd_error_no_memory);
1040 reloc_info[o->target_index].linenos = linenos;
1041 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1042 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1043 != linesz * o->lineno_count))
1048 /* Don't let the linker relocation routines discard the symbols. */
1049 keep_syms = obj_coff_keep_syms (abfd);
1050 obj_coff_keep_syms (abfd) = true;
1056 symesz = bfd_coff_symesz (abfd);
1057 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1058 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1059 esym_end = esym + symcount * symesz;
1060 while (esym < esym_end)
1062 struct internal_syment sym;
1063 union internal_auxent aux;
1065 char buf[SYMNMLEN + 1];
1070 struct xcoff_link_hash_entry *set_toc;
1072 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1074 /* In this pass we are only interested in symbols with csect
1076 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1078 if (sym.n_sclass == C_FILE && csect != NULL)
1080 xcoff_section_data (abfd, csect)->last_symndx =
1082 - (bfd_byte *) obj_coff_external_syms (abfd))
1088 *csect_cache = csect;
1089 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1090 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1092 *csect_cache = NULL;
1093 esym += (sym.n_numaux + 1) * symesz;
1094 sym_hash += sym.n_numaux + 1;
1095 csect_cache += sym.n_numaux + 1;
1099 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1103 /* If this symbol has line number information attached to it,
1104 and we're not stripping it, count the number of entries and
1105 add them to the count for this csect. In the final link pass
1106 we are going to attach line number information by symbol,
1107 rather than by section, in order to more easily handle
1108 garbage collection. */
1109 if ((info->strip == strip_none || info->strip == strip_some)
1112 && ISFCN (sym.n_type))
1114 union internal_auxent auxlin;
1116 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1117 sym.n_type, sym.n_sclass,
1118 0, sym.n_numaux, (PTR) &auxlin);
1119 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1121 asection *enclosing;
1122 bfd_size_type linoff;
1124 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1125 if (enclosing == NULL)
1127 (*_bfd_error_handler)
1128 ("%s: `%s' has line numbers but no enclosing section",
1129 bfd_get_filename (abfd), name);
1130 bfd_set_error (bfd_error_bad_value);
1133 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1134 - enclosing->line_filepos);
1135 if (linoff < enclosing->lineno_count * linesz)
1137 struct internal_lineno lin;
1138 bfd_byte *linpstart;
1140 linpstart = (reloc_info[enclosing->target_index].linenos
1142 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1144 && ((bfd_size_type) lin.l_addr.l_symndx
1146 - (bfd_byte *) obj_coff_external_syms (abfd))
1149 bfd_byte *linpend, *linp;
1151 linpend = (reloc_info[enclosing->target_index].linenos
1152 + enclosing->lineno_count * linesz);
1153 for (linp = linpstart + linesz;
1157 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1159 if (lin.l_lnno == 0)
1162 csect->lineno_count += (linp - linpstart) / linesz;
1163 /* The setting of line_filepos will only be
1164 useful if all the line number entries for a
1165 csect are contiguous; this only matters for
1167 if (csect->line_filepos == 0)
1168 csect->line_filepos =
1169 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1175 /* Pick up the csect auxiliary information. */
1177 if (sym.n_numaux == 0)
1179 (*_bfd_error_handler)
1180 ("%s: class %d symbol `%s' has no aux entries",
1181 bfd_get_filename (abfd), sym.n_sclass, name);
1182 bfd_set_error (bfd_error_bad_value);
1186 bfd_coff_swap_aux_in (abfd,
1187 (PTR) (esym + symesz * sym.n_numaux),
1188 sym.n_type, sym.n_sclass,
1189 sym.n_numaux - 1, sym.n_numaux,
1192 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1202 (*_bfd_error_handler)
1203 ("%s: symbol `%s' has unrecognized csect type %d",
1204 bfd_get_filename (abfd), name, smtyp);
1205 bfd_set_error (bfd_error_bad_value);
1209 /* This is an external reference. */
1210 if (sym.n_sclass == C_HIDEXT
1211 || sym.n_scnum != N_UNDEF
1212 || aux.x_csect.x_scnlen.l != 0)
1214 (*_bfd_error_handler)
1215 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1216 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1217 aux.x_csect.x_scnlen.l);
1218 bfd_set_error (bfd_error_bad_value);
1221 section = bfd_und_section_ptr;
1225 /* This is a csect definition. */
1229 xcoff_section_data (abfd, csect)->last_symndx =
1231 - (bfd_byte *) obj_coff_external_syms (abfd))
1238 /* When we see a TOC anchor, we record the TOC value. */
1239 if (aux.x_csect.x_smclas == XMC_TC0)
1241 if (sym.n_sclass != C_HIDEXT
1242 || aux.x_csect.x_scnlen.l != 0)
1244 (*_bfd_error_handler)
1245 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1246 bfd_get_filename (abfd), name, sym.n_sclass,
1247 aux.x_csect.x_scnlen.l);
1248 bfd_set_error (bfd_error_bad_value);
1251 xcoff_data (abfd)->toc = sym.n_value;
1254 /* We must merge TOC entries for the same symbol. We can
1255 merge two TOC entries if they are both C_HIDEXT, they
1256 both have the same name, they are both 4 bytes long, and
1257 they both have a relocation table entry for an external
1258 symbol with the same name. Unfortunately, this means
1259 that we must look through the relocations. Ick. */
1260 if (aux.x_csect.x_smclas == XMC_TC
1261 && sym.n_sclass == C_HIDEXT
1262 && aux.x_csect.x_scnlen.l == 4
1263 && info->hash->creator == abfd->xvec)
1265 asection *enclosing;
1266 struct internal_reloc *relocs;
1267 bfd_size_type relindx;
1268 struct internal_reloc *rel;
1270 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1271 if (enclosing == NULL)
1274 relocs = reloc_info[enclosing->target_index].relocs;
1275 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1277 rel = relocs + relindx;
1278 if (relindx < enclosing->reloc_count
1279 && rel->r_vaddr == (bfd_vma) sym.n_value
1280 && rel->r_size == 31
1281 && rel->r_type == R_POS)
1284 struct internal_syment relsym;
1286 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1287 + rel->r_symndx * symesz);
1288 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1289 if (relsym.n_sclass == C_EXT)
1291 const char *relname;
1292 char relbuf[SYMNMLEN + 1];
1294 struct xcoff_link_hash_entry *h;
1296 /* At this point we know that the TOC entry is
1297 for an externally visible symbol. */
1298 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1300 if (relname == NULL)
1303 /* We only merge TOC entries if the TC name is
1304 the same as the symbol name. This handles
1305 the normal case, but not common cases like
1306 SYM.P4 which gcc generates to store SYM + 4
1307 in the TOC. FIXME. */
1308 if (strcmp (name, relname) == 0)
1310 copy = (! info->keep_memory
1311 || relsym._n._n_n._n_zeroes != 0
1312 || relsym._n._n_n._n_offset == 0);
1313 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1314 relname, true, copy,
1319 /* At this point h->root.type could be
1320 bfd_link_hash_new. That should be OK,
1321 since we know for sure that we will come
1322 across this symbol as we step through the
1325 /* We store h in *sym_hash for the
1326 convenience of the relocate_section
1330 if (h->toc_section != NULL)
1332 asection **rel_csects;
1334 /* We already have a TOC entry for this
1335 symbol, so we can just ignore this
1338 reloc_info[enclosing->target_index].csects;
1339 rel_csects[relindx] = bfd_und_section_ptr;
1343 /* We are about to create a TOC entry for
1351 /* We need to create a new section. We get the name from
1352 the csect storage mapping class, so that the linker can
1353 accumulate similar csects together. */
1355 static const char *csect_name_by_class[] =
1357 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1358 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1361 const char *csect_name;
1362 asection *enclosing;
1364 if ((aux.x_csect.x_smclas >=
1365 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1366 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1368 (*_bfd_error_handler)
1369 ("%s: symbol `%s' has unrecognized smclas %d",
1370 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1371 bfd_set_error (bfd_error_bad_value);
1375 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1376 csect = bfd_make_section_anyway (abfd, csect_name);
1379 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1380 if (enclosing == NULL)
1382 if (! bfd_is_abs_section (enclosing)
1383 && ((bfd_vma) sym.n_value < enclosing->vma
1384 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1385 > enclosing->vma + enclosing->_raw_size)))
1387 (*_bfd_error_handler)
1388 ("%s: csect `%s' not in enclosing section",
1389 bfd_get_filename (abfd), name);
1390 bfd_set_error (bfd_error_bad_value);
1393 csect->vma = sym.n_value;
1394 csect->filepos = (enclosing->filepos
1397 csect->_raw_size = aux.x_csect.x_scnlen.l;
1398 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1399 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1401 /* Record the enclosing section in the tdata for this new
1403 csect->used_by_bfd =
1404 ((struct coff_section_tdata *)
1405 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1406 if (csect->used_by_bfd == NULL)
1408 bfd_set_error (bfd_error_no_memory);
1411 coff_section_data (abfd, csect)->tdata =
1412 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1413 if (coff_section_data (abfd, csect)->tdata == NULL)
1415 bfd_set_error (bfd_error_no_memory);
1418 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1419 xcoff_section_data (abfd, csect)->lineno_count =
1420 enclosing->lineno_count;
1422 if (enclosing->owner == abfd)
1424 struct internal_reloc *relocs;
1425 bfd_size_type relindx;
1426 struct internal_reloc *rel;
1427 asection **rel_csect;
1429 relocs = reloc_info[enclosing->target_index].relocs;
1430 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1432 rel = relocs + relindx;
1433 rel_csect = (reloc_info[enclosing->target_index].csects
1435 csect->rel_filepos = (enclosing->rel_filepos
1436 + relindx * bfd_coff_relsz (abfd));
1437 while (relindx < enclosing->reloc_count
1438 && *rel_csect == NULL
1439 && rel->r_vaddr < csect->vma + csect->_raw_size)
1442 csect->flags |= SEC_RELOC;
1443 ++csect->reloc_count;
1450 /* There are a number of other fields and section flags
1451 which we do not bother to set. */
1453 csect_index = ((esym
1454 - (bfd_byte *) obj_coff_external_syms (abfd))
1457 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1459 if (first_csect == NULL)
1460 first_csect = csect;
1462 /* If this symbol is C_EXT, we treat it as starting at the
1463 beginning of the newly created section. */
1464 if (sym.n_sclass == C_EXT)
1470 /* If this is a TOC section for a symbol, record it. */
1471 if (set_toc != NULL)
1472 set_toc->toc_section = csect;
1477 /* This is a label definition. The x_scnlen field is the
1478 symbol index of the csect. I believe that this must
1479 always follow the appropriate XTY_SD symbol, so I will
1485 if (aux.x_csect.x_scnlen.l < 0
1486 || (aux.x_csect.x_scnlen.l
1487 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1491 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1493 || (section->flags & SEC_HAS_CONTENTS) == 0)
1498 (*_bfd_error_handler)
1499 ("%s: misplaced XTY_LD `%s'",
1500 bfd_get_filename (abfd), name);
1501 bfd_set_error (bfd_error_bad_value);
1505 value = sym.n_value - csect->vma;
1510 /* This is an unitialized csect. We could base the name on
1511 the storage mapping class, but we don't bother. If this
1512 csect is externally visible, it is a common symbol. */
1516 xcoff_section_data (abfd, csect)->last_symndx =
1518 - (bfd_byte *) obj_coff_external_syms (abfd))
1522 csect = bfd_make_section_anyway (abfd, ".bss");
1525 csect->vma = sym.n_value;
1526 csect->_raw_size = aux.x_csect.x_scnlen.l;
1527 csect->flags |= SEC_ALLOC;
1528 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1529 /* There are a number of other fields and section flags
1530 which we do not bother to set. */
1532 csect_index = ((esym
1533 - (bfd_byte *) obj_coff_external_syms (abfd))
1536 csect->used_by_bfd =
1537 ((struct coff_section_tdata *)
1538 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1539 if (csect->used_by_bfd == NULL)
1541 bfd_set_error (bfd_error_no_memory);
1544 coff_section_data (abfd, csect)->tdata =
1545 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1546 if (coff_section_data (abfd, csect)->tdata == NULL)
1548 bfd_set_error (bfd_error_no_memory);
1551 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1553 if (first_csect == NULL)
1554 first_csect = csect;
1556 if (sym.n_sclass == C_EXT)
1558 csect->flags |= SEC_IS_COMMON;
1559 csect->_raw_size = 0;
1561 value = aux.x_csect.x_scnlen.l;
1567 /* Now we have enough information to add the symbol to the
1568 linker hash table. */
1570 if (sym.n_sclass == C_EXT)
1574 BFD_ASSERT (section != NULL);
1576 /* We must copy the name into memory if we got it from the
1577 syment itself, rather than the string table. */
1578 copy = default_copy;
1579 if (sym._n._n_n._n_zeroes != 0
1580 || sym._n._n_n._n_offset == 0)
1583 if (info->hash->creator == abfd->xvec)
1585 /* If we are statically linking a shared object, it is
1586 OK for symbol redefinitions to occur. I can't figure
1587 out just what the XCOFF linker is doing, but
1588 something like this is required for -bnso to work. */
1589 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1590 name, true, copy, false);
1591 if (*sym_hash == NULL)
1593 if (((*sym_hash)->root.type == bfd_link_hash_defined
1594 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1595 && ! bfd_is_und_section (section)
1596 && ! bfd_is_com_section (section))
1598 if ((abfd->flags & DYNAMIC) != 0)
1600 section = bfd_und_section_ptr;
1603 else if (((*sym_hash)->root.u.def.section->owner->flags
1606 (*sym_hash)->root.type = bfd_link_hash_undefined;
1607 (*sym_hash)->root.u.undef.abfd =
1608 (*sym_hash)->root.u.def.section->owner;
1613 /* _bfd_generic_link_add_one_symbol may call the linker to
1614 generate an error message, and the linker may try to read
1615 the symbol table to give a good error. Right now, the
1616 line numbers are in an inconsistent state, since they are
1617 counted both in the real sections and in the new csects.
1618 We need to leave the count in the real sections so that
1619 the linker can report the line number of the error
1620 correctly, so temporarily clobber the link to the csects
1621 so that the linker will not try to read the line numbers
1622 a second time from the csects. */
1623 BFD_ASSERT (last_real->next == first_csect);
1624 last_real->next = NULL;
1625 if (! (_bfd_generic_link_add_one_symbol
1626 (info, abfd, name, flags, section, value,
1627 (const char *) NULL, copy, true,
1628 (struct bfd_link_hash_entry **) sym_hash)))
1630 last_real->next = first_csect;
1632 if (smtyp == XTY_CM)
1634 if ((*sym_hash)->root.type != bfd_link_hash_common
1635 || (*sym_hash)->root.u.c.p->section != csect)
1637 /* We don't need the common csect we just created. */
1638 csect->_raw_size = 0;
1642 (*sym_hash)->root.u.c.p->alignment_power
1643 = csect->alignment_power;
1647 if (info->hash->creator == abfd->xvec)
1651 if (smtyp == XTY_ER || smtyp == XTY_CM)
1652 flag = XCOFF_REF_REGULAR;
1654 flag = XCOFF_DEF_REGULAR;
1655 (*sym_hash)->flags |= flag;
1657 if ((*sym_hash)->smclas == XMC_UA
1658 || flag == XCOFF_DEF_REGULAR)
1659 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1663 *csect_cache = csect;
1665 esym += (sym.n_numaux + 1) * symesz;
1666 sym_hash += sym.n_numaux + 1;
1667 csect_cache += sym.n_numaux + 1;
1670 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1672 /* Make sure that we have seen all the relocs. */
1673 for (o = abfd->sections; o != first_csect; o = o->next)
1675 /* Reset the section size and the line numebr count, since the
1676 data is now attached to the csects. Don't reset the size of
1677 the .debug section, since we need to read it below in
1678 bfd_xcoff_size_dynamic_sections. */
1679 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1681 o->lineno_count = 0;
1683 if ((o->flags & SEC_RELOC) != 0)
1686 struct internal_reloc *rel;
1687 asection **rel_csect;
1689 rel = reloc_info[o->target_index].relocs;
1690 rel_csect = reloc_info[o->target_index].csects;
1691 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1693 if (*rel_csect == NULL)
1695 (*_bfd_error_handler)
1696 ("%s: reloc %s:%d not in csect",
1697 bfd_get_filename (abfd), o->name, i);
1698 bfd_set_error (bfd_error_bad_value);
1702 /* We identify all symbols which are called, so that we
1703 can create glue code for calls to functions imported
1704 from dynamic objects. */
1705 if (info->hash->creator == abfd->xvec
1706 && *rel_csect != bfd_und_section_ptr
1707 && (rel->r_type == R_BR
1708 || rel->r_type == R_RBR)
1709 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1711 struct xcoff_link_hash_entry *h;
1713 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1714 h->flags |= XCOFF_CALLED;
1715 /* If the symbol name starts with a period, it is
1716 the code of a function. If the symbol is
1717 currently undefined, then add an undefined symbol
1718 for the function descriptor. This should do no
1719 harm, because any regular object that defines the
1720 function should also define the function
1721 descriptor. It helps, because it means that we
1722 will identify the function descriptor with a
1723 dynamic object if a dynamic object defines it. */
1724 if (h->root.root.string[0] == '.'
1725 && h->descriptor == NULL)
1727 struct xcoff_link_hash_entry *hds;
1729 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1730 h->root.root.string + 1,
1734 if (hds->root.type == bfd_link_hash_new)
1736 if (! (_bfd_generic_link_add_one_symbol
1737 (info, abfd, hds->root.root.string,
1738 (flagword) 0, bfd_und_section_ptr,
1739 (bfd_vma) 0, (const char *) NULL, false,
1741 (struct bfd_link_hash_entry **) NULL)))
1744 h->descriptor = hds;
1749 free (reloc_info[o->target_index].csects);
1750 reloc_info[o->target_index].csects = NULL;
1752 /* Reset SEC_RELOC and the reloc_count, since the reloc
1753 information is now attached to the csects. */
1754 o->flags &=~ SEC_RELOC;
1757 /* If we are not keeping memory, free the reloc information. */
1758 if (! info->keep_memory
1759 && coff_section_data (abfd, o) != NULL
1760 && coff_section_data (abfd, o)->relocs != NULL
1761 && ! coff_section_data (abfd, o)->keep_relocs)
1763 free (coff_section_data (abfd, o)->relocs);
1764 coff_section_data (abfd, o)->relocs = NULL;
1768 /* Free up the line numbers. FIXME: We could cache these
1769 somewhere for the final link, to avoid reading them again. */
1770 if (reloc_info[o->target_index].linenos != NULL)
1772 free (reloc_info[o->target_index].linenos);
1773 reloc_info[o->target_index].linenos = NULL;
1779 obj_coff_keep_syms (abfd) = keep_syms;
1784 if (reloc_info != NULL)
1786 for (o = abfd->sections; o != NULL; o = o->next)
1788 if (reloc_info[o->target_index].csects != NULL)
1789 free (reloc_info[o->target_index].csects);
1790 if (reloc_info[o->target_index].linenos != NULL)
1791 free (reloc_info[o->target_index].linenos);
1795 obj_coff_keep_syms (abfd) = keep_syms;
1802 /* This function is used to add symbols from a dynamic object to the
1803 global symbol table. */
1806 xcoff_link_add_dynamic_symbols (abfd, info)
1808 struct bfd_link_info *info;
1810 bfd_size_type symesz;
1813 struct xcoff_import_file *n;
1818 struct xcoff_import_file **pp;
1820 /* We can only handle a dynamic object if we are generating an XCOFF
1822 if (info->hash->creator != abfd->xvec)
1824 (*_bfd_error_handler)
1825 ("%s: XCOFF shared object when not producing XCOFF output",
1826 bfd_get_filename (abfd));
1827 bfd_set_error (bfd_error_invalid_operation);
1831 /* Remove the sections from this object, so that they do not get
1832 included in the link. */
1833 abfd->sections = NULL;
1835 symesz = bfd_coff_symesz (abfd);
1836 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1837 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1838 while (esym < esym_end)
1840 struct internal_syment sym;
1842 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1844 /* I think that every symbol mentioned in a dynamic object must
1845 be defined by that object, perhaps by importing it from
1846 another dynamic object. All we have to do is look up each
1847 external symbol. If we have already put it in the hash
1848 table, we simply set a flag indicating that it appears in a
1851 if (sym.n_sclass == C_EXT)
1854 char buf[SYMNMLEN + 1];
1855 struct xcoff_link_hash_entry *h;
1857 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1861 /* Normally we could not xcoff_link_hash_lookup in an add
1862 symbols routine, since we might not be using an XCOFF
1863 hash table. However, we verified above that we are using
1864 an XCOFF hash table. */
1865 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
1866 false, false, true);
1869 h->flags |= XCOFF_REF_DYNAMIC;
1871 /* If the symbol is undefined, and the current BFD is
1872 not a dynamic object, change the BFD to this dynamic
1873 object, so that we can get the correct import file
1875 if ((h->root.type == bfd_link_hash_undefined
1876 || h->root.type == bfd_link_hash_undefweak)
1877 && (h->root.u.undef.abfd == NULL
1878 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
1879 h->root.u.undef.abfd = abfd;
1881 if (h->smclas == XMC_UA
1882 && sym.n_numaux > 0)
1884 union internal_auxent aux;
1886 bfd_coff_swap_aux_in (abfd,
1887 (PTR) (esym + symesz * sym.n_numaux),
1888 sym.n_type, sym.n_sclass,
1889 sym.n_numaux - 1, sym.n_numaux,
1891 h->smclas = aux.x_csect.x_smclas;
1896 esym += (sym.n_numaux + 1) * symesz;
1899 /* Record this file in the import files. */
1901 n = ((struct xcoff_import_file *)
1902 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
1905 bfd_set_error (bfd_error_no_memory);
1910 /* For some reason, the path entry in the import file list for a
1911 shared object appears to always be empty. The file name is the
1914 if (abfd->my_archive == NULL)
1916 bname = bfd_get_filename (abfd);
1921 bname = bfd_get_filename (abfd->my_archive);
1922 mname = bfd_get_filename (abfd);
1924 s = strrchr (bname, '/');
1930 /* We start c at 1 because the first import file number is reserved
1932 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1934 pp = &(*pp)->next, ++c)
1938 xcoff_data (abfd)->import_file_id = c;
1943 /* Routines that are called after all the input files have been
1944 handled, but before the sections are laid out in memory. */
1946 /* Mark a symbol as not being garbage, including the section in which
1949 static INLINE boolean
1950 xcoff_mark_symbol (info, h)
1951 struct bfd_link_info *info;
1952 struct xcoff_link_hash_entry *h;
1954 if ((h->flags & XCOFF_MARK) != 0)
1957 h->flags |= XCOFF_MARK;
1958 if (h->root.type == bfd_link_hash_defined
1959 || h->root.type == bfd_link_hash_defweak)
1963 hsec = h->root.u.def.section;
1964 if ((hsec->flags & SEC_MARK) == 0)
1966 if (! xcoff_mark (info, hsec))
1971 if (h->toc_section != NULL
1972 && (h->toc_section->flags & SEC_MARK) == 0)
1974 if (! xcoff_mark (info, h->toc_section))
1981 /* The mark phase of garbage collection. For a given section, mark
1982 it, and all the sections which define symbols to which it refers.
1983 Because this function needs to look at the relocs, we also count
1984 the number of relocs which need to be copied into the .loader
1988 xcoff_mark (info, sec)
1989 struct bfd_link_info *info;
1992 if ((sec->flags & SEC_MARK) != 0)
1995 sec->flags |= SEC_MARK;
1997 if (sec->owner->xvec == info->hash->creator
1998 && coff_section_data (sec->owner, sec) != NULL
1999 && xcoff_section_data (sec->owner, sec) != NULL)
2001 register struct xcoff_link_hash_entry **hp, **hpend;
2002 struct internal_reloc *rel, *relend;
2004 /* Mark all the symbols in this section. */
2006 hp = (obj_xcoff_sym_hashes (sec->owner)
2007 + xcoff_section_data (sec->owner, sec)->first_symndx);
2008 hpend = (obj_xcoff_sym_hashes (sec->owner)
2009 + xcoff_section_data (sec->owner, sec)->last_symndx);
2010 for (; hp < hpend; hp++)
2012 register struct xcoff_link_hash_entry *h;
2016 && (h->flags & XCOFF_MARK) == 0)
2018 if (! xcoff_mark_symbol (info, h))
2023 /* Look through the section relocs. */
2025 if ((sec->flags & SEC_RELOC) != 0
2026 && sec->reloc_count > 0)
2028 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2029 (bfd_byte *) NULL, false,
2030 (struct internal_reloc *) NULL);
2033 relend = rel + sec->reloc_count;
2034 for (; rel < relend; rel++)
2037 struct xcoff_link_hash_entry *h;
2039 if ((unsigned int) rel->r_symndx
2040 > obj_raw_syment_count (sec->owner))
2043 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2045 && (h->flags & XCOFF_MARK) == 0)
2047 if (! xcoff_mark_symbol (info, h))
2051 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2053 && (rsec->flags & SEC_MARK) == 0)
2055 if (! xcoff_mark (info, rsec))
2059 /* See if this reloc needs to be copied into the .loader
2061 switch (rel->r_type)
2065 || h->root.type == bfd_link_hash_defined
2066 || h->root.type == bfd_link_hash_defweak
2067 || h->root.type == bfd_link_hash_common
2068 || ((h->flags & XCOFF_CALLED) != 0
2069 && (h->flags & XCOFF_DEF_REGULAR) == 0
2070 && ((h->flags & XCOFF_REF_DYNAMIC) != 0
2072 && (h->root.type == bfd_link_hash_undefined
2073 || h->root.type == bfd_link_hash_undefweak)
2074 && h->root.root.string[0] == '.'))
2081 ++xcoff_hash_table (info)->ldrel_count;
2083 h->flags |= XCOFF_LDREL;
2090 /* We should never need a .loader reloc for a TOC
2096 if (! info->keep_memory
2097 && coff_section_data (sec->owner, sec) != NULL
2098 && coff_section_data (sec->owner, sec)->relocs != NULL
2099 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2101 free (coff_section_data (sec->owner, sec)->relocs);
2102 coff_section_data (sec->owner, sec)->relocs = NULL;
2110 /* The sweep phase of garbage collection. Remove all garbage
2115 struct bfd_link_info *info;
2119 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2123 for (o = sub->sections; o != NULL; o = o->next)
2125 if ((o->flags & SEC_MARK) == 0)
2127 /* Keep all sections from non-XCOFF input files. Keep
2128 special sections. Keep .debug sections for the
2130 if (sub->xvec != info->hash->creator
2131 || o == xcoff_hash_table (info)->debug_section
2132 || o == xcoff_hash_table (info)->loader_section
2133 || o == xcoff_hash_table (info)->linkage_section
2134 || o == xcoff_hash_table (info)->toc_section
2135 || strcmp (o->name, ".debug") == 0)
2136 o->flags |= SEC_MARK;
2141 o->lineno_count = 0;
2148 /* Record the number of elements in a set. This is used to output the
2149 correct csect length. */
2152 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2154 struct bfd_link_info *info;
2155 struct bfd_link_hash_entry *harg;
2158 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2159 struct xcoff_link_size_list *n;
2161 if (! XCOFF_XVECP (output_bfd->xvec))
2164 /* This will hardly ever be called. I don't want to burn four bytes
2165 per global symbol, so instead the size is kept on a linked list
2166 attached to the hash table. */
2168 n = ((struct xcoff_link_size_list *)
2169 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2172 bfd_set_error (bfd_error_no_memory);
2175 n->next = xcoff_hash_table (info)->size_list;
2178 xcoff_hash_table (info)->size_list = n;
2180 h->flags |= XCOFF_HAS_SIZE;
2185 /* Import a symbol. */
2188 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2191 struct bfd_link_info *info;
2192 struct bfd_link_hash_entry *harg;
2194 const char *imppath;
2195 const char *impfile;
2196 const char *impmember;
2198 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2200 if (! XCOFF_XVECP (output_bfd->xvec))
2203 h->flags |= XCOFF_IMPORT;
2205 if (val != (bfd_vma) -1)
2207 if (h->root.type == bfd_link_hash_defined)
2209 if (! ((*info->callbacks->multiple_definition)
2210 (info, h->root.root.string, h->root.u.def.section->owner,
2211 h->root.u.def.section, h->root.u.def.value,
2212 output_bfd, bfd_abs_section_ptr, val)))
2216 h->root.type = bfd_link_hash_defined;
2217 h->root.u.def.section = bfd_abs_section_ptr;
2218 h->root.u.def.value = val;
2221 if (h->ldsym == NULL)
2223 h->ldsym = ((struct internal_ldsym *)
2224 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2225 if (h->ldsym == NULL)
2227 bfd_set_error (bfd_error_no_memory);
2232 if (imppath == NULL)
2233 h->ldsym->l_ifile = (bfd_size_type) -1;
2237 struct xcoff_import_file **pp;
2239 /* We start c at 1 because the first entry in the import list is
2240 reserved for the library search path. */
2241 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2243 pp = &(*pp)->next, ++c)
2245 if (strcmp ((*pp)->path, imppath) == 0
2246 && strcmp ((*pp)->file, impfile) == 0
2247 && strcmp ((*pp)->member, impmember) == 0)
2253 struct xcoff_import_file *n;
2255 n = ((struct xcoff_import_file *)
2256 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2259 bfd_set_error (bfd_error_no_memory);
2265 n->member = impmember;
2269 h->ldsym->l_ifile = c;
2275 /* Export a symbol. */
2278 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2280 struct bfd_link_info *info;
2281 struct bfd_link_hash_entry *harg;
2284 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2286 if (! XCOFF_XVECP (output_bfd->xvec))
2289 h->flags |= XCOFF_EXPORT;
2291 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2292 I'm just going to ignore it until somebody explains it. */
2294 /* Make sure we don't garbage collect this symbol. */
2295 if (! xcoff_mark_symbol (info, h))
2301 /* Count a reloc against a symbol. This is called for relocs
2302 generated by the linker script, typically for global constructors
2306 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2308 struct bfd_link_info *info;
2311 struct xcoff_link_hash_entry *h;
2313 if (! XCOFF_XVECP (output_bfd->xvec))
2316 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, false, false,
2320 (*_bfd_error_handler) ("%s: no such symbol", name);
2321 bfd_set_error (bfd_error_no_symbols);
2325 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2326 ++xcoff_hash_table (info)->ldrel_count;
2328 /* Mark the symbol to avoid garbage collection. */
2329 if (! xcoff_mark_symbol (info, h))
2335 /* This function is called for each symbol to which the linker script
2339 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2341 struct bfd_link_info *info;
2344 struct xcoff_link_hash_entry *h;
2346 if (! XCOFF_XVECP (output_bfd->xvec))
2349 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2354 h->flags |= XCOFF_DEF_REGULAR;
2359 /* This structure is used to pass information through
2360 xcoff_link_hash_traverse. */
2362 struct xcoff_loader_info
2364 /* Set if a problem occurred. */
2368 /* Link information structure. */
2369 struct bfd_link_info *info;
2370 /* Number of ldsym structures. */
2372 /* Size of string table. */
2376 /* Allocated size of string table. */
2380 /* Build the .loader section. This is called by the XCOFF linker
2381 emulation before_allocation routine. We must set the size of the
2382 .loader section before the linker lays out the output file.
2383 LIBPATH is the library path to search for shared objects; this is
2384 normally built from the -L arguments passed to the linker. ENTRY
2385 is the name of the entry point symbol. */
2388 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2389 file_align, maxstack, maxdata, gc,
2392 struct bfd_link_info *info;
2393 const char *libpath;
2395 unsigned long file_align;
2396 unsigned long maxstack;
2397 unsigned long maxdata;
2402 struct xcoff_link_hash_entry *hentry;
2404 struct xcoff_loader_info ldinfo;
2405 size_t impsize, impcount;
2406 struct xcoff_import_file *fl;
2407 struct internal_ldhdr *ldhdr;
2408 bfd_size_type stoff;
2412 struct bfd_strtab_hash *debug_strtab;
2413 bfd_byte *debug_contents = NULL;
2415 if (! XCOFF_XVECP (output_bfd->xvec))
2418 ldinfo.failed = false;
2419 ldinfo.output_bfd = output_bfd;
2421 ldinfo.ldsym_count = 0;
2422 ldinfo.string_size = 0;
2423 ldinfo.strings = NULL;
2424 ldinfo.string_alc = 0;
2426 xcoff_data (output_bfd)->maxstack = maxstack;
2427 xcoff_data (output_bfd)->maxdata = maxdata;
2428 xcoff_data (output_bfd)->modtype = modtype;
2430 xcoff_hash_table (info)->file_align = file_align;
2431 xcoff_hash_table (info)->textro = textro;
2433 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2434 false, false, true);
2437 hentry->flags |= XCOFF_ENTRY;
2438 if (hentry->root.type == bfd_link_hash_defined
2439 || hentry->root.type == bfd_link_hash_defweak)
2440 xcoff_data (output_bfd)->entry_section =
2441 hentry->root.u.def.section->output_section;
2444 /* Garbage collect unused sections. */
2445 if (info->relocateable
2448 || (hentry->root.type != bfd_link_hash_defined
2449 && hentry->root.type != bfd_link_hash_defweak))
2452 xcoff_hash_table (info)->gc = false;
2454 /* We still need to call xcoff_mark, in order to set ldrel_count
2456 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2460 for (o = sub->sections; o != NULL; o = o->next)
2462 if ((o->flags & SEC_MARK) == 0)
2464 if (! xcoff_mark (info, o))
2472 if (! xcoff_mark (info, hentry->root.u.def.section))
2475 xcoff_hash_table (info)->gc = true;
2478 if (info->input_bfds == NULL)
2480 /* I'm not sure what to do in this bizarre case. */
2484 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2489 /* Work out the size of the import file names. Each import file ID
2490 consists of three null terminated strings: the path, the file
2491 name, and the archive member name. The first entry in the list
2492 of names is the path to use to find objects, which the linker has
2493 passed in as the libpath argument. For some reason, the path
2494 entry in the other import file names appears to always be empty. */
2495 impsize = strlen (libpath) + 3;
2497 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2500 impsize += (strlen (fl->path)
2502 + strlen (fl->member)
2506 /* Set up the .loader section header. */
2507 ldhdr = &xcoff_hash_table (info)->ldhdr;
2508 ldhdr->l_version = 1;
2509 ldhdr->l_nsyms = ldinfo.ldsym_count;
2510 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2511 ldhdr->l_istlen = impsize;
2512 ldhdr->l_nimpid = impcount;
2513 ldhdr->l_impoff = (LDHDRSZ
2514 + ldhdr->l_nsyms * LDSYMSZ
2515 + ldhdr->l_nreloc * LDRELSZ);
2516 ldhdr->l_stlen = ldinfo.string_size;
2517 stoff = ldhdr->l_impoff + impsize;
2518 if (ldinfo.string_size == 0)
2521 ldhdr->l_stoff = stoff;
2523 /* We now know the final size of the .loader section. Allocate
2525 lsec = xcoff_hash_table (info)->loader_section;
2526 lsec->_raw_size = stoff + ldhdr->l_stlen;
2527 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2528 if (lsec->contents == NULL)
2530 bfd_set_error (bfd_error_no_memory);
2534 /* Set up the header. */
2535 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2536 (struct external_ldhdr *) lsec->contents);
2538 /* Set up the import file names. */
2539 out = (char *) lsec->contents + ldhdr->l_impoff;
2540 strcpy (out, libpath);
2541 out += strlen (libpath) + 1;
2544 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2546 register const char *s;
2549 while ((*out++ = *s++) != '\0')
2552 while ((*out++ = *s++) != '\0')
2555 while ((*out++ = *s++) != '\0')
2559 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
2561 /* Set up the symbol string table. */
2562 if (ldinfo.string_size > 0)
2564 memcpy (out, ldinfo.strings, ldinfo.string_size);
2565 free (ldinfo.strings);
2566 ldinfo.strings = NULL;
2569 /* We can't set up the symbol table or the relocs yet, because we
2570 don't yet know the final position of the various sections. The
2571 .loader symbols are written out when the corresponding normal
2572 symbols are written out in xcoff_link_input_bfd or
2573 xcoff_write_global_symbol. The .loader relocs are written out
2574 when the corresponding normal relocs are handled in
2575 xcoff_link_input_bfd. */
2577 /* Allocate space for the global linkage section and the global toc
2579 sec = xcoff_hash_table (info)->linkage_section;
2580 if (sec->_raw_size > 0)
2582 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2583 if (sec->contents == NULL)
2585 bfd_set_error (bfd_error_no_memory);
2589 sec = xcoff_hash_table (info)->toc_section;
2590 if (sec->_raw_size > 0)
2592 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2593 if (sec->contents == NULL)
2595 bfd_set_error (bfd_error_no_memory);
2600 /* Now that we've done garbage collection, figure out the contents
2601 of the .debug section. */
2602 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2604 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2607 bfd_size_type symcount;
2608 unsigned long *debug_index;
2610 bfd_byte *esym, *esymend;
2611 bfd_size_type symesz;
2613 if (sub->xvec != info->hash->creator)
2615 subdeb = bfd_get_section_by_name (sub, ".debug");
2616 if (subdeb == NULL || subdeb->_raw_size == 0)
2619 if (info->strip == strip_all
2620 || info->strip == strip_debugger
2621 || info->discard == discard_all)
2623 subdeb->_raw_size = 0;
2627 if (! _bfd_coff_get_external_symbols (sub))
2630 symcount = obj_raw_syment_count (sub);
2631 debug_index = ((unsigned long *)
2632 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2633 if (debug_index == NULL)
2635 bfd_set_error (bfd_error_no_memory);
2638 xcoff_data (sub)->debug_indices = debug_index;
2640 /* Grab the contents of the .debug section. We use malloc and
2641 copy the neams into the debug stringtab, rather than
2642 bfd_alloc, because I expect that, when linking many files
2643 together, many of the strings will be the same. Storing the
2644 strings in the hash table should save space in this case. */
2645 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2646 if (debug_contents == NULL)
2648 bfd_set_error (bfd_error_no_memory);
2651 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2652 (file_ptr) 0, subdeb->_raw_size))
2655 csectpp = xcoff_data (sub)->csects;
2657 symesz = bfd_coff_symesz (sub);
2658 esym = (bfd_byte *) obj_coff_external_syms (sub);
2659 esymend = esym + symcount * symesz;
2660 while (esym < esymend)
2662 struct internal_syment sym;
2664 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2666 *debug_index = (unsigned long) -1;
2668 if (sym._n._n_n._n_zeroes == 0
2671 || ((*csectpp)->flags & SEC_MARK) != 0
2672 || *csectpp == bfd_abs_section_ptr)
2673 && bfd_coff_symname_in_debug (sub, &sym))
2678 name = (char *) debug_contents + sym._n._n_n._n_offset;
2679 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2680 if (indx == (bfd_size_type) -1)
2682 *debug_index = indx;
2685 esym += (sym.n_numaux + 1) * symesz;
2686 csectpp += sym.n_numaux + 1;
2687 debug_index += sym.n_numaux + 1;
2690 free (debug_contents);
2691 debug_contents = NULL;
2693 /* Clear the size of subdeb, so that it is not included directly
2694 in the output file. */
2695 subdeb->_raw_size = 0;
2697 if (! info->keep_memory)
2699 if (! _bfd_coff_free_symbols (sub))
2704 xcoff_hash_table (info)->debug_section->_raw_size =
2705 _bfd_stringtab_size (debug_strtab);
2710 if (ldinfo.strings != NULL)
2711 free (ldinfo.strings);
2712 if (debug_contents != NULL)
2713 free (debug_contents);
2717 /* Add a symbol to the .loader symbols, if necessary. */
2720 xcoff_build_ldsyms (h, p)
2721 struct xcoff_link_hash_entry *h;
2724 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2727 /* We don't want to garbage collect symbols which are not defined in
2728 XCOFF files. This is a convenient place to mark them. */
2729 if (xcoff_hash_table (ldinfo->info)->gc
2730 && (h->flags & XCOFF_MARK) == 0
2731 && (h->root.type == bfd_link_hash_defined
2732 || h->root.type == bfd_link_hash_defweak)
2733 && (h->root.u.def.section->owner == NULL
2734 || (h->root.u.def.section->owner->xvec
2735 != ldinfo->info->hash->creator)))
2736 h->flags |= XCOFF_MARK;
2738 /* If this symbol is called, and it is defined in a dynamic object,
2739 or if we are creating a dynamic object and it is not defined at
2740 all, then we need to set up global linkage code for it. (Unless
2741 we did garbage collection and we didn't need this symbol.) */
2742 if ((h->flags & XCOFF_CALLED) != 0
2743 && (h->flags & XCOFF_DEF_REGULAR) == 0
2744 && (h->root.type == bfd_link_hash_undefined
2745 || h->root.type == bfd_link_hash_undefweak)
2746 && ((h->flags & XCOFF_REF_DYNAMIC) != 0
2747 || ldinfo->info->shared)
2748 && h->root.root.string[0] == '.'
2749 && (! xcoff_hash_table (ldinfo->info)->gc
2750 || (h->flags & XCOFF_MARK) != 0))
2753 struct xcoff_link_hash_entry *hds;
2755 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2756 h->root.type = bfd_link_hash_defined;
2757 h->root.u.def.section = sec;
2758 h->root.u.def.value = sec->_raw_size;
2760 sec->_raw_size += XCOFF_GLINK_SIZE;
2762 /* The global linkage code requires a TOC entry for the
2764 hds = h->descriptor;
2765 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2766 || hds->root.type == bfd_link_hash_undefweak)
2767 && (hds->flags & XCOFF_DEF_REGULAR) == 0
2768 && ((hds->flags & XCOFF_REF_DYNAMIC) != 0
2769 || ldinfo->info->shared));
2770 hds->flags |= XCOFF_MARK;
2771 if (hds->toc_section == NULL)
2773 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2774 hds->u.toc_offset = hds->toc_section->_raw_size;
2775 hds->toc_section->_raw_size += 4;
2776 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2777 ++hds->toc_section->reloc_count;
2779 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2781 /* We need to call xcoff_build_ldsyms recursively here,
2782 because we may already have passed hds on the traversal. */
2783 xcoff_build_ldsyms (hds, p);
2787 /* If this is still a common symbol, and it wasn't garbage
2788 collected, we need to actually allocate space for it in the .bss
2790 if (h->root.type == bfd_link_hash_common
2791 && (! xcoff_hash_table (ldinfo->info)->gc
2792 || (h->flags & XCOFF_MARK) != 0)
2793 && h->root.u.c.p->section->_raw_size == 0)
2795 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2796 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
2799 /* We need to add a symbol to the .loader section if it is mentioned
2800 in a reloc which we are copying to the .loader section and it was
2801 not defined or common, or if it is the entry point, or if it is
2804 if (((h->flags & XCOFF_LDREL) == 0
2805 || h->root.type == bfd_link_hash_defined
2806 || h->root.type == bfd_link_hash_defweak
2807 || h->root.type == bfd_link_hash_common)
2808 && (h->flags & XCOFF_ENTRY) == 0
2809 && (h->flags & XCOFF_EXPORT) == 0)
2815 /* We don't need to add this symbol if we did garbage collection and
2816 we did not mark this symbol. */
2817 if (xcoff_hash_table (ldinfo->info)->gc
2818 && (h->flags & XCOFF_MARK) == 0)
2824 /* We may have already processed this symbol due to the recursive
2826 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2829 /* We need to add this symbol to the .loader symbols. */
2831 /* h->ldsym will already have been allocated for an explicitly
2833 if (h->ldsym == NULL)
2835 h->ldsym = ((struct internal_ldsym *)
2836 bfd_zalloc (ldinfo->output_bfd,
2837 sizeof (struct internal_ldsym)));
2838 if (h->ldsym == NULL)
2840 ldinfo->failed = true;
2841 bfd_set_error (bfd_error_no_memory);
2846 /* The first 3 symbol table indices are reserved to indicate the
2848 h->ldindx = ldinfo->ldsym_count + 3;
2850 ++ldinfo->ldsym_count;
2852 len = strlen (h->root.root.string);
2853 if (len <= SYMNMLEN)
2854 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
2857 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2860 bfd_byte *newstrings;
2862 newalc = ldinfo->string_alc * 2;
2865 while (ldinfo->string_size + len + 3 > newalc)
2868 if (ldinfo->strings == NULL)
2869 newstrings = (bfd_byte *) malloc (newalc);
2871 newstrings = ((bfd_byte *)
2872 realloc ((PTR) ldinfo->strings, newalc));
2873 if (newstrings == NULL)
2875 ldinfo->failed = true;
2876 bfd_set_error (bfd_error_no_memory);
2879 ldinfo->string_alc = newalc;
2880 ldinfo->strings = newstrings;
2883 bfd_put_16 (ldinfo->output_bfd, len + 1,
2884 ldinfo->strings + ldinfo->string_size);
2885 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
2886 h->ldsym->_l._l_l._l_zeroes = 0;
2887 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
2888 ldinfo->string_size += len + 3;
2891 h->flags |= XCOFF_BUILT_LDSYM;
2896 /* Do the final link step. */
2899 _bfd_xcoff_bfd_final_link (abfd, info)
2901 struct bfd_link_info *info;
2903 bfd_size_type symesz;
2904 struct xcoff_final_link_info finfo;
2906 struct bfd_link_order *p;
2907 size_t max_contents_size;
2908 size_t max_sym_count;
2909 size_t max_lineno_count;
2910 size_t max_reloc_count;
2911 size_t max_output_reloc_count;
2912 file_ptr rel_filepos;
2914 file_ptr line_filepos;
2915 unsigned int linesz;
2917 bfd_byte *external_relocs = NULL;
2918 char strbuf[STRING_SIZE_SIZE];
2921 abfd->flags |= DYNAMIC;
2923 symesz = bfd_coff_symesz (abfd);
2926 finfo.output_bfd = abfd;
2927 finfo.strtab = NULL;
2928 finfo.section_info = NULL;
2929 finfo.last_file_index = -1;
2930 finfo.toc_symindx = -1;
2931 finfo.internal_syms = NULL;
2932 finfo.sym_indices = NULL;
2933 finfo.outsyms = NULL;
2934 finfo.linenos = NULL;
2935 finfo.contents = NULL;
2936 finfo.external_relocs = NULL;
2938 finfo.ldsym = ((struct external_ldsym *)
2939 (xcoff_hash_table (info)->loader_section->contents
2941 finfo.ldrel = ((struct external_ldrel *)
2942 (xcoff_hash_table (info)->loader_section->contents
2944 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
2946 xcoff_data (abfd)->coff.link_info = info;
2948 finfo.strtab = _bfd_stringtab_init ();
2949 if (finfo.strtab == NULL)
2952 /* Compute the file positions for all the sections. */
2953 if (abfd->output_has_begun)
2955 if (xcoff_hash_table (info)->file_align != 0)
2962 file_align = xcoff_hash_table (info)->file_align;
2963 if (file_align != 0)
2965 boolean saw_contents;
2970 /* Insert .pad sections before every section which has
2971 contents and is loaded, if it is preceded by some other
2972 section which has contents and is loaded. */
2973 saw_contents = true;
2974 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
2976 (*op)->target_index = indx;
2977 if (strcmp ((*op)->name, ".pad") == 0)
2978 saw_contents = false;
2979 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
2980 && ((*op)->flags & SEC_LOAD) != 0)
2983 saw_contents = true;
2990 n = bfd_make_section_anyway (abfd, ".pad");
2991 BFD_ASSERT (*op == n);
2993 n->flags = SEC_HAS_CONTENTS;
2994 n->alignment_power = 0;
2995 saw_contents = false;
3000 /* Reset the section indices after inserting the new
3003 for (o = abfd->sections; o != NULL; o = o->next)
3006 o->target_index = indx;
3008 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3010 /* Work out appropriate sizes for the .pad sections to force
3011 each section to land on a page boundary. This bit of
3012 code knows what compute_section_file_positions is going
3014 sofar = bfd_coff_filhsz (abfd);
3015 sofar += bfd_coff_aoutsz (abfd);
3016 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3018 for (o = abfd->sections; o != NULL; o = o->next)
3020 if (strcmp (o->name, ".pad") == 0)
3024 BFD_ASSERT (o->_raw_size == 0);
3025 pageoff = sofar & (file_align - 1);
3028 o->_raw_size = file_align - pageoff;
3029 sofar += file_align - pageoff;
3030 o->flags |= SEC_HAS_CONTENTS;
3035 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3036 sofar += BFD_ALIGN (o->_raw_size,
3037 1 << o->alignment_power);
3042 bfd_coff_compute_section_file_positions (abfd);
3045 /* Count the line numbers and relocation entries required for the
3046 output file. Set the file positions for the relocs. */
3047 rel_filepos = obj_relocbase (abfd);
3048 relsz = bfd_coff_relsz (abfd);
3049 max_contents_size = 0;
3050 max_lineno_count = 0;
3051 max_reloc_count = 0;
3052 for (o = abfd->sections; o != NULL; o = o->next)
3055 o->lineno_count = 0;
3056 for (p = o->link_order_head; p != NULL; p = p->next)
3058 if (p->type == bfd_indirect_link_order)
3062 sec = p->u.indirect.section;
3064 if (info->strip == strip_none
3065 || info->strip == strip_some)
3066 o->lineno_count += sec->lineno_count;
3068 o->reloc_count += sec->reloc_count;
3070 if (sec->_raw_size > max_contents_size)
3071 max_contents_size = sec->_raw_size;
3072 if (sec->lineno_count > max_lineno_count)
3073 max_lineno_count = sec->lineno_count;
3074 if (coff_section_data (sec->owner, sec) != NULL
3075 && xcoff_section_data (sec->owner, sec) != NULL
3076 && (xcoff_section_data (sec->owner, sec)->lineno_count
3077 > max_lineno_count))
3079 xcoff_section_data (sec->owner, sec)->lineno_count;
3080 if (sec->reloc_count > max_reloc_count)
3081 max_reloc_count = sec->reloc_count;
3083 else if (p->type == bfd_section_reloc_link_order
3084 || p->type == bfd_symbol_reloc_link_order)
3087 if (o->reloc_count == 0)
3091 o->flags |= SEC_RELOC;
3092 o->rel_filepos = rel_filepos;
3093 rel_filepos += o->reloc_count * relsz;
3097 /* Allocate space for the pointers we need to keep for the relocs. */
3101 /* We use section_count + 1, rather than section_count, because
3102 the target_index fields are 1 based. */
3103 finfo.section_info = ((struct xcoff_link_section_info *)
3104 malloc ((abfd->section_count + 1)
3105 * sizeof (struct xcoff_link_section_info)));
3106 if (finfo.section_info == NULL)
3108 bfd_set_error (bfd_error_no_memory);
3111 for (i = 0; i <= abfd->section_count; i++)
3113 finfo.section_info[i].relocs = NULL;
3114 finfo.section_info[i].rel_hashes = NULL;
3115 finfo.section_info[i].toc_rel_hashes = NULL;
3119 /* We now know the size of the relocs, so we can determine the file
3120 positions of the line numbers. */
3121 line_filepos = rel_filepos;
3122 finfo.line_filepos = line_filepos;
3123 linesz = bfd_coff_linesz (abfd);
3124 max_output_reloc_count = 0;
3125 for (o = abfd->sections; o != NULL; o = o->next)
3127 if (o->lineno_count == 0)
3128 o->line_filepos = 0;
3131 o->line_filepos = line_filepos;
3132 line_filepos += o->lineno_count * linesz;
3135 if (o->reloc_count != 0)
3137 /* We don't know the indices of global symbols until we have
3138 written out all the local symbols. For each section in
3139 the output file, we keep an array of pointers to hash
3140 table entries. Each entry in the array corresponds to a
3141 reloc. When we find a reloc against a global symbol, we
3142 set the corresponding entry in this array so that we can
3143 fix up the symbol index after we have written out all the
3146 Because of this problem, we also keep the relocs in
3147 memory until the end of the link. This wastes memory.
3148 We could backpatch the file later, I suppose, although it
3150 finfo.section_info[o->target_index].relocs =
3151 ((struct internal_reloc *)
3152 malloc (o->reloc_count * sizeof (struct internal_reloc)));
3153 finfo.section_info[o->target_index].rel_hashes =
3154 ((struct xcoff_link_hash_entry **)
3155 malloc (o->reloc_count
3156 * sizeof (struct xcoff_link_hash_entry *)));
3157 if (finfo.section_info[o->target_index].relocs == NULL
3158 || finfo.section_info[o->target_index].rel_hashes == NULL)
3160 bfd_set_error (bfd_error_no_memory);
3164 if (o->reloc_count > max_output_reloc_count)
3165 max_output_reloc_count = o->reloc_count;
3168 /* Reset the reloc and lineno counts, so that we can use them to
3169 count the number of entries we have output so far. */
3171 o->lineno_count = 0;
3174 obj_sym_filepos (abfd) = line_filepos;
3176 /* Figure out the largest number of symbols in an input BFD. Take
3177 the opportunity to clear the output_has_begun fields of all the
3178 input BFD's. We want at least 4 symbols, since that is the
3179 number which xcoff_write_global_symbol may need. */
3181 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3185 sub->output_has_begun = false;
3186 sz = obj_raw_syment_count (sub);
3187 if (sz > max_sym_count)
3191 /* Allocate some buffers used while linking. */
3192 finfo.internal_syms = ((struct internal_syment *)
3193 malloc (max_sym_count
3194 * sizeof (struct internal_syment)));
3195 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
3196 finfo.outsyms = ((bfd_byte *)
3197 malloc ((size_t) ((max_sym_count + 1) * symesz)));
3198 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
3199 * bfd_coff_linesz (abfd));
3200 finfo.contents = (bfd_byte *) malloc (max_contents_size);
3201 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
3202 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3203 || (finfo.sym_indices == NULL && max_sym_count > 0)
3204 || finfo.outsyms == NULL
3205 || (finfo.linenos == NULL && max_lineno_count > 0)
3206 || (finfo.contents == NULL && max_contents_size > 0)
3207 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3209 bfd_set_error (bfd_error_no_memory);
3213 obj_raw_syment_count (abfd) = 0;
3214 xcoff_data (abfd)->toc = (bfd_vma) -1;
3216 /* We now know the position of everything in the file, except that
3217 we don't know the size of the symbol table and therefore we don't
3218 know where the string table starts. We just build the string
3219 table in memory as we go along. We process all the relocations
3220 for a single input file at once. */
3221 for (o = abfd->sections; o != NULL; o = o->next)
3223 for (p = o->link_order_head; p != NULL; p = p->next)
3225 if (p->type == bfd_indirect_link_order
3226 && p->u.indirect.section->owner->xvec == abfd->xvec)
3228 sub = p->u.indirect.section->owner;
3229 if (! sub->output_has_begun)
3231 if (! xcoff_link_input_bfd (&finfo, sub))
3233 sub->output_has_begun = true;
3236 else if (p->type == bfd_section_reloc_link_order
3237 || p->type == bfd_symbol_reloc_link_order)
3239 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3244 if (! _bfd_default_link_order (abfd, info, o, p))
3250 /* Free up the buffers used by xcoff_link_input_bfd. */
3252 if (finfo.internal_syms != NULL)
3254 free (finfo.internal_syms);
3255 finfo.internal_syms = NULL;
3257 if (finfo.sym_indices != NULL)
3259 free (finfo.sym_indices);
3260 finfo.sym_indices = NULL;
3262 if (finfo.linenos != NULL)
3264 free (finfo.linenos);
3265 finfo.linenos = NULL;
3267 if (finfo.contents != NULL)
3269 free (finfo.contents);
3270 finfo.contents = NULL;
3272 if (finfo.external_relocs != NULL)
3274 free (finfo.external_relocs);
3275 finfo.external_relocs = NULL;
3278 /* The value of the last C_FILE symbol is supposed to be -1. Write
3280 if (finfo.last_file_index != -1)
3282 finfo.last_file.n_value = -1;
3283 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3284 (PTR) finfo.outsyms);
3286 (obj_sym_filepos (abfd)
3287 + finfo.last_file_index * symesz),
3289 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3293 /* Write out all the global symbols which do not come from XCOFF
3295 xcoff_link_hash_traverse (xcoff_hash_table (info),
3296 xcoff_write_global_symbol,
3299 if (finfo.outsyms != NULL)
3301 free (finfo.outsyms);
3302 finfo.outsyms = NULL;
3305 /* Now that we have written out all the global symbols, we know the
3306 symbol indices to use for relocs against them, and we can finally
3307 write out the relocs. */
3308 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3309 if (external_relocs == NULL && max_output_reloc_count != 0)
3311 bfd_set_error (bfd_error_no_memory);
3315 for (o = abfd->sections; o != NULL; o = o->next)
3317 struct internal_reloc *irel;
3318 struct internal_reloc *irelend;
3319 struct xcoff_link_hash_entry **rel_hash;
3320 struct xcoff_toc_rel_hash *toc_rel_hash;
3323 if (o->reloc_count == 0)
3326 irel = finfo.section_info[o->target_index].relocs;
3327 irelend = irel + o->reloc_count;
3328 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3329 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3331 if (*rel_hash != NULL)
3333 if ((*rel_hash)->indx < 0)
3335 if (! ((*info->callbacks->unattached_reloc)
3336 (info, (*rel_hash)->root.root.string,
3337 (bfd *) NULL, o, irel->r_vaddr)))
3339 (*rel_hash)->indx = 0;
3341 irel->r_symndx = (*rel_hash)->indx;
3345 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
3346 toc_rel_hash != NULL;
3347 toc_rel_hash = toc_rel_hash->next)
3349 if (toc_rel_hash->h->u.toc_indx < 0)
3351 if (! ((*info->callbacks->unattached_reloc)
3352 (info, toc_rel_hash->h->root.root.string,
3353 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
3355 toc_rel_hash->h->u.toc_indx = 0;
3357 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
3360 /* XCOFF requires that the relocs be sorted by address. We tend
3361 to produce them in the order in which their containing csects
3362 appear in the symbol table, which is not necessarily by
3363 address. So we sort them here. There may be a better way to
3365 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3366 o->reloc_count, sizeof (struct internal_reloc),
3369 irel = finfo.section_info[o->target_index].relocs;
3370 irelend = irel + o->reloc_count;
3371 erel = external_relocs;
3372 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3373 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3375 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3376 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3377 abfd) != relsz * o->reloc_count)
3381 if (external_relocs != NULL)
3383 free (external_relocs);
3384 external_relocs = NULL;
3387 /* Free up the section information. */
3388 if (finfo.section_info != NULL)
3392 for (i = 0; i < abfd->section_count; i++)
3394 if (finfo.section_info[i].relocs != NULL)
3395 free (finfo.section_info[i].relocs);
3396 if (finfo.section_info[i].rel_hashes != NULL)
3397 free (finfo.section_info[i].rel_hashes);
3399 free (finfo.section_info);
3400 finfo.section_info = NULL;
3403 /* Write out the loader section contents. */
3404 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3405 == (xcoff_hash_table (info)->loader_section->contents
3406 + xcoff_hash_table (info)->ldhdr.l_impoff));
3407 o = xcoff_hash_table (info)->loader_section;
3408 if (! bfd_set_section_contents (abfd, o->output_section,
3409 o->contents, o->output_offset,
3413 /* Write out the global linkage section and the toc section. */
3414 o = xcoff_hash_table (info)->linkage_section;
3415 if (o->_raw_size > 0
3416 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3417 o->output_offset, o->_raw_size))
3419 o = xcoff_hash_table (info)->toc_section;
3420 if (o->_raw_size > 0
3421 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3422 o->output_offset, o->_raw_size))
3425 /* Write out the string table. */
3427 (obj_sym_filepos (abfd)
3428 + obj_raw_syment_count (abfd) * symesz),
3432 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3433 (bfd_byte *) strbuf);
3434 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3436 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3439 _bfd_stringtab_free (finfo.strtab);
3441 /* Write out the debugging string table. */
3442 o = xcoff_hash_table (info)->debug_section;
3445 struct bfd_strtab_hash *debug_strtab;
3447 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3448 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3449 >= _bfd_stringtab_size (debug_strtab));
3451 o->output_section->filepos + o->output_offset,
3454 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3458 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3459 not try to write out the symbols. */
3460 bfd_get_symcount (abfd) = 0;
3465 if (finfo.strtab != NULL)
3466 _bfd_stringtab_free (finfo.strtab);
3467 if (finfo.section_info != NULL)
3471 for (i = 0; i < abfd->section_count; i++)
3473 if (finfo.section_info[i].relocs != NULL)
3474 free (finfo.section_info[i].relocs);
3475 if (finfo.section_info[i].rel_hashes != NULL)
3476 free (finfo.section_info[i].rel_hashes);
3478 free (finfo.section_info);
3480 if (finfo.internal_syms != NULL)
3481 free (finfo.internal_syms);
3482 if (finfo.sym_indices != NULL)
3483 free (finfo.sym_indices);
3484 if (finfo.outsyms != NULL)
3485 free (finfo.outsyms);
3486 if (finfo.linenos != NULL)
3487 free (finfo.linenos);
3488 if (finfo.contents != NULL)
3489 free (finfo.contents);
3490 if (finfo.external_relocs != NULL)
3491 free (finfo.external_relocs);
3492 if (external_relocs != NULL)
3493 free (external_relocs);
3497 /* Link an input file into the linker output file. This function
3498 handles all the sections and relocations of the input file at once. */
3501 xcoff_link_input_bfd (finfo, input_bfd)
3502 struct xcoff_final_link_info *finfo;
3506 const char *strings;
3507 bfd_size_type syment_base;
3508 unsigned int n_tmask;
3509 unsigned int n_btshft;
3511 bfd_size_type isymesz;
3512 bfd_size_type osymesz;
3513 bfd_size_type linesz;
3516 struct xcoff_link_hash_entry **sym_hash;
3517 struct internal_syment *isymp;
3519 unsigned long *debug_index;
3521 unsigned long output_index;
3528 /* We can just skip DYNAMIC files, unless this is a static link. */
3529 if ((input_bfd->flags & DYNAMIC) != 0
3530 && ! finfo->info->static_link)
3533 /* Move all the symbols to the output file. */
3535 output_bfd = finfo->output_bfd;
3537 syment_base = obj_raw_syment_count (output_bfd);
3538 isymesz = bfd_coff_symesz (input_bfd);
3539 osymesz = bfd_coff_symesz (output_bfd);
3540 linesz = bfd_coff_linesz (input_bfd);
3541 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3543 n_tmask = coff_data (input_bfd)->local_n_tmask;
3544 n_btshft = coff_data (input_bfd)->local_n_btshft;
3546 /* Define macros so that ISFCN, et. al., macros work correctly. */
3547 #define N_TMASK n_tmask
3548 #define N_BTSHFT n_btshft
3551 if (! finfo->info->keep_memory)
3554 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3557 if (! _bfd_coff_get_external_symbols (input_bfd))
3560 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3561 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3562 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3563 csectpp = xcoff_data (input_bfd)->csects;
3564 debug_index = xcoff_data (input_bfd)->debug_indices;
3565 isymp = finfo->internal_syms;
3566 indexp = finfo->sym_indices;
3567 output_index = syment_base;
3568 outsym = finfo->outsyms;
3572 while (esym < esym_end)
3574 struct internal_syment isym;
3575 union internal_auxent aux;
3581 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3583 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3585 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3587 BFD_ASSERT (isymp->n_numaux > 0);
3588 bfd_coff_swap_aux_in (input_bfd,
3589 (PTR) (esym + isymesz * isymp->n_numaux),
3590 isymp->n_type, isymp->n_sclass,
3591 isymp->n_numaux - 1, isymp->n_numaux,
3593 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3596 /* Make a copy of *isymp so that the relocate_section function
3597 always sees the original values. This is more reliable than
3598 always recomputing the symbol value even if we are stripping
3602 /* If this symbol is in the .loader section, swap out the
3603 .loader symbol information. If this is an external symbol
3604 reference to a defined symbol, though, then wait until we get
3605 to the definition. */
3606 if (isym.n_sclass == C_EXT
3607 && *sym_hash != NULL
3608 && (*sym_hash)->ldsym != NULL
3610 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3612 struct xcoff_link_hash_entry *h;
3613 struct internal_ldsym *ldsym;
3617 if (isym.n_scnum > 0)
3619 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3620 ldsym->l_value = (isym.n_value
3621 + (*csectpp)->output_section->vma
3622 + (*csectpp)->output_offset
3627 ldsym->l_scnum = isym.n_scnum;
3628 ldsym->l_value = isym.n_value;
3631 ldsym->l_smtype = smtyp;
3632 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3633 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3634 || (h->flags & XCOFF_IMPORT) != 0)
3635 ldsym->l_smtype |= L_IMPORT;
3636 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3637 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3638 || (h->flags & XCOFF_EXPORT) != 0)
3639 ldsym->l_smtype |= L_EXPORT;
3640 if ((h->flags & XCOFF_ENTRY) != 0)
3641 ldsym->l_smtype |= L_ENTRY;
3643 ldsym->l_smclas = aux.x_csect.x_smclas;
3645 if (ldsym->l_ifile == (bfd_size_type) -1)
3647 else if (ldsym->l_ifile == 0)
3649 if ((ldsym->l_smtype & L_IMPORT) == 0)
3655 if (h->root.type == bfd_link_hash_defined
3656 || h->root.type == bfd_link_hash_defweak)
3657 impbfd = h->root.u.def.section->owner;
3658 else if (h->root.type == bfd_link_hash_undefined
3659 || h->root.type == bfd_link_hash_undefweak)
3660 impbfd = h->root.u.undef.abfd;
3668 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3669 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3676 BFD_ASSERT (h->ldindx >= 0);
3677 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3678 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3679 finfo->ldsym + h->ldindx - 3);
3687 add = 1 + isym.n_numaux;
3689 /* If we are skipping this csect, we want to skip this symbol. */
3690 if (*csectpp == NULL)
3693 /* If we garbage collected this csect, we want to skip this
3696 && xcoff_hash_table (finfo->info)->gc
3697 && ((*csectpp)->flags & SEC_MARK) == 0
3698 && *csectpp != bfd_abs_section_ptr)
3701 /* An XCOFF linker always skips C_STAT symbols. */
3703 && isymp->n_sclass == C_STAT)
3706 /* We skip all but the first TOC anchor. */
3708 && isymp->n_sclass == C_HIDEXT
3709 && aux.x_csect.x_smclas == XMC_TC0)
3711 if (finfo->toc_symindx != -1)
3715 finfo->toc_symindx = output_index;
3716 xcoff_data (finfo->output_bfd)->toc =
3717 ((*csectpp)->output_section->vma
3718 + (*csectpp)->output_offset
3721 xcoff_data (finfo->output_bfd)->toc_section =
3722 (*csectpp)->output_section;
3727 /* If we are stripping all symbols, we want to skip this one. */
3729 && finfo->info->strip == strip_all)
3732 /* We can skip resolved external references. */
3734 && isym.n_sclass == C_EXT
3736 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3739 /* We can skip common symbols if they got defined somewhere
3742 && isym.n_sclass == C_EXT
3744 && ((*sym_hash)->root.type != bfd_link_hash_common
3745 || (*sym_hash)->root.u.c.p->section != *csectpp)
3746 && ((*sym_hash)->root.type != bfd_link_hash_defined
3747 || (*sym_hash)->root.u.def.section != *csectpp))
3750 /* Skip local symbols if we are discarding them. */
3752 && finfo->info->discard == discard_all
3753 && isym.n_sclass != C_EXT
3754 && (isym.n_sclass != C_HIDEXT
3755 || smtyp != XTY_SD))
3758 /* If we stripping debugging symbols, and this is a debugging
3759 symbol, then skip it. */
3761 && finfo->info->strip == strip_debugger
3762 && isym.n_scnum == N_DEBUG)
3765 /* If some symbols are stripped based on the name, work out the
3766 name and decide whether to skip this symbol. We don't handle
3767 this correctly for symbols whose names are in the .debug
3768 section; to get it right we would need a new bfd_strtab_hash
3769 function to return the string given the index. */
3771 && (finfo->info->strip == strip_some
3772 || finfo->info->discard == discard_l)
3773 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3776 char buf[SYMNMLEN + 1];
3778 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3782 if ((finfo->info->strip == strip_some
3783 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
3785 || (finfo->info->discard == discard_l
3786 && (isym.n_sclass != C_EXT
3787 && (isym.n_sclass != C_HIDEXT
3788 || smtyp != XTY_SD))
3789 && strncmp (name, finfo->info->lprefix,
3790 finfo->info->lprefix_len) == 0))
3794 /* We can not skip the first TOC anchor. */
3797 && finfo->info->strip != strip_all)
3800 /* We now know whether we are to skip this symbol or not. */
3803 /* Adjust the symbol in order to output it. */
3805 if (isym._n._n_n._n_zeroes == 0
3806 && isym._n._n_n._n_offset != 0)
3808 /* This symbol has a long name. Enter it in the string
3809 table we are building. If *debug_index != -1, the
3810 name has already been entered in the .debug section. */
3811 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3812 isym._n._n_n._n_offset = *debug_index;
3818 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
3822 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3823 if (indx == (bfd_size_type) -1)
3825 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3829 if (isym.n_sclass != C_BSTAT
3830 && isym.n_sclass != C_ESTAT
3831 && isym.n_sclass != C_DECL
3832 && isym.n_scnum > 0)
3834 isym.n_scnum = (*csectpp)->output_section->target_index;
3835 isym.n_value += ((*csectpp)->output_section->vma
3836 + (*csectpp)->output_offset
3840 /* The value of a C_FILE symbol is the symbol index of the
3841 next C_FILE symbol. The value of the last C_FILE symbol
3842 is -1. We try to get this right, below, just before we
3843 write the symbols out, but in the general case we may
3844 have to write the symbol out twice. */
3845 if (isym.n_sclass == C_FILE)
3847 if (finfo->last_file_index != -1
3848 && finfo->last_file.n_value != (long) output_index)
3850 /* We must correct the value of the last C_FILE entry. */
3851 finfo->last_file.n_value = output_index;
3852 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3854 /* The last C_FILE symbol is in this input file. */
3855 bfd_coff_swap_sym_out (output_bfd,
3856 (PTR) &finfo->last_file,
3857 (PTR) (finfo->outsyms
3858 + ((finfo->last_file_index
3864 /* We have already written out the last C_FILE
3865 symbol. We need to write it out again. We
3866 borrow *outsym temporarily. */
3867 bfd_coff_swap_sym_out (output_bfd,
3868 (PTR) &finfo->last_file,
3870 if (bfd_seek (output_bfd,
3871 (obj_sym_filepos (output_bfd)
3872 + finfo->last_file_index * osymesz),
3874 || (bfd_write (outsym, osymesz, 1, output_bfd)
3880 finfo->last_file_index = output_index;
3881 finfo->last_file = isym;
3884 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3885 into the line numbers. We update the symbol values when
3886 we handle the line numbers. */
3887 if (isym.n_sclass == C_BINCL
3888 || isym.n_sclass == C_EINCL)
3890 isym.n_value = finfo->line_filepos;
3894 /* Output the symbol. */
3896 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
3898 *indexp = output_index;
3900 if (isym.n_sclass == C_EXT)
3903 struct xcoff_link_hash_entry *h;
3905 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3907 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3908 BFD_ASSERT (h != NULL);
3909 h->indx = output_index;
3912 /* If this is a symbol in the TOC which we may have merged
3913 (class XMC_TC), remember the symbol index of the TOC
3915 if (isym.n_sclass == C_HIDEXT
3916 && aux.x_csect.x_smclas == XMC_TC
3917 && *sym_hash != NULL)
3919 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3920 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3921 (*sym_hash)->u.toc_indx = output_index;
3924 output_index += add;
3925 outsym += add * osymesz;
3928 esym += add * isymesz;
3932 if (debug_index != NULL)
3935 for (--add; add > 0; --add)
3939 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3940 done in a separate pass, because we don't know the correct symbol
3941 indices until we have already decided which symbols we are going
3944 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3945 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3946 isymp = finfo->internal_syms;
3947 indexp = finfo->sym_indices;
3948 csectpp = xcoff_data (input_bfd)->csects;
3949 outsym = finfo->outsyms;
3950 while (esym < esym_end)
3954 add = 1 + isymp->n_numaux;
3957 esym += add * isymesz;
3962 if (isymp->n_sclass == C_BSTAT)
3964 struct internal_syment isym;
3967 /* The value of a C_BSTAT symbol is the symbol table
3968 index of the containing csect. */
3969 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
3970 indx = isym.n_value;
3971 if (indx < obj_raw_syment_count (input_bfd))
3975 symindx = finfo->sym_indices[indx];
3979 isym.n_value = symindx;
3980 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
3988 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3990 union internal_auxent aux;
3992 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
3993 isymp->n_sclass, i, isymp->n_numaux,
3996 if (isymp->n_sclass == C_FILE)
3998 /* This is the file name (or some comment put in by
3999 the compiler). If it is long, we must put it in
4000 the string table. */
4001 if (aux.x_file.x_n.x_zeroes == 0
4002 && aux.x_file.x_n.x_offset != 0)
4004 const char *filename;
4007 BFD_ASSERT (aux.x_file.x_n.x_offset
4008 >= STRING_SIZE_SIZE);
4009 if (strings == NULL)
4011 strings = _bfd_coff_read_string_table (input_bfd);
4012 if (strings == NULL)
4015 filename = strings + aux.x_file.x_n.x_offset;
4016 indx = _bfd_stringtab_add (finfo->strtab, filename,
4018 if (indx == (bfd_size_type) -1)
4020 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4023 else if ((isymp->n_sclass == C_EXT
4024 || isymp->n_sclass == C_HIDEXT)
4025 && i + 1 == isymp->n_numaux)
4027 /* We don't support type checking. I don't know if
4029 aux.x_csect.x_parmhash = 0;
4030 /* I don't think anybody uses these fields, but we'd
4031 better clobber them just in case. */
4032 aux.x_csect.x_stab = 0;
4033 aux.x_csect.x_snstab = 0;
4034 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4038 indx = aux.x_csect.x_scnlen.l;
4039 if (indx < obj_raw_syment_count (input_bfd))
4043 symindx = finfo->sym_indices[indx];
4045 aux.x_sym.x_tagndx.l = 0;
4047 aux.x_sym.x_tagndx.l = symindx;
4051 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4055 if (ISFCN (isymp->n_type)
4056 || ISTAG (isymp->n_sclass)
4057 || isymp->n_sclass == C_BLOCK)
4059 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4061 && indx < obj_raw_syment_count (input_bfd))
4063 /* We look forward through the symbol for
4064 the index of the next symbol we are going
4065 to include. I don't know if this is
4067 while (finfo->sym_indices[indx] < 0
4068 && indx < obj_raw_syment_count (input_bfd))
4070 if (indx >= obj_raw_syment_count (input_bfd))
4071 indx = output_index;
4073 indx = finfo->sym_indices[indx];
4074 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4078 indx = aux.x_sym.x_tagndx.l;
4079 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4083 symindx = finfo->sym_indices[indx];
4085 aux.x_sym.x_tagndx.l = 0;
4087 aux.x_sym.x_tagndx.l = symindx;
4091 /* Copy over the line numbers, unless we are stripping
4092 them. We do this on a symbol by symbol basis in
4093 order to more easily handle garbage collection. */
4094 if ((isymp->n_sclass == C_EXT
4095 || isymp->n_sclass == C_HIDEXT)
4097 && isymp->n_numaux > 1
4098 && ISFCN (isymp->n_type)
4099 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4101 if (finfo->info->strip != strip_none
4102 && finfo->info->strip != strip_some)
4103 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4106 asection *enclosing;
4107 unsigned int enc_count;
4108 bfd_size_type linoff;
4109 struct internal_lineno lin;
4112 enclosing = xcoff_section_data (abfd, o)->enclosing;
4113 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4114 if (oline != enclosing)
4116 if (bfd_seek (input_bfd,
4117 enclosing->line_filepos,
4119 || (bfd_read (finfo->linenos, linesz,
4120 enc_count, input_bfd)
4121 != linesz * enc_count))
4126 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4127 - enclosing->line_filepos);
4129 bfd_coff_swap_lineno_in (input_bfd,
4130 (PTR) (finfo->linenos + linoff),
4133 || ((bfd_size_type) lin.l_addr.l_symndx
4137 obj_coff_external_syms (input_bfd)))
4139 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4142 bfd_byte *linpend, *linp;
4144 bfd_size_type count;
4146 lin.l_addr.l_symndx = *indexp;
4147 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4148 (PTR) (finfo->linenos
4151 linpend = (finfo->linenos
4152 + enc_count * linesz);
4153 offset = (o->output_section->vma
4156 for (linp = finfo->linenos + linoff + linesz;
4160 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4162 if (lin.l_lnno == 0)
4164 lin.l_addr.l_paddr += offset;
4165 bfd_coff_swap_lineno_out (output_bfd,
4170 count = (linp - (finfo->linenos + linoff)) / linesz;
4172 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4173 (o->output_section->line_filepos
4174 + o->output_section->lineno_count * linesz);
4176 if (bfd_seek (output_bfd,
4177 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4179 || (bfd_write (finfo->linenos + linoff,
4180 linesz, count, output_bfd)
4184 o->output_section->lineno_count += count;
4188 struct internal_syment *iisp, *iispend;
4192 /* Update any C_BINCL or C_EINCL symbols
4193 that refer to a line number in the
4194 range we just output. */
4195 iisp = finfo->internal_syms;
4197 + obj_raw_syment_count (input_bfd));
4198 iindp = finfo->sym_indices;
4199 oos = finfo->outsyms;
4200 while (iisp < iispend)
4202 if ((iisp->n_sclass == C_BINCL
4203 || iisp->n_sclass == C_EINCL)
4204 && ((bfd_size_type) iisp->n_value
4205 >= enclosing->line_filepos + linoff)
4206 && ((bfd_size_type) iisp->n_value
4207 < (enclosing->line_filepos
4208 + enc_count * linesz)))
4210 struct internal_syment iis;
4212 bfd_coff_swap_sym_in (output_bfd,
4217 - enclosing->line_filepos
4219 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4220 bfd_coff_swap_sym_out (output_bfd,
4226 iisp += iisp->n_numaux + 1;
4227 iindp += iisp->n_numaux + 1;
4228 oos += (iisp->n_numaux + 1) * osymesz;
4235 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4236 isymp->n_sclass, i, isymp->n_numaux,
4248 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4249 symbol will be the first symbol in the next input file. In the
4250 normal case, this will save us from writing out the C_FILE symbol
4252 if (finfo->last_file_index != -1
4253 && (bfd_size_type) finfo->last_file_index >= syment_base)
4255 finfo->last_file.n_value = output_index;
4256 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
4257 (PTR) (finfo->outsyms
4258 + ((finfo->last_file_index - syment_base)
4262 /* Write the modified symbols to the output file. */
4263 if (outsym > finfo->outsyms)
4265 if (bfd_seek (output_bfd,
4266 obj_sym_filepos (output_bfd) + syment_base * osymesz,
4268 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
4270 != (bfd_size_type) (outsym - finfo->outsyms)))
4273 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4274 + (outsym - finfo->outsyms) / osymesz)
4277 obj_raw_syment_count (output_bfd) = output_index;
4280 /* Don't let the linker relocation routines discard the symbols. */
4281 keep_syms = obj_coff_keep_syms (input_bfd);
4282 obj_coff_keep_syms (input_bfd) = true;
4284 /* Relocate the contents of each section. */
4285 for (o = input_bfd->sections; o != NULL; o = o->next)
4289 if ((o->flags & SEC_HAS_CONTENTS) == 0
4290 || o->_raw_size == 0
4291 || (o->flags & SEC_IN_MEMORY) != 0)
4294 /* We have set filepos correctly for the sections we created to
4295 represent csects, so bfd_get_section_contents should work. */
4296 if (coff_section_data (input_bfd, o) != NULL
4297 && coff_section_data (input_bfd, o)->contents != NULL)
4298 contents = coff_section_data (input_bfd, o)->contents;
4301 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4302 (file_ptr) 0, o->_raw_size))
4304 contents = finfo->contents;
4307 if ((o->flags & SEC_RELOC) != 0)
4310 struct internal_reloc *internal_relocs;
4311 struct internal_reloc *irel;
4313 struct internal_reloc *irelend;
4314 struct xcoff_link_hash_entry **rel_hash;
4317 /* Read in the relocs. */
4318 target_index = o->output_section->target_index;
4319 internal_relocs = (xcoff_read_internal_relocs
4320 (input_bfd, o, false, finfo->external_relocs,
4322 (finfo->section_info[target_index].relocs
4323 + o->output_section->reloc_count)));
4324 if (internal_relocs == NULL)
4327 /* Call processor specific code to relocate the section
4329 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4333 finfo->internal_syms,
4334 xcoff_data (input_bfd)->csects))
4337 offset = o->output_section->vma + o->output_offset - o->vma;
4338 irel = internal_relocs;
4339 irelend = irel + o->reloc_count;
4340 rel_hash = (finfo->section_info[target_index].rel_hashes
4341 + o->output_section->reloc_count);
4342 for (; irel < irelend; irel++, rel_hash++)
4344 struct xcoff_link_hash_entry *h = NULL;
4345 struct internal_ldrel ldrel;
4349 /* Adjust the reloc address and symbol index. */
4351 irel->r_vaddr += offset;
4353 r_symndx = irel->r_symndx;
4357 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4359 && (irel->r_type == R_TOC
4360 || irel->r_type == R_GL
4361 || irel->r_type == R_TCL
4362 || irel->r_type == R_TRL
4363 || irel->r_type == R_TRLA))
4365 /* This is a TOC relative reloc with a symbol
4366 attached. The symbol should be the one which
4367 this reloc is for. We want to make this
4368 reloc against the TOC address of the symbol,
4369 not the symbol itself. */
4370 BFD_ASSERT (h->toc_section != NULL);
4371 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4372 if (h->u.toc_indx != -1)
4373 irel->r_symndx = h->u.toc_indx;
4376 struct xcoff_toc_rel_hash *n;
4377 struct xcoff_link_section_info *si;
4379 n = ((struct xcoff_toc_rel_hash *)
4380 bfd_alloc (finfo->output_bfd,
4381 sizeof (struct xcoff_toc_rel_hash)));
4384 bfd_set_error (bfd_error_no_memory);
4387 si = finfo->section_info + target_index;
4388 n->next = si->toc_rel_hashes;
4391 si->toc_rel_hashes = n;
4396 /* This is a global symbol. */
4398 irel->r_symndx = h->indx;
4401 /* This symbol is being written at the end
4402 of the file, and we do not yet know the
4403 symbol index. We save the pointer to the
4404 hash table entry in the rel_hash list.
4405 We set the indx field to -2 to indicate
4406 that this symbol must not be stripped. */
4415 indx = finfo->sym_indices[r_symndx];
4419 struct internal_syment *is;
4421 /* Relocations against a TC0 TOC anchor are
4422 automatically transformed to be against
4423 the TOC anchor in the output file. */
4424 is = finfo->internal_syms + r_symndx;
4425 if (is->n_sclass == C_HIDEXT
4426 && is->n_numaux > 0)
4429 union internal_auxent aux;
4433 obj_coff_external_syms (input_bfd))
4434 + ((r_symndx + is->n_numaux)
4436 bfd_coff_swap_aux_in (input_bfd, auxptr,
4437 is->n_type, is->n_sclass,
4441 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4442 && aux.x_csect.x_smclas == XMC_TC0)
4443 indx = finfo->toc_symindx;
4448 irel->r_symndx = indx;
4451 struct internal_syment *is;
4453 char buf[SYMNMLEN + 1];
4455 /* This reloc is against a symbol we are
4456 stripping. It would be possible to handle
4457 this case, but I don't think it's worth it. */
4458 is = finfo->internal_syms + r_symndx;
4460 name = (_bfd_coff_internal_syment_name
4461 (input_bfd, is, buf));
4465 if (! ((*finfo->info->callbacks->unattached_reloc)
4466 (finfo->info, name, input_bfd, o,
4473 switch (irel->r_type)
4477 || h->root.type == bfd_link_hash_defined
4478 || h->root.type == bfd_link_hash_defweak
4479 || h->root.type == bfd_link_hash_common)
4486 /* This reloc needs to be copied into the .loader
4488 ldrel.l_vaddr = irel->r_vaddr;
4490 ldrel.l_symndx = -1;
4492 || (h->root.type == bfd_link_hash_defined
4493 || h->root.type == bfd_link_hash_defweak
4494 || h->root.type == bfd_link_hash_common))
4499 sec = xcoff_data (input_bfd)->csects[r_symndx];
4500 else if (h->root.type == bfd_link_hash_common)
4501 sec = h->root.u.c.p->section;
4503 sec = h->root.u.def.section;
4504 sec = sec->output_section;
4506 if (strcmp (sec->name, ".text") == 0)
4508 else if (strcmp (sec->name, ".data") == 0)
4510 else if (strcmp (sec->name, ".bss") == 0)
4514 (*_bfd_error_handler)
4515 ("%s: loader reloc in unrecognized section `%s'",
4516 bfd_get_filename (input_bfd),
4518 bfd_set_error (bfd_error_nonrepresentable_section);
4526 (*_bfd_error_handler)
4527 ("%s: `%s' in loader reloc but not loader sym",
4528 bfd_get_filename (input_bfd),
4529 h->root.root.string);
4530 bfd_set_error (bfd_error_bad_value);
4533 ldrel.l_symndx = h->ldindx;
4535 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4536 ldrel.l_rsecnm = o->output_section->target_index;
4537 if (xcoff_hash_table (finfo->info)->textro
4538 && strcmp (o->output_section->name, ".text") == 0)
4540 (*_bfd_error_handler)
4541 ("%s: loader reloc in read-only section %s",
4542 bfd_get_filename (input_bfd),
4543 bfd_get_section_name (finfo->output_bfd,
4544 o->output_section));
4545 bfd_set_error (bfd_error_invalid_operation);
4548 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4550 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4559 /* We should never need a .loader reloc for a TOC
4565 o->output_section->reloc_count += o->reloc_count;
4568 /* Write out the modified section contents. */
4569 if (! bfd_set_section_contents (output_bfd, o->output_section,
4570 contents, o->output_offset,
4571 (o->_cooked_size != 0
4577 obj_coff_keep_syms (input_bfd) = keep_syms;
4579 if (! finfo->info->keep_memory)
4581 if (! _bfd_coff_free_symbols (input_bfd))
4591 /* Write out a non-XCOFF global symbol. */
4594 xcoff_write_global_symbol (h, p)
4595 struct xcoff_link_hash_entry *h;
4598 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4601 struct internal_syment isym;
4602 union internal_auxent aux;
4604 output_bfd = finfo->output_bfd;
4606 /* If this symbol was garbage collected, just skip it. */
4607 if (xcoff_hash_table (finfo->info)->gc
4608 && (h->flags & XCOFF_MARK) == 0)
4611 /* If we need a .loader section entry, write it out. */
4612 if (h->ldsym != NULL)
4614 struct internal_ldsym *ldsym;
4619 if (h->root.type == bfd_link_hash_undefined
4620 || h->root.type == bfd_link_hash_undefweak)
4623 ldsym->l_scnum = N_UNDEF;
4624 ldsym->l_smtype = XTY_ER;
4625 impbfd = h->root.u.undef.abfd;
4627 else if (h->root.type == bfd_link_hash_defined
4628 || h->root.type == bfd_link_hash_defweak)
4632 sec = h->root.u.def.section;
4633 ldsym->l_value = (sec->output_section->vma
4634 + sec->output_offset
4635 + h->root.u.def.value);
4636 ldsym->l_scnum = sec->output_section->target_index;
4637 ldsym->l_smtype = XTY_SD;
4638 impbfd = sec->owner;
4643 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4644 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4645 || (h->flags & XCOFF_IMPORT) != 0)
4646 ldsym->l_smtype |= L_IMPORT;
4647 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4648 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4649 || (h->flags & XCOFF_EXPORT) != 0)
4650 ldsym->l_smtype |= L_EXPORT;
4651 if ((h->flags & XCOFF_ENTRY) != 0)
4652 ldsym->l_smtype |= L_ENTRY;
4654 ldsym->l_smclas = h->smclas;
4656 if (ldsym->l_ifile == (bfd_size_type) -1)
4658 else if (ldsym->l_ifile == 0)
4660 if ((ldsym->l_smtype & L_IMPORT) == 0)
4662 else if (impbfd == NULL)
4666 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4667 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4673 BFD_ASSERT (h->ldindx >= 0);
4674 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4675 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4679 /* If this symbol needs global linkage code, write it out. */
4680 if (h->root.type == bfd_link_hash_defined
4681 && (h->root.u.def.section
4682 == xcoff_hash_table (finfo->info)->linkage_section))
4688 p = h->root.u.def.section->contents + h->root.u.def.value;
4690 /* The first instruction in the global linkage code loads a
4691 specific TOC element. */
4692 tocoff = (h->descriptor->toc_section->output_section->vma
4693 + h->descriptor->toc_section->output_offset
4694 - xcoff_data (output_bfd)->toc);
4695 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4696 tocoff += h->descriptor->u.toc_offset;
4697 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4699 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
4701 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
4704 /* If we created a TOC entry for this symbol, write out the required
4706 if ((h->flags & XCOFF_SET_TOC) != 0)
4711 struct internal_reloc *irel;
4712 struct internal_ldrel ldrel;
4714 tocsec = h->toc_section;
4715 osec = tocsec->output_section;
4716 oindx = osec->target_index;
4717 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4718 irel->r_vaddr = (osec->vma
4719 + tocsec->output_offset
4722 irel->r_symndx = h->indx;
4726 irel->r_symndx = obj_raw_syment_count (output_bfd);
4728 irel->r_type = R_POS;
4730 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4731 ++osec->reloc_count;
4733 BFD_ASSERT (h->ldindx >= 0);
4734 ldrel.l_vaddr = irel->r_vaddr;
4735 ldrel.l_symndx = h->ldindx;
4736 ldrel.l_rtype = (31 << 8) | R_POS;
4737 ldrel.l_rsecnm = oindx;
4738 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4746 && (finfo->info->strip == strip_all
4747 || (finfo->info->strip == strip_some
4748 && (bfd_hash_lookup (finfo->info->keep_hash,
4749 h->root.root.string, false, false)
4754 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
4757 outsym = finfo->outsyms;
4759 memset (&aux, 0, sizeof aux);
4761 h->indx = obj_raw_syment_count (output_bfd);
4763 if (strlen (h->root.root.string) <= SYMNMLEN)
4764 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
4771 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4773 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
4775 if (indx == (bfd_size_type) -1)
4777 isym._n._n_n._n_zeroes = 0;
4778 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4781 if (h->root.type == bfd_link_hash_undefined
4782 || h->root.type == bfd_link_hash_undefweak)
4785 isym.n_scnum = N_UNDEF;
4786 isym.n_sclass = C_EXT;
4787 aux.x_csect.x_smtyp = XTY_ER;
4789 else if (h->root.type == bfd_link_hash_defined
4790 || h->root.type == bfd_link_hash_defweak)
4792 struct xcoff_link_size_list *l;
4794 isym.n_value = (h->root.u.def.section->output_section->vma
4795 + h->root.u.def.section->output_offset
4796 + h->root.u.def.value);
4797 isym.n_scnum = h->root.u.def.section->output_section->target_index;
4798 isym.n_sclass = C_HIDEXT;
4799 aux.x_csect.x_smtyp = XTY_SD;
4801 if ((h->flags & XCOFF_HAS_SIZE) != 0)
4803 for (l = xcoff_hash_table (finfo->info)->size_list;
4809 aux.x_csect.x_scnlen.l = l->size;
4815 else if (h->root.type == bfd_link_hash_common)
4817 isym.n_value = (h->root.u.c.p->section->output_section->vma
4818 + h->root.u.c.p->section->output_offset);
4819 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
4820 isym.n_sclass = C_EXT;
4821 aux.x_csect.x_smtyp = XTY_CM;
4822 aux.x_csect.x_scnlen.l = h->root.u.c.size;
4827 isym.n_type = T_NULL;
4830 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4831 outsym += bfd_coff_symesz (output_bfd);
4833 aux.x_csect.x_smclas = h->smclas;
4835 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
4837 outsym += bfd_coff_auxesz (output_bfd);
4839 if (h->root.type == bfd_link_hash_defined
4840 || h->root.type == bfd_link_hash_defweak)
4842 /* We just output an SD symbol. Now output an LD symbol. */
4846 isym.n_sclass = C_EXT;
4847 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4848 outsym += bfd_coff_symesz (output_bfd);
4850 aux.x_csect.x_smtyp = XTY_LD;
4851 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
4853 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
4855 outsym += bfd_coff_auxesz (output_bfd);
4858 if (bfd_seek (output_bfd,
4859 (obj_sym_filepos (output_bfd)
4860 + (obj_raw_syment_count (output_bfd)
4861 * bfd_coff_symesz (output_bfd))),
4863 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
4864 != (bfd_size_type) (outsym - finfo->outsyms)))
4866 obj_raw_syment_count (output_bfd) +=
4867 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4872 /* Handle a link order which is supposed to generate a reloc. */
4875 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
4877 struct xcoff_final_link_info *finfo;
4878 asection *output_section;
4879 struct bfd_link_order *link_order;
4881 reloc_howto_type *howto;
4882 struct xcoff_link_hash_entry *h;
4886 struct internal_reloc *irel;
4887 struct xcoff_link_hash_entry **rel_hash_ptr;
4888 struct internal_ldrel ldrel;
4890 if (link_order->type == bfd_section_reloc_link_order)
4892 /* We need to somehow locate a symbol in the right section. The
4893 symbol must either have a value of zero, or we must adjust
4894 the addend by the value of the symbol. FIXME: Write this
4895 when we need it. The old linker couldn't handle this anyhow. */
4899 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4902 bfd_set_error (bfd_error_bad_value);
4906 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
4907 link_order->u.reloc.p->u.name,
4908 false, false, true);
4911 if (! ((*finfo->info->callbacks->unattached_reloc)
4912 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4913 (asection *) NULL, (bfd_vma) 0)))
4918 if (h->root.type == bfd_link_hash_common)
4920 hsec = h->root.u.c.p->section;
4923 else if (h->root.type == bfd_link_hash_defined
4924 || h->root.type == bfd_link_hash_defweak)
4926 hsec = h->root.u.def.section;
4927 hval = h->root.u.def.value;
4935 addend = link_order->u.reloc.p->addend;
4937 addend += (hsec->output_section->vma
4938 + hsec->output_offset
4945 bfd_reloc_status_type rstat;
4948 size = bfd_get_reloc_size (howto);
4949 buf = (bfd_byte *) bfd_zmalloc (size);
4952 bfd_set_error (bfd_error_no_memory);
4956 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
4962 case bfd_reloc_outofrange:
4964 case bfd_reloc_overflow:
4965 if (! ((*finfo->info->callbacks->reloc_overflow)
4966 (finfo->info, link_order->u.reloc.p->u.name,
4967 howto->name, addend, (bfd *) NULL, (asection *) NULL,
4975 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4976 (file_ptr) link_order->offset, size);
4982 /* Store the reloc information in the right place. It will get
4983 swapped and written out at the end of the final_link routine. */
4985 irel = (finfo->section_info[output_section->target_index].relocs
4986 + output_section->reloc_count);
4987 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
4988 + output_section->reloc_count);
4990 memset (irel, 0, sizeof (struct internal_reloc));
4991 *rel_hash_ptr = NULL;
4993 irel->r_vaddr = output_section->vma + link_order->offset;
4996 irel->r_symndx = h->indx;
4999 /* Set the index to -2 to force this symbol to get written out. */
5005 irel->r_type = howto->type;
5006 irel->r_size = howto->bitsize - 1;
5007 if (howto->complain_on_overflow == complain_overflow_signed)
5008 irel->r_size |= 0x80;
5010 ++output_section->reloc_count;
5012 /* Now output the reloc to the .loader section. */
5014 ldrel.l_vaddr = irel->r_vaddr;
5018 const char *secname;
5020 secname = hsec->output_section->name;
5022 if (strcmp (secname, ".text") == 0)
5024 else if (strcmp (secname, ".data") == 0)
5026 else if (strcmp (secname, ".bss") == 0)
5030 (*_bfd_error_handler)
5031 ("%s: loader reloc in unrecognized section `%s'",
5032 bfd_get_filename (output_bfd), secname);
5033 bfd_set_error (bfd_error_nonrepresentable_section);
5041 (*_bfd_error_handler)
5042 ("%s: `%s' in loader reloc but not loader sym",
5043 bfd_get_filename (output_bfd),
5044 h->root.root.string);
5045 bfd_set_error (bfd_error_bad_value);
5048 ldrel.l_symndx = h->ldindx;
5051 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5052 ldrel.l_rsecnm = output_section->target_index;
5053 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5059 /* Sort relocs by VMA. This is called via qsort. */
5062 xcoff_sort_relocs (p1, p2)
5066 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5067 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5069 if (r1->r_vaddr > r2->r_vaddr)
5071 else if (r1->r_vaddr < r2->r_vaddr)
5077 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5078 This is currently the only processor which uses XCOFF; I hope that
5079 will never change. */
5082 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
5083 input_section, contents, relocs, syms,
5086 struct bfd_link_info *info;
5088 asection *input_section;
5090 struct internal_reloc *relocs;
5091 struct internal_syment *syms;
5092 asection **sections;
5094 struct internal_reloc *rel;
5095 struct internal_reloc *relend;
5098 relend = rel + input_section->reloc_count;
5099 for (; rel < relend; rel++)
5102 struct xcoff_link_hash_entry *h;
5103 struct internal_syment *sym;
5106 struct reloc_howto_struct howto;
5107 bfd_reloc_status_type rstat;
5109 /* Relocation type R_REF is a special relocation type which is
5110 merely used to prevent garbage collection from occurring for
5111 the csect including the symbol which it references. */
5112 if (rel->r_type == R_REF)
5115 symndx = rel->r_symndx;
5125 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
5126 sym = syms + symndx;
5127 addend = - sym->n_value;
5130 /* We build the howto information on the fly. */
5132 howto.type = rel->r_type;
5133 howto.rightshift = 0;
5135 howto.bitsize = (rel->r_size & 0x1f) + 1;
5136 howto.pc_relative = false;
5138 if ((rel->r_size & 0x80) != 0)
5139 howto.complain_on_overflow = complain_overflow_signed;
5141 howto.complain_on_overflow = complain_overflow_bitfield;
5142 howto.special_function = NULL;
5143 howto.name = "internal";
5144 howto.partial_inplace = true;
5145 if (howto.bitsize == 32)
5146 howto.src_mask = howto.dst_mask = 0xffffffff;
5149 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
5150 if (howto.bitsize == 16)
5153 howto.pcrel_offset = false;
5163 sec = bfd_abs_section_ptr;
5168 sec = sections[symndx];
5169 val = (sec->output_section->vma
5170 + sec->output_offset
5177 if (h->root.type == bfd_link_hash_defined
5178 || h->root.type == bfd_link_hash_defweak)
5182 sec = h->root.u.def.section;
5183 val = (h->root.u.def.value
5184 + sec->output_section->vma
5185 + sec->output_offset);
5187 else if (h->root.type == bfd_link_hash_common)
5191 sec = h->root.u.c.p->section;
5192 val = (sec->output_section->vma
5193 + sec->output_offset);
5195 else if ((h->flags & XCOFF_REF_DYNAMIC) != 0
5196 || (h->flags & XCOFF_IMPORT) != 0)
5198 /* Every symbol in a shared object is defined somewhere. */
5201 else if (! info->relocateable
5204 if (! ((*info->callbacks->undefined_symbol)
5205 (info, h->root.root.string, input_bfd, input_section,
5206 rel->r_vaddr - input_section->vma)))
5211 /* I took the relocation type definitions from two documents:
5212 the PowerPC AIX Version 4 Application Binary Interface, First
5213 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5214 32-Bit Hardware Implementation (June 30, 1994). Differences
5215 between the documents are noted below. */
5217 switch (rel->r_type)
5222 /* These relocs are defined by the PowerPC ABI to be
5223 relative branches which use half of the difference
5224 between the symbol and the program counter. I can't
5225 quite figure out when this is useful. These relocs are
5226 not defined by the PowerOpen ABI. */
5228 (*_bfd_error_handler)
5229 ("%s: unsupported relocation type 0x%02x",
5230 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
5231 bfd_set_error (bfd_error_bad_value);
5234 /* Simple positive relocation. */
5237 /* Simple negative relocation. */
5241 /* Simple PC relative relocation. */
5242 howto.pc_relative = true;
5245 /* TOC relative relocation. The value in the instruction in
5246 the input file is the offset from the input file TOC to
5247 the desired location. We want the offset from the final
5248 TOC to the desired location. We have:
5253 so we must change insn by on - in.
5256 /* Global linkage relocation. The value of this relocation
5257 is the address of the entry in the TOC section. */
5259 /* Local object TOC address. I can't figure out the
5260 difference between this and case R_GL. */
5262 /* TOC relative relocation. A TOC relative load instruction
5263 which may be changed to a load address instruction.
5264 FIXME: We don't currently implement this optimization. */
5266 /* TOC relative relocation. This is a TOC relative load
5267 address instruction which may be changed to a load
5268 instruction. FIXME: I don't know if this is the correct
5270 if (h != NULL && h->toc_section == NULL)
5272 (*_bfd_error_handler)
5273 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5274 bfd_get_filename (input_bfd), rel->r_vaddr,
5275 h->root.root.string);
5276 bfd_set_error (bfd_error_bad_value);
5281 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5282 val = (h->toc_section->output_section->vma
5283 + h->toc_section->output_offset);
5285 val = ((val - xcoff_data (output_bfd)->toc)
5286 - (sym->n_value - xcoff_data (input_bfd)->toc));
5290 /* Absolute branch. We don't want to mess with the lower
5291 two bits of the instruction. */
5293 /* The PowerPC ABI defines this as an absolute call which
5294 may be modified to become a relative call. The PowerOpen
5295 ABI does not define this relocation type. */
5297 /* Absolute branch which may be modified to become a
5300 /* The PowerPC ABI defines this as an absolute branch to a
5301 fixed address which may be modified to an absolute branch
5302 to a symbol. The PowerOpen ABI does not define this
5305 /* The PowerPC ABI defines this as an absolute branch to a
5306 fixed address which may be modified to a relative branch.
5307 The PowerOpen ABI does not define this relocation type. */
5308 howto.src_mask &= ~3;
5309 howto.dst_mask = howto.src_mask;
5312 /* Relative branch. We don't want to mess with the lower
5313 two bits of the instruction. */
5315 /* The PowerPC ABI defines this as a relative call which may
5316 be modified to become an absolute call. The PowerOpen
5317 ABI does not define this relocation type. */
5319 /* A relative branch which may be modified to become an
5320 absolute branch. FIXME: We don't implement this,
5321 although we should for symbols of storage mapping class
5323 howto.pc_relative = true;
5324 howto.src_mask &= ~3;
5325 howto.dst_mask = howto.src_mask;
5328 /* The PowerPC AIX ABI describes this as a load which may be
5329 changed to a load address. The PowerOpen ABI says this
5330 is the same as case R_POS. */
5333 /* The PowerPC AIX ABI describes this as a load address
5334 which may be changed to a load. The PowerOpen ABI says
5335 this is the same as R_POS. */
5339 /* If we see an R_BR or R_RBR reloc which is jumping to global
5340 linkage code, and it is followed by an appropriate cror nop
5341 instruction, we replace the cror with lwz r2,20(r1). This
5342 restores the TOC after the glink code. Contrariwise, if the
5343 call is followed by a lwz r2,20(r1), but the call is not
5344 going to global linkage code, we can replace the load with a
5346 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
5348 && h->root.type == bfd_link_hash_defined
5349 && (rel->r_vaddr - input_section->vma + 8
5350 <= input_section->_cooked_size))
5355 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
5356 next = bfd_get_32 (input_bfd, pnext);
5357 if (h->smclas == XMC_GL)
5359 if (next == 0x4def7b82 /* cror 15,15,15 */
5360 || next == 0x4ffffb82) /* cror 31,31,31 */
5361 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
5365 if (next == 0x80410014) /* lwz r1,20(r1) */
5366 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
5370 /* A PC relative reloc includes the section address. */
5371 if (howto.pc_relative)
5372 addend += input_section->vma;
5374 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
5376 rel->r_vaddr - input_section->vma,
5385 case bfd_reloc_overflow:
5388 char buf[SYMNMLEN + 1];
5389 char howto_name[10];
5394 name = h->root.root.string;
5397 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
5401 sprintf (howto_name, "0x%02x", rel->r_type);
5403 if (! ((*info->callbacks->reloc_overflow)
5404 (info, name, howto_name, (bfd_vma) 0, input_bfd,
5405 input_section, rel->r_vaddr - input_section->vma)))