2011-03-03 Michael Snyder <msnyder@vmware.com>
[binutils.git] / bfd / xcofflink.c
blob7ff920aeaaf0a6d441722f310d65510bff3c4194
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 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 3 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,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
29 #include "libcoff.h"
30 #include "libxcoff.h"
31 #include "libiberty.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file *next;
49 /* The path. */
50 const char *path;
51 /* The file name. */
52 const char *file;
53 /* The member name. */
54 const char *member;
57 /* Information we keep for each section in the output file during the
58 final link phase. */
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc *relocs;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry **rel_hashes;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash *next;
74 struct xcoff_link_hash_entry *h;
75 struct internal_reloc *rel;
76 } *toc_rel_hashes;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
83 bfd *archive;
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
87 const char *imppath;
88 const char *impfile;
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p : 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p : 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash *debug_strtab;
106 /* The .debug section we will use for the final output. */
107 asection *debug_section;
109 /* The .loader section we will use for the final output. */
110 asection *loader_section;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
114 size_t ldrel_count;
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr;
119 /* The .gl section we use to hold global linkage code. */
120 asection *linkage_section;
122 /* The .tc section we use to hold toc entries we build for global
123 linkage code. */
124 asection *toc_section;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection *descriptor_section;
130 /* The list of import files. */
131 struct xcoff_import_file *imports;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align;
136 /* Whether the .text section must be read-only. */
137 bfd_boolean textro;
139 /* Whether -brtl was specified. */
140 bfd_boolean rtld;
142 /* Whether garbage collection was done. */
143 bfd_boolean gc;
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list *next;
149 struct xcoff_link_hash_entry *h;
150 bfd_size_type size;
152 *size_list;
154 /* Information about archives. */
155 htab_t archive_info;
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info *info;
167 /* Output BFD. */
168 bfd *output_bfd;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash *strtab;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info *section_info;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file;
178 /* Symbol index of TOC symbol. */
179 long toc_symindx;
180 /* Start of .loader symbols. */
181 bfd_byte *ldsym;
182 /* Next .loader reloc to swap out. */
183 bfd_byte *ldrel;
184 /* File position of start of line numbers. */
185 file_ptr line_filepos;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment *internal_syms;
188 /* Buffer large enough to hold output indices of symbols of any
189 input file. */
190 long *sym_indices;
191 /* Buffer large enough to hold output symbols for any input file. */
192 bfd_byte *outsyms;
193 /* Buffer large enough to hold external line numbers for any input
194 section. */
195 bfd_byte *linenos;
196 /* Buffer large enough to hold any input section. */
197 bfd_byte *contents;
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte *external_relocs;
202 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
208 here. */
210 /* Read the contents of a section. */
212 static bfd_boolean
213 xcoff_get_section_contents (bfd *abfd, asection *sec)
215 if (coff_section_data (abfd, sec) == NULL)
217 bfd_size_type amt = sizeof (struct coff_section_tdata);
219 sec->used_by_bfd = bfd_zalloc (abfd, amt);
220 if (sec->used_by_bfd == NULL)
221 return FALSE;
224 if (coff_section_data (abfd, sec)->contents == NULL)
226 bfd_byte *contents;
228 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
230 if (contents != NULL)
231 free (contents);
232 return FALSE;
234 coff_section_data (abfd, sec)->contents = contents;
237 return TRUE;
240 /* Get the size required to hold the dynamic symbols. */
242 long
243 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
245 asection *lsec;
246 bfd_byte *contents;
247 struct internal_ldhdr ldhdr;
249 if ((abfd->flags & DYNAMIC) == 0)
251 bfd_set_error (bfd_error_invalid_operation);
252 return -1;
255 lsec = bfd_get_section_by_name (abfd, ".loader");
256 if (lsec == NULL)
258 bfd_set_error (bfd_error_no_symbols);
259 return -1;
262 if (! xcoff_get_section_contents (abfd, lsec))
263 return -1;
264 contents = coff_section_data (abfd, lsec)->contents;
266 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
268 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
271 /* Get the dynamic symbols. */
273 long
274 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
276 asection *lsec;
277 bfd_byte *contents;
278 struct internal_ldhdr ldhdr;
279 const char *strings;
280 bfd_byte *elsym, *elsymend;
281 coff_symbol_type *symbuf;
283 if ((abfd->flags & DYNAMIC) == 0)
285 bfd_set_error (bfd_error_invalid_operation);
286 return -1;
289 lsec = bfd_get_section_by_name (abfd, ".loader");
290 if (lsec == NULL)
292 bfd_set_error (bfd_error_no_symbols);
293 return -1;
296 if (! xcoff_get_section_contents (abfd, lsec))
297 return -1;
298 contents = coff_section_data (abfd, lsec)->contents;
300 coff_section_data (abfd, lsec)->keep_contents = TRUE;
302 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
304 strings = (char *) contents + ldhdr.l_stoff;
306 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
307 if (symbuf == NULL)
308 return -1;
310 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
312 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
313 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
315 struct internal_ldsym ldsym;
317 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
319 symbuf->symbol.the_bfd = abfd;
321 if (ldsym._l._l_l._l_zeroes == 0)
322 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
323 else
325 char *c;
327 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
328 if (c == NULL)
329 return -1;
330 memcpy (c, ldsym._l._l_name, SYMNMLEN);
331 c[SYMNMLEN] = '\0';
332 symbuf->symbol.name = c;
335 if (ldsym.l_smclas == XMC_XO)
336 symbuf->symbol.section = bfd_abs_section_ptr;
337 else
338 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
339 ldsym.l_scnum);
340 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
342 symbuf->symbol.flags = BSF_NO_FLAGS;
343 if ((ldsym.l_smtype & L_EXPORT) != 0)
345 if ((ldsym.l_smtype & L_WEAK) != 0)
346 symbuf->symbol.flags |= BSF_WEAK;
347 else
348 symbuf->symbol.flags |= BSF_GLOBAL;
351 /* FIXME: We have no way to record the other information stored
352 with the loader symbol. */
353 *psyms = (asymbol *) symbuf;
356 *psyms = NULL;
358 return ldhdr.l_nsyms;
361 /* Get the size required to hold the dynamic relocs. */
363 long
364 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
366 asection *lsec;
367 bfd_byte *contents;
368 struct internal_ldhdr ldhdr;
370 if ((abfd->flags & DYNAMIC) == 0)
372 bfd_set_error (bfd_error_invalid_operation);
373 return -1;
376 lsec = bfd_get_section_by_name (abfd, ".loader");
377 if (lsec == NULL)
379 bfd_set_error (bfd_error_no_symbols);
380 return -1;
383 if (! xcoff_get_section_contents (abfd, lsec))
384 return -1;
385 contents = coff_section_data (abfd, lsec)->contents;
387 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
389 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
392 /* Get the dynamic relocs. */
394 long
395 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
396 arelent **prelocs,
397 asymbol **syms)
399 asection *lsec;
400 bfd_byte *contents;
401 struct internal_ldhdr ldhdr;
402 arelent *relbuf;
403 bfd_byte *elrel, *elrelend;
405 if ((abfd->flags & DYNAMIC) == 0)
407 bfd_set_error (bfd_error_invalid_operation);
408 return -1;
411 lsec = bfd_get_section_by_name (abfd, ".loader");
412 if (lsec == NULL)
414 bfd_set_error (bfd_error_no_symbols);
415 return -1;
418 if (! xcoff_get_section_contents (abfd, lsec))
419 return -1;
420 contents = coff_section_data (abfd, lsec)->contents;
422 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
424 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
425 if (relbuf == NULL)
426 return -1;
428 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
430 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
431 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
432 prelocs++)
434 struct internal_ldrel ldrel;
436 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
438 if (ldrel.l_symndx >= 3)
439 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
440 else
442 const char *name;
443 asection *sec;
445 switch (ldrel.l_symndx)
447 case 0:
448 name = ".text";
449 break;
450 case 1:
451 name = ".data";
452 break;
453 case 2:
454 name = ".bss";
455 break;
456 default:
457 abort ();
458 break;
461 sec = bfd_get_section_by_name (abfd, name);
462 if (sec == NULL)
464 bfd_set_error (bfd_error_bad_value);
465 return -1;
468 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
471 relbuf->address = ldrel.l_vaddr;
472 relbuf->addend = 0;
474 /* Most dynamic relocs have the same type. FIXME: This is only
475 correct if ldrel.l_rtype == 0. In other cases, we should use
476 a different howto. */
477 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
479 /* FIXME: We have no way to record the l_rsecnm field. */
481 *prelocs = relbuf;
484 *prelocs = NULL;
486 return ldhdr.l_nreloc;
489 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 static hashval_t
492 xcoff_archive_info_hash (const void *data)
494 const struct xcoff_archive_info *info;
496 info = (const struct xcoff_archive_info *) data;
497 return htab_hash_pointer (info->archive);
500 static int
501 xcoff_archive_info_eq (const void *data1, const void *data2)
503 const struct xcoff_archive_info *info1;
504 const struct xcoff_archive_info *info2;
506 info1 = (const struct xcoff_archive_info *) data1;
507 info2 = (const struct xcoff_archive_info *) data2;
508 return info1->archive == info2->archive;
511 /* Return information about archive ARCHIVE. Return NULL on error. */
513 static struct xcoff_archive_info *
514 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
516 struct xcoff_link_hash_table *htab;
517 struct xcoff_archive_info *entryp, entry;
518 void **slot;
520 htab = xcoff_hash_table (info);
521 entry.archive = archive;
522 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
523 if (!slot)
524 return NULL;
526 entryp = *slot;
527 if (!entryp)
529 entryp = bfd_zalloc (archive, sizeof (entry));
530 if (!entryp)
531 return NULL;
533 entryp->archive = archive;
534 *slot = entryp;
536 return entryp;
539 /* Routine to create an entry in an XCOFF link hash table. */
541 static struct bfd_hash_entry *
542 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
543 struct bfd_hash_table *table,
544 const char *string)
546 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
548 /* Allocate the structure if it has not already been allocated by a
549 subclass. */
550 if (ret == NULL)
551 ret = bfd_hash_allocate (table, sizeof (* ret));
552 if (ret == NULL)
553 return NULL;
555 /* Call the allocation method of the superclass. */
556 ret = ((struct xcoff_link_hash_entry *)
557 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
558 table, string));
559 if (ret != NULL)
561 /* Set local fields. */
562 ret->indx = -1;
563 ret->toc_section = NULL;
564 ret->u.toc_indx = -1;
565 ret->descriptor = NULL;
566 ret->ldsym = NULL;
567 ret->ldindx = -1;
568 ret->flags = 0;
569 ret->smclas = XMC_UA;
572 return (struct bfd_hash_entry *) ret;
575 /* Create a XCOFF link hash table. */
577 struct bfd_link_hash_table *
578 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
580 struct xcoff_link_hash_table *ret;
581 bfd_size_type amt = sizeof (* ret);
583 ret = bfd_malloc (amt);
584 if (ret == NULL)
585 return NULL;
586 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
587 sizeof (struct xcoff_link_hash_entry)))
589 free (ret);
590 return NULL;
593 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
594 ret->debug_section = NULL;
595 ret->loader_section = NULL;
596 ret->ldrel_count = 0;
597 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
598 ret->linkage_section = NULL;
599 ret->toc_section = NULL;
600 ret->descriptor_section = NULL;
601 ret->imports = NULL;
602 ret->file_align = 0;
603 ret->textro = FALSE;
604 ret->gc = FALSE;
605 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
606 xcoff_archive_info_eq, NULL);
607 memset (ret->special_sections, 0, sizeof ret->special_sections);
609 /* The linker will always generate a full a.out header. We need to
610 record that fact now, before the sizeof_headers routine could be
611 called. */
612 xcoff_data (abfd)->full_aouthdr = TRUE;
614 return &ret->root;
617 /* Free a XCOFF link hash table. */
619 void
620 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
622 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
624 _bfd_stringtab_free (ret->debug_strtab);
625 bfd_hash_table_free (&ret->root.table);
626 free (ret);
629 /* Read internal relocs for an XCOFF csect. This is a wrapper around
630 _bfd_coff_read_internal_relocs which tries to take advantage of any
631 relocs which may have been cached for the enclosing section. */
633 static struct internal_reloc *
634 xcoff_read_internal_relocs (bfd *abfd,
635 asection *sec,
636 bfd_boolean cache,
637 bfd_byte *external_relocs,
638 bfd_boolean require_internal,
639 struct internal_reloc *internal_relocs)
641 if (coff_section_data (abfd, sec) != NULL
642 && coff_section_data (abfd, sec)->relocs == NULL
643 && xcoff_section_data (abfd, sec) != NULL)
645 asection *enclosing;
647 enclosing = xcoff_section_data (abfd, sec)->enclosing;
649 if (enclosing != NULL
650 && (coff_section_data (abfd, enclosing) == NULL
651 || coff_section_data (abfd, enclosing)->relocs == NULL)
652 && cache
653 && enclosing->reloc_count > 0)
655 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
656 external_relocs, FALSE, NULL)
657 == NULL)
658 return NULL;
661 if (enclosing != NULL
662 && coff_section_data (abfd, enclosing) != NULL
663 && coff_section_data (abfd, enclosing)->relocs != NULL)
665 size_t off;
667 off = ((sec->rel_filepos - enclosing->rel_filepos)
668 / bfd_coff_relsz (abfd));
670 if (! require_internal)
671 return coff_section_data (abfd, enclosing)->relocs + off;
672 memcpy (internal_relocs,
673 coff_section_data (abfd, enclosing)->relocs + off,
674 sec->reloc_count * sizeof (struct internal_reloc));
675 return internal_relocs;
679 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
680 require_internal, internal_relocs);
683 /* Split FILENAME into an import path and an import filename,
684 storing them in *IMPPATH and *IMPFILE respectively. */
686 bfd_boolean
687 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
688 const char **imppath, const char **impfile)
690 const char *base;
691 size_t length;
692 char *path;
694 base = lbasename (filename);
695 length = base - filename;
696 if (length == 0)
697 /* The filename has no directory component, so use an empty path. */
698 *imppath = "";
699 else if (length == 1)
700 /* The filename is in the root directory. */
701 *imppath = "/";
702 else
704 /* Extract the (non-empty) directory part. Note that we don't
705 need to strip duplicate directory separators from any part
706 of the string; the native linker doesn't do that either. */
707 path = bfd_alloc (abfd, length);
708 if (path == NULL)
709 return FALSE;
710 memcpy (path, filename, length - 1);
711 path[length - 1] = 0;
712 *imppath = path;
714 *impfile = base;
715 return TRUE;
718 /* Set ARCHIVE's import path as though its filename had been given
719 as FILENAME. */
721 bfd_boolean
722 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
723 bfd *archive, const char *filename)
725 struct xcoff_archive_info *archive_info;
727 archive_info = xcoff_get_archive_info (info, archive);
728 return (archive_info != NULL
729 && bfd_xcoff_split_import_path (archive, filename,
730 &archive_info->imppath,
731 &archive_info->impfile));
734 /* H is an imported symbol. Set the import module's path, file and member
735 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
736 no specific import module is specified. */
738 static bfd_boolean
739 xcoff_set_import_path (struct bfd_link_info *info,
740 struct xcoff_link_hash_entry *h,
741 const char *imppath, const char *impfile,
742 const char *impmember)
744 unsigned int c;
745 struct xcoff_import_file **pp;
747 /* We overload the ldindx field to hold the l_ifile value for this
748 symbol. */
749 BFD_ASSERT (h->ldsym == NULL);
750 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
751 if (imppath == NULL)
752 h->ldindx = -1;
753 else
755 /* We start c at 1 because the first entry in the import list is
756 reserved for the library search path. */
757 for (pp = &xcoff_hash_table (info)->imports, c = 1;
758 *pp != NULL;
759 pp = &(*pp)->next, ++c)
761 if (filename_cmp ((*pp)->path, imppath) == 0
762 && filename_cmp ((*pp)->file, impfile) == 0
763 && filename_cmp ((*pp)->member, impmember) == 0)
764 break;
767 if (*pp == NULL)
769 struct xcoff_import_file *n;
770 bfd_size_type amt = sizeof (* n);
772 n = bfd_alloc (info->output_bfd, amt);
773 if (n == NULL)
774 return FALSE;
775 n->next = NULL;
776 n->path = imppath;
777 n->file = impfile;
778 n->member = impmember;
779 *pp = n;
781 h->ldindx = c;
783 return TRUE;
786 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
787 Return true if LDSYM defines H. */
789 static bfd_boolean
790 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
791 struct internal_ldsym *ldsym)
793 /* If we didn't know about H before processing LDSYM, LDSYM
794 definitely defines H. */
795 if (h->root.type == bfd_link_hash_new)
796 return TRUE;
798 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
799 dynamic symbol, LDSYM trumps the current definition of H. */
800 if ((ldsym->l_smtype & L_WEAK) == 0
801 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
802 && (h->flags & XCOFF_DEF_REGULAR) == 0
803 && (h->root.type == bfd_link_hash_defweak
804 || h->root.type == bfd_link_hash_undefweak))
805 return TRUE;
807 /* If H is currently undefined, LDSYM defines it. */
808 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
809 && (h->root.type == bfd_link_hash_undefined
810 || h->root.type == bfd_link_hash_undefweak))
811 return TRUE;
813 return FALSE;
816 /* This function is used to add symbols from a dynamic object to the
817 global symbol table. */
819 static bfd_boolean
820 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
822 asection *lsec;
823 bfd_byte *contents;
824 struct internal_ldhdr ldhdr;
825 const char *strings;
826 bfd_byte *elsym, *elsymend;
827 struct xcoff_import_file *n;
828 unsigned int c;
829 struct xcoff_import_file **pp;
831 /* We can only handle a dynamic object if we are generating an XCOFF
832 output file. */
833 if (info->output_bfd->xvec != abfd->xvec)
835 (*_bfd_error_handler)
836 (_("%s: XCOFF shared object when not producing XCOFF output"),
837 bfd_get_filename (abfd));
838 bfd_set_error (bfd_error_invalid_operation);
839 return FALSE;
842 /* The symbols we use from a dynamic object are not the symbols in
843 the normal symbol table, but, rather, the symbols in the export
844 table. If there is a global symbol in a dynamic object which is
845 not in the export table, the loader will not be able to find it,
846 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
847 libc.a has symbols in the export table which are not in the
848 symbol table. */
850 /* Read in the .loader section. FIXME: We should really use the
851 o_snloader field in the a.out header, rather than grabbing the
852 section by name. */
853 lsec = bfd_get_section_by_name (abfd, ".loader");
854 if (lsec == NULL)
856 (*_bfd_error_handler)
857 (_("%s: dynamic object with no .loader section"),
858 bfd_get_filename (abfd));
859 bfd_set_error (bfd_error_no_symbols);
860 return FALSE;
863 if (! xcoff_get_section_contents (abfd, lsec))
864 return FALSE;
865 contents = coff_section_data (abfd, lsec)->contents;
867 /* Remove the sections from this object, so that they do not get
868 included in the link. */
869 bfd_section_list_clear (abfd);
871 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
873 strings = (char *) contents + ldhdr.l_stoff;
875 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
877 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
879 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
881 struct internal_ldsym ldsym;
882 char nambuf[SYMNMLEN + 1];
883 const char *name;
884 struct xcoff_link_hash_entry *h;
886 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
888 /* We are only interested in exported symbols. */
889 if ((ldsym.l_smtype & L_EXPORT) == 0)
890 continue;
892 if (ldsym._l._l_l._l_zeroes == 0)
893 name = strings + ldsym._l._l_l._l_offset;
894 else
896 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
897 nambuf[SYMNMLEN] = '\0';
898 name = nambuf;
901 /* Normally we could not call xcoff_link_hash_lookup in an add
902 symbols routine, since we might not be using an XCOFF hash
903 table. However, we verified above that we are using an XCOFF
904 hash table. */
906 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
907 TRUE, TRUE);
908 if (h == NULL)
909 return FALSE;
911 if (!xcoff_dynamic_definition_p (h, &ldsym))
912 continue;
914 h->flags |= XCOFF_DEF_DYNAMIC;
915 h->smclas = ldsym.l_smclas;
916 if (h->smclas == XMC_XO)
918 /* This symbol has an absolute value. */
919 if ((ldsym.l_smtype & L_WEAK) != 0)
920 h->root.type = bfd_link_hash_defweak;
921 else
922 h->root.type = bfd_link_hash_defined;
923 h->root.u.def.section = bfd_abs_section_ptr;
924 h->root.u.def.value = ldsym.l_value;
926 else
928 /* Otherwise, we don't bother to actually define the symbol,
929 since we don't have a section to put it in anyhow.
930 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
931 should be imported from the symbol's undef.abfd. */
932 if ((ldsym.l_smtype & L_WEAK) != 0)
933 h->root.type = bfd_link_hash_undefweak;
934 else
935 h->root.type = bfd_link_hash_undefined;
936 h->root.u.undef.abfd = abfd;
939 /* If this symbol defines a function descriptor, then it
940 implicitly defines the function code as well. */
941 if (h->smclas == XMC_DS
942 || (h->smclas == XMC_XO && name[0] != '.'))
943 h->flags |= XCOFF_DESCRIPTOR;
944 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
946 struct xcoff_link_hash_entry *hds;
948 hds = h->descriptor;
949 if (hds == NULL)
951 char *dsnm;
953 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
954 if (dsnm == NULL)
955 return FALSE;
956 dsnm[0] = '.';
957 strcpy (dsnm + 1, name);
958 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
959 TRUE, TRUE, TRUE);
960 free (dsnm);
961 if (hds == NULL)
962 return FALSE;
964 hds->descriptor = h;
965 h->descriptor = hds;
968 if (xcoff_dynamic_definition_p (hds, &ldsym))
970 hds->root.type = h->root.type;
971 hds->flags |= XCOFF_DEF_DYNAMIC;
972 if (h->smclas == XMC_XO)
974 /* An absolute symbol appears to actually define code, not a
975 function descriptor. This is how some math functions are
976 implemented on AIX 4.1. */
977 hds->smclas = XMC_XO;
978 hds->root.u.def.section = bfd_abs_section_ptr;
979 hds->root.u.def.value = ldsym.l_value;
981 else
983 hds->smclas = XMC_PR;
984 hds->root.u.undef.abfd = abfd;
985 /* We do not want to add this to the undefined
986 symbol list. */
992 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
994 free (coff_section_data (abfd, lsec)->contents);
995 coff_section_data (abfd, lsec)->contents = NULL;
998 /* Record this file in the import files. */
999 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
1000 if (n == NULL)
1001 return FALSE;
1002 n->next = NULL;
1004 if (abfd->my_archive == NULL)
1006 if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
1007 &n->path, &n->file))
1008 return FALSE;
1009 n->member = "";
1011 else
1013 struct xcoff_archive_info *archive_info;
1015 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1016 if (!archive_info->impfile)
1018 if (!bfd_xcoff_split_import_path (archive_info->archive,
1019 archive_info->archive->filename,
1020 &archive_info->imppath,
1021 &archive_info->impfile))
1022 return FALSE;
1024 n->path = archive_info->imppath;
1025 n->file = archive_info->impfile;
1026 n->member = bfd_get_filename (abfd);
1029 /* We start c at 1 because the first import file number is reserved
1030 for LIBPATH. */
1031 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1032 *pp != NULL;
1033 pp = &(*pp)->next, ++c)
1035 *pp = n;
1037 xcoff_data (abfd)->import_file_id = c;
1039 return TRUE;
1042 /* xcoff_link_create_extra_sections
1044 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1046 static bfd_boolean
1047 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1049 bfd_boolean return_value = FALSE;
1051 if (info->output_bfd->xvec == abfd->xvec)
1053 /* We need to build a .loader section, so we do it here. This
1054 won't work if we're producing an XCOFF output file with no
1055 XCOFF input files. FIXME. */
1057 if (!info->relocatable
1058 && xcoff_hash_table (info)->loader_section == NULL)
1060 asection *lsec;
1061 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1063 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1064 if (lsec == NULL)
1065 goto end_return;
1067 xcoff_hash_table (info)->loader_section = lsec;
1070 /* Likewise for the linkage section. */
1071 if (xcoff_hash_table (info)->linkage_section == NULL)
1073 asection *lsec;
1074 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1075 | SEC_IN_MEMORY);
1077 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1078 if (lsec == NULL)
1079 goto end_return;
1081 xcoff_hash_table (info)->linkage_section = lsec;
1082 lsec->alignment_power = 2;
1085 /* Likewise for the TOC section. */
1086 if (xcoff_hash_table (info)->toc_section == NULL)
1088 asection *tsec;
1089 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1090 | SEC_IN_MEMORY);
1092 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1093 if (tsec == NULL)
1094 goto end_return;
1096 xcoff_hash_table (info)->toc_section = tsec;
1097 tsec->alignment_power = 2;
1100 /* Likewise for the descriptor section. */
1101 if (xcoff_hash_table (info)->descriptor_section == NULL)
1103 asection *dsec;
1104 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1105 | SEC_IN_MEMORY);
1107 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1108 if (dsec == NULL)
1109 goto end_return;
1111 xcoff_hash_table (info)->descriptor_section = dsec;
1112 dsec->alignment_power = 2;
1115 /* Likewise for the .debug section. */
1116 if (xcoff_hash_table (info)->debug_section == NULL
1117 && info->strip != strip_all)
1119 asection *dsec;
1120 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1122 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1123 if (dsec == NULL)
1124 goto end_return;
1126 xcoff_hash_table (info)->debug_section = dsec;
1130 return_value = TRUE;
1132 end_return:
1134 return return_value;
1137 /* Returns the index of reloc in RELOCS with the least address greater
1138 than or equal to ADDRESS. The relocs are sorted by address. */
1140 static bfd_size_type
1141 xcoff_find_reloc (struct internal_reloc *relocs,
1142 bfd_size_type count,
1143 bfd_vma address)
1145 bfd_size_type min, max, this;
1147 if (count < 2)
1149 if (count == 1 && relocs[0].r_vaddr < address)
1150 return 1;
1151 else
1152 return 0;
1155 min = 0;
1156 max = count;
1158 /* Do a binary search over (min,max]. */
1159 while (min + 1 < max)
1161 bfd_vma raddr;
1163 this = (max + min) / 2;
1164 raddr = relocs[this].r_vaddr;
1165 if (raddr > address)
1166 max = this;
1167 else if (raddr < address)
1168 min = this;
1169 else
1171 min = this;
1172 break;
1176 if (relocs[min].r_vaddr < address)
1177 return min + 1;
1179 while (min > 0
1180 && relocs[min - 1].r_vaddr == address)
1181 --min;
1183 return min;
1186 /* Add all the symbols from an object file to the hash table.
1188 XCOFF is a weird format. A normal XCOFF .o files will have three
1189 COFF sections--.text, .data, and .bss--but each COFF section will
1190 contain many csects. These csects are described in the symbol
1191 table. From the linker's point of view, each csect must be
1192 considered a section in its own right. For example, a TOC entry is
1193 handled as a small XMC_TC csect. The linker must be able to merge
1194 different TOC entries together, which means that it must be able to
1195 extract the XMC_TC csects from the .data section of the input .o
1196 file.
1198 From the point of view of our linker, this is, of course, a hideous
1199 nightmare. We cope by actually creating sections for each csect,
1200 and discarding the original sections. We then have to handle the
1201 relocation entries carefully, since the only way to tell which
1202 csect they belong to is to examine the address. */
1204 static bfd_boolean
1205 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1207 unsigned int n_tmask;
1208 unsigned int n_btshft;
1209 bfd_boolean default_copy;
1210 bfd_size_type symcount;
1211 struct xcoff_link_hash_entry **sym_hash;
1212 asection **csect_cache;
1213 unsigned int *lineno_counts;
1214 bfd_size_type linesz;
1215 asection *o;
1216 asection *last_real;
1217 bfd_boolean keep_syms;
1218 asection *csect;
1219 unsigned int csect_index;
1220 asection *first_csect;
1221 bfd_size_type symesz;
1222 bfd_byte *esym;
1223 bfd_byte *esym_end;
1224 struct reloc_info_struct
1226 struct internal_reloc *relocs;
1227 asection **csects;
1228 bfd_byte *linenos;
1229 } *reloc_info = NULL;
1230 bfd_size_type amt;
1232 keep_syms = obj_coff_keep_syms (abfd);
1234 if ((abfd->flags & DYNAMIC) != 0
1235 && ! info->static_link)
1237 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1238 return FALSE;
1241 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1242 if (! xcoff_link_create_extra_sections (abfd, info))
1243 goto error_return;
1245 if ((abfd->flags & DYNAMIC) != 0
1246 && ! info->static_link)
1247 return TRUE;
1249 n_tmask = coff_data (abfd)->local_n_tmask;
1250 n_btshft = coff_data (abfd)->local_n_btshft;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1256 if (info->keep_memory)
1257 default_copy = FALSE;
1258 else
1259 default_copy = TRUE;
1261 symcount = obj_raw_syment_count (abfd);
1263 /* We keep a list of the linker hash table entries that correspond
1264 to each external symbol. */
1265 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1266 sym_hash = bfd_zalloc (abfd, amt);
1267 if (sym_hash == NULL && symcount != 0)
1268 goto error_return;
1269 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1271 /* Because of the weird stuff we are doing with XCOFF csects, we can
1272 not easily determine which section a symbol is in, so we store
1273 the information in the tdata for the input file. */
1274 amt = symcount * sizeof (asection *);
1275 csect_cache = bfd_zalloc (abfd, amt);
1276 if (csect_cache == NULL && symcount != 0)
1277 goto error_return;
1278 xcoff_data (abfd)->csects = csect_cache;
1280 /* We garbage-collect line-number information on a symbol-by-symbol
1281 basis, so we need to have quick access to the number of entries
1282 per symbol. */
1283 amt = symcount * sizeof (unsigned int);
1284 lineno_counts = bfd_zalloc (abfd, amt);
1285 if (lineno_counts == NULL && symcount != 0)
1286 goto error_return;
1287 xcoff_data (abfd)->lineno_counts = lineno_counts;
1289 /* While splitting sections into csects, we need to assign the
1290 relocs correctly. The relocs and the csects must both be in
1291 order by VMA within a given section, so we handle this by
1292 scanning along the relocs as we process the csects. We index
1293 into reloc_info using the section target_index. */
1294 amt = abfd->section_count + 1;
1295 amt *= sizeof (struct reloc_info_struct);
1296 reloc_info = bfd_zmalloc (amt);
1297 if (reloc_info == NULL)
1298 goto error_return;
1300 /* Read in the relocs and line numbers for each section. */
1301 linesz = bfd_coff_linesz (abfd);
1302 last_real = NULL;
1303 for (o = abfd->sections; o != NULL; o = o->next)
1305 last_real = o;
1307 if ((o->flags & SEC_RELOC) != 0)
1309 reloc_info[o->target_index].relocs =
1310 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1311 amt = o->reloc_count;
1312 amt *= sizeof (asection *);
1313 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1314 if (reloc_info[o->target_index].csects == NULL)
1315 goto error_return;
1318 if ((info->strip == strip_none || info->strip == strip_some)
1319 && o->lineno_count > 0)
1321 bfd_byte *linenos;
1323 amt = linesz * o->lineno_count;
1324 linenos = bfd_malloc (amt);
1325 if (linenos == NULL)
1326 goto error_return;
1327 reloc_info[o->target_index].linenos = linenos;
1328 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1329 || bfd_bread (linenos, amt, abfd) != amt)
1330 goto error_return;
1334 /* Don't let the linker relocation routines discard the symbols. */
1335 obj_coff_keep_syms (abfd) = TRUE;
1337 csect = NULL;
1338 csect_index = 0;
1339 first_csect = NULL;
1341 symesz = bfd_coff_symesz (abfd);
1342 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1343 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1344 esym_end = esym + symcount * symesz;
1346 while (esym < esym_end)
1348 struct internal_syment sym;
1349 union internal_auxent aux;
1350 const char *name;
1351 char buf[SYMNMLEN + 1];
1352 int smtyp;
1353 asection *section;
1354 bfd_vma value;
1355 struct xcoff_link_hash_entry *set_toc;
1357 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1359 /* In this pass we are only interested in symbols with csect
1360 information. */
1361 if (!CSECT_SYM_P (sym.n_sclass))
1363 /* Set csect_cache,
1364 Normally csect is a .pr, .rw etc. created in the loop
1365 If C_FILE or first time, handle special
1367 Advance esym, sym_hash, csect_hash ptrs. */
1368 if (sym.n_sclass == C_FILE)
1369 csect = NULL;
1370 if (csect != NULL)
1371 *csect_cache = csect;
1372 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1373 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1374 else
1375 *csect_cache = NULL;
1376 esym += (sym.n_numaux + 1) * symesz;
1377 sym_hash += sym.n_numaux + 1;
1378 csect_cache += sym.n_numaux + 1;
1379 lineno_counts += sym.n_numaux + 1;
1381 continue;
1384 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1386 if (name == NULL)
1387 goto error_return;
1389 /* If this symbol has line number information attached to it,
1390 and we're not stripping it, count the number of entries and
1391 add them to the count for this csect. In the final link pass
1392 we are going to attach line number information by symbol,
1393 rather than by section, in order to more easily handle
1394 garbage collection. */
1395 if ((info->strip == strip_none || info->strip == strip_some)
1396 && sym.n_numaux > 1
1397 && csect != NULL
1398 && ISFCN (sym.n_type))
1400 union internal_auxent auxlin;
1402 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1403 sym.n_type, sym.n_sclass,
1404 0, sym.n_numaux, (void *) &auxlin);
1406 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1408 asection *enclosing;
1409 bfd_signed_vma linoff;
1411 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1412 if (enclosing == NULL)
1414 (*_bfd_error_handler)
1415 (_("%B: `%s' has line numbers but no enclosing section"),
1416 abfd, name);
1417 bfd_set_error (bfd_error_bad_value);
1418 goto error_return;
1420 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1421 - enclosing->line_filepos);
1422 /* Explicit cast to bfd_signed_vma for compiler. */
1423 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1425 struct internal_lineno lin;
1426 bfd_byte *linpstart;
1428 linpstart = (reloc_info[enclosing->target_index].linenos
1429 + linoff);
1430 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1431 if (lin.l_lnno == 0
1432 && ((bfd_size_type) lin.l_addr.l_symndx
1433 == ((esym
1434 - (bfd_byte *) obj_coff_external_syms (abfd))
1435 / symesz)))
1437 bfd_byte *linpend, *linp;
1439 linpend = (reloc_info[enclosing->target_index].linenos
1440 + enclosing->lineno_count * linesz);
1441 for (linp = linpstart + linesz;
1442 linp < linpend;
1443 linp += linesz)
1445 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1446 (void *) &lin);
1447 if (lin.l_lnno == 0)
1448 break;
1450 *lineno_counts = (linp - linpstart) / linesz;
1451 /* The setting of line_filepos will only be
1452 useful if all the line number entries for a
1453 csect are contiguous; this only matters for
1454 error reporting. */
1455 if (csect->line_filepos == 0)
1456 csect->line_filepos =
1457 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1463 /* Pick up the csect auxiliary information. */
1464 if (sym.n_numaux == 0)
1466 (*_bfd_error_handler)
1467 (_("%B: class %d symbol `%s' has no aux entries"),
1468 abfd, sym.n_sclass, name);
1469 bfd_set_error (bfd_error_bad_value);
1470 goto error_return;
1473 bfd_coff_swap_aux_in (abfd,
1474 (void *) (esym + symesz * sym.n_numaux),
1475 sym.n_type, sym.n_sclass,
1476 sym.n_numaux - 1, sym.n_numaux,
1477 (void *) &aux);
1479 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1481 section = NULL;
1482 value = 0;
1483 set_toc = NULL;
1485 switch (smtyp)
1487 default:
1488 (*_bfd_error_handler)
1489 (_("%B: symbol `%s' has unrecognized csect type %d"),
1490 abfd, name, smtyp);
1491 bfd_set_error (bfd_error_bad_value);
1492 goto error_return;
1494 case XTY_ER:
1495 /* This is an external reference. */
1496 if (sym.n_sclass == C_HIDEXT
1497 || sym.n_scnum != N_UNDEF
1498 || aux.x_csect.x_scnlen.l != 0)
1500 (*_bfd_error_handler)
1501 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1502 abfd, name, sym.n_sclass, sym.n_scnum,
1503 aux.x_csect.x_scnlen.l);
1504 bfd_set_error (bfd_error_bad_value);
1505 goto error_return;
1508 /* An XMC_XO external reference is actually a reference to
1509 an absolute location. */
1510 if (aux.x_csect.x_smclas != XMC_XO)
1511 section = bfd_und_section_ptr;
1512 else
1514 section = bfd_abs_section_ptr;
1515 value = sym.n_value;
1517 break;
1519 case XTY_SD:
1520 csect = NULL;
1521 csect_index = -(unsigned) 1;
1523 /* When we see a TOC anchor, we record the TOC value. */
1524 if (aux.x_csect.x_smclas == XMC_TC0)
1526 if (sym.n_sclass != C_HIDEXT
1527 || aux.x_csect.x_scnlen.l != 0)
1529 (*_bfd_error_handler)
1530 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1531 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1532 bfd_set_error (bfd_error_bad_value);
1533 goto error_return;
1535 xcoff_data (abfd)->toc = sym.n_value;
1538 /* We must merge TOC entries for the same symbol. We can
1539 merge two TOC entries if they are both C_HIDEXT, they
1540 both have the same name, they are both 4 or 8 bytes long, and
1541 they both have a relocation table entry for an external
1542 symbol with the same name. Unfortunately, this means
1543 that we must look through the relocations. Ick.
1545 Logic for 32 bit vs 64 bit.
1546 32 bit has a csect length of 4 for TOC
1547 64 bit has a csect length of 8 for TOC
1549 The conditions to get past the if-check are not that bad.
1550 They are what is used to create the TOC csects in the first
1551 place. */
1552 if (aux.x_csect.x_smclas == XMC_TC
1553 && sym.n_sclass == C_HIDEXT
1554 && info->output_bfd->xvec == abfd->xvec
1555 && ((bfd_xcoff_is_xcoff32 (abfd)
1556 && aux.x_csect.x_scnlen.l == 4)
1557 || (bfd_xcoff_is_xcoff64 (abfd)
1558 && aux.x_csect.x_scnlen.l == 8)))
1560 asection *enclosing;
1561 struct internal_reloc *relocs;
1562 bfd_size_type relindx;
1563 struct internal_reloc *rel;
1565 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1566 if (enclosing == NULL)
1567 goto error_return;
1569 relocs = reloc_info[enclosing->target_index].relocs;
1570 amt = enclosing->reloc_count;
1571 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1572 rel = relocs + relindx;
1574 /* 32 bit R_POS r_size is 31
1575 64 bit R_POS r_size is 63 */
1576 if (relindx < enclosing->reloc_count
1577 && rel->r_vaddr == (bfd_vma) sym.n_value
1578 && rel->r_type == R_POS
1579 && ((bfd_xcoff_is_xcoff32 (abfd)
1580 && rel->r_size == 31)
1581 || (bfd_xcoff_is_xcoff64 (abfd)
1582 && rel->r_size == 63)))
1584 bfd_byte *erelsym;
1586 struct internal_syment relsym;
1588 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1589 + rel->r_symndx * symesz);
1590 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1591 if (EXTERN_SYM_P (relsym.n_sclass))
1593 const char *relname;
1594 char relbuf[SYMNMLEN + 1];
1595 bfd_boolean copy;
1596 struct xcoff_link_hash_entry *h;
1598 /* At this point we know that the TOC entry is
1599 for an externally visible symbol. */
1600 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1601 relbuf);
1602 if (relname == NULL)
1603 goto error_return;
1605 /* We only merge TOC entries if the TC name is
1606 the same as the symbol name. This handles
1607 the normal case, but not common cases like
1608 SYM.P4 which gcc generates to store SYM + 4
1609 in the TOC. FIXME. */
1610 if (strcmp (name, relname) == 0)
1612 copy = (! info->keep_memory
1613 || relsym._n._n_n._n_zeroes != 0
1614 || relsym._n._n_n._n_offset == 0);
1615 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1616 relname, TRUE, copy,
1617 FALSE);
1618 if (h == NULL)
1619 goto error_return;
1621 /* At this point h->root.type could be
1622 bfd_link_hash_new. That should be OK,
1623 since we know for sure that we will come
1624 across this symbol as we step through the
1625 file. */
1627 /* We store h in *sym_hash for the
1628 convenience of the relocate_section
1629 function. */
1630 *sym_hash = h;
1632 if (h->toc_section != NULL)
1634 asection **rel_csects;
1636 /* We already have a TOC entry for this
1637 symbol, so we can just ignore this
1638 one. */
1639 rel_csects =
1640 reloc_info[enclosing->target_index].csects;
1641 rel_csects[relindx] = bfd_und_section_ptr;
1642 break;
1645 /* We are about to create a TOC entry for
1646 this symbol. */
1647 set_toc = h;
1654 asection *enclosing;
1656 /* We need to create a new section. We get the name from
1657 the csect storage mapping class, so that the linker can
1658 accumulate similar csects together. */
1660 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1661 if (NULL == csect)
1662 goto error_return;
1664 /* The enclosing section is the main section : .data, .text
1665 or .bss that the csect is coming from. */
1666 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1667 if (enclosing == NULL)
1668 goto error_return;
1670 if (! bfd_is_abs_section (enclosing)
1671 && ((bfd_vma) sym.n_value < enclosing->vma
1672 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1673 > enclosing->vma + enclosing->size)))
1675 (*_bfd_error_handler)
1676 (_("%B: csect `%s' not in enclosing section"),
1677 abfd, name);
1678 bfd_set_error (bfd_error_bad_value);
1679 goto error_return;
1681 csect->vma = sym.n_value;
1682 csect->filepos = (enclosing->filepos
1683 + sym.n_value
1684 - enclosing->vma);
1685 csect->size = aux.x_csect.x_scnlen.l;
1686 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1687 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1689 /* Record the enclosing section in the tdata for this new
1690 section. */
1691 amt = sizeof (struct coff_section_tdata);
1692 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1693 if (csect->used_by_bfd == NULL)
1694 goto error_return;
1695 amt = sizeof (struct xcoff_section_tdata);
1696 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1697 if (coff_section_data (abfd, csect)->tdata == NULL)
1698 goto error_return;
1699 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1700 xcoff_section_data (abfd, csect)->lineno_count =
1701 enclosing->lineno_count;
1703 if (enclosing->owner == abfd)
1705 struct internal_reloc *relocs;
1706 bfd_size_type relindx;
1707 struct internal_reloc *rel;
1708 asection **rel_csect;
1710 relocs = reloc_info[enclosing->target_index].relocs;
1711 amt = enclosing->reloc_count;
1712 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1714 rel = relocs + relindx;
1715 rel_csect = (reloc_info[enclosing->target_index].csects
1716 + relindx);
1718 csect->rel_filepos = (enclosing->rel_filepos
1719 + relindx * bfd_coff_relsz (abfd));
1720 while (relindx < enclosing->reloc_count
1721 && *rel_csect == NULL
1722 && rel->r_vaddr < csect->vma + csect->size)
1725 *rel_csect = csect;
1726 csect->flags |= SEC_RELOC;
1727 ++csect->reloc_count;
1728 ++relindx;
1729 ++rel;
1730 ++rel_csect;
1734 /* There are a number of other fields and section flags
1735 which we do not bother to set. */
1737 csect_index = ((esym
1738 - (bfd_byte *) obj_coff_external_syms (abfd))
1739 / symesz);
1741 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1743 if (first_csect == NULL)
1744 first_csect = csect;
1746 /* If this symbol is external, we treat it as starting at the
1747 beginning of the newly created section. */
1748 if (EXTERN_SYM_P (sym.n_sclass))
1750 section = csect;
1751 value = 0;
1754 /* If this is a TOC section for a symbol, record it. */
1755 if (set_toc != NULL)
1756 set_toc->toc_section = csect;
1758 break;
1760 case XTY_LD:
1761 /* This is a label definition. The x_scnlen field is the
1762 symbol index of the csect. Usually the XTY_LD symbol will
1763 follow its appropriate XTY_SD symbol. The .set pseudo op can
1764 cause the XTY_LD to not follow the XTY_SD symbol. */
1766 bfd_boolean bad;
1768 bad = FALSE;
1769 if (aux.x_csect.x_scnlen.l < 0
1770 || (aux.x_csect.x_scnlen.l
1771 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1772 bad = TRUE;
1773 if (! bad)
1775 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1776 if (section == NULL
1777 || (section->flags & SEC_HAS_CONTENTS) == 0)
1778 bad = TRUE;
1780 if (bad)
1782 (*_bfd_error_handler)
1783 (_("%B: misplaced XTY_LD `%s'"),
1784 abfd, name);
1785 bfd_set_error (bfd_error_bad_value);
1786 goto error_return;
1788 csect = section;
1789 value = sym.n_value - csect->vma;
1791 break;
1793 case XTY_CM:
1794 /* This is an unitialized csect. We could base the name on
1795 the storage mapping class, but we don't bother except for
1796 an XMC_TD symbol. If this csect is externally visible,
1797 it is a common symbol. We put XMC_TD symbols in sections
1798 named .tocbss, and rely on the linker script to put that
1799 in the TOC area. */
1801 if (aux.x_csect.x_smclas == XMC_TD)
1803 /* The linker script puts the .td section in the data
1804 section after the .tc section. */
1805 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1806 SEC_ALLOC);
1808 else
1809 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1810 SEC_ALLOC);
1812 if (csect == NULL)
1813 goto error_return;
1814 csect->vma = sym.n_value;
1815 csect->size = aux.x_csect.x_scnlen.l;
1816 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1817 /* There are a number of other fields and section flags
1818 which we do not bother to set. */
1820 csect_index = ((esym
1821 - (bfd_byte *) obj_coff_external_syms (abfd))
1822 / symesz);
1824 amt = sizeof (struct coff_section_tdata);
1825 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1826 if (csect->used_by_bfd == NULL)
1827 goto error_return;
1828 amt = sizeof (struct xcoff_section_tdata);
1829 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1830 if (coff_section_data (abfd, csect)->tdata == NULL)
1831 goto error_return;
1832 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1834 if (first_csect == NULL)
1835 first_csect = csect;
1837 if (EXTERN_SYM_P (sym.n_sclass))
1839 csect->flags |= SEC_IS_COMMON;
1840 csect->size = 0;
1841 section = csect;
1842 value = aux.x_csect.x_scnlen.l;
1845 break;
1848 /* Check for magic symbol names. */
1849 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1850 && aux.x_csect.x_smclas != XMC_TC
1851 && aux.x_csect.x_smclas != XMC_TD)
1853 int i = -1;
1855 if (name[0] == '_')
1857 if (strcmp (name, "_text") == 0)
1858 i = XCOFF_SPECIAL_SECTION_TEXT;
1859 else if (strcmp (name, "_etext") == 0)
1860 i = XCOFF_SPECIAL_SECTION_ETEXT;
1861 else if (strcmp (name, "_data") == 0)
1862 i = XCOFF_SPECIAL_SECTION_DATA;
1863 else if (strcmp (name, "_edata") == 0)
1864 i = XCOFF_SPECIAL_SECTION_EDATA;
1865 else if (strcmp (name, "_end") == 0)
1866 i = XCOFF_SPECIAL_SECTION_END;
1868 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1869 i = XCOFF_SPECIAL_SECTION_END2;
1871 if (i != -1)
1872 xcoff_hash_table (info)->special_sections[i] = csect;
1875 /* Now we have enough information to add the symbol to the
1876 linker hash table. */
1878 if (EXTERN_SYM_P (sym.n_sclass))
1880 bfd_boolean copy;
1881 flagword flags;
1883 BFD_ASSERT (section != NULL);
1885 /* We must copy the name into memory if we got it from the
1886 syment itself, rather than the string table. */
1887 copy = default_copy;
1888 if (sym._n._n_n._n_zeroes != 0
1889 || sym._n._n_n._n_offset == 0)
1890 copy = TRUE;
1892 /* Ignore global linkage code when linking statically. */
1893 if (info->static_link
1894 && (smtyp == XTY_SD || smtyp == XTY_LD)
1895 && aux.x_csect.x_smclas == XMC_GL)
1897 section = bfd_und_section_ptr;
1898 value = 0;
1901 /* The AIX linker appears to only detect multiple symbol
1902 definitions when there is a reference to the symbol. If
1903 a symbol is defined multiple times, and the only
1904 references are from the same object file, the AIX linker
1905 appears to permit it. It does not merge the different
1906 definitions, but handles them independently. On the
1907 other hand, if there is a reference, the linker reports
1908 an error.
1910 This matters because the AIX <net/net_globals.h> header
1911 file actually defines an initialized array, so we have to
1912 actually permit that to work.
1914 Just to make matters even more confusing, the AIX linker
1915 appears to permit multiple symbol definitions whenever
1916 the second definition is in an archive rather than an
1917 object file. This may be a consequence of the manner in
1918 which it handles archives: I think it may load the entire
1919 archive in as separate csects, and then let garbage
1920 collection discard symbols.
1922 We also have to handle the case of statically linking a
1923 shared object, which will cause symbol redefinitions,
1924 although this is an easier case to detect. */
1925 else if (info->output_bfd->xvec == abfd->xvec)
1927 if (! bfd_is_und_section (section))
1928 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1929 name, TRUE, copy, FALSE);
1930 else
1931 /* Make a copy of the symbol name to prevent problems with
1932 merging symbols. */
1933 *sym_hash = ((struct xcoff_link_hash_entry *)
1934 bfd_wrapped_link_hash_lookup (abfd, info, name,
1935 TRUE, TRUE, FALSE));
1937 if (*sym_hash == NULL)
1938 goto error_return;
1939 if (((*sym_hash)->root.type == bfd_link_hash_defined
1940 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1941 && ! bfd_is_und_section (section)
1942 && ! bfd_is_com_section (section))
1944 /* This is a second definition of a defined symbol. */
1945 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1946 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1948 /* The existing symbol is from a shared library.
1949 Replace it. */
1950 (*sym_hash)->root.type = bfd_link_hash_undefined;
1951 (*sym_hash)->root.u.undef.abfd =
1952 (*sym_hash)->root.u.def.section->owner;
1954 else if (abfd->my_archive != NULL)
1956 /* This is a redefinition in an object contained
1957 in an archive. Just ignore it. See the
1958 comment above. */
1959 section = bfd_und_section_ptr;
1960 value = 0;
1962 else if (sym.n_sclass == C_AIX_WEAKEXT
1963 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1965 /* At least one of the definitions is weak.
1966 Allow the normal rules to take effect. */
1968 else if ((*sym_hash)->root.u.undef.next != NULL
1969 || info->hash->undefs_tail == &(*sym_hash)->root)
1971 /* This symbol has been referenced. In this
1972 case, we just continue and permit the
1973 multiple definition error. See the comment
1974 above about the behaviour of the AIX linker. */
1976 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1978 /* The symbols are both csects of the same
1979 class. There is at least a chance that this
1980 is a semi-legitimate redefinition. */
1981 section = bfd_und_section_ptr;
1982 value = 0;
1983 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1986 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1987 && (*sym_hash)->root.type == bfd_link_hash_defined
1988 && (bfd_is_und_section (section)
1989 || bfd_is_com_section (section)))
1991 /* This is a reference to a multiply defined symbol.
1992 Report the error now. See the comment above
1993 about the behaviour of the AIX linker. We could
1994 also do this with warning symbols, but I'm not
1995 sure the XCOFF linker is wholly prepared to
1996 handle them, and that would only be a warning,
1997 not an error. */
1998 if (! ((*info->callbacks->multiple_definition)
1999 (info, (*sym_hash)->root.root.string,
2000 NULL, NULL, (bfd_vma) 0,
2001 (*sym_hash)->root.u.def.section->owner,
2002 (*sym_hash)->root.u.def.section,
2003 (*sym_hash)->root.u.def.value)))
2004 goto error_return;
2005 /* Try not to give this error too many times. */
2006 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2010 /* _bfd_generic_link_add_one_symbol may call the linker to
2011 generate an error message, and the linker may try to read
2012 the symbol table to give a good error. Right now, the
2013 line numbers are in an inconsistent state, since they are
2014 counted both in the real sections and in the new csects.
2015 We need to leave the count in the real sections so that
2016 the linker can report the line number of the error
2017 correctly, so temporarily clobber the link to the csects
2018 so that the linker will not try to read the line numbers
2019 a second time from the csects. */
2020 BFD_ASSERT (last_real->next == first_csect);
2021 last_real->next = NULL;
2022 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2023 if (! (_bfd_generic_link_add_one_symbol
2024 (info, abfd, name, flags, section, value,
2025 NULL, copy, TRUE,
2026 (struct bfd_link_hash_entry **) sym_hash)))
2027 goto error_return;
2028 last_real->next = first_csect;
2030 if (smtyp == XTY_CM)
2032 if ((*sym_hash)->root.type != bfd_link_hash_common
2033 || (*sym_hash)->root.u.c.p->section != csect)
2034 /* We don't need the common csect we just created. */
2035 csect->size = 0;
2036 else
2037 (*sym_hash)->root.u.c.p->alignment_power
2038 = csect->alignment_power;
2041 if (info->output_bfd->xvec == abfd->xvec)
2043 int flag;
2045 if (smtyp == XTY_ER
2046 || smtyp == XTY_CM
2047 || section == bfd_und_section_ptr)
2048 flag = XCOFF_REF_REGULAR;
2049 else
2050 flag = XCOFF_DEF_REGULAR;
2051 (*sym_hash)->flags |= flag;
2053 if ((*sym_hash)->smclas == XMC_UA
2054 || flag == XCOFF_DEF_REGULAR)
2055 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2059 if (smtyp == XTY_ER)
2060 *csect_cache = section;
2061 else
2063 *csect_cache = csect;
2064 if (csect != NULL)
2065 xcoff_section_data (abfd, csect)->last_symndx
2066 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2069 esym += (sym.n_numaux + 1) * symesz;
2070 sym_hash += sym.n_numaux + 1;
2071 csect_cache += sym.n_numaux + 1;
2072 lineno_counts += sym.n_numaux + 1;
2075 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2077 /* Make sure that we have seen all the relocs. */
2078 for (o = abfd->sections; o != first_csect; o = o->next)
2080 /* Reset the section size and the line number count, since the
2081 data is now attached to the csects. Don't reset the size of
2082 the .debug section, since we need to read it below in
2083 bfd_xcoff_size_dynamic_sections. */
2084 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2085 o->size = 0;
2086 o->lineno_count = 0;
2088 if ((o->flags & SEC_RELOC) != 0)
2090 bfd_size_type i;
2091 struct internal_reloc *rel;
2092 asection **rel_csect;
2094 rel = reloc_info[o->target_index].relocs;
2095 rel_csect = reloc_info[o->target_index].csects;
2097 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2099 if (*rel_csect == NULL)
2101 (*_bfd_error_handler)
2102 (_("%B: reloc %s:%d not in csect"),
2103 abfd, o->name, i);
2104 bfd_set_error (bfd_error_bad_value);
2105 goto error_return;
2108 /* We identify all function symbols that are the target
2109 of a relocation, so that we can create glue code for
2110 functions imported from dynamic objects. */
2111 if (info->output_bfd->xvec == abfd->xvec
2112 && *rel_csect != bfd_und_section_ptr
2113 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2115 struct xcoff_link_hash_entry *h;
2117 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2118 /* If the symbol name starts with a period, it is
2119 the code of a function. If the symbol is
2120 currently undefined, then add an undefined symbol
2121 for the function descriptor. This should do no
2122 harm, because any regular object that defines the
2123 function should also define the function
2124 descriptor. It helps, because it means that we
2125 will identify the function descriptor with a
2126 dynamic object if a dynamic object defines it. */
2127 if (h->root.root.string[0] == '.'
2128 && h->descriptor == NULL)
2130 struct xcoff_link_hash_entry *hds;
2131 struct bfd_link_hash_entry *bh;
2133 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2134 h->root.root.string + 1,
2135 TRUE, FALSE, TRUE);
2136 if (hds == NULL)
2137 goto error_return;
2138 if (hds->root.type == bfd_link_hash_new)
2140 bh = &hds->root;
2141 if (! (_bfd_generic_link_add_one_symbol
2142 (info, abfd, hds->root.root.string,
2143 (flagword) 0, bfd_und_section_ptr,
2144 (bfd_vma) 0, NULL, FALSE,
2145 TRUE, &bh)))
2146 goto error_return;
2147 hds = (struct xcoff_link_hash_entry *) bh;
2149 hds->flags |= XCOFF_DESCRIPTOR;
2150 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2151 hds->descriptor = h;
2152 h->descriptor = hds;
2154 if (h->root.root.string[0] == '.')
2155 h->flags |= XCOFF_CALLED;
2159 free (reloc_info[o->target_index].csects);
2160 reloc_info[o->target_index].csects = NULL;
2162 /* Reset SEC_RELOC and the reloc_count, since the reloc
2163 information is now attached to the csects. */
2164 o->flags &=~ SEC_RELOC;
2165 o->reloc_count = 0;
2167 /* If we are not keeping memory, free the reloc information. */
2168 if (! info->keep_memory
2169 && coff_section_data (abfd, o) != NULL
2170 && coff_section_data (abfd, o)->relocs != NULL
2171 && ! coff_section_data (abfd, o)->keep_relocs)
2173 free (coff_section_data (abfd, o)->relocs);
2174 coff_section_data (abfd, o)->relocs = NULL;
2178 /* Free up the line numbers. FIXME: We could cache these
2179 somewhere for the final link, to avoid reading them again. */
2180 if (reloc_info[o->target_index].linenos != NULL)
2182 free (reloc_info[o->target_index].linenos);
2183 reloc_info[o->target_index].linenos = NULL;
2187 free (reloc_info);
2189 obj_coff_keep_syms (abfd) = keep_syms;
2191 return TRUE;
2193 error_return:
2194 if (reloc_info != NULL)
2196 for (o = abfd->sections; o != NULL; o = o->next)
2198 if (reloc_info[o->target_index].csects != NULL)
2199 free (reloc_info[o->target_index].csects);
2200 if (reloc_info[o->target_index].linenos != NULL)
2201 free (reloc_info[o->target_index].linenos);
2203 free (reloc_info);
2205 obj_coff_keep_syms (abfd) = keep_syms;
2206 return FALSE;
2209 #undef N_TMASK
2210 #undef N_BTSHFT
2212 /* Add symbols from an XCOFF object file. */
2214 static bfd_boolean
2215 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2217 if (! _bfd_coff_get_external_symbols (abfd))
2218 return FALSE;
2219 if (! xcoff_link_add_symbols (abfd, info))
2220 return FALSE;
2221 if (! info->keep_memory)
2223 if (! _bfd_coff_free_symbols (abfd))
2224 return FALSE;
2226 return TRUE;
2229 /* Look through the loader symbols to see if this dynamic object
2230 should be included in the link. The native linker uses the loader
2231 symbols, not the normal symbol table, so we do too. */
2233 static bfd_boolean
2234 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2235 struct bfd_link_info *info,
2236 bfd_boolean *pneeded,
2237 bfd **subsbfd)
2239 asection *lsec;
2240 bfd_byte *contents;
2241 struct internal_ldhdr ldhdr;
2242 const char *strings;
2243 bfd_byte *elsym, *elsymend;
2245 *pneeded = FALSE;
2247 lsec = bfd_get_section_by_name (abfd, ".loader");
2248 if (lsec == NULL)
2249 /* There are no symbols, so don't try to include it. */
2250 return TRUE;
2252 if (! xcoff_get_section_contents (abfd, lsec))
2253 return FALSE;
2254 contents = coff_section_data (abfd, lsec)->contents;
2256 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2258 strings = (char *) contents + ldhdr.l_stoff;
2260 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2262 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2263 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2265 struct internal_ldsym ldsym;
2266 char nambuf[SYMNMLEN + 1];
2267 const char *name;
2268 struct bfd_link_hash_entry *h;
2270 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2272 /* We are only interested in exported symbols. */
2273 if ((ldsym.l_smtype & L_EXPORT) == 0)
2274 continue;
2276 if (ldsym._l._l_l._l_zeroes == 0)
2277 name = strings + ldsym._l._l_l._l_offset;
2278 else
2280 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2281 nambuf[SYMNMLEN] = '\0';
2282 name = nambuf;
2285 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2287 /* We are only interested in symbols that are currently
2288 undefined. At this point we know that we are using an XCOFF
2289 hash table. */
2290 if (h != NULL
2291 && h->type == bfd_link_hash_undefined
2292 && (((struct xcoff_link_hash_entry *) h)->flags
2293 & XCOFF_DEF_DYNAMIC) == 0)
2295 if (!(*info->callbacks
2296 ->add_archive_element) (info, abfd, name, subsbfd))
2297 return FALSE;
2298 *pneeded = TRUE;
2299 return TRUE;
2303 /* We do not need this shared object. */
2304 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2306 free (coff_section_data (abfd, lsec)->contents);
2307 coff_section_data (abfd, lsec)->contents = NULL;
2310 return TRUE;
2313 /* Look through the symbols to see if this object file should be
2314 included in the link. */
2316 static bfd_boolean
2317 xcoff_link_check_ar_symbols (bfd *abfd,
2318 struct bfd_link_info *info,
2319 bfd_boolean *pneeded,
2320 bfd **subsbfd)
2322 bfd_size_type symesz;
2323 bfd_byte *esym;
2324 bfd_byte *esym_end;
2326 *pneeded = FALSE;
2328 if ((abfd->flags & DYNAMIC) != 0
2329 && ! info->static_link
2330 && info->output_bfd->xvec == abfd->xvec)
2331 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2333 symesz = bfd_coff_symesz (abfd);
2334 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2335 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2336 while (esym < esym_end)
2338 struct internal_syment sym;
2340 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2342 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2344 const char *name;
2345 char buf[SYMNMLEN + 1];
2346 struct bfd_link_hash_entry *h;
2348 /* This symbol is externally visible, and is defined by this
2349 object file. */
2350 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2352 if (name == NULL)
2353 return FALSE;
2354 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2356 /* We are only interested in symbols that are currently
2357 undefined. If a symbol is currently known to be common,
2358 XCOFF linkers do not bring in an object file which
2359 defines it. We also don't bring in symbols to satisfy
2360 undefined references in shared objects. */
2361 if (h != NULL
2362 && h->type == bfd_link_hash_undefined
2363 && (info->output_bfd->xvec != abfd->xvec
2364 || (((struct xcoff_link_hash_entry *) h)->flags
2365 & XCOFF_DEF_DYNAMIC) == 0))
2367 if (!(*info->callbacks
2368 ->add_archive_element) (info, abfd, name, subsbfd))
2369 return FALSE;
2370 *pneeded = TRUE;
2371 return TRUE;
2375 esym += (sym.n_numaux + 1) * symesz;
2378 /* We do not need this object file. */
2379 return TRUE;
2382 /* Check a single archive element to see if we need to include it in
2383 the link. *PNEEDED is set according to whether this element is
2384 needed in the link or not. This is called via
2385 _bfd_generic_link_add_archive_symbols. */
2387 static bfd_boolean
2388 xcoff_link_check_archive_element (bfd *abfd,
2389 struct bfd_link_info *info,
2390 bfd_boolean *pneeded)
2392 bfd_boolean keep_syms_p;
2393 bfd *oldbfd;
2395 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2396 if (!_bfd_coff_get_external_symbols (abfd))
2397 return FALSE;
2399 oldbfd = abfd;
2400 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2401 return FALSE;
2403 if (*pneeded)
2405 /* Potentially, the add_archive_element hook may have set a
2406 substitute BFD for us. */
2407 if (abfd != oldbfd)
2409 if (!keep_syms_p
2410 && !_bfd_coff_free_symbols (oldbfd))
2411 return FALSE;
2412 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2413 if (!_bfd_coff_get_external_symbols (abfd))
2414 return FALSE;
2416 if (!xcoff_link_add_symbols (abfd, info))
2417 return FALSE;
2418 if (info->keep_memory)
2419 keep_syms_p = TRUE;
2422 if (!keep_syms_p)
2424 if (!_bfd_coff_free_symbols (abfd))
2425 return FALSE;
2428 return TRUE;
2431 /* Given an XCOFF BFD, add symbols to the global hash table as
2432 appropriate. */
2434 bfd_boolean
2435 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2437 switch (bfd_get_format (abfd))
2439 case bfd_object:
2440 return xcoff_link_add_object_symbols (abfd, info);
2442 case bfd_archive:
2443 /* If the archive has a map, do the usual search. We then need
2444 to check the archive for dynamic objects, because they may not
2445 appear in the archive map even though they should, perhaps, be
2446 included. If the archive has no map, we just consider each object
2447 file in turn, since that apparently is what the AIX native linker
2448 does. */
2449 if (bfd_has_map (abfd))
2451 if (! (_bfd_generic_link_add_archive_symbols
2452 (abfd, info, xcoff_link_check_archive_element)))
2453 return FALSE;
2457 bfd *member;
2459 member = bfd_openr_next_archived_file (abfd, NULL);
2460 while (member != NULL)
2462 if (bfd_check_format (member, bfd_object)
2463 && (info->output_bfd->xvec == member->xvec)
2464 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2466 bfd_boolean needed;
2468 if (! xcoff_link_check_archive_element (member, info,
2469 &needed))
2470 return FALSE;
2471 if (needed)
2472 member->archive_pass = -1;
2474 member = bfd_openr_next_archived_file (abfd, member);
2478 return TRUE;
2480 default:
2481 bfd_set_error (bfd_error_wrong_format);
2482 return FALSE;
2486 bfd_boolean
2487 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2488 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2489 struct bfd_link_hash_entry *harg)
2491 struct xcoff_link_hash_entry *h;
2493 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2494 return FALSE;
2496 h = (struct xcoff_link_hash_entry *) harg;
2497 h->flags |= XCOFF_DEF_REGULAR;
2498 return TRUE;
2501 /* If symbol H has not been interpreted as a function descriptor,
2502 see whether it should be. Set up its descriptor information if so. */
2504 static bfd_boolean
2505 xcoff_find_function (struct bfd_link_info *info,
2506 struct xcoff_link_hash_entry *h)
2508 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2509 && h->root.root.string[0] != '.')
2511 char *fnname;
2512 struct xcoff_link_hash_entry *hfn;
2513 bfd_size_type amt;
2515 amt = strlen (h->root.root.string) + 2;
2516 fnname = bfd_malloc (amt);
2517 if (fnname == NULL)
2518 return FALSE;
2519 fnname[0] = '.';
2520 strcpy (fnname + 1, h->root.root.string);
2521 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2522 fnname, FALSE, FALSE, TRUE);
2523 free (fnname);
2524 if (hfn != NULL
2525 && hfn->smclas == XMC_PR
2526 && (hfn->root.type == bfd_link_hash_defined
2527 || hfn->root.type == bfd_link_hash_defweak))
2529 h->flags |= XCOFF_DESCRIPTOR;
2530 h->descriptor = hfn;
2531 hfn->descriptor = h;
2534 return TRUE;
2537 /* Return true if the given bfd contains at least one shared object. */
2539 static bfd_boolean
2540 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2541 bfd *archive)
2543 struct xcoff_archive_info *archive_info;
2544 bfd *member;
2546 archive_info = xcoff_get_archive_info (info, archive);
2547 if (!archive_info->know_contains_shared_object_p)
2549 member = bfd_openr_next_archived_file (archive, NULL);
2550 while (member != NULL && (member->flags & DYNAMIC) == 0)
2551 member = bfd_openr_next_archived_file (archive, member);
2553 archive_info->contains_shared_object_p = (member != NULL);
2554 archive_info->know_contains_shared_object_p = 1;
2556 return archive_info->contains_shared_object_p;
2559 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2560 qualifies for export by -bexpall. */
2562 static bfd_boolean
2563 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2565 /* Exclude symbols beginning with '_'. */
2566 if (h->root.root.string[0] == '_')
2567 return FALSE;
2569 /* Exclude archive members that would otherwise be unreferenced. */
2570 if ((h->flags & XCOFF_MARK) == 0
2571 && (h->root.type == bfd_link_hash_defined
2572 || h->root.type == bfd_link_hash_defweak)
2573 && h->root.u.def.section->owner != NULL
2574 && h->root.u.def.section->owner->my_archive != NULL)
2575 return FALSE;
2577 return TRUE;
2580 /* Return true if symbol H qualifies for the forms of automatic export
2581 specified by AUTO_EXPORT_FLAGS. */
2583 static bfd_boolean
2584 xcoff_auto_export_p (struct bfd_link_info *info,
2585 struct xcoff_link_hash_entry *h,
2586 unsigned int auto_export_flags)
2588 /* Don't automatically export things that were explicitly exported. */
2589 if ((h->flags & XCOFF_EXPORT) != 0)
2590 return FALSE;
2592 /* Don't export things that we don't define. */
2593 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2594 return FALSE;
2596 /* Don't export functions; export their descriptors instead. */
2597 if (h->root.root.string[0] == '.')
2598 return FALSE;
2600 /* We don't export a symbol which is being defined by an object
2601 included from an archive which contains a shared object. The
2602 rationale is that if an archive contains both an unshared and
2603 a shared object, then there must be some reason that the
2604 unshared object is unshared, and we don't want to start
2605 providing a shared version of it. In particular, this solves
2606 a bug involving the _savefNN set of functions. gcc will call
2607 those functions without providing a slot to restore the TOC,
2608 so it is essential that these functions be linked in directly
2609 and not from a shared object, which means that a shared
2610 object which also happens to link them in must not export
2611 them. This is confusing, but I haven't been able to think of
2612 a different approach. Note that the symbols can, of course,
2613 be exported explicitly. */
2614 if (h->root.type == bfd_link_hash_defined
2615 || h->root.type == bfd_link_hash_defweak)
2617 bfd *owner;
2619 owner = h->root.u.def.section->owner;
2620 if (owner != NULL
2621 && owner->my_archive != NULL
2622 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2623 return FALSE;
2626 /* Otherwise, all symbols are exported by -bexpfull. */
2627 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2628 return TRUE;
2630 /* Despite its name, -bexpall exports most but not all symbols. */
2631 if ((auto_export_flags & XCOFF_EXPALL) != 0
2632 && xcoff_covered_by_expall_p (h))
2633 return TRUE;
2635 return FALSE;
2638 /* Return true if relocation REL needs to be copied to the .loader section.
2639 If REL is against a global symbol, H is that symbol, otherwise it
2640 is null. */
2642 static bfd_boolean
2643 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2644 struct xcoff_link_hash_entry *h)
2646 if (!xcoff_hash_table (info)->loader_section)
2647 return FALSE;
2649 switch (rel->r_type)
2651 case R_TOC:
2652 case R_GL:
2653 case R_TCL:
2654 case R_TRL:
2655 case R_TRLA:
2656 /* We should never need a .loader reloc for a TOC-relative reloc. */
2657 return FALSE;
2659 default:
2660 /* In this case, relocations against defined symbols can be resolved
2661 statically. */
2662 if (h == NULL
2663 || h->root.type == bfd_link_hash_defined
2664 || h->root.type == bfd_link_hash_defweak
2665 || h->root.type == bfd_link_hash_common)
2666 return FALSE;
2668 /* We will always provide a local definition of function symbols,
2669 even if we don't have one yet. */
2670 if ((h->flags & XCOFF_CALLED) != 0)
2671 return FALSE;
2673 return TRUE;
2675 case R_POS:
2676 case R_NEG:
2677 case R_RL:
2678 case R_RLA:
2679 /* Absolute relocations against absolute symbols can be
2680 resolved statically. */
2681 if (h != NULL
2682 && (h->root.type == bfd_link_hash_defined
2683 || h->root.type == bfd_link_hash_defweak)
2684 && bfd_is_abs_section (h->root.u.def.section))
2685 return FALSE;
2687 return TRUE;
2691 /* Mark a symbol as not being garbage, including the section in which
2692 it is defined. */
2694 static inline bfd_boolean
2695 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2697 if ((h->flags & XCOFF_MARK) != 0)
2698 return TRUE;
2700 h->flags |= XCOFF_MARK;
2702 /* If we're marking an undefined symbol, try find some way of
2703 defining it. */
2704 if (!info->relocatable
2705 && (h->flags & XCOFF_IMPORT) == 0
2706 && (h->flags & XCOFF_DEF_REGULAR) == 0
2707 && (h->root.type == bfd_link_hash_undefined
2708 || h->root.type == bfd_link_hash_undefweak))
2710 /* First check whether this symbol can be interpreted as an
2711 undefined function descriptor for a defined function symbol. */
2712 if (!xcoff_find_function (info, h))
2713 return FALSE;
2715 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2716 && (h->descriptor->root.type == bfd_link_hash_defined
2717 || h->descriptor->root.type == bfd_link_hash_defweak))
2719 /* This is a descriptor for a defined symbol, but the input
2720 objects have not defined the descriptor itself. Fill in
2721 the definition automatically.
2723 Note that we do this even if we found a dynamic definition
2724 of H. The local function definition logically overrides
2725 the dynamic one. */
2726 asection *sec;
2728 sec = xcoff_hash_table (info)->descriptor_section;
2729 h->root.type = bfd_link_hash_defined;
2730 h->root.u.def.section = sec;
2731 h->root.u.def.value = sec->size;
2732 h->smclas = XMC_DS;
2733 h->flags |= XCOFF_DEF_REGULAR;
2735 /* The size of the function descriptor depends on whether this
2736 is xcoff32 (12) or xcoff64 (24). */
2737 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2739 /* A function descriptor uses two relocs: one for the
2740 associated code, and one for the TOC address. */
2741 xcoff_hash_table (info)->ldrel_count += 2;
2742 sec->reloc_count += 2;
2744 /* Mark the function itself. */
2745 if (!xcoff_mark_symbol (info, h->descriptor))
2746 return FALSE;
2748 /* Mark the TOC section, so that we get an anchor
2749 to relocate against. */
2750 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2751 return FALSE;
2753 /* We handle writing out the contents of the descriptor in
2754 xcoff_write_global_symbol. */
2756 else if (info->static_link)
2757 /* We can't get a symbol value dynamically, so just assume
2758 that it's undefined. */
2759 h->flags |= XCOFF_WAS_UNDEFINED;
2760 else if ((h->flags & XCOFF_CALLED) != 0)
2762 /* This is a function symbol for which we need to create
2763 linkage code. */
2764 asection *sec;
2765 struct xcoff_link_hash_entry *hds;
2767 /* Mark the descriptor (and its TOC section). */
2768 hds = h->descriptor;
2769 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2770 || hds->root.type == bfd_link_hash_undefweak)
2771 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2772 if (!xcoff_mark_symbol (info, hds))
2773 return FALSE;
2775 /* Treat this symbol as undefined if the descriptor was. */
2776 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2777 h->flags |= XCOFF_WAS_UNDEFINED;
2779 /* Allocate room for the global linkage code itself. */
2780 sec = xcoff_hash_table (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 (info->output_bfd);
2788 /* The global linkage code requires a TOC entry for the
2789 descriptor. */
2790 if (hds->toc_section == NULL)
2792 int byte_size;
2794 /* 32 vs 64
2795 xcoff32 uses 4 bytes in the toc.
2796 xcoff64 uses 8 bytes in the toc. */
2797 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2798 byte_size = 8;
2799 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2800 byte_size = 4;
2801 else
2802 return FALSE;
2804 /* Allocate room in the fallback TOC section. */
2805 hds->toc_section = xcoff_hash_table (info)->toc_section;
2806 hds->u.toc_offset = hds->toc_section->size;
2807 hds->toc_section->size += byte_size;
2808 if (!xcoff_mark (info, hds->toc_section))
2809 return FALSE;
2811 /* Allocate room for a static and dynamic R_TOC
2812 relocation. */
2813 ++xcoff_hash_table (info)->ldrel_count;
2814 ++hds->toc_section->reloc_count;
2816 /* Set the index to -2 to force this symbol to
2817 get written out. */
2818 hds->indx = -2;
2819 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2822 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2824 /* Record that the symbol was undefined, then import it.
2825 -brtl links use a special fake import file. */
2826 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2827 if (xcoff_hash_table (info)->rtld)
2829 if (!xcoff_set_import_path (info, h, "", "..", ""))
2830 return FALSE;
2832 else
2834 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2835 return FALSE;
2840 if (h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
2843 asection *hsec;
2845 hsec = h->root.u.def.section;
2846 if (! bfd_is_abs_section (hsec)
2847 && (hsec->flags & SEC_MARK) == 0)
2849 if (! xcoff_mark (info, hsec))
2850 return FALSE;
2854 if (h->toc_section != NULL
2855 && (h->toc_section->flags & SEC_MARK) == 0)
2857 if (! xcoff_mark (info, h->toc_section))
2858 return FALSE;
2861 return TRUE;
2864 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2865 If the symbol exists, set FLAGS. */
2867 static bfd_boolean
2868 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2869 const char *name, unsigned int flags)
2871 struct xcoff_link_hash_entry *h;
2873 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2874 FALSE, FALSE, TRUE);
2875 if (h != NULL)
2877 h->flags |= flags;
2878 if (h->root.type == bfd_link_hash_defined
2879 || h->root.type == bfd_link_hash_defweak)
2881 if (!xcoff_mark (info, h->root.u.def.section))
2882 return FALSE;
2885 return TRUE;
2888 /* The mark phase of garbage collection. For a given section, mark
2889 it, and all the sections which define symbols to which it refers.
2890 Because this function needs to look at the relocs, we also count
2891 the number of relocs which need to be copied into the .loader
2892 section. */
2894 static bfd_boolean
2895 xcoff_mark (struct bfd_link_info *info, asection *sec)
2897 if (bfd_is_abs_section (sec)
2898 || (sec->flags & SEC_MARK) != 0)
2899 return TRUE;
2901 sec->flags |= SEC_MARK;
2903 if (sec->owner->xvec == info->output_bfd->xvec
2904 && coff_section_data (sec->owner, sec) != NULL
2905 && xcoff_section_data (sec->owner, sec) != NULL)
2907 struct xcoff_link_hash_entry **syms;
2908 struct internal_reloc *rel, *relend;
2909 asection **csects;
2910 unsigned long i, first, last;
2912 /* Mark all the symbols in this section. */
2913 syms = obj_xcoff_sym_hashes (sec->owner);
2914 csects = xcoff_data (sec->owner)->csects;
2915 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2916 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2917 for (i = first; i <= last; i++)
2918 if (csects[i] == sec
2919 && syms[i] != NULL
2920 && (syms[i]->flags & XCOFF_MARK) == 0)
2922 if (!xcoff_mark_symbol (info, syms[i]))
2923 return FALSE;
2926 /* Look through the section relocs. */
2927 if ((sec->flags & SEC_RELOC) != 0
2928 && sec->reloc_count > 0)
2930 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2931 NULL, FALSE, NULL);
2932 if (rel == NULL)
2933 return FALSE;
2934 relend = rel + sec->reloc_count;
2935 for (; rel < relend; rel++)
2937 struct xcoff_link_hash_entry *h;
2939 if ((unsigned int) rel->r_symndx
2940 > obj_raw_syment_count (sec->owner))
2941 continue;
2943 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2944 if (h != NULL)
2946 if ((h->flags & XCOFF_MARK) == 0)
2948 if (!xcoff_mark_symbol (info, h))
2949 return FALSE;
2952 else
2954 asection *rsec;
2956 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2957 if (rsec != NULL
2958 && (rsec->flags & SEC_MARK) == 0)
2960 if (!xcoff_mark (info, rsec))
2961 return FALSE;
2965 /* See if this reloc needs to be copied into the .loader
2966 section. */
2967 if (xcoff_need_ldrel_p (info, rel, h))
2969 ++xcoff_hash_table (info)->ldrel_count;
2970 if (h != NULL)
2971 h->flags |= XCOFF_LDREL;
2975 if (! info->keep_memory
2976 && coff_section_data (sec->owner, sec) != NULL
2977 && coff_section_data (sec->owner, sec)->relocs != NULL
2978 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2980 free (coff_section_data (sec->owner, sec)->relocs);
2981 coff_section_data (sec->owner, sec)->relocs = NULL;
2986 return TRUE;
2989 /* Routines that are called after all the input files have been
2990 handled, but before the sections are laid out in memory. */
2992 /* The sweep phase of garbage collection. Remove all garbage
2993 sections. */
2995 static void
2996 xcoff_sweep (struct bfd_link_info *info)
2998 bfd *sub;
3000 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3002 asection *o;
3004 for (o = sub->sections; o != NULL; o = o->next)
3006 if ((o->flags & SEC_MARK) == 0)
3008 /* Keep all sections from non-XCOFF input files. Keep
3009 special sections. Keep .debug sections for the
3010 moment. */
3011 if (sub->xvec != info->output_bfd->xvec
3012 || o == xcoff_hash_table (info)->debug_section
3013 || o == xcoff_hash_table (info)->loader_section
3014 || o == xcoff_hash_table (info)->linkage_section
3015 || o == xcoff_hash_table (info)->descriptor_section
3016 || strcmp (o->name, ".debug") == 0)
3017 o->flags |= SEC_MARK;
3018 else
3020 o->size = 0;
3021 o->reloc_count = 0;
3028 /* Record the number of elements in a set. This is used to output the
3029 correct csect length. */
3031 bfd_boolean
3032 bfd_xcoff_link_record_set (bfd *output_bfd,
3033 struct bfd_link_info *info,
3034 struct bfd_link_hash_entry *harg,
3035 bfd_size_type size)
3037 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3038 struct xcoff_link_size_list *n;
3039 bfd_size_type amt;
3041 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3042 return TRUE;
3044 /* This will hardly ever be called. I don't want to burn four bytes
3045 per global symbol, so instead the size is kept on a linked list
3046 attached to the hash table. */
3047 amt = sizeof (* n);
3048 n = bfd_alloc (output_bfd, amt);
3049 if (n == NULL)
3050 return FALSE;
3051 n->next = xcoff_hash_table (info)->size_list;
3052 n->h = h;
3053 n->size = size;
3054 xcoff_hash_table (info)->size_list = n;
3056 h->flags |= XCOFF_HAS_SIZE;
3058 return TRUE;
3061 /* Import a symbol. */
3063 bfd_boolean
3064 bfd_xcoff_import_symbol (bfd *output_bfd,
3065 struct bfd_link_info *info,
3066 struct bfd_link_hash_entry *harg,
3067 bfd_vma val,
3068 const char *imppath,
3069 const char *impfile,
3070 const char *impmember,
3071 unsigned int syscall_flag)
3073 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3075 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3076 return TRUE;
3078 /* A symbol name which starts with a period is the code for a
3079 function. If the symbol is undefined, then add an undefined
3080 symbol for the function descriptor, and import that instead. */
3081 if (h->root.root.string[0] == '.'
3082 && h->root.type == bfd_link_hash_undefined
3083 && val == (bfd_vma) -1)
3085 struct xcoff_link_hash_entry *hds;
3087 hds = h->descriptor;
3088 if (hds == NULL)
3090 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3091 h->root.root.string + 1,
3092 TRUE, FALSE, TRUE);
3093 if (hds == NULL)
3094 return FALSE;
3095 if (hds->root.type == bfd_link_hash_new)
3097 hds->root.type = bfd_link_hash_undefined;
3098 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3100 hds->flags |= XCOFF_DESCRIPTOR;
3101 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3102 hds->descriptor = h;
3103 h->descriptor = hds;
3106 /* Now, if the descriptor is undefined, import the descriptor
3107 rather than the symbol we were told to import. FIXME: Is
3108 this correct in all cases? */
3109 if (hds->root.type == bfd_link_hash_undefined)
3110 h = hds;
3113 h->flags |= (XCOFF_IMPORT | syscall_flag);
3115 if (val != (bfd_vma) -1)
3117 if (h->root.type == bfd_link_hash_defined
3118 && (! bfd_is_abs_section (h->root.u.def.section)
3119 || h->root.u.def.value != val))
3121 if (! ((*info->callbacks->multiple_definition)
3122 (info, h->root.root.string, h->root.u.def.section->owner,
3123 h->root.u.def.section, h->root.u.def.value,
3124 output_bfd, bfd_abs_section_ptr, val)))
3125 return FALSE;
3128 h->root.type = bfd_link_hash_defined;
3129 h->root.u.def.section = bfd_abs_section_ptr;
3130 h->root.u.def.value = val;
3131 h->smclas = XMC_XO;
3134 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3135 return FALSE;
3137 return TRUE;
3140 /* Export a symbol. */
3142 bfd_boolean
3143 bfd_xcoff_export_symbol (bfd *output_bfd,
3144 struct bfd_link_info *info,
3145 struct bfd_link_hash_entry *harg)
3147 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3149 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3150 return TRUE;
3152 h->flags |= XCOFF_EXPORT;
3154 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3155 I'm just going to ignore it until somebody explains it. */
3157 /* Make sure we don't garbage collect this symbol. */
3158 if (! xcoff_mark_symbol (info, h))
3159 return FALSE;
3161 /* If this is a function descriptor, make sure we don't garbage
3162 collect the associated function code. We normally don't have to
3163 worry about this, because the descriptor will be attached to a
3164 section with relocs, but if we are creating the descriptor
3165 ourselves those relocs will not be visible to the mark code. */
3166 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3168 if (! xcoff_mark_symbol (info, h->descriptor))
3169 return FALSE;
3172 return TRUE;
3175 /* Count a reloc against a symbol. This is called for relocs
3176 generated by the linker script, typically for global constructors
3177 and destructors. */
3179 bfd_boolean
3180 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3181 struct bfd_link_info *info,
3182 const char *name)
3184 struct xcoff_link_hash_entry *h;
3186 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3187 return TRUE;
3189 h = ((struct xcoff_link_hash_entry *)
3190 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3191 FALSE));
3192 if (h == NULL)
3194 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3195 bfd_set_error (bfd_error_no_symbols);
3196 return FALSE;
3199 h->flags |= XCOFF_REF_REGULAR;
3200 if (xcoff_hash_table (info)->loader_section)
3202 h->flags |= XCOFF_LDREL;
3203 ++xcoff_hash_table (info)->ldrel_count;
3206 /* Mark the symbol to avoid garbage collection. */
3207 if (! xcoff_mark_symbol (info, h))
3208 return FALSE;
3210 return TRUE;
3213 /* This function is called for each symbol to which the linker script
3214 assigns a value. */
3216 bfd_boolean
3217 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3218 struct bfd_link_info *info,
3219 const char *name)
3221 struct xcoff_link_hash_entry *h;
3223 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3224 return TRUE;
3226 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3227 FALSE);
3228 if (h == NULL)
3229 return FALSE;
3231 h->flags |= XCOFF_DEF_REGULAR;
3233 return TRUE;
3236 /* An xcoff_link_hash_traverse callback for which DATA points to an
3237 xcoff_loader_info. Mark all symbols that should be automatically
3238 exported. */
3240 static bfd_boolean
3241 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3243 struct xcoff_loader_info *ldinfo;
3245 ldinfo = (struct xcoff_loader_info *) data;
3246 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3248 if (!xcoff_mark_symbol (ldinfo->info, h))
3249 ldinfo->failed = TRUE;
3251 return TRUE;
3254 /* Add a symbol to the .loader symbols, if necessary. */
3256 /* INPUT_BFD has an external symbol associated with hash table entry H
3257 and csect CSECT. Return true if INPUT_BFD defines H. */
3259 static bfd_boolean
3260 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3261 asection *csect)
3263 switch (h->root.type)
3265 case bfd_link_hash_defined:
3266 case bfd_link_hash_defweak:
3267 /* No input bfd owns absolute symbols. They are written by
3268 xcoff_write_global_symbol instead. */
3269 return (!bfd_is_abs_section (csect)
3270 && h->root.u.def.section == csect);
3272 case bfd_link_hash_common:
3273 return h->root.u.c.p->section->owner == input_bfd;
3275 case bfd_link_hash_undefined:
3276 case bfd_link_hash_undefweak:
3277 /* We can't treat undef.abfd as the owner because that bfd
3278 might be a dynamic object. Allow any bfd to claim it. */
3279 return TRUE;
3281 default:
3282 abort ();
3286 /* See if H should have a loader symbol associated with it. */
3288 static bfd_boolean
3289 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3290 struct xcoff_link_hash_entry *h)
3292 bfd_size_type amt;
3294 /* Warn if this symbol is exported but not defined. */
3295 if ((h->flags & XCOFF_EXPORT) != 0
3296 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3298 (*_bfd_error_handler)
3299 (_("warning: attempt to export undefined symbol `%s'"),
3300 h->root.root.string);
3301 return TRUE;
3304 /* We need to add a symbol to the .loader section if it is mentioned
3305 in a reloc which we are copying to the .loader section and it was
3306 not defined or common, or if it is the entry point, or if it is
3307 being exported. */
3308 if (((h->flags & XCOFF_LDREL) == 0
3309 || h->root.type == bfd_link_hash_defined
3310 || h->root.type == bfd_link_hash_defweak
3311 || h->root.type == bfd_link_hash_common)
3312 && (h->flags & XCOFF_ENTRY) == 0
3313 && (h->flags & XCOFF_EXPORT) == 0)
3314 return TRUE;
3316 /* We need to add this symbol to the .loader symbols. */
3318 BFD_ASSERT (h->ldsym == NULL);
3319 amt = sizeof (struct internal_ldsym);
3320 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3321 if (h->ldsym == NULL)
3323 ldinfo->failed = TRUE;
3324 return FALSE;
3327 if ((h->flags & XCOFF_IMPORT) != 0)
3329 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3330 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3331 h->smclas = XMC_DS;
3332 h->ldsym->l_ifile = h->ldindx;
3335 /* The first 3 symbol table indices are reserved to indicate the
3336 data, text and bss sections. */
3337 h->ldindx = ldinfo->ldsym_count + 3;
3339 ++ldinfo->ldsym_count;
3341 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3342 h->ldsym, h->root.root.string))
3343 return FALSE;
3345 h->flags |= XCOFF_BUILT_LDSYM;
3346 return TRUE;
3349 /* An xcoff_htab_traverse callback that is called for each symbol
3350 once garbage collection is complete. */
3352 static bfd_boolean
3353 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3355 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3357 if (h->root.type == bfd_link_hash_warning)
3358 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3360 /* __rtinit, this symbol has special handling. */
3361 if (h->flags & XCOFF_RTINIT)
3362 return TRUE;
3364 /* We don't want to garbage collect symbols which are not defined in
3365 XCOFF files. This is a convenient place to mark them. */
3366 if (xcoff_hash_table (ldinfo->info)->gc
3367 && (h->flags & XCOFF_MARK) == 0
3368 && (h->root.type == bfd_link_hash_defined
3369 || h->root.type == bfd_link_hash_defweak)
3370 && (h->root.u.def.section->owner == NULL
3371 || (h->root.u.def.section->owner->xvec
3372 != ldinfo->info->output_bfd->xvec)))
3373 h->flags |= XCOFF_MARK;
3375 /* Skip discarded symbols. */
3376 if (xcoff_hash_table (ldinfo->info)->gc
3377 && (h->flags & XCOFF_MARK) == 0)
3378 return TRUE;
3380 /* If this is still a common symbol, and it wasn't garbage
3381 collected, we need to actually allocate space for it in the .bss
3382 section. */
3383 if (h->root.type == bfd_link_hash_common
3384 && h->root.u.c.p->section->size == 0)
3386 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3387 h->root.u.c.p->section->size = h->root.u.c.size;
3390 if (xcoff_hash_table (ldinfo->info)->loader_section)
3392 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3393 h->flags |= XCOFF_EXPORT;
3395 if (!xcoff_build_ldsym (ldinfo, h))
3396 return FALSE;
3399 return TRUE;
3402 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3403 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3404 csect information, if any. NAME is the function's name if the name
3405 is stored in the .debug section, otherwise it is null.
3407 Return 1 if we should include an appropriately-adjusted ISYM
3408 in the output file, 0 if we should discard ISYM, or -1 if an
3409 error occured. */
3411 static int
3412 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3413 struct internal_syment *isym,
3414 union internal_auxent *aux,
3415 struct xcoff_link_hash_entry *h,
3416 asection *csect, const char *name)
3418 int smtyp;
3420 /* If we are skipping this csect, we want to strip the symbol too. */
3421 if (csect == NULL)
3422 return 0;
3424 /* Likewise if we garbage-collected the csect. */
3425 if (xcoff_hash_table (info)->gc
3426 && !bfd_is_abs_section (csect)
3427 && !bfd_is_und_section (csect)
3428 && (csect->flags & SEC_MARK) == 0)
3429 return 0;
3431 /* An XCOFF linker always removes C_STAT symbols. */
3432 if (isym->n_sclass == C_STAT)
3433 return 0;
3435 /* We generate the TOC anchor separately. */
3436 if (isym->n_sclass == C_HIDEXT
3437 && aux->x_csect.x_smclas == XMC_TC0)
3438 return 0;
3440 /* If we are stripping all symbols, we want to discard this one. */
3441 if (info->strip == strip_all)
3442 return 0;
3444 /* Discard symbols that are defined elsewhere. */
3445 if (EXTERN_SYM_P (isym->n_sclass))
3447 if ((h->flags & XCOFF_ALLOCATED) != 0)
3448 return 0;
3449 if (!xcoff_final_definition_p (input_bfd, h, csect))
3450 return 0;
3453 /* If we're discarding local symbols, check whether ISYM is local. */
3454 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3455 if (info->discard == discard_all
3456 && !EXTERN_SYM_P (isym->n_sclass)
3457 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3458 return 0;
3460 /* If we're stripping debugging symbols, check whether ISYM is one. */
3461 if (info->strip == strip_debugger
3462 && isym->n_scnum == N_DEBUG)
3463 return 0;
3465 /* If we are stripping symbols based on name, check how ISYM's
3466 name should be handled. */
3467 if (info->strip == strip_some
3468 || info->discard == discard_l)
3470 char buf[SYMNMLEN + 1];
3472 if (name == NULL)
3474 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3475 if (name == NULL)
3476 return -1;
3479 if (info->strip == strip_some
3480 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3481 return 0;
3483 if (info->discard == discard_l
3484 && !EXTERN_SYM_P (isym->n_sclass)
3485 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3486 && bfd_is_local_label_name (input_bfd, name))
3487 return 0;
3490 return 1;
3493 /* Lay out the .loader section, filling in the header and the import paths.
3494 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3496 static bfd_boolean
3497 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3498 const char *libpath)
3500 bfd *output_bfd;
3501 struct xcoff_link_hash_table *htab;
3502 struct internal_ldhdr *ldhdr;
3503 struct xcoff_import_file *fl;
3504 bfd_size_type stoff;
3505 size_t impsize, impcount;
3506 asection *lsec;
3507 char *out;
3509 /* Work out the size of the import file names. Each import file ID
3510 consists of three null terminated strings: the path, the file
3511 name, and the archive member name. The first entry in the list
3512 of names is the path to use to find objects, which the linker has
3513 passed in as the libpath argument. For some reason, the path
3514 entry in the other import file names appears to always be empty. */
3515 output_bfd = ldinfo->output_bfd;
3516 htab = xcoff_hash_table (ldinfo->info);
3517 impsize = strlen (libpath) + 3;
3518 impcount = 1;
3519 for (fl = htab->imports; fl != NULL; fl = fl->next)
3521 ++impcount;
3522 impsize += (strlen (fl->path)
3523 + strlen (fl->file)
3524 + strlen (fl->member)
3525 + 3);
3528 /* Set up the .loader section header. */
3529 ldhdr = &htab->ldhdr;
3530 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3531 ldhdr->l_nsyms = ldinfo->ldsym_count;
3532 ldhdr->l_nreloc = htab->ldrel_count;
3533 ldhdr->l_istlen = impsize;
3534 ldhdr->l_nimpid = impcount;
3535 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3536 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3537 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3538 ldhdr->l_stlen = ldinfo->string_size;
3539 stoff = ldhdr->l_impoff + impsize;
3540 if (ldinfo->string_size == 0)
3541 ldhdr->l_stoff = 0;
3542 else
3543 ldhdr->l_stoff = stoff;
3545 /* 64 bit elements to ldhdr
3546 The swap out routine for 32 bit will ignore them.
3547 Nothing fancy, symbols come after the header and relocs come
3548 after symbols. */
3549 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3550 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3551 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3553 /* We now know the final size of the .loader section. Allocate
3554 space for it. */
3555 lsec = htab->loader_section;
3556 lsec->size = stoff + ldhdr->l_stlen;
3557 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3558 if (lsec->contents == NULL)
3559 return FALSE;
3561 /* Set up the header. */
3562 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3564 /* Set up the import file names. */
3565 out = (char *) lsec->contents + ldhdr->l_impoff;
3566 strcpy (out, libpath);
3567 out += strlen (libpath) + 1;
3568 *out++ = '\0';
3569 *out++ = '\0';
3570 for (fl = htab->imports; fl != NULL; fl = fl->next)
3572 const char *s;
3574 s = fl->path;
3575 while ((*out++ = *s++) != '\0')
3577 s = fl->file;
3578 while ((*out++ = *s++) != '\0')
3580 s = fl->member;
3581 while ((*out++ = *s++) != '\0')
3585 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3587 /* Set up the symbol string table. */
3588 if (ldinfo->string_size > 0)
3590 memcpy (out, ldinfo->strings, ldinfo->string_size);
3591 free (ldinfo->strings);
3592 ldinfo->strings = NULL;
3595 /* We can't set up the symbol table or the relocs yet, because we
3596 don't yet know the final position of the various sections. The
3597 .loader symbols are written out when the corresponding normal
3598 symbols are written out in xcoff_link_input_bfd or
3599 xcoff_write_global_symbol. The .loader relocs are written out
3600 when the corresponding normal relocs are handled in
3601 xcoff_link_input_bfd. */
3603 return TRUE;
3606 /* Build the .loader section. This is called by the XCOFF linker
3607 emulation before_allocation routine. We must set the size of the
3608 .loader section before the linker lays out the output file.
3609 LIBPATH is the library path to search for shared objects; this is
3610 normally built from the -L arguments passed to the linker. ENTRY
3611 is the name of the entry point symbol (the -e linker option).
3612 FILE_ALIGN is the alignment to use for sections within the file
3613 (the -H linker option). MAXSTACK is the maximum stack size (the
3614 -bmaxstack linker option). MAXDATA is the maximum data size (the
3615 -bmaxdata linker option). GC is whether to do garbage collection
3616 (the -bgc linker option). MODTYPE is the module type (the
3617 -bmodtype linker option). TEXTRO is whether the text section must
3618 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3619 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3620 is set by this routine to csects with magic names like _end. */
3622 bfd_boolean
3623 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3624 struct bfd_link_info *info,
3625 const char *libpath,
3626 const char *entry,
3627 unsigned long file_align,
3628 unsigned long maxstack,
3629 unsigned long maxdata,
3630 bfd_boolean gc,
3631 int modtype,
3632 bfd_boolean textro,
3633 unsigned int auto_export_flags,
3634 asection **special_sections,
3635 bfd_boolean rtld)
3637 struct xcoff_loader_info ldinfo;
3638 int i;
3639 asection *sec;
3640 bfd *sub;
3641 struct bfd_strtab_hash *debug_strtab;
3642 bfd_byte *debug_contents = NULL;
3643 bfd_size_type amt;
3645 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3647 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3648 special_sections[i] = NULL;
3649 return TRUE;
3652 ldinfo.failed = FALSE;
3653 ldinfo.output_bfd = output_bfd;
3654 ldinfo.info = info;
3655 ldinfo.auto_export_flags = auto_export_flags;
3656 ldinfo.ldsym_count = 0;
3657 ldinfo.string_size = 0;
3658 ldinfo.strings = NULL;
3659 ldinfo.string_alc = 0;
3661 xcoff_data (output_bfd)->maxstack = maxstack;
3662 xcoff_data (output_bfd)->maxdata = maxdata;
3663 xcoff_data (output_bfd)->modtype = modtype;
3665 xcoff_hash_table (info)->file_align = file_align;
3666 xcoff_hash_table (info)->textro = textro;
3667 xcoff_hash_table (info)->rtld = rtld;
3669 /* __rtinit */
3670 if (xcoff_hash_table (info)->loader_section
3671 && (info->init_function || info->fini_function || rtld))
3673 struct xcoff_link_hash_entry *hsym;
3674 struct internal_ldsym *ldsym;
3676 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3677 "__rtinit", FALSE, FALSE, TRUE);
3678 if (hsym == NULL)
3680 (*_bfd_error_handler)
3681 (_("error: undefined symbol __rtinit"));
3682 return FALSE;
3685 xcoff_mark_symbol (info, hsym);
3686 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3688 /* __rtinit initialized. */
3689 amt = sizeof (* ldsym);
3690 ldsym = bfd_malloc (amt);
3692 ldsym->l_value = 0; /* Will be filled in later. */
3693 ldsym->l_scnum = 2; /* Data section. */
3694 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3695 ldsym->l_smclas = 5; /* .rw. */
3696 ldsym->l_ifile = 0; /* Special system loader symbol. */
3697 ldsym->l_parm = 0; /* NA. */
3699 /* Force __rtinit to be the first symbol in the loader symbol table
3700 See xcoff_build_ldsyms
3702 The first 3 symbol table indices are reserved to indicate the data,
3703 text and bss sections. */
3704 BFD_ASSERT (0 == ldinfo.ldsym_count);
3706 hsym->ldindx = 3;
3707 ldinfo.ldsym_count = 1;
3708 hsym->ldsym = ldsym;
3710 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3711 hsym->ldsym, hsym->root.root.string))
3712 return FALSE;
3714 /* This symbol is written out by xcoff_write_global_symbol
3715 Set stuff up so xcoff_write_global_symbol logic works. */
3716 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3717 hsym->root.type = bfd_link_hash_defined;
3718 hsym->root.u.def.value = 0;
3721 /* Garbage collect unused sections. */
3722 if (info->relocatable || !gc)
3724 gc = FALSE;
3725 xcoff_hash_table (info)->gc = FALSE;
3727 /* We still need to call xcoff_mark, in order to set ldrel_count
3728 correctly. */
3729 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3731 asection *o;
3733 for (o = sub->sections; o != NULL; o = o->next)
3735 /* We shouldn't unconditionaly mark the TOC section.
3736 The output file should only have a TOC if either
3737 (a) one of the input files did or (b) we end up
3738 creating TOC references as part of the link process. */
3739 if (o != xcoff_hash_table (info)->toc_section
3740 && (o->flags & SEC_MARK) == 0)
3742 if (! xcoff_mark (info, o))
3743 goto error_return;
3748 else
3750 if (entry != NULL
3751 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3752 goto error_return;
3753 if (info->init_function != NULL
3754 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3755 goto error_return;
3756 if (info->fini_function != NULL
3757 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3758 goto error_return;
3759 if (auto_export_flags != 0)
3761 xcoff_link_hash_traverse (xcoff_hash_table (info),
3762 xcoff_mark_auto_exports, &ldinfo);
3763 if (ldinfo.failed)
3764 goto error_return;
3766 xcoff_sweep (info);
3767 xcoff_hash_table (info)->gc = TRUE;
3770 /* Return special sections to the caller. */
3771 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3773 sec = xcoff_hash_table (info)->special_sections[i];
3775 if (sec != NULL
3776 && gc
3777 && (sec->flags & SEC_MARK) == 0)
3778 sec = NULL;
3780 special_sections[i] = sec;
3783 if (info->input_bfds == NULL)
3784 /* I'm not sure what to do in this bizarre case. */
3785 return TRUE;
3787 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3788 (void *) &ldinfo);
3789 if (ldinfo.failed)
3790 goto error_return;
3792 if (xcoff_hash_table (info)->loader_section
3793 && !xcoff_build_loader_section (&ldinfo, libpath))
3794 goto error_return;
3796 /* Allocate space for the magic sections. */
3797 sec = xcoff_hash_table (info)->linkage_section;
3798 if (sec->size > 0)
3800 sec->contents = bfd_zalloc (output_bfd, sec->size);
3801 if (sec->contents == NULL)
3802 goto error_return;
3804 sec = xcoff_hash_table (info)->toc_section;
3805 if (sec->size > 0)
3807 sec->contents = bfd_zalloc (output_bfd, sec->size);
3808 if (sec->contents == NULL)
3809 goto error_return;
3811 sec = xcoff_hash_table (info)->descriptor_section;
3812 if (sec->size > 0)
3814 sec->contents = bfd_zalloc (output_bfd, sec->size);
3815 if (sec->contents == NULL)
3816 goto error_return;
3819 /* Now that we've done garbage collection, decide which symbols to keep,
3820 and figure out the contents of the .debug section. */
3821 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3823 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3825 asection *subdeb;
3826 bfd_size_type symcount;
3827 long *debug_index;
3828 asection **csectpp;
3829 unsigned int *lineno_counts;
3830 struct xcoff_link_hash_entry **sym_hash;
3831 bfd_byte *esym, *esymend;
3832 bfd_size_type symesz;
3834 if (sub->xvec != info->output_bfd->xvec)
3835 continue;
3837 if ((sub->flags & DYNAMIC) != 0
3838 && !info->static_link)
3839 continue;
3841 if (! _bfd_coff_get_external_symbols (sub))
3842 goto error_return;
3844 symcount = obj_raw_syment_count (sub);
3845 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3846 if (debug_index == NULL)
3847 goto error_return;
3848 xcoff_data (sub)->debug_indices = debug_index;
3850 if (info->strip == strip_all
3851 || info->strip == strip_debugger
3852 || info->discard == discard_all)
3853 /* We're stripping all debugging information, so there's no need
3854 to read SUB's .debug section. */
3855 subdeb = NULL;
3856 else
3858 /* Grab the contents of SUB's .debug section, if any. */
3859 subdeb = bfd_get_section_by_name (sub, ".debug");
3860 if (subdeb != NULL && subdeb->size > 0)
3862 /* We use malloc and copy the names into the debug
3863 stringtab, rather than bfd_alloc, because I expect
3864 that, when linking many files together, many of the
3865 strings will be the same. Storing the strings in the
3866 hash table should save space in this case. */
3867 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3868 goto error_return;
3872 csectpp = xcoff_data (sub)->csects;
3873 lineno_counts = xcoff_data (sub)->lineno_counts;
3874 sym_hash = obj_xcoff_sym_hashes (sub);
3875 symesz = bfd_coff_symesz (sub);
3876 esym = (bfd_byte *) obj_coff_external_syms (sub);
3877 esymend = esym + symcount * symesz;
3879 while (esym < esymend)
3881 struct internal_syment sym;
3882 union internal_auxent aux;
3883 asection *csect;
3884 const char *name;
3885 int keep_p;
3887 bfd_coff_swap_sym_in (sub, esym, &sym);
3889 /* Read in the csect information, if any. */
3890 if (CSECT_SYM_P (sym.n_sclass))
3892 BFD_ASSERT (sym.n_numaux > 0);
3893 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3894 sym.n_type, sym.n_sclass,
3895 sym.n_numaux - 1, sym.n_numaux, &aux);
3898 /* If this symbol's name is stored in the debug section,
3899 get a pointer to it. */
3900 if (debug_contents != NULL
3901 && sym._n._n_n._n_zeroes == 0
3902 && bfd_coff_symname_in_debug (sub, &sym))
3903 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3904 else
3905 name = NULL;
3907 /* Decide whether to copy this symbol to the output file. */
3908 csect = *csectpp;
3909 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3910 *sym_hash, csect, name);
3911 if (keep_p < 0)
3912 return FALSE;
3914 if (!keep_p)
3915 /* Use a debug_index of -2 to record that a symbol should
3916 be stripped. */
3917 *debug_index = -2;
3918 else
3920 /* See whether we should store the symbol name in the
3921 output .debug section. */
3922 if (name != NULL)
3924 bfd_size_type indx;
3926 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3927 if (indx == (bfd_size_type) -1)
3928 goto error_return;
3929 *debug_index = indx;
3931 else
3932 *debug_index = -1;
3933 if (*sym_hash != 0)
3934 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3935 if (*lineno_counts > 0)
3936 csect->output_section->lineno_count += *lineno_counts;
3939 esym += (sym.n_numaux + 1) * symesz;
3940 csectpp += sym.n_numaux + 1;
3941 sym_hash += sym.n_numaux + 1;
3942 lineno_counts += sym.n_numaux + 1;
3943 debug_index += sym.n_numaux + 1;
3946 if (debug_contents)
3948 free (debug_contents);
3949 debug_contents = NULL;
3951 /* Clear the size of subdeb, so that it is not included directly
3952 in the output file. */
3953 subdeb->size = 0;
3956 if (! info->keep_memory)
3958 if (! _bfd_coff_free_symbols (sub))
3959 goto error_return;
3963 if (info->strip != strip_all)
3964 xcoff_hash_table (info)->debug_section->size =
3965 _bfd_stringtab_size (debug_strtab);
3967 return TRUE;
3969 error_return:
3970 if (ldinfo.strings != NULL)
3971 free (ldinfo.strings);
3972 if (debug_contents != NULL)
3973 free (debug_contents);
3974 return FALSE;
3977 bfd_boolean
3978 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3979 const char *init,
3980 const char *fini,
3981 bfd_boolean rtld)
3983 struct bfd_in_memory *bim;
3985 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3986 if (bim == NULL)
3987 return FALSE;
3989 bim->size = 0;
3990 bim->buffer = 0;
3992 abfd->link_next = 0;
3993 abfd->format = bfd_object;
3994 abfd->iostream = (void *) bim;
3995 abfd->flags = BFD_IN_MEMORY;
3996 abfd->iovec = &_bfd_memory_iovec;
3997 abfd->direction = write_direction;
3998 abfd->origin = 0;
3999 abfd->where = 0;
4001 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
4002 return FALSE;
4004 /* need to reset to unknown or it will not be read back in correctly */
4005 abfd->format = bfd_unknown;
4006 abfd->direction = read_direction;
4007 abfd->where = 0;
4009 return TRUE;
4012 /* Return the section that defines H. Return null if no section does. */
4014 static asection *
4015 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4017 switch (h->root.type)
4019 case bfd_link_hash_defined:
4020 case bfd_link_hash_defweak:
4021 return h->root.u.def.section;
4023 case bfd_link_hash_common:
4024 return h->root.u.c.p->section;
4026 default:
4027 return NULL;
4031 /* Add a .loader relocation for input relocation IREL. If the loader
4032 relocation should be against an output section, HSEC points to the
4033 input section that IREL is against, otherwise HSEC is null. H is the
4034 symbol that IREL is against, or null if it isn't against a global symbol.
4035 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4037 static bfd_boolean
4038 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4039 asection *output_section, bfd *reference_bfd,
4040 struct internal_reloc *irel, asection *hsec,
4041 struct xcoff_link_hash_entry *h)
4043 struct internal_ldrel ldrel;
4045 ldrel.l_vaddr = irel->r_vaddr;
4046 if (hsec != NULL)
4048 const char *secname;
4050 secname = hsec->output_section->name;
4051 if (strcmp (secname, ".text") == 0)
4052 ldrel.l_symndx = 0;
4053 else if (strcmp (secname, ".data") == 0)
4054 ldrel.l_symndx = 1;
4055 else if (strcmp (secname, ".bss") == 0)
4056 ldrel.l_symndx = 2;
4057 else
4059 (*_bfd_error_handler)
4060 (_("%B: loader reloc in unrecognized section `%s'"),
4061 reference_bfd, secname);
4062 bfd_set_error (bfd_error_nonrepresentable_section);
4063 return FALSE;
4066 else if (h != NULL)
4068 if (h->ldindx < 0)
4070 (*_bfd_error_handler)
4071 (_("%B: `%s' in loader reloc but not loader sym"),
4072 reference_bfd, h->root.root.string);
4073 bfd_set_error (bfd_error_bad_value);
4074 return FALSE;
4076 ldrel.l_symndx = h->ldindx;
4078 else
4079 ldrel.l_symndx = -(bfd_size_type) 1;
4081 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4082 ldrel.l_rsecnm = output_section->target_index;
4083 if (xcoff_hash_table (flinfo->info)->textro
4084 && strcmp (output_section->name, ".text") == 0)
4086 (*_bfd_error_handler)
4087 (_("%B: loader reloc in read-only section %A"),
4088 reference_bfd, output_section);
4089 bfd_set_error (bfd_error_invalid_operation);
4090 return FALSE;
4092 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4093 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4094 return TRUE;
4097 /* Link an input file into the linker output file. This function
4098 handles all the sections and relocations of the input file at once. */
4100 static bfd_boolean
4101 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4102 bfd *input_bfd)
4104 bfd *output_bfd;
4105 const char *strings;
4106 bfd_size_type syment_base;
4107 unsigned int n_tmask;
4108 unsigned int n_btshft;
4109 bfd_boolean copy, hash;
4110 bfd_size_type isymesz;
4111 bfd_size_type osymesz;
4112 bfd_size_type linesz;
4113 bfd_byte *esym;
4114 bfd_byte *esym_end;
4115 struct xcoff_link_hash_entry **sym_hash;
4116 struct internal_syment *isymp;
4117 asection **csectpp;
4118 unsigned int *lineno_counts;
4119 long *debug_index;
4120 long *indexp;
4121 unsigned long output_index;
4122 bfd_byte *outsym;
4123 unsigned int incls;
4124 asection *oline;
4125 bfd_boolean keep_syms;
4126 asection *o;
4128 /* We can just skip DYNAMIC files, unless this is a static link. */
4129 if ((input_bfd->flags & DYNAMIC) != 0
4130 && ! flinfo->info->static_link)
4131 return TRUE;
4133 /* Move all the symbols to the output file. */
4134 output_bfd = flinfo->output_bfd;
4135 strings = NULL;
4136 syment_base = obj_raw_syment_count (output_bfd);
4137 isymesz = bfd_coff_symesz (input_bfd);
4138 osymesz = bfd_coff_symesz (output_bfd);
4139 linesz = bfd_coff_linesz (input_bfd);
4140 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4142 n_tmask = coff_data (input_bfd)->local_n_tmask;
4143 n_btshft = coff_data (input_bfd)->local_n_btshft;
4145 /* Define macros so that ISFCN, et. al., macros work correctly. */
4146 #define N_TMASK n_tmask
4147 #define N_BTSHFT n_btshft
4149 copy = FALSE;
4150 if (! flinfo->info->keep_memory)
4151 copy = TRUE;
4152 hash = TRUE;
4153 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4154 hash = FALSE;
4156 if (! _bfd_coff_get_external_symbols (input_bfd))
4157 return FALSE;
4159 /* Make one pass over the symbols and assign indices to symbols that
4160 we have decided to keep. Also use create .loader symbol information
4161 and update information in hash table entries. */
4162 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4163 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4164 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4165 csectpp = xcoff_data (input_bfd)->csects;
4166 debug_index = xcoff_data (input_bfd)->debug_indices;
4167 isymp = flinfo->internal_syms;
4168 indexp = flinfo->sym_indices;
4169 output_index = syment_base;
4170 while (esym < esym_end)
4172 union internal_auxent aux;
4173 int smtyp = 0;
4174 int add;
4176 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4178 /* Read in the csect information, if any. */
4179 if (CSECT_SYM_P (isymp->n_sclass))
4181 BFD_ASSERT (isymp->n_numaux > 0);
4182 bfd_coff_swap_aux_in (input_bfd,
4183 (void *) (esym + isymesz * isymp->n_numaux),
4184 isymp->n_type, isymp->n_sclass,
4185 isymp->n_numaux - 1, isymp->n_numaux,
4186 (void *) &aux);
4188 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4191 /* If this symbol is in the .loader section, swap out the
4192 .loader symbol information. If this is an external symbol
4193 reference to a defined symbol, though, then wait until we get
4194 to the definition. */
4195 if (EXTERN_SYM_P (isymp->n_sclass)
4196 && *sym_hash != NULL
4197 && (*sym_hash)->ldsym != NULL
4198 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4200 struct xcoff_link_hash_entry *h;
4201 struct internal_ldsym *ldsym;
4203 h = *sym_hash;
4204 ldsym = h->ldsym;
4205 if (isymp->n_scnum > 0)
4207 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4208 ldsym->l_value = (isymp->n_value
4209 + (*csectpp)->output_section->vma
4210 + (*csectpp)->output_offset
4211 - (*csectpp)->vma);
4213 else
4215 ldsym->l_scnum = isymp->n_scnum;
4216 ldsym->l_value = isymp->n_value;
4219 ldsym->l_smtype = smtyp;
4220 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4221 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4222 || (h->flags & XCOFF_IMPORT) != 0)
4223 ldsym->l_smtype |= L_IMPORT;
4224 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4225 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4226 || (h->flags & XCOFF_EXPORT) != 0)
4227 ldsym->l_smtype |= L_EXPORT;
4228 if ((h->flags & XCOFF_ENTRY) != 0)
4229 ldsym->l_smtype |= L_ENTRY;
4230 if (isymp->n_sclass == C_AIX_WEAKEXT)
4231 ldsym->l_smtype |= L_WEAK;
4233 ldsym->l_smclas = aux.x_csect.x_smclas;
4235 if (ldsym->l_ifile == (bfd_size_type) -1)
4236 ldsym->l_ifile = 0;
4237 else if (ldsym->l_ifile == 0)
4239 if ((ldsym->l_smtype & L_IMPORT) == 0)
4240 ldsym->l_ifile = 0;
4241 else
4243 bfd *impbfd;
4245 if (h->root.type == bfd_link_hash_defined
4246 || h->root.type == bfd_link_hash_defweak)
4247 impbfd = h->root.u.def.section->owner;
4248 else if (h->root.type == bfd_link_hash_undefined
4249 || h->root.type == bfd_link_hash_undefweak)
4250 impbfd = h->root.u.undef.abfd;
4251 else
4252 impbfd = NULL;
4254 if (impbfd == NULL)
4255 ldsym->l_ifile = 0;
4256 else
4258 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4259 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4264 ldsym->l_parm = 0;
4266 BFD_ASSERT (h->ldindx >= 0);
4267 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4268 (flinfo->ldsym
4269 + ((h->ldindx - 3)
4270 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4271 h->ldsym = NULL;
4273 /* Fill in snentry now that we know the target_index. */
4274 if ((h->flags & XCOFF_ENTRY) != 0
4275 && (h->root.type == bfd_link_hash_defined
4276 || h->root.type == bfd_link_hash_defweak))
4278 xcoff_data (output_bfd)->snentry =
4279 h->root.u.def.section->output_section->target_index;
4283 add = 1 + isymp->n_numaux;
4285 if (*debug_index == -2)
4286 /* We've decided to strip this symbol. */
4287 *indexp = -1;
4288 else
4290 /* Assign the next unused index to this symbol. */
4291 *indexp = output_index;
4293 if (EXTERN_SYM_P (isymp->n_sclass))
4295 BFD_ASSERT (*sym_hash != NULL);
4296 (*sym_hash)->indx = output_index;
4299 /* If this is a symbol in the TOC which we may have merged
4300 (class XMC_TC), remember the symbol index of the TOC
4301 symbol. */
4302 if (isymp->n_sclass == C_HIDEXT
4303 && aux.x_csect.x_smclas == XMC_TC
4304 && *sym_hash != NULL)
4306 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4307 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4308 (*sym_hash)->u.toc_indx = output_index;
4311 output_index += add;
4314 esym += add * isymesz;
4315 isymp += add;
4316 csectpp += add;
4317 sym_hash += add;
4318 debug_index += add;
4319 ++indexp;
4320 for (--add; add > 0; --add)
4321 *indexp++ = -1;
4324 /* Now write out the symbols that we decided to keep. */
4326 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4327 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4328 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4329 isymp = flinfo->internal_syms;
4330 indexp = flinfo->sym_indices;
4331 csectpp = xcoff_data (input_bfd)->csects;
4332 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4333 debug_index = xcoff_data (input_bfd)->debug_indices;
4334 outsym = flinfo->outsyms;
4335 incls = 0;
4336 oline = NULL;
4337 while (esym < esym_end)
4339 int add;
4341 add = 1 + isymp->n_numaux;
4343 if (*indexp < 0)
4344 esym += add * isymesz;
4345 else
4347 struct internal_syment isym;
4348 int i;
4350 /* Adjust the symbol in order to output it. */
4351 isym = *isymp;
4352 if (isym._n._n_n._n_zeroes == 0
4353 && isym._n._n_n._n_offset != 0)
4355 /* This symbol has a long name. Enter it in the string
4356 table we are building. If *debug_index != -1, the
4357 name has already been entered in the .debug section. */
4358 if (*debug_index >= 0)
4359 isym._n._n_n._n_offset = *debug_index;
4360 else
4362 const char *name;
4363 bfd_size_type indx;
4365 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4367 if (name == NULL)
4368 return FALSE;
4369 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4370 if (indx == (bfd_size_type) -1)
4371 return FALSE;
4372 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4376 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4377 multiple definition problems when linking a shared object
4378 statically. (The native linker doesn't enter __rtinit into
4379 the normal table at all, but having a local symbol can make
4380 the objdump output easier to read.) */
4381 if (isym.n_sclass == C_EXT
4382 && *sym_hash
4383 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4384 isym.n_sclass = C_HIDEXT;
4386 /* The value of a C_FILE symbol is the symbol index of the
4387 next C_FILE symbol. The value of the last C_FILE symbol
4388 is -1. We try to get this right, below, just before we
4389 write the symbols out, but in the general case we may
4390 have to write the symbol out twice. */
4391 if (isym.n_sclass == C_FILE)
4393 if (flinfo->last_file_index != -1
4394 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4396 /* We must correct the value of the last C_FILE entry. */
4397 flinfo->last_file.n_value = *indexp;
4398 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4400 /* The last C_FILE symbol is in this input file. */
4401 bfd_coff_swap_sym_out (output_bfd,
4402 (void *) &flinfo->last_file,
4403 (void *) (flinfo->outsyms
4404 + ((flinfo->last_file_index
4405 - syment_base)
4406 * osymesz)));
4408 else
4410 /* We have already written out the last C_FILE
4411 symbol. We need to write it out again. We
4412 borrow *outsym temporarily. */
4413 file_ptr pos;
4415 bfd_coff_swap_sym_out (output_bfd,
4416 (void *) &flinfo->last_file,
4417 (void *) outsym);
4419 pos = obj_sym_filepos (output_bfd);
4420 pos += flinfo->last_file_index * osymesz;
4421 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4422 || (bfd_bwrite (outsym, osymesz, output_bfd)
4423 != osymesz))
4424 return FALSE;
4428 flinfo->last_file_index = *indexp;
4429 flinfo->last_file = isym;
4432 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4433 into the line numbers. We update the symbol values when
4434 we handle the line numbers. */
4435 if (isym.n_sclass == C_BINCL
4436 || isym.n_sclass == C_EINCL)
4438 isym.n_value = flinfo->line_filepos;
4439 ++incls;
4441 /* The value of a C_BSTAT symbol is the symbol table
4442 index of the containing csect. */
4443 else if (isym.n_sclass == C_BSTAT)
4445 bfd_vma indx;
4447 indx = isym.n_value;
4448 if (indx < obj_raw_syment_count (input_bfd))
4450 long symindx;
4452 symindx = flinfo->sym_indices[indx];
4453 if (symindx < 0)
4454 isym.n_value = 0;
4455 else
4456 isym.n_value = symindx;
4459 else if (isym.n_sclass != C_ESTAT
4460 && isym.n_sclass != C_DECL
4461 && isym.n_scnum > 0)
4463 isym.n_scnum = (*csectpp)->output_section->target_index;
4464 isym.n_value += ((*csectpp)->output_section->vma
4465 + (*csectpp)->output_offset
4466 - (*csectpp)->vma);
4469 /* Output the symbol. */
4470 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4472 esym += isymesz;
4473 outsym += osymesz;
4475 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4477 union internal_auxent aux;
4479 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4480 isymp->n_sclass, i, isymp->n_numaux,
4481 (void *) &aux);
4483 if (isymp->n_sclass == C_FILE)
4485 /* This is the file name (or some comment put in by
4486 the compiler). If it is long, we must put it in
4487 the string table. */
4488 if (aux.x_file.x_n.x_zeroes == 0
4489 && aux.x_file.x_n.x_offset != 0)
4491 const char *filename;
4492 bfd_size_type indx;
4494 BFD_ASSERT (aux.x_file.x_n.x_offset
4495 >= STRING_SIZE_SIZE);
4496 if (strings == NULL)
4498 strings = _bfd_coff_read_string_table (input_bfd);
4499 if (strings == NULL)
4500 return FALSE;
4502 filename = strings + aux.x_file.x_n.x_offset;
4503 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4504 hash, copy);
4505 if (indx == (bfd_size_type) -1)
4506 return FALSE;
4507 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4510 else if (CSECT_SYM_P (isymp->n_sclass)
4511 && i + 1 == isymp->n_numaux)
4514 /* We don't support type checking. I don't know if
4515 anybody does. */
4516 aux.x_csect.x_parmhash = 0;
4517 /* I don't think anybody uses these fields, but we'd
4518 better clobber them just in case. */
4519 aux.x_csect.x_stab = 0;
4520 aux.x_csect.x_snstab = 0;
4522 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4524 unsigned long indx;
4526 indx = aux.x_csect.x_scnlen.l;
4527 if (indx < obj_raw_syment_count (input_bfd))
4529 long symindx;
4531 symindx = flinfo->sym_indices[indx];
4532 if (symindx < 0)
4534 aux.x_csect.x_scnlen.l = 0;
4536 else
4538 aux.x_csect.x_scnlen.l = symindx;
4543 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4545 unsigned long indx;
4547 if (ISFCN (isymp->n_type)
4548 || ISTAG (isymp->n_sclass)
4549 || isymp->n_sclass == C_BLOCK
4550 || isymp->n_sclass == C_FCN)
4552 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4553 if (indx > 0
4554 && indx < obj_raw_syment_count (input_bfd))
4556 /* We look forward through the symbol for
4557 the index of the next symbol we are going
4558 to include. I don't know if this is
4559 entirely right. */
4560 while (flinfo->sym_indices[indx] < 0
4561 && indx < obj_raw_syment_count (input_bfd))
4562 ++indx;
4563 if (indx >= obj_raw_syment_count (input_bfd))
4564 indx = output_index;
4565 else
4566 indx = flinfo->sym_indices[indx];
4567 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4572 indx = aux.x_sym.x_tagndx.l;
4573 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4575 long symindx;
4577 symindx = flinfo->sym_indices[indx];
4578 if (symindx < 0)
4579 aux.x_sym.x_tagndx.l = 0;
4580 else
4581 aux.x_sym.x_tagndx.l = symindx;
4586 /* Copy over the line numbers, unless we are stripping
4587 them. We do this on a symbol by symbol basis in
4588 order to more easily handle garbage collection. */
4589 if (CSECT_SYM_P (isymp->n_sclass)
4590 && i == 0
4591 && isymp->n_numaux > 1
4592 && ISFCN (isymp->n_type)
4593 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4595 if (*lineno_counts == 0)
4596 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4597 else
4599 asection *enclosing;
4600 unsigned int enc_count;
4601 bfd_signed_vma linoff;
4602 struct internal_lineno lin;
4603 bfd_byte *linp;
4604 bfd_byte *linpend;
4605 bfd_vma offset;
4606 file_ptr pos;
4607 bfd_size_type amt;
4609 /* Read in the enclosing section's line-number
4610 information, if we haven't already. */
4611 o = *csectpp;
4612 enclosing = xcoff_section_data (abfd, o)->enclosing;
4613 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4614 if (oline != enclosing)
4616 pos = enclosing->line_filepos;
4617 amt = linesz * enc_count;
4618 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4619 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4620 != amt))
4621 return FALSE;
4622 oline = enclosing;
4625 /* Copy across the first entry, adjusting its
4626 symbol index. */
4627 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4628 - enclosing->line_filepos);
4629 linp = flinfo->linenos + linoff;
4630 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4631 lin.l_addr.l_symndx = *indexp;
4632 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4634 /* Copy the other entries, adjusting their addresses. */
4635 linpend = linp + *lineno_counts * linesz;
4636 offset = (o->output_section->vma
4637 + o->output_offset
4638 - o->vma);
4639 for (linp += linesz; linp < linpend; linp += linesz)
4641 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4642 lin.l_addr.l_paddr += offset;
4643 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4646 /* Write out the entries we've just processed. */
4647 pos = (o->output_section->line_filepos
4648 + o->output_section->lineno_count * linesz);
4649 amt = linesz * *lineno_counts;
4650 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4651 || bfd_bwrite (flinfo->linenos + linoff,
4652 amt, output_bfd) != amt)
4653 return FALSE;
4654 o->output_section->lineno_count += *lineno_counts;
4656 /* Record the offset of the symbol's line numbers
4657 in the output file. */
4658 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4660 if (incls > 0)
4662 struct internal_syment *iisp, *iispend;
4663 long *iindp;
4664 bfd_byte *oos;
4665 bfd_vma range_start, range_end;
4666 int iiadd;
4668 /* Update any C_BINCL or C_EINCL symbols
4669 that refer to a line number in the
4670 range we just output. */
4671 iisp = flinfo->internal_syms;
4672 iispend = iisp + obj_raw_syment_count (input_bfd);
4673 iindp = flinfo->sym_indices;
4674 oos = flinfo->outsyms;
4675 range_start = enclosing->line_filepos + linoff;
4676 range_end = range_start + *lineno_counts * linesz;
4677 while (iisp < iispend)
4679 if (*iindp >= 0
4680 && (iisp->n_sclass == C_BINCL
4681 || iisp->n_sclass == C_EINCL)
4682 && iisp->n_value >= range_start
4683 && iisp->n_value < range_end)
4685 struct internal_syment iis;
4687 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4688 iis.n_value = (iisp->n_value
4689 - range_start
4690 + pos);
4691 bfd_coff_swap_sym_out (output_bfd,
4692 &iis, oos);
4693 --incls;
4696 iiadd = 1 + iisp->n_numaux;
4697 if (*iindp >= 0)
4698 oos += iiadd * osymesz;
4699 iisp += iiadd;
4700 iindp += iiadd;
4706 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4707 isymp->n_sclass, i, isymp->n_numaux,
4708 (void *) outsym);
4709 outsym += osymesz;
4710 esym += isymesz;
4714 sym_hash += add;
4715 indexp += add;
4716 isymp += add;
4717 csectpp += add;
4718 lineno_counts += add;
4719 debug_index += add;
4722 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4723 symbol will be the first symbol in the next input file. In the
4724 normal case, this will save us from writing out the C_FILE symbol
4725 again. */
4726 if (flinfo->last_file_index != -1
4727 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4729 flinfo->last_file.n_value = output_index;
4730 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4731 (void *) (flinfo->outsyms
4732 + ((flinfo->last_file_index - syment_base)
4733 * osymesz)));
4736 /* Write the modified symbols to the output file. */
4737 if (outsym > flinfo->outsyms)
4739 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4740 bfd_size_type amt = outsym - flinfo->outsyms;
4741 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4742 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4743 return FALSE;
4745 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4746 + (outsym - flinfo->outsyms) / osymesz)
4747 == output_index);
4749 obj_raw_syment_count (output_bfd) = output_index;
4752 /* Don't let the linker relocation routines discard the symbols. */
4753 keep_syms = obj_coff_keep_syms (input_bfd);
4754 obj_coff_keep_syms (input_bfd) = TRUE;
4756 /* Relocate the contents of each section. */
4757 for (o = input_bfd->sections; o != NULL; o = o->next)
4759 bfd_byte *contents;
4761 if (! o->linker_mark)
4762 /* This section was omitted from the link. */
4763 continue;
4765 if ((o->flags & SEC_HAS_CONTENTS) == 0
4766 || o->size == 0
4767 || (o->flags & SEC_IN_MEMORY) != 0)
4768 continue;
4770 /* We have set filepos correctly for the sections we created to
4771 represent csects, so bfd_get_section_contents should work. */
4772 if (coff_section_data (input_bfd, o) != NULL
4773 && coff_section_data (input_bfd, o)->contents != NULL)
4774 contents = coff_section_data (input_bfd, o)->contents;
4775 else
4777 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4778 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4779 return FALSE;
4780 contents = flinfo->contents;
4783 if ((o->flags & SEC_RELOC) != 0)
4785 int target_index;
4786 struct internal_reloc *internal_relocs;
4787 struct internal_reloc *irel;
4788 bfd_vma offset;
4789 struct internal_reloc *irelend;
4790 struct xcoff_link_hash_entry **rel_hash;
4791 long r_symndx;
4793 /* Read in the relocs. */
4794 target_index = o->output_section->target_index;
4795 internal_relocs = (xcoff_read_internal_relocs
4796 (input_bfd, o, FALSE, flinfo->external_relocs,
4797 TRUE,
4798 (flinfo->section_info[target_index].relocs
4799 + o->output_section->reloc_count)));
4800 if (internal_relocs == NULL)
4801 return FALSE;
4803 /* Call processor specific code to relocate the section
4804 contents. */
4805 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4806 input_bfd, o,
4807 contents,
4808 internal_relocs,
4809 flinfo->internal_syms,
4810 xcoff_data (input_bfd)->csects))
4811 return FALSE;
4813 offset = o->output_section->vma + o->output_offset - o->vma;
4814 irel = internal_relocs;
4815 irelend = irel + o->reloc_count;
4816 rel_hash = (flinfo->section_info[target_index].rel_hashes
4817 + o->output_section->reloc_count);
4818 for (; irel < irelend; irel++, rel_hash++)
4820 struct xcoff_link_hash_entry *h = NULL;
4822 *rel_hash = NULL;
4824 /* Adjust the reloc address and symbol index. */
4826 irel->r_vaddr += offset;
4828 r_symndx = irel->r_symndx;
4830 if (r_symndx == -1)
4831 h = NULL;
4832 else
4833 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4835 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4837 if (h != NULL
4838 && h->smclas != XMC_TD
4839 && (irel->r_type == R_TOC
4840 || irel->r_type == R_GL
4841 || irel->r_type == R_TCL
4842 || irel->r_type == R_TRL
4843 || irel->r_type == R_TRLA))
4845 /* This is a TOC relative reloc with a symbol
4846 attached. The symbol should be the one which
4847 this reloc is for. We want to make this
4848 reloc against the TOC address of the symbol,
4849 not the symbol itself. */
4850 BFD_ASSERT (h->toc_section != NULL);
4851 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4852 if (h->u.toc_indx != -1)
4853 irel->r_symndx = h->u.toc_indx;
4854 else
4856 struct xcoff_toc_rel_hash *n;
4857 struct xcoff_link_section_info *si;
4858 bfd_size_type amt;
4860 amt = sizeof (* n);
4861 n = bfd_alloc (flinfo->output_bfd, amt);
4862 if (n == NULL)
4863 return FALSE;
4864 si = flinfo->section_info + target_index;
4865 n->next = si->toc_rel_hashes;
4866 n->h = h;
4867 n->rel = irel;
4868 si->toc_rel_hashes = n;
4871 else if (h != NULL)
4873 /* This is a global symbol. */
4874 if (h->indx >= 0)
4875 irel->r_symndx = h->indx;
4876 else
4878 /* This symbol is being written at the end
4879 of the file, and we do not yet know the
4880 symbol index. We save the pointer to the
4881 hash table entry in the rel_hash list.
4882 We set the indx field to -2 to indicate
4883 that this symbol must not be stripped. */
4884 *rel_hash = h;
4885 h->indx = -2;
4888 else
4890 long indx;
4892 indx = flinfo->sym_indices[r_symndx];
4894 if (indx == -1)
4896 struct internal_syment *is;
4898 /* Relocations against a TC0 TOC anchor are
4899 automatically transformed to be against
4900 the TOC anchor in the output file. */
4901 is = flinfo->internal_syms + r_symndx;
4902 if (is->n_sclass == C_HIDEXT
4903 && is->n_numaux > 0)
4905 void * auxptr;
4906 union internal_auxent aux;
4908 auxptr = ((void *)
4909 (((bfd_byte *)
4910 obj_coff_external_syms (input_bfd))
4911 + ((r_symndx + is->n_numaux)
4912 * isymesz)));
4913 bfd_coff_swap_aux_in (input_bfd, auxptr,
4914 is->n_type, is->n_sclass,
4915 is->n_numaux - 1,
4916 is->n_numaux,
4917 (void *) &aux);
4918 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4919 && aux.x_csect.x_smclas == XMC_TC0)
4920 indx = flinfo->toc_symindx;
4924 if (indx != -1)
4925 irel->r_symndx = indx;
4926 else
4929 struct internal_syment *is;
4931 const char *name;
4932 char buf[SYMNMLEN + 1];
4934 /* This reloc is against a symbol we are
4935 stripping. It would be possible to handle
4936 this case, but I don't think it's worth it. */
4937 is = flinfo->internal_syms + r_symndx;
4939 name = (_bfd_coff_internal_syment_name
4940 (input_bfd, is, buf));
4942 if (name == NULL)
4943 return FALSE;
4945 if (! ((*flinfo->info->callbacks->unattached_reloc)
4946 (flinfo->info, name, input_bfd, o,
4947 irel->r_vaddr)))
4948 return FALSE;
4953 if (xcoff_need_ldrel_p (flinfo->info, irel, h))
4955 asection *sec;
4957 if (r_symndx == -1)
4958 sec = NULL;
4959 else if (h == NULL)
4960 sec = xcoff_data (input_bfd)->csects[r_symndx];
4961 else
4962 sec = xcoff_symbol_section (h);
4963 if (!xcoff_create_ldrel (output_bfd, flinfo,
4964 o->output_section, input_bfd,
4965 irel, sec, h))
4966 return FALSE;
4970 o->output_section->reloc_count += o->reloc_count;
4973 /* Write out the modified section contents. */
4974 if (! bfd_set_section_contents (output_bfd, o->output_section,
4975 contents, (file_ptr) o->output_offset,
4976 o->size))
4977 return FALSE;
4980 obj_coff_keep_syms (input_bfd) = keep_syms;
4982 if (! flinfo->info->keep_memory)
4984 if (! _bfd_coff_free_symbols (input_bfd))
4985 return FALSE;
4988 return TRUE;
4991 #undef N_TMASK
4992 #undef N_BTSHFT
4994 /* Sort relocs by VMA. This is called via qsort. */
4996 static int
4997 xcoff_sort_relocs (const void * p1, const void * p2)
4999 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5000 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5002 if (r1->r_vaddr > r2->r_vaddr)
5003 return 1;
5004 else if (r1->r_vaddr < r2->r_vaddr)
5005 return -1;
5006 else
5007 return 0;
5010 /* Return true if section SEC is a TOC section. */
5012 static inline bfd_boolean
5013 xcoff_toc_section_p (asection *sec)
5015 const char *name;
5017 name = sec->name;
5018 if (name[0] == '.' && name[1] == 't')
5020 if (name[2] == 'c')
5022 if (name[3] == '0' && name[4] == 0)
5023 return TRUE;
5024 if (name[3] == 0)
5025 return TRUE;
5027 if (name[2] == 'd' && name[3] == 0)
5028 return TRUE;
5030 return FALSE;
5033 /* See if the link requires a TOC (it usually does!). If so, find a
5034 good place to put the TOC anchor csect, and write out the associated
5035 symbol. */
5037 static bfd_boolean
5038 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5040 bfd_vma toc_start, toc_end, start, end, best_address;
5041 asection *sec;
5042 bfd *input_bfd;
5043 int section_index;
5044 struct internal_syment irsym;
5045 union internal_auxent iraux;
5046 file_ptr pos;
5047 size_t size;
5049 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5050 index of a csect at the beginning of the TOC. */
5051 toc_start = ~(bfd_vma) 0;
5052 toc_end = 0;
5053 section_index = -1;
5054 for (input_bfd = flinfo->info->input_bfds;
5055 input_bfd != NULL;
5056 input_bfd = input_bfd->link_next)
5057 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5058 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5060 start = sec->output_section->vma + sec->output_offset;
5061 if (toc_start > start)
5063 toc_start = start;
5064 section_index = sec->output_section->target_index;
5067 end = start + sec->size;
5068 if (toc_end < end)
5069 toc_end = end;
5072 /* There's no need for a TC0 symbol if we don't have a TOC. */
5073 if (toc_end < toc_start)
5075 xcoff_data (output_bfd)->toc = toc_start;
5076 return TRUE;
5079 if (toc_end - toc_start < 0x8000)
5080 /* Every TOC csect can be accessed from TOC_START. */
5081 best_address = toc_start;
5082 else
5084 /* Find the lowest TOC csect that is still within range of TOC_END. */
5085 best_address = toc_end;
5086 for (input_bfd = flinfo->info->input_bfds;
5087 input_bfd != NULL;
5088 input_bfd = input_bfd->link_next)
5089 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5090 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5092 start = sec->output_section->vma + sec->output_offset;
5093 if (start < best_address
5094 && start + 0x8000 >= toc_end)
5096 best_address = start;
5097 section_index = sec->output_section->target_index;
5101 /* Make sure that the start of the TOC is also within range. */
5102 if (best_address > toc_start + 0x8000)
5104 (*_bfd_error_handler)
5105 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5106 "when compiling"),
5107 (unsigned long) (toc_end - toc_start));
5108 bfd_set_error (bfd_error_file_too_big);
5109 return FALSE;
5113 /* Record the chosen TOC value. */
5114 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5115 xcoff_data (output_bfd)->toc = best_address;
5116 xcoff_data (output_bfd)->sntoc = section_index;
5118 /* Fill out the TC0 symbol. */
5119 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &irsym, "TOC"))
5120 return FALSE;
5121 irsym.n_value = best_address;
5122 irsym.n_scnum = section_index;
5123 irsym.n_sclass = C_HIDEXT;
5124 irsym.n_type = T_NULL;
5125 irsym.n_numaux = 1;
5126 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5128 /* Fill out the auxillary csect information. */
5129 memset (&iraux, 0, sizeof iraux);
5130 iraux.x_csect.x_smtyp = XTY_SD;
5131 iraux.x_csect.x_smclas = XMC_TC0;
5132 iraux.x_csect.x_scnlen.l = 0;
5133 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5134 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5136 /* Write the contents to the file. */
5137 pos = obj_sym_filepos (output_bfd);
5138 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5139 size = 2 * bfd_coff_symesz (output_bfd);
5140 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5141 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5142 return FALSE;
5143 obj_raw_syment_count (output_bfd) += 2;
5145 return TRUE;
5148 /* Write out a non-XCOFF global symbol. */
5150 static bfd_boolean
5151 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
5153 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5154 bfd *output_bfd;
5155 bfd_byte *outsym;
5156 struct internal_syment isym;
5157 union internal_auxent aux;
5158 bfd_boolean result;
5159 file_ptr pos;
5160 bfd_size_type amt;
5162 output_bfd = flinfo->output_bfd;
5163 outsym = flinfo->outsyms;
5165 if (h->root.type == bfd_link_hash_warning)
5167 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5168 if (h->root.type == bfd_link_hash_new)
5169 return TRUE;
5172 /* If this symbol was garbage collected, just skip it. */
5173 if (xcoff_hash_table (flinfo->info)->gc
5174 && (h->flags & XCOFF_MARK) == 0)
5175 return TRUE;
5177 /* If we need a .loader section entry, write it out. */
5178 if (h->ldsym != NULL)
5180 struct internal_ldsym *ldsym;
5181 bfd *impbfd;
5183 ldsym = h->ldsym;
5185 if (h->root.type == bfd_link_hash_undefined
5186 || h->root.type == bfd_link_hash_undefweak)
5189 ldsym->l_value = 0;
5190 ldsym->l_scnum = N_UNDEF;
5191 ldsym->l_smtype = XTY_ER;
5192 impbfd = h->root.u.undef.abfd;
5195 else if (h->root.type == bfd_link_hash_defined
5196 || h->root.type == bfd_link_hash_defweak)
5198 asection *sec;
5200 sec = h->root.u.def.section;
5201 ldsym->l_value = (sec->output_section->vma
5202 + sec->output_offset
5203 + h->root.u.def.value);
5204 ldsym->l_scnum = sec->output_section->target_index;
5205 ldsym->l_smtype = XTY_SD;
5206 impbfd = sec->owner;
5209 else
5210 abort ();
5212 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5213 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5214 || (h->flags & XCOFF_IMPORT) != 0)
5215 /* Clear l_smtype
5216 Import symbols are defined so the check above will make
5217 the l_smtype XTY_SD. But this is not correct, it should
5218 be cleared. */
5219 ldsym->l_smtype |= L_IMPORT;
5221 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5222 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5223 || (h->flags & XCOFF_EXPORT) != 0)
5224 ldsym->l_smtype |= L_EXPORT;
5226 if ((h->flags & XCOFF_ENTRY) != 0)
5227 ldsym->l_smtype |= L_ENTRY;
5229 if ((h->flags & XCOFF_RTINIT) != 0)
5230 ldsym->l_smtype = XTY_SD;
5232 ldsym->l_smclas = h->smclas;
5234 if (ldsym->l_smtype & L_IMPORT)
5236 if ((h->root.type == bfd_link_hash_defined
5237 || h->root.type == bfd_link_hash_defweak)
5238 && (h->root.u.def.value != 0))
5239 ldsym->l_smclas = XMC_XO;
5241 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5242 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5243 ldsym->l_smclas = XMC_SV3264;
5245 else if (h->flags & XCOFF_SYSCALL32)
5246 ldsym->l_smclas = XMC_SV;
5248 else if (h->flags & XCOFF_SYSCALL64)
5249 ldsym->l_smclas = XMC_SV64;
5252 if (ldsym->l_ifile == -(bfd_size_type) 1)
5254 ldsym->l_ifile = 0;
5256 else if (ldsym->l_ifile == 0)
5258 if ((ldsym->l_smtype & L_IMPORT) == 0)
5259 ldsym->l_ifile = 0;
5260 else if (impbfd == NULL)
5261 ldsym->l_ifile = 0;
5262 else
5264 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5265 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5269 ldsym->l_parm = 0;
5271 BFD_ASSERT (h->ldindx >= 0);
5273 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5274 (flinfo->ldsym +
5275 (h->ldindx - 3)
5276 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5277 h->ldsym = NULL;
5280 /* If this symbol needs global linkage code, write it out. */
5281 if (h->root.type == bfd_link_hash_defined
5282 && (h->root.u.def.section
5283 == xcoff_hash_table (flinfo->info)->linkage_section))
5285 bfd_byte *p;
5286 bfd_vma tocoff;
5287 unsigned int i;
5289 p = h->root.u.def.section->contents + h->root.u.def.value;
5291 /* The first instruction in the global linkage code loads a
5292 specific TOC element. */
5293 tocoff = (h->descriptor->toc_section->output_section->vma
5294 + h->descriptor->toc_section->output_offset
5295 - xcoff_data (output_bfd)->toc);
5297 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5298 tocoff += h->descriptor->u.toc_offset;
5300 /* The first instruction in the glink code needs to be
5301 cooked to to hold the correct offset in the toc. The
5302 rest are just output raw. */
5303 bfd_put_32 (output_bfd,
5304 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5306 /* Start with i == 1 to get past the first instruction done above
5307 The /4 is because the glink code is in bytes and we are going
5308 4 at a pop. */
5309 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5310 bfd_put_32 (output_bfd,
5311 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5312 &p[4 * i]);
5315 /* If we created a TOC entry for this symbol, write out the required
5316 relocs. */
5317 if ((h->flags & XCOFF_SET_TOC) != 0)
5319 asection *tocsec;
5320 asection *osec;
5321 int oindx;
5322 struct internal_reloc *irel;
5323 struct internal_syment irsym;
5324 union internal_auxent iraux;
5326 tocsec = h->toc_section;
5327 osec = tocsec->output_section;
5328 oindx = osec->target_index;
5329 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5330 irel->r_vaddr = (osec->vma
5331 + tocsec->output_offset
5332 + h->u.toc_offset);
5334 if (h->indx >= 0)
5335 irel->r_symndx = h->indx;
5336 else
5338 h->indx = -2;
5339 irel->r_symndx = obj_raw_syment_count (output_bfd);
5342 BFD_ASSERT (h->ldindx >= 0);
5344 /* Initialize the aux union here instead of closer to when it is
5345 written out below because the length of the csect depends on
5346 whether the output is 32 or 64 bit. */
5347 memset (&iraux, 0, sizeof iraux);
5348 iraux.x_csect.x_smtyp = XTY_SD;
5349 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5350 iraux.x_csect.x_smclas = XMC_TC;
5352 /* 32 bit uses a 32 bit R_POS to do the relocations
5353 64 bit uses a 64 bit R_POS to do the relocations
5355 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5357 Which one is determined by the backend. */
5358 if (bfd_xcoff_is_xcoff64 (output_bfd))
5360 irel->r_size = 63;
5361 iraux.x_csect.x_scnlen.l = 8;
5363 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5365 irel->r_size = 31;
5366 iraux.x_csect.x_scnlen.l = 4;
5368 else
5369 return FALSE;
5371 irel->r_type = R_POS;
5372 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5373 ++osec->reloc_count;
5375 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5376 output_bfd, irel, NULL, h))
5377 return FALSE;
5379 /* We need to emit a symbol to define a csect which holds
5380 the reloc. */
5381 if (flinfo->info->strip != strip_all)
5383 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab,
5384 &irsym, h->root.root.string);
5385 if (!result)
5386 return FALSE;
5388 irsym.n_value = irel->r_vaddr;
5389 irsym.n_scnum = osec->target_index;
5390 irsym.n_sclass = C_HIDEXT;
5391 irsym.n_type = T_NULL;
5392 irsym.n_numaux = 1;
5394 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5395 outsym += bfd_coff_symesz (output_bfd);
5397 /* Note : iraux is initialized above. */
5398 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5399 0, 1, (void *) outsym);
5400 outsym += bfd_coff_auxesz (output_bfd);
5402 if (h->indx >= 0)
5404 /* We aren't going to write out the symbols below, so we
5405 need to write them out now. */
5406 pos = obj_sym_filepos (output_bfd);
5407 pos += (obj_raw_syment_count (output_bfd)
5408 * bfd_coff_symesz (output_bfd));
5409 amt = outsym - flinfo->outsyms;
5410 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5411 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5412 return FALSE;
5413 obj_raw_syment_count (output_bfd) +=
5414 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5416 outsym = flinfo->outsyms;
5421 /* If this symbol is a specially defined function descriptor, write
5422 it out. The first word is the address of the function code
5423 itself, the second word is the address of the TOC, and the third
5424 word is zero.
5426 32 bit vs 64 bit
5427 The addresses for the 32 bit will take 4 bytes and the addresses
5428 for 64 bit will take 8 bytes. Similar for the relocs. This type
5429 of logic was also done above to create a TOC entry in
5430 xcoff_write_global_symbol. */
5431 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5432 && h->root.type == bfd_link_hash_defined
5433 && (h->root.u.def.section
5434 == xcoff_hash_table (flinfo->info)->descriptor_section))
5436 asection *sec;
5437 asection *osec;
5438 int oindx;
5439 bfd_byte *p;
5440 struct xcoff_link_hash_entry *hentry;
5441 asection *esec;
5442 struct internal_reloc *irel;
5443 asection *tsec;
5444 unsigned int reloc_size, byte_size;
5446 if (bfd_xcoff_is_xcoff64 (output_bfd))
5448 reloc_size = 63;
5449 byte_size = 8;
5451 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5453 reloc_size = 31;
5454 byte_size = 4;
5456 else
5457 return FALSE;
5459 sec = h->root.u.def.section;
5460 osec = sec->output_section;
5461 oindx = osec->target_index;
5462 p = sec->contents + h->root.u.def.value;
5464 hentry = h->descriptor;
5465 BFD_ASSERT (hentry != NULL
5466 && (hentry->root.type == bfd_link_hash_defined
5467 || hentry->root.type == bfd_link_hash_defweak));
5468 esec = hentry->root.u.def.section;
5470 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5471 irel->r_vaddr = (osec->vma
5472 + sec->output_offset
5473 + h->root.u.def.value);
5474 irel->r_symndx = esec->output_section->target_index;
5475 irel->r_type = R_POS;
5476 irel->r_size = reloc_size;
5477 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5478 ++osec->reloc_count;
5480 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5481 output_bfd, irel, esec, NULL))
5482 return FALSE;
5484 /* There are three items to write out,
5485 the address of the code
5486 the address of the toc anchor
5487 the environment pointer.
5488 We are ignoring the environment pointer. So set it to zero. */
5489 if (bfd_xcoff_is_xcoff64 (output_bfd))
5491 bfd_put_64 (output_bfd,
5492 (esec->output_section->vma + esec->output_offset
5493 + hentry->root.u.def.value),
5495 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5496 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5498 else
5500 /* 32 bit backend
5501 This logic was already called above so the error case where
5502 the backend is neither has already been checked. */
5503 bfd_put_32 (output_bfd,
5504 (esec->output_section->vma + esec->output_offset
5505 + hentry->root.u.def.value),
5507 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5508 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5511 tsec = coff_section_from_bfd_index (output_bfd,
5512 xcoff_data (output_bfd)->sntoc);
5514 ++irel;
5515 irel->r_vaddr = (osec->vma
5516 + sec->output_offset
5517 + h->root.u.def.value
5518 + byte_size);
5519 irel->r_symndx = tsec->output_section->target_index;
5520 irel->r_type = R_POS;
5521 irel->r_size = reloc_size;
5522 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5523 ++osec->reloc_count;
5525 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5526 output_bfd, irel, tsec, NULL))
5527 return FALSE;
5530 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5532 BFD_ASSERT (outsym == flinfo->outsyms);
5533 return TRUE;
5536 if (h->indx != -2
5537 && (flinfo->info->strip == strip_all
5538 || (flinfo->info->strip == strip_some
5539 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5540 FALSE, FALSE) == NULL)))
5542 BFD_ASSERT (outsym == flinfo->outsyms);
5543 return TRUE;
5546 if (h->indx != -2
5547 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5549 BFD_ASSERT (outsym == flinfo->outsyms);
5550 return TRUE;
5553 memset (&aux, 0, sizeof aux);
5555 h->indx = obj_raw_syment_count (output_bfd);
5557 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &isym,
5558 h->root.root.string);
5559 if (!result)
5560 return FALSE;
5562 if (h->root.type == bfd_link_hash_undefined
5563 || h->root.type == bfd_link_hash_undefweak)
5565 isym.n_value = 0;
5566 isym.n_scnum = N_UNDEF;
5567 if (h->root.type == bfd_link_hash_undefweak
5568 && C_WEAKEXT == C_AIX_WEAKEXT)
5569 isym.n_sclass = C_WEAKEXT;
5570 else
5571 isym.n_sclass = C_EXT;
5572 aux.x_csect.x_smtyp = XTY_ER;
5574 else if ((h->root.type == bfd_link_hash_defined
5575 || h->root.type == bfd_link_hash_defweak)
5576 && h->smclas == XMC_XO)
5578 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5579 isym.n_value = h->root.u.def.value;
5580 isym.n_scnum = N_UNDEF;
5581 if (h->root.type == bfd_link_hash_undefweak
5582 && C_WEAKEXT == C_AIX_WEAKEXT)
5583 isym.n_sclass = C_WEAKEXT;
5584 else
5585 isym.n_sclass = C_EXT;
5586 aux.x_csect.x_smtyp = XTY_ER;
5588 else if (h->root.type == bfd_link_hash_defined
5589 || h->root.type == bfd_link_hash_defweak)
5591 struct xcoff_link_size_list *l;
5593 isym.n_value = (h->root.u.def.section->output_section->vma
5594 + h->root.u.def.section->output_offset
5595 + h->root.u.def.value);
5596 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5597 isym.n_scnum = N_ABS;
5598 else
5599 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5600 isym.n_sclass = C_HIDEXT;
5601 aux.x_csect.x_smtyp = XTY_SD;
5603 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5605 for (l = xcoff_hash_table (flinfo->info)->size_list;
5606 l != NULL;
5607 l = l->next)
5609 if (l->h == h)
5611 aux.x_csect.x_scnlen.l = l->size;
5612 break;
5617 else if (h->root.type == bfd_link_hash_common)
5619 isym.n_value = (h->root.u.c.p->section->output_section->vma
5620 + h->root.u.c.p->section->output_offset);
5621 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5622 isym.n_sclass = C_EXT;
5623 aux.x_csect.x_smtyp = XTY_CM;
5624 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5626 else
5627 abort ();
5629 isym.n_type = T_NULL;
5630 isym.n_numaux = 1;
5632 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5633 outsym += bfd_coff_symesz (output_bfd);
5635 aux.x_csect.x_smclas = h->smclas;
5636 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5637 (void *) outsym);
5638 outsym += bfd_coff_auxesz (output_bfd);
5640 if ((h->root.type == bfd_link_hash_defined
5641 || h->root.type == bfd_link_hash_defweak)
5642 && h->smclas != XMC_XO)
5644 /* We just output an SD symbol. Now output an LD symbol. */
5645 h->indx += 2;
5647 if (h->root.type == bfd_link_hash_undefweak
5648 && C_WEAKEXT == C_AIX_WEAKEXT)
5649 isym.n_sclass = C_WEAKEXT;
5650 else
5651 isym.n_sclass = C_EXT;
5652 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5653 outsym += bfd_coff_symesz (output_bfd);
5655 aux.x_csect.x_smtyp = XTY_LD;
5656 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5657 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5658 (void *) outsym);
5659 outsym += bfd_coff_auxesz (output_bfd);
5662 pos = obj_sym_filepos (output_bfd);
5663 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5664 amt = outsym - flinfo->outsyms;
5665 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5666 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5667 return FALSE;
5668 obj_raw_syment_count (output_bfd) +=
5669 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5671 return TRUE;
5674 /* Handle a link order which is supposed to generate a reloc. */
5676 static bfd_boolean
5677 xcoff_reloc_link_order (bfd *output_bfd,
5678 struct xcoff_final_link_info *flinfo,
5679 asection *output_section,
5680 struct bfd_link_order *link_order)
5682 reloc_howto_type *howto;
5683 struct xcoff_link_hash_entry *h;
5684 asection *hsec;
5685 bfd_vma hval;
5686 bfd_vma addend;
5687 struct internal_reloc *irel;
5688 struct xcoff_link_hash_entry **rel_hash_ptr;
5690 if (link_order->type == bfd_section_reloc_link_order)
5691 /* We need to somehow locate a symbol in the right section. The
5692 symbol must either have a value of zero, or we must adjust
5693 the addend by the value of the symbol. FIXME: Write this
5694 when we need it. The old linker couldn't handle this anyhow. */
5695 abort ();
5697 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5698 if (howto == NULL)
5700 bfd_set_error (bfd_error_bad_value);
5701 return FALSE;
5704 h = ((struct xcoff_link_hash_entry *)
5705 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5706 link_order->u.reloc.p->u.name,
5707 FALSE, FALSE, TRUE));
5708 if (h == NULL)
5710 if (! ((*flinfo->info->callbacks->unattached_reloc)
5711 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5712 return FALSE;
5713 return TRUE;
5716 hsec = xcoff_symbol_section (h);
5717 if (h->root.type == bfd_link_hash_defined
5718 || h->root.type == bfd_link_hash_defweak)
5719 hval = h->root.u.def.value;
5720 else
5721 hval = 0;
5723 addend = link_order->u.reloc.p->addend;
5724 if (hsec != NULL)
5725 addend += (hsec->output_section->vma
5726 + hsec->output_offset
5727 + hval);
5729 if (addend != 0)
5731 bfd_size_type size;
5732 bfd_byte *buf;
5733 bfd_reloc_status_type rstat;
5734 bfd_boolean ok;
5736 size = bfd_get_reloc_size (howto);
5737 buf = bfd_zmalloc (size);
5738 if (buf == NULL)
5739 return FALSE;
5741 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5742 switch (rstat)
5744 case bfd_reloc_ok:
5745 break;
5746 default:
5747 case bfd_reloc_outofrange:
5748 abort ();
5749 case bfd_reloc_overflow:
5750 if (! ((*flinfo->info->callbacks->reloc_overflow)
5751 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5752 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5754 free (buf);
5755 return FALSE;
5757 break;
5759 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5760 (file_ptr) link_order->offset, size);
5761 free (buf);
5762 if (! ok)
5763 return FALSE;
5766 /* Store the reloc information in the right place. It will get
5767 swapped and written out at the end of the final_link routine. */
5768 irel = (flinfo->section_info[output_section->target_index].relocs
5769 + output_section->reloc_count);
5770 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5771 + output_section->reloc_count);
5773 memset (irel, 0, sizeof (struct internal_reloc));
5774 *rel_hash_ptr = NULL;
5776 irel->r_vaddr = output_section->vma + link_order->offset;
5778 if (h->indx >= 0)
5779 irel->r_symndx = h->indx;
5780 else
5782 /* Set the index to -2 to force this symbol to get written out. */
5783 h->indx = -2;
5784 *rel_hash_ptr = h;
5785 irel->r_symndx = 0;
5788 irel->r_type = howto->type;
5789 irel->r_size = howto->bitsize - 1;
5790 if (howto->complain_on_overflow == complain_overflow_signed)
5791 irel->r_size |= 0x80;
5793 ++output_section->reloc_count;
5795 /* Now output the reloc to the .loader section. */
5796 if (xcoff_hash_table (flinfo->info)->loader_section)
5798 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5799 output_bfd, irel, hsec, h))
5800 return FALSE;
5803 return TRUE;
5806 /* Do the final link step. */
5808 bfd_boolean
5809 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5811 bfd_size_type symesz;
5812 struct xcoff_final_link_info flinfo;
5813 asection *o;
5814 struct bfd_link_order *p;
5815 bfd_size_type max_contents_size;
5816 bfd_size_type max_sym_count;
5817 bfd_size_type max_lineno_count;
5818 bfd_size_type max_reloc_count;
5819 bfd_size_type max_output_reloc_count;
5820 file_ptr rel_filepos;
5821 unsigned int relsz;
5822 file_ptr line_filepos;
5823 unsigned int linesz;
5824 bfd *sub;
5825 bfd_byte *external_relocs = NULL;
5826 char strbuf[STRING_SIZE_SIZE];
5827 file_ptr pos;
5828 bfd_size_type amt;
5830 if (info->shared)
5831 abfd->flags |= DYNAMIC;
5833 symesz = bfd_coff_symesz (abfd);
5835 flinfo.info = info;
5836 flinfo.output_bfd = abfd;
5837 flinfo.strtab = NULL;
5838 flinfo.section_info = NULL;
5839 flinfo.last_file_index = -1;
5840 flinfo.toc_symindx = -1;
5841 flinfo.internal_syms = NULL;
5842 flinfo.sym_indices = NULL;
5843 flinfo.outsyms = NULL;
5844 flinfo.linenos = NULL;
5845 flinfo.contents = NULL;
5846 flinfo.external_relocs = NULL;
5848 if (xcoff_hash_table (info)->loader_section)
5850 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5851 + bfd_xcoff_ldhdrsz (abfd));
5852 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5853 + bfd_xcoff_ldhdrsz (abfd)
5854 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5855 * bfd_xcoff_ldsymsz (abfd)));
5857 else
5859 flinfo.ldsym = NULL;
5860 flinfo.ldrel = NULL;
5863 xcoff_data (abfd)->coff.link_info = info;
5865 flinfo.strtab = _bfd_stringtab_init ();
5866 if (flinfo.strtab == NULL)
5867 goto error_return;
5869 /* Count the relocation entries required for the output file.
5870 (We've already counted the line numbers.) Determine a few
5871 maximum sizes. */
5872 max_contents_size = 0;
5873 max_lineno_count = 0;
5874 max_reloc_count = 0;
5875 for (o = abfd->sections; o != NULL; o = o->next)
5877 o->reloc_count = 0;
5878 for (p = o->map_head.link_order; p != NULL; p = p->next)
5880 if (p->type == bfd_indirect_link_order)
5882 asection *sec;
5884 sec = p->u.indirect.section;
5886 /* Mark all sections which are to be included in the
5887 link. This will normally be every section. We need
5888 to do this so that we can identify any sections which
5889 the linker has decided to not include. */
5890 sec->linker_mark = TRUE;
5892 o->reloc_count += sec->reloc_count;
5894 if ((sec->flags & SEC_IN_MEMORY) == 0)
5896 if (sec->rawsize > max_contents_size)
5897 max_contents_size = sec->rawsize;
5898 if (sec->size > max_contents_size)
5899 max_contents_size = sec->size;
5901 if (coff_section_data (sec->owner, sec) != NULL
5902 && xcoff_section_data (sec->owner, sec) != NULL
5903 && (xcoff_section_data (sec->owner, sec)->lineno_count
5904 > max_lineno_count))
5905 max_lineno_count =
5906 xcoff_section_data (sec->owner, sec)->lineno_count;
5907 if (sec->reloc_count > max_reloc_count)
5908 max_reloc_count = sec->reloc_count;
5910 else if (p->type == bfd_section_reloc_link_order
5911 || p->type == bfd_symbol_reloc_link_order)
5912 ++o->reloc_count;
5916 /* Compute the file positions for all the sections. */
5917 if (abfd->output_has_begun)
5919 if (xcoff_hash_table (info)->file_align != 0)
5920 abort ();
5922 else
5924 bfd_vma file_align;
5926 file_align = xcoff_hash_table (info)->file_align;
5927 if (file_align != 0)
5929 bfd_boolean saw_contents;
5930 int indx;
5931 file_ptr sofar;
5933 /* Insert .pad sections before every section which has
5934 contents and is loaded, if it is preceded by some other
5935 section which has contents and is loaded. */
5936 saw_contents = TRUE;
5937 for (o = abfd->sections; o != NULL; o = o->next)
5939 if (strcmp (o->name, ".pad") == 0)
5940 saw_contents = FALSE;
5941 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5942 && (o->flags & SEC_LOAD) != 0)
5944 if (! saw_contents)
5945 saw_contents = TRUE;
5946 else
5948 asection *n;
5950 /* Create a pad section and place it before the section
5951 that needs padding. This requires unlinking and
5952 relinking the bfd's section list. */
5954 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5955 SEC_HAS_CONTENTS);
5956 n->alignment_power = 0;
5958 bfd_section_list_remove (abfd, n);
5959 bfd_section_list_insert_before (abfd, o, n);
5960 saw_contents = FALSE;
5965 /* Reset the section indices after inserting the new
5966 sections. */
5967 indx = 0;
5968 for (o = abfd->sections; o != NULL; o = o->next)
5970 ++indx;
5971 o->target_index = indx;
5973 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5975 /* Work out appropriate sizes for the .pad sections to force
5976 each section to land on a page boundary. This bit of
5977 code knows what compute_section_file_positions is going
5978 to do. */
5979 sofar = bfd_coff_filhsz (abfd);
5980 sofar += bfd_coff_aoutsz (abfd);
5981 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5982 for (o = abfd->sections; o != NULL; o = o->next)
5983 if ((bfd_xcoff_is_reloc_count_overflow
5984 (abfd, (bfd_vma) o->reloc_count))
5985 || (bfd_xcoff_is_lineno_count_overflow
5986 (abfd, (bfd_vma) o->lineno_count)))
5987 /* 64 does not overflow, need to check if 32 does */
5988 sofar += bfd_coff_scnhsz (abfd);
5990 for (o = abfd->sections; o != NULL; o = o->next)
5992 if (strcmp (o->name, ".pad") == 0)
5994 bfd_vma pageoff;
5996 BFD_ASSERT (o->size == 0);
5997 pageoff = sofar & (file_align - 1);
5998 if (pageoff != 0)
6000 o->size = file_align - pageoff;
6001 sofar += file_align - pageoff;
6002 o->flags |= SEC_HAS_CONTENTS;
6005 else
6007 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6008 sofar += BFD_ALIGN (o->size,
6009 1 << o->alignment_power);
6014 if (! bfd_coff_compute_section_file_positions (abfd))
6015 goto error_return;
6018 /* Allocate space for the pointers we need to keep for the relocs. */
6020 unsigned int i;
6022 /* We use section_count + 1, rather than section_count, because
6023 the target_index fields are 1 based. */
6024 amt = abfd->section_count + 1;
6025 amt *= sizeof (struct xcoff_link_section_info);
6026 flinfo.section_info = bfd_malloc (amt);
6027 if (flinfo.section_info == NULL)
6028 goto error_return;
6029 for (i = 0; i <= abfd->section_count; i++)
6031 flinfo.section_info[i].relocs = NULL;
6032 flinfo.section_info[i].rel_hashes = NULL;
6033 flinfo.section_info[i].toc_rel_hashes = NULL;
6037 /* Set the file positions for the relocs. */
6038 rel_filepos = obj_relocbase (abfd);
6039 relsz = bfd_coff_relsz (abfd);
6040 max_output_reloc_count = 0;
6041 for (o = abfd->sections; o != NULL; o = o->next)
6043 if (o->reloc_count == 0)
6044 o->rel_filepos = 0;
6045 else
6047 /* A stripped file has no relocs. However, we still
6048 allocate the buffers, so that later code doesn't have to
6049 worry about whether we are stripping or not. */
6050 if (info->strip == strip_all)
6051 o->rel_filepos = 0;
6052 else
6054 o->flags |= SEC_RELOC;
6055 o->rel_filepos = rel_filepos;
6056 rel_filepos += o->reloc_count * relsz;
6059 /* We don't know the indices of global symbols until we have
6060 written out all the local symbols. For each section in
6061 the output file, we keep an array of pointers to hash
6062 table entries. Each entry in the array corresponds to a
6063 reloc. When we find a reloc against a global symbol, we
6064 set the corresponding entry in this array so that we can
6065 fix up the symbol index after we have written out all the
6066 local symbols.
6068 Because of this problem, we also keep the relocs in
6069 memory until the end of the link. This wastes memory.
6070 We could backpatch the file later, I suppose, although it
6071 would be slow. */
6072 amt = o->reloc_count;
6073 amt *= sizeof (struct internal_reloc);
6074 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6076 amt = o->reloc_count;
6077 amt *= sizeof (struct xcoff_link_hash_entry *);
6078 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6080 if (flinfo.section_info[o->target_index].relocs == NULL
6081 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6082 goto error_return;
6084 if (o->reloc_count > max_output_reloc_count)
6085 max_output_reloc_count = o->reloc_count;
6089 /* We now know the size of the relocs, so we can determine the file
6090 positions of the line numbers. */
6091 line_filepos = rel_filepos;
6092 flinfo.line_filepos = line_filepos;
6093 linesz = bfd_coff_linesz (abfd);
6094 for (o = abfd->sections; o != NULL; o = o->next)
6096 if (o->lineno_count == 0)
6097 o->line_filepos = 0;
6098 else
6100 o->line_filepos = line_filepos;
6101 line_filepos += o->lineno_count * linesz;
6104 /* Reset the reloc and lineno counts, so that we can use them to
6105 count the number of entries we have output so far. */
6106 o->reloc_count = 0;
6107 o->lineno_count = 0;
6110 obj_sym_filepos (abfd) = line_filepos;
6112 /* Figure out the largest number of symbols in an input BFD. Take
6113 the opportunity to clear the output_has_begun fields of all the
6114 input BFD's. We want at least 6 symbols, since that is the
6115 number which xcoff_write_global_symbol may need. */
6116 max_sym_count = 6;
6117 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6119 bfd_size_type sz;
6121 sub->output_has_begun = FALSE;
6122 sz = obj_raw_syment_count (sub);
6123 if (sz > max_sym_count)
6124 max_sym_count = sz;
6127 /* Allocate some buffers used while linking. */
6128 amt = max_sym_count * sizeof (struct internal_syment);
6129 flinfo.internal_syms = bfd_malloc (amt);
6131 amt = max_sym_count * sizeof (long);
6132 flinfo.sym_indices = bfd_malloc (amt);
6134 amt = (max_sym_count + 1) * symesz;
6135 flinfo.outsyms = bfd_malloc (amt);
6137 amt = max_lineno_count * bfd_coff_linesz (abfd);
6138 flinfo.linenos = bfd_malloc (amt);
6140 amt = max_contents_size;
6141 flinfo.contents = bfd_malloc (amt);
6143 amt = max_reloc_count * relsz;
6144 flinfo.external_relocs = bfd_malloc (amt);
6146 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6147 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6148 || flinfo.outsyms == NULL
6149 || (flinfo.linenos == NULL && max_lineno_count > 0)
6150 || (flinfo.contents == NULL && max_contents_size > 0)
6151 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6152 goto error_return;
6154 obj_raw_syment_count (abfd) = 0;
6156 /* Find a TOC symbol, if we need one. */
6157 if (!xcoff_find_tc0 (abfd, &flinfo))
6158 goto error_return;
6160 /* We now know the position of everything in the file, except that
6161 we don't know the size of the symbol table and therefore we don't
6162 know where the string table starts. We just build the string
6163 table in memory as we go along. We process all the relocations
6164 for a single input file at once. */
6165 for (o = abfd->sections; o != NULL; o = o->next)
6167 for (p = o->map_head.link_order; p != NULL; p = p->next)
6169 if (p->type == bfd_indirect_link_order
6170 && p->u.indirect.section->owner->xvec == abfd->xvec)
6172 sub = p->u.indirect.section->owner;
6173 if (! sub->output_has_begun)
6175 if (! xcoff_link_input_bfd (&flinfo, sub))
6176 goto error_return;
6177 sub->output_has_begun = TRUE;
6180 else if (p->type == bfd_section_reloc_link_order
6181 || p->type == bfd_symbol_reloc_link_order)
6183 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6184 goto error_return;
6186 else
6188 if (! _bfd_default_link_order (abfd, info, o, p))
6189 goto error_return;
6194 /* Free up the buffers used by xcoff_link_input_bfd. */
6195 if (flinfo.internal_syms != NULL)
6197 free (flinfo.internal_syms);
6198 flinfo.internal_syms = NULL;
6200 if (flinfo.sym_indices != NULL)
6202 free (flinfo.sym_indices);
6203 flinfo.sym_indices = NULL;
6205 if (flinfo.linenos != NULL)
6207 free (flinfo.linenos);
6208 flinfo.linenos = NULL;
6210 if (flinfo.contents != NULL)
6212 free (flinfo.contents);
6213 flinfo.contents = NULL;
6215 if (flinfo.external_relocs != NULL)
6217 free (flinfo.external_relocs);
6218 flinfo.external_relocs = NULL;
6221 /* The value of the last C_FILE symbol is supposed to be -1. Write
6222 it out again. */
6223 if (flinfo.last_file_index != -1)
6225 flinfo.last_file.n_value = -(bfd_vma) 1;
6226 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6227 (void *) flinfo.outsyms);
6228 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6229 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6230 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6231 goto error_return;
6234 /* Write out all the global symbols which do not come from XCOFF
6235 input files. */
6236 xcoff_link_hash_traverse (xcoff_hash_table (info),
6237 xcoff_write_global_symbol,
6238 (void *) &flinfo);
6240 if (flinfo.outsyms != NULL)
6242 free (flinfo.outsyms);
6243 flinfo.outsyms = NULL;
6246 /* Now that we have written out all the global symbols, we know the
6247 symbol indices to use for relocs against them, and we can finally
6248 write out the relocs. */
6249 amt = max_output_reloc_count * relsz;
6250 external_relocs = bfd_malloc (amt);
6251 if (external_relocs == NULL && max_output_reloc_count != 0)
6252 goto error_return;
6254 for (o = abfd->sections; o != NULL; o = o->next)
6256 struct internal_reloc *irel;
6257 struct internal_reloc *irelend;
6258 struct xcoff_link_hash_entry **rel_hash;
6259 struct xcoff_toc_rel_hash *toc_rel_hash;
6260 bfd_byte *erel;
6261 bfd_size_type rel_size;
6263 /* A stripped file has no relocs. */
6264 if (info->strip == strip_all)
6266 o->reloc_count = 0;
6267 continue;
6270 if (o->reloc_count == 0)
6271 continue;
6273 irel = flinfo.section_info[o->target_index].relocs;
6274 irelend = irel + o->reloc_count;
6275 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6276 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6278 if (*rel_hash != NULL)
6280 if ((*rel_hash)->indx < 0)
6282 if (! ((*info->callbacks->unattached_reloc)
6283 (info, (*rel_hash)->root.root.string,
6284 NULL, o, irel->r_vaddr)))
6285 goto error_return;
6286 (*rel_hash)->indx = 0;
6288 irel->r_symndx = (*rel_hash)->indx;
6292 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6293 toc_rel_hash != NULL;
6294 toc_rel_hash = toc_rel_hash->next)
6296 if (toc_rel_hash->h->u.toc_indx < 0)
6298 if (! ((*info->callbacks->unattached_reloc)
6299 (info, toc_rel_hash->h->root.root.string,
6300 NULL, o, toc_rel_hash->rel->r_vaddr)))
6301 goto error_return;
6302 toc_rel_hash->h->u.toc_indx = 0;
6304 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6307 /* XCOFF requires that the relocs be sorted by address. We tend
6308 to produce them in the order in which their containing csects
6309 appear in the symbol table, which is not necessarily by
6310 address. So we sort them here. There may be a better way to
6311 do this. */
6312 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6313 o->reloc_count, sizeof (struct internal_reloc),
6314 xcoff_sort_relocs);
6316 irel = flinfo.section_info[o->target_index].relocs;
6317 irelend = irel + o->reloc_count;
6318 erel = external_relocs;
6319 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6320 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6322 rel_size = relsz * o->reloc_count;
6323 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6324 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6325 goto error_return;
6328 if (external_relocs != NULL)
6330 free (external_relocs);
6331 external_relocs = NULL;
6334 /* Free up the section information. */
6335 if (flinfo.section_info != NULL)
6337 unsigned int i;
6339 for (i = 0; i < abfd->section_count; i++)
6341 if (flinfo.section_info[i].relocs != NULL)
6342 free (flinfo.section_info[i].relocs);
6343 if (flinfo.section_info[i].rel_hashes != NULL)
6344 free (flinfo.section_info[i].rel_hashes);
6346 free (flinfo.section_info);
6347 flinfo.section_info = NULL;
6350 /* Write out the loader section contents. */
6351 o = xcoff_hash_table (info)->loader_section;
6352 if (o)
6354 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6355 == (xcoff_hash_table (info)->loader_section->contents
6356 + xcoff_hash_table (info)->ldhdr.l_impoff));
6357 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6358 (file_ptr) o->output_offset, o->size))
6359 goto error_return;
6362 /* Write out the magic sections. */
6363 o = xcoff_hash_table (info)->linkage_section;
6364 if (o->size > 0
6365 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6366 (file_ptr) o->output_offset,
6367 o->size))
6368 goto error_return;
6369 o = xcoff_hash_table (info)->toc_section;
6370 if (o->size > 0
6371 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6372 (file_ptr) o->output_offset,
6373 o->size))
6374 goto error_return;
6375 o = xcoff_hash_table (info)->descriptor_section;
6376 if (o->size > 0
6377 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6378 (file_ptr) o->output_offset,
6379 o->size))
6380 goto error_return;
6382 /* Write out the string table. */
6383 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6384 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6385 goto error_return;
6386 H_PUT_32 (abfd,
6387 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6388 strbuf);
6389 amt = STRING_SIZE_SIZE;
6390 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6391 goto error_return;
6392 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6393 goto error_return;
6395 _bfd_stringtab_free (flinfo.strtab);
6397 /* Write out the debugging string table. */
6398 o = xcoff_hash_table (info)->debug_section;
6399 if (o != NULL)
6401 struct bfd_strtab_hash *debug_strtab;
6403 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6404 BFD_ASSERT (o->output_section->size - o->output_offset
6405 >= _bfd_stringtab_size (debug_strtab));
6406 pos = o->output_section->filepos + o->output_offset;
6407 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6408 goto error_return;
6409 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6410 goto error_return;
6413 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6414 not try to write out the symbols. */
6415 bfd_get_symcount (abfd) = 0;
6417 return TRUE;
6419 error_return:
6420 if (flinfo.strtab != NULL)
6421 _bfd_stringtab_free (flinfo.strtab);
6423 if (flinfo.section_info != NULL)
6425 unsigned int i;
6427 for (i = 0; i < abfd->section_count; i++)
6429 if (flinfo.section_info[i].relocs != NULL)
6430 free (flinfo.section_info[i].relocs);
6431 if (flinfo.section_info[i].rel_hashes != NULL)
6432 free (flinfo.section_info[i].rel_hashes);
6434 free (flinfo.section_info);
6437 if (flinfo.internal_syms != NULL)
6438 free (flinfo.internal_syms);
6439 if (flinfo.sym_indices != NULL)
6440 free (flinfo.sym_indices);
6441 if (flinfo.outsyms != NULL)
6442 free (flinfo.outsyms);
6443 if (flinfo.linenos != NULL)
6444 free (flinfo.linenos);
6445 if (flinfo.contents != NULL)
6446 free (flinfo.contents);
6447 if (flinfo.external_relocs != NULL)
6448 free (flinfo.external_relocs);
6449 if (external_relocs != NULL)
6450 free (external_relocs);
6451 return FALSE;