Add AIX 64 shared library support and emulation layer for binutils
[binutils.git] / bfd / xcofflink.c
blobb817426ae68205dd91841ae6f8cbe78782854cd6
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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
44 /* The next entry in the list. */
45 struct xcoff_import_file *next;
46 /* The path. */
47 const char *path;
48 /* The file name. */
49 const char *file;
50 /* The member name. */
51 const char *member;
54 /* Information we keep for each section in the output file during the
55 final link phase. */
57 struct xcoff_link_section_info
59 /* The relocs to be output. */
60 struct internal_reloc *relocs;
61 /* For each reloc against a global symbol whose index was not known
62 when the reloc was handled, the global hash table entry. */
63 struct xcoff_link_hash_entry **rel_hashes;
64 /* If there is a TOC relative reloc against a global symbol, and the
65 index of the TOC symbol is not known when the reloc was handled,
66 an entry is added to this linked list. This is not an array,
67 like rel_hashes, because this case is quite uncommon. */
68 struct xcoff_toc_rel_hash {
69 struct xcoff_toc_rel_hash *next;
70 struct xcoff_link_hash_entry *h;
71 struct internal_reloc *rel;
72 } *toc_rel_hashes;
75 /* Information that we pass around while doing the final link step. */
77 struct xcoff_final_link_info
79 /* General link information. */
80 struct bfd_link_info *info;
81 /* Output BFD. */
82 bfd *output_bfd;
83 /* Hash table for long symbol names. */
84 struct bfd_strtab_hash *strtab;
85 /* Array of information kept for each output section, indexed by the
86 target_index field. */
87 struct xcoff_link_section_info *section_info;
88 /* Symbol index of last C_FILE symbol (-1 if none). */
89 long last_file_index;
90 /* Contents of last C_FILE symbol. */
91 struct internal_syment last_file;
92 /* Symbol index of TOC symbol. */
93 long toc_symindx;
94 /* Start of .loader symbols. */
95 bfd_byte *ldsym;
96 /* Next .loader reloc to swap out. */
97 bfd_byte *ldrel;
98 /* File position of start of line numbers. */
99 file_ptr line_filepos;
100 /* Buffer large enough to hold swapped symbols of any input file. */
101 struct internal_syment *internal_syms;
102 /* Buffer large enough to hold output indices of symbols of any
103 input file. */
104 long *sym_indices;
105 /* Buffer large enough to hold output symbols for any input file. */
106 bfd_byte *outsyms;
107 /* Buffer large enough to hold external line numbers for any input
108 section. */
109 bfd_byte *linenos;
110 /* Buffer large enough to hold any input section. */
111 bfd_byte *contents;
112 /* Buffer large enough to hold external relocs of any input section. */
113 bfd_byte *external_relocs;
116 static struct bfd_hash_entry *xcoff_link_hash_newfunc
117 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
118 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
119 static struct internal_reloc *xcoff_read_internal_relocs
120 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
121 struct internal_reloc *));
122 static boolean xcoff_link_add_object_symbols
123 PARAMS ((bfd *, struct bfd_link_info *));
124 static boolean xcoff_link_check_archive_element
125 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
126 static boolean xcoff_link_check_ar_symbols
127 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
128 static boolean xcoff_link_check_dynamic_ar_symbols
129 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
130 static bfd_size_type xcoff_find_reloc
131 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
132 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
133 static boolean xcoff_link_add_dynamic_symbols
134 PARAMS ((bfd *, struct bfd_link_info *));
135 static boolean xcoff_mark_symbol
136 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
137 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
138 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
139 static boolean xcoff_build_ldsyms
140 PARAMS ((struct xcoff_link_hash_entry *, PTR));
141 static boolean xcoff_link_input_bfd
142 PARAMS ((struct xcoff_final_link_info *, bfd *));
143 static boolean xcoff_write_global_symbol
144 PARAMS ((struct xcoff_link_hash_entry *, PTR));
145 static boolean xcoff_reloc_link_order
146 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
147 struct bfd_link_order *));
148 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
151 /* Routines to read XCOFF dynamic information. This don't really
152 belong here, but we already have the ldsym manipulation routines
153 here. */
155 /* Read the contents of a section. */
157 static boolean
158 xcoff_get_section_contents (abfd, sec)
159 bfd *abfd;
160 asection *sec;
163 if (coff_section_data (abfd, sec) == NULL)
165 bfd_size_type amt = sizeof (struct coff_section_tdata);
166 sec->used_by_bfd = bfd_zalloc (abfd, amt);
167 if (sec->used_by_bfd == NULL)
168 return false;
171 if (coff_section_data (abfd, sec)->contents == NULL)
173 coff_section_data (abfd, sec)->contents = ((bfd_byte *)
174 bfd_malloc (sec->_raw_size));
175 if (coff_section_data (abfd, sec)->contents == NULL)
176 return false;
178 if (! bfd_get_section_contents (abfd, sec,
179 coff_section_data (abfd, sec)->contents,
180 (file_ptr) 0, sec->_raw_size))
181 return false;
184 return true;
187 /* Get the size required to hold the dynamic symbols. */
189 long
190 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
191 bfd *abfd;
193 asection *lsec;
194 bfd_byte *contents;
195 struct internal_ldhdr ldhdr;
197 if ((abfd->flags & DYNAMIC) == 0)
199 bfd_set_error (bfd_error_invalid_operation);
200 return -1;
203 lsec = bfd_get_section_by_name (abfd, ".loader");
204 if (lsec == NULL)
206 bfd_set_error (bfd_error_no_symbols);
207 return -1;
210 if (! xcoff_get_section_contents (abfd, lsec))
211 return -1;
212 contents = coff_section_data (abfd, lsec)->contents;
214 bfd_xcoff_swap_ldhdr_in (abfd, (PTR) contents, &ldhdr);
216 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
219 /* Get the dynamic symbols. */
221 long
222 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
223 bfd *abfd;
224 asymbol **psyms;
226 asection *lsec;
227 bfd_byte *contents;
228 struct internal_ldhdr ldhdr;
229 const char *strings;
230 bfd_byte *elsym, *elsymend;
231 coff_symbol_type *symbuf;
233 if ((abfd->flags & DYNAMIC) == 0)
235 bfd_set_error (bfd_error_invalid_operation);
236 return -1;
239 lsec = bfd_get_section_by_name (abfd, ".loader");
240 if (lsec == NULL)
242 bfd_set_error (bfd_error_no_symbols);
243 return -1;
246 if (! xcoff_get_section_contents (abfd, lsec))
247 return -1;
248 contents = coff_section_data (abfd, lsec)->contents;
250 coff_section_data (abfd, lsec)->keep_contents = true;
252 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
254 strings = (char *) contents + ldhdr.l_stoff;
256 symbuf = ((coff_symbol_type *)
257 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
258 if (symbuf == NULL)
259 return -1;
261 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
263 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
264 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
266 struct internal_ldsym ldsym;
268 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
270 symbuf->symbol.the_bfd = abfd;
272 if (ldsym._l._l_l._l_zeroes == 0)
273 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
274 else
276 char *c;
278 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
279 if (c == NULL)
280 return -1;
281 memcpy (c, ldsym._l._l_name, SYMNMLEN);
282 c[SYMNMLEN] = '\0';
283 symbuf->symbol.name = c;
286 if (ldsym.l_smclas == XMC_XO)
287 symbuf->symbol.section = bfd_abs_section_ptr;
288 else
289 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
290 ldsym.l_scnum);
291 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
293 symbuf->symbol.flags = BSF_NO_FLAGS;
294 if ((ldsym.l_smtype & L_EXPORT) != 0)
295 symbuf->symbol.flags |= BSF_GLOBAL;
297 /* FIXME: We have no way to record the other information stored
298 with the loader symbol. */
300 *psyms = (asymbol *) symbuf;
303 *psyms = NULL;
305 return ldhdr.l_nsyms;
308 /* Get the size required to hold the dynamic relocs. */
310 long
311 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
312 bfd *abfd;
314 asection *lsec;
315 bfd_byte *contents;
316 struct internal_ldhdr ldhdr;
318 if ((abfd->flags & DYNAMIC) == 0)
320 bfd_set_error (bfd_error_invalid_operation);
321 return -1;
324 lsec = bfd_get_section_by_name (abfd, ".loader");
325 if (lsec == NULL)
327 bfd_set_error (bfd_error_no_symbols);
328 return -1;
331 if (! xcoff_get_section_contents (abfd, lsec))
332 return -1;
333 contents = coff_section_data (abfd, lsec)->contents;
335 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
337 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
340 /* Get the dynamic relocs. */
342 long
343 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
344 bfd *abfd;
345 arelent **prelocs;
346 asymbol **syms;
348 asection *lsec;
349 bfd_byte *contents;
350 struct internal_ldhdr ldhdr;
351 arelent *relbuf;
352 bfd_byte *elrel, *elrelend;
354 if ((abfd->flags & DYNAMIC) == 0)
356 bfd_set_error (bfd_error_invalid_operation);
357 return -1;
360 lsec = bfd_get_section_by_name (abfd, ".loader");
361 if (lsec == NULL)
363 bfd_set_error (bfd_error_no_symbols);
364 return -1;
367 if (! xcoff_get_section_contents (abfd, lsec))
368 return -1;
369 contents = coff_section_data (abfd, lsec)->contents;
371 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
373 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
374 if (relbuf == NULL)
375 return -1;
377 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
379 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
380 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
381 prelocs++)
383 struct internal_ldrel ldrel;
385 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
387 if (ldrel.l_symndx >= 3)
388 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
389 else
391 const char *name;
392 asection *sec;
394 switch (ldrel.l_symndx)
396 case 0:
397 name = ".text";
398 break;
399 case 1:
400 name = ".data";
401 break;
402 case 2:
403 name = ".bss";
404 break;
405 default:
406 abort ();
407 break;
410 sec = bfd_get_section_by_name (abfd, name);
411 if (sec == NULL)
413 bfd_set_error (bfd_error_bad_value);
414 return -1;
417 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
420 relbuf->address = ldrel.l_vaddr;
421 relbuf->addend = 0;
423 /* Most dynamic relocs have the same type. FIXME: This is only
424 correct if ldrel.l_rtype == 0. In other cases, we should use
425 a different howto. */
426 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
428 /* FIXME: We have no way to record the l_rsecnm field. */
430 *prelocs = relbuf;
433 *prelocs = NULL;
435 return ldhdr.l_nreloc;
438 /* Routine to create an entry in an XCOFF link hash table. */
440 static struct bfd_hash_entry *
441 xcoff_link_hash_newfunc (entry, table, string)
442 struct bfd_hash_entry *entry;
443 struct bfd_hash_table *table;
444 const char *string;
446 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
448 /* Allocate the structure if it has not already been allocated by a
449 subclass. */
450 if (ret == (struct xcoff_link_hash_entry *) NULL)
451 ret = ((struct xcoff_link_hash_entry *)
452 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
453 if (ret == (struct xcoff_link_hash_entry *) NULL)
454 return (struct bfd_hash_entry *) ret;
456 /* Call the allocation method of the superclass. */
457 ret = ((struct xcoff_link_hash_entry *)
458 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
459 table, string));
460 if (ret != NULL)
462 /* Set local fields. */
463 ret->indx = -1;
464 ret->toc_section = NULL;
465 ret->u.toc_indx = -1;
466 ret->descriptor = NULL;
467 ret->ldsym = NULL;
468 ret->ldindx = -1;
469 ret->flags = 0;
470 ret->smclas = XMC_UA;
473 return (struct bfd_hash_entry *) ret;
476 /* Create a XCOFF link hash table. */
478 struct bfd_link_hash_table *
479 _bfd_xcoff_bfd_link_hash_table_create (abfd)
480 bfd *abfd;
482 struct xcoff_link_hash_table *ret;
483 bfd_size_type amt = sizeof (struct xcoff_link_hash_table);
485 ret = (struct xcoff_link_hash_table *) bfd_alloc (abfd, amt);
486 if (ret == (struct xcoff_link_hash_table *) NULL)
487 return (struct bfd_link_hash_table *) NULL;
488 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
490 bfd_release (abfd, ret);
491 return (struct bfd_link_hash_table *) NULL;
494 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
495 ret->debug_section = NULL;
496 ret->loader_section = NULL;
497 ret->ldrel_count = 0;
498 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
499 ret->linkage_section = NULL;
500 ret->toc_section = NULL;
501 ret->descriptor_section = NULL;
502 ret->imports = NULL;
503 ret->file_align = 0;
504 ret->textro = false;
505 ret->gc = false;
506 memset (ret->special_sections, 0, sizeof ret->special_sections);
508 /* The linker will always generate a full a.out header. We need to
509 record that fact now, before the sizeof_headers routine could be
510 called. */
511 xcoff_data (abfd)->full_aouthdr = true;
513 return &ret->root;
517 /* Read internal relocs for an XCOFF csect. This is a wrapper around
518 _bfd_coff_read_internal_relocs which tries to take advantage of any
519 relocs which may have been cached for the enclosing section. */
521 static struct internal_reloc *
522 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
523 require_internal, internal_relocs)
524 bfd *abfd;
525 asection *sec;
526 boolean cache;
527 bfd_byte *external_relocs;
528 boolean require_internal;
529 struct internal_reloc *internal_relocs;
532 if (coff_section_data (abfd, sec) != NULL
533 && coff_section_data (abfd, sec)->relocs == NULL
534 && xcoff_section_data (abfd, sec) != NULL)
536 asection *enclosing;
538 enclosing = xcoff_section_data (abfd, sec)->enclosing;
540 if (enclosing != NULL
541 && (coff_section_data (abfd, enclosing) == NULL
542 || coff_section_data (abfd, enclosing)->relocs == NULL)
543 && cache
544 && enclosing->reloc_count > 0)
546 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
547 external_relocs, false,
548 (struct internal_reloc *) NULL)
549 == NULL)
550 return NULL;
553 if (enclosing != NULL
554 && coff_section_data (abfd, enclosing) != NULL
555 && coff_section_data (abfd, enclosing)->relocs != NULL)
557 size_t off;
559 off = ((sec->rel_filepos - enclosing->rel_filepos)
560 / bfd_coff_relsz (abfd));
562 if (! require_internal)
563 return coff_section_data (abfd, enclosing)->relocs + off;
564 memcpy (internal_relocs,
565 coff_section_data (abfd, enclosing)->relocs + off,
566 sec->reloc_count * sizeof (struct internal_reloc));
567 return internal_relocs;
571 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
572 require_internal, internal_relocs);
575 /* Given an XCOFF BFD, add symbols to the global hash table as
576 appropriate. */
578 boolean
579 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
580 bfd *abfd;
581 struct bfd_link_info *info;
584 switch (bfd_get_format (abfd))
586 case bfd_object:
587 return xcoff_link_add_object_symbols (abfd, info);
589 case bfd_archive:
590 /* If the archive has a map, do the usual search. We then need
591 to check the archive for stripped dynamic objects, because
592 they will not appear in the archive map even though they
593 should, perhaps, be included. If the archive has no map, we
594 just consider each object file in turn, since that apparently
595 is what the AIX native linker does. */
596 if (bfd_has_map (abfd))
598 if (! (_bfd_generic_link_add_archive_symbols
599 (abfd, info, xcoff_link_check_archive_element)))
600 return false;
604 bfd *member;
606 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
607 while (member != NULL)
609 if (bfd_check_format (member, bfd_object)
610 && (! bfd_has_map (abfd)
611 || ((member->flags & DYNAMIC) != 0
612 && (member->flags & HAS_SYMS) == 0)))
614 boolean needed;
616 if (! xcoff_link_check_archive_element (member, info, &needed))
617 return false;
618 if (needed)
619 member->archive_pass = -1;
621 member = bfd_openr_next_archived_file (abfd, member);
625 return true;
627 default:
628 bfd_set_error (bfd_error_wrong_format);
629 return false;
633 /* Add symbols from an XCOFF object file. */
635 static boolean
636 xcoff_link_add_object_symbols (abfd, info)
637 bfd *abfd;
638 struct bfd_link_info *info;
641 if (! _bfd_coff_get_external_symbols (abfd))
642 return false;
643 if (! xcoff_link_add_symbols (abfd, info))
644 return false;
645 if (! info->keep_memory)
647 if (! _bfd_coff_free_symbols (abfd))
648 return false;
650 return true;
653 /* Check a single archive element to see if we need to include it in
654 the link. *PNEEDED is set according to whether this element is
655 needed in the link or not. This is called via
656 _bfd_generic_link_add_archive_symbols. */
658 static boolean
659 xcoff_link_check_archive_element (abfd, info, pneeded)
660 bfd *abfd;
661 struct bfd_link_info *info;
662 boolean *pneeded;
665 if (! _bfd_coff_get_external_symbols (abfd))
666 return false;
668 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
669 return false;
671 if (*pneeded)
673 if (! xcoff_link_add_symbols (abfd, info))
674 return false;
677 if (! info->keep_memory || ! *pneeded)
679 if (! _bfd_coff_free_symbols (abfd))
680 return false;
683 return true;
686 /* Look through the symbols to see if this object file should be
687 included in the link. */
689 static boolean
690 xcoff_link_check_ar_symbols (abfd, info, pneeded)
691 bfd *abfd;
692 struct bfd_link_info *info;
693 boolean *pneeded;
695 bfd_size_type symesz;
696 bfd_byte *esym;
697 bfd_byte *esym_end;
699 *pneeded = false;
701 if ((abfd->flags & DYNAMIC) != 0
702 && ! info->static_link
703 && info->hash->creator == abfd->xvec)
704 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
706 symesz = bfd_coff_symesz (abfd);
707 esym = (bfd_byte *) obj_coff_external_syms (abfd);
708 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
709 while (esym < esym_end)
711 struct internal_syment sym;
713 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
715 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
717 const char *name;
718 char buf[SYMNMLEN + 1];
719 struct bfd_link_hash_entry *h;
721 /* This symbol is externally visible, and is defined by this
722 object file. */
724 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
726 if (name == NULL)
727 return false;
728 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
730 /* We are only interested in symbols that are currently
731 undefined. If a symbol is currently known to be common,
732 XCOFF linkers do not bring in an object file which
733 defines it. We also don't bring in symbols to satisfy
734 undefined references in shared objects. */
735 if (h != (struct bfd_link_hash_entry *) NULL
736 && h->type == bfd_link_hash_undefined
737 && (info->hash->creator != abfd->xvec
738 || (((struct xcoff_link_hash_entry *) h)->flags
739 & XCOFF_DEF_DYNAMIC) == 0))
741 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
742 return false;
743 *pneeded = true;
744 return true;
748 esym += (sym.n_numaux + 1) * symesz;
751 /* We do not need this object file. */
752 return true;
755 /* Look through the loader symbols to see if this dynamic object
756 should be included in the link. The native linker uses the loader
757 symbols, not the normal symbol table, so we do too. */
759 static boolean
760 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
761 bfd *abfd;
762 struct bfd_link_info *info;
763 boolean *pneeded;
765 asection *lsec;
766 bfd_byte *contents;
767 struct internal_ldhdr ldhdr;
768 const char *strings;
769 bfd_byte *elsym, *elsymend;
771 *pneeded = false;
773 lsec = bfd_get_section_by_name (abfd, ".loader");
774 if (lsec == NULL)
776 /* There are no symbols, so don't try to include it. */
777 return true;
780 if (! xcoff_get_section_contents (abfd, lsec))
781 return false;
782 contents = coff_section_data (abfd, lsec)->contents;
784 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
786 strings = (char *) contents + ldhdr.l_stoff;
788 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
790 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
791 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
793 struct internal_ldsym ldsym;
794 char nambuf[SYMNMLEN + 1];
795 const char *name;
796 struct bfd_link_hash_entry *h;
798 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
800 /* We are only interested in exported symbols. */
801 if ((ldsym.l_smtype & L_EXPORT) == 0)
802 continue;
804 if (ldsym._l._l_l._l_zeroes == 0)
805 name = strings + ldsym._l._l_l._l_offset;
806 else
808 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
809 nambuf[SYMNMLEN] = '\0';
810 name = nambuf;
813 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
815 /* We are only interested in symbols that are currently
816 undefined. At this point we know that we are using an XCOFF
817 hash table. */
818 if (h != NULL
819 && h->type == bfd_link_hash_undefined
820 && (((struct xcoff_link_hash_entry *) h)->flags
821 & XCOFF_DEF_DYNAMIC) == 0)
823 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
824 return false;
825 *pneeded = true;
826 return true;
830 /* We do not need this shared object. */
832 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
834 free (coff_section_data (abfd, lsec)->contents);
835 coff_section_data (abfd, lsec)->contents = NULL;
838 return true;
841 /* Returns the index of reloc in RELOCS with the least address greater
842 than or equal to ADDRESS. The relocs are sorted by address. */
844 static bfd_size_type
845 xcoff_find_reloc (relocs, count, address)
846 struct internal_reloc *relocs;
847 bfd_size_type count;
848 bfd_vma address;
850 bfd_size_type min, max, this;
852 if (count < 2)
854 if (count == 1 && relocs[0].r_vaddr < address)
855 return 1;
856 else
857 return 0;
860 min = 0;
861 max = count;
863 /* Do a binary search over (min,max]. */
864 while (min + 1 < max)
866 bfd_vma raddr;
868 this = (max + min) / 2;
869 raddr = relocs[this].r_vaddr;
870 if (raddr > address)
871 max = this;
872 else if (raddr < address)
873 min = this;
874 else
876 min = this;
877 break;
881 if (relocs[min].r_vaddr < address)
882 return min + 1;
884 while (min > 0
885 && relocs[min - 1].r_vaddr == address)
886 --min;
888 return min;
892 /* xcoff_link_create_extra_sections
894 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
896 static boolean
897 xcoff_link_create_extra_sections(bfd * abfd, struct bfd_link_info *info)
900 boolean return_value = false;
902 if (info->hash->creator == abfd->xvec)
905 /* We need to build a .loader section, so we do it here. This
906 won't work if we're producing an XCOFF output file with no
907 XCOFF input files. FIXME. */
909 if (xcoff_hash_table (info)->loader_section == NULL)
911 asection *lsec;
913 lsec = bfd_make_section_anyway (abfd, ".loader");
914 if (lsec == NULL)
916 goto end_return;
918 xcoff_hash_table (info)->loader_section = lsec;
919 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
922 /* Likewise for the linkage section. */
923 if (xcoff_hash_table (info)->linkage_section == NULL)
925 asection *lsec;
927 lsec = bfd_make_section_anyway (abfd, ".gl");
928 if (lsec == NULL)
930 goto end_return;
933 xcoff_hash_table (info)->linkage_section = lsec;
934 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
935 | SEC_IN_MEMORY);
936 lsec->alignment_power = 2;
939 /* Likewise for the TOC section. */
940 if (xcoff_hash_table (info)->toc_section == NULL)
942 asection *tsec;
944 tsec = bfd_make_section_anyway (abfd, ".tc");
945 if (tsec == NULL)
947 goto end_return;
950 xcoff_hash_table (info)->toc_section = tsec;
951 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
952 | SEC_IN_MEMORY);
953 tsec->alignment_power = 2;
956 /* Likewise for the descriptor section. */
957 if (xcoff_hash_table (info)->descriptor_section == NULL)
959 asection *dsec;
961 dsec = bfd_make_section_anyway (abfd, ".ds");
962 if (dsec == NULL)
964 goto end_return;
967 xcoff_hash_table (info)->descriptor_section = dsec;
968 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
969 | SEC_IN_MEMORY);
970 dsec->alignment_power = 2;
973 /* Likewise for the .debug section. */
974 if (xcoff_hash_table (info)->debug_section == NULL
975 && info->strip != strip_all)
977 asection *dsec;
979 dsec = bfd_make_section_anyway (abfd, ".debug");
980 if (dsec == NULL)
982 goto end_return;
984 xcoff_hash_table (info)->debug_section = dsec;
985 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
989 return_value = true;
991 end_return:
993 return return_value;
996 /* Add all the symbols from an object file to the hash table.
998 XCOFF is a weird format. A normal XCOFF .o files will have three
999 COFF sections--.text, .data, and .bss--but each COFF section will
1000 contain many csects. These csects are described in the symbol
1001 table. From the linker's point of view, each csect must be
1002 considered a section in its own right. For example, a TOC entry is
1003 handled as a small XMC_TC csect. The linker must be able to merge
1004 different TOC entries together, which means that it must be able to
1005 extract the XMC_TC csects from the .data section of the input .o
1006 file.
1008 From the point of view of our linker, this is, of course, a hideous
1009 nightmare. We cope by actually creating sections for each csect,
1010 and discarding the original sections. We then have to handle the
1011 relocation entries carefully, since the only way to tell which
1012 csect they belong to is to examine the address. */
1014 static boolean
1015 xcoff_link_add_symbols (abfd, info)
1016 bfd *abfd;
1017 struct bfd_link_info *info;
1019 unsigned int n_tmask;
1020 unsigned int n_btshft;
1021 boolean default_copy;
1022 bfd_size_type symcount;
1023 struct xcoff_link_hash_entry **sym_hash;
1024 asection **csect_cache;
1025 bfd_size_type linesz;
1026 asection *o;
1027 asection *last_real;
1028 boolean keep_syms;
1029 asection *csect;
1030 unsigned int csect_index;
1031 asection *first_csect;
1032 bfd_size_type symesz;
1033 bfd_byte *esym;
1034 bfd_byte *esym_end;
1035 struct reloc_info_struct
1037 struct internal_reloc *relocs;
1038 asection **csects;
1039 bfd_byte *linenos;
1040 } *reloc_info = NULL;
1041 bfd_size_type amt;
1043 keep_syms = obj_coff_keep_syms (abfd);
1045 if ((abfd->flags & DYNAMIC) != 0
1046 && ! info->static_link)
1048 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1049 return false;
1052 /* create the loader, toc, gl, ds and debug sections, if needed */
1053 if (false == xcoff_link_create_extra_sections(abfd, info))
1054 goto error_return;
1056 if ((abfd->flags & DYNAMIC) != 0
1057 && ! info->static_link)
1058 return true;
1060 n_tmask = coff_data (abfd)->local_n_tmask;
1061 n_btshft = coff_data (abfd)->local_n_btshft;
1063 /* Define macros so that ISFCN, et. al., macros work correctly. */
1064 #define N_TMASK n_tmask
1065 #define N_BTSHFT n_btshft
1067 if (info->keep_memory)
1068 default_copy = false;
1069 else
1070 default_copy = true;
1072 symcount = obj_raw_syment_count (abfd);
1074 /* We keep a list of the linker hash table entries that correspond
1075 to each external symbol. */
1076 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1077 sym_hash = (struct xcoff_link_hash_entry **) bfd_alloc (abfd, amt);
1078 if (sym_hash == NULL && symcount != 0)
1079 goto error_return;
1080 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1081 memset (sym_hash, 0, (size_t) amt);
1083 /* Because of the weird stuff we are doing with XCOFF csects, we can
1084 not easily determine which section a symbol is in, so we store
1085 the information in the tdata for the input file. */
1086 amt = symcount * sizeof (asection *);
1087 csect_cache = (asection **) bfd_alloc (abfd, amt);
1088 if (csect_cache == NULL && symcount != 0)
1089 goto error_return;
1090 xcoff_data (abfd)->csects = csect_cache;
1091 memset (csect_cache, 0, (size_t) amt);
1093 /* While splitting sections into csects, we need to assign the
1094 relocs correctly. The relocs and the csects must both be in
1095 order by VMA within a given section, so we handle this by
1096 scanning along the relocs as we process the csects. We index
1097 into reloc_info using the section target_index. */
1098 amt = abfd->section_count + 1;
1099 amt *= sizeof (struct reloc_info_struct);
1100 reloc_info = (struct reloc_info_struct *) bfd_malloc (amt);
1101 if (reloc_info == NULL)
1102 goto error_return;
1103 memset ((PTR) reloc_info, 0, (size_t) amt);
1105 /* Read in the relocs and line numbers for each section. */
1106 linesz = bfd_coff_linesz (abfd);
1107 last_real = NULL;
1108 for (o = abfd->sections; o != NULL; o = o->next)
1111 last_real = o;
1112 if ((o->flags & SEC_RELOC) != 0)
1115 reloc_info[o->target_index].relocs =
1116 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1117 false, (struct internal_reloc *) NULL);
1118 amt = o->reloc_count;
1119 amt *= sizeof (asection *);
1120 reloc_info[o->target_index].csects = (asection **) bfd_malloc (amt);
1121 if (reloc_info[o->target_index].csects == NULL)
1122 goto error_return;
1123 memset (reloc_info[o->target_index].csects, 0, (size_t) amt);
1127 if ((info->strip == strip_none || info->strip == strip_some)
1128 && o->lineno_count > 0)
1131 bfd_byte *linenos;
1133 amt = linesz * o->lineno_count;
1134 linenos = (bfd_byte *) bfd_malloc (amt);
1135 if (linenos == NULL)
1136 goto error_return;
1137 reloc_info[o->target_index].linenos = linenos;
1138 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1139 || bfd_bread (linenos, amt, abfd) != amt)
1140 goto error_return;
1145 /* Don't let the linker relocation routines discard the symbols. */
1146 obj_coff_keep_syms (abfd) = true;
1148 csect = NULL;
1149 csect_index = 0;
1150 first_csect = NULL;
1152 symesz = bfd_coff_symesz (abfd);
1153 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1154 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1155 esym_end = esym + symcount * symesz;
1157 while (esym < esym_end)
1159 struct internal_syment sym;
1160 union internal_auxent aux;
1161 const char *name;
1162 char buf[SYMNMLEN + 1];
1163 int smtyp;
1164 flagword flags;
1165 asection *section;
1166 bfd_vma value;
1167 struct xcoff_link_hash_entry *set_toc;
1169 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1171 /* In this pass we are only interested in symbols with csect
1172 information. */
1173 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1176 /* Set csect_cache,
1177 Normally csect is a .pr, .rw etc. created in the loop
1178 If C_FILE or first time, handle special
1180 Advance esym, sym_hash, csect_hash ptr's
1181 Keep track of the last_symndx for the current file. */
1182 if (sym.n_sclass == C_FILE && csect != NULL)
1184 xcoff_section_data (abfd, csect)->last_symndx =
1185 ((esym
1186 - (bfd_byte *) obj_coff_external_syms (abfd))
1187 / symesz);
1188 csect = NULL;
1191 if (csect != NULL)
1192 *csect_cache = csect;
1193 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1194 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1195 else
1196 *csect_cache = NULL;
1197 esym += (sym.n_numaux + 1) * symesz;
1198 sym_hash += sym.n_numaux + 1;
1199 csect_cache += sym.n_numaux + 1;
1201 continue;
1204 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1206 if (name == NULL)
1207 goto error_return;
1209 /* If this symbol has line number information attached to it,
1210 and we're not stripping it, count the number of entries and
1211 add them to the count for this csect. In the final link pass
1212 we are going to attach line number information by symbol,
1213 rather than by section, in order to more easily handle
1214 garbage collection. */
1215 if ((info->strip == strip_none || info->strip == strip_some)
1216 && sym.n_numaux > 1
1217 && csect != NULL
1218 && ISFCN (sym.n_type))
1221 union internal_auxent auxlin;
1223 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1224 sym.n_type, sym.n_sclass,
1225 0, sym.n_numaux, (PTR) &auxlin);
1227 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1229 asection *enclosing;
1230 bfd_signed_vma linoff;
1232 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1233 if (enclosing == NULL)
1235 (*_bfd_error_handler)
1236 (_("%s: `%s' has line numbers but no enclosing section"),
1237 bfd_archive_filename (abfd), name);
1238 bfd_set_error (bfd_error_bad_value);
1239 goto error_return;
1241 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1242 - enclosing->line_filepos);
1243 /* explict cast to bfd_signed_vma for compiler */
1244 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1246 struct internal_lineno lin;
1247 bfd_byte *linpstart;
1249 linpstart = (reloc_info[enclosing->target_index].linenos
1250 + linoff);
1251 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1252 if (lin.l_lnno == 0
1253 && ((bfd_size_type) lin.l_addr.l_symndx
1254 == ((esym
1255 - (bfd_byte *) obj_coff_external_syms (abfd))
1256 / symesz)))
1258 bfd_byte *linpend, *linp;
1260 linpend = (reloc_info[enclosing->target_index].linenos
1261 + enclosing->lineno_count * linesz);
1262 for (linp = linpstart + linesz;
1263 linp < linpend;
1264 linp += linesz)
1266 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1267 (PTR) &lin);
1268 if (lin.l_lnno == 0)
1269 break;
1271 csect->lineno_count += (linp - linpstart) / linesz;
1272 /* The setting of line_filepos will only be
1273 useful if all the line number entries for a
1274 csect are contiguous; this only matters for
1275 error reporting. */
1276 if (csect->line_filepos == 0)
1277 csect->line_filepos =
1278 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1284 /* Pick up the csect auxiliary information. */
1286 if (sym.n_numaux == 0)
1288 (*_bfd_error_handler)
1289 (_("%s: class %d symbol `%s' has no aux entries"),
1290 bfd_archive_filename (abfd), sym.n_sclass, name);
1291 bfd_set_error (bfd_error_bad_value);
1292 goto error_return;
1295 bfd_coff_swap_aux_in (abfd,
1296 (PTR) (esym + symesz * sym.n_numaux),
1297 sym.n_type, sym.n_sclass,
1298 sym.n_numaux - 1, sym.n_numaux,
1299 (PTR) &aux);
1301 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1303 flags = BSF_GLOBAL;
1304 section = NULL;
1305 value = 0;
1306 set_toc = NULL;
1308 switch (smtyp)
1310 default:
1311 (*_bfd_error_handler)
1312 (_("%s: symbol `%s' has unrecognized csect type %d"),
1313 bfd_archive_filename (abfd), name, smtyp);
1314 bfd_set_error (bfd_error_bad_value);
1315 goto error_return;
1317 case XTY_ER:
1318 /* This is an external reference. */
1319 if (sym.n_sclass == C_HIDEXT
1320 || sym.n_scnum != N_UNDEF
1321 || aux.x_csect.x_scnlen.l != 0)
1323 (*_bfd_error_handler)
1324 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1325 bfd_archive_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1326 aux.x_csect.x_scnlen.l);
1327 bfd_set_error (bfd_error_bad_value);
1328 goto error_return;
1331 /* An XMC_XO external reference is actually a reference to
1332 an absolute location. */
1333 if (aux.x_csect.x_smclas != XMC_XO)
1334 section = bfd_und_section_ptr;
1335 else
1337 section = bfd_abs_section_ptr;
1338 value = sym.n_value;
1340 break;
1342 case XTY_SD:
1343 /* This is a csect definition. */
1344 if (csect != NULL)
1346 xcoff_section_data (abfd, csect)->last_symndx =
1347 ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1350 csect = NULL;
1351 csect_index = -(unsigned) 1;
1353 /* When we see a TOC anchor, we record the TOC value. */
1354 if (aux.x_csect.x_smclas == XMC_TC0)
1356 if (sym.n_sclass != C_HIDEXT
1357 || aux.x_csect.x_scnlen.l != 0)
1359 (*_bfd_error_handler)
1360 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1361 bfd_archive_filename (abfd), name, sym.n_sclass,
1362 aux.x_csect.x_scnlen.l);
1363 bfd_set_error (bfd_error_bad_value);
1364 goto error_return;
1366 xcoff_data (abfd)->toc = sym.n_value;
1369 /* We must merge TOC entries for the same symbol. We can
1370 merge two TOC entries if they are both C_HIDEXT, they
1371 both have the same name, they are both 4 or 8 bytes long, and
1372 they both have a relocation table entry for an external
1373 symbol with the same name. Unfortunately, this means
1374 that we must look through the relocations. Ick.
1376 Logic for 32 bit vs 64 bit.
1377 32 bit has a csect length of 4 for TOC
1378 64 bit has a csect length of 8 for TOC
1380 The conditions to get past the if-check are not that bad.
1381 They are what is used to create the TOC csects in the first
1382 place. */
1383 if (aux.x_csect.x_smclas == XMC_TC
1384 && sym.n_sclass == C_HIDEXT
1385 && info->hash->creator == abfd->xvec
1386 && ((bfd_xcoff_is_xcoff32 (abfd)
1387 && aux.x_csect.x_scnlen.l == 4)
1388 || (bfd_xcoff_is_xcoff64 (abfd)
1389 && aux.x_csect.x_scnlen.l == 8)))
1391 asection *enclosing;
1392 struct internal_reloc *relocs;
1393 bfd_size_type relindx;
1394 struct internal_reloc *rel;
1396 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1397 if (enclosing == NULL)
1398 goto error_return;
1400 relocs = reloc_info[enclosing->target_index].relocs;
1401 amt = enclosing->reloc_count;
1402 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1403 rel = relocs + relindx;
1405 /* 32 bit R_POS r_size is 31
1406 64 bit R_POS r_size is 63 */
1407 if (relindx < enclosing->reloc_count
1408 && rel->r_vaddr == (bfd_vma) sym.n_value
1409 && rel->r_type == R_POS
1410 && ((bfd_xcoff_is_xcoff32 (abfd)
1411 && rel->r_size == 31)
1412 || (bfd_xcoff_is_xcoff64 (abfd)
1413 && rel->r_size == 63)))
1415 bfd_byte *erelsym;
1417 struct internal_syment relsym;
1419 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1420 + rel->r_symndx * symesz);
1421 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1422 if (relsym.n_sclass == C_EXT)
1424 const char *relname;
1425 char relbuf[SYMNMLEN + 1];
1426 boolean copy;
1427 struct xcoff_link_hash_entry *h;
1429 /* At this point we know that the TOC entry is
1430 for an externally visible symbol. */
1432 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1433 relbuf);
1434 if (relname == NULL)
1435 goto error_return;
1437 /* We only merge TOC entries if the TC name is
1438 the same as the symbol name. This handles
1439 the normal case, but not common cases like
1440 SYM.P4 which gcc generates to store SYM + 4
1441 in the TOC. FIXME. */
1443 if (strcmp (name, relname) == 0)
1445 copy = (! info->keep_memory
1446 || relsym._n._n_n._n_zeroes != 0
1447 || relsym._n._n_n._n_offset == 0);
1448 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1449 relname, true, copy,
1450 false);
1451 if (h == NULL)
1452 goto error_return;
1454 /* At this point h->root.type could be
1455 bfd_link_hash_new. That should be OK,
1456 since we know for sure that we will come
1457 across this symbol as we step through the
1458 file. */
1460 /* We store h in *sym_hash for the
1461 convenience of the relocate_section
1462 function. */
1463 *sym_hash = h;
1465 if (h->toc_section != NULL)
1467 asection **rel_csects;
1469 /* We already have a TOC entry for this
1470 symbol, so we can just ignore this
1471 one. */
1472 rel_csects =
1473 reloc_info[enclosing->target_index].csects;
1474 rel_csects[relindx] = bfd_und_section_ptr;
1475 break;
1478 /* We are about to create a TOC entry for
1479 this symbol. */
1480 set_toc = h;
1481 } /* merge toc reloc */
1482 } /* c_ext */
1483 } /* reloc */
1484 } /* merge toc */
1488 asection *enclosing;
1490 /* We need to create a new section. We get the name from
1491 the csect storage mapping class, so that the linker can
1492 accumulate similar csects together. */
1494 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1495 if (NULL == csect)
1497 goto error_return;
1500 /* The enclosing section is the main section : .data, .text
1501 or .bss that the csect is coming from. */
1502 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1503 if (enclosing == NULL)
1504 goto error_return;
1506 if (! bfd_is_abs_section (enclosing)
1507 && ((bfd_vma) sym.n_value < enclosing->vma
1508 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1509 > enclosing->vma + enclosing->_raw_size)))
1511 (*_bfd_error_handler)
1512 (_("%s: csect `%s' not in enclosing section"),
1513 bfd_archive_filename (abfd), name);
1514 bfd_set_error (bfd_error_bad_value);
1515 goto error_return;
1517 csect->vma = sym.n_value;
1518 csect->filepos = (enclosing->filepos
1519 + sym.n_value
1520 - enclosing->vma);
1521 csect->_raw_size = aux.x_csect.x_scnlen.l;
1522 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1523 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1525 /* Record the enclosing section in the tdata for this new
1526 section. */
1527 amt = sizeof (struct coff_section_tdata);
1528 csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1529 if (csect->used_by_bfd == NULL)
1530 goto error_return;
1531 amt = sizeof (struct xcoff_section_tdata);
1532 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1533 if (coff_section_data (abfd, csect)->tdata == NULL)
1534 goto error_return;
1535 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1536 xcoff_section_data (abfd, csect)->lineno_count =
1537 enclosing->lineno_count;
1539 if (enclosing->owner == abfd)
1541 struct internal_reloc *relocs;
1542 bfd_size_type relindx;
1543 struct internal_reloc *rel;
1544 asection **rel_csect;
1546 relocs = reloc_info[enclosing->target_index].relocs;
1547 amt = enclosing->reloc_count;
1548 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1550 rel = relocs + relindx;
1551 rel_csect = (reloc_info[enclosing->target_index].csects
1552 + relindx);
1554 csect->rel_filepos = (enclosing->rel_filepos
1555 + relindx * bfd_coff_relsz (abfd));
1556 while (relindx < enclosing->reloc_count
1557 && *rel_csect == NULL
1558 && rel->r_vaddr < csect->vma + csect->_raw_size)
1561 *rel_csect = csect;
1562 csect->flags |= SEC_RELOC;
1563 ++csect->reloc_count;
1564 ++relindx;
1565 ++rel;
1566 ++rel_csect;
1570 /* There are a number of other fields and section flags
1571 which we do not bother to set. */
1573 csect_index = ((esym
1574 - (bfd_byte *) obj_coff_external_syms (abfd))
1575 / symesz);
1577 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1579 if (first_csect == NULL)
1580 first_csect = csect;
1582 /* If this symbol is C_EXT, we treat it as starting at the
1583 beginning of the newly created section. */
1584 if (sym.n_sclass == C_EXT)
1586 section = csect;
1587 value = 0;
1590 /* If this is a TOC section for a symbol, record it. */
1591 if (set_toc != NULL)
1592 set_toc->toc_section = csect;
1594 break;
1596 case XTY_LD:
1597 /* This is a label definition. The x_scnlen field is the
1598 symbol index of the csect. Usually the XTY_LD symbol will
1599 follow its appropriate XTY_SD symbol. The .set pseudo op can
1600 cause the XTY_LD to not follow the XTY_SD symbol. */
1602 boolean bad;
1604 bad = false;
1605 if (aux.x_csect.x_scnlen.l < 0
1606 || (aux.x_csect.x_scnlen.l
1607 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1608 bad = true;
1609 if (! bad)
1611 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1612 if (section == NULL
1613 || (section->flags & SEC_HAS_CONTENTS) == 0)
1614 bad = true;
1616 if (bad)
1618 (*_bfd_error_handler)
1619 (_("%s: misplaced XTY_LD `%s'"),
1620 bfd_archive_filename (abfd), name);
1621 bfd_set_error (bfd_error_bad_value);
1622 goto error_return;
1624 csect = section;
1625 value = sym.n_value - csect->vma;
1627 break;
1629 case XTY_CM:
1630 /* This is an unitialized csect. We could base the name on
1631 the storage mapping class, but we don't bother except for
1632 an XMC_TD symbol. If this csect is externally visible,
1633 it is a common symbol. We put XMC_TD symbols in sections
1634 named .tocbss, and rely on the linker script to put that
1635 in the TOC area. */
1637 if (csect != NULL)
1639 xcoff_section_data (abfd, csect)->last_symndx =
1640 ((esym
1641 - (bfd_byte *) obj_coff_external_syms (abfd))
1642 / symesz);
1645 if (aux.x_csect.x_smclas == XMC_TD)
1647 /* The linker script puts the .td section in the data
1648 section after the .tc section. */
1649 csect = bfd_make_section_anyway (abfd, ".td");
1652 else
1654 csect = bfd_make_section_anyway (abfd, ".bss");
1656 if (csect == NULL)
1657 goto error_return;
1658 csect->vma = sym.n_value;
1659 csect->_raw_size = aux.x_csect.x_scnlen.l;
1660 csect->flags |= SEC_ALLOC;
1661 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1662 /* There are a number of other fields and section flags
1663 which we do not bother to set. */
1665 csect_index = ((esym
1666 - (bfd_byte *) obj_coff_external_syms (abfd))
1667 / symesz);
1669 amt = sizeof (struct coff_section_tdata);
1670 csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1671 if (csect->used_by_bfd == NULL)
1672 goto error_return;
1673 amt = sizeof (struct xcoff_section_tdata);
1674 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1675 if (coff_section_data (abfd, csect)->tdata == NULL)
1676 goto error_return;
1677 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1679 if (first_csect == NULL)
1680 first_csect = csect;
1682 if (sym.n_sclass == C_EXT)
1684 csect->flags |= SEC_IS_COMMON;
1685 csect->_raw_size = 0;
1686 section = csect;
1687 value = aux.x_csect.x_scnlen.l;
1690 break;
1693 /* Check for magic symbol names. */
1694 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1695 && aux.x_csect.x_smclas != XMC_TC
1696 && aux.x_csect.x_smclas != XMC_TD)
1699 int i = -1;
1701 if (name[0] == '_')
1703 if (strcmp (name, "_text") == 0)
1704 i = XCOFF_SPECIAL_SECTION_TEXT;
1705 else if (strcmp (name, "_etext") == 0)
1706 i = XCOFF_SPECIAL_SECTION_ETEXT;
1707 else if (strcmp (name, "_data") == 0)
1708 i = XCOFF_SPECIAL_SECTION_DATA;
1709 else if (strcmp (name, "_edata") == 0)
1710 i = XCOFF_SPECIAL_SECTION_EDATA;
1711 else if (strcmp (name, "_end") == 0)
1712 i = XCOFF_SPECIAL_SECTION_END;
1714 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1716 i = XCOFF_SPECIAL_SECTION_END2;
1719 if (i != -1)
1721 xcoff_hash_table (info)->special_sections[i] = csect;
1725 /* Now we have enough information to add the symbol to the
1726 linker hash table. */
1728 if (sym.n_sclass == C_EXT)
1730 boolean copy;
1732 BFD_ASSERT (section != NULL);
1734 /* We must copy the name into memory if we got it from the
1735 syment itself, rather than the string table. */
1736 copy = default_copy;
1737 if (sym._n._n_n._n_zeroes != 0
1738 || sym._n._n_n._n_offset == 0)
1739 copy = true;
1741 /* The AIX linker appears to only detect multiple symbol
1742 definitions when there is a reference to the symbol. If
1743 a symbol is defined multiple times, and the only
1744 references are from the same object file, the AIX linker
1745 appears to permit it. It does not merge the different
1746 definitions, but handles them independently. On the
1747 other hand, if there is a reference, the linker reports
1748 an error.
1750 This matters because the AIX <net/net_globals.h> header
1751 file actually defines an initialized array, so we have to
1752 actually permit that to work.
1754 Just to make matters even more confusing, the AIX linker
1755 appears to permit multiple symbol definitions whenever
1756 the second definition is in an archive rather than an
1757 object file. This may be a consequence of the manner in
1758 which it handles archives: I think it may load the entire
1759 archive in as separate csects, and then let garbage
1760 collection discard symbols.
1762 We also have to handle the case of statically linking a
1763 shared object, which will cause symbol redefinitions,
1764 although this is an easier case to detect. */
1766 if (info->hash->creator == abfd->xvec)
1768 if (! bfd_is_und_section (section))
1769 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1770 name, true, copy, false);
1771 else
1772 *sym_hash = ((struct xcoff_link_hash_entry *)
1773 bfd_wrapped_link_hash_lookup (abfd, info, name,
1774 true, copy, false));
1775 if (*sym_hash == NULL)
1776 goto error_return;
1777 if (((*sym_hash)->root.type == bfd_link_hash_defined
1778 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1779 && ! bfd_is_und_section (section)
1780 && ! bfd_is_com_section (section))
1782 /* This is a second definition of a defined symbol. */
1783 if ((abfd->flags & DYNAMIC) != 0
1784 && ((*sym_hash)->smclas != XMC_GL
1785 || aux.x_csect.x_smclas == XMC_GL
1786 || ((*sym_hash)->root.u.def.section->owner->flags
1787 & DYNAMIC) == 0))
1789 /* The new symbol is from a shared library, and
1790 either the existing symbol is not global
1791 linkage code or this symbol is global linkage
1792 code. If the existing symbol is global
1793 linkage code and the new symbol is not, then
1794 we want to use the new symbol. */
1795 section = bfd_und_section_ptr;
1796 value = 0;
1798 else if (((*sym_hash)->root.u.def.section->owner->flags
1799 & DYNAMIC) != 0)
1801 /* The existing symbol is from a shared library.
1802 Replace it. */
1803 (*sym_hash)->root.type = bfd_link_hash_undefined;
1804 (*sym_hash)->root.u.undef.abfd =
1805 (*sym_hash)->root.u.def.section->owner;
1807 else if (abfd->my_archive != NULL)
1809 /* This is a redefinition in an object contained
1810 in an archive. Just ignore it. See the
1811 comment above. */
1812 section = bfd_und_section_ptr;
1813 value = 0;
1815 else if ((*sym_hash)->root.next != NULL
1816 || info->hash->undefs_tail == &(*sym_hash)->root)
1818 /* This symbol has been referenced. In this
1819 case, we just continue and permit the
1820 multiple definition error. See the comment
1821 above about the behaviour of the AIX linker. */
1823 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1825 /* The symbols are both csects of the same
1826 class. There is at least a chance that this
1827 is a semi-legitimate redefinition. */
1828 section = bfd_und_section_ptr;
1829 value = 0;
1830 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1833 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1834 && ((*sym_hash)->root.type == bfd_link_hash_defined
1835 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1836 && (bfd_is_und_section (section)
1837 || bfd_is_com_section (section)))
1839 /* This is a reference to a multiply defined symbol.
1840 Report the error now. See the comment above
1841 about the behaviour of the AIX linker. We could
1842 also do this with warning symbols, but I'm not
1843 sure the XCOFF linker is wholly prepared to
1844 handle them, and that would only be a warning,
1845 not an error. */
1846 if (! ((*info->callbacks->multiple_definition)
1847 (info, (*sym_hash)->root.root.string,
1848 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0,
1849 (*sym_hash)->root.u.def.section->owner,
1850 (*sym_hash)->root.u.def.section,
1851 (*sym_hash)->root.u.def.value)))
1852 goto error_return;
1853 /* Try not to give this error too many times. */
1854 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1858 /* _bfd_generic_link_add_one_symbol may call the linker to
1859 generate an error message, and the linker may try to read
1860 the symbol table to give a good error. Right now, the
1861 line numbers are in an inconsistent state, since they are
1862 counted both in the real sections and in the new csects.
1863 We need to leave the count in the real sections so that
1864 the linker can report the line number of the error
1865 correctly, so temporarily clobber the link to the csects
1866 so that the linker will not try to read the line numbers
1867 a second time from the csects. */
1868 BFD_ASSERT (last_real->next == first_csect);
1869 last_real->next = NULL;
1870 if (! (_bfd_generic_link_add_one_symbol
1871 (info, abfd, name, flags, section, value,
1872 (const char *) NULL, copy, true,
1873 (struct bfd_link_hash_entry **) sym_hash)))
1874 goto error_return;
1875 last_real->next = first_csect;
1877 if (smtyp == XTY_CM)
1879 if ((*sym_hash)->root.type != bfd_link_hash_common
1880 || (*sym_hash)->root.u.c.p->section != csect)
1882 /* We don't need the common csect we just created. */
1883 csect->_raw_size = 0;
1885 else
1887 (*sym_hash)->root.u.c.p->alignment_power
1888 = csect->alignment_power;
1892 if (info->hash->creator == abfd->xvec)
1894 int flag;
1896 if (smtyp == XTY_ER || smtyp == XTY_CM)
1897 flag = XCOFF_REF_REGULAR;
1898 else
1899 flag = XCOFF_DEF_REGULAR;
1900 (*sym_hash)->flags |= flag;
1902 if ((*sym_hash)->smclas == XMC_UA
1903 || flag == XCOFF_DEF_REGULAR)
1904 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1908 *csect_cache = csect;
1910 esym += (sym.n_numaux + 1) * symesz;
1911 sym_hash += sym.n_numaux + 1;
1912 csect_cache += sym.n_numaux + 1;
1915 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1917 /* Make sure that we have seen all the relocs. */
1918 for (o = abfd->sections; o != first_csect; o = o->next)
1920 /* Reset the section size and the line number count, since the
1921 data is now attached to the csects. Don't reset the size of
1922 the .debug section, since we need to read it below in
1923 bfd_xcoff_size_dynamic_sections. */
1924 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1925 o->_raw_size = 0;
1926 o->lineno_count = 0;
1928 if ((o->flags & SEC_RELOC) != 0)
1930 bfd_size_type i;
1931 struct internal_reloc *rel;
1932 asection **rel_csect;
1934 rel = reloc_info[o->target_index].relocs;
1935 rel_csect = reloc_info[o->target_index].csects;
1937 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1940 if (*rel_csect == NULL)
1942 (*_bfd_error_handler)
1943 (_("%s: reloc %s:%d not in csect"),
1944 bfd_archive_filename (abfd), o->name, i);
1945 bfd_set_error (bfd_error_bad_value);
1946 goto error_return;
1949 /* We identify all symbols which are called, so that we
1950 can create glue code for calls to functions imported
1951 from dynamic objects. */
1952 if (info->hash->creator == abfd->xvec
1953 && *rel_csect != bfd_und_section_ptr
1954 && (rel->r_type == R_BR
1955 || rel->r_type == R_RBR)
1956 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1958 struct xcoff_link_hash_entry *h;
1960 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1961 h->flags |= XCOFF_CALLED;
1962 /* If the symbol name starts with a period, it is
1963 the code of a function. If the symbol is
1964 currently undefined, then add an undefined symbol
1965 for the function descriptor. This should do no
1966 harm, because any regular object that defines the
1967 function should also define the function
1968 descriptor. It helps, because it means that we
1969 will identify the function descriptor with a
1970 dynamic object if a dynamic object defines it. */
1971 if (h->root.root.string[0] == '.'
1972 && h->descriptor == NULL)
1974 struct xcoff_link_hash_entry *hds;
1976 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1977 h->root.root.string + 1,
1978 true, false, true);
1979 if (hds == NULL)
1980 goto error_return;
1981 if (hds->root.type == bfd_link_hash_new)
1983 if (! (_bfd_generic_link_add_one_symbol
1984 (info, abfd, hds->root.root.string,
1985 (flagword) 0, bfd_und_section_ptr,
1986 (bfd_vma) 0, (const char *) NULL, false,
1987 true,
1988 (struct bfd_link_hash_entry **) &hds)))
1989 goto error_return;
1991 hds->flags |= XCOFF_DESCRIPTOR;
1992 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1993 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1994 hds->descriptor = h;
1995 h->descriptor = hds;
2000 free (reloc_info[o->target_index].csects);
2001 reloc_info[o->target_index].csects = NULL;
2003 /* Reset SEC_RELOC and the reloc_count, since the reloc
2004 information is now attached to the csects. */
2005 o->flags &=~ SEC_RELOC;
2006 o->reloc_count = 0;
2008 /* If we are not keeping memory, free the reloc information. */
2009 if (! info->keep_memory
2010 && coff_section_data (abfd, o) != NULL
2011 && coff_section_data (abfd, o)->relocs != NULL
2012 && ! coff_section_data (abfd, o)->keep_relocs)
2014 free (coff_section_data (abfd, o)->relocs);
2015 coff_section_data (abfd, o)->relocs = NULL;
2019 /* Free up the line numbers. FIXME: We could cache these
2020 somewhere for the final link, to avoid reading them again. */
2021 if (reloc_info[o->target_index].linenos != NULL)
2023 free (reloc_info[o->target_index].linenos);
2024 reloc_info[o->target_index].linenos = NULL;
2028 free (reloc_info);
2030 obj_coff_keep_syms (abfd) = keep_syms;
2032 return true;
2034 error_return:
2035 if (reloc_info != NULL)
2037 for (o = abfd->sections; o != NULL; o = o->next)
2039 if (reloc_info[o->target_index].csects != NULL)
2040 free (reloc_info[o->target_index].csects);
2041 if (reloc_info[o->target_index].linenos != NULL)
2042 free (reloc_info[o->target_index].linenos);
2044 free (reloc_info);
2046 obj_coff_keep_syms (abfd) = keep_syms;
2047 return false;
2050 #undef N_TMASK
2051 #undef N_BTSHFT
2053 /* This function is used to add symbols from a dynamic object to the
2054 global symbol table. */
2056 static boolean
2057 xcoff_link_add_dynamic_symbols (abfd, info)
2058 bfd *abfd;
2059 struct bfd_link_info *info;
2061 asection *lsec;
2062 bfd_byte *contents;
2063 struct internal_ldhdr ldhdr;
2064 const char *strings;
2065 bfd_byte *elsym, *elsymend;
2066 struct xcoff_import_file *n;
2067 const char *bname;
2068 const char *mname;
2069 const char *s;
2070 unsigned int c;
2071 struct xcoff_import_file **pp;
2073 /* We can only handle a dynamic object if we are generating an XCOFF
2074 output file. */
2075 if (info->hash->creator != abfd->xvec)
2077 (*_bfd_error_handler)
2078 (_("%s: XCOFF shared object when not producing XCOFF output"),
2079 bfd_get_filename (abfd));
2080 bfd_set_error (bfd_error_invalid_operation);
2081 return false;
2084 /* The symbols we use from a dynamic object are not the symbols in
2085 the normal symbol table, but, rather, the symbols in the export
2086 table. If there is a global symbol in a dynamic object which is
2087 not in the export table, the loader will not be able to find it,
2088 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2089 libc.a has symbols in the export table which are not in the
2090 symbol table. */
2092 /* Read in the .loader section. FIXME: We should really use the
2093 o_snloader field in the a.out header, rather than grabbing the
2094 section by name. */
2095 lsec = bfd_get_section_by_name (abfd, ".loader");
2096 if (lsec == NULL)
2098 (*_bfd_error_handler)
2099 (_("%s: dynamic object with no .loader section"),
2100 bfd_get_filename (abfd));
2101 bfd_set_error (bfd_error_no_symbols);
2102 return false;
2106 if (! xcoff_get_section_contents (abfd, lsec))
2107 return false;
2108 contents = coff_section_data (abfd, lsec)->contents;
2110 /* Remove the sections from this object, so that they do not get
2111 included in the link. */
2112 abfd->sections = NULL;
2114 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2116 strings = (char *) contents + ldhdr.l_stoff;
2118 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
2120 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
2122 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
2124 struct internal_ldsym ldsym;
2125 char nambuf[SYMNMLEN + 1];
2126 const char *name;
2127 struct xcoff_link_hash_entry *h;
2129 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2131 /* We are only interested in exported symbols. */
2132 if ((ldsym.l_smtype & L_EXPORT) == 0)
2133 continue;
2135 if (ldsym._l._l_l._l_zeroes == 0)
2136 name = strings + ldsym._l._l_l._l_offset;
2137 else
2139 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2140 nambuf[SYMNMLEN] = '\0';
2141 name = nambuf;
2144 /* Normally we could not call xcoff_link_hash_lookup in an add
2145 symbols routine, since we might not be using an XCOFF hash
2146 table. However, we verified above that we are using an XCOFF
2147 hash table. */
2149 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2150 true, true);
2151 if (h == NULL)
2152 return false;
2154 h->flags |= XCOFF_DEF_DYNAMIC;
2156 /* If the symbol is undefined, and the BFD it was found in is
2157 not a dynamic object, change the BFD to this dynamic object,
2158 so that we can get the correct import file ID. */
2159 if ((h->root.type == bfd_link_hash_undefined
2160 || h->root.type == bfd_link_hash_undefweak)
2161 && (h->root.u.undef.abfd == NULL
2162 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2163 h->root.u.undef.abfd = abfd;
2165 if (h->root.type == bfd_link_hash_new)
2167 h->root.type = bfd_link_hash_undefined;
2168 h->root.u.undef.abfd = abfd;
2169 /* We do not want to add this to the undefined symbol list. */
2172 if (h->smclas == XMC_UA
2173 || h->root.type == bfd_link_hash_undefined
2174 || h->root.type == bfd_link_hash_undefweak)
2175 h->smclas = ldsym.l_smclas;
2177 /* Unless this is an XMC_XO symbol, we don't bother to actually
2178 define it, since we don't have a section to put it in anyhow.
2179 Instead, the relocation routines handle the DEF_DYNAMIC flag
2180 correctly. */
2182 if (h->smclas == XMC_XO
2183 && (h->root.type == bfd_link_hash_undefined
2184 || h->root.type == bfd_link_hash_undefweak))
2186 /* This symbol has an absolute value. */
2187 h->root.type = bfd_link_hash_defined;
2188 h->root.u.def.section = bfd_abs_section_ptr;
2189 h->root.u.def.value = ldsym.l_value;
2192 /* If this symbol defines a function descriptor, then it
2193 implicitly defines the function code as well. */
2194 if (h->smclas == XMC_DS
2195 || (h->smclas == XMC_XO && name[0] != '.'))
2196 h->flags |= XCOFF_DESCRIPTOR;
2197 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2199 struct xcoff_link_hash_entry *hds;
2201 hds = h->descriptor;
2202 if (hds == NULL)
2204 char *dsnm;
2206 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
2207 if (dsnm == NULL)
2208 return false;
2209 dsnm[0] = '.';
2210 strcpy (dsnm + 1, name);
2211 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2212 true, true, true);
2213 free (dsnm);
2214 if (hds == NULL)
2215 return false;
2217 if (hds->root.type == bfd_link_hash_new)
2219 hds->root.type = bfd_link_hash_undefined;
2220 hds->root.u.undef.abfd = abfd;
2221 /* We do not want to add this to the undefined
2222 symbol list. */
2225 hds->descriptor = h;
2226 h->descriptor = hds;
2229 hds->flags |= XCOFF_DEF_DYNAMIC;
2230 if (hds->smclas == XMC_UA)
2231 hds->smclas = XMC_PR;
2233 /* An absolute symbol appears to actually define code, not a
2234 function descriptor. This is how some math functions are
2235 implemented on AIX 4.1. */
2236 if (h->smclas == XMC_XO
2237 && (hds->root.type == bfd_link_hash_undefined
2238 || hds->root.type == bfd_link_hash_undefweak))
2240 hds->smclas = XMC_XO;
2241 hds->root.type = bfd_link_hash_defined;
2242 hds->root.u.def.section = bfd_abs_section_ptr;
2243 hds->root.u.def.value = ldsym.l_value;
2248 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2250 free (coff_section_data (abfd, lsec)->contents);
2251 coff_section_data (abfd, lsec)->contents = NULL;
2254 /* Record this file in the import files. */
2256 n = ((struct xcoff_import_file *)
2257 bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file)));
2258 if (n == NULL)
2259 return false;
2260 n->next = NULL;
2262 /* For some reason, the path entry in the import file list for a
2263 shared object appears to always be empty. The file name is the
2264 base name. */
2265 n->path = "";
2266 if (abfd->my_archive == NULL)
2268 bname = bfd_get_filename (abfd);
2269 mname = "";
2271 else
2273 bname = bfd_get_filename (abfd->my_archive);
2274 mname = bfd_get_filename (abfd);
2276 s = strrchr (bname, '/');
2277 if (s != NULL)
2278 bname = s + 1;
2279 n->file = bname;
2280 n->member = mname;
2282 /* We start c at 1 because the first import file number is reserved
2283 for LIBPATH. */
2284 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2285 *pp != NULL;
2286 pp = &(*pp)->next, ++c)
2288 *pp = n;
2290 xcoff_data (abfd)->import_file_id = c;
2292 return true;
2295 /* Routines that are called after all the input files have been
2296 handled, but before the sections are laid out in memory. */
2298 /* Mark a symbol as not being garbage, including the section in which
2299 it is defined. */
2301 static INLINE boolean
2302 xcoff_mark_symbol (info, h)
2303 struct bfd_link_info *info;
2304 struct xcoff_link_hash_entry *h;
2307 if ((h->flags & XCOFF_MARK) != 0)
2308 return true;
2310 h->flags |= XCOFF_MARK;
2311 if (h->root.type == bfd_link_hash_defined
2312 || h->root.type == bfd_link_hash_defweak)
2314 asection *hsec;
2316 hsec = h->root.u.def.section;
2317 if (! bfd_is_abs_section (hsec)
2318 && (hsec->flags & SEC_MARK) == 0)
2320 if (! xcoff_mark (info, hsec))
2321 return false;
2325 if (h->toc_section != NULL
2326 && (h->toc_section->flags & SEC_MARK) == 0)
2328 if (! xcoff_mark (info, h->toc_section))
2329 return false;
2332 return true;
2335 /* The mark phase of garbage collection. For a given section, mark
2336 it, and all the sections which define symbols to which it refers.
2337 Because this function needs to look at the relocs, we also count
2338 the number of relocs which need to be copied into the .loader
2339 section. */
2341 static boolean
2342 xcoff_mark (info, sec)
2343 struct bfd_link_info *info;
2344 asection *sec;
2346 if (bfd_is_abs_section (sec)
2347 || (sec->flags & SEC_MARK) != 0)
2348 return true;
2350 sec->flags |= SEC_MARK;
2352 if (sec->owner->xvec == info->hash->creator
2353 && coff_section_data (sec->owner, sec) != NULL
2354 && xcoff_section_data (sec->owner, sec) != NULL)
2356 register struct xcoff_link_hash_entry **hp, **hpend;
2357 struct internal_reloc *rel, *relend;
2359 /* Mark all the symbols in this section. */
2361 hp = (obj_xcoff_sym_hashes (sec->owner)
2362 + xcoff_section_data (sec->owner, sec)->first_symndx);
2363 hpend = (obj_xcoff_sym_hashes (sec->owner)
2364 + xcoff_section_data (sec->owner, sec)->last_symndx);
2365 for (; hp < hpend; hp++)
2367 register struct xcoff_link_hash_entry *h;
2369 h = *hp;
2370 if (h != NULL
2371 && (h->flags & XCOFF_MARK) == 0)
2373 if (! xcoff_mark_symbol (info, h))
2374 return false;
2378 /* Look through the section relocs. */
2380 if ((sec->flags & SEC_RELOC) != 0
2381 && sec->reloc_count > 0)
2383 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2384 (bfd_byte *) NULL, false,
2385 (struct internal_reloc *) NULL);
2386 if (rel == NULL)
2387 return false;
2388 relend = rel + sec->reloc_count;
2389 for (; rel < relend; rel++)
2391 asection *rsec;
2392 struct xcoff_link_hash_entry *h;
2394 if ((unsigned int) rel->r_symndx
2395 > obj_raw_syment_count (sec->owner))
2396 continue;
2398 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2399 if (h != NULL
2400 && (h->flags & XCOFF_MARK) == 0)
2402 if (! xcoff_mark_symbol (info, h))
2403 return false;
2406 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2407 if (rsec != NULL
2408 && (rsec->flags & SEC_MARK) == 0)
2410 if (! xcoff_mark (info, rsec))
2411 return false;
2414 /* See if this reloc needs to be copied into the .loader
2415 section. */
2416 switch (rel->r_type)
2418 default:
2419 if (h == NULL
2420 || h->root.type == bfd_link_hash_defined
2421 || h->root.type == bfd_link_hash_defweak
2422 || h->root.type == bfd_link_hash_common
2423 || ((h->flags & XCOFF_CALLED) != 0
2424 && (h->root.type == bfd_link_hash_undefined
2425 || h->root.type == bfd_link_hash_undefweak)
2426 && h->root.root.string[0] == '.'
2427 && h->descriptor != NULL
2428 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2429 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2430 && (h->descriptor->flags
2431 & XCOFF_DEF_REGULAR) == 0))))
2432 break;
2433 /* Fall through. */
2434 case R_POS:
2435 case R_NEG:
2436 case R_RL:
2437 case R_RLA:
2438 ++xcoff_hash_table (info)->ldrel_count;
2439 if (h != NULL)
2440 h->flags |= XCOFF_LDREL;
2441 break;
2442 case R_TOC:
2443 case R_GL:
2444 case R_TCL:
2445 case R_TRL:
2446 case R_TRLA:
2447 /* We should never need a .loader reloc for a TOC
2448 relative reloc. */
2449 break;
2453 if (! info->keep_memory
2454 && coff_section_data (sec->owner, sec) != NULL
2455 && coff_section_data (sec->owner, sec)->relocs != NULL
2456 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2458 free (coff_section_data (sec->owner, sec)->relocs);
2459 coff_section_data (sec->owner, sec)->relocs = NULL;
2464 return true;
2467 /* The sweep phase of garbage collection. Remove all garbage
2468 sections. */
2470 static void
2471 xcoff_sweep (info)
2472 struct bfd_link_info *info;
2474 bfd *sub;
2476 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2478 asection *o;
2480 for (o = sub->sections; o != NULL; o = o->next)
2482 if ((o->flags & SEC_MARK) == 0)
2484 /* Keep all sections from non-XCOFF input files. Keep
2485 special sections. Keep .debug sections for the
2486 moment. */
2487 if (sub->xvec != info->hash->creator
2488 || o == xcoff_hash_table (info)->debug_section
2489 || o == xcoff_hash_table (info)->loader_section
2490 || o == xcoff_hash_table (info)->linkage_section
2491 || o == xcoff_hash_table (info)->toc_section
2492 || o == xcoff_hash_table (info)->descriptor_section
2493 || strcmp (o->name, ".debug") == 0)
2494 o->flags |= SEC_MARK;
2495 else
2497 o->_raw_size = 0;
2498 o->reloc_count = 0;
2499 o->lineno_count = 0;
2506 /* Record the number of elements in a set. This is used to output the
2507 correct csect length. */
2509 boolean
2510 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2511 bfd *output_bfd;
2512 struct bfd_link_info *info;
2513 struct bfd_link_hash_entry *harg;
2514 bfd_size_type size;
2516 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2517 struct xcoff_link_size_list *n;
2518 bfd_size_type amt;
2520 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2521 return true;
2523 /* This will hardly ever be called. I don't want to burn four bytes
2524 per global symbol, so instead the size is kept on a linked list
2525 attached to the hash table. */
2527 amt = sizeof (struct xcoff_link_size_list);
2528 n = (struct xcoff_link_size_list *) bfd_alloc (output_bfd, amt);
2529 if (n == NULL)
2530 return false;
2531 n->next = xcoff_hash_table (info)->size_list;
2532 n->h = h;
2533 n->size = size;
2534 xcoff_hash_table (info)->size_list = n;
2536 h->flags |= XCOFF_HAS_SIZE;
2538 return true;
2541 /* Import a symbol. */
2543 boolean
2544 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2545 impmember, syscall_flag)
2546 bfd *output_bfd;
2547 struct bfd_link_info *info;
2548 struct bfd_link_hash_entry *harg;
2549 bfd_vma val;
2550 const char *imppath;
2551 const char *impfile;
2552 const char *impmember;
2553 unsigned int syscall_flag;
2555 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2557 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2558 return true;
2560 /* A symbol name which starts with a period is the code for a
2561 function. If the symbol is undefined, then add an undefined
2562 symbol for the function descriptor, and import that instead. */
2563 if (h->root.root.string[0] == '.'
2564 && h->root.type == bfd_link_hash_undefined
2565 && val == (bfd_vma) -1)
2567 struct xcoff_link_hash_entry *hds;
2569 hds = h->descriptor;
2570 if (hds == NULL)
2572 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2573 h->root.root.string + 1,
2574 true, false, true);
2575 if (hds == NULL)
2576 return false;
2577 if (hds->root.type == bfd_link_hash_new)
2579 hds->root.type = bfd_link_hash_undefined;
2580 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2582 hds->flags |= XCOFF_DESCRIPTOR;
2583 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2584 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2585 hds->descriptor = h;
2586 h->descriptor = hds;
2589 /* Now, if the descriptor is undefined, import the descriptor
2590 rather than the symbol we were told to import. FIXME: Is
2591 this correct in all cases? */
2592 if (hds->root.type == bfd_link_hash_undefined)
2593 h = hds;
2596 h->flags |= (XCOFF_IMPORT | syscall_flag);
2598 if (val != (bfd_vma) -1)
2600 if (h->root.type == bfd_link_hash_defined
2601 && (! bfd_is_abs_section (h->root.u.def.section)
2602 || h->root.u.def.value != val))
2604 if (! ((*info->callbacks->multiple_definition)
2605 (info, h->root.root.string, h->root.u.def.section->owner,
2606 h->root.u.def.section, h->root.u.def.value,
2607 output_bfd, bfd_abs_section_ptr, val)))
2608 return false;
2611 h->root.type = bfd_link_hash_defined;
2612 h->root.u.def.section = bfd_abs_section_ptr;
2613 h->root.u.def.value = val;
2616 /* We overload the ldindx field to hold the l_ifile value for this
2617 symbol. */
2618 BFD_ASSERT (h->ldsym == NULL);
2619 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2620 if (imppath == NULL)
2621 h->ldindx = -1;
2622 else
2624 unsigned int c;
2625 struct xcoff_import_file **pp;
2627 /* We start c at 1 because the first entry in the import list is
2628 reserved for the library search path. */
2629 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2630 *pp != NULL;
2631 pp = &(*pp)->next, ++c)
2633 if (strcmp ((*pp)->path, imppath) == 0
2634 && strcmp ((*pp)->file, impfile) == 0
2635 && strcmp ((*pp)->member, impmember) == 0)
2636 break;
2639 if (*pp == NULL)
2641 struct xcoff_import_file *n;
2642 bfd_size_type amt = sizeof (struct xcoff_import_file);
2644 n = (struct xcoff_import_file *) bfd_alloc (output_bfd, amt);
2645 if (n == NULL)
2646 return false;
2647 n->next = NULL;
2648 n->path = imppath;
2649 n->file = impfile;
2650 n->member = impmember;
2651 *pp = n;
2654 h->ldindx = c;
2657 return true;
2660 /* Export a symbol. */
2662 boolean
2663 bfd_xcoff_export_symbol (output_bfd, info, harg)
2664 bfd *output_bfd;
2665 struct bfd_link_info *info;
2666 struct bfd_link_hash_entry *harg;
2668 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2670 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2671 return true;
2673 h->flags |= XCOFF_EXPORT;
2675 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2676 I'm just going to ignore it until somebody explains it. */
2678 /* See if this is a function descriptor. It may be one even though
2679 it is not so marked. */
2680 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2681 && h->root.root.string[0] != '.')
2683 char *fnname;
2684 struct xcoff_link_hash_entry *hfn;
2685 bfd_size_type amt = strlen (h->root.root.string) + 2;
2687 fnname = (char *) bfd_malloc (amt);
2688 if (fnname == NULL)
2689 return false;
2690 fnname[0] = '.';
2691 strcpy (fnname + 1, h->root.root.string);
2692 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2693 fnname, false, false, true);
2694 free (fnname);
2695 if (hfn != NULL
2696 && hfn->smclas == XMC_PR
2697 && (hfn->root.type == bfd_link_hash_defined
2698 || hfn->root.type == bfd_link_hash_defweak))
2700 h->flags |= XCOFF_DESCRIPTOR;
2701 h->descriptor = hfn;
2702 hfn->descriptor = h;
2706 /* Make sure we don't garbage collect this symbol. */
2707 if (! xcoff_mark_symbol (info, h))
2708 return false;
2710 /* If this is a function descriptor, make sure we don't garbage
2711 collect the associated function code. We normally don't have to
2712 worry about this, because the descriptor will be attached to a
2713 section with relocs, but if we are creating the descriptor
2714 ourselves those relocs will not be visible to the mark code. */
2715 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2717 if (! xcoff_mark_symbol (info, h->descriptor))
2718 return false;
2721 return true;
2724 /* Count a reloc against a symbol. This is called for relocs
2725 generated by the linker script, typically for global constructors
2726 and destructors. */
2728 boolean
2729 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2730 bfd *output_bfd;
2731 struct bfd_link_info *info;
2732 const char *name;
2734 struct xcoff_link_hash_entry *h;
2736 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2737 return true;
2739 h = ((struct xcoff_link_hash_entry *)
2740 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
2741 false));
2742 if (h == NULL)
2744 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2745 bfd_set_error (bfd_error_no_symbols);
2746 return false;
2749 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2750 ++xcoff_hash_table (info)->ldrel_count;
2752 /* Mark the symbol to avoid garbage collection. */
2753 if (! xcoff_mark_symbol (info, h))
2754 return false;
2756 return true;
2759 /* This function is called for each symbol to which the linker script
2760 assigns a value. */
2762 boolean
2763 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2764 bfd *output_bfd;
2765 struct bfd_link_info *info;
2766 const char *name;
2768 struct xcoff_link_hash_entry *h;
2770 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2771 return true;
2773 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2774 false);
2775 if (h == NULL)
2776 return false;
2778 h->flags |= XCOFF_DEF_REGULAR;
2780 return true;
2783 /* Build the .loader section. This is called by the XCOFF linker
2784 emulation before_allocation routine. We must set the size of the
2785 .loader section before the linker lays out the output file.
2786 LIBPATH is the library path to search for shared objects; this is
2787 normally built from the -L arguments passed to the linker. ENTRY
2788 is the name of the entry point symbol (the -e linker option).
2789 FILE_ALIGN is the alignment to use for sections within the file
2790 (the -H linker option). MAXSTACK is the maximum stack size (the
2791 -bmaxstack linker option). MAXDATA is the maximum data size (the
2792 -bmaxdata linker option). GC is whether to do garbage collection
2793 (the -bgc linker option). MODTYPE is the module type (the
2794 -bmodtype linker option). TEXTRO is whether the text section must
2795 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2796 whether all defined symbols should be exported (the -unix linker
2797 option). SPECIAL_SECTIONS is set by this routine to csects with
2798 magic names like _end. */
2800 boolean
2801 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2802 file_align, maxstack, maxdata, gc,
2803 modtype, textro, export_defineds,
2804 special_sections, rtld)
2805 bfd *output_bfd;
2806 struct bfd_link_info *info;
2807 const char *libpath;
2808 const char *entry;
2809 unsigned long file_align;
2810 unsigned long maxstack;
2811 unsigned long maxdata;
2812 boolean gc;
2813 int modtype;
2814 boolean textro;
2815 boolean export_defineds;
2816 asection **special_sections;
2817 boolean rtld;
2819 struct xcoff_link_hash_entry *hentry;
2820 asection *lsec;
2821 struct xcoff_loader_info ldinfo;
2822 int i;
2823 size_t impsize, impcount;
2824 struct xcoff_import_file *fl;
2825 struct internal_ldhdr *ldhdr;
2826 bfd_size_type stoff;
2827 register char *out;
2828 asection *sec;
2829 bfd *sub;
2830 struct bfd_strtab_hash *debug_strtab;
2831 bfd_byte *debug_contents = NULL;
2832 bfd_size_type amt;
2834 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2836 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2837 special_sections[i] = NULL;
2838 return true;
2841 ldinfo.failed = false;
2842 ldinfo.output_bfd = output_bfd;
2843 ldinfo.info = info;
2844 ldinfo.export_defineds = export_defineds;
2845 ldinfo.ldsym_count = 0;
2846 ldinfo.string_size = 0;
2847 ldinfo.strings = NULL;
2848 ldinfo.string_alc = 0;
2850 xcoff_data (output_bfd)->maxstack = maxstack;
2851 xcoff_data (output_bfd)->maxdata = maxdata;
2852 xcoff_data (output_bfd)->modtype = modtype;
2854 xcoff_hash_table (info)->file_align = file_align;
2855 xcoff_hash_table (info)->textro = textro;
2857 hentry = NULL;
2858 if (entry != NULL)
2860 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2861 false, false, true);
2862 if (hentry != NULL)
2863 hentry->flags |= XCOFF_ENTRY;
2866 /* __rtinit */
2867 if (info->init_function || info->fini_function || rtld == true)
2869 struct xcoff_link_hash_entry *hsym;
2870 struct internal_ldsym *ldsym;
2872 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
2873 "__rtinit", false, false, true);
2874 if (hsym == NULL)
2876 (*_bfd_error_handler)
2877 (_("error: undefined symbol __rtinit"));
2878 return false;
2881 xcoff_mark_symbol (info, hsym);
2882 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
2884 /* __rtinit initalized */
2885 amt = sizeof (struct internal_ldsym);
2886 ldsym = (struct internal_ldsym *) bfd_malloc (amt);
2888 ldsym->l_value = 0; /* will be filled in later */
2889 ldsym->l_scnum = 2; /* data section */
2890 ldsym->l_smtype = XTY_SD; /* csect section definition */
2891 ldsym->l_smclas = 5; /* .rw */
2892 ldsym->l_ifile = 0; /* special system loader symbol */
2893 ldsym->l_parm = 0; /* NA */
2895 /* Force __rtinit to be the first symbol in the loader symbol table
2896 See xcoff_build_ldsyms
2898 The first 3 symbol table indices are reserved to indicate the data,
2899 text and bss sections. */
2900 BFD_ASSERT (0 == ldinfo.ldsym_count);
2902 hsym->ldindx = 3;
2903 ldinfo.ldsym_count = 1;
2904 hsym->ldsym = ldsym;
2906 if (false == bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
2907 hsym->ldsym,
2908 hsym->root.root.string))
2909 return false;
2911 /* This symbol is written out by xcoff_write_global_symbol
2912 Set stuff up so xcoff_write_global_symbol logic works. */
2913 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
2914 hsym->root.type = bfd_link_hash_defined;
2915 hsym->root.u.def.value = 0;
2918 /* Garbage collect unused sections. */
2919 if (info->relocateable
2920 || ! gc
2921 || hentry == NULL
2922 || (hentry->root.type != bfd_link_hash_defined
2923 && hentry->root.type != bfd_link_hash_defweak))
2925 gc = false;
2926 xcoff_hash_table (info)->gc = false;
2928 /* We still need to call xcoff_mark, in order to set ldrel_count
2929 correctly. */
2930 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2932 asection *o;
2934 for (o = sub->sections; o != NULL; o = o->next)
2936 if ((o->flags & SEC_MARK) == 0)
2938 if (! xcoff_mark (info, o))
2939 goto error_return;
2944 else
2946 if (! xcoff_mark (info, hentry->root.u.def.section))
2947 goto error_return;
2948 xcoff_sweep (info);
2949 xcoff_hash_table (info)->gc = true;
2952 /* Return special sections to the caller. */
2953 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2955 sec = xcoff_hash_table (info)->special_sections[i];
2957 if (sec != NULL
2958 && gc
2959 && (sec->flags & SEC_MARK) == 0)
2961 sec = NULL;
2963 special_sections[i] = sec;
2966 if (info->input_bfds == NULL)
2968 /* I'm not sure what to do in this bizarre case. */
2969 return true;
2972 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2973 (PTR) &ldinfo);
2974 if (ldinfo.failed)
2975 goto error_return;
2977 /* Work out the size of the import file names. Each import file ID
2978 consists of three null terminated strings: the path, the file
2979 name, and the archive member name. The first entry in the list
2980 of names is the path to use to find objects, which the linker has
2981 passed in as the libpath argument. For some reason, the path
2982 entry in the other import file names appears to always be empty. */
2983 impsize = strlen (libpath) + 3;
2984 impcount = 1;
2985 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2987 ++impcount;
2988 impsize += (strlen (fl->path)
2989 + strlen (fl->file)
2990 + strlen (fl->member)
2991 + 3);
2994 /* Set up the .loader section header. */
2995 ldhdr = &xcoff_hash_table (info)->ldhdr;
2996 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
2997 ldhdr->l_nsyms = ldinfo.ldsym_count;
2998 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2999 ldhdr->l_istlen = impsize;
3000 ldhdr->l_nimpid = impcount;
3001 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3002 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3003 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3004 ldhdr->l_stlen = ldinfo.string_size;
3005 stoff = ldhdr->l_impoff + impsize;
3006 if (ldinfo.string_size == 0)
3007 ldhdr->l_stoff = 0;
3008 else
3009 ldhdr->l_stoff = stoff;
3011 /* 64 bit elements to ldhdr
3012 The swap out routine for 32 bit will ignore them.
3013 Nothing fancy, symbols come after the header and relocs come
3014 after symbols. */
3015 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3016 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3017 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3019 /* We now know the final size of the .loader section. Allocate
3020 space for it. */
3021 lsec = xcoff_hash_table (info)->loader_section;
3022 lsec->_raw_size = stoff + ldhdr->l_stlen;
3023 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3024 if (lsec->contents == NULL)
3025 goto error_return;
3027 /* Set up the header. */
3028 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3030 /* Set up the import file names. */
3031 out = (char *) lsec->contents + ldhdr->l_impoff;
3032 strcpy (out, libpath);
3033 out += strlen (libpath) + 1;
3034 *out++ = '\0';
3035 *out++ = '\0';
3036 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3038 register const char *s;
3040 s = fl->path;
3041 while ((*out++ = *s++) != '\0')
3043 s = fl->file;
3044 while ((*out++ = *s++) != '\0')
3046 s = fl->member;
3047 while ((*out++ = *s++) != '\0')
3051 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3053 /* Set up the symbol string table. */
3054 if (ldinfo.string_size > 0)
3056 memcpy (out, ldinfo.strings, ldinfo.string_size);
3057 free (ldinfo.strings);
3058 ldinfo.strings = NULL;
3061 /* We can't set up the symbol table or the relocs yet, because we
3062 don't yet know the final position of the various sections. The
3063 .loader symbols are written out when the corresponding normal
3064 symbols are written out in xcoff_link_input_bfd or
3065 xcoff_write_global_symbol. The .loader relocs are written out
3066 when the corresponding normal relocs are handled in
3067 xcoff_link_input_bfd.
3070 /* Allocate space for the magic sections. */
3071 sec = xcoff_hash_table (info)->linkage_section;
3072 if (sec->_raw_size > 0)
3074 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3075 if (sec->contents == NULL)
3076 goto error_return;
3078 sec = xcoff_hash_table (info)->toc_section;
3079 if (sec->_raw_size > 0)
3081 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3082 if (sec->contents == NULL)
3083 goto error_return;
3085 sec = xcoff_hash_table (info)->descriptor_section;
3086 if (sec->_raw_size > 0)
3088 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3089 if (sec->contents == NULL)
3090 goto error_return;
3093 /* Now that we've done garbage collection, figure out the contents
3094 of the .debug section. */
3095 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3097 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3099 asection *subdeb;
3100 bfd_size_type symcount;
3101 unsigned long *debug_index;
3102 asection **csectpp;
3103 bfd_byte *esym, *esymend;
3104 bfd_size_type symesz;
3106 if (sub->xvec != info->hash->creator)
3107 continue;
3108 subdeb = bfd_get_section_by_name (sub, ".debug");
3109 if (subdeb == NULL || subdeb->_raw_size == 0)
3110 continue;
3112 if (info->strip == strip_all
3113 || info->strip == strip_debugger
3114 || info->discard == discard_all)
3116 subdeb->_raw_size = 0;
3117 continue;
3120 if (! _bfd_coff_get_external_symbols (sub))
3121 goto error_return;
3123 symcount = obj_raw_syment_count (sub);
3124 debug_index = ((unsigned long *)
3125 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3126 if (debug_index == NULL)
3127 goto error_return;
3128 xcoff_data (sub)->debug_indices = debug_index;
3130 /* Grab the contents of the .debug section. We use malloc and
3131 copy the names into the debug stringtab, rather than
3132 bfd_alloc, because I expect that, when linking many files
3133 together, many of the strings will be the same. Storing the
3134 strings in the hash table should save space in this case. */
3135 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3136 if (debug_contents == NULL)
3137 goto error_return;
3138 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3139 (file_ptr) 0, subdeb->_raw_size))
3140 goto error_return;
3142 csectpp = xcoff_data (sub)->csects;
3144 /* Dynamic object do not have csectpp's. */
3145 if (NULL != csectpp)
3147 symesz = bfd_coff_symesz (sub);
3148 esym = (bfd_byte *) obj_coff_external_syms (sub);
3149 esymend = esym + symcount * symesz;
3151 while (esym < esymend)
3153 struct internal_syment sym;
3155 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3157 *debug_index = (unsigned long) -1;
3159 if (sym._n._n_n._n_zeroes == 0
3160 && *csectpp != NULL
3161 && (! gc
3162 || ((*csectpp)->flags & SEC_MARK) != 0
3163 || *csectpp == bfd_abs_section_ptr)
3164 && bfd_coff_symname_in_debug (sub, &sym))
3166 char *name;
3167 bfd_size_type indx;
3169 name = (char *) debug_contents + sym._n._n_n._n_offset;
3170 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3171 if (indx == (bfd_size_type) -1)
3172 goto error_return;
3173 *debug_index = indx;
3176 esym += (sym.n_numaux + 1) * symesz;
3177 csectpp += sym.n_numaux + 1;
3178 debug_index += sym.n_numaux + 1;
3182 free (debug_contents);
3183 debug_contents = NULL;
3185 /* Clear the size of subdeb, so that it is not included directly
3186 in the output file. */
3187 subdeb->_raw_size = 0;
3189 if (! info->keep_memory)
3191 if (! _bfd_coff_free_symbols (sub))
3192 goto error_return;
3196 if (info->strip != strip_all)
3197 xcoff_hash_table (info)->debug_section->_raw_size =
3198 _bfd_stringtab_size (debug_strtab);
3200 return true;
3202 error_return:
3203 if (ldinfo.strings != NULL)
3204 free (ldinfo.strings);
3205 if (debug_contents != NULL)
3206 free (debug_contents);
3207 return false;
3210 boolean
3211 bfd_xcoff_link_generate_rtinit (abfd, init, fini, rtld)
3212 bfd *abfd;
3213 const char *init;
3214 const char *fini;
3215 boolean rtld;
3217 struct bfd_in_memory *bim;
3219 bim = ((struct bfd_in_memory *)
3220 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
3221 if (bim == NULL)
3222 return false;
3224 bim->size = 0;
3225 bim->buffer = 0;
3227 abfd->link_next = 0;
3228 abfd->format = bfd_object;
3229 abfd->iostream = (PTR) bim;
3230 abfd->flags = BFD_IN_MEMORY;
3231 abfd->direction = write_direction;
3232 abfd->where = 0;
3234 if (false == bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3235 return false;
3237 /* need to reset to unknown or it will not be read back in correctly */
3238 abfd->format = bfd_unknown;
3239 abfd->direction = read_direction;
3240 abfd->where = 0;
3242 return true;
3246 /* Add a symbol to the .loader symbols, if necessary. */
3248 static boolean
3249 xcoff_build_ldsyms (h, p)
3250 struct xcoff_link_hash_entry *h;
3251 PTR p;
3253 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3254 bfd_size_type amt;
3256 /* __rtinit, this symbol has special handling. */
3257 if (h->flags & XCOFF_RTINIT)
3258 return true;
3260 /* If this is a final link, and the symbol was defined as a common
3261 symbol in a regular object file, and there was no definition in
3262 any dynamic object, then the linker will have allocated space for
3263 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3264 will not have been set. */
3265 if (h->root.type == bfd_link_hash_defined
3266 && (h->flags & XCOFF_DEF_REGULAR) == 0
3267 && (h->flags & XCOFF_REF_REGULAR) != 0
3268 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3269 && (bfd_is_abs_section (h->root.u.def.section)
3270 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3271 h->flags |= XCOFF_DEF_REGULAR;
3273 /* If all defined symbols should be exported, mark them now. We
3274 don't want to export the actual functions, just the function
3275 descriptors. */
3276 if (ldinfo->export_defineds
3277 && (h->flags & XCOFF_DEF_REGULAR) != 0
3278 && h->root.root.string[0] != '.')
3280 boolean export;
3282 /* We don't export a symbol which is being defined by an object
3283 included from an archive which contains a shared object. The
3284 rationale is that if an archive contains both an unshared and
3285 a shared object, then there must be some reason that the
3286 unshared object is unshared, and we don't want to start
3287 providing a shared version of it. In particular, this solves
3288 a bug involving the _savefNN set of functions. gcc will call
3289 those functions without providing a slot to restore the TOC,
3290 so it is essential that these functions be linked in directly
3291 and not from a shared object, which means that a shared
3292 object which also happens to link them in must not export
3293 them. This is confusing, but I haven't been able to think of
3294 a different approach. Note that the symbols can, of course,
3295 be exported explicitly. */
3296 export = true;
3297 if ((h->root.type == bfd_link_hash_defined
3298 || h->root.type == bfd_link_hash_defweak)
3299 && h->root.u.def.section->owner != NULL
3300 && h->root.u.def.section->owner->my_archive != NULL)
3302 bfd *arbfd, *member;
3304 arbfd = h->root.u.def.section->owner->my_archive;
3305 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3306 while (member != NULL)
3308 if ((member->flags & DYNAMIC) != 0)
3310 export = false;
3311 break;
3313 member = bfd_openr_next_archived_file (arbfd, member);
3317 if (export)
3318 h->flags |= XCOFF_EXPORT;
3321 /* We don't want to garbage collect symbols which are not defined in
3322 XCOFF files. This is a convenient place to mark them. */
3323 if (xcoff_hash_table (ldinfo->info)->gc
3324 && (h->flags & XCOFF_MARK) == 0
3325 && (h->root.type == bfd_link_hash_defined
3326 || h->root.type == bfd_link_hash_defweak)
3327 && (h->root.u.def.section->owner == NULL
3328 || (h->root.u.def.section->owner->xvec
3329 != ldinfo->info->hash->creator)))
3330 h->flags |= XCOFF_MARK;
3332 /* If this symbol is called and defined in a dynamic object, or it
3333 is imported, then we need to set up global linkage code for it.
3334 (Unless we did garbage collection and we didn't need this
3335 symbol.) */
3336 if ((h->flags & XCOFF_CALLED) != 0
3337 && (h->root.type == bfd_link_hash_undefined
3338 || h->root.type == bfd_link_hash_undefweak)
3339 && h->root.root.string[0] == '.'
3340 && h->descriptor != NULL
3341 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3342 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3343 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3344 && (! xcoff_hash_table (ldinfo->info)->gc
3345 || (h->flags & XCOFF_MARK) != 0))
3347 asection *sec;
3348 struct xcoff_link_hash_entry *hds;
3350 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3351 h->root.type = bfd_link_hash_defined;
3352 h->root.u.def.section = sec;
3353 h->root.u.def.value = sec->_raw_size;
3354 h->smclas = XMC_GL;
3355 h->flags |= XCOFF_DEF_REGULAR;
3356 sec->_raw_size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
3358 /* The global linkage code requires a TOC entry for the
3359 descriptor. */
3360 hds = h->descriptor;
3361 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3362 || hds->root.type == bfd_link_hash_undefweak)
3363 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3364 hds->flags |= XCOFF_MARK;
3365 if (hds->toc_section == NULL)
3367 int byte_size;
3369 /* 32 vs 64
3370 xcoff32 uses 4 bytes in the toc.
3371 xcoff64 uses 8 bytes in the toc. */
3372 if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
3373 byte_size = 8;
3374 else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
3375 byte_size = 4;
3376 else
3377 return false;
3379 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3380 hds->u.toc_offset = hds->toc_section->_raw_size;
3381 hds->toc_section->_raw_size += byte_size;
3382 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3383 ++hds->toc_section->reloc_count;
3384 hds->indx = -2;
3385 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3387 /* We need to call xcoff_build_ldsyms recursively here,
3388 because we may already have passed hds on the traversal. */
3389 xcoff_build_ldsyms (hds, p);
3393 /* If this symbol is exported, but not defined, we need to try to
3394 define it. */
3395 if ((h->flags & XCOFF_EXPORT) != 0
3396 && (h->flags & XCOFF_IMPORT) == 0
3397 && (h->flags & XCOFF_DEF_REGULAR) == 0
3398 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3399 && (h->root.type == bfd_link_hash_undefined
3400 || h->root.type == bfd_link_hash_undefweak))
3402 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3403 && (h->descriptor->root.type == bfd_link_hash_defined
3404 || h->descriptor->root.type == bfd_link_hash_defweak))
3406 asection *sec;
3408 /* This is an undefined function descriptor associated with
3409 a defined entry point. We can build up a function
3410 descriptor ourselves. Believe it or not, the AIX linker
3411 actually does this, and there are cases where we need to
3412 do it as well. */
3413 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3414 h->root.type = bfd_link_hash_defined;
3415 h->root.u.def.section = sec;
3416 h->root.u.def.value = sec->_raw_size;
3417 h->smclas = XMC_DS;
3418 h->flags |= XCOFF_DEF_REGULAR;
3420 /* The size of the function descriptor depends if this is an
3421 xcoff32 (12) or xcoff64 (24). */
3422 sec->_raw_size +=
3423 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
3425 /* A function descriptor uses two relocs: one for the
3426 associated code, and one for the TOC address. */
3427 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3428 sec->reloc_count += 2;
3430 /* We handle writing out the contents of the descriptor in
3431 xcoff_write_global_symbol. */
3433 else
3435 (*_bfd_error_handler)
3436 (_("warning: attempt to export undefined symbol `%s'"),
3437 h->root.root.string);
3438 h->ldsym = NULL;
3439 return true;
3443 /* If this is still a common symbol, and it wasn't garbage
3444 collected, we need to actually allocate space for it in the .bss
3445 section. */
3446 if (h->root.type == bfd_link_hash_common
3447 && (! xcoff_hash_table (ldinfo->info)->gc
3448 || (h->flags & XCOFF_MARK) != 0)
3449 && h->root.u.c.p->section->_raw_size == 0)
3451 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3452 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3455 /* We need to add a symbol to the .loader section if it is mentioned
3456 in a reloc which we are copying to the .loader section and it was
3457 not defined or common, or if it is the entry point, or if it is
3458 being exported. */
3460 if (((h->flags & XCOFF_LDREL) == 0
3461 || h->root.type == bfd_link_hash_defined
3462 || h->root.type == bfd_link_hash_defweak
3463 || h->root.type == bfd_link_hash_common)
3464 && (h->flags & XCOFF_ENTRY) == 0
3465 && (h->flags & XCOFF_EXPORT) == 0)
3467 h->ldsym = NULL;
3468 return true;
3471 /* We don't need to add this symbol if we did garbage collection and
3472 we did not mark this symbol. */
3473 if (xcoff_hash_table (ldinfo->info)->gc
3474 && (h->flags & XCOFF_MARK) == 0)
3476 h->ldsym = NULL;
3477 return true;
3480 /* We may have already processed this symbol due to the recursive
3481 call above. */
3482 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3483 return true;
3485 /* We need to add this symbol to the .loader symbols. */
3487 BFD_ASSERT (h->ldsym == NULL);
3488 amt = sizeof (struct internal_ldsym);
3489 h->ldsym = (struct internal_ldsym *) bfd_zalloc (ldinfo->output_bfd, amt);
3490 if (h->ldsym == NULL)
3492 ldinfo->failed = true;
3493 return false;
3496 if ((h->flags & XCOFF_IMPORT) != 0)
3497 h->ldsym->l_ifile = h->ldindx;
3499 /* The first 3 symbol table indices are reserved to indicate the
3500 data, text and bss sections. */
3501 h->ldindx = ldinfo->ldsym_count + 3;
3503 ++ldinfo->ldsym_count;
3505 if (false == bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3506 h->ldsym,
3507 h->root.root.string))
3509 return false;
3512 h->flags |= XCOFF_BUILT_LDSYM;
3514 return true;
3517 /* Do the final link step. */
3519 boolean
3520 _bfd_xcoff_bfd_final_link (abfd, info)
3521 bfd *abfd;
3522 struct bfd_link_info *info;
3524 bfd_size_type symesz;
3525 struct xcoff_final_link_info finfo;
3526 asection *o;
3527 struct bfd_link_order *p;
3528 bfd_size_type max_contents_size;
3529 bfd_size_type max_sym_count;
3530 bfd_size_type max_lineno_count;
3531 bfd_size_type max_reloc_count;
3532 bfd_size_type max_output_reloc_count;
3533 file_ptr rel_filepos;
3534 unsigned int relsz;
3535 file_ptr line_filepos;
3536 unsigned int linesz;
3537 bfd *sub;
3538 bfd_byte *external_relocs = NULL;
3539 char strbuf[STRING_SIZE_SIZE];
3540 file_ptr pos;
3541 bfd_size_type amt;
3543 if (info->shared)
3544 abfd->flags |= DYNAMIC;
3546 symesz = bfd_coff_symesz (abfd);
3548 finfo.info = info;
3549 finfo.output_bfd = abfd;
3550 finfo.strtab = NULL;
3551 finfo.section_info = NULL;
3552 finfo.last_file_index = -1;
3553 finfo.toc_symindx = -1;
3554 finfo.internal_syms = NULL;
3555 finfo.sym_indices = NULL;
3556 finfo.outsyms = NULL;
3557 finfo.linenos = NULL;
3558 finfo.contents = NULL;
3559 finfo.external_relocs = NULL;
3561 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
3562 + bfd_xcoff_ldhdrsz (abfd));
3563 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
3564 + bfd_xcoff_ldhdrsz(abfd)
3565 + (xcoff_hash_table (info)->ldhdr.l_nsyms
3566 * bfd_xcoff_ldsymsz(abfd)));
3568 xcoff_data (abfd)->coff.link_info = info;
3570 finfo.strtab = _bfd_stringtab_init ();
3571 if (finfo.strtab == NULL)
3572 goto error_return;
3574 /* Count the line number and relocation entries required for the
3575 output file. Determine a few maximum sizes. */
3576 max_contents_size = 0;
3577 max_lineno_count = 0;
3578 max_reloc_count = 0;
3579 for (o = abfd->sections; o != NULL; o = o->next)
3581 o->reloc_count = 0;
3582 o->lineno_count = 0;
3583 for (p = o->link_order_head; p != NULL; p = p->next)
3585 if (p->type == bfd_indirect_link_order)
3587 asection *sec;
3589 sec = p->u.indirect.section;
3591 /* Mark all sections which are to be included in the
3592 link. This will normally be every section. We need
3593 to do this so that we can identify any sections which
3594 the linker has decided to not include. */
3595 sec->linker_mark = true;
3597 if (info->strip == strip_none
3598 || info->strip == strip_some)
3599 o->lineno_count += sec->lineno_count;
3601 o->reloc_count += sec->reloc_count;
3603 if (sec->_raw_size > max_contents_size)
3604 max_contents_size = sec->_raw_size;
3605 if (sec->lineno_count > max_lineno_count)
3606 max_lineno_count = sec->lineno_count;
3607 if (coff_section_data (sec->owner, sec) != NULL
3608 && xcoff_section_data (sec->owner, sec) != NULL
3609 && (xcoff_section_data (sec->owner, sec)->lineno_count
3610 > max_lineno_count))
3611 max_lineno_count =
3612 xcoff_section_data (sec->owner, sec)->lineno_count;
3613 if (sec->reloc_count > max_reloc_count)
3614 max_reloc_count = sec->reloc_count;
3616 else if (p->type == bfd_section_reloc_link_order
3617 || p->type == bfd_symbol_reloc_link_order)
3618 ++o->reloc_count;
3622 /* Compute the file positions for all the sections. */
3623 if (abfd->output_has_begun)
3625 if (xcoff_hash_table (info)->file_align != 0)
3626 abort ();
3628 else
3630 bfd_vma file_align;
3632 file_align = xcoff_hash_table (info)->file_align;
3633 if (file_align != 0)
3635 boolean saw_contents;
3636 int indx;
3637 asection **op;
3638 file_ptr sofar;
3640 /* Insert .pad sections before every section which has
3641 contents and is loaded, if it is preceded by some other
3642 section which has contents and is loaded. */
3643 saw_contents = true;
3644 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3646 if (strcmp ((*op)->name, ".pad") == 0)
3647 saw_contents = false;
3648 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3649 && ((*op)->flags & SEC_LOAD) != 0)
3651 if (! saw_contents)
3652 saw_contents = true;
3653 else
3655 asection *n, **st;
3657 /* Create a pad section and place it before the section
3658 that needs padding. This requires unlinking and
3659 relinking the bfd's section list. */
3661 st = abfd->section_tail;
3662 n = bfd_make_section_anyway (abfd, ".pad");
3663 n->flags = SEC_HAS_CONTENTS;
3664 n->alignment_power = 0;
3666 BFD_ASSERT (*st == n);
3667 bfd_section_list_remove (abfd, st);
3668 bfd_section_list_insert (abfd, op, n);
3670 op = &n->next;
3671 saw_contents = false;
3676 /* Reset the section indices after inserting the new
3677 sections. */
3678 indx = 0;
3679 for (o = abfd->sections; o != NULL; o = o->next)
3681 ++indx;
3682 o->target_index = indx;
3684 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3686 /* Work out appropriate sizes for the .pad sections to force
3687 each section to land on a page boundary. This bit of
3688 code knows what compute_section_file_positions is going
3689 to do. */
3690 sofar = bfd_coff_filhsz (abfd);
3691 sofar += bfd_coff_aoutsz (abfd);
3692 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3693 for (o = abfd->sections; o != NULL; o = o->next)
3694 if ((bfd_xcoff_is_reloc_count_overflow
3695 (abfd, (bfd_vma) o->reloc_count))
3696 || (bfd_xcoff_is_lineno_count_overflow
3697 (abfd, (bfd_vma) o->lineno_count)))
3698 /* 64 does not overflow, need to check if 32 does */
3699 sofar += bfd_coff_scnhsz (abfd);
3701 for (o = abfd->sections; o != NULL; o = o->next)
3703 if (strcmp (o->name, ".pad") == 0)
3705 bfd_vma pageoff;
3707 BFD_ASSERT (o->_raw_size == 0);
3708 pageoff = sofar & (file_align - 1);
3709 if (pageoff != 0)
3711 o->_raw_size = file_align - pageoff;
3712 sofar += file_align - pageoff;
3713 o->flags |= SEC_HAS_CONTENTS;
3716 else
3718 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3719 sofar += BFD_ALIGN (o->_raw_size,
3720 1 << o->alignment_power);
3725 if (! bfd_coff_compute_section_file_positions (abfd))
3726 goto error_return;
3729 /* Allocate space for the pointers we need to keep for the relocs. */
3731 unsigned int i;
3733 /* We use section_count + 1, rather than section_count, because
3734 the target_index fields are 1 based. */
3735 amt = abfd->section_count + 1;
3736 amt *= sizeof (struct xcoff_link_section_info);
3737 finfo.section_info = (struct xcoff_link_section_info *) bfd_malloc (amt);
3738 if (finfo.section_info == NULL)
3739 goto error_return;
3740 for (i = 0; i <= abfd->section_count; i++)
3742 finfo.section_info[i].relocs = NULL;
3743 finfo.section_info[i].rel_hashes = NULL;
3744 finfo.section_info[i].toc_rel_hashes = NULL;
3748 /* Set the file positions for the relocs. */
3749 rel_filepos = obj_relocbase (abfd);
3750 relsz = bfd_coff_relsz (abfd);
3751 max_output_reloc_count = 0;
3752 for (o = abfd->sections; o != NULL; o = o->next)
3754 if (o->reloc_count == 0)
3755 o->rel_filepos = 0;
3756 else
3758 /* A stripped file has no relocs. However, we still
3759 allocate the buffers, so that later code doesn't have to
3760 worry about whether we are stripping or not. */
3761 if (info->strip == strip_all)
3762 o->rel_filepos = 0;
3763 else
3765 o->flags |= SEC_RELOC;
3766 o->rel_filepos = rel_filepos;
3767 rel_filepos += o->reloc_count * relsz;
3770 /* We don't know the indices of global symbols until we have
3771 written out all the local symbols. For each section in
3772 the output file, we keep an array of pointers to hash
3773 table entries. Each entry in the array corresponds to a
3774 reloc. When we find a reloc against a global symbol, we
3775 set the corresponding entry in this array so that we can
3776 fix up the symbol index after we have written out all the
3777 local symbols.
3779 Because of this problem, we also keep the relocs in
3780 memory until the end of the link. This wastes memory.
3781 We could backpatch the file later, I suppose, although it
3782 would be slow. */
3783 amt = o->reloc_count;
3784 amt *= sizeof (struct internal_reloc);
3785 finfo.section_info[o->target_index].relocs =
3786 (struct internal_reloc *) bfd_malloc (amt);
3788 amt = o->reloc_count;
3789 amt *= sizeof (struct xcoff_link_hash_entry *);
3790 finfo.section_info[o->target_index].rel_hashes =
3791 (struct xcoff_link_hash_entry **) bfd_malloc (amt);
3793 if (finfo.section_info[o->target_index].relocs == NULL
3794 || finfo.section_info[o->target_index].rel_hashes == NULL)
3795 goto error_return;
3797 if (o->reloc_count > max_output_reloc_count)
3798 max_output_reloc_count = o->reloc_count;
3802 /* We now know the size of the relocs, so we can determine the file
3803 positions of the line numbers. */
3804 line_filepos = rel_filepos;
3805 finfo.line_filepos = line_filepos;
3806 linesz = bfd_coff_linesz (abfd);
3807 for (o = abfd->sections; o != NULL; o = o->next)
3809 if (o->lineno_count == 0)
3810 o->line_filepos = 0;
3811 else
3813 o->line_filepos = line_filepos;
3814 line_filepos += o->lineno_count * linesz;
3817 /* Reset the reloc and lineno counts, so that we can use them to
3818 count the number of entries we have output so far. */
3819 o->reloc_count = 0;
3820 o->lineno_count = 0;
3823 obj_sym_filepos (abfd) = line_filepos;
3825 /* Figure out the largest number of symbols in an input BFD. Take
3826 the opportunity to clear the output_has_begun fields of all the
3827 input BFD's. We want at least 6 symbols, since that is the
3828 number which xcoff_write_global_symbol may need. */
3829 max_sym_count = 6;
3830 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3832 bfd_size_type sz;
3834 sub->output_has_begun = false;
3835 sz = obj_raw_syment_count (sub);
3836 if (sz > max_sym_count)
3837 max_sym_count = sz;
3840 /* Allocate some buffers used while linking. */
3841 amt = max_sym_count * sizeof (struct internal_syment);
3842 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
3844 amt = max_sym_count * sizeof (long);
3845 finfo.sym_indices = (long *) bfd_malloc (amt);
3847 amt = (max_sym_count + 1) * symesz;
3848 finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
3850 amt = max_lineno_count * bfd_coff_linesz (abfd);
3851 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
3853 amt = max_contents_size;
3854 finfo.contents = (bfd_byte *) bfd_malloc (amt);
3856 amt = max_reloc_count * relsz;
3857 finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
3859 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3860 || (finfo.sym_indices == NULL && max_sym_count > 0)
3861 || finfo.outsyms == NULL
3862 || (finfo.linenos == NULL && max_lineno_count > 0)
3863 || (finfo.contents == NULL && max_contents_size > 0)
3864 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3865 goto error_return;
3867 obj_raw_syment_count (abfd) = 0;
3868 xcoff_data (abfd)->toc = (bfd_vma) -1;
3870 /* We now know the position of everything in the file, except that
3871 we don't know the size of the symbol table and therefore we don't
3872 know where the string table starts. We just build the string
3873 table in memory as we go along. We process all the relocations
3874 for a single input file at once. */
3875 for (o = abfd->sections; o != NULL; o = o->next)
3877 for (p = o->link_order_head; p != NULL; p = p->next)
3879 if (p->type == bfd_indirect_link_order
3880 && p->u.indirect.section->owner->xvec == abfd->xvec)
3882 sub = p->u.indirect.section->owner;
3883 if (! sub->output_has_begun)
3885 if (! xcoff_link_input_bfd (&finfo, sub))
3886 goto error_return;
3887 sub->output_has_begun = true;
3890 else if (p->type == bfd_section_reloc_link_order
3891 || p->type == bfd_symbol_reloc_link_order)
3893 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3894 goto error_return;
3896 else
3898 if (! _bfd_default_link_order (abfd, info, o, p))
3899 goto error_return;
3905 /* Free up the buffers used by xcoff_link_input_bfd. */
3907 if (finfo.internal_syms != NULL)
3909 free (finfo.internal_syms);
3910 finfo.internal_syms = NULL;
3912 if (finfo.sym_indices != NULL)
3914 free (finfo.sym_indices);
3915 finfo.sym_indices = NULL;
3917 if (finfo.linenos != NULL)
3919 free (finfo.linenos);
3920 finfo.linenos = NULL;
3922 if (finfo.contents != NULL)
3924 free (finfo.contents);
3925 finfo.contents = NULL;
3927 if (finfo.external_relocs != NULL)
3929 free (finfo.external_relocs);
3930 finfo.external_relocs = NULL;
3933 /* The value of the last C_FILE symbol is supposed to be -1. Write
3934 it out again. */
3935 if (finfo.last_file_index != -1)
3937 finfo.last_file.n_value = -(bfd_vma) 1;
3938 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3939 (PTR) finfo.outsyms);
3940 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
3941 if (bfd_seek (abfd, pos, SEEK_SET) != 0
3942 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
3943 goto error_return;
3946 /* Write out all the global symbols which do not come from XCOFF
3947 input files. */
3948 xcoff_link_hash_traverse (xcoff_hash_table (info),
3949 xcoff_write_global_symbol,
3950 (PTR) &finfo);
3952 if (finfo.outsyms != NULL)
3954 free (finfo.outsyms);
3955 finfo.outsyms = NULL;
3958 /* Now that we have written out all the global symbols, we know the
3959 symbol indices to use for relocs against them, and we can finally
3960 write out the relocs. */
3961 amt = max_output_reloc_count * relsz;
3962 external_relocs = (bfd_byte *) bfd_malloc (amt);
3963 if (external_relocs == NULL && max_output_reloc_count != 0)
3964 goto error_return;
3966 for (o = abfd->sections; o != NULL; o = o->next)
3968 struct internal_reloc *irel;
3969 struct internal_reloc *irelend;
3970 struct xcoff_link_hash_entry **rel_hash;
3971 struct xcoff_toc_rel_hash *toc_rel_hash;
3972 bfd_byte *erel;
3973 bfd_size_type rel_size;
3975 /* A stripped file has no relocs. */
3976 if (info->strip == strip_all)
3978 o->reloc_count = 0;
3979 continue;
3982 if (o->reloc_count == 0)
3983 continue;
3985 irel = finfo.section_info[o->target_index].relocs;
3986 irelend = irel + o->reloc_count;
3987 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3988 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3990 if (*rel_hash != NULL)
3992 if ((*rel_hash)->indx < 0)
3994 if (! ((*info->callbacks->unattached_reloc)
3995 (info, (*rel_hash)->root.root.string,
3996 (bfd *) NULL, o, irel->r_vaddr)))
3997 goto error_return;
3998 (*rel_hash)->indx = 0;
4000 irel->r_symndx = (*rel_hash)->indx;
4004 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4005 toc_rel_hash != NULL;
4006 toc_rel_hash = toc_rel_hash->next)
4008 if (toc_rel_hash->h->u.toc_indx < 0)
4010 if (! ((*info->callbacks->unattached_reloc)
4011 (info, toc_rel_hash->h->root.root.string,
4012 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4013 goto error_return;
4014 toc_rel_hash->h->u.toc_indx = 0;
4016 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4019 /* XCOFF requires that the relocs be sorted by address. We tend
4020 to produce them in the order in which their containing csects
4021 appear in the symbol table, which is not necessarily by
4022 address. So we sort them here. There may be a better way to
4023 do this. */
4024 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4025 o->reloc_count, sizeof (struct internal_reloc),
4026 xcoff_sort_relocs);
4028 irel = finfo.section_info[o->target_index].relocs;
4029 irelend = irel + o->reloc_count;
4030 erel = external_relocs;
4031 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4032 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4034 rel_size = relsz * o->reloc_count;
4035 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4036 || bfd_bwrite ((PTR) external_relocs, rel_size, abfd) != rel_size)
4037 goto error_return;
4040 if (external_relocs != NULL)
4042 free (external_relocs);
4043 external_relocs = NULL;
4046 /* Free up the section information. */
4047 if (finfo.section_info != NULL)
4049 unsigned int i;
4051 for (i = 0; i < abfd->section_count; i++)
4053 if (finfo.section_info[i].relocs != NULL)
4054 free (finfo.section_info[i].relocs);
4055 if (finfo.section_info[i].rel_hashes != NULL)
4056 free (finfo.section_info[i].rel_hashes);
4058 free (finfo.section_info);
4059 finfo.section_info = NULL;
4062 /* Write out the loader section contents. */
4063 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4064 == (xcoff_hash_table (info)->loader_section->contents
4065 + xcoff_hash_table (info)->ldhdr.l_impoff));
4066 o = xcoff_hash_table (info)->loader_section;
4067 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
4068 (file_ptr) o->output_offset, o->_raw_size))
4069 goto error_return;
4071 /* Write out the magic sections. */
4072 o = xcoff_hash_table (info)->linkage_section;
4073 if (o->_raw_size > 0
4074 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4075 (file_ptr) o->output_offset,
4076 o->_raw_size))
4077 goto error_return;
4078 o = xcoff_hash_table (info)->toc_section;
4079 if (o->_raw_size > 0
4080 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4081 (file_ptr) o->output_offset,
4082 o->_raw_size))
4083 goto error_return;
4084 o = xcoff_hash_table (info)->descriptor_section;
4085 if (o->_raw_size > 0
4086 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4087 (file_ptr) o->output_offset,
4088 o->_raw_size))
4089 goto error_return;
4091 /* Write out the string table. */
4092 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
4093 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4094 goto error_return;
4095 H_PUT_32 (abfd,
4096 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4097 strbuf);
4098 amt = STRING_SIZE_SIZE;
4099 if (bfd_bwrite (strbuf, amt, abfd) != amt)
4100 goto error_return;
4101 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4102 goto error_return;
4104 _bfd_stringtab_free (finfo.strtab);
4106 /* Write out the debugging string table. */
4107 o = xcoff_hash_table (info)->debug_section;
4108 if (o != NULL)
4110 struct bfd_strtab_hash *debug_strtab;
4112 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4113 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4114 >= _bfd_stringtab_size (debug_strtab));
4115 pos = o->output_section->filepos + o->output_offset;
4116 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4117 goto error_return;
4118 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4119 goto error_return;
4122 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4123 not try to write out the symbols. */
4124 bfd_get_symcount (abfd) = 0;
4126 return true;
4128 error_return:
4129 if (finfo.strtab != NULL)
4130 _bfd_stringtab_free (finfo.strtab);
4132 if (finfo.section_info != NULL)
4134 unsigned int i;
4136 for (i = 0; i < abfd->section_count; i++)
4138 if (finfo.section_info[i].relocs != NULL)
4139 free (finfo.section_info[i].relocs);
4140 if (finfo.section_info[i].rel_hashes != NULL)
4141 free (finfo.section_info[i].rel_hashes);
4143 free (finfo.section_info);
4146 if (finfo.internal_syms != NULL)
4147 free (finfo.internal_syms);
4148 if (finfo.sym_indices != NULL)
4149 free (finfo.sym_indices);
4150 if (finfo.outsyms != NULL)
4151 free (finfo.outsyms);
4152 if (finfo.linenos != NULL)
4153 free (finfo.linenos);
4154 if (finfo.contents != NULL)
4155 free (finfo.contents);
4156 if (finfo.external_relocs != NULL)
4157 free (finfo.external_relocs);
4158 if (external_relocs != NULL)
4159 free (external_relocs);
4160 return false;
4163 /* Link an input file into the linker output file. This function
4164 handles all the sections and relocations of the input file at once. */
4166 static boolean
4167 xcoff_link_input_bfd (finfo, input_bfd)
4168 struct xcoff_final_link_info *finfo;
4169 bfd *input_bfd;
4171 bfd *output_bfd;
4172 const char *strings;
4173 bfd_size_type syment_base;
4174 unsigned int n_tmask;
4175 unsigned int n_btshft;
4176 boolean copy, hash;
4177 bfd_size_type isymesz;
4178 bfd_size_type osymesz;
4179 bfd_size_type linesz;
4180 bfd_byte *esym;
4181 bfd_byte *esym_end;
4182 struct xcoff_link_hash_entry **sym_hash;
4183 struct internal_syment *isymp;
4184 asection **csectpp;
4185 unsigned long *debug_index;
4186 long *indexp;
4187 unsigned long output_index;
4188 bfd_byte *outsym;
4189 unsigned int incls;
4190 asection *oline;
4191 boolean keep_syms;
4192 asection *o;
4194 /* We can just skip DYNAMIC files, unless this is a static link. */
4195 if ((input_bfd->flags & DYNAMIC) != 0
4196 && ! finfo->info->static_link)
4197 return true;
4199 /* Move all the symbols to the output file. */
4201 output_bfd = finfo->output_bfd;
4202 strings = NULL;
4203 syment_base = obj_raw_syment_count (output_bfd);
4204 isymesz = bfd_coff_symesz (input_bfd);
4205 osymesz = bfd_coff_symesz (output_bfd);
4206 linesz = bfd_coff_linesz (input_bfd);
4207 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4209 n_tmask = coff_data (input_bfd)->local_n_tmask;
4210 n_btshft = coff_data (input_bfd)->local_n_btshft;
4212 /* Define macros so that ISFCN, et. al., macros work correctly. */
4213 #define N_TMASK n_tmask
4214 #define N_BTSHFT n_btshft
4216 copy = false;
4217 if (! finfo->info->keep_memory)
4218 copy = true;
4219 hash = true;
4220 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4221 hash = false;
4223 if (! _bfd_coff_get_external_symbols (input_bfd))
4224 return false;
4226 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4227 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4228 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4229 csectpp = xcoff_data (input_bfd)->csects;
4230 debug_index = xcoff_data (input_bfd)->debug_indices;
4231 isymp = finfo->internal_syms;
4232 indexp = finfo->sym_indices;
4233 output_index = syment_base;
4234 outsym = finfo->outsyms;
4235 incls = 0;
4236 oline = NULL;
4238 while (esym < esym_end)
4241 struct internal_syment isym;
4242 union internal_auxent aux;
4243 int smtyp = 0;
4244 boolean skip;
4245 boolean require;
4246 int add;
4248 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4250 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4251 information. */
4252 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4254 BFD_ASSERT (isymp->n_numaux > 0);
4255 bfd_coff_swap_aux_in (input_bfd,
4256 (PTR) (esym + isymesz * isymp->n_numaux),
4257 isymp->n_type, isymp->n_sclass,
4258 isymp->n_numaux - 1, isymp->n_numaux,
4259 (PTR) &aux);
4261 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4264 /* Make a copy of *isymp so that the relocate_section function
4265 always sees the original values. This is more reliable than
4266 always recomputing the symbol value even if we are stripping
4267 the symbol. */
4268 isym = *isymp;
4270 /* If this symbol is in the .loader section, swap out the
4271 .loader symbol information. If this is an external symbol
4272 reference to a defined symbol, though, then wait until we get
4273 to the definition. */
4274 if (isym.n_sclass == C_EXT
4275 && *sym_hash != NULL
4276 && (*sym_hash)->ldsym != NULL
4277 && (smtyp != XTY_ER
4278 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4280 struct xcoff_link_hash_entry *h;
4281 struct internal_ldsym *ldsym;
4283 h = *sym_hash;
4284 ldsym = h->ldsym;
4285 if (isym.n_scnum > 0)
4287 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4288 ldsym->l_value = (isym.n_value
4289 + (*csectpp)->output_section->vma
4290 + (*csectpp)->output_offset
4291 - (*csectpp)->vma);
4293 else
4295 ldsym->l_scnum = isym.n_scnum;
4296 ldsym->l_value = isym.n_value;
4299 ldsym->l_smtype = smtyp;
4300 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4301 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4302 || (h->flags & XCOFF_IMPORT) != 0)
4303 ldsym->l_smtype |= L_IMPORT;
4304 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4305 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4306 || (h->flags & XCOFF_EXPORT) != 0)
4307 ldsym->l_smtype |= L_EXPORT;
4308 if ((h->flags & XCOFF_ENTRY) != 0)
4309 ldsym->l_smtype |= L_ENTRY;
4311 ldsym->l_smclas = aux.x_csect.x_smclas;
4313 if (ldsym->l_ifile == (bfd_size_type) -1)
4314 ldsym->l_ifile = 0;
4315 else if (ldsym->l_ifile == 0)
4317 if ((ldsym->l_smtype & L_IMPORT) == 0)
4318 ldsym->l_ifile = 0;
4319 else
4321 bfd *impbfd;
4323 if (h->root.type == bfd_link_hash_defined
4324 || h->root.type == bfd_link_hash_defweak)
4325 impbfd = h->root.u.def.section->owner;
4326 else if (h->root.type == bfd_link_hash_undefined
4327 || h->root.type == bfd_link_hash_undefweak)
4328 impbfd = h->root.u.undef.abfd;
4329 else
4330 impbfd = NULL;
4332 if (impbfd == NULL)
4333 ldsym->l_ifile = 0;
4334 else
4336 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4337 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4342 ldsym->l_parm = 0;
4344 BFD_ASSERT (h->ldindx >= 0);
4345 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4346 (finfo->ldsym
4347 + ((h->ldindx - 3)
4348 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
4349 h->ldsym = NULL;
4351 /* Fill in snentry now that we know the target_index. */
4352 if ((h->flags & XCOFF_ENTRY) != 0
4353 && (h->root.type == bfd_link_hash_defined
4354 || h->root.type == bfd_link_hash_defweak))
4356 xcoff_data (output_bfd)->snentry =
4357 h->root.u.def.section->output_section->target_index;
4361 *indexp = -1;
4363 skip = false;
4364 require = false;
4365 add = 1 + isym.n_numaux;
4367 /* If we are skipping this csect, we want to skip this symbol. */
4368 if (*csectpp == NULL)
4369 skip = true;
4371 /* If we garbage collected this csect, we want to skip this
4372 symbol. */
4373 if (! skip
4374 && xcoff_hash_table (finfo->info)->gc
4375 && ((*csectpp)->flags & SEC_MARK) == 0
4376 && *csectpp != bfd_abs_section_ptr)
4377 skip = true;
4379 /* An XCOFF linker always skips C_STAT symbols. */
4380 if (! skip
4381 && isymp->n_sclass == C_STAT)
4382 skip = true;
4384 /* We skip all but the first TOC anchor. */
4385 if (! skip
4386 && isymp->n_sclass == C_HIDEXT
4387 && aux.x_csect.x_smclas == XMC_TC0)
4389 if (finfo->toc_symindx != -1)
4390 skip = true;
4391 else
4393 bfd_vma tocval, tocend;
4394 bfd *inp;
4396 tocval = ((*csectpp)->output_section->vma
4397 + (*csectpp)->output_offset
4398 + isym.n_value
4399 - (*csectpp)->vma);
4401 /* We want to find out if tocval is a good value to use
4402 as the TOC anchor--that is, whether we can access all
4403 of the TOC using a 16 bit offset from tocval. This
4404 test assumes that the TOC comes at the end of the
4405 output section, as it does in the default linker
4406 script. */
4407 tocend = ((*csectpp)->output_section->vma
4408 + (*csectpp)->output_section->_raw_size);
4409 for (inp = finfo->info->input_bfds;
4410 inp != NULL;
4411 inp = inp->link_next)
4414 for (o = inp->sections; o != NULL; o = o->next)
4415 if (strcmp (o->name, ".tocbss") == 0)
4417 bfd_vma new_toc_end;
4418 new_toc_end = (o->output_section->vma
4419 + o->output_offset
4420 + o->_cooked_size);
4421 if (new_toc_end > tocend)
4422 tocend = new_toc_end;
4427 if (tocval + 0x10000 < tocend)
4429 (*_bfd_error_handler)
4430 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4431 (unsigned long) (tocend - tocval));
4432 bfd_set_error (bfd_error_file_too_big);
4433 return false;
4436 if (tocval + 0x8000 < tocend)
4438 bfd_vma tocadd;
4440 tocadd = tocend - (tocval + 0x8000);
4441 tocval += tocadd;
4442 isym.n_value += tocadd;
4445 finfo->toc_symindx = output_index;
4446 xcoff_data (finfo->output_bfd)->toc = tocval;
4447 xcoff_data (finfo->output_bfd)->sntoc =
4448 (*csectpp)->output_section->target_index;
4449 require = true;
4454 /* If we are stripping all symbols, we want to skip this one. */
4455 if (! skip
4456 && finfo->info->strip == strip_all)
4457 skip = true;
4459 /* We can skip resolved external references. */
4460 if (! skip
4461 && isym.n_sclass == C_EXT
4462 && smtyp == XTY_ER
4463 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4464 skip = true;
4466 /* We can skip common symbols if they got defined somewhere
4467 else. */
4468 if (! skip
4469 && isym.n_sclass == C_EXT
4470 && smtyp == XTY_CM
4471 && ((*sym_hash)->root.type != bfd_link_hash_common
4472 || (*sym_hash)->root.u.c.p->section != *csectpp)
4473 && ((*sym_hash)->root.type != bfd_link_hash_defined
4474 || (*sym_hash)->root.u.def.section != *csectpp))
4475 skip = true;
4477 /* Skip local symbols if we are discarding them. */
4478 if (! skip
4479 && finfo->info->discard == discard_all
4480 && isym.n_sclass != C_EXT
4481 && (isym.n_sclass != C_HIDEXT
4482 || smtyp != XTY_SD))
4483 skip = true;
4485 /* If we stripping debugging symbols, and this is a debugging
4486 symbol, then skip it. */
4487 if (! skip
4488 && finfo->info->strip == strip_debugger
4489 && isym.n_scnum == N_DEBUG)
4490 skip = true;
4492 /* If some symbols are stripped based on the name, work out the
4493 name and decide whether to skip this symbol. We don't handle
4494 this correctly for symbols whose names are in the .debug
4495 section; to get it right we would need a new bfd_strtab_hash
4496 function to return the string given the index. */
4497 if (! skip
4498 && (finfo->info->strip == strip_some
4499 || finfo->info->discard == discard_l)
4500 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4502 const char *name;
4503 char buf[SYMNMLEN + 1];
4505 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4507 if (name == NULL)
4508 return false;
4510 if ((finfo->info->strip == strip_some
4511 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4512 false) == NULL))
4513 || (finfo->info->discard == discard_l
4514 && (isym.n_sclass != C_EXT
4515 && (isym.n_sclass != C_HIDEXT
4516 || smtyp != XTY_SD))
4517 && bfd_is_local_label_name (input_bfd, name)))
4518 skip = true;
4521 /* We can not skip the first TOC anchor. */
4522 if (skip
4523 && require
4524 && finfo->info->strip != strip_all)
4525 skip = false;
4527 /* We now know whether we are to skip this symbol or not. */
4528 if (! skip)
4530 /* Adjust the symbol in order to output it. */
4532 if (isym._n._n_n._n_zeroes == 0
4533 && isym._n._n_n._n_offset != 0)
4535 /* This symbol has a long name. Enter it in the string
4536 table we are building. If *debug_index != -1, the
4537 name has already been entered in the .debug section. */
4538 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4539 isym._n._n_n._n_offset = *debug_index;
4540 else
4542 const char *name;
4543 bfd_size_type indx;
4545 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4546 (char *) NULL);
4548 if (name == NULL)
4549 return false;
4550 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4551 if (indx == (bfd_size_type) -1)
4552 return false;
4553 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4557 if (isym.n_sclass != C_BSTAT
4558 && isym.n_sclass != C_ESTAT
4559 && isym.n_sclass != C_DECL
4560 && isym.n_scnum > 0)
4562 isym.n_scnum = (*csectpp)->output_section->target_index;
4563 isym.n_value += ((*csectpp)->output_section->vma
4564 + (*csectpp)->output_offset
4565 - (*csectpp)->vma);
4568 /* The value of a C_FILE symbol is the symbol index of the
4569 next C_FILE symbol. The value of the last C_FILE symbol
4570 is -1. We try to get this right, below, just before we
4571 write the symbols out, but in the general case we may
4572 have to write the symbol out twice. */
4573 if (isym.n_sclass == C_FILE)
4575 if (finfo->last_file_index != -1
4576 && finfo->last_file.n_value != (bfd_vma) output_index)
4578 /* We must correct the value of the last C_FILE entry. */
4579 finfo->last_file.n_value = output_index;
4580 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4582 /* The last C_FILE symbol is in this input file. */
4583 bfd_coff_swap_sym_out (output_bfd,
4584 (PTR) &finfo->last_file,
4585 (PTR) (finfo->outsyms
4586 + ((finfo->last_file_index
4587 - syment_base)
4588 * osymesz)));
4590 else
4592 /* We have already written out the last C_FILE
4593 symbol. We need to write it out again. We
4594 borrow *outsym temporarily. */
4595 file_ptr pos;
4597 bfd_coff_swap_sym_out (output_bfd,
4598 (PTR) &finfo->last_file,
4599 (PTR) outsym);
4601 pos = obj_sym_filepos (output_bfd);
4602 pos += finfo->last_file_index * osymesz;
4603 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4604 || (bfd_bwrite (outsym, osymesz, output_bfd)
4605 != osymesz))
4606 return false;
4610 finfo->last_file_index = output_index;
4611 finfo->last_file = isym;
4614 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4615 into the line numbers. We update the symbol values when
4616 we handle the line numbers. */
4617 if (isym.n_sclass == C_BINCL
4618 || isym.n_sclass == C_EINCL)
4620 isym.n_value = finfo->line_filepos;
4621 ++incls;
4624 /* Output the symbol. */
4626 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4628 *indexp = output_index;
4630 if (isym.n_sclass == C_EXT)
4632 long indx;
4633 struct xcoff_link_hash_entry *h;
4635 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4636 / isymesz);
4637 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4638 BFD_ASSERT (h != NULL);
4639 h->indx = output_index;
4642 /* If this is a symbol in the TOC which we may have merged
4643 (class XMC_TC), remember the symbol index of the TOC
4644 symbol. */
4645 if (isym.n_sclass == C_HIDEXT
4646 && aux.x_csect.x_smclas == XMC_TC
4647 && *sym_hash != NULL)
4649 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4650 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4651 (*sym_hash)->u.toc_indx = output_index;
4654 output_index += add;
4655 outsym += add * osymesz;
4658 esym += add * isymesz;
4659 isymp += add;
4660 csectpp += add;
4661 sym_hash += add;
4662 if (debug_index != NULL)
4663 debug_index += add;
4664 ++indexp;
4665 for (--add; add > 0; --add)
4666 *indexp++ = -1;
4669 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4670 done in a separate pass, because we don't know the correct symbol
4671 indices until we have already decided which symbols we are going
4672 to keep. */
4674 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4675 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4676 isymp = finfo->internal_syms;
4677 indexp = finfo->sym_indices;
4678 csectpp = xcoff_data (input_bfd)->csects;
4679 outsym = finfo->outsyms;
4680 while (esym < esym_end)
4682 int add;
4684 add = 1 + isymp->n_numaux;
4686 if (*indexp < 0)
4687 esym += add * isymesz;
4688 else
4690 int i;
4692 if (isymp->n_sclass == C_BSTAT)
4694 struct internal_syment isym;
4696 bfd_vma indx;
4698 /* The value of a C_BSTAT symbol is the symbol table
4699 index of the containing csect. */
4700 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4701 indx = isym.n_value;
4702 if (indx < obj_raw_syment_count (input_bfd))
4704 long symindx;
4706 symindx = finfo->sym_indices[indx];
4707 if (symindx < 0)
4708 isym.n_value = 0;
4709 else
4710 isym.n_value = symindx;
4711 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4712 (PTR) outsym);
4716 esym += isymesz;
4717 outsym += osymesz;
4719 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4721 union internal_auxent aux;
4723 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4724 isymp->n_sclass, i, isymp->n_numaux,
4725 (PTR) &aux);
4727 if (isymp->n_sclass == C_FILE)
4729 /* This is the file name (or some comment put in by
4730 the compiler). If it is long, we must put it in
4731 the string table. */
4732 if (aux.x_file.x_n.x_zeroes == 0
4733 && aux.x_file.x_n.x_offset != 0)
4735 const char *filename;
4736 bfd_size_type indx;
4738 BFD_ASSERT (aux.x_file.x_n.x_offset
4739 >= STRING_SIZE_SIZE);
4740 if (strings == NULL)
4742 strings = _bfd_coff_read_string_table (input_bfd);
4743 if (strings == NULL)
4744 return false;
4746 filename = strings + aux.x_file.x_n.x_offset;
4747 indx = _bfd_stringtab_add (finfo->strtab, filename,
4748 hash, copy);
4749 if (indx == (bfd_size_type) -1)
4750 return false;
4751 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4754 else if ((isymp->n_sclass == C_EXT
4755 || isymp->n_sclass == C_HIDEXT)
4756 && i + 1 == isymp->n_numaux)
4759 /* We don't support type checking. I don't know if
4760 anybody does. */
4761 aux.x_csect.x_parmhash = 0;
4762 /* I don't think anybody uses these fields, but we'd
4763 better clobber them just in case. */
4764 aux.x_csect.x_stab = 0;
4765 aux.x_csect.x_snstab = 0;
4767 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4769 unsigned long indx;
4771 indx = aux.x_csect.x_scnlen.l;
4772 if (indx < obj_raw_syment_count (input_bfd))
4774 long symindx;
4776 symindx = finfo->sym_indices[indx];
4777 if (symindx < 0)
4779 aux.x_csect.x_scnlen.l = 0;
4781 else
4783 aux.x_csect.x_scnlen.l = symindx;
4788 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4790 unsigned long indx;
4792 if (ISFCN (isymp->n_type)
4793 || ISTAG (isymp->n_sclass)
4794 || isymp->n_sclass == C_BLOCK
4795 || isymp->n_sclass == C_FCN)
4797 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4798 if (indx > 0
4799 && indx < obj_raw_syment_count (input_bfd))
4801 /* We look forward through the symbol for
4802 the index of the next symbol we are going
4803 to include. I don't know if this is
4804 entirely right. */
4805 while (finfo->sym_indices[indx] < 0
4806 && indx < obj_raw_syment_count (input_bfd))
4807 ++indx;
4808 if (indx >= obj_raw_syment_count (input_bfd))
4809 indx = output_index;
4810 else
4811 indx = finfo->sym_indices[indx];
4812 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4817 indx = aux.x_sym.x_tagndx.l;
4818 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4820 long symindx;
4822 symindx = finfo->sym_indices[indx];
4823 if (symindx < 0)
4824 aux.x_sym.x_tagndx.l = 0;
4825 else
4826 aux.x_sym.x_tagndx.l = symindx;
4831 /* Copy over the line numbers, unless we are stripping
4832 them. We do this on a symbol by symbol basis in
4833 order to more easily handle garbage collection. */
4834 if ((isymp->n_sclass == C_EXT
4835 || isymp->n_sclass == C_HIDEXT)
4836 && i == 0
4837 && isymp->n_numaux > 1
4838 && ISFCN (isymp->n_type)
4839 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4841 if (finfo->info->strip != strip_none
4842 && finfo->info->strip != strip_some)
4843 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4844 else
4846 asection *enclosing;
4847 unsigned int enc_count;
4848 bfd_signed_vma linoff;
4849 struct internal_lineno lin;
4851 o = *csectpp;
4852 enclosing = xcoff_section_data (abfd, o)->enclosing;
4853 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4854 if (oline != enclosing)
4856 file_ptr pos = enclosing->line_filepos;
4857 bfd_size_type amt = linesz * enc_count;
4858 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4859 || (bfd_bread (finfo->linenos, amt, input_bfd)
4860 != amt))
4861 return false;
4862 oline = enclosing;
4865 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4866 - enclosing->line_filepos);
4868 bfd_coff_swap_lineno_in (input_bfd,
4869 (PTR) (finfo->linenos + linoff),
4870 (PTR) &lin);
4871 if (lin.l_lnno != 0
4872 || ((bfd_size_type) lin.l_addr.l_symndx
4873 != ((esym
4874 - isymesz
4875 - ((bfd_byte *)
4876 obj_coff_external_syms (input_bfd)))
4877 / isymesz)))
4878 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4879 else
4881 bfd_byte *linpend, *linp;
4882 bfd_vma offset;
4883 bfd_size_type count;
4885 lin.l_addr.l_symndx = *indexp;
4886 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4887 (PTR) (finfo->linenos
4888 + linoff));
4890 linpend = (finfo->linenos
4891 + enc_count * linesz);
4892 offset = (o->output_section->vma
4893 + o->output_offset
4894 - o->vma);
4895 for (linp = finfo->linenos + linoff + linesz;
4896 linp < linpend;
4897 linp += linesz)
4899 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4900 (PTR) &lin);
4901 if (lin.l_lnno == 0)
4902 break;
4903 lin.l_addr.l_paddr += offset;
4904 bfd_coff_swap_lineno_out (output_bfd,
4905 (PTR) &lin,
4906 (PTR) linp);
4909 count = (linp - (finfo->linenos + linoff)) / linesz;
4911 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4912 (o->output_section->line_filepos
4913 + o->output_section->lineno_count * linesz);
4915 if (bfd_seek (output_bfd,
4916 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4917 SEEK_SET) != 0
4918 || (bfd_bwrite (finfo->linenos + linoff,
4919 linesz * count, output_bfd)
4920 != linesz * count))
4921 return false;
4923 o->output_section->lineno_count += count;
4925 if (incls > 0)
4927 struct internal_syment *iisp, *iispend;
4928 long *iindp;
4929 bfd_byte *oos;
4930 int iiadd;
4932 /* Update any C_BINCL or C_EINCL symbols
4933 that refer to a line number in the
4934 range we just output. */
4935 iisp = finfo->internal_syms;
4936 iispend = (iisp
4937 + obj_raw_syment_count (input_bfd));
4938 iindp = finfo->sym_indices;
4939 oos = finfo->outsyms;
4940 while (iisp < iispend)
4942 if (*iindp >= 0
4943 && (iisp->n_sclass == C_BINCL
4944 || iisp->n_sclass == C_EINCL)
4945 && ((bfd_size_type) iisp->n_value
4946 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4947 && ((bfd_size_type) iisp->n_value
4948 < (enclosing->line_filepos
4949 + enc_count * linesz)))
4951 struct internal_syment iis;
4953 bfd_coff_swap_sym_in (output_bfd,
4954 (PTR) oos,
4955 (PTR) &iis);
4956 iis.n_value =
4957 (iisp->n_value
4958 - enclosing->line_filepos
4959 - linoff
4960 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4961 bfd_coff_swap_sym_out (output_bfd,
4962 (PTR) &iis,
4963 (PTR) oos);
4964 --incls;
4967 iiadd = 1 + iisp->n_numaux;
4968 if (*iindp >= 0)
4969 oos += iiadd * osymesz;
4970 iisp += iiadd;
4971 iindp += iiadd;
4978 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4979 isymp->n_sclass, i, isymp->n_numaux,
4980 (PTR) outsym);
4981 outsym += osymesz;
4982 esym += isymesz;
4986 indexp += add;
4987 isymp += add;
4988 csectpp += add;
4991 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4992 symbol will be the first symbol in the next input file. In the
4993 normal case, this will save us from writing out the C_FILE symbol
4994 again. */
4995 if (finfo->last_file_index != -1
4996 && (bfd_size_type) finfo->last_file_index >= syment_base)
4998 finfo->last_file.n_value = output_index;
4999 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5000 (PTR) (finfo->outsyms
5001 + ((finfo->last_file_index - syment_base)
5002 * osymesz)));
5005 /* Write the modified symbols to the output file. */
5006 if (outsym > finfo->outsyms)
5008 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
5009 bfd_size_type amt = outsym - finfo->outsyms;
5010 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5011 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5012 return false;
5014 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5015 + (outsym - finfo->outsyms) / osymesz)
5016 == output_index);
5018 obj_raw_syment_count (output_bfd) = output_index;
5021 /* Don't let the linker relocation routines discard the symbols. */
5022 keep_syms = obj_coff_keep_syms (input_bfd);
5023 obj_coff_keep_syms (input_bfd) = true;
5025 /* Relocate the contents of each section. */
5026 for (o = input_bfd->sections; o != NULL; o = o->next)
5029 bfd_byte *contents;
5031 if (! o->linker_mark)
5033 /* This section was omitted from the link. */
5034 continue;
5037 if ((o->flags & SEC_HAS_CONTENTS) == 0
5038 || o->_raw_size == 0
5039 || (o->flags & SEC_IN_MEMORY) != 0)
5040 continue;
5042 /* We have set filepos correctly for the sections we created to
5043 represent csects, so bfd_get_section_contents should work. */
5044 if (coff_section_data (input_bfd, o) != NULL
5045 && coff_section_data (input_bfd, o)->contents != NULL)
5046 contents = coff_section_data (input_bfd, o)->contents;
5047 else {
5048 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5049 (file_ptr) 0, o->_raw_size))
5050 return false;
5051 contents = finfo->contents;
5054 if ((o->flags & SEC_RELOC) != 0)
5056 int target_index;
5057 struct internal_reloc *internal_relocs;
5058 struct internal_reloc *irel;
5059 bfd_vma offset;
5060 struct internal_reloc *irelend;
5061 struct xcoff_link_hash_entry **rel_hash;
5062 long r_symndx;
5064 /* Read in the relocs. */
5065 target_index = o->output_section->target_index;
5066 internal_relocs = (xcoff_read_internal_relocs
5067 (input_bfd, o, false, finfo->external_relocs,
5068 true,
5069 (finfo->section_info[target_index].relocs
5070 + o->output_section->reloc_count)));
5071 if (internal_relocs == NULL)
5072 return false;
5074 /* Call processor specific code to relocate the section
5075 contents. */
5076 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5077 input_bfd, o,
5078 contents,
5079 internal_relocs,
5080 finfo->internal_syms,
5081 xcoff_data (input_bfd)->csects))
5082 return false;
5084 offset = o->output_section->vma + o->output_offset - o->vma;
5085 irel = internal_relocs;
5086 irelend = irel + o->reloc_count;
5087 rel_hash = (finfo->section_info[target_index].rel_hashes
5088 + o->output_section->reloc_count);
5089 for (; irel < irelend; irel++, rel_hash++)
5091 struct xcoff_link_hash_entry *h = NULL;
5092 struct internal_ldrel ldrel;
5093 boolean quiet;
5095 *rel_hash = NULL;
5097 /* Adjust the reloc address and symbol index. */
5099 irel->r_vaddr += offset;
5101 r_symndx = irel->r_symndx;
5103 if (r_symndx == -1)
5104 h = NULL;
5105 else
5106 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5108 if (r_symndx != -1 && finfo->info->strip != strip_all)
5110 if (h != NULL
5111 && h->smclas != XMC_TD
5112 && (irel->r_type == R_TOC
5113 || irel->r_type == R_GL
5114 || irel->r_type == R_TCL
5115 || irel->r_type == R_TRL
5116 || irel->r_type == R_TRLA))
5118 /* This is a TOC relative reloc with a symbol
5119 attached. The symbol should be the one which
5120 this reloc is for. We want to make this
5121 reloc against the TOC address of the symbol,
5122 not the symbol itself. */
5123 BFD_ASSERT (h->toc_section != NULL);
5124 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5125 if (h->u.toc_indx != -1)
5126 irel->r_symndx = h->u.toc_indx;
5127 else
5129 struct xcoff_toc_rel_hash *n;
5130 struct xcoff_link_section_info *si;
5131 bfd_size_type amt;
5133 amt = sizeof (struct xcoff_toc_rel_hash);
5134 n = ((struct xcoff_toc_rel_hash *)
5135 bfd_alloc (finfo->output_bfd, amt));
5136 if (n == NULL)
5137 return false;
5138 si = finfo->section_info + target_index;
5139 n->next = si->toc_rel_hashes;
5140 n->h = h;
5141 n->rel = irel;
5142 si->toc_rel_hashes = n;
5145 else if (h != NULL)
5147 /* This is a global symbol. */
5148 if (h->indx >= 0)
5149 irel->r_symndx = h->indx;
5150 else
5152 /* This symbol is being written at the end
5153 of the file, and we do not yet know the
5154 symbol index. We save the pointer to the
5155 hash table entry in the rel_hash list.
5156 We set the indx field to -2 to indicate
5157 that this symbol must not be stripped. */
5158 *rel_hash = h;
5159 h->indx = -2;
5162 else
5164 long indx;
5166 indx = finfo->sym_indices[r_symndx];
5168 if (indx == -1)
5170 struct internal_syment *is;
5172 /* Relocations against a TC0 TOC anchor are
5173 automatically transformed to be against
5174 the TOC anchor in the output file. */
5175 is = finfo->internal_syms + r_symndx;
5176 if (is->n_sclass == C_HIDEXT
5177 && is->n_numaux > 0)
5179 PTR auxptr;
5180 union internal_auxent aux;
5182 auxptr = ((PTR)
5183 (((bfd_byte *)
5184 obj_coff_external_syms (input_bfd))
5185 + ((r_symndx + is->n_numaux)
5186 * isymesz)));
5187 bfd_coff_swap_aux_in (input_bfd, auxptr,
5188 is->n_type, is->n_sclass,
5189 is->n_numaux - 1,
5190 is->n_numaux,
5191 (PTR) &aux);
5192 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5193 && aux.x_csect.x_smclas == XMC_TC0)
5194 indx = finfo->toc_symindx;
5198 if (indx != -1)
5199 irel->r_symndx = indx;
5200 else
5203 struct internal_syment *is;
5205 const char *name;
5206 char buf[SYMNMLEN + 1];
5208 /* This reloc is against a symbol we are
5209 stripping. It would be possible to handle
5210 this case, but I don't think it's worth it. */
5211 is = finfo->internal_syms + r_symndx;
5213 name = (_bfd_coff_internal_syment_name
5214 (input_bfd, is, buf));
5216 if (name == NULL)
5217 return false;
5219 if (! ((*finfo->info->callbacks->unattached_reloc)
5220 (finfo->info, name, input_bfd, o,
5221 irel->r_vaddr)))
5222 return false;
5227 quiet = false;
5228 switch (irel->r_type)
5230 default:
5231 if (h == NULL
5232 || h->root.type == bfd_link_hash_defined
5233 || h->root.type == bfd_link_hash_defweak
5234 || h->root.type == bfd_link_hash_common)
5235 break;
5236 /* Fall through. */
5237 case R_POS:
5238 case R_NEG:
5239 case R_RL:
5240 case R_RLA:
5241 /* This reloc needs to be copied into the .loader
5242 section. */
5243 ldrel.l_vaddr = irel->r_vaddr;
5244 if (r_symndx == -1)
5245 ldrel.l_symndx = -(bfd_size_type ) 1;
5246 else if (h == NULL
5247 || (h->root.type == bfd_link_hash_defined
5248 || h->root.type == bfd_link_hash_defweak
5249 || h->root.type == bfd_link_hash_common))
5251 asection *sec;
5253 if (h == NULL)
5254 sec = xcoff_data (input_bfd)->csects[r_symndx];
5255 else if (h->root.type == bfd_link_hash_common)
5256 sec = h->root.u.c.p->section;
5257 else
5258 sec = h->root.u.def.section;
5259 sec = sec->output_section;
5261 if (strcmp (sec->name, ".text") == 0)
5262 ldrel.l_symndx = 0;
5263 else if (strcmp (sec->name, ".data") == 0)
5264 ldrel.l_symndx = 1;
5265 else if (strcmp (sec->name, ".bss") == 0)
5266 ldrel.l_symndx = 2;
5267 else
5269 (*_bfd_error_handler)
5270 (_("%s: loader reloc in unrecognized section `%s'"),
5271 bfd_archive_filename (input_bfd),
5272 sec->name);
5273 bfd_set_error (bfd_error_nonrepresentable_section);
5274 return false;
5277 else
5279 if (! finfo->info->relocateable
5280 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5281 && (h->flags & XCOFF_IMPORT) == 0)
5283 /* We already called the undefined_symbol
5284 callback for this relocation, in
5285 _bfd_ppc_xcoff_relocate_section. Don't
5286 issue any more warnings. */
5287 quiet = true;
5289 if (h->ldindx < 0 && ! quiet)
5291 (*_bfd_error_handler)
5292 (_("%s: `%s' in loader reloc but not loader sym"),
5293 bfd_archive_filename (input_bfd),
5294 h->root.root.string);
5295 bfd_set_error (bfd_error_bad_value);
5296 return false;
5298 ldrel.l_symndx = h->ldindx;
5300 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5301 ldrel.l_rsecnm = o->output_section->target_index;
5302 if (xcoff_hash_table (finfo->info)->textro
5303 && strcmp (o->output_section->name, ".text") == 0
5304 && ! quiet)
5306 (*_bfd_error_handler)
5307 (_("%s: loader reloc in read-only section %s"),
5308 bfd_archive_filename (input_bfd),
5309 bfd_get_section_name (finfo->output_bfd,
5310 o->output_section));
5311 bfd_set_error (bfd_error_invalid_operation);
5312 return false;
5314 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
5315 finfo->ldrel);
5317 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5318 break;
5320 case R_TOC:
5321 case R_GL:
5322 case R_TCL:
5323 case R_TRL:
5324 case R_TRLA:
5325 /* We should never need a .loader reloc for a TOC
5326 relative reloc. */
5327 break;
5331 o->output_section->reloc_count += o->reloc_count;
5334 /* Write out the modified section contents. */
5335 if (! bfd_set_section_contents (output_bfd, o->output_section,
5336 contents, (file_ptr) o->output_offset,
5337 (o->_cooked_size != 0
5338 ? o->_cooked_size
5339 : o->_raw_size)))
5340 return false;
5343 obj_coff_keep_syms (input_bfd) = keep_syms;
5345 if (! finfo->info->keep_memory)
5347 if (! _bfd_coff_free_symbols (input_bfd))
5348 return false;
5351 return true;
5354 #undef N_TMASK
5355 #undef N_BTSHFT
5357 /* Write out a non-XCOFF global symbol. */
5360 static boolean
5361 xcoff_write_global_symbol (h, inf)
5362 struct xcoff_link_hash_entry *h;
5363 PTR inf;
5365 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
5366 bfd *output_bfd;
5367 bfd_byte *outsym;
5368 struct internal_syment isym;
5369 union internal_auxent aux;
5370 boolean result;
5371 file_ptr pos;
5372 bfd_size_type amt;
5374 output_bfd = finfo->output_bfd;
5375 outsym = finfo->outsyms;
5377 /* If this symbol was garbage collected, just skip it. */
5378 if (xcoff_hash_table (finfo->info)->gc
5379 && (h->flags & XCOFF_MARK) == 0)
5380 return true;
5382 /* If we need a .loader section entry, write it out. */
5383 if (h->ldsym != NULL)
5385 struct internal_ldsym *ldsym;
5386 bfd *impbfd;
5388 ldsym = h->ldsym;
5390 if (h->root.type == bfd_link_hash_undefined
5391 || h->root.type == bfd_link_hash_undefweak)
5394 ldsym->l_value = 0;
5395 ldsym->l_scnum = N_UNDEF;
5396 ldsym->l_smtype = XTY_ER;
5397 impbfd = h->root.u.undef.abfd;
5400 else if (h->root.type == bfd_link_hash_defined
5401 || h->root.type == bfd_link_hash_defweak)
5404 asection *sec;
5406 sec = h->root.u.def.section;
5407 ldsym->l_value = (sec->output_section->vma
5408 + sec->output_offset
5409 + h->root.u.def.value);
5410 ldsym->l_scnum = sec->output_section->target_index;
5411 ldsym->l_smtype = XTY_SD;
5412 impbfd = sec->owner;
5415 else
5416 abort ();
5418 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5419 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5420 || (h->flags & XCOFF_IMPORT) != 0)
5422 /* Clear l_smtype
5423 Import symbols are defined so the check above will make
5424 the l_smtype XTY_SD. But this is not correct, it should
5425 be cleared. */
5426 ldsym->l_smtype |= L_IMPORT;
5429 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5430 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5431 || (h->flags & XCOFF_EXPORT) != 0)
5433 ldsym->l_smtype |= L_EXPORT;
5436 if ((h->flags & XCOFF_ENTRY) != 0)
5438 ldsym->l_smtype |= L_ENTRY;
5441 if ((h->flags & XCOFF_RTINIT) != 0)
5443 ldsym->l_smtype = XTY_SD;
5446 ldsym->l_smclas = h->smclas;
5448 if (ldsym->l_smtype & L_IMPORT)
5450 if ((h->root.type == bfd_link_hash_defined
5451 || h->root.type == bfd_link_hash_defweak)
5452 && (h->root.u.def.value != 0))
5454 ldsym->l_smclas = XMC_XO;
5456 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5457 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5459 ldsym->l_smclas = XMC_SV3264;
5461 else if (h->flags & XCOFF_SYSCALL32)
5463 ldsym->l_smclas = XMC_SV;
5465 else if (h->flags & XCOFF_SYSCALL64)
5467 ldsym->l_smclas = XMC_SV64;
5471 if (ldsym->l_ifile == -(bfd_size_type) 1)
5473 ldsym->l_ifile = 0;
5475 else if (ldsym->l_ifile == 0)
5477 if ((ldsym->l_smtype & L_IMPORT) == 0)
5479 ldsym->l_ifile = 0;
5481 else if (impbfd == NULL)
5483 ldsym->l_ifile = 0;
5485 else
5487 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5488 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5492 ldsym->l_parm = 0;
5494 BFD_ASSERT (h->ldindx >= 0);
5496 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5497 (finfo->ldsym +
5498 (h->ldindx - 3)
5499 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
5500 h->ldsym = NULL;
5503 /* If this symbol needs global linkage code, write it out. */
5504 if (h->root.type == bfd_link_hash_defined
5505 && (h->root.u.def.section
5506 == xcoff_hash_table (finfo->info)->linkage_section))
5508 bfd_byte *p;
5509 bfd_vma tocoff;
5510 unsigned int i;
5512 p = h->root.u.def.section->contents + h->root.u.def.value;
5514 /* The first instruction in the global linkage code loads a
5515 specific TOC element. */
5516 tocoff = (h->descriptor->toc_section->output_section->vma
5517 + h->descriptor->toc_section->output_offset
5518 - xcoff_data (output_bfd)->toc);
5520 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5522 tocoff += h->descriptor->u.toc_offset;
5526 /* The first instruction in the glink code needs to be
5527 cooked to to hold the correct offset in the toc. The
5528 rest are just output raw. */
5529 bfd_put_32 (output_bfd,
5530 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5532 /* Start with i == 1 to get past the first instruction done above
5533 The /4 is because the glink code is in bytes and we are going
5534 4 at a pop. */
5535 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5537 bfd_put_32 (output_bfd,
5538 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5539 &p[4 * i]);
5543 /* If we created a TOC entry for this symbol, write out the required
5544 relocs. */
5545 if ((h->flags & XCOFF_SET_TOC) != 0)
5547 asection *tocsec;
5548 asection *osec;
5549 int oindx;
5550 struct internal_reloc *irel;
5551 struct internal_ldrel ldrel;
5552 struct internal_syment irsym;
5553 union internal_auxent iraux;
5555 tocsec = h->toc_section;
5556 osec = tocsec->output_section;
5557 oindx = osec->target_index;
5558 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5559 irel->r_vaddr = (osec->vma
5560 + tocsec->output_offset
5561 + h->u.toc_offset);
5564 if (h->indx >= 0)
5566 irel->r_symndx = h->indx;
5568 else
5570 h->indx = -2;
5571 irel->r_symndx = obj_raw_syment_count (output_bfd);
5574 BFD_ASSERT (h->ldindx >= 0);
5576 /* Initialize the aux union here instead of closer to when it is
5577 written out below because the length of the csect depends on
5578 whether the output is 32 or 64 bit. */
5579 memset (&iraux, 0, sizeof iraux);
5580 iraux.x_csect.x_smtyp = XTY_SD;
5581 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below */
5582 iraux.x_csect.x_smclas = XMC_TC;
5584 /* 32 bit uses a 32 bit R_POS to do the relocations
5585 64 bit uses a 64 bit R_POS to do the relocations
5587 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5589 Which one is determined by the backend. */
5590 if (bfd_xcoff_is_xcoff64 (output_bfd))
5592 irel->r_size = 63;
5593 iraux.x_csect.x_scnlen.l = 8;
5595 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5597 irel->r_size = 31;
5598 iraux.x_csect.x_scnlen.l = 4;
5600 else
5602 return false;
5604 irel->r_type = R_POS;
5605 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5606 ++osec->reloc_count;
5608 ldrel.l_vaddr = irel->r_vaddr;
5609 ldrel.l_symndx = h->ldindx;
5610 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
5611 ldrel.l_rsecnm = oindx;
5612 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5613 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5615 /* We need to emit a symbol to define a csect which holds
5616 the reloc. */
5617 if (finfo->info->strip != strip_all)
5620 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
5621 &irsym, h->root.root.string);
5622 if (false == result)
5624 return false;
5627 irsym.n_value = irel->r_vaddr;
5628 irsym.n_scnum = osec->target_index;
5629 irsym.n_sclass = C_HIDEXT;
5630 irsym.n_type = T_NULL;
5631 irsym.n_numaux = 1;
5633 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5634 outsym += bfd_coff_symesz (output_bfd);
5636 /* note : iraux is initialized above */
5637 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5638 0, 1, (PTR) outsym);
5639 outsym += bfd_coff_auxesz (output_bfd);
5641 if (h->indx >= 0)
5643 /* We aren't going to write out the symbols below, so we
5644 need to write them out now. */
5645 pos = obj_sym_filepos (output_bfd);
5646 pos += (obj_raw_syment_count (output_bfd)
5647 * bfd_coff_symesz (output_bfd));
5648 amt = outsym - finfo->outsyms;
5649 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5650 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5651 return false;
5652 obj_raw_syment_count (output_bfd) +=
5653 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5655 outsym = finfo->outsyms;
5660 /* If this symbol is a specially defined function descriptor, write
5661 it out. The first word is the address of the function code
5662 itself, the second word is the address of the TOC, and the third
5663 word is zero.
5665 32 bit vs 64 bit
5666 The addresses for the 32 bit will take 4 bytes and the addresses
5667 for 64 bit will take 8 bytes. Similar for the relocs. This type
5668 of logic was also done above to create a TOC entry in
5669 xcoff_write_global_symbol. */
5670 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5671 && h->root.type == bfd_link_hash_defined
5672 && (h->root.u.def.section
5673 == xcoff_hash_table (finfo->info)->descriptor_section))
5675 asection *sec;
5676 asection *osec;
5677 int oindx;
5678 bfd_byte *p;
5679 struct xcoff_link_hash_entry *hentry;
5680 asection *esec;
5681 struct internal_reloc *irel;
5682 struct internal_ldrel ldrel;
5683 asection *tsec;
5684 unsigned int reloc_size, byte_size;
5686 if (bfd_xcoff_is_xcoff64 (output_bfd))
5688 reloc_size = 63;
5689 byte_size = 8;
5691 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5693 reloc_size = 31;
5694 byte_size = 4;
5696 else
5698 return false;
5701 sec = h->root.u.def.section;
5702 osec = sec->output_section;
5703 oindx = osec->target_index;
5704 p = sec->contents + h->root.u.def.value;
5706 hentry = h->descriptor;
5707 BFD_ASSERT (hentry != NULL
5708 && (hentry->root.type == bfd_link_hash_defined
5709 || hentry->root.type == bfd_link_hash_defweak));
5710 esec = hentry->root.u.def.section;
5712 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5713 irel->r_vaddr = (osec->vma
5714 + sec->output_offset
5715 + h->root.u.def.value);
5716 irel->r_symndx = esec->output_section->target_index;
5717 irel->r_type = R_POS;
5718 irel->r_size = reloc_size;
5719 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5720 ++osec->reloc_count;
5722 ldrel.l_vaddr = irel->r_vaddr;
5723 if (strcmp (esec->output_section->name, ".text") == 0)
5724 ldrel.l_symndx = 0;
5725 else if (strcmp (esec->output_section->name, ".data") == 0)
5726 ldrel.l_symndx = 1;
5727 else if (strcmp (esec->output_section->name, ".bss") == 0)
5728 ldrel.l_symndx = 2;
5729 else
5731 (*_bfd_error_handler)
5732 (_("%s: loader reloc in unrecognized section `%s'"),
5733 bfd_get_filename (output_bfd),
5734 esec->output_section->name);
5735 bfd_set_error (bfd_error_nonrepresentable_section);
5736 return false;
5738 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5739 ldrel.l_rsecnm = oindx;
5740 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5741 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5743 /* There are three items to write out,
5744 the address of the code
5745 the address of the toc anchor
5746 the environment pointer.
5747 We are ignoring the environment pointer. So set it to zero. */
5748 if (bfd_xcoff_is_xcoff64 (output_bfd))
5750 bfd_put_64 (output_bfd,
5751 (esec->output_section->vma + esec->output_offset
5752 + hentry->root.u.def.value),
5754 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5755 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5757 else
5759 /* 32 bit backend
5760 This logic was already called above so the error case where
5761 the backend is neither has already been checked. */
5762 bfd_put_32 (output_bfd,
5763 (esec->output_section->vma + esec->output_offset
5764 + hentry->root.u.def.value),
5766 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5767 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5770 tsec = coff_section_from_bfd_index (output_bfd,
5771 xcoff_data (output_bfd)->sntoc);
5773 ++irel;
5774 irel->r_vaddr = (osec->vma
5775 + sec->output_offset
5776 + h->root.u.def.value
5777 + byte_size);
5778 irel->r_symndx = tsec->output_section->target_index;
5779 irel->r_type = R_POS;
5780 irel->r_size = reloc_size;
5781 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5782 ++osec->reloc_count;
5784 ldrel.l_vaddr = irel->r_vaddr;
5785 if (strcmp (tsec->output_section->name, ".text") == 0)
5786 ldrel.l_symndx = 0;
5787 else if (strcmp (tsec->output_section->name, ".data") == 0)
5788 ldrel.l_symndx = 1;
5789 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5790 ldrel.l_symndx = 2;
5791 else
5793 (*_bfd_error_handler)
5794 (_("%s: loader reloc in unrecognized section `%s'"),
5795 bfd_get_filename (output_bfd),
5796 tsec->output_section->name);
5797 bfd_set_error (bfd_error_nonrepresentable_section);
5798 return false;
5800 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5801 ldrel.l_rsecnm = oindx;
5802 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5803 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5806 if (h->indx >= 0 || finfo->info->strip == strip_all)
5808 BFD_ASSERT (outsym == finfo->outsyms);
5809 return true;
5812 if (h->indx != -2
5813 && (finfo->info->strip == strip_all
5814 || (finfo->info->strip == strip_some
5815 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5816 false, false) == NULL)))
5818 BFD_ASSERT (outsym == finfo->outsyms);
5819 return true;
5822 if (h->indx != -2
5823 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5825 BFD_ASSERT (outsym == finfo->outsyms);
5826 return true;
5829 memset (&aux, 0, sizeof aux);
5831 h->indx = obj_raw_syment_count (output_bfd);
5833 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5834 h->root.root.string);
5835 if (false == result)
5837 return false;
5840 if (h->root.type == bfd_link_hash_undefined
5841 || h->root.type == bfd_link_hash_undefweak)
5843 isym.n_value = 0;
5844 isym.n_scnum = N_UNDEF;
5845 isym.n_sclass = C_EXT;
5846 aux.x_csect.x_smtyp = XTY_ER;
5848 else if ((h->root.type == bfd_link_hash_defined
5849 || h->root.type == bfd_link_hash_defweak)
5850 && h->smclas == XMC_XO)
5852 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5853 isym.n_value = h->root.u.def.value;
5854 isym.n_scnum = N_UNDEF;
5855 isym.n_sclass = C_EXT;
5856 aux.x_csect.x_smtyp = XTY_ER;
5858 else if (h->root.type == bfd_link_hash_defined
5859 || h->root.type == bfd_link_hash_defweak)
5861 struct xcoff_link_size_list *l;
5863 isym.n_value = (h->root.u.def.section->output_section->vma
5864 + h->root.u.def.section->output_offset
5865 + h->root.u.def.value);
5866 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5867 isym.n_sclass = C_HIDEXT;
5868 aux.x_csect.x_smtyp = XTY_SD;
5870 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5872 for (l = xcoff_hash_table (finfo->info)->size_list;
5873 l != NULL;
5874 l = l->next)
5876 if (l->h == h)
5878 aux.x_csect.x_scnlen.l = l->size;
5879 break;
5884 else if (h->root.type == bfd_link_hash_common)
5886 isym.n_value = (h->root.u.c.p->section->output_section->vma
5887 + h->root.u.c.p->section->output_offset);
5888 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5889 isym.n_sclass = C_EXT;
5890 aux.x_csect.x_smtyp = XTY_CM;
5891 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5893 else
5894 abort ();
5896 isym.n_type = T_NULL;
5897 isym.n_numaux = 1;
5899 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5900 outsym += bfd_coff_symesz (output_bfd);
5902 aux.x_csect.x_smclas = h->smclas;
5903 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5904 (PTR) outsym);
5905 outsym += bfd_coff_auxesz (output_bfd);
5907 if ((h->root.type == bfd_link_hash_defined
5908 || h->root.type == bfd_link_hash_defweak)
5909 && h->smclas != XMC_XO)
5911 /* We just output an SD symbol. Now output an LD symbol. */
5913 h->indx += 2;
5915 isym.n_sclass = C_EXT;
5916 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5917 outsym += bfd_coff_symesz (output_bfd);
5919 aux.x_csect.x_smtyp = XTY_LD;
5920 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5921 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5922 (PTR) outsym);
5923 outsym += bfd_coff_auxesz (output_bfd);
5926 pos = obj_sym_filepos (output_bfd);
5927 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5928 amt = outsym - finfo->outsyms;
5929 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5930 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5931 return false;
5932 obj_raw_syment_count (output_bfd) +=
5933 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5935 return true;
5938 /* Handle a link order which is supposed to generate a reloc. */
5940 static boolean
5941 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5942 bfd *output_bfd;
5943 struct xcoff_final_link_info *finfo;
5944 asection *output_section;
5945 struct bfd_link_order *link_order;
5947 reloc_howto_type *howto;
5948 struct xcoff_link_hash_entry *h;
5949 asection *hsec;
5950 bfd_vma hval;
5951 bfd_vma addend;
5952 struct internal_reloc *irel;
5953 struct xcoff_link_hash_entry **rel_hash_ptr;
5954 struct internal_ldrel ldrel;
5956 if (link_order->type == bfd_section_reloc_link_order)
5958 /* We need to somehow locate a symbol in the right section. The
5959 symbol must either have a value of zero, or we must adjust
5960 the addend by the value of the symbol. FIXME: Write this
5961 when we need it. The old linker couldn't handle this anyhow. */
5962 abort ();
5965 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5966 if (howto == NULL)
5968 bfd_set_error (bfd_error_bad_value);
5969 return false;
5972 h = ((struct xcoff_link_hash_entry *)
5973 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5974 link_order->u.reloc.p->u.name,
5975 false, false, true));
5976 if (h == NULL)
5978 if (! ((*finfo->info->callbacks->unattached_reloc)
5979 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5980 (asection *) NULL, (bfd_vma) 0)))
5981 return false;
5982 return true;
5985 if (h->root.type == bfd_link_hash_common)
5987 hsec = h->root.u.c.p->section;
5988 hval = 0;
5990 else if (h->root.type == bfd_link_hash_defined
5991 || h->root.type == bfd_link_hash_defweak)
5993 hsec = h->root.u.def.section;
5994 hval = h->root.u.def.value;
5996 else
5998 hsec = NULL;
5999 hval = 0;
6002 addend = link_order->u.reloc.p->addend;
6003 if (hsec != NULL)
6004 addend += (hsec->output_section->vma
6005 + hsec->output_offset
6006 + hval);
6008 if (addend != 0)
6010 bfd_size_type size;
6011 bfd_byte *buf;
6012 bfd_reloc_status_type rstat;
6013 boolean ok;
6015 size = bfd_get_reloc_size (howto);
6016 buf = (bfd_byte *) bfd_zmalloc (size);
6017 if (buf == NULL)
6018 return false;
6020 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6021 switch (rstat)
6023 case bfd_reloc_ok:
6024 break;
6025 default:
6026 case bfd_reloc_outofrange:
6027 abort ();
6028 case bfd_reloc_overflow:
6029 if (! ((*finfo->info->callbacks->reloc_overflow)
6030 (finfo->info, link_order->u.reloc.p->u.name,
6031 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6032 (bfd_vma) 0)))
6034 free (buf);
6035 return false;
6037 break;
6039 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6040 (file_ptr) link_order->offset, size);
6041 free (buf);
6042 if (! ok)
6043 return false;
6046 /* Store the reloc information in the right place. It will get
6047 swapped and written out at the end of the final_link routine. */
6049 irel = (finfo->section_info[output_section->target_index].relocs
6050 + output_section->reloc_count);
6051 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6052 + output_section->reloc_count);
6054 memset (irel, 0, sizeof (struct internal_reloc));
6055 *rel_hash_ptr = NULL;
6057 irel->r_vaddr = output_section->vma + link_order->offset;
6059 if (h->indx >= 0)
6060 irel->r_symndx = h->indx;
6061 else
6063 /* Set the index to -2 to force this symbol to get written out. */
6064 h->indx = -2;
6065 *rel_hash_ptr = h;
6066 irel->r_symndx = 0;
6069 irel->r_type = howto->type;
6070 irel->r_size = howto->bitsize - 1;
6071 if (howto->complain_on_overflow == complain_overflow_signed)
6072 irel->r_size |= 0x80;
6074 ++output_section->reloc_count;
6076 /* Now output the reloc to the .loader section. */
6078 ldrel.l_vaddr = irel->r_vaddr;
6080 if (hsec != NULL)
6082 const char *secname;
6084 secname = hsec->output_section->name;
6086 if (strcmp (secname, ".text") == 0)
6087 ldrel.l_symndx = 0;
6088 else if (strcmp (secname, ".data") == 0)
6089 ldrel.l_symndx = 1;
6090 else if (strcmp (secname, ".bss") == 0)
6091 ldrel.l_symndx = 2;
6092 else
6094 (*_bfd_error_handler)
6095 (_("%s: loader reloc in unrecognized section `%s'"),
6096 bfd_get_filename (output_bfd), secname);
6097 bfd_set_error (bfd_error_nonrepresentable_section);
6098 return false;
6101 else
6103 if (h->ldindx < 0)
6105 (*_bfd_error_handler)
6106 (_("%s: `%s' in loader reloc but not loader sym"),
6107 bfd_get_filename (output_bfd),
6108 h->root.root.string);
6109 bfd_set_error (bfd_error_bad_value);
6110 return false;
6112 ldrel.l_symndx = h->ldindx;
6115 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6116 ldrel.l_rsecnm = output_section->target_index;
6117 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6118 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
6120 return true;
6123 /* Sort relocs by VMA. This is called via qsort. */
6125 static int
6126 xcoff_sort_relocs (p1, p2)
6127 const PTR p1;
6128 const PTR p2;
6130 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6131 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6133 if (r1->r_vaddr > r2->r_vaddr)
6134 return 1;
6135 else if (r1->r_vaddr < r2->r_vaddr)
6136 return -1;
6137 else
6138 return 0;