[ gas/ChangeLog ]
[binutils.git] / bfd / xcofflink.c
blob47c330b6c15f1fd21f378d76fddb3698879a5049
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 #undef STRING_SIZE_SIZE
34 #define STRING_SIZE_SIZE 4
36 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
37 This flag will only be used on input sections. */
39 #define SEC_MARK (SEC_ROM)
41 /* The list of import files. */
43 struct xcoff_import_file
45 /* The next entry in the list. */
46 struct xcoff_import_file *next;
47 /* The path. */
48 const char *path;
49 /* The file name. */
50 const char *file;
51 /* The member name. */
52 const char *member;
55 /* Information we keep for each section in the output file during the
56 final link phase. */
58 struct xcoff_link_section_info
60 /* The relocs to be output. */
61 struct internal_reloc *relocs;
62 /* For each reloc against a global symbol whose index was not known
63 when the reloc was handled, the global hash table entry. */
64 struct xcoff_link_hash_entry **rel_hashes;
65 /* If there is a TOC relative reloc against a global symbol, and the
66 index of the TOC symbol is not known when the reloc was handled,
67 an entry is added to this linked list. This is not an array,
68 like rel_hashes, because this case is quite uncommon. */
69 struct xcoff_toc_rel_hash
71 struct xcoff_toc_rel_hash *next;
72 struct xcoff_link_hash_entry *h;
73 struct internal_reloc *rel;
74 } *toc_rel_hashes;
77 /* Information that we pass around while doing the final link step. */
79 struct xcoff_final_link_info
81 /* General link information. */
82 struct bfd_link_info *info;
83 /* Output BFD. */
84 bfd *output_bfd;
85 /* Hash table for long symbol names. */
86 struct bfd_strtab_hash *strtab;
87 /* Array of information kept for each output section, indexed by the
88 target_index field. */
89 struct xcoff_link_section_info *section_info;
90 /* Symbol index of last C_FILE symbol (-1 if none). */
91 long last_file_index;
92 /* Contents of last C_FILE symbol. */
93 struct internal_syment last_file;
94 /* Symbol index of TOC symbol. */
95 long toc_symindx;
96 /* Start of .loader symbols. */
97 bfd_byte *ldsym;
98 /* Next .loader reloc to swap out. */
99 bfd_byte *ldrel;
100 /* File position of start of line numbers. */
101 file_ptr line_filepos;
102 /* Buffer large enough to hold swapped symbols of any input file. */
103 struct internal_syment *internal_syms;
104 /* Buffer large enough to hold output indices of symbols of any
105 input file. */
106 long *sym_indices;
107 /* Buffer large enough to hold output symbols for any input file. */
108 bfd_byte *outsyms;
109 /* Buffer large enough to hold external line numbers for any input
110 section. */
111 bfd_byte *linenos;
112 /* Buffer large enough to hold any input section. */
113 bfd_byte *contents;
114 /* Buffer large enough to hold external relocs of any input section. */
115 bfd_byte *external_relocs;
118 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
122 /* Routines to read XCOFF dynamic information. This don't really
123 belong here, but we already have the ldsym manipulation routines
124 here. */
126 /* Read the contents of a section. */
128 static bfd_boolean
129 xcoff_get_section_contents (bfd *abfd, asection *sec)
131 if (coff_section_data (abfd, sec) == NULL)
133 bfd_size_type amt = sizeof (struct coff_section_tdata);
135 sec->used_by_bfd = bfd_zalloc (abfd, amt);
136 if (sec->used_by_bfd == NULL)
137 return FALSE;
140 if (coff_section_data (abfd, sec)->contents == NULL)
142 bfd_byte *contents;
144 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
146 if (contents != NULL)
147 free (contents);
148 return FALSE;
150 coff_section_data (abfd, sec)->contents = contents;
153 return TRUE;
156 /* Get the size required to hold the dynamic symbols. */
158 long
159 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
161 asection *lsec;
162 bfd_byte *contents;
163 struct internal_ldhdr ldhdr;
165 if ((abfd->flags & DYNAMIC) == 0)
167 bfd_set_error (bfd_error_invalid_operation);
168 return -1;
171 lsec = bfd_get_section_by_name (abfd, ".loader");
172 if (lsec == NULL)
174 bfd_set_error (bfd_error_no_symbols);
175 return -1;
178 if (! xcoff_get_section_contents (abfd, lsec))
179 return -1;
180 contents = coff_section_data (abfd, lsec)->contents;
182 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
184 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
187 /* Get the dynamic symbols. */
189 long
190 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
192 asection *lsec;
193 bfd_byte *contents;
194 struct internal_ldhdr ldhdr;
195 const char *strings;
196 bfd_byte *elsym, *elsymend;
197 coff_symbol_type *symbuf;
199 if ((abfd->flags & DYNAMIC) == 0)
201 bfd_set_error (bfd_error_invalid_operation);
202 return -1;
205 lsec = bfd_get_section_by_name (abfd, ".loader");
206 if (lsec == NULL)
208 bfd_set_error (bfd_error_no_symbols);
209 return -1;
212 if (! xcoff_get_section_contents (abfd, lsec))
213 return -1;
214 contents = coff_section_data (abfd, lsec)->contents;
216 coff_section_data (abfd, lsec)->keep_contents = TRUE;
218 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
220 strings = (char *) contents + ldhdr.l_stoff;
222 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
223 if (symbuf == NULL)
224 return -1;
226 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
228 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
229 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
231 struct internal_ldsym ldsym;
233 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
235 symbuf->symbol.the_bfd = abfd;
237 if (ldsym._l._l_l._l_zeroes == 0)
238 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
239 else
241 char *c;
243 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
244 if (c == NULL)
245 return -1;
246 memcpy (c, ldsym._l._l_name, SYMNMLEN);
247 c[SYMNMLEN] = '\0';
248 symbuf->symbol.name = c;
251 if (ldsym.l_smclas == XMC_XO)
252 symbuf->symbol.section = bfd_abs_section_ptr;
253 else
254 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
255 ldsym.l_scnum);
256 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
258 symbuf->symbol.flags = BSF_NO_FLAGS;
259 if ((ldsym.l_smtype & L_EXPORT) != 0)
260 symbuf->symbol.flags |= BSF_GLOBAL;
262 /* FIXME: We have no way to record the other information stored
263 with the loader symbol. */
264 *psyms = (asymbol *) symbuf;
267 *psyms = NULL;
269 return ldhdr.l_nsyms;
272 /* Get the size required to hold the dynamic relocs. */
274 long
275 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
277 asection *lsec;
278 bfd_byte *contents;
279 struct internal_ldhdr ldhdr;
281 if ((abfd->flags & DYNAMIC) == 0)
283 bfd_set_error (bfd_error_invalid_operation);
284 return -1;
287 lsec = bfd_get_section_by_name (abfd, ".loader");
288 if (lsec == NULL)
290 bfd_set_error (bfd_error_no_symbols);
291 return -1;
294 if (! xcoff_get_section_contents (abfd, lsec))
295 return -1;
296 contents = coff_section_data (abfd, lsec)->contents;
298 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
300 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
303 /* Get the dynamic relocs. */
305 long
306 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
307 arelent **prelocs,
308 asymbol **syms)
310 asection *lsec;
311 bfd_byte *contents;
312 struct internal_ldhdr ldhdr;
313 arelent *relbuf;
314 bfd_byte *elrel, *elrelend;
316 if ((abfd->flags & DYNAMIC) == 0)
318 bfd_set_error (bfd_error_invalid_operation);
319 return -1;
322 lsec = bfd_get_section_by_name (abfd, ".loader");
323 if (lsec == NULL)
325 bfd_set_error (bfd_error_no_symbols);
326 return -1;
329 if (! xcoff_get_section_contents (abfd, lsec))
330 return -1;
331 contents = coff_section_data (abfd, lsec)->contents;
333 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
335 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
336 if (relbuf == NULL)
337 return -1;
339 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
341 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
342 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
343 prelocs++)
345 struct internal_ldrel ldrel;
347 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
349 if (ldrel.l_symndx >= 3)
350 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
351 else
353 const char *name;
354 asection *sec;
356 switch (ldrel.l_symndx)
358 case 0:
359 name = ".text";
360 break;
361 case 1:
362 name = ".data";
363 break;
364 case 2:
365 name = ".bss";
366 break;
367 default:
368 abort ();
369 break;
372 sec = bfd_get_section_by_name (abfd, name);
373 if (sec == NULL)
375 bfd_set_error (bfd_error_bad_value);
376 return -1;
379 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
382 relbuf->address = ldrel.l_vaddr;
383 relbuf->addend = 0;
385 /* Most dynamic relocs have the same type. FIXME: This is only
386 correct if ldrel.l_rtype == 0. In other cases, we should use
387 a different howto. */
388 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
390 /* FIXME: We have no way to record the l_rsecnm field. */
392 *prelocs = relbuf;
395 *prelocs = NULL;
397 return ldhdr.l_nreloc;
400 /* Routine to create an entry in an XCOFF link hash table. */
402 static struct bfd_hash_entry *
403 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
404 struct bfd_hash_table *table,
405 const char *string)
407 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
409 /* Allocate the structure if it has not already been allocated by a
410 subclass. */
411 if (ret == NULL)
412 ret = bfd_hash_allocate (table, sizeof (* ret));
413 if (ret == NULL)
414 return NULL;
416 /* Call the allocation method of the superclass. */
417 ret = ((struct xcoff_link_hash_entry *)
418 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
419 table, string));
420 if (ret != NULL)
422 /* Set local fields. */
423 ret->indx = -1;
424 ret->toc_section = NULL;
425 ret->u.toc_indx = -1;
426 ret->descriptor = NULL;
427 ret->ldsym = NULL;
428 ret->ldindx = -1;
429 ret->flags = 0;
430 ret->smclas = XMC_UA;
433 return (struct bfd_hash_entry *) ret;
436 /* Create a XCOFF link hash table. */
438 struct bfd_link_hash_table *
439 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
441 struct xcoff_link_hash_table *ret;
442 bfd_size_type amt = sizeof (* ret);
444 ret = bfd_malloc (amt);
445 if (ret == NULL)
446 return NULL;
447 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
448 sizeof (struct xcoff_link_hash_entry)))
450 free (ret);
451 return NULL;
454 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
455 ret->debug_section = NULL;
456 ret->loader_section = NULL;
457 ret->ldrel_count = 0;
458 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
459 ret->linkage_section = NULL;
460 ret->toc_section = NULL;
461 ret->descriptor_section = NULL;
462 ret->imports = NULL;
463 ret->file_align = 0;
464 ret->textro = FALSE;
465 ret->gc = FALSE;
466 memset (ret->special_sections, 0, sizeof ret->special_sections);
468 /* The linker will always generate a full a.out header. We need to
469 record that fact now, before the sizeof_headers routine could be
470 called. */
471 xcoff_data (abfd)->full_aouthdr = TRUE;
473 return &ret->root;
476 /* Free a XCOFF link hash table. */
478 void
479 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
481 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
483 _bfd_stringtab_free (ret->debug_strtab);
484 bfd_hash_table_free (&ret->root.table);
485 free (ret);
488 /* Read internal relocs for an XCOFF csect. This is a wrapper around
489 _bfd_coff_read_internal_relocs which tries to take advantage of any
490 relocs which may have been cached for the enclosing section. */
492 static struct internal_reloc *
493 xcoff_read_internal_relocs (bfd *abfd,
494 asection *sec,
495 bfd_boolean cache,
496 bfd_byte *external_relocs,
497 bfd_boolean require_internal,
498 struct internal_reloc *internal_relocs)
500 if (coff_section_data (abfd, sec) != NULL
501 && coff_section_data (abfd, sec)->relocs == NULL
502 && xcoff_section_data (abfd, sec) != NULL)
504 asection *enclosing;
506 enclosing = xcoff_section_data (abfd, sec)->enclosing;
508 if (enclosing != NULL
509 && (coff_section_data (abfd, enclosing) == NULL
510 || coff_section_data (abfd, enclosing)->relocs == NULL)
511 && cache
512 && enclosing->reloc_count > 0)
514 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
515 external_relocs, FALSE, NULL)
516 == NULL)
517 return NULL;
520 if (enclosing != NULL
521 && coff_section_data (abfd, enclosing) != NULL
522 && coff_section_data (abfd, enclosing)->relocs != NULL)
524 size_t off;
526 off = ((sec->rel_filepos - enclosing->rel_filepos)
527 / bfd_coff_relsz (abfd));
529 if (! require_internal)
530 return coff_section_data (abfd, enclosing)->relocs + off;
531 memcpy (internal_relocs,
532 coff_section_data (abfd, enclosing)->relocs + off,
533 sec->reloc_count * sizeof (struct internal_reloc));
534 return internal_relocs;
538 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
539 require_internal, internal_relocs);
542 /* This function is used to add symbols from a dynamic object to the
543 global symbol table. */
545 static bfd_boolean
546 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
548 asection *lsec;
549 bfd_byte *contents;
550 struct internal_ldhdr ldhdr;
551 const char *strings;
552 bfd_byte *elsym, *elsymend;
553 struct xcoff_import_file *n;
554 const char *bname;
555 const char *mname;
556 const char *s;
557 unsigned int c;
558 struct xcoff_import_file **pp;
560 /* We can only handle a dynamic object if we are generating an XCOFF
561 output file. */
562 if (info->hash->creator != abfd->xvec)
564 (*_bfd_error_handler)
565 (_("%s: XCOFF shared object when not producing XCOFF output"),
566 bfd_get_filename (abfd));
567 bfd_set_error (bfd_error_invalid_operation);
568 return FALSE;
571 /* The symbols we use from a dynamic object are not the symbols in
572 the normal symbol table, but, rather, the symbols in the export
573 table. If there is a global symbol in a dynamic object which is
574 not in the export table, the loader will not be able to find it,
575 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
576 libc.a has symbols in the export table which are not in the
577 symbol table. */
579 /* Read in the .loader section. FIXME: We should really use the
580 o_snloader field in the a.out header, rather than grabbing the
581 section by name. */
582 lsec = bfd_get_section_by_name (abfd, ".loader");
583 if (lsec == NULL)
585 (*_bfd_error_handler)
586 (_("%s: dynamic object with no .loader section"),
587 bfd_get_filename (abfd));
588 bfd_set_error (bfd_error_no_symbols);
589 return FALSE;
592 if (! xcoff_get_section_contents (abfd, lsec))
593 return FALSE;
594 contents = coff_section_data (abfd, lsec)->contents;
596 /* Remove the sections from this object, so that they do not get
597 included in the link. */
598 bfd_section_list_clear (abfd);
600 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
602 strings = (char *) contents + ldhdr.l_stoff;
604 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
606 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
608 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
610 struct internal_ldsym ldsym;
611 char nambuf[SYMNMLEN + 1];
612 const char *name;
613 struct xcoff_link_hash_entry *h;
615 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
617 /* We are only interested in exported symbols. */
618 if ((ldsym.l_smtype & L_EXPORT) == 0)
619 continue;
621 if (ldsym._l._l_l._l_zeroes == 0)
622 name = strings + ldsym._l._l_l._l_offset;
623 else
625 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
626 nambuf[SYMNMLEN] = '\0';
627 name = nambuf;
630 /* Normally we could not call xcoff_link_hash_lookup in an add
631 symbols routine, since we might not be using an XCOFF hash
632 table. However, we verified above that we are using an XCOFF
633 hash table. */
635 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
636 TRUE, TRUE);
637 if (h == NULL)
638 return FALSE;
640 h->flags |= XCOFF_DEF_DYNAMIC;
642 /* If the symbol is undefined, and the BFD it was found in is
643 not a dynamic object, change the BFD to this dynamic object,
644 so that we can get the correct import file ID. */
645 if ((h->root.type == bfd_link_hash_undefined
646 || h->root.type == bfd_link_hash_undefweak)
647 && (h->root.u.undef.abfd == NULL
648 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
649 h->root.u.undef.abfd = abfd;
651 if (h->root.type == bfd_link_hash_new)
653 h->root.type = bfd_link_hash_undefined;
654 h->root.u.undef.abfd = abfd;
655 /* We do not want to add this to the undefined symbol list. */
658 if (h->smclas == XMC_UA
659 || h->root.type == bfd_link_hash_undefined
660 || h->root.type == bfd_link_hash_undefweak)
661 h->smclas = ldsym.l_smclas;
663 /* Unless this is an XMC_XO symbol, we don't bother to actually
664 define it, since we don't have a section to put it in anyhow.
665 Instead, the relocation routines handle the DEF_DYNAMIC flag
666 correctly. */
668 if (h->smclas == XMC_XO
669 && (h->root.type == bfd_link_hash_undefined
670 || h->root.type == bfd_link_hash_undefweak))
672 /* This symbol has an absolute value. */
673 h->root.type = bfd_link_hash_defined;
674 h->root.u.def.section = bfd_abs_section_ptr;
675 h->root.u.def.value = ldsym.l_value;
678 /* If this symbol defines a function descriptor, then it
679 implicitly defines the function code as well. */
680 if (h->smclas == XMC_DS
681 || (h->smclas == XMC_XO && name[0] != '.'))
682 h->flags |= XCOFF_DESCRIPTOR;
683 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
685 struct xcoff_link_hash_entry *hds;
687 hds = h->descriptor;
688 if (hds == NULL)
690 char *dsnm;
692 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
693 if (dsnm == NULL)
694 return FALSE;
695 dsnm[0] = '.';
696 strcpy (dsnm + 1, name);
697 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
698 TRUE, TRUE, TRUE);
699 free (dsnm);
700 if (hds == NULL)
701 return FALSE;
703 if (hds->root.type == bfd_link_hash_new)
705 hds->root.type = bfd_link_hash_undefined;
706 hds->root.u.undef.abfd = abfd;
707 /* We do not want to add this to the undefined
708 symbol list. */
711 hds->descriptor = h;
712 h->descriptor = hds;
715 hds->flags |= XCOFF_DEF_DYNAMIC;
716 if (hds->smclas == XMC_UA)
717 hds->smclas = XMC_PR;
719 /* An absolute symbol appears to actually define code, not a
720 function descriptor. This is how some math functions are
721 implemented on AIX 4.1. */
722 if (h->smclas == XMC_XO
723 && (hds->root.type == bfd_link_hash_undefined
724 || hds->root.type == bfd_link_hash_undefweak))
726 hds->smclas = XMC_XO;
727 hds->root.type = bfd_link_hash_defined;
728 hds->root.u.def.section = bfd_abs_section_ptr;
729 hds->root.u.def.value = ldsym.l_value;
734 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
736 free (coff_section_data (abfd, lsec)->contents);
737 coff_section_data (abfd, lsec)->contents = NULL;
740 /* Record this file in the import files. */
741 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
742 if (n == NULL)
743 return FALSE;
744 n->next = NULL;
746 /* For some reason, the path entry in the import file list for a
747 shared object appears to always be empty. The file name is the
748 base name. */
749 n->path = "";
750 if (abfd->my_archive == NULL)
752 bname = bfd_get_filename (abfd);
753 mname = "";
755 else
757 bname = bfd_get_filename (abfd->my_archive);
758 mname = bfd_get_filename (abfd);
760 s = strrchr (bname, '/');
761 if (s != NULL)
762 bname = s + 1;
763 n->file = bname;
764 n->member = mname;
766 /* We start c at 1 because the first import file number is reserved
767 for LIBPATH. */
768 for (pp = &xcoff_hash_table (info)->imports, c = 1;
769 *pp != NULL;
770 pp = &(*pp)->next, ++c)
772 *pp = n;
774 xcoff_data (abfd)->import_file_id = c;
776 return TRUE;
779 /* xcoff_link_create_extra_sections
781 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
783 static bfd_boolean
784 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
786 bfd_boolean return_value = FALSE;
788 if (info->hash->creator == abfd->xvec)
790 /* We need to build a .loader section, so we do it here. This
791 won't work if we're producing an XCOFF output file with no
792 XCOFF input files. FIXME. */
794 if (xcoff_hash_table (info)->loader_section == NULL)
796 asection *lsec;
798 lsec = bfd_make_section_anyway (abfd, ".loader");
799 if (lsec == NULL)
800 goto end_return;
802 xcoff_hash_table (info)->loader_section = lsec;
803 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
806 /* Likewise for the linkage section. */
807 if (xcoff_hash_table (info)->linkage_section == NULL)
809 asection *lsec;
811 lsec = bfd_make_section_anyway (abfd, ".gl");
812 if (lsec == NULL)
813 goto end_return;
815 xcoff_hash_table (info)->linkage_section = lsec;
816 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
817 | SEC_IN_MEMORY);
818 lsec->alignment_power = 2;
821 /* Likewise for the TOC section. */
822 if (xcoff_hash_table (info)->toc_section == NULL)
824 asection *tsec;
826 tsec = bfd_make_section_anyway (abfd, ".tc");
827 if (tsec == NULL)
828 goto end_return;
830 xcoff_hash_table (info)->toc_section = tsec;
831 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
832 | SEC_IN_MEMORY);
833 tsec->alignment_power = 2;
836 /* Likewise for the descriptor section. */
837 if (xcoff_hash_table (info)->descriptor_section == NULL)
839 asection *dsec;
841 dsec = bfd_make_section_anyway (abfd, ".ds");
842 if (dsec == NULL)
843 goto end_return;
845 xcoff_hash_table (info)->descriptor_section = dsec;
846 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
847 | SEC_IN_MEMORY);
848 dsec->alignment_power = 2;
851 /* Likewise for the .debug section. */
852 if (xcoff_hash_table (info)->debug_section == NULL
853 && info->strip != strip_all)
855 asection *dsec;
857 dsec = bfd_make_section_anyway (abfd, ".debug");
858 if (dsec == NULL)
859 goto end_return;
861 xcoff_hash_table (info)->debug_section = dsec;
862 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
866 return_value = TRUE;
868 end_return:
870 return return_value;
873 /* Returns the index of reloc in RELOCS with the least address greater
874 than or equal to ADDRESS. The relocs are sorted by address. */
876 static bfd_size_type
877 xcoff_find_reloc (struct internal_reloc *relocs,
878 bfd_size_type count,
879 bfd_vma address)
881 bfd_size_type min, max, this;
883 if (count < 2)
885 if (count == 1 && relocs[0].r_vaddr < address)
886 return 1;
887 else
888 return 0;
891 min = 0;
892 max = count;
894 /* Do a binary search over (min,max]. */
895 while (min + 1 < max)
897 bfd_vma raddr;
899 this = (max + min) / 2;
900 raddr = relocs[this].r_vaddr;
901 if (raddr > address)
902 max = this;
903 else if (raddr < address)
904 min = this;
905 else
907 min = this;
908 break;
912 if (relocs[min].r_vaddr < address)
913 return min + 1;
915 while (min > 0
916 && relocs[min - 1].r_vaddr == address)
917 --min;
919 return min;
922 /* Add all the symbols from an object file to the hash table.
924 XCOFF is a weird format. A normal XCOFF .o files will have three
925 COFF sections--.text, .data, and .bss--but each COFF section will
926 contain many csects. These csects are described in the symbol
927 table. From the linker's point of view, each csect must be
928 considered a section in its own right. For example, a TOC entry is
929 handled as a small XMC_TC csect. The linker must be able to merge
930 different TOC entries together, which means that it must be able to
931 extract the XMC_TC csects from the .data section of the input .o
932 file.
934 From the point of view of our linker, this is, of course, a hideous
935 nightmare. We cope by actually creating sections for each csect,
936 and discarding the original sections. We then have to handle the
937 relocation entries carefully, since the only way to tell which
938 csect they belong to is to examine the address. */
940 static bfd_boolean
941 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
943 unsigned int n_tmask;
944 unsigned int n_btshft;
945 bfd_boolean default_copy;
946 bfd_size_type symcount;
947 struct xcoff_link_hash_entry **sym_hash;
948 asection **csect_cache;
949 bfd_size_type linesz;
950 asection *o;
951 asection *last_real;
952 bfd_boolean keep_syms;
953 asection *csect;
954 unsigned int csect_index;
955 asection *first_csect;
956 bfd_size_type symesz;
957 bfd_byte *esym;
958 bfd_byte *esym_end;
959 struct reloc_info_struct
961 struct internal_reloc *relocs;
962 asection **csects;
963 bfd_byte *linenos;
964 } *reloc_info = NULL;
965 bfd_size_type amt;
967 keep_syms = obj_coff_keep_syms (abfd);
969 if ((abfd->flags & DYNAMIC) != 0
970 && ! info->static_link)
972 if (! xcoff_link_add_dynamic_symbols (abfd, info))
973 return FALSE;
976 /* Create the loader, toc, gl, ds and debug sections, if needed. */
977 if (! xcoff_link_create_extra_sections (abfd, info))
978 goto error_return;
980 if ((abfd->flags & DYNAMIC) != 0
981 && ! info->static_link)
982 return TRUE;
984 n_tmask = coff_data (abfd)->local_n_tmask;
985 n_btshft = coff_data (abfd)->local_n_btshft;
987 /* Define macros so that ISFCN, et. al., macros work correctly. */
988 #define N_TMASK n_tmask
989 #define N_BTSHFT n_btshft
991 if (info->keep_memory)
992 default_copy = FALSE;
993 else
994 default_copy = TRUE;
996 symcount = obj_raw_syment_count (abfd);
998 /* We keep a list of the linker hash table entries that correspond
999 to each external symbol. */
1000 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1001 sym_hash = bfd_zalloc (abfd, amt);
1002 if (sym_hash == NULL && symcount != 0)
1003 goto error_return;
1004 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1006 /* Because of the weird stuff we are doing with XCOFF csects, we can
1007 not easily determine which section a symbol is in, so we store
1008 the information in the tdata for the input file. */
1009 amt = symcount * sizeof (asection *);
1010 csect_cache = bfd_zalloc (abfd, amt);
1011 if (csect_cache == NULL && symcount != 0)
1012 goto error_return;
1013 xcoff_data (abfd)->csects = csect_cache;
1015 /* While splitting sections into csects, we need to assign the
1016 relocs correctly. The relocs and the csects must both be in
1017 order by VMA within a given section, so we handle this by
1018 scanning along the relocs as we process the csects. We index
1019 into reloc_info using the section target_index. */
1020 amt = abfd->section_count + 1;
1021 amt *= sizeof (struct reloc_info_struct);
1022 reloc_info = bfd_zmalloc (amt);
1023 if (reloc_info == NULL)
1024 goto error_return;
1026 /* Read in the relocs and line numbers for each section. */
1027 linesz = bfd_coff_linesz (abfd);
1028 last_real = NULL;
1029 for (o = abfd->sections; o != NULL; o = o->next)
1031 last_real = o;
1033 if ((o->flags & SEC_RELOC) != 0)
1035 reloc_info[o->target_index].relocs =
1036 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1037 amt = o->reloc_count;
1038 amt *= sizeof (asection *);
1039 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1040 if (reloc_info[o->target_index].csects == NULL)
1041 goto error_return;
1044 if ((info->strip == strip_none || info->strip == strip_some)
1045 && o->lineno_count > 0)
1047 bfd_byte *linenos;
1049 amt = linesz * o->lineno_count;
1050 linenos = bfd_malloc (amt);
1051 if (linenos == NULL)
1052 goto error_return;
1053 reloc_info[o->target_index].linenos = linenos;
1054 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1055 || bfd_bread (linenos, amt, abfd) != amt)
1056 goto error_return;
1060 /* Don't let the linker relocation routines discard the symbols. */
1061 obj_coff_keep_syms (abfd) = TRUE;
1063 csect = NULL;
1064 csect_index = 0;
1065 first_csect = NULL;
1067 symesz = bfd_coff_symesz (abfd);
1068 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1069 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1070 esym_end = esym + symcount * symesz;
1072 while (esym < esym_end)
1074 struct internal_syment sym;
1075 union internal_auxent aux;
1076 const char *name;
1077 char buf[SYMNMLEN + 1];
1078 int smtyp;
1079 flagword flags;
1080 asection *section;
1081 bfd_vma value;
1082 struct xcoff_link_hash_entry *set_toc;
1084 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1086 /* In this pass we are only interested in symbols with csect
1087 information. */
1088 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1090 /* Set csect_cache,
1091 Normally csect is a .pr, .rw etc. created in the loop
1092 If C_FILE or first time, handle special
1094 Advance esym, sym_hash, csect_hash ptr's
1095 Keep track of the last_symndx for the current file. */
1096 if (sym.n_sclass == C_FILE && csect != NULL)
1098 xcoff_section_data (abfd, csect)->last_symndx =
1099 ((esym
1100 - (bfd_byte *) obj_coff_external_syms (abfd))
1101 / symesz);
1102 csect = NULL;
1105 if (csect != NULL)
1106 *csect_cache = csect;
1107 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1108 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1109 else
1110 *csect_cache = NULL;
1111 esym += (sym.n_numaux + 1) * symesz;
1112 sym_hash += sym.n_numaux + 1;
1113 csect_cache += sym.n_numaux + 1;
1115 continue;
1118 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1120 if (name == NULL)
1121 goto error_return;
1123 /* If this symbol has line number information attached to it,
1124 and we're not stripping it, count the number of entries and
1125 add them to the count for this csect. In the final link pass
1126 we are going to attach line number information by symbol,
1127 rather than by section, in order to more easily handle
1128 garbage collection. */
1129 if ((info->strip == strip_none || info->strip == strip_some)
1130 && sym.n_numaux > 1
1131 && csect != NULL
1132 && ISFCN (sym.n_type))
1134 union internal_auxent auxlin;
1136 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1137 sym.n_type, sym.n_sclass,
1138 0, sym.n_numaux, (void *) &auxlin);
1140 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1142 asection *enclosing;
1143 bfd_signed_vma linoff;
1145 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1146 if (enclosing == NULL)
1148 (*_bfd_error_handler)
1149 (_("%B: `%s' has line numbers but no enclosing section"),
1150 abfd, name);
1151 bfd_set_error (bfd_error_bad_value);
1152 goto error_return;
1154 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1155 - enclosing->line_filepos);
1156 /* Explicit cast to bfd_signed_vma for compiler. */
1157 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1159 struct internal_lineno lin;
1160 bfd_byte *linpstart;
1162 linpstart = (reloc_info[enclosing->target_index].linenos
1163 + linoff);
1164 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1165 if (lin.l_lnno == 0
1166 && ((bfd_size_type) lin.l_addr.l_symndx
1167 == ((esym
1168 - (bfd_byte *) obj_coff_external_syms (abfd))
1169 / symesz)))
1171 bfd_byte *linpend, *linp;
1173 linpend = (reloc_info[enclosing->target_index].linenos
1174 + enclosing->lineno_count * linesz);
1175 for (linp = linpstart + linesz;
1176 linp < linpend;
1177 linp += linesz)
1179 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1180 (void *) &lin);
1181 if (lin.l_lnno == 0)
1182 break;
1184 csect->lineno_count += (linp - linpstart) / linesz;
1185 /* The setting of line_filepos will only be
1186 useful if all the line number entries for a
1187 csect are contiguous; this only matters for
1188 error reporting. */
1189 if (csect->line_filepos == 0)
1190 csect->line_filepos =
1191 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1197 /* Pick up the csect auxiliary information. */
1198 if (sym.n_numaux == 0)
1200 (*_bfd_error_handler)
1201 (_("%B: class %d symbol `%s' has no aux entries"),
1202 abfd, sym.n_sclass, name);
1203 bfd_set_error (bfd_error_bad_value);
1204 goto error_return;
1207 bfd_coff_swap_aux_in (abfd,
1208 (void *) (esym + symesz * sym.n_numaux),
1209 sym.n_type, sym.n_sclass,
1210 sym.n_numaux - 1, sym.n_numaux,
1211 (void *) &aux);
1213 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1215 flags = BSF_GLOBAL;
1216 section = NULL;
1217 value = 0;
1218 set_toc = NULL;
1220 switch (smtyp)
1222 default:
1223 (*_bfd_error_handler)
1224 (_("%B: symbol `%s' has unrecognized csect type %d"),
1225 abfd, name, smtyp);
1226 bfd_set_error (bfd_error_bad_value);
1227 goto error_return;
1229 case XTY_ER:
1230 /* This is an external reference. */
1231 if (sym.n_sclass == C_HIDEXT
1232 || sym.n_scnum != N_UNDEF
1233 || aux.x_csect.x_scnlen.l != 0)
1235 (*_bfd_error_handler)
1236 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1237 abfd, name, sym.n_sclass, sym.n_scnum,
1238 aux.x_csect.x_scnlen.l);
1239 bfd_set_error (bfd_error_bad_value);
1240 goto error_return;
1243 /* An XMC_XO external reference is actually a reference to
1244 an absolute location. */
1245 if (aux.x_csect.x_smclas != XMC_XO)
1246 section = bfd_und_section_ptr;
1247 else
1249 section = bfd_abs_section_ptr;
1250 value = sym.n_value;
1252 break;
1254 case XTY_SD:
1255 /* This is a csect definition. */
1256 if (csect != NULL)
1258 xcoff_section_data (abfd, csect)->last_symndx =
1259 ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1262 csect = NULL;
1263 csect_index = -(unsigned) 1;
1265 /* When we see a TOC anchor, we record the TOC value. */
1266 if (aux.x_csect.x_smclas == XMC_TC0)
1268 if (sym.n_sclass != C_HIDEXT
1269 || aux.x_csect.x_scnlen.l != 0)
1271 (*_bfd_error_handler)
1272 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1273 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1274 bfd_set_error (bfd_error_bad_value);
1275 goto error_return;
1277 xcoff_data (abfd)->toc = sym.n_value;
1280 /* We must merge TOC entries for the same symbol. We can
1281 merge two TOC entries if they are both C_HIDEXT, they
1282 both have the same name, they are both 4 or 8 bytes long, and
1283 they both have a relocation table entry for an external
1284 symbol with the same name. Unfortunately, this means
1285 that we must look through the relocations. Ick.
1287 Logic for 32 bit vs 64 bit.
1288 32 bit has a csect length of 4 for TOC
1289 64 bit has a csect length of 8 for TOC
1291 The conditions to get past the if-check are not that bad.
1292 They are what is used to create the TOC csects in the first
1293 place. */
1294 if (aux.x_csect.x_smclas == XMC_TC
1295 && sym.n_sclass == C_HIDEXT
1296 && info->hash->creator == abfd->xvec
1297 && ((bfd_xcoff_is_xcoff32 (abfd)
1298 && aux.x_csect.x_scnlen.l == 4)
1299 || (bfd_xcoff_is_xcoff64 (abfd)
1300 && aux.x_csect.x_scnlen.l == 8)))
1302 asection *enclosing;
1303 struct internal_reloc *relocs;
1304 bfd_size_type relindx;
1305 struct internal_reloc *rel;
1307 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1308 if (enclosing == NULL)
1309 goto error_return;
1311 relocs = reloc_info[enclosing->target_index].relocs;
1312 amt = enclosing->reloc_count;
1313 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1314 rel = relocs + relindx;
1316 /* 32 bit R_POS r_size is 31
1317 64 bit R_POS r_size is 63 */
1318 if (relindx < enclosing->reloc_count
1319 && rel->r_vaddr == (bfd_vma) sym.n_value
1320 && rel->r_type == R_POS
1321 && ((bfd_xcoff_is_xcoff32 (abfd)
1322 && rel->r_size == 31)
1323 || (bfd_xcoff_is_xcoff64 (abfd)
1324 && rel->r_size == 63)))
1326 bfd_byte *erelsym;
1328 struct internal_syment relsym;
1330 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1331 + rel->r_symndx * symesz);
1332 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1333 if (relsym.n_sclass == C_EXT)
1335 const char *relname;
1336 char relbuf[SYMNMLEN + 1];
1337 bfd_boolean copy;
1338 struct xcoff_link_hash_entry *h;
1340 /* At this point we know that the TOC entry is
1341 for an externally visible symbol. */
1342 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1343 relbuf);
1344 if (relname == NULL)
1345 goto error_return;
1347 /* We only merge TOC entries if the TC name is
1348 the same as the symbol name. This handles
1349 the normal case, but not common cases like
1350 SYM.P4 which gcc generates to store SYM + 4
1351 in the TOC. FIXME. */
1352 if (strcmp (name, relname) == 0)
1354 copy = (! info->keep_memory
1355 || relsym._n._n_n._n_zeroes != 0
1356 || relsym._n._n_n._n_offset == 0);
1357 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1358 relname, TRUE, copy,
1359 FALSE);
1360 if (h == NULL)
1361 goto error_return;
1363 /* At this point h->root.type could be
1364 bfd_link_hash_new. That should be OK,
1365 since we know for sure that we will come
1366 across this symbol as we step through the
1367 file. */
1369 /* We store h in *sym_hash for the
1370 convenience of the relocate_section
1371 function. */
1372 *sym_hash = h;
1374 if (h->toc_section != NULL)
1376 asection **rel_csects;
1378 /* We already have a TOC entry for this
1379 symbol, so we can just ignore this
1380 one. */
1381 rel_csects =
1382 reloc_info[enclosing->target_index].csects;
1383 rel_csects[relindx] = bfd_und_section_ptr;
1384 break;
1387 /* We are about to create a TOC entry for
1388 this symbol. */
1389 set_toc = h;
1396 asection *enclosing;
1398 /* We need to create a new section. We get the name from
1399 the csect storage mapping class, so that the linker can
1400 accumulate similar csects together. */
1402 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1403 if (NULL == csect)
1404 goto error_return;
1406 /* The enclosing section is the main section : .data, .text
1407 or .bss that the csect is coming from. */
1408 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1409 if (enclosing == NULL)
1410 goto error_return;
1412 if (! bfd_is_abs_section (enclosing)
1413 && ((bfd_vma) sym.n_value < enclosing->vma
1414 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1415 > enclosing->vma + enclosing->size)))
1417 (*_bfd_error_handler)
1418 (_("%B: csect `%s' not in enclosing section"),
1419 abfd, name);
1420 bfd_set_error (bfd_error_bad_value);
1421 goto error_return;
1423 csect->vma = sym.n_value;
1424 csect->filepos = (enclosing->filepos
1425 + sym.n_value
1426 - enclosing->vma);
1427 csect->size = aux.x_csect.x_scnlen.l;
1428 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1429 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1431 /* Record the enclosing section in the tdata for this new
1432 section. */
1433 amt = sizeof (struct coff_section_tdata);
1434 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1435 if (csect->used_by_bfd == NULL)
1436 goto error_return;
1437 amt = sizeof (struct xcoff_section_tdata);
1438 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1439 if (coff_section_data (abfd, csect)->tdata == NULL)
1440 goto error_return;
1441 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1442 xcoff_section_data (abfd, csect)->lineno_count =
1443 enclosing->lineno_count;
1445 if (enclosing->owner == abfd)
1447 struct internal_reloc *relocs;
1448 bfd_size_type relindx;
1449 struct internal_reloc *rel;
1450 asection **rel_csect;
1452 relocs = reloc_info[enclosing->target_index].relocs;
1453 amt = enclosing->reloc_count;
1454 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1456 rel = relocs + relindx;
1457 rel_csect = (reloc_info[enclosing->target_index].csects
1458 + relindx);
1460 csect->rel_filepos = (enclosing->rel_filepos
1461 + relindx * bfd_coff_relsz (abfd));
1462 while (relindx < enclosing->reloc_count
1463 && *rel_csect == NULL
1464 && rel->r_vaddr < csect->vma + csect->size)
1467 *rel_csect = csect;
1468 csect->flags |= SEC_RELOC;
1469 ++csect->reloc_count;
1470 ++relindx;
1471 ++rel;
1472 ++rel_csect;
1476 /* There are a number of other fields and section flags
1477 which we do not bother to set. */
1479 csect_index = ((esym
1480 - (bfd_byte *) obj_coff_external_syms (abfd))
1481 / symesz);
1483 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1485 if (first_csect == NULL)
1486 first_csect = csect;
1488 /* If this symbol is C_EXT, we treat it as starting at the
1489 beginning of the newly created section. */
1490 if (sym.n_sclass == C_EXT)
1492 section = csect;
1493 value = 0;
1496 /* If this is a TOC section for a symbol, record it. */
1497 if (set_toc != NULL)
1498 set_toc->toc_section = csect;
1500 break;
1502 case XTY_LD:
1503 /* This is a label definition. The x_scnlen field is the
1504 symbol index of the csect. Usually the XTY_LD symbol will
1505 follow its appropriate XTY_SD symbol. The .set pseudo op can
1506 cause the XTY_LD to not follow the XTY_SD symbol. */
1508 bfd_boolean bad;
1510 bad = FALSE;
1511 if (aux.x_csect.x_scnlen.l < 0
1512 || (aux.x_csect.x_scnlen.l
1513 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1514 bad = TRUE;
1515 if (! bad)
1517 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1518 if (section == NULL
1519 || (section->flags & SEC_HAS_CONTENTS) == 0)
1520 bad = TRUE;
1522 if (bad)
1524 (*_bfd_error_handler)
1525 (_("%B: misplaced XTY_LD `%s'"),
1526 abfd, name);
1527 bfd_set_error (bfd_error_bad_value);
1528 goto error_return;
1530 csect = section;
1531 value = sym.n_value - csect->vma;
1533 break;
1535 case XTY_CM:
1536 /* This is an unitialized csect. We could base the name on
1537 the storage mapping class, but we don't bother except for
1538 an XMC_TD symbol. If this csect is externally visible,
1539 it is a common symbol. We put XMC_TD symbols in sections
1540 named .tocbss, and rely on the linker script to put that
1541 in the TOC area. */
1543 if (csect != NULL)
1545 xcoff_section_data (abfd, csect)->last_symndx =
1546 ((esym
1547 - (bfd_byte *) obj_coff_external_syms (abfd))
1548 / symesz);
1551 if (aux.x_csect.x_smclas == XMC_TD)
1553 /* The linker script puts the .td section in the data
1554 section after the .tc section. */
1555 csect = bfd_make_section_anyway (abfd, ".td");
1557 else
1558 csect = bfd_make_section_anyway (abfd, ".bss");
1560 if (csect == NULL)
1561 goto error_return;
1562 csect->vma = sym.n_value;
1563 csect->size = aux.x_csect.x_scnlen.l;
1564 csect->flags |= SEC_ALLOC;
1565 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1566 /* There are a number of other fields and section flags
1567 which we do not bother to set. */
1569 csect_index = ((esym
1570 - (bfd_byte *) obj_coff_external_syms (abfd))
1571 / symesz);
1573 amt = sizeof (struct coff_section_tdata);
1574 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1575 if (csect->used_by_bfd == NULL)
1576 goto error_return;
1577 amt = sizeof (struct xcoff_section_tdata);
1578 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1579 if (coff_section_data (abfd, csect)->tdata == NULL)
1580 goto error_return;
1581 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1583 if (first_csect == NULL)
1584 first_csect = csect;
1586 if (sym.n_sclass == C_EXT)
1588 csect->flags |= SEC_IS_COMMON;
1589 csect->size = 0;
1590 section = csect;
1591 value = aux.x_csect.x_scnlen.l;
1594 break;
1597 /* Check for magic symbol names. */
1598 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1599 && aux.x_csect.x_smclas != XMC_TC
1600 && aux.x_csect.x_smclas != XMC_TD)
1602 int i = -1;
1604 if (name[0] == '_')
1606 if (strcmp (name, "_text") == 0)
1607 i = XCOFF_SPECIAL_SECTION_TEXT;
1608 else if (strcmp (name, "_etext") == 0)
1609 i = XCOFF_SPECIAL_SECTION_ETEXT;
1610 else if (strcmp (name, "_data") == 0)
1611 i = XCOFF_SPECIAL_SECTION_DATA;
1612 else if (strcmp (name, "_edata") == 0)
1613 i = XCOFF_SPECIAL_SECTION_EDATA;
1614 else if (strcmp (name, "_end") == 0)
1615 i = XCOFF_SPECIAL_SECTION_END;
1617 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1618 i = XCOFF_SPECIAL_SECTION_END2;
1620 if (i != -1)
1621 xcoff_hash_table (info)->special_sections[i] = csect;
1624 /* Now we have enough information to add the symbol to the
1625 linker hash table. */
1627 if (sym.n_sclass == C_EXT)
1629 bfd_boolean copy;
1631 BFD_ASSERT (section != NULL);
1633 /* We must copy the name into memory if we got it from the
1634 syment itself, rather than the string table. */
1635 copy = default_copy;
1636 if (sym._n._n_n._n_zeroes != 0
1637 || sym._n._n_n._n_offset == 0)
1638 copy = TRUE;
1640 /* The AIX linker appears to only detect multiple symbol
1641 definitions when there is a reference to the symbol. If
1642 a symbol is defined multiple times, and the only
1643 references are from the same object file, the AIX linker
1644 appears to permit it. It does not merge the different
1645 definitions, but handles them independently. On the
1646 other hand, if there is a reference, the linker reports
1647 an error.
1649 This matters because the AIX <net/net_globals.h> header
1650 file actually defines an initialized array, so we have to
1651 actually permit that to work.
1653 Just to make matters even more confusing, the AIX linker
1654 appears to permit multiple symbol definitions whenever
1655 the second definition is in an archive rather than an
1656 object file. This may be a consequence of the manner in
1657 which it handles archives: I think it may load the entire
1658 archive in as separate csects, and then let garbage
1659 collection discard symbols.
1661 We also have to handle the case of statically linking a
1662 shared object, which will cause symbol redefinitions,
1663 although this is an easier case to detect. */
1665 if (info->hash->creator == abfd->xvec)
1667 if (! bfd_is_und_section (section))
1668 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1669 name, TRUE, copy, FALSE);
1670 else
1671 /* Make a copy of the symbol name to prevent problems with
1672 merging symbols. */
1673 *sym_hash = ((struct xcoff_link_hash_entry *)
1674 bfd_wrapped_link_hash_lookup (abfd, info, name,
1675 TRUE, TRUE, FALSE));
1677 if (*sym_hash == NULL)
1678 goto error_return;
1679 if (((*sym_hash)->root.type == bfd_link_hash_defined
1680 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1681 && ! bfd_is_und_section (section)
1682 && ! bfd_is_com_section (section))
1684 /* This is a second definition of a defined symbol. */
1685 if ((abfd->flags & DYNAMIC) != 0
1686 && ((*sym_hash)->smclas != XMC_GL
1687 || aux.x_csect.x_smclas == XMC_GL
1688 || ((*sym_hash)->root.u.def.section->owner->flags
1689 & DYNAMIC) == 0))
1691 /* The new symbol is from a shared library, and
1692 either the existing symbol is not global
1693 linkage code or this symbol is global linkage
1694 code. If the existing symbol is global
1695 linkage code and the new symbol is not, then
1696 we want to use the new symbol. */
1697 section = bfd_und_section_ptr;
1698 value = 0;
1700 else if (((*sym_hash)->root.u.def.section->owner->flags
1701 & DYNAMIC) != 0)
1703 /* The existing symbol is from a shared library.
1704 Replace it. */
1705 (*sym_hash)->root.type = bfd_link_hash_undefined;
1706 (*sym_hash)->root.u.undef.abfd =
1707 (*sym_hash)->root.u.def.section->owner;
1709 else if (abfd->my_archive != NULL)
1711 /* This is a redefinition in an object contained
1712 in an archive. Just ignore it. See the
1713 comment above. */
1714 section = bfd_und_section_ptr;
1715 value = 0;
1717 else if ((*sym_hash)->root.u.undef.next != NULL
1718 || info->hash->undefs_tail == &(*sym_hash)->root)
1720 /* This symbol has been referenced. In this
1721 case, we just continue and permit the
1722 multiple definition error. See the comment
1723 above about the behaviour of the AIX linker. */
1725 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1727 /* The symbols are both csects of the same
1728 class. There is at least a chance that this
1729 is a semi-legitimate redefinition. */
1730 section = bfd_und_section_ptr;
1731 value = 0;
1732 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1735 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1736 && ((*sym_hash)->root.type == bfd_link_hash_defined
1737 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1738 && (bfd_is_und_section (section)
1739 || bfd_is_com_section (section)))
1741 /* This is a reference to a multiply defined symbol.
1742 Report the error now. See the comment above
1743 about the behaviour of the AIX linker. We could
1744 also do this with warning symbols, but I'm not
1745 sure the XCOFF linker is wholly prepared to
1746 handle them, and that would only be a warning,
1747 not an error. */
1748 if (! ((*info->callbacks->multiple_definition)
1749 (info, (*sym_hash)->root.root.string,
1750 NULL, NULL, (bfd_vma) 0,
1751 (*sym_hash)->root.u.def.section->owner,
1752 (*sym_hash)->root.u.def.section,
1753 (*sym_hash)->root.u.def.value)))
1754 goto error_return;
1755 /* Try not to give this error too many times. */
1756 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1760 /* _bfd_generic_link_add_one_symbol may call the linker to
1761 generate an error message, and the linker may try to read
1762 the symbol table to give a good error. Right now, the
1763 line numbers are in an inconsistent state, since they are
1764 counted both in the real sections and in the new csects.
1765 We need to leave the count in the real sections so that
1766 the linker can report the line number of the error
1767 correctly, so temporarily clobber the link to the csects
1768 so that the linker will not try to read the line numbers
1769 a second time from the csects. */
1770 BFD_ASSERT (last_real->next == first_csect);
1771 last_real->next = NULL;
1772 if (! (_bfd_generic_link_add_one_symbol
1773 (info, abfd, name, flags, section, value,
1774 NULL, copy, TRUE,
1775 (struct bfd_link_hash_entry **) sym_hash)))
1776 goto error_return;
1777 last_real->next = first_csect;
1779 if (smtyp == XTY_CM)
1781 if ((*sym_hash)->root.type != bfd_link_hash_common
1782 || (*sym_hash)->root.u.c.p->section != csect)
1783 /* We don't need the common csect we just created. */
1784 csect->size = 0;
1785 else
1786 (*sym_hash)->root.u.c.p->alignment_power
1787 = csect->alignment_power;
1790 if (info->hash->creator == abfd->xvec)
1792 int flag;
1794 if (smtyp == XTY_ER || smtyp == XTY_CM)
1795 flag = XCOFF_REF_REGULAR;
1796 else
1797 flag = XCOFF_DEF_REGULAR;
1798 (*sym_hash)->flags |= flag;
1800 if ((*sym_hash)->smclas == XMC_UA
1801 || flag == XCOFF_DEF_REGULAR)
1802 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1806 *csect_cache = csect;
1808 esym += (sym.n_numaux + 1) * symesz;
1809 sym_hash += sym.n_numaux + 1;
1810 csect_cache += sym.n_numaux + 1;
1813 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1815 /* Make sure that we have seen all the relocs. */
1816 for (o = abfd->sections; o != first_csect; o = o->next)
1818 /* Reset the section size and the line number count, since the
1819 data is now attached to the csects. Don't reset the size of
1820 the .debug section, since we need to read it below in
1821 bfd_xcoff_size_dynamic_sections. */
1822 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1823 o->size = 0;
1824 o->lineno_count = 0;
1826 if ((o->flags & SEC_RELOC) != 0)
1828 bfd_size_type i;
1829 struct internal_reloc *rel;
1830 asection **rel_csect;
1832 rel = reloc_info[o->target_index].relocs;
1833 rel_csect = reloc_info[o->target_index].csects;
1835 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1837 if (*rel_csect == NULL)
1839 (*_bfd_error_handler)
1840 (_("%B: reloc %s:%d not in csect"),
1841 abfd, o->name, i);
1842 bfd_set_error (bfd_error_bad_value);
1843 goto error_return;
1846 /* We identify all symbols which are called, so that we
1847 can create glue code for calls to functions imported
1848 from dynamic objects. */
1849 if (info->hash->creator == abfd->xvec
1850 && *rel_csect != bfd_und_section_ptr
1851 && (rel->r_type == R_BR
1852 || rel->r_type == R_RBR)
1853 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1855 struct xcoff_link_hash_entry *h;
1857 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1858 h->flags |= XCOFF_CALLED;
1859 /* If the symbol name starts with a period, it is
1860 the code of a function. If the symbol is
1861 currently undefined, then add an undefined symbol
1862 for the function descriptor. This should do no
1863 harm, because any regular object that defines the
1864 function should also define the function
1865 descriptor. It helps, because it means that we
1866 will identify the function descriptor with a
1867 dynamic object if a dynamic object defines it. */
1868 if (h->root.root.string[0] == '.'
1869 && h->descriptor == NULL)
1871 struct xcoff_link_hash_entry *hds;
1872 struct bfd_link_hash_entry *bh;
1874 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1875 h->root.root.string + 1,
1876 TRUE, FALSE, TRUE);
1877 if (hds == NULL)
1878 goto error_return;
1879 if (hds->root.type == bfd_link_hash_new)
1881 bh = &hds->root;
1882 if (! (_bfd_generic_link_add_one_symbol
1883 (info, abfd, hds->root.root.string,
1884 (flagword) 0, bfd_und_section_ptr,
1885 (bfd_vma) 0, NULL, FALSE,
1886 TRUE, &bh)))
1887 goto error_return;
1888 hds = (struct xcoff_link_hash_entry *) bh;
1890 hds->flags |= XCOFF_DESCRIPTOR;
1891 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1892 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1893 hds->descriptor = h;
1894 h->descriptor = hds;
1899 free (reloc_info[o->target_index].csects);
1900 reloc_info[o->target_index].csects = NULL;
1902 /* Reset SEC_RELOC and the reloc_count, since the reloc
1903 information is now attached to the csects. */
1904 o->flags &=~ SEC_RELOC;
1905 o->reloc_count = 0;
1907 /* If we are not keeping memory, free the reloc information. */
1908 if (! info->keep_memory
1909 && coff_section_data (abfd, o) != NULL
1910 && coff_section_data (abfd, o)->relocs != NULL
1911 && ! coff_section_data (abfd, o)->keep_relocs)
1913 free (coff_section_data (abfd, o)->relocs);
1914 coff_section_data (abfd, o)->relocs = NULL;
1918 /* Free up the line numbers. FIXME: We could cache these
1919 somewhere for the final link, to avoid reading them again. */
1920 if (reloc_info[o->target_index].linenos != NULL)
1922 free (reloc_info[o->target_index].linenos);
1923 reloc_info[o->target_index].linenos = NULL;
1927 free (reloc_info);
1929 obj_coff_keep_syms (abfd) = keep_syms;
1931 return TRUE;
1933 error_return:
1934 if (reloc_info != NULL)
1936 for (o = abfd->sections; o != NULL; o = o->next)
1938 if (reloc_info[o->target_index].csects != NULL)
1939 free (reloc_info[o->target_index].csects);
1940 if (reloc_info[o->target_index].linenos != NULL)
1941 free (reloc_info[o->target_index].linenos);
1943 free (reloc_info);
1945 obj_coff_keep_syms (abfd) = keep_syms;
1946 return FALSE;
1949 #undef N_TMASK
1950 #undef N_BTSHFT
1952 /* Add symbols from an XCOFF object file. */
1954 static bfd_boolean
1955 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1957 if (! _bfd_coff_get_external_symbols (abfd))
1958 return FALSE;
1959 if (! xcoff_link_add_symbols (abfd, info))
1960 return FALSE;
1961 if (! info->keep_memory)
1963 if (! _bfd_coff_free_symbols (abfd))
1964 return FALSE;
1966 return TRUE;
1969 /* Look through the loader symbols to see if this dynamic object
1970 should be included in the link. The native linker uses the loader
1971 symbols, not the normal symbol table, so we do too. */
1973 static bfd_boolean
1974 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1975 struct bfd_link_info *info,
1976 bfd_boolean *pneeded)
1978 asection *lsec;
1979 bfd_byte *contents;
1980 struct internal_ldhdr ldhdr;
1981 const char *strings;
1982 bfd_byte *elsym, *elsymend;
1984 *pneeded = FALSE;
1986 lsec = bfd_get_section_by_name (abfd, ".loader");
1987 if (lsec == NULL)
1988 /* There are no symbols, so don't try to include it. */
1989 return TRUE;
1991 if (! xcoff_get_section_contents (abfd, lsec))
1992 return FALSE;
1993 contents = coff_section_data (abfd, lsec)->contents;
1995 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
1997 strings = (char *) contents + ldhdr.l_stoff;
1999 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2001 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2002 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2004 struct internal_ldsym ldsym;
2005 char nambuf[SYMNMLEN + 1];
2006 const char *name;
2007 struct bfd_link_hash_entry *h;
2009 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2011 /* We are only interested in exported symbols. */
2012 if ((ldsym.l_smtype & L_EXPORT) == 0)
2013 continue;
2015 if (ldsym._l._l_l._l_zeroes == 0)
2016 name = strings + ldsym._l._l_l._l_offset;
2017 else
2019 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2020 nambuf[SYMNMLEN] = '\0';
2021 name = nambuf;
2024 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2026 /* We are only interested in symbols that are currently
2027 undefined. At this point we know that we are using an XCOFF
2028 hash table. */
2029 if (h != NULL
2030 && h->type == bfd_link_hash_undefined
2031 && (((struct xcoff_link_hash_entry *) h)->flags
2032 & XCOFF_DEF_DYNAMIC) == 0)
2034 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2035 return FALSE;
2036 *pneeded = TRUE;
2037 return TRUE;
2041 /* We do not need this shared object. */
2042 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2044 free (coff_section_data (abfd, lsec)->contents);
2045 coff_section_data (abfd, lsec)->contents = NULL;
2048 return TRUE;
2051 /* Look through the symbols to see if this object file should be
2052 included in the link. */
2054 static bfd_boolean
2055 xcoff_link_check_ar_symbols (bfd *abfd,
2056 struct bfd_link_info *info,
2057 bfd_boolean *pneeded)
2059 bfd_size_type symesz;
2060 bfd_byte *esym;
2061 bfd_byte *esym_end;
2063 *pneeded = FALSE;
2065 if ((abfd->flags & DYNAMIC) != 0
2066 && ! info->static_link
2067 && info->hash->creator == abfd->xvec)
2068 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2070 symesz = bfd_coff_symesz (abfd);
2071 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2072 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2073 while (esym < esym_end)
2075 struct internal_syment sym;
2077 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2079 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
2081 const char *name;
2082 char buf[SYMNMLEN + 1];
2083 struct bfd_link_hash_entry *h;
2085 /* This symbol is externally visible, and is defined by this
2086 object file. */
2087 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2089 if (name == NULL)
2090 return FALSE;
2091 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2093 /* We are only interested in symbols that are currently
2094 undefined. If a symbol is currently known to be common,
2095 XCOFF linkers do not bring in an object file which
2096 defines it. We also don't bring in symbols to satisfy
2097 undefined references in shared objects. */
2098 if (h != NULL
2099 && h->type == bfd_link_hash_undefined
2100 && (info->hash->creator != abfd->xvec
2101 || (((struct xcoff_link_hash_entry *) h)->flags
2102 & XCOFF_DEF_DYNAMIC) == 0))
2104 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2105 return FALSE;
2106 *pneeded = TRUE;
2107 return TRUE;
2111 esym += (sym.n_numaux + 1) * symesz;
2114 /* We do not need this object file. */
2115 return TRUE;
2118 /* Check a single archive element to see if we need to include it in
2119 the link. *PNEEDED is set according to whether this element is
2120 needed in the link or not. This is called via
2121 _bfd_generic_link_add_archive_symbols. */
2123 static bfd_boolean
2124 xcoff_link_check_archive_element (bfd *abfd,
2125 struct bfd_link_info *info,
2126 bfd_boolean *pneeded)
2128 if (! _bfd_coff_get_external_symbols (abfd))
2129 return FALSE;
2131 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2132 return FALSE;
2134 if (*pneeded)
2136 if (! xcoff_link_add_symbols (abfd, info))
2137 return FALSE;
2140 if (! info->keep_memory || ! *pneeded)
2142 if (! _bfd_coff_free_symbols (abfd))
2143 return FALSE;
2146 return TRUE;
2149 /* Given an XCOFF BFD, add symbols to the global hash table as
2150 appropriate. */
2152 bfd_boolean
2153 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2155 switch (bfd_get_format (abfd))
2157 case bfd_object:
2158 return xcoff_link_add_object_symbols (abfd, info);
2160 case bfd_archive:
2161 /* If the archive has a map, do the usual search. We then need
2162 to check the archive for dynamic objects, because they may not
2163 appear in the archive map even though they should, perhaps, be
2164 included. If the archive has no map, we just consider each object
2165 file in turn, since that apparently is what the AIX native linker
2166 does. */
2167 if (bfd_has_map (abfd))
2169 if (! (_bfd_generic_link_add_archive_symbols
2170 (abfd, info, xcoff_link_check_archive_element)))
2171 return FALSE;
2175 bfd *member;
2177 member = bfd_openr_next_archived_file (abfd, NULL);
2178 while (member != NULL)
2180 if (bfd_check_format (member, bfd_object)
2181 && (info->hash->creator == member->xvec)
2182 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2184 bfd_boolean needed;
2186 if (! xcoff_link_check_archive_element (member, info,
2187 &needed))
2188 return FALSE;
2189 if (needed)
2190 member->archive_pass = -1;
2192 member = bfd_openr_next_archived_file (abfd, member);
2196 return TRUE;
2198 default:
2199 bfd_set_error (bfd_error_wrong_format);
2200 return FALSE;
2204 /* Mark a symbol as not being garbage, including the section in which
2205 it is defined. */
2207 static inline bfd_boolean
2208 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2210 if ((h->flags & XCOFF_MARK) != 0)
2211 return TRUE;
2213 h->flags |= XCOFF_MARK;
2214 if (h->root.type == bfd_link_hash_defined
2215 || h->root.type == bfd_link_hash_defweak)
2217 asection *hsec;
2219 hsec = h->root.u.def.section;
2220 if (! bfd_is_abs_section (hsec)
2221 && (hsec->flags & SEC_MARK) == 0)
2223 if (! xcoff_mark (info, hsec))
2224 return FALSE;
2228 if (h->toc_section != NULL
2229 && (h->toc_section->flags & SEC_MARK) == 0)
2231 if (! xcoff_mark (info, h->toc_section))
2232 return FALSE;
2235 return TRUE;
2238 /* The mark phase of garbage collection. For a given section, mark
2239 it, and all the sections which define symbols to which it refers.
2240 Because this function needs to look at the relocs, we also count
2241 the number of relocs which need to be copied into the .loader
2242 section. */
2244 static bfd_boolean
2245 xcoff_mark (struct bfd_link_info *info, asection *sec)
2247 if (bfd_is_abs_section (sec)
2248 || (sec->flags & SEC_MARK) != 0)
2249 return TRUE;
2251 sec->flags |= SEC_MARK;
2253 if (sec->owner->xvec == info->hash->creator
2254 && coff_section_data (sec->owner, sec) != NULL
2255 && xcoff_section_data (sec->owner, sec) != NULL)
2257 struct xcoff_link_hash_entry **hp, **hpend;
2258 struct internal_reloc *rel, *relend;
2260 /* Mark all the symbols in this section. */
2261 hp = (obj_xcoff_sym_hashes (sec->owner)
2262 + xcoff_section_data (sec->owner, sec)->first_symndx);
2263 hpend = (obj_xcoff_sym_hashes (sec->owner)
2264 + xcoff_section_data (sec->owner, sec)->last_symndx);
2265 for (; hp < hpend; hp++)
2267 struct xcoff_link_hash_entry *h;
2269 h = *hp;
2270 if (h != NULL
2271 && (h->flags & XCOFF_MARK) == 0)
2273 if (! xcoff_mark_symbol (info, h))
2274 return FALSE;
2278 /* Look through the section relocs. */
2279 if ((sec->flags & SEC_RELOC) != 0
2280 && sec->reloc_count > 0)
2282 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2283 NULL, FALSE, NULL);
2284 if (rel == NULL)
2285 return FALSE;
2286 relend = rel + sec->reloc_count;
2287 for (; rel < relend; rel++)
2289 asection *rsec;
2290 struct xcoff_link_hash_entry *h;
2292 if ((unsigned int) rel->r_symndx
2293 > obj_raw_syment_count (sec->owner))
2294 continue;
2296 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2297 if (h != NULL
2298 && (h->flags & XCOFF_MARK) == 0)
2300 if (! xcoff_mark_symbol (info, h))
2301 return FALSE;
2304 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2305 if (rsec != NULL
2306 && (rsec->flags & SEC_MARK) == 0)
2308 if (! xcoff_mark (info, rsec))
2309 return FALSE;
2312 /* See if this reloc needs to be copied into the .loader
2313 section. */
2314 switch (rel->r_type)
2316 default:
2317 if (h == NULL
2318 || h->root.type == bfd_link_hash_defined
2319 || h->root.type == bfd_link_hash_defweak
2320 || h->root.type == bfd_link_hash_common
2321 || ((h->flags & XCOFF_CALLED) != 0
2322 && (h->root.type == bfd_link_hash_undefined
2323 || h->root.type == bfd_link_hash_undefweak)
2324 && h->root.root.string[0] == '.'
2325 && h->descriptor != NULL
2326 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2327 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2328 && (h->descriptor->flags
2329 & XCOFF_DEF_REGULAR) == 0))))
2330 break;
2331 /* Fall through. */
2332 case R_POS:
2333 case R_NEG:
2334 case R_RL:
2335 case R_RLA:
2336 ++xcoff_hash_table (info)->ldrel_count;
2337 if (h != NULL)
2338 h->flags |= XCOFF_LDREL;
2339 break;
2340 case R_TOC:
2341 case R_GL:
2342 case R_TCL:
2343 case R_TRL:
2344 case R_TRLA:
2345 /* We should never need a .loader reloc for a TOC
2346 relative reloc. */
2347 break;
2351 if (! info->keep_memory
2352 && coff_section_data (sec->owner, sec) != NULL
2353 && coff_section_data (sec->owner, sec)->relocs != NULL
2354 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2356 free (coff_section_data (sec->owner, sec)->relocs);
2357 coff_section_data (sec->owner, sec)->relocs = NULL;
2362 return TRUE;
2365 /* Routines that are called after all the input files have been
2366 handled, but before the sections are laid out in memory. */
2368 /* The sweep phase of garbage collection. Remove all garbage
2369 sections. */
2371 static void
2372 xcoff_sweep (struct bfd_link_info *info)
2374 bfd *sub;
2376 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2378 asection *o;
2380 for (o = sub->sections; o != NULL; o = o->next)
2382 if ((o->flags & SEC_MARK) == 0)
2384 /* Keep all sections from non-XCOFF input files. Keep
2385 special sections. Keep .debug sections for the
2386 moment. */
2387 if (sub->xvec != info->hash->creator
2388 || o == xcoff_hash_table (info)->debug_section
2389 || o == xcoff_hash_table (info)->loader_section
2390 || o == xcoff_hash_table (info)->linkage_section
2391 || o == xcoff_hash_table (info)->toc_section
2392 || o == xcoff_hash_table (info)->descriptor_section
2393 || strcmp (o->name, ".debug") == 0)
2394 o->flags |= SEC_MARK;
2395 else
2397 o->size = 0;
2398 o->reloc_count = 0;
2399 o->lineno_count = 0;
2406 /* Record the number of elements in a set. This is used to output the
2407 correct csect length. */
2409 bfd_boolean
2410 bfd_xcoff_link_record_set (bfd *output_bfd,
2411 struct bfd_link_info *info,
2412 struct bfd_link_hash_entry *harg,
2413 bfd_size_type size)
2415 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2416 struct xcoff_link_size_list *n;
2417 bfd_size_type amt;
2419 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2420 return TRUE;
2422 /* This will hardly ever be called. I don't want to burn four bytes
2423 per global symbol, so instead the size is kept on a linked list
2424 attached to the hash table. */
2425 amt = sizeof (* n);
2426 n = bfd_alloc (output_bfd, amt);
2427 if (n == NULL)
2428 return FALSE;
2429 n->next = xcoff_hash_table (info)->size_list;
2430 n->h = h;
2431 n->size = size;
2432 xcoff_hash_table (info)->size_list = n;
2434 h->flags |= XCOFF_HAS_SIZE;
2436 return TRUE;
2439 /* Import a symbol. */
2441 bfd_boolean
2442 bfd_xcoff_import_symbol (bfd *output_bfd,
2443 struct bfd_link_info *info,
2444 struct bfd_link_hash_entry *harg,
2445 bfd_vma val,
2446 const char *imppath,
2447 const char *impfile,
2448 const char *impmember,
2449 unsigned int syscall_flag)
2451 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2453 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2454 return TRUE;
2456 /* A symbol name which starts with a period is the code for a
2457 function. If the symbol is undefined, then add an undefined
2458 symbol for the function descriptor, and import that instead. */
2459 if (h->root.root.string[0] == '.'
2460 && h->root.type == bfd_link_hash_undefined
2461 && val == (bfd_vma) -1)
2463 struct xcoff_link_hash_entry *hds;
2465 hds = h->descriptor;
2466 if (hds == NULL)
2468 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2469 h->root.root.string + 1,
2470 TRUE, FALSE, TRUE);
2471 if (hds == NULL)
2472 return FALSE;
2473 if (hds->root.type == bfd_link_hash_new)
2475 hds->root.type = bfd_link_hash_undefined;
2476 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2478 hds->flags |= XCOFF_DESCRIPTOR;
2479 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2480 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2481 hds->descriptor = h;
2482 h->descriptor = hds;
2485 /* Now, if the descriptor is undefined, import the descriptor
2486 rather than the symbol we were told to import. FIXME: Is
2487 this correct in all cases? */
2488 if (hds->root.type == bfd_link_hash_undefined)
2489 h = hds;
2492 h->flags |= (XCOFF_IMPORT | syscall_flag);
2494 if (val != (bfd_vma) -1)
2496 if (h->root.type == bfd_link_hash_defined
2497 && (! bfd_is_abs_section (h->root.u.def.section)
2498 || h->root.u.def.value != val))
2500 if (! ((*info->callbacks->multiple_definition)
2501 (info, h->root.root.string, h->root.u.def.section->owner,
2502 h->root.u.def.section, h->root.u.def.value,
2503 output_bfd, bfd_abs_section_ptr, val)))
2504 return FALSE;
2507 h->root.type = bfd_link_hash_defined;
2508 h->root.u.def.section = bfd_abs_section_ptr;
2509 h->root.u.def.value = val;
2512 /* We overload the ldindx field to hold the l_ifile value for this
2513 symbol. */
2514 BFD_ASSERT (h->ldsym == NULL);
2515 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2516 if (imppath == NULL)
2517 h->ldindx = -1;
2518 else
2520 unsigned int c;
2521 struct xcoff_import_file **pp;
2523 /* We start c at 1 because the first entry in the import list is
2524 reserved for the library search path. */
2525 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2526 *pp != NULL;
2527 pp = &(*pp)->next, ++c)
2529 if (strcmp ((*pp)->path, imppath) == 0
2530 && strcmp ((*pp)->file, impfile) == 0
2531 && strcmp ((*pp)->member, impmember) == 0)
2532 break;
2535 if (*pp == NULL)
2537 struct xcoff_import_file *n;
2538 bfd_size_type amt = sizeof (* n);
2540 n = bfd_alloc (output_bfd, amt);
2541 if (n == NULL)
2542 return FALSE;
2543 n->next = NULL;
2544 n->path = imppath;
2545 n->file = impfile;
2546 n->member = impmember;
2547 *pp = n;
2550 h->ldindx = c;
2553 return TRUE;
2556 /* Export a symbol. */
2558 bfd_boolean
2559 bfd_xcoff_export_symbol (bfd *output_bfd,
2560 struct bfd_link_info *info,
2561 struct bfd_link_hash_entry *harg)
2563 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2565 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2566 return TRUE;
2568 h->flags |= XCOFF_EXPORT;
2570 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2571 I'm just going to ignore it until somebody explains it. */
2573 /* See if this is a function descriptor. It may be one even though
2574 it is not so marked. */
2575 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2576 && h->root.root.string[0] != '.')
2578 char *fnname;
2579 struct xcoff_link_hash_entry *hfn;
2580 bfd_size_type amt = strlen (h->root.root.string) + 2;
2582 fnname = bfd_malloc (amt);
2583 if (fnname == NULL)
2584 return FALSE;
2585 fnname[0] = '.';
2586 strcpy (fnname + 1, h->root.root.string);
2587 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2588 fnname, FALSE, FALSE, TRUE);
2589 free (fnname);
2590 if (hfn != NULL
2591 && hfn->smclas == XMC_PR
2592 && (hfn->root.type == bfd_link_hash_defined
2593 || hfn->root.type == bfd_link_hash_defweak))
2595 h->flags |= XCOFF_DESCRIPTOR;
2596 h->descriptor = hfn;
2597 hfn->descriptor = h;
2601 /* Make sure we don't garbage collect this symbol. */
2602 if (! xcoff_mark_symbol (info, h))
2603 return FALSE;
2605 /* If this is a function descriptor, make sure we don't garbage
2606 collect the associated function code. We normally don't have to
2607 worry about this, because the descriptor will be attached to a
2608 section with relocs, but if we are creating the descriptor
2609 ourselves those relocs will not be visible to the mark code. */
2610 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2612 if (! xcoff_mark_symbol (info, h->descriptor))
2613 return FALSE;
2616 return TRUE;
2619 /* Count a reloc against a symbol. This is called for relocs
2620 generated by the linker script, typically for global constructors
2621 and destructors. */
2623 bfd_boolean
2624 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2625 struct bfd_link_info *info,
2626 const char *name)
2628 struct xcoff_link_hash_entry *h;
2630 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2631 return TRUE;
2633 h = ((struct xcoff_link_hash_entry *)
2634 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2635 FALSE));
2636 if (h == NULL)
2638 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2639 bfd_set_error (bfd_error_no_symbols);
2640 return FALSE;
2643 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2644 ++xcoff_hash_table (info)->ldrel_count;
2646 /* Mark the symbol to avoid garbage collection. */
2647 if (! xcoff_mark_symbol (info, h))
2648 return FALSE;
2650 return TRUE;
2653 /* This function is called for each symbol to which the linker script
2654 assigns a value. */
2656 bfd_boolean
2657 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2658 struct bfd_link_info *info,
2659 const char *name)
2661 struct xcoff_link_hash_entry *h;
2663 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2664 return TRUE;
2666 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2667 FALSE);
2668 if (h == NULL)
2669 return FALSE;
2671 h->flags |= XCOFF_DEF_REGULAR;
2673 return TRUE;
2676 /* Add a symbol to the .loader symbols, if necessary. */
2678 static bfd_boolean
2679 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
2681 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2682 bfd_size_type amt;
2684 if (h->root.type == bfd_link_hash_warning)
2685 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
2687 /* __rtinit, this symbol has special handling. */
2688 if (h->flags & XCOFF_RTINIT)
2689 return TRUE;
2691 /* If this is a final link, and the symbol was defined as a common
2692 symbol in a regular object file, and there was no definition in
2693 any dynamic object, then the linker will have allocated space for
2694 the symbol in a common section but the XCOFF_DEF_REGULAR flag
2695 will not have been set. */
2696 if (h->root.type == bfd_link_hash_defined
2697 && (h->flags & XCOFF_DEF_REGULAR) == 0
2698 && (h->flags & XCOFF_REF_REGULAR) != 0
2699 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2700 && (bfd_is_abs_section (h->root.u.def.section)
2701 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
2702 h->flags |= XCOFF_DEF_REGULAR;
2704 /* If all defined symbols should be exported, mark them now. We
2705 don't want to export the actual functions, just the function
2706 descriptors. */
2707 if (ldinfo->export_defineds
2708 && (h->flags & XCOFF_DEF_REGULAR) != 0
2709 && h->root.root.string[0] != '.')
2711 bfd_boolean export;
2713 /* We don't export a symbol which is being defined by an object
2714 included from an archive which contains a shared object. The
2715 rationale is that if an archive contains both an unshared and
2716 a shared object, then there must be some reason that the
2717 unshared object is unshared, and we don't want to start
2718 providing a shared version of it. In particular, this solves
2719 a bug involving the _savefNN set of functions. gcc will call
2720 those functions without providing a slot to restore the TOC,
2721 so it is essential that these functions be linked in directly
2722 and not from a shared object, which means that a shared
2723 object which also happens to link them in must not export
2724 them. This is confusing, but I haven't been able to think of
2725 a different approach. Note that the symbols can, of course,
2726 be exported explicitly. */
2727 export = TRUE;
2728 if ((h->root.type == bfd_link_hash_defined
2729 || h->root.type == bfd_link_hash_defweak)
2730 && h->root.u.def.section->owner != NULL
2731 && h->root.u.def.section->owner->my_archive != NULL)
2733 bfd *arbfd, *member;
2735 arbfd = h->root.u.def.section->owner->my_archive;
2736 member = bfd_openr_next_archived_file (arbfd, NULL);
2737 while (member != NULL)
2739 if ((member->flags & DYNAMIC) != 0)
2741 export = FALSE;
2742 break;
2744 member = bfd_openr_next_archived_file (arbfd, member);
2748 if (export)
2749 h->flags |= XCOFF_EXPORT;
2752 /* We don't want to garbage collect symbols which are not defined in
2753 XCOFF files. This is a convenient place to mark them. */
2754 if (xcoff_hash_table (ldinfo->info)->gc
2755 && (h->flags & XCOFF_MARK) == 0
2756 && (h->root.type == bfd_link_hash_defined
2757 || h->root.type == bfd_link_hash_defweak)
2758 && (h->root.u.def.section->owner == NULL
2759 || (h->root.u.def.section->owner->xvec
2760 != ldinfo->info->hash->creator)))
2761 h->flags |= XCOFF_MARK;
2763 /* If this symbol is called and defined in a dynamic object, or it
2764 is imported, then we need to set up global linkage code for it.
2765 (Unless we did garbage collection and we didn't need this
2766 symbol.) */
2767 if ((h->flags & XCOFF_CALLED) != 0
2768 && (h->root.type == bfd_link_hash_undefined
2769 || h->root.type == bfd_link_hash_undefweak)
2770 && h->root.root.string[0] == '.'
2771 && h->descriptor != NULL
2772 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2773 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2774 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
2775 && (! xcoff_hash_table (ldinfo->info)->gc
2776 || (h->flags & XCOFF_MARK) != 0))
2778 asection *sec;
2779 struct xcoff_link_hash_entry *hds;
2781 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2782 h->root.type = bfd_link_hash_defined;
2783 h->root.u.def.section = sec;
2784 h->root.u.def.value = sec->size;
2785 h->smclas = XMC_GL;
2786 h->flags |= XCOFF_DEF_REGULAR;
2787 sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
2789 /* The global linkage code requires a TOC entry for the
2790 descriptor. */
2791 hds = h->descriptor;
2792 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2793 || hds->root.type == bfd_link_hash_undefweak)
2794 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2795 hds->flags |= XCOFF_MARK;
2796 if (hds->toc_section == NULL)
2798 int byte_size;
2800 /* 32 vs 64
2801 xcoff32 uses 4 bytes in the toc.
2802 xcoff64 uses 8 bytes in the toc. */
2803 if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
2804 byte_size = 8;
2805 else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
2806 byte_size = 4;
2807 else
2808 return FALSE;
2810 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2811 hds->u.toc_offset = hds->toc_section->size;
2812 hds->toc_section->size += byte_size;
2813 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2814 ++hds->toc_section->reloc_count;
2815 hds->indx = -2;
2816 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2818 /* We need to call xcoff_build_ldsyms recursively here,
2819 because we may already have passed hds on the traversal. */
2820 xcoff_build_ldsyms (hds, p);
2824 /* If this symbol is exported, but not defined, we need to try to
2825 define it. */
2826 if ((h->flags & XCOFF_EXPORT) != 0
2827 && (h->flags & XCOFF_IMPORT) == 0
2828 && (h->flags & XCOFF_DEF_REGULAR) == 0
2829 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2830 && (h->root.type == bfd_link_hash_undefined
2831 || h->root.type == bfd_link_hash_undefweak))
2833 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2834 && (h->descriptor->root.type == bfd_link_hash_defined
2835 || h->descriptor->root.type == bfd_link_hash_defweak))
2837 asection *sec;
2839 /* This is an undefined function descriptor associated with
2840 a defined entry point. We can build up a function
2841 descriptor ourselves. Believe it or not, the AIX linker
2842 actually does this, and there are cases where we need to
2843 do it as well. */
2844 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
2845 h->root.type = bfd_link_hash_defined;
2846 h->root.u.def.section = sec;
2847 h->root.u.def.value = sec->size;
2848 h->smclas = XMC_DS;
2849 h->flags |= XCOFF_DEF_REGULAR;
2851 /* The size of the function descriptor depends if this is an
2852 xcoff32 (12) or xcoff64 (24). */
2853 sec->size +=
2854 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
2856 /* A function descriptor uses two relocs: one for the
2857 associated code, and one for the TOC address. */
2858 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
2859 sec->reloc_count += 2;
2861 /* We handle writing out the contents of the descriptor in
2862 xcoff_write_global_symbol. */
2864 else
2866 (*_bfd_error_handler)
2867 (_("warning: attempt to export undefined symbol `%s'"),
2868 h->root.root.string);
2869 h->ldsym = NULL;
2870 return TRUE;
2874 /* If this is still a common symbol, and it wasn't garbage
2875 collected, we need to actually allocate space for it in the .bss
2876 section. */
2877 if (h->root.type == bfd_link_hash_common
2878 && (! xcoff_hash_table (ldinfo->info)->gc
2879 || (h->flags & XCOFF_MARK) != 0)
2880 && h->root.u.c.p->section->size == 0)
2882 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2883 h->root.u.c.p->section->size = h->root.u.c.size;
2886 /* We need to add a symbol to the .loader section if it is mentioned
2887 in a reloc which we are copying to the .loader section and it was
2888 not defined or common, or if it is the entry point, or if it is
2889 being exported. */
2891 if (((h->flags & XCOFF_LDREL) == 0
2892 || h->root.type == bfd_link_hash_defined
2893 || h->root.type == bfd_link_hash_defweak
2894 || h->root.type == bfd_link_hash_common)
2895 && (h->flags & XCOFF_ENTRY) == 0
2896 && (h->flags & XCOFF_EXPORT) == 0)
2898 h->ldsym = NULL;
2899 return TRUE;
2902 /* We don't need to add this symbol if we did garbage collection and
2903 we did not mark this symbol. */
2904 if (xcoff_hash_table (ldinfo->info)->gc
2905 && (h->flags & XCOFF_MARK) == 0)
2907 h->ldsym = NULL;
2908 return TRUE;
2911 /* We may have already processed this symbol due to the recursive
2912 call above. */
2913 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2914 return TRUE;
2916 /* We need to add this symbol to the .loader symbols. */
2918 BFD_ASSERT (h->ldsym == NULL);
2919 amt = sizeof (struct internal_ldsym);
2920 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
2921 if (h->ldsym == NULL)
2923 ldinfo->failed = TRUE;
2924 return FALSE;
2927 if ((h->flags & XCOFF_IMPORT) != 0)
2928 h->ldsym->l_ifile = h->ldindx;
2930 /* The first 3 symbol table indices are reserved to indicate the
2931 data, text and bss sections. */
2932 h->ldindx = ldinfo->ldsym_count + 3;
2934 ++ldinfo->ldsym_count;
2936 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
2937 h->ldsym, h->root.root.string))
2938 return FALSE;
2940 h->flags |= XCOFF_BUILT_LDSYM;
2942 return TRUE;
2944 /* Build the .loader section. This is called by the XCOFF linker
2945 emulation before_allocation routine. We must set the size of the
2946 .loader section before the linker lays out the output file.
2947 LIBPATH is the library path to search for shared objects; this is
2948 normally built from the -L arguments passed to the linker. ENTRY
2949 is the name of the entry point symbol (the -e linker option).
2950 FILE_ALIGN is the alignment to use for sections within the file
2951 (the -H linker option). MAXSTACK is the maximum stack size (the
2952 -bmaxstack linker option). MAXDATA is the maximum data size (the
2953 -bmaxdata linker option). GC is whether to do garbage collection
2954 (the -bgc linker option). MODTYPE is the module type (the
2955 -bmodtype linker option). TEXTRO is whether the text section must
2956 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2957 whether all defined symbols should be exported (the -unix linker
2958 option). SPECIAL_SECTIONS is set by this routine to csects with
2959 magic names like _end. */
2961 bfd_boolean
2962 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
2963 struct bfd_link_info *info,
2964 const char *libpath,
2965 const char *entry,
2966 unsigned long file_align,
2967 unsigned long maxstack,
2968 unsigned long maxdata,
2969 bfd_boolean gc,
2970 int modtype,
2971 bfd_boolean textro,
2972 bfd_boolean export_defineds,
2973 asection **special_sections,
2974 bfd_boolean rtld)
2976 struct xcoff_link_hash_entry *hentry;
2977 asection *lsec;
2978 struct xcoff_loader_info ldinfo;
2979 int i;
2980 size_t impsize, impcount;
2981 struct xcoff_import_file *fl;
2982 struct internal_ldhdr *ldhdr;
2983 bfd_size_type stoff;
2984 char *out;
2985 asection *sec;
2986 bfd *sub;
2987 struct bfd_strtab_hash *debug_strtab;
2988 bfd_byte *debug_contents = NULL;
2989 bfd_size_type amt;
2991 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2993 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2994 special_sections[i] = NULL;
2995 return TRUE;
2998 ldinfo.failed = FALSE;
2999 ldinfo.output_bfd = output_bfd;
3000 ldinfo.info = info;
3001 ldinfo.export_defineds = export_defineds;
3002 ldinfo.ldsym_count = 0;
3003 ldinfo.string_size = 0;
3004 ldinfo.strings = NULL;
3005 ldinfo.string_alc = 0;
3007 xcoff_data (output_bfd)->maxstack = maxstack;
3008 xcoff_data (output_bfd)->maxdata = maxdata;
3009 xcoff_data (output_bfd)->modtype = modtype;
3011 xcoff_hash_table (info)->file_align = file_align;
3012 xcoff_hash_table (info)->textro = textro;
3014 hentry = NULL;
3015 if (entry != NULL)
3017 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3018 FALSE, FALSE, TRUE);
3019 if (hentry != NULL)
3020 hentry->flags |= XCOFF_ENTRY;
3023 /* __rtinit */
3024 if (info->init_function || info->fini_function || rtld)
3026 struct xcoff_link_hash_entry *hsym;
3027 struct internal_ldsym *ldsym;
3029 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3030 "__rtinit", FALSE, FALSE, TRUE);
3031 if (hsym == NULL)
3033 (*_bfd_error_handler)
3034 (_("error: undefined symbol __rtinit"));
3035 return FALSE;
3038 xcoff_mark_symbol (info, hsym);
3039 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3041 /* __rtinit initialized. */
3042 amt = sizeof (* ldsym);
3043 ldsym = bfd_malloc (amt);
3045 ldsym->l_value = 0; /* Will be filled in later. */
3046 ldsym->l_scnum = 2; /* Data section. */
3047 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3048 ldsym->l_smclas = 5; /* .rw. */
3049 ldsym->l_ifile = 0; /* Special system loader symbol. */
3050 ldsym->l_parm = 0; /* NA. */
3052 /* Force __rtinit to be the first symbol in the loader symbol table
3053 See xcoff_build_ldsyms
3055 The first 3 symbol table indices are reserved to indicate the data,
3056 text and bss sections. */
3057 BFD_ASSERT (0 == ldinfo.ldsym_count);
3059 hsym->ldindx = 3;
3060 ldinfo.ldsym_count = 1;
3061 hsym->ldsym = ldsym;
3063 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3064 hsym->ldsym, hsym->root.root.string))
3065 return FALSE;
3067 /* This symbol is written out by xcoff_write_global_symbol
3068 Set stuff up so xcoff_write_global_symbol logic works. */
3069 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3070 hsym->root.type = bfd_link_hash_defined;
3071 hsym->root.u.def.value = 0;
3074 /* Garbage collect unused sections. */
3075 if (info->relocatable
3076 || ! gc
3077 || hentry == NULL
3078 || (hentry->root.type != bfd_link_hash_defined
3079 && hentry->root.type != bfd_link_hash_defweak))
3081 gc = FALSE;
3082 xcoff_hash_table (info)->gc = FALSE;
3084 /* We still need to call xcoff_mark, in order to set ldrel_count
3085 correctly. */
3086 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3088 asection *o;
3090 for (o = sub->sections; o != NULL; o = o->next)
3092 if ((o->flags & SEC_MARK) == 0)
3094 if (! xcoff_mark (info, o))
3095 goto error_return;
3100 else
3102 if (! xcoff_mark (info, hentry->root.u.def.section))
3103 goto error_return;
3104 xcoff_sweep (info);
3105 xcoff_hash_table (info)->gc = TRUE;
3108 /* Return special sections to the caller. */
3109 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3111 sec = xcoff_hash_table (info)->special_sections[i];
3113 if (sec != NULL
3114 && gc
3115 && (sec->flags & SEC_MARK) == 0)
3116 sec = NULL;
3118 special_sections[i] = sec;
3121 if (info->input_bfds == NULL)
3122 /* I'm not sure what to do in this bizarre case. */
3123 return TRUE;
3125 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3126 (void *) &ldinfo);
3127 if (ldinfo.failed)
3128 goto error_return;
3130 /* Work out the size of the import file names. Each import file ID
3131 consists of three null terminated strings: the path, the file
3132 name, and the archive member name. The first entry in the list
3133 of names is the path to use to find objects, which the linker has
3134 passed in as the libpath argument. For some reason, the path
3135 entry in the other import file names appears to always be empty. */
3136 impsize = strlen (libpath) + 3;
3137 impcount = 1;
3138 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3140 ++impcount;
3141 impsize += (strlen (fl->path)
3142 + strlen (fl->file)
3143 + strlen (fl->member)
3144 + 3);
3147 /* Set up the .loader section header. */
3148 ldhdr = &xcoff_hash_table (info)->ldhdr;
3149 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3150 ldhdr->l_nsyms = ldinfo.ldsym_count;
3151 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3152 ldhdr->l_istlen = impsize;
3153 ldhdr->l_nimpid = impcount;
3154 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3155 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3156 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3157 ldhdr->l_stlen = ldinfo.string_size;
3158 stoff = ldhdr->l_impoff + impsize;
3159 if (ldinfo.string_size == 0)
3160 ldhdr->l_stoff = 0;
3161 else
3162 ldhdr->l_stoff = stoff;
3164 /* 64 bit elements to ldhdr
3165 The swap out routine for 32 bit will ignore them.
3166 Nothing fancy, symbols come after the header and relocs come
3167 after symbols. */
3168 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3169 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3170 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3172 /* We now know the final size of the .loader section. Allocate
3173 space for it. */
3174 lsec = xcoff_hash_table (info)->loader_section;
3175 lsec->size = stoff + ldhdr->l_stlen;
3176 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3177 if (lsec->contents == NULL)
3178 goto error_return;
3180 /* Set up the header. */
3181 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3183 /* Set up the import file names. */
3184 out = (char *) lsec->contents + ldhdr->l_impoff;
3185 strcpy (out, libpath);
3186 out += strlen (libpath) + 1;
3187 *out++ = '\0';
3188 *out++ = '\0';
3189 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3191 const char *s;
3193 s = fl->path;
3194 while ((*out++ = *s++) != '\0')
3196 s = fl->file;
3197 while ((*out++ = *s++) != '\0')
3199 s = fl->member;
3200 while ((*out++ = *s++) != '\0')
3204 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3206 /* Set up the symbol string table. */
3207 if (ldinfo.string_size > 0)
3209 memcpy (out, ldinfo.strings, ldinfo.string_size);
3210 free (ldinfo.strings);
3211 ldinfo.strings = NULL;
3214 /* We can't set up the symbol table or the relocs yet, because we
3215 don't yet know the final position of the various sections. The
3216 .loader symbols are written out when the corresponding normal
3217 symbols are written out in xcoff_link_input_bfd or
3218 xcoff_write_global_symbol. The .loader relocs are written out
3219 when the corresponding normal relocs are handled in
3220 xcoff_link_input_bfd. */
3222 /* Allocate space for the magic sections. */
3223 sec = xcoff_hash_table (info)->linkage_section;
3224 if (sec->size > 0)
3226 sec->contents = bfd_zalloc (output_bfd, sec->size);
3227 if (sec->contents == NULL)
3228 goto error_return;
3230 sec = xcoff_hash_table (info)->toc_section;
3231 if (sec->size > 0)
3233 sec->contents = bfd_zalloc (output_bfd, sec->size);
3234 if (sec->contents == NULL)
3235 goto error_return;
3237 sec = xcoff_hash_table (info)->descriptor_section;
3238 if (sec->size > 0)
3240 sec->contents = bfd_zalloc (output_bfd, sec->size);
3241 if (sec->contents == NULL)
3242 goto error_return;
3245 /* Now that we've done garbage collection, figure out the contents
3246 of the .debug section. */
3247 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3249 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3251 asection *subdeb;
3252 bfd_size_type symcount;
3253 unsigned long *debug_index;
3254 asection **csectpp;
3255 bfd_byte *esym, *esymend;
3256 bfd_size_type symesz;
3258 if (sub->xvec != info->hash->creator)
3259 continue;
3260 subdeb = bfd_get_section_by_name (sub, ".debug");
3261 if (subdeb == NULL || subdeb->size == 0)
3262 continue;
3264 if (info->strip == strip_all
3265 || info->strip == strip_debugger
3266 || info->discard == discard_all)
3268 subdeb->size = 0;
3269 continue;
3272 if (! _bfd_coff_get_external_symbols (sub))
3273 goto error_return;
3275 symcount = obj_raw_syment_count (sub);
3276 debug_index = bfd_zalloc (sub, symcount * sizeof (unsigned long));
3277 if (debug_index == NULL)
3278 goto error_return;
3279 xcoff_data (sub)->debug_indices = debug_index;
3281 /* Grab the contents of the .debug section. We use malloc and
3282 copy the names into the debug stringtab, rather than
3283 bfd_alloc, because I expect that, when linking many files
3284 together, many of the strings will be the same. Storing the
3285 strings in the hash table should save space in this case. */
3286 if (! bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3287 goto error_return;
3289 csectpp = xcoff_data (sub)->csects;
3291 /* Dynamic object do not have csectpp's. */
3292 if (NULL != csectpp)
3294 symesz = bfd_coff_symesz (sub);
3295 esym = (bfd_byte *) obj_coff_external_syms (sub);
3296 esymend = esym + symcount * symesz;
3298 while (esym < esymend)
3300 struct internal_syment sym;
3302 bfd_coff_swap_sym_in (sub, (void *) esym, (void *) &sym);
3304 *debug_index = (unsigned long) -1;
3306 if (sym._n._n_n._n_zeroes == 0
3307 && *csectpp != NULL
3308 && (! gc
3309 || ((*csectpp)->flags & SEC_MARK) != 0
3310 || *csectpp == bfd_abs_section_ptr)
3311 && bfd_coff_symname_in_debug (sub, &sym))
3313 char *name;
3314 bfd_size_type indx;
3316 name = (char *) debug_contents + sym._n._n_n._n_offset;
3317 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3318 if (indx == (bfd_size_type) -1)
3319 goto error_return;
3320 *debug_index = indx;
3323 esym += (sym.n_numaux + 1) * symesz;
3324 csectpp += sym.n_numaux + 1;
3325 debug_index += sym.n_numaux + 1;
3329 free (debug_contents);
3330 debug_contents = NULL;
3332 /* Clear the size of subdeb, so that it is not included directly
3333 in the output file. */
3334 subdeb->size = 0;
3336 if (! info->keep_memory)
3338 if (! _bfd_coff_free_symbols (sub))
3339 goto error_return;
3343 if (info->strip != strip_all)
3344 xcoff_hash_table (info)->debug_section->size =
3345 _bfd_stringtab_size (debug_strtab);
3347 return TRUE;
3349 error_return:
3350 if (ldinfo.strings != NULL)
3351 free (ldinfo.strings);
3352 if (debug_contents != NULL)
3353 free (debug_contents);
3354 return FALSE;
3357 bfd_boolean
3358 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3359 const char *init,
3360 const char *fini,
3361 bfd_boolean rtld)
3363 struct bfd_in_memory *bim;
3365 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3366 if (bim == NULL)
3367 return FALSE;
3369 bim->size = 0;
3370 bim->buffer = 0;
3372 abfd->link_next = 0;
3373 abfd->format = bfd_object;
3374 abfd->iostream = (void *) bim;
3375 abfd->flags = BFD_IN_MEMORY;
3376 abfd->direction = write_direction;
3377 abfd->where = 0;
3379 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3380 return FALSE;
3382 /* need to reset to unknown or it will not be read back in correctly */
3383 abfd->format = bfd_unknown;
3384 abfd->direction = read_direction;
3385 abfd->where = 0;
3387 return TRUE;
3390 /* Link an input file into the linker output file. This function
3391 handles all the sections and relocations of the input file at once. */
3393 static bfd_boolean
3394 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3395 bfd *input_bfd)
3397 bfd *output_bfd;
3398 const char *strings;
3399 bfd_size_type syment_base;
3400 unsigned int n_tmask;
3401 unsigned int n_btshft;
3402 bfd_boolean copy, hash;
3403 bfd_size_type isymesz;
3404 bfd_size_type osymesz;
3405 bfd_size_type linesz;
3406 bfd_byte *esym;
3407 bfd_byte *esym_end;
3408 struct xcoff_link_hash_entry **sym_hash;
3409 struct internal_syment *isymp;
3410 asection **csectpp;
3411 unsigned long *debug_index;
3412 long *indexp;
3413 unsigned long output_index;
3414 bfd_byte *outsym;
3415 unsigned int incls;
3416 asection *oline;
3417 bfd_boolean keep_syms;
3418 asection *o;
3420 /* We can just skip DYNAMIC files, unless this is a static link. */
3421 if ((input_bfd->flags & DYNAMIC) != 0
3422 && ! finfo->info->static_link)
3423 return TRUE;
3425 /* Move all the symbols to the output file. */
3426 output_bfd = finfo->output_bfd;
3427 strings = NULL;
3428 syment_base = obj_raw_syment_count (output_bfd);
3429 isymesz = bfd_coff_symesz (input_bfd);
3430 osymesz = bfd_coff_symesz (output_bfd);
3431 linesz = bfd_coff_linesz (input_bfd);
3432 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3434 n_tmask = coff_data (input_bfd)->local_n_tmask;
3435 n_btshft = coff_data (input_bfd)->local_n_btshft;
3437 /* Define macros so that ISFCN, et. al., macros work correctly. */
3438 #define N_TMASK n_tmask
3439 #define N_BTSHFT n_btshft
3441 copy = FALSE;
3442 if (! finfo->info->keep_memory)
3443 copy = TRUE;
3444 hash = TRUE;
3445 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3446 hash = FALSE;
3448 if (! _bfd_coff_get_external_symbols (input_bfd))
3449 return FALSE;
3451 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3452 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3453 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3454 csectpp = xcoff_data (input_bfd)->csects;
3455 debug_index = xcoff_data (input_bfd)->debug_indices;
3456 isymp = finfo->internal_syms;
3457 indexp = finfo->sym_indices;
3458 output_index = syment_base;
3459 outsym = finfo->outsyms;
3460 incls = 0;
3461 oline = NULL;
3463 while (esym < esym_end)
3465 struct internal_syment isym;
3466 union internal_auxent aux;
3467 int smtyp = 0;
3468 bfd_boolean skip;
3469 bfd_boolean require;
3470 int add;
3472 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3474 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3475 information. */
3476 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3478 BFD_ASSERT (isymp->n_numaux > 0);
3479 bfd_coff_swap_aux_in (input_bfd,
3480 (void *) (esym + isymesz * isymp->n_numaux),
3481 isymp->n_type, isymp->n_sclass,
3482 isymp->n_numaux - 1, isymp->n_numaux,
3483 (void *) &aux);
3485 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3488 /* Make a copy of *isymp so that the relocate_section function
3489 always sees the original values. This is more reliable than
3490 always recomputing the symbol value even if we are stripping
3491 the symbol. */
3492 isym = *isymp;
3494 /* If this symbol is in the .loader section, swap out the
3495 .loader symbol information. If this is an external symbol
3496 reference to a defined symbol, though, then wait until we get
3497 to the definition. */
3498 if (isym.n_sclass == C_EXT
3499 && *sym_hash != NULL
3500 && (*sym_hash)->ldsym != NULL
3501 && (smtyp != XTY_ER
3502 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3504 struct xcoff_link_hash_entry *h;
3505 struct internal_ldsym *ldsym;
3507 h = *sym_hash;
3508 ldsym = h->ldsym;
3509 if (isym.n_scnum > 0)
3511 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3512 ldsym->l_value = (isym.n_value
3513 + (*csectpp)->output_section->vma
3514 + (*csectpp)->output_offset
3515 - (*csectpp)->vma);
3517 else
3519 ldsym->l_scnum = isym.n_scnum;
3520 ldsym->l_value = isym.n_value;
3523 ldsym->l_smtype = smtyp;
3524 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3525 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3526 || (h->flags & XCOFF_IMPORT) != 0)
3527 ldsym->l_smtype |= L_IMPORT;
3528 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3529 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3530 || (h->flags & XCOFF_EXPORT) != 0)
3531 ldsym->l_smtype |= L_EXPORT;
3532 if ((h->flags & XCOFF_ENTRY) != 0)
3533 ldsym->l_smtype |= L_ENTRY;
3535 ldsym->l_smclas = aux.x_csect.x_smclas;
3537 if (ldsym->l_ifile == (bfd_size_type) -1)
3538 ldsym->l_ifile = 0;
3539 else if (ldsym->l_ifile == 0)
3541 if ((ldsym->l_smtype & L_IMPORT) == 0)
3542 ldsym->l_ifile = 0;
3543 else
3545 bfd *impbfd;
3547 if (h->root.type == bfd_link_hash_defined
3548 || h->root.type == bfd_link_hash_defweak)
3549 impbfd = h->root.u.def.section->owner;
3550 else if (h->root.type == bfd_link_hash_undefined
3551 || h->root.type == bfd_link_hash_undefweak)
3552 impbfd = h->root.u.undef.abfd;
3553 else
3554 impbfd = NULL;
3556 if (impbfd == NULL)
3557 ldsym->l_ifile = 0;
3558 else
3560 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3561 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3566 ldsym->l_parm = 0;
3568 BFD_ASSERT (h->ldindx >= 0);
3569 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3570 (finfo->ldsym
3571 + ((h->ldindx - 3)
3572 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3573 h->ldsym = NULL;
3575 /* Fill in snentry now that we know the target_index. */
3576 if ((h->flags & XCOFF_ENTRY) != 0
3577 && (h->root.type == bfd_link_hash_defined
3578 || h->root.type == bfd_link_hash_defweak))
3580 xcoff_data (output_bfd)->snentry =
3581 h->root.u.def.section->output_section->target_index;
3585 *indexp = -1;
3587 skip = FALSE;
3588 require = FALSE;
3589 add = 1 + isym.n_numaux;
3591 /* If we are skipping this csect, we want to skip this symbol. */
3592 if (*csectpp == NULL)
3593 skip = TRUE;
3595 /* If we garbage collected this csect, we want to skip this
3596 symbol. */
3597 if (! skip
3598 && xcoff_hash_table (finfo->info)->gc
3599 && ((*csectpp)->flags & SEC_MARK) == 0
3600 && *csectpp != bfd_abs_section_ptr)
3601 skip = TRUE;
3603 /* An XCOFF linker always skips C_STAT symbols. */
3604 if (! skip
3605 && isymp->n_sclass == C_STAT)
3606 skip = TRUE;
3608 /* We skip all but the first TOC anchor. */
3609 if (! skip
3610 && isymp->n_sclass == C_HIDEXT
3611 && aux.x_csect.x_smclas == XMC_TC0)
3613 if (finfo->toc_symindx != -1)
3614 skip = TRUE;
3615 else
3617 bfd_vma tocval, tocend;
3618 bfd *inp;
3620 tocval = ((*csectpp)->output_section->vma
3621 + (*csectpp)->output_offset
3622 + isym.n_value
3623 - (*csectpp)->vma);
3625 /* We want to find out if tocval is a good value to use
3626 as the TOC anchor--that is, whether we can access all
3627 of the TOC using a 16 bit offset from tocval. This
3628 test assumes that the TOC comes at the end of the
3629 output section, as it does in the default linker
3630 script. */
3631 tocend = ((*csectpp)->output_section->vma
3632 + (*csectpp)->output_section->size);
3633 for (inp = finfo->info->input_bfds;
3634 inp != NULL;
3635 inp = inp->link_next)
3638 for (o = inp->sections; o != NULL; o = o->next)
3639 if (strcmp (o->name, ".tocbss") == 0)
3641 bfd_vma new_toc_end;
3642 new_toc_end = (o->output_section->vma
3643 + o->output_offset
3644 + o->size);
3645 if (new_toc_end > tocend)
3646 tocend = new_toc_end;
3651 if (tocval + 0x10000 < tocend)
3653 (*_bfd_error_handler)
3654 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
3655 (unsigned long) (tocend - tocval));
3656 bfd_set_error (bfd_error_file_too_big);
3657 return FALSE;
3660 if (tocval + 0x8000 < tocend)
3662 bfd_vma tocadd;
3664 tocadd = tocend - (tocval + 0x8000);
3665 tocval += tocadd;
3666 isym.n_value += tocadd;
3669 finfo->toc_symindx = output_index;
3670 xcoff_data (finfo->output_bfd)->toc = tocval;
3671 xcoff_data (finfo->output_bfd)->sntoc =
3672 (*csectpp)->output_section->target_index;
3673 require = TRUE;
3678 /* If we are stripping all symbols, we want to skip this one. */
3679 if (! skip
3680 && finfo->info->strip == strip_all)
3681 skip = TRUE;
3683 /* We can skip resolved external references. */
3684 if (! skip
3685 && isym.n_sclass == C_EXT
3686 && smtyp == XTY_ER
3687 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3688 skip = TRUE;
3690 /* We can skip common symbols if they got defined somewhere
3691 else. */
3692 if (! skip
3693 && isym.n_sclass == C_EXT
3694 && smtyp == XTY_CM
3695 && ((*sym_hash)->root.type != bfd_link_hash_common
3696 || (*sym_hash)->root.u.c.p->section != *csectpp)
3697 && ((*sym_hash)->root.type != bfd_link_hash_defined
3698 || (*sym_hash)->root.u.def.section != *csectpp))
3699 skip = TRUE;
3701 /* Skip local symbols if we are discarding them. */
3702 if (! skip
3703 && finfo->info->discard == discard_all
3704 && isym.n_sclass != C_EXT
3705 && (isym.n_sclass != C_HIDEXT
3706 || smtyp != XTY_SD))
3707 skip = TRUE;
3709 /* If we stripping debugging symbols, and this is a debugging
3710 symbol, then skip it. */
3711 if (! skip
3712 && finfo->info->strip == strip_debugger
3713 && isym.n_scnum == N_DEBUG)
3714 skip = TRUE;
3716 /* If some symbols are stripped based on the name, work out the
3717 name and decide whether to skip this symbol. We don't handle
3718 this correctly for symbols whose names are in the .debug
3719 section; to get it right we would need a new bfd_strtab_hash
3720 function to return the string given the index. */
3721 if (! skip
3722 && (finfo->info->strip == strip_some
3723 || finfo->info->discard == discard_l)
3724 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3726 const char *name;
3727 char buf[SYMNMLEN + 1];
3729 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3731 if (name == NULL)
3732 return FALSE;
3734 if ((finfo->info->strip == strip_some
3735 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
3736 FALSE) == NULL))
3737 || (finfo->info->discard == discard_l
3738 && (isym.n_sclass != C_EXT
3739 && (isym.n_sclass != C_HIDEXT
3740 || smtyp != XTY_SD))
3741 && bfd_is_local_label_name (input_bfd, name)))
3742 skip = TRUE;
3745 /* We can not skip the first TOC anchor. */
3746 if (skip
3747 && require
3748 && finfo->info->strip != strip_all)
3749 skip = FALSE;
3751 /* We now know whether we are to skip this symbol or not. */
3752 if (! skip)
3754 /* Adjust the symbol in order to output it. */
3756 if (isym._n._n_n._n_zeroes == 0
3757 && isym._n._n_n._n_offset != 0)
3759 /* This symbol has a long name. Enter it in the string
3760 table we are building. If *debug_index != -1, the
3761 name has already been entered in the .debug section. */
3762 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3763 isym._n._n_n._n_offset = *debug_index;
3764 else
3766 const char *name;
3767 bfd_size_type indx;
3769 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
3771 if (name == NULL)
3772 return FALSE;
3773 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3774 if (indx == (bfd_size_type) -1)
3775 return FALSE;
3776 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3780 if (isym.n_sclass != C_BSTAT
3781 && isym.n_sclass != C_ESTAT
3782 && isym.n_sclass != C_DECL
3783 && isym.n_scnum > 0)
3785 isym.n_scnum = (*csectpp)->output_section->target_index;
3786 isym.n_value += ((*csectpp)->output_section->vma
3787 + (*csectpp)->output_offset
3788 - (*csectpp)->vma);
3791 /* The value of a C_FILE symbol is the symbol index of the
3792 next C_FILE symbol. The value of the last C_FILE symbol
3793 is -1. We try to get this right, below, just before we
3794 write the symbols out, but in the general case we may
3795 have to write the symbol out twice. */
3796 if (isym.n_sclass == C_FILE)
3798 if (finfo->last_file_index != -1
3799 && finfo->last_file.n_value != (bfd_vma) output_index)
3801 /* We must correct the value of the last C_FILE entry. */
3802 finfo->last_file.n_value = output_index;
3803 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3805 /* The last C_FILE symbol is in this input file. */
3806 bfd_coff_swap_sym_out (output_bfd,
3807 (void *) &finfo->last_file,
3808 (void *) (finfo->outsyms
3809 + ((finfo->last_file_index
3810 - syment_base)
3811 * osymesz)));
3813 else
3815 /* We have already written out the last C_FILE
3816 symbol. We need to write it out again. We
3817 borrow *outsym temporarily. */
3818 file_ptr pos;
3820 bfd_coff_swap_sym_out (output_bfd,
3821 (void *) &finfo->last_file,
3822 (void *) outsym);
3824 pos = obj_sym_filepos (output_bfd);
3825 pos += finfo->last_file_index * osymesz;
3826 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3827 || (bfd_bwrite (outsym, osymesz, output_bfd)
3828 != osymesz))
3829 return FALSE;
3833 finfo->last_file_index = output_index;
3834 finfo->last_file = isym;
3837 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3838 into the line numbers. We update the symbol values when
3839 we handle the line numbers. */
3840 if (isym.n_sclass == C_BINCL
3841 || isym.n_sclass == C_EINCL)
3843 isym.n_value = finfo->line_filepos;
3844 ++incls;
3847 /* Output the symbol. */
3849 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
3851 *indexp = output_index;
3853 if (isym.n_sclass == C_EXT)
3855 long indx;
3856 struct xcoff_link_hash_entry *h;
3858 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3859 / isymesz);
3860 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3861 BFD_ASSERT (h != NULL);
3862 h->indx = output_index;
3865 /* If this is a symbol in the TOC which we may have merged
3866 (class XMC_TC), remember the symbol index of the TOC
3867 symbol. */
3868 if (isym.n_sclass == C_HIDEXT
3869 && aux.x_csect.x_smclas == XMC_TC
3870 && *sym_hash != NULL)
3872 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3873 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3874 (*sym_hash)->u.toc_indx = output_index;
3877 output_index += add;
3878 outsym += add * osymesz;
3881 esym += add * isymesz;
3882 isymp += add;
3883 csectpp += add;
3884 sym_hash += add;
3885 if (debug_index != NULL)
3886 debug_index += add;
3887 ++indexp;
3888 for (--add; add > 0; --add)
3889 *indexp++ = -1;
3892 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3893 done in a separate pass, because we don't know the correct symbol
3894 indices until we have already decided which symbols we are going
3895 to keep. */
3897 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3898 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3899 isymp = finfo->internal_syms;
3900 indexp = finfo->sym_indices;
3901 csectpp = xcoff_data (input_bfd)->csects;
3902 outsym = finfo->outsyms;
3903 while (esym < esym_end)
3905 int add;
3907 add = 1 + isymp->n_numaux;
3909 if (*indexp < 0)
3910 esym += add * isymesz;
3911 else
3913 int i;
3915 if (isymp->n_sclass == C_BSTAT)
3917 struct internal_syment isym;
3919 bfd_vma indx;
3921 /* The value of a C_BSTAT symbol is the symbol table
3922 index of the containing csect. */
3923 bfd_coff_swap_sym_in (output_bfd, (void *) outsym, (void *) &isym);
3924 indx = isym.n_value;
3925 if (indx < obj_raw_syment_count (input_bfd))
3927 long symindx;
3929 symindx = finfo->sym_indices[indx];
3930 if (symindx < 0)
3931 isym.n_value = 0;
3932 else
3933 isym.n_value = symindx;
3934 bfd_coff_swap_sym_out (output_bfd, (void *) &isym,
3935 (void *) outsym);
3939 esym += isymesz;
3940 outsym += osymesz;
3942 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3944 union internal_auxent aux;
3946 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
3947 isymp->n_sclass, i, isymp->n_numaux,
3948 (void *) &aux);
3950 if (isymp->n_sclass == C_FILE)
3952 /* This is the file name (or some comment put in by
3953 the compiler). If it is long, we must put it in
3954 the string table. */
3955 if (aux.x_file.x_n.x_zeroes == 0
3956 && aux.x_file.x_n.x_offset != 0)
3958 const char *filename;
3959 bfd_size_type indx;
3961 BFD_ASSERT (aux.x_file.x_n.x_offset
3962 >= STRING_SIZE_SIZE);
3963 if (strings == NULL)
3965 strings = _bfd_coff_read_string_table (input_bfd);
3966 if (strings == NULL)
3967 return FALSE;
3969 filename = strings + aux.x_file.x_n.x_offset;
3970 indx = _bfd_stringtab_add (finfo->strtab, filename,
3971 hash, copy);
3972 if (indx == (bfd_size_type) -1)
3973 return FALSE;
3974 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3977 else if ((isymp->n_sclass == C_EXT
3978 || isymp->n_sclass == C_HIDEXT)
3979 && i + 1 == isymp->n_numaux)
3982 /* We don't support type checking. I don't know if
3983 anybody does. */
3984 aux.x_csect.x_parmhash = 0;
3985 /* I don't think anybody uses these fields, but we'd
3986 better clobber them just in case. */
3987 aux.x_csect.x_stab = 0;
3988 aux.x_csect.x_snstab = 0;
3990 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
3992 unsigned long indx;
3994 indx = aux.x_csect.x_scnlen.l;
3995 if (indx < obj_raw_syment_count (input_bfd))
3997 long symindx;
3999 symindx = finfo->sym_indices[indx];
4000 if (symindx < 0)
4002 aux.x_csect.x_scnlen.l = 0;
4004 else
4006 aux.x_csect.x_scnlen.l = symindx;
4011 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4013 unsigned long indx;
4015 if (ISFCN (isymp->n_type)
4016 || ISTAG (isymp->n_sclass)
4017 || isymp->n_sclass == C_BLOCK
4018 || isymp->n_sclass == C_FCN)
4020 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4021 if (indx > 0
4022 && indx < obj_raw_syment_count (input_bfd))
4024 /* We look forward through the symbol for
4025 the index of the next symbol we are going
4026 to include. I don't know if this is
4027 entirely right. */
4028 while (finfo->sym_indices[indx] < 0
4029 && indx < obj_raw_syment_count (input_bfd))
4030 ++indx;
4031 if (indx >= obj_raw_syment_count (input_bfd))
4032 indx = output_index;
4033 else
4034 indx = finfo->sym_indices[indx];
4035 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4040 indx = aux.x_sym.x_tagndx.l;
4041 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4043 long symindx;
4045 symindx = finfo->sym_indices[indx];
4046 if (symindx < 0)
4047 aux.x_sym.x_tagndx.l = 0;
4048 else
4049 aux.x_sym.x_tagndx.l = symindx;
4054 /* Copy over the line numbers, unless we are stripping
4055 them. We do this on a symbol by symbol basis in
4056 order to more easily handle garbage collection. */
4057 if ((isymp->n_sclass == C_EXT
4058 || isymp->n_sclass == C_HIDEXT)
4059 && i == 0
4060 && isymp->n_numaux > 1
4061 && ISFCN (isymp->n_type)
4062 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4064 if (finfo->info->strip != strip_none
4065 && finfo->info->strip != strip_some)
4066 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4067 else
4069 asection *enclosing;
4070 unsigned int enc_count;
4071 bfd_signed_vma linoff;
4072 struct internal_lineno lin;
4074 o = *csectpp;
4075 enclosing = xcoff_section_data (abfd, o)->enclosing;
4076 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4077 if (oline != enclosing)
4079 file_ptr pos = enclosing->line_filepos;
4080 bfd_size_type amt = linesz * enc_count;
4081 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4082 || (bfd_bread (finfo->linenos, amt, input_bfd)
4083 != amt))
4084 return FALSE;
4085 oline = enclosing;
4088 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4089 - enclosing->line_filepos);
4091 bfd_coff_swap_lineno_in (input_bfd,
4092 (void *) (finfo->linenos + linoff),
4093 (void *) &lin);
4094 if (lin.l_lnno != 0
4095 || ((bfd_size_type) lin.l_addr.l_symndx
4096 != ((esym
4097 - isymesz
4098 - ((bfd_byte *)
4099 obj_coff_external_syms (input_bfd)))
4100 / isymesz)))
4101 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4102 else
4104 bfd_byte *linpend, *linp;
4105 bfd_vma offset;
4106 bfd_size_type count;
4108 lin.l_addr.l_symndx = *indexp;
4109 bfd_coff_swap_lineno_out (output_bfd, (void *) &lin,
4110 (void *) (finfo->linenos
4111 + linoff));
4113 linpend = (finfo->linenos
4114 + enc_count * linesz);
4115 offset = (o->output_section->vma
4116 + o->output_offset
4117 - o->vma);
4118 for (linp = finfo->linenos + linoff + linesz;
4119 linp < linpend;
4120 linp += linesz)
4122 bfd_coff_swap_lineno_in (input_bfd, (void *) linp,
4123 (void *) &lin);
4124 if (lin.l_lnno == 0)
4125 break;
4126 lin.l_addr.l_paddr += offset;
4127 bfd_coff_swap_lineno_out (output_bfd,
4128 (void *) &lin,
4129 (void *) linp);
4132 count = (linp - (finfo->linenos + linoff)) / linesz;
4134 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4135 (o->output_section->line_filepos
4136 + o->output_section->lineno_count * linesz);
4138 if (bfd_seek (output_bfd,
4139 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4140 SEEK_SET) != 0
4141 || (bfd_bwrite (finfo->linenos + linoff,
4142 linesz * count, output_bfd)
4143 != linesz * count))
4144 return FALSE;
4146 o->output_section->lineno_count += count;
4148 if (incls > 0)
4150 struct internal_syment *iisp, *iispend;
4151 long *iindp;
4152 bfd_byte *oos;
4153 int iiadd;
4155 /* Update any C_BINCL or C_EINCL symbols
4156 that refer to a line number in the
4157 range we just output. */
4158 iisp = finfo->internal_syms;
4159 iispend = (iisp
4160 + obj_raw_syment_count (input_bfd));
4161 iindp = finfo->sym_indices;
4162 oos = finfo->outsyms;
4163 while (iisp < iispend)
4165 if (*iindp >= 0
4166 && (iisp->n_sclass == C_BINCL
4167 || iisp->n_sclass == C_EINCL)
4168 && ((bfd_size_type) iisp->n_value
4169 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4170 && ((bfd_size_type) iisp->n_value
4171 < (enclosing->line_filepos
4172 + enc_count * linesz)))
4174 struct internal_syment iis;
4176 bfd_coff_swap_sym_in (output_bfd,
4177 (void *) oos,
4178 (void *) &iis);
4179 iis.n_value =
4180 (iisp->n_value
4181 - enclosing->line_filepos
4182 - linoff
4183 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4184 bfd_coff_swap_sym_out (output_bfd,
4185 (void *) &iis,
4186 (void *) oos);
4187 --incls;
4190 iiadd = 1 + iisp->n_numaux;
4191 if (*iindp >= 0)
4192 oos += iiadd * osymesz;
4193 iisp += iiadd;
4194 iindp += iiadd;
4201 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4202 isymp->n_sclass, i, isymp->n_numaux,
4203 (void *) outsym);
4204 outsym += osymesz;
4205 esym += isymesz;
4209 indexp += add;
4210 isymp += add;
4211 csectpp += add;
4214 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4215 symbol will be the first symbol in the next input file. In the
4216 normal case, this will save us from writing out the C_FILE symbol
4217 again. */
4218 if (finfo->last_file_index != -1
4219 && (bfd_size_type) finfo->last_file_index >= syment_base)
4221 finfo->last_file.n_value = output_index;
4222 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4223 (void *) (finfo->outsyms
4224 + ((finfo->last_file_index - syment_base)
4225 * osymesz)));
4228 /* Write the modified symbols to the output file. */
4229 if (outsym > finfo->outsyms)
4231 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4232 bfd_size_type amt = outsym - finfo->outsyms;
4233 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4234 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4235 return FALSE;
4237 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4238 + (outsym - finfo->outsyms) / osymesz)
4239 == output_index);
4241 obj_raw_syment_count (output_bfd) = output_index;
4244 /* Don't let the linker relocation routines discard the symbols. */
4245 keep_syms = obj_coff_keep_syms (input_bfd);
4246 obj_coff_keep_syms (input_bfd) = TRUE;
4248 /* Relocate the contents of each section. */
4249 for (o = input_bfd->sections; o != NULL; o = o->next)
4251 bfd_byte *contents;
4253 if (! o->linker_mark)
4254 /* This section was omitted from the link. */
4255 continue;
4257 if ((o->flags & SEC_HAS_CONTENTS) == 0
4258 || o->size == 0
4259 || (o->flags & SEC_IN_MEMORY) != 0)
4260 continue;
4262 /* We have set filepos correctly for the sections we created to
4263 represent csects, so bfd_get_section_contents should work. */
4264 if (coff_section_data (input_bfd, o) != NULL
4265 && coff_section_data (input_bfd, o)->contents != NULL)
4266 contents = coff_section_data (input_bfd, o)->contents;
4267 else
4269 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4270 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4271 return FALSE;
4272 contents = finfo->contents;
4275 if ((o->flags & SEC_RELOC) != 0)
4277 int target_index;
4278 struct internal_reloc *internal_relocs;
4279 struct internal_reloc *irel;
4280 bfd_vma offset;
4281 struct internal_reloc *irelend;
4282 struct xcoff_link_hash_entry **rel_hash;
4283 long r_symndx;
4285 /* Read in the relocs. */
4286 target_index = o->output_section->target_index;
4287 internal_relocs = (xcoff_read_internal_relocs
4288 (input_bfd, o, FALSE, finfo->external_relocs,
4289 TRUE,
4290 (finfo->section_info[target_index].relocs
4291 + o->output_section->reloc_count)));
4292 if (internal_relocs == NULL)
4293 return FALSE;
4295 /* Call processor specific code to relocate the section
4296 contents. */
4297 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4298 input_bfd, o,
4299 contents,
4300 internal_relocs,
4301 finfo->internal_syms,
4302 xcoff_data (input_bfd)->csects))
4303 return FALSE;
4305 offset = o->output_section->vma + o->output_offset - o->vma;
4306 irel = internal_relocs;
4307 irelend = irel + o->reloc_count;
4308 rel_hash = (finfo->section_info[target_index].rel_hashes
4309 + o->output_section->reloc_count);
4310 for (; irel < irelend; irel++, rel_hash++)
4312 struct xcoff_link_hash_entry *h = NULL;
4313 struct internal_ldrel ldrel;
4314 bfd_boolean quiet;
4316 *rel_hash = NULL;
4318 /* Adjust the reloc address and symbol index. */
4320 irel->r_vaddr += offset;
4322 r_symndx = irel->r_symndx;
4324 if (r_symndx == -1)
4325 h = NULL;
4326 else
4327 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4329 if (r_symndx != -1 && finfo->info->strip != strip_all)
4331 if (h != NULL
4332 && h->smclas != XMC_TD
4333 && (irel->r_type == R_TOC
4334 || irel->r_type == R_GL
4335 || irel->r_type == R_TCL
4336 || irel->r_type == R_TRL
4337 || irel->r_type == R_TRLA))
4339 /* This is a TOC relative reloc with a symbol
4340 attached. The symbol should be the one which
4341 this reloc is for. We want to make this
4342 reloc against the TOC address of the symbol,
4343 not the symbol itself. */
4344 BFD_ASSERT (h->toc_section != NULL);
4345 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4346 if (h->u.toc_indx != -1)
4347 irel->r_symndx = h->u.toc_indx;
4348 else
4350 struct xcoff_toc_rel_hash *n;
4351 struct xcoff_link_section_info *si;
4352 bfd_size_type amt;
4354 amt = sizeof (* n);
4355 n = bfd_alloc (finfo->output_bfd, amt);
4356 if (n == NULL)
4357 return FALSE;
4358 si = finfo->section_info + target_index;
4359 n->next = si->toc_rel_hashes;
4360 n->h = h;
4361 n->rel = irel;
4362 si->toc_rel_hashes = n;
4365 else if (h != NULL)
4367 /* This is a global symbol. */
4368 if (h->indx >= 0)
4369 irel->r_symndx = h->indx;
4370 else
4372 /* This symbol is being written at the end
4373 of the file, and we do not yet know the
4374 symbol index. We save the pointer to the
4375 hash table entry in the rel_hash list.
4376 We set the indx field to -2 to indicate
4377 that this symbol must not be stripped. */
4378 *rel_hash = h;
4379 h->indx = -2;
4382 else
4384 long indx;
4386 indx = finfo->sym_indices[r_symndx];
4388 if (indx == -1)
4390 struct internal_syment *is;
4392 /* Relocations against a TC0 TOC anchor are
4393 automatically transformed to be against
4394 the TOC anchor in the output file. */
4395 is = finfo->internal_syms + r_symndx;
4396 if (is->n_sclass == C_HIDEXT
4397 && is->n_numaux > 0)
4399 void * auxptr;
4400 union internal_auxent aux;
4402 auxptr = ((void *)
4403 (((bfd_byte *)
4404 obj_coff_external_syms (input_bfd))
4405 + ((r_symndx + is->n_numaux)
4406 * isymesz)));
4407 bfd_coff_swap_aux_in (input_bfd, auxptr,
4408 is->n_type, is->n_sclass,
4409 is->n_numaux - 1,
4410 is->n_numaux,
4411 (void *) &aux);
4412 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4413 && aux.x_csect.x_smclas == XMC_TC0)
4414 indx = finfo->toc_symindx;
4418 if (indx != -1)
4419 irel->r_symndx = indx;
4420 else
4423 struct internal_syment *is;
4425 const char *name;
4426 char buf[SYMNMLEN + 1];
4428 /* This reloc is against a symbol we are
4429 stripping. It would be possible to handle
4430 this case, but I don't think it's worth it. */
4431 is = finfo->internal_syms + r_symndx;
4433 name = (_bfd_coff_internal_syment_name
4434 (input_bfd, is, buf));
4436 if (name == NULL)
4437 return FALSE;
4439 if (! ((*finfo->info->callbacks->unattached_reloc)
4440 (finfo->info, name, input_bfd, o,
4441 irel->r_vaddr)))
4442 return FALSE;
4447 quiet = FALSE;
4448 switch (irel->r_type)
4450 default:
4451 if (h == NULL
4452 || h->root.type == bfd_link_hash_defined
4453 || h->root.type == bfd_link_hash_defweak
4454 || h->root.type == bfd_link_hash_common)
4455 break;
4456 /* Fall through. */
4457 case R_POS:
4458 case R_NEG:
4459 case R_RL:
4460 case R_RLA:
4461 /* This reloc needs to be copied into the .loader
4462 section. */
4463 ldrel.l_vaddr = irel->r_vaddr;
4464 if (r_symndx == -1)
4465 ldrel.l_symndx = -(bfd_size_type ) 1;
4466 else if (h == NULL
4467 || (h->root.type == bfd_link_hash_defined
4468 || h->root.type == bfd_link_hash_defweak
4469 || h->root.type == bfd_link_hash_common))
4471 asection *sec;
4473 if (h == NULL)
4474 sec = xcoff_data (input_bfd)->csects[r_symndx];
4475 else if (h->root.type == bfd_link_hash_common)
4476 sec = h->root.u.c.p->section;
4477 else
4478 sec = h->root.u.def.section;
4479 sec = sec->output_section;
4481 if (strcmp (sec->name, ".text") == 0)
4482 ldrel.l_symndx = 0;
4483 else if (strcmp (sec->name, ".data") == 0)
4484 ldrel.l_symndx = 1;
4485 else if (strcmp (sec->name, ".bss") == 0)
4486 ldrel.l_symndx = 2;
4487 else
4489 (*_bfd_error_handler)
4490 (_("%B: loader reloc in unrecognized section `%A'"),
4491 input_bfd, sec);
4492 bfd_set_error (bfd_error_nonrepresentable_section);
4493 return FALSE;
4496 else
4498 if (! finfo->info->relocatable
4499 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
4500 && (h->flags & XCOFF_IMPORT) == 0)
4502 /* We already called the undefined_symbol
4503 callback for this relocation, in
4504 _bfd_ppc_xcoff_relocate_section. Don't
4505 issue any more warnings. */
4506 quiet = TRUE;
4508 if (h->ldindx < 0 && ! quiet)
4510 (*_bfd_error_handler)
4511 (_("%B: `%s' in loader reloc but not loader sym"),
4512 input_bfd,
4513 h->root.root.string);
4514 bfd_set_error (bfd_error_bad_value);
4515 return FALSE;
4517 ldrel.l_symndx = h->ldindx;
4519 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4520 ldrel.l_rsecnm = o->output_section->target_index;
4521 if (xcoff_hash_table (finfo->info)->textro
4522 && strcmp (o->output_section->name, ".text") == 0
4523 && ! quiet)
4525 (*_bfd_error_handler)
4526 (_("%B: loader reloc in read-only section %A"),
4527 input_bfd, o->output_section);
4528 bfd_set_error (bfd_error_invalid_operation);
4529 return FALSE;
4531 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4532 finfo->ldrel);
4534 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4535 break;
4537 case R_TOC:
4538 case R_GL:
4539 case R_TCL:
4540 case R_TRL:
4541 case R_TRLA:
4542 /* We should never need a .loader reloc for a TOC
4543 relative reloc. */
4544 break;
4548 o->output_section->reloc_count += o->reloc_count;
4551 /* Write out the modified section contents. */
4552 if (! bfd_set_section_contents (output_bfd, o->output_section,
4553 contents, (file_ptr) o->output_offset,
4554 o->size))
4555 return FALSE;
4558 obj_coff_keep_syms (input_bfd) = keep_syms;
4560 if (! finfo->info->keep_memory)
4562 if (! _bfd_coff_free_symbols (input_bfd))
4563 return FALSE;
4566 return TRUE;
4569 #undef N_TMASK
4570 #undef N_BTSHFT
4572 /* Sort relocs by VMA. This is called via qsort. */
4574 static int
4575 xcoff_sort_relocs (const void * p1, const void * p2)
4577 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4578 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4580 if (r1->r_vaddr > r2->r_vaddr)
4581 return 1;
4582 else if (r1->r_vaddr < r2->r_vaddr)
4583 return -1;
4584 else
4585 return 0;
4588 /* Write out a non-XCOFF global symbol. */
4590 static bfd_boolean
4591 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4593 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4594 bfd *output_bfd;
4595 bfd_byte *outsym;
4596 struct internal_syment isym;
4597 union internal_auxent aux;
4598 bfd_boolean result;
4599 file_ptr pos;
4600 bfd_size_type amt;
4602 output_bfd = finfo->output_bfd;
4603 outsym = finfo->outsyms;
4605 if (h->root.type == bfd_link_hash_warning)
4607 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4608 if (h->root.type == bfd_link_hash_new)
4609 return TRUE;
4612 /* If this symbol was garbage collected, just skip it. */
4613 if (xcoff_hash_table (finfo->info)->gc
4614 && (h->flags & XCOFF_MARK) == 0)
4615 return TRUE;
4617 /* If we need a .loader section entry, write it out. */
4618 if (h->ldsym != NULL)
4620 struct internal_ldsym *ldsym;
4621 bfd *impbfd;
4623 ldsym = h->ldsym;
4625 if (h->root.type == bfd_link_hash_undefined
4626 || h->root.type == bfd_link_hash_undefweak)
4629 ldsym->l_value = 0;
4630 ldsym->l_scnum = N_UNDEF;
4631 ldsym->l_smtype = XTY_ER;
4632 impbfd = h->root.u.undef.abfd;
4635 else if (h->root.type == bfd_link_hash_defined
4636 || h->root.type == bfd_link_hash_defweak)
4638 asection *sec;
4640 sec = h->root.u.def.section;
4641 ldsym->l_value = (sec->output_section->vma
4642 + sec->output_offset
4643 + h->root.u.def.value);
4644 ldsym->l_scnum = sec->output_section->target_index;
4645 ldsym->l_smtype = XTY_SD;
4646 impbfd = sec->owner;
4649 else
4650 abort ();
4652 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4653 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4654 || (h->flags & XCOFF_IMPORT) != 0)
4655 /* Clear l_smtype
4656 Import symbols are defined so the check above will make
4657 the l_smtype XTY_SD. But this is not correct, it should
4658 be cleared. */
4659 ldsym->l_smtype |= L_IMPORT;
4661 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4662 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4663 || (h->flags & XCOFF_EXPORT) != 0)
4664 ldsym->l_smtype |= L_EXPORT;
4666 if ((h->flags & XCOFF_ENTRY) != 0)
4667 ldsym->l_smtype |= L_ENTRY;
4669 if ((h->flags & XCOFF_RTINIT) != 0)
4670 ldsym->l_smtype = XTY_SD;
4672 ldsym->l_smclas = h->smclas;
4674 if (ldsym->l_smtype & L_IMPORT)
4676 if ((h->root.type == bfd_link_hash_defined
4677 || h->root.type == bfd_link_hash_defweak)
4678 && (h->root.u.def.value != 0))
4679 ldsym->l_smclas = XMC_XO;
4681 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4682 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4683 ldsym->l_smclas = XMC_SV3264;
4685 else if (h->flags & XCOFF_SYSCALL32)
4686 ldsym->l_smclas = XMC_SV;
4688 else if (h->flags & XCOFF_SYSCALL64)
4689 ldsym->l_smclas = XMC_SV64;
4692 if (ldsym->l_ifile == -(bfd_size_type) 1)
4694 ldsym->l_ifile = 0;
4696 else if (ldsym->l_ifile == 0)
4698 if ((ldsym->l_smtype & L_IMPORT) == 0)
4699 ldsym->l_ifile = 0;
4700 else if (impbfd == NULL)
4701 ldsym->l_ifile = 0;
4702 else
4704 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4705 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4709 ldsym->l_parm = 0;
4711 BFD_ASSERT (h->ldindx >= 0);
4713 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4714 (finfo->ldsym +
4715 (h->ldindx - 3)
4716 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4717 h->ldsym = NULL;
4720 /* If this symbol needs global linkage code, write it out. */
4721 if (h->root.type == bfd_link_hash_defined
4722 && (h->root.u.def.section
4723 == xcoff_hash_table (finfo->info)->linkage_section))
4725 bfd_byte *p;
4726 bfd_vma tocoff;
4727 unsigned int i;
4729 p = h->root.u.def.section->contents + h->root.u.def.value;
4731 /* The first instruction in the global linkage code loads a
4732 specific TOC element. */
4733 tocoff = (h->descriptor->toc_section->output_section->vma
4734 + h->descriptor->toc_section->output_offset
4735 - xcoff_data (output_bfd)->toc);
4737 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4738 tocoff += h->descriptor->u.toc_offset;
4740 /* The first instruction in the glink code needs to be
4741 cooked to to hold the correct offset in the toc. The
4742 rest are just output raw. */
4743 bfd_put_32 (output_bfd,
4744 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
4746 /* Start with i == 1 to get past the first instruction done above
4747 The /4 is because the glink code is in bytes and we are going
4748 4 at a pop. */
4749 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
4750 bfd_put_32 (output_bfd,
4751 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
4752 &p[4 * i]);
4755 /* If we created a TOC entry for this symbol, write out the required
4756 relocs. */
4757 if ((h->flags & XCOFF_SET_TOC) != 0)
4759 asection *tocsec;
4760 asection *osec;
4761 int oindx;
4762 struct internal_reloc *irel;
4763 struct internal_ldrel ldrel;
4764 struct internal_syment irsym;
4765 union internal_auxent iraux;
4767 tocsec = h->toc_section;
4768 osec = tocsec->output_section;
4769 oindx = osec->target_index;
4770 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4771 irel->r_vaddr = (osec->vma
4772 + tocsec->output_offset
4773 + h->u.toc_offset);
4775 if (h->indx >= 0)
4776 irel->r_symndx = h->indx;
4777 else
4779 h->indx = -2;
4780 irel->r_symndx = obj_raw_syment_count (output_bfd);
4783 BFD_ASSERT (h->ldindx >= 0);
4785 /* Initialize the aux union here instead of closer to when it is
4786 written out below because the length of the csect depends on
4787 whether the output is 32 or 64 bit. */
4788 memset (&iraux, 0, sizeof iraux);
4789 iraux.x_csect.x_smtyp = XTY_SD;
4790 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
4791 iraux.x_csect.x_smclas = XMC_TC;
4793 /* 32 bit uses a 32 bit R_POS to do the relocations
4794 64 bit uses a 64 bit R_POS to do the relocations
4796 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4798 Which one is determined by the backend. */
4799 if (bfd_xcoff_is_xcoff64 (output_bfd))
4801 irel->r_size = 63;
4802 iraux.x_csect.x_scnlen.l = 8;
4804 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4806 irel->r_size = 31;
4807 iraux.x_csect.x_scnlen.l = 4;
4809 else
4810 return FALSE;
4812 irel->r_type = R_POS;
4813 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4814 ++osec->reloc_count;
4816 ldrel.l_vaddr = irel->r_vaddr;
4817 ldrel.l_symndx = h->ldindx;
4818 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
4819 ldrel.l_rsecnm = oindx;
4820 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4821 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4823 /* We need to emit a symbol to define a csect which holds
4824 the reloc. */
4825 if (finfo->info->strip != strip_all)
4827 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
4828 &irsym, h->root.root.string);
4829 if (!result)
4830 return FALSE;
4832 irsym.n_value = irel->r_vaddr;
4833 irsym.n_scnum = osec->target_index;
4834 irsym.n_sclass = C_HIDEXT;
4835 irsym.n_type = T_NULL;
4836 irsym.n_numaux = 1;
4838 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
4839 outsym += bfd_coff_symesz (output_bfd);
4841 /* Note : iraux is initialized above. */
4842 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
4843 0, 1, (void *) outsym);
4844 outsym += bfd_coff_auxesz (output_bfd);
4846 if (h->indx >= 0)
4848 /* We aren't going to write out the symbols below, so we
4849 need to write them out now. */
4850 pos = obj_sym_filepos (output_bfd);
4851 pos += (obj_raw_syment_count (output_bfd)
4852 * bfd_coff_symesz (output_bfd));
4853 amt = outsym - finfo->outsyms;
4854 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4855 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4856 return FALSE;
4857 obj_raw_syment_count (output_bfd) +=
4858 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4860 outsym = finfo->outsyms;
4865 /* If this symbol is a specially defined function descriptor, write
4866 it out. The first word is the address of the function code
4867 itself, the second word is the address of the TOC, and the third
4868 word is zero.
4870 32 bit vs 64 bit
4871 The addresses for the 32 bit will take 4 bytes and the addresses
4872 for 64 bit will take 8 bytes. Similar for the relocs. This type
4873 of logic was also done above to create a TOC entry in
4874 xcoff_write_global_symbol. */
4875 if ((h->flags & XCOFF_DESCRIPTOR) != 0
4876 && h->root.type == bfd_link_hash_defined
4877 && (h->root.u.def.section
4878 == xcoff_hash_table (finfo->info)->descriptor_section))
4880 asection *sec;
4881 asection *osec;
4882 int oindx;
4883 bfd_byte *p;
4884 struct xcoff_link_hash_entry *hentry;
4885 asection *esec;
4886 struct internal_reloc *irel;
4887 struct internal_ldrel ldrel;
4888 asection *tsec;
4889 unsigned int reloc_size, byte_size;
4891 if (bfd_xcoff_is_xcoff64 (output_bfd))
4893 reloc_size = 63;
4894 byte_size = 8;
4896 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4898 reloc_size = 31;
4899 byte_size = 4;
4901 else
4902 return FALSE;
4904 sec = h->root.u.def.section;
4905 osec = sec->output_section;
4906 oindx = osec->target_index;
4907 p = sec->contents + h->root.u.def.value;
4909 hentry = h->descriptor;
4910 BFD_ASSERT (hentry != NULL
4911 && (hentry->root.type == bfd_link_hash_defined
4912 || hentry->root.type == bfd_link_hash_defweak));
4913 esec = hentry->root.u.def.section;
4915 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4916 irel->r_vaddr = (osec->vma
4917 + sec->output_offset
4918 + h->root.u.def.value);
4919 irel->r_symndx = esec->output_section->target_index;
4920 irel->r_type = R_POS;
4921 irel->r_size = reloc_size;
4922 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4923 ++osec->reloc_count;
4925 ldrel.l_vaddr = irel->r_vaddr;
4926 if (strcmp (esec->output_section->name, ".text") == 0)
4927 ldrel.l_symndx = 0;
4928 else if (strcmp (esec->output_section->name, ".data") == 0)
4929 ldrel.l_symndx = 1;
4930 else if (strcmp (esec->output_section->name, ".bss") == 0)
4931 ldrel.l_symndx = 2;
4932 else
4934 (*_bfd_error_handler)
4935 (_("%s: loader reloc in unrecognized section `%s'"),
4936 bfd_get_filename (output_bfd),
4937 esec->output_section->name);
4938 bfd_set_error (bfd_error_nonrepresentable_section);
4939 return FALSE;
4941 ldrel.l_rtype = (reloc_size << 8) | R_POS;
4942 ldrel.l_rsecnm = oindx;
4943 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4944 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4946 /* There are three items to write out,
4947 the address of the code
4948 the address of the toc anchor
4949 the environment pointer.
4950 We are ignoring the environment pointer. So set it to zero. */
4951 if (bfd_xcoff_is_xcoff64 (output_bfd))
4953 bfd_put_64 (output_bfd,
4954 (esec->output_section->vma + esec->output_offset
4955 + hentry->root.u.def.value),
4957 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
4958 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
4960 else
4962 /* 32 bit backend
4963 This logic was already called above so the error case where
4964 the backend is neither has already been checked. */
4965 bfd_put_32 (output_bfd,
4966 (esec->output_section->vma + esec->output_offset
4967 + hentry->root.u.def.value),
4969 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
4970 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
4973 tsec = coff_section_from_bfd_index (output_bfd,
4974 xcoff_data (output_bfd)->sntoc);
4976 ++irel;
4977 irel->r_vaddr = (osec->vma
4978 + sec->output_offset
4979 + h->root.u.def.value
4980 + byte_size);
4981 irel->r_symndx = tsec->output_section->target_index;
4982 irel->r_type = R_POS;
4983 irel->r_size = reloc_size;
4984 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4985 ++osec->reloc_count;
4987 ldrel.l_vaddr = irel->r_vaddr;
4988 if (strcmp (tsec->output_section->name, ".text") == 0)
4989 ldrel.l_symndx = 0;
4990 else if (strcmp (tsec->output_section->name, ".data") == 0)
4991 ldrel.l_symndx = 1;
4992 else if (strcmp (tsec->output_section->name, ".bss") == 0)
4993 ldrel.l_symndx = 2;
4994 else
4996 (*_bfd_error_handler)
4997 (_("%s: loader reloc in unrecognized section `%s'"),
4998 bfd_get_filename (output_bfd),
4999 tsec->output_section->name);
5000 bfd_set_error (bfd_error_nonrepresentable_section);
5001 return FALSE;
5003 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5004 ldrel.l_rsecnm = oindx;
5005 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5006 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5009 if (h->indx >= 0 || finfo->info->strip == strip_all)
5011 BFD_ASSERT (outsym == finfo->outsyms);
5012 return TRUE;
5015 if (h->indx != -2
5016 && (finfo->info->strip == strip_all
5017 || (finfo->info->strip == strip_some
5018 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5019 FALSE, FALSE) == NULL)))
5021 BFD_ASSERT (outsym == finfo->outsyms);
5022 return TRUE;
5025 if (h->indx != -2
5026 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5028 BFD_ASSERT (outsym == finfo->outsyms);
5029 return TRUE;
5032 memset (&aux, 0, sizeof aux);
5034 h->indx = obj_raw_syment_count (output_bfd);
5036 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5037 h->root.root.string);
5038 if (!result)
5039 return FALSE;
5041 if (h->root.type == bfd_link_hash_undefined
5042 || h->root.type == bfd_link_hash_undefweak)
5044 isym.n_value = 0;
5045 isym.n_scnum = N_UNDEF;
5046 isym.n_sclass = C_EXT;
5047 aux.x_csect.x_smtyp = XTY_ER;
5049 else if ((h->root.type == bfd_link_hash_defined
5050 || h->root.type == bfd_link_hash_defweak)
5051 && h->smclas == XMC_XO)
5053 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5054 isym.n_value = h->root.u.def.value;
5055 isym.n_scnum = N_UNDEF;
5056 isym.n_sclass = C_EXT;
5057 aux.x_csect.x_smtyp = XTY_ER;
5059 else if (h->root.type == bfd_link_hash_defined
5060 || h->root.type == bfd_link_hash_defweak)
5062 struct xcoff_link_size_list *l;
5064 isym.n_value = (h->root.u.def.section->output_section->vma
5065 + h->root.u.def.section->output_offset
5066 + h->root.u.def.value);
5067 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5068 isym.n_scnum = N_ABS;
5069 else
5070 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5071 isym.n_sclass = C_HIDEXT;
5072 aux.x_csect.x_smtyp = XTY_SD;
5074 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5076 for (l = xcoff_hash_table (finfo->info)->size_list;
5077 l != NULL;
5078 l = l->next)
5080 if (l->h == h)
5082 aux.x_csect.x_scnlen.l = l->size;
5083 break;
5088 else if (h->root.type == bfd_link_hash_common)
5090 isym.n_value = (h->root.u.c.p->section->output_section->vma
5091 + h->root.u.c.p->section->output_offset);
5092 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5093 isym.n_sclass = C_EXT;
5094 aux.x_csect.x_smtyp = XTY_CM;
5095 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5097 else
5098 abort ();
5100 isym.n_type = T_NULL;
5101 isym.n_numaux = 1;
5103 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5104 outsym += bfd_coff_symesz (output_bfd);
5106 aux.x_csect.x_smclas = h->smclas;
5107 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5108 (void *) outsym);
5109 outsym += bfd_coff_auxesz (output_bfd);
5111 if ((h->root.type == bfd_link_hash_defined
5112 || h->root.type == bfd_link_hash_defweak)
5113 && h->smclas != XMC_XO)
5115 /* We just output an SD symbol. Now output an LD symbol. */
5116 h->indx += 2;
5118 isym.n_sclass = C_EXT;
5119 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5120 outsym += bfd_coff_symesz (output_bfd);
5122 aux.x_csect.x_smtyp = XTY_LD;
5123 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5124 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5125 (void *) outsym);
5126 outsym += bfd_coff_auxesz (output_bfd);
5129 pos = obj_sym_filepos (output_bfd);
5130 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5131 amt = outsym - finfo->outsyms;
5132 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5133 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5134 return FALSE;
5135 obj_raw_syment_count (output_bfd) +=
5136 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5138 return TRUE;
5141 /* Handle a link order which is supposed to generate a reloc. */
5143 static bfd_boolean
5144 xcoff_reloc_link_order (bfd *output_bfd,
5145 struct xcoff_final_link_info *finfo,
5146 asection *output_section,
5147 struct bfd_link_order *link_order)
5149 reloc_howto_type *howto;
5150 struct xcoff_link_hash_entry *h;
5151 asection *hsec;
5152 bfd_vma hval;
5153 bfd_vma addend;
5154 struct internal_reloc *irel;
5155 struct xcoff_link_hash_entry **rel_hash_ptr;
5156 struct internal_ldrel ldrel;
5158 if (link_order->type == bfd_section_reloc_link_order)
5159 /* We need to somehow locate a symbol in the right section. The
5160 symbol must either have a value of zero, or we must adjust
5161 the addend by the value of the symbol. FIXME: Write this
5162 when we need it. The old linker couldn't handle this anyhow. */
5163 abort ();
5165 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5166 if (howto == NULL)
5168 bfd_set_error (bfd_error_bad_value);
5169 return FALSE;
5172 h = ((struct xcoff_link_hash_entry *)
5173 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5174 link_order->u.reloc.p->u.name,
5175 FALSE, FALSE, TRUE));
5176 if (h == NULL)
5178 if (! ((*finfo->info->callbacks->unattached_reloc)
5179 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5180 return FALSE;
5181 return TRUE;
5184 if (h->root.type == bfd_link_hash_common)
5186 hsec = h->root.u.c.p->section;
5187 hval = 0;
5189 else if (h->root.type == bfd_link_hash_defined
5190 || h->root.type == bfd_link_hash_defweak)
5192 hsec = h->root.u.def.section;
5193 hval = h->root.u.def.value;
5195 else
5197 hsec = NULL;
5198 hval = 0;
5201 addend = link_order->u.reloc.p->addend;
5202 if (hsec != NULL)
5203 addend += (hsec->output_section->vma
5204 + hsec->output_offset
5205 + hval);
5207 if (addend != 0)
5209 bfd_size_type size;
5210 bfd_byte *buf;
5211 bfd_reloc_status_type rstat;
5212 bfd_boolean ok;
5214 size = bfd_get_reloc_size (howto);
5215 buf = bfd_zmalloc (size);
5216 if (buf == NULL)
5217 return FALSE;
5219 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5220 switch (rstat)
5222 case bfd_reloc_ok:
5223 break;
5224 default:
5225 case bfd_reloc_outofrange:
5226 abort ();
5227 case bfd_reloc_overflow:
5228 if (! ((*finfo->info->callbacks->reloc_overflow)
5229 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5230 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5232 free (buf);
5233 return FALSE;
5235 break;
5237 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5238 (file_ptr) link_order->offset, size);
5239 free (buf);
5240 if (! ok)
5241 return FALSE;
5244 /* Store the reloc information in the right place. It will get
5245 swapped and written out at the end of the final_link routine. */
5246 irel = (finfo->section_info[output_section->target_index].relocs
5247 + output_section->reloc_count);
5248 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5249 + output_section->reloc_count);
5251 memset (irel, 0, sizeof (struct internal_reloc));
5252 *rel_hash_ptr = NULL;
5254 irel->r_vaddr = output_section->vma + link_order->offset;
5256 if (h->indx >= 0)
5257 irel->r_symndx = h->indx;
5258 else
5260 /* Set the index to -2 to force this symbol to get written out. */
5261 h->indx = -2;
5262 *rel_hash_ptr = h;
5263 irel->r_symndx = 0;
5266 irel->r_type = howto->type;
5267 irel->r_size = howto->bitsize - 1;
5268 if (howto->complain_on_overflow == complain_overflow_signed)
5269 irel->r_size |= 0x80;
5271 ++output_section->reloc_count;
5273 /* Now output the reloc to the .loader section. */
5275 ldrel.l_vaddr = irel->r_vaddr;
5277 if (hsec != NULL)
5279 const char *secname;
5281 secname = hsec->output_section->name;
5283 if (strcmp (secname, ".text") == 0)
5284 ldrel.l_symndx = 0;
5285 else if (strcmp (secname, ".data") == 0)
5286 ldrel.l_symndx = 1;
5287 else if (strcmp (secname, ".bss") == 0)
5288 ldrel.l_symndx = 2;
5289 else
5291 (*_bfd_error_handler)
5292 (_("%s: loader reloc in unrecognized section `%s'"),
5293 bfd_get_filename (output_bfd), secname);
5294 bfd_set_error (bfd_error_nonrepresentable_section);
5295 return FALSE;
5298 else
5300 if (h->ldindx < 0)
5302 (*_bfd_error_handler)
5303 (_("%s: `%s' in loader reloc but not loader sym"),
5304 bfd_get_filename (output_bfd),
5305 h->root.root.string);
5306 bfd_set_error (bfd_error_bad_value);
5307 return FALSE;
5309 ldrel.l_symndx = h->ldindx;
5312 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5313 ldrel.l_rsecnm = output_section->target_index;
5314 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5315 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5317 return TRUE;
5320 /* Do the final link step. */
5322 bfd_boolean
5323 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5325 bfd_size_type symesz;
5326 struct xcoff_final_link_info finfo;
5327 asection *o;
5328 struct bfd_link_order *p;
5329 bfd_size_type max_contents_size;
5330 bfd_size_type max_sym_count;
5331 bfd_size_type max_lineno_count;
5332 bfd_size_type max_reloc_count;
5333 bfd_size_type max_output_reloc_count;
5334 file_ptr rel_filepos;
5335 unsigned int relsz;
5336 file_ptr line_filepos;
5337 unsigned int linesz;
5338 bfd *sub;
5339 bfd_byte *external_relocs = NULL;
5340 char strbuf[STRING_SIZE_SIZE];
5341 file_ptr pos;
5342 bfd_size_type amt;
5344 if (info->shared)
5345 abfd->flags |= DYNAMIC;
5347 symesz = bfd_coff_symesz (abfd);
5349 finfo.info = info;
5350 finfo.output_bfd = abfd;
5351 finfo.strtab = NULL;
5352 finfo.section_info = NULL;
5353 finfo.last_file_index = -1;
5354 finfo.toc_symindx = -1;
5355 finfo.internal_syms = NULL;
5356 finfo.sym_indices = NULL;
5357 finfo.outsyms = NULL;
5358 finfo.linenos = NULL;
5359 finfo.contents = NULL;
5360 finfo.external_relocs = NULL;
5362 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5363 + bfd_xcoff_ldhdrsz (abfd));
5364 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5365 + bfd_xcoff_ldhdrsz(abfd)
5366 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5367 * bfd_xcoff_ldsymsz(abfd)));
5369 xcoff_data (abfd)->coff.link_info = info;
5371 finfo.strtab = _bfd_stringtab_init ();
5372 if (finfo.strtab == NULL)
5373 goto error_return;
5375 /* Count the line number and relocation entries required for the
5376 output file. Determine a few maximum sizes. */
5377 max_contents_size = 0;
5378 max_lineno_count = 0;
5379 max_reloc_count = 0;
5380 for (o = abfd->sections; o != NULL; o = o->next)
5382 o->reloc_count = 0;
5383 o->lineno_count = 0;
5384 for (p = o->map_head.link_order; p != NULL; p = p->next)
5386 if (p->type == bfd_indirect_link_order)
5388 asection *sec;
5390 sec = p->u.indirect.section;
5392 /* Mark all sections which are to be included in the
5393 link. This will normally be every section. We need
5394 to do this so that we can identify any sections which
5395 the linker has decided to not include. */
5396 sec->linker_mark = TRUE;
5398 if (info->strip == strip_none
5399 || info->strip == strip_some)
5400 o->lineno_count += sec->lineno_count;
5402 o->reloc_count += sec->reloc_count;
5404 if (sec->rawsize > max_contents_size)
5405 max_contents_size = sec->rawsize;
5406 if (sec->size > max_contents_size)
5407 max_contents_size = sec->size;
5408 if (sec->lineno_count > max_lineno_count)
5409 max_lineno_count = sec->lineno_count;
5410 if (coff_section_data (sec->owner, sec) != NULL
5411 && xcoff_section_data (sec->owner, sec) != NULL
5412 && (xcoff_section_data (sec->owner, sec)->lineno_count
5413 > max_lineno_count))
5414 max_lineno_count =
5415 xcoff_section_data (sec->owner, sec)->lineno_count;
5416 if (sec->reloc_count > max_reloc_count)
5417 max_reloc_count = sec->reloc_count;
5419 else if (p->type == bfd_section_reloc_link_order
5420 || p->type == bfd_symbol_reloc_link_order)
5421 ++o->reloc_count;
5425 /* Compute the file positions for all the sections. */
5426 if (abfd->output_has_begun)
5428 if (xcoff_hash_table (info)->file_align != 0)
5429 abort ();
5431 else
5433 bfd_vma file_align;
5435 file_align = xcoff_hash_table (info)->file_align;
5436 if (file_align != 0)
5438 bfd_boolean saw_contents;
5439 int indx;
5440 file_ptr sofar;
5442 /* Insert .pad sections before every section which has
5443 contents and is loaded, if it is preceded by some other
5444 section which has contents and is loaded. */
5445 saw_contents = TRUE;
5446 for (o = abfd->sections; o != NULL; o = o->next)
5448 if (strcmp (o->name, ".pad") == 0)
5449 saw_contents = FALSE;
5450 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5451 && (o->flags & SEC_LOAD) != 0)
5453 if (! saw_contents)
5454 saw_contents = TRUE;
5455 else
5457 asection *n;
5459 /* Create a pad section and place it before the section
5460 that needs padding. This requires unlinking and
5461 relinking the bfd's section list. */
5463 n = bfd_make_section_anyway (abfd, ".pad");
5464 n->flags = SEC_HAS_CONTENTS;
5465 n->alignment_power = 0;
5467 bfd_section_list_remove (abfd, n);
5468 bfd_section_list_insert_before (abfd, o, n);
5469 saw_contents = FALSE;
5474 /* Reset the section indices after inserting the new
5475 sections. */
5476 indx = 0;
5477 for (o = abfd->sections; o != NULL; o = o->next)
5479 ++indx;
5480 o->target_index = indx;
5482 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5484 /* Work out appropriate sizes for the .pad sections to force
5485 each section to land on a page boundary. This bit of
5486 code knows what compute_section_file_positions is going
5487 to do. */
5488 sofar = bfd_coff_filhsz (abfd);
5489 sofar += bfd_coff_aoutsz (abfd);
5490 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5491 for (o = abfd->sections; o != NULL; o = o->next)
5492 if ((bfd_xcoff_is_reloc_count_overflow
5493 (abfd, (bfd_vma) o->reloc_count))
5494 || (bfd_xcoff_is_lineno_count_overflow
5495 (abfd, (bfd_vma) o->lineno_count)))
5496 /* 64 does not overflow, need to check if 32 does */
5497 sofar += bfd_coff_scnhsz (abfd);
5499 for (o = abfd->sections; o != NULL; o = o->next)
5501 if (strcmp (o->name, ".pad") == 0)
5503 bfd_vma pageoff;
5505 BFD_ASSERT (o->size == 0);
5506 pageoff = sofar & (file_align - 1);
5507 if (pageoff != 0)
5509 o->size = file_align - pageoff;
5510 sofar += file_align - pageoff;
5511 o->flags |= SEC_HAS_CONTENTS;
5514 else
5516 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5517 sofar += BFD_ALIGN (o->size,
5518 1 << o->alignment_power);
5523 if (! bfd_coff_compute_section_file_positions (abfd))
5524 goto error_return;
5527 /* Allocate space for the pointers we need to keep for the relocs. */
5529 unsigned int i;
5531 /* We use section_count + 1, rather than section_count, because
5532 the target_index fields are 1 based. */
5533 amt = abfd->section_count + 1;
5534 amt *= sizeof (struct xcoff_link_section_info);
5535 finfo.section_info = bfd_malloc (amt);
5536 if (finfo.section_info == NULL)
5537 goto error_return;
5538 for (i = 0; i <= abfd->section_count; i++)
5540 finfo.section_info[i].relocs = NULL;
5541 finfo.section_info[i].rel_hashes = NULL;
5542 finfo.section_info[i].toc_rel_hashes = NULL;
5546 /* Set the file positions for the relocs. */
5547 rel_filepos = obj_relocbase (abfd);
5548 relsz = bfd_coff_relsz (abfd);
5549 max_output_reloc_count = 0;
5550 for (o = abfd->sections; o != NULL; o = o->next)
5552 if (o->reloc_count == 0)
5553 o->rel_filepos = 0;
5554 else
5556 /* A stripped file has no relocs. However, we still
5557 allocate the buffers, so that later code doesn't have to
5558 worry about whether we are stripping or not. */
5559 if (info->strip == strip_all)
5560 o->rel_filepos = 0;
5561 else
5563 o->flags |= SEC_RELOC;
5564 o->rel_filepos = rel_filepos;
5565 rel_filepos += o->reloc_count * relsz;
5568 /* We don't know the indices of global symbols until we have
5569 written out all the local symbols. For each section in
5570 the output file, we keep an array of pointers to hash
5571 table entries. Each entry in the array corresponds to a
5572 reloc. When we find a reloc against a global symbol, we
5573 set the corresponding entry in this array so that we can
5574 fix up the symbol index after we have written out all the
5575 local symbols.
5577 Because of this problem, we also keep the relocs in
5578 memory until the end of the link. This wastes memory.
5579 We could backpatch the file later, I suppose, although it
5580 would be slow. */
5581 amt = o->reloc_count;
5582 amt *= sizeof (struct internal_reloc);
5583 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5585 amt = o->reloc_count;
5586 amt *= sizeof (struct xcoff_link_hash_entry *);
5587 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5589 if (finfo.section_info[o->target_index].relocs == NULL
5590 || finfo.section_info[o->target_index].rel_hashes == NULL)
5591 goto error_return;
5593 if (o->reloc_count > max_output_reloc_count)
5594 max_output_reloc_count = o->reloc_count;
5598 /* We now know the size of the relocs, so we can determine the file
5599 positions of the line numbers. */
5600 line_filepos = rel_filepos;
5601 finfo.line_filepos = line_filepos;
5602 linesz = bfd_coff_linesz (abfd);
5603 for (o = abfd->sections; o != NULL; o = o->next)
5605 if (o->lineno_count == 0)
5606 o->line_filepos = 0;
5607 else
5609 o->line_filepos = line_filepos;
5610 line_filepos += o->lineno_count * linesz;
5613 /* Reset the reloc and lineno counts, so that we can use them to
5614 count the number of entries we have output so far. */
5615 o->reloc_count = 0;
5616 o->lineno_count = 0;
5619 obj_sym_filepos (abfd) = line_filepos;
5621 /* Figure out the largest number of symbols in an input BFD. Take
5622 the opportunity to clear the output_has_begun fields of all the
5623 input BFD's. We want at least 6 symbols, since that is the
5624 number which xcoff_write_global_symbol may need. */
5625 max_sym_count = 6;
5626 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5628 bfd_size_type sz;
5630 sub->output_has_begun = FALSE;
5631 sz = obj_raw_syment_count (sub);
5632 if (sz > max_sym_count)
5633 max_sym_count = sz;
5636 /* Allocate some buffers used while linking. */
5637 amt = max_sym_count * sizeof (struct internal_syment);
5638 finfo.internal_syms = bfd_malloc (amt);
5640 amt = max_sym_count * sizeof (long);
5641 finfo.sym_indices = bfd_malloc (amt);
5643 amt = (max_sym_count + 1) * symesz;
5644 finfo.outsyms = bfd_malloc (amt);
5646 amt = max_lineno_count * bfd_coff_linesz (abfd);
5647 finfo.linenos = bfd_malloc (amt);
5649 amt = max_contents_size;
5650 finfo.contents = bfd_malloc (amt);
5652 amt = max_reloc_count * relsz;
5653 finfo.external_relocs = bfd_malloc (amt);
5655 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5656 || (finfo.sym_indices == NULL && max_sym_count > 0)
5657 || finfo.outsyms == NULL
5658 || (finfo.linenos == NULL && max_lineno_count > 0)
5659 || (finfo.contents == NULL && max_contents_size > 0)
5660 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5661 goto error_return;
5663 obj_raw_syment_count (abfd) = 0;
5664 xcoff_data (abfd)->toc = (bfd_vma) -1;
5666 /* We now know the position of everything in the file, except that
5667 we don't know the size of the symbol table and therefore we don't
5668 know where the string table starts. We just build the string
5669 table in memory as we go along. We process all the relocations
5670 for a single input file at once. */
5671 for (o = abfd->sections; o != NULL; o = o->next)
5673 for (p = o->map_head.link_order; p != NULL; p = p->next)
5675 if (p->type == bfd_indirect_link_order
5676 && p->u.indirect.section->owner->xvec == abfd->xvec)
5678 sub = p->u.indirect.section->owner;
5679 if (! sub->output_has_begun)
5681 if (! xcoff_link_input_bfd (&finfo, sub))
5682 goto error_return;
5683 sub->output_has_begun = TRUE;
5686 else if (p->type == bfd_section_reloc_link_order
5687 || p->type == bfd_symbol_reloc_link_order)
5689 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5690 goto error_return;
5692 else
5694 if (! _bfd_default_link_order (abfd, info, o, p))
5695 goto error_return;
5700 /* Free up the buffers used by xcoff_link_input_bfd. */
5701 if (finfo.internal_syms != NULL)
5703 free (finfo.internal_syms);
5704 finfo.internal_syms = NULL;
5706 if (finfo.sym_indices != NULL)
5708 free (finfo.sym_indices);
5709 finfo.sym_indices = NULL;
5711 if (finfo.linenos != NULL)
5713 free (finfo.linenos);
5714 finfo.linenos = NULL;
5716 if (finfo.contents != NULL)
5718 free (finfo.contents);
5719 finfo.contents = NULL;
5721 if (finfo.external_relocs != NULL)
5723 free (finfo.external_relocs);
5724 finfo.external_relocs = NULL;
5727 /* The value of the last C_FILE symbol is supposed to be -1. Write
5728 it out again. */
5729 if (finfo.last_file_index != -1)
5731 finfo.last_file.n_value = -(bfd_vma) 1;
5732 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
5733 (void *) finfo.outsyms);
5734 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
5735 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5736 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
5737 goto error_return;
5740 /* Write out all the global symbols which do not come from XCOFF
5741 input files. */
5742 xcoff_link_hash_traverse (xcoff_hash_table (info),
5743 xcoff_write_global_symbol,
5744 (void *) &finfo);
5746 if (finfo.outsyms != NULL)
5748 free (finfo.outsyms);
5749 finfo.outsyms = NULL;
5752 /* Now that we have written out all the global symbols, we know the
5753 symbol indices to use for relocs against them, and we can finally
5754 write out the relocs. */
5755 amt = max_output_reloc_count * relsz;
5756 external_relocs = bfd_malloc (amt);
5757 if (external_relocs == NULL && max_output_reloc_count != 0)
5758 goto error_return;
5760 for (o = abfd->sections; o != NULL; o = o->next)
5762 struct internal_reloc *irel;
5763 struct internal_reloc *irelend;
5764 struct xcoff_link_hash_entry **rel_hash;
5765 struct xcoff_toc_rel_hash *toc_rel_hash;
5766 bfd_byte *erel;
5767 bfd_size_type rel_size;
5769 /* A stripped file has no relocs. */
5770 if (info->strip == strip_all)
5772 o->reloc_count = 0;
5773 continue;
5776 if (o->reloc_count == 0)
5777 continue;
5779 irel = finfo.section_info[o->target_index].relocs;
5780 irelend = irel + o->reloc_count;
5781 rel_hash = finfo.section_info[o->target_index].rel_hashes;
5782 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5784 if (*rel_hash != NULL)
5786 if ((*rel_hash)->indx < 0)
5788 if (! ((*info->callbacks->unattached_reloc)
5789 (info, (*rel_hash)->root.root.string,
5790 NULL, o, irel->r_vaddr)))
5791 goto error_return;
5792 (*rel_hash)->indx = 0;
5794 irel->r_symndx = (*rel_hash)->indx;
5798 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
5799 toc_rel_hash != NULL;
5800 toc_rel_hash = toc_rel_hash->next)
5802 if (toc_rel_hash->h->u.toc_indx < 0)
5804 if (! ((*info->callbacks->unattached_reloc)
5805 (info, toc_rel_hash->h->root.root.string,
5806 NULL, o, toc_rel_hash->rel->r_vaddr)))
5807 goto error_return;
5808 toc_rel_hash->h->u.toc_indx = 0;
5810 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
5813 /* XCOFF requires that the relocs be sorted by address. We tend
5814 to produce them in the order in which their containing csects
5815 appear in the symbol table, which is not necessarily by
5816 address. So we sort them here. There may be a better way to
5817 do this. */
5818 qsort ((void *) finfo.section_info[o->target_index].relocs,
5819 o->reloc_count, sizeof (struct internal_reloc),
5820 xcoff_sort_relocs);
5822 irel = finfo.section_info[o->target_index].relocs;
5823 irelend = irel + o->reloc_count;
5824 erel = external_relocs;
5825 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5826 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
5828 rel_size = relsz * o->reloc_count;
5829 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
5830 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
5831 goto error_return;
5834 if (external_relocs != NULL)
5836 free (external_relocs);
5837 external_relocs = NULL;
5840 /* Free up the section information. */
5841 if (finfo.section_info != NULL)
5843 unsigned int i;
5845 for (i = 0; i < abfd->section_count; i++)
5847 if (finfo.section_info[i].relocs != NULL)
5848 free (finfo.section_info[i].relocs);
5849 if (finfo.section_info[i].rel_hashes != NULL)
5850 free (finfo.section_info[i].rel_hashes);
5852 free (finfo.section_info);
5853 finfo.section_info = NULL;
5856 /* Write out the loader section contents. */
5857 BFD_ASSERT ((bfd_byte *) finfo.ldrel
5858 == (xcoff_hash_table (info)->loader_section->contents
5859 + xcoff_hash_table (info)->ldhdr.l_impoff));
5860 o = xcoff_hash_table (info)->loader_section;
5861 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
5862 (file_ptr) o->output_offset, o->size))
5863 goto error_return;
5865 /* Write out the magic sections. */
5866 o = xcoff_hash_table (info)->linkage_section;
5867 if (o->size > 0
5868 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5869 (file_ptr) o->output_offset,
5870 o->size))
5871 goto error_return;
5872 o = xcoff_hash_table (info)->toc_section;
5873 if (o->size > 0
5874 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5875 (file_ptr) o->output_offset,
5876 o->size))
5877 goto error_return;
5878 o = xcoff_hash_table (info)->descriptor_section;
5879 if (o->size > 0
5880 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5881 (file_ptr) o->output_offset,
5882 o->size))
5883 goto error_return;
5885 /* Write out the string table. */
5886 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
5887 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5888 goto error_return;
5889 H_PUT_32 (abfd,
5890 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
5891 strbuf);
5892 amt = STRING_SIZE_SIZE;
5893 if (bfd_bwrite (strbuf, amt, abfd) != amt)
5894 goto error_return;
5895 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
5896 goto error_return;
5898 _bfd_stringtab_free (finfo.strtab);
5900 /* Write out the debugging string table. */
5901 o = xcoff_hash_table (info)->debug_section;
5902 if (o != NULL)
5904 struct bfd_strtab_hash *debug_strtab;
5906 debug_strtab = xcoff_hash_table (info)->debug_strtab;
5907 BFD_ASSERT (o->output_section->size - o->output_offset
5908 >= _bfd_stringtab_size (debug_strtab));
5909 pos = o->output_section->filepos + o->output_offset;
5910 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5911 goto error_return;
5912 if (! _bfd_stringtab_emit (abfd, debug_strtab))
5913 goto error_return;
5916 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
5917 not try to write out the symbols. */
5918 bfd_get_symcount (abfd) = 0;
5920 return TRUE;
5922 error_return:
5923 if (finfo.strtab != NULL)
5924 _bfd_stringtab_free (finfo.strtab);
5926 if (finfo.section_info != NULL)
5928 unsigned int i;
5930 for (i = 0; i < abfd->section_count; i++)
5932 if (finfo.section_info[i].relocs != NULL)
5933 free (finfo.section_info[i].relocs);
5934 if (finfo.section_info[i].rel_hashes != NULL)
5935 free (finfo.section_info[i].rel_hashes);
5937 free (finfo.section_info);
5940 if (finfo.internal_syms != NULL)
5941 free (finfo.internal_syms);
5942 if (finfo.sym_indices != NULL)
5943 free (finfo.sym_indices);
5944 if (finfo.outsyms != NULL)
5945 free (finfo.outsyms);
5946 if (finfo.linenos != NULL)
5947 free (finfo.linenos);
5948 if (finfo.contents != NULL)
5949 free (finfo.contents);
5950 if (finfo.external_relocs != NULL)
5951 free (finfo.external_relocs);
5952 if (external_relocs != NULL)
5953 free (external_relocs);
5954 return FALSE;