Merge to branch:
[binutils.git] / bfd / xcofflink.c
blobb1cac27b2dc53357c386e00acc1563a919c9a5a1
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 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))
449 free (ret);
450 return NULL;
453 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
454 ret->debug_section = NULL;
455 ret->loader_section = NULL;
456 ret->ldrel_count = 0;
457 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
458 ret->linkage_section = NULL;
459 ret->toc_section = NULL;
460 ret->descriptor_section = NULL;
461 ret->imports = NULL;
462 ret->file_align = 0;
463 ret->textro = FALSE;
464 ret->gc = FALSE;
465 memset (ret->special_sections, 0, sizeof ret->special_sections);
467 /* The linker will always generate a full a.out header. We need to
468 record that fact now, before the sizeof_headers routine could be
469 called. */
470 xcoff_data (abfd)->full_aouthdr = TRUE;
472 return &ret->root;
475 /* Free a XCOFF link hash table. */
477 void
478 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
480 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
482 _bfd_stringtab_free (ret->debug_strtab);
483 bfd_hash_table_free (&ret->root.table);
484 free (ret);
487 /* Read internal relocs for an XCOFF csect. This is a wrapper around
488 _bfd_coff_read_internal_relocs which tries to take advantage of any
489 relocs which may have been cached for the enclosing section. */
491 static struct internal_reloc *
492 xcoff_read_internal_relocs (bfd *abfd,
493 asection *sec,
494 bfd_boolean cache,
495 bfd_byte *external_relocs,
496 bfd_boolean require_internal,
497 struct internal_reloc *internal_relocs)
499 if (coff_section_data (abfd, sec) != NULL
500 && coff_section_data (abfd, sec)->relocs == NULL
501 && xcoff_section_data (abfd, sec) != NULL)
503 asection *enclosing;
505 enclosing = xcoff_section_data (abfd, sec)->enclosing;
507 if (enclosing != NULL
508 && (coff_section_data (abfd, enclosing) == NULL
509 || coff_section_data (abfd, enclosing)->relocs == NULL)
510 && cache
511 && enclosing->reloc_count > 0)
513 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
514 external_relocs, FALSE, NULL)
515 == NULL)
516 return NULL;
519 if (enclosing != NULL
520 && coff_section_data (abfd, enclosing) != NULL
521 && coff_section_data (abfd, enclosing)->relocs != NULL)
523 size_t off;
525 off = ((sec->rel_filepos - enclosing->rel_filepos)
526 / bfd_coff_relsz (abfd));
528 if (! require_internal)
529 return coff_section_data (abfd, enclosing)->relocs + off;
530 memcpy (internal_relocs,
531 coff_section_data (abfd, enclosing)->relocs + off,
532 sec->reloc_count * sizeof (struct internal_reloc));
533 return internal_relocs;
537 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
538 require_internal, internal_relocs);
541 /* This function is used to add symbols from a dynamic object to the
542 global symbol table. */
544 static bfd_boolean
545 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
547 asection *lsec;
548 bfd_byte *contents;
549 struct internal_ldhdr ldhdr;
550 const char *strings;
551 bfd_byte *elsym, *elsymend;
552 struct xcoff_import_file *n;
553 const char *bname;
554 const char *mname;
555 const char *s;
556 unsigned int c;
557 struct xcoff_import_file **pp;
559 /* We can only handle a dynamic object if we are generating an XCOFF
560 output file. */
561 if (info->hash->creator != abfd->xvec)
563 (*_bfd_error_handler)
564 (_("%s: XCOFF shared object when not producing XCOFF output"),
565 bfd_get_filename (abfd));
566 bfd_set_error (bfd_error_invalid_operation);
567 return FALSE;
570 /* The symbols we use from a dynamic object are not the symbols in
571 the normal symbol table, but, rather, the symbols in the export
572 table. If there is a global symbol in a dynamic object which is
573 not in the export table, the loader will not be able to find it,
574 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
575 libc.a has symbols in the export table which are not in the
576 symbol table. */
578 /* Read in the .loader section. FIXME: We should really use the
579 o_snloader field in the a.out header, rather than grabbing the
580 section by name. */
581 lsec = bfd_get_section_by_name (abfd, ".loader");
582 if (lsec == NULL)
584 (*_bfd_error_handler)
585 (_("%s: dynamic object with no .loader section"),
586 bfd_get_filename (abfd));
587 bfd_set_error (bfd_error_no_symbols);
588 return FALSE;
591 if (! xcoff_get_section_contents (abfd, lsec))
592 return FALSE;
593 contents = coff_section_data (abfd, lsec)->contents;
595 /* Remove the sections from this object, so that they do not get
596 included in the link. */
597 bfd_section_list_clear (abfd);
599 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
601 strings = (char *) contents + ldhdr.l_stoff;
603 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
605 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
607 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
609 struct internal_ldsym ldsym;
610 char nambuf[SYMNMLEN + 1];
611 const char *name;
612 struct xcoff_link_hash_entry *h;
614 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
616 /* We are only interested in exported symbols. */
617 if ((ldsym.l_smtype & L_EXPORT) == 0)
618 continue;
620 if (ldsym._l._l_l._l_zeroes == 0)
621 name = strings + ldsym._l._l_l._l_offset;
622 else
624 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
625 nambuf[SYMNMLEN] = '\0';
626 name = nambuf;
629 /* Normally we could not call xcoff_link_hash_lookup in an add
630 symbols routine, since we might not be using an XCOFF hash
631 table. However, we verified above that we are using an XCOFF
632 hash table. */
634 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
635 TRUE, TRUE);
636 if (h == NULL)
637 return FALSE;
639 h->flags |= XCOFF_DEF_DYNAMIC;
641 /* If the symbol is undefined, and the BFD it was found in is
642 not a dynamic object, change the BFD to this dynamic object,
643 so that we can get the correct import file ID. */
644 if ((h->root.type == bfd_link_hash_undefined
645 || h->root.type == bfd_link_hash_undefweak)
646 && (h->root.u.undef.abfd == NULL
647 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
648 h->root.u.undef.abfd = abfd;
650 if (h->root.type == bfd_link_hash_new)
652 h->root.type = bfd_link_hash_undefined;
653 h->root.u.undef.abfd = abfd;
654 /* We do not want to add this to the undefined symbol list. */
657 if (h->smclas == XMC_UA
658 || h->root.type == bfd_link_hash_undefined
659 || h->root.type == bfd_link_hash_undefweak)
660 h->smclas = ldsym.l_smclas;
662 /* Unless this is an XMC_XO symbol, we don't bother to actually
663 define it, since we don't have a section to put it in anyhow.
664 Instead, the relocation routines handle the DEF_DYNAMIC flag
665 correctly. */
667 if (h->smclas == XMC_XO
668 && (h->root.type == bfd_link_hash_undefined
669 || h->root.type == bfd_link_hash_undefweak))
671 /* This symbol has an absolute value. */
672 h->root.type = bfd_link_hash_defined;
673 h->root.u.def.section = bfd_abs_section_ptr;
674 h->root.u.def.value = ldsym.l_value;
677 /* If this symbol defines a function descriptor, then it
678 implicitly defines the function code as well. */
679 if (h->smclas == XMC_DS
680 || (h->smclas == XMC_XO && name[0] != '.'))
681 h->flags |= XCOFF_DESCRIPTOR;
682 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
684 struct xcoff_link_hash_entry *hds;
686 hds = h->descriptor;
687 if (hds == NULL)
689 char *dsnm;
691 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
692 if (dsnm == NULL)
693 return FALSE;
694 dsnm[0] = '.';
695 strcpy (dsnm + 1, name);
696 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
697 TRUE, TRUE, TRUE);
698 free (dsnm);
699 if (hds == NULL)
700 return FALSE;
702 if (hds->root.type == bfd_link_hash_new)
704 hds->root.type = bfd_link_hash_undefined;
705 hds->root.u.undef.abfd = abfd;
706 /* We do not want to add this to the undefined
707 symbol list. */
710 hds->descriptor = h;
711 h->descriptor = hds;
714 hds->flags |= XCOFF_DEF_DYNAMIC;
715 if (hds->smclas == XMC_UA)
716 hds->smclas = XMC_PR;
718 /* An absolute symbol appears to actually define code, not a
719 function descriptor. This is how some math functions are
720 implemented on AIX 4.1. */
721 if (h->smclas == XMC_XO
722 && (hds->root.type == bfd_link_hash_undefined
723 || hds->root.type == bfd_link_hash_undefweak))
725 hds->smclas = XMC_XO;
726 hds->root.type = bfd_link_hash_defined;
727 hds->root.u.def.section = bfd_abs_section_ptr;
728 hds->root.u.def.value = ldsym.l_value;
733 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
735 free (coff_section_data (abfd, lsec)->contents);
736 coff_section_data (abfd, lsec)->contents = NULL;
739 /* Record this file in the import files. */
740 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
741 if (n == NULL)
742 return FALSE;
743 n->next = NULL;
745 /* For some reason, the path entry in the import file list for a
746 shared object appears to always be empty. The file name is the
747 base name. */
748 n->path = "";
749 if (abfd->my_archive == NULL)
751 bname = bfd_get_filename (abfd);
752 mname = "";
754 else
756 bname = bfd_get_filename (abfd->my_archive);
757 mname = bfd_get_filename (abfd);
759 s = strrchr (bname, '/');
760 if (s != NULL)
761 bname = s + 1;
762 n->file = bname;
763 n->member = mname;
765 /* We start c at 1 because the first import file number is reserved
766 for LIBPATH. */
767 for (pp = &xcoff_hash_table (info)->imports, c = 1;
768 *pp != NULL;
769 pp = &(*pp)->next, ++c)
771 *pp = n;
773 xcoff_data (abfd)->import_file_id = c;
775 return TRUE;
778 /* xcoff_link_create_extra_sections
780 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
782 static bfd_boolean
783 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
785 bfd_boolean return_value = FALSE;
787 if (info->hash->creator == abfd->xvec)
789 /* We need to build a .loader section, so we do it here. This
790 won't work if we're producing an XCOFF output file with no
791 XCOFF input files. FIXME. */
793 if (xcoff_hash_table (info)->loader_section == NULL)
795 asection *lsec;
797 lsec = bfd_make_section_anyway (abfd, ".loader");
798 if (lsec == NULL)
799 goto end_return;
801 xcoff_hash_table (info)->loader_section = lsec;
802 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
805 /* Likewise for the linkage section. */
806 if (xcoff_hash_table (info)->linkage_section == NULL)
808 asection *lsec;
810 lsec = bfd_make_section_anyway (abfd, ".gl");
811 if (lsec == NULL)
812 goto end_return;
814 xcoff_hash_table (info)->linkage_section = lsec;
815 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
816 | SEC_IN_MEMORY);
817 lsec->alignment_power = 2;
820 /* Likewise for the TOC section. */
821 if (xcoff_hash_table (info)->toc_section == NULL)
823 asection *tsec;
825 tsec = bfd_make_section_anyway (abfd, ".tc");
826 if (tsec == NULL)
827 goto end_return;
829 xcoff_hash_table (info)->toc_section = tsec;
830 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
831 | SEC_IN_MEMORY);
832 tsec->alignment_power = 2;
835 /* Likewise for the descriptor section. */
836 if (xcoff_hash_table (info)->descriptor_section == NULL)
838 asection *dsec;
840 dsec = bfd_make_section_anyway (abfd, ".ds");
841 if (dsec == NULL)
842 goto end_return;
844 xcoff_hash_table (info)->descriptor_section = dsec;
845 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
846 | SEC_IN_MEMORY);
847 dsec->alignment_power = 2;
850 /* Likewise for the .debug section. */
851 if (xcoff_hash_table (info)->debug_section == NULL
852 && info->strip != strip_all)
854 asection *dsec;
856 dsec = bfd_make_section_anyway (abfd, ".debug");
857 if (dsec == NULL)
858 goto end_return;
860 xcoff_hash_table (info)->debug_section = dsec;
861 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
865 return_value = TRUE;
867 end_return:
869 return return_value;
872 /* Returns the index of reloc in RELOCS with the least address greater
873 than or equal to ADDRESS. The relocs are sorted by address. */
875 static bfd_size_type
876 xcoff_find_reloc (struct internal_reloc *relocs,
877 bfd_size_type count,
878 bfd_vma address)
880 bfd_size_type min, max, this;
882 if (count < 2)
884 if (count == 1 && relocs[0].r_vaddr < address)
885 return 1;
886 else
887 return 0;
890 min = 0;
891 max = count;
893 /* Do a binary search over (min,max]. */
894 while (min + 1 < max)
896 bfd_vma raddr;
898 this = (max + min) / 2;
899 raddr = relocs[this].r_vaddr;
900 if (raddr > address)
901 max = this;
902 else if (raddr < address)
903 min = this;
904 else
906 min = this;
907 break;
911 if (relocs[min].r_vaddr < address)
912 return min + 1;
914 while (min > 0
915 && relocs[min - 1].r_vaddr == address)
916 --min;
918 return min;
921 /* Add all the symbols from an object file to the hash table.
923 XCOFF is a weird format. A normal XCOFF .o files will have three
924 COFF sections--.text, .data, and .bss--but each COFF section will
925 contain many csects. These csects are described in the symbol
926 table. From the linker's point of view, each csect must be
927 considered a section in its own right. For example, a TOC entry is
928 handled as a small XMC_TC csect. The linker must be able to merge
929 different TOC entries together, which means that it must be able to
930 extract the XMC_TC csects from the .data section of the input .o
931 file.
933 From the point of view of our linker, this is, of course, a hideous
934 nightmare. We cope by actually creating sections for each csect,
935 and discarding the original sections. We then have to handle the
936 relocation entries carefully, since the only way to tell which
937 csect they belong to is to examine the address. */
939 static bfd_boolean
940 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
942 unsigned int n_tmask;
943 unsigned int n_btshft;
944 bfd_boolean default_copy;
945 bfd_size_type symcount;
946 struct xcoff_link_hash_entry **sym_hash;
947 asection **csect_cache;
948 bfd_size_type linesz;
949 asection *o;
950 asection *last_real;
951 bfd_boolean keep_syms;
952 asection *csect;
953 unsigned int csect_index;
954 asection *first_csect;
955 bfd_size_type symesz;
956 bfd_byte *esym;
957 bfd_byte *esym_end;
958 struct reloc_info_struct
960 struct internal_reloc *relocs;
961 asection **csects;
962 bfd_byte *linenos;
963 } *reloc_info = NULL;
964 bfd_size_type amt;
966 keep_syms = obj_coff_keep_syms (abfd);
968 if ((abfd->flags & DYNAMIC) != 0
969 && ! info->static_link)
971 if (! xcoff_link_add_dynamic_symbols (abfd, info))
972 return FALSE;
975 /* Create the loader, toc, gl, ds and debug sections, if needed. */
976 if (! xcoff_link_create_extra_sections (abfd, info))
977 goto error_return;
979 if ((abfd->flags & DYNAMIC) != 0
980 && ! info->static_link)
981 return TRUE;
983 n_tmask = coff_data (abfd)->local_n_tmask;
984 n_btshft = coff_data (abfd)->local_n_btshft;
986 /* Define macros so that ISFCN, et. al., macros work correctly. */
987 #define N_TMASK n_tmask
988 #define N_BTSHFT n_btshft
990 if (info->keep_memory)
991 default_copy = FALSE;
992 else
993 default_copy = TRUE;
995 symcount = obj_raw_syment_count (abfd);
997 /* We keep a list of the linker hash table entries that correspond
998 to each external symbol. */
999 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1000 sym_hash = bfd_zalloc (abfd, amt);
1001 if (sym_hash == NULL && symcount != 0)
1002 goto error_return;
1003 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1005 /* Because of the weird stuff we are doing with XCOFF csects, we can
1006 not easily determine which section a symbol is in, so we store
1007 the information in the tdata for the input file. */
1008 amt = symcount * sizeof (asection *);
1009 csect_cache = bfd_zalloc (abfd, amt);
1010 if (csect_cache == NULL && symcount != 0)
1011 goto error_return;
1012 xcoff_data (abfd)->csects = csect_cache;
1014 /* While splitting sections into csects, we need to assign the
1015 relocs correctly. The relocs and the csects must both be in
1016 order by VMA within a given section, so we handle this by
1017 scanning along the relocs as we process the csects. We index
1018 into reloc_info using the section target_index. */
1019 amt = abfd->section_count + 1;
1020 amt *= sizeof (struct reloc_info_struct);
1021 reloc_info = bfd_zmalloc (amt);
1022 if (reloc_info == NULL)
1023 goto error_return;
1025 /* Read in the relocs and line numbers for each section. */
1026 linesz = bfd_coff_linesz (abfd);
1027 last_real = NULL;
1028 for (o = abfd->sections; o != NULL; o = o->next)
1030 last_real = o;
1032 if ((o->flags & SEC_RELOC) != 0)
1034 reloc_info[o->target_index].relocs =
1035 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1036 amt = o->reloc_count;
1037 amt *= sizeof (asection *);
1038 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1039 if (reloc_info[o->target_index].csects == NULL)
1040 goto error_return;
1043 if ((info->strip == strip_none || info->strip == strip_some)
1044 && o->lineno_count > 0)
1046 bfd_byte *linenos;
1048 amt = linesz * o->lineno_count;
1049 linenos = bfd_malloc (amt);
1050 if (linenos == NULL)
1051 goto error_return;
1052 reloc_info[o->target_index].linenos = linenos;
1053 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1054 || bfd_bread (linenos, amt, abfd) != amt)
1055 goto error_return;
1059 /* Don't let the linker relocation routines discard the symbols. */
1060 obj_coff_keep_syms (abfd) = TRUE;
1062 csect = NULL;
1063 csect_index = 0;
1064 first_csect = NULL;
1066 symesz = bfd_coff_symesz (abfd);
1067 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1068 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1069 esym_end = esym + symcount * symesz;
1071 while (esym < esym_end)
1073 struct internal_syment sym;
1074 union internal_auxent aux;
1075 const char *name;
1076 char buf[SYMNMLEN + 1];
1077 int smtyp;
1078 flagword flags;
1079 asection *section;
1080 bfd_vma value;
1081 struct xcoff_link_hash_entry *set_toc;
1083 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1085 /* In this pass we are only interested in symbols with csect
1086 information. */
1087 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1089 /* Set csect_cache,
1090 Normally csect is a .pr, .rw etc. created in the loop
1091 If C_FILE or first time, handle special
1093 Advance esym, sym_hash, csect_hash ptr's
1094 Keep track of the last_symndx for the current file. */
1095 if (sym.n_sclass == C_FILE && csect != NULL)
1097 xcoff_section_data (abfd, csect)->last_symndx =
1098 ((esym
1099 - (bfd_byte *) obj_coff_external_syms (abfd))
1100 / symesz);
1101 csect = NULL;
1104 if (csect != NULL)
1105 *csect_cache = csect;
1106 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1107 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1108 else
1109 *csect_cache = NULL;
1110 esym += (sym.n_numaux + 1) * symesz;
1111 sym_hash += sym.n_numaux + 1;
1112 csect_cache += sym.n_numaux + 1;
1114 continue;
1117 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1119 if (name == NULL)
1120 goto error_return;
1122 /* If this symbol has line number information attached to it,
1123 and we're not stripping it, count the number of entries and
1124 add them to the count for this csect. In the final link pass
1125 we are going to attach line number information by symbol,
1126 rather than by section, in order to more easily handle
1127 garbage collection. */
1128 if ((info->strip == strip_none || info->strip == strip_some)
1129 && sym.n_numaux > 1
1130 && csect != NULL
1131 && ISFCN (sym.n_type))
1133 union internal_auxent auxlin;
1135 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1136 sym.n_type, sym.n_sclass,
1137 0, sym.n_numaux, (void *) &auxlin);
1139 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1141 asection *enclosing;
1142 bfd_signed_vma linoff;
1144 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1145 if (enclosing == NULL)
1147 (*_bfd_error_handler)
1148 (_("%B: `%s' has line numbers but no enclosing section"),
1149 abfd, name);
1150 bfd_set_error (bfd_error_bad_value);
1151 goto error_return;
1153 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1154 - enclosing->line_filepos);
1155 /* Explicit cast to bfd_signed_vma for compiler. */
1156 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1158 struct internal_lineno lin;
1159 bfd_byte *linpstart;
1161 linpstart = (reloc_info[enclosing->target_index].linenos
1162 + linoff);
1163 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1164 if (lin.l_lnno == 0
1165 && ((bfd_size_type) lin.l_addr.l_symndx
1166 == ((esym
1167 - (bfd_byte *) obj_coff_external_syms (abfd))
1168 / symesz)))
1170 bfd_byte *linpend, *linp;
1172 linpend = (reloc_info[enclosing->target_index].linenos
1173 + enclosing->lineno_count * linesz);
1174 for (linp = linpstart + linesz;
1175 linp < linpend;
1176 linp += linesz)
1178 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1179 (void *) &lin);
1180 if (lin.l_lnno == 0)
1181 break;
1183 csect->lineno_count += (linp - linpstart) / linesz;
1184 /* The setting of line_filepos will only be
1185 useful if all the line number entries for a
1186 csect are contiguous; this only matters for
1187 error reporting. */
1188 if (csect->line_filepos == 0)
1189 csect->line_filepos =
1190 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1196 /* Pick up the csect auxiliary information. */
1197 if (sym.n_numaux == 0)
1199 (*_bfd_error_handler)
1200 (_("%B: class %d symbol `%s' has no aux entries"),
1201 abfd, sym.n_sclass, name);
1202 bfd_set_error (bfd_error_bad_value);
1203 goto error_return;
1206 bfd_coff_swap_aux_in (abfd,
1207 (void *) (esym + symesz * sym.n_numaux),
1208 sym.n_type, sym.n_sclass,
1209 sym.n_numaux - 1, sym.n_numaux,
1210 (void *) &aux);
1212 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1214 flags = BSF_GLOBAL;
1215 section = NULL;
1216 value = 0;
1217 set_toc = NULL;
1219 switch (smtyp)
1221 default:
1222 (*_bfd_error_handler)
1223 (_("%B: symbol `%s' has unrecognized csect type %d"),
1224 abfd, name, smtyp);
1225 bfd_set_error (bfd_error_bad_value);
1226 goto error_return;
1228 case XTY_ER:
1229 /* This is an external reference. */
1230 if (sym.n_sclass == C_HIDEXT
1231 || sym.n_scnum != N_UNDEF
1232 || aux.x_csect.x_scnlen.l != 0)
1234 (*_bfd_error_handler)
1235 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1236 abfd, name, sym.n_sclass, sym.n_scnum,
1237 aux.x_csect.x_scnlen.l);
1238 bfd_set_error (bfd_error_bad_value);
1239 goto error_return;
1242 /* An XMC_XO external reference is actually a reference to
1243 an absolute location. */
1244 if (aux.x_csect.x_smclas != XMC_XO)
1245 section = bfd_und_section_ptr;
1246 else
1248 section = bfd_abs_section_ptr;
1249 value = sym.n_value;
1251 break;
1253 case XTY_SD:
1254 /* This is a csect definition. */
1255 if (csect != NULL)
1257 xcoff_section_data (abfd, csect)->last_symndx =
1258 ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1261 csect = NULL;
1262 csect_index = -(unsigned) 1;
1264 /* When we see a TOC anchor, we record the TOC value. */
1265 if (aux.x_csect.x_smclas == XMC_TC0)
1267 if (sym.n_sclass != C_HIDEXT
1268 || aux.x_csect.x_scnlen.l != 0)
1270 (*_bfd_error_handler)
1271 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1272 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1273 bfd_set_error (bfd_error_bad_value);
1274 goto error_return;
1276 xcoff_data (abfd)->toc = sym.n_value;
1279 /* We must merge TOC entries for the same symbol. We can
1280 merge two TOC entries if they are both C_HIDEXT, they
1281 both have the same name, they are both 4 or 8 bytes long, and
1282 they both have a relocation table entry for an external
1283 symbol with the same name. Unfortunately, this means
1284 that we must look through the relocations. Ick.
1286 Logic for 32 bit vs 64 bit.
1287 32 bit has a csect length of 4 for TOC
1288 64 bit has a csect length of 8 for TOC
1290 The conditions to get past the if-check are not that bad.
1291 They are what is used to create the TOC csects in the first
1292 place. */
1293 if (aux.x_csect.x_smclas == XMC_TC
1294 && sym.n_sclass == C_HIDEXT
1295 && info->hash->creator == abfd->xvec
1296 && ((bfd_xcoff_is_xcoff32 (abfd)
1297 && aux.x_csect.x_scnlen.l == 4)
1298 || (bfd_xcoff_is_xcoff64 (abfd)
1299 && aux.x_csect.x_scnlen.l == 8)))
1301 asection *enclosing;
1302 struct internal_reloc *relocs;
1303 bfd_size_type relindx;
1304 struct internal_reloc *rel;
1306 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1307 if (enclosing == NULL)
1308 goto error_return;
1310 relocs = reloc_info[enclosing->target_index].relocs;
1311 amt = enclosing->reloc_count;
1312 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1313 rel = relocs + relindx;
1315 /* 32 bit R_POS r_size is 31
1316 64 bit R_POS r_size is 63 */
1317 if (relindx < enclosing->reloc_count
1318 && rel->r_vaddr == (bfd_vma) sym.n_value
1319 && rel->r_type == R_POS
1320 && ((bfd_xcoff_is_xcoff32 (abfd)
1321 && rel->r_size == 31)
1322 || (bfd_xcoff_is_xcoff64 (abfd)
1323 && rel->r_size == 63)))
1325 bfd_byte *erelsym;
1327 struct internal_syment relsym;
1329 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1330 + rel->r_symndx * symesz);
1331 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1332 if (relsym.n_sclass == C_EXT)
1334 const char *relname;
1335 char relbuf[SYMNMLEN + 1];
1336 bfd_boolean copy;
1337 struct xcoff_link_hash_entry *h;
1339 /* At this point we know that the TOC entry is
1340 for an externally visible symbol. */
1341 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1342 relbuf);
1343 if (relname == NULL)
1344 goto error_return;
1346 /* We only merge TOC entries if the TC name is
1347 the same as the symbol name. This handles
1348 the normal case, but not common cases like
1349 SYM.P4 which gcc generates to store SYM + 4
1350 in the TOC. FIXME. */
1351 if (strcmp (name, relname) == 0)
1353 copy = (! info->keep_memory
1354 || relsym._n._n_n._n_zeroes != 0
1355 || relsym._n._n_n._n_offset == 0);
1356 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1357 relname, TRUE, copy,
1358 FALSE);
1359 if (h == NULL)
1360 goto error_return;
1362 /* At this point h->root.type could be
1363 bfd_link_hash_new. That should be OK,
1364 since we know for sure that we will come
1365 across this symbol as we step through the
1366 file. */
1368 /* We store h in *sym_hash for the
1369 convenience of the relocate_section
1370 function. */
1371 *sym_hash = h;
1373 if (h->toc_section != NULL)
1375 asection **rel_csects;
1377 /* We already have a TOC entry for this
1378 symbol, so we can just ignore this
1379 one. */
1380 rel_csects =
1381 reloc_info[enclosing->target_index].csects;
1382 rel_csects[relindx] = bfd_und_section_ptr;
1383 break;
1386 /* We are about to create a TOC entry for
1387 this symbol. */
1388 set_toc = h;
1395 asection *enclosing;
1397 /* We need to create a new section. We get the name from
1398 the csect storage mapping class, so that the linker can
1399 accumulate similar csects together. */
1401 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1402 if (NULL == csect)
1403 goto error_return;
1405 /* The enclosing section is the main section : .data, .text
1406 or .bss that the csect is coming from. */
1407 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1408 if (enclosing == NULL)
1409 goto error_return;
1411 if (! bfd_is_abs_section (enclosing)
1412 && ((bfd_vma) sym.n_value < enclosing->vma
1413 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1414 > enclosing->vma + enclosing->size)))
1416 (*_bfd_error_handler)
1417 (_("%B: csect `%s' not in enclosing section"),
1418 abfd, name);
1419 bfd_set_error (bfd_error_bad_value);
1420 goto error_return;
1422 csect->vma = sym.n_value;
1423 csect->filepos = (enclosing->filepos
1424 + sym.n_value
1425 - enclosing->vma);
1426 csect->size = aux.x_csect.x_scnlen.l;
1427 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1428 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1430 /* Record the enclosing section in the tdata for this new
1431 section. */
1432 amt = sizeof (struct coff_section_tdata);
1433 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1434 if (csect->used_by_bfd == NULL)
1435 goto error_return;
1436 amt = sizeof (struct xcoff_section_tdata);
1437 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1438 if (coff_section_data (abfd, csect)->tdata == NULL)
1439 goto error_return;
1440 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1441 xcoff_section_data (abfd, csect)->lineno_count =
1442 enclosing->lineno_count;
1444 if (enclosing->owner == abfd)
1446 struct internal_reloc *relocs;
1447 bfd_size_type relindx;
1448 struct internal_reloc *rel;
1449 asection **rel_csect;
1451 relocs = reloc_info[enclosing->target_index].relocs;
1452 amt = enclosing->reloc_count;
1453 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1455 rel = relocs + relindx;
1456 rel_csect = (reloc_info[enclosing->target_index].csects
1457 + relindx);
1459 csect->rel_filepos = (enclosing->rel_filepos
1460 + relindx * bfd_coff_relsz (abfd));
1461 while (relindx < enclosing->reloc_count
1462 && *rel_csect == NULL
1463 && rel->r_vaddr < csect->vma + csect->size)
1466 *rel_csect = csect;
1467 csect->flags |= SEC_RELOC;
1468 ++csect->reloc_count;
1469 ++relindx;
1470 ++rel;
1471 ++rel_csect;
1475 /* There are a number of other fields and section flags
1476 which we do not bother to set. */
1478 csect_index = ((esym
1479 - (bfd_byte *) obj_coff_external_syms (abfd))
1480 / symesz);
1482 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1484 if (first_csect == NULL)
1485 first_csect = csect;
1487 /* If this symbol is C_EXT, we treat it as starting at the
1488 beginning of the newly created section. */
1489 if (sym.n_sclass == C_EXT)
1491 section = csect;
1492 value = 0;
1495 /* If this is a TOC section for a symbol, record it. */
1496 if (set_toc != NULL)
1497 set_toc->toc_section = csect;
1499 break;
1501 case XTY_LD:
1502 /* This is a label definition. The x_scnlen field is the
1503 symbol index of the csect. Usually the XTY_LD symbol will
1504 follow its appropriate XTY_SD symbol. The .set pseudo op can
1505 cause the XTY_LD to not follow the XTY_SD symbol. */
1507 bfd_boolean bad;
1509 bad = FALSE;
1510 if (aux.x_csect.x_scnlen.l < 0
1511 || (aux.x_csect.x_scnlen.l
1512 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1513 bad = TRUE;
1514 if (! bad)
1516 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1517 if (section == NULL
1518 || (section->flags & SEC_HAS_CONTENTS) == 0)
1519 bad = TRUE;
1521 if (bad)
1523 (*_bfd_error_handler)
1524 (_("%B: misplaced XTY_LD `%s'"),
1525 abfd, name);
1526 bfd_set_error (bfd_error_bad_value);
1527 goto error_return;
1529 csect = section;
1530 value = sym.n_value - csect->vma;
1532 break;
1534 case XTY_CM:
1535 /* This is an unitialized csect. We could base the name on
1536 the storage mapping class, but we don't bother except for
1537 an XMC_TD symbol. If this csect is externally visible,
1538 it is a common symbol. We put XMC_TD symbols in sections
1539 named .tocbss, and rely on the linker script to put that
1540 in the TOC area. */
1542 if (csect != NULL)
1544 xcoff_section_data (abfd, csect)->last_symndx =
1545 ((esym
1546 - (bfd_byte *) obj_coff_external_syms (abfd))
1547 / symesz);
1550 if (aux.x_csect.x_smclas == XMC_TD)
1552 /* The linker script puts the .td section in the data
1553 section after the .tc section. */
1554 csect = bfd_make_section_anyway (abfd, ".td");
1556 else
1557 csect = bfd_make_section_anyway (abfd, ".bss");
1559 if (csect == NULL)
1560 goto error_return;
1561 csect->vma = sym.n_value;
1562 csect->size = aux.x_csect.x_scnlen.l;
1563 csect->flags |= SEC_ALLOC;
1564 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1565 /* There are a number of other fields and section flags
1566 which we do not bother to set. */
1568 csect_index = ((esym
1569 - (bfd_byte *) obj_coff_external_syms (abfd))
1570 / symesz);
1572 amt = sizeof (struct coff_section_tdata);
1573 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1574 if (csect->used_by_bfd == NULL)
1575 goto error_return;
1576 amt = sizeof (struct xcoff_section_tdata);
1577 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1578 if (coff_section_data (abfd, csect)->tdata == NULL)
1579 goto error_return;
1580 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1582 if (first_csect == NULL)
1583 first_csect = csect;
1585 if (sym.n_sclass == C_EXT)
1587 csect->flags |= SEC_IS_COMMON;
1588 csect->size = 0;
1589 section = csect;
1590 value = aux.x_csect.x_scnlen.l;
1593 break;
1596 /* Check for magic symbol names. */
1597 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1598 && aux.x_csect.x_smclas != XMC_TC
1599 && aux.x_csect.x_smclas != XMC_TD)
1601 int i = -1;
1603 if (name[0] == '_')
1605 if (strcmp (name, "_text") == 0)
1606 i = XCOFF_SPECIAL_SECTION_TEXT;
1607 else if (strcmp (name, "_etext") == 0)
1608 i = XCOFF_SPECIAL_SECTION_ETEXT;
1609 else if (strcmp (name, "_data") == 0)
1610 i = XCOFF_SPECIAL_SECTION_DATA;
1611 else if (strcmp (name, "_edata") == 0)
1612 i = XCOFF_SPECIAL_SECTION_EDATA;
1613 else if (strcmp (name, "_end") == 0)
1614 i = XCOFF_SPECIAL_SECTION_END;
1616 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1617 i = XCOFF_SPECIAL_SECTION_END2;
1619 if (i != -1)
1620 xcoff_hash_table (info)->special_sections[i] = csect;
1623 /* Now we have enough information to add the symbol to the
1624 linker hash table. */
1626 if (sym.n_sclass == C_EXT)
1628 bfd_boolean copy;
1630 BFD_ASSERT (section != NULL);
1632 /* We must copy the name into memory if we got it from the
1633 syment itself, rather than the string table. */
1634 copy = default_copy;
1635 if (sym._n._n_n._n_zeroes != 0
1636 || sym._n._n_n._n_offset == 0)
1637 copy = TRUE;
1639 /* The AIX linker appears to only detect multiple symbol
1640 definitions when there is a reference to the symbol. If
1641 a symbol is defined multiple times, and the only
1642 references are from the same object file, the AIX linker
1643 appears to permit it. It does not merge the different
1644 definitions, but handles them independently. On the
1645 other hand, if there is a reference, the linker reports
1646 an error.
1648 This matters because the AIX <net/net_globals.h> header
1649 file actually defines an initialized array, so we have to
1650 actually permit that to work.
1652 Just to make matters even more confusing, the AIX linker
1653 appears to permit multiple symbol definitions whenever
1654 the second definition is in an archive rather than an
1655 object file. This may be a consequence of the manner in
1656 which it handles archives: I think it may load the entire
1657 archive in as separate csects, and then let garbage
1658 collection discard symbols.
1660 We also have to handle the case of statically linking a
1661 shared object, which will cause symbol redefinitions,
1662 although this is an easier case to detect. */
1664 if (info->hash->creator == abfd->xvec)
1666 if (! bfd_is_und_section (section))
1667 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1668 name, TRUE, copy, FALSE);
1669 else
1670 /* Make a copy of the symbol name to prevent problems with
1671 merging symbols. */
1672 *sym_hash = ((struct xcoff_link_hash_entry *)
1673 bfd_wrapped_link_hash_lookup (abfd, info, name,
1674 TRUE, TRUE, FALSE));
1676 if (*sym_hash == NULL)
1677 goto error_return;
1678 if (((*sym_hash)->root.type == bfd_link_hash_defined
1679 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1680 && ! bfd_is_und_section (section)
1681 && ! bfd_is_com_section (section))
1683 /* This is a second definition of a defined symbol. */
1684 if ((abfd->flags & DYNAMIC) != 0
1685 && ((*sym_hash)->smclas != XMC_GL
1686 || aux.x_csect.x_smclas == XMC_GL
1687 || ((*sym_hash)->root.u.def.section->owner->flags
1688 & DYNAMIC) == 0))
1690 /* The new symbol is from a shared library, and
1691 either the existing symbol is not global
1692 linkage code or this symbol is global linkage
1693 code. If the existing symbol is global
1694 linkage code and the new symbol is not, then
1695 we want to use the new symbol. */
1696 section = bfd_und_section_ptr;
1697 value = 0;
1699 else if (((*sym_hash)->root.u.def.section->owner->flags
1700 & DYNAMIC) != 0)
1702 /* The existing symbol is from a shared library.
1703 Replace it. */
1704 (*sym_hash)->root.type = bfd_link_hash_undefined;
1705 (*sym_hash)->root.u.undef.abfd =
1706 (*sym_hash)->root.u.def.section->owner;
1708 else if (abfd->my_archive != NULL)
1710 /* This is a redefinition in an object contained
1711 in an archive. Just ignore it. See the
1712 comment above. */
1713 section = bfd_und_section_ptr;
1714 value = 0;
1716 else if ((*sym_hash)->root.u.undef.next != NULL
1717 || info->hash->undefs_tail == &(*sym_hash)->root)
1719 /* This symbol has been referenced. In this
1720 case, we just continue and permit the
1721 multiple definition error. See the comment
1722 above about the behaviour of the AIX linker. */
1724 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1726 /* The symbols are both csects of the same
1727 class. There is at least a chance that this
1728 is a semi-legitimate redefinition. */
1729 section = bfd_und_section_ptr;
1730 value = 0;
1731 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1734 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1735 && ((*sym_hash)->root.type == bfd_link_hash_defined
1736 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1737 && (bfd_is_und_section (section)
1738 || bfd_is_com_section (section)))
1740 /* This is a reference to a multiply defined symbol.
1741 Report the error now. See the comment above
1742 about the behaviour of the AIX linker. We could
1743 also do this with warning symbols, but I'm not
1744 sure the XCOFF linker is wholly prepared to
1745 handle them, and that would only be a warning,
1746 not an error. */
1747 if (! ((*info->callbacks->multiple_definition)
1748 (info, (*sym_hash)->root.root.string,
1749 NULL, NULL, (bfd_vma) 0,
1750 (*sym_hash)->root.u.def.section->owner,
1751 (*sym_hash)->root.u.def.section,
1752 (*sym_hash)->root.u.def.value)))
1753 goto error_return;
1754 /* Try not to give this error too many times. */
1755 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1759 /* _bfd_generic_link_add_one_symbol may call the linker to
1760 generate an error message, and the linker may try to read
1761 the symbol table to give a good error. Right now, the
1762 line numbers are in an inconsistent state, since they are
1763 counted both in the real sections and in the new csects.
1764 We need to leave the count in the real sections so that
1765 the linker can report the line number of the error
1766 correctly, so temporarily clobber the link to the csects
1767 so that the linker will not try to read the line numbers
1768 a second time from the csects. */
1769 BFD_ASSERT (last_real->next == first_csect);
1770 last_real->next = NULL;
1771 if (! (_bfd_generic_link_add_one_symbol
1772 (info, abfd, name, flags, section, value,
1773 NULL, copy, TRUE,
1774 (struct bfd_link_hash_entry **) sym_hash)))
1775 goto error_return;
1776 last_real->next = first_csect;
1778 if (smtyp == XTY_CM)
1780 if ((*sym_hash)->root.type != bfd_link_hash_common
1781 || (*sym_hash)->root.u.c.p->section != csect)
1782 /* We don't need the common csect we just created. */
1783 csect->size = 0;
1784 else
1785 (*sym_hash)->root.u.c.p->alignment_power
1786 = csect->alignment_power;
1789 if (info->hash->creator == abfd->xvec)
1791 int flag;
1793 if (smtyp == XTY_ER || smtyp == XTY_CM)
1794 flag = XCOFF_REF_REGULAR;
1795 else
1796 flag = XCOFF_DEF_REGULAR;
1797 (*sym_hash)->flags |= flag;
1799 if ((*sym_hash)->smclas == XMC_UA
1800 || flag == XCOFF_DEF_REGULAR)
1801 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1805 *csect_cache = csect;
1807 esym += (sym.n_numaux + 1) * symesz;
1808 sym_hash += sym.n_numaux + 1;
1809 csect_cache += sym.n_numaux + 1;
1812 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1814 /* Make sure that we have seen all the relocs. */
1815 for (o = abfd->sections; o != first_csect; o = o->next)
1817 /* Reset the section size and the line number count, since the
1818 data is now attached to the csects. Don't reset the size of
1819 the .debug section, since we need to read it below in
1820 bfd_xcoff_size_dynamic_sections. */
1821 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1822 o->size = 0;
1823 o->lineno_count = 0;
1825 if ((o->flags & SEC_RELOC) != 0)
1827 bfd_size_type i;
1828 struct internal_reloc *rel;
1829 asection **rel_csect;
1831 rel = reloc_info[o->target_index].relocs;
1832 rel_csect = reloc_info[o->target_index].csects;
1834 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1836 if (*rel_csect == NULL)
1838 (*_bfd_error_handler)
1839 (_("%B: reloc %s:%d not in csect"),
1840 abfd, o->name, i);
1841 bfd_set_error (bfd_error_bad_value);
1842 goto error_return;
1845 /* We identify all symbols which are called, so that we
1846 can create glue code for calls to functions imported
1847 from dynamic objects. */
1848 if (info->hash->creator == abfd->xvec
1849 && *rel_csect != bfd_und_section_ptr
1850 && (rel->r_type == R_BR
1851 || rel->r_type == R_RBR)
1852 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1854 struct xcoff_link_hash_entry *h;
1856 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1857 h->flags |= XCOFF_CALLED;
1858 /* If the symbol name starts with a period, it is
1859 the code of a function. If the symbol is
1860 currently undefined, then add an undefined symbol
1861 for the function descriptor. This should do no
1862 harm, because any regular object that defines the
1863 function should also define the function
1864 descriptor. It helps, because it means that we
1865 will identify the function descriptor with a
1866 dynamic object if a dynamic object defines it. */
1867 if (h->root.root.string[0] == '.'
1868 && h->descriptor == NULL)
1870 struct xcoff_link_hash_entry *hds;
1871 struct bfd_link_hash_entry *bh;
1873 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1874 h->root.root.string + 1,
1875 TRUE, FALSE, TRUE);
1876 if (hds == NULL)
1877 goto error_return;
1878 if (hds->root.type == bfd_link_hash_new)
1880 bh = &hds->root;
1881 if (! (_bfd_generic_link_add_one_symbol
1882 (info, abfd, hds->root.root.string,
1883 (flagword) 0, bfd_und_section_ptr,
1884 (bfd_vma) 0, NULL, FALSE,
1885 TRUE, &bh)))
1886 goto error_return;
1887 hds = (struct xcoff_link_hash_entry *) bh;
1889 hds->flags |= XCOFF_DESCRIPTOR;
1890 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1891 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1892 hds->descriptor = h;
1893 h->descriptor = hds;
1898 free (reloc_info[o->target_index].csects);
1899 reloc_info[o->target_index].csects = NULL;
1901 /* Reset SEC_RELOC and the reloc_count, since the reloc
1902 information is now attached to the csects. */
1903 o->flags &=~ SEC_RELOC;
1904 o->reloc_count = 0;
1906 /* If we are not keeping memory, free the reloc information. */
1907 if (! info->keep_memory
1908 && coff_section_data (abfd, o) != NULL
1909 && coff_section_data (abfd, o)->relocs != NULL
1910 && ! coff_section_data (abfd, o)->keep_relocs)
1912 free (coff_section_data (abfd, o)->relocs);
1913 coff_section_data (abfd, o)->relocs = NULL;
1917 /* Free up the line numbers. FIXME: We could cache these
1918 somewhere for the final link, to avoid reading them again. */
1919 if (reloc_info[o->target_index].linenos != NULL)
1921 free (reloc_info[o->target_index].linenos);
1922 reloc_info[o->target_index].linenos = NULL;
1926 free (reloc_info);
1928 obj_coff_keep_syms (abfd) = keep_syms;
1930 return TRUE;
1932 error_return:
1933 if (reloc_info != NULL)
1935 for (o = abfd->sections; o != NULL; o = o->next)
1937 if (reloc_info[o->target_index].csects != NULL)
1938 free (reloc_info[o->target_index].csects);
1939 if (reloc_info[o->target_index].linenos != NULL)
1940 free (reloc_info[o->target_index].linenos);
1942 free (reloc_info);
1944 obj_coff_keep_syms (abfd) = keep_syms;
1945 return FALSE;
1948 #undef N_TMASK
1949 #undef N_BTSHFT
1951 /* Add symbols from an XCOFF object file. */
1953 static bfd_boolean
1954 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1956 if (! _bfd_coff_get_external_symbols (abfd))
1957 return FALSE;
1958 if (! xcoff_link_add_symbols (abfd, info))
1959 return FALSE;
1960 if (! info->keep_memory)
1962 if (! _bfd_coff_free_symbols (abfd))
1963 return FALSE;
1965 return TRUE;
1968 /* Look through the loader symbols to see if this dynamic object
1969 should be included in the link. The native linker uses the loader
1970 symbols, not the normal symbol table, so we do too. */
1972 static bfd_boolean
1973 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1974 struct bfd_link_info *info,
1975 bfd_boolean *pneeded)
1977 asection *lsec;
1978 bfd_byte *contents;
1979 struct internal_ldhdr ldhdr;
1980 const char *strings;
1981 bfd_byte *elsym, *elsymend;
1983 *pneeded = FALSE;
1985 lsec = bfd_get_section_by_name (abfd, ".loader");
1986 if (lsec == NULL)
1987 /* There are no symbols, so don't try to include it. */
1988 return TRUE;
1990 if (! xcoff_get_section_contents (abfd, lsec))
1991 return FALSE;
1992 contents = coff_section_data (abfd, lsec)->contents;
1994 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
1996 strings = (char *) contents + ldhdr.l_stoff;
1998 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2000 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2001 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2003 struct internal_ldsym ldsym;
2004 char nambuf[SYMNMLEN + 1];
2005 const char *name;
2006 struct bfd_link_hash_entry *h;
2008 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2010 /* We are only interested in exported symbols. */
2011 if ((ldsym.l_smtype & L_EXPORT) == 0)
2012 continue;
2014 if (ldsym._l._l_l._l_zeroes == 0)
2015 name = strings + ldsym._l._l_l._l_offset;
2016 else
2018 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2019 nambuf[SYMNMLEN] = '\0';
2020 name = nambuf;
2023 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2025 /* We are only interested in symbols that are currently
2026 undefined. At this point we know that we are using an XCOFF
2027 hash table. */
2028 if (h != NULL
2029 && h->type == bfd_link_hash_undefined
2030 && (((struct xcoff_link_hash_entry *) h)->flags
2031 & XCOFF_DEF_DYNAMIC) == 0)
2033 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2034 return FALSE;
2035 *pneeded = TRUE;
2036 return TRUE;
2040 /* We do not need this shared object. */
2041 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2043 free (coff_section_data (abfd, lsec)->contents);
2044 coff_section_data (abfd, lsec)->contents = NULL;
2047 return TRUE;
2050 /* Look through the symbols to see if this object file should be
2051 included in the link. */
2053 static bfd_boolean
2054 xcoff_link_check_ar_symbols (bfd *abfd,
2055 struct bfd_link_info *info,
2056 bfd_boolean *pneeded)
2058 bfd_size_type symesz;
2059 bfd_byte *esym;
2060 bfd_byte *esym_end;
2062 *pneeded = FALSE;
2064 if ((abfd->flags & DYNAMIC) != 0
2065 && ! info->static_link
2066 && info->hash->creator == abfd->xvec)
2067 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2069 symesz = bfd_coff_symesz (abfd);
2070 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2071 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2072 while (esym < esym_end)
2074 struct internal_syment sym;
2076 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2078 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
2080 const char *name;
2081 char buf[SYMNMLEN + 1];
2082 struct bfd_link_hash_entry *h;
2084 /* This symbol is externally visible, and is defined by this
2085 object file. */
2086 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2088 if (name == NULL)
2089 return FALSE;
2090 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2092 /* We are only interested in symbols that are currently
2093 undefined. If a symbol is currently known to be common,
2094 XCOFF linkers do not bring in an object file which
2095 defines it. We also don't bring in symbols to satisfy
2096 undefined references in shared objects. */
2097 if (h != NULL
2098 && h->type == bfd_link_hash_undefined
2099 && (info->hash->creator != abfd->xvec
2100 || (((struct xcoff_link_hash_entry *) h)->flags
2101 & XCOFF_DEF_DYNAMIC) == 0))
2103 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2104 return FALSE;
2105 *pneeded = TRUE;
2106 return TRUE;
2110 esym += (sym.n_numaux + 1) * symesz;
2113 /* We do not need this object file. */
2114 return TRUE;
2117 /* Check a single archive element to see if we need to include it in
2118 the link. *PNEEDED is set according to whether this element is
2119 needed in the link or not. This is called via
2120 _bfd_generic_link_add_archive_symbols. */
2122 static bfd_boolean
2123 xcoff_link_check_archive_element (bfd *abfd,
2124 struct bfd_link_info *info,
2125 bfd_boolean *pneeded)
2127 if (! _bfd_coff_get_external_symbols (abfd))
2128 return FALSE;
2130 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2131 return FALSE;
2133 if (*pneeded)
2135 if (! xcoff_link_add_symbols (abfd, info))
2136 return FALSE;
2139 if (! info->keep_memory || ! *pneeded)
2141 if (! _bfd_coff_free_symbols (abfd))
2142 return FALSE;
2145 return TRUE;
2148 /* Given an XCOFF BFD, add symbols to the global hash table as
2149 appropriate. */
2151 bfd_boolean
2152 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2154 switch (bfd_get_format (abfd))
2156 case bfd_object:
2157 return xcoff_link_add_object_symbols (abfd, info);
2159 case bfd_archive:
2160 /* If the archive has a map, do the usual search. We then need
2161 to check the archive for dynamic objects, because they may not
2162 appear in the archive map even though they should, perhaps, be
2163 included. If the archive has no map, we just consider each object
2164 file in turn, since that apparently is what the AIX native linker
2165 does. */
2166 if (bfd_has_map (abfd))
2168 if (! (_bfd_generic_link_add_archive_symbols
2169 (abfd, info, xcoff_link_check_archive_element)))
2170 return FALSE;
2174 bfd *member;
2176 member = bfd_openr_next_archived_file (abfd, NULL);
2177 while (member != NULL)
2179 if (bfd_check_format (member, bfd_object)
2180 && (info->hash->creator == member->xvec)
2181 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2183 bfd_boolean needed;
2185 if (! xcoff_link_check_archive_element (member, info,
2186 &needed))
2187 return FALSE;
2188 if (needed)
2189 member->archive_pass = -1;
2191 member = bfd_openr_next_archived_file (abfd, member);
2195 return TRUE;
2197 default:
2198 bfd_set_error (bfd_error_wrong_format);
2199 return FALSE;
2203 /* Mark a symbol as not being garbage, including the section in which
2204 it is defined. */
2206 static inline bfd_boolean
2207 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2209 if ((h->flags & XCOFF_MARK) != 0)
2210 return TRUE;
2212 h->flags |= XCOFF_MARK;
2213 if (h->root.type == bfd_link_hash_defined
2214 || h->root.type == bfd_link_hash_defweak)
2216 asection *hsec;
2218 hsec = h->root.u.def.section;
2219 if (! bfd_is_abs_section (hsec)
2220 && (hsec->flags & SEC_MARK) == 0)
2222 if (! xcoff_mark (info, hsec))
2223 return FALSE;
2227 if (h->toc_section != NULL
2228 && (h->toc_section->flags & SEC_MARK) == 0)
2230 if (! xcoff_mark (info, h->toc_section))
2231 return FALSE;
2234 return TRUE;
2237 /* The mark phase of garbage collection. For a given section, mark
2238 it, and all the sections which define symbols to which it refers.
2239 Because this function needs to look at the relocs, we also count
2240 the number of relocs which need to be copied into the .loader
2241 section. */
2243 static bfd_boolean
2244 xcoff_mark (struct bfd_link_info *info, asection *sec)
2246 if (bfd_is_abs_section (sec)
2247 || (sec->flags & SEC_MARK) != 0)
2248 return TRUE;
2250 sec->flags |= SEC_MARK;
2252 if (sec->owner->xvec == info->hash->creator
2253 && coff_section_data (sec->owner, sec) != NULL
2254 && xcoff_section_data (sec->owner, sec) != NULL)
2256 struct xcoff_link_hash_entry **hp, **hpend;
2257 struct internal_reloc *rel, *relend;
2259 /* Mark all the symbols in this section. */
2260 hp = (obj_xcoff_sym_hashes (sec->owner)
2261 + xcoff_section_data (sec->owner, sec)->first_symndx);
2262 hpend = (obj_xcoff_sym_hashes (sec->owner)
2263 + xcoff_section_data (sec->owner, sec)->last_symndx);
2264 for (; hp < hpend; hp++)
2266 struct xcoff_link_hash_entry *h;
2268 h = *hp;
2269 if (h != NULL
2270 && (h->flags & XCOFF_MARK) == 0)
2272 if (! xcoff_mark_symbol (info, h))
2273 return FALSE;
2277 /* Look through the section relocs. */
2278 if ((sec->flags & SEC_RELOC) != 0
2279 && sec->reloc_count > 0)
2281 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2282 NULL, FALSE, NULL);
2283 if (rel == NULL)
2284 return FALSE;
2285 relend = rel + sec->reloc_count;
2286 for (; rel < relend; rel++)
2288 asection *rsec;
2289 struct xcoff_link_hash_entry *h;
2291 if ((unsigned int) rel->r_symndx
2292 > obj_raw_syment_count (sec->owner))
2293 continue;
2295 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2296 if (h != NULL
2297 && (h->flags & XCOFF_MARK) == 0)
2299 if (! xcoff_mark_symbol (info, h))
2300 return FALSE;
2303 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2304 if (rsec != NULL
2305 && (rsec->flags & SEC_MARK) == 0)
2307 if (! xcoff_mark (info, rsec))
2308 return FALSE;
2311 /* See if this reloc needs to be copied into the .loader
2312 section. */
2313 switch (rel->r_type)
2315 default:
2316 if (h == NULL
2317 || h->root.type == bfd_link_hash_defined
2318 || h->root.type == bfd_link_hash_defweak
2319 || h->root.type == bfd_link_hash_common
2320 || ((h->flags & XCOFF_CALLED) != 0
2321 && (h->root.type == bfd_link_hash_undefined
2322 || h->root.type == bfd_link_hash_undefweak)
2323 && h->root.root.string[0] == '.'
2324 && h->descriptor != NULL
2325 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2326 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2327 && (h->descriptor->flags
2328 & XCOFF_DEF_REGULAR) == 0))))
2329 break;
2330 /* Fall through. */
2331 case R_POS:
2332 case R_NEG:
2333 case R_RL:
2334 case R_RLA:
2335 ++xcoff_hash_table (info)->ldrel_count;
2336 if (h != NULL)
2337 h->flags |= XCOFF_LDREL;
2338 break;
2339 case R_TOC:
2340 case R_GL:
2341 case R_TCL:
2342 case R_TRL:
2343 case R_TRLA:
2344 /* We should never need a .loader reloc for a TOC
2345 relative reloc. */
2346 break;
2350 if (! info->keep_memory
2351 && coff_section_data (sec->owner, sec) != NULL
2352 && coff_section_data (sec->owner, sec)->relocs != NULL
2353 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2355 free (coff_section_data (sec->owner, sec)->relocs);
2356 coff_section_data (sec->owner, sec)->relocs = NULL;
2361 return TRUE;
2364 /* Routines that are called after all the input files have been
2365 handled, but before the sections are laid out in memory. */
2367 /* The sweep phase of garbage collection. Remove all garbage
2368 sections. */
2370 static void
2371 xcoff_sweep (struct bfd_link_info *info)
2373 bfd *sub;
2375 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2377 asection *o;
2379 for (o = sub->sections; o != NULL; o = o->next)
2381 if ((o->flags & SEC_MARK) == 0)
2383 /* Keep all sections from non-XCOFF input files. Keep
2384 special sections. Keep .debug sections for the
2385 moment. */
2386 if (sub->xvec != info->hash->creator
2387 || o == xcoff_hash_table (info)->debug_section
2388 || o == xcoff_hash_table (info)->loader_section
2389 || o == xcoff_hash_table (info)->linkage_section
2390 || o == xcoff_hash_table (info)->toc_section
2391 || o == xcoff_hash_table (info)->descriptor_section
2392 || strcmp (o->name, ".debug") == 0)
2393 o->flags |= SEC_MARK;
2394 else
2396 o->size = 0;
2397 o->reloc_count = 0;
2398 o->lineno_count = 0;
2405 /* Record the number of elements in a set. This is used to output the
2406 correct csect length. */
2408 bfd_boolean
2409 bfd_xcoff_link_record_set (bfd *output_bfd,
2410 struct bfd_link_info *info,
2411 struct bfd_link_hash_entry *harg,
2412 bfd_size_type size)
2414 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2415 struct xcoff_link_size_list *n;
2416 bfd_size_type amt;
2418 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2419 return TRUE;
2421 /* This will hardly ever be called. I don't want to burn four bytes
2422 per global symbol, so instead the size is kept on a linked list
2423 attached to the hash table. */
2424 amt = sizeof (* n);
2425 n = bfd_alloc (output_bfd, amt);
2426 if (n == NULL)
2427 return FALSE;
2428 n->next = xcoff_hash_table (info)->size_list;
2429 n->h = h;
2430 n->size = size;
2431 xcoff_hash_table (info)->size_list = n;
2433 h->flags |= XCOFF_HAS_SIZE;
2435 return TRUE;
2438 /* Import a symbol. */
2440 bfd_boolean
2441 bfd_xcoff_import_symbol (bfd *output_bfd,
2442 struct bfd_link_info *info,
2443 struct bfd_link_hash_entry *harg,
2444 bfd_vma val,
2445 const char *imppath,
2446 const char *impfile,
2447 const char *impmember,
2448 unsigned int syscall_flag)
2450 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2452 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2453 return TRUE;
2455 /* A symbol name which starts with a period is the code for a
2456 function. If the symbol is undefined, then add an undefined
2457 symbol for the function descriptor, and import that instead. */
2458 if (h->root.root.string[0] == '.'
2459 && h->root.type == bfd_link_hash_undefined
2460 && val == (bfd_vma) -1)
2462 struct xcoff_link_hash_entry *hds;
2464 hds = h->descriptor;
2465 if (hds == NULL)
2467 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2468 h->root.root.string + 1,
2469 TRUE, FALSE, TRUE);
2470 if (hds == NULL)
2471 return FALSE;
2472 if (hds->root.type == bfd_link_hash_new)
2474 hds->root.type = bfd_link_hash_undefined;
2475 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2477 hds->flags |= XCOFF_DESCRIPTOR;
2478 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2479 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2480 hds->descriptor = h;
2481 h->descriptor = hds;
2484 /* Now, if the descriptor is undefined, import the descriptor
2485 rather than the symbol we were told to import. FIXME: Is
2486 this correct in all cases? */
2487 if (hds->root.type == bfd_link_hash_undefined)
2488 h = hds;
2491 h->flags |= (XCOFF_IMPORT | syscall_flag);
2493 if (val != (bfd_vma) -1)
2495 if (h->root.type == bfd_link_hash_defined
2496 && (! bfd_is_abs_section (h->root.u.def.section)
2497 || h->root.u.def.value != val))
2499 if (! ((*info->callbacks->multiple_definition)
2500 (info, h->root.root.string, h->root.u.def.section->owner,
2501 h->root.u.def.section, h->root.u.def.value,
2502 output_bfd, bfd_abs_section_ptr, val)))
2503 return FALSE;
2506 h->root.type = bfd_link_hash_defined;
2507 h->root.u.def.section = bfd_abs_section_ptr;
2508 h->root.u.def.value = val;
2511 /* We overload the ldindx field to hold the l_ifile value for this
2512 symbol. */
2513 BFD_ASSERT (h->ldsym == NULL);
2514 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2515 if (imppath == NULL)
2516 h->ldindx = -1;
2517 else
2519 unsigned int c;
2520 struct xcoff_import_file **pp;
2522 /* We start c at 1 because the first entry in the import list is
2523 reserved for the library search path. */
2524 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2525 *pp != NULL;
2526 pp = &(*pp)->next, ++c)
2528 if (strcmp ((*pp)->path, imppath) == 0
2529 && strcmp ((*pp)->file, impfile) == 0
2530 && strcmp ((*pp)->member, impmember) == 0)
2531 break;
2534 if (*pp == NULL)
2536 struct xcoff_import_file *n;
2537 bfd_size_type amt = sizeof (* n);
2539 n = bfd_alloc (output_bfd, amt);
2540 if (n == NULL)
2541 return FALSE;
2542 n->next = NULL;
2543 n->path = imppath;
2544 n->file = impfile;
2545 n->member = impmember;
2546 *pp = n;
2549 h->ldindx = c;
2552 return TRUE;
2555 /* Export a symbol. */
2557 bfd_boolean
2558 bfd_xcoff_export_symbol (bfd *output_bfd,
2559 struct bfd_link_info *info,
2560 struct bfd_link_hash_entry *harg)
2562 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2564 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2565 return TRUE;
2567 h->flags |= XCOFF_EXPORT;
2569 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2570 I'm just going to ignore it until somebody explains it. */
2572 /* See if this is a function descriptor. It may be one even though
2573 it is not so marked. */
2574 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2575 && h->root.root.string[0] != '.')
2577 char *fnname;
2578 struct xcoff_link_hash_entry *hfn;
2579 bfd_size_type amt = strlen (h->root.root.string) + 2;
2581 fnname = bfd_malloc (amt);
2582 if (fnname == NULL)
2583 return FALSE;
2584 fnname[0] = '.';
2585 strcpy (fnname + 1, h->root.root.string);
2586 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2587 fnname, FALSE, FALSE, TRUE);
2588 free (fnname);
2589 if (hfn != NULL
2590 && hfn->smclas == XMC_PR
2591 && (hfn->root.type == bfd_link_hash_defined
2592 || hfn->root.type == bfd_link_hash_defweak))
2594 h->flags |= XCOFF_DESCRIPTOR;
2595 h->descriptor = hfn;
2596 hfn->descriptor = h;
2600 /* Make sure we don't garbage collect this symbol. */
2601 if (! xcoff_mark_symbol (info, h))
2602 return FALSE;
2604 /* If this is a function descriptor, make sure we don't garbage
2605 collect the associated function code. We normally don't have to
2606 worry about this, because the descriptor will be attached to a
2607 section with relocs, but if we are creating the descriptor
2608 ourselves those relocs will not be visible to the mark code. */
2609 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2611 if (! xcoff_mark_symbol (info, h->descriptor))
2612 return FALSE;
2615 return TRUE;
2618 /* Count a reloc against a symbol. This is called for relocs
2619 generated by the linker script, typically for global constructors
2620 and destructors. */
2622 bfd_boolean
2623 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2624 struct bfd_link_info *info,
2625 const char *name)
2627 struct xcoff_link_hash_entry *h;
2629 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2630 return TRUE;
2632 h = ((struct xcoff_link_hash_entry *)
2633 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2634 FALSE));
2635 if (h == NULL)
2637 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2638 bfd_set_error (bfd_error_no_symbols);
2639 return FALSE;
2642 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2643 ++xcoff_hash_table (info)->ldrel_count;
2645 /* Mark the symbol to avoid garbage collection. */
2646 if (! xcoff_mark_symbol (info, h))
2647 return FALSE;
2649 return TRUE;
2652 /* This function is called for each symbol to which the linker script
2653 assigns a value. */
2655 bfd_boolean
2656 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2657 struct bfd_link_info *info,
2658 const char *name)
2660 struct xcoff_link_hash_entry *h;
2662 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2663 return TRUE;
2665 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2666 FALSE);
2667 if (h == NULL)
2668 return FALSE;
2670 h->flags |= XCOFF_DEF_REGULAR;
2672 return TRUE;
2675 /* Add a symbol to the .loader symbols, if necessary. */
2677 static bfd_boolean
2678 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
2680 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2681 bfd_size_type amt;
2683 if (h->root.type == bfd_link_hash_warning)
2684 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
2686 /* __rtinit, this symbol has special handling. */
2687 if (h->flags & XCOFF_RTINIT)
2688 return TRUE;
2690 /* If this is a final link, and the symbol was defined as a common
2691 symbol in a regular object file, and there was no definition in
2692 any dynamic object, then the linker will have allocated space for
2693 the symbol in a common section but the XCOFF_DEF_REGULAR flag
2694 will not have been set. */
2695 if (h->root.type == bfd_link_hash_defined
2696 && (h->flags & XCOFF_DEF_REGULAR) == 0
2697 && (h->flags & XCOFF_REF_REGULAR) != 0
2698 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2699 && (bfd_is_abs_section (h->root.u.def.section)
2700 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
2701 h->flags |= XCOFF_DEF_REGULAR;
2703 /* If all defined symbols should be exported, mark them now. We
2704 don't want to export the actual functions, just the function
2705 descriptors. */
2706 if (ldinfo->export_defineds
2707 && (h->flags & XCOFF_DEF_REGULAR) != 0
2708 && h->root.root.string[0] != '.')
2710 bfd_boolean export;
2712 /* We don't export a symbol which is being defined by an object
2713 included from an archive which contains a shared object. The
2714 rationale is that if an archive contains both an unshared and
2715 a shared object, then there must be some reason that the
2716 unshared object is unshared, and we don't want to start
2717 providing a shared version of it. In particular, this solves
2718 a bug involving the _savefNN set of functions. gcc will call
2719 those functions without providing a slot to restore the TOC,
2720 so it is essential that these functions be linked in directly
2721 and not from a shared object, which means that a shared
2722 object which also happens to link them in must not export
2723 them. This is confusing, but I haven't been able to think of
2724 a different approach. Note that the symbols can, of course,
2725 be exported explicitly. */
2726 export = TRUE;
2727 if ((h->root.type == bfd_link_hash_defined
2728 || h->root.type == bfd_link_hash_defweak)
2729 && h->root.u.def.section->owner != NULL
2730 && h->root.u.def.section->owner->my_archive != NULL)
2732 bfd *arbfd, *member;
2734 arbfd = h->root.u.def.section->owner->my_archive;
2735 member = bfd_openr_next_archived_file (arbfd, NULL);
2736 while (member != NULL)
2738 if ((member->flags & DYNAMIC) != 0)
2740 export = FALSE;
2741 break;
2743 member = bfd_openr_next_archived_file (arbfd, member);
2747 if (export)
2748 h->flags |= XCOFF_EXPORT;
2751 /* We don't want to garbage collect symbols which are not defined in
2752 XCOFF files. This is a convenient place to mark them. */
2753 if (xcoff_hash_table (ldinfo->info)->gc
2754 && (h->flags & XCOFF_MARK) == 0
2755 && (h->root.type == bfd_link_hash_defined
2756 || h->root.type == bfd_link_hash_defweak)
2757 && (h->root.u.def.section->owner == NULL
2758 || (h->root.u.def.section->owner->xvec
2759 != ldinfo->info->hash->creator)))
2760 h->flags |= XCOFF_MARK;
2762 /* If this symbol is called and defined in a dynamic object, or it
2763 is imported, then we need to set up global linkage code for it.
2764 (Unless we did garbage collection and we didn't need this
2765 symbol.) */
2766 if ((h->flags & XCOFF_CALLED) != 0
2767 && (h->root.type == bfd_link_hash_undefined
2768 || h->root.type == bfd_link_hash_undefweak)
2769 && h->root.root.string[0] == '.'
2770 && h->descriptor != NULL
2771 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2772 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2773 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
2774 && (! xcoff_hash_table (ldinfo->info)->gc
2775 || (h->flags & XCOFF_MARK) != 0))
2777 asection *sec;
2778 struct xcoff_link_hash_entry *hds;
2780 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2781 h->root.type = bfd_link_hash_defined;
2782 h->root.u.def.section = sec;
2783 h->root.u.def.value = sec->size;
2784 h->smclas = XMC_GL;
2785 h->flags |= XCOFF_DEF_REGULAR;
2786 sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
2788 /* The global linkage code requires a TOC entry for the
2789 descriptor. */
2790 hds = h->descriptor;
2791 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2792 || hds->root.type == bfd_link_hash_undefweak)
2793 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2794 hds->flags |= XCOFF_MARK;
2795 if (hds->toc_section == NULL)
2797 int byte_size;
2799 /* 32 vs 64
2800 xcoff32 uses 4 bytes in the toc.
2801 xcoff64 uses 8 bytes in the toc. */
2802 if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
2803 byte_size = 8;
2804 else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
2805 byte_size = 4;
2806 else
2807 return FALSE;
2809 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2810 hds->u.toc_offset = hds->toc_section->size;
2811 hds->toc_section->size += byte_size;
2812 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2813 ++hds->toc_section->reloc_count;
2814 hds->indx = -2;
2815 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2817 /* We need to call xcoff_build_ldsyms recursively here,
2818 because we may already have passed hds on the traversal. */
2819 xcoff_build_ldsyms (hds, p);
2823 /* If this symbol is exported, but not defined, we need to try to
2824 define it. */
2825 if ((h->flags & XCOFF_EXPORT) != 0
2826 && (h->flags & XCOFF_IMPORT) == 0
2827 && (h->flags & XCOFF_DEF_REGULAR) == 0
2828 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2829 && (h->root.type == bfd_link_hash_undefined
2830 || h->root.type == bfd_link_hash_undefweak))
2832 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2833 && (h->descriptor->root.type == bfd_link_hash_defined
2834 || h->descriptor->root.type == bfd_link_hash_defweak))
2836 asection *sec;
2838 /* This is an undefined function descriptor associated with
2839 a defined entry point. We can build up a function
2840 descriptor ourselves. Believe it or not, the AIX linker
2841 actually does this, and there are cases where we need to
2842 do it as well. */
2843 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
2844 h->root.type = bfd_link_hash_defined;
2845 h->root.u.def.section = sec;
2846 h->root.u.def.value = sec->size;
2847 h->smclas = XMC_DS;
2848 h->flags |= XCOFF_DEF_REGULAR;
2850 /* The size of the function descriptor depends if this is an
2851 xcoff32 (12) or xcoff64 (24). */
2852 sec->size +=
2853 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
2855 /* A function descriptor uses two relocs: one for the
2856 associated code, and one for the TOC address. */
2857 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
2858 sec->reloc_count += 2;
2860 /* We handle writing out the contents of the descriptor in
2861 xcoff_write_global_symbol. */
2863 else
2865 (*_bfd_error_handler)
2866 (_("warning: attempt to export undefined symbol `%s'"),
2867 h->root.root.string);
2868 h->ldsym = NULL;
2869 return TRUE;
2873 /* If this is still a common symbol, and it wasn't garbage
2874 collected, we need to actually allocate space for it in the .bss
2875 section. */
2876 if (h->root.type == bfd_link_hash_common
2877 && (! xcoff_hash_table (ldinfo->info)->gc
2878 || (h->flags & XCOFF_MARK) != 0)
2879 && h->root.u.c.p->section->size == 0)
2881 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2882 h->root.u.c.p->section->size = h->root.u.c.size;
2885 /* We need to add a symbol to the .loader section if it is mentioned
2886 in a reloc which we are copying to the .loader section and it was
2887 not defined or common, or if it is the entry point, or if it is
2888 being exported. */
2890 if (((h->flags & XCOFF_LDREL) == 0
2891 || h->root.type == bfd_link_hash_defined
2892 || h->root.type == bfd_link_hash_defweak
2893 || h->root.type == bfd_link_hash_common)
2894 && (h->flags & XCOFF_ENTRY) == 0
2895 && (h->flags & XCOFF_EXPORT) == 0)
2897 h->ldsym = NULL;
2898 return TRUE;
2901 /* We don't need to add this symbol if we did garbage collection and
2902 we did not mark this symbol. */
2903 if (xcoff_hash_table (ldinfo->info)->gc
2904 && (h->flags & XCOFF_MARK) == 0)
2906 h->ldsym = NULL;
2907 return TRUE;
2910 /* We may have already processed this symbol due to the recursive
2911 call above. */
2912 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2913 return TRUE;
2915 /* We need to add this symbol to the .loader symbols. */
2917 BFD_ASSERT (h->ldsym == NULL);
2918 amt = sizeof (struct internal_ldsym);
2919 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
2920 if (h->ldsym == NULL)
2922 ldinfo->failed = TRUE;
2923 return FALSE;
2926 if ((h->flags & XCOFF_IMPORT) != 0)
2927 h->ldsym->l_ifile = h->ldindx;
2929 /* The first 3 symbol table indices are reserved to indicate the
2930 data, text and bss sections. */
2931 h->ldindx = ldinfo->ldsym_count + 3;
2933 ++ldinfo->ldsym_count;
2935 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
2936 h->ldsym, h->root.root.string))
2937 return FALSE;
2939 h->flags |= XCOFF_BUILT_LDSYM;
2941 return TRUE;
2943 /* Build the .loader section. This is called by the XCOFF linker
2944 emulation before_allocation routine. We must set the size of the
2945 .loader section before the linker lays out the output file.
2946 LIBPATH is the library path to search for shared objects; this is
2947 normally built from the -L arguments passed to the linker. ENTRY
2948 is the name of the entry point symbol (the -e linker option).
2949 FILE_ALIGN is the alignment to use for sections within the file
2950 (the -H linker option). MAXSTACK is the maximum stack size (the
2951 -bmaxstack linker option). MAXDATA is the maximum data size (the
2952 -bmaxdata linker option). GC is whether to do garbage collection
2953 (the -bgc linker option). MODTYPE is the module type (the
2954 -bmodtype linker option). TEXTRO is whether the text section must
2955 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2956 whether all defined symbols should be exported (the -unix linker
2957 option). SPECIAL_SECTIONS is set by this routine to csects with
2958 magic names like _end. */
2960 bfd_boolean
2961 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
2962 struct bfd_link_info *info,
2963 const char *libpath,
2964 const char *entry,
2965 unsigned long file_align,
2966 unsigned long maxstack,
2967 unsigned long maxdata,
2968 bfd_boolean gc,
2969 int modtype,
2970 bfd_boolean textro,
2971 bfd_boolean export_defineds,
2972 asection **special_sections,
2973 bfd_boolean rtld)
2975 struct xcoff_link_hash_entry *hentry;
2976 asection *lsec;
2977 struct xcoff_loader_info ldinfo;
2978 int i;
2979 size_t impsize, impcount;
2980 struct xcoff_import_file *fl;
2981 struct internal_ldhdr *ldhdr;
2982 bfd_size_type stoff;
2983 char *out;
2984 asection *sec;
2985 bfd *sub;
2986 struct bfd_strtab_hash *debug_strtab;
2987 bfd_byte *debug_contents = NULL;
2988 bfd_size_type amt;
2990 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2992 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2993 special_sections[i] = NULL;
2994 return TRUE;
2997 ldinfo.failed = FALSE;
2998 ldinfo.output_bfd = output_bfd;
2999 ldinfo.info = info;
3000 ldinfo.export_defineds = export_defineds;
3001 ldinfo.ldsym_count = 0;
3002 ldinfo.string_size = 0;
3003 ldinfo.strings = NULL;
3004 ldinfo.string_alc = 0;
3006 xcoff_data (output_bfd)->maxstack = maxstack;
3007 xcoff_data (output_bfd)->maxdata = maxdata;
3008 xcoff_data (output_bfd)->modtype = modtype;
3010 xcoff_hash_table (info)->file_align = file_align;
3011 xcoff_hash_table (info)->textro = textro;
3013 hentry = NULL;
3014 if (entry != NULL)
3016 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3017 FALSE, FALSE, TRUE);
3018 if (hentry != NULL)
3019 hentry->flags |= XCOFF_ENTRY;
3022 /* __rtinit */
3023 if (info->init_function || info->fini_function || rtld)
3025 struct xcoff_link_hash_entry *hsym;
3026 struct internal_ldsym *ldsym;
3028 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3029 "__rtinit", FALSE, FALSE, TRUE);
3030 if (hsym == NULL)
3032 (*_bfd_error_handler)
3033 (_("error: undefined symbol __rtinit"));
3034 return FALSE;
3037 xcoff_mark_symbol (info, hsym);
3038 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3040 /* __rtinit initialized. */
3041 amt = sizeof (* ldsym);
3042 ldsym = bfd_malloc (amt);
3044 ldsym->l_value = 0; /* Will be filled in later. */
3045 ldsym->l_scnum = 2; /* Data section. */
3046 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3047 ldsym->l_smclas = 5; /* .rw. */
3048 ldsym->l_ifile = 0; /* Special system loader symbol. */
3049 ldsym->l_parm = 0; /* NA. */
3051 /* Force __rtinit to be the first symbol in the loader symbol table
3052 See xcoff_build_ldsyms
3054 The first 3 symbol table indices are reserved to indicate the data,
3055 text and bss sections. */
3056 BFD_ASSERT (0 == ldinfo.ldsym_count);
3058 hsym->ldindx = 3;
3059 ldinfo.ldsym_count = 1;
3060 hsym->ldsym = ldsym;
3062 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3063 hsym->ldsym, hsym->root.root.string))
3064 return FALSE;
3066 /* This symbol is written out by xcoff_write_global_symbol
3067 Set stuff up so xcoff_write_global_symbol logic works. */
3068 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3069 hsym->root.type = bfd_link_hash_defined;
3070 hsym->root.u.def.value = 0;
3073 /* Garbage collect unused sections. */
3074 if (info->relocatable
3075 || ! gc
3076 || hentry == NULL
3077 || (hentry->root.type != bfd_link_hash_defined
3078 && hentry->root.type != bfd_link_hash_defweak))
3080 gc = FALSE;
3081 xcoff_hash_table (info)->gc = FALSE;
3083 /* We still need to call xcoff_mark, in order to set ldrel_count
3084 correctly. */
3085 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3087 asection *o;
3089 for (o = sub->sections; o != NULL; o = o->next)
3091 if ((o->flags & SEC_MARK) == 0)
3093 if (! xcoff_mark (info, o))
3094 goto error_return;
3099 else
3101 if (! xcoff_mark (info, hentry->root.u.def.section))
3102 goto error_return;
3103 xcoff_sweep (info);
3104 xcoff_hash_table (info)->gc = TRUE;
3107 /* Return special sections to the caller. */
3108 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3110 sec = xcoff_hash_table (info)->special_sections[i];
3112 if (sec != NULL
3113 && gc
3114 && (sec->flags & SEC_MARK) == 0)
3115 sec = NULL;
3117 special_sections[i] = sec;
3120 if (info->input_bfds == NULL)
3121 /* I'm not sure what to do in this bizarre case. */
3122 return TRUE;
3124 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3125 (void *) &ldinfo);
3126 if (ldinfo.failed)
3127 goto error_return;
3129 /* Work out the size of the import file names. Each import file ID
3130 consists of three null terminated strings: the path, the file
3131 name, and the archive member name. The first entry in the list
3132 of names is the path to use to find objects, which the linker has
3133 passed in as the libpath argument. For some reason, the path
3134 entry in the other import file names appears to always be empty. */
3135 impsize = strlen (libpath) + 3;
3136 impcount = 1;
3137 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3139 ++impcount;
3140 impsize += (strlen (fl->path)
3141 + strlen (fl->file)
3142 + strlen (fl->member)
3143 + 3);
3146 /* Set up the .loader section header. */
3147 ldhdr = &xcoff_hash_table (info)->ldhdr;
3148 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3149 ldhdr->l_nsyms = ldinfo.ldsym_count;
3150 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3151 ldhdr->l_istlen = impsize;
3152 ldhdr->l_nimpid = impcount;
3153 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3154 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3155 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3156 ldhdr->l_stlen = ldinfo.string_size;
3157 stoff = ldhdr->l_impoff + impsize;
3158 if (ldinfo.string_size == 0)
3159 ldhdr->l_stoff = 0;
3160 else
3161 ldhdr->l_stoff = stoff;
3163 /* 64 bit elements to ldhdr
3164 The swap out routine for 32 bit will ignore them.
3165 Nothing fancy, symbols come after the header and relocs come
3166 after symbols. */
3167 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3168 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3169 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3171 /* We now know the final size of the .loader section. Allocate
3172 space for it. */
3173 lsec = xcoff_hash_table (info)->loader_section;
3174 lsec->size = stoff + ldhdr->l_stlen;
3175 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3176 if (lsec->contents == NULL)
3177 goto error_return;
3179 /* Set up the header. */
3180 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3182 /* Set up the import file names. */
3183 out = (char *) lsec->contents + ldhdr->l_impoff;
3184 strcpy (out, libpath);
3185 out += strlen (libpath) + 1;
3186 *out++ = '\0';
3187 *out++ = '\0';
3188 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3190 const char *s;
3192 s = fl->path;
3193 while ((*out++ = *s++) != '\0')
3195 s = fl->file;
3196 while ((*out++ = *s++) != '\0')
3198 s = fl->member;
3199 while ((*out++ = *s++) != '\0')
3203 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3205 /* Set up the symbol string table. */
3206 if (ldinfo.string_size > 0)
3208 memcpy (out, ldinfo.strings, ldinfo.string_size);
3209 free (ldinfo.strings);
3210 ldinfo.strings = NULL;
3213 /* We can't set up the symbol table or the relocs yet, because we
3214 don't yet know the final position of the various sections. The
3215 .loader symbols are written out when the corresponding normal
3216 symbols are written out in xcoff_link_input_bfd or
3217 xcoff_write_global_symbol. The .loader relocs are written out
3218 when the corresponding normal relocs are handled in
3219 xcoff_link_input_bfd. */
3221 /* Allocate space for the magic sections. */
3222 sec = xcoff_hash_table (info)->linkage_section;
3223 if (sec->size > 0)
3225 sec->contents = bfd_zalloc (output_bfd, sec->size);
3226 if (sec->contents == NULL)
3227 goto error_return;
3229 sec = xcoff_hash_table (info)->toc_section;
3230 if (sec->size > 0)
3232 sec->contents = bfd_zalloc (output_bfd, sec->size);
3233 if (sec->contents == NULL)
3234 goto error_return;
3236 sec = xcoff_hash_table (info)->descriptor_section;
3237 if (sec->size > 0)
3239 sec->contents = bfd_zalloc (output_bfd, sec->size);
3240 if (sec->contents == NULL)
3241 goto error_return;
3244 /* Now that we've done garbage collection, figure out the contents
3245 of the .debug section. */
3246 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3248 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3250 asection *subdeb;
3251 bfd_size_type symcount;
3252 unsigned long *debug_index;
3253 asection **csectpp;
3254 bfd_byte *esym, *esymend;
3255 bfd_size_type symesz;
3257 if (sub->xvec != info->hash->creator)
3258 continue;
3259 subdeb = bfd_get_section_by_name (sub, ".debug");
3260 if (subdeb == NULL || subdeb->size == 0)
3261 continue;
3263 if (info->strip == strip_all
3264 || info->strip == strip_debugger
3265 || info->discard == discard_all)
3267 subdeb->size = 0;
3268 continue;
3271 if (! _bfd_coff_get_external_symbols (sub))
3272 goto error_return;
3274 symcount = obj_raw_syment_count (sub);
3275 debug_index = bfd_zalloc (sub, symcount * sizeof (unsigned long));
3276 if (debug_index == NULL)
3277 goto error_return;
3278 xcoff_data (sub)->debug_indices = debug_index;
3280 /* Grab the contents of the .debug section. We use malloc and
3281 copy the names into the debug stringtab, rather than
3282 bfd_alloc, because I expect that, when linking many files
3283 together, many of the strings will be the same. Storing the
3284 strings in the hash table should save space in this case. */
3285 if (! bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3286 goto error_return;
3288 csectpp = xcoff_data (sub)->csects;
3290 /* Dynamic object do not have csectpp's. */
3291 if (NULL != csectpp)
3293 symesz = bfd_coff_symesz (sub);
3294 esym = (bfd_byte *) obj_coff_external_syms (sub);
3295 esymend = esym + symcount * symesz;
3297 while (esym < esymend)
3299 struct internal_syment sym;
3301 bfd_coff_swap_sym_in (sub, (void *) esym, (void *) &sym);
3303 *debug_index = (unsigned long) -1;
3305 if (sym._n._n_n._n_zeroes == 0
3306 && *csectpp != NULL
3307 && (! gc
3308 || ((*csectpp)->flags & SEC_MARK) != 0
3309 || *csectpp == bfd_abs_section_ptr)
3310 && bfd_coff_symname_in_debug (sub, &sym))
3312 char *name;
3313 bfd_size_type indx;
3315 name = (char *) debug_contents + sym._n._n_n._n_offset;
3316 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3317 if (indx == (bfd_size_type) -1)
3318 goto error_return;
3319 *debug_index = indx;
3322 esym += (sym.n_numaux + 1) * symesz;
3323 csectpp += sym.n_numaux + 1;
3324 debug_index += sym.n_numaux + 1;
3328 free (debug_contents);
3329 debug_contents = NULL;
3331 /* Clear the size of subdeb, so that it is not included directly
3332 in the output file. */
3333 subdeb->size = 0;
3335 if (! info->keep_memory)
3337 if (! _bfd_coff_free_symbols (sub))
3338 goto error_return;
3342 if (info->strip != strip_all)
3343 xcoff_hash_table (info)->debug_section->size =
3344 _bfd_stringtab_size (debug_strtab);
3346 return TRUE;
3348 error_return:
3349 if (ldinfo.strings != NULL)
3350 free (ldinfo.strings);
3351 if (debug_contents != NULL)
3352 free (debug_contents);
3353 return FALSE;
3356 bfd_boolean
3357 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3358 const char *init,
3359 const char *fini,
3360 bfd_boolean rtld)
3362 struct bfd_in_memory *bim;
3364 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3365 if (bim == NULL)
3366 return FALSE;
3368 bim->size = 0;
3369 bim->buffer = 0;
3371 abfd->link_next = 0;
3372 abfd->format = bfd_object;
3373 abfd->iostream = (void *) bim;
3374 abfd->flags = BFD_IN_MEMORY;
3375 abfd->direction = write_direction;
3376 abfd->where = 0;
3378 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3379 return FALSE;
3381 /* need to reset to unknown or it will not be read back in correctly */
3382 abfd->format = bfd_unknown;
3383 abfd->direction = read_direction;
3384 abfd->where = 0;
3386 return TRUE;
3389 /* Link an input file into the linker output file. This function
3390 handles all the sections and relocations of the input file at once. */
3392 static bfd_boolean
3393 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3394 bfd *input_bfd)
3396 bfd *output_bfd;
3397 const char *strings;
3398 bfd_size_type syment_base;
3399 unsigned int n_tmask;
3400 unsigned int n_btshft;
3401 bfd_boolean copy, hash;
3402 bfd_size_type isymesz;
3403 bfd_size_type osymesz;
3404 bfd_size_type linesz;
3405 bfd_byte *esym;
3406 bfd_byte *esym_end;
3407 struct xcoff_link_hash_entry **sym_hash;
3408 struct internal_syment *isymp;
3409 asection **csectpp;
3410 unsigned long *debug_index;
3411 long *indexp;
3412 unsigned long output_index;
3413 bfd_byte *outsym;
3414 unsigned int incls;
3415 asection *oline;
3416 bfd_boolean keep_syms;
3417 asection *o;
3419 /* We can just skip DYNAMIC files, unless this is a static link. */
3420 if ((input_bfd->flags & DYNAMIC) != 0
3421 && ! finfo->info->static_link)
3422 return TRUE;
3424 /* Move all the symbols to the output file. */
3425 output_bfd = finfo->output_bfd;
3426 strings = NULL;
3427 syment_base = obj_raw_syment_count (output_bfd);
3428 isymesz = bfd_coff_symesz (input_bfd);
3429 osymesz = bfd_coff_symesz (output_bfd);
3430 linesz = bfd_coff_linesz (input_bfd);
3431 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3433 n_tmask = coff_data (input_bfd)->local_n_tmask;
3434 n_btshft = coff_data (input_bfd)->local_n_btshft;
3436 /* Define macros so that ISFCN, et. al., macros work correctly. */
3437 #define N_TMASK n_tmask
3438 #define N_BTSHFT n_btshft
3440 copy = FALSE;
3441 if (! finfo->info->keep_memory)
3442 copy = TRUE;
3443 hash = TRUE;
3444 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3445 hash = FALSE;
3447 if (! _bfd_coff_get_external_symbols (input_bfd))
3448 return FALSE;
3450 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3451 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3452 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3453 csectpp = xcoff_data (input_bfd)->csects;
3454 debug_index = xcoff_data (input_bfd)->debug_indices;
3455 isymp = finfo->internal_syms;
3456 indexp = finfo->sym_indices;
3457 output_index = syment_base;
3458 outsym = finfo->outsyms;
3459 incls = 0;
3460 oline = NULL;
3462 while (esym < esym_end)
3464 struct internal_syment isym;
3465 union internal_auxent aux;
3466 int smtyp = 0;
3467 bfd_boolean skip;
3468 bfd_boolean require;
3469 int add;
3471 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3473 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3474 information. */
3475 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3477 BFD_ASSERT (isymp->n_numaux > 0);
3478 bfd_coff_swap_aux_in (input_bfd,
3479 (void *) (esym + isymesz * isymp->n_numaux),
3480 isymp->n_type, isymp->n_sclass,
3481 isymp->n_numaux - 1, isymp->n_numaux,
3482 (void *) &aux);
3484 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3487 /* Make a copy of *isymp so that the relocate_section function
3488 always sees the original values. This is more reliable than
3489 always recomputing the symbol value even if we are stripping
3490 the symbol. */
3491 isym = *isymp;
3493 /* If this symbol is in the .loader section, swap out the
3494 .loader symbol information. If this is an external symbol
3495 reference to a defined symbol, though, then wait until we get
3496 to the definition. */
3497 if (isym.n_sclass == C_EXT
3498 && *sym_hash != NULL
3499 && (*sym_hash)->ldsym != NULL
3500 && (smtyp != XTY_ER
3501 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3503 struct xcoff_link_hash_entry *h;
3504 struct internal_ldsym *ldsym;
3506 h = *sym_hash;
3507 ldsym = h->ldsym;
3508 if (isym.n_scnum > 0)
3510 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3511 ldsym->l_value = (isym.n_value
3512 + (*csectpp)->output_section->vma
3513 + (*csectpp)->output_offset
3514 - (*csectpp)->vma);
3516 else
3518 ldsym->l_scnum = isym.n_scnum;
3519 ldsym->l_value = isym.n_value;
3522 ldsym->l_smtype = smtyp;
3523 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3524 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3525 || (h->flags & XCOFF_IMPORT) != 0)
3526 ldsym->l_smtype |= L_IMPORT;
3527 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3528 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3529 || (h->flags & XCOFF_EXPORT) != 0)
3530 ldsym->l_smtype |= L_EXPORT;
3531 if ((h->flags & XCOFF_ENTRY) != 0)
3532 ldsym->l_smtype |= L_ENTRY;
3534 ldsym->l_smclas = aux.x_csect.x_smclas;
3536 if (ldsym->l_ifile == (bfd_size_type) -1)
3537 ldsym->l_ifile = 0;
3538 else if (ldsym->l_ifile == 0)
3540 if ((ldsym->l_smtype & L_IMPORT) == 0)
3541 ldsym->l_ifile = 0;
3542 else
3544 bfd *impbfd;
3546 if (h->root.type == bfd_link_hash_defined
3547 || h->root.type == bfd_link_hash_defweak)
3548 impbfd = h->root.u.def.section->owner;
3549 else if (h->root.type == bfd_link_hash_undefined
3550 || h->root.type == bfd_link_hash_undefweak)
3551 impbfd = h->root.u.undef.abfd;
3552 else
3553 impbfd = NULL;
3555 if (impbfd == NULL)
3556 ldsym->l_ifile = 0;
3557 else
3559 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3560 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3565 ldsym->l_parm = 0;
3567 BFD_ASSERT (h->ldindx >= 0);
3568 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3569 (finfo->ldsym
3570 + ((h->ldindx - 3)
3571 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3572 h->ldsym = NULL;
3574 /* Fill in snentry now that we know the target_index. */
3575 if ((h->flags & XCOFF_ENTRY) != 0
3576 && (h->root.type == bfd_link_hash_defined
3577 || h->root.type == bfd_link_hash_defweak))
3579 xcoff_data (output_bfd)->snentry =
3580 h->root.u.def.section->output_section->target_index;
3584 *indexp = -1;
3586 skip = FALSE;
3587 require = FALSE;
3588 add = 1 + isym.n_numaux;
3590 /* If we are skipping this csect, we want to skip this symbol. */
3591 if (*csectpp == NULL)
3592 skip = TRUE;
3594 /* If we garbage collected this csect, we want to skip this
3595 symbol. */
3596 if (! skip
3597 && xcoff_hash_table (finfo->info)->gc
3598 && ((*csectpp)->flags & SEC_MARK) == 0
3599 && *csectpp != bfd_abs_section_ptr)
3600 skip = TRUE;
3602 /* An XCOFF linker always skips C_STAT symbols. */
3603 if (! skip
3604 && isymp->n_sclass == C_STAT)
3605 skip = TRUE;
3607 /* We skip all but the first TOC anchor. */
3608 if (! skip
3609 && isymp->n_sclass == C_HIDEXT
3610 && aux.x_csect.x_smclas == XMC_TC0)
3612 if (finfo->toc_symindx != -1)
3613 skip = TRUE;
3614 else
3616 bfd_vma tocval, tocend;
3617 bfd *inp;
3619 tocval = ((*csectpp)->output_section->vma
3620 + (*csectpp)->output_offset
3621 + isym.n_value
3622 - (*csectpp)->vma);
3624 /* We want to find out if tocval is a good value to use
3625 as the TOC anchor--that is, whether we can access all
3626 of the TOC using a 16 bit offset from tocval. This
3627 test assumes that the TOC comes at the end of the
3628 output section, as it does in the default linker
3629 script. */
3630 tocend = ((*csectpp)->output_section->vma
3631 + (*csectpp)->output_section->size);
3632 for (inp = finfo->info->input_bfds;
3633 inp != NULL;
3634 inp = inp->link_next)
3637 for (o = inp->sections; o != NULL; o = o->next)
3638 if (strcmp (o->name, ".tocbss") == 0)
3640 bfd_vma new_toc_end;
3641 new_toc_end = (o->output_section->vma
3642 + o->output_offset
3643 + o->size);
3644 if (new_toc_end > tocend)
3645 tocend = new_toc_end;
3650 if (tocval + 0x10000 < tocend)
3652 (*_bfd_error_handler)
3653 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
3654 (unsigned long) (tocend - tocval));
3655 bfd_set_error (bfd_error_file_too_big);
3656 return FALSE;
3659 if (tocval + 0x8000 < tocend)
3661 bfd_vma tocadd;
3663 tocadd = tocend - (tocval + 0x8000);
3664 tocval += tocadd;
3665 isym.n_value += tocadd;
3668 finfo->toc_symindx = output_index;
3669 xcoff_data (finfo->output_bfd)->toc = tocval;
3670 xcoff_data (finfo->output_bfd)->sntoc =
3671 (*csectpp)->output_section->target_index;
3672 require = TRUE;
3677 /* If we are stripping all symbols, we want to skip this one. */
3678 if (! skip
3679 && finfo->info->strip == strip_all)
3680 skip = TRUE;
3682 /* We can skip resolved external references. */
3683 if (! skip
3684 && isym.n_sclass == C_EXT
3685 && smtyp == XTY_ER
3686 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3687 skip = TRUE;
3689 /* We can skip common symbols if they got defined somewhere
3690 else. */
3691 if (! skip
3692 && isym.n_sclass == C_EXT
3693 && smtyp == XTY_CM
3694 && ((*sym_hash)->root.type != bfd_link_hash_common
3695 || (*sym_hash)->root.u.c.p->section != *csectpp)
3696 && ((*sym_hash)->root.type != bfd_link_hash_defined
3697 || (*sym_hash)->root.u.def.section != *csectpp))
3698 skip = TRUE;
3700 /* Skip local symbols if we are discarding them. */
3701 if (! skip
3702 && finfo->info->discard == discard_all
3703 && isym.n_sclass != C_EXT
3704 && (isym.n_sclass != C_HIDEXT
3705 || smtyp != XTY_SD))
3706 skip = TRUE;
3708 /* If we stripping debugging symbols, and this is a debugging
3709 symbol, then skip it. */
3710 if (! skip
3711 && finfo->info->strip == strip_debugger
3712 && isym.n_scnum == N_DEBUG)
3713 skip = TRUE;
3715 /* If some symbols are stripped based on the name, work out the
3716 name and decide whether to skip this symbol. We don't handle
3717 this correctly for symbols whose names are in the .debug
3718 section; to get it right we would need a new bfd_strtab_hash
3719 function to return the string given the index. */
3720 if (! skip
3721 && (finfo->info->strip == strip_some
3722 || finfo->info->discard == discard_l)
3723 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3725 const char *name;
3726 char buf[SYMNMLEN + 1];
3728 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3730 if (name == NULL)
3731 return FALSE;
3733 if ((finfo->info->strip == strip_some
3734 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
3735 FALSE) == NULL))
3736 || (finfo->info->discard == discard_l
3737 && (isym.n_sclass != C_EXT
3738 && (isym.n_sclass != C_HIDEXT
3739 || smtyp != XTY_SD))
3740 && bfd_is_local_label_name (input_bfd, name)))
3741 skip = TRUE;
3744 /* We can not skip the first TOC anchor. */
3745 if (skip
3746 && require
3747 && finfo->info->strip != strip_all)
3748 skip = FALSE;
3750 /* We now know whether we are to skip this symbol or not. */
3751 if (! skip)
3753 /* Adjust the symbol in order to output it. */
3755 if (isym._n._n_n._n_zeroes == 0
3756 && isym._n._n_n._n_offset != 0)
3758 /* This symbol has a long name. Enter it in the string
3759 table we are building. If *debug_index != -1, the
3760 name has already been entered in the .debug section. */
3761 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3762 isym._n._n_n._n_offset = *debug_index;
3763 else
3765 const char *name;
3766 bfd_size_type indx;
3768 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
3770 if (name == NULL)
3771 return FALSE;
3772 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3773 if (indx == (bfd_size_type) -1)
3774 return FALSE;
3775 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3779 if (isym.n_sclass != C_BSTAT
3780 && isym.n_sclass != C_ESTAT
3781 && isym.n_sclass != C_DECL
3782 && isym.n_scnum > 0)
3784 isym.n_scnum = (*csectpp)->output_section->target_index;
3785 isym.n_value += ((*csectpp)->output_section->vma
3786 + (*csectpp)->output_offset
3787 - (*csectpp)->vma);
3790 /* The value of a C_FILE symbol is the symbol index of the
3791 next C_FILE symbol. The value of the last C_FILE symbol
3792 is -1. We try to get this right, below, just before we
3793 write the symbols out, but in the general case we may
3794 have to write the symbol out twice. */
3795 if (isym.n_sclass == C_FILE)
3797 if (finfo->last_file_index != -1
3798 && finfo->last_file.n_value != (bfd_vma) output_index)
3800 /* We must correct the value of the last C_FILE entry. */
3801 finfo->last_file.n_value = output_index;
3802 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3804 /* The last C_FILE symbol is in this input file. */
3805 bfd_coff_swap_sym_out (output_bfd,
3806 (void *) &finfo->last_file,
3807 (void *) (finfo->outsyms
3808 + ((finfo->last_file_index
3809 - syment_base)
3810 * osymesz)));
3812 else
3814 /* We have already written out the last C_FILE
3815 symbol. We need to write it out again. We
3816 borrow *outsym temporarily. */
3817 file_ptr pos;
3819 bfd_coff_swap_sym_out (output_bfd,
3820 (void *) &finfo->last_file,
3821 (void *) outsym);
3823 pos = obj_sym_filepos (output_bfd);
3824 pos += finfo->last_file_index * osymesz;
3825 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3826 || (bfd_bwrite (outsym, osymesz, output_bfd)
3827 != osymesz))
3828 return FALSE;
3832 finfo->last_file_index = output_index;
3833 finfo->last_file = isym;
3836 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3837 into the line numbers. We update the symbol values when
3838 we handle the line numbers. */
3839 if (isym.n_sclass == C_BINCL
3840 || isym.n_sclass == C_EINCL)
3842 isym.n_value = finfo->line_filepos;
3843 ++incls;
3846 /* Output the symbol. */
3848 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
3850 *indexp = output_index;
3852 if (isym.n_sclass == C_EXT)
3854 long indx;
3855 struct xcoff_link_hash_entry *h;
3857 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3858 / isymesz);
3859 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3860 BFD_ASSERT (h != NULL);
3861 h->indx = output_index;
3864 /* If this is a symbol in the TOC which we may have merged
3865 (class XMC_TC), remember the symbol index of the TOC
3866 symbol. */
3867 if (isym.n_sclass == C_HIDEXT
3868 && aux.x_csect.x_smclas == XMC_TC
3869 && *sym_hash != NULL)
3871 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3872 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3873 (*sym_hash)->u.toc_indx = output_index;
3876 output_index += add;
3877 outsym += add * osymesz;
3880 esym += add * isymesz;
3881 isymp += add;
3882 csectpp += add;
3883 sym_hash += add;
3884 if (debug_index != NULL)
3885 debug_index += add;
3886 ++indexp;
3887 for (--add; add > 0; --add)
3888 *indexp++ = -1;
3891 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3892 done in a separate pass, because we don't know the correct symbol
3893 indices until we have already decided which symbols we are going
3894 to keep. */
3896 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3897 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3898 isymp = finfo->internal_syms;
3899 indexp = finfo->sym_indices;
3900 csectpp = xcoff_data (input_bfd)->csects;
3901 outsym = finfo->outsyms;
3902 while (esym < esym_end)
3904 int add;
3906 add = 1 + isymp->n_numaux;
3908 if (*indexp < 0)
3909 esym += add * isymesz;
3910 else
3912 int i;
3914 if (isymp->n_sclass == C_BSTAT)
3916 struct internal_syment isym;
3918 bfd_vma indx;
3920 /* The value of a C_BSTAT symbol is the symbol table
3921 index of the containing csect. */
3922 bfd_coff_swap_sym_in (output_bfd, (void *) outsym, (void *) &isym);
3923 indx = isym.n_value;
3924 if (indx < obj_raw_syment_count (input_bfd))
3926 long symindx;
3928 symindx = finfo->sym_indices[indx];
3929 if (symindx < 0)
3930 isym.n_value = 0;
3931 else
3932 isym.n_value = symindx;
3933 bfd_coff_swap_sym_out (output_bfd, (void *) &isym,
3934 (void *) outsym);
3938 esym += isymesz;
3939 outsym += osymesz;
3941 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3943 union internal_auxent aux;
3945 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
3946 isymp->n_sclass, i, isymp->n_numaux,
3947 (void *) &aux);
3949 if (isymp->n_sclass == C_FILE)
3951 /* This is the file name (or some comment put in by
3952 the compiler). If it is long, we must put it in
3953 the string table. */
3954 if (aux.x_file.x_n.x_zeroes == 0
3955 && aux.x_file.x_n.x_offset != 0)
3957 const char *filename;
3958 bfd_size_type indx;
3960 BFD_ASSERT (aux.x_file.x_n.x_offset
3961 >= STRING_SIZE_SIZE);
3962 if (strings == NULL)
3964 strings = _bfd_coff_read_string_table (input_bfd);
3965 if (strings == NULL)
3966 return FALSE;
3968 filename = strings + aux.x_file.x_n.x_offset;
3969 indx = _bfd_stringtab_add (finfo->strtab, filename,
3970 hash, copy);
3971 if (indx == (bfd_size_type) -1)
3972 return FALSE;
3973 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3976 else if ((isymp->n_sclass == C_EXT
3977 || isymp->n_sclass == C_HIDEXT)
3978 && i + 1 == isymp->n_numaux)
3981 /* We don't support type checking. I don't know if
3982 anybody does. */
3983 aux.x_csect.x_parmhash = 0;
3984 /* I don't think anybody uses these fields, but we'd
3985 better clobber them just in case. */
3986 aux.x_csect.x_stab = 0;
3987 aux.x_csect.x_snstab = 0;
3989 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
3991 unsigned long indx;
3993 indx = aux.x_csect.x_scnlen.l;
3994 if (indx < obj_raw_syment_count (input_bfd))
3996 long symindx;
3998 symindx = finfo->sym_indices[indx];
3999 if (symindx < 0)
4001 aux.x_csect.x_scnlen.l = 0;
4003 else
4005 aux.x_csect.x_scnlen.l = symindx;
4010 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4012 unsigned long indx;
4014 if (ISFCN (isymp->n_type)
4015 || ISTAG (isymp->n_sclass)
4016 || isymp->n_sclass == C_BLOCK
4017 || isymp->n_sclass == C_FCN)
4019 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4020 if (indx > 0
4021 && indx < obj_raw_syment_count (input_bfd))
4023 /* We look forward through the symbol for
4024 the index of the next symbol we are going
4025 to include. I don't know if this is
4026 entirely right. */
4027 while (finfo->sym_indices[indx] < 0
4028 && indx < obj_raw_syment_count (input_bfd))
4029 ++indx;
4030 if (indx >= obj_raw_syment_count (input_bfd))
4031 indx = output_index;
4032 else
4033 indx = finfo->sym_indices[indx];
4034 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4039 indx = aux.x_sym.x_tagndx.l;
4040 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4042 long symindx;
4044 symindx = finfo->sym_indices[indx];
4045 if (symindx < 0)
4046 aux.x_sym.x_tagndx.l = 0;
4047 else
4048 aux.x_sym.x_tagndx.l = symindx;
4053 /* Copy over the line numbers, unless we are stripping
4054 them. We do this on a symbol by symbol basis in
4055 order to more easily handle garbage collection. */
4056 if ((isymp->n_sclass == C_EXT
4057 || isymp->n_sclass == C_HIDEXT)
4058 && i == 0
4059 && isymp->n_numaux > 1
4060 && ISFCN (isymp->n_type)
4061 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4063 if (finfo->info->strip != strip_none
4064 && finfo->info->strip != strip_some)
4065 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4066 else
4068 asection *enclosing;
4069 unsigned int enc_count;
4070 bfd_signed_vma linoff;
4071 struct internal_lineno lin;
4073 o = *csectpp;
4074 enclosing = xcoff_section_data (abfd, o)->enclosing;
4075 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4076 if (oline != enclosing)
4078 file_ptr pos = enclosing->line_filepos;
4079 bfd_size_type amt = linesz * enc_count;
4080 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4081 || (bfd_bread (finfo->linenos, amt, input_bfd)
4082 != amt))
4083 return FALSE;
4084 oline = enclosing;
4087 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4088 - enclosing->line_filepos);
4090 bfd_coff_swap_lineno_in (input_bfd,
4091 (void *) (finfo->linenos + linoff),
4092 (void *) &lin);
4093 if (lin.l_lnno != 0
4094 || ((bfd_size_type) lin.l_addr.l_symndx
4095 != ((esym
4096 - isymesz
4097 - ((bfd_byte *)
4098 obj_coff_external_syms (input_bfd)))
4099 / isymesz)))
4100 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4101 else
4103 bfd_byte *linpend, *linp;
4104 bfd_vma offset;
4105 bfd_size_type count;
4107 lin.l_addr.l_symndx = *indexp;
4108 bfd_coff_swap_lineno_out (output_bfd, (void *) &lin,
4109 (void *) (finfo->linenos
4110 + linoff));
4112 linpend = (finfo->linenos
4113 + enc_count * linesz);
4114 offset = (o->output_section->vma
4115 + o->output_offset
4116 - o->vma);
4117 for (linp = finfo->linenos + linoff + linesz;
4118 linp < linpend;
4119 linp += linesz)
4121 bfd_coff_swap_lineno_in (input_bfd, (void *) linp,
4122 (void *) &lin);
4123 if (lin.l_lnno == 0)
4124 break;
4125 lin.l_addr.l_paddr += offset;
4126 bfd_coff_swap_lineno_out (output_bfd,
4127 (void *) &lin,
4128 (void *) linp);
4131 count = (linp - (finfo->linenos + linoff)) / linesz;
4133 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4134 (o->output_section->line_filepos
4135 + o->output_section->lineno_count * linesz);
4137 if (bfd_seek (output_bfd,
4138 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4139 SEEK_SET) != 0
4140 || (bfd_bwrite (finfo->linenos + linoff,
4141 linesz * count, output_bfd)
4142 != linesz * count))
4143 return FALSE;
4145 o->output_section->lineno_count += count;
4147 if (incls > 0)
4149 struct internal_syment *iisp, *iispend;
4150 long *iindp;
4151 bfd_byte *oos;
4152 int iiadd;
4154 /* Update any C_BINCL or C_EINCL symbols
4155 that refer to a line number in the
4156 range we just output. */
4157 iisp = finfo->internal_syms;
4158 iispend = (iisp
4159 + obj_raw_syment_count (input_bfd));
4160 iindp = finfo->sym_indices;
4161 oos = finfo->outsyms;
4162 while (iisp < iispend)
4164 if (*iindp >= 0
4165 && (iisp->n_sclass == C_BINCL
4166 || iisp->n_sclass == C_EINCL)
4167 && ((bfd_size_type) iisp->n_value
4168 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4169 && ((bfd_size_type) iisp->n_value
4170 < (enclosing->line_filepos
4171 + enc_count * linesz)))
4173 struct internal_syment iis;
4175 bfd_coff_swap_sym_in (output_bfd,
4176 (void *) oos,
4177 (void *) &iis);
4178 iis.n_value =
4179 (iisp->n_value
4180 - enclosing->line_filepos
4181 - linoff
4182 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4183 bfd_coff_swap_sym_out (output_bfd,
4184 (void *) &iis,
4185 (void *) oos);
4186 --incls;
4189 iiadd = 1 + iisp->n_numaux;
4190 if (*iindp >= 0)
4191 oos += iiadd * osymesz;
4192 iisp += iiadd;
4193 iindp += iiadd;
4200 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4201 isymp->n_sclass, i, isymp->n_numaux,
4202 (void *) outsym);
4203 outsym += osymesz;
4204 esym += isymesz;
4208 indexp += add;
4209 isymp += add;
4210 csectpp += add;
4213 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4214 symbol will be the first symbol in the next input file. In the
4215 normal case, this will save us from writing out the C_FILE symbol
4216 again. */
4217 if (finfo->last_file_index != -1
4218 && (bfd_size_type) finfo->last_file_index >= syment_base)
4220 finfo->last_file.n_value = output_index;
4221 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4222 (void *) (finfo->outsyms
4223 + ((finfo->last_file_index - syment_base)
4224 * osymesz)));
4227 /* Write the modified symbols to the output file. */
4228 if (outsym > finfo->outsyms)
4230 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4231 bfd_size_type amt = outsym - finfo->outsyms;
4232 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4233 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4234 return FALSE;
4236 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4237 + (outsym - finfo->outsyms) / osymesz)
4238 == output_index);
4240 obj_raw_syment_count (output_bfd) = output_index;
4243 /* Don't let the linker relocation routines discard the symbols. */
4244 keep_syms = obj_coff_keep_syms (input_bfd);
4245 obj_coff_keep_syms (input_bfd) = TRUE;
4247 /* Relocate the contents of each section. */
4248 for (o = input_bfd->sections; o != NULL; o = o->next)
4250 bfd_byte *contents;
4252 if (! o->linker_mark)
4253 /* This section was omitted from the link. */
4254 continue;
4256 if ((o->flags & SEC_HAS_CONTENTS) == 0
4257 || o->size == 0
4258 || (o->flags & SEC_IN_MEMORY) != 0)
4259 continue;
4261 /* We have set filepos correctly for the sections we created to
4262 represent csects, so bfd_get_section_contents should work. */
4263 if (coff_section_data (input_bfd, o) != NULL
4264 && coff_section_data (input_bfd, o)->contents != NULL)
4265 contents = coff_section_data (input_bfd, o)->contents;
4266 else
4268 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4269 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4270 return FALSE;
4271 contents = finfo->contents;
4274 if ((o->flags & SEC_RELOC) != 0)
4276 int target_index;
4277 struct internal_reloc *internal_relocs;
4278 struct internal_reloc *irel;
4279 bfd_vma offset;
4280 struct internal_reloc *irelend;
4281 struct xcoff_link_hash_entry **rel_hash;
4282 long r_symndx;
4284 /* Read in the relocs. */
4285 target_index = o->output_section->target_index;
4286 internal_relocs = (xcoff_read_internal_relocs
4287 (input_bfd, o, FALSE, finfo->external_relocs,
4288 TRUE,
4289 (finfo->section_info[target_index].relocs
4290 + o->output_section->reloc_count)));
4291 if (internal_relocs == NULL)
4292 return FALSE;
4294 /* Call processor specific code to relocate the section
4295 contents. */
4296 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4297 input_bfd, o,
4298 contents,
4299 internal_relocs,
4300 finfo->internal_syms,
4301 xcoff_data (input_bfd)->csects))
4302 return FALSE;
4304 offset = o->output_section->vma + o->output_offset - o->vma;
4305 irel = internal_relocs;
4306 irelend = irel + o->reloc_count;
4307 rel_hash = (finfo->section_info[target_index].rel_hashes
4308 + o->output_section->reloc_count);
4309 for (; irel < irelend; irel++, rel_hash++)
4311 struct xcoff_link_hash_entry *h = NULL;
4312 struct internal_ldrel ldrel;
4313 bfd_boolean quiet;
4315 *rel_hash = NULL;
4317 /* Adjust the reloc address and symbol index. */
4319 irel->r_vaddr += offset;
4321 r_symndx = irel->r_symndx;
4323 if (r_symndx == -1)
4324 h = NULL;
4325 else
4326 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4328 if (r_symndx != -1 && finfo->info->strip != strip_all)
4330 if (h != NULL
4331 && h->smclas != XMC_TD
4332 && (irel->r_type == R_TOC
4333 || irel->r_type == R_GL
4334 || irel->r_type == R_TCL
4335 || irel->r_type == R_TRL
4336 || irel->r_type == R_TRLA))
4338 /* This is a TOC relative reloc with a symbol
4339 attached. The symbol should be the one which
4340 this reloc is for. We want to make this
4341 reloc against the TOC address of the symbol,
4342 not the symbol itself. */
4343 BFD_ASSERT (h->toc_section != NULL);
4344 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4345 if (h->u.toc_indx != -1)
4346 irel->r_symndx = h->u.toc_indx;
4347 else
4349 struct xcoff_toc_rel_hash *n;
4350 struct xcoff_link_section_info *si;
4351 bfd_size_type amt;
4353 amt = sizeof (* n);
4354 n = bfd_alloc (finfo->output_bfd, amt);
4355 if (n == NULL)
4356 return FALSE;
4357 si = finfo->section_info + target_index;
4358 n->next = si->toc_rel_hashes;
4359 n->h = h;
4360 n->rel = irel;
4361 si->toc_rel_hashes = n;
4364 else if (h != NULL)
4366 /* This is a global symbol. */
4367 if (h->indx >= 0)
4368 irel->r_symndx = h->indx;
4369 else
4371 /* This symbol is being written at the end
4372 of the file, and we do not yet know the
4373 symbol index. We save the pointer to the
4374 hash table entry in the rel_hash list.
4375 We set the indx field to -2 to indicate
4376 that this symbol must not be stripped. */
4377 *rel_hash = h;
4378 h->indx = -2;
4381 else
4383 long indx;
4385 indx = finfo->sym_indices[r_symndx];
4387 if (indx == -1)
4389 struct internal_syment *is;
4391 /* Relocations against a TC0 TOC anchor are
4392 automatically transformed to be against
4393 the TOC anchor in the output file. */
4394 is = finfo->internal_syms + r_symndx;
4395 if (is->n_sclass == C_HIDEXT
4396 && is->n_numaux > 0)
4398 void * auxptr;
4399 union internal_auxent aux;
4401 auxptr = ((void *)
4402 (((bfd_byte *)
4403 obj_coff_external_syms (input_bfd))
4404 + ((r_symndx + is->n_numaux)
4405 * isymesz)));
4406 bfd_coff_swap_aux_in (input_bfd, auxptr,
4407 is->n_type, is->n_sclass,
4408 is->n_numaux - 1,
4409 is->n_numaux,
4410 (void *) &aux);
4411 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4412 && aux.x_csect.x_smclas == XMC_TC0)
4413 indx = finfo->toc_symindx;
4417 if (indx != -1)
4418 irel->r_symndx = indx;
4419 else
4422 struct internal_syment *is;
4424 const char *name;
4425 char buf[SYMNMLEN + 1];
4427 /* This reloc is against a symbol we are
4428 stripping. It would be possible to handle
4429 this case, but I don't think it's worth it. */
4430 is = finfo->internal_syms + r_symndx;
4432 name = (_bfd_coff_internal_syment_name
4433 (input_bfd, is, buf));
4435 if (name == NULL)
4436 return FALSE;
4438 if (! ((*finfo->info->callbacks->unattached_reloc)
4439 (finfo->info, name, input_bfd, o,
4440 irel->r_vaddr)))
4441 return FALSE;
4446 quiet = FALSE;
4447 switch (irel->r_type)
4449 default:
4450 if (h == NULL
4451 || h->root.type == bfd_link_hash_defined
4452 || h->root.type == bfd_link_hash_defweak
4453 || h->root.type == bfd_link_hash_common)
4454 break;
4455 /* Fall through. */
4456 case R_POS:
4457 case R_NEG:
4458 case R_RL:
4459 case R_RLA:
4460 /* This reloc needs to be copied into the .loader
4461 section. */
4462 ldrel.l_vaddr = irel->r_vaddr;
4463 if (r_symndx == -1)
4464 ldrel.l_symndx = -(bfd_size_type ) 1;
4465 else if (h == NULL
4466 || (h->root.type == bfd_link_hash_defined
4467 || h->root.type == bfd_link_hash_defweak
4468 || h->root.type == bfd_link_hash_common))
4470 asection *sec;
4472 if (h == NULL)
4473 sec = xcoff_data (input_bfd)->csects[r_symndx];
4474 else if (h->root.type == bfd_link_hash_common)
4475 sec = h->root.u.c.p->section;
4476 else
4477 sec = h->root.u.def.section;
4478 sec = sec->output_section;
4480 if (strcmp (sec->name, ".text") == 0)
4481 ldrel.l_symndx = 0;
4482 else if (strcmp (sec->name, ".data") == 0)
4483 ldrel.l_symndx = 1;
4484 else if (strcmp (sec->name, ".bss") == 0)
4485 ldrel.l_symndx = 2;
4486 else
4488 (*_bfd_error_handler)
4489 (_("%B: loader reloc in unrecognized section `%A'"),
4490 input_bfd, sec);
4491 bfd_set_error (bfd_error_nonrepresentable_section);
4492 return FALSE;
4495 else
4497 if (! finfo->info->relocatable
4498 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
4499 && (h->flags & XCOFF_IMPORT) == 0)
4501 /* We already called the undefined_symbol
4502 callback for this relocation, in
4503 _bfd_ppc_xcoff_relocate_section. Don't
4504 issue any more warnings. */
4505 quiet = TRUE;
4507 if (h->ldindx < 0 && ! quiet)
4509 (*_bfd_error_handler)
4510 (_("%B: `%s' in loader reloc but not loader sym"),
4511 input_bfd,
4512 h->root.root.string);
4513 bfd_set_error (bfd_error_bad_value);
4514 return FALSE;
4516 ldrel.l_symndx = h->ldindx;
4518 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4519 ldrel.l_rsecnm = o->output_section->target_index;
4520 if (xcoff_hash_table (finfo->info)->textro
4521 && strcmp (o->output_section->name, ".text") == 0
4522 && ! quiet)
4524 (*_bfd_error_handler)
4525 (_("%B: loader reloc in read-only section %A"),
4526 input_bfd, o->output_section);
4527 bfd_set_error (bfd_error_invalid_operation);
4528 return FALSE;
4530 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4531 finfo->ldrel);
4533 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4534 break;
4536 case R_TOC:
4537 case R_GL:
4538 case R_TCL:
4539 case R_TRL:
4540 case R_TRLA:
4541 /* We should never need a .loader reloc for a TOC
4542 relative reloc. */
4543 break;
4547 o->output_section->reloc_count += o->reloc_count;
4550 /* Write out the modified section contents. */
4551 if (! bfd_set_section_contents (output_bfd, o->output_section,
4552 contents, (file_ptr) o->output_offset,
4553 o->size))
4554 return FALSE;
4557 obj_coff_keep_syms (input_bfd) = keep_syms;
4559 if (! finfo->info->keep_memory)
4561 if (! _bfd_coff_free_symbols (input_bfd))
4562 return FALSE;
4565 return TRUE;
4568 #undef N_TMASK
4569 #undef N_BTSHFT
4571 /* Sort relocs by VMA. This is called via qsort. */
4573 static int
4574 xcoff_sort_relocs (const void * p1, const void * p2)
4576 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4577 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4579 if (r1->r_vaddr > r2->r_vaddr)
4580 return 1;
4581 else if (r1->r_vaddr < r2->r_vaddr)
4582 return -1;
4583 else
4584 return 0;
4587 /* Write out a non-XCOFF global symbol. */
4589 static bfd_boolean
4590 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4592 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4593 bfd *output_bfd;
4594 bfd_byte *outsym;
4595 struct internal_syment isym;
4596 union internal_auxent aux;
4597 bfd_boolean result;
4598 file_ptr pos;
4599 bfd_size_type amt;
4601 output_bfd = finfo->output_bfd;
4602 outsym = finfo->outsyms;
4604 if (h->root.type == bfd_link_hash_warning)
4606 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4607 if (h->root.type == bfd_link_hash_new)
4608 return TRUE;
4611 /* If this symbol was garbage collected, just skip it. */
4612 if (xcoff_hash_table (finfo->info)->gc
4613 && (h->flags & XCOFF_MARK) == 0)
4614 return TRUE;
4616 /* If we need a .loader section entry, write it out. */
4617 if (h->ldsym != NULL)
4619 struct internal_ldsym *ldsym;
4620 bfd *impbfd;
4622 ldsym = h->ldsym;
4624 if (h->root.type == bfd_link_hash_undefined
4625 || h->root.type == bfd_link_hash_undefweak)
4628 ldsym->l_value = 0;
4629 ldsym->l_scnum = N_UNDEF;
4630 ldsym->l_smtype = XTY_ER;
4631 impbfd = h->root.u.undef.abfd;
4634 else if (h->root.type == bfd_link_hash_defined
4635 || h->root.type == bfd_link_hash_defweak)
4637 asection *sec;
4639 sec = h->root.u.def.section;
4640 ldsym->l_value = (sec->output_section->vma
4641 + sec->output_offset
4642 + h->root.u.def.value);
4643 ldsym->l_scnum = sec->output_section->target_index;
4644 ldsym->l_smtype = XTY_SD;
4645 impbfd = sec->owner;
4648 else
4649 abort ();
4651 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4652 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4653 || (h->flags & XCOFF_IMPORT) != 0)
4654 /* Clear l_smtype
4655 Import symbols are defined so the check above will make
4656 the l_smtype XTY_SD. But this is not correct, it should
4657 be cleared. */
4658 ldsym->l_smtype |= L_IMPORT;
4660 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4661 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4662 || (h->flags & XCOFF_EXPORT) != 0)
4663 ldsym->l_smtype |= L_EXPORT;
4665 if ((h->flags & XCOFF_ENTRY) != 0)
4666 ldsym->l_smtype |= L_ENTRY;
4668 if ((h->flags & XCOFF_RTINIT) != 0)
4669 ldsym->l_smtype = XTY_SD;
4671 ldsym->l_smclas = h->smclas;
4673 if (ldsym->l_smtype & L_IMPORT)
4675 if ((h->root.type == bfd_link_hash_defined
4676 || h->root.type == bfd_link_hash_defweak)
4677 && (h->root.u.def.value != 0))
4678 ldsym->l_smclas = XMC_XO;
4680 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4681 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4682 ldsym->l_smclas = XMC_SV3264;
4684 else if (h->flags & XCOFF_SYSCALL32)
4685 ldsym->l_smclas = XMC_SV;
4687 else if (h->flags & XCOFF_SYSCALL64)
4688 ldsym->l_smclas = XMC_SV64;
4691 if (ldsym->l_ifile == -(bfd_size_type) 1)
4693 ldsym->l_ifile = 0;
4695 else if (ldsym->l_ifile == 0)
4697 if ((ldsym->l_smtype & L_IMPORT) == 0)
4698 ldsym->l_ifile = 0;
4699 else if (impbfd == NULL)
4700 ldsym->l_ifile = 0;
4701 else
4703 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4704 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4708 ldsym->l_parm = 0;
4710 BFD_ASSERT (h->ldindx >= 0);
4712 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4713 (finfo->ldsym +
4714 (h->ldindx - 3)
4715 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4716 h->ldsym = NULL;
4719 /* If this symbol needs global linkage code, write it out. */
4720 if (h->root.type == bfd_link_hash_defined
4721 && (h->root.u.def.section
4722 == xcoff_hash_table (finfo->info)->linkage_section))
4724 bfd_byte *p;
4725 bfd_vma tocoff;
4726 unsigned int i;
4728 p = h->root.u.def.section->contents + h->root.u.def.value;
4730 /* The first instruction in the global linkage code loads a
4731 specific TOC element. */
4732 tocoff = (h->descriptor->toc_section->output_section->vma
4733 + h->descriptor->toc_section->output_offset
4734 - xcoff_data (output_bfd)->toc);
4736 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4737 tocoff += h->descriptor->u.toc_offset;
4739 /* The first instruction in the glink code needs to be
4740 cooked to to hold the correct offset in the toc. The
4741 rest are just output raw. */
4742 bfd_put_32 (output_bfd,
4743 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
4745 /* Start with i == 1 to get past the first instruction done above
4746 The /4 is because the glink code is in bytes and we are going
4747 4 at a pop. */
4748 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
4749 bfd_put_32 (output_bfd,
4750 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
4751 &p[4 * i]);
4754 /* If we created a TOC entry for this symbol, write out the required
4755 relocs. */
4756 if ((h->flags & XCOFF_SET_TOC) != 0)
4758 asection *tocsec;
4759 asection *osec;
4760 int oindx;
4761 struct internal_reloc *irel;
4762 struct internal_ldrel ldrel;
4763 struct internal_syment irsym;
4764 union internal_auxent iraux;
4766 tocsec = h->toc_section;
4767 osec = tocsec->output_section;
4768 oindx = osec->target_index;
4769 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4770 irel->r_vaddr = (osec->vma
4771 + tocsec->output_offset
4772 + h->u.toc_offset);
4774 if (h->indx >= 0)
4775 irel->r_symndx = h->indx;
4776 else
4778 h->indx = -2;
4779 irel->r_symndx = obj_raw_syment_count (output_bfd);
4782 BFD_ASSERT (h->ldindx >= 0);
4784 /* Initialize the aux union here instead of closer to when it is
4785 written out below because the length of the csect depends on
4786 whether the output is 32 or 64 bit. */
4787 memset (&iraux, 0, sizeof iraux);
4788 iraux.x_csect.x_smtyp = XTY_SD;
4789 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
4790 iraux.x_csect.x_smclas = XMC_TC;
4792 /* 32 bit uses a 32 bit R_POS to do the relocations
4793 64 bit uses a 64 bit R_POS to do the relocations
4795 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4797 Which one is determined by the backend. */
4798 if (bfd_xcoff_is_xcoff64 (output_bfd))
4800 irel->r_size = 63;
4801 iraux.x_csect.x_scnlen.l = 8;
4803 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4805 irel->r_size = 31;
4806 iraux.x_csect.x_scnlen.l = 4;
4808 else
4809 return FALSE;
4811 irel->r_type = R_POS;
4812 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4813 ++osec->reloc_count;
4815 ldrel.l_vaddr = irel->r_vaddr;
4816 ldrel.l_symndx = h->ldindx;
4817 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
4818 ldrel.l_rsecnm = oindx;
4819 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4820 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4822 /* We need to emit a symbol to define a csect which holds
4823 the reloc. */
4824 if (finfo->info->strip != strip_all)
4826 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
4827 &irsym, h->root.root.string);
4828 if (!result)
4829 return FALSE;
4831 irsym.n_value = irel->r_vaddr;
4832 irsym.n_scnum = osec->target_index;
4833 irsym.n_sclass = C_HIDEXT;
4834 irsym.n_type = T_NULL;
4835 irsym.n_numaux = 1;
4837 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
4838 outsym += bfd_coff_symesz (output_bfd);
4840 /* Note : iraux is initialized above. */
4841 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
4842 0, 1, (void *) outsym);
4843 outsym += bfd_coff_auxesz (output_bfd);
4845 if (h->indx >= 0)
4847 /* We aren't going to write out the symbols below, so we
4848 need to write them out now. */
4849 pos = obj_sym_filepos (output_bfd);
4850 pos += (obj_raw_syment_count (output_bfd)
4851 * bfd_coff_symesz (output_bfd));
4852 amt = outsym - finfo->outsyms;
4853 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4854 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4855 return FALSE;
4856 obj_raw_syment_count (output_bfd) +=
4857 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4859 outsym = finfo->outsyms;
4864 /* If this symbol is a specially defined function descriptor, write
4865 it out. The first word is the address of the function code
4866 itself, the second word is the address of the TOC, and the third
4867 word is zero.
4869 32 bit vs 64 bit
4870 The addresses for the 32 bit will take 4 bytes and the addresses
4871 for 64 bit will take 8 bytes. Similar for the relocs. This type
4872 of logic was also done above to create a TOC entry in
4873 xcoff_write_global_symbol. */
4874 if ((h->flags & XCOFF_DESCRIPTOR) != 0
4875 && h->root.type == bfd_link_hash_defined
4876 && (h->root.u.def.section
4877 == xcoff_hash_table (finfo->info)->descriptor_section))
4879 asection *sec;
4880 asection *osec;
4881 int oindx;
4882 bfd_byte *p;
4883 struct xcoff_link_hash_entry *hentry;
4884 asection *esec;
4885 struct internal_reloc *irel;
4886 struct internal_ldrel ldrel;
4887 asection *tsec;
4888 unsigned int reloc_size, byte_size;
4890 if (bfd_xcoff_is_xcoff64 (output_bfd))
4892 reloc_size = 63;
4893 byte_size = 8;
4895 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4897 reloc_size = 31;
4898 byte_size = 4;
4900 else
4901 return FALSE;
4903 sec = h->root.u.def.section;
4904 osec = sec->output_section;
4905 oindx = osec->target_index;
4906 p = sec->contents + h->root.u.def.value;
4908 hentry = h->descriptor;
4909 BFD_ASSERT (hentry != NULL
4910 && (hentry->root.type == bfd_link_hash_defined
4911 || hentry->root.type == bfd_link_hash_defweak));
4912 esec = hentry->root.u.def.section;
4914 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4915 irel->r_vaddr = (osec->vma
4916 + sec->output_offset
4917 + h->root.u.def.value);
4918 irel->r_symndx = esec->output_section->target_index;
4919 irel->r_type = R_POS;
4920 irel->r_size = reloc_size;
4921 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4922 ++osec->reloc_count;
4924 ldrel.l_vaddr = irel->r_vaddr;
4925 if (strcmp (esec->output_section->name, ".text") == 0)
4926 ldrel.l_symndx = 0;
4927 else if (strcmp (esec->output_section->name, ".data") == 0)
4928 ldrel.l_symndx = 1;
4929 else if (strcmp (esec->output_section->name, ".bss") == 0)
4930 ldrel.l_symndx = 2;
4931 else
4933 (*_bfd_error_handler)
4934 (_("%s: loader reloc in unrecognized section `%s'"),
4935 bfd_get_filename (output_bfd),
4936 esec->output_section->name);
4937 bfd_set_error (bfd_error_nonrepresentable_section);
4938 return FALSE;
4940 ldrel.l_rtype = (reloc_size << 8) | R_POS;
4941 ldrel.l_rsecnm = oindx;
4942 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4943 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4945 /* There are three items to write out,
4946 the address of the code
4947 the address of the toc anchor
4948 the environment pointer.
4949 We are ignoring the environment pointer. So set it to zero. */
4950 if (bfd_xcoff_is_xcoff64 (output_bfd))
4952 bfd_put_64 (output_bfd,
4953 (esec->output_section->vma + esec->output_offset
4954 + hentry->root.u.def.value),
4956 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
4957 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
4959 else
4961 /* 32 bit backend
4962 This logic was already called above so the error case where
4963 the backend is neither has already been checked. */
4964 bfd_put_32 (output_bfd,
4965 (esec->output_section->vma + esec->output_offset
4966 + hentry->root.u.def.value),
4968 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
4969 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
4972 tsec = coff_section_from_bfd_index (output_bfd,
4973 xcoff_data (output_bfd)->sntoc);
4975 ++irel;
4976 irel->r_vaddr = (osec->vma
4977 + sec->output_offset
4978 + h->root.u.def.value
4979 + byte_size);
4980 irel->r_symndx = tsec->output_section->target_index;
4981 irel->r_type = R_POS;
4982 irel->r_size = reloc_size;
4983 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4984 ++osec->reloc_count;
4986 ldrel.l_vaddr = irel->r_vaddr;
4987 if (strcmp (tsec->output_section->name, ".text") == 0)
4988 ldrel.l_symndx = 0;
4989 else if (strcmp (tsec->output_section->name, ".data") == 0)
4990 ldrel.l_symndx = 1;
4991 else if (strcmp (tsec->output_section->name, ".bss") == 0)
4992 ldrel.l_symndx = 2;
4993 else
4995 (*_bfd_error_handler)
4996 (_("%s: loader reloc in unrecognized section `%s'"),
4997 bfd_get_filename (output_bfd),
4998 tsec->output_section->name);
4999 bfd_set_error (bfd_error_nonrepresentable_section);
5000 return FALSE;
5002 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5003 ldrel.l_rsecnm = oindx;
5004 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5005 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5008 if (h->indx >= 0 || finfo->info->strip == strip_all)
5010 BFD_ASSERT (outsym == finfo->outsyms);
5011 return TRUE;
5014 if (h->indx != -2
5015 && (finfo->info->strip == strip_all
5016 || (finfo->info->strip == strip_some
5017 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5018 FALSE, FALSE) == NULL)))
5020 BFD_ASSERT (outsym == finfo->outsyms);
5021 return TRUE;
5024 if (h->indx != -2
5025 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5027 BFD_ASSERT (outsym == finfo->outsyms);
5028 return TRUE;
5031 memset (&aux, 0, sizeof aux);
5033 h->indx = obj_raw_syment_count (output_bfd);
5035 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5036 h->root.root.string);
5037 if (!result)
5038 return FALSE;
5040 if (h->root.type == bfd_link_hash_undefined
5041 || h->root.type == bfd_link_hash_undefweak)
5043 isym.n_value = 0;
5044 isym.n_scnum = N_UNDEF;
5045 isym.n_sclass = C_EXT;
5046 aux.x_csect.x_smtyp = XTY_ER;
5048 else if ((h->root.type == bfd_link_hash_defined
5049 || h->root.type == bfd_link_hash_defweak)
5050 && h->smclas == XMC_XO)
5052 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5053 isym.n_value = h->root.u.def.value;
5054 isym.n_scnum = N_UNDEF;
5055 isym.n_sclass = C_EXT;
5056 aux.x_csect.x_smtyp = XTY_ER;
5058 else if (h->root.type == bfd_link_hash_defined
5059 || h->root.type == bfd_link_hash_defweak)
5061 struct xcoff_link_size_list *l;
5063 isym.n_value = (h->root.u.def.section->output_section->vma
5064 + h->root.u.def.section->output_offset
5065 + h->root.u.def.value);
5066 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5067 isym.n_scnum = N_ABS;
5068 else
5069 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5070 isym.n_sclass = C_HIDEXT;
5071 aux.x_csect.x_smtyp = XTY_SD;
5073 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5075 for (l = xcoff_hash_table (finfo->info)->size_list;
5076 l != NULL;
5077 l = l->next)
5079 if (l->h == h)
5081 aux.x_csect.x_scnlen.l = l->size;
5082 break;
5087 else if (h->root.type == bfd_link_hash_common)
5089 isym.n_value = (h->root.u.c.p->section->output_section->vma
5090 + h->root.u.c.p->section->output_offset);
5091 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5092 isym.n_sclass = C_EXT;
5093 aux.x_csect.x_smtyp = XTY_CM;
5094 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5096 else
5097 abort ();
5099 isym.n_type = T_NULL;
5100 isym.n_numaux = 1;
5102 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5103 outsym += bfd_coff_symesz (output_bfd);
5105 aux.x_csect.x_smclas = h->smclas;
5106 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5107 (void *) outsym);
5108 outsym += bfd_coff_auxesz (output_bfd);
5110 if ((h->root.type == bfd_link_hash_defined
5111 || h->root.type == bfd_link_hash_defweak)
5112 && h->smclas != XMC_XO)
5114 /* We just output an SD symbol. Now output an LD symbol. */
5115 h->indx += 2;
5117 isym.n_sclass = C_EXT;
5118 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5119 outsym += bfd_coff_symesz (output_bfd);
5121 aux.x_csect.x_smtyp = XTY_LD;
5122 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5123 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5124 (void *) outsym);
5125 outsym += bfd_coff_auxesz (output_bfd);
5128 pos = obj_sym_filepos (output_bfd);
5129 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5130 amt = outsym - finfo->outsyms;
5131 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5132 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5133 return FALSE;
5134 obj_raw_syment_count (output_bfd) +=
5135 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5137 return TRUE;
5140 /* Handle a link order which is supposed to generate a reloc. */
5142 static bfd_boolean
5143 xcoff_reloc_link_order (bfd *output_bfd,
5144 struct xcoff_final_link_info *finfo,
5145 asection *output_section,
5146 struct bfd_link_order *link_order)
5148 reloc_howto_type *howto;
5149 struct xcoff_link_hash_entry *h;
5150 asection *hsec;
5151 bfd_vma hval;
5152 bfd_vma addend;
5153 struct internal_reloc *irel;
5154 struct xcoff_link_hash_entry **rel_hash_ptr;
5155 struct internal_ldrel ldrel;
5157 if (link_order->type == bfd_section_reloc_link_order)
5158 /* We need to somehow locate a symbol in the right section. The
5159 symbol must either have a value of zero, or we must adjust
5160 the addend by the value of the symbol. FIXME: Write this
5161 when we need it. The old linker couldn't handle this anyhow. */
5162 abort ();
5164 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5165 if (howto == NULL)
5167 bfd_set_error (bfd_error_bad_value);
5168 return FALSE;
5171 h = ((struct xcoff_link_hash_entry *)
5172 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5173 link_order->u.reloc.p->u.name,
5174 FALSE, FALSE, TRUE));
5175 if (h == NULL)
5177 if (! ((*finfo->info->callbacks->unattached_reloc)
5178 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5179 return FALSE;
5180 return TRUE;
5183 if (h->root.type == bfd_link_hash_common)
5185 hsec = h->root.u.c.p->section;
5186 hval = 0;
5188 else if (h->root.type == bfd_link_hash_defined
5189 || h->root.type == bfd_link_hash_defweak)
5191 hsec = h->root.u.def.section;
5192 hval = h->root.u.def.value;
5194 else
5196 hsec = NULL;
5197 hval = 0;
5200 addend = link_order->u.reloc.p->addend;
5201 if (hsec != NULL)
5202 addend += (hsec->output_section->vma
5203 + hsec->output_offset
5204 + hval);
5206 if (addend != 0)
5208 bfd_size_type size;
5209 bfd_byte *buf;
5210 bfd_reloc_status_type rstat;
5211 bfd_boolean ok;
5213 size = bfd_get_reloc_size (howto);
5214 buf = bfd_zmalloc (size);
5215 if (buf == NULL)
5216 return FALSE;
5218 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5219 switch (rstat)
5221 case bfd_reloc_ok:
5222 break;
5223 default:
5224 case bfd_reloc_outofrange:
5225 abort ();
5226 case bfd_reloc_overflow:
5227 if (! ((*finfo->info->callbacks->reloc_overflow)
5228 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5229 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5231 free (buf);
5232 return FALSE;
5234 break;
5236 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5237 (file_ptr) link_order->offset, size);
5238 free (buf);
5239 if (! ok)
5240 return FALSE;
5243 /* Store the reloc information in the right place. It will get
5244 swapped and written out at the end of the final_link routine. */
5245 irel = (finfo->section_info[output_section->target_index].relocs
5246 + output_section->reloc_count);
5247 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5248 + output_section->reloc_count);
5250 memset (irel, 0, sizeof (struct internal_reloc));
5251 *rel_hash_ptr = NULL;
5253 irel->r_vaddr = output_section->vma + link_order->offset;
5255 if (h->indx >= 0)
5256 irel->r_symndx = h->indx;
5257 else
5259 /* Set the index to -2 to force this symbol to get written out. */
5260 h->indx = -2;
5261 *rel_hash_ptr = h;
5262 irel->r_symndx = 0;
5265 irel->r_type = howto->type;
5266 irel->r_size = howto->bitsize - 1;
5267 if (howto->complain_on_overflow == complain_overflow_signed)
5268 irel->r_size |= 0x80;
5270 ++output_section->reloc_count;
5272 /* Now output the reloc to the .loader section. */
5274 ldrel.l_vaddr = irel->r_vaddr;
5276 if (hsec != NULL)
5278 const char *secname;
5280 secname = hsec->output_section->name;
5282 if (strcmp (secname, ".text") == 0)
5283 ldrel.l_symndx = 0;
5284 else if (strcmp (secname, ".data") == 0)
5285 ldrel.l_symndx = 1;
5286 else if (strcmp (secname, ".bss") == 0)
5287 ldrel.l_symndx = 2;
5288 else
5290 (*_bfd_error_handler)
5291 (_("%s: loader reloc in unrecognized section `%s'"),
5292 bfd_get_filename (output_bfd), secname);
5293 bfd_set_error (bfd_error_nonrepresentable_section);
5294 return FALSE;
5297 else
5299 if (h->ldindx < 0)
5301 (*_bfd_error_handler)
5302 (_("%s: `%s' in loader reloc but not loader sym"),
5303 bfd_get_filename (output_bfd),
5304 h->root.root.string);
5305 bfd_set_error (bfd_error_bad_value);
5306 return FALSE;
5308 ldrel.l_symndx = h->ldindx;
5311 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5312 ldrel.l_rsecnm = output_section->target_index;
5313 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5314 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5316 return TRUE;
5319 /* Do the final link step. */
5321 bfd_boolean
5322 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5324 bfd_size_type symesz;
5325 struct xcoff_final_link_info finfo;
5326 asection *o;
5327 struct bfd_link_order *p;
5328 bfd_size_type max_contents_size;
5329 bfd_size_type max_sym_count;
5330 bfd_size_type max_lineno_count;
5331 bfd_size_type max_reloc_count;
5332 bfd_size_type max_output_reloc_count;
5333 file_ptr rel_filepos;
5334 unsigned int relsz;
5335 file_ptr line_filepos;
5336 unsigned int linesz;
5337 bfd *sub;
5338 bfd_byte *external_relocs = NULL;
5339 char strbuf[STRING_SIZE_SIZE];
5340 file_ptr pos;
5341 bfd_size_type amt;
5343 if (info->shared)
5344 abfd->flags |= DYNAMIC;
5346 symesz = bfd_coff_symesz (abfd);
5348 finfo.info = info;
5349 finfo.output_bfd = abfd;
5350 finfo.strtab = NULL;
5351 finfo.section_info = NULL;
5352 finfo.last_file_index = -1;
5353 finfo.toc_symindx = -1;
5354 finfo.internal_syms = NULL;
5355 finfo.sym_indices = NULL;
5356 finfo.outsyms = NULL;
5357 finfo.linenos = NULL;
5358 finfo.contents = NULL;
5359 finfo.external_relocs = NULL;
5361 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5362 + bfd_xcoff_ldhdrsz (abfd));
5363 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5364 + bfd_xcoff_ldhdrsz(abfd)
5365 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5366 * bfd_xcoff_ldsymsz(abfd)));
5368 xcoff_data (abfd)->coff.link_info = info;
5370 finfo.strtab = _bfd_stringtab_init ();
5371 if (finfo.strtab == NULL)
5372 goto error_return;
5374 /* Count the line number and relocation entries required for the
5375 output file. Determine a few maximum sizes. */
5376 max_contents_size = 0;
5377 max_lineno_count = 0;
5378 max_reloc_count = 0;
5379 for (o = abfd->sections; o != NULL; o = o->next)
5381 o->reloc_count = 0;
5382 o->lineno_count = 0;
5383 for (p = o->map_head.link_order; p != NULL; p = p->next)
5385 if (p->type == bfd_indirect_link_order)
5387 asection *sec;
5389 sec = p->u.indirect.section;
5391 /* Mark all sections which are to be included in the
5392 link. This will normally be every section. We need
5393 to do this so that we can identify any sections which
5394 the linker has decided to not include. */
5395 sec->linker_mark = TRUE;
5397 if (info->strip == strip_none
5398 || info->strip == strip_some)
5399 o->lineno_count += sec->lineno_count;
5401 o->reloc_count += sec->reloc_count;
5403 if (sec->rawsize > max_contents_size)
5404 max_contents_size = sec->rawsize;
5405 if (sec->size > max_contents_size)
5406 max_contents_size = sec->size;
5407 if (sec->lineno_count > max_lineno_count)
5408 max_lineno_count = sec->lineno_count;
5409 if (coff_section_data (sec->owner, sec) != NULL
5410 && xcoff_section_data (sec->owner, sec) != NULL
5411 && (xcoff_section_data (sec->owner, sec)->lineno_count
5412 > max_lineno_count))
5413 max_lineno_count =
5414 xcoff_section_data (sec->owner, sec)->lineno_count;
5415 if (sec->reloc_count > max_reloc_count)
5416 max_reloc_count = sec->reloc_count;
5418 else if (p->type == bfd_section_reloc_link_order
5419 || p->type == bfd_symbol_reloc_link_order)
5420 ++o->reloc_count;
5424 /* Compute the file positions for all the sections. */
5425 if (abfd->output_has_begun)
5427 if (xcoff_hash_table (info)->file_align != 0)
5428 abort ();
5430 else
5432 bfd_vma file_align;
5434 file_align = xcoff_hash_table (info)->file_align;
5435 if (file_align != 0)
5437 bfd_boolean saw_contents;
5438 int indx;
5439 file_ptr sofar;
5441 /* Insert .pad sections before every section which has
5442 contents and is loaded, if it is preceded by some other
5443 section which has contents and is loaded. */
5444 saw_contents = TRUE;
5445 for (o = abfd->sections; o != NULL; o = o->next)
5447 if (strcmp (o->name, ".pad") == 0)
5448 saw_contents = FALSE;
5449 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5450 && (o->flags & SEC_LOAD) != 0)
5452 if (! saw_contents)
5453 saw_contents = TRUE;
5454 else
5456 asection *n;
5458 /* Create a pad section and place it before the section
5459 that needs padding. This requires unlinking and
5460 relinking the bfd's section list. */
5462 n = bfd_make_section_anyway (abfd, ".pad");
5463 n->flags = SEC_HAS_CONTENTS;
5464 n->alignment_power = 0;
5466 bfd_section_list_remove (abfd, n);
5467 bfd_section_list_insert_before (abfd, o, n);
5468 saw_contents = FALSE;
5473 /* Reset the section indices after inserting the new
5474 sections. */
5475 indx = 0;
5476 for (o = abfd->sections; o != NULL; o = o->next)
5478 ++indx;
5479 o->target_index = indx;
5481 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5483 /* Work out appropriate sizes for the .pad sections to force
5484 each section to land on a page boundary. This bit of
5485 code knows what compute_section_file_positions is going
5486 to do. */
5487 sofar = bfd_coff_filhsz (abfd);
5488 sofar += bfd_coff_aoutsz (abfd);
5489 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5490 for (o = abfd->sections; o != NULL; o = o->next)
5491 if ((bfd_xcoff_is_reloc_count_overflow
5492 (abfd, (bfd_vma) o->reloc_count))
5493 || (bfd_xcoff_is_lineno_count_overflow
5494 (abfd, (bfd_vma) o->lineno_count)))
5495 /* 64 does not overflow, need to check if 32 does */
5496 sofar += bfd_coff_scnhsz (abfd);
5498 for (o = abfd->sections; o != NULL; o = o->next)
5500 if (strcmp (o->name, ".pad") == 0)
5502 bfd_vma pageoff;
5504 BFD_ASSERT (o->size == 0);
5505 pageoff = sofar & (file_align - 1);
5506 if (pageoff != 0)
5508 o->size = file_align - pageoff;
5509 sofar += file_align - pageoff;
5510 o->flags |= SEC_HAS_CONTENTS;
5513 else
5515 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5516 sofar += BFD_ALIGN (o->size,
5517 1 << o->alignment_power);
5522 if (! bfd_coff_compute_section_file_positions (abfd))
5523 goto error_return;
5526 /* Allocate space for the pointers we need to keep for the relocs. */
5528 unsigned int i;
5530 /* We use section_count + 1, rather than section_count, because
5531 the target_index fields are 1 based. */
5532 amt = abfd->section_count + 1;
5533 amt *= sizeof (struct xcoff_link_section_info);
5534 finfo.section_info = bfd_malloc (amt);
5535 if (finfo.section_info == NULL)
5536 goto error_return;
5537 for (i = 0; i <= abfd->section_count; i++)
5539 finfo.section_info[i].relocs = NULL;
5540 finfo.section_info[i].rel_hashes = NULL;
5541 finfo.section_info[i].toc_rel_hashes = NULL;
5545 /* Set the file positions for the relocs. */
5546 rel_filepos = obj_relocbase (abfd);
5547 relsz = bfd_coff_relsz (abfd);
5548 max_output_reloc_count = 0;
5549 for (o = abfd->sections; o != NULL; o = o->next)
5551 if (o->reloc_count == 0)
5552 o->rel_filepos = 0;
5553 else
5555 /* A stripped file has no relocs. However, we still
5556 allocate the buffers, so that later code doesn't have to
5557 worry about whether we are stripping or not. */
5558 if (info->strip == strip_all)
5559 o->rel_filepos = 0;
5560 else
5562 o->flags |= SEC_RELOC;
5563 o->rel_filepos = rel_filepos;
5564 rel_filepos += o->reloc_count * relsz;
5567 /* We don't know the indices of global symbols until we have
5568 written out all the local symbols. For each section in
5569 the output file, we keep an array of pointers to hash
5570 table entries. Each entry in the array corresponds to a
5571 reloc. When we find a reloc against a global symbol, we
5572 set the corresponding entry in this array so that we can
5573 fix up the symbol index after we have written out all the
5574 local symbols.
5576 Because of this problem, we also keep the relocs in
5577 memory until the end of the link. This wastes memory.
5578 We could backpatch the file later, I suppose, although it
5579 would be slow. */
5580 amt = o->reloc_count;
5581 amt *= sizeof (struct internal_reloc);
5582 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5584 amt = o->reloc_count;
5585 amt *= sizeof (struct xcoff_link_hash_entry *);
5586 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5588 if (finfo.section_info[o->target_index].relocs == NULL
5589 || finfo.section_info[o->target_index].rel_hashes == NULL)
5590 goto error_return;
5592 if (o->reloc_count > max_output_reloc_count)
5593 max_output_reloc_count = o->reloc_count;
5597 /* We now know the size of the relocs, so we can determine the file
5598 positions of the line numbers. */
5599 line_filepos = rel_filepos;
5600 finfo.line_filepos = line_filepos;
5601 linesz = bfd_coff_linesz (abfd);
5602 for (o = abfd->sections; o != NULL; o = o->next)
5604 if (o->lineno_count == 0)
5605 o->line_filepos = 0;
5606 else
5608 o->line_filepos = line_filepos;
5609 line_filepos += o->lineno_count * linesz;
5612 /* Reset the reloc and lineno counts, so that we can use them to
5613 count the number of entries we have output so far. */
5614 o->reloc_count = 0;
5615 o->lineno_count = 0;
5618 obj_sym_filepos (abfd) = line_filepos;
5620 /* Figure out the largest number of symbols in an input BFD. Take
5621 the opportunity to clear the output_has_begun fields of all the
5622 input BFD's. We want at least 6 symbols, since that is the
5623 number which xcoff_write_global_symbol may need. */
5624 max_sym_count = 6;
5625 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5627 bfd_size_type sz;
5629 sub->output_has_begun = FALSE;
5630 sz = obj_raw_syment_count (sub);
5631 if (sz > max_sym_count)
5632 max_sym_count = sz;
5635 /* Allocate some buffers used while linking. */
5636 amt = max_sym_count * sizeof (struct internal_syment);
5637 finfo.internal_syms = bfd_malloc (amt);
5639 amt = max_sym_count * sizeof (long);
5640 finfo.sym_indices = bfd_malloc (amt);
5642 amt = (max_sym_count + 1) * symesz;
5643 finfo.outsyms = bfd_malloc (amt);
5645 amt = max_lineno_count * bfd_coff_linesz (abfd);
5646 finfo.linenos = bfd_malloc (amt);
5648 amt = max_contents_size;
5649 finfo.contents = bfd_malloc (amt);
5651 amt = max_reloc_count * relsz;
5652 finfo.external_relocs = bfd_malloc (amt);
5654 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5655 || (finfo.sym_indices == NULL && max_sym_count > 0)
5656 || finfo.outsyms == NULL
5657 || (finfo.linenos == NULL && max_lineno_count > 0)
5658 || (finfo.contents == NULL && max_contents_size > 0)
5659 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5660 goto error_return;
5662 obj_raw_syment_count (abfd) = 0;
5663 xcoff_data (abfd)->toc = (bfd_vma) -1;
5665 /* We now know the position of everything in the file, except that
5666 we don't know the size of the symbol table and therefore we don't
5667 know where the string table starts. We just build the string
5668 table in memory as we go along. We process all the relocations
5669 for a single input file at once. */
5670 for (o = abfd->sections; o != NULL; o = o->next)
5672 for (p = o->map_head.link_order; p != NULL; p = p->next)
5674 if (p->type == bfd_indirect_link_order
5675 && p->u.indirect.section->owner->xvec == abfd->xvec)
5677 sub = p->u.indirect.section->owner;
5678 if (! sub->output_has_begun)
5680 if (! xcoff_link_input_bfd (&finfo, sub))
5681 goto error_return;
5682 sub->output_has_begun = TRUE;
5685 else if (p->type == bfd_section_reloc_link_order
5686 || p->type == bfd_symbol_reloc_link_order)
5688 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5689 goto error_return;
5691 else
5693 if (! _bfd_default_link_order (abfd, info, o, p))
5694 goto error_return;
5699 /* Free up the buffers used by xcoff_link_input_bfd. */
5700 if (finfo.internal_syms != NULL)
5702 free (finfo.internal_syms);
5703 finfo.internal_syms = NULL;
5705 if (finfo.sym_indices != NULL)
5707 free (finfo.sym_indices);
5708 finfo.sym_indices = NULL;
5710 if (finfo.linenos != NULL)
5712 free (finfo.linenos);
5713 finfo.linenos = NULL;
5715 if (finfo.contents != NULL)
5717 free (finfo.contents);
5718 finfo.contents = NULL;
5720 if (finfo.external_relocs != NULL)
5722 free (finfo.external_relocs);
5723 finfo.external_relocs = NULL;
5726 /* The value of the last C_FILE symbol is supposed to be -1. Write
5727 it out again. */
5728 if (finfo.last_file_index != -1)
5730 finfo.last_file.n_value = -(bfd_vma) 1;
5731 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
5732 (void *) finfo.outsyms);
5733 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
5734 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5735 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
5736 goto error_return;
5739 /* Write out all the global symbols which do not come from XCOFF
5740 input files. */
5741 xcoff_link_hash_traverse (xcoff_hash_table (info),
5742 xcoff_write_global_symbol,
5743 (void *) &finfo);
5745 if (finfo.outsyms != NULL)
5747 free (finfo.outsyms);
5748 finfo.outsyms = NULL;
5751 /* Now that we have written out all the global symbols, we know the
5752 symbol indices to use for relocs against them, and we can finally
5753 write out the relocs. */
5754 amt = max_output_reloc_count * relsz;
5755 external_relocs = bfd_malloc (amt);
5756 if (external_relocs == NULL && max_output_reloc_count != 0)
5757 goto error_return;
5759 for (o = abfd->sections; o != NULL; o = o->next)
5761 struct internal_reloc *irel;
5762 struct internal_reloc *irelend;
5763 struct xcoff_link_hash_entry **rel_hash;
5764 struct xcoff_toc_rel_hash *toc_rel_hash;
5765 bfd_byte *erel;
5766 bfd_size_type rel_size;
5768 /* A stripped file has no relocs. */
5769 if (info->strip == strip_all)
5771 o->reloc_count = 0;
5772 continue;
5775 if (o->reloc_count == 0)
5776 continue;
5778 irel = finfo.section_info[o->target_index].relocs;
5779 irelend = irel + o->reloc_count;
5780 rel_hash = finfo.section_info[o->target_index].rel_hashes;
5781 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5783 if (*rel_hash != NULL)
5785 if ((*rel_hash)->indx < 0)
5787 if (! ((*info->callbacks->unattached_reloc)
5788 (info, (*rel_hash)->root.root.string,
5789 NULL, o, irel->r_vaddr)))
5790 goto error_return;
5791 (*rel_hash)->indx = 0;
5793 irel->r_symndx = (*rel_hash)->indx;
5797 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
5798 toc_rel_hash != NULL;
5799 toc_rel_hash = toc_rel_hash->next)
5801 if (toc_rel_hash->h->u.toc_indx < 0)
5803 if (! ((*info->callbacks->unattached_reloc)
5804 (info, toc_rel_hash->h->root.root.string,
5805 NULL, o, toc_rel_hash->rel->r_vaddr)))
5806 goto error_return;
5807 toc_rel_hash->h->u.toc_indx = 0;
5809 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
5812 /* XCOFF requires that the relocs be sorted by address. We tend
5813 to produce them in the order in which their containing csects
5814 appear in the symbol table, which is not necessarily by
5815 address. So we sort them here. There may be a better way to
5816 do this. */
5817 qsort ((void *) finfo.section_info[o->target_index].relocs,
5818 o->reloc_count, sizeof (struct internal_reloc),
5819 xcoff_sort_relocs);
5821 irel = finfo.section_info[o->target_index].relocs;
5822 irelend = irel + o->reloc_count;
5823 erel = external_relocs;
5824 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5825 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
5827 rel_size = relsz * o->reloc_count;
5828 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
5829 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
5830 goto error_return;
5833 if (external_relocs != NULL)
5835 free (external_relocs);
5836 external_relocs = NULL;
5839 /* Free up the section information. */
5840 if (finfo.section_info != NULL)
5842 unsigned int i;
5844 for (i = 0; i < abfd->section_count; i++)
5846 if (finfo.section_info[i].relocs != NULL)
5847 free (finfo.section_info[i].relocs);
5848 if (finfo.section_info[i].rel_hashes != NULL)
5849 free (finfo.section_info[i].rel_hashes);
5851 free (finfo.section_info);
5852 finfo.section_info = NULL;
5855 /* Write out the loader section contents. */
5856 BFD_ASSERT ((bfd_byte *) finfo.ldrel
5857 == (xcoff_hash_table (info)->loader_section->contents
5858 + xcoff_hash_table (info)->ldhdr.l_impoff));
5859 o = xcoff_hash_table (info)->loader_section;
5860 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
5861 (file_ptr) o->output_offset, o->size))
5862 goto error_return;
5864 /* Write out the magic sections. */
5865 o = xcoff_hash_table (info)->linkage_section;
5866 if (o->size > 0
5867 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5868 (file_ptr) o->output_offset,
5869 o->size))
5870 goto error_return;
5871 o = xcoff_hash_table (info)->toc_section;
5872 if (o->size > 0
5873 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5874 (file_ptr) o->output_offset,
5875 o->size))
5876 goto error_return;
5877 o = xcoff_hash_table (info)->descriptor_section;
5878 if (o->size > 0
5879 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5880 (file_ptr) o->output_offset,
5881 o->size))
5882 goto error_return;
5884 /* Write out the string table. */
5885 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
5886 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5887 goto error_return;
5888 H_PUT_32 (abfd,
5889 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
5890 strbuf);
5891 amt = STRING_SIZE_SIZE;
5892 if (bfd_bwrite (strbuf, amt, abfd) != amt)
5893 goto error_return;
5894 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
5895 goto error_return;
5897 _bfd_stringtab_free (finfo.strtab);
5899 /* Write out the debugging string table. */
5900 o = xcoff_hash_table (info)->debug_section;
5901 if (o != NULL)
5903 struct bfd_strtab_hash *debug_strtab;
5905 debug_strtab = xcoff_hash_table (info)->debug_strtab;
5906 BFD_ASSERT (o->output_section->size - o->output_offset
5907 >= _bfd_stringtab_size (debug_strtab));
5908 pos = o->output_section->filepos + o->output_offset;
5909 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5910 goto error_return;
5911 if (! _bfd_stringtab_emit (abfd, debug_strtab))
5912 goto error_return;
5915 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
5916 not try to write out the symbols. */
5917 bfd_get_symcount (abfd) = 0;
5919 return TRUE;
5921 error_return:
5922 if (finfo.strtab != NULL)
5923 _bfd_stringtab_free (finfo.strtab);
5925 if (finfo.section_info != NULL)
5927 unsigned int i;
5929 for (i = 0; i < abfd->section_count; i++)
5931 if (finfo.section_info[i].relocs != NULL)
5932 free (finfo.section_info[i].relocs);
5933 if (finfo.section_info[i].rel_hashes != NULL)
5934 free (finfo.section_info[i].rel_hashes);
5936 free (finfo.section_info);
5939 if (finfo.internal_syms != NULL)
5940 free (finfo.internal_syms);
5941 if (finfo.sym_indices != NULL)
5942 free (finfo.sym_indices);
5943 if (finfo.outsyms != NULL)
5944 free (finfo.outsyms);
5945 if (finfo.linenos != NULL)
5946 free (finfo.linenos);
5947 if (finfo.contents != NULL)
5948 free (finfo.contents);
5949 if (finfo.external_relocs != NULL)
5950 free (finfo.external_relocs);
5951 if (external_relocs != NULL)
5952 free (external_relocs);
5953 return FALSE;