* plugin.c (is_visible_from_outside): New function.
[binutils.git] / bfd / xcofflink.c
blobc055b260d6a5b4b0f012b74186dba9453ddcefae
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 (strcmp ((*pp)->path, imppath) == 0
762 && strcmp ((*pp)->file, impfile) == 0
763 && strcmp ((*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->add_archive_element)
2296 (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->add_archive_element)
2368 (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 *subsbfd = NULL;
2395 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2396 if (! _bfd_coff_get_external_symbols (abfd))
2397 return FALSE;
2399 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded, &subsbfd))
2400 return FALSE;
2402 if (*pneeded)
2404 /* Potentially, the add_archive_element hook may have set a
2405 substitute BFD for us. */
2406 if (subsbfd && !_bfd_coff_get_external_symbols (subsbfd))
2407 return FALSE;
2408 if (! xcoff_link_add_symbols (subsbfd ? subsbfd : abfd, info))
2409 return FALSE;
2410 if (info->keep_memory)
2411 keep_syms_p = TRUE;
2414 if (!keep_syms_p)
2416 if (! _bfd_coff_free_symbols (abfd))
2417 return FALSE;
2420 return TRUE;
2423 /* Given an XCOFF BFD, add symbols to the global hash table as
2424 appropriate. */
2426 bfd_boolean
2427 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2429 switch (bfd_get_format (abfd))
2431 case bfd_object:
2432 return xcoff_link_add_object_symbols (abfd, info);
2434 case bfd_archive:
2435 /* If the archive has a map, do the usual search. We then need
2436 to check the archive for dynamic objects, because they may not
2437 appear in the archive map even though they should, perhaps, be
2438 included. If the archive has no map, we just consider each object
2439 file in turn, since that apparently is what the AIX native linker
2440 does. */
2441 if (bfd_has_map (abfd))
2443 if (! (_bfd_generic_link_add_archive_symbols
2444 (abfd, info, xcoff_link_check_archive_element)))
2445 return FALSE;
2449 bfd *member;
2451 member = bfd_openr_next_archived_file (abfd, NULL);
2452 while (member != NULL)
2454 if (bfd_check_format (member, bfd_object)
2455 && (info->output_bfd->xvec == member->xvec)
2456 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2458 bfd_boolean needed;
2460 if (! xcoff_link_check_archive_element (member, info,
2461 &needed))
2462 return FALSE;
2463 if (needed)
2464 member->archive_pass = -1;
2466 member = bfd_openr_next_archived_file (abfd, member);
2470 return TRUE;
2472 default:
2473 bfd_set_error (bfd_error_wrong_format);
2474 return FALSE;
2478 bfd_boolean
2479 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2480 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2481 struct bfd_link_hash_entry *harg)
2483 struct xcoff_link_hash_entry *h;
2485 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2486 return FALSE;
2488 h = (struct xcoff_link_hash_entry *) harg;
2489 h->flags |= XCOFF_DEF_REGULAR;
2490 return TRUE;
2493 /* If symbol H has not been interpreted as a function descriptor,
2494 see whether it should be. Set up its descriptor information if so. */
2496 static bfd_boolean
2497 xcoff_find_function (struct bfd_link_info *info,
2498 struct xcoff_link_hash_entry *h)
2500 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2501 && h->root.root.string[0] != '.')
2503 char *fnname;
2504 struct xcoff_link_hash_entry *hfn;
2505 bfd_size_type amt;
2507 amt = strlen (h->root.root.string) + 2;
2508 fnname = bfd_malloc (amt);
2509 if (fnname == NULL)
2510 return FALSE;
2511 fnname[0] = '.';
2512 strcpy (fnname + 1, h->root.root.string);
2513 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2514 fnname, FALSE, FALSE, TRUE);
2515 free (fnname);
2516 if (hfn != NULL
2517 && hfn->smclas == XMC_PR
2518 && (hfn->root.type == bfd_link_hash_defined
2519 || hfn->root.type == bfd_link_hash_defweak))
2521 h->flags |= XCOFF_DESCRIPTOR;
2522 h->descriptor = hfn;
2523 hfn->descriptor = h;
2526 return TRUE;
2529 /* Return true if the given bfd contains at least one shared object. */
2531 static bfd_boolean
2532 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2533 bfd *archive)
2535 struct xcoff_archive_info *archive_info;
2536 bfd *member;
2538 archive_info = xcoff_get_archive_info (info, archive);
2539 if (!archive_info->know_contains_shared_object_p)
2541 member = bfd_openr_next_archived_file (archive, NULL);
2542 while (member != NULL && (member->flags & DYNAMIC) == 0)
2543 member = bfd_openr_next_archived_file (archive, member);
2545 archive_info->contains_shared_object_p = (member != NULL);
2546 archive_info->know_contains_shared_object_p = 1;
2548 return archive_info->contains_shared_object_p;
2551 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2552 qualifies for export by -bexpall. */
2554 static bfd_boolean
2555 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2557 /* Exclude symbols beginning with '_'. */
2558 if (h->root.root.string[0] == '_')
2559 return FALSE;
2561 /* Exclude archive members that would otherwise be unreferenced. */
2562 if ((h->flags & XCOFF_MARK) == 0
2563 && (h->root.type == bfd_link_hash_defined
2564 || h->root.type == bfd_link_hash_defweak)
2565 && h->root.u.def.section->owner != NULL
2566 && h->root.u.def.section->owner->my_archive != NULL)
2567 return FALSE;
2569 return TRUE;
2572 /* Return true if symbol H qualifies for the forms of automatic export
2573 specified by AUTO_EXPORT_FLAGS. */
2575 static bfd_boolean
2576 xcoff_auto_export_p (struct bfd_link_info *info,
2577 struct xcoff_link_hash_entry *h,
2578 unsigned int auto_export_flags)
2580 /* Don't automatically export things that were explicitly exported. */
2581 if ((h->flags & XCOFF_EXPORT) != 0)
2582 return FALSE;
2584 /* Don't export things that we don't define. */
2585 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2586 return FALSE;
2588 /* Don't export functions; export their descriptors instead. */
2589 if (h->root.root.string[0] == '.')
2590 return FALSE;
2592 /* We don't export a symbol which is being defined by an object
2593 included from an archive which contains a shared object. The
2594 rationale is that if an archive contains both an unshared and
2595 a shared object, then there must be some reason that the
2596 unshared object is unshared, and we don't want to start
2597 providing a shared version of it. In particular, this solves
2598 a bug involving the _savefNN set of functions. gcc will call
2599 those functions without providing a slot to restore the TOC,
2600 so it is essential that these functions be linked in directly
2601 and not from a shared object, which means that a shared
2602 object which also happens to link them in must not export
2603 them. This is confusing, but I haven't been able to think of
2604 a different approach. Note that the symbols can, of course,
2605 be exported explicitly. */
2606 if (h->root.type == bfd_link_hash_defined
2607 || h->root.type == bfd_link_hash_defweak)
2609 bfd *owner;
2611 owner = h->root.u.def.section->owner;
2612 if (owner != NULL
2613 && owner->my_archive != NULL
2614 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2615 return FALSE;
2618 /* Otherwise, all symbols are exported by -bexpfull. */
2619 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2620 return TRUE;
2622 /* Despite its name, -bexpall exports most but not all symbols. */
2623 if ((auto_export_flags & XCOFF_EXPALL) != 0
2624 && xcoff_covered_by_expall_p (h))
2625 return TRUE;
2627 return FALSE;
2630 /* Return true if relocation REL needs to be copied to the .loader section.
2631 If REL is against a global symbol, H is that symbol, otherwise it
2632 is null. */
2634 static bfd_boolean
2635 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2636 struct xcoff_link_hash_entry *h)
2638 if (!xcoff_hash_table (info)->loader_section)
2639 return FALSE;
2641 switch (rel->r_type)
2643 case R_TOC:
2644 case R_GL:
2645 case R_TCL:
2646 case R_TRL:
2647 case R_TRLA:
2648 /* We should never need a .loader reloc for a TOC-relative reloc. */
2649 return FALSE;
2651 default:
2652 /* In this case, relocations against defined symbols can be resolved
2653 statically. */
2654 if (h == NULL
2655 || h->root.type == bfd_link_hash_defined
2656 || h->root.type == bfd_link_hash_defweak
2657 || h->root.type == bfd_link_hash_common)
2658 return FALSE;
2660 /* We will always provide a local definition of function symbols,
2661 even if we don't have one yet. */
2662 if ((h->flags & XCOFF_CALLED) != 0)
2663 return FALSE;
2665 return TRUE;
2667 case R_POS:
2668 case R_NEG:
2669 case R_RL:
2670 case R_RLA:
2671 /* Absolute relocations against absolute symbols can be
2672 resolved statically. */
2673 if (h != NULL
2674 && (h->root.type == bfd_link_hash_defined
2675 || h->root.type == bfd_link_hash_defweak)
2676 && bfd_is_abs_section (h->root.u.def.section))
2677 return FALSE;
2679 return TRUE;
2683 /* Mark a symbol as not being garbage, including the section in which
2684 it is defined. */
2686 static inline bfd_boolean
2687 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2689 if ((h->flags & XCOFF_MARK) != 0)
2690 return TRUE;
2692 h->flags |= XCOFF_MARK;
2694 /* If we're marking an undefined symbol, try find some way of
2695 defining it. */
2696 if (!info->relocatable
2697 && (h->flags & XCOFF_IMPORT) == 0
2698 && (h->flags & XCOFF_DEF_REGULAR) == 0
2699 && (h->root.type == bfd_link_hash_undefined
2700 || h->root.type == bfd_link_hash_undefweak))
2702 /* First check whether this symbol can be interpreted as an
2703 undefined function descriptor for a defined function symbol. */
2704 if (!xcoff_find_function (info, h))
2705 return FALSE;
2707 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2708 && (h->descriptor->root.type == bfd_link_hash_defined
2709 || h->descriptor->root.type == bfd_link_hash_defweak))
2711 /* This is a descriptor for a defined symbol, but the input
2712 objects have not defined the descriptor itself. Fill in
2713 the definition automatically.
2715 Note that we do this even if we found a dynamic definition
2716 of H. The local function definition logically overrides
2717 the dynamic one. */
2718 asection *sec;
2720 sec = xcoff_hash_table (info)->descriptor_section;
2721 h->root.type = bfd_link_hash_defined;
2722 h->root.u.def.section = sec;
2723 h->root.u.def.value = sec->size;
2724 h->smclas = XMC_DS;
2725 h->flags |= XCOFF_DEF_REGULAR;
2727 /* The size of the function descriptor depends on whether this
2728 is xcoff32 (12) or xcoff64 (24). */
2729 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2731 /* A function descriptor uses two relocs: one for the
2732 associated code, and one for the TOC address. */
2733 xcoff_hash_table (info)->ldrel_count += 2;
2734 sec->reloc_count += 2;
2736 /* Mark the function itself. */
2737 if (!xcoff_mark_symbol (info, h->descriptor))
2738 return FALSE;
2740 /* Mark the TOC section, so that we get an anchor
2741 to relocate against. */
2742 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2743 return FALSE;
2745 /* We handle writing out the contents of the descriptor in
2746 xcoff_write_global_symbol. */
2748 else if (info->static_link)
2749 /* We can't get a symbol value dynamically, so just assume
2750 that it's undefined. */
2751 h->flags |= XCOFF_WAS_UNDEFINED;
2752 else if ((h->flags & XCOFF_CALLED) != 0)
2754 /* This is a function symbol for which we need to create
2755 linkage code. */
2756 asection *sec;
2757 struct xcoff_link_hash_entry *hds;
2759 /* Mark the descriptor (and its TOC section). */
2760 hds = h->descriptor;
2761 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2762 || hds->root.type == bfd_link_hash_undefweak)
2763 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2764 if (!xcoff_mark_symbol (info, hds))
2765 return FALSE;
2767 /* Treat this symbol as undefined if the descriptor was. */
2768 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2769 h->flags |= XCOFF_WAS_UNDEFINED;
2771 /* Allocate room for the global linkage code itself. */
2772 sec = xcoff_hash_table (info)->linkage_section;
2773 h->root.type = bfd_link_hash_defined;
2774 h->root.u.def.section = sec;
2775 h->root.u.def.value = sec->size;
2776 h->smclas = XMC_GL;
2777 h->flags |= XCOFF_DEF_REGULAR;
2778 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2780 /* The global linkage code requires a TOC entry for the
2781 descriptor. */
2782 if (hds->toc_section == NULL)
2784 int byte_size;
2786 /* 32 vs 64
2787 xcoff32 uses 4 bytes in the toc.
2788 xcoff64 uses 8 bytes in the toc. */
2789 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2790 byte_size = 8;
2791 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2792 byte_size = 4;
2793 else
2794 return FALSE;
2796 /* Allocate room in the fallback TOC section. */
2797 hds->toc_section = xcoff_hash_table (info)->toc_section;
2798 hds->u.toc_offset = hds->toc_section->size;
2799 hds->toc_section->size += byte_size;
2800 if (!xcoff_mark (info, hds->toc_section))
2801 return FALSE;
2803 /* Allocate room for a static and dynamic R_TOC
2804 relocation. */
2805 ++xcoff_hash_table (info)->ldrel_count;
2806 ++hds->toc_section->reloc_count;
2808 /* Set the index to -2 to force this symbol to
2809 get written out. */
2810 hds->indx = -2;
2811 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2814 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2816 /* Record that the symbol was undefined, then import it.
2817 -brtl links use a special fake import file. */
2818 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2819 if (xcoff_hash_table (info)->rtld)
2821 if (!xcoff_set_import_path (info, h, "", "..", ""))
2822 return FALSE;
2824 else
2826 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2827 return FALSE;
2832 if (h->root.type == bfd_link_hash_defined
2833 || h->root.type == bfd_link_hash_defweak)
2835 asection *hsec;
2837 hsec = h->root.u.def.section;
2838 if (! bfd_is_abs_section (hsec)
2839 && (hsec->flags & SEC_MARK) == 0)
2841 if (! xcoff_mark (info, hsec))
2842 return FALSE;
2846 if (h->toc_section != NULL
2847 && (h->toc_section->flags & SEC_MARK) == 0)
2849 if (! xcoff_mark (info, h->toc_section))
2850 return FALSE;
2853 return TRUE;
2856 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2857 If the symbol exists, set FLAGS. */
2859 static bfd_boolean
2860 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2861 const char *name, unsigned int flags)
2863 struct xcoff_link_hash_entry *h;
2865 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2866 FALSE, FALSE, TRUE);
2867 if (h != NULL)
2869 h->flags |= flags;
2870 if (h->root.type == bfd_link_hash_defined
2871 || h->root.type == bfd_link_hash_defweak)
2873 if (!xcoff_mark (info, h->root.u.def.section))
2874 return FALSE;
2877 return TRUE;
2880 /* The mark phase of garbage collection. For a given section, mark
2881 it, and all the sections which define symbols to which it refers.
2882 Because this function needs to look at the relocs, we also count
2883 the number of relocs which need to be copied into the .loader
2884 section. */
2886 static bfd_boolean
2887 xcoff_mark (struct bfd_link_info *info, asection *sec)
2889 if (bfd_is_abs_section (sec)
2890 || (sec->flags & SEC_MARK) != 0)
2891 return TRUE;
2893 sec->flags |= SEC_MARK;
2895 if (sec->owner->xvec == info->output_bfd->xvec
2896 && coff_section_data (sec->owner, sec) != NULL
2897 && xcoff_section_data (sec->owner, sec) != NULL)
2899 struct xcoff_link_hash_entry **syms;
2900 struct internal_reloc *rel, *relend;
2901 asection **csects;
2902 unsigned long i, first, last;
2904 /* Mark all the symbols in this section. */
2905 syms = obj_xcoff_sym_hashes (sec->owner);
2906 csects = xcoff_data (sec->owner)->csects;
2907 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2908 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2909 for (i = first; i <= last; i++)
2910 if (csects[i] == sec
2911 && syms[i] != NULL
2912 && (syms[i]->flags & XCOFF_MARK) == 0)
2914 if (!xcoff_mark_symbol (info, syms[i]))
2915 return FALSE;
2918 /* Look through the section relocs. */
2919 if ((sec->flags & SEC_RELOC) != 0
2920 && sec->reloc_count > 0)
2922 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2923 NULL, FALSE, NULL);
2924 if (rel == NULL)
2925 return FALSE;
2926 relend = rel + sec->reloc_count;
2927 for (; rel < relend; rel++)
2929 struct xcoff_link_hash_entry *h;
2931 if ((unsigned int) rel->r_symndx
2932 > obj_raw_syment_count (sec->owner))
2933 continue;
2935 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2936 if (h != NULL)
2938 if ((h->flags & XCOFF_MARK) == 0)
2940 if (!xcoff_mark_symbol (info, h))
2941 return FALSE;
2944 else
2946 asection *rsec;
2948 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2949 if (rsec != NULL
2950 && (rsec->flags & SEC_MARK) == 0)
2952 if (!xcoff_mark (info, rsec))
2953 return FALSE;
2957 /* See if this reloc needs to be copied into the .loader
2958 section. */
2959 if (xcoff_need_ldrel_p (info, rel, h))
2961 ++xcoff_hash_table (info)->ldrel_count;
2962 if (h != NULL)
2963 h->flags |= XCOFF_LDREL;
2967 if (! info->keep_memory
2968 && coff_section_data (sec->owner, sec) != NULL
2969 && coff_section_data (sec->owner, sec)->relocs != NULL
2970 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2972 free (coff_section_data (sec->owner, sec)->relocs);
2973 coff_section_data (sec->owner, sec)->relocs = NULL;
2978 return TRUE;
2981 /* Routines that are called after all the input files have been
2982 handled, but before the sections are laid out in memory. */
2984 /* The sweep phase of garbage collection. Remove all garbage
2985 sections. */
2987 static void
2988 xcoff_sweep (struct bfd_link_info *info)
2990 bfd *sub;
2992 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2994 asection *o;
2996 for (o = sub->sections; o != NULL; o = o->next)
2998 if ((o->flags & SEC_MARK) == 0)
3000 /* Keep all sections from non-XCOFF input files. Keep
3001 special sections. Keep .debug sections for the
3002 moment. */
3003 if (sub->xvec != info->output_bfd->xvec
3004 || o == xcoff_hash_table (info)->debug_section
3005 || o == xcoff_hash_table (info)->loader_section
3006 || o == xcoff_hash_table (info)->linkage_section
3007 || o == xcoff_hash_table (info)->descriptor_section
3008 || strcmp (o->name, ".debug") == 0)
3009 o->flags |= SEC_MARK;
3010 else
3012 o->size = 0;
3013 o->reloc_count = 0;
3020 /* Record the number of elements in a set. This is used to output the
3021 correct csect length. */
3023 bfd_boolean
3024 bfd_xcoff_link_record_set (bfd *output_bfd,
3025 struct bfd_link_info *info,
3026 struct bfd_link_hash_entry *harg,
3027 bfd_size_type size)
3029 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3030 struct xcoff_link_size_list *n;
3031 bfd_size_type amt;
3033 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3034 return TRUE;
3036 /* This will hardly ever be called. I don't want to burn four bytes
3037 per global symbol, so instead the size is kept on a linked list
3038 attached to the hash table. */
3039 amt = sizeof (* n);
3040 n = bfd_alloc (output_bfd, amt);
3041 if (n == NULL)
3042 return FALSE;
3043 n->next = xcoff_hash_table (info)->size_list;
3044 n->h = h;
3045 n->size = size;
3046 xcoff_hash_table (info)->size_list = n;
3048 h->flags |= XCOFF_HAS_SIZE;
3050 return TRUE;
3053 /* Import a symbol. */
3055 bfd_boolean
3056 bfd_xcoff_import_symbol (bfd *output_bfd,
3057 struct bfd_link_info *info,
3058 struct bfd_link_hash_entry *harg,
3059 bfd_vma val,
3060 const char *imppath,
3061 const char *impfile,
3062 const char *impmember,
3063 unsigned int syscall_flag)
3065 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3067 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3068 return TRUE;
3070 /* A symbol name which starts with a period is the code for a
3071 function. If the symbol is undefined, then add an undefined
3072 symbol for the function descriptor, and import that instead. */
3073 if (h->root.root.string[0] == '.'
3074 && h->root.type == bfd_link_hash_undefined
3075 && val == (bfd_vma) -1)
3077 struct xcoff_link_hash_entry *hds;
3079 hds = h->descriptor;
3080 if (hds == NULL)
3082 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3083 h->root.root.string + 1,
3084 TRUE, FALSE, TRUE);
3085 if (hds == NULL)
3086 return FALSE;
3087 if (hds->root.type == bfd_link_hash_new)
3089 hds->root.type = bfd_link_hash_undefined;
3090 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3092 hds->flags |= XCOFF_DESCRIPTOR;
3093 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3094 hds->descriptor = h;
3095 h->descriptor = hds;
3098 /* Now, if the descriptor is undefined, import the descriptor
3099 rather than the symbol we were told to import. FIXME: Is
3100 this correct in all cases? */
3101 if (hds->root.type == bfd_link_hash_undefined)
3102 h = hds;
3105 h->flags |= (XCOFF_IMPORT | syscall_flag);
3107 if (val != (bfd_vma) -1)
3109 if (h->root.type == bfd_link_hash_defined
3110 && (! bfd_is_abs_section (h->root.u.def.section)
3111 || h->root.u.def.value != val))
3113 if (! ((*info->callbacks->multiple_definition)
3114 (info, h->root.root.string, h->root.u.def.section->owner,
3115 h->root.u.def.section, h->root.u.def.value,
3116 output_bfd, bfd_abs_section_ptr, val)))
3117 return FALSE;
3120 h->root.type = bfd_link_hash_defined;
3121 h->root.u.def.section = bfd_abs_section_ptr;
3122 h->root.u.def.value = val;
3123 h->smclas = XMC_XO;
3126 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3127 return FALSE;
3129 return TRUE;
3132 /* Export a symbol. */
3134 bfd_boolean
3135 bfd_xcoff_export_symbol (bfd *output_bfd,
3136 struct bfd_link_info *info,
3137 struct bfd_link_hash_entry *harg)
3139 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3141 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3142 return TRUE;
3144 h->flags |= XCOFF_EXPORT;
3146 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3147 I'm just going to ignore it until somebody explains it. */
3149 /* Make sure we don't garbage collect this symbol. */
3150 if (! xcoff_mark_symbol (info, h))
3151 return FALSE;
3153 /* If this is a function descriptor, make sure we don't garbage
3154 collect the associated function code. We normally don't have to
3155 worry about this, because the descriptor will be attached to a
3156 section with relocs, but if we are creating the descriptor
3157 ourselves those relocs will not be visible to the mark code. */
3158 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3160 if (! xcoff_mark_symbol (info, h->descriptor))
3161 return FALSE;
3164 return TRUE;
3167 /* Count a reloc against a symbol. This is called for relocs
3168 generated by the linker script, typically for global constructors
3169 and destructors. */
3171 bfd_boolean
3172 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3173 struct bfd_link_info *info,
3174 const char *name)
3176 struct xcoff_link_hash_entry *h;
3178 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3179 return TRUE;
3181 h = ((struct xcoff_link_hash_entry *)
3182 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3183 FALSE));
3184 if (h == NULL)
3186 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3187 bfd_set_error (bfd_error_no_symbols);
3188 return FALSE;
3191 h->flags |= XCOFF_REF_REGULAR;
3192 if (xcoff_hash_table (info)->loader_section)
3194 h->flags |= XCOFF_LDREL;
3195 ++xcoff_hash_table (info)->ldrel_count;
3198 /* Mark the symbol to avoid garbage collection. */
3199 if (! xcoff_mark_symbol (info, h))
3200 return FALSE;
3202 return TRUE;
3205 /* This function is called for each symbol to which the linker script
3206 assigns a value. */
3208 bfd_boolean
3209 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3210 struct bfd_link_info *info,
3211 const char *name)
3213 struct xcoff_link_hash_entry *h;
3215 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3216 return TRUE;
3218 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3219 FALSE);
3220 if (h == NULL)
3221 return FALSE;
3223 h->flags |= XCOFF_DEF_REGULAR;
3225 return TRUE;
3228 /* An xcoff_link_hash_traverse callback for which DATA points to an
3229 xcoff_loader_info. Mark all symbols that should be automatically
3230 exported. */
3232 static bfd_boolean
3233 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3235 struct xcoff_loader_info *ldinfo;
3237 ldinfo = (struct xcoff_loader_info *) data;
3238 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3240 if (!xcoff_mark_symbol (ldinfo->info, h))
3241 ldinfo->failed = TRUE;
3243 return TRUE;
3246 /* Add a symbol to the .loader symbols, if necessary. */
3248 /* INPUT_BFD has an external symbol associated with hash table entry H
3249 and csect CSECT. Return true if INPUT_BFD defines H. */
3251 static bfd_boolean
3252 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3253 asection *csect)
3255 switch (h->root.type)
3257 case bfd_link_hash_defined:
3258 case bfd_link_hash_defweak:
3259 /* No input bfd owns absolute symbols. They are written by
3260 xcoff_write_global_symbol instead. */
3261 return (!bfd_is_abs_section (csect)
3262 && h->root.u.def.section == csect);
3264 case bfd_link_hash_common:
3265 return h->root.u.c.p->section->owner == input_bfd;
3267 case bfd_link_hash_undefined:
3268 case bfd_link_hash_undefweak:
3269 /* We can't treat undef.abfd as the owner because that bfd
3270 might be a dynamic object. Allow any bfd to claim it. */
3271 return TRUE;
3273 default:
3274 abort ();
3278 /* See if H should have a loader symbol associated with it. */
3280 static bfd_boolean
3281 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3282 struct xcoff_link_hash_entry *h)
3284 bfd_size_type amt;
3286 /* Warn if this symbol is exported but not defined. */
3287 if ((h->flags & XCOFF_EXPORT) != 0
3288 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3290 (*_bfd_error_handler)
3291 (_("warning: attempt to export undefined symbol `%s'"),
3292 h->root.root.string);
3293 return TRUE;
3296 /* We need to add a symbol to the .loader section if it is mentioned
3297 in a reloc which we are copying to the .loader section and it was
3298 not defined or common, or if it is the entry point, or if it is
3299 being exported. */
3300 if (((h->flags & XCOFF_LDREL) == 0
3301 || h->root.type == bfd_link_hash_defined
3302 || h->root.type == bfd_link_hash_defweak
3303 || h->root.type == bfd_link_hash_common)
3304 && (h->flags & XCOFF_ENTRY) == 0
3305 && (h->flags & XCOFF_EXPORT) == 0)
3306 return TRUE;
3308 /* We need to add this symbol to the .loader symbols. */
3310 BFD_ASSERT (h->ldsym == NULL);
3311 amt = sizeof (struct internal_ldsym);
3312 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3313 if (h->ldsym == NULL)
3315 ldinfo->failed = TRUE;
3316 return FALSE;
3319 if ((h->flags & XCOFF_IMPORT) != 0)
3321 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3322 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3323 h->smclas = XMC_DS;
3324 h->ldsym->l_ifile = h->ldindx;
3327 /* The first 3 symbol table indices are reserved to indicate the
3328 data, text and bss sections. */
3329 h->ldindx = ldinfo->ldsym_count + 3;
3331 ++ldinfo->ldsym_count;
3333 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3334 h->ldsym, h->root.root.string))
3335 return FALSE;
3337 h->flags |= XCOFF_BUILT_LDSYM;
3338 return TRUE;
3341 /* An xcoff_htab_traverse callback that is called for each symbol
3342 once garbage collection is complete. */
3344 static bfd_boolean
3345 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3347 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3349 if (h->root.type == bfd_link_hash_warning)
3350 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3352 /* __rtinit, this symbol has special handling. */
3353 if (h->flags & XCOFF_RTINIT)
3354 return TRUE;
3356 /* We don't want to garbage collect symbols which are not defined in
3357 XCOFF files. This is a convenient place to mark them. */
3358 if (xcoff_hash_table (ldinfo->info)->gc
3359 && (h->flags & XCOFF_MARK) == 0
3360 && (h->root.type == bfd_link_hash_defined
3361 || h->root.type == bfd_link_hash_defweak)
3362 && (h->root.u.def.section->owner == NULL
3363 || (h->root.u.def.section->owner->xvec
3364 != ldinfo->info->output_bfd->xvec)))
3365 h->flags |= XCOFF_MARK;
3367 /* Skip discarded symbols. */
3368 if (xcoff_hash_table (ldinfo->info)->gc
3369 && (h->flags & XCOFF_MARK) == 0)
3370 return TRUE;
3372 /* If this is still a common symbol, and it wasn't garbage
3373 collected, we need to actually allocate space for it in the .bss
3374 section. */
3375 if (h->root.type == bfd_link_hash_common
3376 && h->root.u.c.p->section->size == 0)
3378 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3379 h->root.u.c.p->section->size = h->root.u.c.size;
3382 if (xcoff_hash_table (ldinfo->info)->loader_section)
3384 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3385 h->flags |= XCOFF_EXPORT;
3387 if (!xcoff_build_ldsym (ldinfo, h))
3388 return FALSE;
3391 return TRUE;
3394 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3395 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3396 csect information, if any. NAME is the function's name if the name
3397 is stored in the .debug section, otherwise it is null.
3399 Return 1 if we should include an appropriately-adjusted ISYM
3400 in the output file, 0 if we should discard ISYM, or -1 if an
3401 error occured. */
3403 static int
3404 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3405 struct internal_syment *isym,
3406 union internal_auxent *aux,
3407 struct xcoff_link_hash_entry *h,
3408 asection *csect, const char *name)
3410 int smtyp;
3412 /* If we are skipping this csect, we want to strip the symbol too. */
3413 if (csect == NULL)
3414 return 0;
3416 /* Likewise if we garbage-collected the csect. */
3417 if (xcoff_hash_table (info)->gc
3418 && !bfd_is_abs_section (csect)
3419 && !bfd_is_und_section (csect)
3420 && (csect->flags & SEC_MARK) == 0)
3421 return 0;
3423 /* An XCOFF linker always removes C_STAT symbols. */
3424 if (isym->n_sclass == C_STAT)
3425 return 0;
3427 /* We generate the TOC anchor separately. */
3428 if (isym->n_sclass == C_HIDEXT
3429 && aux->x_csect.x_smclas == XMC_TC0)
3430 return 0;
3432 /* If we are stripping all symbols, we want to discard this one. */
3433 if (info->strip == strip_all)
3434 return 0;
3436 /* Discard symbols that are defined elsewhere. */
3437 if (EXTERN_SYM_P (isym->n_sclass))
3439 if ((h->flags & XCOFF_ALLOCATED) != 0)
3440 return 0;
3441 if (!xcoff_final_definition_p (input_bfd, h, csect))
3442 return 0;
3445 /* If we're discarding local symbols, check whether ISYM is local. */
3446 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3447 if (info->discard == discard_all
3448 && !EXTERN_SYM_P (isym->n_sclass)
3449 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3450 return 0;
3452 /* If we're stripping debugging symbols, check whether ISYM is one. */
3453 if (info->strip == strip_debugger
3454 && isym->n_scnum == N_DEBUG)
3455 return 0;
3457 /* If we are stripping symbols based on name, check how ISYM's
3458 name should be handled. */
3459 if (info->strip == strip_some
3460 || info->discard == discard_l)
3462 char buf[SYMNMLEN + 1];
3464 if (name == NULL)
3466 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3467 if (name == NULL)
3468 return -1;
3471 if (info->strip == strip_some
3472 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3473 return 0;
3475 if (info->discard == discard_l
3476 && !EXTERN_SYM_P (isym->n_sclass)
3477 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3478 && bfd_is_local_label_name (input_bfd, name))
3479 return 0;
3482 return 1;
3485 /* Lay out the .loader section, filling in the header and the import paths.
3486 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3488 static bfd_boolean
3489 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3490 const char *libpath)
3492 bfd *output_bfd;
3493 struct xcoff_link_hash_table *htab;
3494 struct internal_ldhdr *ldhdr;
3495 struct xcoff_import_file *fl;
3496 bfd_size_type stoff;
3497 size_t impsize, impcount;
3498 asection *lsec;
3499 char *out;
3501 /* Work out the size of the import file names. Each import file ID
3502 consists of three null terminated strings: the path, the file
3503 name, and the archive member name. The first entry in the list
3504 of names is the path to use to find objects, which the linker has
3505 passed in as the libpath argument. For some reason, the path
3506 entry in the other import file names appears to always be empty. */
3507 output_bfd = ldinfo->output_bfd;
3508 htab = xcoff_hash_table (ldinfo->info);
3509 impsize = strlen (libpath) + 3;
3510 impcount = 1;
3511 for (fl = htab->imports; fl != NULL; fl = fl->next)
3513 ++impcount;
3514 impsize += (strlen (fl->path)
3515 + strlen (fl->file)
3516 + strlen (fl->member)
3517 + 3);
3520 /* Set up the .loader section header. */
3521 ldhdr = &htab->ldhdr;
3522 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3523 ldhdr->l_nsyms = ldinfo->ldsym_count;
3524 ldhdr->l_nreloc = htab->ldrel_count;
3525 ldhdr->l_istlen = impsize;
3526 ldhdr->l_nimpid = impcount;
3527 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3528 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3529 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3530 ldhdr->l_stlen = ldinfo->string_size;
3531 stoff = ldhdr->l_impoff + impsize;
3532 if (ldinfo->string_size == 0)
3533 ldhdr->l_stoff = 0;
3534 else
3535 ldhdr->l_stoff = stoff;
3537 /* 64 bit elements to ldhdr
3538 The swap out routine for 32 bit will ignore them.
3539 Nothing fancy, symbols come after the header and relocs come
3540 after symbols. */
3541 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3542 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3543 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3545 /* We now know the final size of the .loader section. Allocate
3546 space for it. */
3547 lsec = htab->loader_section;
3548 lsec->size = stoff + ldhdr->l_stlen;
3549 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3550 if (lsec->contents == NULL)
3551 return FALSE;
3553 /* Set up the header. */
3554 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3556 /* Set up the import file names. */
3557 out = (char *) lsec->contents + ldhdr->l_impoff;
3558 strcpy (out, libpath);
3559 out += strlen (libpath) + 1;
3560 *out++ = '\0';
3561 *out++ = '\0';
3562 for (fl = htab->imports; fl != NULL; fl = fl->next)
3564 const char *s;
3566 s = fl->path;
3567 while ((*out++ = *s++) != '\0')
3569 s = fl->file;
3570 while ((*out++ = *s++) != '\0')
3572 s = fl->member;
3573 while ((*out++ = *s++) != '\0')
3577 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3579 /* Set up the symbol string table. */
3580 if (ldinfo->string_size > 0)
3582 memcpy (out, ldinfo->strings, ldinfo->string_size);
3583 free (ldinfo->strings);
3584 ldinfo->strings = NULL;
3587 /* We can't set up the symbol table or the relocs yet, because we
3588 don't yet know the final position of the various sections. The
3589 .loader symbols are written out when the corresponding normal
3590 symbols are written out in xcoff_link_input_bfd or
3591 xcoff_write_global_symbol. The .loader relocs are written out
3592 when the corresponding normal relocs are handled in
3593 xcoff_link_input_bfd. */
3595 return TRUE;
3598 /* Build the .loader section. This is called by the XCOFF linker
3599 emulation before_allocation routine. We must set the size of the
3600 .loader section before the linker lays out the output file.
3601 LIBPATH is the library path to search for shared objects; this is
3602 normally built from the -L arguments passed to the linker. ENTRY
3603 is the name of the entry point symbol (the -e linker option).
3604 FILE_ALIGN is the alignment to use for sections within the file
3605 (the -H linker option). MAXSTACK is the maximum stack size (the
3606 -bmaxstack linker option). MAXDATA is the maximum data size (the
3607 -bmaxdata linker option). GC is whether to do garbage collection
3608 (the -bgc linker option). MODTYPE is the module type (the
3609 -bmodtype linker option). TEXTRO is whether the text section must
3610 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3611 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3612 is set by this routine to csects with magic names like _end. */
3614 bfd_boolean
3615 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3616 struct bfd_link_info *info,
3617 const char *libpath,
3618 const char *entry,
3619 unsigned long file_align,
3620 unsigned long maxstack,
3621 unsigned long maxdata,
3622 bfd_boolean gc,
3623 int modtype,
3624 bfd_boolean textro,
3625 unsigned int auto_export_flags,
3626 asection **special_sections,
3627 bfd_boolean rtld)
3629 struct xcoff_loader_info ldinfo;
3630 int i;
3631 asection *sec;
3632 bfd *sub;
3633 struct bfd_strtab_hash *debug_strtab;
3634 bfd_byte *debug_contents = NULL;
3635 bfd_size_type amt;
3637 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3639 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3640 special_sections[i] = NULL;
3641 return TRUE;
3644 ldinfo.failed = FALSE;
3645 ldinfo.output_bfd = output_bfd;
3646 ldinfo.info = info;
3647 ldinfo.auto_export_flags = auto_export_flags;
3648 ldinfo.ldsym_count = 0;
3649 ldinfo.string_size = 0;
3650 ldinfo.strings = NULL;
3651 ldinfo.string_alc = 0;
3653 xcoff_data (output_bfd)->maxstack = maxstack;
3654 xcoff_data (output_bfd)->maxdata = maxdata;
3655 xcoff_data (output_bfd)->modtype = modtype;
3657 xcoff_hash_table (info)->file_align = file_align;
3658 xcoff_hash_table (info)->textro = textro;
3659 xcoff_hash_table (info)->rtld = rtld;
3661 /* __rtinit */
3662 if (xcoff_hash_table (info)->loader_section
3663 && (info->init_function || info->fini_function || rtld))
3665 struct xcoff_link_hash_entry *hsym;
3666 struct internal_ldsym *ldsym;
3668 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3669 "__rtinit", FALSE, FALSE, TRUE);
3670 if (hsym == NULL)
3672 (*_bfd_error_handler)
3673 (_("error: undefined symbol __rtinit"));
3674 return FALSE;
3677 xcoff_mark_symbol (info, hsym);
3678 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3680 /* __rtinit initialized. */
3681 amt = sizeof (* ldsym);
3682 ldsym = bfd_malloc (amt);
3684 ldsym->l_value = 0; /* Will be filled in later. */
3685 ldsym->l_scnum = 2; /* Data section. */
3686 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3687 ldsym->l_smclas = 5; /* .rw. */
3688 ldsym->l_ifile = 0; /* Special system loader symbol. */
3689 ldsym->l_parm = 0; /* NA. */
3691 /* Force __rtinit to be the first symbol in the loader symbol table
3692 See xcoff_build_ldsyms
3694 The first 3 symbol table indices are reserved to indicate the data,
3695 text and bss sections. */
3696 BFD_ASSERT (0 == ldinfo.ldsym_count);
3698 hsym->ldindx = 3;
3699 ldinfo.ldsym_count = 1;
3700 hsym->ldsym = ldsym;
3702 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3703 hsym->ldsym, hsym->root.root.string))
3704 return FALSE;
3706 /* This symbol is written out by xcoff_write_global_symbol
3707 Set stuff up so xcoff_write_global_symbol logic works. */
3708 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3709 hsym->root.type = bfd_link_hash_defined;
3710 hsym->root.u.def.value = 0;
3713 /* Garbage collect unused sections. */
3714 if (info->relocatable || !gc)
3716 gc = FALSE;
3717 xcoff_hash_table (info)->gc = FALSE;
3719 /* We still need to call xcoff_mark, in order to set ldrel_count
3720 correctly. */
3721 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3723 asection *o;
3725 for (o = sub->sections; o != NULL; o = o->next)
3727 /* We shouldn't unconditionaly mark the TOC section.
3728 The output file should only have a TOC if either
3729 (a) one of the input files did or (b) we end up
3730 creating TOC references as part of the link process. */
3731 if (o != xcoff_hash_table (info)->toc_section
3732 && (o->flags & SEC_MARK) == 0)
3734 if (! xcoff_mark (info, o))
3735 goto error_return;
3740 else
3742 if (entry != NULL
3743 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3744 goto error_return;
3745 if (info->init_function != NULL
3746 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3747 goto error_return;
3748 if (info->fini_function != NULL
3749 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3750 goto error_return;
3751 if (auto_export_flags != 0)
3753 xcoff_link_hash_traverse (xcoff_hash_table (info),
3754 xcoff_mark_auto_exports, &ldinfo);
3755 if (ldinfo.failed)
3756 goto error_return;
3758 xcoff_sweep (info);
3759 xcoff_hash_table (info)->gc = TRUE;
3762 /* Return special sections to the caller. */
3763 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3765 sec = xcoff_hash_table (info)->special_sections[i];
3767 if (sec != NULL
3768 && gc
3769 && (sec->flags & SEC_MARK) == 0)
3770 sec = NULL;
3772 special_sections[i] = sec;
3775 if (info->input_bfds == NULL)
3776 /* I'm not sure what to do in this bizarre case. */
3777 return TRUE;
3779 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3780 (void *) &ldinfo);
3781 if (ldinfo.failed)
3782 goto error_return;
3784 if (xcoff_hash_table (info)->loader_section
3785 && !xcoff_build_loader_section (&ldinfo, libpath))
3786 goto error_return;
3788 /* Allocate space for the magic sections. */
3789 sec = xcoff_hash_table (info)->linkage_section;
3790 if (sec->size > 0)
3792 sec->contents = bfd_zalloc (output_bfd, sec->size);
3793 if (sec->contents == NULL)
3794 goto error_return;
3796 sec = xcoff_hash_table (info)->toc_section;
3797 if (sec->size > 0)
3799 sec->contents = bfd_zalloc (output_bfd, sec->size);
3800 if (sec->contents == NULL)
3801 goto error_return;
3803 sec = xcoff_hash_table (info)->descriptor_section;
3804 if (sec->size > 0)
3806 sec->contents = bfd_zalloc (output_bfd, sec->size);
3807 if (sec->contents == NULL)
3808 goto error_return;
3811 /* Now that we've done garbage collection, decide which symbols to keep,
3812 and figure out the contents of the .debug section. */
3813 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3815 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3817 asection *subdeb;
3818 bfd_size_type symcount;
3819 long *debug_index;
3820 asection **csectpp;
3821 unsigned int *lineno_counts;
3822 struct xcoff_link_hash_entry **sym_hash;
3823 bfd_byte *esym, *esymend;
3824 bfd_size_type symesz;
3826 if (sub->xvec != info->output_bfd->xvec)
3827 continue;
3829 if ((sub->flags & DYNAMIC) != 0
3830 && !info->static_link)
3831 continue;
3833 if (! _bfd_coff_get_external_symbols (sub))
3834 goto error_return;
3836 symcount = obj_raw_syment_count (sub);
3837 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3838 if (debug_index == NULL)
3839 goto error_return;
3840 xcoff_data (sub)->debug_indices = debug_index;
3842 if (info->strip == strip_all
3843 || info->strip == strip_debugger
3844 || info->discard == discard_all)
3845 /* We're stripping all debugging information, so there's no need
3846 to read SUB's .debug section. */
3847 subdeb = NULL;
3848 else
3850 /* Grab the contents of SUB's .debug section, if any. */
3851 subdeb = bfd_get_section_by_name (sub, ".debug");
3852 if (subdeb != NULL && subdeb->size > 0)
3854 /* We use malloc and copy the names into the debug
3855 stringtab, rather than bfd_alloc, because I expect
3856 that, when linking many files together, many of the
3857 strings will be the same. Storing the strings in the
3858 hash table should save space in this case. */
3859 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3860 goto error_return;
3864 csectpp = xcoff_data (sub)->csects;
3865 lineno_counts = xcoff_data (sub)->lineno_counts;
3866 sym_hash = obj_xcoff_sym_hashes (sub);
3867 symesz = bfd_coff_symesz (sub);
3868 esym = (bfd_byte *) obj_coff_external_syms (sub);
3869 esymend = esym + symcount * symesz;
3871 while (esym < esymend)
3873 struct internal_syment sym;
3874 union internal_auxent aux;
3875 asection *csect;
3876 const char *name;
3877 int keep_p;
3879 bfd_coff_swap_sym_in (sub, esym, &sym);
3881 /* Read in the csect information, if any. */
3882 if (CSECT_SYM_P (sym.n_sclass))
3884 BFD_ASSERT (sym.n_numaux > 0);
3885 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3886 sym.n_type, sym.n_sclass,
3887 sym.n_numaux - 1, sym.n_numaux, &aux);
3890 /* If this symbol's name is stored in the debug section,
3891 get a pointer to it. */
3892 if (debug_contents != NULL
3893 && sym._n._n_n._n_zeroes == 0
3894 && bfd_coff_symname_in_debug (sub, &sym))
3895 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3896 else
3897 name = NULL;
3899 /* Decide whether to copy this symbol to the output file. */
3900 csect = *csectpp;
3901 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3902 *sym_hash, csect, name);
3903 if (keep_p < 0)
3904 return FALSE;
3906 if (!keep_p)
3907 /* Use a debug_index of -2 to record that a symbol should
3908 be stripped. */
3909 *debug_index = -2;
3910 else
3912 /* See whether we should store the symbol name in the
3913 output .debug section. */
3914 if (name != NULL)
3916 bfd_size_type indx;
3918 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3919 if (indx == (bfd_size_type) -1)
3920 goto error_return;
3921 *debug_index = indx;
3923 else
3924 *debug_index = -1;
3925 if (*sym_hash != 0)
3926 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3927 if (*lineno_counts > 0)
3928 csect->output_section->lineno_count += *lineno_counts;
3931 esym += (sym.n_numaux + 1) * symesz;
3932 csectpp += sym.n_numaux + 1;
3933 sym_hash += sym.n_numaux + 1;
3934 lineno_counts += sym.n_numaux + 1;
3935 debug_index += sym.n_numaux + 1;
3938 if (debug_contents)
3940 free (debug_contents);
3941 debug_contents = NULL;
3943 /* Clear the size of subdeb, so that it is not included directly
3944 in the output file. */
3945 subdeb->size = 0;
3948 if (! info->keep_memory)
3950 if (! _bfd_coff_free_symbols (sub))
3951 goto error_return;
3955 if (info->strip != strip_all)
3956 xcoff_hash_table (info)->debug_section->size =
3957 _bfd_stringtab_size (debug_strtab);
3959 return TRUE;
3961 error_return:
3962 if (ldinfo.strings != NULL)
3963 free (ldinfo.strings);
3964 if (debug_contents != NULL)
3965 free (debug_contents);
3966 return FALSE;
3969 bfd_boolean
3970 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3971 const char *init,
3972 const char *fini,
3973 bfd_boolean rtld)
3975 struct bfd_in_memory *bim;
3977 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3978 if (bim == NULL)
3979 return FALSE;
3981 bim->size = 0;
3982 bim->buffer = 0;
3984 abfd->link_next = 0;
3985 abfd->format = bfd_object;
3986 abfd->iostream = (void *) bim;
3987 abfd->flags = BFD_IN_MEMORY;
3988 abfd->iovec = &_bfd_memory_iovec;
3989 abfd->direction = write_direction;
3990 abfd->origin = 0;
3991 abfd->where = 0;
3993 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3994 return FALSE;
3996 /* need to reset to unknown or it will not be read back in correctly */
3997 abfd->format = bfd_unknown;
3998 abfd->direction = read_direction;
3999 abfd->where = 0;
4001 return TRUE;
4004 /* Return the section that defines H. Return null if no section does. */
4006 static asection *
4007 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4009 switch (h->root.type)
4011 case bfd_link_hash_defined:
4012 case bfd_link_hash_defweak:
4013 return h->root.u.def.section;
4015 case bfd_link_hash_common:
4016 return h->root.u.c.p->section;
4018 default:
4019 return NULL;
4023 /* Add a .loader relocation for input relocation IREL. If the loader
4024 relocation should be against an output section, HSEC points to the
4025 input section that IREL is against, otherwise HSEC is null. H is the
4026 symbol that IREL is against, or null if it isn't against a global symbol.
4027 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4029 static bfd_boolean
4030 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4031 asection *output_section, bfd *reference_bfd,
4032 struct internal_reloc *irel, asection *hsec,
4033 struct xcoff_link_hash_entry *h)
4035 struct internal_ldrel ldrel;
4037 ldrel.l_vaddr = irel->r_vaddr;
4038 if (hsec != NULL)
4040 const char *secname;
4042 secname = hsec->output_section->name;
4043 if (strcmp (secname, ".text") == 0)
4044 ldrel.l_symndx = 0;
4045 else if (strcmp (secname, ".data") == 0)
4046 ldrel.l_symndx = 1;
4047 else if (strcmp (secname, ".bss") == 0)
4048 ldrel.l_symndx = 2;
4049 else
4051 (*_bfd_error_handler)
4052 (_("%B: loader reloc in unrecognized section `%s'"),
4053 reference_bfd, secname);
4054 bfd_set_error (bfd_error_nonrepresentable_section);
4055 return FALSE;
4058 else if (h != NULL)
4060 if (h->ldindx < 0)
4062 (*_bfd_error_handler)
4063 (_("%B: `%s' in loader reloc but not loader sym"),
4064 reference_bfd, h->root.root.string);
4065 bfd_set_error (bfd_error_bad_value);
4066 return FALSE;
4068 ldrel.l_symndx = h->ldindx;
4070 else
4071 ldrel.l_symndx = -(bfd_size_type) 1;
4073 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4074 ldrel.l_rsecnm = output_section->target_index;
4075 if (xcoff_hash_table (flinfo->info)->textro
4076 && strcmp (output_section->name, ".text") == 0)
4078 (*_bfd_error_handler)
4079 (_("%B: loader reloc in read-only section %A"),
4080 reference_bfd, output_section);
4081 bfd_set_error (bfd_error_invalid_operation);
4082 return FALSE;
4084 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4085 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4086 return TRUE;
4089 /* Link an input file into the linker output file. This function
4090 handles all the sections and relocations of the input file at once. */
4092 static bfd_boolean
4093 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4094 bfd *input_bfd)
4096 bfd *output_bfd;
4097 const char *strings;
4098 bfd_size_type syment_base;
4099 unsigned int n_tmask;
4100 unsigned int n_btshft;
4101 bfd_boolean copy, hash;
4102 bfd_size_type isymesz;
4103 bfd_size_type osymesz;
4104 bfd_size_type linesz;
4105 bfd_byte *esym;
4106 bfd_byte *esym_end;
4107 struct xcoff_link_hash_entry **sym_hash;
4108 struct internal_syment *isymp;
4109 asection **csectpp;
4110 unsigned int *lineno_counts;
4111 long *debug_index;
4112 long *indexp;
4113 unsigned long output_index;
4114 bfd_byte *outsym;
4115 unsigned int incls;
4116 asection *oline;
4117 bfd_boolean keep_syms;
4118 asection *o;
4120 /* We can just skip DYNAMIC files, unless this is a static link. */
4121 if ((input_bfd->flags & DYNAMIC) != 0
4122 && ! flinfo->info->static_link)
4123 return TRUE;
4125 /* Move all the symbols to the output file. */
4126 output_bfd = flinfo->output_bfd;
4127 strings = NULL;
4128 syment_base = obj_raw_syment_count (output_bfd);
4129 isymesz = bfd_coff_symesz (input_bfd);
4130 osymesz = bfd_coff_symesz (output_bfd);
4131 linesz = bfd_coff_linesz (input_bfd);
4132 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4134 n_tmask = coff_data (input_bfd)->local_n_tmask;
4135 n_btshft = coff_data (input_bfd)->local_n_btshft;
4137 /* Define macros so that ISFCN, et. al., macros work correctly. */
4138 #define N_TMASK n_tmask
4139 #define N_BTSHFT n_btshft
4141 copy = FALSE;
4142 if (! flinfo->info->keep_memory)
4143 copy = TRUE;
4144 hash = TRUE;
4145 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4146 hash = FALSE;
4148 if (! _bfd_coff_get_external_symbols (input_bfd))
4149 return FALSE;
4151 /* Make one pass over the symbols and assign indices to symbols that
4152 we have decided to keep. Also use create .loader symbol information
4153 and update information in hash table entries. */
4154 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4155 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4156 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4157 csectpp = xcoff_data (input_bfd)->csects;
4158 debug_index = xcoff_data (input_bfd)->debug_indices;
4159 isymp = flinfo->internal_syms;
4160 indexp = flinfo->sym_indices;
4161 output_index = syment_base;
4162 while (esym < esym_end)
4164 union internal_auxent aux;
4165 int smtyp = 0;
4166 int add;
4168 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4170 /* Read in the csect information, if any. */
4171 if (CSECT_SYM_P (isymp->n_sclass))
4173 BFD_ASSERT (isymp->n_numaux > 0);
4174 bfd_coff_swap_aux_in (input_bfd,
4175 (void *) (esym + isymesz * isymp->n_numaux),
4176 isymp->n_type, isymp->n_sclass,
4177 isymp->n_numaux - 1, isymp->n_numaux,
4178 (void *) &aux);
4180 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4183 /* If this symbol is in the .loader section, swap out the
4184 .loader symbol information. If this is an external symbol
4185 reference to a defined symbol, though, then wait until we get
4186 to the definition. */
4187 if (EXTERN_SYM_P (isymp->n_sclass)
4188 && *sym_hash != NULL
4189 && (*sym_hash)->ldsym != NULL
4190 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4192 struct xcoff_link_hash_entry *h;
4193 struct internal_ldsym *ldsym;
4195 h = *sym_hash;
4196 ldsym = h->ldsym;
4197 if (isymp->n_scnum > 0)
4199 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4200 ldsym->l_value = (isymp->n_value
4201 + (*csectpp)->output_section->vma
4202 + (*csectpp)->output_offset
4203 - (*csectpp)->vma);
4205 else
4207 ldsym->l_scnum = isymp->n_scnum;
4208 ldsym->l_value = isymp->n_value;
4211 ldsym->l_smtype = smtyp;
4212 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4213 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4214 || (h->flags & XCOFF_IMPORT) != 0)
4215 ldsym->l_smtype |= L_IMPORT;
4216 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4217 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4218 || (h->flags & XCOFF_EXPORT) != 0)
4219 ldsym->l_smtype |= L_EXPORT;
4220 if ((h->flags & XCOFF_ENTRY) != 0)
4221 ldsym->l_smtype |= L_ENTRY;
4222 if (isymp->n_sclass == C_AIX_WEAKEXT)
4223 ldsym->l_smtype |= L_WEAK;
4225 ldsym->l_smclas = aux.x_csect.x_smclas;
4227 if (ldsym->l_ifile == (bfd_size_type) -1)
4228 ldsym->l_ifile = 0;
4229 else if (ldsym->l_ifile == 0)
4231 if ((ldsym->l_smtype & L_IMPORT) == 0)
4232 ldsym->l_ifile = 0;
4233 else
4235 bfd *impbfd;
4237 if (h->root.type == bfd_link_hash_defined
4238 || h->root.type == bfd_link_hash_defweak)
4239 impbfd = h->root.u.def.section->owner;
4240 else if (h->root.type == bfd_link_hash_undefined
4241 || h->root.type == bfd_link_hash_undefweak)
4242 impbfd = h->root.u.undef.abfd;
4243 else
4244 impbfd = NULL;
4246 if (impbfd == NULL)
4247 ldsym->l_ifile = 0;
4248 else
4250 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4251 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4256 ldsym->l_parm = 0;
4258 BFD_ASSERT (h->ldindx >= 0);
4259 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4260 (flinfo->ldsym
4261 + ((h->ldindx - 3)
4262 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4263 h->ldsym = NULL;
4265 /* Fill in snentry now that we know the target_index. */
4266 if ((h->flags & XCOFF_ENTRY) != 0
4267 && (h->root.type == bfd_link_hash_defined
4268 || h->root.type == bfd_link_hash_defweak))
4270 xcoff_data (output_bfd)->snentry =
4271 h->root.u.def.section->output_section->target_index;
4275 add = 1 + isymp->n_numaux;
4277 if (*debug_index == -2)
4278 /* We've decided to strip this symbol. */
4279 *indexp = -1;
4280 else
4282 /* Assign the next unused index to this symbol. */
4283 *indexp = output_index;
4285 if (EXTERN_SYM_P (isymp->n_sclass))
4287 BFD_ASSERT (*sym_hash != NULL);
4288 (*sym_hash)->indx = output_index;
4291 /* If this is a symbol in the TOC which we may have merged
4292 (class XMC_TC), remember the symbol index of the TOC
4293 symbol. */
4294 if (isymp->n_sclass == C_HIDEXT
4295 && aux.x_csect.x_smclas == XMC_TC
4296 && *sym_hash != NULL)
4298 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4299 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4300 (*sym_hash)->u.toc_indx = output_index;
4303 output_index += add;
4306 esym += add * isymesz;
4307 isymp += add;
4308 csectpp += add;
4309 sym_hash += add;
4310 debug_index += add;
4311 ++indexp;
4312 for (--add; add > 0; --add)
4313 *indexp++ = -1;
4316 /* Now write out the symbols that we decided to keep. */
4318 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4319 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4320 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4321 isymp = flinfo->internal_syms;
4322 indexp = flinfo->sym_indices;
4323 csectpp = xcoff_data (input_bfd)->csects;
4324 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4325 debug_index = xcoff_data (input_bfd)->debug_indices;
4326 outsym = flinfo->outsyms;
4327 incls = 0;
4328 oline = NULL;
4329 while (esym < esym_end)
4331 int add;
4333 add = 1 + isymp->n_numaux;
4335 if (*indexp < 0)
4336 esym += add * isymesz;
4337 else
4339 struct internal_syment isym;
4340 int i;
4342 /* Adjust the symbol in order to output it. */
4343 isym = *isymp;
4344 if (isym._n._n_n._n_zeroes == 0
4345 && isym._n._n_n._n_offset != 0)
4347 /* This symbol has a long name. Enter it in the string
4348 table we are building. If *debug_index != -1, the
4349 name has already been entered in the .debug section. */
4350 if (*debug_index >= 0)
4351 isym._n._n_n._n_offset = *debug_index;
4352 else
4354 const char *name;
4355 bfd_size_type indx;
4357 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4359 if (name == NULL)
4360 return FALSE;
4361 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4362 if (indx == (bfd_size_type) -1)
4363 return FALSE;
4364 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4368 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4369 multiple definition problems when linking a shared object
4370 statically. (The native linker doesn't enter __rtinit into
4371 the normal table at all, but having a local symbol can make
4372 the objdump output easier to read.) */
4373 if (isym.n_sclass == C_EXT
4374 && *sym_hash
4375 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4376 isym.n_sclass = C_HIDEXT;
4378 /* The value of a C_FILE symbol is the symbol index of the
4379 next C_FILE symbol. The value of the last C_FILE symbol
4380 is -1. We try to get this right, below, just before we
4381 write the symbols out, but in the general case we may
4382 have to write the symbol out twice. */
4383 if (isym.n_sclass == C_FILE)
4385 if (flinfo->last_file_index != -1
4386 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4388 /* We must correct the value of the last C_FILE entry. */
4389 flinfo->last_file.n_value = *indexp;
4390 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4392 /* The last C_FILE symbol is in this input file. */
4393 bfd_coff_swap_sym_out (output_bfd,
4394 (void *) &flinfo->last_file,
4395 (void *) (flinfo->outsyms
4396 + ((flinfo->last_file_index
4397 - syment_base)
4398 * osymesz)));
4400 else
4402 /* We have already written out the last C_FILE
4403 symbol. We need to write it out again. We
4404 borrow *outsym temporarily. */
4405 file_ptr pos;
4407 bfd_coff_swap_sym_out (output_bfd,
4408 (void *) &flinfo->last_file,
4409 (void *) outsym);
4411 pos = obj_sym_filepos (output_bfd);
4412 pos += flinfo->last_file_index * osymesz;
4413 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4414 || (bfd_bwrite (outsym, osymesz, output_bfd)
4415 != osymesz))
4416 return FALSE;
4420 flinfo->last_file_index = *indexp;
4421 flinfo->last_file = isym;
4424 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4425 into the line numbers. We update the symbol values when
4426 we handle the line numbers. */
4427 if (isym.n_sclass == C_BINCL
4428 || isym.n_sclass == C_EINCL)
4430 isym.n_value = flinfo->line_filepos;
4431 ++incls;
4433 /* The value of a C_BSTAT symbol is the symbol table
4434 index of the containing csect. */
4435 else if (isym.n_sclass == C_BSTAT)
4437 bfd_vma indx;
4439 indx = isym.n_value;
4440 if (indx < obj_raw_syment_count (input_bfd))
4442 long symindx;
4444 symindx = flinfo->sym_indices[indx];
4445 if (symindx < 0)
4446 isym.n_value = 0;
4447 else
4448 isym.n_value = symindx;
4451 else if (isym.n_sclass != C_ESTAT
4452 && isym.n_sclass != C_DECL
4453 && isym.n_scnum > 0)
4455 isym.n_scnum = (*csectpp)->output_section->target_index;
4456 isym.n_value += ((*csectpp)->output_section->vma
4457 + (*csectpp)->output_offset
4458 - (*csectpp)->vma);
4461 /* Output the symbol. */
4462 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4464 esym += isymesz;
4465 outsym += osymesz;
4467 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4469 union internal_auxent aux;
4471 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4472 isymp->n_sclass, i, isymp->n_numaux,
4473 (void *) &aux);
4475 if (isymp->n_sclass == C_FILE)
4477 /* This is the file name (or some comment put in by
4478 the compiler). If it is long, we must put it in
4479 the string table. */
4480 if (aux.x_file.x_n.x_zeroes == 0
4481 && aux.x_file.x_n.x_offset != 0)
4483 const char *filename;
4484 bfd_size_type indx;
4486 BFD_ASSERT (aux.x_file.x_n.x_offset
4487 >= STRING_SIZE_SIZE);
4488 if (strings == NULL)
4490 strings = _bfd_coff_read_string_table (input_bfd);
4491 if (strings == NULL)
4492 return FALSE;
4494 filename = strings + aux.x_file.x_n.x_offset;
4495 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4496 hash, copy);
4497 if (indx == (bfd_size_type) -1)
4498 return FALSE;
4499 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4502 else if (CSECT_SYM_P (isymp->n_sclass)
4503 && i + 1 == isymp->n_numaux)
4506 /* We don't support type checking. I don't know if
4507 anybody does. */
4508 aux.x_csect.x_parmhash = 0;
4509 /* I don't think anybody uses these fields, but we'd
4510 better clobber them just in case. */
4511 aux.x_csect.x_stab = 0;
4512 aux.x_csect.x_snstab = 0;
4514 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4516 unsigned long indx;
4518 indx = aux.x_csect.x_scnlen.l;
4519 if (indx < obj_raw_syment_count (input_bfd))
4521 long symindx;
4523 symindx = flinfo->sym_indices[indx];
4524 if (symindx < 0)
4526 aux.x_csect.x_scnlen.l = 0;
4528 else
4530 aux.x_csect.x_scnlen.l = symindx;
4535 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4537 unsigned long indx;
4539 if (ISFCN (isymp->n_type)
4540 || ISTAG (isymp->n_sclass)
4541 || isymp->n_sclass == C_BLOCK
4542 || isymp->n_sclass == C_FCN)
4544 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4545 if (indx > 0
4546 && indx < obj_raw_syment_count (input_bfd))
4548 /* We look forward through the symbol for
4549 the index of the next symbol we are going
4550 to include. I don't know if this is
4551 entirely right. */
4552 while (flinfo->sym_indices[indx] < 0
4553 && indx < obj_raw_syment_count (input_bfd))
4554 ++indx;
4555 if (indx >= obj_raw_syment_count (input_bfd))
4556 indx = output_index;
4557 else
4558 indx = flinfo->sym_indices[indx];
4559 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4564 indx = aux.x_sym.x_tagndx.l;
4565 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4567 long symindx;
4569 symindx = flinfo->sym_indices[indx];
4570 if (symindx < 0)
4571 aux.x_sym.x_tagndx.l = 0;
4572 else
4573 aux.x_sym.x_tagndx.l = symindx;
4578 /* Copy over the line numbers, unless we are stripping
4579 them. We do this on a symbol by symbol basis in
4580 order to more easily handle garbage collection. */
4581 if (CSECT_SYM_P (isymp->n_sclass)
4582 && i == 0
4583 && isymp->n_numaux > 1
4584 && ISFCN (isymp->n_type)
4585 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4587 if (*lineno_counts == 0)
4588 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4589 else
4591 asection *enclosing;
4592 unsigned int enc_count;
4593 bfd_signed_vma linoff;
4594 struct internal_lineno lin;
4595 bfd_byte *linp;
4596 bfd_byte *linpend;
4597 bfd_vma offset;
4598 file_ptr pos;
4599 bfd_size_type amt;
4601 /* Read in the enclosing section's line-number
4602 information, if we haven't already. */
4603 o = *csectpp;
4604 enclosing = xcoff_section_data (abfd, o)->enclosing;
4605 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4606 if (oline != enclosing)
4608 pos = enclosing->line_filepos;
4609 amt = linesz * enc_count;
4610 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4611 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4612 != amt))
4613 return FALSE;
4614 oline = enclosing;
4617 /* Copy across the first entry, adjusting its
4618 symbol index. */
4619 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4620 - enclosing->line_filepos);
4621 linp = flinfo->linenos + linoff;
4622 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4623 lin.l_addr.l_symndx = *indexp;
4624 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4626 /* Copy the other entries, adjusting their addresses. */
4627 linpend = linp + *lineno_counts * linesz;
4628 offset = (o->output_section->vma
4629 + o->output_offset
4630 - o->vma);
4631 for (linp += linesz; linp < linpend; linp += linesz)
4633 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4634 lin.l_addr.l_paddr += offset;
4635 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4638 /* Write out the entries we've just processed. */
4639 pos = (o->output_section->line_filepos
4640 + o->output_section->lineno_count * linesz);
4641 amt = linesz * *lineno_counts;
4642 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4643 || bfd_bwrite (flinfo->linenos + linoff,
4644 amt, output_bfd) != amt)
4645 return FALSE;
4646 o->output_section->lineno_count += *lineno_counts;
4648 /* Record the offset of the symbol's line numbers
4649 in the output file. */
4650 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4652 if (incls > 0)
4654 struct internal_syment *iisp, *iispend;
4655 long *iindp;
4656 bfd_byte *oos;
4657 bfd_vma range_start, range_end;
4658 int iiadd;
4660 /* Update any C_BINCL or C_EINCL symbols
4661 that refer to a line number in the
4662 range we just output. */
4663 iisp = flinfo->internal_syms;
4664 iispend = iisp + obj_raw_syment_count (input_bfd);
4665 iindp = flinfo->sym_indices;
4666 oos = flinfo->outsyms;
4667 range_start = enclosing->line_filepos + linoff;
4668 range_end = range_start + *lineno_counts * linesz;
4669 while (iisp < iispend)
4671 if (*iindp >= 0
4672 && (iisp->n_sclass == C_BINCL
4673 || iisp->n_sclass == C_EINCL)
4674 && iisp->n_value >= range_start
4675 && iisp->n_value < range_end)
4677 struct internal_syment iis;
4679 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4680 iis.n_value = (iisp->n_value
4681 - range_start
4682 + pos);
4683 bfd_coff_swap_sym_out (output_bfd,
4684 &iis, oos);
4685 --incls;
4688 iiadd = 1 + iisp->n_numaux;
4689 if (*iindp >= 0)
4690 oos += iiadd * osymesz;
4691 iisp += iiadd;
4692 iindp += iiadd;
4698 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4699 isymp->n_sclass, i, isymp->n_numaux,
4700 (void *) outsym);
4701 outsym += osymesz;
4702 esym += isymesz;
4706 sym_hash += add;
4707 indexp += add;
4708 isymp += add;
4709 csectpp += add;
4710 lineno_counts += add;
4711 debug_index += add;
4714 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4715 symbol will be the first symbol in the next input file. In the
4716 normal case, this will save us from writing out the C_FILE symbol
4717 again. */
4718 if (flinfo->last_file_index != -1
4719 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4721 flinfo->last_file.n_value = output_index;
4722 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4723 (void *) (flinfo->outsyms
4724 + ((flinfo->last_file_index - syment_base)
4725 * osymesz)));
4728 /* Write the modified symbols to the output file. */
4729 if (outsym > flinfo->outsyms)
4731 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4732 bfd_size_type amt = outsym - flinfo->outsyms;
4733 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4734 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4735 return FALSE;
4737 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4738 + (outsym - flinfo->outsyms) / osymesz)
4739 == output_index);
4741 obj_raw_syment_count (output_bfd) = output_index;
4744 /* Don't let the linker relocation routines discard the symbols. */
4745 keep_syms = obj_coff_keep_syms (input_bfd);
4746 obj_coff_keep_syms (input_bfd) = TRUE;
4748 /* Relocate the contents of each section. */
4749 for (o = input_bfd->sections; o != NULL; o = o->next)
4751 bfd_byte *contents;
4753 if (! o->linker_mark)
4754 /* This section was omitted from the link. */
4755 continue;
4757 if ((o->flags & SEC_HAS_CONTENTS) == 0
4758 || o->size == 0
4759 || (o->flags & SEC_IN_MEMORY) != 0)
4760 continue;
4762 /* We have set filepos correctly for the sections we created to
4763 represent csects, so bfd_get_section_contents should work. */
4764 if (coff_section_data (input_bfd, o) != NULL
4765 && coff_section_data (input_bfd, o)->contents != NULL)
4766 contents = coff_section_data (input_bfd, o)->contents;
4767 else
4769 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4770 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4771 return FALSE;
4772 contents = flinfo->contents;
4775 if ((o->flags & SEC_RELOC) != 0)
4777 int target_index;
4778 struct internal_reloc *internal_relocs;
4779 struct internal_reloc *irel;
4780 bfd_vma offset;
4781 struct internal_reloc *irelend;
4782 struct xcoff_link_hash_entry **rel_hash;
4783 long r_symndx;
4785 /* Read in the relocs. */
4786 target_index = o->output_section->target_index;
4787 internal_relocs = (xcoff_read_internal_relocs
4788 (input_bfd, o, FALSE, flinfo->external_relocs,
4789 TRUE,
4790 (flinfo->section_info[target_index].relocs
4791 + o->output_section->reloc_count)));
4792 if (internal_relocs == NULL)
4793 return FALSE;
4795 /* Call processor specific code to relocate the section
4796 contents. */
4797 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4798 input_bfd, o,
4799 contents,
4800 internal_relocs,
4801 flinfo->internal_syms,
4802 xcoff_data (input_bfd)->csects))
4803 return FALSE;
4805 offset = o->output_section->vma + o->output_offset - o->vma;
4806 irel = internal_relocs;
4807 irelend = irel + o->reloc_count;
4808 rel_hash = (flinfo->section_info[target_index].rel_hashes
4809 + o->output_section->reloc_count);
4810 for (; irel < irelend; irel++, rel_hash++)
4812 struct xcoff_link_hash_entry *h = NULL;
4814 *rel_hash = NULL;
4816 /* Adjust the reloc address and symbol index. */
4818 irel->r_vaddr += offset;
4820 r_symndx = irel->r_symndx;
4822 if (r_symndx == -1)
4823 h = NULL;
4824 else
4825 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4827 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4829 if (h != NULL
4830 && h->smclas != XMC_TD
4831 && (irel->r_type == R_TOC
4832 || irel->r_type == R_GL
4833 || irel->r_type == R_TCL
4834 || irel->r_type == R_TRL
4835 || irel->r_type == R_TRLA))
4837 /* This is a TOC relative reloc with a symbol
4838 attached. The symbol should be the one which
4839 this reloc is for. We want to make this
4840 reloc against the TOC address of the symbol,
4841 not the symbol itself. */
4842 BFD_ASSERT (h->toc_section != NULL);
4843 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4844 if (h->u.toc_indx != -1)
4845 irel->r_symndx = h->u.toc_indx;
4846 else
4848 struct xcoff_toc_rel_hash *n;
4849 struct xcoff_link_section_info *si;
4850 bfd_size_type amt;
4852 amt = sizeof (* n);
4853 n = bfd_alloc (flinfo->output_bfd, amt);
4854 if (n == NULL)
4855 return FALSE;
4856 si = flinfo->section_info + target_index;
4857 n->next = si->toc_rel_hashes;
4858 n->h = h;
4859 n->rel = irel;
4860 si->toc_rel_hashes = n;
4863 else if (h != NULL)
4865 /* This is a global symbol. */
4866 if (h->indx >= 0)
4867 irel->r_symndx = h->indx;
4868 else
4870 /* This symbol is being written at the end
4871 of the file, and we do not yet know the
4872 symbol index. We save the pointer to the
4873 hash table entry in the rel_hash list.
4874 We set the indx field to -2 to indicate
4875 that this symbol must not be stripped. */
4876 *rel_hash = h;
4877 h->indx = -2;
4880 else
4882 long indx;
4884 indx = flinfo->sym_indices[r_symndx];
4886 if (indx == -1)
4888 struct internal_syment *is;
4890 /* Relocations against a TC0 TOC anchor are
4891 automatically transformed to be against
4892 the TOC anchor in the output file. */
4893 is = flinfo->internal_syms + r_symndx;
4894 if (is->n_sclass == C_HIDEXT
4895 && is->n_numaux > 0)
4897 void * auxptr;
4898 union internal_auxent aux;
4900 auxptr = ((void *)
4901 (((bfd_byte *)
4902 obj_coff_external_syms (input_bfd))
4903 + ((r_symndx + is->n_numaux)
4904 * isymesz)));
4905 bfd_coff_swap_aux_in (input_bfd, auxptr,
4906 is->n_type, is->n_sclass,
4907 is->n_numaux - 1,
4908 is->n_numaux,
4909 (void *) &aux);
4910 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4911 && aux.x_csect.x_smclas == XMC_TC0)
4912 indx = flinfo->toc_symindx;
4916 if (indx != -1)
4917 irel->r_symndx = indx;
4918 else
4921 struct internal_syment *is;
4923 const char *name;
4924 char buf[SYMNMLEN + 1];
4926 /* This reloc is against a symbol we are
4927 stripping. It would be possible to handle
4928 this case, but I don't think it's worth it. */
4929 is = flinfo->internal_syms + r_symndx;
4931 name = (_bfd_coff_internal_syment_name
4932 (input_bfd, is, buf));
4934 if (name == NULL)
4935 return FALSE;
4937 if (! ((*flinfo->info->callbacks->unattached_reloc)
4938 (flinfo->info, name, input_bfd, o,
4939 irel->r_vaddr)))
4940 return FALSE;
4945 if (xcoff_need_ldrel_p (flinfo->info, irel, h))
4947 asection *sec;
4949 if (r_symndx == -1)
4950 sec = NULL;
4951 else if (h == NULL)
4952 sec = xcoff_data (input_bfd)->csects[r_symndx];
4953 else
4954 sec = xcoff_symbol_section (h);
4955 if (!xcoff_create_ldrel (output_bfd, flinfo,
4956 o->output_section, input_bfd,
4957 irel, sec, h))
4958 return FALSE;
4962 o->output_section->reloc_count += o->reloc_count;
4965 /* Write out the modified section contents. */
4966 if (! bfd_set_section_contents (output_bfd, o->output_section,
4967 contents, (file_ptr) o->output_offset,
4968 o->size))
4969 return FALSE;
4972 obj_coff_keep_syms (input_bfd) = keep_syms;
4974 if (! flinfo->info->keep_memory)
4976 if (! _bfd_coff_free_symbols (input_bfd))
4977 return FALSE;
4980 return TRUE;
4983 #undef N_TMASK
4984 #undef N_BTSHFT
4986 /* Sort relocs by VMA. This is called via qsort. */
4988 static int
4989 xcoff_sort_relocs (const void * p1, const void * p2)
4991 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4992 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4994 if (r1->r_vaddr > r2->r_vaddr)
4995 return 1;
4996 else if (r1->r_vaddr < r2->r_vaddr)
4997 return -1;
4998 else
4999 return 0;
5002 /* Return true if section SEC is a TOC section. */
5004 static inline bfd_boolean
5005 xcoff_toc_section_p (asection *sec)
5007 const char *name;
5009 name = sec->name;
5010 if (name[0] == '.' && name[1] == 't')
5012 if (name[2] == 'c')
5014 if (name[3] == '0' && name[4] == 0)
5015 return TRUE;
5016 if (name[3] == 0)
5017 return TRUE;
5019 if (name[2] == 'd' && name[3] == 0)
5020 return TRUE;
5022 return FALSE;
5025 /* See if the link requires a TOC (it usually does!). If so, find a
5026 good place to put the TOC anchor csect, and write out the associated
5027 symbol. */
5029 static bfd_boolean
5030 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5032 bfd_vma toc_start, toc_end, start, end, best_address;
5033 asection *sec;
5034 bfd *input_bfd;
5035 int section_index;
5036 struct internal_syment irsym;
5037 union internal_auxent iraux;
5038 file_ptr pos;
5039 size_t size;
5041 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5042 index of a csect at the beginning of the TOC. */
5043 toc_start = ~(bfd_vma) 0;
5044 toc_end = 0;
5045 section_index = -1;
5046 for (input_bfd = flinfo->info->input_bfds;
5047 input_bfd != NULL;
5048 input_bfd = input_bfd->link_next)
5049 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5050 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5052 start = sec->output_section->vma + sec->output_offset;
5053 if (toc_start > start)
5055 toc_start = start;
5056 section_index = sec->output_section->target_index;
5059 end = start + sec->size;
5060 if (toc_end < end)
5061 toc_end = end;
5064 /* There's no need for a TC0 symbol if we don't have a TOC. */
5065 if (toc_end < toc_start)
5067 xcoff_data (output_bfd)->toc = toc_start;
5068 return TRUE;
5071 if (toc_end - toc_start < 0x8000)
5072 /* Every TOC csect can be accessed from TOC_START. */
5073 best_address = toc_start;
5074 else
5076 /* Find the lowest TOC csect that is still within range of TOC_END. */
5077 best_address = toc_end;
5078 for (input_bfd = flinfo->info->input_bfds;
5079 input_bfd != NULL;
5080 input_bfd = input_bfd->link_next)
5081 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5082 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5084 start = sec->output_section->vma + sec->output_offset;
5085 if (start < best_address
5086 && start + 0x8000 >= toc_end)
5088 best_address = start;
5089 section_index = sec->output_section->target_index;
5093 /* Make sure that the start of the TOC is also within range. */
5094 if (best_address > toc_start + 0x8000)
5096 (*_bfd_error_handler)
5097 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5098 "when compiling"),
5099 (unsigned long) (toc_end - toc_start));
5100 bfd_set_error (bfd_error_file_too_big);
5101 return FALSE;
5105 /* Record the chosen TOC value. */
5106 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5107 xcoff_data (output_bfd)->toc = best_address;
5108 xcoff_data (output_bfd)->sntoc = section_index;
5110 /* Fill out the TC0 symbol. */
5111 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &irsym, "TOC"))
5112 return FALSE;
5113 irsym.n_value = best_address;
5114 irsym.n_scnum = section_index;
5115 irsym.n_sclass = C_HIDEXT;
5116 irsym.n_type = T_NULL;
5117 irsym.n_numaux = 1;
5118 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5120 /* Fill out the auxillary csect information. */
5121 memset (&iraux, 0, sizeof iraux);
5122 iraux.x_csect.x_smtyp = XTY_SD;
5123 iraux.x_csect.x_smclas = XMC_TC0;
5124 iraux.x_csect.x_scnlen.l = 0;
5125 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5126 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5128 /* Write the contents to the file. */
5129 pos = obj_sym_filepos (output_bfd);
5130 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5131 size = 2 * bfd_coff_symesz (output_bfd);
5132 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5133 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5134 return FALSE;
5135 obj_raw_syment_count (output_bfd) += 2;
5137 return TRUE;
5140 /* Write out a non-XCOFF global symbol. */
5142 static bfd_boolean
5143 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
5145 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5146 bfd *output_bfd;
5147 bfd_byte *outsym;
5148 struct internal_syment isym;
5149 union internal_auxent aux;
5150 bfd_boolean result;
5151 file_ptr pos;
5152 bfd_size_type amt;
5154 output_bfd = flinfo->output_bfd;
5155 outsym = flinfo->outsyms;
5157 if (h->root.type == bfd_link_hash_warning)
5159 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5160 if (h->root.type == bfd_link_hash_new)
5161 return TRUE;
5164 /* If this symbol was garbage collected, just skip it. */
5165 if (xcoff_hash_table (flinfo->info)->gc
5166 && (h->flags & XCOFF_MARK) == 0)
5167 return TRUE;
5169 /* If we need a .loader section entry, write it out. */
5170 if (h->ldsym != NULL)
5172 struct internal_ldsym *ldsym;
5173 bfd *impbfd;
5175 ldsym = h->ldsym;
5177 if (h->root.type == bfd_link_hash_undefined
5178 || h->root.type == bfd_link_hash_undefweak)
5181 ldsym->l_value = 0;
5182 ldsym->l_scnum = N_UNDEF;
5183 ldsym->l_smtype = XTY_ER;
5184 impbfd = h->root.u.undef.abfd;
5187 else if (h->root.type == bfd_link_hash_defined
5188 || h->root.type == bfd_link_hash_defweak)
5190 asection *sec;
5192 sec = h->root.u.def.section;
5193 ldsym->l_value = (sec->output_section->vma
5194 + sec->output_offset
5195 + h->root.u.def.value);
5196 ldsym->l_scnum = sec->output_section->target_index;
5197 ldsym->l_smtype = XTY_SD;
5198 impbfd = sec->owner;
5201 else
5202 abort ();
5204 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5205 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5206 || (h->flags & XCOFF_IMPORT) != 0)
5207 /* Clear l_smtype
5208 Import symbols are defined so the check above will make
5209 the l_smtype XTY_SD. But this is not correct, it should
5210 be cleared. */
5211 ldsym->l_smtype |= L_IMPORT;
5213 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5214 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5215 || (h->flags & XCOFF_EXPORT) != 0)
5216 ldsym->l_smtype |= L_EXPORT;
5218 if ((h->flags & XCOFF_ENTRY) != 0)
5219 ldsym->l_smtype |= L_ENTRY;
5221 if ((h->flags & XCOFF_RTINIT) != 0)
5222 ldsym->l_smtype = XTY_SD;
5224 ldsym->l_smclas = h->smclas;
5226 if (ldsym->l_smtype & L_IMPORT)
5228 if ((h->root.type == bfd_link_hash_defined
5229 || h->root.type == bfd_link_hash_defweak)
5230 && (h->root.u.def.value != 0))
5231 ldsym->l_smclas = XMC_XO;
5233 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5234 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5235 ldsym->l_smclas = XMC_SV3264;
5237 else if (h->flags & XCOFF_SYSCALL32)
5238 ldsym->l_smclas = XMC_SV;
5240 else if (h->flags & XCOFF_SYSCALL64)
5241 ldsym->l_smclas = XMC_SV64;
5244 if (ldsym->l_ifile == -(bfd_size_type) 1)
5246 ldsym->l_ifile = 0;
5248 else if (ldsym->l_ifile == 0)
5250 if ((ldsym->l_smtype & L_IMPORT) == 0)
5251 ldsym->l_ifile = 0;
5252 else if (impbfd == NULL)
5253 ldsym->l_ifile = 0;
5254 else
5256 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5257 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5261 ldsym->l_parm = 0;
5263 BFD_ASSERT (h->ldindx >= 0);
5265 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5266 (flinfo->ldsym +
5267 (h->ldindx - 3)
5268 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5269 h->ldsym = NULL;
5272 /* If this symbol needs global linkage code, write it out. */
5273 if (h->root.type == bfd_link_hash_defined
5274 && (h->root.u.def.section
5275 == xcoff_hash_table (flinfo->info)->linkage_section))
5277 bfd_byte *p;
5278 bfd_vma tocoff;
5279 unsigned int i;
5281 p = h->root.u.def.section->contents + h->root.u.def.value;
5283 /* The first instruction in the global linkage code loads a
5284 specific TOC element. */
5285 tocoff = (h->descriptor->toc_section->output_section->vma
5286 + h->descriptor->toc_section->output_offset
5287 - xcoff_data (output_bfd)->toc);
5289 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5290 tocoff += h->descriptor->u.toc_offset;
5292 /* The first instruction in the glink code needs to be
5293 cooked to to hold the correct offset in the toc. The
5294 rest are just output raw. */
5295 bfd_put_32 (output_bfd,
5296 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5298 /* Start with i == 1 to get past the first instruction done above
5299 The /4 is because the glink code is in bytes and we are going
5300 4 at a pop. */
5301 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5302 bfd_put_32 (output_bfd,
5303 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5304 &p[4 * i]);
5307 /* If we created a TOC entry for this symbol, write out the required
5308 relocs. */
5309 if ((h->flags & XCOFF_SET_TOC) != 0)
5311 asection *tocsec;
5312 asection *osec;
5313 int oindx;
5314 struct internal_reloc *irel;
5315 struct internal_syment irsym;
5316 union internal_auxent iraux;
5318 tocsec = h->toc_section;
5319 osec = tocsec->output_section;
5320 oindx = osec->target_index;
5321 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5322 irel->r_vaddr = (osec->vma
5323 + tocsec->output_offset
5324 + h->u.toc_offset);
5326 if (h->indx >= 0)
5327 irel->r_symndx = h->indx;
5328 else
5330 h->indx = -2;
5331 irel->r_symndx = obj_raw_syment_count (output_bfd);
5334 BFD_ASSERT (h->ldindx >= 0);
5336 /* Initialize the aux union here instead of closer to when it is
5337 written out below because the length of the csect depends on
5338 whether the output is 32 or 64 bit. */
5339 memset (&iraux, 0, sizeof iraux);
5340 iraux.x_csect.x_smtyp = XTY_SD;
5341 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5342 iraux.x_csect.x_smclas = XMC_TC;
5344 /* 32 bit uses a 32 bit R_POS to do the relocations
5345 64 bit uses a 64 bit R_POS to do the relocations
5347 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5349 Which one is determined by the backend. */
5350 if (bfd_xcoff_is_xcoff64 (output_bfd))
5352 irel->r_size = 63;
5353 iraux.x_csect.x_scnlen.l = 8;
5355 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5357 irel->r_size = 31;
5358 iraux.x_csect.x_scnlen.l = 4;
5360 else
5361 return FALSE;
5363 irel->r_type = R_POS;
5364 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5365 ++osec->reloc_count;
5367 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5368 output_bfd, irel, NULL, h))
5369 return FALSE;
5371 /* We need to emit a symbol to define a csect which holds
5372 the reloc. */
5373 if (flinfo->info->strip != strip_all)
5375 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab,
5376 &irsym, h->root.root.string);
5377 if (!result)
5378 return FALSE;
5380 irsym.n_value = irel->r_vaddr;
5381 irsym.n_scnum = osec->target_index;
5382 irsym.n_sclass = C_HIDEXT;
5383 irsym.n_type = T_NULL;
5384 irsym.n_numaux = 1;
5386 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5387 outsym += bfd_coff_symesz (output_bfd);
5389 /* Note : iraux is initialized above. */
5390 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5391 0, 1, (void *) outsym);
5392 outsym += bfd_coff_auxesz (output_bfd);
5394 if (h->indx >= 0)
5396 /* We aren't going to write out the symbols below, so we
5397 need to write them out now. */
5398 pos = obj_sym_filepos (output_bfd);
5399 pos += (obj_raw_syment_count (output_bfd)
5400 * bfd_coff_symesz (output_bfd));
5401 amt = outsym - flinfo->outsyms;
5402 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5403 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5404 return FALSE;
5405 obj_raw_syment_count (output_bfd) +=
5406 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5408 outsym = flinfo->outsyms;
5413 /* If this symbol is a specially defined function descriptor, write
5414 it out. The first word is the address of the function code
5415 itself, the second word is the address of the TOC, and the third
5416 word is zero.
5418 32 bit vs 64 bit
5419 The addresses for the 32 bit will take 4 bytes and the addresses
5420 for 64 bit will take 8 bytes. Similar for the relocs. This type
5421 of logic was also done above to create a TOC entry in
5422 xcoff_write_global_symbol. */
5423 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5424 && h->root.type == bfd_link_hash_defined
5425 && (h->root.u.def.section
5426 == xcoff_hash_table (flinfo->info)->descriptor_section))
5428 asection *sec;
5429 asection *osec;
5430 int oindx;
5431 bfd_byte *p;
5432 struct xcoff_link_hash_entry *hentry;
5433 asection *esec;
5434 struct internal_reloc *irel;
5435 asection *tsec;
5436 unsigned int reloc_size, byte_size;
5438 if (bfd_xcoff_is_xcoff64 (output_bfd))
5440 reloc_size = 63;
5441 byte_size = 8;
5443 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5445 reloc_size = 31;
5446 byte_size = 4;
5448 else
5449 return FALSE;
5451 sec = h->root.u.def.section;
5452 osec = sec->output_section;
5453 oindx = osec->target_index;
5454 p = sec->contents + h->root.u.def.value;
5456 hentry = h->descriptor;
5457 BFD_ASSERT (hentry != NULL
5458 && (hentry->root.type == bfd_link_hash_defined
5459 || hentry->root.type == bfd_link_hash_defweak));
5460 esec = hentry->root.u.def.section;
5462 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5463 irel->r_vaddr = (osec->vma
5464 + sec->output_offset
5465 + h->root.u.def.value);
5466 irel->r_symndx = esec->output_section->target_index;
5467 irel->r_type = R_POS;
5468 irel->r_size = reloc_size;
5469 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5470 ++osec->reloc_count;
5472 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5473 output_bfd, irel, esec, NULL))
5474 return FALSE;
5476 /* There are three items to write out,
5477 the address of the code
5478 the address of the toc anchor
5479 the environment pointer.
5480 We are ignoring the environment pointer. So set it to zero. */
5481 if (bfd_xcoff_is_xcoff64 (output_bfd))
5483 bfd_put_64 (output_bfd,
5484 (esec->output_section->vma + esec->output_offset
5485 + hentry->root.u.def.value),
5487 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5488 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5490 else
5492 /* 32 bit backend
5493 This logic was already called above so the error case where
5494 the backend is neither has already been checked. */
5495 bfd_put_32 (output_bfd,
5496 (esec->output_section->vma + esec->output_offset
5497 + hentry->root.u.def.value),
5499 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5500 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5503 tsec = coff_section_from_bfd_index (output_bfd,
5504 xcoff_data (output_bfd)->sntoc);
5506 ++irel;
5507 irel->r_vaddr = (osec->vma
5508 + sec->output_offset
5509 + h->root.u.def.value
5510 + byte_size);
5511 irel->r_symndx = tsec->output_section->target_index;
5512 irel->r_type = R_POS;
5513 irel->r_size = reloc_size;
5514 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5515 ++osec->reloc_count;
5517 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5518 output_bfd, irel, tsec, NULL))
5519 return FALSE;
5522 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5524 BFD_ASSERT (outsym == flinfo->outsyms);
5525 return TRUE;
5528 if (h->indx != -2
5529 && (flinfo->info->strip == strip_all
5530 || (flinfo->info->strip == strip_some
5531 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5532 FALSE, FALSE) == NULL)))
5534 BFD_ASSERT (outsym == flinfo->outsyms);
5535 return TRUE;
5538 if (h->indx != -2
5539 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5541 BFD_ASSERT (outsym == flinfo->outsyms);
5542 return TRUE;
5545 memset (&aux, 0, sizeof aux);
5547 h->indx = obj_raw_syment_count (output_bfd);
5549 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &isym,
5550 h->root.root.string);
5551 if (!result)
5552 return FALSE;
5554 if (h->root.type == bfd_link_hash_undefined
5555 || h->root.type == bfd_link_hash_undefweak)
5557 isym.n_value = 0;
5558 isym.n_scnum = N_UNDEF;
5559 if (h->root.type == bfd_link_hash_undefweak
5560 && C_WEAKEXT == C_AIX_WEAKEXT)
5561 isym.n_sclass = C_WEAKEXT;
5562 else
5563 isym.n_sclass = C_EXT;
5564 aux.x_csect.x_smtyp = XTY_ER;
5566 else if ((h->root.type == bfd_link_hash_defined
5567 || h->root.type == bfd_link_hash_defweak)
5568 && h->smclas == XMC_XO)
5570 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5571 isym.n_value = h->root.u.def.value;
5572 isym.n_scnum = N_UNDEF;
5573 if (h->root.type == bfd_link_hash_undefweak
5574 && C_WEAKEXT == C_AIX_WEAKEXT)
5575 isym.n_sclass = C_WEAKEXT;
5576 else
5577 isym.n_sclass = C_EXT;
5578 aux.x_csect.x_smtyp = XTY_ER;
5580 else if (h->root.type == bfd_link_hash_defined
5581 || h->root.type == bfd_link_hash_defweak)
5583 struct xcoff_link_size_list *l;
5585 isym.n_value = (h->root.u.def.section->output_section->vma
5586 + h->root.u.def.section->output_offset
5587 + h->root.u.def.value);
5588 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5589 isym.n_scnum = N_ABS;
5590 else
5591 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5592 isym.n_sclass = C_HIDEXT;
5593 aux.x_csect.x_smtyp = XTY_SD;
5595 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5597 for (l = xcoff_hash_table (flinfo->info)->size_list;
5598 l != NULL;
5599 l = l->next)
5601 if (l->h == h)
5603 aux.x_csect.x_scnlen.l = l->size;
5604 break;
5609 else if (h->root.type == bfd_link_hash_common)
5611 isym.n_value = (h->root.u.c.p->section->output_section->vma
5612 + h->root.u.c.p->section->output_offset);
5613 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5614 isym.n_sclass = C_EXT;
5615 aux.x_csect.x_smtyp = XTY_CM;
5616 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5618 else
5619 abort ();
5621 isym.n_type = T_NULL;
5622 isym.n_numaux = 1;
5624 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5625 outsym += bfd_coff_symesz (output_bfd);
5627 aux.x_csect.x_smclas = h->smclas;
5628 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5629 (void *) outsym);
5630 outsym += bfd_coff_auxesz (output_bfd);
5632 if ((h->root.type == bfd_link_hash_defined
5633 || h->root.type == bfd_link_hash_defweak)
5634 && h->smclas != XMC_XO)
5636 /* We just output an SD symbol. Now output an LD symbol. */
5637 h->indx += 2;
5639 if (h->root.type == bfd_link_hash_undefweak
5640 && C_WEAKEXT == C_AIX_WEAKEXT)
5641 isym.n_sclass = C_WEAKEXT;
5642 else
5643 isym.n_sclass = C_EXT;
5644 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5645 outsym += bfd_coff_symesz (output_bfd);
5647 aux.x_csect.x_smtyp = XTY_LD;
5648 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5649 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5650 (void *) outsym);
5651 outsym += bfd_coff_auxesz (output_bfd);
5654 pos = obj_sym_filepos (output_bfd);
5655 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5656 amt = outsym - flinfo->outsyms;
5657 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5658 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5659 return FALSE;
5660 obj_raw_syment_count (output_bfd) +=
5661 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5663 return TRUE;
5666 /* Handle a link order which is supposed to generate a reloc. */
5668 static bfd_boolean
5669 xcoff_reloc_link_order (bfd *output_bfd,
5670 struct xcoff_final_link_info *flinfo,
5671 asection *output_section,
5672 struct bfd_link_order *link_order)
5674 reloc_howto_type *howto;
5675 struct xcoff_link_hash_entry *h;
5676 asection *hsec;
5677 bfd_vma hval;
5678 bfd_vma addend;
5679 struct internal_reloc *irel;
5680 struct xcoff_link_hash_entry **rel_hash_ptr;
5682 if (link_order->type == bfd_section_reloc_link_order)
5683 /* We need to somehow locate a symbol in the right section. The
5684 symbol must either have a value of zero, or we must adjust
5685 the addend by the value of the symbol. FIXME: Write this
5686 when we need it. The old linker couldn't handle this anyhow. */
5687 abort ();
5689 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5690 if (howto == NULL)
5692 bfd_set_error (bfd_error_bad_value);
5693 return FALSE;
5696 h = ((struct xcoff_link_hash_entry *)
5697 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5698 link_order->u.reloc.p->u.name,
5699 FALSE, FALSE, TRUE));
5700 if (h == NULL)
5702 if (! ((*flinfo->info->callbacks->unattached_reloc)
5703 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5704 return FALSE;
5705 return TRUE;
5708 hsec = xcoff_symbol_section (h);
5709 if (h->root.type == bfd_link_hash_defined
5710 || h->root.type == bfd_link_hash_defweak)
5711 hval = h->root.u.def.value;
5712 else
5713 hval = 0;
5715 addend = link_order->u.reloc.p->addend;
5716 if (hsec != NULL)
5717 addend += (hsec->output_section->vma
5718 + hsec->output_offset
5719 + hval);
5721 if (addend != 0)
5723 bfd_size_type size;
5724 bfd_byte *buf;
5725 bfd_reloc_status_type rstat;
5726 bfd_boolean ok;
5728 size = bfd_get_reloc_size (howto);
5729 buf = bfd_zmalloc (size);
5730 if (buf == NULL)
5731 return FALSE;
5733 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5734 switch (rstat)
5736 case bfd_reloc_ok:
5737 break;
5738 default:
5739 case bfd_reloc_outofrange:
5740 abort ();
5741 case bfd_reloc_overflow:
5742 if (! ((*flinfo->info->callbacks->reloc_overflow)
5743 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5744 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5746 free (buf);
5747 return FALSE;
5749 break;
5751 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5752 (file_ptr) link_order->offset, size);
5753 free (buf);
5754 if (! ok)
5755 return FALSE;
5758 /* Store the reloc information in the right place. It will get
5759 swapped and written out at the end of the final_link routine. */
5760 irel = (flinfo->section_info[output_section->target_index].relocs
5761 + output_section->reloc_count);
5762 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5763 + output_section->reloc_count);
5765 memset (irel, 0, sizeof (struct internal_reloc));
5766 *rel_hash_ptr = NULL;
5768 irel->r_vaddr = output_section->vma + link_order->offset;
5770 if (h->indx >= 0)
5771 irel->r_symndx = h->indx;
5772 else
5774 /* Set the index to -2 to force this symbol to get written out. */
5775 h->indx = -2;
5776 *rel_hash_ptr = h;
5777 irel->r_symndx = 0;
5780 irel->r_type = howto->type;
5781 irel->r_size = howto->bitsize - 1;
5782 if (howto->complain_on_overflow == complain_overflow_signed)
5783 irel->r_size |= 0x80;
5785 ++output_section->reloc_count;
5787 /* Now output the reloc to the .loader section. */
5788 if (xcoff_hash_table (flinfo->info)->loader_section)
5790 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5791 output_bfd, irel, hsec, h))
5792 return FALSE;
5795 return TRUE;
5798 /* Do the final link step. */
5800 bfd_boolean
5801 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5803 bfd_size_type symesz;
5804 struct xcoff_final_link_info flinfo;
5805 asection *o;
5806 struct bfd_link_order *p;
5807 bfd_size_type max_contents_size;
5808 bfd_size_type max_sym_count;
5809 bfd_size_type max_lineno_count;
5810 bfd_size_type max_reloc_count;
5811 bfd_size_type max_output_reloc_count;
5812 file_ptr rel_filepos;
5813 unsigned int relsz;
5814 file_ptr line_filepos;
5815 unsigned int linesz;
5816 bfd *sub;
5817 bfd_byte *external_relocs = NULL;
5818 char strbuf[STRING_SIZE_SIZE];
5819 file_ptr pos;
5820 bfd_size_type amt;
5822 if (info->shared)
5823 abfd->flags |= DYNAMIC;
5825 symesz = bfd_coff_symesz (abfd);
5827 flinfo.info = info;
5828 flinfo.output_bfd = abfd;
5829 flinfo.strtab = NULL;
5830 flinfo.section_info = NULL;
5831 flinfo.last_file_index = -1;
5832 flinfo.toc_symindx = -1;
5833 flinfo.internal_syms = NULL;
5834 flinfo.sym_indices = NULL;
5835 flinfo.outsyms = NULL;
5836 flinfo.linenos = NULL;
5837 flinfo.contents = NULL;
5838 flinfo.external_relocs = NULL;
5840 if (xcoff_hash_table (info)->loader_section)
5842 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5843 + bfd_xcoff_ldhdrsz (abfd));
5844 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5845 + bfd_xcoff_ldhdrsz (abfd)
5846 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5847 * bfd_xcoff_ldsymsz (abfd)));
5849 else
5851 flinfo.ldsym = NULL;
5852 flinfo.ldrel = NULL;
5855 xcoff_data (abfd)->coff.link_info = info;
5857 flinfo.strtab = _bfd_stringtab_init ();
5858 if (flinfo.strtab == NULL)
5859 goto error_return;
5861 /* Count the relocation entries required for the output file.
5862 (We've already counted the line numbers.) Determine a few
5863 maximum sizes. */
5864 max_contents_size = 0;
5865 max_lineno_count = 0;
5866 max_reloc_count = 0;
5867 for (o = abfd->sections; o != NULL; o = o->next)
5869 o->reloc_count = 0;
5870 for (p = o->map_head.link_order; p != NULL; p = p->next)
5872 if (p->type == bfd_indirect_link_order)
5874 asection *sec;
5876 sec = p->u.indirect.section;
5878 /* Mark all sections which are to be included in the
5879 link. This will normally be every section. We need
5880 to do this so that we can identify any sections which
5881 the linker has decided to not include. */
5882 sec->linker_mark = TRUE;
5884 o->reloc_count += sec->reloc_count;
5886 if ((sec->flags & SEC_IN_MEMORY) == 0)
5888 if (sec->rawsize > max_contents_size)
5889 max_contents_size = sec->rawsize;
5890 if (sec->size > max_contents_size)
5891 max_contents_size = sec->size;
5893 if (coff_section_data (sec->owner, sec) != NULL
5894 && xcoff_section_data (sec->owner, sec) != NULL
5895 && (xcoff_section_data (sec->owner, sec)->lineno_count
5896 > max_lineno_count))
5897 max_lineno_count =
5898 xcoff_section_data (sec->owner, sec)->lineno_count;
5899 if (sec->reloc_count > max_reloc_count)
5900 max_reloc_count = sec->reloc_count;
5902 else if (p->type == bfd_section_reloc_link_order
5903 || p->type == bfd_symbol_reloc_link_order)
5904 ++o->reloc_count;
5908 /* Compute the file positions for all the sections. */
5909 if (abfd->output_has_begun)
5911 if (xcoff_hash_table (info)->file_align != 0)
5912 abort ();
5914 else
5916 bfd_vma file_align;
5918 file_align = xcoff_hash_table (info)->file_align;
5919 if (file_align != 0)
5921 bfd_boolean saw_contents;
5922 int indx;
5923 file_ptr sofar;
5925 /* Insert .pad sections before every section which has
5926 contents and is loaded, if it is preceded by some other
5927 section which has contents and is loaded. */
5928 saw_contents = TRUE;
5929 for (o = abfd->sections; o != NULL; o = o->next)
5931 if (strcmp (o->name, ".pad") == 0)
5932 saw_contents = FALSE;
5933 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5934 && (o->flags & SEC_LOAD) != 0)
5936 if (! saw_contents)
5937 saw_contents = TRUE;
5938 else
5940 asection *n;
5942 /* Create a pad section and place it before the section
5943 that needs padding. This requires unlinking and
5944 relinking the bfd's section list. */
5946 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5947 SEC_HAS_CONTENTS);
5948 n->alignment_power = 0;
5950 bfd_section_list_remove (abfd, n);
5951 bfd_section_list_insert_before (abfd, o, n);
5952 saw_contents = FALSE;
5957 /* Reset the section indices after inserting the new
5958 sections. */
5959 indx = 0;
5960 for (o = abfd->sections; o != NULL; o = o->next)
5962 ++indx;
5963 o->target_index = indx;
5965 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5967 /* Work out appropriate sizes for the .pad sections to force
5968 each section to land on a page boundary. This bit of
5969 code knows what compute_section_file_positions is going
5970 to do. */
5971 sofar = bfd_coff_filhsz (abfd);
5972 sofar += bfd_coff_aoutsz (abfd);
5973 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5974 for (o = abfd->sections; o != NULL; o = o->next)
5975 if ((bfd_xcoff_is_reloc_count_overflow
5976 (abfd, (bfd_vma) o->reloc_count))
5977 || (bfd_xcoff_is_lineno_count_overflow
5978 (abfd, (bfd_vma) o->lineno_count)))
5979 /* 64 does not overflow, need to check if 32 does */
5980 sofar += bfd_coff_scnhsz (abfd);
5982 for (o = abfd->sections; o != NULL; o = o->next)
5984 if (strcmp (o->name, ".pad") == 0)
5986 bfd_vma pageoff;
5988 BFD_ASSERT (o->size == 0);
5989 pageoff = sofar & (file_align - 1);
5990 if (pageoff != 0)
5992 o->size = file_align - pageoff;
5993 sofar += file_align - pageoff;
5994 o->flags |= SEC_HAS_CONTENTS;
5997 else
5999 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6000 sofar += BFD_ALIGN (o->size,
6001 1 << o->alignment_power);
6006 if (! bfd_coff_compute_section_file_positions (abfd))
6007 goto error_return;
6010 /* Allocate space for the pointers we need to keep for the relocs. */
6012 unsigned int i;
6014 /* We use section_count + 1, rather than section_count, because
6015 the target_index fields are 1 based. */
6016 amt = abfd->section_count + 1;
6017 amt *= sizeof (struct xcoff_link_section_info);
6018 flinfo.section_info = bfd_malloc (amt);
6019 if (flinfo.section_info == NULL)
6020 goto error_return;
6021 for (i = 0; i <= abfd->section_count; i++)
6023 flinfo.section_info[i].relocs = NULL;
6024 flinfo.section_info[i].rel_hashes = NULL;
6025 flinfo.section_info[i].toc_rel_hashes = NULL;
6029 /* Set the file positions for the relocs. */
6030 rel_filepos = obj_relocbase (abfd);
6031 relsz = bfd_coff_relsz (abfd);
6032 max_output_reloc_count = 0;
6033 for (o = abfd->sections; o != NULL; o = o->next)
6035 if (o->reloc_count == 0)
6036 o->rel_filepos = 0;
6037 else
6039 /* A stripped file has no relocs. However, we still
6040 allocate the buffers, so that later code doesn't have to
6041 worry about whether we are stripping or not. */
6042 if (info->strip == strip_all)
6043 o->rel_filepos = 0;
6044 else
6046 o->flags |= SEC_RELOC;
6047 o->rel_filepos = rel_filepos;
6048 rel_filepos += o->reloc_count * relsz;
6051 /* We don't know the indices of global symbols until we have
6052 written out all the local symbols. For each section in
6053 the output file, we keep an array of pointers to hash
6054 table entries. Each entry in the array corresponds to a
6055 reloc. When we find a reloc against a global symbol, we
6056 set the corresponding entry in this array so that we can
6057 fix up the symbol index after we have written out all the
6058 local symbols.
6060 Because of this problem, we also keep the relocs in
6061 memory until the end of the link. This wastes memory.
6062 We could backpatch the file later, I suppose, although it
6063 would be slow. */
6064 amt = o->reloc_count;
6065 amt *= sizeof (struct internal_reloc);
6066 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6068 amt = o->reloc_count;
6069 amt *= sizeof (struct xcoff_link_hash_entry *);
6070 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6072 if (flinfo.section_info[o->target_index].relocs == NULL
6073 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6074 goto error_return;
6076 if (o->reloc_count > max_output_reloc_count)
6077 max_output_reloc_count = o->reloc_count;
6081 /* We now know the size of the relocs, so we can determine the file
6082 positions of the line numbers. */
6083 line_filepos = rel_filepos;
6084 flinfo.line_filepos = line_filepos;
6085 linesz = bfd_coff_linesz (abfd);
6086 for (o = abfd->sections; o != NULL; o = o->next)
6088 if (o->lineno_count == 0)
6089 o->line_filepos = 0;
6090 else
6092 o->line_filepos = line_filepos;
6093 line_filepos += o->lineno_count * linesz;
6096 /* Reset the reloc and lineno counts, so that we can use them to
6097 count the number of entries we have output so far. */
6098 o->reloc_count = 0;
6099 o->lineno_count = 0;
6102 obj_sym_filepos (abfd) = line_filepos;
6104 /* Figure out the largest number of symbols in an input BFD. Take
6105 the opportunity to clear the output_has_begun fields of all the
6106 input BFD's. We want at least 6 symbols, since that is the
6107 number which xcoff_write_global_symbol may need. */
6108 max_sym_count = 6;
6109 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6111 bfd_size_type sz;
6113 sub->output_has_begun = FALSE;
6114 sz = obj_raw_syment_count (sub);
6115 if (sz > max_sym_count)
6116 max_sym_count = sz;
6119 /* Allocate some buffers used while linking. */
6120 amt = max_sym_count * sizeof (struct internal_syment);
6121 flinfo.internal_syms = bfd_malloc (amt);
6123 amt = max_sym_count * sizeof (long);
6124 flinfo.sym_indices = bfd_malloc (amt);
6126 amt = (max_sym_count + 1) * symesz;
6127 flinfo.outsyms = bfd_malloc (amt);
6129 amt = max_lineno_count * bfd_coff_linesz (abfd);
6130 flinfo.linenos = bfd_malloc (amt);
6132 amt = max_contents_size;
6133 flinfo.contents = bfd_malloc (amt);
6135 amt = max_reloc_count * relsz;
6136 flinfo.external_relocs = bfd_malloc (amt);
6138 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6139 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6140 || flinfo.outsyms == NULL
6141 || (flinfo.linenos == NULL && max_lineno_count > 0)
6142 || (flinfo.contents == NULL && max_contents_size > 0)
6143 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6144 goto error_return;
6146 obj_raw_syment_count (abfd) = 0;
6148 /* Find a TOC symbol, if we need one. */
6149 if (!xcoff_find_tc0 (abfd, &flinfo))
6150 goto error_return;
6152 /* We now know the position of everything in the file, except that
6153 we don't know the size of the symbol table and therefore we don't
6154 know where the string table starts. We just build the string
6155 table in memory as we go along. We process all the relocations
6156 for a single input file at once. */
6157 for (o = abfd->sections; o != NULL; o = o->next)
6159 for (p = o->map_head.link_order; p != NULL; p = p->next)
6161 if (p->type == bfd_indirect_link_order
6162 && p->u.indirect.section->owner->xvec == abfd->xvec)
6164 sub = p->u.indirect.section->owner;
6165 if (! sub->output_has_begun)
6167 if (! xcoff_link_input_bfd (&flinfo, sub))
6168 goto error_return;
6169 sub->output_has_begun = TRUE;
6172 else if (p->type == bfd_section_reloc_link_order
6173 || p->type == bfd_symbol_reloc_link_order)
6175 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6176 goto error_return;
6178 else
6180 if (! _bfd_default_link_order (abfd, info, o, p))
6181 goto error_return;
6186 /* Free up the buffers used by xcoff_link_input_bfd. */
6187 if (flinfo.internal_syms != NULL)
6189 free (flinfo.internal_syms);
6190 flinfo.internal_syms = NULL;
6192 if (flinfo.sym_indices != NULL)
6194 free (flinfo.sym_indices);
6195 flinfo.sym_indices = NULL;
6197 if (flinfo.linenos != NULL)
6199 free (flinfo.linenos);
6200 flinfo.linenos = NULL;
6202 if (flinfo.contents != NULL)
6204 free (flinfo.contents);
6205 flinfo.contents = NULL;
6207 if (flinfo.external_relocs != NULL)
6209 free (flinfo.external_relocs);
6210 flinfo.external_relocs = NULL;
6213 /* The value of the last C_FILE symbol is supposed to be -1. Write
6214 it out again. */
6215 if (flinfo.last_file_index != -1)
6217 flinfo.last_file.n_value = -(bfd_vma) 1;
6218 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6219 (void *) flinfo.outsyms);
6220 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6221 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6222 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6223 goto error_return;
6226 /* Write out all the global symbols which do not come from XCOFF
6227 input files. */
6228 xcoff_link_hash_traverse (xcoff_hash_table (info),
6229 xcoff_write_global_symbol,
6230 (void *) &flinfo);
6232 if (flinfo.outsyms != NULL)
6234 free (flinfo.outsyms);
6235 flinfo.outsyms = NULL;
6238 /* Now that we have written out all the global symbols, we know the
6239 symbol indices to use for relocs against them, and we can finally
6240 write out the relocs. */
6241 amt = max_output_reloc_count * relsz;
6242 external_relocs = bfd_malloc (amt);
6243 if (external_relocs == NULL && max_output_reloc_count != 0)
6244 goto error_return;
6246 for (o = abfd->sections; o != NULL; o = o->next)
6248 struct internal_reloc *irel;
6249 struct internal_reloc *irelend;
6250 struct xcoff_link_hash_entry **rel_hash;
6251 struct xcoff_toc_rel_hash *toc_rel_hash;
6252 bfd_byte *erel;
6253 bfd_size_type rel_size;
6255 /* A stripped file has no relocs. */
6256 if (info->strip == strip_all)
6258 o->reloc_count = 0;
6259 continue;
6262 if (o->reloc_count == 0)
6263 continue;
6265 irel = flinfo.section_info[o->target_index].relocs;
6266 irelend = irel + o->reloc_count;
6267 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6268 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6270 if (*rel_hash != NULL)
6272 if ((*rel_hash)->indx < 0)
6274 if (! ((*info->callbacks->unattached_reloc)
6275 (info, (*rel_hash)->root.root.string,
6276 NULL, o, irel->r_vaddr)))
6277 goto error_return;
6278 (*rel_hash)->indx = 0;
6280 irel->r_symndx = (*rel_hash)->indx;
6284 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6285 toc_rel_hash != NULL;
6286 toc_rel_hash = toc_rel_hash->next)
6288 if (toc_rel_hash->h->u.toc_indx < 0)
6290 if (! ((*info->callbacks->unattached_reloc)
6291 (info, toc_rel_hash->h->root.root.string,
6292 NULL, o, toc_rel_hash->rel->r_vaddr)))
6293 goto error_return;
6294 toc_rel_hash->h->u.toc_indx = 0;
6296 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6299 /* XCOFF requires that the relocs be sorted by address. We tend
6300 to produce them in the order in which their containing csects
6301 appear in the symbol table, which is not necessarily by
6302 address. So we sort them here. There may be a better way to
6303 do this. */
6304 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6305 o->reloc_count, sizeof (struct internal_reloc),
6306 xcoff_sort_relocs);
6308 irel = flinfo.section_info[o->target_index].relocs;
6309 irelend = irel + o->reloc_count;
6310 erel = external_relocs;
6311 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6312 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6314 rel_size = relsz * o->reloc_count;
6315 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6316 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6317 goto error_return;
6320 if (external_relocs != NULL)
6322 free (external_relocs);
6323 external_relocs = NULL;
6326 /* Free up the section information. */
6327 if (flinfo.section_info != NULL)
6329 unsigned int i;
6331 for (i = 0; i < abfd->section_count; i++)
6333 if (flinfo.section_info[i].relocs != NULL)
6334 free (flinfo.section_info[i].relocs);
6335 if (flinfo.section_info[i].rel_hashes != NULL)
6336 free (flinfo.section_info[i].rel_hashes);
6338 free (flinfo.section_info);
6339 flinfo.section_info = NULL;
6342 /* Write out the loader section contents. */
6343 o = xcoff_hash_table (info)->loader_section;
6344 if (o)
6346 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6347 == (xcoff_hash_table (info)->loader_section->contents
6348 + xcoff_hash_table (info)->ldhdr.l_impoff));
6349 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6350 (file_ptr) o->output_offset, o->size))
6351 goto error_return;
6354 /* Write out the magic sections. */
6355 o = xcoff_hash_table (info)->linkage_section;
6356 if (o->size > 0
6357 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6358 (file_ptr) o->output_offset,
6359 o->size))
6360 goto error_return;
6361 o = xcoff_hash_table (info)->toc_section;
6362 if (o->size > 0
6363 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6364 (file_ptr) o->output_offset,
6365 o->size))
6366 goto error_return;
6367 o = xcoff_hash_table (info)->descriptor_section;
6368 if (o->size > 0
6369 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6370 (file_ptr) o->output_offset,
6371 o->size))
6372 goto error_return;
6374 /* Write out the string table. */
6375 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6376 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6377 goto error_return;
6378 H_PUT_32 (abfd,
6379 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6380 strbuf);
6381 amt = STRING_SIZE_SIZE;
6382 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6383 goto error_return;
6384 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6385 goto error_return;
6387 _bfd_stringtab_free (flinfo.strtab);
6389 /* Write out the debugging string table. */
6390 o = xcoff_hash_table (info)->debug_section;
6391 if (o != NULL)
6393 struct bfd_strtab_hash *debug_strtab;
6395 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6396 BFD_ASSERT (o->output_section->size - o->output_offset
6397 >= _bfd_stringtab_size (debug_strtab));
6398 pos = o->output_section->filepos + o->output_offset;
6399 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6400 goto error_return;
6401 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6402 goto error_return;
6405 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6406 not try to write out the symbols. */
6407 bfd_get_symcount (abfd) = 0;
6409 return TRUE;
6411 error_return:
6412 if (flinfo.strtab != NULL)
6413 _bfd_stringtab_free (flinfo.strtab);
6415 if (flinfo.section_info != NULL)
6417 unsigned int i;
6419 for (i = 0; i < abfd->section_count; i++)
6421 if (flinfo.section_info[i].relocs != NULL)
6422 free (flinfo.section_info[i].relocs);
6423 if (flinfo.section_info[i].rel_hashes != NULL)
6424 free (flinfo.section_info[i].rel_hashes);
6426 free (flinfo.section_info);
6429 if (flinfo.internal_syms != NULL)
6430 free (flinfo.internal_syms);
6431 if (flinfo.sym_indices != NULL)
6432 free (flinfo.sym_indices);
6433 if (flinfo.outsyms != NULL)
6434 free (flinfo.outsyms);
6435 if (flinfo.linenos != NULL)
6436 free (flinfo.linenos);
6437 if (flinfo.contents != NULL)
6438 free (flinfo.contents);
6439 if (flinfo.external_relocs != NULL)
6440 free (flinfo.external_relocs);
6441 if (external_relocs != NULL)
6442 free (external_relocs);
6443 return FALSE;