* cgen-ibld.in (extract_normal): Match type of VALUE and MASK
[binutils.git] / bfd / xcofflink.c
blob4e25c595d453e4f617cb328942e1dda0634f06cc
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
28 #include "libcoff.h"
29 #include "libxcoff.h"
31 /* This file holds the XCOFF linker code. */
33 #define STRING_SIZE_SIZE (4)
35 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
36 This flag will only be used on input sections. */
38 #define SEC_MARK (SEC_ROM)
40 /* The list of import files. */
42 struct xcoff_import_file {
43 /* The next entry in the list. */
44 struct xcoff_import_file *next;
45 /* The path. */
46 const char *path;
47 /* The file name. */
48 const char *file;
49 /* The member name. */
50 const char *member;
53 /* Information we keep for each section in the output file during the
54 final link phase. */
56 struct xcoff_link_section_info {
57 /* The relocs to be output. */
58 struct internal_reloc *relocs;
59 /* For each reloc against a global symbol whose index was not known
60 when the reloc was handled, the global hash table entry. */
61 struct xcoff_link_hash_entry **rel_hashes;
62 /* If there is a TOC relative reloc against a global symbol, and the
63 index of the TOC symbol is not known when the reloc was handled,
64 an entry is added to this linked list. This is not an array,
65 like rel_hashes, because this case is quite uncommon. */
66 struct xcoff_toc_rel_hash {
67 struct xcoff_toc_rel_hash *next;
68 struct xcoff_link_hash_entry *h;
69 struct internal_reloc *rel;
70 } *toc_rel_hashes;
73 /* Information that we pass around while doing the final link step. */
75 struct xcoff_final_link_info {
76 /* General link information. */
77 struct bfd_link_info *info;
78 /* Output BFD. */
79 bfd *output_bfd;
80 /* Hash table for long symbol names. */
81 struct bfd_strtab_hash *strtab;
82 /* Array of information kept for each output section, indexed by the
83 target_index field. */
84 struct xcoff_link_section_info *section_info;
85 /* Symbol index of last C_FILE symbol (-1 if none). */
86 long last_file_index;
87 /* Contents of last C_FILE symbol. */
88 struct internal_syment last_file;
89 /* Symbol index of TOC symbol. */
90 long toc_symindx;
91 /* Start of .loader symbols. */
92 bfd_byte *ldsym;
93 /* Next .loader reloc to swap out. */
94 bfd_byte *ldrel;
95 /* File position of start of line numbers. */
96 file_ptr line_filepos;
97 /* Buffer large enough to hold swapped symbols of any input file. */
98 struct internal_syment *internal_syms;
99 /* Buffer large enough to hold output indices of symbols of any
100 input file. */
101 long *sym_indices;
102 /* Buffer large enough to hold output symbols for any input file. */
103 bfd_byte *outsyms;
104 /* Buffer large enough to hold external line numbers for any input
105 section. */
106 bfd_byte *linenos;
107 /* Buffer large enough to hold any input section. */
108 bfd_byte *contents;
109 /* Buffer large enough to hold external relocs of any input section. */
110 bfd_byte *external_relocs;
113 static struct bfd_hash_entry *xcoff_link_hash_newfunc
114 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
115 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
116 static struct internal_reloc *xcoff_read_internal_relocs
117 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
118 struct internal_reloc *));
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_check_dynamic_ar_symbols
126 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
127 static bfd_size_type xcoff_find_reloc
128 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
129 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
130 static boolean xcoff_link_add_dynamic_symbols
131 PARAMS ((bfd *, struct bfd_link_info *));
132 static boolean xcoff_mark_symbol
133 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
134 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
135 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
136 static boolean xcoff_build_ldsyms
137 PARAMS ((struct xcoff_link_hash_entry *, PTR));
138 static boolean xcoff_link_input_bfd
139 PARAMS ((struct xcoff_final_link_info *, bfd *));
140 static boolean xcoff_write_global_symbol
141 PARAMS ((struct xcoff_link_hash_entry *, PTR));
142 static boolean xcoff_reloc_link_order
143 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
144 struct bfd_link_order *));
145 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
148 /* Routines to read XCOFF dynamic information. This don't really
149 belong here, but we already have the ldsym manipulation routines
150 here. */
152 /* Read the contents of a section. */
154 static boolean
155 xcoff_get_section_contents (abfd, sec)
156 bfd *abfd;
157 asection *sec;
160 if (coff_section_data (abfd, sec) == NULL)
162 sec->used_by_bfd = bfd_zalloc (abfd,
163 sizeof (struct coff_section_tdata));
164 if (sec->used_by_bfd == NULL)
165 return false;
168 if (coff_section_data (abfd, sec)->contents == NULL)
170 coff_section_data (abfd, sec)->contents =
171 (bfd_byte *) bfd_malloc (sec->_raw_size);
172 if (coff_section_data (abfd, sec)->contents == NULL)
173 return false;
175 if (! bfd_get_section_contents (abfd, sec,
176 coff_section_data (abfd, sec)->contents,
177 (file_ptr) 0, sec->_raw_size))
178 return false;
181 return true;
184 /* Get the size required to hold the dynamic symbols. */
186 long
187 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
188 bfd *abfd;
190 asection *lsec;
191 bfd_byte *contents;
192 struct internal_ldhdr ldhdr;
194 if ((abfd->flags & DYNAMIC) == 0)
196 bfd_set_error (bfd_error_invalid_operation);
197 return -1;
200 lsec = bfd_get_section_by_name (abfd, ".loader");
201 if (lsec == NULL)
203 bfd_set_error (bfd_error_no_symbols);
204 return -1;
207 if (! xcoff_get_section_contents (abfd, lsec))
208 return -1;
209 contents = coff_section_data (abfd, lsec)->contents;
211 bfd_xcoff_swap_ldhdr_in (abfd, (PTR) contents, &ldhdr);
213 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
216 /* Get the dynamic symbols. */
218 long
219 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
220 bfd *abfd;
221 asymbol **psyms;
223 asection *lsec;
224 bfd_byte *contents;
225 struct internal_ldhdr ldhdr;
226 const char *strings;
227 bfd_byte *elsym, *elsymend;
228 coff_symbol_type *symbuf;
230 if ((abfd->flags & DYNAMIC) == 0)
232 bfd_set_error (bfd_error_invalid_operation);
233 return -1;
236 lsec = bfd_get_section_by_name (abfd, ".loader");
237 if (lsec == NULL)
239 bfd_set_error (bfd_error_no_symbols);
240 return -1;
243 if (! xcoff_get_section_contents (abfd, lsec))
244 return -1;
245 contents = coff_section_data (abfd, lsec)->contents;
247 coff_section_data (abfd, lsec)->keep_contents = true;
249 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
251 strings = (char *) contents + ldhdr.l_stoff;
253 symbuf = ((coff_symbol_type *)
254 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
255 if (symbuf == NULL)
256 return -1;
258 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
260 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
261 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
263 struct internal_ldsym ldsym;
265 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
267 symbuf->symbol.the_bfd = abfd;
269 if (ldsym._l._l_l._l_zeroes == 0)
270 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
271 else
273 char *c;
275 c = bfd_alloc (abfd, SYMNMLEN + 1);
276 if (c == NULL)
277 return -1;
278 memcpy (c, ldsym._l._l_name, SYMNMLEN);
279 c[SYMNMLEN] = '\0';
280 symbuf->symbol.name = c;
283 if (ldsym.l_smclas == XMC_XO)
284 symbuf->symbol.section = bfd_abs_section_ptr;
285 else
286 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
287 ldsym.l_scnum);
288 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
290 symbuf->symbol.flags = BSF_NO_FLAGS;
291 if ((ldsym.l_smtype & L_EXPORT) != 0)
292 symbuf->symbol.flags |= BSF_GLOBAL;
294 /* FIXME: We have no way to record the other information stored
295 with the loader symbol. */
297 *psyms = (asymbol *) symbuf;
300 *psyms = NULL;
302 return ldhdr.l_nsyms;
305 /* Get the size required to hold the dynamic relocs. */
307 long
308 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
309 bfd *abfd;
311 asection *lsec;
312 bfd_byte *contents;
313 struct internal_ldhdr ldhdr;
315 if ((abfd->flags & DYNAMIC) == 0)
317 bfd_set_error (bfd_error_invalid_operation);
318 return -1;
321 lsec = bfd_get_section_by_name (abfd, ".loader");
322 if (lsec == NULL)
324 bfd_set_error (bfd_error_no_symbols);
325 return -1;
328 if (! xcoff_get_section_contents (abfd, lsec))
329 return -1;
330 contents = coff_section_data (abfd, lsec)->contents;
332 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
334 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
337 /* Get the dynamic relocs. */
339 long
340 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
341 bfd *abfd;
342 arelent **prelocs;
343 asymbol **syms;
345 asection *lsec;
346 bfd_byte *contents;
347 struct internal_ldhdr ldhdr;
348 arelent *relbuf;
349 bfd_byte *elrel, *elrelend;
351 if ((abfd->flags & DYNAMIC) == 0)
353 bfd_set_error (bfd_error_invalid_operation);
354 return -1;
357 lsec = bfd_get_section_by_name (abfd, ".loader");
358 if (lsec == NULL)
360 bfd_set_error (bfd_error_no_symbols);
361 return -1;
364 if (! xcoff_get_section_contents (abfd, lsec))
365 return -1;
366 contents = coff_section_data (abfd, lsec)->contents;
368 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
370 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
371 if (relbuf == NULL)
372 return -1;
374 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
376 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
377 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
378 prelocs++)
380 struct internal_ldrel ldrel;
382 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
384 if (ldrel.l_symndx >= 3)
385 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
386 else
388 const char *name;
389 asection *sec;
391 switch (ldrel.l_symndx)
393 case 0:
394 name = ".text";
395 break;
396 case 1:
397 name = ".data";
398 break;
399 case 2:
400 name = ".bss";
401 break;
402 default:
403 abort ();
404 break;
407 sec = bfd_get_section_by_name (abfd, name);
408 if (sec == NULL)
410 bfd_set_error (bfd_error_bad_value);
411 return -1;
414 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
417 relbuf->address = ldrel.l_vaddr;
418 relbuf->addend = 0;
420 /* Most dynamic relocs have the same type. FIXME: This is only
421 correct if ldrel.l_rtype == 0. In other cases, we should use
422 a different howto. */
423 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
425 /* FIXME: We have no way to record the l_rsecnm field. */
427 *prelocs = relbuf;
430 *prelocs = NULL;
432 return ldhdr.l_nreloc;
435 /* Routine to create an entry in an XCOFF link hash table. */
437 static struct bfd_hash_entry *
438 xcoff_link_hash_newfunc (entry, table, string)
439 struct bfd_hash_entry *entry;
440 struct bfd_hash_table *table;
441 const char *string;
443 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
445 /* Allocate the structure if it has not already been allocated by a
446 subclass. */
447 if (ret == (struct xcoff_link_hash_entry *) NULL)
448 ret = ((struct xcoff_link_hash_entry *)
449 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
450 if (ret == (struct xcoff_link_hash_entry *) NULL)
451 return (struct bfd_hash_entry *) ret;
453 /* Call the allocation method of the superclass. */
454 ret = ((struct xcoff_link_hash_entry *)
455 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
456 table, string));
457 if (ret != NULL)
459 /* Set local fields. */
460 ret->indx = -1;
461 ret->toc_section = NULL;
462 ret->u.toc_indx = -1;
463 ret->descriptor = NULL;
464 ret->ldsym = NULL;
465 ret->ldindx = -1;
466 ret->flags = 0;
467 ret->smclas = XMC_UA;
470 return (struct bfd_hash_entry *) ret;
473 /* Create a XCOFF link hash table. */
475 struct bfd_link_hash_table *
476 _bfd_xcoff_bfd_link_hash_table_create (abfd)
477 bfd *abfd;
479 struct xcoff_link_hash_table *ret;
481 ret = ((struct xcoff_link_hash_table *)
482 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
483 if (ret == (struct xcoff_link_hash_table *) NULL)
484 return (struct bfd_link_hash_table *) NULL;
485 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
487 bfd_release (abfd, ret);
488 return (struct bfd_link_hash_table *) NULL;
491 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
492 ret->debug_section = NULL;
493 ret->loader_section = NULL;
494 ret->ldrel_count = 0;
495 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
496 ret->linkage_section = NULL;
497 ret->toc_section = NULL;
498 ret->descriptor_section = NULL;
499 ret->imports = NULL;
500 ret->file_align = 0;
501 ret->textro = false;
502 ret->gc = false;
503 memset (ret->special_sections, 0, sizeof ret->special_sections);
505 /* The linker will always generate a full a.out header. We need to
506 record that fact now, before the sizeof_headers routine could be
507 called. */
508 xcoff_data (abfd)->full_aouthdr = true;
510 return &ret->root;
514 /* Read internal relocs for an XCOFF csect. This is a wrapper around
515 _bfd_coff_read_internal_relocs which tries to take advantage of any
516 relocs which may have been cached for the enclosing section. */
518 static struct internal_reloc *
519 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
520 require_internal, internal_relocs)
521 bfd *abfd;
522 asection *sec;
523 boolean cache;
524 bfd_byte *external_relocs;
525 boolean require_internal;
526 struct internal_reloc *internal_relocs;
529 if (coff_section_data (abfd, sec) != NULL
530 && coff_section_data (abfd, sec)->relocs == NULL
531 && xcoff_section_data (abfd, sec) != NULL)
533 asection *enclosing;
535 enclosing = xcoff_section_data (abfd, sec)->enclosing;
537 if (enclosing != NULL
538 && (coff_section_data (abfd, enclosing) == NULL
539 || coff_section_data (abfd, enclosing)->relocs == NULL)
540 && cache
541 && enclosing->reloc_count > 0)
543 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
544 external_relocs, false,
545 (struct internal_reloc *) NULL)
546 == NULL)
547 return NULL;
550 if (enclosing != NULL
551 && coff_section_data (abfd, enclosing) != NULL
552 && coff_section_data (abfd, enclosing)->relocs != NULL)
554 size_t off;
556 off = ((sec->rel_filepos - enclosing->rel_filepos)
557 / bfd_coff_relsz (abfd));
559 if (! require_internal)
560 return coff_section_data (abfd, enclosing)->relocs + off;
561 memcpy (internal_relocs,
562 coff_section_data (abfd, enclosing)->relocs + off,
563 sec->reloc_count * sizeof (struct internal_reloc));
564 return internal_relocs;
568 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
569 require_internal, internal_relocs);
572 /* Given an XCOFF BFD, add symbols to the global hash table as
573 appropriate. */
575 boolean
576 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
577 bfd *abfd;
578 struct bfd_link_info *info;
581 switch (bfd_get_format (abfd))
583 case bfd_object:
584 return xcoff_link_add_object_symbols (abfd, info);
586 case bfd_archive:
587 /* If the archive has a map, do the usual search. We then need
588 to check the archive for stripped dynamic objects, because
589 they will not appear in the archive map even though they
590 should, perhaps, be included. If the archive has no map, we
591 just consider each object file in turn, since that apparently
592 is what the AIX native linker does. */
593 if (bfd_has_map (abfd))
595 if (! (_bfd_generic_link_add_archive_symbols
596 (abfd, info, xcoff_link_check_archive_element)))
597 return false;
601 bfd *member;
603 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
604 while (member != NULL)
606 if (bfd_check_format (member, bfd_object)
607 && (! bfd_has_map (abfd)
608 || ((member->flags & DYNAMIC) != 0
609 && (member->flags & HAS_SYMS) == 0)))
611 boolean needed;
613 if (! xcoff_link_check_archive_element (member, info, &needed))
614 return false;
615 if (needed)
616 member->archive_pass = -1;
618 member = bfd_openr_next_archived_file (abfd, member);
622 return true;
624 default:
625 bfd_set_error (bfd_error_wrong_format);
626 return false;
630 /* Add symbols from an XCOFF object file. */
632 static boolean
633 xcoff_link_add_object_symbols (abfd, info)
634 bfd *abfd;
635 struct bfd_link_info *info;
638 if (! _bfd_coff_get_external_symbols (abfd))
639 return false;
640 if (! xcoff_link_add_symbols (abfd, info))
641 return false;
642 if (! info->keep_memory)
644 if (! _bfd_coff_free_symbols (abfd))
645 return false;
647 return true;
650 /* Check a single archive element to see if we need to include it in
651 the link. *PNEEDED is set according to whether this element is
652 needed in the link or not. This is called via
653 _bfd_generic_link_add_archive_symbols. */
655 static boolean
656 xcoff_link_check_archive_element (abfd, info, pneeded)
657 bfd *abfd;
658 struct bfd_link_info *info;
659 boolean *pneeded;
662 if (! _bfd_coff_get_external_symbols (abfd))
663 return false;
665 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
666 return false;
668 if (*pneeded)
670 if (! xcoff_link_add_symbols (abfd, info))
671 return false;
674 if (! info->keep_memory || ! *pneeded)
676 if (! _bfd_coff_free_symbols (abfd))
677 return false;
680 return true;
683 /* Look through the symbols to see if this object file should be
684 included in the link. */
686 static boolean
687 xcoff_link_check_ar_symbols (abfd, info, pneeded)
688 bfd *abfd;
689 struct bfd_link_info *info;
690 boolean *pneeded;
692 bfd_size_type symesz;
693 bfd_byte *esym;
694 bfd_byte *esym_end;
696 *pneeded = false;
698 if ((abfd->flags & DYNAMIC) != 0
699 && ! info->static_link
700 && info->hash->creator == abfd->xvec)
701 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
703 symesz = bfd_coff_symesz (abfd);
704 esym = (bfd_byte *) obj_coff_external_syms (abfd);
705 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
706 while (esym < esym_end)
708 struct internal_syment sym;
710 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
712 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
714 const char *name;
715 char buf[SYMNMLEN + 1];
716 struct bfd_link_hash_entry *h;
718 /* This symbol is externally visible, and is defined by this
719 object file. */
721 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
723 if (name == NULL)
724 return false;
725 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
727 /* We are only interested in symbols that are currently
728 undefined. If a symbol is currently known to be common,
729 XCOFF linkers do not bring in an object file which
730 defines it. We also don't bring in symbols to satisfy
731 undefined references in shared objects. */
732 if (h != (struct bfd_link_hash_entry *) NULL
733 && h->type == bfd_link_hash_undefined
734 && (info->hash->creator != abfd->xvec
735 || (((struct xcoff_link_hash_entry *) h)->flags
736 & XCOFF_DEF_DYNAMIC) == 0))
738 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
739 return false;
740 *pneeded = true;
741 return true;
745 esym += (sym.n_numaux + 1) * symesz;
748 /* We do not need this object file. */
749 return true;
752 /* Look through the loader symbols to see if this dynamic object
753 should be included in the link. The native linker uses the loader
754 symbols, not the normal symbol table, so we do too. */
756 static boolean
757 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
758 bfd *abfd;
759 struct bfd_link_info *info;
760 boolean *pneeded;
762 asection *lsec;
763 bfd_byte *contents;
764 struct internal_ldhdr ldhdr;
765 const char *strings;
766 bfd_byte *elsym, *elsymend;
768 *pneeded = false;
770 lsec = bfd_get_section_by_name (abfd, ".loader");
771 if (lsec == NULL)
773 /* There are no symbols, so don't try to include it. */
774 return true;
777 if (! xcoff_get_section_contents (abfd, lsec))
778 return false;
779 contents = coff_section_data (abfd, lsec)->contents;
781 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
783 strings = (char *) contents + ldhdr.l_stoff;
785 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
787 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
788 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
790 struct internal_ldsym ldsym;
791 char nambuf[SYMNMLEN + 1];
792 const char *name;
793 struct bfd_link_hash_entry *h;
795 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
797 /* We are only interested in exported symbols. */
798 if ((ldsym.l_smtype & L_EXPORT) == 0)
799 continue;
801 if (ldsym._l._l_l._l_zeroes == 0)
802 name = strings + ldsym._l._l_l._l_offset;
803 else
805 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
806 nambuf[SYMNMLEN] = '\0';
807 name = nambuf;
810 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
812 /* We are only interested in symbols that are currently
813 undefined. At this point we know that we are using an XCOFF
814 hash table. */
815 if (h != NULL
816 && h->type == bfd_link_hash_undefined
817 && (((struct xcoff_link_hash_entry *) h)->flags
818 & XCOFF_DEF_DYNAMIC) == 0)
820 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
821 return false;
822 *pneeded = true;
823 return true;
827 /* We do not need this shared object. */
829 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
831 free (coff_section_data (abfd, lsec)->contents);
832 coff_section_data (abfd, lsec)->contents = NULL;
835 return true;
838 /* Returns the index of reloc in RELOCS with the least address greater
839 than or equal to ADDRESS. The relocs are sorted by address. */
841 static bfd_size_type
842 xcoff_find_reloc (relocs, count, address)
843 struct internal_reloc *relocs;
844 bfd_size_type count;
845 bfd_vma address;
847 bfd_size_type min, max, this;
849 if (count < 2)
851 if (count == 1 && relocs[0].r_vaddr < address)
852 return 1;
853 else
854 return 0;
857 min = 0;
858 max = count;
860 /* Do a binary search over (min,max]. */
861 while (min + 1 < max)
863 bfd_vma raddr;
865 this = (max + min) / 2;
866 raddr = relocs[this].r_vaddr;
867 if (raddr > address)
868 max = this;
869 else if (raddr < address)
870 min = this;
871 else
873 min = this;
874 break;
878 if (relocs[min].r_vaddr < address)
879 return min + 1;
881 while (min > 0
882 && relocs[min - 1].r_vaddr == address)
883 --min;
885 return min;
890 * xcoff_link_create_extra_sections
892 * Takes care of creating the .loader, .gl, .ds, .debug and sections
894 static boolean
895 xcoff_link_create_extra_sections(bfd * abfd, struct bfd_link_info *info) {
897 boolean return_value = false;
899 if (info->hash->creator == abfd->xvec) {
901 /* We need to build a .loader section, so we do it here. This
902 won't work if we're producing an XCOFF output file with no
903 XCOFF input files. FIXME. */
905 if (xcoff_hash_table (info)->loader_section == NULL) {
906 asection *lsec;
908 lsec = bfd_make_section_anyway (abfd, ".loader");
909 if (lsec == NULL) {
910 goto end_return;
912 xcoff_hash_table (info)->loader_section = lsec;
913 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
916 /* Likewise for the linkage section. */
917 if (xcoff_hash_table (info)->linkage_section == NULL) {
918 asection *lsec;
920 lsec = bfd_make_section_anyway (abfd, ".gl");
921 if (lsec == NULL) {
922 goto end_return;
925 xcoff_hash_table (info)->linkage_section = lsec;
926 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
927 | SEC_IN_MEMORY);
928 lsec->alignment_power = 2;
931 /* Likewise for the TOC section. */
932 if (xcoff_hash_table (info)->toc_section == NULL) {
933 asection *tsec;
935 tsec = bfd_make_section_anyway (abfd, ".tc");
936 if (tsec == NULL) {
937 goto end_return;
940 xcoff_hash_table (info)->toc_section = tsec;
941 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
942 | SEC_IN_MEMORY);
943 tsec->alignment_power = 2;
946 /* Likewise for the descriptor section. */
947 if (xcoff_hash_table (info)->descriptor_section == NULL) {
948 asection *dsec;
950 dsec = bfd_make_section_anyway (abfd, ".ds");
951 if (dsec == NULL) {
952 goto end_return;
955 xcoff_hash_table (info)->descriptor_section = dsec;
956 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
957 | SEC_IN_MEMORY);
958 dsec->alignment_power = 2;
961 /* Likewise for the .debug section. */
962 if (xcoff_hash_table (info)->debug_section == NULL
963 && info->strip != strip_all) {
964 asection *dsec;
966 dsec = bfd_make_section_anyway (abfd, ".debug");
967 if (dsec == NULL) {
968 goto end_return;
970 xcoff_hash_table (info)->debug_section = dsec;
971 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
975 return_value = true;
977 end_return:
979 return return_value;
982 /* Add all the symbols from an object file to the hash table.
984 XCOFF is a weird format. A normal XCOFF .o files will have three
985 COFF sections--.text, .data, and .bss--but each COFF section will
986 contain many csects. These csects are described in the symbol
987 table. From the linker's point of view, each csect must be
988 considered a section in its own right. For example, a TOC entry is
989 handled as a small XMC_TC csect. The linker must be able to merge
990 different TOC entries together, which means that it must be able to
991 extract the XMC_TC csects from the .data section of the input .o
992 file.
994 From the point of view of our linker, this is, of course, a hideous
995 nightmare. We cope by actually creating sections for each csect,
996 and discarding the original sections. We then have to handle the
997 relocation entries carefully, since the only way to tell which
998 csect they belong to is to examine the address. */
1000 static boolean
1001 xcoff_link_add_symbols (abfd, info)
1002 bfd *abfd;
1003 struct bfd_link_info *info;
1005 unsigned int n_tmask;
1006 unsigned int n_btshft;
1007 boolean default_copy;
1008 bfd_size_type symcount;
1009 struct xcoff_link_hash_entry **sym_hash;
1010 asection **csect_cache;
1011 bfd_size_type linesz;
1012 asection *o;
1013 asection *last_real;
1014 boolean keep_syms;
1015 asection *csect;
1016 unsigned int csect_index;
1017 asection *first_csect;
1018 bfd_size_type symesz;
1019 bfd_byte *esym;
1020 bfd_byte *esym_end;
1021 struct reloc_info_struct
1023 struct internal_reloc *relocs;
1024 asection **csects;
1025 bfd_byte *linenos;
1026 } *reloc_info = NULL;
1028 keep_syms = obj_coff_keep_syms (abfd);
1030 if ((abfd->flags & DYNAMIC) != 0
1031 && ! info->static_link) {
1032 if (! xcoff_link_add_dynamic_symbols (abfd, info)) {
1033 return false;
1037 /* create the loader, toc, gl, ds and debug sections, if needed */
1038 if (false == xcoff_link_create_extra_sections(abfd, info)) {
1039 goto error_return;
1042 if ((abfd->flags & DYNAMIC) != 0
1043 && ! info->static_link)
1044 return true;
1046 n_tmask = coff_data (abfd)->local_n_tmask;
1047 n_btshft = coff_data (abfd)->local_n_btshft;
1049 /* Define macros so that ISFCN, et. al., macros work correctly. */
1050 #define N_TMASK n_tmask
1051 #define N_BTSHFT n_btshft
1053 if (info->keep_memory)
1054 default_copy = false;
1055 else
1056 default_copy = true;
1058 symcount = obj_raw_syment_count (abfd);
1060 /* We keep a list of the linker hash table entries that correspond
1061 to each external symbol. */
1062 sym_hash = ((struct xcoff_link_hash_entry **)
1063 bfd_alloc (abfd,
1064 (symcount
1065 * sizeof (struct xcoff_link_hash_entry *))));
1066 if (sym_hash == NULL && symcount != 0)
1067 goto error_return;
1068 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1069 memset (sym_hash, 0,
1070 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1072 /* Because of the weird stuff we are doing with XCOFF csects, we can
1073 not easily determine which section a symbol is in, so we store
1074 the information in the tdata for the input file. */
1075 csect_cache = ((asection **)
1076 bfd_alloc (abfd, symcount * sizeof (asection *)));
1077 if (csect_cache == NULL && symcount != 0)
1078 goto error_return;
1079 xcoff_data (abfd)->csects = csect_cache;
1080 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1082 /* While splitting sections into csects, we need to assign the
1083 relocs correctly. The relocs and the csects must both be in
1084 order by VMA within a given section, so we handle this by
1085 scanning along the relocs as we process the csects. We index
1086 into reloc_info using the section target_index. */
1087 reloc_info = ((struct reloc_info_struct *)
1088 bfd_malloc ((abfd->section_count + 1)
1089 * sizeof (struct reloc_info_struct)));
1090 if (reloc_info == NULL)
1091 goto error_return;
1092 memset ((PTR) reloc_info, 0,
1093 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1095 /* Read in the relocs and line numbers for each section. */
1096 linesz = bfd_coff_linesz (abfd);
1097 last_real = NULL;
1098 for (o = abfd->sections; o != NULL; o = o->next) {
1100 last_real = o;
1101 if ((o->flags & SEC_RELOC) != 0) {
1103 reloc_info[o->target_index].relocs =
1104 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1105 false, (struct internal_reloc *) NULL);
1106 reloc_info[o->target_index].csects =
1107 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1108 if (reloc_info[o->target_index].csects == NULL)
1109 goto error_return;
1110 memset (reloc_info[o->target_index].csects, 0,
1111 o->reloc_count * sizeof (asection *));
1115 if ((info->strip == strip_none || info->strip == strip_some)
1116 && o->lineno_count > 0) {
1118 bfd_byte *linenos;
1120 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1121 if (linenos == NULL)
1122 goto error_return;
1123 reloc_info[o->target_index].linenos = linenos;
1124 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1125 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1126 != linesz * o->lineno_count))
1127 goto error_return;
1133 /* Don't let the linker relocation routines discard the symbols. */
1134 obj_coff_keep_syms (abfd) = true;
1136 csect = NULL;
1137 csect_index = 0;
1138 first_csect = NULL;
1140 symesz = bfd_coff_symesz (abfd);
1141 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1142 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1143 esym_end = esym + symcount * symesz;
1145 while (esym < esym_end) {
1146 struct internal_syment sym;
1147 union internal_auxent aux;
1148 const char *name;
1149 char buf[SYMNMLEN + 1];
1150 int smtyp;
1151 flagword flags;
1152 asection *section;
1153 bfd_vma value;
1154 struct xcoff_link_hash_entry *set_toc;
1156 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1158 /* In this pass we are only interested in symbols with csect
1159 information. */
1160 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT) {
1163 * Set csect_cache,
1164 * Normally csect is a .pr, .rw etc. created in the loop
1165 * If C_FILE or first time, handle special
1167 * Advance esym, sym_hash, csect_hash ptr's
1168 * Keep track of the last_symndx for the current file
1170 if (sym.n_sclass == C_FILE && csect != NULL) {
1171 xcoff_section_data (abfd, csect)->last_symndx =
1172 ((esym
1173 - (bfd_byte *) obj_coff_external_syms (abfd))
1174 / symesz);
1175 csect = NULL;
1178 if (csect != NULL)
1179 *csect_cache = csect;
1180 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1181 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1182 else
1183 *csect_cache = NULL;
1184 esym += (sym.n_numaux + 1) * symesz;
1185 sym_hash += sym.n_numaux + 1;
1186 csect_cache += sym.n_numaux + 1;
1188 continue;
1191 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1193 if (name == NULL)
1194 goto error_return;
1196 /* If this symbol has line number information attached to it,
1197 and we're not stripping it, count the number of entries and
1198 add them to the count for this csect. In the final link pass
1199 we are going to attach line number information by symbol,
1200 rather than by section, in order to more easily handle
1201 garbage collection. */
1202 if ((info->strip == strip_none || info->strip == strip_some) &&
1203 (sym.n_numaux > 1) &&
1204 (csect != NULL) &&
1205 (ISFCN (sym.n_type))) {
1207 union internal_auxent auxlin;
1209 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1210 sym.n_type, sym.n_sclass,
1211 0, sym.n_numaux, (PTR) &auxlin);
1213 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) {
1214 asection *enclosing;
1215 bfd_signed_vma linoff;
1217 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1218 if (enclosing == NULL) {
1219 (*_bfd_error_handler)
1220 (_("%s: `%s' has line numbers but no enclosing section"),
1221 bfd_get_filename (abfd), name);
1222 bfd_set_error (bfd_error_bad_value);
1223 goto error_return;
1225 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1226 - enclosing->line_filepos);
1227 /* explict cast to bfd_signed_vma for compiler */
1228 if (linoff < (bfd_signed_vma)(enclosing->lineno_count * linesz)) {
1229 struct internal_lineno lin;
1230 bfd_byte *linpstart;
1232 linpstart = (reloc_info[enclosing->target_index].linenos
1233 + linoff);
1234 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1235 if (lin.l_lnno == 0
1236 && ((bfd_size_type) lin.l_addr.l_symndx
1237 == ((esym
1238 - (bfd_byte *) obj_coff_external_syms (abfd))
1239 / symesz))) {
1240 bfd_byte *linpend, *linp;
1242 linpend = (reloc_info[enclosing->target_index].linenos
1243 + enclosing->lineno_count * linesz);
1244 for (linp = linpstart + linesz; linp < linpend; linp += linesz) {
1245 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1246 (PTR) &lin);
1247 if (lin.l_lnno == 0)
1248 break;
1250 csect->lineno_count += (linp - linpstart) / linesz;
1251 /* The setting of line_filepos will only be
1252 useful if all the line number entries for a
1253 csect are contiguous; this only matters for
1254 error reporting. */
1255 if (csect->line_filepos == 0)
1256 csect->line_filepos =
1257 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1263 /* Pick up the csect auxiliary information. */
1265 if (sym.n_numaux == 0) {
1266 (*_bfd_error_handler)
1267 (_("%s: class %d symbol `%s' has no aux entries"),
1268 bfd_get_filename (abfd), sym.n_sclass, name);
1269 bfd_set_error (bfd_error_bad_value);
1270 goto error_return;
1273 bfd_coff_swap_aux_in (abfd,
1274 (PTR) (esym + symesz * sym.n_numaux),
1275 sym.n_type, sym.n_sclass,
1276 sym.n_numaux - 1, sym.n_numaux,
1277 (PTR) &aux);
1279 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1281 flags = BSF_GLOBAL;
1282 section = NULL;
1283 value = 0;
1284 set_toc = NULL;
1286 switch (smtyp)
1288 default:
1289 (*_bfd_error_handler)
1290 (_("%s: symbol `%s' has unrecognized csect type %d"),
1291 bfd_get_filename (abfd), name, smtyp);
1292 bfd_set_error (bfd_error_bad_value);
1293 goto error_return;
1295 case XTY_ER:
1296 /* This is an external reference. */
1297 if (sym.n_sclass == C_HIDEXT
1298 || sym.n_scnum != N_UNDEF
1299 || aux.x_csect.x_scnlen.l != 0)
1301 (*_bfd_error_handler)
1302 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1303 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1304 aux.x_csect.x_scnlen.l);
1305 bfd_set_error (bfd_error_bad_value);
1306 goto error_return;
1309 /* An XMC_XO external reference is actually a reference to
1310 an absolute location. */
1311 if (aux.x_csect.x_smclas != XMC_XO)
1312 section = bfd_und_section_ptr;
1313 else
1315 section = bfd_abs_section_ptr;
1316 value = sym.n_value;
1318 break;
1320 case XTY_SD:
1321 /* This is a csect definition. */
1322 if (csect != NULL)
1324 xcoff_section_data (abfd, csect)->last_symndx =
1325 ((esym
1326 - (bfd_byte *) obj_coff_external_syms (abfd))
1327 / symesz);
1330 csect = NULL;
1331 csect_index = -1;
1333 /* When we see a TOC anchor, we record the TOC value. */
1334 if (aux.x_csect.x_smclas == XMC_TC0)
1336 if (sym.n_sclass != C_HIDEXT
1337 || aux.x_csect.x_scnlen.l != 0)
1339 (*_bfd_error_handler)
1340 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1341 bfd_get_filename (abfd), name, sym.n_sclass,
1342 aux.x_csect.x_scnlen.l);
1343 bfd_set_error (bfd_error_bad_value);
1344 goto error_return;
1346 xcoff_data (abfd)->toc = sym.n_value;
1350 * We must merge TOC entries for the same symbol. We can
1351 * merge two TOC entries if they are both C_HIDEXT, they
1352 * both have the same name, they are both 4 or 8 bytes long, and
1353 * they both have a relocation table entry for an external
1354 * symbol with the same name. Unfortunately, this means
1355 * that we must look through the relocations. Ick.
1357 * Logic for 32 bit vs 64 bit.
1358 * 32 bit has a csect length of 4 for TOC
1359 * 64 bit has a csect length of 8 for TOC
1361 * The conditions to get past the if-check are not that bad.
1362 * They are what is used to create the TOC csects in the first
1363 * place
1365 if ((aux.x_csect.x_smclas == XMC_TC) &&
1366 (sym.n_sclass == C_HIDEXT) &&
1367 (info->hash->creator == abfd->xvec) &&
1368 (((bfd_xcoff_is_xcoff32(abfd)) &&
1369 (aux.x_csect.x_scnlen.l == 4)) ||
1370 ((bfd_xcoff_is_xcoff64(abfd)) &&
1371 (aux.x_csect.x_scnlen.l == 8)))) {
1373 asection *enclosing;
1374 struct internal_reloc *relocs;
1375 bfd_size_type relindx;
1376 struct internal_reloc *rel;
1378 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1379 if (enclosing == NULL)
1380 goto error_return;
1382 relocs = reloc_info[enclosing->target_index].relocs;
1383 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1384 sym.n_value);
1385 rel = relocs + relindx;
1388 * 32 bit R_POS r_size is 31
1389 * 64 bit R_POS r_size is 63
1391 if ((relindx < enclosing->reloc_count) &&
1392 (rel->r_vaddr == (bfd_vma) sym.n_value) &&
1393 (rel->r_type == R_POS) &&
1394 (((bfd_xcoff_is_xcoff32(abfd)) &&
1395 (rel->r_size == 31)) ||
1396 ((bfd_xcoff_is_xcoff64(abfd)) &&
1397 (rel->r_size == 63)))) {
1399 bfd_byte *erelsym;
1401 struct internal_syment relsym;
1403 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1404 + rel->r_symndx * symesz);
1405 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1406 if (relsym.n_sclass == C_EXT) {
1407 const char *relname;
1408 char relbuf[SYMNMLEN + 1];
1409 boolean copy;
1410 struct xcoff_link_hash_entry *h;
1412 /* At this point we know that the TOC entry is
1413 for an externally visible symbol. */
1415 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1416 relbuf);
1417 if (relname == NULL)
1418 goto error_return;
1420 /* We only merge TOC entries if the TC name is
1421 the same as the symbol name. This handles
1422 the normal case, but not common cases like
1423 SYM.P4 which gcc generates to store SYM + 4
1424 in the TOC. FIXME. */
1426 if (strcmp (name, relname) == 0) {
1427 copy = (! info->keep_memory
1428 || relsym._n._n_n._n_zeroes != 0
1429 || relsym._n._n_n._n_offset == 0);
1430 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1431 relname, true, copy,
1432 false);
1433 if (h == NULL)
1434 goto error_return;
1436 /* At this point h->root.type could be
1437 bfd_link_hash_new. That should be OK,
1438 since we know for sure that we will come
1439 across this symbol as we step through the
1440 file. */
1442 /* We store h in *sym_hash for the
1443 convenience of the relocate_section
1444 function. */
1445 *sym_hash = h;
1447 if (h->toc_section != NULL) {
1448 asection **rel_csects;
1450 /* We already have a TOC entry for this
1451 symbol, so we can just ignore this
1452 one. */
1453 rel_csects =
1454 reloc_info[enclosing->target_index].csects;
1455 rel_csects[relindx] = bfd_und_section_ptr;
1456 break;
1459 /* We are about to create a TOC entry for
1460 this symbol. */
1461 set_toc = h;
1462 } /* merge toc reloc */
1463 } /* c_ext */
1464 } /* reloc */
1465 } /* merge toc */
1469 asection *enclosing;
1471 /* We need to create a new section. We get the name from
1472 the csect storage mapping class, so that the linker can
1473 accumulate similar csects together. */
1475 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1476 if (NULL == csect) {
1477 goto error_return;
1481 * The enclosing section is the main section : .data, .text
1482 * or .bss that the csect is coming from
1484 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1485 if (enclosing == NULL)
1486 goto error_return;
1488 if (! bfd_is_abs_section (enclosing) &&
1489 ((bfd_vma) sym.n_value < enclosing->vma ||
1490 ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1491 > enclosing->vma + enclosing->_raw_size))) {
1492 (*_bfd_error_handler)
1493 (_("%s: csect `%s' not in enclosing section"),
1494 bfd_get_filename (abfd), name);
1495 bfd_set_error (bfd_error_bad_value);
1496 goto error_return;
1498 csect->vma = sym.n_value;
1499 csect->filepos = (enclosing->filepos
1500 + sym.n_value
1501 - enclosing->vma);
1502 csect->_raw_size = aux.x_csect.x_scnlen.l;
1503 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1504 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1506 /* Record the enclosing section in the tdata for this new
1507 section. */
1508 csect->used_by_bfd =
1509 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1510 if (csect->used_by_bfd == NULL)
1511 goto error_return;
1512 coff_section_data (abfd, csect)->tdata =
1513 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1514 if (coff_section_data (abfd, csect)->tdata == NULL)
1515 goto error_return;
1516 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1517 xcoff_section_data (abfd, csect)->lineno_count =
1518 enclosing->lineno_count;
1520 if (enclosing->owner == abfd) {
1521 struct internal_reloc *relocs;
1522 bfd_size_type relindx;
1523 struct internal_reloc *rel;
1524 asection **rel_csect;
1526 relocs = reloc_info[enclosing->target_index].relocs;
1527 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1528 csect->vma);
1530 rel = relocs + relindx;
1531 rel_csect = (reloc_info[enclosing->target_index].csects
1532 + relindx);
1534 csect->rel_filepos = (enclosing->rel_filepos
1535 + relindx * bfd_coff_relsz (abfd));
1536 while (relindx < enclosing->reloc_count &&
1537 *rel_csect == NULL &&
1538 rel->r_vaddr < csect->vma + csect->_raw_size) {
1540 *rel_csect = csect;
1541 csect->flags |= SEC_RELOC;
1542 ++csect->reloc_count;
1543 ++relindx;
1544 ++rel;
1545 ++rel_csect;
1549 /* There are a number of other fields and section flags
1550 which we do not bother to set. */
1552 csect_index = ((esym
1553 - (bfd_byte *) obj_coff_external_syms (abfd))
1554 / symesz);
1556 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1558 if (first_csect == NULL)
1559 first_csect = csect;
1561 /* If this symbol is C_EXT, we treat it as starting at the
1562 beginning of the newly created section. */
1563 if (sym.n_sclass == C_EXT)
1565 section = csect;
1566 value = 0;
1569 /* If this is a TOC section for a symbol, record it. */
1570 if (set_toc != NULL)
1571 set_toc->toc_section = csect;
1573 break;
1575 case XTY_LD:
1576 /* This is a label definition. The x_scnlen field is the
1577 symbol index of the csect. I believe that this must
1578 always follow the appropriate XTY_SD symbol, so I will
1579 insist on it. */
1581 boolean bad;
1583 bad = false;
1584 if (aux.x_csect.x_scnlen.l < 0
1585 || (aux.x_csect.x_scnlen.l
1586 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1587 bad = true;
1588 if (! bad)
1590 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1591 if (section == NULL
1592 || (section->flags & SEC_HAS_CONTENTS) == 0)
1593 bad = true;
1595 if (bad)
1597 (*_bfd_error_handler)
1598 (_("%s: misplaced XTY_LD `%s'"),
1599 bfd_get_filename (abfd), name);
1600 bfd_set_error (bfd_error_bad_value);
1601 goto error_return;
1604 value = sym.n_value - csect->vma;
1606 break;
1608 case XTY_CM:
1609 /* This is an unitialized csect. We could base the name on
1610 the storage mapping class, but we don't bother except for
1611 an XMC_TD symbol. If this csect is externally visible,
1612 it is a common symbol. We put XMC_TD symbols in sections
1613 named .tocbss, and rely on the linker script to put that
1614 in the TOC area. */
1616 if (csect != NULL)
1618 xcoff_section_data (abfd, csect)->last_symndx =
1619 ((esym
1620 - (bfd_byte *) obj_coff_external_syms (abfd))
1621 / symesz);
1624 if (aux.x_csect.x_smclas == XMC_TD) {
1626 * The linker script puts the .td section in the data
1627 * section after the .tc section
1629 csect = bfd_make_section_anyway (abfd, ".td");
1631 } else {
1632 csect = bfd_make_section_anyway (abfd, ".bss");
1634 if (csect == NULL)
1635 goto error_return;
1636 csect->vma = sym.n_value;
1637 csect->_raw_size = aux.x_csect.x_scnlen.l;
1638 csect->flags |= SEC_ALLOC;
1639 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1640 /* There are a number of other fields and section flags
1641 which we do not bother to set. */
1643 csect_index = ((esym
1644 - (bfd_byte *) obj_coff_external_syms (abfd))
1645 / symesz);
1647 csect->used_by_bfd =
1648 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1649 if (csect->used_by_bfd == NULL)
1650 goto error_return;
1651 coff_section_data (abfd, csect)->tdata =
1652 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1653 if (coff_section_data (abfd, csect)->tdata == NULL)
1654 goto error_return;
1655 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1657 if (first_csect == NULL)
1658 first_csect = csect;
1660 if (sym.n_sclass == C_EXT)
1662 csect->flags |= SEC_IS_COMMON;
1663 csect->_raw_size = 0;
1664 section = csect;
1665 value = aux.x_csect.x_scnlen.l;
1668 break;
1671 /* Check for magic symbol names. */
1672 if ((smtyp == XTY_SD || smtyp == XTY_CM) &&
1673 (aux.x_csect.x_smclas != XMC_TC) &&
1674 (aux.x_csect.x_smclas != XMC_TD)) {
1676 int i = -1;
1678 if (name[0] == '_') {
1679 if (strcmp (name, "_text") == 0)
1680 i = XCOFF_SPECIAL_SECTION_TEXT;
1681 else if (strcmp (name, "_etext") == 0)
1682 i = XCOFF_SPECIAL_SECTION_ETEXT;
1683 else if (strcmp (name, "_data") == 0)
1684 i = XCOFF_SPECIAL_SECTION_DATA;
1685 else if (strcmp (name, "_edata") == 0)
1686 i = XCOFF_SPECIAL_SECTION_EDATA;
1687 else if (strcmp (name, "_end") == 0)
1688 i = XCOFF_SPECIAL_SECTION_END;
1689 } else if (name[0] == 'e' && strcmp (name, "end") == 0) {
1690 i = XCOFF_SPECIAL_SECTION_END2;
1693 if (i != -1) {
1694 xcoff_hash_table (info)->special_sections[i] = csect;
1698 /* Now we have enough information to add the symbol to the
1699 linker hash table. */
1701 if (sym.n_sclass == C_EXT)
1703 boolean copy;
1705 BFD_ASSERT (section != NULL);
1707 /* We must copy the name into memory if we got it from the
1708 syment itself, rather than the string table. */
1709 copy = default_copy;
1710 if (sym._n._n_n._n_zeroes != 0
1711 || sym._n._n_n._n_offset == 0)
1712 copy = true;
1714 /* The AIX linker appears to only detect multiple symbol
1715 definitions when there is a reference to the symbol. If
1716 a symbol is defined multiple times, and the only
1717 references are from the same object file, the AIX linker
1718 appears to permit it. It does not merge the different
1719 definitions, but handles them independently. On the
1720 other hand, if there is a reference, the linker reports
1721 an error.
1723 This matters because the AIX <net/net_globals.h> header
1724 file actually defines an initialized array, so we have to
1725 actually permit that to work.
1727 Just to make matters even more confusing, the AIX linker
1728 appears to permit multiple symbol definitions whenever
1729 the second definition is in an archive rather than an
1730 object file. This may be a consequence of the manner in
1731 which it handles archives: I think it may load the entire
1732 archive in as separate csects, and then let garbage
1733 collection discard symbols.
1735 We also have to handle the case of statically linking a
1736 shared object, which will cause symbol redefinitions,
1737 although this is an easier case to detect. */
1739 if (info->hash->creator == abfd->xvec)
1741 if (! bfd_is_und_section (section))
1742 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1743 name, true, copy, false);
1744 else
1745 *sym_hash = ((struct xcoff_link_hash_entry *)
1746 bfd_wrapped_link_hash_lookup (abfd, info, name,
1747 true, copy, false));
1748 if (*sym_hash == NULL)
1749 goto error_return;
1750 if (((*sym_hash)->root.type == bfd_link_hash_defined
1751 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1752 && ! bfd_is_und_section (section)
1753 && ! bfd_is_com_section (section))
1755 /* This is a second definition of a defined symbol. */
1756 if ((abfd->flags & DYNAMIC) != 0
1757 && ((*sym_hash)->smclas != XMC_GL
1758 || aux.x_csect.x_smclas == XMC_GL
1759 || ((*sym_hash)->root.u.def.section->owner->flags
1760 & DYNAMIC) == 0))
1762 /* The new symbol is from a shared library, and
1763 either the existing symbol is not global
1764 linkage code or this symbol is global linkage
1765 code. If the existing symbol is global
1766 linkage code and the new symbol is not, then
1767 we want to use the new symbol. */
1768 section = bfd_und_section_ptr;
1769 value = 0;
1771 else if (((*sym_hash)->root.u.def.section->owner->flags
1772 & DYNAMIC) != 0)
1774 /* The existing symbol is from a shared library.
1775 Replace it. */
1776 (*sym_hash)->root.type = bfd_link_hash_undefined;
1777 (*sym_hash)->root.u.undef.abfd =
1778 (*sym_hash)->root.u.def.section->owner;
1780 else if (abfd->my_archive != NULL)
1782 /* This is a redefinition in an object contained
1783 in an archive. Just ignore it. See the
1784 comment above. */
1785 section = bfd_und_section_ptr;
1786 value = 0;
1788 else if ((*sym_hash)->root.next != NULL
1789 || info->hash->undefs_tail == &(*sym_hash)->root)
1791 /* This symbol has been referenced. In this
1792 case, we just continue and permit the
1793 multiple definition error. See the comment
1794 above about the behaviour of the AIX linker. */
1796 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1798 /* The symbols are both csects of the same
1799 class. There is at least a chance that this
1800 is a semi-legitimate redefinition. */
1801 section = bfd_und_section_ptr;
1802 value = 0;
1803 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1806 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1807 && ((*sym_hash)->root.type == bfd_link_hash_defined
1808 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1809 && (bfd_is_und_section (section)
1810 || bfd_is_com_section (section)))
1812 /* This is a reference to a multiply defined symbol.
1813 Report the error now. See the comment above
1814 about the behaviour of the AIX linker. We could
1815 also do this with warning symbols, but I'm not
1816 sure the XCOFF linker is wholly prepared to
1817 handle them, and that would only be a warning,
1818 not an error. */
1819 if (! ((*info->callbacks->multiple_definition)
1820 (info, (*sym_hash)->root.root.string,
1821 (bfd *) NULL, (asection *) NULL, 0,
1822 (*sym_hash)->root.u.def.section->owner,
1823 (*sym_hash)->root.u.def.section,
1824 (*sym_hash)->root.u.def.value)))
1825 goto error_return;
1826 /* Try not to give this error too many times. */
1827 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1831 /* _bfd_generic_link_add_one_symbol may call the linker to
1832 generate an error message, and the linker may try to read
1833 the symbol table to give a good error. Right now, the
1834 line numbers are in an inconsistent state, since they are
1835 counted both in the real sections and in the new csects.
1836 We need to leave the count in the real sections so that
1837 the linker can report the line number of the error
1838 correctly, so temporarily clobber the link to the csects
1839 so that the linker will not try to read the line numbers
1840 a second time from the csects. */
1841 BFD_ASSERT (last_real->next == first_csect);
1842 last_real->next = NULL;
1843 if (! (_bfd_generic_link_add_one_symbol
1844 (info, abfd, name, flags, section, value,
1845 (const char *) NULL, copy, true,
1846 (struct bfd_link_hash_entry **) sym_hash)))
1847 goto error_return;
1848 last_real->next = first_csect;
1850 if (smtyp == XTY_CM)
1852 if ((*sym_hash)->root.type != bfd_link_hash_common
1853 || (*sym_hash)->root.u.c.p->section != csect)
1855 /* We don't need the common csect we just created. */
1856 csect->_raw_size = 0;
1858 else
1860 (*sym_hash)->root.u.c.p->alignment_power
1861 = csect->alignment_power;
1865 if (info->hash->creator == abfd->xvec)
1867 int flag;
1869 if (smtyp == XTY_ER || smtyp == XTY_CM)
1870 flag = XCOFF_REF_REGULAR;
1871 else
1872 flag = XCOFF_DEF_REGULAR;
1873 (*sym_hash)->flags |= flag;
1875 if ((*sym_hash)->smclas == XMC_UA
1876 || flag == XCOFF_DEF_REGULAR)
1877 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1881 *csect_cache = csect;
1883 esym += (sym.n_numaux + 1) * symesz;
1884 sym_hash += sym.n_numaux + 1;
1885 csect_cache += sym.n_numaux + 1;
1888 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1890 /* Make sure that we have seen all the relocs. */
1891 for (o = abfd->sections; o != first_csect; o = o->next)
1893 /* Reset the section size and the line number count, since the
1894 data is now attached to the csects. Don't reset the size of
1895 the .debug section, since we need to read it below in
1896 bfd_xcoff_size_dynamic_sections. */
1897 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1898 o->_raw_size = 0;
1899 o->lineno_count = 0;
1901 if ((o->flags & SEC_RELOC) != 0)
1903 bfd_size_type i;
1904 struct internal_reloc *rel;
1905 asection **rel_csect;
1907 rel = reloc_info[o->target_index].relocs;
1908 rel_csect = reloc_info[o->target_index].csects;
1910 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1913 if (*rel_csect == NULL)
1915 (*_bfd_error_handler)
1916 (_("%s: reloc %s:%d not in csect"),
1917 bfd_get_filename (abfd), o->name, i);
1918 bfd_set_error (bfd_error_bad_value);
1919 goto error_return;
1922 /* We identify all symbols which are called, so that we
1923 can create glue code for calls to functions imported
1924 from dynamic objects. */
1925 if (info->hash->creator == abfd->xvec
1926 && *rel_csect != bfd_und_section_ptr
1927 && (rel->r_type == R_BR
1928 || rel->r_type == R_RBR)
1929 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1931 struct xcoff_link_hash_entry *h;
1933 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1934 h->flags |= XCOFF_CALLED;
1935 /* If the symbol name starts with a period, it is
1936 the code of a function. If the symbol is
1937 currently undefined, then add an undefined symbol
1938 for the function descriptor. This should do no
1939 harm, because any regular object that defines the
1940 function should also define the function
1941 descriptor. It helps, because it means that we
1942 will identify the function descriptor with a
1943 dynamic object if a dynamic object defines it. */
1944 if (h->root.root.string[0] == '.'
1945 && h->descriptor == NULL)
1947 struct xcoff_link_hash_entry *hds;
1949 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1950 h->root.root.string + 1,
1951 true, false, true);
1952 if (hds == NULL)
1953 goto error_return;
1954 if (hds->root.type == bfd_link_hash_new)
1956 if (! (_bfd_generic_link_add_one_symbol
1957 (info, abfd, hds->root.root.string,
1958 (flagword) 0, bfd_und_section_ptr,
1959 (bfd_vma) 0, (const char *) NULL, false,
1960 true,
1961 (struct bfd_link_hash_entry **) &hds)))
1962 goto error_return;
1964 hds->flags |= XCOFF_DESCRIPTOR;
1965 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1966 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1967 hds->descriptor = h;
1968 h->descriptor = hds;
1973 free (reloc_info[o->target_index].csects);
1974 reloc_info[o->target_index].csects = NULL;
1976 /* Reset SEC_RELOC and the reloc_count, since the reloc
1977 information is now attached to the csects. */
1978 o->flags &=~ SEC_RELOC;
1979 o->reloc_count = 0;
1981 /* If we are not keeping memory, free the reloc information. */
1982 if (! info->keep_memory
1983 && coff_section_data (abfd, o) != NULL
1984 && coff_section_data (abfd, o)->relocs != NULL
1985 && ! coff_section_data (abfd, o)->keep_relocs)
1987 free (coff_section_data (abfd, o)->relocs);
1988 coff_section_data (abfd, o)->relocs = NULL;
1992 /* Free up the line numbers. FIXME: We could cache these
1993 somewhere for the final link, to avoid reading them again. */
1994 if (reloc_info[o->target_index].linenos != NULL)
1996 free (reloc_info[o->target_index].linenos);
1997 reloc_info[o->target_index].linenos = NULL;
2001 free (reloc_info);
2003 obj_coff_keep_syms (abfd) = keep_syms;
2005 return true;
2007 error_return:
2008 if (reloc_info != NULL)
2010 for (o = abfd->sections; o != NULL; o = o->next)
2012 if (reloc_info[o->target_index].csects != NULL)
2013 free (reloc_info[o->target_index].csects);
2014 if (reloc_info[o->target_index].linenos != NULL)
2015 free (reloc_info[o->target_index].linenos);
2017 free (reloc_info);
2019 obj_coff_keep_syms (abfd) = keep_syms;
2020 return false;
2023 #undef N_TMASK
2024 #undef N_BTSHFT
2026 /* This function is used to add symbols from a dynamic object to the
2027 global symbol table. */
2029 static boolean
2030 xcoff_link_add_dynamic_symbols (abfd, info)
2031 bfd *abfd;
2032 struct bfd_link_info *info;
2034 asection *lsec;
2035 bfd_byte *contents;
2036 struct internal_ldhdr ldhdr;
2037 const char *strings;
2038 bfd_byte *elsym, *elsymend;
2039 struct xcoff_import_file *n;
2040 const char *bname;
2041 const char *mname;
2042 const char *s;
2043 unsigned int c;
2044 struct xcoff_import_file **pp;
2046 /* We can only handle a dynamic object if we are generating an XCOFF
2047 output file. */
2048 if (info->hash->creator != abfd->xvec)
2050 (*_bfd_error_handler)
2051 (_("%s: XCOFF shared object when not producing XCOFF output"),
2052 bfd_get_filename (abfd));
2053 bfd_set_error (bfd_error_invalid_operation);
2054 return false;
2057 /* The symbols we use from a dynamic object are not the symbols in
2058 the normal symbol table, but, rather, the symbols in the export
2059 table. If there is a global symbol in a dynamic object which is
2060 not in the export table, the loader will not be able to find it,
2061 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2062 libc.a has symbols in the export table which are not in the
2063 symbol table. */
2065 /* Read in the .loader section. FIXME: We should really use the
2066 o_snloader field in the a.out header, rather than grabbing the
2067 section by name. */
2068 lsec = bfd_get_section_by_name (abfd, ".loader");
2069 if (lsec == NULL)
2071 (*_bfd_error_handler)
2072 (_("%s: dynamic object with no .loader section"),
2073 bfd_get_filename (abfd));
2074 bfd_set_error (bfd_error_no_symbols);
2075 return false;
2079 if (! xcoff_get_section_contents (abfd, lsec))
2080 return false;
2081 contents = coff_section_data (abfd, lsec)->contents;
2083 /* Remove the sections from this object, so that they do not get
2084 included in the link. */
2085 abfd->sections = NULL;
2087 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2089 strings = (char *) contents + ldhdr.l_stoff;
2091 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
2093 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
2095 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
2097 struct internal_ldsym ldsym;
2098 char nambuf[SYMNMLEN + 1];
2099 const char *name;
2100 struct xcoff_link_hash_entry *h;
2102 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2104 /* We are only interested in exported symbols. */
2105 if ((ldsym.l_smtype & L_EXPORT) == 0)
2106 continue;
2108 if (ldsym._l._l_l._l_zeroes == 0)
2109 name = strings + ldsym._l._l_l._l_offset;
2110 else
2112 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2113 nambuf[SYMNMLEN] = '\0';
2114 name = nambuf;
2117 /* Normally we could not call xcoff_link_hash_lookup in an add
2118 symbols routine, since we might not be using an XCOFF hash
2119 table. However, we verified above that we are using an XCOFF
2120 hash table. */
2122 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2123 true, true);
2124 if (h == NULL)
2125 return false;
2127 h->flags |= XCOFF_DEF_DYNAMIC;
2129 /* If the symbol is undefined, and the BFD it was found in is
2130 not a dynamic object, change the BFD to this dynamic object,
2131 so that we can get the correct import file ID. */
2132 if ((h->root.type == bfd_link_hash_undefined
2133 || h->root.type == bfd_link_hash_undefweak)
2134 && (h->root.u.undef.abfd == NULL
2135 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2136 h->root.u.undef.abfd = abfd;
2138 if (h->root.type == bfd_link_hash_new)
2140 h->root.type = bfd_link_hash_undefined;
2141 h->root.u.undef.abfd = abfd;
2142 /* We do not want to add this to the undefined symbol list. */
2145 if (h->smclas == XMC_UA
2146 || h->root.type == bfd_link_hash_undefined
2147 || h->root.type == bfd_link_hash_undefweak)
2148 h->smclas = ldsym.l_smclas;
2150 /* Unless this is an XMC_XO symbol, we don't bother to actually
2151 define it, since we don't have a section to put it in anyhow.
2152 Instead, the relocation routines handle the DEF_DYNAMIC flag
2153 correctly. */
2155 if (h->smclas == XMC_XO
2156 && (h->root.type == bfd_link_hash_undefined
2157 || h->root.type == bfd_link_hash_undefweak))
2159 /* This symbol has an absolute value. */
2160 h->root.type = bfd_link_hash_defined;
2161 h->root.u.def.section = bfd_abs_section_ptr;
2162 h->root.u.def.value = ldsym.l_value;
2165 /* If this symbol defines a function descriptor, then it
2166 implicitly defines the function code as well. */
2167 if (h->smclas == XMC_DS
2168 || (h->smclas == XMC_XO && name[0] != '.'))
2169 h->flags |= XCOFF_DESCRIPTOR;
2170 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2172 struct xcoff_link_hash_entry *hds;
2174 hds = h->descriptor;
2175 if (hds == NULL)
2177 char *dsnm;
2179 dsnm = bfd_malloc (strlen (name) + 2);
2180 if (dsnm == NULL)
2181 return false;
2182 dsnm[0] = '.';
2183 strcpy (dsnm + 1, name);
2184 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2185 true, true, true);
2186 free (dsnm);
2187 if (hds == NULL)
2188 return false;
2190 if (hds->root.type == bfd_link_hash_new)
2192 hds->root.type = bfd_link_hash_undefined;
2193 hds->root.u.undef.abfd = abfd;
2194 /* We do not want to add this to the undefined
2195 symbol list. */
2198 hds->descriptor = h;
2199 h->descriptor = hds;
2202 hds->flags |= XCOFF_DEF_DYNAMIC;
2203 if (hds->smclas == XMC_UA)
2204 hds->smclas = XMC_PR;
2206 /* An absolute symbol appears to actually define code, not a
2207 function descriptor. This is how some math functions are
2208 implemented on AIX 4.1. */
2209 if (h->smclas == XMC_XO
2210 && (hds->root.type == bfd_link_hash_undefined
2211 || hds->root.type == bfd_link_hash_undefweak))
2213 hds->smclas = XMC_XO;
2214 hds->root.type = bfd_link_hash_defined;
2215 hds->root.u.def.section = bfd_abs_section_ptr;
2216 hds->root.u.def.value = ldsym.l_value;
2221 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2223 free (coff_section_data (abfd, lsec)->contents);
2224 coff_section_data (abfd, lsec)->contents = NULL;
2227 /* Record this file in the import files. */
2229 n = ((struct xcoff_import_file *)
2230 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2231 if (n == NULL)
2232 return false;
2233 n->next = NULL;
2235 /* For some reason, the path entry in the import file list for a
2236 shared object appears to always be empty. The file name is the
2237 base name. */
2238 n->path = "";
2239 if (abfd->my_archive == NULL)
2241 bname = bfd_get_filename (abfd);
2242 mname = "";
2244 else
2246 bname = bfd_get_filename (abfd->my_archive);
2247 mname = bfd_get_filename (abfd);
2249 s = strrchr (bname, '/');
2250 if (s != NULL)
2251 bname = s + 1;
2252 n->file = bname;
2253 n->member = mname;
2255 /* We start c at 1 because the first import file number is reserved
2256 for LIBPATH. */
2257 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2258 *pp != NULL;
2259 pp = &(*pp)->next, ++c)
2261 *pp = n;
2263 xcoff_data (abfd)->import_file_id = c;
2265 return true;
2268 /* Routines that are called after all the input files have been
2269 handled, but before the sections are laid out in memory. */
2271 /* Mark a symbol as not being garbage, including the section in which
2272 it is defined. */
2274 static INLINE boolean
2275 xcoff_mark_symbol (info, h)
2276 struct bfd_link_info *info;
2277 struct xcoff_link_hash_entry *h;
2280 if ((h->flags & XCOFF_MARK) != 0)
2281 return true;
2283 h->flags |= XCOFF_MARK;
2284 if (h->root.type == bfd_link_hash_defined
2285 || h->root.type == bfd_link_hash_defweak)
2287 asection *hsec;
2289 hsec = h->root.u.def.section;
2290 if (! bfd_is_abs_section (hsec)
2291 && (hsec->flags & SEC_MARK) == 0)
2293 if (! xcoff_mark (info, hsec))
2294 return false;
2298 if (h->toc_section != NULL
2299 && (h->toc_section->flags & SEC_MARK) == 0)
2301 if (! xcoff_mark (info, h->toc_section))
2302 return false;
2305 return true;
2308 /* The mark phase of garbage collection. For a given section, mark
2309 it, and all the sections which define symbols to which it refers.
2310 Because this function needs to look at the relocs, we also count
2311 the number of relocs which need to be copied into the .loader
2312 section. */
2314 static boolean
2315 xcoff_mark (info, sec)
2316 struct bfd_link_info *info;
2317 asection *sec;
2319 if (bfd_is_abs_section (sec)
2320 || (sec->flags & SEC_MARK) != 0)
2321 return true;
2323 sec->flags |= SEC_MARK;
2325 if (sec->owner->xvec == info->hash->creator
2326 && coff_section_data (sec->owner, sec) != NULL
2327 && xcoff_section_data (sec->owner, sec) != NULL)
2329 register struct xcoff_link_hash_entry **hp, **hpend;
2330 struct internal_reloc *rel, *relend;
2332 /* Mark all the symbols in this section. */
2334 hp = (obj_xcoff_sym_hashes (sec->owner)
2335 + xcoff_section_data (sec->owner, sec)->first_symndx);
2336 hpend = (obj_xcoff_sym_hashes (sec->owner)
2337 + xcoff_section_data (sec->owner, sec)->last_symndx);
2338 for (; hp < hpend; hp++)
2340 register struct xcoff_link_hash_entry *h;
2342 h = *hp;
2343 if (h != NULL
2344 && (h->flags & XCOFF_MARK) == 0)
2346 if (! xcoff_mark_symbol (info, h))
2347 return false;
2351 /* Look through the section relocs. */
2353 if ((sec->flags & SEC_RELOC) != 0
2354 && sec->reloc_count > 0)
2356 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2357 (bfd_byte *) NULL, false,
2358 (struct internal_reloc *) NULL);
2359 if (rel == NULL)
2360 return false;
2361 relend = rel + sec->reloc_count;
2362 for (; rel < relend; rel++)
2364 asection *rsec;
2365 struct xcoff_link_hash_entry *h;
2367 if ((unsigned int) rel->r_symndx
2368 > obj_raw_syment_count (sec->owner))
2369 continue;
2371 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2372 if (h != NULL
2373 && (h->flags & XCOFF_MARK) == 0)
2375 if (! xcoff_mark_symbol (info, h))
2376 return false;
2379 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2380 if (rsec != NULL
2381 && (rsec->flags & SEC_MARK) == 0)
2383 if (! xcoff_mark (info, rsec))
2384 return false;
2387 /* See if this reloc needs to be copied into the .loader
2388 section. */
2389 switch (rel->r_type)
2391 default:
2392 if (h == NULL
2393 || h->root.type == bfd_link_hash_defined
2394 || h->root.type == bfd_link_hash_defweak
2395 || h->root.type == bfd_link_hash_common
2396 || ((h->flags & XCOFF_CALLED) != 0
2397 && (h->root.type == bfd_link_hash_undefined
2398 || h->root.type == bfd_link_hash_undefweak)
2399 && h->root.root.string[0] == '.'
2400 && h->descriptor != NULL
2401 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2402 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2403 && (h->descriptor->flags
2404 & XCOFF_DEF_REGULAR) == 0))))
2405 break;
2406 /* Fall through. */
2407 case R_POS:
2408 case R_NEG:
2409 case R_RL:
2410 case R_RLA:
2411 ++xcoff_hash_table (info)->ldrel_count;
2412 if (h != NULL)
2413 h->flags |= XCOFF_LDREL;
2414 break;
2415 case R_TOC:
2416 case R_GL:
2417 case R_TCL:
2418 case R_TRL:
2419 case R_TRLA:
2420 /* We should never need a .loader reloc for a TOC
2421 relative reloc. */
2422 break;
2426 if (! info->keep_memory
2427 && coff_section_data (sec->owner, sec) != NULL
2428 && coff_section_data (sec->owner, sec)->relocs != NULL
2429 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2431 free (coff_section_data (sec->owner, sec)->relocs);
2432 coff_section_data (sec->owner, sec)->relocs = NULL;
2437 return true;
2440 /* The sweep phase of garbage collection. Remove all garbage
2441 sections. */
2443 static void
2444 xcoff_sweep (info)
2445 struct bfd_link_info *info;
2447 bfd *sub;
2449 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2451 asection *o;
2453 for (o = sub->sections; o != NULL; o = o->next)
2455 if ((o->flags & SEC_MARK) == 0)
2457 /* Keep all sections from non-XCOFF input files. Keep
2458 special sections. Keep .debug sections for the
2459 moment. */
2460 if (sub->xvec != info->hash->creator
2461 || o == xcoff_hash_table (info)->debug_section
2462 || o == xcoff_hash_table (info)->loader_section
2463 || o == xcoff_hash_table (info)->linkage_section
2464 || o == xcoff_hash_table (info)->toc_section
2465 || o == xcoff_hash_table (info)->descriptor_section
2466 || strcmp (o->name, ".debug") == 0)
2467 o->flags |= SEC_MARK;
2468 else
2470 o->_raw_size = 0;
2471 o->reloc_count = 0;
2472 o->lineno_count = 0;
2479 /* Record the number of elements in a set. This is used to output the
2480 correct csect length. */
2482 boolean
2483 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2484 bfd *output_bfd;
2485 struct bfd_link_info *info;
2486 struct bfd_link_hash_entry *harg;
2487 bfd_size_type size;
2489 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2490 struct xcoff_link_size_list *n;
2492 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2493 return true;
2495 /* This will hardly ever be called. I don't want to burn four bytes
2496 per global symbol, so instead the size is kept on a linked list
2497 attached to the hash table. */
2499 n = ((struct xcoff_link_size_list *)
2500 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2501 if (n == NULL)
2502 return false;
2503 n->next = xcoff_hash_table (info)->size_list;
2504 n->h = h;
2505 n->size = size;
2506 xcoff_hash_table (info)->size_list = n;
2508 h->flags |= XCOFF_HAS_SIZE;
2510 return true;
2513 /* Import a symbol. */
2515 boolean
2516 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2517 impmember)
2518 bfd *output_bfd;
2519 struct bfd_link_info *info;
2520 struct bfd_link_hash_entry *harg;
2521 bfd_vma val;
2522 const char *imppath;
2523 const char *impfile;
2524 const char *impmember;
2526 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2528 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2529 return true;
2531 /* A symbol name which starts with a period is the code for a
2532 function. If the symbol is undefined, then add an undefined
2533 symbol for the function descriptor, and import that instead. */
2534 if (h->root.root.string[0] == '.'
2535 && h->root.type == bfd_link_hash_undefined
2536 && val == (bfd_vma) -1)
2538 struct xcoff_link_hash_entry *hds;
2540 hds = h->descriptor;
2541 if (hds == NULL)
2543 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2544 h->root.root.string + 1,
2545 true, false, true);
2546 if (hds == NULL)
2547 return false;
2548 if (hds->root.type == bfd_link_hash_new)
2550 hds->root.type = bfd_link_hash_undefined;
2551 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2553 hds->flags |= XCOFF_DESCRIPTOR;
2554 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2555 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2556 hds->descriptor = h;
2557 h->descriptor = hds;
2560 /* Now, if the descriptor is undefined, import the descriptor
2561 rather than the symbol we were told to import. FIXME: Is
2562 this correct in all cases? */
2563 if (hds->root.type == bfd_link_hash_undefined)
2564 h = hds;
2567 h->flags |= XCOFF_IMPORT;
2569 if (val != (bfd_vma) -1)
2571 if (h->root.type == bfd_link_hash_defined
2572 && (! bfd_is_abs_section (h->root.u.def.section)
2573 || h->root.u.def.value != val))
2575 if (! ((*info->callbacks->multiple_definition)
2576 (info, h->root.root.string, h->root.u.def.section->owner,
2577 h->root.u.def.section, h->root.u.def.value,
2578 output_bfd, bfd_abs_section_ptr, val)))
2579 return false;
2582 h->root.type = bfd_link_hash_defined;
2583 h->root.u.def.section = bfd_abs_section_ptr;
2584 h->root.u.def.value = val;
2587 /* We overload the ldindx field to hold the l_ifile value for this
2588 symbol. */
2589 BFD_ASSERT (h->ldsym == NULL);
2590 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2591 if (imppath == NULL)
2592 h->ldindx = -1;
2593 else
2595 unsigned int c;
2596 struct xcoff_import_file **pp;
2598 /* We start c at 1 because the first entry in the import list is
2599 reserved for the library search path. */
2600 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2601 *pp != NULL;
2602 pp = &(*pp)->next, ++c)
2604 if (strcmp ((*pp)->path, imppath) == 0
2605 && strcmp ((*pp)->file, impfile) == 0
2606 && strcmp ((*pp)->member, impmember) == 0)
2607 break;
2610 if (*pp == NULL)
2612 struct xcoff_import_file *n;
2614 n = ((struct xcoff_import_file *)
2615 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2616 if (n == NULL)
2617 return false;
2618 n->next = NULL;
2619 n->path = imppath;
2620 n->file = impfile;
2621 n->member = impmember;
2622 *pp = n;
2625 h->ldindx = c;
2628 return true;
2631 /* Export a symbol. */
2633 boolean
2634 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2635 bfd *output_bfd;
2636 struct bfd_link_info *info;
2637 struct bfd_link_hash_entry *harg;
2638 boolean syscall ATTRIBUTE_UNUSED;
2640 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2642 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2643 return true;
2645 h->flags |= XCOFF_EXPORT;
2647 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2648 I'm just going to ignore it until somebody explains it. */
2650 /* See if this is a function descriptor. It may be one even though
2651 it is not so marked. */
2652 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2653 && h->root.root.string[0] != '.')
2655 char *fnname;
2656 struct xcoff_link_hash_entry *hfn;
2658 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
2659 if (fnname == NULL)
2660 return false;
2661 fnname[0] = '.';
2662 strcpy (fnname + 1, h->root.root.string);
2663 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2664 fnname, false, false, true);
2665 free (fnname);
2666 if (hfn != NULL
2667 && hfn->smclas == XMC_PR
2668 && (hfn->root.type == bfd_link_hash_defined
2669 || hfn->root.type == bfd_link_hash_defweak))
2671 h->flags |= XCOFF_DESCRIPTOR;
2672 h->descriptor = hfn;
2673 hfn->descriptor = h;
2677 /* Make sure we don't garbage collect this symbol. */
2678 if (! xcoff_mark_symbol (info, h))
2679 return false;
2681 /* If this is a function descriptor, make sure we don't garbage
2682 collect the associated function code. We normally don't have to
2683 worry about this, because the descriptor will be attached to a
2684 section with relocs, but if we are creating the descriptor
2685 ourselves those relocs will not be visible to the mark code. */
2686 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2688 if (! xcoff_mark_symbol (info, h->descriptor))
2689 return false;
2692 return true;
2695 /* Count a reloc against a symbol. This is called for relocs
2696 generated by the linker script, typically for global constructors
2697 and destructors. */
2699 boolean
2700 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2701 bfd *output_bfd;
2702 struct bfd_link_info *info;
2703 const char *name;
2705 struct xcoff_link_hash_entry *h;
2707 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2708 return true;
2710 h = ((struct xcoff_link_hash_entry *)
2711 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
2712 false));
2713 if (h == NULL)
2715 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2716 bfd_set_error (bfd_error_no_symbols);
2717 return false;
2720 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2721 ++xcoff_hash_table (info)->ldrel_count;
2723 /* Mark the symbol to avoid garbage collection. */
2724 if (! xcoff_mark_symbol (info, h))
2725 return false;
2727 return true;
2730 /* This function is called for each symbol to which the linker script
2731 assigns a value. */
2733 boolean
2734 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2735 bfd *output_bfd;
2736 struct bfd_link_info *info;
2737 const char *name;
2739 struct xcoff_link_hash_entry *h;
2741 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2742 return true;
2744 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2745 false);
2746 if (h == NULL)
2747 return false;
2749 h->flags |= XCOFF_DEF_REGULAR;
2751 return true;
2754 /* Build the .loader section. This is called by the XCOFF linker
2755 emulation before_allocation routine. We must set the size of the
2756 .loader section before the linker lays out the output file.
2757 LIBPATH is the library path to search for shared objects; this is
2758 normally built from the -L arguments passed to the linker. ENTRY
2759 is the name of the entry point symbol (the -e linker option).
2760 FILE_ALIGN is the alignment to use for sections within the file
2761 (the -H linker option). MAXSTACK is the maximum stack size (the
2762 -bmaxstack linker option). MAXDATA is the maximum data size (the
2763 -bmaxdata linker option). GC is whether to do garbage collection
2764 (the -bgc linker option). MODTYPE is the module type (the
2765 -bmodtype linker option). TEXTRO is whether the text section must
2766 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2767 whether all defined symbols should be exported (the -unix linker
2768 option). SPECIAL_SECTIONS is set by this routine to csects with
2769 magic names like _end. */
2771 boolean
2772 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2773 file_align, maxstack, maxdata, gc,
2774 modtype, textro, export_defineds,
2775 special_sections)
2776 bfd *output_bfd;
2777 struct bfd_link_info *info;
2778 const char *libpath;
2779 const char *entry;
2780 unsigned long file_align;
2781 unsigned long maxstack;
2782 unsigned long maxdata;
2783 boolean gc;
2784 int modtype;
2785 boolean textro;
2786 boolean export_defineds;
2787 asection **special_sections;
2789 struct xcoff_link_hash_entry *hentry;
2790 asection *lsec;
2791 struct xcoff_loader_info ldinfo;
2792 int i;
2793 size_t impsize, impcount;
2794 struct xcoff_import_file *fl;
2795 struct internal_ldhdr *ldhdr;
2796 bfd_size_type stoff;
2797 register char *out;
2798 asection *sec;
2799 bfd *sub;
2800 struct bfd_strtab_hash *debug_strtab;
2801 bfd_byte *debug_contents = NULL;
2803 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) {
2805 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2806 special_sections[i] = NULL;
2807 return true;
2810 ldinfo.failed = false;
2811 ldinfo.output_bfd = output_bfd;
2812 ldinfo.info = info;
2813 ldinfo.export_defineds = export_defineds;
2814 ldinfo.ldsym_count = 0;
2815 ldinfo.string_size = 0;
2816 ldinfo.strings = NULL;
2817 ldinfo.string_alc = 0;
2819 xcoff_data (output_bfd)->maxstack = maxstack;
2820 xcoff_data (output_bfd)->maxdata = maxdata;
2821 xcoff_data (output_bfd)->modtype = modtype;
2823 xcoff_hash_table (info)->file_align = file_align;
2824 xcoff_hash_table (info)->textro = textro;
2826 if (entry == NULL) {
2827 hentry = NULL;
2828 } else {
2829 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2830 false, false, true);
2831 if (hentry != NULL)
2832 hentry->flags |= XCOFF_ENTRY;
2835 /* __rtinit */
2836 if (info->init_function || info->fini_function) {
2837 struct xcoff_link_hash_entry *hrtinit;
2838 struct internal_ldsym *ldsym;
2840 hrtinit = xcoff_link_hash_lookup (xcoff_hash_table (info),
2841 "__rtinit",
2842 false, false, true);
2843 if (hrtinit != NULL) {
2844 xcoff_mark_symbol (info, hrtinit);
2845 hrtinit->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
2846 } else {
2847 (*_bfd_error_handler)
2848 (_("error: undefined symbol __rtinit"));
2850 return false;
2855 * __rtinit initalized here
2856 * Some information, like the location of the .initfini seciton will
2857 * be filled in later
2859 * name or offset taken case of below with bfd_xcoff_put_ldsymbol_name
2861 ldsym = (struct internal_ldsym *)
2862 bfd_malloc(sizeof (struct internal_ldsym));
2864 ldsym->l_value = 0; /* will be filled in later */
2865 ldsym->l_scnum = 2; /* data section */
2866 ldsym->l_smtype = XTY_SD; /* csect section definition */
2867 ldsym->l_smclas = 5; /* .rw */
2868 ldsym->l_ifile = 0; /* special system loader symbol */
2869 ldsym->l_parm = 0; /* NA */
2872 * Force __rtinit to be the first symbol in the loader symbol table
2873 * See xcoff_build_ldsyms
2875 * The first 3 symbol table indices are reserved to indicate the data,
2876 * text and bss sections.
2878 BFD_ASSERT (0 == ldinfo.ldsym_count);
2880 hrtinit->ldindx = 3;
2881 ldinfo.ldsym_count = 1;
2882 hrtinit->ldsym = ldsym;
2884 if (false == bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
2885 hrtinit->ldsym,
2886 hrtinit->root.root.string)) {
2887 return false;
2891 * This symbol is written out by xcoff_write_global_symbol
2892 * Set stuff up so xcoff_write_global_symbol logic works.
2894 hrtinit->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
2895 hrtinit->root.type = bfd_link_hash_defined;
2896 hrtinit->root.u.def.value = 0;
2899 /* Garbage collect unused sections. */
2900 if (info->relocateable
2901 || ! gc
2902 || hentry == NULL
2903 || (hentry->root.type != bfd_link_hash_defined
2904 && hentry->root.type != bfd_link_hash_defweak))
2906 gc = false;
2907 xcoff_hash_table (info)->gc = false;
2909 /* We still need to call xcoff_mark, in order to set ldrel_count
2910 correctly. */
2911 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2913 asection *o;
2915 for (o = sub->sections; o != NULL; o = o->next)
2917 if ((o->flags & SEC_MARK) == 0)
2919 if (! xcoff_mark (info, o))
2920 goto error_return;
2925 else
2927 if (! xcoff_mark (info, hentry->root.u.def.section))
2928 goto error_return;
2929 xcoff_sweep (info);
2930 xcoff_hash_table (info)->gc = true;
2933 /* Return special sections to the caller. */
2934 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) {
2935 asection *sec;
2937 sec = xcoff_hash_table (info)->special_sections[i];
2939 if ((sec != NULL) &&
2940 (gc && (sec->flags & SEC_MARK) == 0)) {
2941 sec = NULL;
2943 special_sections[i] = sec;
2946 if (info->input_bfds == NULL) {
2947 /* I'm not sure what to do in this bizarre case. */
2948 return true;
2951 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2952 (PTR) &ldinfo);
2953 if (ldinfo.failed)
2954 goto error_return;
2956 /* Work out the size of the import file names. Each import file ID
2957 consists of three null terminated strings: the path, the file
2958 name, and the archive member name. The first entry in the list
2959 of names is the path to use to find objects, which the linker has
2960 passed in as the libpath argument. For some reason, the path
2961 entry in the other import file names appears to always be empty. */
2962 impsize = strlen (libpath) + 3;
2963 impcount = 1;
2964 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2966 ++impcount;
2967 impsize += (strlen (fl->path)
2968 + strlen (fl->file)
2969 + strlen (fl->member)
2970 + 3);
2973 /* Set up the .loader section header. */
2974 ldhdr = &xcoff_hash_table (info)->ldhdr;
2975 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
2976 ldhdr->l_nsyms = ldinfo.ldsym_count;
2977 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2978 ldhdr->l_istlen = impsize;
2979 ldhdr->l_nimpid = impcount;
2980 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
2981 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
2982 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
2983 ldhdr->l_stlen = ldinfo.string_size;
2984 stoff = ldhdr->l_impoff + impsize;
2985 if (ldinfo.string_size == 0)
2986 ldhdr->l_stoff = 0;
2987 else
2988 ldhdr->l_stoff = stoff;
2991 * 64 bit elements to ldhdr
2992 * The swap out routine for 32 bit will ignore them.
2993 * Nothing fancy, symbols come after the header and relocs come after symbols
2995 ldhdr->l_symoff = bfd_xcoff_ldhdrsz(output_bfd);
2996 ldhdr->l_rldoff = bfd_xcoff_ldhdrsz(output_bfd) +
2997 (ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd));
2999 /* We now know the final size of the .loader section. Allocate
3000 space for it. */
3001 lsec = xcoff_hash_table (info)->loader_section;
3002 lsec->_raw_size = stoff + ldhdr->l_stlen;
3003 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3004 if (lsec->contents == NULL)
3005 goto error_return;
3007 /* Set up the header. */
3008 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3010 /* Set up the import file names. */
3011 out = (char *) lsec->contents + ldhdr->l_impoff;
3012 strcpy (out, libpath);
3013 out += strlen (libpath) + 1;
3014 *out++ = '\0';
3015 *out++ = '\0';
3016 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3018 register const char *s;
3020 s = fl->path;
3021 while ((*out++ = *s++) != '\0')
3023 s = fl->file;
3024 while ((*out++ = *s++) != '\0')
3026 s = fl->member;
3027 while ((*out++ = *s++) != '\0')
3031 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3033 /* Set up the symbol string table. */
3034 if (ldinfo.string_size > 0)
3036 memcpy (out, ldinfo.strings, ldinfo.string_size);
3037 free (ldinfo.strings);
3038 ldinfo.strings = NULL;
3041 /* We can't set up the symbol table or the relocs yet, because we
3042 don't yet know the final position of the various sections. The
3043 .loader symbols are written out when the corresponding normal
3044 symbols are written out in xcoff_link_input_bfd or
3045 xcoff_write_global_symbol. The .loader relocs are written out
3046 when the corresponding normal relocs are handled in
3047 xcoff_link_input_bfd.
3050 /* Allocate space for the magic sections. */
3051 sec = xcoff_hash_table (info)->linkage_section;
3052 if (sec->_raw_size > 0)
3054 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3055 if (sec->contents == NULL)
3056 goto error_return;
3058 sec = xcoff_hash_table (info)->toc_section;
3059 if (sec->_raw_size > 0)
3061 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3062 if (sec->contents == NULL)
3063 goto error_return;
3065 sec = xcoff_hash_table (info)->descriptor_section;
3066 if (sec->_raw_size > 0)
3068 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3069 if (sec->contents == NULL)
3070 goto error_return;
3073 /* Now that we've done garbage collection, figure out the contents
3074 of the .debug section. */
3075 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3077 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3079 asection *subdeb;
3080 bfd_size_type symcount;
3081 unsigned long *debug_index;
3082 asection **csectpp;
3083 bfd_byte *esym, *esymend;
3084 bfd_size_type symesz;
3086 if (sub->xvec != info->hash->creator)
3087 continue;
3088 subdeb = bfd_get_section_by_name (sub, ".debug");
3089 if (subdeb == NULL || subdeb->_raw_size == 0)
3090 continue;
3092 if (info->strip == strip_all
3093 || info->strip == strip_debugger
3094 || info->discard == discard_all)
3096 subdeb->_raw_size = 0;
3097 continue;
3100 if (! _bfd_coff_get_external_symbols (sub))
3101 goto error_return;
3103 symcount = obj_raw_syment_count (sub);
3104 debug_index = ((unsigned long *)
3105 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3106 if (debug_index == NULL)
3107 goto error_return;
3108 xcoff_data (sub)->debug_indices = debug_index;
3110 /* Grab the contents of the .debug section. We use malloc and
3111 copy the names into the debug stringtab, rather than
3112 bfd_alloc, because I expect that, when linking many files
3113 together, many of the strings will be the same. Storing the
3114 strings in the hash table should save space in this case. */
3115 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3116 if (debug_contents == NULL)
3117 goto error_return;
3118 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3119 (file_ptr) 0, subdeb->_raw_size))
3120 goto error_return;
3122 csectpp = xcoff_data (sub)->csects;
3124 symesz = bfd_coff_symesz (sub);
3125 esym = (bfd_byte *) obj_coff_external_syms (sub);
3126 esymend = esym + symcount * symesz;
3127 while (esym < esymend)
3129 struct internal_syment sym;
3131 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3133 *debug_index = (unsigned long) -1;
3135 if (sym._n._n_n._n_zeroes == 0
3136 && *csectpp != NULL
3137 && (! gc
3138 || ((*csectpp)->flags & SEC_MARK) != 0
3139 || *csectpp == bfd_abs_section_ptr)
3140 && bfd_coff_symname_in_debug (sub, &sym))
3142 char *name;
3143 bfd_size_type indx;
3145 name = (char *) debug_contents + sym._n._n_n._n_offset;
3146 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3147 if (indx == (bfd_size_type) -1)
3148 goto error_return;
3149 *debug_index = indx;
3152 esym += (sym.n_numaux + 1) * symesz;
3153 csectpp += sym.n_numaux + 1;
3154 debug_index += sym.n_numaux + 1;
3157 free (debug_contents);
3158 debug_contents = NULL;
3160 /* Clear the size of subdeb, so that it is not included directly
3161 in the output file. */
3162 subdeb->_raw_size = 0;
3164 if (! info->keep_memory)
3166 if (! _bfd_coff_free_symbols (sub))
3167 goto error_return;
3171 if (info->strip != strip_all)
3172 xcoff_hash_table (info)->debug_section->_raw_size =
3173 _bfd_stringtab_size (debug_strtab);
3175 return true;
3177 error_return:
3178 if (ldinfo.strings != NULL)
3179 free (ldinfo.strings);
3180 if (debug_contents != NULL)
3181 free (debug_contents);
3182 return false;
3185 /* Add a symbol to the .loader symbols, if necessary. */
3187 static boolean
3188 xcoff_build_ldsyms (h, p)
3189 struct xcoff_link_hash_entry *h;
3190 PTR p;
3192 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3195 * __rtinit
3196 * Special handling of this symbol to make is the first symbol in the
3197 * loader symbol table. Make sure this pass through does not undo it
3199 if (h->flags & XCOFF_RTINIT) {
3200 return true;
3203 /* If this is a final link, and the symbol was defined as a common
3204 symbol in a regular object file, and there was no definition in
3205 any dynamic object, then the linker will have allocated space for
3206 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3207 will not have been set. */
3208 if (h->root.type == bfd_link_hash_defined
3209 && (h->flags & XCOFF_DEF_REGULAR) == 0
3210 && (h->flags & XCOFF_REF_REGULAR) != 0
3211 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3212 && (bfd_is_abs_section (h->root.u.def.section)
3213 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3214 h->flags |= XCOFF_DEF_REGULAR;
3216 /* If all defined symbols should be exported, mark them now. We
3217 don't want to export the actual functions, just the function
3218 descriptors. */
3219 if (ldinfo->export_defineds
3220 && (h->flags & XCOFF_DEF_REGULAR) != 0
3221 && h->root.root.string[0] != '.')
3223 boolean export;
3225 /* We don't export a symbol which is being defined by an object
3226 included from an archive which contains a shared object. The
3227 rationale is that if an archive contains both an unshared and
3228 a shared object, then there must be some reason that the
3229 unshared object is unshared, and we don't want to start
3230 providing a shared version of it. In particular, this solves
3231 a bug involving the _savefNN set of functions. gcc will call
3232 those functions without providing a slot to restore the TOC,
3233 so it is essential that these functions be linked in directly
3234 and not from a shared object, which means that a shared
3235 object which also happens to link them in must not export
3236 them. This is confusing, but I haven't been able to think of
3237 a different approach. Note that the symbols can, of course,
3238 be exported explicitly. */
3239 export = true;
3240 if ((h->root.type == bfd_link_hash_defined
3241 || h->root.type == bfd_link_hash_defweak)
3242 && h->root.u.def.section->owner != NULL
3243 && h->root.u.def.section->owner->my_archive != NULL)
3245 bfd *arbfd, *member;
3247 arbfd = h->root.u.def.section->owner->my_archive;
3248 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3249 while (member != NULL)
3251 if ((member->flags & DYNAMIC) != 0)
3253 export = false;
3254 break;
3256 member = bfd_openr_next_archived_file (arbfd, member);
3260 if (export)
3261 h->flags |= XCOFF_EXPORT;
3264 /* We don't want to garbage collect symbols which are not defined in
3265 XCOFF files. This is a convenient place to mark them. */
3266 if (xcoff_hash_table (ldinfo->info)->gc
3267 && (h->flags & XCOFF_MARK) == 0
3268 && (h->root.type == bfd_link_hash_defined
3269 || h->root.type == bfd_link_hash_defweak)
3270 && (h->root.u.def.section->owner == NULL
3271 || (h->root.u.def.section->owner->xvec
3272 != ldinfo->info->hash->creator)))
3273 h->flags |= XCOFF_MARK;
3275 /* If this symbol is called and defined in a dynamic object, or it
3276 is imported, then we need to set up global linkage code for it.
3277 (Unless we did garbage collection and we didn't need this
3278 symbol.) */
3279 if ((h->flags & XCOFF_CALLED) != 0
3280 && (h->root.type == bfd_link_hash_undefined
3281 || h->root.type == bfd_link_hash_undefweak)
3282 && h->root.root.string[0] == '.'
3283 && h->descriptor != NULL
3284 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3285 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3286 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3287 && (! xcoff_hash_table (ldinfo->info)->gc
3288 || (h->flags & XCOFF_MARK) != 0))
3290 asection *sec;
3291 struct xcoff_link_hash_entry *hds;
3293 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3294 h->root.type = bfd_link_hash_defined;
3295 h->root.u.def.section = sec;
3296 h->root.u.def.value = sec->_raw_size;
3297 h->smclas = XMC_GL;
3298 h->flags |= XCOFF_DEF_REGULAR;
3299 sec->_raw_size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
3301 /* The global linkage code requires a TOC entry for the
3302 descriptor. */
3303 hds = h->descriptor;
3304 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3305 || hds->root.type == bfd_link_hash_undefweak)
3306 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3307 hds->flags |= XCOFF_MARK;
3308 if (hds->toc_section == NULL) {
3309 int byte_size;
3312 * 32 vs 64
3313 * xcoff32 uses 4 bytes in the toc
3314 * xcoff64 uses 8 bytes in the toc
3316 if (bfd_xcoff_is_xcoff64(ldinfo->output_bfd)) {
3317 byte_size = 8;
3318 } else if (bfd_xcoff_is_xcoff32(ldinfo->output_bfd)) {
3319 byte_size = 4;
3320 } else {
3321 return false;
3324 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3325 hds->u.toc_offset = hds->toc_section->_raw_size;
3326 hds->toc_section->_raw_size += byte_size;
3327 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3328 ++hds->toc_section->reloc_count;
3329 hds->indx = -2;
3330 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3333 * We need to call xcoff_build_ldsyms recursively here,
3334 * because we may already have passed hds on the traversal.
3336 xcoff_build_ldsyms (hds, p);
3340 /* If this symbol is exported, but not defined, we need to try to
3341 define it. */
3342 if ((h->flags & XCOFF_EXPORT) != 0
3343 && (h->flags & XCOFF_IMPORT) == 0
3344 && (h->flags & XCOFF_DEF_REGULAR) == 0
3345 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3346 && (h->root.type == bfd_link_hash_undefined
3347 || h->root.type == bfd_link_hash_undefweak))
3349 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3350 && (h->descriptor->root.type == bfd_link_hash_defined
3351 || h->descriptor->root.type == bfd_link_hash_defweak))
3353 asection *sec;
3355 /* This is an undefined function descriptor associated with
3356 a defined entry point. We can build up a function
3357 descriptor ourselves. Believe it or not, the AIX linker
3358 actually does this, and there are cases where we need to
3359 do it as well. */
3360 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3361 h->root.type = bfd_link_hash_defined;
3362 h->root.u.def.section = sec;
3363 h->root.u.def.value = sec->_raw_size;
3364 h->smclas = XMC_DS;
3365 h->flags |= XCOFF_DEF_REGULAR;
3368 * The size of the function descriptor depends if this is an
3369 * xcoff32 (12) or xcoff64 (24)
3371 sec->_raw_size +=
3372 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
3375 * A function descriptor uses two relocs: one for the
3376 * associated code, and one for the TOC address.
3378 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3379 sec->reloc_count += 2;
3382 * We handle writing out the contents of the descriptor in
3383 * xcoff_write_global_symbol.
3386 else
3388 (*_bfd_error_handler)
3389 (_("warning: attempt to export undefined symbol `%s'"),
3390 h->root.root.string);
3391 h->ldsym = NULL;
3392 return true;
3396 /* If this is still a common symbol, and it wasn't garbage
3397 collected, we need to actually allocate space for it in the .bss
3398 section. */
3399 if (h->root.type == bfd_link_hash_common
3400 && (! xcoff_hash_table (ldinfo->info)->gc
3401 || (h->flags & XCOFF_MARK) != 0)
3402 && h->root.u.c.p->section->_raw_size == 0)
3404 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3405 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3408 /* We need to add a symbol to the .loader section if it is mentioned
3409 in a reloc which we are copying to the .loader section and it was
3410 not defined or common, or if it is the entry point, or if it is
3411 being exported. */
3413 if (((h->flags & XCOFF_LDREL) == 0
3414 || h->root.type == bfd_link_hash_defined
3415 || h->root.type == bfd_link_hash_defweak
3416 || h->root.type == bfd_link_hash_common)
3417 && (h->flags & XCOFF_ENTRY) == 0
3418 && (h->flags & XCOFF_EXPORT) == 0)
3420 h->ldsym = NULL;
3421 return true;
3424 /* We don't need to add this symbol if we did garbage collection and
3425 we did not mark this symbol. */
3426 if (xcoff_hash_table (ldinfo->info)->gc
3427 && (h->flags & XCOFF_MARK) == 0)
3429 h->ldsym = NULL;
3430 return true;
3433 /* We may have already processed this symbol due to the recursive
3434 call above. */
3435 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3436 return true;
3438 /* We need to add this symbol to the .loader symbols. */
3440 BFD_ASSERT (h->ldsym == NULL);
3441 h->ldsym = ((struct internal_ldsym *)
3442 bfd_zalloc (ldinfo->output_bfd,
3443 sizeof (struct internal_ldsym)));
3444 if (h->ldsym == NULL)
3446 ldinfo->failed = true;
3447 return false;
3450 if ((h->flags & XCOFF_IMPORT) != 0)
3451 h->ldsym->l_ifile = h->ldindx;
3454 * The first 3 symbol table indices are reserved to indicate the data,
3455 * text and bss sections.
3457 h->ldindx = ldinfo->ldsym_count + 3;
3459 ++ldinfo->ldsym_count;
3461 if (false == bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3462 h->ldsym,
3463 h->root.root.string)) {
3464 return false;
3467 h->flags |= XCOFF_BUILT_LDSYM;
3469 return true;
3472 /* Do the final link step. */
3474 boolean
3475 _bfd_xcoff_bfd_final_link (abfd, info)
3476 bfd *abfd;
3477 struct bfd_link_info *info;
3479 bfd_size_type symesz;
3480 struct xcoff_final_link_info finfo;
3481 asection *o;
3482 struct bfd_link_order *p;
3483 size_t max_contents_size;
3484 size_t max_sym_count;
3485 size_t max_lineno_count;
3486 size_t max_reloc_count;
3487 size_t max_output_reloc_count;
3488 file_ptr rel_filepos;
3489 unsigned int relsz;
3490 file_ptr line_filepos;
3491 unsigned int linesz;
3492 bfd *sub;
3493 bfd_byte *external_relocs = NULL;
3494 char strbuf[STRING_SIZE_SIZE];
3496 if (info->shared)
3497 abfd->flags |= DYNAMIC;
3499 symesz = bfd_coff_symesz (abfd);
3501 finfo.info = info;
3502 finfo.output_bfd = abfd;
3503 finfo.strtab = NULL;
3504 finfo.section_info = NULL;
3505 finfo.last_file_index = -1;
3506 finfo.toc_symindx = -1;
3507 finfo.internal_syms = NULL;
3508 finfo.sym_indices = NULL;
3509 finfo.outsyms = NULL;
3510 finfo.linenos = NULL;
3511 finfo.contents = NULL;
3512 finfo.external_relocs = NULL;
3514 finfo.ldsym = xcoff_hash_table (info)->loader_section->contents +
3515 bfd_xcoff_ldhdrsz(abfd);
3516 finfo.ldrel = xcoff_hash_table (info)->loader_section->contents +
3517 bfd_xcoff_ldhdrsz(abfd) +
3518 xcoff_hash_table (info)->ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
3520 xcoff_data (abfd)->coff.link_info = info;
3522 finfo.strtab = _bfd_stringtab_init ();
3523 if (finfo.strtab == NULL)
3524 goto error_return;
3526 /* Count the line number and relocation entries required for the
3527 output file. Determine a few maximum sizes. */
3528 max_contents_size = 0;
3529 max_lineno_count = 0;
3530 max_reloc_count = 0;
3531 for (o = abfd->sections; o != NULL; o = o->next)
3533 o->reloc_count = 0;
3534 o->lineno_count = 0;
3535 for (p = o->link_order_head; p != NULL; p = p->next)
3537 if (p->type == bfd_indirect_link_order)
3539 asection *sec;
3541 sec = p->u.indirect.section;
3543 /* Mark all sections which are to be included in the
3544 link. This will normally be every section. We need
3545 to do this so that we can identify any sections which
3546 the linker has decided to not include. */
3547 sec->linker_mark = true;
3549 if (info->strip == strip_none
3550 || info->strip == strip_some)
3551 o->lineno_count += sec->lineno_count;
3553 o->reloc_count += sec->reloc_count;
3555 if (sec->_raw_size > max_contents_size)
3556 max_contents_size = sec->_raw_size;
3557 if (sec->lineno_count > max_lineno_count)
3558 max_lineno_count = sec->lineno_count;
3559 if (coff_section_data (sec->owner, sec) != NULL
3560 && xcoff_section_data (sec->owner, sec) != NULL
3561 && (xcoff_section_data (sec->owner, sec)->lineno_count
3562 > max_lineno_count))
3563 max_lineno_count =
3564 xcoff_section_data (sec->owner, sec)->lineno_count;
3565 if (sec->reloc_count > max_reloc_count)
3566 max_reloc_count = sec->reloc_count;
3568 else if (p->type == bfd_section_reloc_link_order
3569 || p->type == bfd_symbol_reloc_link_order)
3570 ++o->reloc_count;
3574 /* Compute the file positions for all the sections. */
3575 if (abfd->output_has_begun)
3577 if (xcoff_hash_table (info)->file_align != 0)
3578 abort ();
3580 else
3582 bfd_vma file_align;
3584 file_align = xcoff_hash_table (info)->file_align;
3585 if (file_align != 0)
3587 boolean saw_contents;
3588 int indx;
3589 asection **op;
3590 file_ptr sofar;
3592 /* Insert .pad sections before every section which has
3593 contents and is loaded, if it is preceded by some other
3594 section which has contents and is loaded. */
3595 saw_contents = true;
3596 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3598 if (strcmp ((*op)->name, ".pad") == 0)
3599 saw_contents = false;
3600 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3601 && ((*op)->flags & SEC_LOAD) != 0)
3603 if (! saw_contents)
3604 saw_contents = true;
3605 else
3607 asection *n, *hold;
3609 hold = *op;
3610 *op = NULL;
3611 n = bfd_make_section_anyway (abfd, ".pad");
3612 BFD_ASSERT (*op == n);
3613 n->next = hold;
3614 n->flags = SEC_HAS_CONTENTS;
3615 n->alignment_power = 0;
3616 saw_contents = false;
3621 /* Reset the section indices after inserting the new
3622 sections. */
3623 indx = 0;
3624 for (o = abfd->sections; o != NULL; o = o->next)
3626 ++indx;
3627 o->target_index = indx;
3629 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3631 /* Work out appropriate sizes for the .pad sections to force
3632 each section to land on a page boundary. This bit of
3633 code knows what compute_section_file_positions is going
3634 to do. */
3635 sofar = bfd_coff_filhsz (abfd);
3636 sofar += bfd_coff_aoutsz (abfd);
3637 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3638 for (o = abfd->sections; o != NULL; o = o->next) {
3639 if ((true ==
3640 bfd_xcoff_is_reloc_count_overflow(abfd, o->reloc_count)) ||
3641 (true ==
3642 bfd_xcoff_is_lineno_count_overflow(abfd, o->lineno_count))) {
3644 /* 64 does not overflow, need to check if 32 does */
3645 sofar += bfd_coff_scnhsz (abfd);
3648 for (o = abfd->sections; o != NULL; o = o->next)
3650 if (strcmp (o->name, ".pad") == 0)
3652 bfd_vma pageoff;
3654 BFD_ASSERT (o->_raw_size == 0);
3655 pageoff = sofar & (file_align - 1);
3656 if (pageoff != 0)
3658 o->_raw_size = file_align - pageoff;
3659 sofar += file_align - pageoff;
3660 o->flags |= SEC_HAS_CONTENTS;
3663 else
3665 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3666 sofar += BFD_ALIGN (o->_raw_size,
3667 1 << o->alignment_power);
3672 if (! bfd_coff_compute_section_file_positions (abfd))
3673 goto error_return;
3676 /* Allocate space for the pointers we need to keep for the relocs. */
3678 unsigned int i;
3680 /* We use section_count + 1, rather than section_count, because
3681 the target_index fields are 1 based. */
3682 finfo.section_info =
3683 ((struct xcoff_link_section_info *)
3684 bfd_malloc ((abfd->section_count + 1)
3685 * sizeof (struct xcoff_link_section_info)));
3686 if (finfo.section_info == NULL)
3687 goto error_return;
3688 for (i = 0; i <= abfd->section_count; i++)
3690 finfo.section_info[i].relocs = NULL;
3691 finfo.section_info[i].rel_hashes = NULL;
3692 finfo.section_info[i].toc_rel_hashes = NULL;
3696 /* Set the file positions for the relocs. */
3697 rel_filepos = obj_relocbase (abfd);
3698 relsz = bfd_coff_relsz (abfd);
3699 max_output_reloc_count = 0;
3700 for (o = abfd->sections; o != NULL; o = o->next)
3702 if (o->reloc_count == 0)
3703 o->rel_filepos = 0;
3704 else
3706 /* A stripped file has no relocs. However, we still
3707 allocate the buffers, so that later code doesn't have to
3708 worry about whether we are stripping or not. */
3709 if (info->strip == strip_all)
3710 o->rel_filepos = 0;
3711 else
3713 o->flags |= SEC_RELOC;
3714 o->rel_filepos = rel_filepos;
3715 rel_filepos += o->reloc_count * relsz;
3718 /* We don't know the indices of global symbols until we have
3719 written out all the local symbols. For each section in
3720 the output file, we keep an array of pointers to hash
3721 table entries. Each entry in the array corresponds to a
3722 reloc. When we find a reloc against a global symbol, we
3723 set the corresponding entry in this array so that we can
3724 fix up the symbol index after we have written out all the
3725 local symbols.
3727 Because of this problem, we also keep the relocs in
3728 memory until the end of the link. This wastes memory.
3729 We could backpatch the file later, I suppose, although it
3730 would be slow. */
3731 finfo.section_info[o->target_index].relocs =
3732 ((struct internal_reloc *)
3733 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
3734 finfo.section_info[o->target_index].rel_hashes =
3735 ((struct xcoff_link_hash_entry **)
3736 bfd_malloc (o->reloc_count
3737 * sizeof (struct xcoff_link_hash_entry *)));
3738 if (finfo.section_info[o->target_index].relocs == NULL
3739 || finfo.section_info[o->target_index].rel_hashes == NULL)
3740 goto error_return;
3742 if (o->reloc_count > max_output_reloc_count)
3743 max_output_reloc_count = o->reloc_count;
3747 /* We now know the size of the relocs, so we can determine the file
3748 positions of the line numbers. */
3749 line_filepos = rel_filepos;
3750 finfo.line_filepos = line_filepos;
3751 linesz = bfd_coff_linesz (abfd);
3752 for (o = abfd->sections; o != NULL; o = o->next)
3754 if (o->lineno_count == 0)
3755 o->line_filepos = 0;
3756 else
3758 o->line_filepos = line_filepos;
3759 line_filepos += o->lineno_count * linesz;
3762 /* Reset the reloc and lineno counts, so that we can use them to
3763 count the number of entries we have output so far. */
3764 o->reloc_count = 0;
3765 o->lineno_count = 0;
3768 obj_sym_filepos (abfd) = line_filepos;
3770 /* Figure out the largest number of symbols in an input BFD. Take
3771 the opportunity to clear the output_has_begun fields of all the
3772 input BFD's. We want at least 6 symbols, since that is the
3773 number which xcoff_write_global_symbol may need. */
3774 max_sym_count = 6;
3775 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3777 size_t sz;
3779 sub->output_has_begun = false;
3780 sz = obj_raw_syment_count (sub);
3781 if (sz > max_sym_count)
3782 max_sym_count = sz;
3785 /* Allocate some buffers used while linking. */
3786 finfo.internal_syms = ((struct internal_syment *)
3787 bfd_malloc (max_sym_count
3788 * sizeof (struct internal_syment)));
3790 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
3791 finfo.outsyms = ((bfd_byte *)
3792 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
3793 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
3794 * bfd_coff_linesz (abfd));
3795 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3796 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
3797 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3798 || (finfo.sym_indices == NULL && max_sym_count > 0)
3799 || finfo.outsyms == NULL
3800 || (finfo.linenos == NULL && max_lineno_count > 0)
3801 || (finfo.contents == NULL && max_contents_size > 0)
3802 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3803 goto error_return;
3805 obj_raw_syment_count (abfd) = 0;
3806 xcoff_data (abfd)->toc = (bfd_vma) -1;
3808 /* We now know the position of everything in the file, except that
3809 we don't know the size of the symbol table and therefore we don't
3810 know where the string table starts. We just build the string
3811 table in memory as we go along. We process all the relocations
3812 for a single input file at once. */
3813 for (o = abfd->sections; o != NULL; o = o->next)
3815 for (p = o->link_order_head; p != NULL; p = p->next)
3817 if (p->type == bfd_indirect_link_order
3818 && p->u.indirect.section->owner->xvec == abfd->xvec)
3820 sub = p->u.indirect.section->owner;
3821 if (! sub->output_has_begun)
3823 if (! xcoff_link_input_bfd (&finfo, sub))
3824 goto error_return;
3825 sub->output_has_begun = true;
3828 else if (p->type == bfd_section_reloc_link_order
3829 || p->type == bfd_symbol_reloc_link_order)
3831 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3832 goto error_return;
3834 else
3836 if (! _bfd_default_link_order (abfd, info, o, p))
3837 goto error_return;
3843 /* Free up the buffers used by xcoff_link_input_bfd. */
3845 if (finfo.internal_syms != NULL) {
3846 free (finfo.internal_syms);
3847 finfo.internal_syms = NULL;
3849 if (finfo.sym_indices != NULL) {
3850 free (finfo.sym_indices);
3851 finfo.sym_indices = NULL;
3853 if (finfo.linenos != NULL) {
3854 free (finfo.linenos);
3855 finfo.linenos = NULL;
3857 if (finfo.contents != NULL) {
3858 free (finfo.contents);
3859 finfo.contents = NULL;
3861 if (finfo.external_relocs != NULL) {
3862 free (finfo.external_relocs);
3863 finfo.external_relocs = NULL;
3866 /* The value of the last C_FILE symbol is supposed to be -1. Write
3867 it out again. */
3868 if (finfo.last_file_index != -1) {
3869 finfo.last_file.n_value = -1;
3870 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3871 (PTR) finfo.outsyms);
3872 if (bfd_seek (abfd,
3873 (obj_sym_filepos (abfd)
3874 + finfo.last_file_index * symesz),
3875 SEEK_SET) != 0
3876 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3877 goto error_return;
3880 /* init-fini */
3881 if (info->init_function || info->fini_function) {
3882 struct xcoff_link_hash_entry *hrtinit;
3884 hrtinit = xcoff_link_hash_lookup (xcoff_hash_table (info),
3885 "__rtinit",
3886 false, false, true);
3890 /* Write out all the global symbols which do not come from XCOFF
3891 input files. */
3892 xcoff_link_hash_traverse (xcoff_hash_table (info),
3893 xcoff_write_global_symbol,
3894 (PTR) &finfo);
3896 if (finfo.outsyms != NULL)
3898 free (finfo.outsyms);
3899 finfo.outsyms = NULL;
3902 /* Now that we have written out all the global symbols, we know the
3903 symbol indices to use for relocs against them, and we can finally
3904 write out the relocs. */
3905 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
3906 if (external_relocs == NULL && max_output_reloc_count != 0)
3907 goto error_return;
3909 for (o = abfd->sections; o != NULL; o = o->next) {
3910 struct internal_reloc *irel;
3911 struct internal_reloc *irelend;
3912 struct xcoff_link_hash_entry **rel_hash;
3913 struct xcoff_toc_rel_hash *toc_rel_hash;
3914 bfd_byte *erel;
3916 /* A stripped file has no relocs. */
3917 if (info->strip == strip_all) {
3918 o->reloc_count = 0;
3919 continue;
3922 if (o->reloc_count == 0)
3923 continue;
3925 irel = finfo.section_info[o->target_index].relocs;
3926 irelend = irel + o->reloc_count;
3927 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3928 for (; irel < irelend; irel++, rel_hash++, erel += relsz) {
3929 if (*rel_hash != NULL) {
3930 if ((*rel_hash)->indx < 0) {
3931 if (! ((*info->callbacks->unattached_reloc)
3932 (info, (*rel_hash)->root.root.string,
3933 (bfd *) NULL, o, irel->r_vaddr)))
3934 goto error_return;
3935 (*rel_hash)->indx = 0;
3937 irel->r_symndx = (*rel_hash)->indx;
3941 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
3942 toc_rel_hash != NULL;
3943 toc_rel_hash = toc_rel_hash->next) {
3944 if (toc_rel_hash->h->u.toc_indx < 0) {
3945 if (! ((*info->callbacks->unattached_reloc)
3946 (info, toc_rel_hash->h->root.root.string,
3947 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
3948 goto error_return;
3949 toc_rel_hash->h->u.toc_indx = 0;
3951 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
3954 /* XCOFF requires that the relocs be sorted by address. We tend
3955 to produce them in the order in which their containing csects
3956 appear in the symbol table, which is not necessarily by
3957 address. So we sort them here. There may be a better way to
3958 do this. */
3959 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3960 o->reloc_count, sizeof (struct internal_reloc),
3961 xcoff_sort_relocs);
3963 irel = finfo.section_info[o->target_index].relocs;
3964 irelend = irel + o->reloc_count;
3965 erel = external_relocs;
3966 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3967 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3969 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3970 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3971 abfd) != relsz * o->reloc_count)
3972 goto error_return;
3975 if (external_relocs != NULL) {
3976 free (external_relocs);
3977 external_relocs = NULL;
3980 /* Free up the section information. */
3981 if (finfo.section_info != NULL) {
3982 unsigned int i;
3984 for (i = 0; i < abfd->section_count; i++) {
3985 if (finfo.section_info[i].relocs != NULL)
3986 free (finfo.section_info[i].relocs);
3987 if (finfo.section_info[i].rel_hashes != NULL)
3988 free (finfo.section_info[i].rel_hashes);
3990 free (finfo.section_info);
3991 finfo.section_info = NULL;
3994 /* Write out the loader section contents. */
3995 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3996 == (xcoff_hash_table (info)->loader_section->contents
3997 + xcoff_hash_table (info)->ldhdr.l_impoff));
3998 o = xcoff_hash_table (info)->loader_section;
3999 if (! bfd_set_section_contents (abfd, o->output_section,
4000 o->contents, o->output_offset,
4001 o->_raw_size))
4002 goto error_return;
4004 /* Write out the magic sections. */
4005 o = xcoff_hash_table (info)->linkage_section;
4006 if (o->_raw_size > 0
4007 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4008 o->output_offset, o->_raw_size))
4009 goto error_return;
4010 o = xcoff_hash_table (info)->toc_section;
4011 if (o->_raw_size > 0
4012 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4013 o->output_offset, o->_raw_size))
4014 goto error_return;
4015 o = xcoff_hash_table (info)->descriptor_section;
4016 if (o->_raw_size > 0
4017 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4018 o->output_offset, o->_raw_size))
4019 goto error_return;
4021 /* Write out the string table. */
4022 if (bfd_seek (abfd,
4023 (obj_sym_filepos (abfd)
4024 + obj_raw_syment_count (abfd) * symesz),
4025 SEEK_SET) != 0)
4026 goto error_return;
4027 bfd_h_put_32 (abfd,
4028 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4029 (bfd_byte *) strbuf);
4030 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4031 goto error_return;
4032 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4033 goto error_return;
4035 _bfd_stringtab_free (finfo.strtab);
4037 /* Write out the debugging string table. */
4038 o = xcoff_hash_table (info)->debug_section;
4039 if (o != NULL) {
4040 struct bfd_strtab_hash *debug_strtab;
4042 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4043 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4044 >= _bfd_stringtab_size (debug_strtab));
4045 if (bfd_seek (abfd,
4046 o->output_section->filepos + o->output_offset,
4047 SEEK_SET) != 0)
4048 goto error_return;
4049 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4050 goto error_return;
4053 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4054 not try to write out the symbols. */
4055 bfd_get_symcount (abfd) = 0;
4057 return true;
4059 error_return:
4060 if (finfo.strtab != NULL)
4061 _bfd_stringtab_free (finfo.strtab);
4063 if (finfo.section_info != NULL) {
4064 unsigned int i;
4066 for (i = 0; i < abfd->section_count; i++) {
4067 if (finfo.section_info[i].relocs != NULL)
4068 free (finfo.section_info[i].relocs);
4069 if (finfo.section_info[i].rel_hashes != NULL)
4070 free (finfo.section_info[i].rel_hashes);
4072 free (finfo.section_info);
4075 if (finfo.internal_syms != NULL)
4076 free (finfo.internal_syms);
4077 if (finfo.sym_indices != NULL)
4078 free (finfo.sym_indices);
4079 if (finfo.outsyms != NULL)
4080 free (finfo.outsyms);
4081 if (finfo.linenos != NULL)
4082 free (finfo.linenos);
4083 if (finfo.contents != NULL)
4084 free (finfo.contents);
4085 if (finfo.external_relocs != NULL)
4086 free (finfo.external_relocs);
4087 if (external_relocs != NULL)
4088 free (external_relocs);
4089 return false;
4092 /* Link an input file into the linker output file. This function
4093 handles all the sections and relocations of the input file at once. */
4095 static boolean
4096 xcoff_link_input_bfd (finfo, input_bfd)
4097 struct xcoff_final_link_info *finfo;
4098 bfd *input_bfd;
4100 bfd *output_bfd;
4101 const char *strings;
4102 bfd_size_type syment_base;
4103 unsigned int n_tmask;
4104 unsigned int n_btshft;
4105 boolean copy, hash;
4106 bfd_size_type isymesz;
4107 bfd_size_type osymesz;
4108 bfd_size_type linesz;
4109 bfd_byte *esym;
4110 bfd_byte *esym_end;
4111 struct xcoff_link_hash_entry **sym_hash;
4112 struct internal_syment *isymp;
4113 asection **csectpp;
4114 unsigned long *debug_index;
4115 long *indexp;
4116 unsigned long output_index;
4117 bfd_byte *outsym;
4118 unsigned int incls;
4119 asection *oline;
4120 boolean keep_syms;
4121 asection *o;
4123 /* We can just skip DYNAMIC files, unless this is a static link. */
4124 if ((input_bfd->flags & DYNAMIC) != 0
4125 && ! finfo->info->static_link)
4126 return true;
4128 /* Move all the symbols to the output file. */
4130 output_bfd = finfo->output_bfd;
4131 strings = NULL;
4132 syment_base = obj_raw_syment_count (output_bfd);
4133 isymesz = bfd_coff_symesz (input_bfd);
4134 osymesz = bfd_coff_symesz (output_bfd);
4135 linesz = bfd_coff_linesz (input_bfd);
4136 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4138 n_tmask = coff_data (input_bfd)->local_n_tmask;
4139 n_btshft = coff_data (input_bfd)->local_n_btshft;
4141 /* Define macros so that ISFCN, et. al., macros work correctly. */
4142 #define N_TMASK n_tmask
4143 #define N_BTSHFT n_btshft
4145 copy = false;
4146 if (! finfo->info->keep_memory)
4147 copy = true;
4148 hash = true;
4149 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4150 hash = false;
4152 if (! _bfd_coff_get_external_symbols (input_bfd))
4153 return false;
4155 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4156 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4157 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4158 csectpp = xcoff_data (input_bfd)->csects;
4159 debug_index = xcoff_data (input_bfd)->debug_indices;
4160 isymp = finfo->internal_syms;
4161 indexp = finfo->sym_indices;
4162 output_index = syment_base;
4163 outsym = finfo->outsyms;
4164 incls = 0;
4165 oline = NULL;
4167 while (esym < esym_end)
4170 struct internal_syment isym;
4171 union internal_auxent aux;
4172 int smtyp = 0;
4173 boolean skip;
4174 boolean require;
4175 int add;
4177 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4179 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4180 information. */
4181 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4183 BFD_ASSERT (isymp->n_numaux > 0);
4184 bfd_coff_swap_aux_in (input_bfd,
4185 (PTR) (esym + isymesz * isymp->n_numaux),
4186 isymp->n_type, isymp->n_sclass,
4187 isymp->n_numaux - 1, isymp->n_numaux,
4188 (PTR) &aux);
4190 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4193 /* Make a copy of *isymp so that the relocate_section function
4194 always sees the original values. This is more reliable than
4195 always recomputing the symbol value even if we are stripping
4196 the symbol. */
4197 isym = *isymp;
4199 /* If this symbol is in the .loader section, swap out the
4200 .loader symbol information. If this is an external symbol
4201 reference to a defined symbol, though, then wait until we get
4202 to the definition. */
4203 if (isym.n_sclass == C_EXT
4204 && *sym_hash != NULL
4205 && (*sym_hash)->ldsym != NULL
4206 && (smtyp != XTY_ER
4207 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4209 struct xcoff_link_hash_entry *h;
4210 struct internal_ldsym *ldsym;
4212 h = *sym_hash;
4213 ldsym = h->ldsym;
4214 if (isym.n_scnum > 0)
4216 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4217 ldsym->l_value = (isym.n_value
4218 + (*csectpp)->output_section->vma
4219 + (*csectpp)->output_offset
4220 - (*csectpp)->vma);
4222 else
4224 ldsym->l_scnum = isym.n_scnum;
4225 ldsym->l_value = isym.n_value;
4228 ldsym->l_smtype = smtyp;
4229 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4230 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4231 || (h->flags & XCOFF_IMPORT) != 0)
4232 ldsym->l_smtype |= L_IMPORT;
4233 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4234 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4235 || (h->flags & XCOFF_EXPORT) != 0)
4236 ldsym->l_smtype |= L_EXPORT;
4237 if ((h->flags & XCOFF_ENTRY) != 0)
4238 ldsym->l_smtype |= L_ENTRY;
4240 ldsym->l_smclas = aux.x_csect.x_smclas;
4242 if (ldsym->l_ifile == (bfd_size_type) -1)
4243 ldsym->l_ifile = 0;
4244 else if (ldsym->l_ifile == 0)
4246 if ((ldsym->l_smtype & L_IMPORT) == 0)
4247 ldsym->l_ifile = 0;
4248 else
4250 bfd *impbfd;
4252 if (h->root.type == bfd_link_hash_defined
4253 || h->root.type == bfd_link_hash_defweak)
4254 impbfd = h->root.u.def.section->owner;
4255 else if (h->root.type == bfd_link_hash_undefined
4256 || h->root.type == bfd_link_hash_undefweak)
4257 impbfd = h->root.u.undef.abfd;
4258 else
4259 impbfd = NULL;
4261 if (impbfd == NULL)
4262 ldsym->l_ifile = 0;
4263 else
4265 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4266 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4271 ldsym->l_parm = 0;
4273 BFD_ASSERT (h->ldindx >= 0);
4274 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4275 finfo->ldsym + (h->ldindx - 3) *
4276 bfd_xcoff_ldsymsz(finfo->output_bfd));
4277 h->ldsym = NULL;
4279 /* Fill in snentry now that we know the target_index. */
4280 if ((h->flags & XCOFF_ENTRY) != 0 &&
4281 (h->root.type == bfd_link_hash_defined
4282 || h->root.type == bfd_link_hash_defweak)) {
4283 xcoff_data (output_bfd)->snentry =
4284 h->root.u.def.section->output_section->target_index;
4288 *indexp = -1;
4290 skip = false;
4291 require = false;
4292 add = 1 + isym.n_numaux;
4294 /* If we are skipping this csect, we want to skip this symbol. */
4295 if (*csectpp == NULL)
4296 skip = true;
4298 /* If we garbage collected this csect, we want to skip this
4299 symbol. */
4300 if (! skip
4301 && xcoff_hash_table (finfo->info)->gc
4302 && ((*csectpp)->flags & SEC_MARK) == 0
4303 && *csectpp != bfd_abs_section_ptr)
4304 skip = true;
4306 /* An XCOFF linker always skips C_STAT symbols. */
4307 if (! skip
4308 && isymp->n_sclass == C_STAT)
4309 skip = true;
4311 /* We skip all but the first TOC anchor. */
4312 if (! skip
4313 && isymp->n_sclass == C_HIDEXT
4314 && aux.x_csect.x_smclas == XMC_TC0)
4316 if (finfo->toc_symindx != -1)
4317 skip = true;
4318 else
4320 bfd_vma tocval, tocend;
4321 bfd *inp;
4323 tocval = ((*csectpp)->output_section->vma
4324 + (*csectpp)->output_offset
4325 + isym.n_value
4326 - (*csectpp)->vma);
4328 /* We want to find out if tocval is a good value to use
4329 as the TOC anchor--that is, whether we can access all
4330 of the TOC using a 16 bit offset from tocval. This
4331 test assumes that the TOC comes at the end of the
4332 output section, as it does in the default linker
4333 script. */
4334 tocend = ((*csectpp)->output_section->vma
4335 + (*csectpp)->output_section->_raw_size);
4336 for (inp = finfo->info->input_bfds;
4337 inp != NULL;
4338 inp = inp->link_next)
4340 asection *o;
4342 for (o = inp->sections; o != NULL; o = o->next)
4343 if (strcmp (o->name, ".tocbss") == 0)
4345 bfd_vma new_toc_end;
4346 new_toc_end = (o->output_section->vma
4347 + o->output_offset
4348 + o->_cooked_size);
4349 if (new_toc_end > tocend)
4350 tocend = new_toc_end;
4355 if (tocval + 0x10000 < tocend)
4357 (*_bfd_error_handler)
4358 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4359 (unsigned long) (tocend - tocval));
4360 bfd_set_error (bfd_error_file_too_big);
4361 return false;
4364 if (tocval + 0x8000 < tocend)
4366 bfd_vma tocadd;
4368 tocadd = tocend - (tocval + 0x8000);
4369 tocval += tocadd;
4370 isym.n_value += tocadd;
4373 finfo->toc_symindx = output_index;
4374 xcoff_data (finfo->output_bfd)->toc = tocval;
4375 xcoff_data (finfo->output_bfd)->sntoc =
4376 (*csectpp)->output_section->target_index;
4377 require = true;
4382 /* If we are stripping all symbols, we want to skip this one. */
4383 if (! skip
4384 && finfo->info->strip == strip_all)
4385 skip = true;
4387 /* We can skip resolved external references. */
4388 if (! skip
4389 && isym.n_sclass == C_EXT
4390 && smtyp == XTY_ER
4391 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4392 skip = true;
4394 /* We can skip common symbols if they got defined somewhere
4395 else. */
4396 if (! skip
4397 && isym.n_sclass == C_EXT
4398 && smtyp == XTY_CM
4399 && ((*sym_hash)->root.type != bfd_link_hash_common
4400 || (*sym_hash)->root.u.c.p->section != *csectpp)
4401 && ((*sym_hash)->root.type != bfd_link_hash_defined
4402 || (*sym_hash)->root.u.def.section != *csectpp))
4403 skip = true;
4405 /* Skip local symbols if we are discarding them. */
4406 if (! skip
4407 && finfo->info->discard == discard_all
4408 && isym.n_sclass != C_EXT
4409 && (isym.n_sclass != C_HIDEXT
4410 || smtyp != XTY_SD))
4411 skip = true;
4413 /* If we stripping debugging symbols, and this is a debugging
4414 symbol, then skip it. */
4415 if (! skip
4416 && finfo->info->strip == strip_debugger
4417 && isym.n_scnum == N_DEBUG)
4418 skip = true;
4420 /* If some symbols are stripped based on the name, work out the
4421 name and decide whether to skip this symbol. We don't handle
4422 this correctly for symbols whose names are in the .debug
4423 section; to get it right we would need a new bfd_strtab_hash
4424 function to return the string given the index. */
4425 if (! skip
4426 && (finfo->info->strip == strip_some
4427 || finfo->info->discard == discard_l)
4428 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4430 const char *name;
4431 char buf[SYMNMLEN + 1];
4433 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4435 if (name == NULL)
4436 return false;
4438 if ((finfo->info->strip == strip_some
4439 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4440 false) == NULL))
4441 || (finfo->info->discard == discard_l
4442 && (isym.n_sclass != C_EXT
4443 && (isym.n_sclass != C_HIDEXT
4444 || smtyp != XTY_SD))
4445 && bfd_is_local_label_name (input_bfd, name)))
4446 skip = true;
4449 /* We can not skip the first TOC anchor. */
4450 if (skip
4451 && require
4452 && finfo->info->strip != strip_all)
4453 skip = false;
4455 /* We now know whether we are to skip this symbol or not. */
4456 if (! skip)
4458 /* Adjust the symbol in order to output it. */
4460 if (isym._n._n_n._n_zeroes == 0
4461 && isym._n._n_n._n_offset != 0)
4463 /* This symbol has a long name. Enter it in the string
4464 table we are building. If *debug_index != -1, the
4465 name has already been entered in the .debug section. */
4466 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4467 isym._n._n_n._n_offset = *debug_index;
4468 else
4470 const char *name;
4471 bfd_size_type indx;
4473 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4474 (char *) NULL);
4476 if (name == NULL)
4477 return false;
4478 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4479 if (indx == (bfd_size_type) -1)
4480 return false;
4481 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4485 if (isym.n_sclass != C_BSTAT
4486 && isym.n_sclass != C_ESTAT
4487 && isym.n_sclass != C_DECL
4488 && isym.n_scnum > 0)
4490 isym.n_scnum = (*csectpp)->output_section->target_index;
4491 isym.n_value += ((*csectpp)->output_section->vma
4492 + (*csectpp)->output_offset
4493 - (*csectpp)->vma);
4496 /* The value of a C_FILE symbol is the symbol index of the
4497 next C_FILE symbol. The value of the last C_FILE symbol
4498 is -1. We try to get this right, below, just before we
4499 write the symbols out, but in the general case we may
4500 have to write the symbol out twice. */
4501 if (isym.n_sclass == C_FILE)
4503 if (finfo->last_file_index != -1
4504 && finfo->last_file.n_value != (bfd_vma) output_index)
4506 /* We must correct the value of the last C_FILE entry. */
4507 finfo->last_file.n_value = output_index;
4508 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4510 /* The last C_FILE symbol is in this input file. */
4511 bfd_coff_swap_sym_out (output_bfd,
4512 (PTR) &finfo->last_file,
4513 (PTR) (finfo->outsyms
4514 + ((finfo->last_file_index
4515 - syment_base)
4516 * osymesz)));
4518 else
4520 /* We have already written out the last C_FILE
4521 symbol. We need to write it out again. We
4522 borrow *outsym temporarily. */
4523 bfd_coff_swap_sym_out (output_bfd,
4524 (PTR) &finfo->last_file,
4525 (PTR) outsym);
4526 if (bfd_seek (output_bfd,
4527 (obj_sym_filepos (output_bfd)
4528 + finfo->last_file_index * osymesz),
4529 SEEK_SET) != 0
4530 || (bfd_write (outsym, osymesz, 1, output_bfd)
4531 != osymesz))
4532 return false;
4536 finfo->last_file_index = output_index;
4537 finfo->last_file = isym;
4540 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4541 into the line numbers. We update the symbol values when
4542 we handle the line numbers. */
4543 if (isym.n_sclass == C_BINCL
4544 || isym.n_sclass == C_EINCL)
4546 isym.n_value = finfo->line_filepos;
4547 ++incls;
4550 /* Output the symbol. */
4552 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4554 *indexp = output_index;
4556 if (isym.n_sclass == C_EXT)
4558 long indx;
4559 struct xcoff_link_hash_entry *h;
4561 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4562 / isymesz);
4563 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4564 BFD_ASSERT (h != NULL);
4565 h->indx = output_index;
4568 /* If this is a symbol in the TOC which we may have merged
4569 (class XMC_TC), remember the symbol index of the TOC
4570 symbol. */
4571 if (isym.n_sclass == C_HIDEXT
4572 && aux.x_csect.x_smclas == XMC_TC
4573 && *sym_hash != NULL)
4575 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4576 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4577 (*sym_hash)->u.toc_indx = output_index;
4580 output_index += add;
4581 outsym += add * osymesz;
4584 esym += add * isymesz;
4585 isymp += add;
4586 csectpp += add;
4587 sym_hash += add;
4588 if (debug_index != NULL)
4589 debug_index += add;
4590 ++indexp;
4591 for (--add; add > 0; --add)
4592 *indexp++ = -1;
4595 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4596 done in a separate pass, because we don't know the correct symbol
4597 indices until we have already decided which symbols we are going
4598 to keep. */
4600 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4601 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4602 isymp = finfo->internal_syms;
4603 indexp = finfo->sym_indices;
4604 csectpp = xcoff_data (input_bfd)->csects;
4605 outsym = finfo->outsyms;
4606 while (esym < esym_end)
4608 int add;
4610 add = 1 + isymp->n_numaux;
4612 if (*indexp < 0)
4613 esym += add * isymesz;
4614 else
4616 int i;
4618 if (isymp->n_sclass == C_BSTAT)
4620 struct internal_syment isym;
4622 bfd_vma indx;
4624 /* The value of a C_BSTAT symbol is the symbol table
4625 index of the containing csect. */
4626 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4627 indx = isym.n_value;
4628 if (indx < obj_raw_syment_count (input_bfd))
4630 long symindx;
4632 symindx = finfo->sym_indices[indx];
4633 if (symindx < 0)
4634 isym.n_value = 0;
4635 else
4636 isym.n_value = symindx;
4637 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4638 (PTR) outsym);
4642 esym += isymesz;
4643 outsym += osymesz;
4645 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4647 union internal_auxent aux;
4649 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4650 isymp->n_sclass, i, isymp->n_numaux,
4651 (PTR) &aux);
4653 if (isymp->n_sclass == C_FILE)
4655 /* This is the file name (or some comment put in by
4656 the compiler). If it is long, we must put it in
4657 the string table. */
4658 if (aux.x_file.x_n.x_zeroes == 0
4659 && aux.x_file.x_n.x_offset != 0)
4661 const char *filename;
4662 bfd_size_type indx;
4664 BFD_ASSERT (aux.x_file.x_n.x_offset
4665 >= STRING_SIZE_SIZE);
4666 if (strings == NULL)
4668 strings = _bfd_coff_read_string_table (input_bfd);
4669 if (strings == NULL)
4670 return false;
4672 filename = strings + aux.x_file.x_n.x_offset;
4673 indx = _bfd_stringtab_add (finfo->strtab, filename,
4674 hash, copy);
4675 if (indx == (bfd_size_type) -1)
4676 return false;
4677 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4680 else if ((isymp->n_sclass == C_EXT
4681 || isymp->n_sclass == C_HIDEXT)
4682 && i + 1 == isymp->n_numaux)
4685 /* We don't support type checking. I don't know if
4686 anybody does. */
4687 aux.x_csect.x_parmhash = 0;
4688 /* I don't think anybody uses these fields, but we'd
4689 better clobber them just in case. */
4690 aux.x_csect.x_stab = 0;
4691 aux.x_csect.x_snstab = 0;
4693 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4695 bfd_signed_vma indx;
4698 indx = aux.x_csect.x_scnlen.l;
4699 if (indx < obj_raw_syment_count (input_bfd))
4701 long symindx;
4703 symindx = finfo->sym_indices[indx];
4704 if (symindx < 0) {
4705 aux.x_csect.x_scnlen.l = 0;
4706 } else {
4707 aux.x_csect.x_scnlen.l = symindx;
4712 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4714 unsigned long indx;
4716 if (ISFCN (isymp->n_type)
4717 || ISTAG (isymp->n_sclass)
4718 || isymp->n_sclass == C_BLOCK
4719 || isymp->n_sclass == C_FCN)
4721 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4722 if (indx > 0
4723 && indx < obj_raw_syment_count (input_bfd))
4725 /* We look forward through the symbol for
4726 the index of the next symbol we are going
4727 to include. I don't know if this is
4728 entirely right. */
4729 while (finfo->sym_indices[indx] < 0
4730 && indx < obj_raw_syment_count (input_bfd))
4731 ++indx;
4732 if (indx >= obj_raw_syment_count (input_bfd))
4733 indx = output_index;
4734 else
4735 indx = finfo->sym_indices[indx];
4736 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4741 indx = aux.x_sym.x_tagndx.l;
4742 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4744 long symindx;
4746 symindx = finfo->sym_indices[indx];
4747 if (symindx < 0)
4748 aux.x_sym.x_tagndx.l = 0;
4749 else
4750 aux.x_sym.x_tagndx.l = symindx;
4755 /* Copy over the line numbers, unless we are stripping
4756 them. We do this on a symbol by symbol basis in
4757 order to more easily handle garbage collection. */
4758 if ((isymp->n_sclass == C_EXT
4759 || isymp->n_sclass == C_HIDEXT)
4760 && i == 0
4761 && isymp->n_numaux > 1
4762 && ISFCN (isymp->n_type)
4763 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4765 if (finfo->info->strip != strip_none
4766 && finfo->info->strip != strip_some)
4767 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4768 else
4770 asection *enclosing;
4771 unsigned int enc_count;
4772 bfd_signed_vma linoff;
4773 struct internal_lineno lin;
4775 o = *csectpp;
4776 enclosing = xcoff_section_data (abfd, o)->enclosing;
4777 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4778 if (oline != enclosing)
4780 if (bfd_seek (input_bfd,
4781 enclosing->line_filepos,
4782 SEEK_SET) != 0
4783 || (bfd_read (finfo->linenos, linesz,
4784 enc_count, input_bfd)
4785 != linesz * enc_count))
4786 return false;
4787 oline = enclosing;
4790 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4791 - enclosing->line_filepos);
4793 bfd_coff_swap_lineno_in (input_bfd,
4794 (PTR) (finfo->linenos + linoff),
4795 (PTR) &lin);
4796 if (lin.l_lnno != 0
4797 || ((bfd_size_type) lin.l_addr.l_symndx
4798 != ((esym
4799 - isymesz
4800 - ((bfd_byte *)
4801 obj_coff_external_syms (input_bfd)))
4802 / isymesz)))
4803 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4804 else
4806 bfd_byte *linpend, *linp;
4807 bfd_vma offset;
4808 bfd_size_type count;
4810 lin.l_addr.l_symndx = *indexp;
4811 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4812 (PTR) (finfo->linenos
4813 + linoff));
4815 linpend = (finfo->linenos
4816 + enc_count * linesz);
4817 offset = (o->output_section->vma
4818 + o->output_offset
4819 - o->vma);
4820 for (linp = finfo->linenos + linoff + linesz;
4821 linp < linpend;
4822 linp += linesz)
4824 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4825 (PTR) &lin);
4826 if (lin.l_lnno == 0)
4827 break;
4828 lin.l_addr.l_paddr += offset;
4829 bfd_coff_swap_lineno_out (output_bfd,
4830 (PTR) &lin,
4831 (PTR) linp);
4834 count = (linp - (finfo->linenos + linoff)) / linesz;
4836 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4837 (o->output_section->line_filepos
4838 + o->output_section->lineno_count * linesz);
4840 if (bfd_seek (output_bfd,
4841 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4842 SEEK_SET) != 0
4843 || (bfd_write (finfo->linenos + linoff,
4844 linesz, count, output_bfd)
4845 != linesz * count))
4846 return false;
4848 o->output_section->lineno_count += count;
4850 if (incls > 0)
4852 struct internal_syment *iisp, *iispend;
4853 long *iindp;
4854 bfd_byte *oos;
4855 int iiadd;
4857 /* Update any C_BINCL or C_EINCL symbols
4858 that refer to a line number in the
4859 range we just output. */
4860 iisp = finfo->internal_syms;
4861 iispend = (iisp
4862 + obj_raw_syment_count (input_bfd));
4863 iindp = finfo->sym_indices;
4864 oos = finfo->outsyms;
4865 while (iisp < iispend)
4867 if (*iindp >= 0
4868 && (iisp->n_sclass == C_BINCL
4869 || iisp->n_sclass == C_EINCL)
4870 && ((bfd_size_type) iisp->n_value
4871 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4872 && ((bfd_size_type) iisp->n_value
4873 < (enclosing->line_filepos
4874 + enc_count * linesz)))
4876 struct internal_syment iis;
4878 bfd_coff_swap_sym_in (output_bfd,
4879 (PTR) oos,
4880 (PTR) &iis);
4881 iis.n_value =
4882 (iisp->n_value
4883 - enclosing->line_filepos
4884 - linoff
4885 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4886 bfd_coff_swap_sym_out (output_bfd,
4887 (PTR) &iis,
4888 (PTR) oos);
4889 --incls;
4892 iiadd = 1 + iisp->n_numaux;
4893 if (*iindp >= 0)
4894 oos += iiadd * osymesz;
4895 iisp += iiadd;
4896 iindp += iiadd;
4903 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4904 isymp->n_sclass, i, isymp->n_numaux,
4905 (PTR) outsym);
4906 outsym += osymesz;
4907 esym += isymesz;
4911 indexp += add;
4912 isymp += add;
4913 csectpp += add;
4916 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4917 symbol will be the first symbol in the next input file. In the
4918 normal case, this will save us from writing out the C_FILE symbol
4919 again. */
4920 if (finfo->last_file_index != -1
4921 && (bfd_size_type) finfo->last_file_index >= syment_base)
4923 finfo->last_file.n_value = output_index;
4924 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
4925 (PTR) (finfo->outsyms
4926 + ((finfo->last_file_index - syment_base)
4927 * osymesz)));
4930 /* Write the modified symbols to the output file. */
4931 if (outsym > finfo->outsyms)
4933 if (bfd_seek (output_bfd,
4934 obj_sym_filepos (output_bfd) + syment_base * osymesz,
4935 SEEK_SET) != 0
4936 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
4937 output_bfd)
4938 != (bfd_size_type) (outsym - finfo->outsyms)))
4939 return false;
4941 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4942 + (outsym - finfo->outsyms) / osymesz)
4943 == output_index);
4945 obj_raw_syment_count (output_bfd) = output_index;
4948 /* Don't let the linker relocation routines discard the symbols. */
4949 keep_syms = obj_coff_keep_syms (input_bfd);
4950 obj_coff_keep_syms (input_bfd) = true;
4952 /* Relocate the contents of each section. */
4953 for (o = input_bfd->sections; o != NULL; o = o->next) {
4955 bfd_byte *contents;
4957 if (! o->linker_mark){
4958 /* This section was omitted from the link. */
4959 continue;
4962 if ((o->flags & SEC_HAS_CONTENTS) == 0
4963 || o->_raw_size == 0
4964 || (o->flags & SEC_IN_MEMORY) != 0)
4965 continue;
4967 /* We have set filepos correctly for the sections we created to
4968 represent csects, so bfd_get_section_contents should work. */
4969 if (coff_section_data (input_bfd, o) != NULL
4970 && coff_section_data (input_bfd, o)->contents != NULL)
4971 contents = coff_section_data (input_bfd, o)->contents;
4972 else {
4973 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4974 (file_ptr) 0, o->_raw_size))
4975 return false;
4976 contents = finfo->contents;
4979 if ((o->flags & SEC_RELOC) != 0) {
4980 int target_index;
4981 struct internal_reloc *internal_relocs;
4982 struct internal_reloc *irel;
4983 bfd_vma offset;
4984 struct internal_reloc *irelend;
4985 struct xcoff_link_hash_entry **rel_hash;
4986 long r_symndx;
4988 /* Read in the relocs. */
4989 target_index = o->output_section->target_index;
4990 internal_relocs = (xcoff_read_internal_relocs
4991 (input_bfd, o, false, finfo->external_relocs,
4992 true,
4993 (finfo->section_info[target_index].relocs
4994 + o->output_section->reloc_count)));
4995 if (internal_relocs == NULL)
4996 return false;
4998 /* Call processor specific code to relocate the section
4999 contents. */
5000 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5001 input_bfd, o,
5002 contents,
5003 internal_relocs,
5004 finfo->internal_syms,
5005 xcoff_data (input_bfd)->csects))
5006 return false;
5008 offset = o->output_section->vma + o->output_offset - o->vma;
5009 irel = internal_relocs;
5010 irelend = irel + o->reloc_count;
5011 rel_hash = (finfo->section_info[target_index].rel_hashes
5012 + o->output_section->reloc_count);
5013 for (; irel < irelend; irel++, rel_hash++) {
5014 struct xcoff_link_hash_entry *h = NULL;
5015 struct internal_ldrel ldrel;
5016 boolean quiet;
5018 *rel_hash = NULL;
5020 /* Adjust the reloc address and symbol index. */
5022 irel->r_vaddr += offset;
5024 r_symndx = irel->r_symndx;
5026 if (r_symndx == -1)
5027 h = NULL;
5028 else
5029 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5031 if (r_symndx != -1 && finfo->info->strip != strip_all)
5033 if (h != NULL
5034 && h->smclas != XMC_TD
5035 && (irel->r_type == R_TOC
5036 || irel->r_type == R_GL
5037 || irel->r_type == R_TCL
5038 || irel->r_type == R_TRL
5039 || irel->r_type == R_TRLA))
5041 /* This is a TOC relative reloc with a symbol
5042 attached. The symbol should be the one which
5043 this reloc is for. We want to make this
5044 reloc against the TOC address of the symbol,
5045 not the symbol itself. */
5046 BFD_ASSERT (h->toc_section != NULL);
5047 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5048 if (h->u.toc_indx != -1)
5049 irel->r_symndx = h->u.toc_indx;
5050 else
5052 struct xcoff_toc_rel_hash *n;
5053 struct xcoff_link_section_info *si;
5055 n = ((struct xcoff_toc_rel_hash *)
5056 bfd_alloc (finfo->output_bfd,
5057 sizeof (struct xcoff_toc_rel_hash)));
5058 if (n == NULL)
5059 return false;
5060 si = finfo->section_info + target_index;
5061 n->next = si->toc_rel_hashes;
5062 n->h = h;
5063 n->rel = irel;
5064 si->toc_rel_hashes = n;
5067 else if (h != NULL)
5069 /* This is a global symbol. */
5070 if (h->indx >= 0)
5071 irel->r_symndx = h->indx;
5072 else
5074 /* This symbol is being written at the end
5075 of the file, and we do not yet know the
5076 symbol index. We save the pointer to the
5077 hash table entry in the rel_hash list.
5078 We set the indx field to -2 to indicate
5079 that this symbol must not be stripped. */
5080 *rel_hash = h;
5081 h->indx = -2;
5084 else
5086 long indx;
5088 indx = finfo->sym_indices[r_symndx];
5090 if (indx == -1)
5092 struct internal_syment *is;
5094 /* Relocations against a TC0 TOC anchor are
5095 automatically transformed to be against
5096 the TOC anchor in the output file. */
5097 is = finfo->internal_syms + r_symndx;
5098 if (is->n_sclass == C_HIDEXT
5099 && is->n_numaux > 0)
5101 PTR auxptr;
5102 union internal_auxent aux;
5104 auxptr = ((PTR)
5105 (((bfd_byte *)
5106 obj_coff_external_syms (input_bfd))
5107 + ((r_symndx + is->n_numaux)
5108 * isymesz)));
5109 bfd_coff_swap_aux_in (input_bfd, auxptr,
5110 is->n_type, is->n_sclass,
5111 is->n_numaux - 1,
5112 is->n_numaux,
5113 (PTR) &aux);
5114 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5115 && aux.x_csect.x_smclas == XMC_TC0)
5116 indx = finfo->toc_symindx;
5120 if (indx != -1)
5121 irel->r_symndx = indx;
5122 else
5125 struct internal_syment *is;
5127 const char *name;
5128 char buf[SYMNMLEN + 1];
5130 /* This reloc is against a symbol we are
5131 stripping. It would be possible to handle
5132 this case, but I don't think it's worth it. */
5133 is = finfo->internal_syms + r_symndx;
5135 name = (_bfd_coff_internal_syment_name
5136 (input_bfd, is, buf));
5138 if (name == NULL)
5139 return false;
5141 if (! ((*finfo->info->callbacks->unattached_reloc)
5142 (finfo->info, name, input_bfd, o,
5143 irel->r_vaddr)))
5144 return false;
5149 quiet = false;
5150 switch (irel->r_type)
5152 default:
5153 if (h == NULL
5154 || h->root.type == bfd_link_hash_defined
5155 || h->root.type == bfd_link_hash_defweak
5156 || h->root.type == bfd_link_hash_common)
5157 break;
5158 /* Fall through. */
5159 case R_POS:
5160 case R_NEG:
5161 case R_RL:
5162 case R_RLA:
5163 /* This reloc needs to be copied into the .loader
5164 section. */
5165 ldrel.l_vaddr = irel->r_vaddr;
5166 if (r_symndx == -1)
5167 ldrel.l_symndx = -1;
5168 else if (h == NULL
5169 || (h->root.type == bfd_link_hash_defined
5170 || h->root.type == bfd_link_hash_defweak
5171 || h->root.type == bfd_link_hash_common))
5173 asection *sec;
5175 if (h == NULL)
5176 sec = xcoff_data (input_bfd)->csects[r_symndx];
5177 else if (h->root.type == bfd_link_hash_common)
5178 sec = h->root.u.c.p->section;
5179 else
5180 sec = h->root.u.def.section;
5181 sec = sec->output_section;
5183 if (strcmp (sec->name, ".text") == 0)
5184 ldrel.l_symndx = 0;
5185 else if (strcmp (sec->name, ".data") == 0)
5186 ldrel.l_symndx = 1;
5187 else if (strcmp (sec->name, ".bss") == 0)
5188 ldrel.l_symndx = 2;
5189 else
5191 (*_bfd_error_handler)
5192 (_("%s: loader reloc in unrecognized section `%s'"),
5193 bfd_get_filename (input_bfd),
5194 sec->name);
5195 bfd_set_error (bfd_error_nonrepresentable_section);
5196 return false;
5199 else
5201 if (! finfo->info->relocateable
5202 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5203 && (h->flags & XCOFF_IMPORT) == 0)
5205 /* We already called the undefined_symbol
5206 callback for this relocation, in
5207 _bfd_ppc_xcoff_relocate_section. Don't
5208 issue any more warnings. */
5209 quiet = true;
5211 if (h->ldindx < 0 && ! quiet)
5213 (*_bfd_error_handler)
5214 (_("%s: `%s' in loader reloc but not loader sym"),
5215 bfd_get_filename (input_bfd),
5216 h->root.root.string);
5217 bfd_set_error (bfd_error_bad_value);
5218 return false;
5220 ldrel.l_symndx = h->ldindx;
5222 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5223 ldrel.l_rsecnm = o->output_section->target_index;
5224 if (xcoff_hash_table (finfo->info)->textro
5225 && strcmp (o->output_section->name, ".text") == 0
5226 && ! quiet)
5228 (*_bfd_error_handler)
5229 (_("%s: loader reloc in read-only section %s"),
5230 bfd_get_filename (input_bfd),
5231 bfd_get_section_name (finfo->output_bfd,
5232 o->output_section));
5233 bfd_set_error (bfd_error_invalid_operation);
5234 return false;
5236 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
5237 finfo->ldrel);
5239 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5240 break;
5242 case R_TOC:
5243 case R_GL:
5244 case R_TCL:
5245 case R_TRL:
5246 case R_TRLA:
5247 /* We should never need a .loader reloc for a TOC
5248 relative reloc. */
5249 break;
5253 o->output_section->reloc_count += o->reloc_count;
5256 /* Write out the modified section contents. */
5257 if (! bfd_set_section_contents (output_bfd, o->output_section,
5258 contents, o->output_offset,
5259 (o->_cooked_size != 0
5260 ? o->_cooked_size
5261 : o->_raw_size)))
5262 return false;
5265 obj_coff_keep_syms (input_bfd) = keep_syms;
5267 if (! finfo->info->keep_memory)
5269 if (! _bfd_coff_free_symbols (input_bfd))
5270 return false;
5273 return true;
5276 #undef N_TMASK
5277 #undef N_BTSHFT
5279 /* Write out a non-XCOFF global symbol. */
5282 static boolean
5283 xcoff_write_global_symbol (h, p)
5284 struct xcoff_link_hash_entry *h;
5285 PTR p;
5287 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5288 bfd *output_bfd;
5289 bfd_byte *outsym;
5290 struct internal_syment isym;
5291 union internal_auxent aux;
5292 boolean result;
5294 output_bfd = finfo->output_bfd;
5295 outsym = finfo->outsyms;
5297 /* If this symbol was garbage collected, just skip it. */
5298 if (xcoff_hash_table (finfo->info)->gc
5299 && (h->flags & XCOFF_MARK) == 0)
5300 return true;
5302 /* If we need a .loader section entry, write it out. */
5303 if (h->ldsym != NULL) {
5304 struct internal_ldsym *ldsym;
5305 bfd *impbfd;
5307 ldsym = h->ldsym;
5309 if (h->root.type == bfd_link_hash_undefined ||
5310 h->root.type == bfd_link_hash_undefweak) {
5312 ldsym->l_value = 0;
5313 ldsym->l_scnum = N_UNDEF;
5314 ldsym->l_smtype = XTY_ER;
5315 impbfd = h->root.u.undef.abfd;
5317 } else if (h->root.type == bfd_link_hash_defined ||
5318 h->root.type == bfd_link_hash_defweak) {
5320 asection *sec;
5322 sec = h->root.u.def.section;
5323 ldsym->l_value = (sec->output_section->vma
5324 + sec->output_offset
5325 + h->root.u.def.value);
5326 ldsym->l_scnum = sec->output_section->target_index;
5327 ldsym->l_smtype = XTY_SD;
5328 impbfd = sec->owner;
5330 } else {
5331 abort ();
5334 if (((h->flags & XCOFF_DEF_REGULAR) == 0 &&
5335 (h->flags & XCOFF_DEF_DYNAMIC) != 0) ||
5336 (h->flags & XCOFF_IMPORT) != 0) {
5337 ldsym->l_smtype |= L_IMPORT;
5340 if (((h->flags & XCOFF_DEF_REGULAR) != 0 &&
5341 (h->flags & XCOFF_DEF_DYNAMIC) != 0) ||
5342 (h->flags & XCOFF_EXPORT) != 0) {
5343 ldsym->l_smtype |= L_EXPORT;
5346 if ((h->flags & XCOFF_ENTRY) != 0) {
5347 ldsym->l_smtype |= L_ENTRY;
5350 if ((h->flags & XCOFF_RTINIT) != 0) {
5351 ldsym->l_smtype = XTY_SD;
5354 ldsym->l_smclas = h->smclas;
5356 if (ldsym->l_ifile == (bfd_size_type) -1) {
5357 ldsym->l_ifile = 0;
5359 } else if (ldsym->l_ifile == 0) {
5360 if ((ldsym->l_smtype & L_IMPORT) == 0) {
5361 ldsym->l_ifile = 0;
5362 } else if (impbfd == NULL) {
5363 ldsym->l_ifile = 0;
5364 } else {
5365 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5366 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5370 ldsym->l_parm = 0;
5372 BFD_ASSERT (h->ldindx >= 0);
5374 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym +
5375 (h->ldindx - 3) *
5376 bfd_xcoff_ldsymsz(finfo->output_bfd));
5377 h->ldsym = NULL;
5380 /* If this symbol needs global linkage code, write it out. */
5381 if (h->root.type == bfd_link_hash_defined &&
5382 (h->root.u.def.section ==
5383 xcoff_hash_table (finfo->info)->linkage_section)) {
5385 bfd_byte *p;
5386 bfd_vma tocoff;
5387 unsigned int i;
5389 p = h->root.u.def.section->contents + h->root.u.def.value;
5391 /* The first instruction in the global linkage code loads a
5392 specific TOC element. */
5393 tocoff = (h->descriptor->toc_section->output_section->vma
5394 + h->descriptor->toc_section->output_offset
5395 - xcoff_data (output_bfd)->toc);
5397 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0) {
5398 tocoff += h->descriptor->u.toc_offset;
5403 * The first instruction in the glink code needs to be cooked to
5404 * to hold the correct offset in the toc. The rest are just outputted
5405 * raw.
5407 bfd_put_32 (output_bfd,
5408 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5411 * Start with i == 1 to get past the first instruction done above
5412 * The /4 is because the glink code is in bytes and we are going 4
5413 * at a pop
5415 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++) {
5416 bfd_put_32 (output_bfd, bfd_xcoff_glink_code(output_bfd, i),
5417 &p[4 * i]);
5421 /* If we created a TOC entry for this symbol, write out the required
5422 relocs. */
5423 if ((h->flags & XCOFF_SET_TOC) != 0)
5425 asection *tocsec;
5426 asection *osec;
5427 int oindx;
5428 struct internal_reloc *irel;
5429 struct internal_ldrel ldrel;
5430 struct internal_syment irsym;
5431 union internal_auxent iraux;
5433 tocsec = h->toc_section;
5434 osec = tocsec->output_section;
5435 oindx = osec->target_index;
5436 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5437 irel->r_vaddr = (osec->vma
5438 + tocsec->output_offset
5439 + h->u.toc_offset);
5442 if (h->indx >= 0) {
5443 irel->r_symndx = h->indx;
5444 } else {
5445 h->indx = -2;
5446 irel->r_symndx = obj_raw_syment_count (output_bfd);
5449 BFD_ASSERT (h->ldindx >= 0);
5452 * Initialize the aux union here instead of closer to when it is
5453 * written out below because the length of the csect depends on
5454 * whether the output is 32 or 64 bit
5456 memset (&iraux, 0, sizeof iraux);
5457 iraux.x_csect.x_smtyp = XTY_SD;
5458 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below */
5459 iraux.x_csect.x_smclas = XMC_TC;
5463 * 32 bit uses a 32 bit R_POS to do the relocations
5464 * 64 bit uses a 64 bit R_POS to do the relocations
5466 * Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5468 * Which one is determined by the backend
5470 if (bfd_xcoff_is_xcoff64(output_bfd)) {
5471 irel->r_size = 63;
5472 iraux.x_csect.x_scnlen.l = 8;
5473 } else if (bfd_xcoff_is_xcoff32(output_bfd)) {
5474 irel->r_size = 31;
5475 iraux.x_csect.x_scnlen.l = 4;
5476 } else {
5477 return false;
5479 irel->r_type = R_POS;
5480 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5481 ++osec->reloc_count;
5483 ldrel.l_vaddr = irel->r_vaddr;
5484 ldrel.l_symndx = h->ldindx;
5485 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
5486 ldrel.l_rsecnm = oindx;
5487 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5488 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5490 /* We need to emit a symbol to define a csect which holds the reloc. */
5491 if (finfo->info->strip != strip_all) {
5493 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &irsym,
5494 h->root.root.string);
5495 if (false == result) {
5496 return false;
5499 irsym.n_value = irel->r_vaddr;
5500 irsym.n_scnum = osec->target_index;
5501 irsym.n_sclass = C_HIDEXT;
5502 irsym.n_type = T_NULL;
5503 irsym.n_numaux = 1;
5505 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5506 outsym += bfd_coff_symesz (output_bfd);
5508 /* note : iraux is initialized above */
5509 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5510 0, 1, (PTR) outsym);
5511 outsym += bfd_coff_auxesz (output_bfd);
5513 if (h->indx >= 0)
5515 /* We aren't going to write out the symbols below, so we
5516 need to write them out now. */
5517 if (bfd_seek (output_bfd,
5518 (obj_sym_filepos (output_bfd)
5519 + (obj_raw_syment_count (output_bfd)
5520 * bfd_coff_symesz (output_bfd))),
5521 SEEK_SET) != 0
5522 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5523 output_bfd)
5524 != (bfd_size_type) (outsym - finfo->outsyms)))
5525 return false;
5526 obj_raw_syment_count (output_bfd) +=
5527 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5529 outsym = finfo->outsyms;
5535 * If this symbol is a specially defined function descriptor, write
5536 * it out. The first word is the address of the function code
5537 * itself, the second word is the address of the TOC, and the third
5538 * word is zero.
5540 * 32 bit vs 64 bit
5541 * The addresses for the 32 bit will take 4 bytes and the addresses
5542 * for 64 bit will take 8 bytes. Similar for the relocs. This type
5543 * of logic was also done above to create a TOC entry in
5544 * xcoff_write_global_symbol
5546 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5547 && h->root.type == bfd_link_hash_defined
5548 && (h->root.u.def.section
5549 == xcoff_hash_table (finfo->info)->descriptor_section))
5551 asection *sec;
5552 asection *osec;
5553 int oindx;
5554 bfd_byte *p;
5555 struct xcoff_link_hash_entry *hentry;
5556 asection *esec;
5557 struct internal_reloc *irel;
5558 struct internal_ldrel ldrel;
5559 asection *tsec;
5560 unsigned int reloc_size, byte_size;
5562 if (bfd_xcoff_is_xcoff64(output_bfd)) {
5563 reloc_size = 63;
5564 byte_size = 8;
5565 } else if (bfd_xcoff_is_xcoff32(output_bfd)) {
5566 reloc_size = 31;
5567 byte_size = 4;
5568 } else {
5569 return false;
5572 sec = h->root.u.def.section;
5573 osec = sec->output_section;
5574 oindx = osec->target_index;
5575 p = sec->contents + h->root.u.def.value;
5577 hentry = h->descriptor;
5578 BFD_ASSERT (hentry != NULL
5579 && (hentry->root.type == bfd_link_hash_defined
5580 || hentry->root.type == bfd_link_hash_defweak));
5581 esec = hentry->root.u.def.section;
5583 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5584 irel->r_vaddr = (osec->vma
5585 + sec->output_offset
5586 + h->root.u.def.value);
5587 irel->r_symndx = esec->output_section->target_index;
5588 irel->r_type = R_POS;
5589 irel->r_size = reloc_size;
5590 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5591 ++osec->reloc_count;
5593 ldrel.l_vaddr = irel->r_vaddr;
5594 if (strcmp (esec->output_section->name, ".text") == 0)
5595 ldrel.l_symndx = 0;
5596 else if (strcmp (esec->output_section->name, ".data") == 0)
5597 ldrel.l_symndx = 1;
5598 else if (strcmp (esec->output_section->name, ".bss") == 0)
5599 ldrel.l_symndx = 2;
5600 else
5602 (*_bfd_error_handler)
5603 (_("%s: loader reloc in unrecognized section `%s'"),
5604 bfd_get_filename (output_bfd),
5605 esec->output_section->name);
5606 bfd_set_error (bfd_error_nonrepresentable_section);
5607 return false;
5609 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5610 ldrel.l_rsecnm = oindx;
5611 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5612 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5615 * There are three items to write out,
5616 * the address of the code
5617 * the address of the toc anchor
5618 * the environment pointer.
5619 * We are ignoring the environment pointer. So set it to zero
5621 if (bfd_xcoff_is_xcoff64(output_bfd)) {
5622 bfd_put_64 (output_bfd, (esec->output_section->vma +
5623 esec->output_offset +
5624 hentry->root.u.def.value), p);
5625 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5626 bfd_put_64 (output_bfd, 0, p + 16);
5627 } else {
5629 * 32 bit backend
5631 * This logic was already called above so the error case where
5632 * the backend is neither has already been checked
5634 bfd_put_32 (output_bfd, (esec->output_section->vma +
5635 esec->output_offset +
5636 hentry->root.u.def.value), p);
5637 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5638 bfd_put_32 (output_bfd, 0, p + 8);
5641 tsec = coff_section_from_bfd_index (output_bfd,
5642 xcoff_data (output_bfd)->sntoc);
5644 ++irel;
5645 irel->r_vaddr = (osec->vma
5646 + sec->output_offset
5647 + h->root.u.def.value
5648 + byte_size);
5649 irel->r_symndx = tsec->output_section->target_index;
5650 irel->r_type = R_POS;
5651 irel->r_size = reloc_size;
5652 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5653 ++osec->reloc_count;
5655 ldrel.l_vaddr = irel->r_vaddr;
5656 if (strcmp (tsec->output_section->name, ".text") == 0)
5657 ldrel.l_symndx = 0;
5658 else if (strcmp (tsec->output_section->name, ".data") == 0)
5659 ldrel.l_symndx = 1;
5660 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5661 ldrel.l_symndx = 2;
5662 else
5664 (*_bfd_error_handler)
5665 (_("%s: loader reloc in unrecognized section `%s'"),
5666 bfd_get_filename (output_bfd),
5667 tsec->output_section->name);
5668 bfd_set_error (bfd_error_nonrepresentable_section);
5669 return false;
5671 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5672 ldrel.l_rsecnm = oindx;
5673 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5674 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5677 if (h->indx >= 0 || finfo->info->strip == strip_all)
5679 BFD_ASSERT (outsym == finfo->outsyms);
5680 return true;
5683 if (h->indx != -2
5684 && (finfo->info->strip == strip_all
5685 || (finfo->info->strip == strip_some
5686 && (bfd_hash_lookup (finfo->info->keep_hash,
5687 h->root.root.string, false, false)
5688 == NULL))))
5690 BFD_ASSERT (outsym == finfo->outsyms);
5691 return true;
5694 if (h->indx != -2
5695 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5697 BFD_ASSERT (outsym == finfo->outsyms);
5698 return true;
5701 memset (&aux, 0, sizeof aux);
5703 h->indx = obj_raw_syment_count (output_bfd);
5705 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5706 h->root.root.string);
5707 if (false == result) {
5708 return false;
5711 if (h->root.type == bfd_link_hash_undefined
5712 || h->root.type == bfd_link_hash_undefweak)
5714 isym.n_value = 0;
5715 isym.n_scnum = N_UNDEF;
5716 isym.n_sclass = C_EXT;
5717 aux.x_csect.x_smtyp = XTY_ER;
5719 else if ((h->root.type == bfd_link_hash_defined
5720 || h->root.type == bfd_link_hash_defweak)
5721 && h->smclas == XMC_XO)
5723 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5724 isym.n_value = h->root.u.def.value;
5725 isym.n_scnum = N_UNDEF;
5726 isym.n_sclass = C_EXT;
5727 aux.x_csect.x_smtyp = XTY_ER;
5729 else if (h->root.type == bfd_link_hash_defined
5730 || h->root.type == bfd_link_hash_defweak)
5732 struct xcoff_link_size_list *l;
5734 isym.n_value = (h->root.u.def.section->output_section->vma
5735 + h->root.u.def.section->output_offset
5736 + h->root.u.def.value);
5737 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5738 isym.n_sclass = C_HIDEXT;
5739 aux.x_csect.x_smtyp = XTY_SD;
5741 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5743 for (l = xcoff_hash_table (finfo->info)->size_list;
5744 l != NULL;
5745 l = l->next)
5747 if (l->h == h)
5749 aux.x_csect.x_scnlen.l = l->size;
5750 break;
5755 else if (h->root.type == bfd_link_hash_common)
5757 isym.n_value = (h->root.u.c.p->section->output_section->vma
5758 + h->root.u.c.p->section->output_offset);
5759 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5760 isym.n_sclass = C_EXT;
5761 aux.x_csect.x_smtyp = XTY_CM;
5762 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5764 else
5765 abort ();
5767 isym.n_type = T_NULL;
5768 isym.n_numaux = 1;
5770 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5771 outsym += bfd_coff_symesz (output_bfd);
5773 aux.x_csect.x_smclas = h->smclas;
5774 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5775 (PTR) outsym);
5776 outsym += bfd_coff_auxesz (output_bfd);
5778 if ((h->root.type == bfd_link_hash_defined
5779 || h->root.type == bfd_link_hash_defweak)
5780 && h->smclas != XMC_XO)
5782 /* We just output an SD symbol. Now output an LD symbol. */
5784 h->indx += 2;
5786 isym.n_sclass = C_EXT;
5787 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5788 outsym += bfd_coff_symesz (output_bfd);
5790 aux.x_csect.x_smtyp = XTY_LD;
5791 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5792 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5793 (PTR) outsym);
5794 outsym += bfd_coff_auxesz (output_bfd);
5797 if (bfd_seek (output_bfd,
5798 (obj_sym_filepos (output_bfd)
5799 + (obj_raw_syment_count (output_bfd)
5800 * bfd_coff_symesz (output_bfd))),
5801 SEEK_SET) != 0
5802 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
5803 != (bfd_size_type) (outsym - finfo->outsyms)))
5804 return false;
5805 obj_raw_syment_count (output_bfd) +=
5806 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5808 return true;
5811 /* Handle a link order which is supposed to generate a reloc. */
5813 static boolean
5814 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5815 bfd *output_bfd;
5816 struct xcoff_final_link_info *finfo;
5817 asection *output_section;
5818 struct bfd_link_order *link_order;
5820 reloc_howto_type *howto;
5821 struct xcoff_link_hash_entry *h;
5822 asection *hsec;
5823 bfd_vma hval;
5824 bfd_vma addend;
5825 struct internal_reloc *irel;
5826 struct xcoff_link_hash_entry **rel_hash_ptr;
5827 struct internal_ldrel ldrel;
5829 if (link_order->type == bfd_section_reloc_link_order)
5831 /* We need to somehow locate a symbol in the right section. The
5832 symbol must either have a value of zero, or we must adjust
5833 the addend by the value of the symbol. FIXME: Write this
5834 when we need it. The old linker couldn't handle this anyhow. */
5835 abort ();
5838 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5839 if (howto == NULL)
5841 bfd_set_error (bfd_error_bad_value);
5842 return false;
5845 h = ((struct xcoff_link_hash_entry *)
5846 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5847 link_order->u.reloc.p->u.name,
5848 false, false, true));
5849 if (h == NULL)
5851 if (! ((*finfo->info->callbacks->unattached_reloc)
5852 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5853 (asection *) NULL, (bfd_vma) 0)))
5854 return false;
5855 return true;
5858 if (h->root.type == bfd_link_hash_common)
5860 hsec = h->root.u.c.p->section;
5861 hval = 0;
5863 else if (h->root.type == bfd_link_hash_defined
5864 || h->root.type == bfd_link_hash_defweak)
5866 hsec = h->root.u.def.section;
5867 hval = h->root.u.def.value;
5869 else
5871 hsec = NULL;
5872 hval = 0;
5875 addend = link_order->u.reloc.p->addend;
5876 if (hsec != NULL)
5877 addend += (hsec->output_section->vma
5878 + hsec->output_offset
5879 + hval);
5881 if (addend != 0)
5883 bfd_size_type size;
5884 bfd_byte *buf;
5885 bfd_reloc_status_type rstat;
5886 boolean ok;
5888 size = bfd_get_reloc_size (howto);
5889 buf = (bfd_byte *) bfd_zmalloc (size);
5890 if (buf == NULL)
5891 return false;
5893 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5894 switch (rstat)
5896 case bfd_reloc_ok:
5897 break;
5898 default:
5899 case bfd_reloc_outofrange:
5900 abort ();
5901 case bfd_reloc_overflow:
5902 if (! ((*finfo->info->callbacks->reloc_overflow)
5903 (finfo->info, link_order->u.reloc.p->u.name,
5904 howto->name, addend, (bfd *) NULL, (asection *) NULL,
5905 (bfd_vma) 0)))
5907 free (buf);
5908 return false;
5910 break;
5912 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5913 (file_ptr) link_order->offset, size);
5914 free (buf);
5915 if (! ok)
5916 return false;
5919 /* Store the reloc information in the right place. It will get
5920 swapped and written out at the end of the final_link routine. */
5922 irel = (finfo->section_info[output_section->target_index].relocs
5923 + output_section->reloc_count);
5924 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5925 + output_section->reloc_count);
5927 memset (irel, 0, sizeof (struct internal_reloc));
5928 *rel_hash_ptr = NULL;
5930 irel->r_vaddr = output_section->vma + link_order->offset;
5932 if (h->indx >= 0)
5933 irel->r_symndx = h->indx;
5934 else
5936 /* Set the index to -2 to force this symbol to get written out. */
5937 h->indx = -2;
5938 *rel_hash_ptr = h;
5939 irel->r_symndx = 0;
5942 irel->r_type = howto->type;
5943 irel->r_size = howto->bitsize - 1;
5944 if (howto->complain_on_overflow == complain_overflow_signed)
5945 irel->r_size |= 0x80;
5947 ++output_section->reloc_count;
5949 /* Now output the reloc to the .loader section. */
5951 ldrel.l_vaddr = irel->r_vaddr;
5953 if (hsec != NULL)
5955 const char *secname;
5957 secname = hsec->output_section->name;
5959 if (strcmp (secname, ".text") == 0)
5960 ldrel.l_symndx = 0;
5961 else if (strcmp (secname, ".data") == 0)
5962 ldrel.l_symndx = 1;
5963 else if (strcmp (secname, ".bss") == 0)
5964 ldrel.l_symndx = 2;
5965 else
5967 (*_bfd_error_handler)
5968 (_("%s: loader reloc in unrecognized section `%s'"),
5969 bfd_get_filename (output_bfd), secname);
5970 bfd_set_error (bfd_error_nonrepresentable_section);
5971 return false;
5974 else
5976 if (h->ldindx < 0)
5978 (*_bfd_error_handler)
5979 (_("%s: `%s' in loader reloc but not loader sym"),
5980 bfd_get_filename (output_bfd),
5981 h->root.root.string);
5982 bfd_set_error (bfd_error_bad_value);
5983 return false;
5985 ldrel.l_symndx = h->ldindx;
5988 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5989 ldrel.l_rsecnm = output_section->target_index;
5990 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5991 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5993 return true;
5996 /* Sort relocs by VMA. This is called via qsort. */
5998 static int
5999 xcoff_sort_relocs (p1, p2)
6000 const PTR p1;
6001 const PTR p2;
6003 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6004 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6006 if (r1->r_vaddr > r2->r_vaddr)
6007 return 1;
6008 else if (r1->r_vaddr < r2->r_vaddr)
6009 return -1;
6010 else
6011 return 0;