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. A lot of it is very similar
29 to the COFF linker code. However, it is different enough that I
30 chose to avoid trying to hack up the COFF code to support XCOFF.
31 That leads to a certain amount of duplicated code, alas. */
33 #define STRING_SIZE_SIZE (4)
35 /* Get the XCOFF hash table entries for a BFD. */
36 #define obj_xcoff_sym_hashes(bfd) \
37 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
39 /* An entry in the XCOFF linker hash table. */
41 struct xcoff_link_hash_entry
43 struct bfd_link_hash_entry root;
45 /* Whether this symbol came from an XCOFF input file. */
48 /* Symbol index in output file. Set to -1 initially. Set to -2 if
49 there is a reloc against this symbol. */
52 /* Whether we have created a TOC entry for this symbol. */
56 /* The XCOFF linker hash table. */
58 struct xcoff_link_hash_table
60 struct bfd_link_hash_table root;
62 /* The .debug string hash table. We need to compute this while
63 reading the input files, so that we know how large the .debug
64 section will be before we assign section positions. */
65 struct bfd_strtab_hash *debug_strtab;
67 /* The .debug section we will use for the final output. */
68 asection *debug_section;
71 /* Information we keep for each section in the output file. */
73 struct xcoff_link_section_info
75 /* The relocs to be output. */
76 struct internal_reloc *relocs;
77 /* For each reloc against a global symbol whose index was not known
78 when the reloc was handled, the global hash table entry. */
79 struct xcoff_link_hash_entry **rel_hashes;
82 /* Information that we pass around while doing the final link step. */
84 struct xcoff_final_link_info
86 /* General link information. */
87 struct bfd_link_info *info;
90 /* Hash table for long symbol names. */
91 struct bfd_strtab_hash *strtab;
92 /* Array of information kept for each output section, indexed by the
93 target_index field. */
94 struct xcoff_link_section_info *section_info;
95 /* Symbol index of last C_FILE symbol (-1 if none). */
97 /* Contents of last C_FILE symbol. */
98 struct internal_syment last_file;
99 /* Symbol index of TOC symbol. */
101 /* Buffer large enough to hold swapped symbols of any input file. */
102 struct internal_syment *internal_syms;
103 /* Buffer large enough to hold output indices of symbols of any
106 /* Buffer large enough to hold output symbols for any input file. */
108 /* Buffer large enough to hold external line numbers for any input
111 /* Buffer large enough to hold any input section. */
113 /* Buffer large enough to hold external relocs of any input section. */
114 bfd_byte *external_relocs;
117 static struct bfd_hash_entry *xcoff_link_hash_newfunc
118 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
119 static boolean xcoff_link_add_object_symbols
120 PARAMS ((bfd *, struct bfd_link_info *));
121 static boolean xcoff_link_check_archive_element
122 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
123 static boolean xcoff_link_check_ar_symbols
124 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
125 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
126 static boolean xcoff_link_input_bfd
127 PARAMS ((struct xcoff_final_link_info *, bfd *));
128 static boolean xcoff_write_global_symbol
129 PARAMS ((struct xcoff_link_hash_entry *, PTR));
130 static boolean xcoff_reloc_link_order
131 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
132 struct bfd_link_order *));
133 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
135 /* Routine to create an entry in an XCOFF link hash table. */
137 static struct bfd_hash_entry *
138 xcoff_link_hash_newfunc (entry, table, string)
139 struct bfd_hash_entry *entry;
140 struct bfd_hash_table *table;
143 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
145 /* Allocate the structure if it has not already been allocated by a
147 if (ret == (struct xcoff_link_hash_entry *) NULL)
148 ret = ((struct xcoff_link_hash_entry *)
149 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
150 if (ret == (struct xcoff_link_hash_entry *) NULL)
152 bfd_set_error (bfd_error_no_memory);
153 return (struct bfd_hash_entry *) ret;
156 /* Call the allocation method of the superclass. */
157 ret = ((struct xcoff_link_hash_entry *)
158 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
162 /* Set local fields. */
168 return (struct bfd_hash_entry *) ret;
171 /* Create a XCOFF link hash table. */
173 struct bfd_link_hash_table *
174 _bfd_xcoff_bfd_link_hash_table_create (abfd)
177 struct xcoff_link_hash_table *ret;
179 ret = ((struct xcoff_link_hash_table *)
180 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
181 if (ret == (struct xcoff_link_hash_table *) NULL)
183 bfd_set_error (bfd_error_no_memory);
184 return (struct bfd_link_hash_table *) NULL;
186 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
188 bfd_release (abfd, ret);
189 return (struct bfd_link_hash_table *) NULL;
192 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
193 ret->debug_section = NULL;
198 /* Look up an entry in an XCOFF link hash table. */
200 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
201 ((struct xcoff_link_hash_entry *) \
202 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
205 /* Traverse an XCOFF link hash table. */
207 #define xcoff_link_hash_traverse(table, func, info) \
208 (bfd_link_hash_traverse \
210 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
213 /* Get the XCOFF link hash table from the info structure. This is
216 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
218 /* Given an XCOFF BFD, add symbols to the global hash table as
222 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
224 struct bfd_link_info *info;
226 switch (bfd_get_format (abfd))
229 return xcoff_link_add_object_symbols (abfd, info);
231 return (_bfd_generic_link_add_archive_symbols
232 (abfd, info, xcoff_link_check_archive_element));
234 bfd_set_error (bfd_error_wrong_format);
239 /* Add symbols from an XCOFF object file. */
242 xcoff_link_add_object_symbols (abfd, info)
244 struct bfd_link_info *info;
246 if (! _bfd_coff_get_external_symbols (abfd))
248 if (! xcoff_link_add_symbols (abfd, info))
250 if (! info->keep_memory)
252 if (! _bfd_coff_free_symbols (abfd))
258 /* Check a single archive element to see if we need to include it in
259 the link. *PNEEDED is set according to whether this element is
260 needed in the link or not. This is called via
261 _bfd_generic_link_add_archive_symbols. */
264 xcoff_link_check_archive_element (abfd, info, pneeded)
266 struct bfd_link_info *info;
269 if (! _bfd_coff_get_external_symbols (abfd))
272 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
277 if (! xcoff_link_add_symbols (abfd, info))
281 if (! info->keep_memory || ! *pneeded)
283 if (! _bfd_coff_free_symbols (abfd))
290 /* Look through the symbols to see if this object file should be
291 included in the link. */
294 xcoff_link_check_ar_symbols (abfd, info, pneeded)
296 struct bfd_link_info *info;
299 bfd_size_type symesz;
305 symesz = bfd_coff_symesz (abfd);
306 esym = (bfd_byte *) obj_coff_external_syms (abfd);
307 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
308 while (esym < esym_end)
310 struct internal_syment sym;
312 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
314 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
317 char buf[SYMNMLEN + 1];
318 struct bfd_link_hash_entry *h;
320 /* This symbol is externally visible, and is defined by this
323 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
326 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
328 /* We are only interested in symbols that are currently
329 undefined. If a symbol is currently known to be common,
330 XCOFF linkers do not bring in an object file which
332 if (h != (struct bfd_link_hash_entry *) NULL
333 && h->type == bfd_link_hash_undefined)
335 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
342 esym += (sym.n_numaux + 1) * symesz;
345 /* We do not need this object file. */
349 /* Add all the symbols from an object file to the hash table.
351 XCOFF is a weird format. A normal XCOFF .o files will have three
352 COFF sections--.text, .data, and .bss--but each COFF section will
353 contain many csects. These csects are described in the symbol
354 table. From the linker's point of view, each csect must be
355 considered a section in its own right. For example, a TOC entry is
356 handled as a small XMC_TC csect. The linker must be able to merge
357 different TOC entries together, which means that it must be able to
358 extract the XMC_TC csects from the .data section of the input .o
361 From the point of view of our linker, this is, of course, a hideous
362 nightmare. We cope by actually creating sections for each csect,
363 and discarding the original sections. We then have to handle the
364 relocation entries carefully, since the only way to tell which
365 csect they belong to is to examine the address. */
368 xcoff_link_add_symbols (abfd, info)
370 struct bfd_link_info *info;
372 boolean default_copy;
373 bfd_size_type symcount;
374 struct xcoff_link_hash_entry **sym_hash;
375 asection **csect_cache;
376 asection *debug_section;
377 unsigned long *debug_index = NULL;
378 bfd_byte *debug_contents = NULL;
381 unsigned int csect_index;
382 asection *first_csect;
384 bfd_size_type symesz;
387 struct reloc_info_struct
389 struct internal_reloc *relocs;
391 } *reloc_info = NULL;
393 if (info->keep_memory)
394 default_copy = false;
398 symcount = obj_raw_syment_count (abfd);
400 /* We keep a list of the linker hash table entries that correspond
401 to each external symbol. */
402 sym_hash = ((struct xcoff_link_hash_entry **)
405 * sizeof (struct xcoff_link_hash_entry *))));
406 if (sym_hash == NULL && symcount != 0)
408 bfd_set_error (bfd_error_no_memory);
411 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
413 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
415 /* Because of the weird stuff we are doing with XCOFF csects, we can
416 not easily determine which section a symbol is in, so we store
417 the information in the tdata for the input file. */
418 csect_cache = ((asection **)
419 bfd_alloc (abfd, symcount * sizeof (asection *)));
420 if (csect_cache == NULL && symcount != 0)
422 bfd_set_error (bfd_error_no_memory);
425 xcoff_data (abfd)->csects = csect_cache;
426 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
428 /* XCOFF stores debugging symbol names in the .debug section. We
429 need to determine the size of the final .debug section while
430 reading the input files, before the linker starts deciding where
431 to put everything. We build a hash table for the final .debug
432 section. For each symbol whose name goes into it, we store the
433 index in the .debug section in the debug_indices array. */
434 debug_section = bfd_get_section_by_name (abfd, ".debug");
435 if (debug_section != NULL && debug_section->_raw_size > 0)
437 debug_index = ((unsigned long *)
438 bfd_zalloc (abfd, symcount * sizeof (unsigned long)));
439 if (debug_index == NULL && symcount != 0)
441 bfd_set_error (bfd_error_no_memory);
444 xcoff_data (abfd)->debug_indices = debug_index;
446 /* Grab the contents of the current .debug section. We use
447 malloc and copy the names into the string tab hash table,
448 rather than bfd_alloc, because I expect that when linking
449 many files together, many of the strings will be the same.
450 Storing the strings in the hash table should save space in
452 debug_contents = (bfd_byte *) malloc (debug_section->_raw_size);
453 if (debug_contents == NULL)
455 bfd_set_error (bfd_error_no_memory);
458 if (! bfd_get_section_contents (abfd, debug_section,
459 (PTR) debug_contents, (file_ptr) 0,
460 debug_section->_raw_size))
464 /* While splitting sections into csects, we need to assign the
465 relocs correctly. The relocs and the csects must both be in
466 order by VMA within a given section, so we handle this by
467 scanning along the relocs as we process the csects. We index
468 into reloc_info using the section target_index. */
469 reloc_info = ((struct reloc_info_struct *)
470 malloc ((abfd->section_count + 1)
471 * sizeof (struct reloc_info_struct)));
472 if (reloc_info == NULL)
474 bfd_set_error (bfd_error_no_memory);
477 memset ((PTR) reloc_info, 0,
478 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
480 /* Read in the relocs for each section which has relocs. */
481 for (sub = abfd->sections; sub != NULL; sub = sub->next)
483 if ((sub->flags & SEC_RELOC) != 0)
485 reloc_info[sub->target_index].relocs =
486 _bfd_coff_read_internal_relocs (abfd, sub, true,
487 (bfd_byte *) NULL, false,
488 (struct internal_reloc *) NULL);
489 reloc_info[sub->target_index].used =
490 (boolean *) malloc (sub->reloc_count * sizeof (boolean));
491 if (reloc_info[sub->target_index].used == NULL)
493 bfd_set_error (bfd_error_no_memory);
496 memset (reloc_info[sub->target_index].used, 0,
497 sub->reloc_count * sizeof (boolean));
506 symesz = bfd_coff_symesz (abfd);
507 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
508 esym = (bfd_byte *) obj_coff_external_syms (abfd);
509 esym_end = esym + symcount * symesz;
510 while (esym < esym_end)
512 struct internal_syment sym;
513 union internal_auxent aux;
515 char buf[SYMNMLEN + 1];
521 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
523 *debug_index = (unsigned long) -1;
525 /* If the name of this symbol is in the .debug section, and it
526 looks like we are going to keep it, add the name to the
527 .debug hash table. */
528 if (info->hash->creator == abfd->xvec
529 && sym._n._n_n._n_zeroes == 0
530 && info->strip != strip_all
531 && info->strip != strip_debugger
532 && info->discard != discard_all
533 && bfd_coff_symname_in_debug (abfd, &sym))
537 /* Add 2 to skip the length. */
538 name = debug_contents + sym._n._n_n._n_offset;
540 indx = _bfd_stringtab_add (xcoff_hash_table (info)->debug_strtab,
542 if (indx == (bfd_size_type) -1)
548 /* In this pass we are only interested in symbols with csect
550 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
553 *csect_cache = csect;
554 else if (first_csect == NULL)
555 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
558 esym += (sym.n_numaux + 1) * symesz;
559 sym_hash += sym.n_numaux + 1;
560 csect_cache += sym.n_numaux + 1;
561 if (debug_index != NULL)
562 debug_index += sym.n_numaux + 1;
566 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
570 /* Pick up the csect auxiliary information. */
572 if (sym.n_numaux == 0)
574 (*_bfd_error_handler)
575 ("%s: class %d symbol `%s' has no aux entries",
576 bfd_get_filename (abfd), sym.n_sclass, name);
577 bfd_set_error (bfd_error_bad_value);
581 bfd_coff_swap_aux_in (abfd,
582 (PTR) (esym + symesz * sym.n_numaux),
583 sym.n_type, sym.n_sclass,
584 sym.n_numaux - 1, sym.n_numaux,
587 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
596 (*_bfd_error_handler)
597 ("%s: symbol `%s' has unrecognized csect type %d",
598 bfd_get_filename (abfd), name, smtyp);
599 bfd_set_error (bfd_error_bad_value);
603 /* This is an external reference. */
604 if (sym.n_sclass == C_HIDEXT
605 || sym.n_scnum != N_UNDEF
606 || aux.x_csect.x_scnlen.l != 0)
608 (*_bfd_error_handler)
609 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
610 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
611 aux.x_csect.x_scnlen.l);
612 bfd_set_error (bfd_error_bad_value);
615 section = bfd_und_section_ptr;
619 /* This is a csect definition. */
624 /* When we see a TOC anchor, we record the TOC value. */
625 if (aux.x_csect.x_smclas == XMC_TC0)
627 if (sym.n_sclass != C_HIDEXT
628 || aux.x_csect.x_scnlen.l != 0)
630 (*_bfd_error_handler)
631 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
632 bfd_get_filename (abfd), name, sym.n_sclass,
633 aux.x_csect.x_scnlen.l);
634 bfd_set_error (bfd_error_bad_value);
637 xcoff_data (abfd)->toc = sym.n_value;
640 /* We must merge TOC entries for the same symbol. We can
641 merge two TOC entries if they are both C_HIDEXT, they
642 both have the same name, they are both 4 bytes long, and
643 they both have a relocation table entry for an external
644 symbol with the same name. Unfortunately, this means
645 that we must look through the relocations. Ick. */
646 if (aux.x_csect.x_smclas == XMC_TC
647 && sym.n_sclass == C_HIDEXT
648 && aux.x_csect.x_scnlen.l == 4)
651 bfd_size_type relindx;
652 struct internal_reloc *rel;
655 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
656 if (enclosing == NULL)
659 /* XCOFF requires that relocs be sorted by address, so
660 we could do a binary search here. FIXME. */
661 rel = reloc_info[enclosing->target_index].relocs;
662 used = reloc_info[enclosing->target_index].used;
664 relindx < enclosing->reloc_count;
665 relindx++, rel++, used++)
668 && rel->r_vaddr == (bfd_vma) sym.n_value
673 if (relindx < enclosing->reloc_count)
676 struct internal_syment relsym;
678 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
679 + rel->r_symndx * symesz);
680 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
681 if (relsym.n_sclass == C_EXT)
684 char relbuf[SYMNMLEN + 1];
686 struct xcoff_link_hash_entry *h;
688 /* At this point we know that the TOC entry is
689 for an externally visible symbol. */
690 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
694 copy = (! info->keep_memory
695 || relsym._n._n_n._n_zeroes != 0
696 || relsym._n._n_n._n_offset == 0);
697 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
698 relname, true, copy, false);
702 /* At this point h->root.type could be
703 bfd_link_hash_new. That should be OK, since
704 we know for sure that we will come across
705 this symbol as we step through the file. */
707 /* Stuff h into *sym_hash. We normally only set
708 *sym_hash for global variables. Remembering
709 it here saves us doing this work again later. */
714 /* We already have a TOC entry for this
715 symbol, so we can just ignore this one.
716 We need to skip the csect, and the reloc
722 /* We are about to create a TOC entry for this
729 /* We need to create a new section. We get the name from
730 the csect storage mapping class, so that the linker can
731 accumulate similar csects together. */
733 static const char *csect_name_by_class[] =
735 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
736 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
739 const char *csect_name;
741 struct internal_reloc *rel;
742 bfd_size_type relindx;
745 if ((aux.x_csect.x_smclas >=
746 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
747 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
749 (*_bfd_error_handler)
750 ("%s: symbol `%s' has unrecognized smclas %d",
751 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
752 bfd_set_error (bfd_error_bad_value);
756 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
757 csect = bfd_make_section_anyway (abfd, csect_name);
760 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
761 if (enclosing == NULL)
763 if ((bfd_vma) sym.n_value < enclosing->vma
764 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
765 > enclosing->vma + enclosing->_raw_size))
767 (*_bfd_error_handler)
768 ("%s: csect `%s' not in enclosing section",
769 bfd_get_filename (abfd), name);
770 bfd_set_error (bfd_error_bad_value);
773 csect->vma = sym.n_value;
774 csect->filepos = (enclosing->filepos
777 csect->_raw_size = aux.x_csect.x_scnlen.l;
778 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
779 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
781 /* XCOFF requires that relocs be sorted by address, so we
782 could do a binary search here. FIXME. (XCOFF
783 unfortunately does not require that symbols be sorted
784 by address, or this would be a simple merge). */
785 rel = reloc_info[enclosing->target_index].relocs;
786 used = reloc_info[enclosing->target_index].used;
788 relindx < enclosing->reloc_count;
789 relindx++, rel++, used++)
792 && rel->r_vaddr >= csect->vma
793 && rel->r_vaddr < csect->vma + csect->_raw_size)
795 csect->rel_filepos = (enclosing->rel_filepos
796 + relindx * bfd_coff_relsz (abfd));
800 while (relindx < enclosing->reloc_count
802 && rel->r_vaddr >= csect->vma
803 && rel->r_vaddr < csect->vma + csect->_raw_size)
806 csect->flags |= SEC_RELOC;
807 ++csect->reloc_count;
813 /* There are a number of other fields and section flags
814 which we do not bother to set. */
816 /* Record the enclosing section in the tdata for this new
819 ((struct coff_section_tdata *)
820 bfd_alloc (abfd, sizeof (struct coff_section_tdata)));
821 if (csect->used_by_bfd == NULL)
823 bfd_set_error (bfd_error_no_memory);
826 coff_section_data (abfd, csect)->tdata =
827 bfd_alloc (abfd, sizeof (struct xcoff_section_tdata));
828 if (coff_section_data (abfd, csect)->tdata == NULL)
830 bfd_set_error (bfd_error_no_memory);
833 xcoff_section_data (abfd, csect)->enclosing = enclosing;
836 - (bfd_byte *) obj_coff_external_syms (abfd))
839 if (first_csect == NULL)
842 /* If this symbol is C_EXT, we treat it as starting at the
843 beginning of the newly created section. */
844 if (sym.n_sclass == C_EXT)
853 /* This is a label definition. The x_scnlen field is the
854 symbol index of the csect. I believe that this must
855 always follow the appropriate XTY_SD symbol, so I will
861 if (sym.n_sclass != C_EXT
862 || aux.x_csect.x_scnlen.l < 0
863 || (aux.x_csect.x_scnlen.l
864 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
868 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
870 || (section->flags & SEC_HAS_CONTENTS) == 0)
875 (*_bfd_error_handler)
876 ("%s: misplaced XTY_LD `%s'",
877 bfd_get_filename (abfd), name);
878 bfd_set_error (bfd_error_bad_value);
882 value = sym.n_value - csect->vma;
887 /* This is an unitialized csect. We could base the name on
888 the storage mapping class, but we don't bother. If this
889 csect is externally visible, it is a common symbol. */
890 if (sym.n_sclass == C_EXT)
892 section = bfd_com_section_ptr;
893 value = aux.x_csect.x_scnlen.l;
897 csect = bfd_make_section_anyway (abfd, ".bss");
901 csect->_raw_size = aux.x_csect.x_scnlen.l;
902 csect->flags |= SEC_ALLOC;
903 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
904 /* There are a number of other fields and section flags
905 which we do not bother to set. */
908 - (bfd_byte *) obj_coff_external_syms (abfd))
911 if (first_csect == NULL)
917 /* Now we have enough information to add the symbol to the
918 linker hash table. */
920 if (sym.n_sclass == C_EXT)
924 BFD_ASSERT (section != NULL);
926 /* We must copy the name into memory if we got it from the
927 syment itself, rather than the string table. */
929 if (sym._n._n_n._n_zeroes != 0
930 || sym._n._n_n._n_offset == 0)
933 if (! (_bfd_generic_link_add_one_symbol
934 (info, abfd, name, flags, section, value,
935 (const char *) NULL, copy, false,
936 (struct bfd_link_hash_entry **) sym_hash)))
939 if (info->hash->creator == abfd->xvec)
940 (*sym_hash)->xcoff = true;
943 *csect_cache = csect;
945 esym += (sym.n_numaux + 1) * symesz;
946 sym_hash += sym.n_numaux + 1;
947 csect_cache += sym.n_numaux + 1;
948 if (debug_index != NULL)
949 debug_index += sym.n_numaux + 1;
954 /* Update the size of the .debug section we are building. */
955 if (xcoff_hash_table (info)->debug_section == NULL)
959 n = bfd_make_section_anyway (abfd, ".debug");
962 n->flags |= SEC_HAS_CONTENTS;
963 /* Note that reading from this section will fail. */
964 xcoff_hash_table (info)->debug_section = n;
966 xcoff_hash_table (info)->debug_section->_raw_size =
967 _bfd_stringtab_size (xcoff_hash_table (info)->debug_strtab);
970 /* Make sure that we have seen all the relocs. */
971 for (sub = abfd->sections; sub != first_csect; sub = sub->next)
973 /* Reset the section size, since the data is now attached to the
977 if ((sub->flags & SEC_RELOC) != 0)
981 for (i = 0; i < sub->reloc_count; i++)
983 if (! reloc_info[sub->target_index].used[i])
985 (*_bfd_error_handler)
986 ("%s: reloc %s:%d not in csect",
987 bfd_get_filename (abfd), sub->name, i);
988 bfd_set_error (bfd_error_bad_value);
993 free (reloc_info[sub->target_index].used);
994 reloc_info[sub->target_index].used = NULL;
996 /* Reset SEC_RELOC and the reloc_count, since the reloc
997 information is now attached to the csects. */
998 sub->flags &=~ SEC_RELOC;
999 sub->reloc_count = 0;
1001 /* If we are not keeping memory, free the reloc information. */
1002 if (! info->keep_memory
1003 && coff_section_data (abfd, sub) != NULL
1004 && coff_section_data (abfd, sub)->relocs != NULL)
1006 free (coff_section_data (abfd, sub)->relocs);
1007 coff_section_data (abfd, sub)->relocs = NULL;
1014 if (debug_contents != NULL)
1016 free (debug_contents);
1017 debug_contents = NULL;
1023 if (debug_contents != NULL)
1024 free (debug_contents);
1025 if (reloc_info != NULL)
1027 for (sub = abfd->sections; sub != NULL; sub = sub->next)
1029 if (reloc_info[sub->target_index].used != NULL)
1030 free (reloc_info[sub->target_index].used);
1037 /* Do the final link step. */
1040 _bfd_xcoff_bfd_final_link (abfd, info)
1042 struct bfd_link_info *info;
1044 bfd_size_type symesz;
1045 struct xcoff_final_link_info finfo;
1047 struct bfd_link_order *p;
1048 size_t max_contents_size;
1049 size_t max_sym_count;
1050 size_t max_lineno_count;
1051 size_t max_reloc_count;
1052 size_t max_output_reloc_count;
1053 file_ptr rel_filepos;
1055 file_ptr line_filepos;
1056 unsigned int linesz;
1058 bfd_byte *external_relocs = NULL;
1059 char strbuf[STRING_SIZE_SIZE];
1061 symesz = bfd_coff_symesz (abfd);
1064 finfo.output_bfd = abfd;
1065 finfo.strtab = NULL;
1066 finfo.section_info = NULL;
1067 finfo.last_file_index = -1;
1068 finfo.toc_symindx = -1;
1069 finfo.internal_syms = NULL;
1070 finfo.sym_indices = NULL;
1071 finfo.outsyms = NULL;
1072 finfo.linenos = NULL;
1073 finfo.contents = NULL;
1074 finfo.external_relocs = NULL;
1076 xcoff_data (abfd)->coff.link_info = info;
1078 finfo.strtab = _bfd_stringtab_init ();
1079 if (finfo.strtab == NULL)
1082 /* Compute the file positions for all the sections. */
1083 if (! abfd->output_has_begun)
1084 bfd_coff_compute_section_file_positions (abfd);
1086 /* Count the line numbers and relocation entries required for the
1087 output file. Set the file positions for the relocs. */
1088 rel_filepos = obj_relocbase (abfd);
1089 relsz = bfd_coff_relsz (abfd);
1090 max_contents_size = 0;
1091 max_lineno_count = 0;
1092 max_reloc_count = 0;
1093 for (o = abfd->sections; o != NULL; o = o->next)
1096 o->lineno_count = 0;
1097 for (p = o->link_order_head; p != NULL; p = p->next)
1099 if (p->type == bfd_indirect_link_order)
1103 sec = p->u.indirect.section;
1105 if (info->strip == strip_none
1106 || info->strip == strip_some)
1107 o->lineno_count += sec->lineno_count;
1109 o->reloc_count += sec->reloc_count;
1111 if (sec->_raw_size > max_contents_size)
1112 max_contents_size = sec->_raw_size;
1113 if (sec->lineno_count > max_lineno_count)
1114 max_lineno_count = sec->lineno_count;
1115 if (sec->reloc_count > max_reloc_count)
1116 max_reloc_count = sec->reloc_count;
1118 else if (p->type == bfd_section_reloc_link_order
1119 || p->type == bfd_symbol_reloc_link_order)
1122 if (o->reloc_count == 0)
1126 o->flags |= SEC_RELOC;
1127 o->rel_filepos = rel_filepos;
1128 rel_filepos += o->reloc_count * relsz;
1132 /* Allocate space for the pointers we need to keep for the relocs. */
1136 /* We use section_count + 1, rather than section_count, because
1137 the target_index fields are 1 based. */
1138 finfo.section_info = ((struct xcoff_link_section_info *)
1139 malloc ((abfd->section_count + 1)
1140 * sizeof (struct xcoff_link_section_info)));
1141 if (finfo.section_info == NULL)
1143 bfd_set_error (bfd_error_no_memory);
1146 for (i = 0; i <= abfd->section_count; i++)
1148 finfo.section_info[i].relocs = NULL;
1149 finfo.section_info[i].rel_hashes = NULL;
1153 /* We now know the size of the relocs, so we can determine the file
1154 positions of the line numbers. */
1155 line_filepos = rel_filepos;
1156 linesz = bfd_coff_linesz (abfd);
1157 max_output_reloc_count = 0;
1158 for (o = abfd->sections; o != NULL; o = o->next)
1160 if (o->lineno_count == 0)
1161 o->line_filepos = 0;
1164 o->line_filepos = line_filepos;
1165 line_filepos += o->lineno_count * linesz;
1168 if (o->reloc_count != 0)
1170 /* We don't know the indices of global symbols until we have
1171 written out all the local symbols. For each section in
1172 the output file, we keep an array of pointers to hash
1173 table entries. Each entry in the array corresponds to a
1174 reloc. When we find a reloc against a global symbol, we
1175 set the corresponding entry in this array so that we can
1176 fix up the symbol index after we have written out all the
1179 Because of this problem, we also keep the relocs in
1180 memory until the end of the link. This wastes memory.
1181 We could backpatch the file later, I suppose, although it
1183 finfo.section_info[o->target_index].relocs =
1184 ((struct internal_reloc *)
1185 malloc (o->reloc_count * sizeof (struct internal_reloc)));
1186 finfo.section_info[o->target_index].rel_hashes =
1187 ((struct xcoff_link_hash_entry **)
1188 malloc (o->reloc_count
1189 * sizeof (struct xcoff_link_hash_entry *)));
1190 if (finfo.section_info[o->target_index].relocs == NULL
1191 || finfo.section_info[o->target_index].rel_hashes == NULL)
1193 bfd_set_error (bfd_error_no_memory);
1197 if (o->reloc_count > max_output_reloc_count)
1198 max_output_reloc_count = o->reloc_count;
1201 /* Reset the reloc and lineno counts, so that we can use them to
1202 count the number of entries we have output so far. */
1204 o->lineno_count = 0;
1207 obj_sym_filepos (abfd) = line_filepos;
1209 /* Figure out the largest number of symbols in an input BFD. Take
1210 the opportunity to clear the output_has_begun fields of all the
1211 input BFD's. We want at least 4 symbols, since that is the
1212 number which xcoff_write_global_symbol may need. */
1214 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1218 sub->output_has_begun = false;
1219 sz = obj_raw_syment_count (sub);
1220 if (sz > max_sym_count)
1224 /* Allocate some buffers used while linking. */
1225 finfo.internal_syms = ((struct internal_syment *)
1226 malloc (max_sym_count
1227 * sizeof (struct internal_syment)));
1228 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
1229 finfo.outsyms = ((bfd_byte *)
1230 malloc ((size_t) ((max_sym_count + 1) * symesz)));
1231 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
1232 * bfd_coff_linesz (abfd));
1233 finfo.contents = (bfd_byte *) malloc (max_contents_size);
1234 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
1235 if ((finfo.internal_syms == NULL && max_sym_count > 0)
1236 || (finfo.sym_indices == NULL && max_sym_count > 0)
1237 || finfo.outsyms == NULL
1238 || (finfo.linenos == NULL && max_lineno_count > 0)
1239 || (finfo.contents == NULL && max_contents_size > 0)
1240 || (finfo.external_relocs == NULL && max_reloc_count > 0))
1242 bfd_set_error (bfd_error_no_memory);
1246 obj_raw_syment_count (abfd) = 0;
1247 xcoff_data (abfd)->toc = (bfd_vma) -1;
1249 /* Write out all the global symbols which do not come from XCOFF
1250 input files. In COFF these must come at the end, but in XCOFF it
1251 doesn't seem to matter. */
1252 xcoff_link_hash_traverse (xcoff_hash_table (info),
1253 xcoff_write_global_symbol,
1256 /* We now know the position of everything in the file, except that
1257 we don't know the size of the symbol table and therefore we don't
1258 know where the string table starts. We just build the string
1259 table in memory as we go along. We process all the relocations
1260 for a single input file at once. */
1261 for (o = abfd->sections; o != NULL; o = o->next)
1263 for (p = o->link_order_head; p != NULL; p = p->next)
1265 if (p->type == bfd_indirect_link_order
1266 && p->u.indirect.section->owner->xvec == abfd->xvec)
1268 sub = p->u.indirect.section->owner;
1269 if (! sub->output_has_begun)
1271 if (! xcoff_link_input_bfd (&finfo, sub))
1273 sub->output_has_begun = true;
1276 else if (p->type == bfd_section_reloc_link_order
1277 || p->type == bfd_symbol_reloc_link_order)
1279 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
1284 if (! _bfd_default_link_order (abfd, info, o, p))
1290 /* Free up the buffers used by xcoff_link_input_bfd. */
1292 if (finfo.internal_syms != NULL)
1294 free (finfo.internal_syms);
1295 finfo.internal_syms = NULL;
1297 if (finfo.sym_indices != NULL)
1299 free (finfo.sym_indices);
1300 finfo.sym_indices = NULL;
1302 if (finfo.linenos != NULL)
1304 free (finfo.linenos);
1305 finfo.linenos = NULL;
1307 if (finfo.contents != NULL)
1309 free (finfo.contents);
1310 finfo.contents = NULL;
1312 if (finfo.external_relocs != NULL)
1314 free (finfo.external_relocs);
1315 finfo.external_relocs = NULL;
1318 /* The value of the last C_FILE symbol is supposed to be -1. Write
1320 if (finfo.last_file_index != -1)
1322 finfo.last_file.n_value = -1;
1323 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
1324 (PTR) finfo.outsyms);
1326 (obj_sym_filepos (abfd)
1327 + finfo.last_file_index * symesz),
1329 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
1333 if (finfo.outsyms != NULL)
1335 free (finfo.outsyms);
1336 finfo.outsyms = NULL;
1339 /* Now that we have written out all the global symbols, we know the
1340 symbol indices to use for relocs against them, and we can finally
1341 write out the relocs. */
1342 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
1343 if (external_relocs == NULL && max_output_reloc_count != 0)
1345 bfd_set_error (bfd_error_no_memory);
1349 for (o = abfd->sections; o != NULL; o = o->next)
1351 struct internal_reloc *irel;
1352 struct internal_reloc *irelend;
1353 struct xcoff_link_hash_entry **rel_hash;
1356 if (o->reloc_count == 0)
1359 /* XCOFF requires that the relocs be sorted by address. We tend
1360 to produce them in the order in which their containing csects
1361 appear in the symbol table, which is not necessarily by
1362 address. So we sort them here. There may be a better way to
1364 qsort ((PTR) finfo.section_info[o->target_index].relocs,
1365 o->reloc_count, sizeof (struct internal_reloc),
1368 irel = finfo.section_info[o->target_index].relocs;
1369 irelend = irel + o->reloc_count;
1370 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1371 erel = external_relocs;
1372 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1374 if (*rel_hash != NULL)
1376 if ((*rel_hash)->indx < 0)
1378 if (! ((*info->callbacks->unattached_reloc)
1379 (info, (*rel_hash)->root.root.string, abfd, o,
1382 (*rel_hash)->indx = 0;
1384 irel->r_symndx = (*rel_hash)->indx;
1386 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
1389 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
1390 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
1391 abfd) != relsz * o->reloc_count)
1395 if (external_relocs != NULL)
1397 free (external_relocs);
1398 external_relocs = NULL;
1401 /* Free up the section information. */
1402 if (finfo.section_info != NULL)
1406 for (i = 0; i < abfd->section_count; i++)
1408 if (finfo.section_info[i].relocs != NULL)
1409 free (finfo.section_info[i].relocs);
1410 if (finfo.section_info[i].rel_hashes != NULL)
1411 free (finfo.section_info[i].rel_hashes);
1413 free (finfo.section_info);
1414 finfo.section_info = NULL;
1417 /* Write out the string table. */
1419 (obj_sym_filepos (abfd)
1420 + obj_raw_syment_count (abfd) * symesz),
1424 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1425 (bfd_byte *) strbuf);
1426 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1428 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1431 _bfd_stringtab_free (finfo.strtab);
1433 /* Write out the debugging string table. */
1434 o = xcoff_hash_table (info)->debug_section;
1437 struct bfd_strtab_hash *debug_strtab;
1439 debug_strtab = xcoff_hash_table (info)->debug_strtab;
1440 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
1441 >= _bfd_stringtab_size (debug_strtab));
1443 o->output_section->filepos + o->output_offset,
1446 if (! _bfd_stringtab_emit (abfd, debug_strtab))
1450 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1451 not try to write out the symbols. */
1452 bfd_get_symcount (abfd) = 0;
1457 if (finfo.strtab != NULL)
1458 _bfd_stringtab_free (finfo.strtab);
1459 if (finfo.section_info != NULL)
1463 for (i = 0; i < abfd->section_count; i++)
1465 if (finfo.section_info[i].relocs != NULL)
1466 free (finfo.section_info[i].relocs);
1467 if (finfo.section_info[i].rel_hashes != NULL)
1468 free (finfo.section_info[i].rel_hashes);
1470 free (finfo.section_info);
1472 if (finfo.internal_syms != NULL)
1473 free (finfo.internal_syms);
1474 if (finfo.sym_indices != NULL)
1475 free (finfo.sym_indices);
1476 if (finfo.outsyms != NULL)
1477 free (finfo.outsyms);
1478 if (finfo.linenos != NULL)
1479 free (finfo.linenos);
1480 if (finfo.contents != NULL)
1481 free (finfo.contents);
1482 if (finfo.external_relocs != NULL)
1483 free (finfo.external_relocs);
1484 if (external_relocs != NULL)
1485 free (external_relocs);
1489 /* Link an input file into the linker output file. This function
1490 handles all the sections and relocations of the input file at once. */
1493 xcoff_link_input_bfd (finfo, input_bfd)
1494 struct xcoff_final_link_info *finfo;
1498 const char *strings;
1499 bfd_size_type syment_base;
1500 unsigned int n_tmask;
1501 unsigned int n_btshft;
1503 bfd_size_type isymesz;
1504 bfd_size_type osymesz;
1505 bfd_size_type linesz;
1508 struct internal_syment *isymp;
1510 unsigned long *debug_index;
1512 unsigned long output_index;
1514 struct xcoff_link_hash_entry **sym_hash;
1518 /* Move all the symbols to the output file. */
1520 output_bfd = finfo->output_bfd;
1522 syment_base = obj_raw_syment_count (output_bfd);
1523 isymesz = bfd_coff_symesz (input_bfd);
1524 osymesz = bfd_coff_symesz (output_bfd);
1525 linesz = bfd_coff_linesz (input_bfd);
1526 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1528 n_tmask = coff_data (input_bfd)->local_n_tmask;
1529 n_btshft = coff_data (input_bfd)->local_n_btshft;
1531 /* Define macros so that ISFCN, et. al., macros work correctly. */
1532 #define N_TMASK n_tmask
1533 #define N_BTSHFT n_btshft
1536 if (! finfo->info->keep_memory)
1539 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1542 if (! _bfd_coff_get_external_symbols (input_bfd))
1545 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1546 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1547 sym_hash = obj_xcoff_sym_hashes (input_bfd);
1548 csectpp = xcoff_data (input_bfd)->csects;
1549 debug_index = xcoff_data (input_bfd)->debug_indices;
1550 isymp = finfo->internal_syms;
1551 indexp = finfo->sym_indices;
1552 output_index = syment_base;
1553 outsym = finfo->outsyms;
1555 while (esym < esym_end)
1557 struct internal_syment isym;
1558 union internal_auxent aux;
1564 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1566 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
1568 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
1570 BFD_ASSERT (isymp->n_numaux > 0);
1571 bfd_coff_swap_aux_in (input_bfd,
1572 (PTR) (esym + isymesz * isymp->n_numaux),
1573 isymp->n_type, isymp->n_sclass,
1574 isymp->n_numaux - 1, isymp->n_numaux,
1576 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1579 /* Make a copy of *isymp so that the relocate_section function
1580 always sees the original values. This is more reliable than
1581 always recomputing the symbol value even if we are stripping
1589 add = 1 + isym.n_numaux;
1591 /* If we are skipping this csect, we want to skip this symbol. */
1592 if (*csectpp == NULL)
1595 /* An XCOFF linker always skips C_STAT symbols. */
1597 && isymp->n_sclass == C_STAT)
1600 /* We skip all but the first TOC anchor. */
1602 && isymp->n_sclass == C_HIDEXT
1603 && aux.x_csect.x_smclas == XMC_TC0)
1605 if (finfo->toc_symindx != -1)
1609 finfo->toc_symindx = output_index;
1610 xcoff_data (finfo->output_bfd)->toc =
1611 ((*csectpp)->output_section->vma
1612 + (*csectpp)->output_offset
1619 /* If we are stripping all symbols, we want to skip this one. */
1621 && finfo->info->strip == strip_all)
1624 /* We can skip resolved external references. */
1626 && isym.n_sclass == C_EXT
1628 && (*sym_hash)->root.type != bfd_link_hash_undefined)
1631 /* Skip local symbols if we are discarding them. */
1633 && finfo->info->discard == discard_all
1634 && isym.n_sclass != C_EXT
1635 && (isym.n_sclass != C_HIDEXT
1636 || smtyp != XTY_SD))
1639 /* If we stripping debugging symbols, and this is a debugging
1640 symbol, then skip it. */
1642 && finfo->info->strip == strip_debugger
1643 && isym.n_scnum == N_DEBUG)
1646 /* If some symbols are stripped based on the name, work out the
1647 name and decide whether to skip this symbol. We don't handle
1648 this correctly for symbols whose names are in the .debug
1649 section; to get it right we would need a new bfd_strtab_hash
1650 function to return the string given the index. */
1652 && (finfo->info->strip == strip_some
1653 || finfo->info->discard == discard_l)
1654 && (debug_index == NULL || *debug_index == (unsigned long) -1))
1657 char buf[SYMNMLEN + 1];
1659 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1663 if ((finfo->info->strip == strip_some
1664 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1666 || (finfo->info->discard == discard_l
1667 && (isym.n_sclass != C_EXT
1668 && (isym.n_sclass != C_HIDEXT
1669 || smtyp != XTY_SD))
1670 && strncmp (name, finfo->info->lprefix,
1671 finfo->info->lprefix_len) == 0))
1675 /* On the other hand, we can't skip global symbols which have
1676 relocs against them. FIXME: This won't always work, because
1677 we're not always outputting the global symbols at the end, as
1680 && *sym_hash != NULL
1681 && (*sym_hash)->indx == -2
1682 && finfo->info->strip != strip_all)
1685 /* We can not skip the first TOC anchor. */
1688 && finfo->info->strip != strip_all)
1691 /* We now know whether we are to skip this symbol or not. */
1694 /* Adjust the symbol in order to output it. */
1696 if (isym._n._n_n._n_zeroes == 0
1697 && isym._n._n_n._n_offset != 0)
1699 /* This symbol has a long name. Enter it in the string
1700 table we are building. If *debug_index != -1, the
1701 name has already been entered in the .debug section. */
1702 if (debug_index != NULL && *debug_index != (unsigned long) -1)
1703 isym._n._n_n._n_offset = *debug_index;
1709 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1713 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1714 if (indx == (bfd_size_type) -1)
1716 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1720 if (isym.n_sclass == C_BSTAT)
1724 /* The value of a C_BSTAT symbol is the symbol table
1725 index of the containing csect. */
1727 indx = isym.n_value;
1728 if (indx < obj_raw_syment_count (input_bfd))
1732 symindx = finfo->sym_indices[indx];
1736 isym.n_value = symindx;
1739 else if (isym.n_scnum > 0)
1741 isym.n_scnum = (*csectpp)->output_section->target_index;
1742 isym.n_value += ((*csectpp)->output_section->vma
1743 + (*csectpp)->output_offset
1747 /* The value of a C_FILE symbol is the symbol index of the
1748 next C_FILE symbol. The value of the last C_FILE symbol
1749 is -1. We try to get this right, below, just before we
1750 write the symbols out, but in the general case we may
1751 have to write the symbol out twice. */
1752 if (isym.n_sclass == C_FILE)
1754 if (finfo->last_file_index != -1
1755 && finfo->last_file.n_value != (long) output_index)
1757 /* We must correct the value of the last C_FILE entry. */
1758 finfo->last_file.n_value = output_index;
1759 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1761 /* The last C_FILE symbol is in this input file. */
1762 bfd_coff_swap_sym_out (output_bfd,
1763 (PTR) &finfo->last_file,
1764 (PTR) (finfo->outsyms
1765 + ((finfo->last_file_index
1771 /* We have already written out the last C_FILE
1772 symbol. We need to write it out again. We
1773 borrow *outsym temporarily. */
1774 bfd_coff_swap_sym_out (output_bfd,
1775 (PTR) &finfo->last_file,
1777 if (bfd_seek (output_bfd,
1778 (obj_sym_filepos (output_bfd)
1779 + finfo->last_file_index * osymesz),
1781 || (bfd_write (outsym, osymesz, 1, output_bfd)
1787 finfo->last_file_index = output_index;
1788 finfo->last_file = isym;
1791 /* Output the symbol. */
1793 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1795 *indexp = output_index;
1797 if (isym.n_sclass == C_EXT)
1800 struct xcoff_link_hash_entry *h;
1802 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1804 h = obj_xcoff_sym_hashes (input_bfd)[indx];
1805 BFD_ASSERT (h != NULL);
1806 h->indx = output_index;
1809 output_index += add;
1810 outsym += add * osymesz;
1813 esym += add * isymesz;
1817 if (debug_index != NULL)
1820 for (--add; add > 0; --add)
1824 /* Fix up the aux entries. This must be done in a separate pass,
1825 because we don't know the correct symbol indices until we have
1826 already decided which symbols we are going to keep. */
1828 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1829 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1830 isymp = finfo->internal_syms;
1831 indexp = finfo->sym_indices;
1832 outsym = finfo->outsyms;
1833 while (esym < esym_end)
1837 add = 1 + isymp->n_numaux;
1840 esym += add * isymesz;
1848 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1850 union internal_auxent aux;
1852 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1853 isymp->n_sclass, i, isymp->n_numaux,
1856 if (isymp->n_sclass == C_FILE)
1858 /* This is the file name (or some comment put in by
1859 the compiler). If it is long, we must put it in
1860 the string table. */
1861 if (aux.x_file.x_n.x_zeroes == 0
1862 && aux.x_file.x_n.x_offset != 0)
1864 const char *filename;
1867 BFD_ASSERT (aux.x_file.x_n.x_offset
1868 >= STRING_SIZE_SIZE);
1869 if (strings == NULL)
1871 strings = _bfd_coff_read_string_table (input_bfd);
1872 if (strings == NULL)
1875 filename = strings + aux.x_file.x_n.x_offset;
1876 indx = _bfd_stringtab_add (finfo->strtab, filename,
1878 if (indx == (bfd_size_type) -1)
1880 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1883 else if ((isymp->n_sclass == C_EXT
1884 || isymp->n_sclass == C_HIDEXT)
1885 && i + 1 == isymp->n_numaux)
1887 /* We don't support type checking. I don't know if
1889 aux.x_csect.x_parmhash = 0;
1890 /* I don't think anybody uses these fields, but we'd
1891 better clobber them just in case. */
1892 aux.x_csect.x_stab = 0;
1893 aux.x_csect.x_snstab = 0;
1894 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
1898 indx = aux.x_csect.x_scnlen.l;
1899 if (indx < obj_raw_syment_count (input_bfd))
1903 symindx = finfo->sym_indices[indx];
1905 aux.x_sym.x_tagndx.l = 0;
1907 aux.x_sym.x_tagndx.l = symindx;
1911 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1915 if (ISFCN (isymp->n_type)
1916 || ISTAG (isymp->n_sclass)
1917 || isymp->n_sclass == C_BLOCK)
1919 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
1921 && indx < obj_raw_syment_count (input_bfd))
1923 /* We look forward through the symbol for
1924 the index of the next symbol we are going
1925 to include. I don't know if this is
1927 while (finfo->sym_indices[indx] < 0
1928 && indx < obj_raw_syment_count (input_bfd))
1930 if (indx >= obj_raw_syment_count (input_bfd))
1931 indx = output_index;
1933 indx = finfo->sym_indices[indx];
1934 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1938 indx = aux.x_sym.x_tagndx.l;
1939 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1943 symindx = finfo->sym_indices[indx];
1945 aux.x_sym.x_tagndx.l = 0;
1947 aux.x_sym.x_tagndx.l = symindx;
1951 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
1952 isymp->n_sclass, i, isymp->n_numaux,
1963 /* Relocate the line numbers, unless we are stripping them. */
1964 if (finfo->info->strip == strip_none
1965 || finfo->info->strip == strip_some)
1967 for (o = input_bfd->sections; o != NULL; o = o->next)
1973 /* FIXME: If SEC_HAS_CONTENTS is not set for the section,
1974 then build_link_order in ldwrite.c will not have created
1975 a link order, which means that we will not have seen this
1976 input section in _bfd_xcoff_final_link, which means that
1977 we will not have allocated space for the line numbers of
1978 this section. I don't think line numbers can be
1979 meaningful for a section which does not have
1980 SEC_HAS_CONTENTS set, but, if they do, this must be
1982 if (o->lineno_count == 0
1983 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
1986 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1987 || bfd_read (finfo->linenos, linesz, o->lineno_count,
1988 input_bfd) != linesz * o->lineno_count)
1991 offset = o->output_section->vma + o->output_offset - o->vma;
1992 eline = finfo->linenos;
1993 elineend = eline + linesz * o->lineno_count;
1994 for (; eline < elineend; eline += linesz)
1996 struct internal_lineno iline;
1998 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2000 if (iline.l_lnno != 0)
2001 iline.l_addr.l_paddr += offset;
2002 else if (iline.l_addr.l_symndx >= 0
2003 && ((unsigned long) iline.l_addr.l_symndx
2004 < obj_raw_syment_count (input_bfd)))
2008 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2012 /* These line numbers are attached to a symbol
2013 which we are stripping. We should really
2014 just discard the line numbers, but that would
2015 be a pain because we have already counted
2021 struct internal_syment is;
2022 union internal_auxent ia;
2024 /* Fix up the lnnoptr field in the aux entry of
2025 the symbol. It turns out that we can't do
2026 this when we modify the symbol aux entries,
2027 because gas sometimes screws up the lnnoptr
2028 field and makes it an offset from the start
2029 of the line numbers rather than an absolute
2031 bfd_coff_swap_sym_in (output_bfd,
2032 (PTR) (finfo->outsyms
2033 + ((indx - syment_base)
2036 if ((ISFCN (is.n_type)
2037 || is.n_sclass == C_BLOCK)
2038 && ((is.n_sclass == C_EXT
2039 || is.n_sclass == C_HIDEXT)
2041 : is.n_numaux >= 1))
2045 auxptr = (PTR) (finfo->outsyms
2046 + ((indx - syment_base + 1)
2048 bfd_coff_swap_aux_in (output_bfd, auxptr,
2049 is.n_type, is.n_sclass,
2050 0, is.n_numaux, (PTR) &ia);
2051 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2052 (o->output_section->line_filepos
2053 + o->output_section->lineno_count * linesz
2054 + eline - finfo->linenos);
2055 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2056 is.n_type, is.n_sclass, 0,
2057 is.n_numaux, auxptr);
2061 iline.l_addr.l_symndx = indx;
2064 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
2067 if (bfd_seek (output_bfd,
2068 (o->output_section->line_filepos
2069 + o->output_section->lineno_count * linesz),
2071 || bfd_write (finfo->linenos, linesz, o->lineno_count,
2072 output_bfd) != linesz * o->lineno_count)
2075 o->output_section->lineno_count += o->lineno_count;
2079 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2080 symbol will be the first symbol in the next input file. In the
2081 normal case, this will save us from writing out the C_FILE symbol
2083 if (finfo->last_file_index != -1
2084 && (bfd_size_type) finfo->last_file_index >= syment_base)
2086 finfo->last_file.n_value = output_index;
2087 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2088 (PTR) (finfo->outsyms
2089 + ((finfo->last_file_index - syment_base)
2093 /* Write the modified symbols to the output file. */
2094 if (outsym > finfo->outsyms)
2096 if (bfd_seek (output_bfd,
2097 obj_sym_filepos (output_bfd) + syment_base * osymesz,
2099 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
2101 != (bfd_size_type) (outsym - finfo->outsyms)))
2104 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2105 + (outsym - finfo->outsyms) / osymesz)
2108 obj_raw_syment_count (output_bfd) = output_index;
2111 /* Don't let the linker relocation routines discard the symbols. */
2112 keep_syms = obj_coff_keep_syms (input_bfd);
2113 obj_coff_keep_syms (input_bfd) = true;
2115 /* Relocate the contents of each section. */
2116 for (o = input_bfd->sections; o != NULL; o = o->next)
2120 if ((o->flags & SEC_HAS_CONTENTS) == 0
2121 || o->_raw_size == 0)
2124 /* We have set filepos correctly for the sections we created to
2125 represent csects, so bfd_get_section_contents should work. */
2126 if (coff_section_data (input_bfd, o) != NULL
2127 && coff_section_data (input_bfd, o)->contents != NULL)
2128 contents = coff_section_data (input_bfd, o)->contents;
2131 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2132 (file_ptr) 0, o->_raw_size))
2134 contents = finfo->contents;
2137 if ((o->flags & SEC_RELOC) != 0)
2140 struct internal_reloc *internal_relocs;
2141 struct internal_reloc *irel;
2143 struct internal_reloc *irelend;
2144 struct xcoff_link_hash_entry **rel_hash;
2146 /* Read in the relocs. We have set rel_filepos correctly
2147 for the sections we created to represent csects, so this
2149 target_index = o->output_section->target_index;
2150 internal_relocs = (_bfd_coff_read_internal_relocs
2151 (input_bfd, o, false, finfo->external_relocs,
2153 (finfo->section_info[target_index].relocs
2154 + o->output_section->reloc_count)));
2155 if (internal_relocs == NULL)
2158 /* Call processor specific code to relocate the section
2160 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2164 finfo->internal_syms,
2165 xcoff_data (input_bfd)->csects))
2168 offset = o->output_section->vma + o->output_offset - o->vma;
2169 irel = internal_relocs;
2170 irelend = irel + o->reloc_count;
2171 rel_hash = (finfo->section_info[target_index].rel_hashes
2172 + o->output_section->reloc_count);
2173 for (; irel < irelend; irel++, rel_hash++)
2175 struct xcoff_link_hash_entry *h;
2179 /* Adjust the reloc address and symbol index. */
2181 irel->r_vaddr += offset;
2183 if (irel->r_symndx == -1)
2186 h = obj_xcoff_sym_hashes (input_bfd)[irel->r_symndx];
2189 /* This is a global symbol. */
2191 irel->r_symndx = h->indx;
2194 /* This symbol is being written at the end of
2195 the file, and we do not yet know the symbol
2196 index. We save the pointer to the hash table
2197 entry in the rel_hash list. We set the indx
2198 field to -2 to indicate that this symbol must
2208 indx = finfo->sym_indices[irel->r_symndx];
2212 struct internal_syment *is;
2214 /* Relocations against a TC0 TOC anchor are
2215 automatically transformed to be against the
2216 TOC anchor in the output file. */
2217 is = finfo->internal_syms + irel->r_symndx;
2218 if (is->n_sclass == C_HIDEXT
2219 && is->n_numaux > 0)
2222 union internal_auxent aux;
2224 auxptr = (PTR) (((bfd_byte *)
2225 obj_coff_external_syms (input_bfd))
2226 + ((irel->r_symndx + is->n_numaux)
2228 bfd_coff_swap_aux_in (input_bfd, auxptr,
2229 is->n_type, is->n_sclass,
2233 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
2234 && aux.x_csect.x_smclas == XMC_TC0)
2235 indx = finfo->toc_symindx;
2240 irel->r_symndx = indx;
2243 struct internal_syment *is;
2245 char buf[SYMNMLEN + 1];
2247 /* This reloc is against a symbol we are
2248 stripping. It would be possible to handle
2249 this case, but I don't think it's worth it. */
2250 is = finfo->internal_syms + irel->r_symndx;
2252 name = (_bfd_coff_internal_syment_name
2253 (input_bfd, is, buf));
2257 if (! ((*finfo->info->callbacks->unattached_reloc)
2258 (finfo->info, name, input_bfd, o,
2265 o->output_section->reloc_count += o->reloc_count;
2268 /* Write out the modified section contents. */
2269 if (! bfd_set_section_contents (output_bfd, o->output_section,
2270 contents, o->output_offset,
2271 (o->_cooked_size != 0
2277 obj_coff_keep_syms (input_bfd) = keep_syms;
2279 if (! finfo->info->keep_memory)
2281 if (! _bfd_coff_free_symbols (input_bfd))
2288 /* Write out a non-XCOFF global symbol. */
2291 xcoff_write_global_symbol (h, p)
2292 struct xcoff_link_hash_entry *h;
2295 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
2298 struct internal_syment isym;
2299 union internal_auxent aux;
2301 /* XCOFF global symbols will be written out in xcoff_link_input_bfd.
2302 The only symbols we need to handle here are those from non-XCOFF
2303 files and from the linker script. */
2307 output_bfd = finfo->output_bfd;
2308 outsym = finfo->outsyms;
2310 memset (&aux, 0, sizeof aux);
2312 h->indx = obj_raw_syment_count (output_bfd);
2314 if (strlen (h->root.root.string) <= SYMNMLEN)
2315 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2322 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2324 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2326 if (indx == (bfd_size_type) -1)
2328 isym._n._n_n._n_zeroes = 0;
2329 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2332 if (h->root.type == bfd_link_hash_undefined
2333 || h->root.type == bfd_link_hash_undefweak)
2336 isym.n_scnum = N_UNDEF;
2337 isym.n_sclass = C_EXT;
2338 aux.x_csect.x_smtyp = XTY_ER;
2340 else if (h->root.type == bfd_link_hash_defined
2341 || h->root.type == bfd_link_hash_defweak)
2343 isym.n_value = (h->root.u.def.section->output_section->vma
2344 + h->root.u.def.section->output_offset
2345 + h->root.u.def.value);
2346 isym.n_scnum = h->root.u.def.section->output_section->target_index;
2347 isym.n_sclass = C_HIDEXT;
2348 aux.x_csect.x_smtyp = XTY_SD;
2349 /* I don't know what the csect length should be in this case. */
2354 isym.n_type = T_NULL;
2357 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
2358 outsym += bfd_coff_symesz (output_bfd);
2360 aux.x_csect.x_smclas = XMC_UA;
2362 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
2364 outsym += bfd_coff_auxesz (output_bfd);
2366 if ((h->root.type == bfd_link_hash_defined
2367 || h->root.type == bfd_link_hash_defweak)
2368 && aux.x_csect.x_smclas != XMC_TC0)
2370 /* We just output an SD symbol. Now output an LD symbol. */
2374 isym.n_sclass = C_EXT;
2375 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
2376 outsym += bfd_coff_symesz (output_bfd);
2378 aux.x_csect.x_smtyp = XTY_LD;
2379 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
2381 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
2383 outsym += bfd_coff_auxesz (output_bfd);
2386 if (bfd_seek (output_bfd,
2387 (obj_sym_filepos (output_bfd)
2388 + (obj_raw_syment_count (output_bfd)
2389 * bfd_coff_symesz (output_bfd))),
2391 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
2392 != (bfd_size_type) (outsym - finfo->outsyms)))
2394 obj_raw_syment_count (output_bfd) +=
2395 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
2400 /* Handle a link order which is supposed to generate a reloc. */
2403 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2405 struct xcoff_final_link_info *finfo;
2406 asection *output_section;
2407 struct bfd_link_order *link_order;
2409 reloc_howto_type *howto;
2410 struct internal_reloc *irel;
2411 struct xcoff_link_hash_entry **rel_hash_ptr;
2413 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2416 bfd_set_error (bfd_error_bad_value);
2420 if (link_order->u.reloc.p->addend != 0)
2424 bfd_reloc_status_type rstat;
2427 size = bfd_get_reloc_size (howto);
2428 buf = (bfd_byte *) bfd_zmalloc (size);
2431 bfd_set_error (bfd_error_no_memory);
2435 rstat = _bfd_relocate_contents (howto, output_bfd,
2436 link_order->u.reloc.p->addend, buf);
2442 case bfd_reloc_outofrange:
2444 case bfd_reloc_overflow:
2445 if (! ((*finfo->info->callbacks->reloc_overflow)
2447 (link_order->type == bfd_section_reloc_link_order
2448 ? bfd_section_name (output_bfd,
2449 link_order->u.reloc.p->u.section)
2450 : link_order->u.reloc.p->u.name),
2451 howto->name, link_order->u.reloc.p->addend,
2452 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2459 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2460 (file_ptr) link_order->offset, size);
2466 /* Store the reloc information in the right place. It will get
2467 swapped and written out at the end of the final_link routine. */
2469 irel = (finfo->section_info[output_section->target_index].relocs
2470 + output_section->reloc_count);
2471 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2472 + output_section->reloc_count);
2474 memset (irel, 0, sizeof (struct internal_reloc));
2475 *rel_hash_ptr = NULL;
2477 irel->r_vaddr = output_section->vma + link_order->offset;
2479 if (link_order->type == bfd_section_reloc_link_order)
2481 /* We need to somehow locate a symbol in the right section. The
2482 symbol must either have a value of zero, or we must adjust
2483 the addend by the value of the symbol. FIXME: Write this
2484 when we need it. The old linker couldn't handle this anyhow. */
2486 *rel_hash_ptr = NULL;
2491 struct xcoff_link_hash_entry *h;
2493 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
2494 link_order->u.reloc.p->u.name,
2495 false, false, true);
2499 irel->r_symndx = h->indx;
2502 /* Set the index to -2 to force this symbol to get
2511 if (! ((*finfo->info->callbacks->unattached_reloc)
2512 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2513 (asection *) NULL, (bfd_vma) 0)))
2519 irel->r_type = howto->type;
2520 irel->r_size = howto->bitsize - 1;
2521 if (howto->complain_on_overflow == complain_overflow_signed)
2522 irel->r_size |= 0x80;
2524 ++output_section->reloc_count;
2529 /* Sort relocs by VMA. This is called via qsort. */
2532 xcoff_sort_relocs (p1, p2)
2536 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
2537 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
2539 if (r1->r_vaddr > r2->r_vaddr)
2541 else if (r1->r_vaddr < r2->r_vaddr)
2547 /* This is the relocation function for the RS/6000/POWER/PowerPC.
2548 This is currently the only processor which uses XCOFF; I hope that
2549 will never change. */
2552 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
2553 input_section, contents, relocs, syms,
2556 struct bfd_link_info *info;
2558 asection *input_section;
2560 struct internal_reloc *relocs;
2561 struct internal_syment *syms;
2562 asection **sections;
2564 struct internal_reloc *rel;
2565 struct internal_reloc *relend;
2568 relend = rel + input_section->reloc_count;
2569 for (; rel < relend; rel++)
2572 struct xcoff_link_hash_entry *h;
2573 struct internal_syment *sym;
2576 struct reloc_howto_struct howto;
2577 bfd_reloc_status_type rstat;
2579 /* Relocation type 0x0f is a special relocation type which is
2580 merely used to prevent garbage collection from occurring for
2581 the csect including the symbol which it references. */
2582 if (rel->r_type == 0x0f)
2585 symndx = rel->r_symndx;
2595 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
2596 sym = syms + symndx;
2597 addend = - sym->n_value;
2600 /* We build the howto information on the fly. */
2602 howto.type = rel->r_type;
2603 howto.rightshift = 0;
2605 howto.bitsize = (rel->r_size & 0x1f) + 1;
2606 howto.pc_relative = false;
2608 if ((rel->r_size & 0x80) != 0)
2609 howto.complain_on_overflow = complain_overflow_signed;
2611 howto.complain_on_overflow = complain_overflow_bitfield;
2612 howto.special_function = NULL;
2613 howto.name = "internal";
2614 howto.partial_inplace = true;
2615 if (howto.bitsize == 32)
2616 howto.src_mask = howto.dst_mask = 0xffffffff;
2619 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
2620 if (howto.bitsize == 16)
2623 howto.pcrel_offset = false;
2633 sec = bfd_abs_section_ptr;
2638 sec = sections[symndx];
2639 val = (sec->output_section->vma
2640 + sec->output_offset
2647 if (h->root.type == bfd_link_hash_defined
2648 || h->root.type == bfd_link_hash_defweak)
2652 sec = h->root.u.def.section;
2653 val = (h->root.u.def.value
2654 + sec->output_section->vma
2655 + sec->output_offset);
2657 else if (! info->relocateable)
2659 if (! ((*info->callbacks->undefined_symbol)
2660 (info, h->root.root.string, input_bfd, input_section,
2661 rel->r_vaddr - input_section->vma)))
2666 /* I took the relocation type definitions from two documents:
2667 the PowerPC AIX Version 4 Application Binary Interface, First
2668 Edition (April 1992), and the PowerOpen ABI, Big-Endian
2669 32-Bit Hardware Implementation (June 30, 1994). Differences
2670 between the documents are noted below. */
2672 switch (rel->r_type)
2677 /* These relocs are defined by the PowerPC ABI to be
2678 relative branches which use half of the difference
2679 between the symbol and the program counter. I can't
2680 quite figure out when this is useful. These relocs are
2681 not defined by the PowerOpen ABI. */
2683 (*_bfd_error_handler)
2684 ("%s: unsupported relocation type 0x%02x",
2685 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
2686 bfd_set_error (bfd_error_bad_value);
2689 /* Simple positive relocation. */
2692 /* Simple negative relocation. */
2696 /* Simple PC relative relocation. */
2697 howto.pc_relative = true;
2700 /* TOC relative relocation. The value in the instruction in
2701 the input file is the offset from the input file TOC to
2702 the desired location. We want the offset from the final
2703 TOC to the desired location. We have:
2708 so we must change insn by on - in.
2711 /* Global linkage relocation. The value of this relocation
2712 is the address of the entry in the TOC section. */
2714 /* Local object TOC address. I can't figure out the
2715 difference between this and case 0x05. */
2717 /* TOC relative relocation. A TOC relative load instruction
2718 which may be changed to a load address instruction.
2719 FIXME: We don't currently implement this optimization. */
2721 /* TOC relative relocation. This is a TOC relative load
2722 address instruction which may be changed to a load
2723 instruction. FIXME: I don't know if this is the correct
2725 val = ((val - xcoff_data (output_bfd)->toc)
2726 - (sym->n_value - xcoff_data (input_bfd)->toc));
2730 /* Absolute branch. We don't want to mess with the lower
2731 two bits of the instruction. */
2733 /* The PowerPC ABI defines this as an absolute call which
2734 may be modified to become a relative call. The PowerOpen
2735 ABI does not define this relocation type. */
2737 /* Absolute branch which may be modified to become a
2740 /* The PowerPC ABI defines this as an absolute branch to a
2741 fixed address which may be modified to an absolute branch
2742 to a symbol. The PowerOpen ABI does not define this
2745 /* The PowerPC ABI defines this as an absolute branch to a
2746 fixed address which may be modified to a relative branch.
2747 The PowerOpen ABI does not define this relocation type. */
2748 howto.src_mask &= ~3;
2749 howto.dst_mask = howto.src_mask;
2752 /* Relative branch. We don't want to mess with the lower
2753 two bits of the instruction. */
2755 /* The PowerPC ABI defines this as a relative call which may
2756 be modified to become an absolute call. The PowerOpen
2757 ABI does not define this relocation type. */
2759 /* A relative branch which may be modified to become an
2760 absolute branch. FIXME: We don't implement this,
2761 although we should for symbols of storage mapping class
2763 howto.pc_relative = true;
2764 howto.src_mask &= ~3;
2765 howto.dst_mask = howto.src_mask;
2768 /* The PowerPC AIX ABI describes this as a load which may be
2769 changed to a load address. The PowerOpen ABI says this
2770 is the same as case 0x00. */
2773 /* The PowerPC AIX ABI describes this as a load address
2774 which may be changed to a load. The PowerOpen ABI says
2775 this is the same as case 0x00. */
2779 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
2781 rel->r_vaddr - input_section->vma,
2790 case bfd_reloc_overflow:
2793 char buf[SYMNMLEN + 1];
2794 char howto_name[10];
2799 name = h->root.root.string;
2802 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2806 sprintf (howto_name, "0x%02x", rel->r_type);
2808 if (! ((*info->callbacks->reloc_overflow)
2809 (info, name, howto_name, (bfd_vma) 0, input_bfd,
2810 input_section, rel->r_vaddr - input_section->vma)))