PR 3704
[binutils.git] / bfd / xcofflink.c
blobe76b95cdd51687f727ab2e69f663a1f95c927b58
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;
797 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
799 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
800 if (lsec == NULL)
801 goto end_return;
803 xcoff_hash_table (info)->loader_section = lsec;
806 /* Likewise for the linkage section. */
807 if (xcoff_hash_table (info)->linkage_section == NULL)
809 asection *lsec;
810 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
811 | SEC_IN_MEMORY);
813 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
814 if (lsec == NULL)
815 goto end_return;
817 xcoff_hash_table (info)->linkage_section = lsec;
818 lsec->alignment_power = 2;
821 /* Likewise for the TOC section. */
822 if (xcoff_hash_table (info)->toc_section == NULL)
824 asection *tsec;
825 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
826 | SEC_IN_MEMORY);
828 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
829 if (tsec == NULL)
830 goto end_return;
832 xcoff_hash_table (info)->toc_section = tsec;
833 tsec->alignment_power = 2;
836 /* Likewise for the descriptor section. */
837 if (xcoff_hash_table (info)->descriptor_section == NULL)
839 asection *dsec;
840 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
841 | SEC_IN_MEMORY);
843 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
844 if (dsec == NULL)
845 goto end_return;
847 xcoff_hash_table (info)->descriptor_section = dsec;
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;
856 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
858 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
859 if (dsec == NULL)
860 goto end_return;
862 xcoff_hash_table (info)->debug_section = dsec;
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_with_flags (abfd, ".td",
1556 SEC_ALLOC);
1558 else
1559 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1560 SEC_ALLOC);
1562 if (csect == NULL)
1563 goto error_return;
1564 csect->vma = sym.n_value;
1565 csect->size = aux.x_csect.x_scnlen.l;
1566 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1567 /* There are a number of other fields and section flags
1568 which we do not bother to set. */
1570 csect_index = ((esym
1571 - (bfd_byte *) obj_coff_external_syms (abfd))
1572 / symesz);
1574 amt = sizeof (struct coff_section_tdata);
1575 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1576 if (csect->used_by_bfd == NULL)
1577 goto error_return;
1578 amt = sizeof (struct xcoff_section_tdata);
1579 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1580 if (coff_section_data (abfd, csect)->tdata == NULL)
1581 goto error_return;
1582 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1584 if (first_csect == NULL)
1585 first_csect = csect;
1587 if (sym.n_sclass == C_EXT)
1589 csect->flags |= SEC_IS_COMMON;
1590 csect->size = 0;
1591 section = csect;
1592 value = aux.x_csect.x_scnlen.l;
1595 break;
1598 /* Check for magic symbol names. */
1599 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1600 && aux.x_csect.x_smclas != XMC_TC
1601 && aux.x_csect.x_smclas != XMC_TD)
1603 int i = -1;
1605 if (name[0] == '_')
1607 if (strcmp (name, "_text") == 0)
1608 i = XCOFF_SPECIAL_SECTION_TEXT;
1609 else if (strcmp (name, "_etext") == 0)
1610 i = XCOFF_SPECIAL_SECTION_ETEXT;
1611 else if (strcmp (name, "_data") == 0)
1612 i = XCOFF_SPECIAL_SECTION_DATA;
1613 else if (strcmp (name, "_edata") == 0)
1614 i = XCOFF_SPECIAL_SECTION_EDATA;
1615 else if (strcmp (name, "_end") == 0)
1616 i = XCOFF_SPECIAL_SECTION_END;
1618 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1619 i = XCOFF_SPECIAL_SECTION_END2;
1621 if (i != -1)
1622 xcoff_hash_table (info)->special_sections[i] = csect;
1625 /* Now we have enough information to add the symbol to the
1626 linker hash table. */
1628 if (sym.n_sclass == C_EXT)
1630 bfd_boolean copy;
1632 BFD_ASSERT (section != NULL);
1634 /* We must copy the name into memory if we got it from the
1635 syment itself, rather than the string table. */
1636 copy = default_copy;
1637 if (sym._n._n_n._n_zeroes != 0
1638 || sym._n._n_n._n_offset == 0)
1639 copy = TRUE;
1641 /* The AIX linker appears to only detect multiple symbol
1642 definitions when there is a reference to the symbol. If
1643 a symbol is defined multiple times, and the only
1644 references are from the same object file, the AIX linker
1645 appears to permit it. It does not merge the different
1646 definitions, but handles them independently. On the
1647 other hand, if there is a reference, the linker reports
1648 an error.
1650 This matters because the AIX <net/net_globals.h> header
1651 file actually defines an initialized array, so we have to
1652 actually permit that to work.
1654 Just to make matters even more confusing, the AIX linker
1655 appears to permit multiple symbol definitions whenever
1656 the second definition is in an archive rather than an
1657 object file. This may be a consequence of the manner in
1658 which it handles archives: I think it may load the entire
1659 archive in as separate csects, and then let garbage
1660 collection discard symbols.
1662 We also have to handle the case of statically linking a
1663 shared object, which will cause symbol redefinitions,
1664 although this is an easier case to detect. */
1666 if (info->hash->creator == abfd->xvec)
1668 if (! bfd_is_und_section (section))
1669 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1670 name, TRUE, copy, FALSE);
1671 else
1672 /* Make a copy of the symbol name to prevent problems with
1673 merging symbols. */
1674 *sym_hash = ((struct xcoff_link_hash_entry *)
1675 bfd_wrapped_link_hash_lookup (abfd, info, name,
1676 TRUE, TRUE, FALSE));
1678 if (*sym_hash == NULL)
1679 goto error_return;
1680 if (((*sym_hash)->root.type == bfd_link_hash_defined
1681 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1682 && ! bfd_is_und_section (section)
1683 && ! bfd_is_com_section (section))
1685 /* This is a second definition of a defined symbol. */
1686 if ((abfd->flags & DYNAMIC) != 0
1687 && ((*sym_hash)->smclas != XMC_GL
1688 || aux.x_csect.x_smclas == XMC_GL
1689 || ((*sym_hash)->root.u.def.section->owner->flags
1690 & DYNAMIC) == 0))
1692 /* The new symbol is from a shared library, and
1693 either the existing symbol is not global
1694 linkage code or this symbol is global linkage
1695 code. If the existing symbol is global
1696 linkage code and the new symbol is not, then
1697 we want to use the new symbol. */
1698 section = bfd_und_section_ptr;
1699 value = 0;
1701 else if (((*sym_hash)->root.u.def.section->owner->flags
1702 & DYNAMIC) != 0)
1704 /* The existing symbol is from a shared library.
1705 Replace it. */
1706 (*sym_hash)->root.type = bfd_link_hash_undefined;
1707 (*sym_hash)->root.u.undef.abfd =
1708 (*sym_hash)->root.u.def.section->owner;
1710 else if (abfd->my_archive != NULL)
1712 /* This is a redefinition in an object contained
1713 in an archive. Just ignore it. See the
1714 comment above. */
1715 section = bfd_und_section_ptr;
1716 value = 0;
1718 else if ((*sym_hash)->root.u.undef.next != NULL
1719 || info->hash->undefs_tail == &(*sym_hash)->root)
1721 /* This symbol has been referenced. In this
1722 case, we just continue and permit the
1723 multiple definition error. See the comment
1724 above about the behaviour of the AIX linker. */
1726 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1728 /* The symbols are both csects of the same
1729 class. There is at least a chance that this
1730 is a semi-legitimate redefinition. */
1731 section = bfd_und_section_ptr;
1732 value = 0;
1733 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1736 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1737 && ((*sym_hash)->root.type == bfd_link_hash_defined
1738 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1739 && (bfd_is_und_section (section)
1740 || bfd_is_com_section (section)))
1742 /* This is a reference to a multiply defined symbol.
1743 Report the error now. See the comment above
1744 about the behaviour of the AIX linker. We could
1745 also do this with warning symbols, but I'm not
1746 sure the XCOFF linker is wholly prepared to
1747 handle them, and that would only be a warning,
1748 not an error. */
1749 if (! ((*info->callbacks->multiple_definition)
1750 (info, (*sym_hash)->root.root.string,
1751 NULL, NULL, (bfd_vma) 0,
1752 (*sym_hash)->root.u.def.section->owner,
1753 (*sym_hash)->root.u.def.section,
1754 (*sym_hash)->root.u.def.value)))
1755 goto error_return;
1756 /* Try not to give this error too many times. */
1757 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1761 /* _bfd_generic_link_add_one_symbol may call the linker to
1762 generate an error message, and the linker may try to read
1763 the symbol table to give a good error. Right now, the
1764 line numbers are in an inconsistent state, since they are
1765 counted both in the real sections and in the new csects.
1766 We need to leave the count in the real sections so that
1767 the linker can report the line number of the error
1768 correctly, so temporarily clobber the link to the csects
1769 so that the linker will not try to read the line numbers
1770 a second time from the csects. */
1771 BFD_ASSERT (last_real->next == first_csect);
1772 last_real->next = NULL;
1773 if (! (_bfd_generic_link_add_one_symbol
1774 (info, abfd, name, flags, section, value,
1775 NULL, copy, TRUE,
1776 (struct bfd_link_hash_entry **) sym_hash)))
1777 goto error_return;
1778 last_real->next = first_csect;
1780 if (smtyp == XTY_CM)
1782 if ((*sym_hash)->root.type != bfd_link_hash_common
1783 || (*sym_hash)->root.u.c.p->section != csect)
1784 /* We don't need the common csect we just created. */
1785 csect->size = 0;
1786 else
1787 (*sym_hash)->root.u.c.p->alignment_power
1788 = csect->alignment_power;
1791 if (info->hash->creator == abfd->xvec)
1793 int flag;
1795 if (smtyp == XTY_ER || smtyp == XTY_CM)
1796 flag = XCOFF_REF_REGULAR;
1797 else
1798 flag = XCOFF_DEF_REGULAR;
1799 (*sym_hash)->flags |= flag;
1801 if ((*sym_hash)->smclas == XMC_UA
1802 || flag == XCOFF_DEF_REGULAR)
1803 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1807 *csect_cache = csect;
1809 esym += (sym.n_numaux + 1) * symesz;
1810 sym_hash += sym.n_numaux + 1;
1811 csect_cache += sym.n_numaux + 1;
1814 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1816 /* Make sure that we have seen all the relocs. */
1817 for (o = abfd->sections; o != first_csect; o = o->next)
1819 /* Reset the section size and the line number count, since the
1820 data is now attached to the csects. Don't reset the size of
1821 the .debug section, since we need to read it below in
1822 bfd_xcoff_size_dynamic_sections. */
1823 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1824 o->size = 0;
1825 o->lineno_count = 0;
1827 if ((o->flags & SEC_RELOC) != 0)
1829 bfd_size_type i;
1830 struct internal_reloc *rel;
1831 asection **rel_csect;
1833 rel = reloc_info[o->target_index].relocs;
1834 rel_csect = reloc_info[o->target_index].csects;
1836 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1838 if (*rel_csect == NULL)
1840 (*_bfd_error_handler)
1841 (_("%B: reloc %s:%d not in csect"),
1842 abfd, o->name, i);
1843 bfd_set_error (bfd_error_bad_value);
1844 goto error_return;
1847 /* We identify all symbols which are called, so that we
1848 can create glue code for calls to functions imported
1849 from dynamic objects. */
1850 if (info->hash->creator == abfd->xvec
1851 && *rel_csect != bfd_und_section_ptr
1852 && (rel->r_type == R_BR
1853 || rel->r_type == R_RBR)
1854 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1856 struct xcoff_link_hash_entry *h;
1858 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1859 h->flags |= XCOFF_CALLED;
1860 /* If the symbol name starts with a period, it is
1861 the code of a function. If the symbol is
1862 currently undefined, then add an undefined symbol
1863 for the function descriptor. This should do no
1864 harm, because any regular object that defines the
1865 function should also define the function
1866 descriptor. It helps, because it means that we
1867 will identify the function descriptor with a
1868 dynamic object if a dynamic object defines it. */
1869 if (h->root.root.string[0] == '.'
1870 && h->descriptor == NULL)
1872 struct xcoff_link_hash_entry *hds;
1873 struct bfd_link_hash_entry *bh;
1875 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1876 h->root.root.string + 1,
1877 TRUE, FALSE, TRUE);
1878 if (hds == NULL)
1879 goto error_return;
1880 if (hds->root.type == bfd_link_hash_new)
1882 bh = &hds->root;
1883 if (! (_bfd_generic_link_add_one_symbol
1884 (info, abfd, hds->root.root.string,
1885 (flagword) 0, bfd_und_section_ptr,
1886 (bfd_vma) 0, NULL, FALSE,
1887 TRUE, &bh)))
1888 goto error_return;
1889 hds = (struct xcoff_link_hash_entry *) bh;
1891 hds->flags |= XCOFF_DESCRIPTOR;
1892 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1893 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1894 hds->descriptor = h;
1895 h->descriptor = hds;
1900 free (reloc_info[o->target_index].csects);
1901 reloc_info[o->target_index].csects = NULL;
1903 /* Reset SEC_RELOC and the reloc_count, since the reloc
1904 information is now attached to the csects. */
1905 o->flags &=~ SEC_RELOC;
1906 o->reloc_count = 0;
1908 /* If we are not keeping memory, free the reloc information. */
1909 if (! info->keep_memory
1910 && coff_section_data (abfd, o) != NULL
1911 && coff_section_data (abfd, o)->relocs != NULL
1912 && ! coff_section_data (abfd, o)->keep_relocs)
1914 free (coff_section_data (abfd, o)->relocs);
1915 coff_section_data (abfd, o)->relocs = NULL;
1919 /* Free up the line numbers. FIXME: We could cache these
1920 somewhere for the final link, to avoid reading them again. */
1921 if (reloc_info[o->target_index].linenos != NULL)
1923 free (reloc_info[o->target_index].linenos);
1924 reloc_info[o->target_index].linenos = NULL;
1928 free (reloc_info);
1930 obj_coff_keep_syms (abfd) = keep_syms;
1932 return TRUE;
1934 error_return:
1935 if (reloc_info != NULL)
1937 for (o = abfd->sections; o != NULL; o = o->next)
1939 if (reloc_info[o->target_index].csects != NULL)
1940 free (reloc_info[o->target_index].csects);
1941 if (reloc_info[o->target_index].linenos != NULL)
1942 free (reloc_info[o->target_index].linenos);
1944 free (reloc_info);
1946 obj_coff_keep_syms (abfd) = keep_syms;
1947 return FALSE;
1950 #undef N_TMASK
1951 #undef N_BTSHFT
1953 /* Add symbols from an XCOFF object file. */
1955 static bfd_boolean
1956 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1958 if (! _bfd_coff_get_external_symbols (abfd))
1959 return FALSE;
1960 if (! xcoff_link_add_symbols (abfd, info))
1961 return FALSE;
1962 if (! info->keep_memory)
1964 if (! _bfd_coff_free_symbols (abfd))
1965 return FALSE;
1967 return TRUE;
1970 /* Look through the loader symbols to see if this dynamic object
1971 should be included in the link. The native linker uses the loader
1972 symbols, not the normal symbol table, so we do too. */
1974 static bfd_boolean
1975 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1976 struct bfd_link_info *info,
1977 bfd_boolean *pneeded)
1979 asection *lsec;
1980 bfd_byte *contents;
1981 struct internal_ldhdr ldhdr;
1982 const char *strings;
1983 bfd_byte *elsym, *elsymend;
1985 *pneeded = FALSE;
1987 lsec = bfd_get_section_by_name (abfd, ".loader");
1988 if (lsec == NULL)
1989 /* There are no symbols, so don't try to include it. */
1990 return TRUE;
1992 if (! xcoff_get_section_contents (abfd, lsec))
1993 return FALSE;
1994 contents = coff_section_data (abfd, lsec)->contents;
1996 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
1998 strings = (char *) contents + ldhdr.l_stoff;
2000 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2002 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2003 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2005 struct internal_ldsym ldsym;
2006 char nambuf[SYMNMLEN + 1];
2007 const char *name;
2008 struct bfd_link_hash_entry *h;
2010 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2012 /* We are only interested in exported symbols. */
2013 if ((ldsym.l_smtype & L_EXPORT) == 0)
2014 continue;
2016 if (ldsym._l._l_l._l_zeroes == 0)
2017 name = strings + ldsym._l._l_l._l_offset;
2018 else
2020 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2021 nambuf[SYMNMLEN] = '\0';
2022 name = nambuf;
2025 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2027 /* We are only interested in symbols that are currently
2028 undefined. At this point we know that we are using an XCOFF
2029 hash table. */
2030 if (h != NULL
2031 && h->type == bfd_link_hash_undefined
2032 && (((struct xcoff_link_hash_entry *) h)->flags
2033 & XCOFF_DEF_DYNAMIC) == 0)
2035 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2036 return FALSE;
2037 *pneeded = TRUE;
2038 return TRUE;
2042 /* We do not need this shared object. */
2043 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2045 free (coff_section_data (abfd, lsec)->contents);
2046 coff_section_data (abfd, lsec)->contents = NULL;
2049 return TRUE;
2052 /* Look through the symbols to see if this object file should be
2053 included in the link. */
2055 static bfd_boolean
2056 xcoff_link_check_ar_symbols (bfd *abfd,
2057 struct bfd_link_info *info,
2058 bfd_boolean *pneeded)
2060 bfd_size_type symesz;
2061 bfd_byte *esym;
2062 bfd_byte *esym_end;
2064 *pneeded = FALSE;
2066 if ((abfd->flags & DYNAMIC) != 0
2067 && ! info->static_link
2068 && info->hash->creator == abfd->xvec)
2069 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2071 symesz = bfd_coff_symesz (abfd);
2072 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2073 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2074 while (esym < esym_end)
2076 struct internal_syment sym;
2078 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2080 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
2082 const char *name;
2083 char buf[SYMNMLEN + 1];
2084 struct bfd_link_hash_entry *h;
2086 /* This symbol is externally visible, and is defined by this
2087 object file. */
2088 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2090 if (name == NULL)
2091 return FALSE;
2092 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2094 /* We are only interested in symbols that are currently
2095 undefined. If a symbol is currently known to be common,
2096 XCOFF linkers do not bring in an object file which
2097 defines it. We also don't bring in symbols to satisfy
2098 undefined references in shared objects. */
2099 if (h != NULL
2100 && h->type == bfd_link_hash_undefined
2101 && (info->hash->creator != abfd->xvec
2102 || (((struct xcoff_link_hash_entry *) h)->flags
2103 & XCOFF_DEF_DYNAMIC) == 0))
2105 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2106 return FALSE;
2107 *pneeded = TRUE;
2108 return TRUE;
2112 esym += (sym.n_numaux + 1) * symesz;
2115 /* We do not need this object file. */
2116 return TRUE;
2119 /* Check a single archive element to see if we need to include it in
2120 the link. *PNEEDED is set according to whether this element is
2121 needed in the link or not. This is called via
2122 _bfd_generic_link_add_archive_symbols. */
2124 static bfd_boolean
2125 xcoff_link_check_archive_element (bfd *abfd,
2126 struct bfd_link_info *info,
2127 bfd_boolean *pneeded)
2129 if (! _bfd_coff_get_external_symbols (abfd))
2130 return FALSE;
2132 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2133 return FALSE;
2135 if (*pneeded)
2137 if (! xcoff_link_add_symbols (abfd, info))
2138 return FALSE;
2141 if (! info->keep_memory || ! *pneeded)
2143 if (! _bfd_coff_free_symbols (abfd))
2144 return FALSE;
2147 return TRUE;
2150 /* Given an XCOFF BFD, add symbols to the global hash table as
2151 appropriate. */
2153 bfd_boolean
2154 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2156 switch (bfd_get_format (abfd))
2158 case bfd_object:
2159 return xcoff_link_add_object_symbols (abfd, info);
2161 case bfd_archive:
2162 /* If the archive has a map, do the usual search. We then need
2163 to check the archive for dynamic objects, because they may not
2164 appear in the archive map even though they should, perhaps, be
2165 included. If the archive has no map, we just consider each object
2166 file in turn, since that apparently is what the AIX native linker
2167 does. */
2168 if (bfd_has_map (abfd))
2170 if (! (_bfd_generic_link_add_archive_symbols
2171 (abfd, info, xcoff_link_check_archive_element)))
2172 return FALSE;
2176 bfd *member;
2178 member = bfd_openr_next_archived_file (abfd, NULL);
2179 while (member != NULL)
2181 if (bfd_check_format (member, bfd_object)
2182 && (info->hash->creator == member->xvec)
2183 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2185 bfd_boolean needed;
2187 if (! xcoff_link_check_archive_element (member, info,
2188 &needed))
2189 return FALSE;
2190 if (needed)
2191 member->archive_pass = -1;
2193 member = bfd_openr_next_archived_file (abfd, member);
2197 return TRUE;
2199 default:
2200 bfd_set_error (bfd_error_wrong_format);
2201 return FALSE;
2205 /* Mark a symbol as not being garbage, including the section in which
2206 it is defined. */
2208 static inline bfd_boolean
2209 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2211 if ((h->flags & XCOFF_MARK) != 0)
2212 return TRUE;
2214 h->flags |= XCOFF_MARK;
2215 if (h->root.type == bfd_link_hash_defined
2216 || h->root.type == bfd_link_hash_defweak)
2218 asection *hsec;
2220 hsec = h->root.u.def.section;
2221 if (! bfd_is_abs_section (hsec)
2222 && (hsec->flags & SEC_MARK) == 0)
2224 if (! xcoff_mark (info, hsec))
2225 return FALSE;
2229 if (h->toc_section != NULL
2230 && (h->toc_section->flags & SEC_MARK) == 0)
2232 if (! xcoff_mark (info, h->toc_section))
2233 return FALSE;
2236 return TRUE;
2239 /* The mark phase of garbage collection. For a given section, mark
2240 it, and all the sections which define symbols to which it refers.
2241 Because this function needs to look at the relocs, we also count
2242 the number of relocs which need to be copied into the .loader
2243 section. */
2245 static bfd_boolean
2246 xcoff_mark (struct bfd_link_info *info, asection *sec)
2248 if (bfd_is_abs_section (sec)
2249 || (sec->flags & SEC_MARK) != 0)
2250 return TRUE;
2252 sec->flags |= SEC_MARK;
2254 if (sec->owner->xvec == info->hash->creator
2255 && coff_section_data (sec->owner, sec) != NULL
2256 && xcoff_section_data (sec->owner, sec) != NULL)
2258 struct xcoff_link_hash_entry **hp, **hpend;
2259 struct internal_reloc *rel, *relend;
2261 /* Mark all the symbols in this section. */
2262 hp = (obj_xcoff_sym_hashes (sec->owner)
2263 + xcoff_section_data (sec->owner, sec)->first_symndx);
2264 hpend = (obj_xcoff_sym_hashes (sec->owner)
2265 + xcoff_section_data (sec->owner, sec)->last_symndx);
2266 for (; hp < hpend; hp++)
2268 struct xcoff_link_hash_entry *h;
2270 h = *hp;
2271 if (h != NULL
2272 && (h->flags & XCOFF_MARK) == 0)
2274 if (! xcoff_mark_symbol (info, h))
2275 return FALSE;
2279 /* Look through the section relocs. */
2280 if ((sec->flags & SEC_RELOC) != 0
2281 && sec->reloc_count > 0)
2283 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2284 NULL, FALSE, NULL);
2285 if (rel == NULL)
2286 return FALSE;
2287 relend = rel + sec->reloc_count;
2288 for (; rel < relend; rel++)
2290 asection *rsec;
2291 struct xcoff_link_hash_entry *h;
2293 if ((unsigned int) rel->r_symndx
2294 > obj_raw_syment_count (sec->owner))
2295 continue;
2297 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2298 if (h != NULL
2299 && (h->flags & XCOFF_MARK) == 0)
2301 if (! xcoff_mark_symbol (info, h))
2302 return FALSE;
2305 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2306 if (rsec != NULL
2307 && (rsec->flags & SEC_MARK) == 0)
2309 if (! xcoff_mark (info, rsec))
2310 return FALSE;
2313 /* See if this reloc needs to be copied into the .loader
2314 section. */
2315 switch (rel->r_type)
2317 default:
2318 if (h == NULL
2319 || h->root.type == bfd_link_hash_defined
2320 || h->root.type == bfd_link_hash_defweak
2321 || h->root.type == bfd_link_hash_common
2322 || ((h->flags & XCOFF_CALLED) != 0
2323 && (h->root.type == bfd_link_hash_undefined
2324 || h->root.type == bfd_link_hash_undefweak)
2325 && h->root.root.string[0] == '.'
2326 && h->descriptor != NULL
2327 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2328 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2329 && (h->descriptor->flags
2330 & XCOFF_DEF_REGULAR) == 0))))
2331 break;
2332 /* Fall through. */
2333 case R_POS:
2334 case R_NEG:
2335 case R_RL:
2336 case R_RLA:
2337 ++xcoff_hash_table (info)->ldrel_count;
2338 if (h != NULL)
2339 h->flags |= XCOFF_LDREL;
2340 break;
2341 case R_TOC:
2342 case R_GL:
2343 case R_TCL:
2344 case R_TRL:
2345 case R_TRLA:
2346 /* We should never need a .loader reloc for a TOC
2347 relative reloc. */
2348 break;
2352 if (! info->keep_memory
2353 && coff_section_data (sec->owner, sec) != NULL
2354 && coff_section_data (sec->owner, sec)->relocs != NULL
2355 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2357 free (coff_section_data (sec->owner, sec)->relocs);
2358 coff_section_data (sec->owner, sec)->relocs = NULL;
2363 return TRUE;
2366 /* Routines that are called after all the input files have been
2367 handled, but before the sections are laid out in memory. */
2369 /* The sweep phase of garbage collection. Remove all garbage
2370 sections. */
2372 static void
2373 xcoff_sweep (struct bfd_link_info *info)
2375 bfd *sub;
2377 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2379 asection *o;
2381 for (o = sub->sections; o != NULL; o = o->next)
2383 if ((o->flags & SEC_MARK) == 0)
2385 /* Keep all sections from non-XCOFF input files. Keep
2386 special sections. Keep .debug sections for the
2387 moment. */
2388 if (sub->xvec != info->hash->creator
2389 || o == xcoff_hash_table (info)->debug_section
2390 || o == xcoff_hash_table (info)->loader_section
2391 || o == xcoff_hash_table (info)->linkage_section
2392 || o == xcoff_hash_table (info)->toc_section
2393 || o == xcoff_hash_table (info)->descriptor_section
2394 || strcmp (o->name, ".debug") == 0)
2395 o->flags |= SEC_MARK;
2396 else
2398 o->size = 0;
2399 o->reloc_count = 0;
2400 o->lineno_count = 0;
2407 /* Record the number of elements in a set. This is used to output the
2408 correct csect length. */
2410 bfd_boolean
2411 bfd_xcoff_link_record_set (bfd *output_bfd,
2412 struct bfd_link_info *info,
2413 struct bfd_link_hash_entry *harg,
2414 bfd_size_type size)
2416 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2417 struct xcoff_link_size_list *n;
2418 bfd_size_type amt;
2420 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2421 return TRUE;
2423 /* This will hardly ever be called. I don't want to burn four bytes
2424 per global symbol, so instead the size is kept on a linked list
2425 attached to the hash table. */
2426 amt = sizeof (* n);
2427 n = bfd_alloc (output_bfd, amt);
2428 if (n == NULL)
2429 return FALSE;
2430 n->next = xcoff_hash_table (info)->size_list;
2431 n->h = h;
2432 n->size = size;
2433 xcoff_hash_table (info)->size_list = n;
2435 h->flags |= XCOFF_HAS_SIZE;
2437 return TRUE;
2440 /* Import a symbol. */
2442 bfd_boolean
2443 bfd_xcoff_import_symbol (bfd *output_bfd,
2444 struct bfd_link_info *info,
2445 struct bfd_link_hash_entry *harg,
2446 bfd_vma val,
2447 const char *imppath,
2448 const char *impfile,
2449 const char *impmember,
2450 unsigned int syscall_flag)
2452 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2454 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2455 return TRUE;
2457 /* A symbol name which starts with a period is the code for a
2458 function. If the symbol is undefined, then add an undefined
2459 symbol for the function descriptor, and import that instead. */
2460 if (h->root.root.string[0] == '.'
2461 && h->root.type == bfd_link_hash_undefined
2462 && val == (bfd_vma) -1)
2464 struct xcoff_link_hash_entry *hds;
2466 hds = h->descriptor;
2467 if (hds == NULL)
2469 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2470 h->root.root.string + 1,
2471 TRUE, FALSE, TRUE);
2472 if (hds == NULL)
2473 return FALSE;
2474 if (hds->root.type == bfd_link_hash_new)
2476 hds->root.type = bfd_link_hash_undefined;
2477 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2479 hds->flags |= XCOFF_DESCRIPTOR;
2480 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2481 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2482 hds->descriptor = h;
2483 h->descriptor = hds;
2486 /* Now, if the descriptor is undefined, import the descriptor
2487 rather than the symbol we were told to import. FIXME: Is
2488 this correct in all cases? */
2489 if (hds->root.type == bfd_link_hash_undefined)
2490 h = hds;
2493 h->flags |= (XCOFF_IMPORT | syscall_flag);
2495 if (val != (bfd_vma) -1)
2497 if (h->root.type == bfd_link_hash_defined
2498 && (! bfd_is_abs_section (h->root.u.def.section)
2499 || h->root.u.def.value != val))
2501 if (! ((*info->callbacks->multiple_definition)
2502 (info, h->root.root.string, h->root.u.def.section->owner,
2503 h->root.u.def.section, h->root.u.def.value,
2504 output_bfd, bfd_abs_section_ptr, val)))
2505 return FALSE;
2508 h->root.type = bfd_link_hash_defined;
2509 h->root.u.def.section = bfd_abs_section_ptr;
2510 h->root.u.def.value = val;
2513 /* We overload the ldindx field to hold the l_ifile value for this
2514 symbol. */
2515 BFD_ASSERT (h->ldsym == NULL);
2516 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2517 if (imppath == NULL)
2518 h->ldindx = -1;
2519 else
2521 unsigned int c;
2522 struct xcoff_import_file **pp;
2524 /* We start c at 1 because the first entry in the import list is
2525 reserved for the library search path. */
2526 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2527 *pp != NULL;
2528 pp = &(*pp)->next, ++c)
2530 if (strcmp ((*pp)->path, imppath) == 0
2531 && strcmp ((*pp)->file, impfile) == 0
2532 && strcmp ((*pp)->member, impmember) == 0)
2533 break;
2536 if (*pp == NULL)
2538 struct xcoff_import_file *n;
2539 bfd_size_type amt = sizeof (* n);
2541 n = bfd_alloc (output_bfd, amt);
2542 if (n == NULL)
2543 return FALSE;
2544 n->next = NULL;
2545 n->path = imppath;
2546 n->file = impfile;
2547 n->member = impmember;
2548 *pp = n;
2551 h->ldindx = c;
2554 return TRUE;
2557 /* Export a symbol. */
2559 bfd_boolean
2560 bfd_xcoff_export_symbol (bfd *output_bfd,
2561 struct bfd_link_info *info,
2562 struct bfd_link_hash_entry *harg)
2564 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2566 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2567 return TRUE;
2569 h->flags |= XCOFF_EXPORT;
2571 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2572 I'm just going to ignore it until somebody explains it. */
2574 /* See if this is a function descriptor. It may be one even though
2575 it is not so marked. */
2576 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2577 && h->root.root.string[0] != '.')
2579 char *fnname;
2580 struct xcoff_link_hash_entry *hfn;
2581 bfd_size_type amt = strlen (h->root.root.string) + 2;
2583 fnname = bfd_malloc (amt);
2584 if (fnname == NULL)
2585 return FALSE;
2586 fnname[0] = '.';
2587 strcpy (fnname + 1, h->root.root.string);
2588 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2589 fnname, FALSE, FALSE, TRUE);
2590 free (fnname);
2591 if (hfn != NULL
2592 && hfn->smclas == XMC_PR
2593 && (hfn->root.type == bfd_link_hash_defined
2594 || hfn->root.type == bfd_link_hash_defweak))
2596 h->flags |= XCOFF_DESCRIPTOR;
2597 h->descriptor = hfn;
2598 hfn->descriptor = h;
2602 /* Make sure we don't garbage collect this symbol. */
2603 if (! xcoff_mark_symbol (info, h))
2604 return FALSE;
2606 /* If this is a function descriptor, make sure we don't garbage
2607 collect the associated function code. We normally don't have to
2608 worry about this, because the descriptor will be attached to a
2609 section with relocs, but if we are creating the descriptor
2610 ourselves those relocs will not be visible to the mark code. */
2611 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2613 if (! xcoff_mark_symbol (info, h->descriptor))
2614 return FALSE;
2617 return TRUE;
2620 /* Count a reloc against a symbol. This is called for relocs
2621 generated by the linker script, typically for global constructors
2622 and destructors. */
2624 bfd_boolean
2625 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2626 struct bfd_link_info *info,
2627 const char *name)
2629 struct xcoff_link_hash_entry *h;
2631 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2632 return TRUE;
2634 h = ((struct xcoff_link_hash_entry *)
2635 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2636 FALSE));
2637 if (h == NULL)
2639 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2640 bfd_set_error (bfd_error_no_symbols);
2641 return FALSE;
2644 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2645 ++xcoff_hash_table (info)->ldrel_count;
2647 /* Mark the symbol to avoid garbage collection. */
2648 if (! xcoff_mark_symbol (info, h))
2649 return FALSE;
2651 return TRUE;
2654 /* This function is called for each symbol to which the linker script
2655 assigns a value. */
2657 bfd_boolean
2658 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2659 struct bfd_link_info *info,
2660 const char *name)
2662 struct xcoff_link_hash_entry *h;
2664 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2665 return TRUE;
2667 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2668 FALSE);
2669 if (h == NULL)
2670 return FALSE;
2672 h->flags |= XCOFF_DEF_REGULAR;
2674 return TRUE;
2677 /* Add a symbol to the .loader symbols, if necessary. */
2679 static bfd_boolean
2680 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
2682 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2683 bfd_size_type amt;
2685 if (h->root.type == bfd_link_hash_warning)
2686 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
2688 /* __rtinit, this symbol has special handling. */
2689 if (h->flags & XCOFF_RTINIT)
2690 return TRUE;
2692 /* If this is a final link, and the symbol was defined as a common
2693 symbol in a regular object file, and there was no definition in
2694 any dynamic object, then the linker will have allocated space for
2695 the symbol in a common section but the XCOFF_DEF_REGULAR flag
2696 will not have been set. */
2697 if (h->root.type == bfd_link_hash_defined
2698 && (h->flags & XCOFF_DEF_REGULAR) == 0
2699 && (h->flags & XCOFF_REF_REGULAR) != 0
2700 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2701 && (bfd_is_abs_section (h->root.u.def.section)
2702 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
2703 h->flags |= XCOFF_DEF_REGULAR;
2705 /* If all defined symbols should be exported, mark them now. We
2706 don't want to export the actual functions, just the function
2707 descriptors. */
2708 if (ldinfo->export_defineds
2709 && (h->flags & XCOFF_DEF_REGULAR) != 0
2710 && h->root.root.string[0] != '.')
2712 bfd_boolean export;
2714 /* We don't export a symbol which is being defined by an object
2715 included from an archive which contains a shared object. The
2716 rationale is that if an archive contains both an unshared and
2717 a shared object, then there must be some reason that the
2718 unshared object is unshared, and we don't want to start
2719 providing a shared version of it. In particular, this solves
2720 a bug involving the _savefNN set of functions. gcc will call
2721 those functions without providing a slot to restore the TOC,
2722 so it is essential that these functions be linked in directly
2723 and not from a shared object, which means that a shared
2724 object which also happens to link them in must not export
2725 them. This is confusing, but I haven't been able to think of
2726 a different approach. Note that the symbols can, of course,
2727 be exported explicitly. */
2728 export = TRUE;
2729 if ((h->root.type == bfd_link_hash_defined
2730 || h->root.type == bfd_link_hash_defweak)
2731 && h->root.u.def.section->owner != NULL
2732 && h->root.u.def.section->owner->my_archive != NULL)
2734 bfd *arbfd, *member;
2736 arbfd = h->root.u.def.section->owner->my_archive;
2737 member = bfd_openr_next_archived_file (arbfd, NULL);
2738 while (member != NULL)
2740 if ((member->flags & DYNAMIC) != 0)
2742 export = FALSE;
2743 break;
2745 member = bfd_openr_next_archived_file (arbfd, member);
2749 if (export)
2750 h->flags |= XCOFF_EXPORT;
2753 /* We don't want to garbage collect symbols which are not defined in
2754 XCOFF files. This is a convenient place to mark them. */
2755 if (xcoff_hash_table (ldinfo->info)->gc
2756 && (h->flags & XCOFF_MARK) == 0
2757 && (h->root.type == bfd_link_hash_defined
2758 || h->root.type == bfd_link_hash_defweak)
2759 && (h->root.u.def.section->owner == NULL
2760 || (h->root.u.def.section->owner->xvec
2761 != ldinfo->info->hash->creator)))
2762 h->flags |= XCOFF_MARK;
2764 /* If this symbol is called and defined in a dynamic object, or it
2765 is imported, then we need to set up global linkage code for it.
2766 (Unless we did garbage collection and we didn't need this
2767 symbol.) */
2768 if ((h->flags & XCOFF_CALLED) != 0
2769 && (h->root.type == bfd_link_hash_undefined
2770 || h->root.type == bfd_link_hash_undefweak)
2771 && h->root.root.string[0] == '.'
2772 && h->descriptor != NULL
2773 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2774 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2775 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
2776 && (! xcoff_hash_table (ldinfo->info)->gc
2777 || (h->flags & XCOFF_MARK) != 0))
2779 asection *sec;
2780 struct xcoff_link_hash_entry *hds;
2782 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2783 h->root.type = bfd_link_hash_defined;
2784 h->root.u.def.section = sec;
2785 h->root.u.def.value = sec->size;
2786 h->smclas = XMC_GL;
2787 h->flags |= XCOFF_DEF_REGULAR;
2788 sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
2790 /* The global linkage code requires a TOC entry for the
2791 descriptor. */
2792 hds = h->descriptor;
2793 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2794 || hds->root.type == bfd_link_hash_undefweak)
2795 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2796 hds->flags |= XCOFF_MARK;
2797 if (hds->toc_section == NULL)
2799 int byte_size;
2801 /* 32 vs 64
2802 xcoff32 uses 4 bytes in the toc.
2803 xcoff64 uses 8 bytes in the toc. */
2804 if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
2805 byte_size = 8;
2806 else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
2807 byte_size = 4;
2808 else
2809 return FALSE;
2811 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2812 hds->u.toc_offset = hds->toc_section->size;
2813 hds->toc_section->size += byte_size;
2814 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2815 ++hds->toc_section->reloc_count;
2816 hds->indx = -2;
2817 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2819 /* We need to call xcoff_build_ldsyms recursively here,
2820 because we may already have passed hds on the traversal. */
2821 xcoff_build_ldsyms (hds, p);
2825 /* If this symbol is exported, but not defined, we need to try to
2826 define it. */
2827 if ((h->flags & XCOFF_EXPORT) != 0
2828 && (h->flags & XCOFF_IMPORT) == 0
2829 && (h->flags & XCOFF_DEF_REGULAR) == 0
2830 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2831 && (h->root.type == bfd_link_hash_undefined
2832 || h->root.type == bfd_link_hash_undefweak))
2834 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2835 && (h->descriptor->root.type == bfd_link_hash_defined
2836 || h->descriptor->root.type == bfd_link_hash_defweak))
2838 asection *sec;
2840 /* This is an undefined function descriptor associated with
2841 a defined entry point. We can build up a function
2842 descriptor ourselves. Believe it or not, the AIX linker
2843 actually does this, and there are cases where we need to
2844 do it as well. */
2845 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
2846 h->root.type = bfd_link_hash_defined;
2847 h->root.u.def.section = sec;
2848 h->root.u.def.value = sec->size;
2849 h->smclas = XMC_DS;
2850 h->flags |= XCOFF_DEF_REGULAR;
2852 /* The size of the function descriptor depends if this is an
2853 xcoff32 (12) or xcoff64 (24). */
2854 sec->size +=
2855 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
2857 /* A function descriptor uses two relocs: one for the
2858 associated code, and one for the TOC address. */
2859 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
2860 sec->reloc_count += 2;
2862 /* We handle writing out the contents of the descriptor in
2863 xcoff_write_global_symbol. */
2865 else
2867 (*_bfd_error_handler)
2868 (_("warning: attempt to export undefined symbol `%s'"),
2869 h->root.root.string);
2870 h->ldsym = NULL;
2871 return TRUE;
2875 /* If this is still a common symbol, and it wasn't garbage
2876 collected, we need to actually allocate space for it in the .bss
2877 section. */
2878 if (h->root.type == bfd_link_hash_common
2879 && (! xcoff_hash_table (ldinfo->info)->gc
2880 || (h->flags & XCOFF_MARK) != 0)
2881 && h->root.u.c.p->section->size == 0)
2883 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2884 h->root.u.c.p->section->size = h->root.u.c.size;
2887 /* We need to add a symbol to the .loader section if it is mentioned
2888 in a reloc which we are copying to the .loader section and it was
2889 not defined or common, or if it is the entry point, or if it is
2890 being exported. */
2892 if (((h->flags & XCOFF_LDREL) == 0
2893 || h->root.type == bfd_link_hash_defined
2894 || h->root.type == bfd_link_hash_defweak
2895 || h->root.type == bfd_link_hash_common)
2896 && (h->flags & XCOFF_ENTRY) == 0
2897 && (h->flags & XCOFF_EXPORT) == 0)
2899 h->ldsym = NULL;
2900 return TRUE;
2903 /* We don't need to add this symbol if we did garbage collection and
2904 we did not mark this symbol. */
2905 if (xcoff_hash_table (ldinfo->info)->gc
2906 && (h->flags & XCOFF_MARK) == 0)
2908 h->ldsym = NULL;
2909 return TRUE;
2912 /* We may have already processed this symbol due to the recursive
2913 call above. */
2914 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2915 return TRUE;
2917 /* We need to add this symbol to the .loader symbols. */
2919 BFD_ASSERT (h->ldsym == NULL);
2920 amt = sizeof (struct internal_ldsym);
2921 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
2922 if (h->ldsym == NULL)
2924 ldinfo->failed = TRUE;
2925 return FALSE;
2928 if ((h->flags & XCOFF_IMPORT) != 0)
2929 h->ldsym->l_ifile = h->ldindx;
2931 /* The first 3 symbol table indices are reserved to indicate the
2932 data, text and bss sections. */
2933 h->ldindx = ldinfo->ldsym_count + 3;
2935 ++ldinfo->ldsym_count;
2937 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
2938 h->ldsym, h->root.root.string))
2939 return FALSE;
2941 h->flags |= XCOFF_BUILT_LDSYM;
2943 return TRUE;
2945 /* Build the .loader section. This is called by the XCOFF linker
2946 emulation before_allocation routine. We must set the size of the
2947 .loader section before the linker lays out the output file.
2948 LIBPATH is the library path to search for shared objects; this is
2949 normally built from the -L arguments passed to the linker. ENTRY
2950 is the name of the entry point symbol (the -e linker option).
2951 FILE_ALIGN is the alignment to use for sections within the file
2952 (the -H linker option). MAXSTACK is the maximum stack size (the
2953 -bmaxstack linker option). MAXDATA is the maximum data size (the
2954 -bmaxdata linker option). GC is whether to do garbage collection
2955 (the -bgc linker option). MODTYPE is the module type (the
2956 -bmodtype linker option). TEXTRO is whether the text section must
2957 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2958 whether all defined symbols should be exported (the -unix linker
2959 option). SPECIAL_SECTIONS is set by this routine to csects with
2960 magic names like _end. */
2962 bfd_boolean
2963 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
2964 struct bfd_link_info *info,
2965 const char *libpath,
2966 const char *entry,
2967 unsigned long file_align,
2968 unsigned long maxstack,
2969 unsigned long maxdata,
2970 bfd_boolean gc,
2971 int modtype,
2972 bfd_boolean textro,
2973 bfd_boolean export_defineds,
2974 asection **special_sections,
2975 bfd_boolean rtld)
2977 struct xcoff_link_hash_entry *hentry;
2978 asection *lsec;
2979 struct xcoff_loader_info ldinfo;
2980 int i;
2981 size_t impsize, impcount;
2982 struct xcoff_import_file *fl;
2983 struct internal_ldhdr *ldhdr;
2984 bfd_size_type stoff;
2985 char *out;
2986 asection *sec;
2987 bfd *sub;
2988 struct bfd_strtab_hash *debug_strtab;
2989 bfd_byte *debug_contents = NULL;
2990 bfd_size_type amt;
2992 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2994 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2995 special_sections[i] = NULL;
2996 return TRUE;
2999 ldinfo.failed = FALSE;
3000 ldinfo.output_bfd = output_bfd;
3001 ldinfo.info = info;
3002 ldinfo.export_defineds = export_defineds;
3003 ldinfo.ldsym_count = 0;
3004 ldinfo.string_size = 0;
3005 ldinfo.strings = NULL;
3006 ldinfo.string_alc = 0;
3008 xcoff_data (output_bfd)->maxstack = maxstack;
3009 xcoff_data (output_bfd)->maxdata = maxdata;
3010 xcoff_data (output_bfd)->modtype = modtype;
3012 xcoff_hash_table (info)->file_align = file_align;
3013 xcoff_hash_table (info)->textro = textro;
3015 hentry = NULL;
3016 if (entry != NULL)
3018 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3019 FALSE, FALSE, TRUE);
3020 if (hentry != NULL)
3021 hentry->flags |= XCOFF_ENTRY;
3024 /* __rtinit */
3025 if (info->init_function || info->fini_function || rtld)
3027 struct xcoff_link_hash_entry *hsym;
3028 struct internal_ldsym *ldsym;
3030 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3031 "__rtinit", FALSE, FALSE, TRUE);
3032 if (hsym == NULL)
3034 (*_bfd_error_handler)
3035 (_("error: undefined symbol __rtinit"));
3036 return FALSE;
3039 xcoff_mark_symbol (info, hsym);
3040 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3042 /* __rtinit initialized. */
3043 amt = sizeof (* ldsym);
3044 ldsym = bfd_malloc (amt);
3046 ldsym->l_value = 0; /* Will be filled in later. */
3047 ldsym->l_scnum = 2; /* Data section. */
3048 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3049 ldsym->l_smclas = 5; /* .rw. */
3050 ldsym->l_ifile = 0; /* Special system loader symbol. */
3051 ldsym->l_parm = 0; /* NA. */
3053 /* Force __rtinit to be the first symbol in the loader symbol table
3054 See xcoff_build_ldsyms
3056 The first 3 symbol table indices are reserved to indicate the data,
3057 text and bss sections. */
3058 BFD_ASSERT (0 == ldinfo.ldsym_count);
3060 hsym->ldindx = 3;
3061 ldinfo.ldsym_count = 1;
3062 hsym->ldsym = ldsym;
3064 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3065 hsym->ldsym, hsym->root.root.string))
3066 return FALSE;
3068 /* This symbol is written out by xcoff_write_global_symbol
3069 Set stuff up so xcoff_write_global_symbol logic works. */
3070 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3071 hsym->root.type = bfd_link_hash_defined;
3072 hsym->root.u.def.value = 0;
3075 /* Garbage collect unused sections. */
3076 if (info->relocatable
3077 || ! gc
3078 || hentry == NULL
3079 || (hentry->root.type != bfd_link_hash_defined
3080 && hentry->root.type != bfd_link_hash_defweak))
3082 gc = FALSE;
3083 xcoff_hash_table (info)->gc = FALSE;
3085 /* We still need to call xcoff_mark, in order to set ldrel_count
3086 correctly. */
3087 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3089 asection *o;
3091 for (o = sub->sections; o != NULL; o = o->next)
3093 if ((o->flags & SEC_MARK) == 0)
3095 if (! xcoff_mark (info, o))
3096 goto error_return;
3101 else
3103 if (! xcoff_mark (info, hentry->root.u.def.section))
3104 goto error_return;
3105 xcoff_sweep (info);
3106 xcoff_hash_table (info)->gc = TRUE;
3109 /* Return special sections to the caller. */
3110 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3112 sec = xcoff_hash_table (info)->special_sections[i];
3114 if (sec != NULL
3115 && gc
3116 && (sec->flags & SEC_MARK) == 0)
3117 sec = NULL;
3119 special_sections[i] = sec;
3122 if (info->input_bfds == NULL)
3123 /* I'm not sure what to do in this bizarre case. */
3124 return TRUE;
3126 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3127 (void *) &ldinfo);
3128 if (ldinfo.failed)
3129 goto error_return;
3131 /* Work out the size of the import file names. Each import file ID
3132 consists of three null terminated strings: the path, the file
3133 name, and the archive member name. The first entry in the list
3134 of names is the path to use to find objects, which the linker has
3135 passed in as the libpath argument. For some reason, the path
3136 entry in the other import file names appears to always be empty. */
3137 impsize = strlen (libpath) + 3;
3138 impcount = 1;
3139 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3141 ++impcount;
3142 impsize += (strlen (fl->path)
3143 + strlen (fl->file)
3144 + strlen (fl->member)
3145 + 3);
3148 /* Set up the .loader section header. */
3149 ldhdr = &xcoff_hash_table (info)->ldhdr;
3150 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3151 ldhdr->l_nsyms = ldinfo.ldsym_count;
3152 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3153 ldhdr->l_istlen = impsize;
3154 ldhdr->l_nimpid = impcount;
3155 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3156 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3157 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3158 ldhdr->l_stlen = ldinfo.string_size;
3159 stoff = ldhdr->l_impoff + impsize;
3160 if (ldinfo.string_size == 0)
3161 ldhdr->l_stoff = 0;
3162 else
3163 ldhdr->l_stoff = stoff;
3165 /* 64 bit elements to ldhdr
3166 The swap out routine for 32 bit will ignore them.
3167 Nothing fancy, symbols come after the header and relocs come
3168 after symbols. */
3169 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3170 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3171 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3173 /* We now know the final size of the .loader section. Allocate
3174 space for it. */
3175 lsec = xcoff_hash_table (info)->loader_section;
3176 lsec->size = stoff + ldhdr->l_stlen;
3177 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3178 if (lsec->contents == NULL)
3179 goto error_return;
3181 /* Set up the header. */
3182 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3184 /* Set up the import file names. */
3185 out = (char *) lsec->contents + ldhdr->l_impoff;
3186 strcpy (out, libpath);
3187 out += strlen (libpath) + 1;
3188 *out++ = '\0';
3189 *out++ = '\0';
3190 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3192 const char *s;
3194 s = fl->path;
3195 while ((*out++ = *s++) != '\0')
3197 s = fl->file;
3198 while ((*out++ = *s++) != '\0')
3200 s = fl->member;
3201 while ((*out++ = *s++) != '\0')
3205 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3207 /* Set up the symbol string table. */
3208 if (ldinfo.string_size > 0)
3210 memcpy (out, ldinfo.strings, ldinfo.string_size);
3211 free (ldinfo.strings);
3212 ldinfo.strings = NULL;
3215 /* We can't set up the symbol table or the relocs yet, because we
3216 don't yet know the final position of the various sections. The
3217 .loader symbols are written out when the corresponding normal
3218 symbols are written out in xcoff_link_input_bfd or
3219 xcoff_write_global_symbol. The .loader relocs are written out
3220 when the corresponding normal relocs are handled in
3221 xcoff_link_input_bfd. */
3223 /* Allocate space for the magic sections. */
3224 sec = xcoff_hash_table (info)->linkage_section;
3225 if (sec->size > 0)
3227 sec->contents = bfd_zalloc (output_bfd, sec->size);
3228 if (sec->contents == NULL)
3229 goto error_return;
3231 sec = xcoff_hash_table (info)->toc_section;
3232 if (sec->size > 0)
3234 sec->contents = bfd_zalloc (output_bfd, sec->size);
3235 if (sec->contents == NULL)
3236 goto error_return;
3238 sec = xcoff_hash_table (info)->descriptor_section;
3239 if (sec->size > 0)
3241 sec->contents = bfd_zalloc (output_bfd, sec->size);
3242 if (sec->contents == NULL)
3243 goto error_return;
3246 /* Now that we've done garbage collection, figure out the contents
3247 of the .debug section. */
3248 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3250 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3252 asection *subdeb;
3253 bfd_size_type symcount;
3254 unsigned long *debug_index;
3255 asection **csectpp;
3256 bfd_byte *esym, *esymend;
3257 bfd_size_type symesz;
3259 if (sub->xvec != info->hash->creator)
3260 continue;
3261 subdeb = bfd_get_section_by_name (sub, ".debug");
3262 if (subdeb == NULL || subdeb->size == 0)
3263 continue;
3265 if (info->strip == strip_all
3266 || info->strip == strip_debugger
3267 || info->discard == discard_all)
3269 subdeb->size = 0;
3270 continue;
3273 if (! _bfd_coff_get_external_symbols (sub))
3274 goto error_return;
3276 symcount = obj_raw_syment_count (sub);
3277 debug_index = bfd_zalloc (sub, symcount * sizeof (unsigned long));
3278 if (debug_index == NULL)
3279 goto error_return;
3280 xcoff_data (sub)->debug_indices = debug_index;
3282 /* Grab the contents of the .debug section. We use malloc and
3283 copy the names into the debug stringtab, rather than
3284 bfd_alloc, because I expect that, when linking many files
3285 together, many of the strings will be the same. Storing the
3286 strings in the hash table should save space in this case. */
3287 if (! bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3288 goto error_return;
3290 csectpp = xcoff_data (sub)->csects;
3292 /* Dynamic object do not have csectpp's. */
3293 if (NULL != csectpp)
3295 symesz = bfd_coff_symesz (sub);
3296 esym = (bfd_byte *) obj_coff_external_syms (sub);
3297 esymend = esym + symcount * symesz;
3299 while (esym < esymend)
3301 struct internal_syment sym;
3303 bfd_coff_swap_sym_in (sub, (void *) esym, (void *) &sym);
3305 *debug_index = (unsigned long) -1;
3307 if (sym._n._n_n._n_zeroes == 0
3308 && *csectpp != NULL
3309 && (! gc
3310 || ((*csectpp)->flags & SEC_MARK) != 0
3311 || *csectpp == bfd_abs_section_ptr)
3312 && bfd_coff_symname_in_debug (sub, &sym))
3314 char *name;
3315 bfd_size_type indx;
3317 name = (char *) debug_contents + sym._n._n_n._n_offset;
3318 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3319 if (indx == (bfd_size_type) -1)
3320 goto error_return;
3321 *debug_index = indx;
3324 esym += (sym.n_numaux + 1) * symesz;
3325 csectpp += sym.n_numaux + 1;
3326 debug_index += sym.n_numaux + 1;
3330 free (debug_contents);
3331 debug_contents = NULL;
3333 /* Clear the size of subdeb, so that it is not included directly
3334 in the output file. */
3335 subdeb->size = 0;
3337 if (! info->keep_memory)
3339 if (! _bfd_coff_free_symbols (sub))
3340 goto error_return;
3344 if (info->strip != strip_all)
3345 xcoff_hash_table (info)->debug_section->size =
3346 _bfd_stringtab_size (debug_strtab);
3348 return TRUE;
3350 error_return:
3351 if (ldinfo.strings != NULL)
3352 free (ldinfo.strings);
3353 if (debug_contents != NULL)
3354 free (debug_contents);
3355 return FALSE;
3358 bfd_boolean
3359 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3360 const char *init,
3361 const char *fini,
3362 bfd_boolean rtld)
3364 struct bfd_in_memory *bim;
3366 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3367 if (bim == NULL)
3368 return FALSE;
3370 bim->size = 0;
3371 bim->buffer = 0;
3373 abfd->link_next = 0;
3374 abfd->format = bfd_object;
3375 abfd->iostream = (void *) bim;
3376 abfd->flags = BFD_IN_MEMORY;
3377 abfd->direction = write_direction;
3378 abfd->where = 0;
3380 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3381 return FALSE;
3383 /* need to reset to unknown or it will not be read back in correctly */
3384 abfd->format = bfd_unknown;
3385 abfd->direction = read_direction;
3386 abfd->where = 0;
3388 return TRUE;
3391 /* Link an input file into the linker output file. This function
3392 handles all the sections and relocations of the input file at once. */
3394 static bfd_boolean
3395 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3396 bfd *input_bfd)
3398 bfd *output_bfd;
3399 const char *strings;
3400 bfd_size_type syment_base;
3401 unsigned int n_tmask;
3402 unsigned int n_btshft;
3403 bfd_boolean copy, hash;
3404 bfd_size_type isymesz;
3405 bfd_size_type osymesz;
3406 bfd_size_type linesz;
3407 bfd_byte *esym;
3408 bfd_byte *esym_end;
3409 struct xcoff_link_hash_entry **sym_hash;
3410 struct internal_syment *isymp;
3411 asection **csectpp;
3412 unsigned long *debug_index;
3413 long *indexp;
3414 unsigned long output_index;
3415 bfd_byte *outsym;
3416 unsigned int incls;
3417 asection *oline;
3418 bfd_boolean keep_syms;
3419 asection *o;
3421 /* We can just skip DYNAMIC files, unless this is a static link. */
3422 if ((input_bfd->flags & DYNAMIC) != 0
3423 && ! finfo->info->static_link)
3424 return TRUE;
3426 /* Move all the symbols to the output file. */
3427 output_bfd = finfo->output_bfd;
3428 strings = NULL;
3429 syment_base = obj_raw_syment_count (output_bfd);
3430 isymesz = bfd_coff_symesz (input_bfd);
3431 osymesz = bfd_coff_symesz (output_bfd);
3432 linesz = bfd_coff_linesz (input_bfd);
3433 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3435 n_tmask = coff_data (input_bfd)->local_n_tmask;
3436 n_btshft = coff_data (input_bfd)->local_n_btshft;
3438 /* Define macros so that ISFCN, et. al., macros work correctly. */
3439 #define N_TMASK n_tmask
3440 #define N_BTSHFT n_btshft
3442 copy = FALSE;
3443 if (! finfo->info->keep_memory)
3444 copy = TRUE;
3445 hash = TRUE;
3446 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3447 hash = FALSE;
3449 if (! _bfd_coff_get_external_symbols (input_bfd))
3450 return FALSE;
3452 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3453 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3454 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3455 csectpp = xcoff_data (input_bfd)->csects;
3456 debug_index = xcoff_data (input_bfd)->debug_indices;
3457 isymp = finfo->internal_syms;
3458 indexp = finfo->sym_indices;
3459 output_index = syment_base;
3460 outsym = finfo->outsyms;
3461 incls = 0;
3462 oline = NULL;
3464 while (esym < esym_end)
3466 struct internal_syment isym;
3467 union internal_auxent aux;
3468 int smtyp = 0;
3469 bfd_boolean skip;
3470 bfd_boolean require;
3471 int add;
3473 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3475 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3476 information. */
3477 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3479 BFD_ASSERT (isymp->n_numaux > 0);
3480 bfd_coff_swap_aux_in (input_bfd,
3481 (void *) (esym + isymesz * isymp->n_numaux),
3482 isymp->n_type, isymp->n_sclass,
3483 isymp->n_numaux - 1, isymp->n_numaux,
3484 (void *) &aux);
3486 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3489 /* Make a copy of *isymp so that the relocate_section function
3490 always sees the original values. This is more reliable than
3491 always recomputing the symbol value even if we are stripping
3492 the symbol. */
3493 isym = *isymp;
3495 /* If this symbol is in the .loader section, swap out the
3496 .loader symbol information. If this is an external symbol
3497 reference to a defined symbol, though, then wait until we get
3498 to the definition. */
3499 if (isym.n_sclass == C_EXT
3500 && *sym_hash != NULL
3501 && (*sym_hash)->ldsym != NULL
3502 && (smtyp != XTY_ER
3503 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3505 struct xcoff_link_hash_entry *h;
3506 struct internal_ldsym *ldsym;
3508 h = *sym_hash;
3509 ldsym = h->ldsym;
3510 if (isym.n_scnum > 0)
3512 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3513 ldsym->l_value = (isym.n_value
3514 + (*csectpp)->output_section->vma
3515 + (*csectpp)->output_offset
3516 - (*csectpp)->vma);
3518 else
3520 ldsym->l_scnum = isym.n_scnum;
3521 ldsym->l_value = isym.n_value;
3524 ldsym->l_smtype = smtyp;
3525 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3526 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3527 || (h->flags & XCOFF_IMPORT) != 0)
3528 ldsym->l_smtype |= L_IMPORT;
3529 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3530 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3531 || (h->flags & XCOFF_EXPORT) != 0)
3532 ldsym->l_smtype |= L_EXPORT;
3533 if ((h->flags & XCOFF_ENTRY) != 0)
3534 ldsym->l_smtype |= L_ENTRY;
3536 ldsym->l_smclas = aux.x_csect.x_smclas;
3538 if (ldsym->l_ifile == (bfd_size_type) -1)
3539 ldsym->l_ifile = 0;
3540 else if (ldsym->l_ifile == 0)
3542 if ((ldsym->l_smtype & L_IMPORT) == 0)
3543 ldsym->l_ifile = 0;
3544 else
3546 bfd *impbfd;
3548 if (h->root.type == bfd_link_hash_defined
3549 || h->root.type == bfd_link_hash_defweak)
3550 impbfd = h->root.u.def.section->owner;
3551 else if (h->root.type == bfd_link_hash_undefined
3552 || h->root.type == bfd_link_hash_undefweak)
3553 impbfd = h->root.u.undef.abfd;
3554 else
3555 impbfd = NULL;
3557 if (impbfd == NULL)
3558 ldsym->l_ifile = 0;
3559 else
3561 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3562 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3567 ldsym->l_parm = 0;
3569 BFD_ASSERT (h->ldindx >= 0);
3570 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3571 (finfo->ldsym
3572 + ((h->ldindx - 3)
3573 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3574 h->ldsym = NULL;
3576 /* Fill in snentry now that we know the target_index. */
3577 if ((h->flags & XCOFF_ENTRY) != 0
3578 && (h->root.type == bfd_link_hash_defined
3579 || h->root.type == bfd_link_hash_defweak))
3581 xcoff_data (output_bfd)->snentry =
3582 h->root.u.def.section->output_section->target_index;
3586 *indexp = -1;
3588 skip = FALSE;
3589 require = FALSE;
3590 add = 1 + isym.n_numaux;
3592 /* If we are skipping this csect, we want to skip this symbol. */
3593 if (*csectpp == NULL)
3594 skip = TRUE;
3596 /* If we garbage collected this csect, we want to skip this
3597 symbol. */
3598 if (! skip
3599 && xcoff_hash_table (finfo->info)->gc
3600 && ((*csectpp)->flags & SEC_MARK) == 0
3601 && *csectpp != bfd_abs_section_ptr)
3602 skip = TRUE;
3604 /* An XCOFF linker always skips C_STAT symbols. */
3605 if (! skip
3606 && isymp->n_sclass == C_STAT)
3607 skip = TRUE;
3609 /* We skip all but the first TOC anchor. */
3610 if (! skip
3611 && isymp->n_sclass == C_HIDEXT
3612 && aux.x_csect.x_smclas == XMC_TC0)
3614 if (finfo->toc_symindx != -1)
3615 skip = TRUE;
3616 else
3618 bfd_vma tocval, tocend;
3619 bfd *inp;
3621 tocval = ((*csectpp)->output_section->vma
3622 + (*csectpp)->output_offset
3623 + isym.n_value
3624 - (*csectpp)->vma);
3626 /* We want to find out if tocval is a good value to use
3627 as the TOC anchor--that is, whether we can access all
3628 of the TOC using a 16 bit offset from tocval. This
3629 test assumes that the TOC comes at the end of the
3630 output section, as it does in the default linker
3631 script. */
3632 tocend = ((*csectpp)->output_section->vma
3633 + (*csectpp)->output_section->size);
3634 for (inp = finfo->info->input_bfds;
3635 inp != NULL;
3636 inp = inp->link_next)
3639 for (o = inp->sections; o != NULL; o = o->next)
3640 if (strcmp (o->name, ".tocbss") == 0)
3642 bfd_vma new_toc_end;
3643 new_toc_end = (o->output_section->vma
3644 + o->output_offset
3645 + o->size);
3646 if (new_toc_end > tocend)
3647 tocend = new_toc_end;
3652 if (tocval + 0x10000 < tocend)
3654 (*_bfd_error_handler)
3655 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
3656 (unsigned long) (tocend - tocval));
3657 bfd_set_error (bfd_error_file_too_big);
3658 return FALSE;
3661 if (tocval + 0x8000 < tocend)
3663 bfd_vma tocadd;
3665 tocadd = tocend - (tocval + 0x8000);
3666 tocval += tocadd;
3667 isym.n_value += tocadd;
3670 finfo->toc_symindx = output_index;
3671 xcoff_data (finfo->output_bfd)->toc = tocval;
3672 xcoff_data (finfo->output_bfd)->sntoc =
3673 (*csectpp)->output_section->target_index;
3674 require = TRUE;
3679 /* If we are stripping all symbols, we want to skip this one. */
3680 if (! skip
3681 && finfo->info->strip == strip_all)
3682 skip = TRUE;
3684 /* We can skip resolved external references. */
3685 if (! skip
3686 && isym.n_sclass == C_EXT
3687 && smtyp == XTY_ER
3688 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3689 skip = TRUE;
3691 /* We can skip common symbols if they got defined somewhere
3692 else. */
3693 if (! skip
3694 && isym.n_sclass == C_EXT
3695 && smtyp == XTY_CM
3696 && ((*sym_hash)->root.type != bfd_link_hash_common
3697 || (*sym_hash)->root.u.c.p->section != *csectpp)
3698 && ((*sym_hash)->root.type != bfd_link_hash_defined
3699 || (*sym_hash)->root.u.def.section != *csectpp))
3700 skip = TRUE;
3702 /* Skip local symbols if we are discarding them. */
3703 if (! skip
3704 && finfo->info->discard == discard_all
3705 && isym.n_sclass != C_EXT
3706 && (isym.n_sclass != C_HIDEXT
3707 || smtyp != XTY_SD))
3708 skip = TRUE;
3710 /* If we stripping debugging symbols, and this is a debugging
3711 symbol, then skip it. */
3712 if (! skip
3713 && finfo->info->strip == strip_debugger
3714 && isym.n_scnum == N_DEBUG)
3715 skip = TRUE;
3717 /* If some symbols are stripped based on the name, work out the
3718 name and decide whether to skip this symbol. We don't handle
3719 this correctly for symbols whose names are in the .debug
3720 section; to get it right we would need a new bfd_strtab_hash
3721 function to return the string given the index. */
3722 if (! skip
3723 && (finfo->info->strip == strip_some
3724 || finfo->info->discard == discard_l)
3725 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3727 const char *name;
3728 char buf[SYMNMLEN + 1];
3730 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3732 if (name == NULL)
3733 return FALSE;
3735 if ((finfo->info->strip == strip_some
3736 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
3737 FALSE) == NULL))
3738 || (finfo->info->discard == discard_l
3739 && (isym.n_sclass != C_EXT
3740 && (isym.n_sclass != C_HIDEXT
3741 || smtyp != XTY_SD))
3742 && bfd_is_local_label_name (input_bfd, name)))
3743 skip = TRUE;
3746 /* We can not skip the first TOC anchor. */
3747 if (skip
3748 && require
3749 && finfo->info->strip != strip_all)
3750 skip = FALSE;
3752 /* We now know whether we are to skip this symbol or not. */
3753 if (! skip)
3755 /* Adjust the symbol in order to output it. */
3757 if (isym._n._n_n._n_zeroes == 0
3758 && isym._n._n_n._n_offset != 0)
3760 /* This symbol has a long name. Enter it in the string
3761 table we are building. If *debug_index != -1, the
3762 name has already been entered in the .debug section. */
3763 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3764 isym._n._n_n._n_offset = *debug_index;
3765 else
3767 const char *name;
3768 bfd_size_type indx;
3770 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
3772 if (name == NULL)
3773 return FALSE;
3774 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3775 if (indx == (bfd_size_type) -1)
3776 return FALSE;
3777 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3781 if (isym.n_sclass != C_BSTAT
3782 && isym.n_sclass != C_ESTAT
3783 && isym.n_sclass != C_DECL
3784 && isym.n_scnum > 0)
3786 isym.n_scnum = (*csectpp)->output_section->target_index;
3787 isym.n_value += ((*csectpp)->output_section->vma
3788 + (*csectpp)->output_offset
3789 - (*csectpp)->vma);
3792 /* The value of a C_FILE symbol is the symbol index of the
3793 next C_FILE symbol. The value of the last C_FILE symbol
3794 is -1. We try to get this right, below, just before we
3795 write the symbols out, but in the general case we may
3796 have to write the symbol out twice. */
3797 if (isym.n_sclass == C_FILE)
3799 if (finfo->last_file_index != -1
3800 && finfo->last_file.n_value != (bfd_vma) output_index)
3802 /* We must correct the value of the last C_FILE entry. */
3803 finfo->last_file.n_value = output_index;
3804 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3806 /* The last C_FILE symbol is in this input file. */
3807 bfd_coff_swap_sym_out (output_bfd,
3808 (void *) &finfo->last_file,
3809 (void *) (finfo->outsyms
3810 + ((finfo->last_file_index
3811 - syment_base)
3812 * osymesz)));
3814 else
3816 /* We have already written out the last C_FILE
3817 symbol. We need to write it out again. We
3818 borrow *outsym temporarily. */
3819 file_ptr pos;
3821 bfd_coff_swap_sym_out (output_bfd,
3822 (void *) &finfo->last_file,
3823 (void *) outsym);
3825 pos = obj_sym_filepos (output_bfd);
3826 pos += finfo->last_file_index * osymesz;
3827 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3828 || (bfd_bwrite (outsym, osymesz, output_bfd)
3829 != osymesz))
3830 return FALSE;
3834 finfo->last_file_index = output_index;
3835 finfo->last_file = isym;
3838 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3839 into the line numbers. We update the symbol values when
3840 we handle the line numbers. */
3841 if (isym.n_sclass == C_BINCL
3842 || isym.n_sclass == C_EINCL)
3844 isym.n_value = finfo->line_filepos;
3845 ++incls;
3848 /* Output the symbol. */
3850 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
3852 *indexp = output_index;
3854 if (isym.n_sclass == C_EXT)
3856 long indx;
3857 struct xcoff_link_hash_entry *h;
3859 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3860 / isymesz);
3861 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3862 BFD_ASSERT (h != NULL);
3863 h->indx = output_index;
3866 /* If this is a symbol in the TOC which we may have merged
3867 (class XMC_TC), remember the symbol index of the TOC
3868 symbol. */
3869 if (isym.n_sclass == C_HIDEXT
3870 && aux.x_csect.x_smclas == XMC_TC
3871 && *sym_hash != NULL)
3873 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3874 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3875 (*sym_hash)->u.toc_indx = output_index;
3878 output_index += add;
3879 outsym += add * osymesz;
3882 esym += add * isymesz;
3883 isymp += add;
3884 csectpp += add;
3885 sym_hash += add;
3886 if (debug_index != NULL)
3887 debug_index += add;
3888 ++indexp;
3889 for (--add; add > 0; --add)
3890 *indexp++ = -1;
3893 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3894 done in a separate pass, because we don't know the correct symbol
3895 indices until we have already decided which symbols we are going
3896 to keep. */
3898 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3899 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3900 isymp = finfo->internal_syms;
3901 indexp = finfo->sym_indices;
3902 csectpp = xcoff_data (input_bfd)->csects;
3903 outsym = finfo->outsyms;
3904 while (esym < esym_end)
3906 int add;
3908 add = 1 + isymp->n_numaux;
3910 if (*indexp < 0)
3911 esym += add * isymesz;
3912 else
3914 int i;
3916 if (isymp->n_sclass == C_BSTAT)
3918 struct internal_syment isym;
3920 bfd_vma indx;
3922 /* The value of a C_BSTAT symbol is the symbol table
3923 index of the containing csect. */
3924 bfd_coff_swap_sym_in (output_bfd, (void *) outsym, (void *) &isym);
3925 indx = isym.n_value;
3926 if (indx < obj_raw_syment_count (input_bfd))
3928 long symindx;
3930 symindx = finfo->sym_indices[indx];
3931 if (symindx < 0)
3932 isym.n_value = 0;
3933 else
3934 isym.n_value = symindx;
3935 bfd_coff_swap_sym_out (output_bfd, (void *) &isym,
3936 (void *) outsym);
3940 esym += isymesz;
3941 outsym += osymesz;
3943 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3945 union internal_auxent aux;
3947 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
3948 isymp->n_sclass, i, isymp->n_numaux,
3949 (void *) &aux);
3951 if (isymp->n_sclass == C_FILE)
3953 /* This is the file name (or some comment put in by
3954 the compiler). If it is long, we must put it in
3955 the string table. */
3956 if (aux.x_file.x_n.x_zeroes == 0
3957 && aux.x_file.x_n.x_offset != 0)
3959 const char *filename;
3960 bfd_size_type indx;
3962 BFD_ASSERT (aux.x_file.x_n.x_offset
3963 >= STRING_SIZE_SIZE);
3964 if (strings == NULL)
3966 strings = _bfd_coff_read_string_table (input_bfd);
3967 if (strings == NULL)
3968 return FALSE;
3970 filename = strings + aux.x_file.x_n.x_offset;
3971 indx = _bfd_stringtab_add (finfo->strtab, filename,
3972 hash, copy);
3973 if (indx == (bfd_size_type) -1)
3974 return FALSE;
3975 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3978 else if ((isymp->n_sclass == C_EXT
3979 || isymp->n_sclass == C_HIDEXT)
3980 && i + 1 == isymp->n_numaux)
3983 /* We don't support type checking. I don't know if
3984 anybody does. */
3985 aux.x_csect.x_parmhash = 0;
3986 /* I don't think anybody uses these fields, but we'd
3987 better clobber them just in case. */
3988 aux.x_csect.x_stab = 0;
3989 aux.x_csect.x_snstab = 0;
3991 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
3993 unsigned long indx;
3995 indx = aux.x_csect.x_scnlen.l;
3996 if (indx < obj_raw_syment_count (input_bfd))
3998 long symindx;
4000 symindx = finfo->sym_indices[indx];
4001 if (symindx < 0)
4003 aux.x_csect.x_scnlen.l = 0;
4005 else
4007 aux.x_csect.x_scnlen.l = symindx;
4012 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4014 unsigned long indx;
4016 if (ISFCN (isymp->n_type)
4017 || ISTAG (isymp->n_sclass)
4018 || isymp->n_sclass == C_BLOCK
4019 || isymp->n_sclass == C_FCN)
4021 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4022 if (indx > 0
4023 && indx < obj_raw_syment_count (input_bfd))
4025 /* We look forward through the symbol for
4026 the index of the next symbol we are going
4027 to include. I don't know if this is
4028 entirely right. */
4029 while (finfo->sym_indices[indx] < 0
4030 && indx < obj_raw_syment_count (input_bfd))
4031 ++indx;
4032 if (indx >= obj_raw_syment_count (input_bfd))
4033 indx = output_index;
4034 else
4035 indx = finfo->sym_indices[indx];
4036 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4041 indx = aux.x_sym.x_tagndx.l;
4042 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4044 long symindx;
4046 symindx = finfo->sym_indices[indx];
4047 if (symindx < 0)
4048 aux.x_sym.x_tagndx.l = 0;
4049 else
4050 aux.x_sym.x_tagndx.l = symindx;
4055 /* Copy over the line numbers, unless we are stripping
4056 them. We do this on a symbol by symbol basis in
4057 order to more easily handle garbage collection. */
4058 if ((isymp->n_sclass == C_EXT
4059 || isymp->n_sclass == C_HIDEXT)
4060 && i == 0
4061 && isymp->n_numaux > 1
4062 && ISFCN (isymp->n_type)
4063 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4065 if (finfo->info->strip != strip_none
4066 && finfo->info->strip != strip_some)
4067 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4068 else
4070 asection *enclosing;
4071 unsigned int enc_count;
4072 bfd_signed_vma linoff;
4073 struct internal_lineno lin;
4075 o = *csectpp;
4076 enclosing = xcoff_section_data (abfd, o)->enclosing;
4077 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4078 if (oline != enclosing)
4080 file_ptr pos = enclosing->line_filepos;
4081 bfd_size_type amt = linesz * enc_count;
4082 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4083 || (bfd_bread (finfo->linenos, amt, input_bfd)
4084 != amt))
4085 return FALSE;
4086 oline = enclosing;
4089 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4090 - enclosing->line_filepos);
4092 bfd_coff_swap_lineno_in (input_bfd,
4093 (void *) (finfo->linenos + linoff),
4094 (void *) &lin);
4095 if (lin.l_lnno != 0
4096 || ((bfd_size_type) lin.l_addr.l_symndx
4097 != ((esym
4098 - isymesz
4099 - ((bfd_byte *)
4100 obj_coff_external_syms (input_bfd)))
4101 / isymesz)))
4102 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4103 else
4105 bfd_byte *linpend, *linp;
4106 bfd_vma offset;
4107 bfd_size_type count;
4109 lin.l_addr.l_symndx = *indexp;
4110 bfd_coff_swap_lineno_out (output_bfd, (void *) &lin,
4111 (void *) (finfo->linenos
4112 + linoff));
4114 linpend = (finfo->linenos
4115 + enc_count * linesz);
4116 offset = (o->output_section->vma
4117 + o->output_offset
4118 - o->vma);
4119 for (linp = finfo->linenos + linoff + linesz;
4120 linp < linpend;
4121 linp += linesz)
4123 bfd_coff_swap_lineno_in (input_bfd, (void *) linp,
4124 (void *) &lin);
4125 if (lin.l_lnno == 0)
4126 break;
4127 lin.l_addr.l_paddr += offset;
4128 bfd_coff_swap_lineno_out (output_bfd,
4129 (void *) &lin,
4130 (void *) linp);
4133 count = (linp - (finfo->linenos + linoff)) / linesz;
4135 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4136 (o->output_section->line_filepos
4137 + o->output_section->lineno_count * linesz);
4139 if (bfd_seek (output_bfd,
4140 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4141 SEEK_SET) != 0
4142 || (bfd_bwrite (finfo->linenos + linoff,
4143 linesz * count, output_bfd)
4144 != linesz * count))
4145 return FALSE;
4147 o->output_section->lineno_count += count;
4149 if (incls > 0)
4151 struct internal_syment *iisp, *iispend;
4152 long *iindp;
4153 bfd_byte *oos;
4154 int iiadd;
4156 /* Update any C_BINCL or C_EINCL symbols
4157 that refer to a line number in the
4158 range we just output. */
4159 iisp = finfo->internal_syms;
4160 iispend = (iisp
4161 + obj_raw_syment_count (input_bfd));
4162 iindp = finfo->sym_indices;
4163 oos = finfo->outsyms;
4164 while (iisp < iispend)
4166 if (*iindp >= 0
4167 && (iisp->n_sclass == C_BINCL
4168 || iisp->n_sclass == C_EINCL)
4169 && ((bfd_size_type) iisp->n_value
4170 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4171 && ((bfd_size_type) iisp->n_value
4172 < (enclosing->line_filepos
4173 + enc_count * linesz)))
4175 struct internal_syment iis;
4177 bfd_coff_swap_sym_in (output_bfd,
4178 (void *) oos,
4179 (void *) &iis);
4180 iis.n_value =
4181 (iisp->n_value
4182 - enclosing->line_filepos
4183 - linoff
4184 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4185 bfd_coff_swap_sym_out (output_bfd,
4186 (void *) &iis,
4187 (void *) oos);
4188 --incls;
4191 iiadd = 1 + iisp->n_numaux;
4192 if (*iindp >= 0)
4193 oos += iiadd * osymesz;
4194 iisp += iiadd;
4195 iindp += iiadd;
4202 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4203 isymp->n_sclass, i, isymp->n_numaux,
4204 (void *) outsym);
4205 outsym += osymesz;
4206 esym += isymesz;
4210 indexp += add;
4211 isymp += add;
4212 csectpp += add;
4215 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4216 symbol will be the first symbol in the next input file. In the
4217 normal case, this will save us from writing out the C_FILE symbol
4218 again. */
4219 if (finfo->last_file_index != -1
4220 && (bfd_size_type) finfo->last_file_index >= syment_base)
4222 finfo->last_file.n_value = output_index;
4223 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4224 (void *) (finfo->outsyms
4225 + ((finfo->last_file_index - syment_base)
4226 * osymesz)));
4229 /* Write the modified symbols to the output file. */
4230 if (outsym > finfo->outsyms)
4232 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4233 bfd_size_type amt = outsym - finfo->outsyms;
4234 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4235 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4236 return FALSE;
4238 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4239 + (outsym - finfo->outsyms) / osymesz)
4240 == output_index);
4242 obj_raw_syment_count (output_bfd) = output_index;
4245 /* Don't let the linker relocation routines discard the symbols. */
4246 keep_syms = obj_coff_keep_syms (input_bfd);
4247 obj_coff_keep_syms (input_bfd) = TRUE;
4249 /* Relocate the contents of each section. */
4250 for (o = input_bfd->sections; o != NULL; o = o->next)
4252 bfd_byte *contents;
4254 if (! o->linker_mark)
4255 /* This section was omitted from the link. */
4256 continue;
4258 if ((o->flags & SEC_HAS_CONTENTS) == 0
4259 || o->size == 0
4260 || (o->flags & SEC_IN_MEMORY) != 0)
4261 continue;
4263 /* We have set filepos correctly for the sections we created to
4264 represent csects, so bfd_get_section_contents should work. */
4265 if (coff_section_data (input_bfd, o) != NULL
4266 && coff_section_data (input_bfd, o)->contents != NULL)
4267 contents = coff_section_data (input_bfd, o)->contents;
4268 else
4270 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4271 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4272 return FALSE;
4273 contents = finfo->contents;
4276 if ((o->flags & SEC_RELOC) != 0)
4278 int target_index;
4279 struct internal_reloc *internal_relocs;
4280 struct internal_reloc *irel;
4281 bfd_vma offset;
4282 struct internal_reloc *irelend;
4283 struct xcoff_link_hash_entry **rel_hash;
4284 long r_symndx;
4286 /* Read in the relocs. */
4287 target_index = o->output_section->target_index;
4288 internal_relocs = (xcoff_read_internal_relocs
4289 (input_bfd, o, FALSE, finfo->external_relocs,
4290 TRUE,
4291 (finfo->section_info[target_index].relocs
4292 + o->output_section->reloc_count)));
4293 if (internal_relocs == NULL)
4294 return FALSE;
4296 /* Call processor specific code to relocate the section
4297 contents. */
4298 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4299 input_bfd, o,
4300 contents,
4301 internal_relocs,
4302 finfo->internal_syms,
4303 xcoff_data (input_bfd)->csects))
4304 return FALSE;
4306 offset = o->output_section->vma + o->output_offset - o->vma;
4307 irel = internal_relocs;
4308 irelend = irel + o->reloc_count;
4309 rel_hash = (finfo->section_info[target_index].rel_hashes
4310 + o->output_section->reloc_count);
4311 for (; irel < irelend; irel++, rel_hash++)
4313 struct xcoff_link_hash_entry *h = NULL;
4314 struct internal_ldrel ldrel;
4315 bfd_boolean quiet;
4317 *rel_hash = NULL;
4319 /* Adjust the reloc address and symbol index. */
4321 irel->r_vaddr += offset;
4323 r_symndx = irel->r_symndx;
4325 if (r_symndx == -1)
4326 h = NULL;
4327 else
4328 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4330 if (r_symndx != -1 && finfo->info->strip != strip_all)
4332 if (h != NULL
4333 && h->smclas != XMC_TD
4334 && (irel->r_type == R_TOC
4335 || irel->r_type == R_GL
4336 || irel->r_type == R_TCL
4337 || irel->r_type == R_TRL
4338 || irel->r_type == R_TRLA))
4340 /* This is a TOC relative reloc with a symbol
4341 attached. The symbol should be the one which
4342 this reloc is for. We want to make this
4343 reloc against the TOC address of the symbol,
4344 not the symbol itself. */
4345 BFD_ASSERT (h->toc_section != NULL);
4346 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4347 if (h->u.toc_indx != -1)
4348 irel->r_symndx = h->u.toc_indx;
4349 else
4351 struct xcoff_toc_rel_hash *n;
4352 struct xcoff_link_section_info *si;
4353 bfd_size_type amt;
4355 amt = sizeof (* n);
4356 n = bfd_alloc (finfo->output_bfd, amt);
4357 if (n == NULL)
4358 return FALSE;
4359 si = finfo->section_info + target_index;
4360 n->next = si->toc_rel_hashes;
4361 n->h = h;
4362 n->rel = irel;
4363 si->toc_rel_hashes = n;
4366 else if (h != NULL)
4368 /* This is a global symbol. */
4369 if (h->indx >= 0)
4370 irel->r_symndx = h->indx;
4371 else
4373 /* This symbol is being written at the end
4374 of the file, and we do not yet know the
4375 symbol index. We save the pointer to the
4376 hash table entry in the rel_hash list.
4377 We set the indx field to -2 to indicate
4378 that this symbol must not be stripped. */
4379 *rel_hash = h;
4380 h->indx = -2;
4383 else
4385 long indx;
4387 indx = finfo->sym_indices[r_symndx];
4389 if (indx == -1)
4391 struct internal_syment *is;
4393 /* Relocations against a TC0 TOC anchor are
4394 automatically transformed to be against
4395 the TOC anchor in the output file. */
4396 is = finfo->internal_syms + r_symndx;
4397 if (is->n_sclass == C_HIDEXT
4398 && is->n_numaux > 0)
4400 void * auxptr;
4401 union internal_auxent aux;
4403 auxptr = ((void *)
4404 (((bfd_byte *)
4405 obj_coff_external_syms (input_bfd))
4406 + ((r_symndx + is->n_numaux)
4407 * isymesz)));
4408 bfd_coff_swap_aux_in (input_bfd, auxptr,
4409 is->n_type, is->n_sclass,
4410 is->n_numaux - 1,
4411 is->n_numaux,
4412 (void *) &aux);
4413 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4414 && aux.x_csect.x_smclas == XMC_TC0)
4415 indx = finfo->toc_symindx;
4419 if (indx != -1)
4420 irel->r_symndx = indx;
4421 else
4424 struct internal_syment *is;
4426 const char *name;
4427 char buf[SYMNMLEN + 1];
4429 /* This reloc is against a symbol we are
4430 stripping. It would be possible to handle
4431 this case, but I don't think it's worth it. */
4432 is = finfo->internal_syms + r_symndx;
4434 name = (_bfd_coff_internal_syment_name
4435 (input_bfd, is, buf));
4437 if (name == NULL)
4438 return FALSE;
4440 if (! ((*finfo->info->callbacks->unattached_reloc)
4441 (finfo->info, name, input_bfd, o,
4442 irel->r_vaddr)))
4443 return FALSE;
4448 quiet = FALSE;
4449 switch (irel->r_type)
4451 default:
4452 if (h == NULL
4453 || h->root.type == bfd_link_hash_defined
4454 || h->root.type == bfd_link_hash_defweak
4455 || h->root.type == bfd_link_hash_common)
4456 break;
4457 /* Fall through. */
4458 case R_POS:
4459 case R_NEG:
4460 case R_RL:
4461 case R_RLA:
4462 /* This reloc needs to be copied into the .loader
4463 section. */
4464 ldrel.l_vaddr = irel->r_vaddr;
4465 if (r_symndx == -1)
4466 ldrel.l_symndx = -(bfd_size_type ) 1;
4467 else if (h == NULL
4468 || (h->root.type == bfd_link_hash_defined
4469 || h->root.type == bfd_link_hash_defweak
4470 || h->root.type == bfd_link_hash_common))
4472 asection *sec;
4474 if (h == NULL)
4475 sec = xcoff_data (input_bfd)->csects[r_symndx];
4476 else if (h->root.type == bfd_link_hash_common)
4477 sec = h->root.u.c.p->section;
4478 else
4479 sec = h->root.u.def.section;
4480 sec = sec->output_section;
4482 if (strcmp (sec->name, ".text") == 0)
4483 ldrel.l_symndx = 0;
4484 else if (strcmp (sec->name, ".data") == 0)
4485 ldrel.l_symndx = 1;
4486 else if (strcmp (sec->name, ".bss") == 0)
4487 ldrel.l_symndx = 2;
4488 else
4490 (*_bfd_error_handler)
4491 (_("%B: loader reloc in unrecognized section `%A'"),
4492 input_bfd, sec);
4493 bfd_set_error (bfd_error_nonrepresentable_section);
4494 return FALSE;
4497 else
4499 if (! finfo->info->relocatable
4500 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
4501 && (h->flags & XCOFF_IMPORT) == 0)
4503 /* We already called the undefined_symbol
4504 callback for this relocation, in
4505 _bfd_ppc_xcoff_relocate_section. Don't
4506 issue any more warnings. */
4507 quiet = TRUE;
4509 if (h->ldindx < 0 && ! quiet)
4511 (*_bfd_error_handler)
4512 (_("%B: `%s' in loader reloc but not loader sym"),
4513 input_bfd,
4514 h->root.root.string);
4515 bfd_set_error (bfd_error_bad_value);
4516 return FALSE;
4518 ldrel.l_symndx = h->ldindx;
4520 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4521 ldrel.l_rsecnm = o->output_section->target_index;
4522 if (xcoff_hash_table (finfo->info)->textro
4523 && strcmp (o->output_section->name, ".text") == 0
4524 && ! quiet)
4526 (*_bfd_error_handler)
4527 (_("%B: loader reloc in read-only section %A"),
4528 input_bfd, o->output_section);
4529 bfd_set_error (bfd_error_invalid_operation);
4530 return FALSE;
4532 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4533 finfo->ldrel);
4535 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4536 break;
4538 case R_TOC:
4539 case R_GL:
4540 case R_TCL:
4541 case R_TRL:
4542 case R_TRLA:
4543 /* We should never need a .loader reloc for a TOC
4544 relative reloc. */
4545 break;
4549 o->output_section->reloc_count += o->reloc_count;
4552 /* Write out the modified section contents. */
4553 if (! bfd_set_section_contents (output_bfd, o->output_section,
4554 contents, (file_ptr) o->output_offset,
4555 o->size))
4556 return FALSE;
4559 obj_coff_keep_syms (input_bfd) = keep_syms;
4561 if (! finfo->info->keep_memory)
4563 if (! _bfd_coff_free_symbols (input_bfd))
4564 return FALSE;
4567 return TRUE;
4570 #undef N_TMASK
4571 #undef N_BTSHFT
4573 /* Sort relocs by VMA. This is called via qsort. */
4575 static int
4576 xcoff_sort_relocs (const void * p1, const void * p2)
4578 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4579 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4581 if (r1->r_vaddr > r2->r_vaddr)
4582 return 1;
4583 else if (r1->r_vaddr < r2->r_vaddr)
4584 return -1;
4585 else
4586 return 0;
4589 /* Write out a non-XCOFF global symbol. */
4591 static bfd_boolean
4592 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4594 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4595 bfd *output_bfd;
4596 bfd_byte *outsym;
4597 struct internal_syment isym;
4598 union internal_auxent aux;
4599 bfd_boolean result;
4600 file_ptr pos;
4601 bfd_size_type amt;
4603 output_bfd = finfo->output_bfd;
4604 outsym = finfo->outsyms;
4606 if (h->root.type == bfd_link_hash_warning)
4608 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4609 if (h->root.type == bfd_link_hash_new)
4610 return TRUE;
4613 /* If this symbol was garbage collected, just skip it. */
4614 if (xcoff_hash_table (finfo->info)->gc
4615 && (h->flags & XCOFF_MARK) == 0)
4616 return TRUE;
4618 /* If we need a .loader section entry, write it out. */
4619 if (h->ldsym != NULL)
4621 struct internal_ldsym *ldsym;
4622 bfd *impbfd;
4624 ldsym = h->ldsym;
4626 if (h->root.type == bfd_link_hash_undefined
4627 || h->root.type == bfd_link_hash_undefweak)
4630 ldsym->l_value = 0;
4631 ldsym->l_scnum = N_UNDEF;
4632 ldsym->l_smtype = XTY_ER;
4633 impbfd = h->root.u.undef.abfd;
4636 else if (h->root.type == bfd_link_hash_defined
4637 || h->root.type == bfd_link_hash_defweak)
4639 asection *sec;
4641 sec = h->root.u.def.section;
4642 ldsym->l_value = (sec->output_section->vma
4643 + sec->output_offset
4644 + h->root.u.def.value);
4645 ldsym->l_scnum = sec->output_section->target_index;
4646 ldsym->l_smtype = XTY_SD;
4647 impbfd = sec->owner;
4650 else
4651 abort ();
4653 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4654 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4655 || (h->flags & XCOFF_IMPORT) != 0)
4656 /* Clear l_smtype
4657 Import symbols are defined so the check above will make
4658 the l_smtype XTY_SD. But this is not correct, it should
4659 be cleared. */
4660 ldsym->l_smtype |= L_IMPORT;
4662 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4663 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4664 || (h->flags & XCOFF_EXPORT) != 0)
4665 ldsym->l_smtype |= L_EXPORT;
4667 if ((h->flags & XCOFF_ENTRY) != 0)
4668 ldsym->l_smtype |= L_ENTRY;
4670 if ((h->flags & XCOFF_RTINIT) != 0)
4671 ldsym->l_smtype = XTY_SD;
4673 ldsym->l_smclas = h->smclas;
4675 if (ldsym->l_smtype & L_IMPORT)
4677 if ((h->root.type == bfd_link_hash_defined
4678 || h->root.type == bfd_link_hash_defweak)
4679 && (h->root.u.def.value != 0))
4680 ldsym->l_smclas = XMC_XO;
4682 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4683 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4684 ldsym->l_smclas = XMC_SV3264;
4686 else if (h->flags & XCOFF_SYSCALL32)
4687 ldsym->l_smclas = XMC_SV;
4689 else if (h->flags & XCOFF_SYSCALL64)
4690 ldsym->l_smclas = XMC_SV64;
4693 if (ldsym->l_ifile == -(bfd_size_type) 1)
4695 ldsym->l_ifile = 0;
4697 else if (ldsym->l_ifile == 0)
4699 if ((ldsym->l_smtype & L_IMPORT) == 0)
4700 ldsym->l_ifile = 0;
4701 else if (impbfd == NULL)
4702 ldsym->l_ifile = 0;
4703 else
4705 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4706 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4710 ldsym->l_parm = 0;
4712 BFD_ASSERT (h->ldindx >= 0);
4714 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4715 (finfo->ldsym +
4716 (h->ldindx - 3)
4717 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4718 h->ldsym = NULL;
4721 /* If this symbol needs global linkage code, write it out. */
4722 if (h->root.type == bfd_link_hash_defined
4723 && (h->root.u.def.section
4724 == xcoff_hash_table (finfo->info)->linkage_section))
4726 bfd_byte *p;
4727 bfd_vma tocoff;
4728 unsigned int i;
4730 p = h->root.u.def.section->contents + h->root.u.def.value;
4732 /* The first instruction in the global linkage code loads a
4733 specific TOC element. */
4734 tocoff = (h->descriptor->toc_section->output_section->vma
4735 + h->descriptor->toc_section->output_offset
4736 - xcoff_data (output_bfd)->toc);
4738 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4739 tocoff += h->descriptor->u.toc_offset;
4741 /* The first instruction in the glink code needs to be
4742 cooked to to hold the correct offset in the toc. The
4743 rest are just output raw. */
4744 bfd_put_32 (output_bfd,
4745 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
4747 /* Start with i == 1 to get past the first instruction done above
4748 The /4 is because the glink code is in bytes and we are going
4749 4 at a pop. */
4750 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
4751 bfd_put_32 (output_bfd,
4752 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
4753 &p[4 * i]);
4756 /* If we created a TOC entry for this symbol, write out the required
4757 relocs. */
4758 if ((h->flags & XCOFF_SET_TOC) != 0)
4760 asection *tocsec;
4761 asection *osec;
4762 int oindx;
4763 struct internal_reloc *irel;
4764 struct internal_ldrel ldrel;
4765 struct internal_syment irsym;
4766 union internal_auxent iraux;
4768 tocsec = h->toc_section;
4769 osec = tocsec->output_section;
4770 oindx = osec->target_index;
4771 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4772 irel->r_vaddr = (osec->vma
4773 + tocsec->output_offset
4774 + h->u.toc_offset);
4776 if (h->indx >= 0)
4777 irel->r_symndx = h->indx;
4778 else
4780 h->indx = -2;
4781 irel->r_symndx = obj_raw_syment_count (output_bfd);
4784 BFD_ASSERT (h->ldindx >= 0);
4786 /* Initialize the aux union here instead of closer to when it is
4787 written out below because the length of the csect depends on
4788 whether the output is 32 or 64 bit. */
4789 memset (&iraux, 0, sizeof iraux);
4790 iraux.x_csect.x_smtyp = XTY_SD;
4791 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
4792 iraux.x_csect.x_smclas = XMC_TC;
4794 /* 32 bit uses a 32 bit R_POS to do the relocations
4795 64 bit uses a 64 bit R_POS to do the relocations
4797 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4799 Which one is determined by the backend. */
4800 if (bfd_xcoff_is_xcoff64 (output_bfd))
4802 irel->r_size = 63;
4803 iraux.x_csect.x_scnlen.l = 8;
4805 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4807 irel->r_size = 31;
4808 iraux.x_csect.x_scnlen.l = 4;
4810 else
4811 return FALSE;
4813 irel->r_type = R_POS;
4814 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4815 ++osec->reloc_count;
4817 ldrel.l_vaddr = irel->r_vaddr;
4818 ldrel.l_symndx = h->ldindx;
4819 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
4820 ldrel.l_rsecnm = oindx;
4821 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4822 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4824 /* We need to emit a symbol to define a csect which holds
4825 the reloc. */
4826 if (finfo->info->strip != strip_all)
4828 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
4829 &irsym, h->root.root.string);
4830 if (!result)
4831 return FALSE;
4833 irsym.n_value = irel->r_vaddr;
4834 irsym.n_scnum = osec->target_index;
4835 irsym.n_sclass = C_HIDEXT;
4836 irsym.n_type = T_NULL;
4837 irsym.n_numaux = 1;
4839 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
4840 outsym += bfd_coff_symesz (output_bfd);
4842 /* Note : iraux is initialized above. */
4843 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
4844 0, 1, (void *) outsym);
4845 outsym += bfd_coff_auxesz (output_bfd);
4847 if (h->indx >= 0)
4849 /* We aren't going to write out the symbols below, so we
4850 need to write them out now. */
4851 pos = obj_sym_filepos (output_bfd);
4852 pos += (obj_raw_syment_count (output_bfd)
4853 * bfd_coff_symesz (output_bfd));
4854 amt = outsym - finfo->outsyms;
4855 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4856 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4857 return FALSE;
4858 obj_raw_syment_count (output_bfd) +=
4859 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4861 outsym = finfo->outsyms;
4866 /* If this symbol is a specially defined function descriptor, write
4867 it out. The first word is the address of the function code
4868 itself, the second word is the address of the TOC, and the third
4869 word is zero.
4871 32 bit vs 64 bit
4872 The addresses for the 32 bit will take 4 bytes and the addresses
4873 for 64 bit will take 8 bytes. Similar for the relocs. This type
4874 of logic was also done above to create a TOC entry in
4875 xcoff_write_global_symbol. */
4876 if ((h->flags & XCOFF_DESCRIPTOR) != 0
4877 && h->root.type == bfd_link_hash_defined
4878 && (h->root.u.def.section
4879 == xcoff_hash_table (finfo->info)->descriptor_section))
4881 asection *sec;
4882 asection *osec;
4883 int oindx;
4884 bfd_byte *p;
4885 struct xcoff_link_hash_entry *hentry;
4886 asection *esec;
4887 struct internal_reloc *irel;
4888 struct internal_ldrel ldrel;
4889 asection *tsec;
4890 unsigned int reloc_size, byte_size;
4892 if (bfd_xcoff_is_xcoff64 (output_bfd))
4894 reloc_size = 63;
4895 byte_size = 8;
4897 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4899 reloc_size = 31;
4900 byte_size = 4;
4902 else
4903 return FALSE;
4905 sec = h->root.u.def.section;
4906 osec = sec->output_section;
4907 oindx = osec->target_index;
4908 p = sec->contents + h->root.u.def.value;
4910 hentry = h->descriptor;
4911 BFD_ASSERT (hentry != NULL
4912 && (hentry->root.type == bfd_link_hash_defined
4913 || hentry->root.type == bfd_link_hash_defweak));
4914 esec = hentry->root.u.def.section;
4916 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4917 irel->r_vaddr = (osec->vma
4918 + sec->output_offset
4919 + h->root.u.def.value);
4920 irel->r_symndx = esec->output_section->target_index;
4921 irel->r_type = R_POS;
4922 irel->r_size = reloc_size;
4923 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4924 ++osec->reloc_count;
4926 ldrel.l_vaddr = irel->r_vaddr;
4927 if (strcmp (esec->output_section->name, ".text") == 0)
4928 ldrel.l_symndx = 0;
4929 else if (strcmp (esec->output_section->name, ".data") == 0)
4930 ldrel.l_symndx = 1;
4931 else if (strcmp (esec->output_section->name, ".bss") == 0)
4932 ldrel.l_symndx = 2;
4933 else
4935 (*_bfd_error_handler)
4936 (_("%s: loader reloc in unrecognized section `%s'"),
4937 bfd_get_filename (output_bfd),
4938 esec->output_section->name);
4939 bfd_set_error (bfd_error_nonrepresentable_section);
4940 return FALSE;
4942 ldrel.l_rtype = (reloc_size << 8) | R_POS;
4943 ldrel.l_rsecnm = oindx;
4944 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4945 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4947 /* There are three items to write out,
4948 the address of the code
4949 the address of the toc anchor
4950 the environment pointer.
4951 We are ignoring the environment pointer. So set it to zero. */
4952 if (bfd_xcoff_is_xcoff64 (output_bfd))
4954 bfd_put_64 (output_bfd,
4955 (esec->output_section->vma + esec->output_offset
4956 + hentry->root.u.def.value),
4958 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
4959 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
4961 else
4963 /* 32 bit backend
4964 This logic was already called above so the error case where
4965 the backend is neither has already been checked. */
4966 bfd_put_32 (output_bfd,
4967 (esec->output_section->vma + esec->output_offset
4968 + hentry->root.u.def.value),
4970 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
4971 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
4974 tsec = coff_section_from_bfd_index (output_bfd,
4975 xcoff_data (output_bfd)->sntoc);
4977 ++irel;
4978 irel->r_vaddr = (osec->vma
4979 + sec->output_offset
4980 + h->root.u.def.value
4981 + byte_size);
4982 irel->r_symndx = tsec->output_section->target_index;
4983 irel->r_type = R_POS;
4984 irel->r_size = reloc_size;
4985 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4986 ++osec->reloc_count;
4988 ldrel.l_vaddr = irel->r_vaddr;
4989 if (strcmp (tsec->output_section->name, ".text") == 0)
4990 ldrel.l_symndx = 0;
4991 else if (strcmp (tsec->output_section->name, ".data") == 0)
4992 ldrel.l_symndx = 1;
4993 else if (strcmp (tsec->output_section->name, ".bss") == 0)
4994 ldrel.l_symndx = 2;
4995 else
4997 (*_bfd_error_handler)
4998 (_("%s: loader reloc in unrecognized section `%s'"),
4999 bfd_get_filename (output_bfd),
5000 tsec->output_section->name);
5001 bfd_set_error (bfd_error_nonrepresentable_section);
5002 return FALSE;
5004 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5005 ldrel.l_rsecnm = oindx;
5006 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5007 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5010 if (h->indx >= 0 || finfo->info->strip == strip_all)
5012 BFD_ASSERT (outsym == finfo->outsyms);
5013 return TRUE;
5016 if (h->indx != -2
5017 && (finfo->info->strip == strip_all
5018 || (finfo->info->strip == strip_some
5019 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5020 FALSE, FALSE) == NULL)))
5022 BFD_ASSERT (outsym == finfo->outsyms);
5023 return TRUE;
5026 if (h->indx != -2
5027 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5029 BFD_ASSERT (outsym == finfo->outsyms);
5030 return TRUE;
5033 memset (&aux, 0, sizeof aux);
5035 h->indx = obj_raw_syment_count (output_bfd);
5037 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5038 h->root.root.string);
5039 if (!result)
5040 return FALSE;
5042 if (h->root.type == bfd_link_hash_undefined
5043 || h->root.type == bfd_link_hash_undefweak)
5045 isym.n_value = 0;
5046 isym.n_scnum = N_UNDEF;
5047 isym.n_sclass = C_EXT;
5048 aux.x_csect.x_smtyp = XTY_ER;
5050 else if ((h->root.type == bfd_link_hash_defined
5051 || h->root.type == bfd_link_hash_defweak)
5052 && h->smclas == XMC_XO)
5054 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5055 isym.n_value = h->root.u.def.value;
5056 isym.n_scnum = N_UNDEF;
5057 isym.n_sclass = C_EXT;
5058 aux.x_csect.x_smtyp = XTY_ER;
5060 else if (h->root.type == bfd_link_hash_defined
5061 || h->root.type == bfd_link_hash_defweak)
5063 struct xcoff_link_size_list *l;
5065 isym.n_value = (h->root.u.def.section->output_section->vma
5066 + h->root.u.def.section->output_offset
5067 + h->root.u.def.value);
5068 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5069 isym.n_scnum = N_ABS;
5070 else
5071 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5072 isym.n_sclass = C_HIDEXT;
5073 aux.x_csect.x_smtyp = XTY_SD;
5075 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5077 for (l = xcoff_hash_table (finfo->info)->size_list;
5078 l != NULL;
5079 l = l->next)
5081 if (l->h == h)
5083 aux.x_csect.x_scnlen.l = l->size;
5084 break;
5089 else if (h->root.type == bfd_link_hash_common)
5091 isym.n_value = (h->root.u.c.p->section->output_section->vma
5092 + h->root.u.c.p->section->output_offset);
5093 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5094 isym.n_sclass = C_EXT;
5095 aux.x_csect.x_smtyp = XTY_CM;
5096 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5098 else
5099 abort ();
5101 isym.n_type = T_NULL;
5102 isym.n_numaux = 1;
5104 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5105 outsym += bfd_coff_symesz (output_bfd);
5107 aux.x_csect.x_smclas = h->smclas;
5108 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5109 (void *) outsym);
5110 outsym += bfd_coff_auxesz (output_bfd);
5112 if ((h->root.type == bfd_link_hash_defined
5113 || h->root.type == bfd_link_hash_defweak)
5114 && h->smclas != XMC_XO)
5116 /* We just output an SD symbol. Now output an LD symbol. */
5117 h->indx += 2;
5119 isym.n_sclass = C_EXT;
5120 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5121 outsym += bfd_coff_symesz (output_bfd);
5123 aux.x_csect.x_smtyp = XTY_LD;
5124 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5125 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5126 (void *) outsym);
5127 outsym += bfd_coff_auxesz (output_bfd);
5130 pos = obj_sym_filepos (output_bfd);
5131 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5132 amt = outsym - finfo->outsyms;
5133 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5134 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5135 return FALSE;
5136 obj_raw_syment_count (output_bfd) +=
5137 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5139 return TRUE;
5142 /* Handle a link order which is supposed to generate a reloc. */
5144 static bfd_boolean
5145 xcoff_reloc_link_order (bfd *output_bfd,
5146 struct xcoff_final_link_info *finfo,
5147 asection *output_section,
5148 struct bfd_link_order *link_order)
5150 reloc_howto_type *howto;
5151 struct xcoff_link_hash_entry *h;
5152 asection *hsec;
5153 bfd_vma hval;
5154 bfd_vma addend;
5155 struct internal_reloc *irel;
5156 struct xcoff_link_hash_entry **rel_hash_ptr;
5157 struct internal_ldrel ldrel;
5159 if (link_order->type == bfd_section_reloc_link_order)
5160 /* We need to somehow locate a symbol in the right section. The
5161 symbol must either have a value of zero, or we must adjust
5162 the addend by the value of the symbol. FIXME: Write this
5163 when we need it. The old linker couldn't handle this anyhow. */
5164 abort ();
5166 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5167 if (howto == NULL)
5169 bfd_set_error (bfd_error_bad_value);
5170 return FALSE;
5173 h = ((struct xcoff_link_hash_entry *)
5174 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5175 link_order->u.reloc.p->u.name,
5176 FALSE, FALSE, TRUE));
5177 if (h == NULL)
5179 if (! ((*finfo->info->callbacks->unattached_reloc)
5180 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5181 return FALSE;
5182 return TRUE;
5185 if (h->root.type == bfd_link_hash_common)
5187 hsec = h->root.u.c.p->section;
5188 hval = 0;
5190 else if (h->root.type == bfd_link_hash_defined
5191 || h->root.type == bfd_link_hash_defweak)
5193 hsec = h->root.u.def.section;
5194 hval = h->root.u.def.value;
5196 else
5198 hsec = NULL;
5199 hval = 0;
5202 addend = link_order->u.reloc.p->addend;
5203 if (hsec != NULL)
5204 addend += (hsec->output_section->vma
5205 + hsec->output_offset
5206 + hval);
5208 if (addend != 0)
5210 bfd_size_type size;
5211 bfd_byte *buf;
5212 bfd_reloc_status_type rstat;
5213 bfd_boolean ok;
5215 size = bfd_get_reloc_size (howto);
5216 buf = bfd_zmalloc (size);
5217 if (buf == NULL)
5218 return FALSE;
5220 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5221 switch (rstat)
5223 case bfd_reloc_ok:
5224 break;
5225 default:
5226 case bfd_reloc_outofrange:
5227 abort ();
5228 case bfd_reloc_overflow:
5229 if (! ((*finfo->info->callbacks->reloc_overflow)
5230 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5231 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5233 free (buf);
5234 return FALSE;
5236 break;
5238 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5239 (file_ptr) link_order->offset, size);
5240 free (buf);
5241 if (! ok)
5242 return FALSE;
5245 /* Store the reloc information in the right place. It will get
5246 swapped and written out at the end of the final_link routine. */
5247 irel = (finfo->section_info[output_section->target_index].relocs
5248 + output_section->reloc_count);
5249 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5250 + output_section->reloc_count);
5252 memset (irel, 0, sizeof (struct internal_reloc));
5253 *rel_hash_ptr = NULL;
5255 irel->r_vaddr = output_section->vma + link_order->offset;
5257 if (h->indx >= 0)
5258 irel->r_symndx = h->indx;
5259 else
5261 /* Set the index to -2 to force this symbol to get written out. */
5262 h->indx = -2;
5263 *rel_hash_ptr = h;
5264 irel->r_symndx = 0;
5267 irel->r_type = howto->type;
5268 irel->r_size = howto->bitsize - 1;
5269 if (howto->complain_on_overflow == complain_overflow_signed)
5270 irel->r_size |= 0x80;
5272 ++output_section->reloc_count;
5274 /* Now output the reloc to the .loader section. */
5276 ldrel.l_vaddr = irel->r_vaddr;
5278 if (hsec != NULL)
5280 const char *secname;
5282 secname = hsec->output_section->name;
5284 if (strcmp (secname, ".text") == 0)
5285 ldrel.l_symndx = 0;
5286 else if (strcmp (secname, ".data") == 0)
5287 ldrel.l_symndx = 1;
5288 else if (strcmp (secname, ".bss") == 0)
5289 ldrel.l_symndx = 2;
5290 else
5292 (*_bfd_error_handler)
5293 (_("%s: loader reloc in unrecognized section `%s'"),
5294 bfd_get_filename (output_bfd), secname);
5295 bfd_set_error (bfd_error_nonrepresentable_section);
5296 return FALSE;
5299 else
5301 if (h->ldindx < 0)
5303 (*_bfd_error_handler)
5304 (_("%s: `%s' in loader reloc but not loader sym"),
5305 bfd_get_filename (output_bfd),
5306 h->root.root.string);
5307 bfd_set_error (bfd_error_bad_value);
5308 return FALSE;
5310 ldrel.l_symndx = h->ldindx;
5313 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5314 ldrel.l_rsecnm = output_section->target_index;
5315 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5316 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5318 return TRUE;
5321 /* Do the final link step. */
5323 bfd_boolean
5324 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5326 bfd_size_type symesz;
5327 struct xcoff_final_link_info finfo;
5328 asection *o;
5329 struct bfd_link_order *p;
5330 bfd_size_type max_contents_size;
5331 bfd_size_type max_sym_count;
5332 bfd_size_type max_lineno_count;
5333 bfd_size_type max_reloc_count;
5334 bfd_size_type max_output_reloc_count;
5335 file_ptr rel_filepos;
5336 unsigned int relsz;
5337 file_ptr line_filepos;
5338 unsigned int linesz;
5339 bfd *sub;
5340 bfd_byte *external_relocs = NULL;
5341 char strbuf[STRING_SIZE_SIZE];
5342 file_ptr pos;
5343 bfd_size_type amt;
5345 if (info->shared)
5346 abfd->flags |= DYNAMIC;
5348 symesz = bfd_coff_symesz (abfd);
5350 finfo.info = info;
5351 finfo.output_bfd = abfd;
5352 finfo.strtab = NULL;
5353 finfo.section_info = NULL;
5354 finfo.last_file_index = -1;
5355 finfo.toc_symindx = -1;
5356 finfo.internal_syms = NULL;
5357 finfo.sym_indices = NULL;
5358 finfo.outsyms = NULL;
5359 finfo.linenos = NULL;
5360 finfo.contents = NULL;
5361 finfo.external_relocs = NULL;
5363 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5364 + bfd_xcoff_ldhdrsz (abfd));
5365 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5366 + bfd_xcoff_ldhdrsz(abfd)
5367 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5368 * bfd_xcoff_ldsymsz(abfd)));
5370 xcoff_data (abfd)->coff.link_info = info;
5372 finfo.strtab = _bfd_stringtab_init ();
5373 if (finfo.strtab == NULL)
5374 goto error_return;
5376 /* Count the line number and relocation entries required for the
5377 output file. Determine a few maximum sizes. */
5378 max_contents_size = 0;
5379 max_lineno_count = 0;
5380 max_reloc_count = 0;
5381 for (o = abfd->sections; o != NULL; o = o->next)
5383 o->reloc_count = 0;
5384 o->lineno_count = 0;
5385 for (p = o->map_head.link_order; p != NULL; p = p->next)
5387 if (p->type == bfd_indirect_link_order)
5389 asection *sec;
5391 sec = p->u.indirect.section;
5393 /* Mark all sections which are to be included in the
5394 link. This will normally be every section. We need
5395 to do this so that we can identify any sections which
5396 the linker has decided to not include. */
5397 sec->linker_mark = TRUE;
5399 if (info->strip == strip_none
5400 || info->strip == strip_some)
5401 o->lineno_count += sec->lineno_count;
5403 o->reloc_count += sec->reloc_count;
5405 if (sec->rawsize > max_contents_size)
5406 max_contents_size = sec->rawsize;
5407 if (sec->size > max_contents_size)
5408 max_contents_size = sec->size;
5409 if (sec->lineno_count > max_lineno_count)
5410 max_lineno_count = sec->lineno_count;
5411 if (coff_section_data (sec->owner, sec) != NULL
5412 && xcoff_section_data (sec->owner, sec) != NULL
5413 && (xcoff_section_data (sec->owner, sec)->lineno_count
5414 > max_lineno_count))
5415 max_lineno_count =
5416 xcoff_section_data (sec->owner, sec)->lineno_count;
5417 if (sec->reloc_count > max_reloc_count)
5418 max_reloc_count = sec->reloc_count;
5420 else if (p->type == bfd_section_reloc_link_order
5421 || p->type == bfd_symbol_reloc_link_order)
5422 ++o->reloc_count;
5426 /* Compute the file positions for all the sections. */
5427 if (abfd->output_has_begun)
5429 if (xcoff_hash_table (info)->file_align != 0)
5430 abort ();
5432 else
5434 bfd_vma file_align;
5436 file_align = xcoff_hash_table (info)->file_align;
5437 if (file_align != 0)
5439 bfd_boolean saw_contents;
5440 int indx;
5441 file_ptr sofar;
5443 /* Insert .pad sections before every section which has
5444 contents and is loaded, if it is preceded by some other
5445 section which has contents and is loaded. */
5446 saw_contents = TRUE;
5447 for (o = abfd->sections; o != NULL; o = o->next)
5449 if (strcmp (o->name, ".pad") == 0)
5450 saw_contents = FALSE;
5451 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5452 && (o->flags & SEC_LOAD) != 0)
5454 if (! saw_contents)
5455 saw_contents = TRUE;
5456 else
5458 asection *n;
5460 /* Create a pad section and place it before the section
5461 that needs padding. This requires unlinking and
5462 relinking the bfd's section list. */
5464 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5465 SEC_HAS_CONTENTS);
5466 n->alignment_power = 0;
5468 bfd_section_list_remove (abfd, n);
5469 bfd_section_list_insert_before (abfd, o, n);
5470 saw_contents = FALSE;
5475 /* Reset the section indices after inserting the new
5476 sections. */
5477 indx = 0;
5478 for (o = abfd->sections; o != NULL; o = o->next)
5480 ++indx;
5481 o->target_index = indx;
5483 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5485 /* Work out appropriate sizes for the .pad sections to force
5486 each section to land on a page boundary. This bit of
5487 code knows what compute_section_file_positions is going
5488 to do. */
5489 sofar = bfd_coff_filhsz (abfd);
5490 sofar += bfd_coff_aoutsz (abfd);
5491 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5492 for (o = abfd->sections; o != NULL; o = o->next)
5493 if ((bfd_xcoff_is_reloc_count_overflow
5494 (abfd, (bfd_vma) o->reloc_count))
5495 || (bfd_xcoff_is_lineno_count_overflow
5496 (abfd, (bfd_vma) o->lineno_count)))
5497 /* 64 does not overflow, need to check if 32 does */
5498 sofar += bfd_coff_scnhsz (abfd);
5500 for (o = abfd->sections; o != NULL; o = o->next)
5502 if (strcmp (o->name, ".pad") == 0)
5504 bfd_vma pageoff;
5506 BFD_ASSERT (o->size == 0);
5507 pageoff = sofar & (file_align - 1);
5508 if (pageoff != 0)
5510 o->size = file_align - pageoff;
5511 sofar += file_align - pageoff;
5512 o->flags |= SEC_HAS_CONTENTS;
5515 else
5517 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5518 sofar += BFD_ALIGN (o->size,
5519 1 << o->alignment_power);
5524 if (! bfd_coff_compute_section_file_positions (abfd))
5525 goto error_return;
5528 /* Allocate space for the pointers we need to keep for the relocs. */
5530 unsigned int i;
5532 /* We use section_count + 1, rather than section_count, because
5533 the target_index fields are 1 based. */
5534 amt = abfd->section_count + 1;
5535 amt *= sizeof (struct xcoff_link_section_info);
5536 finfo.section_info = bfd_malloc (amt);
5537 if (finfo.section_info == NULL)
5538 goto error_return;
5539 for (i = 0; i <= abfd->section_count; i++)
5541 finfo.section_info[i].relocs = NULL;
5542 finfo.section_info[i].rel_hashes = NULL;
5543 finfo.section_info[i].toc_rel_hashes = NULL;
5547 /* Set the file positions for the relocs. */
5548 rel_filepos = obj_relocbase (abfd);
5549 relsz = bfd_coff_relsz (abfd);
5550 max_output_reloc_count = 0;
5551 for (o = abfd->sections; o != NULL; o = o->next)
5553 if (o->reloc_count == 0)
5554 o->rel_filepos = 0;
5555 else
5557 /* A stripped file has no relocs. However, we still
5558 allocate the buffers, so that later code doesn't have to
5559 worry about whether we are stripping or not. */
5560 if (info->strip == strip_all)
5561 o->rel_filepos = 0;
5562 else
5564 o->flags |= SEC_RELOC;
5565 o->rel_filepos = rel_filepos;
5566 rel_filepos += o->reloc_count * relsz;
5569 /* We don't know the indices of global symbols until we have
5570 written out all the local symbols. For each section in
5571 the output file, we keep an array of pointers to hash
5572 table entries. Each entry in the array corresponds to a
5573 reloc. When we find a reloc against a global symbol, we
5574 set the corresponding entry in this array so that we can
5575 fix up the symbol index after we have written out all the
5576 local symbols.
5578 Because of this problem, we also keep the relocs in
5579 memory until the end of the link. This wastes memory.
5580 We could backpatch the file later, I suppose, although it
5581 would be slow. */
5582 amt = o->reloc_count;
5583 amt *= sizeof (struct internal_reloc);
5584 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5586 amt = o->reloc_count;
5587 amt *= sizeof (struct xcoff_link_hash_entry *);
5588 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5590 if (finfo.section_info[o->target_index].relocs == NULL
5591 || finfo.section_info[o->target_index].rel_hashes == NULL)
5592 goto error_return;
5594 if (o->reloc_count > max_output_reloc_count)
5595 max_output_reloc_count = o->reloc_count;
5599 /* We now know the size of the relocs, so we can determine the file
5600 positions of the line numbers. */
5601 line_filepos = rel_filepos;
5602 finfo.line_filepos = line_filepos;
5603 linesz = bfd_coff_linesz (abfd);
5604 for (o = abfd->sections; o != NULL; o = o->next)
5606 if (o->lineno_count == 0)
5607 o->line_filepos = 0;
5608 else
5610 o->line_filepos = line_filepos;
5611 line_filepos += o->lineno_count * linesz;
5614 /* Reset the reloc and lineno counts, so that we can use them to
5615 count the number of entries we have output so far. */
5616 o->reloc_count = 0;
5617 o->lineno_count = 0;
5620 obj_sym_filepos (abfd) = line_filepos;
5622 /* Figure out the largest number of symbols in an input BFD. Take
5623 the opportunity to clear the output_has_begun fields of all the
5624 input BFD's. We want at least 6 symbols, since that is the
5625 number which xcoff_write_global_symbol may need. */
5626 max_sym_count = 6;
5627 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5629 bfd_size_type sz;
5631 sub->output_has_begun = FALSE;
5632 sz = obj_raw_syment_count (sub);
5633 if (sz > max_sym_count)
5634 max_sym_count = sz;
5637 /* Allocate some buffers used while linking. */
5638 amt = max_sym_count * sizeof (struct internal_syment);
5639 finfo.internal_syms = bfd_malloc (amt);
5641 amt = max_sym_count * sizeof (long);
5642 finfo.sym_indices = bfd_malloc (amt);
5644 amt = (max_sym_count + 1) * symesz;
5645 finfo.outsyms = bfd_malloc (amt);
5647 amt = max_lineno_count * bfd_coff_linesz (abfd);
5648 finfo.linenos = bfd_malloc (amt);
5650 amt = max_contents_size;
5651 finfo.contents = bfd_malloc (amt);
5653 amt = max_reloc_count * relsz;
5654 finfo.external_relocs = bfd_malloc (amt);
5656 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5657 || (finfo.sym_indices == NULL && max_sym_count > 0)
5658 || finfo.outsyms == NULL
5659 || (finfo.linenos == NULL && max_lineno_count > 0)
5660 || (finfo.contents == NULL && max_contents_size > 0)
5661 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5662 goto error_return;
5664 obj_raw_syment_count (abfd) = 0;
5665 xcoff_data (abfd)->toc = (bfd_vma) -1;
5667 /* We now know the position of everything in the file, except that
5668 we don't know the size of the symbol table and therefore we don't
5669 know where the string table starts. We just build the string
5670 table in memory as we go along. We process all the relocations
5671 for a single input file at once. */
5672 for (o = abfd->sections; o != NULL; o = o->next)
5674 for (p = o->map_head.link_order; p != NULL; p = p->next)
5676 if (p->type == bfd_indirect_link_order
5677 && p->u.indirect.section->owner->xvec == abfd->xvec)
5679 sub = p->u.indirect.section->owner;
5680 if (! sub->output_has_begun)
5682 if (! xcoff_link_input_bfd (&finfo, sub))
5683 goto error_return;
5684 sub->output_has_begun = TRUE;
5687 else if (p->type == bfd_section_reloc_link_order
5688 || p->type == bfd_symbol_reloc_link_order)
5690 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5691 goto error_return;
5693 else
5695 if (! _bfd_default_link_order (abfd, info, o, p))
5696 goto error_return;
5701 /* Free up the buffers used by xcoff_link_input_bfd. */
5702 if (finfo.internal_syms != NULL)
5704 free (finfo.internal_syms);
5705 finfo.internal_syms = NULL;
5707 if (finfo.sym_indices != NULL)
5709 free (finfo.sym_indices);
5710 finfo.sym_indices = NULL;
5712 if (finfo.linenos != NULL)
5714 free (finfo.linenos);
5715 finfo.linenos = NULL;
5717 if (finfo.contents != NULL)
5719 free (finfo.contents);
5720 finfo.contents = NULL;
5722 if (finfo.external_relocs != NULL)
5724 free (finfo.external_relocs);
5725 finfo.external_relocs = NULL;
5728 /* The value of the last C_FILE symbol is supposed to be -1. Write
5729 it out again. */
5730 if (finfo.last_file_index != -1)
5732 finfo.last_file.n_value = -(bfd_vma) 1;
5733 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
5734 (void *) finfo.outsyms);
5735 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
5736 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5737 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
5738 goto error_return;
5741 /* Write out all the global symbols which do not come from XCOFF
5742 input files. */
5743 xcoff_link_hash_traverse (xcoff_hash_table (info),
5744 xcoff_write_global_symbol,
5745 (void *) &finfo);
5747 if (finfo.outsyms != NULL)
5749 free (finfo.outsyms);
5750 finfo.outsyms = NULL;
5753 /* Now that we have written out all the global symbols, we know the
5754 symbol indices to use for relocs against them, and we can finally
5755 write out the relocs. */
5756 amt = max_output_reloc_count * relsz;
5757 external_relocs = bfd_malloc (amt);
5758 if (external_relocs == NULL && max_output_reloc_count != 0)
5759 goto error_return;
5761 for (o = abfd->sections; o != NULL; o = o->next)
5763 struct internal_reloc *irel;
5764 struct internal_reloc *irelend;
5765 struct xcoff_link_hash_entry **rel_hash;
5766 struct xcoff_toc_rel_hash *toc_rel_hash;
5767 bfd_byte *erel;
5768 bfd_size_type rel_size;
5770 /* A stripped file has no relocs. */
5771 if (info->strip == strip_all)
5773 o->reloc_count = 0;
5774 continue;
5777 if (o->reloc_count == 0)
5778 continue;
5780 irel = finfo.section_info[o->target_index].relocs;
5781 irelend = irel + o->reloc_count;
5782 rel_hash = finfo.section_info[o->target_index].rel_hashes;
5783 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5785 if (*rel_hash != NULL)
5787 if ((*rel_hash)->indx < 0)
5789 if (! ((*info->callbacks->unattached_reloc)
5790 (info, (*rel_hash)->root.root.string,
5791 NULL, o, irel->r_vaddr)))
5792 goto error_return;
5793 (*rel_hash)->indx = 0;
5795 irel->r_symndx = (*rel_hash)->indx;
5799 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
5800 toc_rel_hash != NULL;
5801 toc_rel_hash = toc_rel_hash->next)
5803 if (toc_rel_hash->h->u.toc_indx < 0)
5805 if (! ((*info->callbacks->unattached_reloc)
5806 (info, toc_rel_hash->h->root.root.string,
5807 NULL, o, toc_rel_hash->rel->r_vaddr)))
5808 goto error_return;
5809 toc_rel_hash->h->u.toc_indx = 0;
5811 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
5814 /* XCOFF requires that the relocs be sorted by address. We tend
5815 to produce them in the order in which their containing csects
5816 appear in the symbol table, which is not necessarily by
5817 address. So we sort them here. There may be a better way to
5818 do this. */
5819 qsort ((void *) finfo.section_info[o->target_index].relocs,
5820 o->reloc_count, sizeof (struct internal_reloc),
5821 xcoff_sort_relocs);
5823 irel = finfo.section_info[o->target_index].relocs;
5824 irelend = irel + o->reloc_count;
5825 erel = external_relocs;
5826 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5827 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
5829 rel_size = relsz * o->reloc_count;
5830 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
5831 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
5832 goto error_return;
5835 if (external_relocs != NULL)
5837 free (external_relocs);
5838 external_relocs = NULL;
5841 /* Free up the section information. */
5842 if (finfo.section_info != NULL)
5844 unsigned int i;
5846 for (i = 0; i < abfd->section_count; i++)
5848 if (finfo.section_info[i].relocs != NULL)
5849 free (finfo.section_info[i].relocs);
5850 if (finfo.section_info[i].rel_hashes != NULL)
5851 free (finfo.section_info[i].rel_hashes);
5853 free (finfo.section_info);
5854 finfo.section_info = NULL;
5857 /* Write out the loader section contents. */
5858 BFD_ASSERT ((bfd_byte *) finfo.ldrel
5859 == (xcoff_hash_table (info)->loader_section->contents
5860 + xcoff_hash_table (info)->ldhdr.l_impoff));
5861 o = xcoff_hash_table (info)->loader_section;
5862 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
5863 (file_ptr) o->output_offset, o->size))
5864 goto error_return;
5866 /* Write out the magic sections. */
5867 o = xcoff_hash_table (info)->linkage_section;
5868 if (o->size > 0
5869 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5870 (file_ptr) o->output_offset,
5871 o->size))
5872 goto error_return;
5873 o = xcoff_hash_table (info)->toc_section;
5874 if (o->size > 0
5875 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5876 (file_ptr) o->output_offset,
5877 o->size))
5878 goto error_return;
5879 o = xcoff_hash_table (info)->descriptor_section;
5880 if (o->size > 0
5881 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5882 (file_ptr) o->output_offset,
5883 o->size))
5884 goto error_return;
5886 /* Write out the string table. */
5887 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
5888 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5889 goto error_return;
5890 H_PUT_32 (abfd,
5891 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
5892 strbuf);
5893 amt = STRING_SIZE_SIZE;
5894 if (bfd_bwrite (strbuf, amt, abfd) != amt)
5895 goto error_return;
5896 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
5897 goto error_return;
5899 _bfd_stringtab_free (finfo.strtab);
5901 /* Write out the debugging string table. */
5902 o = xcoff_hash_table (info)->debug_section;
5903 if (o != NULL)
5905 struct bfd_strtab_hash *debug_strtab;
5907 debug_strtab = xcoff_hash_table (info)->debug_strtab;
5908 BFD_ASSERT (o->output_section->size - o->output_offset
5909 >= _bfd_stringtab_size (debug_strtab));
5910 pos = o->output_section->filepos + o->output_offset;
5911 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5912 goto error_return;
5913 if (! _bfd_stringtab_emit (abfd, debug_strtab))
5914 goto error_return;
5917 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
5918 not try to write out the symbols. */
5919 bfd_get_symcount (abfd) = 0;
5921 return TRUE;
5923 error_return:
5924 if (finfo.strtab != NULL)
5925 _bfd_stringtab_free (finfo.strtab);
5927 if (finfo.section_info != NULL)
5929 unsigned int i;
5931 for (i = 0; i < abfd->section_count; i++)
5933 if (finfo.section_info[i].relocs != NULL)
5934 free (finfo.section_info[i].relocs);
5935 if (finfo.section_info[i].rel_hashes != NULL)
5936 free (finfo.section_info[i].rel_hashes);
5938 free (finfo.section_info);
5941 if (finfo.internal_syms != NULL)
5942 free (finfo.internal_syms);
5943 if (finfo.sym_indices != NULL)
5944 free (finfo.sym_indices);
5945 if (finfo.outsyms != NULL)
5946 free (finfo.outsyms);
5947 if (finfo.linenos != NULL)
5948 free (finfo.linenos);
5949 if (finfo.contents != NULL)
5950 free (finfo.contents);
5951 if (finfo.external_relocs != NULL)
5952 free (finfo.external_relocs);
5953 if (external_relocs != NULL)
5954 free (external_relocs);
5955 return FALSE;