* arm-dis.c: Correct top-level comment.
[binutils.git] / bfd / xcofflink.c
blob8142e168331334dded1312055dfbdb984bc52504
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 bfd_boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
119 static struct internal_reloc *xcoff_read_internal_relocs
120 PARAMS ((bfd *, asection *, bfd_boolean, bfd_byte *, bfd_boolean,
121 struct internal_reloc *));
122 static bfd_boolean xcoff_link_add_object_symbols
123 PARAMS ((bfd *, struct bfd_link_info *));
124 static bfd_boolean xcoff_link_check_archive_element
125 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
126 static bfd_boolean xcoff_link_check_ar_symbols
127 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
128 static bfd_boolean xcoff_link_check_dynamic_ar_symbols
129 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
130 static bfd_size_type xcoff_find_reloc
131 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
132 static bfd_boolean xcoff_link_add_symbols
133 PARAMS ((bfd *, struct bfd_link_info *));
134 static bfd_boolean xcoff_link_add_dynamic_symbols
135 PARAMS ((bfd *, struct bfd_link_info *));
136 static bfd_boolean xcoff_mark_symbol
137 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
138 static bfd_boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
139 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
140 static bfd_boolean xcoff_build_ldsyms
141 PARAMS ((struct xcoff_link_hash_entry *, PTR));
142 static bfd_boolean xcoff_link_input_bfd
143 PARAMS ((struct xcoff_final_link_info *, bfd *));
144 static bfd_boolean xcoff_write_global_symbol
145 PARAMS ((struct xcoff_link_hash_entry *, PTR));
146 static bfd_boolean xcoff_reloc_link_order
147 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
148 struct bfd_link_order *));
149 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
152 /* Routines to read XCOFF dynamic information. This don't really
153 belong here, but we already have the ldsym manipulation routines
154 here. */
156 /* Read the contents of a section. */
158 static bfd_boolean
159 xcoff_get_section_contents (abfd, sec)
160 bfd *abfd;
161 asection *sec;
164 if (coff_section_data (abfd, sec) == NULL)
166 bfd_size_type amt = sizeof (struct coff_section_tdata);
167 sec->used_by_bfd = bfd_zalloc (abfd, amt);
168 if (sec->used_by_bfd == NULL)
169 return FALSE;
172 if (coff_section_data (abfd, sec)->contents == NULL)
174 bfd_byte *contents;
175 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
177 if (contents != NULL)
178 free (contents);
179 return FALSE;
181 coff_section_data (abfd, sec)->contents = contents;
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_malloc (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 free (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;
516 /* Free a XCOFF link hash table. */
518 void
519 _bfd_xcoff_bfd_link_hash_table_free (hash)
520 struct bfd_link_hash_table *hash;
522 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
524 _bfd_stringtab_free (ret->debug_strtab);
525 bfd_hash_table_free (&ret->root.table);
526 free (ret);
529 /* Read internal relocs for an XCOFF csect. This is a wrapper around
530 _bfd_coff_read_internal_relocs which tries to take advantage of any
531 relocs which may have been cached for the enclosing section. */
533 static struct internal_reloc *
534 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
535 require_internal, internal_relocs)
536 bfd *abfd;
537 asection *sec;
538 bfd_boolean cache;
539 bfd_byte *external_relocs;
540 bfd_boolean require_internal;
541 struct internal_reloc *internal_relocs;
544 if (coff_section_data (abfd, sec) != NULL
545 && coff_section_data (abfd, sec)->relocs == NULL
546 && xcoff_section_data (abfd, sec) != NULL)
548 asection *enclosing;
550 enclosing = xcoff_section_data (abfd, sec)->enclosing;
552 if (enclosing != NULL
553 && (coff_section_data (abfd, enclosing) == NULL
554 || coff_section_data (abfd, enclosing)->relocs == NULL)
555 && cache
556 && enclosing->reloc_count > 0)
558 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
559 external_relocs, FALSE,
560 (struct internal_reloc *) NULL)
561 == NULL)
562 return NULL;
565 if (enclosing != NULL
566 && coff_section_data (abfd, enclosing) != NULL
567 && coff_section_data (abfd, enclosing)->relocs != NULL)
569 size_t off;
571 off = ((sec->rel_filepos - enclosing->rel_filepos)
572 / bfd_coff_relsz (abfd));
574 if (! require_internal)
575 return coff_section_data (abfd, enclosing)->relocs + off;
576 memcpy (internal_relocs,
577 coff_section_data (abfd, enclosing)->relocs + off,
578 sec->reloc_count * sizeof (struct internal_reloc));
579 return internal_relocs;
583 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
584 require_internal, internal_relocs);
587 /* Given an XCOFF BFD, add symbols to the global hash table as
588 appropriate. */
590 bfd_boolean
591 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
592 bfd *abfd;
593 struct bfd_link_info *info;
596 switch (bfd_get_format (abfd))
598 case bfd_object:
599 return xcoff_link_add_object_symbols (abfd, info);
601 case bfd_archive:
602 /* If the archive has a map, do the usual search. We then need
603 to check the archive for dynamic objects, because they may not
604 appear in the archive map even though they should, perhaps, be
605 included. If the archive has no map, we just consider each object
606 file in turn, since that apparently is what the AIX native linker
607 does. */
608 if (bfd_has_map (abfd))
610 if (! (_bfd_generic_link_add_archive_symbols
611 (abfd, info, xcoff_link_check_archive_element)))
612 return FALSE;
616 bfd *member;
618 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
619 while (member != NULL)
621 if (bfd_check_format (member, bfd_object)
622 && (info->hash->creator == member->xvec)
623 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
625 bfd_boolean needed;
627 if (! xcoff_link_check_archive_element (member, info,
628 &needed))
629 return FALSE;
630 if (needed)
631 member->archive_pass = -1;
633 member = bfd_openr_next_archived_file (abfd, member);
637 return TRUE;
639 default:
640 bfd_set_error (bfd_error_wrong_format);
641 return FALSE;
645 /* Add symbols from an XCOFF object file. */
647 static bfd_boolean
648 xcoff_link_add_object_symbols (abfd, info)
649 bfd *abfd;
650 struct bfd_link_info *info;
653 if (! _bfd_coff_get_external_symbols (abfd))
654 return FALSE;
655 if (! xcoff_link_add_symbols (abfd, info))
656 return FALSE;
657 if (! info->keep_memory)
659 if (! _bfd_coff_free_symbols (abfd))
660 return FALSE;
662 return TRUE;
665 /* Check a single archive element to see if we need to include it in
666 the link. *PNEEDED is set according to whether this element is
667 needed in the link or not. This is called via
668 _bfd_generic_link_add_archive_symbols. */
670 static bfd_boolean
671 xcoff_link_check_archive_element (abfd, info, pneeded)
672 bfd *abfd;
673 struct bfd_link_info *info;
674 bfd_boolean *pneeded;
677 if (! _bfd_coff_get_external_symbols (abfd))
678 return FALSE;
680 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
681 return FALSE;
683 if (*pneeded)
685 if (! xcoff_link_add_symbols (abfd, info))
686 return FALSE;
689 if (! info->keep_memory || ! *pneeded)
691 if (! _bfd_coff_free_symbols (abfd))
692 return FALSE;
695 return TRUE;
698 /* Look through the symbols to see if this object file should be
699 included in the link. */
701 static bfd_boolean
702 xcoff_link_check_ar_symbols (abfd, info, pneeded)
703 bfd *abfd;
704 struct bfd_link_info *info;
705 bfd_boolean *pneeded;
707 bfd_size_type symesz;
708 bfd_byte *esym;
709 bfd_byte *esym_end;
711 *pneeded = FALSE;
713 if ((abfd->flags & DYNAMIC) != 0
714 && ! info->static_link
715 && info->hash->creator == abfd->xvec)
716 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
718 symesz = bfd_coff_symesz (abfd);
719 esym = (bfd_byte *) obj_coff_external_syms (abfd);
720 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
721 while (esym < esym_end)
723 struct internal_syment sym;
725 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
727 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
729 const char *name;
730 char buf[SYMNMLEN + 1];
731 struct bfd_link_hash_entry *h;
733 /* This symbol is externally visible, and is defined by this
734 object file. */
736 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
738 if (name == NULL)
739 return FALSE;
740 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
742 /* We are only interested in symbols that are currently
743 undefined. If a symbol is currently known to be common,
744 XCOFF linkers do not bring in an object file which
745 defines it. We also don't bring in symbols to satisfy
746 undefined references in shared objects. */
747 if (h != (struct bfd_link_hash_entry *) NULL
748 && h->type == bfd_link_hash_undefined
749 && (info->hash->creator != abfd->xvec
750 || (((struct xcoff_link_hash_entry *) h)->flags
751 & XCOFF_DEF_DYNAMIC) == 0))
753 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
754 return FALSE;
755 *pneeded = TRUE;
756 return TRUE;
760 esym += (sym.n_numaux + 1) * symesz;
763 /* We do not need this object file. */
764 return TRUE;
767 /* Look through the loader symbols to see if this dynamic object
768 should be included in the link. The native linker uses the loader
769 symbols, not the normal symbol table, so we do too. */
771 static bfd_boolean
772 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
773 bfd *abfd;
774 struct bfd_link_info *info;
775 bfd_boolean *pneeded;
777 asection *lsec;
778 bfd_byte *contents;
779 struct internal_ldhdr ldhdr;
780 const char *strings;
781 bfd_byte *elsym, *elsymend;
783 *pneeded = FALSE;
785 lsec = bfd_get_section_by_name (abfd, ".loader");
786 if (lsec == NULL)
788 /* There are no symbols, so don't try to include it. */
789 return TRUE;
792 if (! xcoff_get_section_contents (abfd, lsec))
793 return FALSE;
794 contents = coff_section_data (abfd, lsec)->contents;
796 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
798 strings = (char *) contents + ldhdr.l_stoff;
800 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
802 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
803 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
805 struct internal_ldsym ldsym;
806 char nambuf[SYMNMLEN + 1];
807 const char *name;
808 struct bfd_link_hash_entry *h;
810 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
812 /* We are only interested in exported symbols. */
813 if ((ldsym.l_smtype & L_EXPORT) == 0)
814 continue;
816 if (ldsym._l._l_l._l_zeroes == 0)
817 name = strings + ldsym._l._l_l._l_offset;
818 else
820 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
821 nambuf[SYMNMLEN] = '\0';
822 name = nambuf;
825 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
827 /* We are only interested in symbols that are currently
828 undefined. At this point we know that we are using an XCOFF
829 hash table. */
830 if (h != NULL
831 && h->type == bfd_link_hash_undefined
832 && (((struct xcoff_link_hash_entry *) h)->flags
833 & XCOFF_DEF_DYNAMIC) == 0)
835 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
836 return FALSE;
837 *pneeded = TRUE;
838 return TRUE;
842 /* We do not need this shared object. */
844 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
846 free (coff_section_data (abfd, lsec)->contents);
847 coff_section_data (abfd, lsec)->contents = NULL;
850 return TRUE;
853 /* Returns the index of reloc in RELOCS with the least address greater
854 than or equal to ADDRESS. The relocs are sorted by address. */
856 static bfd_size_type
857 xcoff_find_reloc (relocs, count, address)
858 struct internal_reloc *relocs;
859 bfd_size_type count;
860 bfd_vma address;
862 bfd_size_type min, max, this;
864 if (count < 2)
866 if (count == 1 && relocs[0].r_vaddr < address)
867 return 1;
868 else
869 return 0;
872 min = 0;
873 max = count;
875 /* Do a binary search over (min,max]. */
876 while (min + 1 < max)
878 bfd_vma raddr;
880 this = (max + min) / 2;
881 raddr = relocs[this].r_vaddr;
882 if (raddr > address)
883 max = this;
884 else if (raddr < address)
885 min = this;
886 else
888 min = this;
889 break;
893 if (relocs[min].r_vaddr < address)
894 return min + 1;
896 while (min > 0
897 && relocs[min - 1].r_vaddr == address)
898 --min;
900 return min;
904 /* xcoff_link_create_extra_sections
906 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
908 static bfd_boolean
909 xcoff_link_create_extra_sections(bfd * abfd, struct bfd_link_info *info)
912 bfd_boolean return_value = FALSE;
914 if (info->hash->creator == abfd->xvec)
917 /* We need to build a .loader section, so we do it here. This
918 won't work if we're producing an XCOFF output file with no
919 XCOFF input files. FIXME. */
921 if (xcoff_hash_table (info)->loader_section == NULL)
923 asection *lsec;
925 lsec = bfd_make_section_anyway (abfd, ".loader");
926 if (lsec == NULL)
928 goto end_return;
930 xcoff_hash_table (info)->loader_section = lsec;
931 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
934 /* Likewise for the linkage section. */
935 if (xcoff_hash_table (info)->linkage_section == NULL)
937 asection *lsec;
939 lsec = bfd_make_section_anyway (abfd, ".gl");
940 if (lsec == NULL)
942 goto end_return;
945 xcoff_hash_table (info)->linkage_section = lsec;
946 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
947 | SEC_IN_MEMORY);
948 lsec->alignment_power = 2;
951 /* Likewise for the TOC section. */
952 if (xcoff_hash_table (info)->toc_section == NULL)
954 asection *tsec;
956 tsec = bfd_make_section_anyway (abfd, ".tc");
957 if (tsec == NULL)
959 goto end_return;
962 xcoff_hash_table (info)->toc_section = tsec;
963 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
964 | SEC_IN_MEMORY);
965 tsec->alignment_power = 2;
968 /* Likewise for the descriptor section. */
969 if (xcoff_hash_table (info)->descriptor_section == NULL)
971 asection *dsec;
973 dsec = bfd_make_section_anyway (abfd, ".ds");
974 if (dsec == NULL)
976 goto end_return;
979 xcoff_hash_table (info)->descriptor_section = dsec;
980 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
981 | SEC_IN_MEMORY);
982 dsec->alignment_power = 2;
985 /* Likewise for the .debug section. */
986 if (xcoff_hash_table (info)->debug_section == NULL
987 && info->strip != strip_all)
989 asection *dsec;
991 dsec = bfd_make_section_anyway (abfd, ".debug");
992 if (dsec == NULL)
994 goto end_return;
996 xcoff_hash_table (info)->debug_section = dsec;
997 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1001 return_value = TRUE;
1003 end_return:
1005 return return_value;
1008 /* Add all the symbols from an object file to the hash table.
1010 XCOFF is a weird format. A normal XCOFF .o files will have three
1011 COFF sections--.text, .data, and .bss--but each COFF section will
1012 contain many csects. These csects are described in the symbol
1013 table. From the linker's point of view, each csect must be
1014 considered a section in its own right. For example, a TOC entry is
1015 handled as a small XMC_TC csect. The linker must be able to merge
1016 different TOC entries together, which means that it must be able to
1017 extract the XMC_TC csects from the .data section of the input .o
1018 file.
1020 From the point of view of our linker, this is, of course, a hideous
1021 nightmare. We cope by actually creating sections for each csect,
1022 and discarding the original sections. We then have to handle the
1023 relocation entries carefully, since the only way to tell which
1024 csect they belong to is to examine the address. */
1026 static bfd_boolean
1027 xcoff_link_add_symbols (abfd, info)
1028 bfd *abfd;
1029 struct bfd_link_info *info;
1031 unsigned int n_tmask;
1032 unsigned int n_btshft;
1033 bfd_boolean default_copy;
1034 bfd_size_type symcount;
1035 struct xcoff_link_hash_entry **sym_hash;
1036 asection **csect_cache;
1037 bfd_size_type linesz;
1038 asection *o;
1039 asection *last_real;
1040 bfd_boolean keep_syms;
1041 asection *csect;
1042 unsigned int csect_index;
1043 asection *first_csect;
1044 bfd_size_type symesz;
1045 bfd_byte *esym;
1046 bfd_byte *esym_end;
1047 struct reloc_info_struct
1049 struct internal_reloc *relocs;
1050 asection **csects;
1051 bfd_byte *linenos;
1052 } *reloc_info = NULL;
1053 bfd_size_type amt;
1055 keep_syms = obj_coff_keep_syms (abfd);
1057 if ((abfd->flags & DYNAMIC) != 0
1058 && ! info->static_link)
1060 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1061 return FALSE;
1064 /* create the loader, toc, gl, ds and debug sections, if needed */
1065 if (! xcoff_link_create_extra_sections (abfd, info))
1066 goto error_return;
1068 if ((abfd->flags & DYNAMIC) != 0
1069 && ! info->static_link)
1070 return TRUE;
1072 n_tmask = coff_data (abfd)->local_n_tmask;
1073 n_btshft = coff_data (abfd)->local_n_btshft;
1075 /* Define macros so that ISFCN, et. al., macros work correctly. */
1076 #define N_TMASK n_tmask
1077 #define N_BTSHFT n_btshft
1079 if (info->keep_memory)
1080 default_copy = FALSE;
1081 else
1082 default_copy = TRUE;
1084 symcount = obj_raw_syment_count (abfd);
1086 /* We keep a list of the linker hash table entries that correspond
1087 to each external symbol. */
1088 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1089 sym_hash = (struct xcoff_link_hash_entry **) bfd_zalloc (abfd, amt);
1090 if (sym_hash == NULL && symcount != 0)
1091 goto error_return;
1092 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1094 /* Because of the weird stuff we are doing with XCOFF csects, we can
1095 not easily determine which section a symbol is in, so we store
1096 the information in the tdata for the input file. */
1097 amt = symcount * sizeof (asection *);
1098 csect_cache = (asection **) bfd_zalloc (abfd, amt);
1099 if (csect_cache == NULL && symcount != 0)
1100 goto error_return;
1101 xcoff_data (abfd)->csects = csect_cache;
1103 /* While splitting sections into csects, we need to assign the
1104 relocs correctly. The relocs and the csects must both be in
1105 order by VMA within a given section, so we handle this by
1106 scanning along the relocs as we process the csects. We index
1107 into reloc_info using the section target_index. */
1108 amt = abfd->section_count + 1;
1109 amt *= sizeof (struct reloc_info_struct);
1110 reloc_info = (struct reloc_info_struct *) bfd_zmalloc (amt);
1111 if (reloc_info == NULL)
1112 goto error_return;
1114 /* Read in the relocs and line numbers for each section. */
1115 linesz = bfd_coff_linesz (abfd);
1116 last_real = NULL;
1117 for (o = abfd->sections; o != NULL; o = o->next)
1120 last_real = o;
1121 if ((o->flags & SEC_RELOC) != 0)
1124 reloc_info[o->target_index].relocs =
1125 xcoff_read_internal_relocs (abfd, o, TRUE, (bfd_byte *) NULL,
1126 FALSE, (struct internal_reloc *) NULL);
1127 amt = o->reloc_count;
1128 amt *= sizeof (asection *);
1129 reloc_info[o->target_index].csects = (asection **) bfd_zmalloc (amt);
1130 if (reloc_info[o->target_index].csects == NULL)
1131 goto error_return;
1134 if ((info->strip == strip_none || info->strip == strip_some)
1135 && o->lineno_count > 0)
1138 bfd_byte *linenos;
1140 amt = linesz * o->lineno_count;
1141 linenos = (bfd_byte *) bfd_malloc (amt);
1142 if (linenos == NULL)
1143 goto error_return;
1144 reloc_info[o->target_index].linenos = linenos;
1145 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1146 || bfd_bread (linenos, amt, abfd) != amt)
1147 goto error_return;
1152 /* Don't let the linker relocation routines discard the symbols. */
1153 obj_coff_keep_syms (abfd) = TRUE;
1155 csect = NULL;
1156 csect_index = 0;
1157 first_csect = NULL;
1159 symesz = bfd_coff_symesz (abfd);
1160 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1161 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1162 esym_end = esym + symcount * symesz;
1164 while (esym < esym_end)
1166 struct internal_syment sym;
1167 union internal_auxent aux;
1168 const char *name;
1169 char buf[SYMNMLEN + 1];
1170 int smtyp;
1171 flagword flags;
1172 asection *section;
1173 bfd_vma value;
1174 struct xcoff_link_hash_entry *set_toc;
1176 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1178 /* In this pass we are only interested in symbols with csect
1179 information. */
1180 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1183 /* Set csect_cache,
1184 Normally csect is a .pr, .rw etc. created in the loop
1185 If C_FILE or first time, handle special
1187 Advance esym, sym_hash, csect_hash ptr's
1188 Keep track of the last_symndx for the current file. */
1189 if (sym.n_sclass == C_FILE && csect != NULL)
1191 xcoff_section_data (abfd, csect)->last_symndx =
1192 ((esym
1193 - (bfd_byte *) obj_coff_external_syms (abfd))
1194 / symesz);
1195 csect = NULL;
1198 if (csect != NULL)
1199 *csect_cache = csect;
1200 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1201 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1202 else
1203 *csect_cache = NULL;
1204 esym += (sym.n_numaux + 1) * symesz;
1205 sym_hash += sym.n_numaux + 1;
1206 csect_cache += sym.n_numaux + 1;
1208 continue;
1211 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1213 if (name == NULL)
1214 goto error_return;
1216 /* If this symbol has line number information attached to it,
1217 and we're not stripping it, count the number of entries and
1218 add them to the count for this csect. In the final link pass
1219 we are going to attach line number information by symbol,
1220 rather than by section, in order to more easily handle
1221 garbage collection. */
1222 if ((info->strip == strip_none || info->strip == strip_some)
1223 && sym.n_numaux > 1
1224 && csect != NULL
1225 && ISFCN (sym.n_type))
1228 union internal_auxent auxlin;
1230 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1231 sym.n_type, sym.n_sclass,
1232 0, sym.n_numaux, (PTR) &auxlin);
1234 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1236 asection *enclosing;
1237 bfd_signed_vma linoff;
1239 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1240 if (enclosing == NULL)
1242 (*_bfd_error_handler)
1243 (_("%B: `%s' has line numbers but no enclosing section"),
1244 abfd, name);
1245 bfd_set_error (bfd_error_bad_value);
1246 goto error_return;
1248 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1249 - enclosing->line_filepos);
1250 /* explicit cast to bfd_signed_vma for compiler */
1251 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1253 struct internal_lineno lin;
1254 bfd_byte *linpstart;
1256 linpstart = (reloc_info[enclosing->target_index].linenos
1257 + linoff);
1258 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1259 if (lin.l_lnno == 0
1260 && ((bfd_size_type) lin.l_addr.l_symndx
1261 == ((esym
1262 - (bfd_byte *) obj_coff_external_syms (abfd))
1263 / symesz)))
1265 bfd_byte *linpend, *linp;
1267 linpend = (reloc_info[enclosing->target_index].linenos
1268 + enclosing->lineno_count * linesz);
1269 for (linp = linpstart + linesz;
1270 linp < linpend;
1271 linp += linesz)
1273 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1274 (PTR) &lin);
1275 if (lin.l_lnno == 0)
1276 break;
1278 csect->lineno_count += (linp - linpstart) / linesz;
1279 /* The setting of line_filepos will only be
1280 useful if all the line number entries for a
1281 csect are contiguous; this only matters for
1282 error reporting. */
1283 if (csect->line_filepos == 0)
1284 csect->line_filepos =
1285 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1291 /* Pick up the csect auxiliary information. */
1293 if (sym.n_numaux == 0)
1295 (*_bfd_error_handler)
1296 (_("%B: class %d symbol `%s' has no aux entries"),
1297 abfd, sym.n_sclass, name);
1298 bfd_set_error (bfd_error_bad_value);
1299 goto error_return;
1302 bfd_coff_swap_aux_in (abfd,
1303 (PTR) (esym + symesz * sym.n_numaux),
1304 sym.n_type, sym.n_sclass,
1305 sym.n_numaux - 1, sym.n_numaux,
1306 (PTR) &aux);
1308 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1310 flags = BSF_GLOBAL;
1311 section = NULL;
1312 value = 0;
1313 set_toc = NULL;
1315 switch (smtyp)
1317 default:
1318 (*_bfd_error_handler)
1319 (_("%B: symbol `%s' has unrecognized csect type %d"),
1320 abfd, name, smtyp);
1321 bfd_set_error (bfd_error_bad_value);
1322 goto error_return;
1324 case XTY_ER:
1325 /* This is an external reference. */
1326 if (sym.n_sclass == C_HIDEXT
1327 || sym.n_scnum != N_UNDEF
1328 || aux.x_csect.x_scnlen.l != 0)
1330 (*_bfd_error_handler)
1331 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1332 abfd, name, sym.n_sclass, sym.n_scnum,
1333 aux.x_csect.x_scnlen.l);
1334 bfd_set_error (bfd_error_bad_value);
1335 goto error_return;
1338 /* An XMC_XO external reference is actually a reference to
1339 an absolute location. */
1340 if (aux.x_csect.x_smclas != XMC_XO)
1341 section = bfd_und_section_ptr;
1342 else
1344 section = bfd_abs_section_ptr;
1345 value = sym.n_value;
1347 break;
1349 case XTY_SD:
1350 /* This is a csect definition. */
1351 if (csect != NULL)
1353 xcoff_section_data (abfd, csect)->last_symndx =
1354 ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1357 csect = NULL;
1358 csect_index = -(unsigned) 1;
1360 /* When we see a TOC anchor, we record the TOC value. */
1361 if (aux.x_csect.x_smclas == XMC_TC0)
1363 if (sym.n_sclass != C_HIDEXT
1364 || aux.x_csect.x_scnlen.l != 0)
1366 (*_bfd_error_handler)
1367 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1368 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1369 bfd_set_error (bfd_error_bad_value);
1370 goto error_return;
1372 xcoff_data (abfd)->toc = sym.n_value;
1375 /* We must merge TOC entries for the same symbol. We can
1376 merge two TOC entries if they are both C_HIDEXT, they
1377 both have the same name, they are both 4 or 8 bytes long, and
1378 they both have a relocation table entry for an external
1379 symbol with the same name. Unfortunately, this means
1380 that we must look through the relocations. Ick.
1382 Logic for 32 bit vs 64 bit.
1383 32 bit has a csect length of 4 for TOC
1384 64 bit has a csect length of 8 for TOC
1386 The conditions to get past the if-check are not that bad.
1387 They are what is used to create the TOC csects in the first
1388 place. */
1389 if (aux.x_csect.x_smclas == XMC_TC
1390 && sym.n_sclass == C_HIDEXT
1391 && info->hash->creator == abfd->xvec
1392 && ((bfd_xcoff_is_xcoff32 (abfd)
1393 && aux.x_csect.x_scnlen.l == 4)
1394 || (bfd_xcoff_is_xcoff64 (abfd)
1395 && aux.x_csect.x_scnlen.l == 8)))
1397 asection *enclosing;
1398 struct internal_reloc *relocs;
1399 bfd_size_type relindx;
1400 struct internal_reloc *rel;
1402 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1403 if (enclosing == NULL)
1404 goto error_return;
1406 relocs = reloc_info[enclosing->target_index].relocs;
1407 amt = enclosing->reloc_count;
1408 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1409 rel = relocs + relindx;
1411 /* 32 bit R_POS r_size is 31
1412 64 bit R_POS r_size is 63 */
1413 if (relindx < enclosing->reloc_count
1414 && rel->r_vaddr == (bfd_vma) sym.n_value
1415 && rel->r_type == R_POS
1416 && ((bfd_xcoff_is_xcoff32 (abfd)
1417 && rel->r_size == 31)
1418 || (bfd_xcoff_is_xcoff64 (abfd)
1419 && rel->r_size == 63)))
1421 bfd_byte *erelsym;
1423 struct internal_syment relsym;
1425 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1426 + rel->r_symndx * symesz);
1427 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1428 if (relsym.n_sclass == C_EXT)
1430 const char *relname;
1431 char relbuf[SYMNMLEN + 1];
1432 bfd_boolean copy;
1433 struct xcoff_link_hash_entry *h;
1435 /* At this point we know that the TOC entry is
1436 for an externally visible symbol. */
1438 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1439 relbuf);
1440 if (relname == NULL)
1441 goto error_return;
1443 /* We only merge TOC entries if the TC name is
1444 the same as the symbol name. This handles
1445 the normal case, but not common cases like
1446 SYM.P4 which gcc generates to store SYM + 4
1447 in the TOC. FIXME. */
1449 if (strcmp (name, relname) == 0)
1451 copy = (! info->keep_memory
1452 || relsym._n._n_n._n_zeroes != 0
1453 || relsym._n._n_n._n_offset == 0);
1454 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1455 relname, TRUE, copy,
1456 FALSE);
1457 if (h == NULL)
1458 goto error_return;
1460 /* At this point h->root.type could be
1461 bfd_link_hash_new. That should be OK,
1462 since we know for sure that we will come
1463 across this symbol as we step through the
1464 file. */
1466 /* We store h in *sym_hash for the
1467 convenience of the relocate_section
1468 function. */
1469 *sym_hash = h;
1471 if (h->toc_section != NULL)
1473 asection **rel_csects;
1475 /* We already have a TOC entry for this
1476 symbol, so we can just ignore this
1477 one. */
1478 rel_csects =
1479 reloc_info[enclosing->target_index].csects;
1480 rel_csects[relindx] = bfd_und_section_ptr;
1481 break;
1484 /* We are about to create a TOC entry for
1485 this symbol. */
1486 set_toc = h;
1487 } /* merge toc reloc */
1488 } /* c_ext */
1489 } /* reloc */
1490 } /* merge toc */
1494 asection *enclosing;
1496 /* We need to create a new section. We get the name from
1497 the csect storage mapping class, so that the linker can
1498 accumulate similar csects together. */
1500 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1501 if (NULL == csect)
1503 goto error_return;
1506 /* The enclosing section is the main section : .data, .text
1507 or .bss that the csect is coming from. */
1508 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1509 if (enclosing == NULL)
1510 goto error_return;
1512 if (! bfd_is_abs_section (enclosing)
1513 && ((bfd_vma) sym.n_value < enclosing->vma
1514 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1515 > enclosing->vma + enclosing->size)))
1517 (*_bfd_error_handler)
1518 (_("%B: csect `%s' not in enclosing section"),
1519 abfd, name);
1520 bfd_set_error (bfd_error_bad_value);
1521 goto error_return;
1523 csect->vma = sym.n_value;
1524 csect->filepos = (enclosing->filepos
1525 + sym.n_value
1526 - enclosing->vma);
1527 csect->size = aux.x_csect.x_scnlen.l;
1528 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1529 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1531 /* Record the enclosing section in the tdata for this new
1532 section. */
1533 amt = sizeof (struct coff_section_tdata);
1534 csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1535 if (csect->used_by_bfd == NULL)
1536 goto error_return;
1537 amt = sizeof (struct xcoff_section_tdata);
1538 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1539 if (coff_section_data (abfd, csect)->tdata == NULL)
1540 goto error_return;
1541 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1542 xcoff_section_data (abfd, csect)->lineno_count =
1543 enclosing->lineno_count;
1545 if (enclosing->owner == abfd)
1547 struct internal_reloc *relocs;
1548 bfd_size_type relindx;
1549 struct internal_reloc *rel;
1550 asection **rel_csect;
1552 relocs = reloc_info[enclosing->target_index].relocs;
1553 amt = enclosing->reloc_count;
1554 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1556 rel = relocs + relindx;
1557 rel_csect = (reloc_info[enclosing->target_index].csects
1558 + relindx);
1560 csect->rel_filepos = (enclosing->rel_filepos
1561 + relindx * bfd_coff_relsz (abfd));
1562 while (relindx < enclosing->reloc_count
1563 && *rel_csect == NULL
1564 && rel->r_vaddr < csect->vma + csect->size)
1567 *rel_csect = csect;
1568 csect->flags |= SEC_RELOC;
1569 ++csect->reloc_count;
1570 ++relindx;
1571 ++rel;
1572 ++rel_csect;
1576 /* There are a number of other fields and section flags
1577 which we do not bother to set. */
1579 csect_index = ((esym
1580 - (bfd_byte *) obj_coff_external_syms (abfd))
1581 / symesz);
1583 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1585 if (first_csect == NULL)
1586 first_csect = csect;
1588 /* If this symbol is C_EXT, we treat it as starting at the
1589 beginning of the newly created section. */
1590 if (sym.n_sclass == C_EXT)
1592 section = csect;
1593 value = 0;
1596 /* If this is a TOC section for a symbol, record it. */
1597 if (set_toc != NULL)
1598 set_toc->toc_section = csect;
1600 break;
1602 case XTY_LD:
1603 /* This is a label definition. The x_scnlen field is the
1604 symbol index of the csect. Usually the XTY_LD symbol will
1605 follow its appropriate XTY_SD symbol. The .set pseudo op can
1606 cause the XTY_LD to not follow the XTY_SD symbol. */
1608 bfd_boolean bad;
1610 bad = FALSE;
1611 if (aux.x_csect.x_scnlen.l < 0
1612 || (aux.x_csect.x_scnlen.l
1613 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1614 bad = TRUE;
1615 if (! bad)
1617 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1618 if (section == NULL
1619 || (section->flags & SEC_HAS_CONTENTS) == 0)
1620 bad = TRUE;
1622 if (bad)
1624 (*_bfd_error_handler)
1625 (_("%B: misplaced XTY_LD `%s'"),
1626 abfd, name);
1627 bfd_set_error (bfd_error_bad_value);
1628 goto error_return;
1630 csect = section;
1631 value = sym.n_value - csect->vma;
1633 break;
1635 case XTY_CM:
1636 /* This is an unitialized csect. We could base the name on
1637 the storage mapping class, but we don't bother except for
1638 an XMC_TD symbol. If this csect is externally visible,
1639 it is a common symbol. We put XMC_TD symbols in sections
1640 named .tocbss, and rely on the linker script to put that
1641 in the TOC area. */
1643 if (csect != NULL)
1645 xcoff_section_data (abfd, csect)->last_symndx =
1646 ((esym
1647 - (bfd_byte *) obj_coff_external_syms (abfd))
1648 / symesz);
1651 if (aux.x_csect.x_smclas == XMC_TD)
1653 /* The linker script puts the .td section in the data
1654 section after the .tc section. */
1655 csect = bfd_make_section_anyway (abfd, ".td");
1658 else
1660 csect = bfd_make_section_anyway (abfd, ".bss");
1662 if (csect == NULL)
1663 goto error_return;
1664 csect->vma = sym.n_value;
1665 csect->size = aux.x_csect.x_scnlen.l;
1666 csect->flags |= SEC_ALLOC;
1667 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1668 /* There are a number of other fields and section flags
1669 which we do not bother to set. */
1671 csect_index = ((esym
1672 - (bfd_byte *) obj_coff_external_syms (abfd))
1673 / symesz);
1675 amt = sizeof (struct coff_section_tdata);
1676 csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1677 if (csect->used_by_bfd == NULL)
1678 goto error_return;
1679 amt = sizeof (struct xcoff_section_tdata);
1680 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1681 if (coff_section_data (abfd, csect)->tdata == NULL)
1682 goto error_return;
1683 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1685 if (first_csect == NULL)
1686 first_csect = csect;
1688 if (sym.n_sclass == C_EXT)
1690 csect->flags |= SEC_IS_COMMON;
1691 csect->size = 0;
1692 section = csect;
1693 value = aux.x_csect.x_scnlen.l;
1696 break;
1699 /* Check for magic symbol names. */
1700 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1701 && aux.x_csect.x_smclas != XMC_TC
1702 && aux.x_csect.x_smclas != XMC_TD)
1705 int i = -1;
1707 if (name[0] == '_')
1709 if (strcmp (name, "_text") == 0)
1710 i = XCOFF_SPECIAL_SECTION_TEXT;
1711 else if (strcmp (name, "_etext") == 0)
1712 i = XCOFF_SPECIAL_SECTION_ETEXT;
1713 else if (strcmp (name, "_data") == 0)
1714 i = XCOFF_SPECIAL_SECTION_DATA;
1715 else if (strcmp (name, "_edata") == 0)
1716 i = XCOFF_SPECIAL_SECTION_EDATA;
1717 else if (strcmp (name, "_end") == 0)
1718 i = XCOFF_SPECIAL_SECTION_END;
1720 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1722 i = XCOFF_SPECIAL_SECTION_END2;
1725 if (i != -1)
1727 xcoff_hash_table (info)->special_sections[i] = csect;
1731 /* Now we have enough information to add the symbol to the
1732 linker hash table. */
1734 if (sym.n_sclass == C_EXT)
1736 bfd_boolean copy;
1738 BFD_ASSERT (section != NULL);
1740 /* We must copy the name into memory if we got it from the
1741 syment itself, rather than the string table. */
1742 copy = default_copy;
1743 if (sym._n._n_n._n_zeroes != 0
1744 || sym._n._n_n._n_offset == 0)
1745 copy = TRUE;
1747 /* The AIX linker appears to only detect multiple symbol
1748 definitions when there is a reference to the symbol. If
1749 a symbol is defined multiple times, and the only
1750 references are from the same object file, the AIX linker
1751 appears to permit it. It does not merge the different
1752 definitions, but handles them independently. On the
1753 other hand, if there is a reference, the linker reports
1754 an error.
1756 This matters because the AIX <net/net_globals.h> header
1757 file actually defines an initialized array, so we have to
1758 actually permit that to work.
1760 Just to make matters even more confusing, the AIX linker
1761 appears to permit multiple symbol definitions whenever
1762 the second definition is in an archive rather than an
1763 object file. This may be a consequence of the manner in
1764 which it handles archives: I think it may load the entire
1765 archive in as separate csects, and then let garbage
1766 collection discard symbols.
1768 We also have to handle the case of statically linking a
1769 shared object, which will cause symbol redefinitions,
1770 although this is an easier case to detect. */
1772 if (info->hash->creator == abfd->xvec)
1774 if (! bfd_is_und_section (section))
1776 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1777 name, TRUE, copy, FALSE);
1779 else
1781 /* Make a copy of the symbol name to prevent problems with
1782 merging symbols. */
1783 *sym_hash = ((struct xcoff_link_hash_entry *)
1784 bfd_wrapped_link_hash_lookup (abfd, info, name,
1785 TRUE, TRUE,
1786 FALSE));
1788 if (*sym_hash == NULL)
1789 goto error_return;
1790 if (((*sym_hash)->root.type == bfd_link_hash_defined
1791 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1792 && ! bfd_is_und_section (section)
1793 && ! bfd_is_com_section (section))
1795 /* This is a second definition of a defined symbol. */
1796 if ((abfd->flags & DYNAMIC) != 0
1797 && ((*sym_hash)->smclas != XMC_GL
1798 || aux.x_csect.x_smclas == XMC_GL
1799 || ((*sym_hash)->root.u.def.section->owner->flags
1800 & DYNAMIC) == 0))
1802 /* The new symbol is from a shared library, and
1803 either the existing symbol is not global
1804 linkage code or this symbol is global linkage
1805 code. If the existing symbol is global
1806 linkage code and the new symbol is not, then
1807 we want to use the new symbol. */
1808 section = bfd_und_section_ptr;
1809 value = 0;
1811 else if (((*sym_hash)->root.u.def.section->owner->flags
1812 & DYNAMIC) != 0)
1814 /* The existing symbol is from a shared library.
1815 Replace it. */
1816 (*sym_hash)->root.type = bfd_link_hash_undefined;
1817 (*sym_hash)->root.u.undef.abfd =
1818 (*sym_hash)->root.u.def.section->owner;
1820 else if (abfd->my_archive != NULL)
1822 /* This is a redefinition in an object contained
1823 in an archive. Just ignore it. See the
1824 comment above. */
1825 section = bfd_und_section_ptr;
1826 value = 0;
1828 else if ((*sym_hash)->root.u.undef.next != NULL
1829 || info->hash->undefs_tail == &(*sym_hash)->root)
1831 /* This symbol has been referenced. In this
1832 case, we just continue and permit the
1833 multiple definition error. See the comment
1834 above about the behaviour of the AIX linker. */
1836 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1838 /* The symbols are both csects of the same
1839 class. There is at least a chance that this
1840 is a semi-legitimate redefinition. */
1841 section = bfd_und_section_ptr;
1842 value = 0;
1843 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1846 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1847 && ((*sym_hash)->root.type == bfd_link_hash_defined
1848 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1849 && (bfd_is_und_section (section)
1850 || bfd_is_com_section (section)))
1852 /* This is a reference to a multiply defined symbol.
1853 Report the error now. See the comment above
1854 about the behaviour of the AIX linker. We could
1855 also do this with warning symbols, but I'm not
1856 sure the XCOFF linker is wholly prepared to
1857 handle them, and that would only be a warning,
1858 not an error. */
1859 if (! ((*info->callbacks->multiple_definition)
1860 (info, (*sym_hash)->root.root.string,
1861 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0,
1862 (*sym_hash)->root.u.def.section->owner,
1863 (*sym_hash)->root.u.def.section,
1864 (*sym_hash)->root.u.def.value)))
1865 goto error_return;
1866 /* Try not to give this error too many times. */
1867 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1871 /* _bfd_generic_link_add_one_symbol may call the linker to
1872 generate an error message, and the linker may try to read
1873 the symbol table to give a good error. Right now, the
1874 line numbers are in an inconsistent state, since they are
1875 counted both in the real sections and in the new csects.
1876 We need to leave the count in the real sections so that
1877 the linker can report the line number of the error
1878 correctly, so temporarily clobber the link to the csects
1879 so that the linker will not try to read the line numbers
1880 a second time from the csects. */
1881 BFD_ASSERT (last_real->next == first_csect);
1882 last_real->next = NULL;
1883 if (! (_bfd_generic_link_add_one_symbol
1884 (info, abfd, name, flags, section, value,
1885 (const char *) NULL, copy, TRUE,
1886 (struct bfd_link_hash_entry **) sym_hash)))
1887 goto error_return;
1888 last_real->next = first_csect;
1890 if (smtyp == XTY_CM)
1892 if ((*sym_hash)->root.type != bfd_link_hash_common
1893 || (*sym_hash)->root.u.c.p->section != csect)
1895 /* We don't need the common csect we just created. */
1896 csect->size = 0;
1898 else
1900 (*sym_hash)->root.u.c.p->alignment_power
1901 = csect->alignment_power;
1905 if (info->hash->creator == abfd->xvec)
1907 int flag;
1909 if (smtyp == XTY_ER || smtyp == XTY_CM)
1910 flag = XCOFF_REF_REGULAR;
1911 else
1912 flag = XCOFF_DEF_REGULAR;
1913 (*sym_hash)->flags |= flag;
1915 if ((*sym_hash)->smclas == XMC_UA
1916 || flag == XCOFF_DEF_REGULAR)
1917 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1921 *csect_cache = csect;
1923 esym += (sym.n_numaux + 1) * symesz;
1924 sym_hash += sym.n_numaux + 1;
1925 csect_cache += sym.n_numaux + 1;
1928 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1930 /* Make sure that we have seen all the relocs. */
1931 for (o = abfd->sections; o != first_csect; o = o->next)
1933 /* Reset the section size and the line number count, since the
1934 data is now attached to the csects. Don't reset the size of
1935 the .debug section, since we need to read it below in
1936 bfd_xcoff_size_dynamic_sections. */
1937 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1938 o->size = 0;
1939 o->lineno_count = 0;
1941 if ((o->flags & SEC_RELOC) != 0)
1943 bfd_size_type i;
1944 struct internal_reloc *rel;
1945 asection **rel_csect;
1947 rel = reloc_info[o->target_index].relocs;
1948 rel_csect = reloc_info[o->target_index].csects;
1950 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1953 if (*rel_csect == NULL)
1955 (*_bfd_error_handler)
1956 (_("%B: reloc %s:%d not in csect"),
1957 abfd, o->name, i);
1958 bfd_set_error (bfd_error_bad_value);
1959 goto error_return;
1962 /* We identify all symbols which are called, so that we
1963 can create glue code for calls to functions imported
1964 from dynamic objects. */
1965 if (info->hash->creator == abfd->xvec
1966 && *rel_csect != bfd_und_section_ptr
1967 && (rel->r_type == R_BR
1968 || rel->r_type == R_RBR)
1969 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1971 struct xcoff_link_hash_entry *h;
1973 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1974 h->flags |= XCOFF_CALLED;
1975 /* If the symbol name starts with a period, it is
1976 the code of a function. If the symbol is
1977 currently undefined, then add an undefined symbol
1978 for the function descriptor. This should do no
1979 harm, because any regular object that defines the
1980 function should also define the function
1981 descriptor. It helps, because it means that we
1982 will identify the function descriptor with a
1983 dynamic object if a dynamic object defines it. */
1984 if (h->root.root.string[0] == '.'
1985 && h->descriptor == NULL)
1987 struct xcoff_link_hash_entry *hds;
1988 struct bfd_link_hash_entry *bh;
1990 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1991 h->root.root.string + 1,
1992 TRUE, FALSE, TRUE);
1993 if (hds == NULL)
1994 goto error_return;
1995 if (hds->root.type == bfd_link_hash_new)
1997 bh = &hds->root;
1998 if (! (_bfd_generic_link_add_one_symbol
1999 (info, abfd, hds->root.root.string,
2000 (flagword) 0, bfd_und_section_ptr,
2001 (bfd_vma) 0, (const char *) NULL, FALSE,
2002 TRUE, &bh)))
2003 goto error_return;
2004 hds = (struct xcoff_link_hash_entry *) bh;
2006 hds->flags |= XCOFF_DESCRIPTOR;
2007 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2008 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2009 hds->descriptor = h;
2010 h->descriptor = hds;
2015 free (reloc_info[o->target_index].csects);
2016 reloc_info[o->target_index].csects = NULL;
2018 /* Reset SEC_RELOC and the reloc_count, since the reloc
2019 information is now attached to the csects. */
2020 o->flags &=~ SEC_RELOC;
2021 o->reloc_count = 0;
2023 /* If we are not keeping memory, free the reloc information. */
2024 if (! info->keep_memory
2025 && coff_section_data (abfd, o) != NULL
2026 && coff_section_data (abfd, o)->relocs != NULL
2027 && ! coff_section_data (abfd, o)->keep_relocs)
2029 free (coff_section_data (abfd, o)->relocs);
2030 coff_section_data (abfd, o)->relocs = NULL;
2034 /* Free up the line numbers. FIXME: We could cache these
2035 somewhere for the final link, to avoid reading them again. */
2036 if (reloc_info[o->target_index].linenos != NULL)
2038 free (reloc_info[o->target_index].linenos);
2039 reloc_info[o->target_index].linenos = NULL;
2043 free (reloc_info);
2045 obj_coff_keep_syms (abfd) = keep_syms;
2047 return TRUE;
2049 error_return:
2050 if (reloc_info != NULL)
2052 for (o = abfd->sections; o != NULL; o = o->next)
2054 if (reloc_info[o->target_index].csects != NULL)
2055 free (reloc_info[o->target_index].csects);
2056 if (reloc_info[o->target_index].linenos != NULL)
2057 free (reloc_info[o->target_index].linenos);
2059 free (reloc_info);
2061 obj_coff_keep_syms (abfd) = keep_syms;
2062 return FALSE;
2065 #undef N_TMASK
2066 #undef N_BTSHFT
2068 /* This function is used to add symbols from a dynamic object to the
2069 global symbol table. */
2071 static bfd_boolean
2072 xcoff_link_add_dynamic_symbols (abfd, info)
2073 bfd *abfd;
2074 struct bfd_link_info *info;
2076 asection *lsec;
2077 bfd_byte *contents;
2078 struct internal_ldhdr ldhdr;
2079 const char *strings;
2080 bfd_byte *elsym, *elsymend;
2081 struct xcoff_import_file *n;
2082 const char *bname;
2083 const char *mname;
2084 const char *s;
2085 unsigned int c;
2086 struct xcoff_import_file **pp;
2088 /* We can only handle a dynamic object if we are generating an XCOFF
2089 output file. */
2090 if (info->hash->creator != abfd->xvec)
2092 (*_bfd_error_handler)
2093 (_("%s: XCOFF shared object when not producing XCOFF output"),
2094 bfd_get_filename (abfd));
2095 bfd_set_error (bfd_error_invalid_operation);
2096 return FALSE;
2099 /* The symbols we use from a dynamic object are not the symbols in
2100 the normal symbol table, but, rather, the symbols in the export
2101 table. If there is a global symbol in a dynamic object which is
2102 not in the export table, the loader will not be able to find it,
2103 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2104 libc.a has symbols in the export table which are not in the
2105 symbol table. */
2107 /* Read in the .loader section. FIXME: We should really use the
2108 o_snloader field in the a.out header, rather than grabbing the
2109 section by name. */
2110 lsec = bfd_get_section_by_name (abfd, ".loader");
2111 if (lsec == NULL)
2113 (*_bfd_error_handler)
2114 (_("%s: dynamic object with no .loader section"),
2115 bfd_get_filename (abfd));
2116 bfd_set_error (bfd_error_no_symbols);
2117 return FALSE;
2121 if (! xcoff_get_section_contents (abfd, lsec))
2122 return FALSE;
2123 contents = coff_section_data (abfd, lsec)->contents;
2125 /* Remove the sections from this object, so that they do not get
2126 included in the link. */
2127 bfd_section_list_clear (abfd);
2129 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2131 strings = (char *) contents + ldhdr.l_stoff;
2133 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
2135 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
2137 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
2139 struct internal_ldsym ldsym;
2140 char nambuf[SYMNMLEN + 1];
2141 const char *name;
2142 struct xcoff_link_hash_entry *h;
2144 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2146 /* We are only interested in exported symbols. */
2147 if ((ldsym.l_smtype & L_EXPORT) == 0)
2148 continue;
2150 if (ldsym._l._l_l._l_zeroes == 0)
2151 name = strings + ldsym._l._l_l._l_offset;
2152 else
2154 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2155 nambuf[SYMNMLEN] = '\0';
2156 name = nambuf;
2159 /* Normally we could not call xcoff_link_hash_lookup in an add
2160 symbols routine, since we might not be using an XCOFF hash
2161 table. However, we verified above that we are using an XCOFF
2162 hash table. */
2164 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
2165 TRUE, TRUE);
2166 if (h == NULL)
2167 return FALSE;
2169 h->flags |= XCOFF_DEF_DYNAMIC;
2171 /* If the symbol is undefined, and the BFD it was found in is
2172 not a dynamic object, change the BFD to this dynamic object,
2173 so that we can get the correct import file ID. */
2174 if ((h->root.type == bfd_link_hash_undefined
2175 || h->root.type == bfd_link_hash_undefweak)
2176 && (h->root.u.undef.abfd == NULL
2177 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2178 h->root.u.undef.abfd = abfd;
2180 if (h->root.type == bfd_link_hash_new)
2182 h->root.type = bfd_link_hash_undefined;
2183 h->root.u.undef.abfd = abfd;
2184 /* We do not want to add this to the undefined symbol list. */
2187 if (h->smclas == XMC_UA
2188 || h->root.type == bfd_link_hash_undefined
2189 || h->root.type == bfd_link_hash_undefweak)
2190 h->smclas = ldsym.l_smclas;
2192 /* Unless this is an XMC_XO symbol, we don't bother to actually
2193 define it, since we don't have a section to put it in anyhow.
2194 Instead, the relocation routines handle the DEF_DYNAMIC flag
2195 correctly. */
2197 if (h->smclas == XMC_XO
2198 && (h->root.type == bfd_link_hash_undefined
2199 || h->root.type == bfd_link_hash_undefweak))
2201 /* This symbol has an absolute value. */
2202 h->root.type = bfd_link_hash_defined;
2203 h->root.u.def.section = bfd_abs_section_ptr;
2204 h->root.u.def.value = ldsym.l_value;
2207 /* If this symbol defines a function descriptor, then it
2208 implicitly defines the function code as well. */
2209 if (h->smclas == XMC_DS
2210 || (h->smclas == XMC_XO && name[0] != '.'))
2211 h->flags |= XCOFF_DESCRIPTOR;
2212 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2214 struct xcoff_link_hash_entry *hds;
2216 hds = h->descriptor;
2217 if (hds == NULL)
2219 char *dsnm;
2221 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
2222 if (dsnm == NULL)
2223 return FALSE;
2224 dsnm[0] = '.';
2225 strcpy (dsnm + 1, name);
2226 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2227 TRUE, TRUE, TRUE);
2228 free (dsnm);
2229 if (hds == NULL)
2230 return FALSE;
2232 if (hds->root.type == bfd_link_hash_new)
2234 hds->root.type = bfd_link_hash_undefined;
2235 hds->root.u.undef.abfd = abfd;
2236 /* We do not want to add this to the undefined
2237 symbol list. */
2240 hds->descriptor = h;
2241 h->descriptor = hds;
2244 hds->flags |= XCOFF_DEF_DYNAMIC;
2245 if (hds->smclas == XMC_UA)
2246 hds->smclas = XMC_PR;
2248 /* An absolute symbol appears to actually define code, not a
2249 function descriptor. This is how some math functions are
2250 implemented on AIX 4.1. */
2251 if (h->smclas == XMC_XO
2252 && (hds->root.type == bfd_link_hash_undefined
2253 || hds->root.type == bfd_link_hash_undefweak))
2255 hds->smclas = XMC_XO;
2256 hds->root.type = bfd_link_hash_defined;
2257 hds->root.u.def.section = bfd_abs_section_ptr;
2258 hds->root.u.def.value = ldsym.l_value;
2263 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2265 free (coff_section_data (abfd, lsec)->contents);
2266 coff_section_data (abfd, lsec)->contents = NULL;
2269 /* Record this file in the import files. */
2271 n = ((struct xcoff_import_file *)
2272 bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file)));
2273 if (n == NULL)
2274 return FALSE;
2275 n->next = NULL;
2277 /* For some reason, the path entry in the import file list for a
2278 shared object appears to always be empty. The file name is the
2279 base name. */
2280 n->path = "";
2281 if (abfd->my_archive == NULL)
2283 bname = bfd_get_filename (abfd);
2284 mname = "";
2286 else
2288 bname = bfd_get_filename (abfd->my_archive);
2289 mname = bfd_get_filename (abfd);
2291 s = strrchr (bname, '/');
2292 if (s != NULL)
2293 bname = s + 1;
2294 n->file = bname;
2295 n->member = mname;
2297 /* We start c at 1 because the first import file number is reserved
2298 for LIBPATH. */
2299 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2300 *pp != NULL;
2301 pp = &(*pp)->next, ++c)
2303 *pp = n;
2305 xcoff_data (abfd)->import_file_id = c;
2307 return TRUE;
2310 /* Routines that are called after all the input files have been
2311 handled, but before the sections are laid out in memory. */
2313 /* Mark a symbol as not being garbage, including the section in which
2314 it is defined. */
2316 static INLINE bfd_boolean
2317 xcoff_mark_symbol (info, h)
2318 struct bfd_link_info *info;
2319 struct xcoff_link_hash_entry *h;
2322 if ((h->flags & XCOFF_MARK) != 0)
2323 return TRUE;
2325 h->flags |= XCOFF_MARK;
2326 if (h->root.type == bfd_link_hash_defined
2327 || h->root.type == bfd_link_hash_defweak)
2329 asection *hsec;
2331 hsec = h->root.u.def.section;
2332 if (! bfd_is_abs_section (hsec)
2333 && (hsec->flags & SEC_MARK) == 0)
2335 if (! xcoff_mark (info, hsec))
2336 return FALSE;
2340 if (h->toc_section != NULL
2341 && (h->toc_section->flags & SEC_MARK) == 0)
2343 if (! xcoff_mark (info, h->toc_section))
2344 return FALSE;
2347 return TRUE;
2350 /* The mark phase of garbage collection. For a given section, mark
2351 it, and all the sections which define symbols to which it refers.
2352 Because this function needs to look at the relocs, we also count
2353 the number of relocs which need to be copied into the .loader
2354 section. */
2356 static bfd_boolean
2357 xcoff_mark (info, sec)
2358 struct bfd_link_info *info;
2359 asection *sec;
2361 if (bfd_is_abs_section (sec)
2362 || (sec->flags & SEC_MARK) != 0)
2363 return TRUE;
2365 sec->flags |= SEC_MARK;
2367 if (sec->owner->xvec == info->hash->creator
2368 && coff_section_data (sec->owner, sec) != NULL
2369 && xcoff_section_data (sec->owner, sec) != NULL)
2371 register struct xcoff_link_hash_entry **hp, **hpend;
2372 struct internal_reloc *rel, *relend;
2374 /* Mark all the symbols in this section. */
2376 hp = (obj_xcoff_sym_hashes (sec->owner)
2377 + xcoff_section_data (sec->owner, sec)->first_symndx);
2378 hpend = (obj_xcoff_sym_hashes (sec->owner)
2379 + xcoff_section_data (sec->owner, sec)->last_symndx);
2380 for (; hp < hpend; hp++)
2382 register struct xcoff_link_hash_entry *h;
2384 h = *hp;
2385 if (h != NULL
2386 && (h->flags & XCOFF_MARK) == 0)
2388 if (! xcoff_mark_symbol (info, h))
2389 return FALSE;
2393 /* Look through the section relocs. */
2395 if ((sec->flags & SEC_RELOC) != 0
2396 && sec->reloc_count > 0)
2398 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2399 (bfd_byte *) NULL, FALSE,
2400 (struct internal_reloc *) NULL);
2401 if (rel == NULL)
2402 return FALSE;
2403 relend = rel + sec->reloc_count;
2404 for (; rel < relend; rel++)
2406 asection *rsec;
2407 struct xcoff_link_hash_entry *h;
2409 if ((unsigned int) rel->r_symndx
2410 > obj_raw_syment_count (sec->owner))
2411 continue;
2413 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2414 if (h != NULL
2415 && (h->flags & XCOFF_MARK) == 0)
2417 if (! xcoff_mark_symbol (info, h))
2418 return FALSE;
2421 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2422 if (rsec != NULL
2423 && (rsec->flags & SEC_MARK) == 0)
2425 if (! xcoff_mark (info, rsec))
2426 return FALSE;
2429 /* See if this reloc needs to be copied into the .loader
2430 section. */
2431 switch (rel->r_type)
2433 default:
2434 if (h == NULL
2435 || h->root.type == bfd_link_hash_defined
2436 || h->root.type == bfd_link_hash_defweak
2437 || h->root.type == bfd_link_hash_common
2438 || ((h->flags & XCOFF_CALLED) != 0
2439 && (h->root.type == bfd_link_hash_undefined
2440 || h->root.type == bfd_link_hash_undefweak)
2441 && h->root.root.string[0] == '.'
2442 && h->descriptor != NULL
2443 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2444 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2445 && (h->descriptor->flags
2446 & XCOFF_DEF_REGULAR) == 0))))
2447 break;
2448 /* Fall through. */
2449 case R_POS:
2450 case R_NEG:
2451 case R_RL:
2452 case R_RLA:
2453 ++xcoff_hash_table (info)->ldrel_count;
2454 if (h != NULL)
2455 h->flags |= XCOFF_LDREL;
2456 break;
2457 case R_TOC:
2458 case R_GL:
2459 case R_TCL:
2460 case R_TRL:
2461 case R_TRLA:
2462 /* We should never need a .loader reloc for a TOC
2463 relative reloc. */
2464 break;
2468 if (! info->keep_memory
2469 && coff_section_data (sec->owner, sec) != NULL
2470 && coff_section_data (sec->owner, sec)->relocs != NULL
2471 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2473 free (coff_section_data (sec->owner, sec)->relocs);
2474 coff_section_data (sec->owner, sec)->relocs = NULL;
2479 return TRUE;
2482 /* The sweep phase of garbage collection. Remove all garbage
2483 sections. */
2485 static void
2486 xcoff_sweep (info)
2487 struct bfd_link_info *info;
2489 bfd *sub;
2491 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2493 asection *o;
2495 for (o = sub->sections; o != NULL; o = o->next)
2497 if ((o->flags & SEC_MARK) == 0)
2499 /* Keep all sections from non-XCOFF input files. Keep
2500 special sections. Keep .debug sections for the
2501 moment. */
2502 if (sub->xvec != info->hash->creator
2503 || o == xcoff_hash_table (info)->debug_section
2504 || o == xcoff_hash_table (info)->loader_section
2505 || o == xcoff_hash_table (info)->linkage_section
2506 || o == xcoff_hash_table (info)->toc_section
2507 || o == xcoff_hash_table (info)->descriptor_section
2508 || strcmp (o->name, ".debug") == 0)
2509 o->flags |= SEC_MARK;
2510 else
2512 o->size = 0;
2513 o->reloc_count = 0;
2514 o->lineno_count = 0;
2521 /* Record the number of elements in a set. This is used to output the
2522 correct csect length. */
2524 bfd_boolean
2525 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2526 bfd *output_bfd;
2527 struct bfd_link_info *info;
2528 struct bfd_link_hash_entry *harg;
2529 bfd_size_type size;
2531 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2532 struct xcoff_link_size_list *n;
2533 bfd_size_type amt;
2535 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2536 return TRUE;
2538 /* This will hardly ever be called. I don't want to burn four bytes
2539 per global symbol, so instead the size is kept on a linked list
2540 attached to the hash table. */
2542 amt = sizeof (struct xcoff_link_size_list);
2543 n = (struct xcoff_link_size_list *) bfd_alloc (output_bfd, amt);
2544 if (n == NULL)
2545 return FALSE;
2546 n->next = xcoff_hash_table (info)->size_list;
2547 n->h = h;
2548 n->size = size;
2549 xcoff_hash_table (info)->size_list = n;
2551 h->flags |= XCOFF_HAS_SIZE;
2553 return TRUE;
2556 /* Import a symbol. */
2558 bfd_boolean
2559 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2560 impmember, syscall_flag)
2561 bfd *output_bfd;
2562 struct bfd_link_info *info;
2563 struct bfd_link_hash_entry *harg;
2564 bfd_vma val;
2565 const char *imppath;
2566 const char *impfile;
2567 const char *impmember;
2568 unsigned int syscall_flag;
2570 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2572 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2573 return TRUE;
2575 /* A symbol name which starts with a period is the code for a
2576 function. If the symbol is undefined, then add an undefined
2577 symbol for the function descriptor, and import that instead. */
2578 if (h->root.root.string[0] == '.'
2579 && h->root.type == bfd_link_hash_undefined
2580 && val == (bfd_vma) -1)
2582 struct xcoff_link_hash_entry *hds;
2584 hds = h->descriptor;
2585 if (hds == NULL)
2587 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2588 h->root.root.string + 1,
2589 TRUE, FALSE, TRUE);
2590 if (hds == NULL)
2591 return FALSE;
2592 if (hds->root.type == bfd_link_hash_new)
2594 hds->root.type = bfd_link_hash_undefined;
2595 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2597 hds->flags |= XCOFF_DESCRIPTOR;
2598 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2599 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2600 hds->descriptor = h;
2601 h->descriptor = hds;
2604 /* Now, if the descriptor is undefined, import the descriptor
2605 rather than the symbol we were told to import. FIXME: Is
2606 this correct in all cases? */
2607 if (hds->root.type == bfd_link_hash_undefined)
2608 h = hds;
2611 h->flags |= (XCOFF_IMPORT | syscall_flag);
2613 if (val != (bfd_vma) -1)
2615 if (h->root.type == bfd_link_hash_defined
2616 && (! bfd_is_abs_section (h->root.u.def.section)
2617 || h->root.u.def.value != val))
2619 if (! ((*info->callbacks->multiple_definition)
2620 (info, h->root.root.string, h->root.u.def.section->owner,
2621 h->root.u.def.section, h->root.u.def.value,
2622 output_bfd, bfd_abs_section_ptr, val)))
2623 return FALSE;
2626 h->root.type = bfd_link_hash_defined;
2627 h->root.u.def.section = bfd_abs_section_ptr;
2628 h->root.u.def.value = val;
2631 /* We overload the ldindx field to hold the l_ifile value for this
2632 symbol. */
2633 BFD_ASSERT (h->ldsym == NULL);
2634 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2635 if (imppath == NULL)
2636 h->ldindx = -1;
2637 else
2639 unsigned int c;
2640 struct xcoff_import_file **pp;
2642 /* We start c at 1 because the first entry in the import list is
2643 reserved for the library search path. */
2644 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2645 *pp != NULL;
2646 pp = &(*pp)->next, ++c)
2648 if (strcmp ((*pp)->path, imppath) == 0
2649 && strcmp ((*pp)->file, impfile) == 0
2650 && strcmp ((*pp)->member, impmember) == 0)
2651 break;
2654 if (*pp == NULL)
2656 struct xcoff_import_file *n;
2657 bfd_size_type amt = sizeof (struct xcoff_import_file);
2659 n = (struct xcoff_import_file *) bfd_alloc (output_bfd, amt);
2660 if (n == NULL)
2661 return FALSE;
2662 n->next = NULL;
2663 n->path = imppath;
2664 n->file = impfile;
2665 n->member = impmember;
2666 *pp = n;
2669 h->ldindx = c;
2672 return TRUE;
2675 /* Export a symbol. */
2677 bfd_boolean
2678 bfd_xcoff_export_symbol (output_bfd, info, harg)
2679 bfd *output_bfd;
2680 struct bfd_link_info *info;
2681 struct bfd_link_hash_entry *harg;
2683 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2685 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2686 return TRUE;
2688 h->flags |= XCOFF_EXPORT;
2690 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2691 I'm just going to ignore it until somebody explains it. */
2693 /* See if this is a function descriptor. It may be one even though
2694 it is not so marked. */
2695 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2696 && h->root.root.string[0] != '.')
2698 char *fnname;
2699 struct xcoff_link_hash_entry *hfn;
2700 bfd_size_type amt = strlen (h->root.root.string) + 2;
2702 fnname = (char *) bfd_malloc (amt);
2703 if (fnname == NULL)
2704 return FALSE;
2705 fnname[0] = '.';
2706 strcpy (fnname + 1, h->root.root.string);
2707 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2708 fnname, FALSE, FALSE, TRUE);
2709 free (fnname);
2710 if (hfn != NULL
2711 && hfn->smclas == XMC_PR
2712 && (hfn->root.type == bfd_link_hash_defined
2713 || hfn->root.type == bfd_link_hash_defweak))
2715 h->flags |= XCOFF_DESCRIPTOR;
2716 h->descriptor = hfn;
2717 hfn->descriptor = h;
2721 /* Make sure we don't garbage collect this symbol. */
2722 if (! xcoff_mark_symbol (info, h))
2723 return FALSE;
2725 /* If this is a function descriptor, make sure we don't garbage
2726 collect the associated function code. We normally don't have to
2727 worry about this, because the descriptor will be attached to a
2728 section with relocs, but if we are creating the descriptor
2729 ourselves those relocs will not be visible to the mark code. */
2730 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2732 if (! xcoff_mark_symbol (info, h->descriptor))
2733 return FALSE;
2736 return TRUE;
2739 /* Count a reloc against a symbol. This is called for relocs
2740 generated by the linker script, typically for global constructors
2741 and destructors. */
2743 bfd_boolean
2744 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2745 bfd *output_bfd;
2746 struct bfd_link_info *info;
2747 const char *name;
2749 struct xcoff_link_hash_entry *h;
2751 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2752 return TRUE;
2754 h = ((struct xcoff_link_hash_entry *)
2755 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2756 FALSE));
2757 if (h == NULL)
2759 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2760 bfd_set_error (bfd_error_no_symbols);
2761 return FALSE;
2764 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2765 ++xcoff_hash_table (info)->ldrel_count;
2767 /* Mark the symbol to avoid garbage collection. */
2768 if (! xcoff_mark_symbol (info, h))
2769 return FALSE;
2771 return TRUE;
2774 /* This function is called for each symbol to which the linker script
2775 assigns a value. */
2777 bfd_boolean
2778 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2779 bfd *output_bfd;
2780 struct bfd_link_info *info;
2781 const char *name;
2783 struct xcoff_link_hash_entry *h;
2785 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2786 return TRUE;
2788 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2789 FALSE);
2790 if (h == NULL)
2791 return FALSE;
2793 h->flags |= XCOFF_DEF_REGULAR;
2795 return TRUE;
2798 /* Build the .loader section. This is called by the XCOFF linker
2799 emulation before_allocation routine. We must set the size of the
2800 .loader section before the linker lays out the output file.
2801 LIBPATH is the library path to search for shared objects; this is
2802 normally built from the -L arguments passed to the linker. ENTRY
2803 is the name of the entry point symbol (the -e linker option).
2804 FILE_ALIGN is the alignment to use for sections within the file
2805 (the -H linker option). MAXSTACK is the maximum stack size (the
2806 -bmaxstack linker option). MAXDATA is the maximum data size (the
2807 -bmaxdata linker option). GC is whether to do garbage collection
2808 (the -bgc linker option). MODTYPE is the module type (the
2809 -bmodtype linker option). TEXTRO is whether the text section must
2810 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2811 whether all defined symbols should be exported (the -unix linker
2812 option). SPECIAL_SECTIONS is set by this routine to csects with
2813 magic names like _end. */
2815 bfd_boolean
2816 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2817 file_align, maxstack, maxdata, gc,
2818 modtype, textro, export_defineds,
2819 special_sections, rtld)
2820 bfd *output_bfd;
2821 struct bfd_link_info *info;
2822 const char *libpath;
2823 const char *entry;
2824 unsigned long file_align;
2825 unsigned long maxstack;
2826 unsigned long maxdata;
2827 bfd_boolean gc;
2828 int modtype;
2829 bfd_boolean textro;
2830 bfd_boolean export_defineds;
2831 asection **special_sections;
2832 bfd_boolean rtld;
2834 struct xcoff_link_hash_entry *hentry;
2835 asection *lsec;
2836 struct xcoff_loader_info ldinfo;
2837 int i;
2838 size_t impsize, impcount;
2839 struct xcoff_import_file *fl;
2840 struct internal_ldhdr *ldhdr;
2841 bfd_size_type stoff;
2842 register char *out;
2843 asection *sec;
2844 bfd *sub;
2845 struct bfd_strtab_hash *debug_strtab;
2846 bfd_byte *debug_contents = NULL;
2847 bfd_size_type amt;
2849 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2851 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2852 special_sections[i] = NULL;
2853 return TRUE;
2856 ldinfo.failed = FALSE;
2857 ldinfo.output_bfd = output_bfd;
2858 ldinfo.info = info;
2859 ldinfo.export_defineds = export_defineds;
2860 ldinfo.ldsym_count = 0;
2861 ldinfo.string_size = 0;
2862 ldinfo.strings = NULL;
2863 ldinfo.string_alc = 0;
2865 xcoff_data (output_bfd)->maxstack = maxstack;
2866 xcoff_data (output_bfd)->maxdata = maxdata;
2867 xcoff_data (output_bfd)->modtype = modtype;
2869 xcoff_hash_table (info)->file_align = file_align;
2870 xcoff_hash_table (info)->textro = textro;
2872 hentry = NULL;
2873 if (entry != NULL)
2875 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2876 FALSE, FALSE, TRUE);
2877 if (hentry != NULL)
2878 hentry->flags |= XCOFF_ENTRY;
2881 /* __rtinit */
2882 if (info->init_function || info->fini_function || rtld)
2884 struct xcoff_link_hash_entry *hsym;
2885 struct internal_ldsym *ldsym;
2887 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
2888 "__rtinit", FALSE, FALSE, TRUE);
2889 if (hsym == NULL)
2891 (*_bfd_error_handler)
2892 (_("error: undefined symbol __rtinit"));
2893 return FALSE;
2896 xcoff_mark_symbol (info, hsym);
2897 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
2899 /* __rtinit initialized */
2900 amt = sizeof (struct internal_ldsym);
2901 ldsym = (struct internal_ldsym *) bfd_malloc (amt);
2903 ldsym->l_value = 0; /* will be filled in later */
2904 ldsym->l_scnum = 2; /* data section */
2905 ldsym->l_smtype = XTY_SD; /* csect section definition */
2906 ldsym->l_smclas = 5; /* .rw */
2907 ldsym->l_ifile = 0; /* special system loader symbol */
2908 ldsym->l_parm = 0; /* NA */
2910 /* Force __rtinit to be the first symbol in the loader symbol table
2911 See xcoff_build_ldsyms
2913 The first 3 symbol table indices are reserved to indicate the data,
2914 text and bss sections. */
2915 BFD_ASSERT (0 == ldinfo.ldsym_count);
2917 hsym->ldindx = 3;
2918 ldinfo.ldsym_count = 1;
2919 hsym->ldsym = ldsym;
2921 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
2922 hsym->ldsym, hsym->root.root.string))
2923 return FALSE;
2925 /* This symbol is written out by xcoff_write_global_symbol
2926 Set stuff up so xcoff_write_global_symbol logic works. */
2927 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
2928 hsym->root.type = bfd_link_hash_defined;
2929 hsym->root.u.def.value = 0;
2932 /* Garbage collect unused sections. */
2933 if (info->relocatable
2934 || ! gc
2935 || hentry == NULL
2936 || (hentry->root.type != bfd_link_hash_defined
2937 && hentry->root.type != bfd_link_hash_defweak))
2939 gc = FALSE;
2940 xcoff_hash_table (info)->gc = FALSE;
2942 /* We still need to call xcoff_mark, in order to set ldrel_count
2943 correctly. */
2944 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2946 asection *o;
2948 for (o = sub->sections; o != NULL; o = o->next)
2950 if ((o->flags & SEC_MARK) == 0)
2952 if (! xcoff_mark (info, o))
2953 goto error_return;
2958 else
2960 if (! xcoff_mark (info, hentry->root.u.def.section))
2961 goto error_return;
2962 xcoff_sweep (info);
2963 xcoff_hash_table (info)->gc = TRUE;
2966 /* Return special sections to the caller. */
2967 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2969 sec = xcoff_hash_table (info)->special_sections[i];
2971 if (sec != NULL
2972 && gc
2973 && (sec->flags & SEC_MARK) == 0)
2975 sec = NULL;
2977 special_sections[i] = sec;
2980 if (info->input_bfds == NULL)
2982 /* I'm not sure what to do in this bizarre case. */
2983 return TRUE;
2986 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2987 (PTR) &ldinfo);
2988 if (ldinfo.failed)
2989 goto error_return;
2991 /* Work out the size of the import file names. Each import file ID
2992 consists of three null terminated strings: the path, the file
2993 name, and the archive member name. The first entry in the list
2994 of names is the path to use to find objects, which the linker has
2995 passed in as the libpath argument. For some reason, the path
2996 entry in the other import file names appears to always be empty. */
2997 impsize = strlen (libpath) + 3;
2998 impcount = 1;
2999 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3001 ++impcount;
3002 impsize += (strlen (fl->path)
3003 + strlen (fl->file)
3004 + strlen (fl->member)
3005 + 3);
3008 /* Set up the .loader section header. */
3009 ldhdr = &xcoff_hash_table (info)->ldhdr;
3010 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3011 ldhdr->l_nsyms = ldinfo.ldsym_count;
3012 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3013 ldhdr->l_istlen = impsize;
3014 ldhdr->l_nimpid = impcount;
3015 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3016 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3017 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3018 ldhdr->l_stlen = ldinfo.string_size;
3019 stoff = ldhdr->l_impoff + impsize;
3020 if (ldinfo.string_size == 0)
3021 ldhdr->l_stoff = 0;
3022 else
3023 ldhdr->l_stoff = stoff;
3025 /* 64 bit elements to ldhdr
3026 The swap out routine for 32 bit will ignore them.
3027 Nothing fancy, symbols come after the header and relocs come
3028 after symbols. */
3029 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3030 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3031 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3033 /* We now know the final size of the .loader section. Allocate
3034 space for it. */
3035 lsec = xcoff_hash_table (info)->loader_section;
3036 lsec->size = stoff + ldhdr->l_stlen;
3037 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->size);
3038 if (lsec->contents == NULL)
3039 goto error_return;
3041 /* Set up the header. */
3042 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3044 /* Set up the import file names. */
3045 out = (char *) lsec->contents + ldhdr->l_impoff;
3046 strcpy (out, libpath);
3047 out += strlen (libpath) + 1;
3048 *out++ = '\0';
3049 *out++ = '\0';
3050 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3052 register const char *s;
3054 s = fl->path;
3055 while ((*out++ = *s++) != '\0')
3057 s = fl->file;
3058 while ((*out++ = *s++) != '\0')
3060 s = fl->member;
3061 while ((*out++ = *s++) != '\0')
3065 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3067 /* Set up the symbol string table. */
3068 if (ldinfo.string_size > 0)
3070 memcpy (out, ldinfo.strings, ldinfo.string_size);
3071 free (ldinfo.strings);
3072 ldinfo.strings = NULL;
3075 /* We can't set up the symbol table or the relocs yet, because we
3076 don't yet know the final position of the various sections. The
3077 .loader symbols are written out when the corresponding normal
3078 symbols are written out in xcoff_link_input_bfd or
3079 xcoff_write_global_symbol. The .loader relocs are written out
3080 when the corresponding normal relocs are handled in
3081 xcoff_link_input_bfd.
3084 /* Allocate space for the magic sections. */
3085 sec = xcoff_hash_table (info)->linkage_section;
3086 if (sec->size > 0)
3088 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->size);
3089 if (sec->contents == NULL)
3090 goto error_return;
3092 sec = xcoff_hash_table (info)->toc_section;
3093 if (sec->size > 0)
3095 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->size);
3096 if (sec->contents == NULL)
3097 goto error_return;
3099 sec = xcoff_hash_table (info)->descriptor_section;
3100 if (sec->size > 0)
3102 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->size);
3103 if (sec->contents == NULL)
3104 goto error_return;
3107 /* Now that we've done garbage collection, figure out the contents
3108 of the .debug section. */
3109 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3111 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3113 asection *subdeb;
3114 bfd_size_type symcount;
3115 unsigned long *debug_index;
3116 asection **csectpp;
3117 bfd_byte *esym, *esymend;
3118 bfd_size_type symesz;
3120 if (sub->xvec != info->hash->creator)
3121 continue;
3122 subdeb = bfd_get_section_by_name (sub, ".debug");
3123 if (subdeb == NULL || subdeb->size == 0)
3124 continue;
3126 if (info->strip == strip_all
3127 || info->strip == strip_debugger
3128 || info->discard == discard_all)
3130 subdeb->size = 0;
3131 continue;
3134 if (! _bfd_coff_get_external_symbols (sub))
3135 goto error_return;
3137 symcount = obj_raw_syment_count (sub);
3138 debug_index = ((unsigned long *)
3139 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3140 if (debug_index == NULL)
3141 goto error_return;
3142 xcoff_data (sub)->debug_indices = debug_index;
3144 /* Grab the contents of the .debug section. We use malloc and
3145 copy the names into the debug stringtab, rather than
3146 bfd_alloc, because I expect that, when linking many files
3147 together, many of the strings will be the same. Storing the
3148 strings in the hash table should save space in this case. */
3149 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3150 goto error_return;
3152 csectpp = xcoff_data (sub)->csects;
3154 /* Dynamic object do not have csectpp's. */
3155 if (NULL != csectpp)
3157 symesz = bfd_coff_symesz (sub);
3158 esym = (bfd_byte *) obj_coff_external_syms (sub);
3159 esymend = esym + symcount * symesz;
3161 while (esym < esymend)
3163 struct internal_syment sym;
3165 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3167 *debug_index = (unsigned long) -1;
3169 if (sym._n._n_n._n_zeroes == 0
3170 && *csectpp != NULL
3171 && (! gc
3172 || ((*csectpp)->flags & SEC_MARK) != 0
3173 || *csectpp == bfd_abs_section_ptr)
3174 && bfd_coff_symname_in_debug (sub, &sym))
3176 char *name;
3177 bfd_size_type indx;
3179 name = (char *) debug_contents + sym._n._n_n._n_offset;
3180 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3181 if (indx == (bfd_size_type) -1)
3182 goto error_return;
3183 *debug_index = indx;
3186 esym += (sym.n_numaux + 1) * symesz;
3187 csectpp += sym.n_numaux + 1;
3188 debug_index += sym.n_numaux + 1;
3192 free (debug_contents);
3193 debug_contents = NULL;
3195 /* Clear the size of subdeb, so that it is not included directly
3196 in the output file. */
3197 subdeb->size = 0;
3199 if (! info->keep_memory)
3201 if (! _bfd_coff_free_symbols (sub))
3202 goto error_return;
3206 if (info->strip != strip_all)
3207 xcoff_hash_table (info)->debug_section->size =
3208 _bfd_stringtab_size (debug_strtab);
3210 return TRUE;
3212 error_return:
3213 if (ldinfo.strings != NULL)
3214 free (ldinfo.strings);
3215 if (debug_contents != NULL)
3216 free (debug_contents);
3217 return FALSE;
3220 bfd_boolean
3221 bfd_xcoff_link_generate_rtinit (abfd, init, fini, rtld)
3222 bfd *abfd;
3223 const char *init;
3224 const char *fini;
3225 bfd_boolean rtld;
3227 struct bfd_in_memory *bim;
3229 bim = ((struct bfd_in_memory *)
3230 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
3231 if (bim == NULL)
3232 return FALSE;
3234 bim->size = 0;
3235 bim->buffer = 0;
3237 abfd->link_next = 0;
3238 abfd->format = bfd_object;
3239 abfd->iostream = (PTR) bim;
3240 abfd->flags = BFD_IN_MEMORY;
3241 abfd->direction = write_direction;
3242 abfd->where = 0;
3244 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3245 return FALSE;
3247 /* need to reset to unknown or it will not be read back in correctly */
3248 abfd->format = bfd_unknown;
3249 abfd->direction = read_direction;
3250 abfd->where = 0;
3252 return TRUE;
3256 /* Add a symbol to the .loader symbols, if necessary. */
3258 static bfd_boolean
3259 xcoff_build_ldsyms (h, p)
3260 struct xcoff_link_hash_entry *h;
3261 PTR p;
3263 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3264 bfd_size_type amt;
3266 if (h->root.type == bfd_link_hash_warning)
3267 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3269 /* __rtinit, this symbol has special handling. */
3270 if (h->flags & XCOFF_RTINIT)
3271 return TRUE;
3273 /* If this is a final link, and the symbol was defined as a common
3274 symbol in a regular object file, and there was no definition in
3275 any dynamic object, then the linker will have allocated space for
3276 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3277 will not have been set. */
3278 if (h->root.type == bfd_link_hash_defined
3279 && (h->flags & XCOFF_DEF_REGULAR) == 0
3280 && (h->flags & XCOFF_REF_REGULAR) != 0
3281 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3282 && (bfd_is_abs_section (h->root.u.def.section)
3283 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3284 h->flags |= XCOFF_DEF_REGULAR;
3286 /* If all defined symbols should be exported, mark them now. We
3287 don't want to export the actual functions, just the function
3288 descriptors. */
3289 if (ldinfo->export_defineds
3290 && (h->flags & XCOFF_DEF_REGULAR) != 0
3291 && h->root.root.string[0] != '.')
3293 bfd_boolean export;
3295 /* We don't export a symbol which is being defined by an object
3296 included from an archive which contains a shared object. The
3297 rationale is that if an archive contains both an unshared and
3298 a shared object, then there must be some reason that the
3299 unshared object is unshared, and we don't want to start
3300 providing a shared version of it. In particular, this solves
3301 a bug involving the _savefNN set of functions. gcc will call
3302 those functions without providing a slot to restore the TOC,
3303 so it is essential that these functions be linked in directly
3304 and not from a shared object, which means that a shared
3305 object which also happens to link them in must not export
3306 them. This is confusing, but I haven't been able to think of
3307 a different approach. Note that the symbols can, of course,
3308 be exported explicitly. */
3309 export = TRUE;
3310 if ((h->root.type == bfd_link_hash_defined
3311 || h->root.type == bfd_link_hash_defweak)
3312 && h->root.u.def.section->owner != NULL
3313 && h->root.u.def.section->owner->my_archive != NULL)
3315 bfd *arbfd, *member;
3317 arbfd = h->root.u.def.section->owner->my_archive;
3318 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3319 while (member != NULL)
3321 if ((member->flags & DYNAMIC) != 0)
3323 export = FALSE;
3324 break;
3326 member = bfd_openr_next_archived_file (arbfd, member);
3330 if (export)
3331 h->flags |= XCOFF_EXPORT;
3334 /* We don't want to garbage collect symbols which are not defined in
3335 XCOFF files. This is a convenient place to mark them. */
3336 if (xcoff_hash_table (ldinfo->info)->gc
3337 && (h->flags & XCOFF_MARK) == 0
3338 && (h->root.type == bfd_link_hash_defined
3339 || h->root.type == bfd_link_hash_defweak)
3340 && (h->root.u.def.section->owner == NULL
3341 || (h->root.u.def.section->owner->xvec
3342 != ldinfo->info->hash->creator)))
3343 h->flags |= XCOFF_MARK;
3345 /* If this symbol is called and defined in a dynamic object, or it
3346 is imported, then we need to set up global linkage code for it.
3347 (Unless we did garbage collection and we didn't need this
3348 symbol.) */
3349 if ((h->flags & XCOFF_CALLED) != 0
3350 && (h->root.type == bfd_link_hash_undefined
3351 || h->root.type == bfd_link_hash_undefweak)
3352 && h->root.root.string[0] == '.'
3353 && h->descriptor != NULL
3354 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3355 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3356 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3357 && (! xcoff_hash_table (ldinfo->info)->gc
3358 || (h->flags & XCOFF_MARK) != 0))
3360 asection *sec;
3361 struct xcoff_link_hash_entry *hds;
3363 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3364 h->root.type = bfd_link_hash_defined;
3365 h->root.u.def.section = sec;
3366 h->root.u.def.value = sec->size;
3367 h->smclas = XMC_GL;
3368 h->flags |= XCOFF_DEF_REGULAR;
3369 sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
3371 /* The global linkage code requires a TOC entry for the
3372 descriptor. */
3373 hds = h->descriptor;
3374 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3375 || hds->root.type == bfd_link_hash_undefweak)
3376 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3377 hds->flags |= XCOFF_MARK;
3378 if (hds->toc_section == NULL)
3380 int byte_size;
3382 /* 32 vs 64
3383 xcoff32 uses 4 bytes in the toc.
3384 xcoff64 uses 8 bytes in the toc. */
3385 if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
3386 byte_size = 8;
3387 else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
3388 byte_size = 4;
3389 else
3390 return FALSE;
3392 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3393 hds->u.toc_offset = hds->toc_section->size;
3394 hds->toc_section->size += byte_size;
3395 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3396 ++hds->toc_section->reloc_count;
3397 hds->indx = -2;
3398 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3400 /* We need to call xcoff_build_ldsyms recursively here,
3401 because we may already have passed hds on the traversal. */
3402 xcoff_build_ldsyms (hds, p);
3406 /* If this symbol is exported, but not defined, we need to try to
3407 define it. */
3408 if ((h->flags & XCOFF_EXPORT) != 0
3409 && (h->flags & XCOFF_IMPORT) == 0
3410 && (h->flags & XCOFF_DEF_REGULAR) == 0
3411 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3412 && (h->root.type == bfd_link_hash_undefined
3413 || h->root.type == bfd_link_hash_undefweak))
3415 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3416 && (h->descriptor->root.type == bfd_link_hash_defined
3417 || h->descriptor->root.type == bfd_link_hash_defweak))
3419 asection *sec;
3421 /* This is an undefined function descriptor associated with
3422 a defined entry point. We can build up a function
3423 descriptor ourselves. Believe it or not, the AIX linker
3424 actually does this, and there are cases where we need to
3425 do it as well. */
3426 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3427 h->root.type = bfd_link_hash_defined;
3428 h->root.u.def.section = sec;
3429 h->root.u.def.value = sec->size;
3430 h->smclas = XMC_DS;
3431 h->flags |= XCOFF_DEF_REGULAR;
3433 /* The size of the function descriptor depends if this is an
3434 xcoff32 (12) or xcoff64 (24). */
3435 sec->size +=
3436 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
3438 /* A function descriptor uses two relocs: one for the
3439 associated code, and one for the TOC address. */
3440 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3441 sec->reloc_count += 2;
3443 /* We handle writing out the contents of the descriptor in
3444 xcoff_write_global_symbol. */
3446 else
3448 (*_bfd_error_handler)
3449 (_("warning: attempt to export undefined symbol `%s'"),
3450 h->root.root.string);
3451 h->ldsym = NULL;
3452 return TRUE;
3456 /* If this is still a common symbol, and it wasn't garbage
3457 collected, we need to actually allocate space for it in the .bss
3458 section. */
3459 if (h->root.type == bfd_link_hash_common
3460 && (! xcoff_hash_table (ldinfo->info)->gc
3461 || (h->flags & XCOFF_MARK) != 0)
3462 && h->root.u.c.p->section->size == 0)
3464 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3465 h->root.u.c.p->section->size = h->root.u.c.size;
3468 /* We need to add a symbol to the .loader section if it is mentioned
3469 in a reloc which we are copying to the .loader section and it was
3470 not defined or common, or if it is the entry point, or if it is
3471 being exported. */
3473 if (((h->flags & XCOFF_LDREL) == 0
3474 || h->root.type == bfd_link_hash_defined
3475 || h->root.type == bfd_link_hash_defweak
3476 || h->root.type == bfd_link_hash_common)
3477 && (h->flags & XCOFF_ENTRY) == 0
3478 && (h->flags & XCOFF_EXPORT) == 0)
3480 h->ldsym = NULL;
3481 return TRUE;
3484 /* We don't need to add this symbol if we did garbage collection and
3485 we did not mark this symbol. */
3486 if (xcoff_hash_table (ldinfo->info)->gc
3487 && (h->flags & XCOFF_MARK) == 0)
3489 h->ldsym = NULL;
3490 return TRUE;
3493 /* We may have already processed this symbol due to the recursive
3494 call above. */
3495 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3496 return TRUE;
3498 /* We need to add this symbol to the .loader symbols. */
3500 BFD_ASSERT (h->ldsym == NULL);
3501 amt = sizeof (struct internal_ldsym);
3502 h->ldsym = (struct internal_ldsym *) bfd_zalloc (ldinfo->output_bfd, amt);
3503 if (h->ldsym == NULL)
3505 ldinfo->failed = TRUE;
3506 return FALSE;
3509 if ((h->flags & XCOFF_IMPORT) != 0)
3510 h->ldsym->l_ifile = h->ldindx;
3512 /* The first 3 symbol table indices are reserved to indicate the
3513 data, text and bss sections. */
3514 h->ldindx = ldinfo->ldsym_count + 3;
3516 ++ldinfo->ldsym_count;
3518 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3519 h->ldsym, h->root.root.string))
3521 return FALSE;
3524 h->flags |= XCOFF_BUILT_LDSYM;
3526 return TRUE;
3529 /* Do the final link step. */
3531 bfd_boolean
3532 _bfd_xcoff_bfd_final_link (abfd, info)
3533 bfd *abfd;
3534 struct bfd_link_info *info;
3536 bfd_size_type symesz;
3537 struct xcoff_final_link_info finfo;
3538 asection *o;
3539 struct bfd_link_order *p;
3540 bfd_size_type max_contents_size;
3541 bfd_size_type max_sym_count;
3542 bfd_size_type max_lineno_count;
3543 bfd_size_type max_reloc_count;
3544 bfd_size_type max_output_reloc_count;
3545 file_ptr rel_filepos;
3546 unsigned int relsz;
3547 file_ptr line_filepos;
3548 unsigned int linesz;
3549 bfd *sub;
3550 bfd_byte *external_relocs = NULL;
3551 char strbuf[STRING_SIZE_SIZE];
3552 file_ptr pos;
3553 bfd_size_type amt;
3555 if (info->shared)
3556 abfd->flags |= DYNAMIC;
3558 symesz = bfd_coff_symesz (abfd);
3560 finfo.info = info;
3561 finfo.output_bfd = abfd;
3562 finfo.strtab = NULL;
3563 finfo.section_info = NULL;
3564 finfo.last_file_index = -1;
3565 finfo.toc_symindx = -1;
3566 finfo.internal_syms = NULL;
3567 finfo.sym_indices = NULL;
3568 finfo.outsyms = NULL;
3569 finfo.linenos = NULL;
3570 finfo.contents = NULL;
3571 finfo.external_relocs = NULL;
3573 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
3574 + bfd_xcoff_ldhdrsz (abfd));
3575 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
3576 + bfd_xcoff_ldhdrsz(abfd)
3577 + (xcoff_hash_table (info)->ldhdr.l_nsyms
3578 * bfd_xcoff_ldsymsz(abfd)));
3580 xcoff_data (abfd)->coff.link_info = info;
3582 finfo.strtab = _bfd_stringtab_init ();
3583 if (finfo.strtab == NULL)
3584 goto error_return;
3586 /* Count the line number and relocation entries required for the
3587 output file. Determine a few maximum sizes. */
3588 max_contents_size = 0;
3589 max_lineno_count = 0;
3590 max_reloc_count = 0;
3591 for (o = abfd->sections; o != NULL; o = o->next)
3593 o->reloc_count = 0;
3594 o->lineno_count = 0;
3595 for (p = o->link_order_head; p != NULL; p = p->next)
3597 if (p->type == bfd_indirect_link_order)
3599 asection *sec;
3601 sec = p->u.indirect.section;
3603 /* Mark all sections which are to be included in the
3604 link. This will normally be every section. We need
3605 to do this so that we can identify any sections which
3606 the linker has decided to not include. */
3607 sec->linker_mark = TRUE;
3609 if (info->strip == strip_none
3610 || info->strip == strip_some)
3611 o->lineno_count += sec->lineno_count;
3613 o->reloc_count += sec->reloc_count;
3615 if (sec->rawsize > max_contents_size)
3616 max_contents_size = sec->rawsize;
3617 if (sec->size > max_contents_size)
3618 max_contents_size = sec->size;
3619 if (sec->lineno_count > max_lineno_count)
3620 max_lineno_count = sec->lineno_count;
3621 if (coff_section_data (sec->owner, sec) != NULL
3622 && xcoff_section_data (sec->owner, sec) != NULL
3623 && (xcoff_section_data (sec->owner, sec)->lineno_count
3624 > max_lineno_count))
3625 max_lineno_count =
3626 xcoff_section_data (sec->owner, sec)->lineno_count;
3627 if (sec->reloc_count > max_reloc_count)
3628 max_reloc_count = sec->reloc_count;
3630 else if (p->type == bfd_section_reloc_link_order
3631 || p->type == bfd_symbol_reloc_link_order)
3632 ++o->reloc_count;
3636 /* Compute the file positions for all the sections. */
3637 if (abfd->output_has_begun)
3639 if (xcoff_hash_table (info)->file_align != 0)
3640 abort ();
3642 else
3644 bfd_vma file_align;
3646 file_align = xcoff_hash_table (info)->file_align;
3647 if (file_align != 0)
3649 bfd_boolean saw_contents;
3650 int indx;
3651 asection **op;
3652 file_ptr sofar;
3654 /* Insert .pad sections before every section which has
3655 contents and is loaded, if it is preceded by some other
3656 section which has contents and is loaded. */
3657 saw_contents = TRUE;
3658 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3660 if (strcmp ((*op)->name, ".pad") == 0)
3661 saw_contents = FALSE;
3662 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3663 && ((*op)->flags & SEC_LOAD) != 0)
3665 if (! saw_contents)
3666 saw_contents = TRUE;
3667 else
3669 asection *n, **st;
3671 /* Create a pad section and place it before the section
3672 that needs padding. This requires unlinking and
3673 relinking the bfd's section list. */
3675 st = abfd->section_tail;
3676 n = bfd_make_section_anyway (abfd, ".pad");
3677 n->flags = SEC_HAS_CONTENTS;
3678 n->alignment_power = 0;
3680 BFD_ASSERT (*st == n);
3681 bfd_section_list_remove (abfd, st);
3682 bfd_section_list_insert (abfd, op, n);
3684 op = &n->next;
3685 saw_contents = FALSE;
3690 /* Reset the section indices after inserting the new
3691 sections. */
3692 indx = 0;
3693 for (o = abfd->sections; o != NULL; o = o->next)
3695 ++indx;
3696 o->target_index = indx;
3698 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3700 /* Work out appropriate sizes for the .pad sections to force
3701 each section to land on a page boundary. This bit of
3702 code knows what compute_section_file_positions is going
3703 to do. */
3704 sofar = bfd_coff_filhsz (abfd);
3705 sofar += bfd_coff_aoutsz (abfd);
3706 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3707 for (o = abfd->sections; o != NULL; o = o->next)
3708 if ((bfd_xcoff_is_reloc_count_overflow
3709 (abfd, (bfd_vma) o->reloc_count))
3710 || (bfd_xcoff_is_lineno_count_overflow
3711 (abfd, (bfd_vma) o->lineno_count)))
3712 /* 64 does not overflow, need to check if 32 does */
3713 sofar += bfd_coff_scnhsz (abfd);
3715 for (o = abfd->sections; o != NULL; o = o->next)
3717 if (strcmp (o->name, ".pad") == 0)
3719 bfd_vma pageoff;
3721 BFD_ASSERT (o->size == 0);
3722 pageoff = sofar & (file_align - 1);
3723 if (pageoff != 0)
3725 o->size = file_align - pageoff;
3726 sofar += file_align - pageoff;
3727 o->flags |= SEC_HAS_CONTENTS;
3730 else
3732 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3733 sofar += BFD_ALIGN (o->size,
3734 1 << o->alignment_power);
3739 if (! bfd_coff_compute_section_file_positions (abfd))
3740 goto error_return;
3743 /* Allocate space for the pointers we need to keep for the relocs. */
3745 unsigned int i;
3747 /* We use section_count + 1, rather than section_count, because
3748 the target_index fields are 1 based. */
3749 amt = abfd->section_count + 1;
3750 amt *= sizeof (struct xcoff_link_section_info);
3751 finfo.section_info = (struct xcoff_link_section_info *) bfd_malloc (amt);
3752 if (finfo.section_info == NULL)
3753 goto error_return;
3754 for (i = 0; i <= abfd->section_count; i++)
3756 finfo.section_info[i].relocs = NULL;
3757 finfo.section_info[i].rel_hashes = NULL;
3758 finfo.section_info[i].toc_rel_hashes = NULL;
3762 /* Set the file positions for the relocs. */
3763 rel_filepos = obj_relocbase (abfd);
3764 relsz = bfd_coff_relsz (abfd);
3765 max_output_reloc_count = 0;
3766 for (o = abfd->sections; o != NULL; o = o->next)
3768 if (o->reloc_count == 0)
3769 o->rel_filepos = 0;
3770 else
3772 /* A stripped file has no relocs. However, we still
3773 allocate the buffers, so that later code doesn't have to
3774 worry about whether we are stripping or not. */
3775 if (info->strip == strip_all)
3776 o->rel_filepos = 0;
3777 else
3779 o->flags |= SEC_RELOC;
3780 o->rel_filepos = rel_filepos;
3781 rel_filepos += o->reloc_count * relsz;
3784 /* We don't know the indices of global symbols until we have
3785 written out all the local symbols. For each section in
3786 the output file, we keep an array of pointers to hash
3787 table entries. Each entry in the array corresponds to a
3788 reloc. When we find a reloc against a global symbol, we
3789 set the corresponding entry in this array so that we can
3790 fix up the symbol index after we have written out all the
3791 local symbols.
3793 Because of this problem, we also keep the relocs in
3794 memory until the end of the link. This wastes memory.
3795 We could backpatch the file later, I suppose, although it
3796 would be slow. */
3797 amt = o->reloc_count;
3798 amt *= sizeof (struct internal_reloc);
3799 finfo.section_info[o->target_index].relocs =
3800 (struct internal_reloc *) bfd_malloc (amt);
3802 amt = o->reloc_count;
3803 amt *= sizeof (struct xcoff_link_hash_entry *);
3804 finfo.section_info[o->target_index].rel_hashes =
3805 (struct xcoff_link_hash_entry **) bfd_malloc (amt);
3807 if (finfo.section_info[o->target_index].relocs == NULL
3808 || finfo.section_info[o->target_index].rel_hashes == NULL)
3809 goto error_return;
3811 if (o->reloc_count > max_output_reloc_count)
3812 max_output_reloc_count = o->reloc_count;
3816 /* We now know the size of the relocs, so we can determine the file
3817 positions of the line numbers. */
3818 line_filepos = rel_filepos;
3819 finfo.line_filepos = line_filepos;
3820 linesz = bfd_coff_linesz (abfd);
3821 for (o = abfd->sections; o != NULL; o = o->next)
3823 if (o->lineno_count == 0)
3824 o->line_filepos = 0;
3825 else
3827 o->line_filepos = line_filepos;
3828 line_filepos += o->lineno_count * linesz;
3831 /* Reset the reloc and lineno counts, so that we can use them to
3832 count the number of entries we have output so far. */
3833 o->reloc_count = 0;
3834 o->lineno_count = 0;
3837 obj_sym_filepos (abfd) = line_filepos;
3839 /* Figure out the largest number of symbols in an input BFD. Take
3840 the opportunity to clear the output_has_begun fields of all the
3841 input BFD's. We want at least 6 symbols, since that is the
3842 number which xcoff_write_global_symbol may need. */
3843 max_sym_count = 6;
3844 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3846 bfd_size_type sz;
3848 sub->output_has_begun = FALSE;
3849 sz = obj_raw_syment_count (sub);
3850 if (sz > max_sym_count)
3851 max_sym_count = sz;
3854 /* Allocate some buffers used while linking. */
3855 amt = max_sym_count * sizeof (struct internal_syment);
3856 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
3858 amt = max_sym_count * sizeof (long);
3859 finfo.sym_indices = (long *) bfd_malloc (amt);
3861 amt = (max_sym_count + 1) * symesz;
3862 finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
3864 amt = max_lineno_count * bfd_coff_linesz (abfd);
3865 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
3867 amt = max_contents_size;
3868 finfo.contents = (bfd_byte *) bfd_malloc (amt);
3870 amt = max_reloc_count * relsz;
3871 finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
3873 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3874 || (finfo.sym_indices == NULL && max_sym_count > 0)
3875 || finfo.outsyms == NULL
3876 || (finfo.linenos == NULL && max_lineno_count > 0)
3877 || (finfo.contents == NULL && max_contents_size > 0)
3878 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3879 goto error_return;
3881 obj_raw_syment_count (abfd) = 0;
3882 xcoff_data (abfd)->toc = (bfd_vma) -1;
3884 /* We now know the position of everything in the file, except that
3885 we don't know the size of the symbol table and therefore we don't
3886 know where the string table starts. We just build the string
3887 table in memory as we go along. We process all the relocations
3888 for a single input file at once. */
3889 for (o = abfd->sections; o != NULL; o = o->next)
3891 for (p = o->link_order_head; p != NULL; p = p->next)
3893 if (p->type == bfd_indirect_link_order
3894 && p->u.indirect.section->owner->xvec == abfd->xvec)
3896 sub = p->u.indirect.section->owner;
3897 if (! sub->output_has_begun)
3899 if (! xcoff_link_input_bfd (&finfo, sub))
3900 goto error_return;
3901 sub->output_has_begun = TRUE;
3904 else if (p->type == bfd_section_reloc_link_order
3905 || p->type == bfd_symbol_reloc_link_order)
3907 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3908 goto error_return;
3910 else
3912 if (! _bfd_default_link_order (abfd, info, o, p))
3913 goto error_return;
3919 /* Free up the buffers used by xcoff_link_input_bfd. */
3921 if (finfo.internal_syms != NULL)
3923 free (finfo.internal_syms);
3924 finfo.internal_syms = NULL;
3926 if (finfo.sym_indices != NULL)
3928 free (finfo.sym_indices);
3929 finfo.sym_indices = NULL;
3931 if (finfo.linenos != NULL)
3933 free (finfo.linenos);
3934 finfo.linenos = NULL;
3936 if (finfo.contents != NULL)
3938 free (finfo.contents);
3939 finfo.contents = NULL;
3941 if (finfo.external_relocs != NULL)
3943 free (finfo.external_relocs);
3944 finfo.external_relocs = NULL;
3947 /* The value of the last C_FILE symbol is supposed to be -1. Write
3948 it out again. */
3949 if (finfo.last_file_index != -1)
3951 finfo.last_file.n_value = -(bfd_vma) 1;
3952 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3953 (PTR) finfo.outsyms);
3954 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
3955 if (bfd_seek (abfd, pos, SEEK_SET) != 0
3956 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
3957 goto error_return;
3960 /* Write out all the global symbols which do not come from XCOFF
3961 input files. */
3962 xcoff_link_hash_traverse (xcoff_hash_table (info),
3963 xcoff_write_global_symbol,
3964 (PTR) &finfo);
3966 if (finfo.outsyms != NULL)
3968 free (finfo.outsyms);
3969 finfo.outsyms = NULL;
3972 /* Now that we have written out all the global symbols, we know the
3973 symbol indices to use for relocs against them, and we can finally
3974 write out the relocs. */
3975 amt = max_output_reloc_count * relsz;
3976 external_relocs = (bfd_byte *) bfd_malloc (amt);
3977 if (external_relocs == NULL && max_output_reloc_count != 0)
3978 goto error_return;
3980 for (o = abfd->sections; o != NULL; o = o->next)
3982 struct internal_reloc *irel;
3983 struct internal_reloc *irelend;
3984 struct xcoff_link_hash_entry **rel_hash;
3985 struct xcoff_toc_rel_hash *toc_rel_hash;
3986 bfd_byte *erel;
3987 bfd_size_type rel_size;
3989 /* A stripped file has no relocs. */
3990 if (info->strip == strip_all)
3992 o->reloc_count = 0;
3993 continue;
3996 if (o->reloc_count == 0)
3997 continue;
3999 irel = finfo.section_info[o->target_index].relocs;
4000 irelend = irel + o->reloc_count;
4001 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4002 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4004 if (*rel_hash != NULL)
4006 if ((*rel_hash)->indx < 0)
4008 if (! ((*info->callbacks->unattached_reloc)
4009 (info, (*rel_hash)->root.root.string,
4010 (bfd *) NULL, o, irel->r_vaddr)))
4011 goto error_return;
4012 (*rel_hash)->indx = 0;
4014 irel->r_symndx = (*rel_hash)->indx;
4018 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4019 toc_rel_hash != NULL;
4020 toc_rel_hash = toc_rel_hash->next)
4022 if (toc_rel_hash->h->u.toc_indx < 0)
4024 if (! ((*info->callbacks->unattached_reloc)
4025 (info, toc_rel_hash->h->root.root.string,
4026 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4027 goto error_return;
4028 toc_rel_hash->h->u.toc_indx = 0;
4030 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4033 /* XCOFF requires that the relocs be sorted by address. We tend
4034 to produce them in the order in which their containing csects
4035 appear in the symbol table, which is not necessarily by
4036 address. So we sort them here. There may be a better way to
4037 do this. */
4038 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4039 o->reloc_count, sizeof (struct internal_reloc),
4040 xcoff_sort_relocs);
4042 irel = finfo.section_info[o->target_index].relocs;
4043 irelend = irel + o->reloc_count;
4044 erel = external_relocs;
4045 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4046 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4048 rel_size = relsz * o->reloc_count;
4049 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4050 || bfd_bwrite ((PTR) external_relocs, rel_size, abfd) != rel_size)
4051 goto error_return;
4054 if (external_relocs != NULL)
4056 free (external_relocs);
4057 external_relocs = NULL;
4060 /* Free up the section information. */
4061 if (finfo.section_info != NULL)
4063 unsigned int i;
4065 for (i = 0; i < abfd->section_count; i++)
4067 if (finfo.section_info[i].relocs != NULL)
4068 free (finfo.section_info[i].relocs);
4069 if (finfo.section_info[i].rel_hashes != NULL)
4070 free (finfo.section_info[i].rel_hashes);
4072 free (finfo.section_info);
4073 finfo.section_info = NULL;
4076 /* Write out the loader section contents. */
4077 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4078 == (xcoff_hash_table (info)->loader_section->contents
4079 + xcoff_hash_table (info)->ldhdr.l_impoff));
4080 o = xcoff_hash_table (info)->loader_section;
4081 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
4082 (file_ptr) o->output_offset, o->size))
4083 goto error_return;
4085 /* Write out the magic sections. */
4086 o = xcoff_hash_table (info)->linkage_section;
4087 if (o->size > 0
4088 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4089 (file_ptr) o->output_offset,
4090 o->size))
4091 goto error_return;
4092 o = xcoff_hash_table (info)->toc_section;
4093 if (o->size > 0
4094 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4095 (file_ptr) o->output_offset,
4096 o->size))
4097 goto error_return;
4098 o = xcoff_hash_table (info)->descriptor_section;
4099 if (o->size > 0
4100 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4101 (file_ptr) o->output_offset,
4102 o->size))
4103 goto error_return;
4105 /* Write out the string table. */
4106 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
4107 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4108 goto error_return;
4109 H_PUT_32 (abfd,
4110 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4111 strbuf);
4112 amt = STRING_SIZE_SIZE;
4113 if (bfd_bwrite (strbuf, amt, abfd) != amt)
4114 goto error_return;
4115 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4116 goto error_return;
4118 _bfd_stringtab_free (finfo.strtab);
4120 /* Write out the debugging string table. */
4121 o = xcoff_hash_table (info)->debug_section;
4122 if (o != NULL)
4124 struct bfd_strtab_hash *debug_strtab;
4126 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4127 BFD_ASSERT (o->output_section->size - o->output_offset
4128 >= _bfd_stringtab_size (debug_strtab));
4129 pos = o->output_section->filepos + o->output_offset;
4130 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4131 goto error_return;
4132 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4133 goto error_return;
4136 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4137 not try to write out the symbols. */
4138 bfd_get_symcount (abfd) = 0;
4140 return TRUE;
4142 error_return:
4143 if (finfo.strtab != NULL)
4144 _bfd_stringtab_free (finfo.strtab);
4146 if (finfo.section_info != NULL)
4148 unsigned int i;
4150 for (i = 0; i < abfd->section_count; i++)
4152 if (finfo.section_info[i].relocs != NULL)
4153 free (finfo.section_info[i].relocs);
4154 if (finfo.section_info[i].rel_hashes != NULL)
4155 free (finfo.section_info[i].rel_hashes);
4157 free (finfo.section_info);
4160 if (finfo.internal_syms != NULL)
4161 free (finfo.internal_syms);
4162 if (finfo.sym_indices != NULL)
4163 free (finfo.sym_indices);
4164 if (finfo.outsyms != NULL)
4165 free (finfo.outsyms);
4166 if (finfo.linenos != NULL)
4167 free (finfo.linenos);
4168 if (finfo.contents != NULL)
4169 free (finfo.contents);
4170 if (finfo.external_relocs != NULL)
4171 free (finfo.external_relocs);
4172 if (external_relocs != NULL)
4173 free (external_relocs);
4174 return FALSE;
4177 /* Link an input file into the linker output file. This function
4178 handles all the sections and relocations of the input file at once. */
4180 static bfd_boolean
4181 xcoff_link_input_bfd (finfo, input_bfd)
4182 struct xcoff_final_link_info *finfo;
4183 bfd *input_bfd;
4185 bfd *output_bfd;
4186 const char *strings;
4187 bfd_size_type syment_base;
4188 unsigned int n_tmask;
4189 unsigned int n_btshft;
4190 bfd_boolean copy, hash;
4191 bfd_size_type isymesz;
4192 bfd_size_type osymesz;
4193 bfd_size_type linesz;
4194 bfd_byte *esym;
4195 bfd_byte *esym_end;
4196 struct xcoff_link_hash_entry **sym_hash;
4197 struct internal_syment *isymp;
4198 asection **csectpp;
4199 unsigned long *debug_index;
4200 long *indexp;
4201 unsigned long output_index;
4202 bfd_byte *outsym;
4203 unsigned int incls;
4204 asection *oline;
4205 bfd_boolean keep_syms;
4206 asection *o;
4208 /* We can just skip DYNAMIC files, unless this is a static link. */
4209 if ((input_bfd->flags & DYNAMIC) != 0
4210 && ! finfo->info->static_link)
4211 return TRUE;
4213 /* Move all the symbols to the output file. */
4215 output_bfd = finfo->output_bfd;
4216 strings = NULL;
4217 syment_base = obj_raw_syment_count (output_bfd);
4218 isymesz = bfd_coff_symesz (input_bfd);
4219 osymesz = bfd_coff_symesz (output_bfd);
4220 linesz = bfd_coff_linesz (input_bfd);
4221 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4223 n_tmask = coff_data (input_bfd)->local_n_tmask;
4224 n_btshft = coff_data (input_bfd)->local_n_btshft;
4226 /* Define macros so that ISFCN, et. al., macros work correctly. */
4227 #define N_TMASK n_tmask
4228 #define N_BTSHFT n_btshft
4230 copy = FALSE;
4231 if (! finfo->info->keep_memory)
4232 copy = TRUE;
4233 hash = TRUE;
4234 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4235 hash = FALSE;
4237 if (! _bfd_coff_get_external_symbols (input_bfd))
4238 return FALSE;
4240 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4241 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4242 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4243 csectpp = xcoff_data (input_bfd)->csects;
4244 debug_index = xcoff_data (input_bfd)->debug_indices;
4245 isymp = finfo->internal_syms;
4246 indexp = finfo->sym_indices;
4247 output_index = syment_base;
4248 outsym = finfo->outsyms;
4249 incls = 0;
4250 oline = NULL;
4252 while (esym < esym_end)
4255 struct internal_syment isym;
4256 union internal_auxent aux;
4257 int smtyp = 0;
4258 bfd_boolean skip;
4259 bfd_boolean require;
4260 int add;
4262 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4264 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4265 information. */
4266 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4268 BFD_ASSERT (isymp->n_numaux > 0);
4269 bfd_coff_swap_aux_in (input_bfd,
4270 (PTR) (esym + isymesz * isymp->n_numaux),
4271 isymp->n_type, isymp->n_sclass,
4272 isymp->n_numaux - 1, isymp->n_numaux,
4273 (PTR) &aux);
4275 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4278 /* Make a copy of *isymp so that the relocate_section function
4279 always sees the original values. This is more reliable than
4280 always recomputing the symbol value even if we are stripping
4281 the symbol. */
4282 isym = *isymp;
4284 /* If this symbol is in the .loader section, swap out the
4285 .loader symbol information. If this is an external symbol
4286 reference to a defined symbol, though, then wait until we get
4287 to the definition. */
4288 if (isym.n_sclass == C_EXT
4289 && *sym_hash != NULL
4290 && (*sym_hash)->ldsym != NULL
4291 && (smtyp != XTY_ER
4292 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4294 struct xcoff_link_hash_entry *h;
4295 struct internal_ldsym *ldsym;
4297 h = *sym_hash;
4298 ldsym = h->ldsym;
4299 if (isym.n_scnum > 0)
4301 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4302 ldsym->l_value = (isym.n_value
4303 + (*csectpp)->output_section->vma
4304 + (*csectpp)->output_offset
4305 - (*csectpp)->vma);
4307 else
4309 ldsym->l_scnum = isym.n_scnum;
4310 ldsym->l_value = isym.n_value;
4313 ldsym->l_smtype = smtyp;
4314 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4315 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4316 || (h->flags & XCOFF_IMPORT) != 0)
4317 ldsym->l_smtype |= L_IMPORT;
4318 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4319 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4320 || (h->flags & XCOFF_EXPORT) != 0)
4321 ldsym->l_smtype |= L_EXPORT;
4322 if ((h->flags & XCOFF_ENTRY) != 0)
4323 ldsym->l_smtype |= L_ENTRY;
4325 ldsym->l_smclas = aux.x_csect.x_smclas;
4327 if (ldsym->l_ifile == (bfd_size_type) -1)
4328 ldsym->l_ifile = 0;
4329 else if (ldsym->l_ifile == 0)
4331 if ((ldsym->l_smtype & L_IMPORT) == 0)
4332 ldsym->l_ifile = 0;
4333 else
4335 bfd *impbfd;
4337 if (h->root.type == bfd_link_hash_defined
4338 || h->root.type == bfd_link_hash_defweak)
4339 impbfd = h->root.u.def.section->owner;
4340 else if (h->root.type == bfd_link_hash_undefined
4341 || h->root.type == bfd_link_hash_undefweak)
4342 impbfd = h->root.u.undef.abfd;
4343 else
4344 impbfd = NULL;
4346 if (impbfd == NULL)
4347 ldsym->l_ifile = 0;
4348 else
4350 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4351 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4356 ldsym->l_parm = 0;
4358 BFD_ASSERT (h->ldindx >= 0);
4359 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4360 (finfo->ldsym
4361 + ((h->ldindx - 3)
4362 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
4363 h->ldsym = NULL;
4365 /* Fill in snentry now that we know the target_index. */
4366 if ((h->flags & XCOFF_ENTRY) != 0
4367 && (h->root.type == bfd_link_hash_defined
4368 || h->root.type == bfd_link_hash_defweak))
4370 xcoff_data (output_bfd)->snentry =
4371 h->root.u.def.section->output_section->target_index;
4375 *indexp = -1;
4377 skip = FALSE;
4378 require = FALSE;
4379 add = 1 + isym.n_numaux;
4381 /* If we are skipping this csect, we want to skip this symbol. */
4382 if (*csectpp == NULL)
4383 skip = TRUE;
4385 /* If we garbage collected this csect, we want to skip this
4386 symbol. */
4387 if (! skip
4388 && xcoff_hash_table (finfo->info)->gc
4389 && ((*csectpp)->flags & SEC_MARK) == 0
4390 && *csectpp != bfd_abs_section_ptr)
4391 skip = TRUE;
4393 /* An XCOFF linker always skips C_STAT symbols. */
4394 if (! skip
4395 && isymp->n_sclass == C_STAT)
4396 skip = TRUE;
4398 /* We skip all but the first TOC anchor. */
4399 if (! skip
4400 && isymp->n_sclass == C_HIDEXT
4401 && aux.x_csect.x_smclas == XMC_TC0)
4403 if (finfo->toc_symindx != -1)
4404 skip = TRUE;
4405 else
4407 bfd_vma tocval, tocend;
4408 bfd *inp;
4410 tocval = ((*csectpp)->output_section->vma
4411 + (*csectpp)->output_offset
4412 + isym.n_value
4413 - (*csectpp)->vma);
4415 /* We want to find out if tocval is a good value to use
4416 as the TOC anchor--that is, whether we can access all
4417 of the TOC using a 16 bit offset from tocval. This
4418 test assumes that the TOC comes at the end of the
4419 output section, as it does in the default linker
4420 script. */
4421 tocend = ((*csectpp)->output_section->vma
4422 + (*csectpp)->output_section->size);
4423 for (inp = finfo->info->input_bfds;
4424 inp != NULL;
4425 inp = inp->link_next)
4428 for (o = inp->sections; o != NULL; o = o->next)
4429 if (strcmp (o->name, ".tocbss") == 0)
4431 bfd_vma new_toc_end;
4432 new_toc_end = (o->output_section->vma
4433 + o->output_offset
4434 + o->size);
4435 if (new_toc_end > tocend)
4436 tocend = new_toc_end;
4441 if (tocval + 0x10000 < tocend)
4443 (*_bfd_error_handler)
4444 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4445 (unsigned long) (tocend - tocval));
4446 bfd_set_error (bfd_error_file_too_big);
4447 return FALSE;
4450 if (tocval + 0x8000 < tocend)
4452 bfd_vma tocadd;
4454 tocadd = tocend - (tocval + 0x8000);
4455 tocval += tocadd;
4456 isym.n_value += tocadd;
4459 finfo->toc_symindx = output_index;
4460 xcoff_data (finfo->output_bfd)->toc = tocval;
4461 xcoff_data (finfo->output_bfd)->sntoc =
4462 (*csectpp)->output_section->target_index;
4463 require = TRUE;
4468 /* If we are stripping all symbols, we want to skip this one. */
4469 if (! skip
4470 && finfo->info->strip == strip_all)
4471 skip = TRUE;
4473 /* We can skip resolved external references. */
4474 if (! skip
4475 && isym.n_sclass == C_EXT
4476 && smtyp == XTY_ER
4477 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4478 skip = TRUE;
4480 /* We can skip common symbols if they got defined somewhere
4481 else. */
4482 if (! skip
4483 && isym.n_sclass == C_EXT
4484 && smtyp == XTY_CM
4485 && ((*sym_hash)->root.type != bfd_link_hash_common
4486 || (*sym_hash)->root.u.c.p->section != *csectpp)
4487 && ((*sym_hash)->root.type != bfd_link_hash_defined
4488 || (*sym_hash)->root.u.def.section != *csectpp))
4489 skip = TRUE;
4491 /* Skip local symbols if we are discarding them. */
4492 if (! skip
4493 && finfo->info->discard == discard_all
4494 && isym.n_sclass != C_EXT
4495 && (isym.n_sclass != C_HIDEXT
4496 || smtyp != XTY_SD))
4497 skip = TRUE;
4499 /* If we stripping debugging symbols, and this is a debugging
4500 symbol, then skip it. */
4501 if (! skip
4502 && finfo->info->strip == strip_debugger
4503 && isym.n_scnum == N_DEBUG)
4504 skip = TRUE;
4506 /* If some symbols are stripped based on the name, work out the
4507 name and decide whether to skip this symbol. We don't handle
4508 this correctly for symbols whose names are in the .debug
4509 section; to get it right we would need a new bfd_strtab_hash
4510 function to return the string given the index. */
4511 if (! skip
4512 && (finfo->info->strip == strip_some
4513 || finfo->info->discard == discard_l)
4514 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4516 const char *name;
4517 char buf[SYMNMLEN + 1];
4519 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4521 if (name == NULL)
4522 return FALSE;
4524 if ((finfo->info->strip == strip_some
4525 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
4526 FALSE) == NULL))
4527 || (finfo->info->discard == discard_l
4528 && (isym.n_sclass != C_EXT
4529 && (isym.n_sclass != C_HIDEXT
4530 || smtyp != XTY_SD))
4531 && bfd_is_local_label_name (input_bfd, name)))
4532 skip = TRUE;
4535 /* We can not skip the first TOC anchor. */
4536 if (skip
4537 && require
4538 && finfo->info->strip != strip_all)
4539 skip = FALSE;
4541 /* We now know whether we are to skip this symbol or not. */
4542 if (! skip)
4544 /* Adjust the symbol in order to output it. */
4546 if (isym._n._n_n._n_zeroes == 0
4547 && isym._n._n_n._n_offset != 0)
4549 /* This symbol has a long name. Enter it in the string
4550 table we are building. If *debug_index != -1, the
4551 name has already been entered in the .debug section. */
4552 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4553 isym._n._n_n._n_offset = *debug_index;
4554 else
4556 const char *name;
4557 bfd_size_type indx;
4559 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4560 (char *) NULL);
4562 if (name == NULL)
4563 return FALSE;
4564 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4565 if (indx == (bfd_size_type) -1)
4566 return FALSE;
4567 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4571 if (isym.n_sclass != C_BSTAT
4572 && isym.n_sclass != C_ESTAT
4573 && isym.n_sclass != C_DECL
4574 && isym.n_scnum > 0)
4576 isym.n_scnum = (*csectpp)->output_section->target_index;
4577 isym.n_value += ((*csectpp)->output_section->vma
4578 + (*csectpp)->output_offset
4579 - (*csectpp)->vma);
4582 /* The value of a C_FILE symbol is the symbol index of the
4583 next C_FILE symbol. The value of the last C_FILE symbol
4584 is -1. We try to get this right, below, just before we
4585 write the symbols out, but in the general case we may
4586 have to write the symbol out twice. */
4587 if (isym.n_sclass == C_FILE)
4589 if (finfo->last_file_index != -1
4590 && finfo->last_file.n_value != (bfd_vma) output_index)
4592 /* We must correct the value of the last C_FILE entry. */
4593 finfo->last_file.n_value = output_index;
4594 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4596 /* The last C_FILE symbol is in this input file. */
4597 bfd_coff_swap_sym_out (output_bfd,
4598 (PTR) &finfo->last_file,
4599 (PTR) (finfo->outsyms
4600 + ((finfo->last_file_index
4601 - syment_base)
4602 * osymesz)));
4604 else
4606 /* We have already written out the last C_FILE
4607 symbol. We need to write it out again. We
4608 borrow *outsym temporarily. */
4609 file_ptr pos;
4611 bfd_coff_swap_sym_out (output_bfd,
4612 (PTR) &finfo->last_file,
4613 (PTR) outsym);
4615 pos = obj_sym_filepos (output_bfd);
4616 pos += finfo->last_file_index * osymesz;
4617 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4618 || (bfd_bwrite (outsym, osymesz, output_bfd)
4619 != osymesz))
4620 return FALSE;
4624 finfo->last_file_index = output_index;
4625 finfo->last_file = isym;
4628 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4629 into the line numbers. We update the symbol values when
4630 we handle the line numbers. */
4631 if (isym.n_sclass == C_BINCL
4632 || isym.n_sclass == C_EINCL)
4634 isym.n_value = finfo->line_filepos;
4635 ++incls;
4638 /* Output the symbol. */
4640 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4642 *indexp = output_index;
4644 if (isym.n_sclass == C_EXT)
4646 long indx;
4647 struct xcoff_link_hash_entry *h;
4649 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4650 / isymesz);
4651 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4652 BFD_ASSERT (h != NULL);
4653 h->indx = output_index;
4656 /* If this is a symbol in the TOC which we may have merged
4657 (class XMC_TC), remember the symbol index of the TOC
4658 symbol. */
4659 if (isym.n_sclass == C_HIDEXT
4660 && aux.x_csect.x_smclas == XMC_TC
4661 && *sym_hash != NULL)
4663 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4664 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4665 (*sym_hash)->u.toc_indx = output_index;
4668 output_index += add;
4669 outsym += add * osymesz;
4672 esym += add * isymesz;
4673 isymp += add;
4674 csectpp += add;
4675 sym_hash += add;
4676 if (debug_index != NULL)
4677 debug_index += add;
4678 ++indexp;
4679 for (--add; add > 0; --add)
4680 *indexp++ = -1;
4683 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4684 done in a separate pass, because we don't know the correct symbol
4685 indices until we have already decided which symbols we are going
4686 to keep. */
4688 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4689 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4690 isymp = finfo->internal_syms;
4691 indexp = finfo->sym_indices;
4692 csectpp = xcoff_data (input_bfd)->csects;
4693 outsym = finfo->outsyms;
4694 while (esym < esym_end)
4696 int add;
4698 add = 1 + isymp->n_numaux;
4700 if (*indexp < 0)
4701 esym += add * isymesz;
4702 else
4704 int i;
4706 if (isymp->n_sclass == C_BSTAT)
4708 struct internal_syment isym;
4710 bfd_vma indx;
4712 /* The value of a C_BSTAT symbol is the symbol table
4713 index of the containing csect. */
4714 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4715 indx = isym.n_value;
4716 if (indx < obj_raw_syment_count (input_bfd))
4718 long symindx;
4720 symindx = finfo->sym_indices[indx];
4721 if (symindx < 0)
4722 isym.n_value = 0;
4723 else
4724 isym.n_value = symindx;
4725 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4726 (PTR) outsym);
4730 esym += isymesz;
4731 outsym += osymesz;
4733 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4735 union internal_auxent aux;
4737 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4738 isymp->n_sclass, i, isymp->n_numaux,
4739 (PTR) &aux);
4741 if (isymp->n_sclass == C_FILE)
4743 /* This is the file name (or some comment put in by
4744 the compiler). If it is long, we must put it in
4745 the string table. */
4746 if (aux.x_file.x_n.x_zeroes == 0
4747 && aux.x_file.x_n.x_offset != 0)
4749 const char *filename;
4750 bfd_size_type indx;
4752 BFD_ASSERT (aux.x_file.x_n.x_offset
4753 >= STRING_SIZE_SIZE);
4754 if (strings == NULL)
4756 strings = _bfd_coff_read_string_table (input_bfd);
4757 if (strings == NULL)
4758 return FALSE;
4760 filename = strings + aux.x_file.x_n.x_offset;
4761 indx = _bfd_stringtab_add (finfo->strtab, filename,
4762 hash, copy);
4763 if (indx == (bfd_size_type) -1)
4764 return FALSE;
4765 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4768 else if ((isymp->n_sclass == C_EXT
4769 || isymp->n_sclass == C_HIDEXT)
4770 && i + 1 == isymp->n_numaux)
4773 /* We don't support type checking. I don't know if
4774 anybody does. */
4775 aux.x_csect.x_parmhash = 0;
4776 /* I don't think anybody uses these fields, but we'd
4777 better clobber them just in case. */
4778 aux.x_csect.x_stab = 0;
4779 aux.x_csect.x_snstab = 0;
4781 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4783 unsigned long indx;
4785 indx = aux.x_csect.x_scnlen.l;
4786 if (indx < obj_raw_syment_count (input_bfd))
4788 long symindx;
4790 symindx = finfo->sym_indices[indx];
4791 if (symindx < 0)
4793 aux.x_csect.x_scnlen.l = 0;
4795 else
4797 aux.x_csect.x_scnlen.l = symindx;
4802 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4804 unsigned long indx;
4806 if (ISFCN (isymp->n_type)
4807 || ISTAG (isymp->n_sclass)
4808 || isymp->n_sclass == C_BLOCK
4809 || isymp->n_sclass == C_FCN)
4811 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4812 if (indx > 0
4813 && indx < obj_raw_syment_count (input_bfd))
4815 /* We look forward through the symbol for
4816 the index of the next symbol we are going
4817 to include. I don't know if this is
4818 entirely right. */
4819 while (finfo->sym_indices[indx] < 0
4820 && indx < obj_raw_syment_count (input_bfd))
4821 ++indx;
4822 if (indx >= obj_raw_syment_count (input_bfd))
4823 indx = output_index;
4824 else
4825 indx = finfo->sym_indices[indx];
4826 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4831 indx = aux.x_sym.x_tagndx.l;
4832 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4834 long symindx;
4836 symindx = finfo->sym_indices[indx];
4837 if (symindx < 0)
4838 aux.x_sym.x_tagndx.l = 0;
4839 else
4840 aux.x_sym.x_tagndx.l = symindx;
4845 /* Copy over the line numbers, unless we are stripping
4846 them. We do this on a symbol by symbol basis in
4847 order to more easily handle garbage collection. */
4848 if ((isymp->n_sclass == C_EXT
4849 || isymp->n_sclass == C_HIDEXT)
4850 && i == 0
4851 && isymp->n_numaux > 1
4852 && ISFCN (isymp->n_type)
4853 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4855 if (finfo->info->strip != strip_none
4856 && finfo->info->strip != strip_some)
4857 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4858 else
4860 asection *enclosing;
4861 unsigned int enc_count;
4862 bfd_signed_vma linoff;
4863 struct internal_lineno lin;
4865 o = *csectpp;
4866 enclosing = xcoff_section_data (abfd, o)->enclosing;
4867 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4868 if (oline != enclosing)
4870 file_ptr pos = enclosing->line_filepos;
4871 bfd_size_type amt = linesz * enc_count;
4872 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4873 || (bfd_bread (finfo->linenos, amt, input_bfd)
4874 != amt))
4875 return FALSE;
4876 oline = enclosing;
4879 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4880 - enclosing->line_filepos);
4882 bfd_coff_swap_lineno_in (input_bfd,
4883 (PTR) (finfo->linenos + linoff),
4884 (PTR) &lin);
4885 if (lin.l_lnno != 0
4886 || ((bfd_size_type) lin.l_addr.l_symndx
4887 != ((esym
4888 - isymesz
4889 - ((bfd_byte *)
4890 obj_coff_external_syms (input_bfd)))
4891 / isymesz)))
4892 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4893 else
4895 bfd_byte *linpend, *linp;
4896 bfd_vma offset;
4897 bfd_size_type count;
4899 lin.l_addr.l_symndx = *indexp;
4900 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4901 (PTR) (finfo->linenos
4902 + linoff));
4904 linpend = (finfo->linenos
4905 + enc_count * linesz);
4906 offset = (o->output_section->vma
4907 + o->output_offset
4908 - o->vma);
4909 for (linp = finfo->linenos + linoff + linesz;
4910 linp < linpend;
4911 linp += linesz)
4913 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4914 (PTR) &lin);
4915 if (lin.l_lnno == 0)
4916 break;
4917 lin.l_addr.l_paddr += offset;
4918 bfd_coff_swap_lineno_out (output_bfd,
4919 (PTR) &lin,
4920 (PTR) linp);
4923 count = (linp - (finfo->linenos + linoff)) / linesz;
4925 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4926 (o->output_section->line_filepos
4927 + o->output_section->lineno_count * linesz);
4929 if (bfd_seek (output_bfd,
4930 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4931 SEEK_SET) != 0
4932 || (bfd_bwrite (finfo->linenos + linoff,
4933 linesz * count, output_bfd)
4934 != linesz * count))
4935 return FALSE;
4937 o->output_section->lineno_count += count;
4939 if (incls > 0)
4941 struct internal_syment *iisp, *iispend;
4942 long *iindp;
4943 bfd_byte *oos;
4944 int iiadd;
4946 /* Update any C_BINCL or C_EINCL symbols
4947 that refer to a line number in the
4948 range we just output. */
4949 iisp = finfo->internal_syms;
4950 iispend = (iisp
4951 + obj_raw_syment_count (input_bfd));
4952 iindp = finfo->sym_indices;
4953 oos = finfo->outsyms;
4954 while (iisp < iispend)
4956 if (*iindp >= 0
4957 && (iisp->n_sclass == C_BINCL
4958 || iisp->n_sclass == C_EINCL)
4959 && ((bfd_size_type) iisp->n_value
4960 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4961 && ((bfd_size_type) iisp->n_value
4962 < (enclosing->line_filepos
4963 + enc_count * linesz)))
4965 struct internal_syment iis;
4967 bfd_coff_swap_sym_in (output_bfd,
4968 (PTR) oos,
4969 (PTR) &iis);
4970 iis.n_value =
4971 (iisp->n_value
4972 - enclosing->line_filepos
4973 - linoff
4974 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4975 bfd_coff_swap_sym_out (output_bfd,
4976 (PTR) &iis,
4977 (PTR) oos);
4978 --incls;
4981 iiadd = 1 + iisp->n_numaux;
4982 if (*iindp >= 0)
4983 oos += iiadd * osymesz;
4984 iisp += iiadd;
4985 iindp += iiadd;
4992 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4993 isymp->n_sclass, i, isymp->n_numaux,
4994 (PTR) outsym);
4995 outsym += osymesz;
4996 esym += isymesz;
5000 indexp += add;
5001 isymp += add;
5002 csectpp += add;
5005 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5006 symbol will be the first symbol in the next input file. In the
5007 normal case, this will save us from writing out the C_FILE symbol
5008 again. */
5009 if (finfo->last_file_index != -1
5010 && (bfd_size_type) finfo->last_file_index >= syment_base)
5012 finfo->last_file.n_value = output_index;
5013 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5014 (PTR) (finfo->outsyms
5015 + ((finfo->last_file_index - syment_base)
5016 * osymesz)));
5019 /* Write the modified symbols to the output file. */
5020 if (outsym > finfo->outsyms)
5022 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
5023 bfd_size_type amt = outsym - finfo->outsyms;
5024 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5025 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5026 return FALSE;
5028 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5029 + (outsym - finfo->outsyms) / osymesz)
5030 == output_index);
5032 obj_raw_syment_count (output_bfd) = output_index;
5035 /* Don't let the linker relocation routines discard the symbols. */
5036 keep_syms = obj_coff_keep_syms (input_bfd);
5037 obj_coff_keep_syms (input_bfd) = TRUE;
5039 /* Relocate the contents of each section. */
5040 for (o = input_bfd->sections; o != NULL; o = o->next)
5043 bfd_byte *contents;
5045 if (! o->linker_mark)
5047 /* This section was omitted from the link. */
5048 continue;
5051 if ((o->flags & SEC_HAS_CONTENTS) == 0
5052 || o->size == 0
5053 || (o->flags & SEC_IN_MEMORY) != 0)
5054 continue;
5056 /* We have set filepos correctly for the sections we created to
5057 represent csects, so bfd_get_section_contents should work. */
5058 if (coff_section_data (input_bfd, o) != NULL
5059 && coff_section_data (input_bfd, o)->contents != NULL)
5060 contents = coff_section_data (input_bfd, o)->contents;
5061 else
5063 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
5064 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
5065 return FALSE;
5066 contents = finfo->contents;
5069 if ((o->flags & SEC_RELOC) != 0)
5071 int target_index;
5072 struct internal_reloc *internal_relocs;
5073 struct internal_reloc *irel;
5074 bfd_vma offset;
5075 struct internal_reloc *irelend;
5076 struct xcoff_link_hash_entry **rel_hash;
5077 long r_symndx;
5079 /* Read in the relocs. */
5080 target_index = o->output_section->target_index;
5081 internal_relocs = (xcoff_read_internal_relocs
5082 (input_bfd, o, FALSE, finfo->external_relocs,
5083 TRUE,
5084 (finfo->section_info[target_index].relocs
5085 + o->output_section->reloc_count)));
5086 if (internal_relocs == NULL)
5087 return FALSE;
5089 /* Call processor specific code to relocate the section
5090 contents. */
5091 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5092 input_bfd, o,
5093 contents,
5094 internal_relocs,
5095 finfo->internal_syms,
5096 xcoff_data (input_bfd)->csects))
5097 return FALSE;
5099 offset = o->output_section->vma + o->output_offset - o->vma;
5100 irel = internal_relocs;
5101 irelend = irel + o->reloc_count;
5102 rel_hash = (finfo->section_info[target_index].rel_hashes
5103 + o->output_section->reloc_count);
5104 for (; irel < irelend; irel++, rel_hash++)
5106 struct xcoff_link_hash_entry *h = NULL;
5107 struct internal_ldrel ldrel;
5108 bfd_boolean quiet;
5110 *rel_hash = NULL;
5112 /* Adjust the reloc address and symbol index. */
5114 irel->r_vaddr += offset;
5116 r_symndx = irel->r_symndx;
5118 if (r_symndx == -1)
5119 h = NULL;
5120 else
5121 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5123 if (r_symndx != -1 && finfo->info->strip != strip_all)
5125 if (h != NULL
5126 && h->smclas != XMC_TD
5127 && (irel->r_type == R_TOC
5128 || irel->r_type == R_GL
5129 || irel->r_type == R_TCL
5130 || irel->r_type == R_TRL
5131 || irel->r_type == R_TRLA))
5133 /* This is a TOC relative reloc with a symbol
5134 attached. The symbol should be the one which
5135 this reloc is for. We want to make this
5136 reloc against the TOC address of the symbol,
5137 not the symbol itself. */
5138 BFD_ASSERT (h->toc_section != NULL);
5139 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5140 if (h->u.toc_indx != -1)
5141 irel->r_symndx = h->u.toc_indx;
5142 else
5144 struct xcoff_toc_rel_hash *n;
5145 struct xcoff_link_section_info *si;
5146 bfd_size_type amt;
5148 amt = sizeof (struct xcoff_toc_rel_hash);
5149 n = ((struct xcoff_toc_rel_hash *)
5150 bfd_alloc (finfo->output_bfd, amt));
5151 if (n == NULL)
5152 return FALSE;
5153 si = finfo->section_info + target_index;
5154 n->next = si->toc_rel_hashes;
5155 n->h = h;
5156 n->rel = irel;
5157 si->toc_rel_hashes = n;
5160 else if (h != NULL)
5162 /* This is a global symbol. */
5163 if (h->indx >= 0)
5164 irel->r_symndx = h->indx;
5165 else
5167 /* This symbol is being written at the end
5168 of the file, and we do not yet know the
5169 symbol index. We save the pointer to the
5170 hash table entry in the rel_hash list.
5171 We set the indx field to -2 to indicate
5172 that this symbol must not be stripped. */
5173 *rel_hash = h;
5174 h->indx = -2;
5177 else
5179 long indx;
5181 indx = finfo->sym_indices[r_symndx];
5183 if (indx == -1)
5185 struct internal_syment *is;
5187 /* Relocations against a TC0 TOC anchor are
5188 automatically transformed to be against
5189 the TOC anchor in the output file. */
5190 is = finfo->internal_syms + r_symndx;
5191 if (is->n_sclass == C_HIDEXT
5192 && is->n_numaux > 0)
5194 PTR auxptr;
5195 union internal_auxent aux;
5197 auxptr = ((PTR)
5198 (((bfd_byte *)
5199 obj_coff_external_syms (input_bfd))
5200 + ((r_symndx + is->n_numaux)
5201 * isymesz)));
5202 bfd_coff_swap_aux_in (input_bfd, auxptr,
5203 is->n_type, is->n_sclass,
5204 is->n_numaux - 1,
5205 is->n_numaux,
5206 (PTR) &aux);
5207 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5208 && aux.x_csect.x_smclas == XMC_TC0)
5209 indx = finfo->toc_symindx;
5213 if (indx != -1)
5214 irel->r_symndx = indx;
5215 else
5218 struct internal_syment *is;
5220 const char *name;
5221 char buf[SYMNMLEN + 1];
5223 /* This reloc is against a symbol we are
5224 stripping. It would be possible to handle
5225 this case, but I don't think it's worth it. */
5226 is = finfo->internal_syms + r_symndx;
5228 name = (_bfd_coff_internal_syment_name
5229 (input_bfd, is, buf));
5231 if (name == NULL)
5232 return FALSE;
5234 if (! ((*finfo->info->callbacks->unattached_reloc)
5235 (finfo->info, name, input_bfd, o,
5236 irel->r_vaddr)))
5237 return FALSE;
5242 quiet = FALSE;
5243 switch (irel->r_type)
5245 default:
5246 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)
5250 break;
5251 /* Fall through. */
5252 case R_POS:
5253 case R_NEG:
5254 case R_RL:
5255 case R_RLA:
5256 /* This reloc needs to be copied into the .loader
5257 section. */
5258 ldrel.l_vaddr = irel->r_vaddr;
5259 if (r_symndx == -1)
5260 ldrel.l_symndx = -(bfd_size_type ) 1;
5261 else if (h == NULL
5262 || (h->root.type == bfd_link_hash_defined
5263 || h->root.type == bfd_link_hash_defweak
5264 || h->root.type == bfd_link_hash_common))
5266 asection *sec;
5268 if (h == NULL)
5269 sec = xcoff_data (input_bfd)->csects[r_symndx];
5270 else if (h->root.type == bfd_link_hash_common)
5271 sec = h->root.u.c.p->section;
5272 else
5273 sec = h->root.u.def.section;
5274 sec = sec->output_section;
5276 if (strcmp (sec->name, ".text") == 0)
5277 ldrel.l_symndx = 0;
5278 else if (strcmp (sec->name, ".data") == 0)
5279 ldrel.l_symndx = 1;
5280 else if (strcmp (sec->name, ".bss") == 0)
5281 ldrel.l_symndx = 2;
5282 else
5284 (*_bfd_error_handler)
5285 (_("%B: loader reloc in unrecognized section `%A'"),
5286 input_bfd, sec);
5287 bfd_set_error (bfd_error_nonrepresentable_section);
5288 return FALSE;
5291 else
5293 if (! finfo->info->relocatable
5294 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5295 && (h->flags & XCOFF_IMPORT) == 0)
5297 /* We already called the undefined_symbol
5298 callback for this relocation, in
5299 _bfd_ppc_xcoff_relocate_section. Don't
5300 issue any more warnings. */
5301 quiet = TRUE;
5303 if (h->ldindx < 0 && ! quiet)
5305 (*_bfd_error_handler)
5306 (_("%B: `%s' in loader reloc but not loader sym"),
5307 input_bfd,
5308 h->root.root.string);
5309 bfd_set_error (bfd_error_bad_value);
5310 return FALSE;
5312 ldrel.l_symndx = h->ldindx;
5314 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5315 ldrel.l_rsecnm = o->output_section->target_index;
5316 if (xcoff_hash_table (finfo->info)->textro
5317 && strcmp (o->output_section->name, ".text") == 0
5318 && ! quiet)
5320 (*_bfd_error_handler)
5321 (_("%B: loader reloc in read-only section %A"),
5322 input_bfd, o->output_section);
5323 bfd_set_error (bfd_error_invalid_operation);
5324 return FALSE;
5326 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
5327 finfo->ldrel);
5329 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5330 break;
5332 case R_TOC:
5333 case R_GL:
5334 case R_TCL:
5335 case R_TRL:
5336 case R_TRLA:
5337 /* We should never need a .loader reloc for a TOC
5338 relative reloc. */
5339 break;
5343 o->output_section->reloc_count += o->reloc_count;
5346 /* Write out the modified section contents. */
5347 if (! bfd_set_section_contents (output_bfd, o->output_section,
5348 contents, (file_ptr) o->output_offset,
5349 o->size))
5350 return FALSE;
5353 obj_coff_keep_syms (input_bfd) = keep_syms;
5355 if (! finfo->info->keep_memory)
5357 if (! _bfd_coff_free_symbols (input_bfd))
5358 return FALSE;
5361 return TRUE;
5364 #undef N_TMASK
5365 #undef N_BTSHFT
5367 /* Write out a non-XCOFF global symbol. */
5370 static bfd_boolean
5371 xcoff_write_global_symbol (h, inf)
5372 struct xcoff_link_hash_entry *h;
5373 PTR inf;
5375 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
5376 bfd *output_bfd;
5377 bfd_byte *outsym;
5378 struct internal_syment isym;
5379 union internal_auxent aux;
5380 bfd_boolean result;
5381 file_ptr pos;
5382 bfd_size_type amt;
5384 output_bfd = finfo->output_bfd;
5385 outsym = finfo->outsyms;
5387 if (h->root.type == bfd_link_hash_warning)
5389 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5390 if (h->root.type == bfd_link_hash_new)
5391 return TRUE;
5394 /* If this symbol was garbage collected, just skip it. */
5395 if (xcoff_hash_table (finfo->info)->gc
5396 && (h->flags & XCOFF_MARK) == 0)
5397 return TRUE;
5399 /* If we need a .loader section entry, write it out. */
5400 if (h->ldsym != NULL)
5402 struct internal_ldsym *ldsym;
5403 bfd *impbfd;
5405 ldsym = h->ldsym;
5407 if (h->root.type == bfd_link_hash_undefined
5408 || h->root.type == bfd_link_hash_undefweak)
5411 ldsym->l_value = 0;
5412 ldsym->l_scnum = N_UNDEF;
5413 ldsym->l_smtype = XTY_ER;
5414 impbfd = h->root.u.undef.abfd;
5417 else if (h->root.type == bfd_link_hash_defined
5418 || h->root.type == bfd_link_hash_defweak)
5421 asection *sec;
5423 sec = h->root.u.def.section;
5424 ldsym->l_value = (sec->output_section->vma
5425 + sec->output_offset
5426 + h->root.u.def.value);
5427 ldsym->l_scnum = sec->output_section->target_index;
5428 ldsym->l_smtype = XTY_SD;
5429 impbfd = sec->owner;
5432 else
5433 abort ();
5435 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5436 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5437 || (h->flags & XCOFF_IMPORT) != 0)
5439 /* Clear l_smtype
5440 Import symbols are defined so the check above will make
5441 the l_smtype XTY_SD. But this is not correct, it should
5442 be cleared. */
5443 ldsym->l_smtype |= L_IMPORT;
5446 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5447 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5448 || (h->flags & XCOFF_EXPORT) != 0)
5450 ldsym->l_smtype |= L_EXPORT;
5453 if ((h->flags & XCOFF_ENTRY) != 0)
5455 ldsym->l_smtype |= L_ENTRY;
5458 if ((h->flags & XCOFF_RTINIT) != 0)
5460 ldsym->l_smtype = XTY_SD;
5463 ldsym->l_smclas = h->smclas;
5465 if (ldsym->l_smtype & L_IMPORT)
5467 if ((h->root.type == bfd_link_hash_defined
5468 || h->root.type == bfd_link_hash_defweak)
5469 && (h->root.u.def.value != 0))
5471 ldsym->l_smclas = XMC_XO;
5473 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5474 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5476 ldsym->l_smclas = XMC_SV3264;
5478 else if (h->flags & XCOFF_SYSCALL32)
5480 ldsym->l_smclas = XMC_SV;
5482 else if (h->flags & XCOFF_SYSCALL64)
5484 ldsym->l_smclas = XMC_SV64;
5488 if (ldsym->l_ifile == -(bfd_size_type) 1)
5490 ldsym->l_ifile = 0;
5492 else if (ldsym->l_ifile == 0)
5494 if ((ldsym->l_smtype & L_IMPORT) == 0)
5496 ldsym->l_ifile = 0;
5498 else if (impbfd == NULL)
5500 ldsym->l_ifile = 0;
5502 else
5504 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5505 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5509 ldsym->l_parm = 0;
5511 BFD_ASSERT (h->ldindx >= 0);
5513 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5514 (finfo->ldsym +
5515 (h->ldindx - 3)
5516 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
5517 h->ldsym = NULL;
5520 /* If this symbol needs global linkage code, write it out. */
5521 if (h->root.type == bfd_link_hash_defined
5522 && (h->root.u.def.section
5523 == xcoff_hash_table (finfo->info)->linkage_section))
5525 bfd_byte *p;
5526 bfd_vma tocoff;
5527 unsigned int i;
5529 p = h->root.u.def.section->contents + h->root.u.def.value;
5531 /* The first instruction in the global linkage code loads a
5532 specific TOC element. */
5533 tocoff = (h->descriptor->toc_section->output_section->vma
5534 + h->descriptor->toc_section->output_offset
5535 - xcoff_data (output_bfd)->toc);
5537 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5539 tocoff += h->descriptor->u.toc_offset;
5543 /* The first instruction in the glink code needs to be
5544 cooked to to hold the correct offset in the toc. The
5545 rest are just output raw. */
5546 bfd_put_32 (output_bfd,
5547 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5549 /* Start with i == 1 to get past the first instruction done above
5550 The /4 is because the glink code is in bytes and we are going
5551 4 at a pop. */
5552 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5554 bfd_put_32 (output_bfd,
5555 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5556 &p[4 * i]);
5560 /* If we created a TOC entry for this symbol, write out the required
5561 relocs. */
5562 if ((h->flags & XCOFF_SET_TOC) != 0)
5564 asection *tocsec;
5565 asection *osec;
5566 int oindx;
5567 struct internal_reloc *irel;
5568 struct internal_ldrel ldrel;
5569 struct internal_syment irsym;
5570 union internal_auxent iraux;
5572 tocsec = h->toc_section;
5573 osec = tocsec->output_section;
5574 oindx = osec->target_index;
5575 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5576 irel->r_vaddr = (osec->vma
5577 + tocsec->output_offset
5578 + h->u.toc_offset);
5581 if (h->indx >= 0)
5583 irel->r_symndx = h->indx;
5585 else
5587 h->indx = -2;
5588 irel->r_symndx = obj_raw_syment_count (output_bfd);
5591 BFD_ASSERT (h->ldindx >= 0);
5593 /* Initialize the aux union here instead of closer to when it is
5594 written out below because the length of the csect depends on
5595 whether the output is 32 or 64 bit. */
5596 memset (&iraux, 0, sizeof iraux);
5597 iraux.x_csect.x_smtyp = XTY_SD;
5598 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below */
5599 iraux.x_csect.x_smclas = XMC_TC;
5601 /* 32 bit uses a 32 bit R_POS to do the relocations
5602 64 bit uses a 64 bit R_POS to do the relocations
5604 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5606 Which one is determined by the backend. */
5607 if (bfd_xcoff_is_xcoff64 (output_bfd))
5609 irel->r_size = 63;
5610 iraux.x_csect.x_scnlen.l = 8;
5612 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5614 irel->r_size = 31;
5615 iraux.x_csect.x_scnlen.l = 4;
5617 else
5619 return FALSE;
5621 irel->r_type = R_POS;
5622 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5623 ++osec->reloc_count;
5625 ldrel.l_vaddr = irel->r_vaddr;
5626 ldrel.l_symndx = h->ldindx;
5627 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
5628 ldrel.l_rsecnm = oindx;
5629 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5630 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5632 /* We need to emit a symbol to define a csect which holds
5633 the reloc. */
5634 if (finfo->info->strip != strip_all)
5637 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
5638 &irsym, h->root.root.string);
5639 if (!result)
5640 return FALSE;
5642 irsym.n_value = irel->r_vaddr;
5643 irsym.n_scnum = osec->target_index;
5644 irsym.n_sclass = C_HIDEXT;
5645 irsym.n_type = T_NULL;
5646 irsym.n_numaux = 1;
5648 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5649 outsym += bfd_coff_symesz (output_bfd);
5651 /* note : iraux is initialized above */
5652 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5653 0, 1, (PTR) outsym);
5654 outsym += bfd_coff_auxesz (output_bfd);
5656 if (h->indx >= 0)
5658 /* We aren't going to write out the symbols below, so we
5659 need to write them out now. */
5660 pos = obj_sym_filepos (output_bfd);
5661 pos += (obj_raw_syment_count (output_bfd)
5662 * bfd_coff_symesz (output_bfd));
5663 amt = outsym - finfo->outsyms;
5664 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5665 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5666 return FALSE;
5667 obj_raw_syment_count (output_bfd) +=
5668 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5670 outsym = finfo->outsyms;
5675 /* If this symbol is a specially defined function descriptor, write
5676 it out. The first word is the address of the function code
5677 itself, the second word is the address of the TOC, and the third
5678 word is zero.
5680 32 bit vs 64 bit
5681 The addresses for the 32 bit will take 4 bytes and the addresses
5682 for 64 bit will take 8 bytes. Similar for the relocs. This type
5683 of logic was also done above to create a TOC entry in
5684 xcoff_write_global_symbol. */
5685 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5686 && h->root.type == bfd_link_hash_defined
5687 && (h->root.u.def.section
5688 == xcoff_hash_table (finfo->info)->descriptor_section))
5690 asection *sec;
5691 asection *osec;
5692 int oindx;
5693 bfd_byte *p;
5694 struct xcoff_link_hash_entry *hentry;
5695 asection *esec;
5696 struct internal_reloc *irel;
5697 struct internal_ldrel ldrel;
5698 asection *tsec;
5699 unsigned int reloc_size, byte_size;
5701 if (bfd_xcoff_is_xcoff64 (output_bfd))
5703 reloc_size = 63;
5704 byte_size = 8;
5706 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5708 reloc_size = 31;
5709 byte_size = 4;
5711 else
5713 return FALSE;
5716 sec = h->root.u.def.section;
5717 osec = sec->output_section;
5718 oindx = osec->target_index;
5719 p = sec->contents + h->root.u.def.value;
5721 hentry = h->descriptor;
5722 BFD_ASSERT (hentry != NULL
5723 && (hentry->root.type == bfd_link_hash_defined
5724 || hentry->root.type == bfd_link_hash_defweak));
5725 esec = hentry->root.u.def.section;
5727 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5728 irel->r_vaddr = (osec->vma
5729 + sec->output_offset
5730 + h->root.u.def.value);
5731 irel->r_symndx = esec->output_section->target_index;
5732 irel->r_type = R_POS;
5733 irel->r_size = reloc_size;
5734 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5735 ++osec->reloc_count;
5737 ldrel.l_vaddr = irel->r_vaddr;
5738 if (strcmp (esec->output_section->name, ".text") == 0)
5739 ldrel.l_symndx = 0;
5740 else if (strcmp (esec->output_section->name, ".data") == 0)
5741 ldrel.l_symndx = 1;
5742 else if (strcmp (esec->output_section->name, ".bss") == 0)
5743 ldrel.l_symndx = 2;
5744 else
5746 (*_bfd_error_handler)
5747 (_("%s: loader reloc in unrecognized section `%s'"),
5748 bfd_get_filename (output_bfd),
5749 esec->output_section->name);
5750 bfd_set_error (bfd_error_nonrepresentable_section);
5751 return FALSE;
5753 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5754 ldrel.l_rsecnm = oindx;
5755 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5756 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5758 /* There are three items to write out,
5759 the address of the code
5760 the address of the toc anchor
5761 the environment pointer.
5762 We are ignoring the environment pointer. So set it to zero. */
5763 if (bfd_xcoff_is_xcoff64 (output_bfd))
5765 bfd_put_64 (output_bfd,
5766 (esec->output_section->vma + esec->output_offset
5767 + hentry->root.u.def.value),
5769 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5770 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5772 else
5774 /* 32 bit backend
5775 This logic was already called above so the error case where
5776 the backend is neither has already been checked. */
5777 bfd_put_32 (output_bfd,
5778 (esec->output_section->vma + esec->output_offset
5779 + hentry->root.u.def.value),
5781 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5782 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5785 tsec = coff_section_from_bfd_index (output_bfd,
5786 xcoff_data (output_bfd)->sntoc);
5788 ++irel;
5789 irel->r_vaddr = (osec->vma
5790 + sec->output_offset
5791 + h->root.u.def.value
5792 + byte_size);
5793 irel->r_symndx = tsec->output_section->target_index;
5794 irel->r_type = R_POS;
5795 irel->r_size = reloc_size;
5796 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5797 ++osec->reloc_count;
5799 ldrel.l_vaddr = irel->r_vaddr;
5800 if (strcmp (tsec->output_section->name, ".text") == 0)
5801 ldrel.l_symndx = 0;
5802 else if (strcmp (tsec->output_section->name, ".data") == 0)
5803 ldrel.l_symndx = 1;
5804 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5805 ldrel.l_symndx = 2;
5806 else
5808 (*_bfd_error_handler)
5809 (_("%s: loader reloc in unrecognized section `%s'"),
5810 bfd_get_filename (output_bfd),
5811 tsec->output_section->name);
5812 bfd_set_error (bfd_error_nonrepresentable_section);
5813 return FALSE;
5815 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5816 ldrel.l_rsecnm = oindx;
5817 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5818 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5821 if (h->indx >= 0 || finfo->info->strip == strip_all)
5823 BFD_ASSERT (outsym == finfo->outsyms);
5824 return TRUE;
5827 if (h->indx != -2
5828 && (finfo->info->strip == strip_all
5829 || (finfo->info->strip == strip_some
5830 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5831 FALSE, FALSE) == NULL)))
5833 BFD_ASSERT (outsym == finfo->outsyms);
5834 return TRUE;
5837 if (h->indx != -2
5838 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5840 BFD_ASSERT (outsym == finfo->outsyms);
5841 return TRUE;
5844 memset (&aux, 0, sizeof aux);
5846 h->indx = obj_raw_syment_count (output_bfd);
5848 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5849 h->root.root.string);
5850 if (!result)
5851 return FALSE;
5853 if (h->root.type == bfd_link_hash_undefined
5854 || h->root.type == bfd_link_hash_undefweak)
5856 isym.n_value = 0;
5857 isym.n_scnum = N_UNDEF;
5858 isym.n_sclass = C_EXT;
5859 aux.x_csect.x_smtyp = XTY_ER;
5861 else if ((h->root.type == bfd_link_hash_defined
5862 || h->root.type == bfd_link_hash_defweak)
5863 && h->smclas == XMC_XO)
5865 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5866 isym.n_value = h->root.u.def.value;
5867 isym.n_scnum = N_UNDEF;
5868 isym.n_sclass = C_EXT;
5869 aux.x_csect.x_smtyp = XTY_ER;
5871 else if (h->root.type == bfd_link_hash_defined
5872 || h->root.type == bfd_link_hash_defweak)
5874 struct xcoff_link_size_list *l;
5876 isym.n_value = (h->root.u.def.section->output_section->vma
5877 + h->root.u.def.section->output_offset
5878 + h->root.u.def.value);
5879 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5880 isym.n_scnum = N_ABS;
5881 else
5882 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5883 isym.n_sclass = C_HIDEXT;
5884 aux.x_csect.x_smtyp = XTY_SD;
5886 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5888 for (l = xcoff_hash_table (finfo->info)->size_list;
5889 l != NULL;
5890 l = l->next)
5892 if (l->h == h)
5894 aux.x_csect.x_scnlen.l = l->size;
5895 break;
5900 else if (h->root.type == bfd_link_hash_common)
5902 isym.n_value = (h->root.u.c.p->section->output_section->vma
5903 + h->root.u.c.p->section->output_offset);
5904 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5905 isym.n_sclass = C_EXT;
5906 aux.x_csect.x_smtyp = XTY_CM;
5907 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5909 else
5910 abort ();
5912 isym.n_type = T_NULL;
5913 isym.n_numaux = 1;
5915 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5916 outsym += bfd_coff_symesz (output_bfd);
5918 aux.x_csect.x_smclas = h->smclas;
5919 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5920 (PTR) outsym);
5921 outsym += bfd_coff_auxesz (output_bfd);
5923 if ((h->root.type == bfd_link_hash_defined
5924 || h->root.type == bfd_link_hash_defweak)
5925 && h->smclas != XMC_XO)
5927 /* We just output an SD symbol. Now output an LD symbol. */
5929 h->indx += 2;
5931 isym.n_sclass = C_EXT;
5932 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5933 outsym += bfd_coff_symesz (output_bfd);
5935 aux.x_csect.x_smtyp = XTY_LD;
5936 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5937 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5938 (PTR) outsym);
5939 outsym += bfd_coff_auxesz (output_bfd);
5942 pos = obj_sym_filepos (output_bfd);
5943 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5944 amt = outsym - finfo->outsyms;
5945 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5946 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5947 return FALSE;
5948 obj_raw_syment_count (output_bfd) +=
5949 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5951 return TRUE;
5954 /* Handle a link order which is supposed to generate a reloc. */
5956 static bfd_boolean
5957 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5958 bfd *output_bfd;
5959 struct xcoff_final_link_info *finfo;
5960 asection *output_section;
5961 struct bfd_link_order *link_order;
5963 reloc_howto_type *howto;
5964 struct xcoff_link_hash_entry *h;
5965 asection *hsec;
5966 bfd_vma hval;
5967 bfd_vma addend;
5968 struct internal_reloc *irel;
5969 struct xcoff_link_hash_entry **rel_hash_ptr;
5970 struct internal_ldrel ldrel;
5972 if (link_order->type == bfd_section_reloc_link_order)
5974 /* We need to somehow locate a symbol in the right section. The
5975 symbol must either have a value of zero, or we must adjust
5976 the addend by the value of the symbol. FIXME: Write this
5977 when we need it. The old linker couldn't handle this anyhow. */
5978 abort ();
5981 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5982 if (howto == NULL)
5984 bfd_set_error (bfd_error_bad_value);
5985 return FALSE;
5988 h = ((struct xcoff_link_hash_entry *)
5989 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5990 link_order->u.reloc.p->u.name,
5991 FALSE, FALSE, TRUE));
5992 if (h == NULL)
5994 if (! ((*finfo->info->callbacks->unattached_reloc)
5995 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5996 (asection *) NULL, (bfd_vma) 0)))
5997 return FALSE;
5998 return TRUE;
6001 if (h->root.type == bfd_link_hash_common)
6003 hsec = h->root.u.c.p->section;
6004 hval = 0;
6006 else if (h->root.type == bfd_link_hash_defined
6007 || h->root.type == bfd_link_hash_defweak)
6009 hsec = h->root.u.def.section;
6010 hval = h->root.u.def.value;
6012 else
6014 hsec = NULL;
6015 hval = 0;
6018 addend = link_order->u.reloc.p->addend;
6019 if (hsec != NULL)
6020 addend += (hsec->output_section->vma
6021 + hsec->output_offset
6022 + hval);
6024 if (addend != 0)
6026 bfd_size_type size;
6027 bfd_byte *buf;
6028 bfd_reloc_status_type rstat;
6029 bfd_boolean ok;
6031 size = bfd_get_reloc_size (howto);
6032 buf = (bfd_byte *) bfd_zmalloc (size);
6033 if (buf == NULL)
6034 return FALSE;
6036 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6037 switch (rstat)
6039 case bfd_reloc_ok:
6040 break;
6041 default:
6042 case bfd_reloc_outofrange:
6043 abort ();
6044 case bfd_reloc_overflow:
6045 if (! ((*finfo->info->callbacks->reloc_overflow)
6046 (finfo->info, NULL, link_order->u.reloc.p->u.name,
6047 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6048 (bfd_vma) 0)))
6050 free (buf);
6051 return FALSE;
6053 break;
6055 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6056 (file_ptr) link_order->offset, size);
6057 free (buf);
6058 if (! ok)
6059 return FALSE;
6062 /* Store the reloc information in the right place. It will get
6063 swapped and written out at the end of the final_link routine. */
6065 irel = (finfo->section_info[output_section->target_index].relocs
6066 + output_section->reloc_count);
6067 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6068 + output_section->reloc_count);
6070 memset (irel, 0, sizeof (struct internal_reloc));
6071 *rel_hash_ptr = NULL;
6073 irel->r_vaddr = output_section->vma + link_order->offset;
6075 if (h->indx >= 0)
6076 irel->r_symndx = h->indx;
6077 else
6079 /* Set the index to -2 to force this symbol to get written out. */
6080 h->indx = -2;
6081 *rel_hash_ptr = h;
6082 irel->r_symndx = 0;
6085 irel->r_type = howto->type;
6086 irel->r_size = howto->bitsize - 1;
6087 if (howto->complain_on_overflow == complain_overflow_signed)
6088 irel->r_size |= 0x80;
6090 ++output_section->reloc_count;
6092 /* Now output the reloc to the .loader section. */
6094 ldrel.l_vaddr = irel->r_vaddr;
6096 if (hsec != NULL)
6098 const char *secname;
6100 secname = hsec->output_section->name;
6102 if (strcmp (secname, ".text") == 0)
6103 ldrel.l_symndx = 0;
6104 else if (strcmp (secname, ".data") == 0)
6105 ldrel.l_symndx = 1;
6106 else if (strcmp (secname, ".bss") == 0)
6107 ldrel.l_symndx = 2;
6108 else
6110 (*_bfd_error_handler)
6111 (_("%s: loader reloc in unrecognized section `%s'"),
6112 bfd_get_filename (output_bfd), secname);
6113 bfd_set_error (bfd_error_nonrepresentable_section);
6114 return FALSE;
6117 else
6119 if (h->ldindx < 0)
6121 (*_bfd_error_handler)
6122 (_("%s: `%s' in loader reloc but not loader sym"),
6123 bfd_get_filename (output_bfd),
6124 h->root.root.string);
6125 bfd_set_error (bfd_error_bad_value);
6126 return FALSE;
6128 ldrel.l_symndx = h->ldindx;
6131 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6132 ldrel.l_rsecnm = output_section->target_index;
6133 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6134 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
6136 return TRUE;
6139 /* Sort relocs by VMA. This is called via qsort. */
6141 static int
6142 xcoff_sort_relocs (p1, p2)
6143 const PTR p1;
6144 const PTR p2;
6146 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6147 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6149 if (r1->r_vaddr > r2->r_vaddr)
6150 return 1;
6151 else if (r1->r_vaddr < r2->r_vaddr)
6152 return -1;
6153 else
6154 return 0;