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;
943 /* Likewise for the .debug section. */
944 if (xcoff_hash_table (info)->debug_section == NULL)
948 dsec = bfd_make_section_anyway (abfd, ".debug");
951 xcoff_hash_table (info)->debug_section = dsec;
952 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
955 if (info->keep_memory)
956 default_copy = false;
960 symcount = obj_raw_syment_count (abfd);
962 /* We keep a list of the linker hash table entries that correspond
963 to each external symbol. */
964 sym_hash = ((struct xcoff_link_hash_entry **)
967 * sizeof (struct xcoff_link_hash_entry *))));
968 if (sym_hash == NULL && symcount != 0)
970 bfd_set_error (bfd_error_no_memory);
973 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
975 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
977 /* Because of the weird stuff we are doing with XCOFF csects, we can
978 not easily determine which section a symbol is in, so we store
979 the information in the tdata for the input file. */
980 csect_cache = ((asection **)
981 bfd_alloc (abfd, symcount * sizeof (asection *)));
982 if (csect_cache == NULL && symcount != 0)
984 bfd_set_error (bfd_error_no_memory);
987 xcoff_data (abfd)->csects = csect_cache;
988 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
990 /* While splitting sections into csects, we need to assign the
991 relocs correctly. The relocs and the csects must both be in
992 order by VMA within a given section, so we handle this by
993 scanning along the relocs as we process the csects. We index
994 into reloc_info using the section target_index. */
995 reloc_info = ((struct reloc_info_struct *)
996 malloc ((abfd->section_count + 1)
997 * sizeof (struct reloc_info_struct)));
998 if (reloc_info == NULL)
1000 bfd_set_error (bfd_error_no_memory);
1003 memset ((PTR) reloc_info, 0,
1004 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1006 /* Read in the relocs and line numbers for each section. */
1007 linesz = bfd_coff_linesz (abfd);
1009 for (o = abfd->sections; o != NULL; o = o->next)
1012 if ((o->flags & SEC_RELOC) != 0)
1014 reloc_info[o->target_index].relocs =
1015 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1016 false, (struct internal_reloc *) NULL);
1017 reloc_info[o->target_index].csects =
1018 (asection **) malloc (o->reloc_count * sizeof (asection *));
1019 if (reloc_info[o->target_index].csects == NULL)
1021 bfd_set_error (bfd_error_no_memory);
1024 memset (reloc_info[o->target_index].csects, 0,
1025 o->reloc_count * sizeof (asection *));
1028 if ((info->strip == strip_none || info->strip == strip_some)
1029 && o->lineno_count > 0)
1033 linenos = (bfd_byte *) malloc (o->lineno_count * linesz);
1034 if (linenos == NULL)
1036 bfd_set_error (bfd_error_no_memory);
1039 reloc_info[o->target_index].linenos = linenos;
1040 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1041 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1042 != linesz * o->lineno_count))
1047 /* Don't let the linker relocation routines discard the symbols. */
1048 keep_syms = obj_coff_keep_syms (abfd);
1049 obj_coff_keep_syms (abfd) = true;
1055 symesz = bfd_coff_symesz (abfd);
1056 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1057 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1058 esym_end = esym + symcount * symesz;
1059 while (esym < esym_end)
1061 struct internal_syment sym;
1062 union internal_auxent aux;
1064 char buf[SYMNMLEN + 1];
1069 struct xcoff_link_hash_entry *set_toc;
1071 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1073 /* In this pass we are only interested in symbols with csect
1075 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1077 if (sym.n_sclass == C_FILE && csect != NULL)
1079 xcoff_section_data (abfd, csect)->last_symndx =
1081 - (bfd_byte *) obj_coff_external_syms (abfd))
1087 *csect_cache = csect;
1088 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1089 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1091 *csect_cache = NULL;
1092 esym += (sym.n_numaux + 1) * symesz;
1093 sym_hash += sym.n_numaux + 1;
1094 csect_cache += sym.n_numaux + 1;
1098 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1102 /* If this symbol has line number information attached to it,
1103 and we're not stripping it, count the number of entries and
1104 add them to the count for this csect. In the final link pass
1105 we are going to attach line number information by symbol,
1106 rather than by section, in order to more easily handle
1107 garbage collection. */
1108 if ((info->strip == strip_none || info->strip == strip_some)
1111 && ISFCN (sym.n_type))
1113 union internal_auxent auxlin;
1115 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1116 sym.n_type, sym.n_sclass,
1117 0, sym.n_numaux, (PTR) &auxlin);
1118 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1120 asection *enclosing;
1121 bfd_size_type linoff;
1123 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1124 if (enclosing == NULL)
1126 (*_bfd_error_handler)
1127 ("%s: `%s' has line numbers but no enclosing section",
1128 bfd_get_filename (abfd), name);
1129 bfd_set_error (bfd_error_bad_value);
1132 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1133 - enclosing->line_filepos);
1134 if (linoff < enclosing->lineno_count * linesz)
1136 struct internal_lineno lin;
1137 bfd_byte *linpstart;
1139 linpstart = (reloc_info[enclosing->target_index].linenos
1141 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1143 && ((bfd_size_type) lin.l_addr.l_symndx
1145 - (bfd_byte *) obj_coff_external_syms (abfd))
1148 bfd_byte *linpend, *linp;
1150 linpend = (reloc_info[enclosing->target_index].linenos
1151 + enclosing->lineno_count * linesz);
1152 for (linp = linpstart + linesz;
1156 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1158 if (lin.l_lnno == 0)
1161 csect->lineno_count += (linp - linpstart) / linesz;
1162 /* The setting of line_filepos will only be
1163 useful if all the line number entries for a
1164 csect are contiguous; this only matters for
1166 if (csect->line_filepos == 0)
1167 csect->line_filepos =
1168 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1174 /* Pick up the csect auxiliary information. */
1176 if (sym.n_numaux == 0)
1178 (*_bfd_error_handler)
1179 ("%s: class %d symbol `%s' has no aux entries",
1180 bfd_get_filename (abfd), sym.n_sclass, name);
1181 bfd_set_error (bfd_error_bad_value);
1185 bfd_coff_swap_aux_in (abfd,
1186 (PTR) (esym + symesz * sym.n_numaux),
1187 sym.n_type, sym.n_sclass,
1188 sym.n_numaux - 1, sym.n_numaux,
1191 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1201 (*_bfd_error_handler)
1202 ("%s: symbol `%s' has unrecognized csect type %d",
1203 bfd_get_filename (abfd), name, smtyp);
1204 bfd_set_error (bfd_error_bad_value);
1208 /* This is an external reference. */
1209 if (sym.n_sclass == C_HIDEXT
1210 || sym.n_scnum != N_UNDEF
1211 || aux.x_csect.x_scnlen.l != 0)
1213 (*_bfd_error_handler)
1214 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1215 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1216 aux.x_csect.x_scnlen.l);
1217 bfd_set_error (bfd_error_bad_value);
1220 section = bfd_und_section_ptr;
1224 /* This is a csect definition. */
1228 xcoff_section_data (abfd, csect)->last_symndx =
1230 - (bfd_byte *) obj_coff_external_syms (abfd))
1237 /* When we see a TOC anchor, we record the TOC value. */
1238 if (aux.x_csect.x_smclas == XMC_TC0)
1240 if (sym.n_sclass != C_HIDEXT
1241 || aux.x_csect.x_scnlen.l != 0)
1243 (*_bfd_error_handler)
1244 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1245 bfd_get_filename (abfd), name, sym.n_sclass,
1246 aux.x_csect.x_scnlen.l);
1247 bfd_set_error (bfd_error_bad_value);
1250 xcoff_data (abfd)->toc = sym.n_value;
1253 /* We must merge TOC entries for the same symbol. We can
1254 merge two TOC entries if they are both C_HIDEXT, they
1255 both have the same name, they are both 4 bytes long, and
1256 they both have a relocation table entry for an external
1257 symbol with the same name. Unfortunately, this means
1258 that we must look through the relocations. Ick. */
1259 if (aux.x_csect.x_smclas == XMC_TC
1260 && sym.n_sclass == C_HIDEXT
1261 && aux.x_csect.x_scnlen.l == 4
1262 && info->hash->creator == abfd->xvec)
1264 asection *enclosing;
1265 struct internal_reloc *relocs;
1266 bfd_size_type relindx;
1267 struct internal_reloc *rel;
1269 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1270 if (enclosing == NULL)
1273 relocs = reloc_info[enclosing->target_index].relocs;
1274 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1276 rel = relocs + relindx;
1277 if (relindx < enclosing->reloc_count
1278 && rel->r_vaddr == (bfd_vma) sym.n_value
1279 && rel->r_size == 31
1280 && rel->r_type == R_POS)
1283 struct internal_syment relsym;
1285 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1286 + rel->r_symndx * symesz);
1287 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1288 if (relsym.n_sclass == C_EXT)
1290 const char *relname;
1291 char relbuf[SYMNMLEN + 1];
1293 struct xcoff_link_hash_entry *h;
1295 /* At this point we know that the TOC entry is
1296 for an externally visible symbol. */
1297 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1299 if (relname == NULL)
1302 /* We only merge TOC entries if the TC name is
1303 the same as the symbol name. This handles
1304 the normal case, but not common cases like
1305 SYM.P4 which gcc generates to store SYM + 4
1306 in the TOC. FIXME. */
1307 if (strcmp (name, relname) == 0)
1309 copy = (! info->keep_memory
1310 || relsym._n._n_n._n_zeroes != 0
1311 || relsym._n._n_n._n_offset == 0);
1312 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1313 relname, true, copy,
1318 /* At this point h->root.type could be
1319 bfd_link_hash_new. That should be OK,
1320 since we know for sure that we will come
1321 across this symbol as we step through the
1324 /* We store h in *sym_hash for the
1325 convenience of the relocate_section
1329 if (h->toc_section != NULL)
1331 asection **rel_csects;
1333 /* We already have a TOC entry for this
1334 symbol, so we can just ignore this
1337 reloc_info[enclosing->target_index].csects;
1338 rel_csects[relindx] = bfd_und_section_ptr;
1342 /* We are about to create a TOC entry for
1350 /* We need to create a new section. We get the name from
1351 the csect storage mapping class, so that the linker can
1352 accumulate similar csects together. */
1354 static const char *csect_name_by_class[] =
1356 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1357 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1360 const char *csect_name;
1361 asection *enclosing;
1363 if ((aux.x_csect.x_smclas >=
1364 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1365 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1367 (*_bfd_error_handler)
1368 ("%s: symbol `%s' has unrecognized smclas %d",
1369 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1370 bfd_set_error (bfd_error_bad_value);
1374 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1375 csect = bfd_make_section_anyway (abfd, csect_name);
1378 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1379 if (enclosing == NULL)
1381 if (! bfd_is_abs_section (enclosing)
1382 && ((bfd_vma) sym.n_value < enclosing->vma
1383 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1384 > enclosing->vma + enclosing->_raw_size)))
1386 (*_bfd_error_handler)
1387 ("%s: csect `%s' not in enclosing section",
1388 bfd_get_filename (abfd), name);
1389 bfd_set_error (bfd_error_bad_value);
1392 csect->vma = sym.n_value;
1393 csect->filepos = (enclosing->filepos
1396 csect->_raw_size = aux.x_csect.x_scnlen.l;
1397 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1398 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1400 /* Record the enclosing section in the tdata for this new
1402 csect->used_by_bfd =
1403 ((struct coff_section_tdata *)
1404 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1405 if (csect->used_by_bfd == NULL)
1407 bfd_set_error (bfd_error_no_memory);
1410 coff_section_data (abfd, csect)->tdata =
1411 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1412 if (coff_section_data (abfd, csect)->tdata == NULL)
1414 bfd_set_error (bfd_error_no_memory);
1417 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1418 xcoff_section_data (abfd, csect)->lineno_count =
1419 enclosing->lineno_count;
1421 if (enclosing->owner == abfd)
1423 struct internal_reloc *relocs;
1424 bfd_size_type relindx;
1425 struct internal_reloc *rel;
1426 asection **rel_csect;
1428 relocs = reloc_info[enclosing->target_index].relocs;
1429 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1431 rel = relocs + relindx;
1432 rel_csect = (reloc_info[enclosing->target_index].csects
1434 csect->rel_filepos = (enclosing->rel_filepos
1435 + relindx * bfd_coff_relsz (abfd));
1436 while (relindx < enclosing->reloc_count
1437 && *rel_csect == NULL
1438 && rel->r_vaddr < csect->vma + csect->_raw_size)
1441 csect->flags |= SEC_RELOC;
1442 ++csect->reloc_count;
1449 /* There are a number of other fields and section flags
1450 which we do not bother to set. */
1452 csect_index = ((esym
1453 - (bfd_byte *) obj_coff_external_syms (abfd))
1456 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1458 if (first_csect == NULL)
1459 first_csect = csect;
1461 /* If this symbol is C_EXT, we treat it as starting at the
1462 beginning of the newly created section. */
1463 if (sym.n_sclass == C_EXT)
1469 /* If this is a TOC section for a symbol, record it. */
1470 if (set_toc != NULL)
1471 set_toc->toc_section = csect;
1476 /* This is a label definition. The x_scnlen field is the
1477 symbol index of the csect. I believe that this must
1478 always follow the appropriate XTY_SD symbol, so I will
1484 if (aux.x_csect.x_scnlen.l < 0
1485 || (aux.x_csect.x_scnlen.l
1486 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1490 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1492 || (section->flags & SEC_HAS_CONTENTS) == 0)
1497 (*_bfd_error_handler)
1498 ("%s: misplaced XTY_LD `%s'",
1499 bfd_get_filename (abfd), name);
1500 bfd_set_error (bfd_error_bad_value);
1504 value = sym.n_value - csect->vma;
1509 /* This is an unitialized csect. We could base the name on
1510 the storage mapping class, but we don't bother. If this
1511 csect is externally visible, it is a common symbol. */
1515 xcoff_section_data (abfd, csect)->last_symndx =
1517 - (bfd_byte *) obj_coff_external_syms (abfd))
1521 csect = bfd_make_section_anyway (abfd, ".bss");
1524 csect->vma = sym.n_value;
1525 csect->_raw_size = aux.x_csect.x_scnlen.l;
1526 csect->flags |= SEC_ALLOC;
1527 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1528 /* There are a number of other fields and section flags
1529 which we do not bother to set. */
1531 csect_index = ((esym
1532 - (bfd_byte *) obj_coff_external_syms (abfd))
1535 csect->used_by_bfd =
1536 ((struct coff_section_tdata *)
1537 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1538 if (csect->used_by_bfd == NULL)
1540 bfd_set_error (bfd_error_no_memory);
1543 coff_section_data (abfd, csect)->tdata =
1544 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1545 if (coff_section_data (abfd, csect)->tdata == NULL)
1547 bfd_set_error (bfd_error_no_memory);
1550 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1552 if (first_csect == NULL)
1553 first_csect = csect;
1555 if (sym.n_sclass == C_EXT)
1557 csect->flags |= SEC_IS_COMMON;
1558 csect->_raw_size = 0;
1560 value = aux.x_csect.x_scnlen.l;
1566 /* Now we have enough information to add the symbol to the
1567 linker hash table. */
1569 if (sym.n_sclass == C_EXT)
1573 BFD_ASSERT (section != NULL);
1575 /* We must copy the name into memory if we got it from the
1576 syment itself, rather than the string table. */
1577 copy = default_copy;
1578 if (sym._n._n_n._n_zeroes != 0
1579 || sym._n._n_n._n_offset == 0)
1582 if (info->hash->creator == abfd->xvec)
1584 /* If we are statically linking a shared object, it is
1585 OK for symbol redefinitions to occur. I can't figure
1586 out just what the XCOFF linker is doing, but
1587 something like this is required for -bnso to work. */
1588 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1589 name, true, copy, false);
1590 if (*sym_hash == NULL)
1592 if (((*sym_hash)->root.type == bfd_link_hash_defined
1593 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1594 && ! bfd_is_und_section (section)
1595 && ! bfd_is_com_section (section))
1597 if ((abfd->flags & DYNAMIC) != 0)
1599 section = bfd_und_section_ptr;
1602 else if (((*sym_hash)->root.u.def.section->owner->flags
1605 (*sym_hash)->root.type = bfd_link_hash_undefined;
1606 (*sym_hash)->root.u.undef.abfd =
1607 (*sym_hash)->root.u.def.section->owner;
1612 /* _bfd_generic_link_add_one_symbol may call the linker to
1613 generate an error message, and the linker may try to read
1614 the symbol table to give a good error. Right now, the
1615 line numbers are in an inconsistent state, since they are
1616 counted both in the real sections and in the new csects.
1617 We need to leave the count in the real sections so that
1618 the linker can report the line number of the error
1619 correctly, so temporarily clobber the link to the csects
1620 so that the linker will not try to read the line numbers
1621 a second time from the csects. */
1622 BFD_ASSERT (last_real->next == first_csect);
1623 last_real->next = NULL;
1624 if (! (_bfd_generic_link_add_one_symbol
1625 (info, abfd, name, flags, section, value,
1626 (const char *) NULL, copy, true,
1627 (struct bfd_link_hash_entry **) sym_hash)))
1629 last_real->next = first_csect;
1631 if (smtyp == XTY_CM)
1633 if ((*sym_hash)->root.type != bfd_link_hash_common
1634 || (*sym_hash)->root.u.c.p->section != csect)
1636 /* We don't need the common csect we just created. */
1637 csect->_raw_size = 0;
1641 (*sym_hash)->root.u.c.p->alignment_power
1642 = csect->alignment_power;
1646 if (info->hash->creator == abfd->xvec)
1650 if (smtyp == XTY_ER || smtyp == XTY_CM)
1651 flag = XCOFF_REF_REGULAR;
1653 flag = XCOFF_DEF_REGULAR;
1654 (*sym_hash)->flags |= flag;
1656 if ((*sym_hash)->smclas == XMC_UA
1657 || flag == XCOFF_DEF_REGULAR)
1658 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1662 *csect_cache = csect;
1664 esym += (sym.n_numaux + 1) * symesz;
1665 sym_hash += sym.n_numaux + 1;
1666 csect_cache += sym.n_numaux + 1;
1669 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1671 /* Make sure that we have seen all the relocs. */
1672 for (o = abfd->sections; o != first_csect; o = o->next)
1674 /* Reset the section size and the line numebr count, since the
1675 data is now attached to the csects. Don't reset the size of
1676 the .debug section, since we need to read it below in
1677 bfd_xcoff_size_dynamic_sections. */
1678 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1680 o->lineno_count = 0;
1682 if ((o->flags & SEC_RELOC) != 0)
1685 struct internal_reloc *rel;
1686 asection **rel_csect;
1688 rel = reloc_info[o->target_index].relocs;
1689 rel_csect = reloc_info[o->target_index].csects;
1690 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1692 if (*rel_csect == NULL)
1694 (*_bfd_error_handler)
1695 ("%s: reloc %s:%d not in csect",
1696 bfd_get_filename (abfd), o->name, i);
1697 bfd_set_error (bfd_error_bad_value);
1701 /* We identify all symbols which are called, so that we
1702 can create glue code for calls to functions imported
1703 from dynamic objects. */
1704 if (info->hash->creator == abfd->xvec
1705 && *rel_csect != bfd_und_section_ptr
1706 && (rel->r_type == R_BR
1707 || rel->r_type == R_RBR)
1708 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1710 struct xcoff_link_hash_entry *h;
1712 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1713 h->flags |= XCOFF_CALLED;
1714 /* If the symbol name starts with a period, it is
1715 the code of a function. If the symbol is
1716 currently undefined, then add an undefined symbol
1717 for the function descriptor. This should do no
1718 harm, because any regular object that defines the
1719 function should also define the function
1720 descriptor. It helps, because it means that we
1721 will identify the function descriptor with a
1722 dynamic object if a dynamic object defines it. */
1723 if (h->root.root.string[0] == '.'
1724 && h->descriptor == NULL)
1726 struct xcoff_link_hash_entry *hds;
1728 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1729 h->root.root.string + 1,
1733 if (hds->root.type == bfd_link_hash_new)
1735 if (! (_bfd_generic_link_add_one_symbol
1736 (info, abfd, hds->root.root.string,
1737 (flagword) 0, bfd_und_section_ptr,
1738 (bfd_vma) 0, (const char *) NULL, false,
1740 (struct bfd_link_hash_entry **) NULL)))
1743 h->descriptor = hds;
1748 free (reloc_info[o->target_index].csects);
1749 reloc_info[o->target_index].csects = NULL;
1751 /* Reset SEC_RELOC and the reloc_count, since the reloc
1752 information is now attached to the csects. */
1753 o->flags &=~ SEC_RELOC;
1756 /* If we are not keeping memory, free the reloc information. */
1757 if (! info->keep_memory
1758 && coff_section_data (abfd, o) != NULL
1759 && coff_section_data (abfd, o)->relocs != NULL
1760 && ! coff_section_data (abfd, o)->keep_relocs)
1762 free (coff_section_data (abfd, o)->relocs);
1763 coff_section_data (abfd, o)->relocs = NULL;
1767 /* Free up the line numbers. FIXME: We could cache these
1768 somewhere for the final link, to avoid reading them again. */
1769 if (reloc_info[o->target_index].linenos != NULL)
1771 free (reloc_info[o->target_index].linenos);
1772 reloc_info[o->target_index].linenos = NULL;
1778 obj_coff_keep_syms (abfd) = keep_syms;
1783 if (reloc_info != NULL)
1785 for (o = abfd->sections; o != NULL; o = o->next)
1787 if (reloc_info[o->target_index].csects != NULL)
1788 free (reloc_info[o->target_index].csects);
1789 if (reloc_info[o->target_index].linenos != NULL)
1790 free (reloc_info[o->target_index].linenos);
1794 obj_coff_keep_syms (abfd) = keep_syms;
1801 /* This function is used to add symbols from a dynamic object to the
1802 global symbol table. */
1805 xcoff_link_add_dynamic_symbols (abfd, info)
1807 struct bfd_link_info *info;
1809 bfd_size_type symesz;
1812 struct xcoff_import_file *n;
1817 struct xcoff_import_file **pp;
1819 /* We can only handle a dynamic object if we are generating an XCOFF
1821 if (info->hash->creator != abfd->xvec)
1823 (*_bfd_error_handler)
1824 ("%s: XCOFF shared object when not producing XCOFF output",
1825 bfd_get_filename (abfd));
1826 bfd_set_error (bfd_error_invalid_operation);
1830 /* Remove the sections from this object, so that they do not get
1831 included in the link. */
1832 abfd->sections = NULL;
1834 symesz = bfd_coff_symesz (abfd);
1835 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1836 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1837 while (esym < esym_end)
1839 struct internal_syment sym;
1841 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1843 /* I think that every symbol mentioned in a dynamic object must
1844 be defined by that object, perhaps by importing it from
1845 another dynamic object. All we have to do is look up each
1846 external symbol. If we have already put it in the hash
1847 table, we simply set a flag indicating that it appears in a
1850 if (sym.n_sclass == C_EXT)
1853 char buf[SYMNMLEN + 1];
1854 struct xcoff_link_hash_entry *h;
1856 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1860 /* Normally we could not xcoff_link_hash_lookup in an add
1861 symbols routine, since we might not be using an XCOFF
1862 hash table. However, we verified above that we are using
1863 an XCOFF hash table. */
1864 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
1865 false, false, true);
1868 h->flags |= XCOFF_REF_DYNAMIC;
1870 /* If the symbol is undefined, and the current BFD is
1871 not a dynamic object, change the BFD to this dynamic
1872 object, so that we can get the correct import file
1874 if ((h->root.type == bfd_link_hash_undefined
1875 || h->root.type == bfd_link_hash_undefweak)
1876 && (h->root.u.undef.abfd == NULL
1877 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
1878 h->root.u.undef.abfd = abfd;
1880 if (h->smclas == XMC_UA
1881 && sym.n_numaux > 0)
1883 union internal_auxent aux;
1885 bfd_coff_swap_aux_in (abfd,
1886 (PTR) (esym + symesz * sym.n_numaux),
1887 sym.n_type, sym.n_sclass,
1888 sym.n_numaux - 1, sym.n_numaux,
1890 h->smclas = aux.x_csect.x_smclas;
1895 esym += (sym.n_numaux + 1) * symesz;
1898 /* Record this file in the import files. */
1900 n = ((struct xcoff_import_file *)
1901 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
1904 bfd_set_error (bfd_error_no_memory);
1909 /* For some reason, the path entry in the import file list for a
1910 shared object appears to always be empty. The file name is the
1913 if (abfd->my_archive == NULL)
1915 bname = bfd_get_filename (abfd);
1920 bname = bfd_get_filename (abfd->my_archive);
1921 mname = bfd_get_filename (abfd);
1923 s = strrchr (bname, '/');
1929 /* We start c at 1 because the first import file number is reserved
1931 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1933 pp = &(*pp)->next, ++c)
1937 xcoff_data (abfd)->import_file_id = c;
1942 /* Routines that are called after all the input files have been
1943 handled, but before the sections are laid out in memory. */
1945 /* Mark a symbol as not being garbage, including the section in which
1948 static INLINE boolean
1949 xcoff_mark_symbol (info, h)
1950 struct bfd_link_info *info;
1951 struct xcoff_link_hash_entry *h;
1953 if ((h->flags & XCOFF_MARK) != 0)
1956 h->flags |= XCOFF_MARK;
1957 if (h->root.type == bfd_link_hash_defined
1958 || h->root.type == bfd_link_hash_defweak)
1962 hsec = h->root.u.def.section;
1963 if ((hsec->flags & SEC_MARK) == 0)
1965 if (! xcoff_mark (info, hsec))
1970 if (h->toc_section != NULL
1971 && (h->toc_section->flags & SEC_MARK) == 0)
1973 if (! xcoff_mark (info, h->toc_section))
1980 /* The mark phase of garbage collection. For a given section, mark
1981 it, and all the sections which define symbols to which it refers.
1982 Because this function needs to look at the relocs, we also count
1983 the number of relocs which need to be copied into the .loader
1987 xcoff_mark (info, sec)
1988 struct bfd_link_info *info;
1991 if ((sec->flags & SEC_MARK) != 0)
1994 sec->flags |= SEC_MARK;
1996 if (sec->owner->xvec == info->hash->creator
1997 && coff_section_data (sec->owner, sec) != NULL
1998 && xcoff_section_data (sec->owner, sec) != NULL)
2000 register struct xcoff_link_hash_entry **hp, **hpend;
2001 struct internal_reloc *rel, *relend;
2003 /* Mark all the symbols in this section. */
2005 hp = (obj_xcoff_sym_hashes (sec->owner)
2006 + xcoff_section_data (sec->owner, sec)->first_symndx);
2007 hpend = (obj_xcoff_sym_hashes (sec->owner)
2008 + xcoff_section_data (sec->owner, sec)->last_symndx);
2009 for (; hp < hpend; hp++)
2011 register struct xcoff_link_hash_entry *h;
2015 && (h->flags & XCOFF_MARK) == 0)
2017 if (! xcoff_mark_symbol (info, h))
2022 /* Look through the section relocs. */
2024 if ((sec->flags & SEC_RELOC) != 0
2025 && sec->reloc_count > 0)
2027 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2028 (bfd_byte *) NULL, false,
2029 (struct internal_reloc *) NULL);
2032 relend = rel + sec->reloc_count;
2033 for (; rel < relend; rel++)
2036 struct xcoff_link_hash_entry *h;
2038 if ((unsigned int) rel->r_symndx
2039 > obj_raw_syment_count (sec->owner))
2042 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2044 && (h->flags & XCOFF_MARK) == 0)
2046 if (! xcoff_mark_symbol (info, h))
2050 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2052 && (rsec->flags & SEC_MARK) == 0)
2054 if (! xcoff_mark (info, rsec))
2058 /* See if this reloc needs to be copied into the .loader
2060 switch (rel->r_type)
2064 || h->root.type == bfd_link_hash_defined
2065 || h->root.type == bfd_link_hash_defweak
2066 || h->root.type == bfd_link_hash_common
2067 || ((h->flags & XCOFF_CALLED) != 0
2068 && (h->flags & XCOFF_DEF_REGULAR) == 0
2069 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2070 && (h->root.type == bfd_link_hash_undefined
2071 || h->root.type == bfd_link_hash_undefweak)
2072 && h->root.root.string[0] == '.'))
2079 ++xcoff_hash_table (info)->ldrel_count;
2081 h->flags |= XCOFF_LDREL;
2088 /* We should never need a .loader reloc for a TOC
2094 if (! info->keep_memory
2095 && coff_section_data (sec->owner, sec) != NULL
2096 && coff_section_data (sec->owner, sec)->relocs != NULL
2097 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2099 free (coff_section_data (sec->owner, sec)->relocs);
2100 coff_section_data (sec->owner, sec)->relocs = NULL;
2108 /* The sweep phase of garbage collection. Remove all garbage
2113 struct bfd_link_info *info;
2117 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2121 for (o = sub->sections; o != NULL; o = o->next)
2123 if ((o->flags & SEC_MARK) == 0)
2125 /* Keep all sections from non-XCOFF input files. Keep
2126 special sections. Keep .debug sections for the
2128 if (sub->xvec != info->hash->creator
2129 || o == xcoff_hash_table (info)->debug_section
2130 || o == xcoff_hash_table (info)->loader_section
2131 || o == xcoff_hash_table (info)->linkage_section
2132 || o == xcoff_hash_table (info)->toc_section
2133 || strcmp (o->name, ".debug") == 0)
2134 o->flags |= SEC_MARK;
2139 o->lineno_count = 0;
2146 /* Record the number of elements in a set. This is used to output the
2147 correct csect length. */
2150 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2152 struct bfd_link_info *info;
2153 struct bfd_link_hash_entry *harg;
2156 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2157 struct xcoff_link_size_list *n;
2159 if (! XCOFF_XVECP (output_bfd->xvec))
2162 /* This will hardly ever be called. I don't want to burn four bytes
2163 per global symbol, so instead the size is kept on a linked list
2164 attached to the hash table. */
2166 n = ((struct xcoff_link_size_list *)
2167 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2170 bfd_set_error (bfd_error_no_memory);
2173 n->next = xcoff_hash_table (info)->size_list;
2176 xcoff_hash_table (info)->size_list = n;
2178 h->flags |= XCOFF_HAS_SIZE;
2183 /* Import a symbol. */
2186 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2189 struct bfd_link_info *info;
2190 struct bfd_link_hash_entry *harg;
2192 const char *imppath;
2193 const char *impfile;
2194 const char *impmember;
2196 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2198 if (! XCOFF_XVECP (output_bfd->xvec))
2201 h->flags |= XCOFF_IMPORT;
2203 if (val != (bfd_vma) -1)
2205 if (h->root.type == bfd_link_hash_defined)
2207 if (! ((*info->callbacks->multiple_definition)
2208 (info, h->root.root.string, h->root.u.def.section->owner,
2209 h->root.u.def.section, h->root.u.def.value,
2210 output_bfd, bfd_abs_section_ptr, val)))
2214 h->root.type = bfd_link_hash_defined;
2215 h->root.u.def.section = bfd_abs_section_ptr;
2216 h->root.u.def.value = val;
2219 if (h->ldsym == NULL)
2221 h->ldsym = ((struct internal_ldsym *)
2222 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2223 if (h->ldsym == NULL)
2225 bfd_set_error (bfd_error_no_memory);
2230 if (imppath == NULL)
2231 h->ldsym->l_ifile = (bfd_size_type) -1;
2235 struct xcoff_import_file **pp;
2237 /* We start c at 1 because the first entry in the import list is
2238 reserved for the library search path. */
2239 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2241 pp = &(*pp)->next, ++c)
2243 if (strcmp ((*pp)->path, imppath) == 0
2244 && strcmp ((*pp)->file, impfile) == 0
2245 && strcmp ((*pp)->member, impmember) == 0)
2251 struct xcoff_import_file *n;
2253 n = ((struct xcoff_import_file *)
2254 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2257 bfd_set_error (bfd_error_no_memory);
2263 n->member = impmember;
2267 h->ldsym->l_ifile = c;
2273 /* Export a symbol. */
2276 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2278 struct bfd_link_info *info;
2279 struct bfd_link_hash_entry *harg;
2282 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2284 if (! XCOFF_XVECP (output_bfd->xvec))
2287 h->flags |= XCOFF_EXPORT;
2289 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2290 I'm just going to ignore it until somebody explains it. */
2292 /* Make sure we don't garbage collect this symbol. */
2293 if (! xcoff_mark_symbol (info, h))
2299 /* Count a reloc against a symbol. This is called for relocs
2300 generated by the linker script, typically for global constructors
2304 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2306 struct bfd_link_info *info;
2309 struct xcoff_link_hash_entry *h;
2311 if (! XCOFF_XVECP (output_bfd->xvec))
2314 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, false, false,
2318 (*_bfd_error_handler) ("%s: no such symbol", name);
2319 bfd_set_error (bfd_error_no_symbols);
2323 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2324 ++xcoff_hash_table (info)->ldrel_count;
2326 /* Mark the symbol to avoid garbage collection. */
2327 if (! xcoff_mark_symbol (info, h))
2333 /* This function is called for each symbol to which the linker script
2337 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2339 struct bfd_link_info *info;
2342 struct xcoff_link_hash_entry *h;
2344 if (! XCOFF_XVECP (output_bfd->xvec))
2347 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2352 h->flags |= XCOFF_DEF_REGULAR;
2357 /* This structure is used to pass information through
2358 xcoff_link_hash_traverse. */
2360 struct xcoff_loader_info
2362 /* Set if a problem occurred. */
2366 /* Link information structure. */
2367 struct bfd_link_info *info;
2368 /* Number of ldsym structures. */
2370 /* Size of string table. */
2374 /* Allocated size of string table. */
2378 /* Build the .loader section. This is called by the XCOFF linker
2379 emulation before_allocation routine. We must set the size of the
2380 .loader section before the linker lays out the output file.
2381 LIBPATH is the library path to search for shared objects; this is
2382 normally built from the -L arguments passed to the linker. ENTRY
2383 is the name of the entry point symbol. */
2386 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2387 file_align, maxstack, maxdata, gc,
2390 struct bfd_link_info *info;
2391 const char *libpath;
2393 unsigned long file_align;
2394 unsigned long maxstack;
2395 unsigned long maxdata;
2400 struct xcoff_link_hash_entry *hentry;
2402 struct xcoff_loader_info ldinfo;
2403 size_t impsize, impcount;
2404 struct xcoff_import_file *fl;
2405 struct internal_ldhdr *ldhdr;
2406 bfd_size_type stoff;
2410 struct bfd_strtab_hash *debug_strtab;
2411 bfd_byte *debug_contents = NULL;
2413 if (! XCOFF_XVECP (output_bfd->xvec))
2416 ldinfo.failed = false;
2417 ldinfo.output_bfd = output_bfd;
2419 ldinfo.ldsym_count = 0;
2420 ldinfo.string_size = 0;
2421 ldinfo.strings = NULL;
2422 ldinfo.string_alc = 0;
2424 xcoff_data (output_bfd)->maxstack = maxstack;
2425 xcoff_data (output_bfd)->maxdata = maxdata;
2426 xcoff_data (output_bfd)->modtype = modtype;
2428 xcoff_hash_table (info)->file_align = file_align;
2429 xcoff_hash_table (info)->textro = textro;
2431 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2432 false, false, true);
2435 hentry->flags |= XCOFF_ENTRY;
2436 if (hentry->root.type == bfd_link_hash_defined
2437 || hentry->root.type == bfd_link_hash_defweak)
2438 xcoff_data (output_bfd)->entry_section =
2439 hentry->root.u.def.section->output_section;
2442 /* Garbage collect unused sections. */
2443 if (info->relocateable
2446 || (hentry->root.type != bfd_link_hash_defined
2447 && hentry->root.type != bfd_link_hash_defweak))
2450 xcoff_hash_table (info)->gc = false;
2452 /* We still need to call xcoff_mark, in order to set ldrel_count
2454 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2458 for (o = sub->sections; o != NULL; o = o->next)
2460 if ((o->flags & SEC_MARK) == 0)
2462 if (! xcoff_mark (info, o))
2470 if (! xcoff_mark (info, hentry->root.u.def.section))
2473 xcoff_hash_table (info)->gc = true;
2476 if (info->input_bfds == NULL)
2478 /* I'm not sure what to do in this bizarre case. */
2482 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2487 /* Work out the size of the import file names. Each import file ID
2488 consists of three null terminated strings: the path, the file
2489 name, and the archive member name. The first entry in the list
2490 of names is the path to use to find objects, which the linker has
2491 passed in as the libpath argument. For some reason, the path
2492 entry in the other import file names appears to always be empty. */
2493 impsize = strlen (libpath) + 3;
2495 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2498 impsize += (strlen (fl->path)
2500 + strlen (fl->member)
2504 /* Set up the .loader section header. */
2505 ldhdr = &xcoff_hash_table (info)->ldhdr;
2506 ldhdr->l_version = 1;
2507 ldhdr->l_nsyms = ldinfo.ldsym_count;
2508 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2509 ldhdr->l_istlen = impsize;
2510 ldhdr->l_nimpid = impcount;
2511 ldhdr->l_impoff = (LDHDRSZ
2512 + ldhdr->l_nsyms * LDSYMSZ
2513 + ldhdr->l_nreloc * LDRELSZ);
2514 ldhdr->l_stlen = ldinfo.string_size;
2515 stoff = ldhdr->l_impoff + impsize;
2516 if (ldinfo.string_size == 0)
2519 ldhdr->l_stoff = stoff;
2521 /* We now know the final size of the .loader section. Allocate
2523 lsec = xcoff_hash_table (info)->loader_section;
2524 lsec->_raw_size = stoff + ldhdr->l_stlen;
2525 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2526 if (lsec->contents == NULL)
2528 bfd_set_error (bfd_error_no_memory);
2532 /* Set up the header. */
2533 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2534 (struct external_ldhdr *) lsec->contents);
2536 /* Set up the import file names. */
2537 out = (char *) lsec->contents + ldhdr->l_impoff;
2538 strcpy (out, libpath);
2539 out += strlen (libpath) + 1;
2542 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2544 register const char *s;
2547 while ((*out++ = *s++) != '\0')
2550 while ((*out++ = *s++) != '\0')
2553 while ((*out++ = *s++) != '\0')
2557 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
2559 /* Set up the symbol string table. */
2560 if (ldinfo.string_size > 0)
2562 memcpy (out, ldinfo.strings, ldinfo.string_size);
2563 free (ldinfo.strings);
2564 ldinfo.strings = NULL;
2567 /* We can't set up the symbol table or the relocs yet, because we
2568 don't yet know the final position of the various sections. The
2569 .loader symbols are written out when the corresponding normal
2570 symbols are written out in xcoff_link_input_bfd or
2571 xcoff_write_global_symbol. The .loader relocs are written out
2572 when the corresponding normal relocs are handled in
2573 xcoff_link_input_bfd. */
2575 /* Allocate space for the global linkage section and the global toc
2577 sec = xcoff_hash_table (info)->linkage_section;
2578 if (sec->_raw_size > 0)
2580 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2581 if (sec->contents == NULL)
2583 bfd_set_error (bfd_error_no_memory);
2587 sec = xcoff_hash_table (info)->toc_section;
2588 if (sec->_raw_size > 0)
2590 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2591 if (sec->contents == NULL)
2593 bfd_set_error (bfd_error_no_memory);
2598 /* Now that we've done garbage collection, figure out the contents
2599 of the .debug section. */
2600 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2602 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2605 bfd_size_type symcount;
2606 unsigned long *debug_index;
2608 bfd_byte *esym, *esymend;
2609 bfd_size_type symesz;
2611 if (sub->xvec != info->hash->creator)
2613 subdeb = bfd_get_section_by_name (sub, ".debug");
2614 if (subdeb == NULL || subdeb->_raw_size == 0)
2617 if (info->strip == strip_all
2618 || info->strip == strip_debugger
2619 || info->discard == discard_all)
2621 subdeb->_raw_size = 0;
2625 if (! _bfd_coff_get_external_symbols (sub))
2628 symcount = obj_raw_syment_count (sub);
2629 debug_index = ((unsigned long *)
2630 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2631 if (debug_index == NULL)
2633 bfd_set_error (bfd_error_no_memory);
2636 xcoff_data (sub)->debug_indices = debug_index;
2638 /* Grab the contents of the .debug section. We use malloc and
2639 copy the neams into the debug stringtab, rather than
2640 bfd_alloc, because I expect that, when linking many files
2641 together, many of the strings will be the same. Storing the
2642 strings in the hash table should save space in this case. */
2643 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2644 if (debug_contents == NULL)
2646 bfd_set_error (bfd_error_no_memory);
2649 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2650 (file_ptr) 0, subdeb->_raw_size))
2653 csectpp = xcoff_data (sub)->csects;
2655 symesz = bfd_coff_symesz (sub);
2656 esym = (bfd_byte *) obj_coff_external_syms (sub);
2657 esymend = esym + symcount * symesz;
2658 while (esym < esymend)
2660 struct internal_syment sym;
2662 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2664 *debug_index = (unsigned long) -1;
2666 if (sym._n._n_n._n_zeroes == 0
2669 || ((*csectpp)->flags & SEC_MARK) != 0
2670 || *csectpp == bfd_abs_section_ptr)
2671 && bfd_coff_symname_in_debug (sub, &sym))
2676 name = (char *) debug_contents + sym._n._n_n._n_offset;
2677 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2678 if (indx == (bfd_size_type) -1)
2680 *debug_index = indx;
2683 esym += (sym.n_numaux + 1) * symesz;
2684 csectpp += sym.n_numaux + 1;
2685 debug_index += sym.n_numaux + 1;
2688 free (debug_contents);
2689 debug_contents = NULL;
2691 /* Clear the size of subdeb, so that it is not included directly
2692 in the output file. */
2693 subdeb->_raw_size = 0;
2695 if (! info->keep_memory)
2697 if (! _bfd_coff_free_symbols (sub))
2702 xcoff_hash_table (info)->debug_section->_raw_size =
2703 _bfd_stringtab_size (debug_strtab);
2708 if (ldinfo.strings != NULL)
2709 free (ldinfo.strings);
2710 if (debug_contents != NULL)
2711 free (debug_contents);
2715 /* Add a symbol to the .loader symbols, if necessary. */
2718 xcoff_build_ldsyms (h, p)
2719 struct xcoff_link_hash_entry *h;
2722 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2725 /* We don't want to garbage collect symbols which are not defined in
2726 XCOFF files. This is a convenient place to mark them. */
2727 if (xcoff_hash_table (ldinfo->info)->gc
2728 && (h->flags & XCOFF_MARK) == 0
2729 && (h->root.type == bfd_link_hash_defined
2730 || h->root.type == bfd_link_hash_defweak)
2731 && (h->root.u.def.section->owner == NULL
2732 || (h->root.u.def.section->owner->xvec
2733 != ldinfo->info->hash->creator)))
2734 h->flags |= XCOFF_MARK;
2736 /* If this symbol is called, and it is defined in a dynamic object,
2737 then we need to set up global linkage code for it. (Unless we
2738 did garbage collection and we didn't need this symbol.) */
2739 if ((h->flags & XCOFF_CALLED) != 0
2740 && (h->flags & XCOFF_DEF_REGULAR) == 0
2741 && (h->flags & XCOFF_REF_DYNAMIC) != 0
2742 && (h->root.type == bfd_link_hash_undefined
2743 || h->root.type == bfd_link_hash_undefweak)
2744 && h->root.root.string[0] == '.'
2745 && (! xcoff_hash_table (ldinfo->info)->gc
2746 || (h->flags & XCOFF_MARK) != 0))
2749 struct xcoff_link_hash_entry *hds;
2751 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2752 h->root.type = bfd_link_hash_defined;
2753 h->root.u.def.section = sec;
2754 h->root.u.def.value = sec->_raw_size;
2756 sec->_raw_size += XCOFF_GLINK_SIZE;
2758 /* The global linkage code requires a TOC entry for the
2760 hds = h->descriptor;
2761 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2762 || hds->root.type == bfd_link_hash_undefweak)
2763 && (hds->flags & XCOFF_DEF_REGULAR) == 0
2764 && (hds->flags & XCOFF_REF_DYNAMIC) != 0);
2765 hds->flags |= XCOFF_MARK;
2766 if (hds->toc_section == NULL)
2768 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2769 hds->u.toc_offset = hds->toc_section->_raw_size;
2770 hds->toc_section->_raw_size += 4;
2771 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2772 ++hds->toc_section->reloc_count;
2774 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2776 /* We need to call xcoff_build_ldsyms recursively here,
2777 because we may already have passed hds on the traversal. */
2778 xcoff_build_ldsyms (hds, p);
2782 /* If this is still a common symbol, and it wasn't garbage
2783 collected, we need to actually allocate space for it in the .bss
2785 if (h->root.type == bfd_link_hash_common
2786 && (! xcoff_hash_table (ldinfo->info)->gc
2787 || (h->flags & XCOFF_MARK) != 0)
2788 && h->root.u.c.p->section->_raw_size == 0)
2790 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2791 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
2794 /* We need to add a symbol to the .loader section if it is mentioned
2795 in a reloc which we are copying to the .loader section and it was
2796 not defined or common, or if it is the entry point, or if it is
2799 if (((h->flags & XCOFF_LDREL) == 0
2800 || h->root.type == bfd_link_hash_defined
2801 || h->root.type == bfd_link_hash_defweak
2802 || h->root.type == bfd_link_hash_common)
2803 && (h->flags & XCOFF_ENTRY) == 0
2804 && (h->flags & XCOFF_EXPORT) == 0)
2810 /* We don't need to add this symbol if we did garbage collection and
2811 we did not mark this symbol. */
2812 if (xcoff_hash_table (ldinfo->info)->gc
2813 && (h->flags & XCOFF_MARK) == 0)
2819 /* We may have already processed this symbol due to the recursive
2821 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2824 /* We need to add this symbol to the .loader symbols. */
2826 /* h->ldsym will already have been allocated for an explicitly
2828 if (h->ldsym == NULL)
2830 h->ldsym = ((struct internal_ldsym *)
2831 bfd_zalloc (ldinfo->output_bfd,
2832 sizeof (struct internal_ldsym)));
2833 if (h->ldsym == NULL)
2835 ldinfo->failed = true;
2836 bfd_set_error (bfd_error_no_memory);
2841 /* The first 3 symbol table indices are reserved to indicate the
2843 h->ldindx = ldinfo->ldsym_count + 3;
2845 ++ldinfo->ldsym_count;
2847 len = strlen (h->root.root.string);
2848 if (len <= SYMNMLEN)
2849 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
2852 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
2855 bfd_byte *newstrings;
2857 newalc = ldinfo->string_alc * 2;
2860 while (ldinfo->string_size + len + 3 > newalc)
2863 if (ldinfo->strings == NULL)
2864 newstrings = (bfd_byte *) malloc (newalc);
2866 newstrings = ((bfd_byte *)
2867 realloc ((PTR) ldinfo->strings, newalc));
2868 if (newstrings == NULL)
2870 ldinfo->failed = true;
2871 bfd_set_error (bfd_error_no_memory);
2874 ldinfo->string_alc = newalc;
2875 ldinfo->strings = newstrings;
2878 bfd_put_16 (ldinfo->output_bfd, len + 1,
2879 ldinfo->strings + ldinfo->string_size);
2880 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
2881 h->ldsym->_l._l_l._l_zeroes = 0;
2882 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
2883 ldinfo->string_size += len + 3;
2886 h->flags |= XCOFF_BUILT_LDSYM;
2891 /* Do the final link step. */
2894 _bfd_xcoff_bfd_final_link (abfd, info)
2896 struct bfd_link_info *info;
2898 bfd_size_type symesz;
2899 struct xcoff_final_link_info finfo;
2901 struct bfd_link_order *p;
2902 size_t max_contents_size;
2903 size_t max_sym_count;
2904 size_t max_lineno_count;
2905 size_t max_reloc_count;
2906 size_t max_output_reloc_count;
2907 file_ptr rel_filepos;
2909 file_ptr line_filepos;
2910 unsigned int linesz;
2912 bfd_byte *external_relocs = NULL;
2913 char strbuf[STRING_SIZE_SIZE];
2916 abfd->flags |= DYNAMIC;
2918 symesz = bfd_coff_symesz (abfd);
2921 finfo.output_bfd = abfd;
2922 finfo.strtab = NULL;
2923 finfo.section_info = NULL;
2924 finfo.last_file_index = -1;
2925 finfo.toc_symindx = -1;
2926 finfo.internal_syms = NULL;
2927 finfo.sym_indices = NULL;
2928 finfo.outsyms = NULL;
2929 finfo.linenos = NULL;
2930 finfo.contents = NULL;
2931 finfo.external_relocs = NULL;
2933 finfo.ldsym = ((struct external_ldsym *)
2934 (xcoff_hash_table (info)->loader_section->contents
2936 finfo.ldrel = ((struct external_ldrel *)
2937 (xcoff_hash_table (info)->loader_section->contents
2939 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
2941 xcoff_data (abfd)->coff.link_info = info;
2943 finfo.strtab = _bfd_stringtab_init ();
2944 if (finfo.strtab == NULL)
2947 /* Compute the file positions for all the sections. */
2948 if (abfd->output_has_begun)
2950 if (xcoff_hash_table (info)->file_align != 0)
2957 file_align = xcoff_hash_table (info)->file_align;
2958 if (file_align != 0)
2960 boolean saw_contents;
2965 /* Insert .pad sections before every section which has
2966 contents and is loaded, if it is preceded by some other
2967 section which has contents and is loaded. */
2968 saw_contents = true;
2969 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
2971 (*op)->target_index = indx;
2972 if (strcmp ((*op)->name, ".pad") == 0)
2973 saw_contents = false;
2974 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
2975 && ((*op)->flags & SEC_LOAD) != 0)
2978 saw_contents = true;
2985 n = bfd_make_section_anyway (abfd, ".pad");
2986 BFD_ASSERT (*op == n);
2988 n->flags = SEC_HAS_CONTENTS;
2989 n->alignment_power = 0;
2990 saw_contents = false;
2995 /* Reset the section indices after inserting the new
2998 for (o = abfd->sections; o != NULL; o = o->next)
3001 o->target_index = indx;
3003 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3005 /* Work out appropriate sizes for the .pad sections to force
3006 each section to land on a page boundary. This bit of
3007 code knows what compute_section_file_positions is going
3009 sofar = bfd_coff_filhsz (abfd);
3010 sofar += bfd_coff_aoutsz (abfd);
3011 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3013 for (o = abfd->sections; o != NULL; o = o->next)
3015 if (strcmp (o->name, ".pad") == 0)
3019 BFD_ASSERT (o->_raw_size == 0);
3020 pageoff = sofar & (file_align - 1);
3023 o->_raw_size = file_align - pageoff;
3024 sofar += file_align - pageoff;
3025 o->flags |= SEC_HAS_CONTENTS;
3030 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3031 sofar += BFD_ALIGN (o->_raw_size,
3032 1 << o->alignment_power);
3037 bfd_coff_compute_section_file_positions (abfd);
3040 /* Count the line numbers and relocation entries required for the
3041 output file. Set the file positions for the relocs. */
3042 rel_filepos = obj_relocbase (abfd);
3043 relsz = bfd_coff_relsz (abfd);
3044 max_contents_size = 0;
3045 max_lineno_count = 0;
3046 max_reloc_count = 0;
3047 for (o = abfd->sections; o != NULL; o = o->next)
3050 o->lineno_count = 0;
3051 for (p = o->link_order_head; p != NULL; p = p->next)
3053 if (p->type == bfd_indirect_link_order)
3057 sec = p->u.indirect.section;
3059 if (info->strip == strip_none
3060 || info->strip == strip_some)
3061 o->lineno_count += sec->lineno_count;
3063 o->reloc_count += sec->reloc_count;
3065 if (sec->_raw_size > max_contents_size)
3066 max_contents_size = sec->_raw_size;
3067 if (sec->lineno_count > max_lineno_count)
3068 max_lineno_count = sec->lineno_count;
3069 if (coff_section_data (sec->owner, sec) != NULL
3070 && xcoff_section_data (sec->owner, sec) != NULL
3071 && (xcoff_section_data (sec->owner, sec)->lineno_count
3072 > max_lineno_count))
3074 xcoff_section_data (sec->owner, sec)->lineno_count;
3075 if (sec->reloc_count > max_reloc_count)
3076 max_reloc_count = sec->reloc_count;
3078 else if (p->type == bfd_section_reloc_link_order
3079 || p->type == bfd_symbol_reloc_link_order)
3082 if (o->reloc_count == 0)
3086 o->flags |= SEC_RELOC;
3087 o->rel_filepos = rel_filepos;
3088 rel_filepos += o->reloc_count * relsz;
3092 /* Allocate space for the pointers we need to keep for the relocs. */
3096 /* We use section_count + 1, rather than section_count, because
3097 the target_index fields are 1 based. */
3098 finfo.section_info = ((struct xcoff_link_section_info *)
3099 malloc ((abfd->section_count + 1)
3100 * sizeof (struct xcoff_link_section_info)));
3101 if (finfo.section_info == NULL)
3103 bfd_set_error (bfd_error_no_memory);
3106 for (i = 0; i <= abfd->section_count; i++)
3108 finfo.section_info[i].relocs = NULL;
3109 finfo.section_info[i].rel_hashes = NULL;
3110 finfo.section_info[i].toc_rel_hashes = NULL;
3114 /* We now know the size of the relocs, so we can determine the file
3115 positions of the line numbers. */
3116 line_filepos = rel_filepos;
3117 finfo.line_filepos = line_filepos;
3118 linesz = bfd_coff_linesz (abfd);
3119 max_output_reloc_count = 0;
3120 for (o = abfd->sections; o != NULL; o = o->next)
3122 if (o->lineno_count == 0)
3123 o->line_filepos = 0;
3126 o->line_filepos = line_filepos;
3127 line_filepos += o->lineno_count * linesz;
3130 if (o->reloc_count != 0)
3132 /* We don't know the indices of global symbols until we have
3133 written out all the local symbols. For each section in
3134 the output file, we keep an array of pointers to hash
3135 table entries. Each entry in the array corresponds to a
3136 reloc. When we find a reloc against a global symbol, we
3137 set the corresponding entry in this array so that we can
3138 fix up the symbol index after we have written out all the
3141 Because of this problem, we also keep the relocs in
3142 memory until the end of the link. This wastes memory.
3143 We could backpatch the file later, I suppose, although it
3145 finfo.section_info[o->target_index].relocs =
3146 ((struct internal_reloc *)
3147 malloc (o->reloc_count * sizeof (struct internal_reloc)));
3148 finfo.section_info[o->target_index].rel_hashes =
3149 ((struct xcoff_link_hash_entry **)
3150 malloc (o->reloc_count
3151 * sizeof (struct xcoff_link_hash_entry *)));
3152 if (finfo.section_info[o->target_index].relocs == NULL
3153 || finfo.section_info[o->target_index].rel_hashes == NULL)
3155 bfd_set_error (bfd_error_no_memory);
3159 if (o->reloc_count > max_output_reloc_count)
3160 max_output_reloc_count = o->reloc_count;
3163 /* Reset the reloc and lineno counts, so that we can use them to
3164 count the number of entries we have output so far. */
3166 o->lineno_count = 0;
3169 obj_sym_filepos (abfd) = line_filepos;
3171 /* Figure out the largest number of symbols in an input BFD. Take
3172 the opportunity to clear the output_has_begun fields of all the
3173 input BFD's. We want at least 4 symbols, since that is the
3174 number which xcoff_write_global_symbol may need. */
3176 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3180 sub->output_has_begun = false;
3181 sz = obj_raw_syment_count (sub);
3182 if (sz > max_sym_count)
3186 /* Allocate some buffers used while linking. */
3187 finfo.internal_syms = ((struct internal_syment *)
3188 malloc (max_sym_count
3189 * sizeof (struct internal_syment)));
3190 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
3191 finfo.outsyms = ((bfd_byte *)
3192 malloc ((size_t) ((max_sym_count + 1) * symesz)));
3193 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
3194 * bfd_coff_linesz (abfd));
3195 finfo.contents = (bfd_byte *) malloc (max_contents_size);
3196 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
3197 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3198 || (finfo.sym_indices == NULL && max_sym_count > 0)
3199 || finfo.outsyms == NULL
3200 || (finfo.linenos == NULL && max_lineno_count > 0)
3201 || (finfo.contents == NULL && max_contents_size > 0)
3202 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3204 bfd_set_error (bfd_error_no_memory);
3208 obj_raw_syment_count (abfd) = 0;
3209 xcoff_data (abfd)->toc = (bfd_vma) -1;
3211 /* We now know the position of everything in the file, except that
3212 we don't know the size of the symbol table and therefore we don't
3213 know where the string table starts. We just build the string
3214 table in memory as we go along. We process all the relocations
3215 for a single input file at once. */
3216 for (o = abfd->sections; o != NULL; o = o->next)
3218 for (p = o->link_order_head; p != NULL; p = p->next)
3220 if (p->type == bfd_indirect_link_order
3221 && p->u.indirect.section->owner->xvec == abfd->xvec)
3223 sub = p->u.indirect.section->owner;
3224 if (! sub->output_has_begun)
3226 if (! xcoff_link_input_bfd (&finfo, sub))
3228 sub->output_has_begun = true;
3231 else if (p->type == bfd_section_reloc_link_order
3232 || p->type == bfd_symbol_reloc_link_order)
3234 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3239 if (! _bfd_default_link_order (abfd, info, o, p))
3245 /* Free up the buffers used by xcoff_link_input_bfd. */
3247 if (finfo.internal_syms != NULL)
3249 free (finfo.internal_syms);
3250 finfo.internal_syms = NULL;
3252 if (finfo.sym_indices != NULL)
3254 free (finfo.sym_indices);
3255 finfo.sym_indices = NULL;
3257 if (finfo.linenos != NULL)
3259 free (finfo.linenos);
3260 finfo.linenos = NULL;
3262 if (finfo.contents != NULL)
3264 free (finfo.contents);
3265 finfo.contents = NULL;
3267 if (finfo.external_relocs != NULL)
3269 free (finfo.external_relocs);
3270 finfo.external_relocs = NULL;
3273 /* The value of the last C_FILE symbol is supposed to be -1. Write
3275 if (finfo.last_file_index != -1)
3277 finfo.last_file.n_value = -1;
3278 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3279 (PTR) finfo.outsyms);
3281 (obj_sym_filepos (abfd)
3282 + finfo.last_file_index * symesz),
3284 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3288 /* Write out all the global symbols which do not come from XCOFF
3290 xcoff_link_hash_traverse (xcoff_hash_table (info),
3291 xcoff_write_global_symbol,
3294 if (finfo.outsyms != NULL)
3296 free (finfo.outsyms);
3297 finfo.outsyms = NULL;
3300 /* Now that we have written out all the global symbols, we know the
3301 symbol indices to use for relocs against them, and we can finally
3302 write out the relocs. */
3303 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3304 if (external_relocs == NULL && max_output_reloc_count != 0)
3306 bfd_set_error (bfd_error_no_memory);
3310 for (o = abfd->sections; o != NULL; o = o->next)
3312 struct internal_reloc *irel;
3313 struct internal_reloc *irelend;
3314 struct xcoff_link_hash_entry **rel_hash;
3315 struct xcoff_toc_rel_hash *toc_rel_hash;
3318 if (o->reloc_count == 0)
3321 irel = finfo.section_info[o->target_index].relocs;
3322 irelend = irel + o->reloc_count;
3323 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3324 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3326 if (*rel_hash != NULL)
3328 if ((*rel_hash)->indx < 0)
3330 if (! ((*info->callbacks->unattached_reloc)
3331 (info, (*rel_hash)->root.root.string,
3332 (bfd *) NULL, o, irel->r_vaddr)))
3334 (*rel_hash)->indx = 0;
3336 irel->r_symndx = (*rel_hash)->indx;
3340 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
3341 toc_rel_hash != NULL;
3342 toc_rel_hash = toc_rel_hash->next)
3344 if (toc_rel_hash->h->u.toc_indx < 0)
3346 if (! ((*info->callbacks->unattached_reloc)
3347 (info, toc_rel_hash->h->root.root.string,
3348 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
3350 toc_rel_hash->h->u.toc_indx = 0;
3352 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
3355 /* XCOFF requires that the relocs be sorted by address. We tend
3356 to produce them in the order in which their containing csects
3357 appear in the symbol table, which is not necessarily by
3358 address. So we sort them here. There may be a better way to
3360 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3361 o->reloc_count, sizeof (struct internal_reloc),
3364 irel = finfo.section_info[o->target_index].relocs;
3365 irelend = irel + o->reloc_count;
3366 erel = external_relocs;
3367 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3368 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3370 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3371 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3372 abfd) != relsz * o->reloc_count)
3376 if (external_relocs != NULL)
3378 free (external_relocs);
3379 external_relocs = NULL;
3382 /* Free up the section information. */
3383 if (finfo.section_info != NULL)
3387 for (i = 0; i < abfd->section_count; i++)
3389 if (finfo.section_info[i].relocs != NULL)
3390 free (finfo.section_info[i].relocs);
3391 if (finfo.section_info[i].rel_hashes != NULL)
3392 free (finfo.section_info[i].rel_hashes);
3394 free (finfo.section_info);
3395 finfo.section_info = NULL;
3398 /* Write out the loader section contents. */
3399 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3400 == (xcoff_hash_table (info)->loader_section->contents
3401 + xcoff_hash_table (info)->ldhdr.l_impoff));
3402 o = xcoff_hash_table (info)->loader_section;
3403 if (! bfd_set_section_contents (abfd, o->output_section,
3404 o->contents, o->output_offset,
3408 /* Write out the global linkage section and the toc section. */
3409 o = xcoff_hash_table (info)->linkage_section;
3410 if (o->_raw_size > 0
3411 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3412 o->output_offset, o->_raw_size))
3414 o = xcoff_hash_table (info)->toc_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))
3420 /* Write out the string table. */
3422 (obj_sym_filepos (abfd)
3423 + obj_raw_syment_count (abfd) * symesz),
3427 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3428 (bfd_byte *) strbuf);
3429 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3431 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3434 _bfd_stringtab_free (finfo.strtab);
3436 /* Write out the debugging string table. */
3437 o = xcoff_hash_table (info)->debug_section;
3440 struct bfd_strtab_hash *debug_strtab;
3442 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3443 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3444 >= _bfd_stringtab_size (debug_strtab));
3446 o->output_section->filepos + o->output_offset,
3449 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3453 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3454 not try to write out the symbols. */
3455 bfd_get_symcount (abfd) = 0;
3460 if (finfo.strtab != NULL)
3461 _bfd_stringtab_free (finfo.strtab);
3462 if (finfo.section_info != NULL)
3466 for (i = 0; i < abfd->section_count; i++)
3468 if (finfo.section_info[i].relocs != NULL)
3469 free (finfo.section_info[i].relocs);
3470 if (finfo.section_info[i].rel_hashes != NULL)
3471 free (finfo.section_info[i].rel_hashes);
3473 free (finfo.section_info);
3475 if (finfo.internal_syms != NULL)
3476 free (finfo.internal_syms);
3477 if (finfo.sym_indices != NULL)
3478 free (finfo.sym_indices);
3479 if (finfo.outsyms != NULL)
3480 free (finfo.outsyms);
3481 if (finfo.linenos != NULL)
3482 free (finfo.linenos);
3483 if (finfo.contents != NULL)
3484 free (finfo.contents);
3485 if (finfo.external_relocs != NULL)
3486 free (finfo.external_relocs);
3487 if (external_relocs != NULL)
3488 free (external_relocs);
3492 /* Link an input file into the linker output file. This function
3493 handles all the sections and relocations of the input file at once. */
3496 xcoff_link_input_bfd (finfo, input_bfd)
3497 struct xcoff_final_link_info *finfo;
3501 const char *strings;
3502 bfd_size_type syment_base;
3503 unsigned int n_tmask;
3504 unsigned int n_btshft;
3506 bfd_size_type isymesz;
3507 bfd_size_type osymesz;
3508 bfd_size_type linesz;
3511 struct xcoff_link_hash_entry **sym_hash;
3512 struct internal_syment *isymp;
3514 unsigned long *debug_index;
3516 unsigned long output_index;
3523 /* We can just skip DYNAMIC files, unless this is a static link. */
3524 if ((input_bfd->flags & DYNAMIC) != 0
3525 && ! finfo->info->static_link)
3528 /* Move all the symbols to the output file. */
3530 output_bfd = finfo->output_bfd;
3532 syment_base = obj_raw_syment_count (output_bfd);
3533 isymesz = bfd_coff_symesz (input_bfd);
3534 osymesz = bfd_coff_symesz (output_bfd);
3535 linesz = bfd_coff_linesz (input_bfd);
3536 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3538 n_tmask = coff_data (input_bfd)->local_n_tmask;
3539 n_btshft = coff_data (input_bfd)->local_n_btshft;
3541 /* Define macros so that ISFCN, et. al., macros work correctly. */
3542 #define N_TMASK n_tmask
3543 #define N_BTSHFT n_btshft
3546 if (! finfo->info->keep_memory)
3549 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3552 if (! _bfd_coff_get_external_symbols (input_bfd))
3555 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3556 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3557 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3558 csectpp = xcoff_data (input_bfd)->csects;
3559 debug_index = xcoff_data (input_bfd)->debug_indices;
3560 isymp = finfo->internal_syms;
3561 indexp = finfo->sym_indices;
3562 output_index = syment_base;
3563 outsym = finfo->outsyms;
3567 while (esym < esym_end)
3569 struct internal_syment isym;
3570 union internal_auxent aux;
3576 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3578 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3580 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3582 BFD_ASSERT (isymp->n_numaux > 0);
3583 bfd_coff_swap_aux_in (input_bfd,
3584 (PTR) (esym + isymesz * isymp->n_numaux),
3585 isymp->n_type, isymp->n_sclass,
3586 isymp->n_numaux - 1, isymp->n_numaux,
3588 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3591 /* Make a copy of *isymp so that the relocate_section function
3592 always sees the original values. This is more reliable than
3593 always recomputing the symbol value even if we are stripping
3597 /* If this symbol is in the .loader section, swap out the
3598 .loader symbol information. If this is an external symbol
3599 reference to a defined symbol, though, then wait until we get
3600 to the definition. */
3601 if (isym.n_sclass == C_EXT
3602 && *sym_hash != NULL
3603 && (*sym_hash)->ldsym != NULL
3605 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3607 struct xcoff_link_hash_entry *h;
3608 struct internal_ldsym *ldsym;
3612 if (isym.n_scnum > 0)
3614 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3615 ldsym->l_value = (isym.n_value
3616 + (*csectpp)->output_section->vma
3617 + (*csectpp)->output_offset
3622 ldsym->l_scnum = isym.n_scnum;
3623 ldsym->l_value = isym.n_value;
3626 ldsym->l_smtype = smtyp;
3627 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3628 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3629 || (h->flags & XCOFF_IMPORT) != 0)
3630 ldsym->l_smtype |= L_IMPORT;
3631 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3632 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
3633 || (h->flags & XCOFF_EXPORT) != 0)
3634 ldsym->l_smtype |= L_EXPORT;
3635 if ((h->flags & XCOFF_ENTRY) != 0)
3636 ldsym->l_smtype |= L_ENTRY;
3638 ldsym->l_smclas = aux.x_csect.x_smclas;
3640 if (ldsym->l_ifile == (bfd_size_type) -1)
3642 else if (ldsym->l_ifile == 0)
3644 if ((ldsym->l_smtype & L_IMPORT) == 0)
3650 if (h->root.type == bfd_link_hash_defined
3651 || h->root.type == bfd_link_hash_defweak)
3652 impbfd = h->root.u.def.section->owner;
3653 else if (h->root.type == bfd_link_hash_undefined
3654 || h->root.type == bfd_link_hash_undefweak)
3655 impbfd = h->root.u.undef.abfd;
3663 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3664 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3671 BFD_ASSERT (h->ldindx >= 0);
3672 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3673 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3674 finfo->ldsym + h->ldindx - 3);
3682 add = 1 + isym.n_numaux;
3684 /* If we are skipping this csect, we want to skip this symbol. */
3685 if (*csectpp == NULL)
3688 /* If we garbage collected this csect, we want to skip this
3691 && xcoff_hash_table (finfo->info)->gc
3692 && ((*csectpp)->flags & SEC_MARK) == 0
3693 && *csectpp != bfd_abs_section_ptr)
3696 /* An XCOFF linker always skips C_STAT symbols. */
3698 && isymp->n_sclass == C_STAT)
3701 /* We skip all but the first TOC anchor. */
3703 && isymp->n_sclass == C_HIDEXT
3704 && aux.x_csect.x_smclas == XMC_TC0)
3706 if (finfo->toc_symindx != -1)
3710 finfo->toc_symindx = output_index;
3711 xcoff_data (finfo->output_bfd)->toc =
3712 ((*csectpp)->output_section->vma
3713 + (*csectpp)->output_offset
3716 xcoff_data (finfo->output_bfd)->toc_section =
3717 (*csectpp)->output_section;
3722 /* If we are stripping all symbols, we want to skip this one. */
3724 && finfo->info->strip == strip_all)
3727 /* We can skip resolved external references. */
3729 && isym.n_sclass == C_EXT
3731 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3734 /* We can skip common symbols if they got defined somewhere
3737 && isym.n_sclass == C_EXT
3739 && ((*sym_hash)->root.type != bfd_link_hash_common
3740 || (*sym_hash)->root.u.c.p->section != *csectpp)
3741 && ((*sym_hash)->root.type != bfd_link_hash_defined
3742 || (*sym_hash)->root.u.def.section != *csectpp))
3745 /* Skip local symbols if we are discarding them. */
3747 && finfo->info->discard == discard_all
3748 && isym.n_sclass != C_EXT
3749 && (isym.n_sclass != C_HIDEXT
3750 || smtyp != XTY_SD))
3753 /* If we stripping debugging symbols, and this is a debugging
3754 symbol, then skip it. */
3756 && finfo->info->strip == strip_debugger
3757 && isym.n_scnum == N_DEBUG)
3760 /* If some symbols are stripped based on the name, work out the
3761 name and decide whether to skip this symbol. We don't handle
3762 this correctly for symbols whose names are in the .debug
3763 section; to get it right we would need a new bfd_strtab_hash
3764 function to return the string given the index. */
3766 && (finfo->info->strip == strip_some
3767 || finfo->info->discard == discard_l)
3768 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3771 char buf[SYMNMLEN + 1];
3773 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3777 if ((finfo->info->strip == strip_some
3778 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
3780 || (finfo->info->discard == discard_l
3781 && (isym.n_sclass != C_EXT
3782 && (isym.n_sclass != C_HIDEXT
3783 || smtyp != XTY_SD))
3784 && strncmp (name, finfo->info->lprefix,
3785 finfo->info->lprefix_len) == 0))
3789 /* We can not skip the first TOC anchor. */
3792 && finfo->info->strip != strip_all)
3795 /* We now know whether we are to skip this symbol or not. */
3798 /* Adjust the symbol in order to output it. */
3800 if (isym._n._n_n._n_zeroes == 0
3801 && isym._n._n_n._n_offset != 0)
3803 /* This symbol has a long name. Enter it in the string
3804 table we are building. If *debug_index != -1, the
3805 name has already been entered in the .debug section. */
3806 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3807 isym._n._n_n._n_offset = *debug_index;
3813 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
3817 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3818 if (indx == (bfd_size_type) -1)
3820 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3824 if (isym.n_sclass != C_BSTAT
3825 && isym.n_sclass != C_ESTAT
3826 && isym.n_sclass != C_DECL
3827 && isym.n_scnum > 0)
3829 isym.n_scnum = (*csectpp)->output_section->target_index;
3830 isym.n_value += ((*csectpp)->output_section->vma
3831 + (*csectpp)->output_offset
3835 /* The value of a C_FILE symbol is the symbol index of the
3836 next C_FILE symbol. The value of the last C_FILE symbol
3837 is -1. We try to get this right, below, just before we
3838 write the symbols out, but in the general case we may
3839 have to write the symbol out twice. */
3840 if (isym.n_sclass == C_FILE)
3842 if (finfo->last_file_index != -1
3843 && finfo->last_file.n_value != (long) output_index)
3845 /* We must correct the value of the last C_FILE entry. */
3846 finfo->last_file.n_value = output_index;
3847 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3849 /* The last C_FILE symbol is in this input file. */
3850 bfd_coff_swap_sym_out (output_bfd,
3851 (PTR) &finfo->last_file,
3852 (PTR) (finfo->outsyms
3853 + ((finfo->last_file_index
3859 /* We have already written out the last C_FILE
3860 symbol. We need to write it out again. We
3861 borrow *outsym temporarily. */
3862 bfd_coff_swap_sym_out (output_bfd,
3863 (PTR) &finfo->last_file,
3865 if (bfd_seek (output_bfd,
3866 (obj_sym_filepos (output_bfd)
3867 + finfo->last_file_index * osymesz),
3869 || (bfd_write (outsym, osymesz, 1, output_bfd)
3875 finfo->last_file_index = output_index;
3876 finfo->last_file = isym;
3879 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3880 into the line numbers. We update the symbol values when
3881 we handle the line numbers. */
3882 if (isym.n_sclass == C_BINCL
3883 || isym.n_sclass == C_EINCL)
3885 isym.n_value = finfo->line_filepos;
3889 /* Output the symbol. */
3891 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
3893 *indexp = output_index;
3895 if (isym.n_sclass == C_EXT)
3898 struct xcoff_link_hash_entry *h;
3900 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3902 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3903 BFD_ASSERT (h != NULL);
3904 h->indx = output_index;
3907 /* If this is a symbol in the TOC which we may have merged
3908 (class XMC_TC), remember the symbol index of the TOC
3910 if (isym.n_sclass == C_HIDEXT
3911 && aux.x_csect.x_smclas == XMC_TC
3912 && *sym_hash != NULL)
3914 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3915 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3916 (*sym_hash)->u.toc_indx = output_index;
3919 output_index += add;
3920 outsym += add * osymesz;
3923 esym += add * isymesz;
3927 if (debug_index != NULL)
3930 for (--add; add > 0; --add)
3934 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3935 done in a separate pass, because we don't know the correct symbol
3936 indices until we have already decided which symbols we are going
3939 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3940 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3941 isymp = finfo->internal_syms;
3942 indexp = finfo->sym_indices;
3943 csectpp = xcoff_data (input_bfd)->csects;
3944 outsym = finfo->outsyms;
3945 while (esym < esym_end)
3949 add = 1 + isymp->n_numaux;
3952 esym += add * isymesz;
3957 if (isymp->n_sclass == C_BSTAT)
3959 struct internal_syment isym;
3962 /* The value of a C_BSTAT symbol is the symbol table
3963 index of the containing csect. */
3964 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
3965 indx = isym.n_value;
3966 if (indx < obj_raw_syment_count (input_bfd))
3970 symindx = finfo->sym_indices[indx];
3974 isym.n_value = symindx;
3975 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
3983 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3985 union internal_auxent aux;
3987 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
3988 isymp->n_sclass, i, isymp->n_numaux,
3991 if (isymp->n_sclass == C_FILE)
3993 /* This is the file name (or some comment put in by
3994 the compiler). If it is long, we must put it in
3995 the string table. */
3996 if (aux.x_file.x_n.x_zeroes == 0
3997 && aux.x_file.x_n.x_offset != 0)
3999 const char *filename;
4002 BFD_ASSERT (aux.x_file.x_n.x_offset
4003 >= STRING_SIZE_SIZE);
4004 if (strings == NULL)
4006 strings = _bfd_coff_read_string_table (input_bfd);
4007 if (strings == NULL)
4010 filename = strings + aux.x_file.x_n.x_offset;
4011 indx = _bfd_stringtab_add (finfo->strtab, filename,
4013 if (indx == (bfd_size_type) -1)
4015 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4018 else if ((isymp->n_sclass == C_EXT
4019 || isymp->n_sclass == C_HIDEXT)
4020 && i + 1 == isymp->n_numaux)
4022 /* We don't support type checking. I don't know if
4024 aux.x_csect.x_parmhash = 0;
4025 /* I don't think anybody uses these fields, but we'd
4026 better clobber them just in case. */
4027 aux.x_csect.x_stab = 0;
4028 aux.x_csect.x_snstab = 0;
4029 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4033 indx = aux.x_csect.x_scnlen.l;
4034 if (indx < obj_raw_syment_count (input_bfd))
4038 symindx = finfo->sym_indices[indx];
4040 aux.x_sym.x_tagndx.l = 0;
4042 aux.x_sym.x_tagndx.l = symindx;
4046 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4050 if (ISFCN (isymp->n_type)
4051 || ISTAG (isymp->n_sclass)
4052 || isymp->n_sclass == C_BLOCK)
4054 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4056 && indx < obj_raw_syment_count (input_bfd))
4058 /* We look forward through the symbol for
4059 the index of the next symbol we are going
4060 to include. I don't know if this is
4062 while (finfo->sym_indices[indx] < 0
4063 && indx < obj_raw_syment_count (input_bfd))
4065 if (indx >= obj_raw_syment_count (input_bfd))
4066 indx = output_index;
4068 indx = finfo->sym_indices[indx];
4069 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4073 indx = aux.x_sym.x_tagndx.l;
4074 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4078 symindx = finfo->sym_indices[indx];
4080 aux.x_sym.x_tagndx.l = 0;
4082 aux.x_sym.x_tagndx.l = symindx;
4086 /* Copy over the line numbers, unless we are stripping
4087 them. We do this on a symbol by symbol basis in
4088 order to more easily handle garbage collection. */
4089 if ((isymp->n_sclass == C_EXT
4090 || isymp->n_sclass == C_HIDEXT)
4092 && isymp->n_numaux > 1
4093 && ISFCN (isymp->n_type)
4094 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4096 if (finfo->info->strip != strip_none
4097 && finfo->info->strip != strip_some)
4098 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4101 asection *enclosing;
4102 unsigned int enc_count;
4103 bfd_size_type linoff;
4104 struct internal_lineno lin;
4107 enclosing = xcoff_section_data (abfd, o)->enclosing;
4108 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4109 if (oline != enclosing)
4111 if (bfd_seek (input_bfd,
4112 enclosing->line_filepos,
4114 || (bfd_read (finfo->linenos, linesz,
4115 enc_count, input_bfd)
4116 != linesz * enc_count))
4121 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4122 - enclosing->line_filepos);
4124 bfd_coff_swap_lineno_in (input_bfd,
4125 (PTR) (finfo->linenos + linoff),
4128 || ((bfd_size_type) lin.l_addr.l_symndx
4132 obj_coff_external_syms (input_bfd)))
4134 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4137 bfd_byte *linpend, *linp;
4139 bfd_size_type count;
4141 lin.l_addr.l_symndx = *indexp;
4142 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4143 (PTR) (finfo->linenos
4146 linpend = (finfo->linenos
4147 + enc_count * linesz);
4148 offset = (o->output_section->vma
4151 for (linp = finfo->linenos + linoff + linesz;
4155 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4157 if (lin.l_lnno == 0)
4159 lin.l_addr.l_paddr += offset;
4160 bfd_coff_swap_lineno_out (output_bfd,
4165 count = (linp - (finfo->linenos + linoff)) / linesz;
4167 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4168 (o->output_section->line_filepos
4169 + o->output_section->lineno_count * linesz);
4171 if (bfd_seek (output_bfd,
4172 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4174 || (bfd_write (finfo->linenos + linoff,
4175 linesz, count, output_bfd)
4179 o->output_section->lineno_count += count;
4183 struct internal_syment *iisp, *iispend;
4187 /* Update any C_BINCL or C_EINCL symbols
4188 that refer to a line number in the
4189 range we just output. */
4190 iisp = finfo->internal_syms;
4192 + obj_raw_syment_count (input_bfd));
4193 iindp = finfo->sym_indices;
4194 oos = finfo->outsyms;
4195 while (iisp < iispend)
4197 if ((iisp->n_sclass == C_BINCL
4198 || iisp->n_sclass == C_EINCL)
4199 && ((bfd_size_type) iisp->n_value
4200 >= enclosing->line_filepos + linoff)
4201 && ((bfd_size_type) iisp->n_value
4202 < (enclosing->line_filepos
4203 + enc_count * linesz)))
4205 struct internal_syment iis;
4207 bfd_coff_swap_sym_in (output_bfd,
4212 - enclosing->line_filepos
4214 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4215 bfd_coff_swap_sym_out (output_bfd,
4221 iisp += iisp->n_numaux + 1;
4222 iindp += iisp->n_numaux + 1;
4223 oos += (iisp->n_numaux + 1) * osymesz;
4230 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4231 isymp->n_sclass, i, isymp->n_numaux,
4243 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4244 symbol will be the first symbol in the next input file. In the
4245 normal case, this will save us from writing out the C_FILE symbol
4247 if (finfo->last_file_index != -1
4248 && (bfd_size_type) finfo->last_file_index >= syment_base)
4250 finfo->last_file.n_value = output_index;
4251 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
4252 (PTR) (finfo->outsyms
4253 + ((finfo->last_file_index - syment_base)
4257 /* Write the modified symbols to the output file. */
4258 if (outsym > finfo->outsyms)
4260 if (bfd_seek (output_bfd,
4261 obj_sym_filepos (output_bfd) + syment_base * osymesz,
4263 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
4265 != (bfd_size_type) (outsym - finfo->outsyms)))
4268 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4269 + (outsym - finfo->outsyms) / osymesz)
4272 obj_raw_syment_count (output_bfd) = output_index;
4275 /* Don't let the linker relocation routines discard the symbols. */
4276 keep_syms = obj_coff_keep_syms (input_bfd);
4277 obj_coff_keep_syms (input_bfd) = true;
4279 /* Relocate the contents of each section. */
4280 for (o = input_bfd->sections; o != NULL; o = o->next)
4284 if ((o->flags & SEC_HAS_CONTENTS) == 0
4285 || o->_raw_size == 0
4286 || (o->flags & SEC_IN_MEMORY) != 0)
4289 /* We have set filepos correctly for the sections we created to
4290 represent csects, so bfd_get_section_contents should work. */
4291 if (coff_section_data (input_bfd, o) != NULL
4292 && coff_section_data (input_bfd, o)->contents != NULL)
4293 contents = coff_section_data (input_bfd, o)->contents;
4296 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4297 (file_ptr) 0, o->_raw_size))
4299 contents = finfo->contents;
4302 if ((o->flags & SEC_RELOC) != 0)
4305 struct internal_reloc *internal_relocs;
4306 struct internal_reloc *irel;
4308 struct internal_reloc *irelend;
4309 struct xcoff_link_hash_entry **rel_hash;
4312 /* Read in the relocs. */
4313 target_index = o->output_section->target_index;
4314 internal_relocs = (xcoff_read_internal_relocs
4315 (input_bfd, o, false, finfo->external_relocs,
4317 (finfo->section_info[target_index].relocs
4318 + o->output_section->reloc_count)));
4319 if (internal_relocs == NULL)
4322 /* Call processor specific code to relocate the section
4324 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4328 finfo->internal_syms,
4329 xcoff_data (input_bfd)->csects))
4332 offset = o->output_section->vma + o->output_offset - o->vma;
4333 irel = internal_relocs;
4334 irelend = irel + o->reloc_count;
4335 rel_hash = (finfo->section_info[target_index].rel_hashes
4336 + o->output_section->reloc_count);
4337 for (; irel < irelend; irel++, rel_hash++)
4339 struct xcoff_link_hash_entry *h = NULL;
4340 struct internal_ldrel ldrel;
4344 /* Adjust the reloc address and symbol index. */
4346 irel->r_vaddr += offset;
4348 r_symndx = irel->r_symndx;
4352 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4354 && (irel->r_type == R_TOC
4355 || irel->r_type == R_GL
4356 || irel->r_type == R_TCL
4357 || irel->r_type == R_TRL
4358 || irel->r_type == R_TRLA))
4360 /* This is a TOC relative reloc with a symbol
4361 attached. The symbol should be the one which
4362 this reloc is for. We want to make this
4363 reloc against the TOC address of the symbol,
4364 not the symbol itself. */
4365 BFD_ASSERT (h->toc_section != NULL);
4366 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4367 if (h->u.toc_indx != -1)
4368 irel->r_symndx = h->u.toc_indx;
4371 struct xcoff_toc_rel_hash *n;
4372 struct xcoff_link_section_info *si;
4374 n = ((struct xcoff_toc_rel_hash *)
4375 bfd_alloc (finfo->output_bfd,
4376 sizeof (struct xcoff_toc_rel_hash)));
4379 bfd_set_error (bfd_error_no_memory);
4382 si = finfo->section_info + target_index;
4383 n->next = si->toc_rel_hashes;
4386 si->toc_rel_hashes = n;
4391 /* This is a global symbol. */
4393 irel->r_symndx = h->indx;
4396 /* This symbol is being written at the end
4397 of the file, and we do not yet know the
4398 symbol index. We save the pointer to the
4399 hash table entry in the rel_hash list.
4400 We set the indx field to -2 to indicate
4401 that this symbol must not be stripped. */
4410 indx = finfo->sym_indices[r_symndx];
4414 struct internal_syment *is;
4416 /* Relocations against a TC0 TOC anchor are
4417 automatically transformed to be against
4418 the TOC anchor in the output file. */
4419 is = finfo->internal_syms + r_symndx;
4420 if (is->n_sclass == C_HIDEXT
4421 && is->n_numaux > 0)
4424 union internal_auxent aux;
4428 obj_coff_external_syms (input_bfd))
4429 + ((r_symndx + is->n_numaux)
4431 bfd_coff_swap_aux_in (input_bfd, auxptr,
4432 is->n_type, is->n_sclass,
4436 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4437 && aux.x_csect.x_smclas == XMC_TC0)
4438 indx = finfo->toc_symindx;
4443 irel->r_symndx = indx;
4446 struct internal_syment *is;
4448 char buf[SYMNMLEN + 1];
4450 /* This reloc is against a symbol we are
4451 stripping. It would be possible to handle
4452 this case, but I don't think it's worth it. */
4453 is = finfo->internal_syms + r_symndx;
4455 name = (_bfd_coff_internal_syment_name
4456 (input_bfd, is, buf));
4460 if (! ((*finfo->info->callbacks->unattached_reloc)
4461 (finfo->info, name, input_bfd, o,
4468 switch (irel->r_type)
4472 || h->root.type == bfd_link_hash_defined
4473 || h->root.type == bfd_link_hash_defweak
4474 || h->root.type == bfd_link_hash_common)
4481 /* This reloc needs to be copied into the .loader
4483 ldrel.l_vaddr = irel->r_vaddr;
4485 ldrel.l_symndx = -1;
4487 || (h->root.type == bfd_link_hash_defined
4488 || h->root.type == bfd_link_hash_defweak
4489 || h->root.type == bfd_link_hash_common))
4494 sec = xcoff_data (input_bfd)->csects[r_symndx];
4495 else if (h->root.type == bfd_link_hash_common)
4496 sec = h->root.u.c.p->section;
4498 sec = h->root.u.def.section;
4499 sec = sec->output_section;
4501 if (strcmp (sec->name, ".text") == 0)
4503 else if (strcmp (sec->name, ".data") == 0)
4505 else if (strcmp (sec->name, ".bss") == 0)
4509 (*_bfd_error_handler)
4510 ("%s: loader reloc in unrecognized section `%s'",
4511 bfd_get_filename (input_bfd),
4513 bfd_set_error (bfd_error_nonrepresentable_section);
4521 (*_bfd_error_handler)
4522 ("%s: `%s' in loader reloc but not loader sym",
4523 bfd_get_filename (input_bfd),
4524 h->root.root.string);
4525 bfd_set_error (bfd_error_bad_value);
4528 ldrel.l_symndx = h->ldindx;
4530 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4531 ldrel.l_rsecnm = o->output_section->target_index;
4532 if (xcoff_hash_table (finfo->info)->textro
4533 && strcmp (o->output_section->name, ".text") == 0)
4535 (*_bfd_error_handler)
4536 ("%s: loader reloc in read-only section %s",
4537 bfd_get_filename (input_bfd),
4538 bfd_get_section_name (finfo->output_bfd,
4539 o->output_section));
4540 bfd_set_error (bfd_error_invalid_operation);
4543 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4545 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4554 /* We should never need a .loader reloc for a TOC
4560 o->output_section->reloc_count += o->reloc_count;
4563 /* Write out the modified section contents. */
4564 if (! bfd_set_section_contents (output_bfd, o->output_section,
4565 contents, o->output_offset,
4566 (o->_cooked_size != 0
4572 obj_coff_keep_syms (input_bfd) = keep_syms;
4574 if (! finfo->info->keep_memory)
4576 if (! _bfd_coff_free_symbols (input_bfd))
4586 /* Write out a non-XCOFF global symbol. */
4589 xcoff_write_global_symbol (h, p)
4590 struct xcoff_link_hash_entry *h;
4593 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4596 struct internal_syment isym;
4597 union internal_auxent aux;
4599 output_bfd = finfo->output_bfd;
4601 /* If this symbol was garbage collected, just skip it. */
4602 if (xcoff_hash_table (finfo->info)->gc
4603 && (h->flags & XCOFF_MARK) == 0)
4606 /* If we need a .loader section entry, write it out. */
4607 if (h->ldsym != NULL)
4609 struct internal_ldsym *ldsym;
4614 if (h->root.type == bfd_link_hash_undefined
4615 || h->root.type == bfd_link_hash_undefweak)
4618 ldsym->l_scnum = N_UNDEF;
4619 ldsym->l_smtype = XTY_ER;
4620 impbfd = h->root.u.undef.abfd;
4622 else if (h->root.type == bfd_link_hash_defined
4623 || h->root.type == bfd_link_hash_defweak)
4627 sec = h->root.u.def.section;
4628 ldsym->l_value = (sec->output_section->vma
4629 + sec->output_offset
4630 + h->root.u.def.value);
4631 ldsym->l_scnum = sec->output_section->target_index;
4632 ldsym->l_smtype = XTY_SD;
4633 impbfd = sec->owner;
4638 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4639 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4640 || (h->flags & XCOFF_IMPORT) != 0)
4641 ldsym->l_smtype |= L_IMPORT;
4642 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4643 && (h->flags & XCOFF_REF_DYNAMIC) != 0)
4644 || (h->flags & XCOFF_EXPORT) != 0)
4645 ldsym->l_smtype |= L_EXPORT;
4646 if ((h->flags & XCOFF_ENTRY) != 0)
4647 ldsym->l_smtype |= L_ENTRY;
4649 ldsym->l_smclas = h->smclas;
4651 if (ldsym->l_ifile == (bfd_size_type) -1)
4653 else if (ldsym->l_ifile == 0)
4655 if ((ldsym->l_smtype & L_IMPORT) == 0)
4657 else if (impbfd == NULL)
4661 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4662 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4668 BFD_ASSERT (h->ldindx >= 0);
4669 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4670 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4674 /* If this symbol needs global linkage code, write it out. */
4675 if (h->root.type == bfd_link_hash_defined
4676 && (h->root.u.def.section
4677 == xcoff_hash_table (finfo->info)->linkage_section))
4683 p = h->root.u.def.section->contents + h->root.u.def.value;
4685 /* The first instruction in the global linkage code loads a
4686 specific TOC element. */
4687 tocoff = (h->descriptor->toc_section->output_section->vma
4688 + h->descriptor->toc_section->output_offset
4689 - xcoff_data (output_bfd)->toc);
4690 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4691 tocoff += h->descriptor->u.toc_offset;
4692 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4694 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
4696 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
4699 /* If we created a TOC entry for this symbol, write out the required
4701 if ((h->flags & XCOFF_SET_TOC) != 0)
4706 struct internal_reloc *irel;
4707 struct internal_ldrel ldrel;
4709 tocsec = h->toc_section;
4710 osec = tocsec->output_section;
4711 oindx = osec->target_index;
4712 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4713 irel->r_vaddr = (osec->vma
4714 + tocsec->output_offset
4717 irel->r_symndx = h->indx;
4721 irel->r_symndx = obj_raw_syment_count (output_bfd);
4723 irel->r_type = R_POS;
4725 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4726 ++osec->reloc_count;
4728 BFD_ASSERT (h->ldindx >= 0);
4729 ldrel.l_vaddr = irel->r_vaddr;
4730 ldrel.l_symndx = h->ldindx;
4731 ldrel.l_rtype = (31 << 8) | R_POS;
4732 ldrel.l_rsecnm = oindx;
4733 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4741 && (finfo->info->strip == strip_all
4742 || (finfo->info->strip == strip_some
4743 && (bfd_hash_lookup (finfo->info->keep_hash,
4744 h->root.root.string, false, false)
4749 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
4752 outsym = finfo->outsyms;
4754 memset (&aux, 0, sizeof aux);
4756 h->indx = obj_raw_syment_count (output_bfd);
4758 if (strlen (h->root.root.string) <= SYMNMLEN)
4759 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
4766 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4768 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
4770 if (indx == (bfd_size_type) -1)
4772 isym._n._n_n._n_zeroes = 0;
4773 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4776 if (h->root.type == bfd_link_hash_undefined
4777 || h->root.type == bfd_link_hash_undefweak)
4780 isym.n_scnum = N_UNDEF;
4781 isym.n_sclass = C_EXT;
4782 aux.x_csect.x_smtyp = XTY_ER;
4784 else if (h->root.type == bfd_link_hash_defined
4785 || h->root.type == bfd_link_hash_defweak)
4787 struct xcoff_link_size_list *l;
4789 isym.n_value = (h->root.u.def.section->output_section->vma
4790 + h->root.u.def.section->output_offset
4791 + h->root.u.def.value);
4792 isym.n_scnum = h->root.u.def.section->output_section->target_index;
4793 isym.n_sclass = C_HIDEXT;
4794 aux.x_csect.x_smtyp = XTY_SD;
4796 if ((h->flags & XCOFF_HAS_SIZE) != 0)
4798 for (l = xcoff_hash_table (finfo->info)->size_list;
4804 aux.x_csect.x_scnlen.l = l->size;
4810 else if (h->root.type == bfd_link_hash_common)
4812 isym.n_value = (h->root.u.c.p->section->output_section->vma
4813 + h->root.u.c.p->section->output_offset);
4814 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
4815 isym.n_sclass = C_EXT;
4816 aux.x_csect.x_smtyp = XTY_CM;
4817 aux.x_csect.x_scnlen.l = h->root.u.c.size;
4822 isym.n_type = T_NULL;
4825 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4826 outsym += bfd_coff_symesz (output_bfd);
4828 aux.x_csect.x_smclas = h->smclas;
4830 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
4832 outsym += bfd_coff_auxesz (output_bfd);
4834 if (h->root.type == bfd_link_hash_defined
4835 || h->root.type == bfd_link_hash_defweak)
4837 /* We just output an SD symbol. Now output an LD symbol. */
4841 isym.n_sclass = C_EXT;
4842 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4843 outsym += bfd_coff_symesz (output_bfd);
4845 aux.x_csect.x_smtyp = XTY_LD;
4846 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
4848 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
4850 outsym += bfd_coff_auxesz (output_bfd);
4853 if (bfd_seek (output_bfd,
4854 (obj_sym_filepos (output_bfd)
4855 + (obj_raw_syment_count (output_bfd)
4856 * bfd_coff_symesz (output_bfd))),
4858 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
4859 != (bfd_size_type) (outsym - finfo->outsyms)))
4861 obj_raw_syment_count (output_bfd) +=
4862 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4867 /* Handle a link order which is supposed to generate a reloc. */
4870 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
4872 struct xcoff_final_link_info *finfo;
4873 asection *output_section;
4874 struct bfd_link_order *link_order;
4876 reloc_howto_type *howto;
4877 struct xcoff_link_hash_entry *h;
4881 struct internal_reloc *irel;
4882 struct xcoff_link_hash_entry **rel_hash_ptr;
4883 struct internal_ldrel ldrel;
4885 if (link_order->type == bfd_section_reloc_link_order)
4887 /* We need to somehow locate a symbol in the right section. The
4888 symbol must either have a value of zero, or we must adjust
4889 the addend by the value of the symbol. FIXME: Write this
4890 when we need it. The old linker couldn't handle this anyhow. */
4894 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4897 bfd_set_error (bfd_error_bad_value);
4901 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
4902 link_order->u.reloc.p->u.name,
4903 false, false, true);
4906 if (! ((*finfo->info->callbacks->unattached_reloc)
4907 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4908 (asection *) NULL, (bfd_vma) 0)))
4913 if (h->root.type == bfd_link_hash_common)
4915 hsec = h->root.u.c.p->section;
4918 else if (h->root.type == bfd_link_hash_defined
4919 || h->root.type == bfd_link_hash_defweak)
4921 hsec = h->root.u.def.section;
4922 hval = h->root.u.def.value;
4930 addend = link_order->u.reloc.p->addend;
4932 addend += (hsec->output_section->vma
4933 + hsec->output_offset
4940 bfd_reloc_status_type rstat;
4943 size = bfd_get_reloc_size (howto);
4944 buf = (bfd_byte *) bfd_zmalloc (size);
4947 bfd_set_error (bfd_error_no_memory);
4951 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
4957 case bfd_reloc_outofrange:
4959 case bfd_reloc_overflow:
4960 if (! ((*finfo->info->callbacks->reloc_overflow)
4961 (finfo->info, link_order->u.reloc.p->u.name,
4962 howto->name, addend, (bfd *) NULL, (asection *) NULL,
4970 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4971 (file_ptr) link_order->offset, size);
4977 /* Store the reloc information in the right place. It will get
4978 swapped and written out at the end of the final_link routine. */
4980 irel = (finfo->section_info[output_section->target_index].relocs
4981 + output_section->reloc_count);
4982 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
4983 + output_section->reloc_count);
4985 memset (irel, 0, sizeof (struct internal_reloc));
4986 *rel_hash_ptr = NULL;
4988 irel->r_vaddr = output_section->vma + link_order->offset;
4991 irel->r_symndx = h->indx;
4994 /* Set the index to -2 to force this symbol to get written out. */
5000 irel->r_type = howto->type;
5001 irel->r_size = howto->bitsize - 1;
5002 if (howto->complain_on_overflow == complain_overflow_signed)
5003 irel->r_size |= 0x80;
5005 ++output_section->reloc_count;
5007 /* Now output the reloc to the .loader section. */
5009 ldrel.l_vaddr = irel->r_vaddr;
5013 const char *secname;
5015 secname = hsec->output_section->name;
5017 if (strcmp (secname, ".text") == 0)
5019 else if (strcmp (secname, ".data") == 0)
5021 else if (strcmp (secname, ".bss") == 0)
5025 (*_bfd_error_handler)
5026 ("%s: loader reloc in unrecognized section `%s'",
5027 bfd_get_filename (output_bfd), secname);
5028 bfd_set_error (bfd_error_nonrepresentable_section);
5036 (*_bfd_error_handler)
5037 ("%s: `%s' in loader reloc but not loader sym",
5038 bfd_get_filename (output_bfd),
5039 h->root.root.string);
5040 bfd_set_error (bfd_error_bad_value);
5043 ldrel.l_symndx = h->ldindx;
5046 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5047 ldrel.l_rsecnm = output_section->target_index;
5048 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5054 /* Sort relocs by VMA. This is called via qsort. */
5057 xcoff_sort_relocs (p1, p2)
5061 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5062 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5064 if (r1->r_vaddr > r2->r_vaddr)
5066 else if (r1->r_vaddr < r2->r_vaddr)
5072 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5073 This is currently the only processor which uses XCOFF; I hope that
5074 will never change. */
5077 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
5078 input_section, contents, relocs, syms,
5081 struct bfd_link_info *info;
5083 asection *input_section;
5085 struct internal_reloc *relocs;
5086 struct internal_syment *syms;
5087 asection **sections;
5089 struct internal_reloc *rel;
5090 struct internal_reloc *relend;
5093 relend = rel + input_section->reloc_count;
5094 for (; rel < relend; rel++)
5097 struct xcoff_link_hash_entry *h;
5098 struct internal_syment *sym;
5101 struct reloc_howto_struct howto;
5102 bfd_reloc_status_type rstat;
5104 /* Relocation type R_REF is a special relocation type which is
5105 merely used to prevent garbage collection from occurring for
5106 the csect including the symbol which it references. */
5107 if (rel->r_type == R_REF)
5110 symndx = rel->r_symndx;
5120 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
5121 sym = syms + symndx;
5122 addend = - sym->n_value;
5125 /* We build the howto information on the fly. */
5127 howto.type = rel->r_type;
5128 howto.rightshift = 0;
5130 howto.bitsize = (rel->r_size & 0x1f) + 1;
5131 howto.pc_relative = false;
5133 if ((rel->r_size & 0x80) != 0)
5134 howto.complain_on_overflow = complain_overflow_signed;
5136 howto.complain_on_overflow = complain_overflow_bitfield;
5137 howto.special_function = NULL;
5138 howto.name = "internal";
5139 howto.partial_inplace = true;
5140 if (howto.bitsize == 32)
5141 howto.src_mask = howto.dst_mask = 0xffffffff;
5144 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
5145 if (howto.bitsize == 16)
5148 howto.pcrel_offset = false;
5158 sec = bfd_abs_section_ptr;
5163 sec = sections[symndx];
5164 val = (sec->output_section->vma
5165 + sec->output_offset
5172 if (h->root.type == bfd_link_hash_defined
5173 || h->root.type == bfd_link_hash_defweak)
5177 sec = h->root.u.def.section;
5178 val = (h->root.u.def.value
5179 + sec->output_section->vma
5180 + sec->output_offset);
5182 else if (h->root.type == bfd_link_hash_common)
5186 sec = h->root.u.c.p->section;
5187 val = (sec->output_section->vma
5188 + sec->output_offset);
5190 else if ((h->flags & XCOFF_REF_DYNAMIC) != 0
5191 || (h->flags & XCOFF_IMPORT) != 0)
5193 /* Every symbol in a shared object is defined somewhere. */
5196 else if (! info->relocateable
5199 if (! ((*info->callbacks->undefined_symbol)
5200 (info, h->root.root.string, input_bfd, input_section,
5201 rel->r_vaddr - input_section->vma)))
5206 /* I took the relocation type definitions from two documents:
5207 the PowerPC AIX Version 4 Application Binary Interface, First
5208 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5209 32-Bit Hardware Implementation (June 30, 1994). Differences
5210 between the documents are noted below. */
5212 switch (rel->r_type)
5217 /* These relocs are defined by the PowerPC ABI to be
5218 relative branches which use half of the difference
5219 between the symbol and the program counter. I can't
5220 quite figure out when this is useful. These relocs are
5221 not defined by the PowerOpen ABI. */
5223 (*_bfd_error_handler)
5224 ("%s: unsupported relocation type 0x%02x",
5225 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
5226 bfd_set_error (bfd_error_bad_value);
5229 /* Simple positive relocation. */
5232 /* Simple negative relocation. */
5236 /* Simple PC relative relocation. */
5237 howto.pc_relative = true;
5240 /* TOC relative relocation. The value in the instruction in
5241 the input file is the offset from the input file TOC to
5242 the desired location. We want the offset from the final
5243 TOC to the desired location. We have:
5248 so we must change insn by on - in.
5251 /* Global linkage relocation. The value of this relocation
5252 is the address of the entry in the TOC section. */
5254 /* Local object TOC address. I can't figure out the
5255 difference between this and case R_GL. */
5257 /* TOC relative relocation. A TOC relative load instruction
5258 which may be changed to a load address instruction.
5259 FIXME: We don't currently implement this optimization. */
5261 /* TOC relative relocation. This is a TOC relative load
5262 address instruction which may be changed to a load
5263 instruction. FIXME: I don't know if this is the correct
5265 if (h != NULL && h->toc_section == NULL)
5267 (*_bfd_error_handler)
5268 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5269 bfd_get_filename (input_bfd), rel->r_vaddr,
5270 h->root.root.string);
5271 bfd_set_error (bfd_error_bad_value);
5276 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5277 val = (h->toc_section->output_section->vma
5278 + h->toc_section->output_offset);
5280 val = ((val - xcoff_data (output_bfd)->toc)
5281 - (sym->n_value - xcoff_data (input_bfd)->toc));
5285 /* Absolute branch. We don't want to mess with the lower
5286 two bits of the instruction. */
5288 /* The PowerPC ABI defines this as an absolute call which
5289 may be modified to become a relative call. The PowerOpen
5290 ABI does not define this relocation type. */
5292 /* Absolute branch which may be modified to become a
5295 /* The PowerPC ABI defines this as an absolute branch to a
5296 fixed address which may be modified to an absolute branch
5297 to a symbol. The PowerOpen ABI does not define this
5300 /* The PowerPC ABI defines this as an absolute branch to a
5301 fixed address which may be modified to a relative branch.
5302 The PowerOpen ABI does not define this relocation type. */
5303 howto.src_mask &= ~3;
5304 howto.dst_mask = howto.src_mask;
5307 /* Relative branch. We don't want to mess with the lower
5308 two bits of the instruction. */
5310 /* The PowerPC ABI defines this as a relative call which may
5311 be modified to become an absolute call. The PowerOpen
5312 ABI does not define this relocation type. */
5314 /* A relative branch which may be modified to become an
5315 absolute branch. FIXME: We don't implement this,
5316 although we should for symbols of storage mapping class
5318 howto.pc_relative = true;
5319 howto.src_mask &= ~3;
5320 howto.dst_mask = howto.src_mask;
5323 /* The PowerPC AIX ABI describes this as a load which may be
5324 changed to a load address. The PowerOpen ABI says this
5325 is the same as case R_POS. */
5328 /* The PowerPC AIX ABI describes this as a load address
5329 which may be changed to a load. The PowerOpen ABI says
5330 this is the same as R_POS. */
5334 /* If we see an R_BR or R_RBR reloc which is jumping to global
5335 linkage code, and it is followed by an appropriate cror nop
5336 instruction, we replace the cror with lwz r2,20(r1). This
5337 restores the TOC after the glink code. Contrariwise, if the
5338 call is followed by a lwz r2,20(r1), but the call is not
5339 going to global linkage code, we can replace the load with a
5341 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
5343 && h->root.type == bfd_link_hash_defined
5344 && (rel->r_vaddr - input_section->vma + 8
5345 <= input_section->_cooked_size))
5350 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
5351 next = bfd_get_32 (input_bfd, pnext);
5352 if (h->smclas == XMC_GL)
5354 if (next == 0x4def7b82 /* cror 15,15,15 */
5355 || next == 0x4ffffb82) /* cror 31,31,31 */
5356 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
5360 if (next == 0x80410014) /* lwz r1,20(r1) */
5361 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
5365 /* A PC relative reloc includes the section address. */
5366 if (howto.pc_relative)
5367 addend += input_section->vma;
5369 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
5371 rel->r_vaddr - input_section->vma,
5380 case bfd_reloc_overflow:
5383 char buf[SYMNMLEN + 1];
5384 char howto_name[10];
5389 name = h->root.root.string;
5392 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
5396 sprintf (howto_name, "0x%02x", rel->r_type);
5398 if (! ((*info->callbacks->reloc_overflow)
5399 (info, name, howto_name, (bfd_vma) 0, input_bfd,
5400 input_section, rel->r_vaddr - input_section->vma)))