gmp-utils: New API to simply use of GMP's integer/rational/float objects
[binutils-gdb.git] / bfd / xcofflink.c
blobca7d7fec04bba08cf2a6bb6056511a0467121e4a
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
28 #include "libcoff.h"
29 #include "libxcoff.h"
30 #include "libiberty.h"
31 #include "xcofflink.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 size_t 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 free (contents);
231 return FALSE;
233 coff_section_data (abfd, sec)->contents = contents;
236 return TRUE;
239 /* Get the size required to hold the dynamic symbols. */
241 long
242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
244 asection *lsec;
245 bfd_byte *contents;
246 struct internal_ldhdr ldhdr;
248 if ((abfd->flags & DYNAMIC) == 0)
250 bfd_set_error (bfd_error_invalid_operation);
251 return -1;
254 lsec = bfd_get_section_by_name (abfd, ".loader");
255 if (lsec == NULL)
257 bfd_set_error (bfd_error_no_symbols);
258 return -1;
261 if (! xcoff_get_section_contents (abfd, lsec))
262 return -1;
263 contents = coff_section_data (abfd, lsec)->contents;
265 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
267 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
270 /* Get the dynamic symbols. */
272 long
273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
275 asection *lsec;
276 bfd_byte *contents;
277 struct internal_ldhdr ldhdr;
278 const char *strings;
279 bfd_byte *elsym, *elsymend;
280 coff_symbol_type *symbuf;
282 if ((abfd->flags & DYNAMIC) == 0)
284 bfd_set_error (bfd_error_invalid_operation);
285 return -1;
288 lsec = bfd_get_section_by_name (abfd, ".loader");
289 if (lsec == NULL)
291 bfd_set_error (bfd_error_no_symbols);
292 return -1;
295 if (! xcoff_get_section_contents (abfd, lsec))
296 return -1;
297 contents = coff_section_data (abfd, lsec)->contents;
299 coff_section_data (abfd, lsec)->keep_contents = TRUE;
301 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
303 strings = (char *) contents + ldhdr.l_stoff;
305 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
306 if (symbuf == NULL)
307 return -1;
309 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
311 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
312 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
314 struct internal_ldsym ldsym;
316 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
318 symbuf->symbol.the_bfd = abfd;
320 if (ldsym._l._l_l._l_zeroes == 0)
321 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
322 else
324 char *c;
326 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
327 if (c == NULL)
328 return -1;
329 memcpy (c, ldsym._l._l_name, SYMNMLEN);
330 c[SYMNMLEN] = '\0';
331 symbuf->symbol.name = c;
334 if (ldsym.l_smclas == XMC_XO)
335 symbuf->symbol.section = bfd_abs_section_ptr;
336 else
337 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
338 ldsym.l_scnum);
339 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
341 symbuf->symbol.flags = BSF_NO_FLAGS;
342 if ((ldsym.l_smtype & L_EXPORT) != 0)
344 if ((ldsym.l_smtype & L_WEAK) != 0)
345 symbuf->symbol.flags |= BSF_WEAK;
346 else
347 symbuf->symbol.flags |= BSF_GLOBAL;
350 /* FIXME: We have no way to record the other information stored
351 with the loader symbol. */
352 *psyms = (asymbol *) symbuf;
355 *psyms = NULL;
357 return ldhdr.l_nsyms;
360 /* Get the size required to hold the dynamic relocs. */
362 long
363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
365 asection *lsec;
366 bfd_byte *contents;
367 struct internal_ldhdr ldhdr;
369 if ((abfd->flags & DYNAMIC) == 0)
371 bfd_set_error (bfd_error_invalid_operation);
372 return -1;
375 lsec = bfd_get_section_by_name (abfd, ".loader");
376 if (lsec == NULL)
378 bfd_set_error (bfd_error_no_symbols);
379 return -1;
382 if (! xcoff_get_section_contents (abfd, lsec))
383 return -1;
384 contents = coff_section_data (abfd, lsec)->contents;
386 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
388 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
391 /* Get the dynamic relocs. */
393 long
394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
395 arelent **prelocs,
396 asymbol **syms)
398 asection *lsec;
399 bfd_byte *contents;
400 struct internal_ldhdr ldhdr;
401 arelent *relbuf;
402 bfd_byte *elrel, *elrelend;
404 if ((abfd->flags & DYNAMIC) == 0)
406 bfd_set_error (bfd_error_invalid_operation);
407 return -1;
410 lsec = bfd_get_section_by_name (abfd, ".loader");
411 if (lsec == NULL)
413 bfd_set_error (bfd_error_no_symbols);
414 return -1;
417 if (! xcoff_get_section_contents (abfd, lsec))
418 return -1;
419 contents = coff_section_data (abfd, lsec)->contents;
421 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
423 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
424 if (relbuf == NULL)
425 return -1;
427 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
429 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
430 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
431 prelocs++)
433 struct internal_ldrel ldrel;
435 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
437 if (ldrel.l_symndx >= 3)
438 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
439 else
441 const char *name;
442 asection *sec;
444 switch (ldrel.l_symndx)
446 case 0:
447 name = ".text";
448 break;
449 case 1:
450 name = ".data";
451 break;
452 case 2:
453 name = ".bss";
454 break;
455 default:
456 abort ();
457 break;
460 sec = bfd_get_section_by_name (abfd, name);
461 if (sec == NULL)
463 bfd_set_error (bfd_error_bad_value);
464 return -1;
467 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
470 relbuf->address = ldrel.l_vaddr;
471 relbuf->addend = 0;
473 /* Most dynamic relocs have the same type. FIXME: This is only
474 correct if ldrel.l_rtype == 0. In other cases, we should use
475 a different howto. */
476 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
478 /* FIXME: We have no way to record the l_rsecnm field. */
480 *prelocs = relbuf;
483 *prelocs = NULL;
485 return ldhdr.l_nreloc;
488 /* Hash functions for xcoff_link_hash_table's archive_info. */
490 static hashval_t
491 xcoff_archive_info_hash (const void *data)
493 const struct xcoff_archive_info *info;
495 info = (const struct xcoff_archive_info *) data;
496 return htab_hash_pointer (info->archive);
499 static int
500 xcoff_archive_info_eq (const void *data1, const void *data2)
502 const struct xcoff_archive_info *info1;
503 const struct xcoff_archive_info *info2;
505 info1 = (const struct xcoff_archive_info *) data1;
506 info2 = (const struct xcoff_archive_info *) data2;
507 return info1->archive == info2->archive;
510 /* Return information about archive ARCHIVE. Return NULL on error. */
512 static struct xcoff_archive_info *
513 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
515 struct xcoff_link_hash_table *htab;
516 struct xcoff_archive_info *entryp, entry;
517 void **slot;
519 htab = xcoff_hash_table (info);
520 entry.archive = archive;
521 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
522 if (!slot)
523 return NULL;
525 entryp = *slot;
526 if (!entryp)
528 entryp = bfd_zalloc (info->output_bfd, sizeof (entry));
529 if (!entryp)
530 return NULL;
532 entryp->archive = archive;
533 *slot = entryp;
535 return entryp;
538 /* Routine to create an entry in an XCOFF link hash table. */
540 static struct bfd_hash_entry *
541 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
542 struct bfd_hash_table *table,
543 const char *string)
545 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
547 /* Allocate the structure if it has not already been allocated by a
548 subclass. */
549 if (ret == NULL)
550 ret = bfd_hash_allocate (table, sizeof (* ret));
551 if (ret == NULL)
552 return NULL;
554 /* Call the allocation method of the superclass. */
555 ret = ((struct xcoff_link_hash_entry *)
556 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
557 table, string));
558 if (ret != NULL)
560 /* Set local fields. */
561 ret->indx = -1;
562 ret->toc_section = NULL;
563 ret->u.toc_indx = -1;
564 ret->descriptor = NULL;
565 ret->ldsym = NULL;
566 ret->ldindx = -1;
567 ret->flags = 0;
568 ret->smclas = XMC_UA;
571 return (struct bfd_hash_entry *) ret;
574 /* Destroy an XCOFF link hash table. */
576 static void
577 _bfd_xcoff_bfd_link_hash_table_free (bfd *obfd)
579 struct xcoff_link_hash_table *ret;
581 ret = (struct xcoff_link_hash_table *) obfd->link.hash;
582 if (ret->archive_info)
583 htab_delete (ret->archive_info);
584 if (ret->debug_strtab)
585 _bfd_stringtab_free (ret->debug_strtab);
586 _bfd_generic_link_hash_table_free (obfd);
589 /* Create an XCOFF link hash table. */
591 struct bfd_link_hash_table *
592 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
594 struct xcoff_link_hash_table *ret;
595 size_t amt = sizeof (* ret);
597 ret = bfd_zmalloc (amt);
598 if (ret == NULL)
599 return NULL;
600 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
601 sizeof (struct xcoff_link_hash_entry)))
603 free (ret);
604 return NULL;
607 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
608 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
609 xcoff_archive_info_eq, NULL);
610 if (!ret->debug_strtab || !ret->archive_info)
612 _bfd_xcoff_bfd_link_hash_table_free (abfd);
613 return NULL;
615 ret->root.hash_table_free = _bfd_xcoff_bfd_link_hash_table_free;
617 /* The linker will always generate a full a.out header. We need to
618 record that fact now, before the sizeof_headers routine could be
619 called. */
620 xcoff_data (abfd)->full_aouthdr = TRUE;
622 return &ret->root;
625 /* Read internal relocs for an XCOFF csect. This is a wrapper around
626 _bfd_coff_read_internal_relocs which tries to take advantage of any
627 relocs which may have been cached for the enclosing section. */
629 static struct internal_reloc *
630 xcoff_read_internal_relocs (bfd *abfd,
631 asection *sec,
632 bfd_boolean cache,
633 bfd_byte *external_relocs,
634 bfd_boolean require_internal,
635 struct internal_reloc *internal_relocs)
637 if (coff_section_data (abfd, sec) != NULL
638 && coff_section_data (abfd, sec)->relocs == NULL
639 && xcoff_section_data (abfd, sec) != NULL)
641 asection *enclosing;
643 enclosing = xcoff_section_data (abfd, sec)->enclosing;
645 if (enclosing != NULL
646 && (coff_section_data (abfd, enclosing) == NULL
647 || coff_section_data (abfd, enclosing)->relocs == NULL)
648 && cache
649 && enclosing->reloc_count > 0)
651 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
652 external_relocs, FALSE, NULL)
653 == NULL)
654 return NULL;
657 if (enclosing != NULL
658 && coff_section_data (abfd, enclosing) != NULL
659 && coff_section_data (abfd, enclosing)->relocs != NULL)
661 size_t off;
663 off = ((sec->rel_filepos - enclosing->rel_filepos)
664 / bfd_coff_relsz (abfd));
666 if (! require_internal)
667 return coff_section_data (abfd, enclosing)->relocs + off;
668 memcpy (internal_relocs,
669 coff_section_data (abfd, enclosing)->relocs + off,
670 sec->reloc_count * sizeof (struct internal_reloc));
671 return internal_relocs;
675 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
676 require_internal, internal_relocs);
679 /* Split FILENAME into an import path and an import filename,
680 storing them in *IMPPATH and *IMPFILE respectively. */
682 bfd_boolean
683 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
684 const char **imppath, const char **impfile)
686 const char *base;
687 size_t length;
688 char *path;
690 base = lbasename (filename);
691 length = base - filename;
692 if (length == 0)
693 /* The filename has no directory component, so use an empty path. */
694 *imppath = "";
695 else if (length == 1)
696 /* The filename is in the root directory. */
697 *imppath = "/";
698 else
700 /* Extract the (non-empty) directory part. Note that we don't
701 need to strip duplicate directory separators from any part
702 of the string; the native linker doesn't do that either. */
703 path = bfd_alloc (abfd, length);
704 if (path == NULL)
705 return FALSE;
706 memcpy (path, filename, length - 1);
707 path[length - 1] = 0;
708 *imppath = path;
710 *impfile = base;
711 return TRUE;
714 /* Set ARCHIVE's import path as though its filename had been given
715 as FILENAME. */
717 bfd_boolean
718 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
719 bfd *archive, const char *filename)
721 struct xcoff_archive_info *archive_info;
723 archive_info = xcoff_get_archive_info (info, archive);
724 return (archive_info != NULL
725 && bfd_xcoff_split_import_path (archive, filename,
726 &archive_info->imppath,
727 &archive_info->impfile));
730 /* H is an imported symbol. Set the import module's path, file and member
731 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
732 no specific import module is specified. */
734 static bfd_boolean
735 xcoff_set_import_path (struct bfd_link_info *info,
736 struct xcoff_link_hash_entry *h,
737 const char *imppath, const char *impfile,
738 const char *impmember)
740 unsigned int c;
741 struct xcoff_import_file **pp;
743 /* We overload the ldindx field to hold the l_ifile value for this
744 symbol. */
745 BFD_ASSERT (h->ldsym == NULL);
746 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
747 if (imppath == NULL)
748 h->ldindx = -1;
749 else
751 /* We start c at 1 because the first entry in the import list is
752 reserved for the library search path. */
753 for (pp = &xcoff_hash_table (info)->imports, c = 1;
754 *pp != NULL;
755 pp = &(*pp)->next, ++c)
757 if (filename_cmp ((*pp)->path, imppath) == 0
758 && filename_cmp ((*pp)->file, impfile) == 0
759 && filename_cmp ((*pp)->member, impmember) == 0)
760 break;
763 if (*pp == NULL)
765 struct xcoff_import_file *n;
766 size_t amt = sizeof (*n);
768 n = bfd_alloc (info->output_bfd, amt);
769 if (n == NULL)
770 return FALSE;
771 n->next = NULL;
772 n->path = imppath;
773 n->file = impfile;
774 n->member = impmember;
775 *pp = n;
777 h->ldindx = c;
779 return TRUE;
782 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
783 Return true if LDSYM defines H. */
785 static bfd_boolean
786 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
787 struct internal_ldsym *ldsym)
789 /* If we didn't know about H before processing LDSYM, LDSYM
790 definitely defines H. */
791 if (h->root.type == bfd_link_hash_new)
792 return TRUE;
794 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
795 dynamic symbol, LDSYM trumps the current definition of H. */
796 if ((ldsym->l_smtype & L_WEAK) == 0
797 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
798 && (h->flags & XCOFF_DEF_REGULAR) == 0
799 && (h->root.type == bfd_link_hash_defweak
800 || h->root.type == bfd_link_hash_undefweak))
801 return TRUE;
803 /* If H is currently undefined, LDSYM defines it. */
804 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
805 && (h->root.type == bfd_link_hash_undefined
806 || h->root.type == bfd_link_hash_undefweak))
807 return TRUE;
809 return FALSE;
812 /* This function is used to add symbols from a dynamic object to the
813 global symbol table. */
815 static bfd_boolean
816 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
818 asection *lsec;
819 bfd_byte *contents;
820 struct internal_ldhdr ldhdr;
821 const char *strings;
822 bfd_byte *elsym, *elsymend;
823 struct xcoff_import_file *n;
824 unsigned int c;
825 struct xcoff_import_file **pp;
827 /* We can only handle a dynamic object if we are generating an XCOFF
828 output file. */
829 if (info->output_bfd->xvec != abfd->xvec)
831 _bfd_error_handler
832 (_("%pB: XCOFF shared object when not producing XCOFF output"),
833 abfd);
834 bfd_set_error (bfd_error_invalid_operation);
835 return FALSE;
838 /* The symbols we use from a dynamic object are not the symbols in
839 the normal symbol table, but, rather, the symbols in the export
840 table. If there is a global symbol in a dynamic object which is
841 not in the export table, the loader will not be able to find it,
842 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
843 libc.a has symbols in the export table which are not in the
844 symbol table. */
846 /* Read in the .loader section. FIXME: We should really use the
847 o_snloader field in the a.out header, rather than grabbing the
848 section by name. */
849 lsec = bfd_get_section_by_name (abfd, ".loader");
850 if (lsec == NULL)
852 _bfd_error_handler
853 (_("%pB: dynamic object with no .loader section"),
854 abfd);
855 bfd_set_error (bfd_error_no_symbols);
856 return FALSE;
859 if (! xcoff_get_section_contents (abfd, lsec))
860 return FALSE;
861 contents = coff_section_data (abfd, lsec)->contents;
863 /* Remove the sections from this object, so that they do not get
864 included in the link. */
865 bfd_section_list_clear (abfd);
867 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
869 strings = (char *) contents + ldhdr.l_stoff;
871 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
873 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
875 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
877 struct internal_ldsym ldsym;
878 char nambuf[SYMNMLEN + 1];
879 const char *name;
880 struct xcoff_link_hash_entry *h;
882 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
884 /* We are only interested in exported symbols. */
885 if ((ldsym.l_smtype & L_EXPORT) == 0)
886 continue;
888 if (ldsym._l._l_l._l_zeroes == 0)
889 name = strings + ldsym._l._l_l._l_offset;
890 else
892 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
893 nambuf[SYMNMLEN] = '\0';
894 name = nambuf;
897 /* Normally we could not call xcoff_link_hash_lookup in an add
898 symbols routine, since we might not be using an XCOFF hash
899 table. However, we verified above that we are using an XCOFF
900 hash table. */
902 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
903 TRUE, TRUE);
904 if (h == NULL)
905 return FALSE;
907 if (!xcoff_dynamic_definition_p (h, &ldsym))
908 continue;
910 h->flags |= XCOFF_DEF_DYNAMIC;
911 h->smclas = ldsym.l_smclas;
912 if (h->smclas == XMC_XO)
914 /* This symbol has an absolute value. */
915 if ((ldsym.l_smtype & L_WEAK) != 0)
916 h->root.type = bfd_link_hash_defweak;
917 else
918 h->root.type = bfd_link_hash_defined;
919 h->root.u.def.section = bfd_abs_section_ptr;
920 h->root.u.def.value = ldsym.l_value;
922 else
924 /* Otherwise, we don't bother to actually define the symbol,
925 since we don't have a section to put it in anyhow.
926 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
927 should be imported from the symbol's undef.abfd. */
928 if ((ldsym.l_smtype & L_WEAK) != 0)
929 h->root.type = bfd_link_hash_undefweak;
930 else
931 h->root.type = bfd_link_hash_undefined;
932 h->root.u.undef.abfd = abfd;
935 /* If this symbol defines a function descriptor, then it
936 implicitly defines the function code as well. */
937 if (h->smclas == XMC_DS
938 || (h->smclas == XMC_XO && name[0] != '.'))
939 h->flags |= XCOFF_DESCRIPTOR;
940 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
942 struct xcoff_link_hash_entry *hds;
944 hds = h->descriptor;
945 if (hds == NULL)
947 char *dsnm;
949 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
950 if (dsnm == NULL)
951 return FALSE;
952 dsnm[0] = '.';
953 strcpy (dsnm + 1, name);
954 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
955 TRUE, TRUE, TRUE);
956 free (dsnm);
957 if (hds == NULL)
958 return FALSE;
960 hds->descriptor = h;
961 h->descriptor = hds;
964 if (xcoff_dynamic_definition_p (hds, &ldsym))
966 hds->root.type = h->root.type;
967 hds->flags |= XCOFF_DEF_DYNAMIC;
968 if (h->smclas == XMC_XO)
970 /* An absolute symbol appears to actually define code, not a
971 function descriptor. This is how some math functions are
972 implemented on AIX 4.1. */
973 hds->smclas = XMC_XO;
974 hds->root.u.def.section = bfd_abs_section_ptr;
975 hds->root.u.def.value = ldsym.l_value;
977 else
979 hds->smclas = XMC_PR;
980 hds->root.u.undef.abfd = abfd;
981 /* We do not want to add this to the undefined
982 symbol list. */
988 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
990 free (coff_section_data (abfd, lsec)->contents);
991 coff_section_data (abfd, lsec)->contents = NULL;
994 /* Record this file in the import files. */
995 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
996 if (n == NULL)
997 return FALSE;
998 n->next = NULL;
1000 if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
1002 if (!bfd_xcoff_split_import_path (abfd, bfd_get_filename (abfd),
1003 &n->path, &n->file))
1004 return FALSE;
1005 n->member = "";
1007 else
1009 struct xcoff_archive_info *archive_info;
1011 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1012 if (!archive_info->impfile)
1014 if (!bfd_xcoff_split_import_path (archive_info->archive,
1015 bfd_get_filename (archive_info
1016 ->archive),
1017 &archive_info->imppath,
1018 &archive_info->impfile))
1019 return FALSE;
1021 n->path = archive_info->imppath;
1022 n->file = archive_info->impfile;
1023 n->member = bfd_get_filename (abfd);
1026 /* We start c at 1 because the first import file number is reserved
1027 for LIBPATH. */
1028 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1029 *pp != NULL;
1030 pp = &(*pp)->next, ++c)
1032 *pp = n;
1034 xcoff_data (abfd)->import_file_id = c;
1036 return TRUE;
1039 /* xcoff_link_create_extra_sections
1041 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1043 static bfd_boolean
1044 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1046 bfd_boolean return_value = FALSE;
1048 if (info->output_bfd->xvec == abfd->xvec)
1050 /* We need to build a .loader section, so we do it here. This
1051 won't work if we're producing an XCOFF output file with no
1052 XCOFF input files. FIXME. */
1054 if (!bfd_link_relocatable (info)
1055 && xcoff_hash_table (info)->loader_section == NULL)
1057 asection *lsec;
1058 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1060 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1061 if (lsec == NULL)
1062 goto end_return;
1064 xcoff_hash_table (info)->loader_section = lsec;
1067 /* Likewise for the linkage section. */
1068 if (xcoff_hash_table (info)->linkage_section == NULL)
1070 asection *lsec;
1071 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1072 | SEC_IN_MEMORY);
1074 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1075 if (lsec == NULL)
1076 goto end_return;
1078 xcoff_hash_table (info)->linkage_section = lsec;
1079 lsec->alignment_power = 2;
1082 /* Likewise for the TOC section. */
1083 if (xcoff_hash_table (info)->toc_section == NULL)
1085 asection *tsec;
1086 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1087 | SEC_IN_MEMORY);
1089 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1090 if (tsec == NULL)
1091 goto end_return;
1093 xcoff_hash_table (info)->toc_section = tsec;
1094 tsec->alignment_power = 2;
1097 /* Likewise for the descriptor section. */
1098 if (xcoff_hash_table (info)->descriptor_section == NULL)
1100 asection *dsec;
1101 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1102 | SEC_IN_MEMORY);
1104 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1105 if (dsec == NULL)
1106 goto end_return;
1108 xcoff_hash_table (info)->descriptor_section = dsec;
1109 dsec->alignment_power = 2;
1112 /* Likewise for the .debug section. */
1113 if (xcoff_hash_table (info)->debug_section == NULL
1114 && info->strip != strip_all)
1116 asection *dsec;
1117 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1119 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1120 if (dsec == NULL)
1121 goto end_return;
1123 xcoff_hash_table (info)->debug_section = dsec;
1127 return_value = TRUE;
1129 end_return:
1131 return return_value;
1134 /* Returns the index of reloc in RELOCS with the least address greater
1135 than or equal to ADDRESS. The relocs are sorted by address. */
1137 static bfd_size_type
1138 xcoff_find_reloc (struct internal_reloc *relocs,
1139 bfd_size_type count,
1140 bfd_vma address)
1142 bfd_size_type min, max, this;
1144 if (count < 2)
1146 if (count == 1 && relocs[0].r_vaddr < address)
1147 return 1;
1148 else
1149 return 0;
1152 min = 0;
1153 max = count;
1155 /* Do a binary search over (min,max]. */
1156 while (min + 1 < max)
1158 bfd_vma raddr;
1160 this = (max + min) / 2;
1161 raddr = relocs[this].r_vaddr;
1162 if (raddr > address)
1163 max = this;
1164 else if (raddr < address)
1165 min = this;
1166 else
1168 min = this;
1169 break;
1173 if (relocs[min].r_vaddr < address)
1174 return min + 1;
1176 while (min > 0
1177 && relocs[min - 1].r_vaddr == address)
1178 --min;
1180 return min;
1183 /* Add all the symbols from an object file to the hash table.
1185 XCOFF is a weird format. A normal XCOFF .o files will have three
1186 COFF sections--.text, .data, and .bss--but each COFF section will
1187 contain many csects. These csects are described in the symbol
1188 table. From the linker's point of view, each csect must be
1189 considered a section in its own right. For example, a TOC entry is
1190 handled as a small XMC_TC csect. The linker must be able to merge
1191 different TOC entries together, which means that it must be able to
1192 extract the XMC_TC csects from the .data section of the input .o
1193 file.
1195 From the point of view of our linker, this is, of course, a hideous
1196 nightmare. We cope by actually creating sections for each csect,
1197 and discarding the original sections. We then have to handle the
1198 relocation entries carefully, since the only way to tell which
1199 csect they belong to is to examine the address. */
1201 static bfd_boolean
1202 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1204 unsigned int n_tmask;
1205 unsigned int n_btshft;
1206 bfd_boolean default_copy;
1207 bfd_size_type symcount;
1208 struct xcoff_link_hash_entry **sym_hash;
1209 asection **csect_cache;
1210 unsigned int *lineno_counts;
1211 bfd_size_type linesz;
1212 asection *o;
1213 asection *last_real;
1214 bfd_boolean keep_syms;
1215 asection *csect;
1216 unsigned int csect_index;
1217 asection *first_csect;
1218 bfd_size_type symesz;
1219 bfd_byte *esym;
1220 bfd_byte *esym_end;
1221 struct reloc_info_struct
1223 struct internal_reloc *relocs;
1224 asection **csects;
1225 bfd_byte *linenos;
1226 } *reloc_info = NULL;
1227 bfd_size_type amt;
1229 keep_syms = obj_coff_keep_syms (abfd);
1231 if ((abfd->flags & DYNAMIC) != 0
1232 && ! info->static_link)
1234 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1235 return FALSE;
1238 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1239 if (! xcoff_link_create_extra_sections (abfd, info))
1240 goto error_return;
1242 if ((abfd->flags & DYNAMIC) != 0
1243 && ! info->static_link)
1244 return TRUE;
1246 n_tmask = coff_data (abfd)->local_n_tmask;
1247 n_btshft = coff_data (abfd)->local_n_btshft;
1249 /* Define macros so that ISFCN, et. al., macros work correctly. */
1250 #define N_TMASK n_tmask
1251 #define N_BTSHFT n_btshft
1253 if (info->keep_memory)
1254 default_copy = FALSE;
1255 else
1256 default_copy = TRUE;
1258 symcount = obj_raw_syment_count (abfd);
1260 /* We keep a list of the linker hash table entries that correspond
1261 to each external symbol. */
1262 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1263 sym_hash = bfd_zalloc (abfd, amt);
1264 if (sym_hash == NULL && symcount != 0)
1265 goto error_return;
1266 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1268 /* Because of the weird stuff we are doing with XCOFF csects, we can
1269 not easily determine which section a symbol is in, so we store
1270 the information in the tdata for the input file. */
1271 amt = symcount * sizeof (asection *);
1272 csect_cache = bfd_zalloc (abfd, amt);
1273 if (csect_cache == NULL && symcount != 0)
1274 goto error_return;
1275 xcoff_data (abfd)->csects = csect_cache;
1277 /* We garbage-collect line-number information on a symbol-by-symbol
1278 basis, so we need to have quick access to the number of entries
1279 per symbol. */
1280 amt = symcount * sizeof (unsigned int);
1281 lineno_counts = bfd_zalloc (abfd, amt);
1282 if (lineno_counts == NULL && symcount != 0)
1283 goto error_return;
1284 xcoff_data (abfd)->lineno_counts = lineno_counts;
1286 /* While splitting sections into csects, we need to assign the
1287 relocs correctly. The relocs and the csects must both be in
1288 order by VMA within a given section, so we handle this by
1289 scanning along the relocs as we process the csects. We index
1290 into reloc_info using the section target_index. */
1291 amt = abfd->section_count + 1;
1292 amt *= sizeof (struct reloc_info_struct);
1293 reloc_info = bfd_zmalloc (amt);
1294 if (reloc_info == NULL)
1295 goto error_return;
1297 /* Read in the relocs and line numbers for each section. */
1298 linesz = bfd_coff_linesz (abfd);
1299 last_real = NULL;
1300 for (o = abfd->sections; o != NULL; o = o->next)
1302 last_real = o;
1304 if ((o->flags & SEC_RELOC) != 0)
1306 reloc_info[o->target_index].relocs =
1307 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1308 amt = o->reloc_count;
1309 amt *= sizeof (asection *);
1310 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1311 if (reloc_info[o->target_index].csects == NULL)
1312 goto error_return;
1315 if ((info->strip == strip_none || info->strip == strip_some)
1316 && o->lineno_count > 0)
1318 bfd_byte *linenos;
1320 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0)
1321 goto error_return;
1322 if (_bfd_mul_overflow (linesz, o->lineno_count, &amt))
1324 bfd_set_error (bfd_error_file_too_big);
1325 goto error_return;
1327 linenos = _bfd_malloc_and_read (abfd, amt, amt);
1328 if (linenos == NULL)
1329 goto error_return;
1330 reloc_info[o->target_index].linenos = linenos;
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 || sym.n_sclass == C_DWARF)
1369 csect = NULL;
1370 if (csect != NULL)
1371 *csect_cache = csect;
1372 else if (first_csect == NULL
1373 || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1374 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1375 else
1376 *csect_cache = NULL;
1377 esym += (sym.n_numaux + 1) * symesz;
1378 sym_hash += sym.n_numaux + 1;
1379 csect_cache += sym.n_numaux + 1;
1380 lineno_counts += sym.n_numaux + 1;
1382 continue;
1385 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1387 if (name == NULL)
1388 goto error_return;
1390 /* If this symbol has line number information attached to it,
1391 and we're not stripping it, count the number of entries and
1392 add them to the count for this csect. In the final link pass
1393 we are going to attach line number information by symbol,
1394 rather than by section, in order to more easily handle
1395 garbage collection. */
1396 if ((info->strip == strip_none || info->strip == strip_some)
1397 && sym.n_numaux > 1
1398 && csect != NULL
1399 && ISFCN (sym.n_type))
1401 union internal_auxent auxlin;
1403 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1404 sym.n_type, sym.n_sclass,
1405 0, sym.n_numaux, (void *) &auxlin);
1407 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1409 asection *enclosing;
1410 bfd_signed_vma linoff;
1412 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1413 if (enclosing == NULL)
1415 _bfd_error_handler
1416 /* xgettext:c-format */
1417 (_("%pB: `%s' has line numbers but no enclosing section"),
1418 abfd, name);
1419 bfd_set_error (bfd_error_bad_value);
1420 goto error_return;
1422 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1423 - enclosing->line_filepos);
1424 /* Explicit cast to bfd_signed_vma for compiler. */
1425 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1427 struct internal_lineno lin;
1428 bfd_byte *linpstart;
1430 linpstart = (reloc_info[enclosing->target_index].linenos
1431 + linoff);
1432 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1433 if (lin.l_lnno == 0
1434 && ((bfd_size_type) lin.l_addr.l_symndx
1435 == ((esym
1436 - (bfd_byte *) obj_coff_external_syms (abfd))
1437 / symesz)))
1439 bfd_byte *linpend, *linp;
1441 linpend = (reloc_info[enclosing->target_index].linenos
1442 + enclosing->lineno_count * linesz);
1443 for (linp = linpstart + linesz;
1444 linp < linpend;
1445 linp += linesz)
1447 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1448 (void *) &lin);
1449 if (lin.l_lnno == 0)
1450 break;
1452 *lineno_counts = (linp - linpstart) / linesz;
1453 /* The setting of line_filepos will only be
1454 useful if all the line number entries for a
1455 csect are contiguous; this only matters for
1456 error reporting. */
1457 if (csect->line_filepos == 0)
1458 csect->line_filepos =
1459 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1465 /* Pick up the csect auxiliary information. */
1466 if (sym.n_numaux == 0)
1468 _bfd_error_handler
1469 /* xgettext:c-format */
1470 (_("%pB: class %d symbol `%s' has no aux entries"),
1471 abfd, sym.n_sclass, name);
1472 bfd_set_error (bfd_error_bad_value);
1473 goto error_return;
1476 bfd_coff_swap_aux_in (abfd,
1477 (void *) (esym + symesz * sym.n_numaux),
1478 sym.n_type, sym.n_sclass,
1479 sym.n_numaux - 1, sym.n_numaux,
1480 (void *) &aux);
1482 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1484 section = NULL;
1485 value = 0;
1486 set_toc = NULL;
1488 switch (smtyp)
1490 default:
1491 _bfd_error_handler
1492 /* xgettext:c-format */
1493 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1494 abfd, name, smtyp);
1495 bfd_set_error (bfd_error_bad_value);
1496 goto error_return;
1498 case XTY_ER:
1499 /* This is an external reference. */
1500 if (sym.n_sclass == C_HIDEXT
1501 || sym.n_scnum != N_UNDEF
1502 || aux.x_csect.x_scnlen.l != 0)
1504 _bfd_error_handler
1505 /* xgettext:c-format */
1506 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1507 "scnlen %" PRId64),
1508 abfd, name, sym.n_sclass, sym.n_scnum,
1509 (int64_t) aux.x_csect.x_scnlen.l);
1510 bfd_set_error (bfd_error_bad_value);
1511 goto error_return;
1514 /* An XMC_XO external reference is actually a reference to
1515 an absolute location. */
1516 if (aux.x_csect.x_smclas != XMC_XO)
1517 section = bfd_und_section_ptr;
1518 else
1520 section = bfd_abs_section_ptr;
1521 value = sym.n_value;
1523 break;
1525 case XTY_SD:
1526 csect = NULL;
1527 csect_index = -(unsigned) 1;
1529 /* When we see a TOC anchor, we record the TOC value. */
1530 if (aux.x_csect.x_smclas == XMC_TC0)
1532 if (sym.n_sclass != C_HIDEXT
1533 || aux.x_csect.x_scnlen.l != 0)
1535 _bfd_error_handler
1536 /* xgettext:c-format */
1537 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64),
1538 abfd, name, sym.n_sclass, (int64_t) aux.x_csect.x_scnlen.l);
1539 bfd_set_error (bfd_error_bad_value);
1540 goto error_return;
1542 xcoff_data (abfd)->toc = sym.n_value;
1545 /* We must merge TOC entries for the same symbol. We can
1546 merge two TOC entries if they are both C_HIDEXT, they
1547 both have the same name, they are both 4 or 8 bytes long, and
1548 they both have a relocation table entry for an external
1549 symbol with the same name. Unfortunately, this means
1550 that we must look through the relocations. Ick.
1552 Logic for 32 bit vs 64 bit.
1553 32 bit has a csect length of 4 for TOC
1554 64 bit has a csect length of 8 for TOC
1556 The conditions to get past the if-check are not that bad.
1557 They are what is used to create the TOC csects in the first
1558 place. */
1559 if (aux.x_csect.x_smclas == XMC_TC
1560 && sym.n_sclass == C_HIDEXT
1561 && info->output_bfd->xvec == abfd->xvec
1562 && ((bfd_xcoff_is_xcoff32 (abfd)
1563 && aux.x_csect.x_scnlen.l == 4)
1564 || (bfd_xcoff_is_xcoff64 (abfd)
1565 && aux.x_csect.x_scnlen.l == 8)))
1567 asection *enclosing;
1568 struct internal_reloc *relocs;
1569 bfd_size_type relindx;
1570 struct internal_reloc *rel;
1572 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1573 if (enclosing == NULL)
1574 goto error_return;
1576 relocs = reloc_info[enclosing->target_index].relocs;
1577 amt = enclosing->reloc_count;
1578 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1579 rel = relocs + relindx;
1581 /* 32 bit R_POS r_size is 31
1582 64 bit R_POS r_size is 63 */
1583 if (relindx < enclosing->reloc_count
1584 && rel->r_vaddr == (bfd_vma) sym.n_value
1585 && rel->r_type == R_POS
1586 && ((bfd_xcoff_is_xcoff32 (abfd)
1587 && rel->r_size == 31)
1588 || (bfd_xcoff_is_xcoff64 (abfd)
1589 && rel->r_size == 63)))
1591 bfd_byte *erelsym;
1593 struct internal_syment relsym;
1595 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1596 + rel->r_symndx * symesz);
1597 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1598 if (EXTERN_SYM_P (relsym.n_sclass))
1600 const char *relname;
1601 char relbuf[SYMNMLEN + 1];
1602 bfd_boolean copy;
1603 struct xcoff_link_hash_entry *h;
1605 /* At this point we know that the TOC entry is
1606 for an externally visible symbol. */
1607 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1608 relbuf);
1609 if (relname == NULL)
1610 goto error_return;
1612 /* We only merge TOC entries if the TC name is
1613 the same as the symbol name. This handles
1614 the normal case, but not common cases like
1615 SYM.P4 which gcc generates to store SYM + 4
1616 in the TOC. FIXME. */
1617 if (strcmp (name, relname) == 0)
1619 copy = (! info->keep_memory
1620 || relsym._n._n_n._n_zeroes != 0
1621 || relsym._n._n_n._n_offset == 0);
1622 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1623 relname, TRUE, copy,
1624 FALSE);
1625 if (h == NULL)
1626 goto error_return;
1628 /* At this point h->root.type could be
1629 bfd_link_hash_new. That should be OK,
1630 since we know for sure that we will come
1631 across this symbol as we step through the
1632 file. */
1634 /* We store h in *sym_hash for the
1635 convenience of the relocate_section
1636 function. */
1637 *sym_hash = h;
1639 if (h->toc_section != NULL)
1641 asection **rel_csects;
1643 /* We already have a TOC entry for this
1644 symbol, so we can just ignore this
1645 one. */
1646 rel_csects =
1647 reloc_info[enclosing->target_index].csects;
1648 rel_csects[relindx] = bfd_und_section_ptr;
1649 break;
1652 /* We are about to create a TOC entry for
1653 this symbol. */
1654 set_toc = h;
1661 asection *enclosing;
1663 /* We need to create a new section. We get the name from
1664 the csect storage mapping class, so that the linker can
1665 accumulate similar csects together. */
1667 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1668 if (NULL == csect)
1669 goto error_return;
1671 /* The enclosing section is the main section : .data, .text
1672 or .bss that the csect is coming from. */
1673 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1674 if (enclosing == NULL)
1675 goto error_return;
1677 if (! bfd_is_abs_section (enclosing)
1678 && ((bfd_vma) sym.n_value < enclosing->vma
1679 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1680 > enclosing->vma + enclosing->size)))
1682 _bfd_error_handler
1683 /* xgettext:c-format */
1684 (_("%pB: csect `%s' not in enclosing section"),
1685 abfd, name);
1686 bfd_set_error (bfd_error_bad_value);
1687 goto error_return;
1689 csect->vma = sym.n_value;
1690 csect->filepos = (enclosing->filepos
1691 + sym.n_value
1692 - enclosing->vma);
1693 csect->size = aux.x_csect.x_scnlen.l;
1694 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1695 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1697 /* Record the enclosing section in the tdata for this new
1698 section. */
1699 amt = sizeof (struct coff_section_tdata);
1700 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1701 if (csect->used_by_bfd == NULL)
1702 goto error_return;
1703 amt = sizeof (struct xcoff_section_tdata);
1704 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1705 if (coff_section_data (abfd, csect)->tdata == NULL)
1706 goto error_return;
1707 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1708 xcoff_section_data (abfd, csect)->lineno_count =
1709 enclosing->lineno_count;
1711 if (enclosing->owner == abfd)
1713 struct internal_reloc *relocs;
1714 bfd_size_type relindx;
1715 struct internal_reloc *rel;
1716 asection **rel_csect;
1718 relocs = reloc_info[enclosing->target_index].relocs;
1719 amt = enclosing->reloc_count;
1720 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1722 rel = relocs + relindx;
1723 rel_csect = (reloc_info[enclosing->target_index].csects
1724 + relindx);
1726 csect->rel_filepos = (enclosing->rel_filepos
1727 + relindx * bfd_coff_relsz (abfd));
1728 while (relindx < enclosing->reloc_count
1729 && *rel_csect == NULL
1730 && rel->r_vaddr < csect->vma + csect->size)
1733 *rel_csect = csect;
1734 csect->flags |= SEC_RELOC;
1735 ++csect->reloc_count;
1736 ++relindx;
1737 ++rel;
1738 ++rel_csect;
1742 /* There are a number of other fields and section flags
1743 which we do not bother to set. */
1745 csect_index = ((esym
1746 - (bfd_byte *) obj_coff_external_syms (abfd))
1747 / symesz);
1749 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1751 if (first_csect == NULL)
1752 first_csect = csect;
1754 /* If this symbol is external, we treat it as starting at the
1755 beginning of the newly created section. */
1756 if (EXTERN_SYM_P (sym.n_sclass))
1758 section = csect;
1759 value = 0;
1762 /* If this is a TOC section for a symbol, record it. */
1763 if (set_toc != NULL)
1764 set_toc->toc_section = csect;
1766 break;
1768 case XTY_LD:
1769 /* This is a label definition. The x_scnlen field is the
1770 symbol index of the csect. Usually the XTY_LD symbol will
1771 follow its appropriate XTY_SD symbol. The .set pseudo op can
1772 cause the XTY_LD to not follow the XTY_SD symbol. */
1774 bfd_boolean bad;
1776 bad = FALSE;
1777 if (aux.x_csect.x_scnlen.l < 0
1778 || (aux.x_csect.x_scnlen.l
1779 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1780 bad = TRUE;
1781 if (! bad)
1783 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1784 if (section == NULL
1785 || (section->flags & SEC_HAS_CONTENTS) == 0)
1786 bad = TRUE;
1788 if (bad)
1790 _bfd_error_handler
1791 /* xgettext:c-format */
1792 (_("%pB: misplaced XTY_LD `%s'"),
1793 abfd, name);
1794 bfd_set_error (bfd_error_bad_value);
1795 goto error_return;
1797 csect = section;
1798 value = sym.n_value - csect->vma;
1800 break;
1802 case XTY_CM:
1803 /* This is an unitialized csect. We could base the name on
1804 the storage mapping class, but we don't bother except for
1805 an XMC_TD symbol. If this csect is externally visible,
1806 it is a common symbol. We put XMC_TD symbols in sections
1807 named .tocbss, and rely on the linker script to put that
1808 in the TOC area. */
1810 if (aux.x_csect.x_smclas == XMC_TD)
1812 /* The linker script puts the .td section in the data
1813 section after the .tc section. */
1814 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1815 SEC_ALLOC);
1817 else
1818 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1819 SEC_ALLOC);
1821 if (csect == NULL)
1822 goto error_return;
1823 csect->vma = sym.n_value;
1824 csect->size = aux.x_csect.x_scnlen.l;
1825 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1826 /* There are a number of other fields and section flags
1827 which we do not bother to set. */
1829 csect_index = ((esym
1830 - (bfd_byte *) obj_coff_external_syms (abfd))
1831 / symesz);
1833 amt = sizeof (struct coff_section_tdata);
1834 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1835 if (csect->used_by_bfd == NULL)
1836 goto error_return;
1837 amt = sizeof (struct xcoff_section_tdata);
1838 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1839 if (coff_section_data (abfd, csect)->tdata == NULL)
1840 goto error_return;
1841 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1843 if (first_csect == NULL)
1844 first_csect = csect;
1846 if (EXTERN_SYM_P (sym.n_sclass))
1848 csect->flags |= SEC_IS_COMMON;
1849 csect->size = 0;
1850 section = csect;
1851 value = aux.x_csect.x_scnlen.l;
1854 break;
1857 /* Check for magic symbol names. */
1858 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1859 && aux.x_csect.x_smclas != XMC_TC
1860 && aux.x_csect.x_smclas != XMC_TD)
1862 int i = -1;
1864 if (name[0] == '_')
1866 if (strcmp (name, "_text") == 0)
1867 i = XCOFF_SPECIAL_SECTION_TEXT;
1868 else if (strcmp (name, "_etext") == 0)
1869 i = XCOFF_SPECIAL_SECTION_ETEXT;
1870 else if (strcmp (name, "_data") == 0)
1871 i = XCOFF_SPECIAL_SECTION_DATA;
1872 else if (strcmp (name, "_edata") == 0)
1873 i = XCOFF_SPECIAL_SECTION_EDATA;
1874 else if (strcmp (name, "_end") == 0)
1875 i = XCOFF_SPECIAL_SECTION_END;
1877 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1878 i = XCOFF_SPECIAL_SECTION_END2;
1880 if (i != -1)
1881 xcoff_hash_table (info)->special_sections[i] = csect;
1884 /* Now we have enough information to add the symbol to the
1885 linker hash table. */
1887 if (EXTERN_SYM_P (sym.n_sclass))
1889 bfd_boolean copy, ok;
1890 flagword flags;
1892 BFD_ASSERT (section != NULL);
1894 /* We must copy the name into memory if we got it from the
1895 syment itself, rather than the string table. */
1896 copy = default_copy;
1897 if (sym._n._n_n._n_zeroes != 0
1898 || sym._n._n_n._n_offset == 0)
1899 copy = TRUE;
1901 /* Ignore global linkage code when linking statically. */
1902 if (info->static_link
1903 && (smtyp == XTY_SD || smtyp == XTY_LD)
1904 && aux.x_csect.x_smclas == XMC_GL)
1906 section = bfd_und_section_ptr;
1907 value = 0;
1910 /* The AIX linker appears to only detect multiple symbol
1911 definitions when there is a reference to the symbol. If
1912 a symbol is defined multiple times, and the only
1913 references are from the same object file, the AIX linker
1914 appears to permit it. It does not merge the different
1915 definitions, but handles them independently. On the
1916 other hand, if there is a reference, the linker reports
1917 an error.
1919 This matters because the AIX <net/net_globals.h> header
1920 file actually defines an initialized array, so we have to
1921 actually permit that to work.
1923 Just to make matters even more confusing, the AIX linker
1924 appears to permit multiple symbol definitions whenever
1925 the second definition is in an archive rather than an
1926 object file. This may be a consequence of the manner in
1927 which it handles archives: I think it may load the entire
1928 archive in as separate csects, and then let garbage
1929 collection discard symbols.
1931 We also have to handle the case of statically linking a
1932 shared object, which will cause symbol redefinitions,
1933 although this is an easier case to detect. */
1934 else if (info->output_bfd->xvec == abfd->xvec)
1936 if (! bfd_is_und_section (section))
1937 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1938 name, TRUE, copy, FALSE);
1939 else
1940 /* Make a copy of the symbol name to prevent problems with
1941 merging symbols. */
1942 *sym_hash = ((struct xcoff_link_hash_entry *)
1943 bfd_wrapped_link_hash_lookup (abfd, info, name,
1944 TRUE, TRUE, FALSE));
1946 if (*sym_hash == NULL)
1947 goto error_return;
1948 if (((*sym_hash)->root.type == bfd_link_hash_defined
1949 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1950 && ! bfd_is_und_section (section)
1951 && ! bfd_is_com_section (section))
1953 /* This is a second definition of a defined symbol. */
1954 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1955 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1957 /* The existing symbol is from a shared library.
1958 Replace it. */
1959 (*sym_hash)->root.type = bfd_link_hash_undefined;
1960 (*sym_hash)->root.u.undef.abfd =
1961 (*sym_hash)->root.u.def.section->owner;
1963 else if (abfd->my_archive != NULL)
1965 /* This is a redefinition in an object contained
1966 in an archive. Just ignore it. See the
1967 comment above. */
1968 section = bfd_und_section_ptr;
1969 value = 0;
1971 else if (sym.n_sclass == C_AIX_WEAKEXT
1972 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1974 /* At least one of the definitions is weak.
1975 Allow the normal rules to take effect. */
1977 else if ((*sym_hash)->root.u.undef.next != NULL
1978 || info->hash->undefs_tail == &(*sym_hash)->root)
1980 /* This symbol has been referenced. In this
1981 case, we just continue and permit the
1982 multiple definition error. See the comment
1983 above about the behaviour of the AIX linker. */
1985 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1987 /* The symbols are both csects of the same
1988 class. There is at least a chance that this
1989 is a semi-legitimate redefinition. */
1990 section = bfd_und_section_ptr;
1991 value = 0;
1992 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1995 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1996 && (*sym_hash)->root.type == bfd_link_hash_defined
1997 && (bfd_is_und_section (section)
1998 || bfd_is_com_section (section)))
2000 /* This is a reference to a multiply defined symbol.
2001 Report the error now. See the comment above
2002 about the behaviour of the AIX linker. We could
2003 also do this with warning symbols, but I'm not
2004 sure the XCOFF linker is wholly prepared to
2005 handle them, and that would only be a warning,
2006 not an error. */
2007 (*info->callbacks->multiple_definition) (info,
2008 &(*sym_hash)->root,
2009 NULL, NULL,
2010 (bfd_vma) 0);
2011 /* Try not to give this error too many times. */
2012 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2016 /* _bfd_generic_link_add_one_symbol may call the linker to
2017 generate an error message, and the linker may try to read
2018 the symbol table to give a good error. Right now, the
2019 line numbers are in an inconsistent state, since they are
2020 counted both in the real sections and in the new csects.
2021 We need to leave the count in the real sections so that
2022 the linker can report the line number of the error
2023 correctly, so temporarily clobber the link to the csects
2024 so that the linker will not try to read the line numbers
2025 a second time from the csects. */
2026 BFD_ASSERT (last_real->next == first_csect);
2027 last_real->next = NULL;
2028 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2029 ok = (_bfd_generic_link_add_one_symbol
2030 (info, abfd, name, flags, section, value, NULL, copy, TRUE,
2031 (struct bfd_link_hash_entry **) sym_hash));
2032 last_real->next = first_csect;
2033 if (!ok)
2034 goto error_return;
2036 if (smtyp == XTY_CM)
2038 if ((*sym_hash)->root.type != bfd_link_hash_common
2039 || (*sym_hash)->root.u.c.p->section != csect)
2040 /* We don't need the common csect we just created. */
2041 csect->size = 0;
2042 else
2043 (*sym_hash)->root.u.c.p->alignment_power
2044 = csect->alignment_power;
2047 if (info->output_bfd->xvec == abfd->xvec)
2049 int flag;
2051 if (smtyp == XTY_ER
2052 || smtyp == XTY_CM
2053 || section == bfd_und_section_ptr)
2054 flag = XCOFF_REF_REGULAR;
2055 else
2056 flag = XCOFF_DEF_REGULAR;
2057 (*sym_hash)->flags |= flag;
2059 if ((*sym_hash)->smclas == XMC_UA
2060 || flag == XCOFF_DEF_REGULAR)
2061 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2065 if (smtyp == XTY_ER)
2066 *csect_cache = section;
2067 else
2069 *csect_cache = csect;
2070 if (csect != NULL)
2071 xcoff_section_data (abfd, csect)->last_symndx
2072 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2075 esym += (sym.n_numaux + 1) * symesz;
2076 sym_hash += sym.n_numaux + 1;
2077 csect_cache += sym.n_numaux + 1;
2078 lineno_counts += sym.n_numaux + 1;
2081 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2083 /* Make sure that we have seen all the relocs. */
2084 for (o = abfd->sections; o != first_csect; o = o->next)
2086 /* Debugging sections have no csects. */
2087 if (bfd_section_flags (o) & SEC_DEBUGGING)
2088 continue;
2090 /* Reset the section size and the line number count, since the
2091 data is now attached to the csects. Don't reset the size of
2092 the .debug section, since we need to read it below in
2093 bfd_xcoff_size_dynamic_sections. */
2094 if (strcmp (bfd_section_name (o), ".debug") != 0)
2095 o->size = 0;
2096 o->lineno_count = 0;
2098 if ((o->flags & SEC_RELOC) != 0)
2100 bfd_size_type i;
2101 struct internal_reloc *rel;
2102 asection **rel_csect;
2104 rel = reloc_info[o->target_index].relocs;
2105 rel_csect = reloc_info[o->target_index].csects;
2107 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2109 if (*rel_csect == NULL)
2111 _bfd_error_handler
2112 /* xgettext:c-format */
2113 (_("%pB: reloc %s:%" PRId64 " not in csect"),
2114 abfd, o->name, (int64_t) i);
2115 bfd_set_error (bfd_error_bad_value);
2116 goto error_return;
2119 /* We identify all function symbols that are the target
2120 of a relocation, so that we can create glue code for
2121 functions imported from dynamic objects. */
2122 if (info->output_bfd->xvec == abfd->xvec
2123 && *rel_csect != bfd_und_section_ptr
2124 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2126 struct xcoff_link_hash_entry *h;
2128 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2129 /* If the symbol name starts with a period, it is
2130 the code of a function. If the symbol is
2131 currently undefined, then add an undefined symbol
2132 for the function descriptor. This should do no
2133 harm, because any regular object that defines the
2134 function should also define the function
2135 descriptor. It helps, because it means that we
2136 will identify the function descriptor with a
2137 dynamic object if a dynamic object defines it. */
2138 if (h->root.root.string[0] == '.'
2139 && h->descriptor == NULL)
2141 struct xcoff_link_hash_entry *hds;
2142 struct bfd_link_hash_entry *bh;
2144 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2145 h->root.root.string + 1,
2146 TRUE, FALSE, TRUE);
2147 if (hds == NULL)
2148 goto error_return;
2149 if (hds->root.type == bfd_link_hash_new)
2151 bh = &hds->root;
2152 if (! (_bfd_generic_link_add_one_symbol
2153 (info, abfd, hds->root.root.string,
2154 (flagword) 0, bfd_und_section_ptr,
2155 (bfd_vma) 0, NULL, FALSE,
2156 TRUE, &bh)))
2157 goto error_return;
2158 hds = (struct xcoff_link_hash_entry *) bh;
2160 hds->flags |= XCOFF_DESCRIPTOR;
2161 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2162 hds->descriptor = h;
2163 h->descriptor = hds;
2165 if (h->root.root.string[0] == '.')
2166 h->flags |= XCOFF_CALLED;
2170 free (reloc_info[o->target_index].csects);
2171 reloc_info[o->target_index].csects = NULL;
2173 /* Reset SEC_RELOC and the reloc_count, since the reloc
2174 information is now attached to the csects. */
2175 o->flags &=~ SEC_RELOC;
2176 o->reloc_count = 0;
2178 /* If we are not keeping memory, free the reloc information. */
2179 if (! info->keep_memory
2180 && coff_section_data (abfd, o) != NULL
2181 && ! coff_section_data (abfd, o)->keep_relocs)
2183 free (coff_section_data (abfd, o)->relocs);
2184 coff_section_data (abfd, o)->relocs = NULL;
2188 /* Free up the line numbers. FIXME: We could cache these
2189 somewhere for the final link, to avoid reading them again. */
2190 free (reloc_info[o->target_index].linenos);
2191 reloc_info[o->target_index].linenos = NULL;
2194 free (reloc_info);
2196 obj_coff_keep_syms (abfd) = keep_syms;
2198 return TRUE;
2200 error_return:
2201 if (reloc_info != NULL)
2203 for (o = abfd->sections; o != NULL; o = o->next)
2205 free (reloc_info[o->target_index].csects);
2206 free (reloc_info[o->target_index].linenos);
2208 free (reloc_info);
2210 obj_coff_keep_syms (abfd) = keep_syms;
2211 return FALSE;
2214 #undef N_TMASK
2215 #undef N_BTSHFT
2217 /* Add symbols from an XCOFF object file. */
2219 static bfd_boolean
2220 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2222 if (! _bfd_coff_get_external_symbols (abfd))
2223 return FALSE;
2224 if (! xcoff_link_add_symbols (abfd, info))
2225 return FALSE;
2226 if (! info->keep_memory)
2228 if (! _bfd_coff_free_symbols (abfd))
2229 return FALSE;
2231 return TRUE;
2234 /* Look through the loader symbols to see if this dynamic object
2235 should be included in the link. The native linker uses the loader
2236 symbols, not the normal symbol table, so we do too. */
2238 static bfd_boolean
2239 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2240 struct bfd_link_info *info,
2241 bfd_boolean *pneeded,
2242 bfd **subsbfd)
2244 asection *lsec;
2245 bfd_byte *contents;
2246 struct internal_ldhdr ldhdr;
2247 const char *strings;
2248 bfd_byte *elsym, *elsymend;
2250 *pneeded = FALSE;
2252 lsec = bfd_get_section_by_name (abfd, ".loader");
2253 if (lsec == NULL)
2254 /* There are no symbols, so don't try to include it. */
2255 return TRUE;
2257 if (! xcoff_get_section_contents (abfd, lsec))
2258 return FALSE;
2259 contents = coff_section_data (abfd, lsec)->contents;
2261 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2263 strings = (char *) contents + ldhdr.l_stoff;
2265 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2267 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2268 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2270 struct internal_ldsym ldsym;
2271 char nambuf[SYMNMLEN + 1];
2272 const char *name;
2273 struct bfd_link_hash_entry *h;
2275 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2277 /* We are only interested in exported symbols. */
2278 if ((ldsym.l_smtype & L_EXPORT) == 0)
2279 continue;
2281 if (ldsym._l._l_l._l_zeroes == 0)
2282 name = strings + ldsym._l._l_l._l_offset;
2283 else
2285 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2286 nambuf[SYMNMLEN] = '\0';
2287 name = nambuf;
2290 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2292 /* We are only interested in symbols that are currently
2293 undefined. At this point we know that we are using an XCOFF
2294 hash table. */
2295 if (h != NULL
2296 && h->type == bfd_link_hash_undefined
2297 && (((struct xcoff_link_hash_entry *) h)->flags
2298 & XCOFF_DEF_DYNAMIC) == 0)
2300 if (!(*info->callbacks
2301 ->add_archive_element) (info, abfd, name, subsbfd))
2302 continue;
2303 *pneeded = TRUE;
2304 return TRUE;
2308 /* We do not need this shared object. */
2309 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2311 free (coff_section_data (abfd, lsec)->contents);
2312 coff_section_data (abfd, lsec)->contents = NULL;
2315 return TRUE;
2318 /* Look through the symbols to see if this object file should be
2319 included in the link. */
2321 static bfd_boolean
2322 xcoff_link_check_ar_symbols (bfd *abfd,
2323 struct bfd_link_info *info,
2324 bfd_boolean *pneeded,
2325 bfd **subsbfd)
2327 bfd_size_type symesz;
2328 bfd_byte *esym;
2329 bfd_byte *esym_end;
2331 *pneeded = FALSE;
2333 if ((abfd->flags & DYNAMIC) != 0
2334 && ! info->static_link
2335 && info->output_bfd->xvec == abfd->xvec)
2336 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2338 symesz = bfd_coff_symesz (abfd);
2339 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2340 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2341 while (esym < esym_end)
2343 struct internal_syment sym;
2345 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2347 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2349 const char *name;
2350 char buf[SYMNMLEN + 1];
2351 struct bfd_link_hash_entry *h;
2353 /* This symbol is externally visible, and is defined by this
2354 object file. */
2355 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2357 if (name == NULL)
2358 return FALSE;
2359 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2361 /* We are only interested in symbols that are currently
2362 undefined. If a symbol is currently known to be common,
2363 XCOFF linkers do not bring in an object file which
2364 defines it. We also don't bring in symbols to satisfy
2365 undefined references in shared objects. */
2366 if (h != NULL
2367 && h->type == bfd_link_hash_undefined
2368 && (info->output_bfd->xvec != abfd->xvec
2369 || (((struct xcoff_link_hash_entry *) h)->flags
2370 & XCOFF_DEF_DYNAMIC) == 0))
2372 if (!(*info->callbacks
2373 ->add_archive_element) (info, abfd, name, subsbfd))
2374 continue;
2375 *pneeded = TRUE;
2376 return TRUE;
2380 esym += (sym.n_numaux + 1) * symesz;
2383 /* We do not need this object file. */
2384 return TRUE;
2387 /* Check a single archive element to see if we need to include it in
2388 the link. *PNEEDED is set according to whether this element is
2389 needed in the link or not. This is called via
2390 _bfd_generic_link_add_archive_symbols. */
2392 static bfd_boolean
2393 xcoff_link_check_archive_element (bfd *abfd,
2394 struct bfd_link_info *info,
2395 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
2396 const char *name ATTRIBUTE_UNUSED,
2397 bfd_boolean *pneeded)
2399 bfd_boolean keep_syms_p;
2400 bfd *oldbfd;
2402 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2403 if (!_bfd_coff_get_external_symbols (abfd))
2404 return FALSE;
2406 oldbfd = abfd;
2407 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2408 return FALSE;
2410 if (*pneeded)
2412 /* Potentially, the add_archive_element hook may have set a
2413 substitute BFD for us. */
2414 if (abfd != oldbfd)
2416 if (!keep_syms_p
2417 && !_bfd_coff_free_symbols (oldbfd))
2418 return FALSE;
2419 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2420 if (!_bfd_coff_get_external_symbols (abfd))
2421 return FALSE;
2423 if (!xcoff_link_add_symbols (abfd, info))
2424 return FALSE;
2425 if (info->keep_memory)
2426 keep_syms_p = TRUE;
2429 if (!keep_syms_p)
2431 if (!_bfd_coff_free_symbols (abfd))
2432 return FALSE;
2435 return TRUE;
2438 /* Given an XCOFF BFD, add symbols to the global hash table as
2439 appropriate. */
2441 bfd_boolean
2442 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2444 switch (bfd_get_format (abfd))
2446 case bfd_object:
2447 return xcoff_link_add_object_symbols (abfd, info);
2449 case bfd_archive:
2450 /* If the archive has a map, do the usual search. We then need
2451 to check the archive for dynamic objects, because they may not
2452 appear in the archive map even though they should, perhaps, be
2453 included. If the archive has no map, we just consider each object
2454 file in turn, since that apparently is what the AIX native linker
2455 does. */
2456 if (bfd_has_map (abfd))
2458 if (! (_bfd_generic_link_add_archive_symbols
2459 (abfd, info, xcoff_link_check_archive_element)))
2460 return FALSE;
2464 bfd *member;
2466 member = bfd_openr_next_archived_file (abfd, NULL);
2467 while (member != NULL)
2469 if (bfd_check_format (member, bfd_object)
2470 && (info->output_bfd->xvec == member->xvec)
2471 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2473 bfd_boolean needed;
2475 if (! xcoff_link_check_archive_element (member, info,
2476 NULL, NULL, &needed))
2477 return FALSE;
2478 if (needed)
2479 member->archive_pass = -1;
2481 member = bfd_openr_next_archived_file (abfd, member);
2485 return TRUE;
2487 default:
2488 bfd_set_error (bfd_error_wrong_format);
2489 return FALSE;
2493 bfd_boolean
2494 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2495 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2496 struct bfd_link_hash_entry *harg)
2498 struct xcoff_link_hash_entry *h;
2500 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2501 return FALSE;
2503 h = (struct xcoff_link_hash_entry *) harg;
2504 h->flags |= XCOFF_DEF_REGULAR;
2505 return TRUE;
2508 /* If symbol H has not been interpreted as a function descriptor,
2509 see whether it should be. Set up its descriptor information if so. */
2511 static bfd_boolean
2512 xcoff_find_function (struct bfd_link_info *info,
2513 struct xcoff_link_hash_entry *h)
2515 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2516 && h->root.root.string[0] != '.')
2518 char *fnname;
2519 struct xcoff_link_hash_entry *hfn;
2520 size_t amt;
2522 amt = strlen (h->root.root.string) + 2;
2523 fnname = bfd_malloc (amt);
2524 if (fnname == NULL)
2525 return FALSE;
2526 fnname[0] = '.';
2527 strcpy (fnname + 1, h->root.root.string);
2528 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2529 fnname, FALSE, FALSE, TRUE);
2530 free (fnname);
2531 if (hfn != NULL
2532 && hfn->smclas == XMC_PR
2533 && (hfn->root.type == bfd_link_hash_defined
2534 || hfn->root.type == bfd_link_hash_defweak))
2536 h->flags |= XCOFF_DESCRIPTOR;
2537 h->descriptor = hfn;
2538 hfn->descriptor = h;
2541 return TRUE;
2544 /* Return true if the given bfd contains at least one shared object. */
2546 static bfd_boolean
2547 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2548 bfd *archive)
2550 struct xcoff_archive_info *archive_info;
2551 bfd *member;
2553 archive_info = xcoff_get_archive_info (info, archive);
2554 if (!archive_info->know_contains_shared_object_p)
2556 member = bfd_openr_next_archived_file (archive, NULL);
2557 while (member != NULL && (member->flags & DYNAMIC) == 0)
2558 member = bfd_openr_next_archived_file (archive, member);
2560 archive_info->contains_shared_object_p = (member != NULL);
2561 archive_info->know_contains_shared_object_p = 1;
2563 return archive_info->contains_shared_object_p;
2566 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2567 qualifies for export by -bexpall. */
2569 static bfd_boolean
2570 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2572 /* Exclude symbols beginning with '_'. */
2573 if (h->root.root.string[0] == '_')
2574 return FALSE;
2576 /* Exclude archive members that would otherwise be unreferenced. */
2577 if ((h->flags & XCOFF_MARK) == 0
2578 && (h->root.type == bfd_link_hash_defined
2579 || h->root.type == bfd_link_hash_defweak)
2580 && h->root.u.def.section->owner != NULL
2581 && h->root.u.def.section->owner->my_archive != NULL)
2582 return FALSE;
2584 return TRUE;
2587 /* Return true if symbol H qualifies for the forms of automatic export
2588 specified by AUTO_EXPORT_FLAGS. */
2590 static bfd_boolean
2591 xcoff_auto_export_p (struct bfd_link_info *info,
2592 struct xcoff_link_hash_entry *h,
2593 unsigned int auto_export_flags)
2595 /* Don't automatically export things that were explicitly exported. */
2596 if ((h->flags & XCOFF_EXPORT) != 0)
2597 return FALSE;
2599 /* Don't export things that we don't define. */
2600 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2601 return FALSE;
2603 /* Don't export functions; export their descriptors instead. */
2604 if (h->root.root.string[0] == '.')
2605 return FALSE;
2607 /* We don't export a symbol which is being defined by an object
2608 included from an archive which contains a shared object. The
2609 rationale is that if an archive contains both an unshared and
2610 a shared object, then there must be some reason that the
2611 unshared object is unshared, and we don't want to start
2612 providing a shared version of it. In particular, this solves
2613 a bug involving the _savefNN set of functions. gcc will call
2614 those functions without providing a slot to restore the TOC,
2615 so it is essential that these functions be linked in directly
2616 and not from a shared object, which means that a shared
2617 object which also happens to link them in must not export
2618 them. This is confusing, but I haven't been able to think of
2619 a different approach. Note that the symbols can, of course,
2620 be exported explicitly. */
2621 if (h->root.type == bfd_link_hash_defined
2622 || h->root.type == bfd_link_hash_defweak)
2624 bfd *owner;
2626 owner = h->root.u.def.section->owner;
2627 if (owner != NULL
2628 && owner->my_archive != NULL
2629 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2630 return FALSE;
2633 /* Otherwise, all symbols are exported by -bexpfull. */
2634 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2635 return TRUE;
2637 /* Despite its name, -bexpall exports most but not all symbols. */
2638 if ((auto_export_flags & XCOFF_EXPALL) != 0
2639 && xcoff_covered_by_expall_p (h))
2640 return TRUE;
2642 return FALSE;
2645 /* Return true if relocation REL needs to be copied to the .loader section.
2646 If REL is against a global symbol, H is that symbol, otherwise it
2647 is null. */
2649 static bfd_boolean
2650 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2651 struct xcoff_link_hash_entry *h)
2653 if (!xcoff_hash_table (info)->loader_section)
2654 return FALSE;
2656 switch (rel->r_type)
2658 case R_TOC:
2659 case R_GL:
2660 case R_TCL:
2661 case R_TRL:
2662 case R_TRLA:
2663 /* We should never need a .loader reloc for a TOC-relative reloc. */
2664 return FALSE;
2666 default:
2667 /* In this case, relocations against defined symbols can be resolved
2668 statically. */
2669 if (h == NULL
2670 || h->root.type == bfd_link_hash_defined
2671 || h->root.type == bfd_link_hash_defweak
2672 || h->root.type == bfd_link_hash_common)
2673 return FALSE;
2675 /* We will always provide a local definition of function symbols,
2676 even if we don't have one yet. */
2677 if ((h->flags & XCOFF_CALLED) != 0)
2678 return FALSE;
2680 return TRUE;
2682 case R_POS:
2683 case R_NEG:
2684 case R_RL:
2685 case R_RLA:
2686 /* Absolute relocations against absolute symbols can be
2687 resolved statically. */
2688 if (h != NULL
2689 && (h->root.type == bfd_link_hash_defined
2690 || h->root.type == bfd_link_hash_defweak)
2691 && !h->root.rel_from_abs)
2693 asection *sec = h->root.u.def.section;
2694 if (bfd_is_abs_section (sec)
2695 || (sec != NULL
2696 && bfd_is_abs_section (sec->output_section)))
2697 return FALSE;
2699 return TRUE;
2703 /* Mark a symbol as not being garbage, including the section in which
2704 it is defined. */
2706 static inline bfd_boolean
2707 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2709 if ((h->flags & XCOFF_MARK) != 0)
2710 return TRUE;
2712 h->flags |= XCOFF_MARK;
2714 /* If we're marking an undefined symbol, try find some way of
2715 defining it. */
2716 if (!bfd_link_relocatable (info)
2717 && (h->flags & XCOFF_IMPORT) == 0
2718 && (h->flags & XCOFF_DEF_REGULAR) == 0
2719 && (h->root.type == bfd_link_hash_undefined
2720 || h->root.type == bfd_link_hash_undefweak))
2722 /* First check whether this symbol can be interpreted as an
2723 undefined function descriptor for a defined function symbol. */
2724 if (!xcoff_find_function (info, h))
2725 return FALSE;
2727 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2728 && (h->descriptor->root.type == bfd_link_hash_defined
2729 || h->descriptor->root.type == bfd_link_hash_defweak))
2731 /* This is a descriptor for a defined symbol, but the input
2732 objects have not defined the descriptor itself. Fill in
2733 the definition automatically.
2735 Note that we do this even if we found a dynamic definition
2736 of H. The local function definition logically overrides
2737 the dynamic one. */
2738 asection *sec;
2740 sec = xcoff_hash_table (info)->descriptor_section;
2741 h->root.type = bfd_link_hash_defined;
2742 h->root.u.def.section = sec;
2743 h->root.u.def.value = sec->size;
2744 h->smclas = XMC_DS;
2745 h->flags |= XCOFF_DEF_REGULAR;
2747 /* The size of the function descriptor depends on whether this
2748 is xcoff32 (12) or xcoff64 (24). */
2749 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2751 /* A function descriptor uses two relocs: one for the
2752 associated code, and one for the TOC address. */
2753 xcoff_hash_table (info)->ldrel_count += 2;
2754 sec->reloc_count += 2;
2756 /* Mark the function itself. */
2757 if (!xcoff_mark_symbol (info, h->descriptor))
2758 return FALSE;
2760 /* Mark the TOC section, so that we get an anchor
2761 to relocate against. */
2762 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2763 return FALSE;
2765 /* We handle writing out the contents of the descriptor in
2766 xcoff_write_global_symbol. */
2768 else if (info->static_link)
2769 /* We can't get a symbol value dynamically, so just assume
2770 that it's undefined. */
2771 h->flags |= XCOFF_WAS_UNDEFINED;
2772 else if ((h->flags & XCOFF_CALLED) != 0)
2774 /* This is a function symbol for which we need to create
2775 linkage code. */
2776 asection *sec;
2777 struct xcoff_link_hash_entry *hds;
2779 /* Mark the descriptor (and its TOC section). */
2780 hds = h->descriptor;
2781 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2782 || hds->root.type == bfd_link_hash_undefweak)
2783 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2784 if (!xcoff_mark_symbol (info, hds))
2785 return FALSE;
2787 /* Treat this symbol as undefined if the descriptor was. */
2788 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2789 h->flags |= XCOFF_WAS_UNDEFINED;
2791 /* Allocate room for the global linkage code itself. */
2792 sec = xcoff_hash_table (info)->linkage_section;
2793 h->root.type = bfd_link_hash_defined;
2794 h->root.u.def.section = sec;
2795 h->root.u.def.value = sec->size;
2796 h->smclas = XMC_GL;
2797 h->flags |= XCOFF_DEF_REGULAR;
2798 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2800 /* The global linkage code requires a TOC entry for the
2801 descriptor. */
2802 if (hds->toc_section == NULL)
2804 int byte_size;
2806 /* 32 vs 64
2807 xcoff32 uses 4 bytes in the toc.
2808 xcoff64 uses 8 bytes in the toc. */
2809 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2810 byte_size = 8;
2811 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2812 byte_size = 4;
2813 else
2814 return FALSE;
2816 /* Allocate room in the fallback TOC section. */
2817 hds->toc_section = xcoff_hash_table (info)->toc_section;
2818 hds->u.toc_offset = hds->toc_section->size;
2819 hds->toc_section->size += byte_size;
2820 if (!xcoff_mark (info, hds->toc_section))
2821 return FALSE;
2823 /* Allocate room for a static and dynamic R_TOC
2824 relocation. */
2825 ++xcoff_hash_table (info)->ldrel_count;
2826 ++hds->toc_section->reloc_count;
2828 /* Set the index to -2 to force this symbol to
2829 get written out. */
2830 hds->indx = -2;
2831 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2834 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2836 /* Record that the symbol was undefined, then import it.
2837 -brtl links use a special fake import file. */
2838 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2839 if (xcoff_hash_table (info)->rtld)
2841 if (!xcoff_set_import_path (info, h, "", "..", ""))
2842 return FALSE;
2844 else
2846 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2847 return FALSE;
2852 if (h->root.type == bfd_link_hash_defined
2853 || h->root.type == bfd_link_hash_defweak)
2855 asection *hsec;
2857 hsec = h->root.u.def.section;
2858 if (! bfd_is_abs_section (hsec)
2859 && (hsec->flags & SEC_MARK) == 0)
2861 if (! xcoff_mark (info, hsec))
2862 return FALSE;
2866 if (h->toc_section != NULL
2867 && (h->toc_section->flags & SEC_MARK) == 0)
2869 if (! xcoff_mark (info, h->toc_section))
2870 return FALSE;
2873 return TRUE;
2876 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2877 If the symbol exists, set FLAGS. */
2879 static bfd_boolean
2880 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2881 const char *name, unsigned int flags)
2883 struct xcoff_link_hash_entry *h;
2885 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2886 FALSE, FALSE, TRUE);
2887 if (h != NULL)
2889 h->flags |= flags;
2890 if (h->root.type == bfd_link_hash_defined
2891 || h->root.type == bfd_link_hash_defweak)
2893 if (!xcoff_mark (info, h->root.u.def.section))
2894 return FALSE;
2897 return TRUE;
2900 /* The mark phase of garbage collection. For a given section, mark
2901 it, and all the sections which define symbols to which it refers.
2902 Because this function needs to look at the relocs, we also count
2903 the number of relocs which need to be copied into the .loader
2904 section. */
2906 static bfd_boolean
2907 xcoff_mark (struct bfd_link_info *info, asection *sec)
2909 if (bfd_is_const_section (sec)
2910 || (sec->flags & SEC_MARK) != 0)
2911 return TRUE;
2913 sec->flags |= SEC_MARK;
2915 if (sec->owner->xvec == info->output_bfd->xvec
2916 && coff_section_data (sec->owner, sec) != NULL
2917 && xcoff_section_data (sec->owner, sec) != NULL)
2919 struct xcoff_link_hash_entry **syms;
2920 struct internal_reloc *rel, *relend;
2921 asection **csects;
2922 unsigned long i, first, last;
2924 /* Mark all the symbols in this section. */
2925 syms = obj_xcoff_sym_hashes (sec->owner);
2926 csects = xcoff_data (sec->owner)->csects;
2927 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2928 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2929 for (i = first; i <= last; i++)
2930 if (csects[i] == sec
2931 && syms[i] != NULL
2932 && (syms[i]->flags & XCOFF_MARK) == 0)
2934 if (!xcoff_mark_symbol (info, syms[i]))
2935 return FALSE;
2938 /* Look through the section relocs. */
2939 if ((sec->flags & SEC_RELOC) != 0
2940 && sec->reloc_count > 0)
2942 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2943 NULL, FALSE, NULL);
2944 if (rel == NULL)
2945 return FALSE;
2946 relend = rel + sec->reloc_count;
2947 for (; rel < relend; rel++)
2949 struct xcoff_link_hash_entry *h;
2951 if ((unsigned int) rel->r_symndx
2952 > obj_raw_syment_count (sec->owner))
2953 continue;
2955 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2956 if (h != NULL)
2958 if ((h->flags & XCOFF_MARK) == 0)
2960 if (!xcoff_mark_symbol (info, h))
2961 return FALSE;
2964 else
2966 asection *rsec;
2968 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2969 if (rsec != NULL
2970 && (rsec->flags & SEC_MARK) == 0)
2972 if (!xcoff_mark (info, rsec))
2973 return FALSE;
2977 /* See if this reloc needs to be copied into the .loader
2978 section. */
2979 if (xcoff_need_ldrel_p (info, rel, h))
2981 ++xcoff_hash_table (info)->ldrel_count;
2982 if (h != NULL)
2983 h->flags |= XCOFF_LDREL;
2987 if (! info->keep_memory
2988 && coff_section_data (sec->owner, sec) != NULL
2989 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2991 free (coff_section_data (sec->owner, sec)->relocs);
2992 coff_section_data (sec->owner, sec)->relocs = NULL;
2997 return TRUE;
3000 /* Routines that are called after all the input files have been
3001 handled, but before the sections are laid out in memory. */
3003 /* The sweep phase of garbage collection. Remove all garbage
3004 sections. */
3006 static void
3007 xcoff_sweep (struct bfd_link_info *info)
3009 bfd *sub;
3011 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3013 asection *o;
3015 for (o = sub->sections; o != NULL; o = o->next)
3017 if ((o->flags & SEC_MARK) == 0)
3019 /* Keep all sections from non-XCOFF input files. Keep
3020 special sections. Keep .debug sections for the
3021 moment. */
3022 if (sub->xvec != info->output_bfd->xvec
3023 || o == xcoff_hash_table (info)->debug_section
3024 || o == xcoff_hash_table (info)->loader_section
3025 || o == xcoff_hash_table (info)->linkage_section
3026 || o == xcoff_hash_table (info)->descriptor_section
3027 || (bfd_section_flags (o) & SEC_DEBUGGING)
3028 || strcmp (o->name, ".debug") == 0)
3029 o->flags |= SEC_MARK;
3030 else
3032 o->size = 0;
3033 o->reloc_count = 0;
3040 /* Record the number of elements in a set. This is used to output the
3041 correct csect length. */
3043 bfd_boolean
3044 bfd_xcoff_link_record_set (bfd *output_bfd,
3045 struct bfd_link_info *info,
3046 struct bfd_link_hash_entry *harg,
3047 bfd_size_type size)
3049 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3050 struct xcoff_link_size_list *n;
3051 size_t amt;
3053 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3054 return TRUE;
3056 /* This will hardly ever be called. I don't want to burn four bytes
3057 per global symbol, so instead the size is kept on a linked list
3058 attached to the hash table. */
3059 amt = sizeof (* n);
3060 n = bfd_alloc (output_bfd, amt);
3061 if (n == NULL)
3062 return FALSE;
3063 n->next = xcoff_hash_table (info)->size_list;
3064 n->h = h;
3065 n->size = size;
3066 xcoff_hash_table (info)->size_list = n;
3068 h->flags |= XCOFF_HAS_SIZE;
3070 return TRUE;
3073 /* Import a symbol. */
3075 bfd_boolean
3076 bfd_xcoff_import_symbol (bfd *output_bfd,
3077 struct bfd_link_info *info,
3078 struct bfd_link_hash_entry *harg,
3079 bfd_vma val,
3080 const char *imppath,
3081 const char *impfile,
3082 const char *impmember,
3083 unsigned int syscall_flag)
3085 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3087 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3088 return TRUE;
3090 /* A symbol name which starts with a period is the code for a
3091 function. If the symbol is undefined, then add an undefined
3092 symbol for the function descriptor, and import that instead. */
3093 if (h->root.root.string[0] == '.'
3094 && h->root.type == bfd_link_hash_undefined
3095 && val == (bfd_vma) -1)
3097 struct xcoff_link_hash_entry *hds;
3099 hds = h->descriptor;
3100 if (hds == NULL)
3102 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3103 h->root.root.string + 1,
3104 TRUE, FALSE, TRUE);
3105 if (hds == NULL)
3106 return FALSE;
3107 if (hds->root.type == bfd_link_hash_new)
3109 hds->root.type = bfd_link_hash_undefined;
3110 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3112 hds->flags |= XCOFF_DESCRIPTOR;
3113 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3114 hds->descriptor = h;
3115 h->descriptor = hds;
3118 /* Now, if the descriptor is undefined, import the descriptor
3119 rather than the symbol we were told to import. FIXME: Is
3120 this correct in all cases? */
3121 if (hds->root.type == bfd_link_hash_undefined)
3122 h = hds;
3125 h->flags |= (XCOFF_IMPORT | syscall_flag);
3127 if (val != (bfd_vma) -1)
3129 if (h->root.type == bfd_link_hash_defined)
3130 (*info->callbacks->multiple_definition) (info, &h->root, output_bfd,
3131 bfd_abs_section_ptr, val);
3133 h->root.type = bfd_link_hash_defined;
3134 h->root.u.def.section = bfd_abs_section_ptr;
3135 h->root.u.def.value = val;
3136 h->smclas = XMC_XO;
3139 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3140 return FALSE;
3142 return TRUE;
3145 /* Export a symbol. */
3147 bfd_boolean
3148 bfd_xcoff_export_symbol (bfd *output_bfd,
3149 struct bfd_link_info *info,
3150 struct bfd_link_hash_entry *harg)
3152 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3154 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3155 return TRUE;
3157 h->flags |= XCOFF_EXPORT;
3159 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3160 I'm just going to ignore it until somebody explains it. */
3162 /* Make sure we don't garbage collect this symbol. */
3163 if (! xcoff_mark_symbol (info, h))
3164 return FALSE;
3166 /* If this is a function descriptor, make sure we don't garbage
3167 collect the associated function code. We normally don't have to
3168 worry about this, because the descriptor will be attached to a
3169 section with relocs, but if we are creating the descriptor
3170 ourselves those relocs will not be visible to the mark code. */
3171 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3173 if (! xcoff_mark_symbol (info, h->descriptor))
3174 return FALSE;
3177 return TRUE;
3180 /* Count a reloc against a symbol. This is called for relocs
3181 generated by the linker script, typically for global constructors
3182 and destructors. */
3184 bfd_boolean
3185 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3186 struct bfd_link_info *info,
3187 const char *name)
3189 struct xcoff_link_hash_entry *h;
3191 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3192 return TRUE;
3194 h = ((struct xcoff_link_hash_entry *)
3195 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3196 FALSE));
3197 if (h == NULL)
3199 _bfd_error_handler (_("%s: no such symbol"), name);
3200 bfd_set_error (bfd_error_no_symbols);
3201 return FALSE;
3204 h->flags |= XCOFF_REF_REGULAR;
3205 if (xcoff_hash_table (info)->loader_section)
3207 h->flags |= XCOFF_LDREL;
3208 ++xcoff_hash_table (info)->ldrel_count;
3211 /* Mark the symbol to avoid garbage collection. */
3212 if (! xcoff_mark_symbol (info, h))
3213 return FALSE;
3215 return TRUE;
3218 /* This function is called for each symbol to which the linker script
3219 assigns a value.
3220 FIXME: In cases like the linker test ld-scripts/defined5 where a
3221 symbol is defined both by an input object file and the script,
3222 the script definition doesn't override the object file definition
3223 as is usual for other targets. At least not when the symbol is
3224 output. Other uses of the symbol value by the linker do use the
3225 script value. */
3227 bfd_boolean
3228 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3229 struct bfd_link_info *info,
3230 const char *name)
3232 struct xcoff_link_hash_entry *h;
3234 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3235 return TRUE;
3237 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3238 FALSE);
3239 if (h == NULL)
3240 return FALSE;
3242 h->flags |= XCOFF_DEF_REGULAR;
3244 return TRUE;
3247 /* An xcoff_link_hash_traverse callback for which DATA points to an
3248 xcoff_loader_info. Mark all symbols that should be automatically
3249 exported. */
3251 static bfd_boolean
3252 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3254 struct xcoff_loader_info *ldinfo;
3256 ldinfo = (struct xcoff_loader_info *) data;
3257 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3259 if (!xcoff_mark_symbol (ldinfo->info, h))
3260 ldinfo->failed = TRUE;
3262 return TRUE;
3265 /* Add a symbol to the .loader symbols, if necessary. */
3267 /* INPUT_BFD has an external symbol associated with hash table entry H
3268 and csect CSECT. Return true if INPUT_BFD defines H. */
3270 static bfd_boolean
3271 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3272 asection *csect)
3274 switch (h->root.type)
3276 case bfd_link_hash_defined:
3277 case bfd_link_hash_defweak:
3278 /* No input bfd owns absolute symbols. They are written by
3279 xcoff_write_global_symbol instead. */
3280 return (!bfd_is_abs_section (csect)
3281 && h->root.u.def.section == csect);
3283 case bfd_link_hash_common:
3284 return h->root.u.c.p->section->owner == input_bfd;
3286 case bfd_link_hash_undefined:
3287 case bfd_link_hash_undefweak:
3288 /* We can't treat undef.abfd as the owner because that bfd
3289 might be a dynamic object. Allow any bfd to claim it. */
3290 return TRUE;
3292 default:
3293 abort ();
3297 /* See if H should have a loader symbol associated with it. */
3299 static bfd_boolean
3300 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3301 struct xcoff_link_hash_entry *h)
3303 size_t amt;
3305 /* Warn if this symbol is exported but not defined. */
3306 if ((h->flags & XCOFF_EXPORT) != 0
3307 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3309 _bfd_error_handler
3310 (_("warning: attempt to export undefined symbol `%s'"),
3311 h->root.root.string);
3312 return TRUE;
3315 /* We need to add a symbol to the .loader section if it is mentioned
3316 in a reloc which we are copying to the .loader section and it was
3317 not defined or common, or if it is the entry point, or if it is
3318 being exported. */
3319 if (((h->flags & XCOFF_LDREL) == 0
3320 || h->root.type == bfd_link_hash_defined
3321 || h->root.type == bfd_link_hash_defweak
3322 || h->root.type == bfd_link_hash_common)
3323 && (h->flags & XCOFF_ENTRY) == 0
3324 && (h->flags & XCOFF_EXPORT) == 0)
3325 return TRUE;
3327 /* We need to add this symbol to the .loader symbols. */
3329 BFD_ASSERT (h->ldsym == NULL);
3330 amt = sizeof (struct internal_ldsym);
3331 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3332 if (h->ldsym == NULL)
3334 ldinfo->failed = TRUE;
3335 return FALSE;
3338 if ((h->flags & XCOFF_IMPORT) != 0)
3340 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3341 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3342 h->smclas = XMC_DS;
3343 h->ldsym->l_ifile = h->ldindx;
3346 /* The first 3 symbol table indices are reserved to indicate the
3347 data, text and bss sections. */
3348 h->ldindx = ldinfo->ldsym_count + 3;
3350 ++ldinfo->ldsym_count;
3352 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3353 h->ldsym, h->root.root.string))
3354 return FALSE;
3356 h->flags |= XCOFF_BUILT_LDSYM;
3357 return TRUE;
3360 /* An xcoff_htab_traverse callback that is called for each symbol
3361 once garbage collection is complete. */
3363 static bfd_boolean
3364 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3366 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3368 /* __rtinit, this symbol has special handling. */
3369 if (h->flags & XCOFF_RTINIT)
3370 return TRUE;
3372 /* We don't want to garbage collect symbols which are not defined in
3373 XCOFF files. This is a convenient place to mark them. */
3374 if (xcoff_hash_table (ldinfo->info)->gc
3375 && (h->flags & XCOFF_MARK) == 0
3376 && (h->root.type == bfd_link_hash_defined
3377 || h->root.type == bfd_link_hash_defweak)
3378 && (h->root.u.def.section->owner == NULL
3379 || (h->root.u.def.section->owner->xvec
3380 != ldinfo->info->output_bfd->xvec)))
3381 h->flags |= XCOFF_MARK;
3383 /* Skip discarded symbols. */
3384 if (xcoff_hash_table (ldinfo->info)->gc
3385 && (h->flags & XCOFF_MARK) == 0)
3386 return TRUE;
3388 /* If this is still a common symbol, and it wasn't garbage
3389 collected, we need to actually allocate space for it in the .bss
3390 section. */
3391 if (h->root.type == bfd_link_hash_common
3392 && h->root.u.c.p->section->size == 0)
3394 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3395 h->root.u.c.p->section->size = h->root.u.c.size;
3398 if (xcoff_hash_table (ldinfo->info)->loader_section)
3400 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3401 h->flags |= XCOFF_EXPORT;
3403 if (!xcoff_build_ldsym (ldinfo, h))
3404 return FALSE;
3407 return TRUE;
3410 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3411 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3412 csect information, if any. NAME is the function's name if the name
3413 is stored in the .debug section, otherwise it is null.
3415 Return 1 if we should include an appropriately-adjusted ISYM
3416 in the output file, 0 if we should discard ISYM, or -1 if an
3417 error occured. */
3419 static int
3420 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3421 struct internal_syment *isym,
3422 union internal_auxent *aux,
3423 struct xcoff_link_hash_entry *h,
3424 asection *csect, const char *name)
3426 int smtyp;
3428 /* If we are skipping this csect, we want to strip the symbol too. */
3429 if (csect == NULL)
3430 return 0;
3432 /* Likewise if we garbage-collected the csect. */
3433 if (xcoff_hash_table (info)->gc
3434 && !bfd_is_abs_section (csect)
3435 && !bfd_is_und_section (csect)
3436 && (csect->flags & SEC_MARK) == 0)
3437 return 0;
3439 /* An XCOFF linker always removes C_STAT symbols. */
3440 if (isym->n_sclass == C_STAT)
3441 return 0;
3443 /* We generate the TOC anchor separately. */
3444 if (isym->n_sclass == C_HIDEXT
3445 && aux->x_csect.x_smclas == XMC_TC0)
3446 return 0;
3448 /* If we are stripping all symbols, we want to discard this one. */
3449 if (info->strip == strip_all)
3450 return 0;
3452 /* Discard symbols that are defined elsewhere. */
3453 if (EXTERN_SYM_P (isym->n_sclass))
3455 if ((h->flags & XCOFF_ALLOCATED) != 0)
3456 return 0;
3457 if (!xcoff_final_definition_p (input_bfd, h, csect))
3458 return 0;
3461 /* If we're discarding local symbols, check whether ISYM is local. */
3462 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3463 if (info->discard == discard_all
3464 && !EXTERN_SYM_P (isym->n_sclass)
3465 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3466 return 0;
3468 /* If we're stripping debugging symbols, check whether ISYM is one. */
3469 if (info->strip == strip_debugger
3470 && isym->n_scnum == N_DEBUG)
3471 return 0;
3473 /* If we are stripping symbols based on name, check how ISYM's
3474 name should be handled. */
3475 if (info->strip == strip_some
3476 || info->discard == discard_l)
3478 char buf[SYMNMLEN + 1];
3480 if (name == NULL)
3482 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3483 if (name == NULL)
3484 return -1;
3487 if (info->strip == strip_some
3488 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3489 return 0;
3491 if (info->discard == discard_l
3492 && !EXTERN_SYM_P (isym->n_sclass)
3493 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3494 && bfd_is_local_label_name (input_bfd, name))
3495 return 0;
3498 return 1;
3501 /* Lay out the .loader section, filling in the header and the import paths.
3502 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3504 static bfd_boolean
3505 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3506 const char *libpath)
3508 bfd *output_bfd;
3509 struct xcoff_link_hash_table *htab;
3510 struct internal_ldhdr *ldhdr;
3511 struct xcoff_import_file *fl;
3512 bfd_size_type stoff;
3513 size_t impsize, impcount;
3514 asection *lsec;
3515 char *out;
3517 /* Work out the size of the import file names. Each import file ID
3518 consists of three null terminated strings: the path, the file
3519 name, and the archive member name. The first entry in the list
3520 of names is the path to use to find objects, which the linker has
3521 passed in as the libpath argument. For some reason, the path
3522 entry in the other import file names appears to always be empty. */
3523 output_bfd = ldinfo->output_bfd;
3524 htab = xcoff_hash_table (ldinfo->info);
3525 impsize = strlen (libpath) + 3;
3526 impcount = 1;
3527 for (fl = htab->imports; fl != NULL; fl = fl->next)
3529 ++impcount;
3530 impsize += (strlen (fl->path)
3531 + strlen (fl->file)
3532 + strlen (fl->member)
3533 + 3);
3536 /* Set up the .loader section header. */
3537 ldhdr = &htab->ldhdr;
3538 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3539 ldhdr->l_nsyms = ldinfo->ldsym_count;
3540 ldhdr->l_nreloc = htab->ldrel_count;
3541 ldhdr->l_istlen = impsize;
3542 ldhdr->l_nimpid = impcount;
3543 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3544 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3545 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3546 ldhdr->l_stlen = ldinfo->string_size;
3547 stoff = ldhdr->l_impoff + impsize;
3548 if (ldinfo->string_size == 0)
3549 ldhdr->l_stoff = 0;
3550 else
3551 ldhdr->l_stoff = stoff;
3553 /* 64 bit elements to ldhdr
3554 The swap out routine for 32 bit will ignore them.
3555 Nothing fancy, symbols come after the header and relocs come
3556 after symbols. */
3557 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3558 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3559 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3561 /* We now know the final size of the .loader section. Allocate
3562 space for it. */
3563 lsec = htab->loader_section;
3564 lsec->size = stoff + ldhdr->l_stlen;
3565 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3566 if (lsec->contents == NULL)
3567 return FALSE;
3569 /* Set up the header. */
3570 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3572 /* Set up the import file names. */
3573 out = (char *) lsec->contents + ldhdr->l_impoff;
3574 strcpy (out, libpath);
3575 out += strlen (libpath) + 1;
3576 *out++ = '\0';
3577 *out++ = '\0';
3578 for (fl = htab->imports; fl != NULL; fl = fl->next)
3580 const char *s;
3582 s = fl->path;
3583 while ((*out++ = *s++) != '\0')
3585 s = fl->file;
3586 while ((*out++ = *s++) != '\0')
3588 s = fl->member;
3589 while ((*out++ = *s++) != '\0')
3593 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3595 /* Set up the symbol string table. */
3596 if (ldinfo->string_size > 0)
3598 memcpy (out, ldinfo->strings, ldinfo->string_size);
3599 free (ldinfo->strings);
3600 ldinfo->strings = NULL;
3603 /* We can't set up the symbol table or the relocs yet, because we
3604 don't yet know the final position of the various sections. The
3605 .loader symbols are written out when the corresponding normal
3606 symbols are written out in xcoff_link_input_bfd or
3607 xcoff_write_global_symbol. The .loader relocs are written out
3608 when the corresponding normal relocs are handled in
3609 xcoff_link_input_bfd. */
3611 return TRUE;
3614 /* Build the .loader section. This is called by the XCOFF linker
3615 emulation before_allocation routine. We must set the size of the
3616 .loader section before the linker lays out the output file.
3617 LIBPATH is the library path to search for shared objects; this is
3618 normally built from the -L arguments passed to the linker. ENTRY
3619 is the name of the entry point symbol (the -e linker option).
3620 FILE_ALIGN is the alignment to use for sections within the file
3621 (the -H linker option). MAXSTACK is the maximum stack size (the
3622 -bmaxstack linker option). MAXDATA is the maximum data size (the
3623 -bmaxdata linker option). GC is whether to do garbage collection
3624 (the -bgc linker option). MODTYPE is the module type (the
3625 -bmodtype linker option). TEXTRO is whether the text section must
3626 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3627 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3628 is set by this routine to csects with magic names like _end. */
3630 bfd_boolean
3631 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3632 struct bfd_link_info *info,
3633 const char *libpath,
3634 const char *entry,
3635 unsigned long file_align,
3636 unsigned long maxstack,
3637 unsigned long maxdata,
3638 bfd_boolean gc,
3639 int modtype,
3640 bfd_boolean textro,
3641 unsigned int auto_export_flags,
3642 asection **special_sections,
3643 bfd_boolean rtld)
3645 struct xcoff_loader_info ldinfo;
3646 int i;
3647 asection *sec;
3648 bfd *sub;
3649 struct bfd_strtab_hash *debug_strtab;
3650 bfd_byte *debug_contents = NULL;
3651 size_t amt;
3653 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3655 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3656 special_sections[i] = NULL;
3657 return TRUE;
3660 ldinfo.failed = FALSE;
3661 ldinfo.output_bfd = output_bfd;
3662 ldinfo.info = info;
3663 ldinfo.auto_export_flags = auto_export_flags;
3664 ldinfo.ldsym_count = 0;
3665 ldinfo.string_size = 0;
3666 ldinfo.strings = NULL;
3667 ldinfo.string_alc = 0;
3669 xcoff_data (output_bfd)->maxstack = maxstack;
3670 xcoff_data (output_bfd)->maxdata = maxdata;
3671 xcoff_data (output_bfd)->modtype = modtype;
3673 xcoff_hash_table (info)->file_align = file_align;
3674 xcoff_hash_table (info)->textro = textro;
3675 xcoff_hash_table (info)->rtld = rtld;
3677 /* __rtinit */
3678 if (xcoff_hash_table (info)->loader_section
3679 && (info->init_function || info->fini_function || rtld))
3681 struct xcoff_link_hash_entry *hsym;
3682 struct internal_ldsym *ldsym;
3684 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3685 "__rtinit", FALSE, FALSE, TRUE);
3686 if (hsym == NULL)
3688 _bfd_error_handler
3689 (_("error: undefined symbol __rtinit"));
3690 return FALSE;
3693 xcoff_mark_symbol (info, hsym);
3694 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3696 /* __rtinit initialized. */
3697 amt = sizeof (* ldsym);
3698 ldsym = bfd_malloc (amt);
3700 ldsym->l_value = 0; /* Will be filled in later. */
3701 ldsym->l_scnum = 2; /* Data section. */
3702 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3703 ldsym->l_smclas = 5; /* .rw. */
3704 ldsym->l_ifile = 0; /* Special system loader symbol. */
3705 ldsym->l_parm = 0; /* NA. */
3707 /* Force __rtinit to be the first symbol in the loader symbol table
3708 See xcoff_build_ldsyms
3710 The first 3 symbol table indices are reserved to indicate the data,
3711 text and bss sections. */
3712 BFD_ASSERT (0 == ldinfo.ldsym_count);
3714 hsym->ldindx = 3;
3715 ldinfo.ldsym_count = 1;
3716 hsym->ldsym = ldsym;
3718 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3719 hsym->ldsym, hsym->root.root.string))
3720 return FALSE;
3722 /* This symbol is written out by xcoff_write_global_symbol
3723 Set stuff up so xcoff_write_global_symbol logic works. */
3724 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3725 hsym->root.type = bfd_link_hash_defined;
3726 hsym->root.u.def.value = 0;
3729 /* Garbage collect unused sections. */
3730 if (bfd_link_relocatable (info) || !gc)
3732 gc = FALSE;
3733 xcoff_hash_table (info)->gc = FALSE;
3735 /* We still need to call xcoff_mark, in order to set ldrel_count
3736 correctly. */
3737 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3739 asection *o;
3741 for (o = sub->sections; o != NULL; o = o->next)
3743 /* We shouldn't unconditionaly mark the TOC section.
3744 The output file should only have a TOC if either
3745 (a) one of the input files did or (b) we end up
3746 creating TOC references as part of the link process. */
3747 if (o != xcoff_hash_table (info)->toc_section
3748 && (o->flags & SEC_MARK) == 0)
3750 if (! xcoff_mark (info, o))
3751 goto error_return;
3756 else
3758 if (entry != NULL
3759 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3760 goto error_return;
3761 if (info->init_function != NULL
3762 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3763 goto error_return;
3764 if (info->fini_function != NULL
3765 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3766 goto error_return;
3767 if (auto_export_flags != 0)
3769 xcoff_link_hash_traverse (xcoff_hash_table (info),
3770 xcoff_mark_auto_exports, &ldinfo);
3771 if (ldinfo.failed)
3772 goto error_return;
3774 xcoff_sweep (info);
3775 xcoff_hash_table (info)->gc = TRUE;
3778 /* Return special sections to the caller. */
3779 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3781 sec = xcoff_hash_table (info)->special_sections[i];
3783 if (sec != NULL
3784 && gc
3785 && (sec->flags & SEC_MARK) == 0)
3786 sec = NULL;
3788 special_sections[i] = sec;
3791 if (info->input_bfds == NULL)
3792 /* I'm not sure what to do in this bizarre case. */
3793 return TRUE;
3795 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3796 (void *) &ldinfo);
3797 if (ldinfo.failed)
3798 goto error_return;
3800 if (xcoff_hash_table (info)->loader_section
3801 && !xcoff_build_loader_section (&ldinfo, libpath))
3802 goto error_return;
3804 /* Allocate space for the magic sections. */
3805 sec = xcoff_hash_table (info)->linkage_section;
3806 if (sec->size > 0)
3808 sec->contents = bfd_zalloc (output_bfd, sec->size);
3809 if (sec->contents == NULL)
3810 goto error_return;
3812 sec = xcoff_hash_table (info)->toc_section;
3813 if (sec->size > 0)
3815 sec->contents = bfd_zalloc (output_bfd, sec->size);
3816 if (sec->contents == NULL)
3817 goto error_return;
3819 sec = xcoff_hash_table (info)->descriptor_section;
3820 if (sec->size > 0)
3822 sec->contents = bfd_zalloc (output_bfd, sec->size);
3823 if (sec->contents == NULL)
3824 goto error_return;
3827 /* Now that we've done garbage collection, decide which symbols to keep,
3828 and figure out the contents of the .debug section. */
3829 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3831 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3833 asection *subdeb;
3834 bfd_size_type symcount;
3835 long *debug_index;
3836 asection **csectpp;
3837 unsigned int *lineno_counts;
3838 struct xcoff_link_hash_entry **sym_hash;
3839 bfd_byte *esym, *esymend;
3840 bfd_size_type symesz;
3842 if (sub->xvec != info->output_bfd->xvec)
3843 continue;
3845 if ((sub->flags & DYNAMIC) != 0
3846 && !info->static_link)
3847 continue;
3849 if (! _bfd_coff_get_external_symbols (sub))
3850 goto error_return;
3852 symcount = obj_raw_syment_count (sub);
3853 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3854 if (debug_index == NULL)
3855 goto error_return;
3856 xcoff_data (sub)->debug_indices = debug_index;
3858 if (info->strip == strip_all
3859 || info->strip == strip_debugger
3860 || info->discard == discard_all)
3861 /* We're stripping all debugging information, so there's no need
3862 to read SUB's .debug section. */
3863 subdeb = NULL;
3864 else
3866 /* Grab the contents of SUB's .debug section, if any. */
3867 subdeb = bfd_get_section_by_name (sub, ".debug");
3868 if (subdeb != NULL && subdeb->size > 0)
3870 /* We use malloc and copy the names into the debug
3871 stringtab, rather than bfd_alloc, because I expect
3872 that, when linking many files together, many of the
3873 strings will be the same. Storing the strings in the
3874 hash table should save space in this case. */
3875 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3876 goto error_return;
3880 csectpp = xcoff_data (sub)->csects;
3881 lineno_counts = xcoff_data (sub)->lineno_counts;
3882 sym_hash = obj_xcoff_sym_hashes (sub);
3883 symesz = bfd_coff_symesz (sub);
3884 esym = (bfd_byte *) obj_coff_external_syms (sub);
3885 esymend = esym + symcount * symesz;
3887 while (esym < esymend)
3889 struct internal_syment sym;
3890 union internal_auxent aux;
3891 asection *csect;
3892 const char *name;
3893 int keep_p;
3895 bfd_coff_swap_sym_in (sub, esym, &sym);
3897 /* Read in the csect information, if any. */
3898 if (CSECT_SYM_P (sym.n_sclass))
3900 BFD_ASSERT (sym.n_numaux > 0);
3901 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3902 sym.n_type, sym.n_sclass,
3903 sym.n_numaux - 1, sym.n_numaux, &aux);
3906 /* If this symbol's name is stored in the debug section,
3907 get a pointer to it. */
3908 if (debug_contents != NULL
3909 && sym._n._n_n._n_zeroes == 0
3910 && bfd_coff_symname_in_debug (sub, &sym))
3911 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3912 else
3913 name = NULL;
3915 /* Decide whether to copy this symbol to the output file. */
3916 csect = *csectpp;
3917 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3918 *sym_hash, csect, name);
3919 if (keep_p < 0)
3920 return FALSE;
3922 if (!keep_p)
3923 /* Use a debug_index of -2 to record that a symbol should
3924 be stripped. */
3925 *debug_index = -2;
3926 else
3928 /* See whether we should store the symbol name in the
3929 output .debug section. */
3930 if (name != NULL)
3932 bfd_size_type indx;
3934 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3935 if (indx == (bfd_size_type) -1)
3936 goto error_return;
3937 *debug_index = indx;
3939 else
3940 *debug_index = -1;
3941 if (*sym_hash != 0)
3942 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3943 if (*lineno_counts > 0)
3944 csect->output_section->lineno_count += *lineno_counts;
3947 esym += (sym.n_numaux + 1) * symesz;
3948 csectpp += sym.n_numaux + 1;
3949 sym_hash += sym.n_numaux + 1;
3950 lineno_counts += sym.n_numaux + 1;
3951 debug_index += sym.n_numaux + 1;
3954 if (debug_contents)
3956 free (debug_contents);
3957 debug_contents = NULL;
3959 /* Clear the size of subdeb, so that it is not included directly
3960 in the output file. */
3961 subdeb->size = 0;
3964 if (! info->keep_memory)
3966 if (! _bfd_coff_free_symbols (sub))
3967 goto error_return;
3971 if (info->strip != strip_all)
3972 xcoff_hash_table (info)->debug_section->size =
3973 _bfd_stringtab_size (debug_strtab);
3975 return TRUE;
3977 error_return:
3978 free (ldinfo.strings);
3979 free (debug_contents);
3980 return FALSE;
3983 bfd_boolean
3984 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3985 const char *init,
3986 const char *fini,
3987 bfd_boolean rtld)
3989 struct bfd_in_memory *bim;
3991 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3992 if (bim == NULL)
3993 return FALSE;
3995 bim->size = 0;
3996 bim->buffer = 0;
3998 abfd->link.next = 0;
3999 abfd->format = bfd_object;
4000 abfd->iostream = (void *) bim;
4001 abfd->flags = BFD_IN_MEMORY;
4002 abfd->iovec = &_bfd_memory_iovec;
4003 abfd->direction = write_direction;
4004 abfd->origin = 0;
4005 abfd->where = 0;
4007 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
4008 return FALSE;
4010 /* need to reset to unknown or it will not be read back in correctly */
4011 abfd->format = bfd_unknown;
4012 abfd->direction = read_direction;
4013 abfd->where = 0;
4015 return TRUE;
4018 /* Return the section that defines H. Return null if no section does. */
4020 static asection *
4021 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4023 switch (h->root.type)
4025 case bfd_link_hash_defined:
4026 case bfd_link_hash_defweak:
4027 return h->root.u.def.section;
4029 case bfd_link_hash_common:
4030 return h->root.u.c.p->section;
4032 default:
4033 return NULL;
4037 /* Add a .loader relocation for input relocation IREL. If the loader
4038 relocation should be against an output section, HSEC points to the
4039 input section that IREL is against, otherwise HSEC is null. H is the
4040 symbol that IREL is against, or null if it isn't against a global symbol.
4041 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4043 static bfd_boolean
4044 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4045 asection *output_section, bfd *reference_bfd,
4046 struct internal_reloc *irel, asection *hsec,
4047 struct xcoff_link_hash_entry *h)
4049 struct internal_ldrel ldrel;
4051 ldrel.l_vaddr = irel->r_vaddr;
4052 if (hsec != NULL)
4054 const char *secname;
4056 secname = hsec->output_section->name;
4057 if (strcmp (secname, ".text") == 0)
4058 ldrel.l_symndx = 0;
4059 else if (strcmp (secname, ".data") == 0)
4060 ldrel.l_symndx = 1;
4061 else if (strcmp (secname, ".bss") == 0)
4062 ldrel.l_symndx = 2;
4063 else
4065 _bfd_error_handler
4066 /* xgettext:c-format */
4067 (_("%pB: loader reloc in unrecognized section `%s'"),
4068 reference_bfd, secname);
4069 bfd_set_error (bfd_error_nonrepresentable_section);
4070 return FALSE;
4073 else if (h != NULL)
4075 if (h->ldindx < 0)
4077 _bfd_error_handler
4078 /* xgettext:c-format */
4079 (_("%pB: `%s' in loader reloc but not loader sym"),
4080 reference_bfd, h->root.root.string);
4081 bfd_set_error (bfd_error_bad_value);
4082 return FALSE;
4084 ldrel.l_symndx = h->ldindx;
4086 else
4087 ldrel.l_symndx = -(bfd_size_type) 1;
4089 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4090 ldrel.l_rsecnm = output_section->target_index;
4091 if (xcoff_hash_table (flinfo->info)->textro
4092 && strcmp (output_section->name, ".text") == 0)
4094 _bfd_error_handler
4095 /* xgettext:c-format */
4096 (_("%pB: loader reloc in read-only section %pA"),
4097 reference_bfd, output_section);
4098 bfd_set_error (bfd_error_invalid_operation);
4099 return FALSE;
4101 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4102 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4103 return TRUE;
4106 /* Link an input file into the linker output file. This function
4107 handles all the sections and relocations of the input file at once. */
4109 static bfd_boolean
4110 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4111 bfd *input_bfd)
4113 bfd *output_bfd;
4114 const char *strings;
4115 bfd_size_type syment_base;
4116 unsigned int n_tmask;
4117 unsigned int n_btshft;
4118 bfd_boolean copy, hash;
4119 bfd_size_type isymesz;
4120 bfd_size_type osymesz;
4121 bfd_size_type linesz;
4122 bfd_byte *esym;
4123 bfd_byte *esym_end;
4124 struct xcoff_link_hash_entry **sym_hash;
4125 struct internal_syment *isymp;
4126 asection **csectpp;
4127 unsigned int *lineno_counts;
4128 long *debug_index;
4129 long *indexp;
4130 unsigned long output_index;
4131 bfd_byte *outsym;
4132 unsigned int incls;
4133 asection *oline;
4134 bfd_boolean keep_syms;
4135 asection *o;
4137 /* We can just skip DYNAMIC files, unless this is a static link. */
4138 if ((input_bfd->flags & DYNAMIC) != 0
4139 && ! flinfo->info->static_link)
4140 return TRUE;
4142 /* Move all the symbols to the output file. */
4143 output_bfd = flinfo->output_bfd;
4144 strings = NULL;
4145 syment_base = obj_raw_syment_count (output_bfd);
4146 isymesz = bfd_coff_symesz (input_bfd);
4147 osymesz = bfd_coff_symesz (output_bfd);
4148 linesz = bfd_coff_linesz (input_bfd);
4149 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4151 n_tmask = coff_data (input_bfd)->local_n_tmask;
4152 n_btshft = coff_data (input_bfd)->local_n_btshft;
4154 /* Define macros so that ISFCN, et. al., macros work correctly. */
4155 #define N_TMASK n_tmask
4156 #define N_BTSHFT n_btshft
4158 copy = FALSE;
4159 if (! flinfo->info->keep_memory)
4160 copy = TRUE;
4161 hash = TRUE;
4162 if (flinfo->info->traditional_format)
4163 hash = FALSE;
4165 if (! _bfd_coff_get_external_symbols (input_bfd))
4166 return FALSE;
4168 /* Make one pass over the symbols and assign indices to symbols that
4169 we have decided to keep. Also use create .loader symbol information
4170 and update information in hash table entries. */
4171 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4172 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4173 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4174 csectpp = xcoff_data (input_bfd)->csects;
4175 debug_index = xcoff_data (input_bfd)->debug_indices;
4176 isymp = flinfo->internal_syms;
4177 indexp = flinfo->sym_indices;
4178 output_index = syment_base;
4179 while (esym < esym_end)
4181 union internal_auxent aux;
4182 int smtyp = 0;
4183 int add;
4185 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4187 /* Read in the csect information, if any. */
4188 if (CSECT_SYM_P (isymp->n_sclass))
4190 BFD_ASSERT (isymp->n_numaux > 0);
4191 bfd_coff_swap_aux_in (input_bfd,
4192 (void *) (esym + isymesz * isymp->n_numaux),
4193 isymp->n_type, isymp->n_sclass,
4194 isymp->n_numaux - 1, isymp->n_numaux,
4195 (void *) &aux);
4197 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4200 /* If this symbol is in the .loader section, swap out the
4201 .loader symbol information. If this is an external symbol
4202 reference to a defined symbol, though, then wait until we get
4203 to the definition. */
4204 if (EXTERN_SYM_P (isymp->n_sclass)
4205 && *sym_hash != NULL
4206 && (*sym_hash)->ldsym != NULL
4207 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4209 struct xcoff_link_hash_entry *h;
4210 struct internal_ldsym *ldsym;
4212 h = *sym_hash;
4213 ldsym = h->ldsym;
4214 if (isymp->n_scnum > 0)
4216 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4217 ldsym->l_value = (isymp->n_value
4218 + (*csectpp)->output_section->vma
4219 + (*csectpp)->output_offset
4220 - (*csectpp)->vma);
4222 else
4224 ldsym->l_scnum = isymp->n_scnum;
4225 ldsym->l_value = isymp->n_value;
4228 ldsym->l_smtype = smtyp;
4229 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4230 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4231 || (h->flags & XCOFF_IMPORT) != 0)
4232 ldsym->l_smtype |= L_IMPORT;
4233 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4234 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4235 || (h->flags & XCOFF_EXPORT) != 0)
4236 ldsym->l_smtype |= L_EXPORT;
4237 if ((h->flags & XCOFF_ENTRY) != 0)
4238 ldsym->l_smtype |= L_ENTRY;
4239 if (isymp->n_sclass == C_AIX_WEAKEXT)
4240 ldsym->l_smtype |= L_WEAK;
4242 ldsym->l_smclas = aux.x_csect.x_smclas;
4244 if (ldsym->l_ifile == (bfd_size_type) -1)
4245 ldsym->l_ifile = 0;
4246 else if (ldsym->l_ifile == 0)
4248 if ((ldsym->l_smtype & L_IMPORT) == 0)
4249 ldsym->l_ifile = 0;
4250 else
4252 bfd *impbfd;
4254 if (h->root.type == bfd_link_hash_defined
4255 || h->root.type == bfd_link_hash_defweak)
4256 impbfd = h->root.u.def.section->owner;
4257 else if (h->root.type == bfd_link_hash_undefined
4258 || h->root.type == bfd_link_hash_undefweak)
4259 impbfd = h->root.u.undef.abfd;
4260 else
4261 impbfd = NULL;
4263 if (impbfd == NULL)
4264 ldsym->l_ifile = 0;
4265 else
4267 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4268 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4273 ldsym->l_parm = 0;
4275 BFD_ASSERT (h->ldindx >= 0);
4276 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4277 (flinfo->ldsym
4278 + ((h->ldindx - 3)
4279 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4280 h->ldsym = NULL;
4282 /* Fill in snentry now that we know the target_index. */
4283 if ((h->flags & XCOFF_ENTRY) != 0
4284 && (h->root.type == bfd_link_hash_defined
4285 || h->root.type == bfd_link_hash_defweak))
4287 xcoff_data (output_bfd)->snentry =
4288 h->root.u.def.section->output_section->target_index;
4292 add = 1 + isymp->n_numaux;
4294 if (*debug_index == -2)
4295 /* We've decided to strip this symbol. */
4296 *indexp = -1;
4297 else
4299 /* Assign the next unused index to this symbol. */
4300 *indexp = output_index;
4302 if (EXTERN_SYM_P (isymp->n_sclass))
4304 BFD_ASSERT (*sym_hash != NULL);
4305 (*sym_hash)->indx = output_index;
4308 /* If this is a symbol in the TOC which we may have merged
4309 (class XMC_TC), remember the symbol index of the TOC
4310 symbol. */
4311 if (isymp->n_sclass == C_HIDEXT
4312 && aux.x_csect.x_smclas == XMC_TC
4313 && *sym_hash != NULL)
4315 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4316 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4317 (*sym_hash)->u.toc_indx = output_index;
4320 output_index += add;
4323 esym += add * isymesz;
4324 isymp += add;
4325 csectpp += add;
4326 sym_hash += add;
4327 debug_index += add;
4328 ++indexp;
4329 for (--add; add > 0; --add)
4330 *indexp++ = -1;
4333 /* Now write out the symbols that we decided to keep. */
4335 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4336 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4337 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4338 isymp = flinfo->internal_syms;
4339 indexp = flinfo->sym_indices;
4340 csectpp = xcoff_data (input_bfd)->csects;
4341 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4342 debug_index = xcoff_data (input_bfd)->debug_indices;
4343 outsym = flinfo->outsyms;
4344 incls = 0;
4345 oline = NULL;
4346 while (esym < esym_end)
4348 int add;
4350 add = 1 + isymp->n_numaux;
4352 if (*indexp < 0)
4353 esym += add * isymesz;
4354 else
4356 struct internal_syment isym;
4357 int i;
4359 /* Adjust the symbol in order to output it. */
4360 isym = *isymp;
4361 if (isym._n._n_n._n_zeroes == 0
4362 && isym._n._n_n._n_offset != 0)
4364 /* This symbol has a long name. Enter it in the string
4365 table we are building. If *debug_index != -1, the
4366 name has already been entered in the .debug section. */
4367 if (*debug_index >= 0)
4368 isym._n._n_n._n_offset = *debug_index;
4369 else
4371 const char *name;
4372 bfd_size_type indx;
4374 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4376 if (name == NULL)
4377 return FALSE;
4378 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4379 if (indx == (bfd_size_type) -1)
4380 return FALSE;
4381 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4385 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4386 multiple definition problems when linking a shared object
4387 statically. (The native linker doesn't enter __rtinit into
4388 the normal table at all, but having a local symbol can make
4389 the objdump output easier to read.) */
4390 if (isym.n_sclass == C_EXT
4391 && *sym_hash
4392 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4393 isym.n_sclass = C_HIDEXT;
4395 /* The value of a C_FILE symbol is the symbol index of the
4396 next C_FILE symbol. The value of the last C_FILE symbol
4397 is -1. We try to get this right, below, just before we
4398 write the symbols out, but in the general case we may
4399 have to write the symbol out twice. */
4400 if (isym.n_sclass == C_FILE)
4402 if (flinfo->last_file_index != -1
4403 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4405 /* We must correct the value of the last C_FILE entry. */
4406 flinfo->last_file.n_value = *indexp;
4407 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4409 /* The last C_FILE symbol is in this input file. */
4410 bfd_coff_swap_sym_out (output_bfd,
4411 (void *) &flinfo->last_file,
4412 (void *) (flinfo->outsyms
4413 + ((flinfo->last_file_index
4414 - syment_base)
4415 * osymesz)));
4417 else
4419 /* We have already written out the last C_FILE
4420 symbol. We need to write it out again. We
4421 borrow *outsym temporarily. */
4422 file_ptr pos;
4424 bfd_coff_swap_sym_out (output_bfd,
4425 (void *) &flinfo->last_file,
4426 (void *) outsym);
4428 pos = obj_sym_filepos (output_bfd);
4429 pos += flinfo->last_file_index * osymesz;
4430 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4431 || (bfd_bwrite (outsym, osymesz, output_bfd)
4432 != osymesz))
4433 return FALSE;
4437 flinfo->last_file_index = *indexp;
4438 flinfo->last_file = isym;
4441 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4442 into the line numbers. We update the symbol values when
4443 we handle the line numbers. */
4444 if (isym.n_sclass == C_BINCL
4445 || isym.n_sclass == C_EINCL)
4447 isym.n_value = flinfo->line_filepos;
4448 ++incls;
4450 /* The value of a C_BSTAT symbol is the symbol table
4451 index of the containing csect. */
4452 else if (isym.n_sclass == C_BSTAT)
4454 bfd_vma indx;
4456 indx = isym.n_value;
4457 if (indx < obj_raw_syment_count (input_bfd))
4459 long symindx;
4461 symindx = flinfo->sym_indices[indx];
4462 if (symindx < 0)
4463 isym.n_value = 0;
4464 else
4465 isym.n_value = symindx;
4468 else if (isym.n_sclass != C_ESTAT
4469 && isym.n_sclass != C_DECL
4470 && isym.n_scnum > 0)
4472 isym.n_scnum = (*csectpp)->output_section->target_index;
4473 isym.n_value += ((*csectpp)->output_section->vma
4474 + (*csectpp)->output_offset
4475 - (*csectpp)->vma);
4478 /* Output the symbol. */
4479 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4481 esym += isymesz;
4482 outsym += osymesz;
4484 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4486 union internal_auxent aux;
4488 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4489 isymp->n_sclass, i, isymp->n_numaux,
4490 (void *) &aux);
4492 if (isymp->n_sclass == C_FILE)
4494 /* This is the file name (or some comment put in by
4495 the compiler). If it is long, we must put it in
4496 the string table. */
4497 if (aux.x_file.x_n.x_zeroes == 0
4498 && aux.x_file.x_n.x_offset != 0)
4500 const char *filename;
4501 bfd_size_type indx;
4503 BFD_ASSERT (aux.x_file.x_n.x_offset
4504 >= STRING_SIZE_SIZE);
4505 if (strings == NULL)
4507 strings = _bfd_coff_read_string_table (input_bfd);
4508 if (strings == NULL)
4509 return FALSE;
4511 if ((bfd_size_type) aux.x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd))
4512 filename = _("<corrupt>");
4513 else
4514 filename = strings + aux.x_file.x_n.x_offset;
4515 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4516 hash, copy);
4517 if (indx == (bfd_size_type) -1)
4518 return FALSE;
4519 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4522 else if (CSECT_SYM_P (isymp->n_sclass)
4523 && i + 1 == isymp->n_numaux)
4526 /* We don't support type checking. I don't know if
4527 anybody does. */
4528 aux.x_csect.x_parmhash = 0;
4529 /* I don't think anybody uses these fields, but we'd
4530 better clobber them just in case. */
4531 aux.x_csect.x_stab = 0;
4532 aux.x_csect.x_snstab = 0;
4534 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4536 unsigned long indx;
4538 indx = aux.x_csect.x_scnlen.l;
4539 if (indx < obj_raw_syment_count (input_bfd))
4541 long symindx;
4543 symindx = flinfo->sym_indices[indx];
4544 if (symindx < 0)
4546 aux.x_csect.x_scnlen.l = 0;
4548 else
4550 aux.x_csect.x_scnlen.l = symindx;
4555 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4557 unsigned long indx;
4559 if (ISFCN (isymp->n_type)
4560 || ISTAG (isymp->n_sclass)
4561 || isymp->n_sclass == C_BLOCK
4562 || isymp->n_sclass == C_FCN)
4564 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4565 if (indx > 0
4566 && indx < obj_raw_syment_count (input_bfd))
4568 /* We look forward through the symbol for
4569 the index of the next symbol we are going
4570 to include. I don't know if this is
4571 entirely right. */
4572 while (flinfo->sym_indices[indx] < 0
4573 && indx < obj_raw_syment_count (input_bfd))
4574 ++indx;
4575 if (indx >= obj_raw_syment_count (input_bfd))
4576 indx = output_index;
4577 else
4578 indx = flinfo->sym_indices[indx];
4579 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4584 indx = aux.x_sym.x_tagndx.l;
4585 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4587 long symindx;
4589 symindx = flinfo->sym_indices[indx];
4590 if (symindx < 0)
4591 aux.x_sym.x_tagndx.l = 0;
4592 else
4593 aux.x_sym.x_tagndx.l = symindx;
4598 /* Copy over the line numbers, unless we are stripping
4599 them. We do this on a symbol by symbol basis in
4600 order to more easily handle garbage collection. */
4601 if (CSECT_SYM_P (isymp->n_sclass)
4602 && i == 0
4603 && isymp->n_numaux > 1
4604 && ISFCN (isymp->n_type)
4605 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4607 if (*lineno_counts == 0)
4608 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4609 else
4611 asection *enclosing;
4612 unsigned int enc_count;
4613 bfd_signed_vma linoff;
4614 struct internal_lineno lin;
4615 bfd_byte *linp;
4616 bfd_byte *linpend;
4617 bfd_vma offset;
4618 file_ptr pos;
4619 bfd_size_type amt;
4621 /* Read in the enclosing section's line-number
4622 information, if we haven't already. */
4623 o = *csectpp;
4624 enclosing = xcoff_section_data (abfd, o)->enclosing;
4625 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4626 if (oline != enclosing)
4628 pos = enclosing->line_filepos;
4629 amt = linesz * enc_count;
4630 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4631 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4632 != amt))
4633 return FALSE;
4634 oline = enclosing;
4637 /* Copy across the first entry, adjusting its
4638 symbol index. */
4639 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4640 - enclosing->line_filepos);
4641 linp = flinfo->linenos + linoff;
4642 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4643 lin.l_addr.l_symndx = *indexp;
4644 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4646 /* Copy the other entries, adjusting their addresses. */
4647 linpend = linp + *lineno_counts * linesz;
4648 offset = (o->output_section->vma
4649 + o->output_offset
4650 - o->vma);
4651 for (linp += linesz; linp < linpend; linp += linesz)
4653 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4654 lin.l_addr.l_paddr += offset;
4655 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4658 /* Write out the entries we've just processed. */
4659 pos = (o->output_section->line_filepos
4660 + o->output_section->lineno_count * linesz);
4661 amt = linesz * *lineno_counts;
4662 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4663 || bfd_bwrite (flinfo->linenos + linoff,
4664 amt, output_bfd) != amt)
4665 return FALSE;
4666 o->output_section->lineno_count += *lineno_counts;
4668 /* Record the offset of the symbol's line numbers
4669 in the output file. */
4670 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4672 if (incls > 0)
4674 struct internal_syment *iisp, *iispend;
4675 long *iindp;
4676 bfd_byte *oos;
4677 bfd_vma range_start, range_end;
4678 int iiadd;
4680 /* Update any C_BINCL or C_EINCL symbols
4681 that refer to a line number in the
4682 range we just output. */
4683 iisp = flinfo->internal_syms;
4684 iispend = iisp + obj_raw_syment_count (input_bfd);
4685 iindp = flinfo->sym_indices;
4686 oos = flinfo->outsyms;
4687 range_start = enclosing->line_filepos + linoff;
4688 range_end = range_start + *lineno_counts * linesz;
4689 while (iisp < iispend)
4691 if (*iindp >= 0
4692 && (iisp->n_sclass == C_BINCL
4693 || iisp->n_sclass == C_EINCL)
4694 && iisp->n_value >= range_start
4695 && iisp->n_value < range_end)
4697 struct internal_syment iis;
4699 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4700 iis.n_value = (iisp->n_value
4701 - range_start
4702 + pos);
4703 bfd_coff_swap_sym_out (output_bfd,
4704 &iis, oos);
4705 --incls;
4708 iiadd = 1 + iisp->n_numaux;
4709 if (*iindp >= 0)
4710 oos += iiadd * osymesz;
4711 iisp += iiadd;
4712 iindp += iiadd;
4718 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4719 isymp->n_sclass, i, isymp->n_numaux,
4720 (void *) outsym);
4721 outsym += osymesz;
4722 esym += isymesz;
4726 sym_hash += add;
4727 indexp += add;
4728 isymp += add;
4729 csectpp += add;
4730 lineno_counts += add;
4731 debug_index += add;
4734 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4735 symbol will be the first symbol in the next input file. In the
4736 normal case, this will save us from writing out the C_FILE symbol
4737 again. */
4738 if (flinfo->last_file_index != -1
4739 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4741 flinfo->last_file.n_value = output_index;
4742 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4743 (void *) (flinfo->outsyms
4744 + ((flinfo->last_file_index - syment_base)
4745 * osymesz)));
4748 /* Write the modified symbols to the output file. */
4749 if (outsym > flinfo->outsyms)
4751 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4752 bfd_size_type amt = outsym - flinfo->outsyms;
4753 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4754 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4755 return FALSE;
4757 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4758 + (outsym - flinfo->outsyms) / osymesz)
4759 == output_index);
4761 obj_raw_syment_count (output_bfd) = output_index;
4764 /* Don't let the linker relocation routines discard the symbols. */
4765 keep_syms = obj_coff_keep_syms (input_bfd);
4766 obj_coff_keep_syms (input_bfd) = TRUE;
4768 /* Relocate the contents of each section. */
4769 for (o = input_bfd->sections; o != NULL; o = o->next)
4771 bfd_byte *contents;
4773 if (! o->linker_mark)
4774 /* This section was omitted from the link. */
4775 continue;
4777 if ((o->flags & SEC_HAS_CONTENTS) == 0
4778 || o->size == 0
4779 || (o->flags & SEC_IN_MEMORY) != 0)
4780 continue;
4782 /* We have set filepos correctly for the sections we created to
4783 represent csects, so bfd_get_section_contents should work. */
4784 if (coff_section_data (input_bfd, o) != NULL
4785 && coff_section_data (input_bfd, o)->contents != NULL)
4786 contents = coff_section_data (input_bfd, o)->contents;
4787 else
4789 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4790 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4791 goto err_out;
4792 contents = flinfo->contents;
4795 if ((o->flags & SEC_RELOC) != 0)
4797 int target_index;
4798 struct internal_reloc *internal_relocs;
4799 struct internal_reloc *irel;
4800 bfd_vma offset;
4801 struct internal_reloc *irelend;
4802 struct xcoff_link_hash_entry **rel_hash;
4803 long r_symndx;
4805 /* Read in the relocs. */
4806 target_index = o->output_section->target_index;
4807 internal_relocs = (xcoff_read_internal_relocs
4808 (input_bfd, o, FALSE, flinfo->external_relocs,
4809 TRUE,
4810 (flinfo->section_info[target_index].relocs
4811 + o->output_section->reloc_count)));
4812 if (internal_relocs == NULL)
4813 goto err_out;
4815 /* Call processor specific code to relocate the section
4816 contents. */
4817 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4818 input_bfd, o,
4819 contents,
4820 internal_relocs,
4821 flinfo->internal_syms,
4822 xcoff_data (input_bfd)->csects))
4823 goto err_out;
4825 offset = o->output_section->vma + o->output_offset - o->vma;
4826 irel = internal_relocs;
4827 irelend = irel + o->reloc_count;
4828 rel_hash = (flinfo->section_info[target_index].rel_hashes
4829 + o->output_section->reloc_count);
4830 for (; irel < irelend; irel++, rel_hash++)
4832 struct xcoff_link_hash_entry *h = NULL;
4834 *rel_hash = NULL;
4836 /* Adjust the reloc address and symbol index. */
4838 irel->r_vaddr += offset;
4840 r_symndx = irel->r_symndx;
4842 if (r_symndx == -1)
4843 h = NULL;
4844 else
4845 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4847 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4849 if (h != NULL
4850 && h->smclas != XMC_TD
4851 && (irel->r_type == R_TOC
4852 || irel->r_type == R_GL
4853 || irel->r_type == R_TCL
4854 || irel->r_type == R_TRL
4855 || irel->r_type == R_TRLA))
4857 /* This is a TOC relative reloc with a symbol
4858 attached. The symbol should be the one which
4859 this reloc is for. We want to make this
4860 reloc against the TOC address of the symbol,
4861 not the symbol itself. */
4862 BFD_ASSERT (h->toc_section != NULL);
4863 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4864 if (h->u.toc_indx != -1)
4865 irel->r_symndx = h->u.toc_indx;
4866 else
4868 struct xcoff_toc_rel_hash *n;
4869 struct xcoff_link_section_info *si;
4870 size_t amt;
4872 amt = sizeof (* n);
4873 n = bfd_alloc (flinfo->output_bfd, amt);
4874 if (n == NULL)
4875 goto err_out;
4876 si = flinfo->section_info + target_index;
4877 n->next = si->toc_rel_hashes;
4878 n->h = h;
4879 n->rel = irel;
4880 si->toc_rel_hashes = n;
4883 else if (h != NULL)
4885 /* This is a global symbol. */
4886 if (h->indx >= 0)
4887 irel->r_symndx = h->indx;
4888 else
4890 /* This symbol is being written at the end
4891 of the file, and we do not yet know the
4892 symbol index. We save the pointer to the
4893 hash table entry in the rel_hash list.
4894 We set the indx field to -2 to indicate
4895 that this symbol must not be stripped. */
4896 *rel_hash = h;
4897 h->indx = -2;
4900 else
4902 long indx;
4904 indx = flinfo->sym_indices[r_symndx];
4906 if (indx == -1)
4908 struct internal_syment *is;
4910 /* Relocations against a TC0 TOC anchor are
4911 automatically transformed to be against
4912 the TOC anchor in the output file. */
4913 is = flinfo->internal_syms + r_symndx;
4914 if (is->n_sclass == C_HIDEXT
4915 && is->n_numaux > 0)
4917 void * auxptr;
4918 union internal_auxent aux;
4920 auxptr = ((void *)
4921 (((bfd_byte *)
4922 obj_coff_external_syms (input_bfd))
4923 + ((r_symndx + is->n_numaux)
4924 * isymesz)));
4925 bfd_coff_swap_aux_in (input_bfd, auxptr,
4926 is->n_type, is->n_sclass,
4927 is->n_numaux - 1,
4928 is->n_numaux,
4929 (void *) &aux);
4930 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4931 && aux.x_csect.x_smclas == XMC_TC0)
4932 indx = flinfo->toc_symindx;
4936 if (indx != -1)
4937 irel->r_symndx = indx;
4938 else
4941 struct internal_syment *is;
4943 const char *name;
4944 char buf[SYMNMLEN + 1];
4946 /* This reloc is against a symbol we are
4947 stripping. It would be possible to handle
4948 this case, but I don't think it's worth it. */
4949 is = flinfo->internal_syms + r_symndx;
4951 if (is->n_sclass != C_DWARF)
4953 name = (_bfd_coff_internal_syment_name
4954 (input_bfd, is, buf));
4956 if (name == NULL)
4957 goto err_out;
4959 (*flinfo->info->callbacks->unattached_reloc)
4960 (flinfo->info, name,
4961 input_bfd, o, irel->r_vaddr);
4967 if ((o->flags & SEC_DEBUGGING) == 0
4968 && xcoff_need_ldrel_p (flinfo->info, irel, h))
4970 asection *sec;
4972 if (r_symndx == -1)
4973 sec = NULL;
4974 else if (h == NULL)
4975 sec = xcoff_data (input_bfd)->csects[r_symndx];
4976 else
4977 sec = xcoff_symbol_section (h);
4978 if (!xcoff_create_ldrel (output_bfd, flinfo,
4979 o->output_section, input_bfd,
4980 irel, sec, h))
4981 goto err_out;
4985 o->output_section->reloc_count += o->reloc_count;
4988 /* Write out the modified section contents. */
4989 if (! bfd_set_section_contents (output_bfd, o->output_section,
4990 contents, (file_ptr) o->output_offset,
4991 o->size))
4992 goto err_out;
4995 obj_coff_keep_syms (input_bfd) = keep_syms;
4997 if (! flinfo->info->keep_memory)
4999 if (! _bfd_coff_free_symbols (input_bfd))
5000 return FALSE;
5003 return TRUE;
5005 err_out:
5006 obj_coff_keep_syms (input_bfd) = keep_syms;
5007 return FALSE;
5010 #undef N_TMASK
5011 #undef N_BTSHFT
5013 /* Sort relocs by VMA. This is called via qsort. */
5015 static int
5016 xcoff_sort_relocs (const void * p1, const void * p2)
5018 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5019 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5021 if (r1->r_vaddr > r2->r_vaddr)
5022 return 1;
5023 else if (r1->r_vaddr < r2->r_vaddr)
5024 return -1;
5025 else
5026 return 0;
5029 /* Return true if section SEC is a TOC section. */
5031 static inline bfd_boolean
5032 xcoff_toc_section_p (asection *sec)
5034 const char *name;
5036 name = sec->name;
5037 if (name[0] == '.' && name[1] == 't')
5039 if (name[2] == 'c')
5041 if (name[3] == '0' && name[4] == 0)
5042 return TRUE;
5043 if (name[3] == 0)
5044 return TRUE;
5046 if (name[2] == 'd' && name[3] == 0)
5047 return TRUE;
5049 return FALSE;
5052 /* See if the link requires a TOC (it usually does!). If so, find a
5053 good place to put the TOC anchor csect, and write out the associated
5054 symbol. */
5056 static bfd_boolean
5057 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5059 bfd_vma toc_start, toc_end, start, end, best_address;
5060 asection *sec;
5061 bfd *input_bfd;
5062 int section_index;
5063 struct internal_syment irsym;
5064 union internal_auxent iraux;
5065 file_ptr pos;
5066 size_t size;
5068 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5069 index of a csect at the beginning of the TOC. */
5070 toc_start = ~(bfd_vma) 0;
5071 toc_end = 0;
5072 section_index = -1;
5073 for (input_bfd = flinfo->info->input_bfds;
5074 input_bfd != NULL;
5075 input_bfd = input_bfd->link.next)
5076 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5077 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5079 start = sec->output_section->vma + sec->output_offset;
5080 if (toc_start > start)
5082 toc_start = start;
5083 section_index = sec->output_section->target_index;
5086 end = start + sec->size;
5087 if (toc_end < end)
5088 toc_end = end;
5091 /* There's no need for a TC0 symbol if we don't have a TOC. */
5092 if (toc_end < toc_start)
5094 xcoff_data (output_bfd)->toc = toc_start;
5095 return TRUE;
5098 if (toc_end - toc_start < 0x8000)
5099 /* Every TOC csect can be accessed from TOC_START. */
5100 best_address = toc_start;
5101 else
5103 /* Find the lowest TOC csect that is still within range of TOC_END. */
5104 best_address = toc_end;
5105 for (input_bfd = flinfo->info->input_bfds;
5106 input_bfd != NULL;
5107 input_bfd = input_bfd->link.next)
5108 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5109 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5111 start = sec->output_section->vma + sec->output_offset;
5112 if (start < best_address
5113 && start + 0x8000 >= toc_end)
5115 best_address = start;
5116 section_index = sec->output_section->target_index;
5120 /* Make sure that the start of the TOC is also within range. */
5121 if (best_address > toc_start + 0x8000)
5123 _bfd_error_handler
5124 (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc "
5125 "when compiling"),
5126 (uint64_t) (toc_end - toc_start));
5127 bfd_set_error (bfd_error_file_too_big);
5128 return FALSE;
5132 /* Record the chosen TOC value. */
5133 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5134 xcoff_data (output_bfd)->toc = best_address;
5135 xcoff_data (output_bfd)->sntoc = section_index;
5137 /* Fill out the TC0 symbol. */
5138 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
5139 &irsym, "TOC"))
5140 return FALSE;
5141 irsym.n_value = best_address;
5142 irsym.n_scnum = section_index;
5143 irsym.n_sclass = C_HIDEXT;
5144 irsym.n_type = T_NULL;
5145 irsym.n_numaux = 1;
5146 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5148 /* Fill out the auxillary csect information. */
5149 memset (&iraux, 0, sizeof iraux);
5150 iraux.x_csect.x_smtyp = XTY_SD;
5151 iraux.x_csect.x_smclas = XMC_TC0;
5152 iraux.x_csect.x_scnlen.l = 0;
5153 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5154 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5156 /* Write the contents to the file. */
5157 pos = obj_sym_filepos (output_bfd);
5158 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5159 size = 2 * bfd_coff_symesz (output_bfd);
5160 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5161 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5162 return FALSE;
5163 obj_raw_syment_count (output_bfd) += 2;
5165 return TRUE;
5168 /* Write out a non-XCOFF global symbol. */
5170 static bfd_boolean
5171 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
5173 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
5174 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5175 bfd *output_bfd;
5176 bfd_byte *outsym;
5177 struct internal_syment isym;
5178 union internal_auxent aux;
5179 bfd_boolean result;
5180 file_ptr pos;
5181 bfd_size_type amt;
5183 output_bfd = flinfo->output_bfd;
5184 outsym = flinfo->outsyms;
5186 if (h->root.type == bfd_link_hash_warning)
5188 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5189 if (h->root.type == bfd_link_hash_new)
5190 return TRUE;
5193 /* If this symbol was garbage collected, just skip it. */
5194 if (xcoff_hash_table (flinfo->info)->gc
5195 && (h->flags & XCOFF_MARK) == 0)
5196 return TRUE;
5198 /* If we need a .loader section entry, write it out. */
5199 if (h->ldsym != NULL)
5201 struct internal_ldsym *ldsym;
5202 bfd *impbfd;
5204 ldsym = h->ldsym;
5206 if (h->root.type == bfd_link_hash_undefined
5207 || h->root.type == bfd_link_hash_undefweak)
5210 ldsym->l_value = 0;
5211 ldsym->l_scnum = N_UNDEF;
5212 ldsym->l_smtype = XTY_ER;
5213 impbfd = h->root.u.undef.abfd;
5216 else if (h->root.type == bfd_link_hash_defined
5217 || h->root.type == bfd_link_hash_defweak)
5219 asection *sec;
5221 sec = h->root.u.def.section;
5222 ldsym->l_value = (sec->output_section->vma
5223 + sec->output_offset
5224 + h->root.u.def.value);
5225 ldsym->l_scnum = sec->output_section->target_index;
5226 ldsym->l_smtype = XTY_SD;
5227 impbfd = sec->owner;
5230 else
5231 abort ();
5233 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5234 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5235 || (h->flags & XCOFF_IMPORT) != 0)
5236 /* Clear l_smtype
5237 Import symbols are defined so the check above will make
5238 the l_smtype XTY_SD. But this is not correct, it should
5239 be cleared. */
5240 ldsym->l_smtype |= L_IMPORT;
5242 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5243 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5244 || (h->flags & XCOFF_EXPORT) != 0)
5245 ldsym->l_smtype |= L_EXPORT;
5247 if ((h->flags & XCOFF_ENTRY) != 0)
5248 ldsym->l_smtype |= L_ENTRY;
5250 if ((h->flags & XCOFF_RTINIT) != 0)
5251 ldsym->l_smtype = XTY_SD;
5253 ldsym->l_smclas = h->smclas;
5255 if (ldsym->l_smtype & L_IMPORT)
5257 if ((h->root.type == bfd_link_hash_defined
5258 || h->root.type == bfd_link_hash_defweak)
5259 && (h->root.u.def.value != 0))
5260 ldsym->l_smclas = XMC_XO;
5262 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5263 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5264 ldsym->l_smclas = XMC_SV3264;
5266 else if (h->flags & XCOFF_SYSCALL32)
5267 ldsym->l_smclas = XMC_SV;
5269 else if (h->flags & XCOFF_SYSCALL64)
5270 ldsym->l_smclas = XMC_SV64;
5273 if (ldsym->l_ifile == -(bfd_size_type) 1)
5275 ldsym->l_ifile = 0;
5277 else if (ldsym->l_ifile == 0)
5279 if ((ldsym->l_smtype & L_IMPORT) == 0)
5280 ldsym->l_ifile = 0;
5281 else if (impbfd == NULL)
5282 ldsym->l_ifile = 0;
5283 else
5285 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5286 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5290 ldsym->l_parm = 0;
5292 BFD_ASSERT (h->ldindx >= 0);
5294 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5295 (flinfo->ldsym +
5296 (h->ldindx - 3)
5297 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5298 h->ldsym = NULL;
5301 /* If this symbol needs global linkage code, write it out. */
5302 if (h->root.type == bfd_link_hash_defined
5303 && (h->root.u.def.section
5304 == xcoff_hash_table (flinfo->info)->linkage_section))
5306 bfd_byte *p;
5307 bfd_vma tocoff;
5308 unsigned int i;
5310 p = h->root.u.def.section->contents + h->root.u.def.value;
5312 /* The first instruction in the global linkage code loads a
5313 specific TOC element. */
5314 tocoff = (h->descriptor->toc_section->output_section->vma
5315 + h->descriptor->toc_section->output_offset
5316 - xcoff_data (output_bfd)->toc);
5318 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5319 tocoff += h->descriptor->u.toc_offset;
5321 /* The first instruction in the glink code needs to be
5322 cooked to hold the correct offset in the toc. The
5323 rest are just output raw. */
5324 bfd_put_32 (output_bfd,
5325 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5327 /* Start with i == 1 to get past the first instruction done above
5328 The /4 is because the glink code is in bytes and we are going
5329 4 at a pop. */
5330 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5331 bfd_put_32 (output_bfd,
5332 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5333 &p[4 * i]);
5336 /* If we created a TOC entry for this symbol, write out the required
5337 relocs. */
5338 if ((h->flags & XCOFF_SET_TOC) != 0)
5340 asection *tocsec;
5341 asection *osec;
5342 int oindx;
5343 struct internal_reloc *irel;
5344 struct internal_syment irsym;
5345 union internal_auxent iraux;
5347 tocsec = h->toc_section;
5348 osec = tocsec->output_section;
5349 oindx = osec->target_index;
5350 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5351 irel->r_vaddr = (osec->vma
5352 + tocsec->output_offset
5353 + h->u.toc_offset);
5355 if (h->indx >= 0)
5356 irel->r_symndx = h->indx;
5357 else
5359 h->indx = -2;
5360 irel->r_symndx = obj_raw_syment_count (output_bfd);
5363 BFD_ASSERT (h->ldindx >= 0);
5365 /* Initialize the aux union here instead of closer to when it is
5366 written out below because the length of the csect depends on
5367 whether the output is 32 or 64 bit. */
5368 memset (&iraux, 0, sizeof iraux);
5369 iraux.x_csect.x_smtyp = XTY_SD;
5370 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5371 iraux.x_csect.x_smclas = XMC_TC;
5373 /* 32 bit uses a 32 bit R_POS to do the relocations
5374 64 bit uses a 64 bit R_POS to do the relocations
5376 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5378 Which one is determined by the backend. */
5379 if (bfd_xcoff_is_xcoff64 (output_bfd))
5381 irel->r_size = 63;
5382 iraux.x_csect.x_scnlen.l = 8;
5384 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5386 irel->r_size = 31;
5387 iraux.x_csect.x_scnlen.l = 4;
5389 else
5390 return FALSE;
5392 irel->r_type = R_POS;
5393 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5394 ++osec->reloc_count;
5396 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5397 output_bfd, irel, NULL, h))
5398 return FALSE;
5400 /* We need to emit a symbol to define a csect which holds
5401 the reloc. */
5402 if (flinfo->info->strip != strip_all)
5404 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info,
5405 flinfo->strtab,
5406 &irsym, h->root.root.string);
5407 if (!result)
5408 return FALSE;
5410 irsym.n_value = irel->r_vaddr;
5411 irsym.n_scnum = osec->target_index;
5412 irsym.n_sclass = C_HIDEXT;
5413 irsym.n_type = T_NULL;
5414 irsym.n_numaux = 1;
5416 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5417 outsym += bfd_coff_symesz (output_bfd);
5419 /* Note : iraux is initialized above. */
5420 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5421 0, 1, (void *) outsym);
5422 outsym += bfd_coff_auxesz (output_bfd);
5424 if (h->indx >= 0)
5426 /* We aren't going to write out the symbols below, so we
5427 need to write them out now. */
5428 pos = obj_sym_filepos (output_bfd);
5429 pos += (obj_raw_syment_count (output_bfd)
5430 * bfd_coff_symesz (output_bfd));
5431 amt = outsym - flinfo->outsyms;
5432 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5433 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5434 return FALSE;
5435 obj_raw_syment_count (output_bfd) +=
5436 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5438 outsym = flinfo->outsyms;
5443 /* If this symbol is a specially defined function descriptor, write
5444 it out. The first word is the address of the function code
5445 itself, the second word is the address of the TOC, and the third
5446 word is zero.
5448 32 bit vs 64 bit
5449 The addresses for the 32 bit will take 4 bytes and the addresses
5450 for 64 bit will take 8 bytes. Similar for the relocs. This type
5451 of logic was also done above to create a TOC entry in
5452 xcoff_write_global_symbol. */
5453 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5454 && h->root.type == bfd_link_hash_defined
5455 && (h->root.u.def.section
5456 == xcoff_hash_table (flinfo->info)->descriptor_section))
5458 asection *sec;
5459 asection *osec;
5460 int oindx;
5461 bfd_byte *p;
5462 struct xcoff_link_hash_entry *hentry;
5463 asection *esec;
5464 struct internal_reloc *irel;
5465 asection *tsec;
5466 unsigned int reloc_size, byte_size;
5468 if (bfd_xcoff_is_xcoff64 (output_bfd))
5470 reloc_size = 63;
5471 byte_size = 8;
5473 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5475 reloc_size = 31;
5476 byte_size = 4;
5478 else
5479 return FALSE;
5481 sec = h->root.u.def.section;
5482 osec = sec->output_section;
5483 oindx = osec->target_index;
5484 p = sec->contents + h->root.u.def.value;
5486 hentry = h->descriptor;
5487 BFD_ASSERT (hentry != NULL
5488 && (hentry->root.type == bfd_link_hash_defined
5489 || hentry->root.type == bfd_link_hash_defweak));
5490 esec = hentry->root.u.def.section;
5492 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5493 irel->r_vaddr = (osec->vma
5494 + sec->output_offset
5495 + h->root.u.def.value);
5496 irel->r_symndx = esec->output_section->target_index;
5497 irel->r_type = R_POS;
5498 irel->r_size = reloc_size;
5499 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5500 ++osec->reloc_count;
5502 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5503 output_bfd, irel, esec, NULL))
5504 return FALSE;
5506 /* There are three items to write out,
5507 the address of the code
5508 the address of the toc anchor
5509 the environment pointer.
5510 We are ignoring the environment pointer. So set it to zero. */
5511 if (bfd_xcoff_is_xcoff64 (output_bfd))
5513 bfd_put_64 (output_bfd,
5514 (esec->output_section->vma + esec->output_offset
5515 + hentry->root.u.def.value),
5517 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5518 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5520 else
5522 /* 32 bit backend
5523 This logic was already called above so the error case where
5524 the backend is neither has already been checked. */
5525 bfd_put_32 (output_bfd,
5526 (esec->output_section->vma + esec->output_offset
5527 + hentry->root.u.def.value),
5529 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5530 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5533 tsec = coff_section_from_bfd_index (output_bfd,
5534 xcoff_data (output_bfd)->sntoc);
5536 ++irel;
5537 irel->r_vaddr = (osec->vma
5538 + sec->output_offset
5539 + h->root.u.def.value
5540 + byte_size);
5541 irel->r_symndx = tsec->output_section->target_index;
5542 irel->r_type = R_POS;
5543 irel->r_size = reloc_size;
5544 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5545 ++osec->reloc_count;
5547 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5548 output_bfd, irel, tsec, NULL))
5549 return FALSE;
5552 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5554 BFD_ASSERT (outsym == flinfo->outsyms);
5555 return TRUE;
5558 if (h->indx != -2
5559 && (flinfo->info->strip == strip_all
5560 || (flinfo->info->strip == strip_some
5561 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5562 FALSE, FALSE) == NULL)))
5564 BFD_ASSERT (outsym == flinfo->outsyms);
5565 return TRUE;
5568 if (h->indx != -2
5569 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5571 BFD_ASSERT (outsym == flinfo->outsyms);
5572 return TRUE;
5575 memset (&aux, 0, sizeof aux);
5577 h->indx = obj_raw_syment_count (output_bfd);
5579 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
5580 &isym, h->root.root.string);
5581 if (!result)
5582 return FALSE;
5584 if (h->root.type == bfd_link_hash_undefined
5585 || h->root.type == bfd_link_hash_undefweak)
5587 isym.n_value = 0;
5588 isym.n_scnum = N_UNDEF;
5589 if (h->root.type == bfd_link_hash_undefweak
5590 && C_WEAKEXT == C_AIX_WEAKEXT)
5591 isym.n_sclass = C_WEAKEXT;
5592 else
5593 isym.n_sclass = C_EXT;
5594 aux.x_csect.x_smtyp = XTY_ER;
5596 else if ((h->root.type == bfd_link_hash_defined
5597 || h->root.type == bfd_link_hash_defweak)
5598 && h->smclas == XMC_XO)
5600 BFD_ASSERT (bfd_is_abs_symbol (&h->root));
5601 isym.n_value = h->root.u.def.value;
5602 isym.n_scnum = N_UNDEF;
5603 if (h->root.type == bfd_link_hash_defweak
5604 && C_WEAKEXT == C_AIX_WEAKEXT)
5605 isym.n_sclass = C_WEAKEXT;
5606 else
5607 isym.n_sclass = C_EXT;
5608 aux.x_csect.x_smtyp = XTY_ER;
5610 else if (h->root.type == bfd_link_hash_defined
5611 || h->root.type == bfd_link_hash_defweak)
5613 struct xcoff_link_size_list *l;
5615 isym.n_value = (h->root.u.def.section->output_section->vma
5616 + h->root.u.def.section->output_offset
5617 + h->root.u.def.value);
5618 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5619 isym.n_scnum = N_ABS;
5620 else
5621 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5622 isym.n_sclass = C_HIDEXT;
5623 aux.x_csect.x_smtyp = XTY_SD;
5625 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5627 for (l = xcoff_hash_table (flinfo->info)->size_list;
5628 l != NULL;
5629 l = l->next)
5631 if (l->h == h)
5633 aux.x_csect.x_scnlen.l = l->size;
5634 break;
5639 else if (h->root.type == bfd_link_hash_common)
5641 isym.n_value = (h->root.u.c.p->section->output_section->vma
5642 + h->root.u.c.p->section->output_offset);
5643 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5644 isym.n_sclass = C_EXT;
5645 aux.x_csect.x_smtyp = XTY_CM;
5646 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5648 else
5649 abort ();
5651 isym.n_type = T_NULL;
5652 isym.n_numaux = 1;
5654 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5655 outsym += bfd_coff_symesz (output_bfd);
5657 aux.x_csect.x_smclas = h->smclas;
5658 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5659 (void *) outsym);
5660 outsym += bfd_coff_auxesz (output_bfd);
5662 if ((h->root.type == bfd_link_hash_defined
5663 || h->root.type == bfd_link_hash_defweak)
5664 && h->smclas != XMC_XO)
5666 /* We just output an SD symbol. Now output an LD symbol. */
5667 h->indx += 2;
5669 if (h->root.type == bfd_link_hash_defweak
5670 && C_WEAKEXT == C_AIX_WEAKEXT)
5671 isym.n_sclass = C_WEAKEXT;
5672 else
5673 isym.n_sclass = C_EXT;
5674 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5675 outsym += bfd_coff_symesz (output_bfd);
5677 aux.x_csect.x_smtyp = XTY_LD;
5678 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5679 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5680 (void *) outsym);
5681 outsym += bfd_coff_auxesz (output_bfd);
5684 pos = obj_sym_filepos (output_bfd);
5685 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5686 amt = outsym - flinfo->outsyms;
5687 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5688 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5689 return FALSE;
5690 obj_raw_syment_count (output_bfd) +=
5691 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5693 return TRUE;
5696 /* Handle a link order which is supposed to generate a reloc. */
5698 static bfd_boolean
5699 xcoff_reloc_link_order (bfd *output_bfd,
5700 struct xcoff_final_link_info *flinfo,
5701 asection *output_section,
5702 struct bfd_link_order *link_order)
5704 reloc_howto_type *howto;
5705 struct xcoff_link_hash_entry *h;
5706 asection *hsec;
5707 bfd_vma hval;
5708 bfd_vma addend;
5709 struct internal_reloc *irel;
5710 struct xcoff_link_hash_entry **rel_hash_ptr;
5712 if (link_order->type == bfd_section_reloc_link_order)
5713 /* We need to somehow locate a symbol in the right section. The
5714 symbol must either have a value of zero, or we must adjust
5715 the addend by the value of the symbol. FIXME: Write this
5716 when we need it. The old linker couldn't handle this anyhow. */
5717 abort ();
5719 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5720 if (howto == NULL)
5722 bfd_set_error (bfd_error_bad_value);
5723 return FALSE;
5726 h = ((struct xcoff_link_hash_entry *)
5727 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5728 link_order->u.reloc.p->u.name,
5729 FALSE, FALSE, TRUE));
5730 if (h == NULL)
5732 (*flinfo->info->callbacks->unattached_reloc)
5733 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
5734 return TRUE;
5737 hsec = xcoff_symbol_section (h);
5738 if (h->root.type == bfd_link_hash_defined
5739 || h->root.type == bfd_link_hash_defweak)
5740 hval = h->root.u.def.value;
5741 else
5742 hval = 0;
5744 addend = link_order->u.reloc.p->addend;
5745 if (hsec != NULL)
5746 addend += (hsec->output_section->vma
5747 + hsec->output_offset
5748 + hval);
5750 if (addend != 0)
5752 bfd_size_type size;
5753 bfd_byte *buf;
5754 bfd_reloc_status_type rstat;
5755 bfd_boolean ok;
5757 size = bfd_get_reloc_size (howto);
5758 buf = bfd_zmalloc (size);
5759 if (buf == NULL && size != 0)
5760 return FALSE;
5762 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5763 switch (rstat)
5765 case bfd_reloc_ok:
5766 break;
5767 default:
5768 case bfd_reloc_outofrange:
5769 abort ();
5770 case bfd_reloc_overflow:
5771 (*flinfo->info->callbacks->reloc_overflow)
5772 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5773 howto->name, addend, NULL, NULL, (bfd_vma) 0);
5774 break;
5776 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5777 (file_ptr) link_order->offset, size);
5778 free (buf);
5779 if (! ok)
5780 return FALSE;
5783 /* Store the reloc information in the right place. It will get
5784 swapped and written out at the end of the final_link routine. */
5785 irel = (flinfo->section_info[output_section->target_index].relocs
5786 + output_section->reloc_count);
5787 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5788 + output_section->reloc_count);
5790 memset (irel, 0, sizeof (struct internal_reloc));
5791 *rel_hash_ptr = NULL;
5793 irel->r_vaddr = output_section->vma + link_order->offset;
5795 if (h->indx >= 0)
5796 irel->r_symndx = h->indx;
5797 else
5799 /* Set the index to -2 to force this symbol to get written out. */
5800 h->indx = -2;
5801 *rel_hash_ptr = h;
5802 irel->r_symndx = 0;
5805 irel->r_type = howto->type;
5806 irel->r_size = howto->bitsize - 1;
5807 if (howto->complain_on_overflow == complain_overflow_signed)
5808 irel->r_size |= 0x80;
5810 ++output_section->reloc_count;
5812 /* Now output the reloc to the .loader section. */
5813 if (xcoff_hash_table (flinfo->info)->loader_section)
5815 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5816 output_bfd, irel, hsec, h))
5817 return FALSE;
5820 return TRUE;
5823 /* Do the final link step. */
5825 bfd_boolean
5826 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5828 bfd_size_type symesz;
5829 struct xcoff_final_link_info flinfo;
5830 asection *o;
5831 struct bfd_link_order *p;
5832 bfd_size_type max_contents_size;
5833 bfd_size_type max_sym_count;
5834 bfd_size_type max_lineno_count;
5835 bfd_size_type max_reloc_count;
5836 bfd_size_type max_output_reloc_count;
5837 file_ptr rel_filepos;
5838 unsigned int relsz;
5839 file_ptr line_filepos;
5840 unsigned int linesz;
5841 bfd *sub;
5842 bfd_byte *external_relocs = NULL;
5843 char strbuf[STRING_SIZE_SIZE];
5844 file_ptr pos;
5845 bfd_size_type amt;
5847 if (bfd_link_pic (info))
5848 abfd->flags |= DYNAMIC;
5850 symesz = bfd_coff_symesz (abfd);
5852 flinfo.info = info;
5853 flinfo.output_bfd = abfd;
5854 flinfo.strtab = NULL;
5855 flinfo.section_info = NULL;
5856 flinfo.last_file_index = -1;
5857 flinfo.toc_symindx = -1;
5858 flinfo.internal_syms = NULL;
5859 flinfo.sym_indices = NULL;
5860 flinfo.outsyms = NULL;
5861 flinfo.linenos = NULL;
5862 flinfo.contents = NULL;
5863 flinfo.external_relocs = NULL;
5865 if (xcoff_hash_table (info)->loader_section)
5867 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5868 + bfd_xcoff_ldhdrsz (abfd));
5869 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5870 + bfd_xcoff_ldhdrsz (abfd)
5871 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5872 * bfd_xcoff_ldsymsz (abfd)));
5874 else
5876 flinfo.ldsym = NULL;
5877 flinfo.ldrel = NULL;
5880 xcoff_data (abfd)->coff.link_info = info;
5882 flinfo.strtab = _bfd_stringtab_init ();
5883 if (flinfo.strtab == NULL)
5884 goto error_return;
5886 /* Count the relocation entries required for the output file.
5887 (We've already counted the line numbers.) Determine a few
5888 maximum sizes. */
5889 max_contents_size = 0;
5890 max_lineno_count = 0;
5891 max_reloc_count = 0;
5892 for (o = abfd->sections; o != NULL; o = o->next)
5894 o->reloc_count = 0;
5895 for (p = o->map_head.link_order; p != NULL; p = p->next)
5897 if (p->type == bfd_indirect_link_order)
5899 asection *sec;
5901 sec = p->u.indirect.section;
5903 /* Mark all sections which are to be included in the
5904 link. This will normally be every section. We need
5905 to do this so that we can identify any sections which
5906 the linker has decided to not include. */
5907 sec->linker_mark = TRUE;
5909 o->reloc_count += sec->reloc_count;
5911 if ((sec->flags & SEC_IN_MEMORY) == 0)
5913 if (sec->rawsize > max_contents_size)
5914 max_contents_size = sec->rawsize;
5915 if (sec->size > max_contents_size)
5916 max_contents_size = sec->size;
5918 if (coff_section_data (sec->owner, sec) != NULL
5919 && xcoff_section_data (sec->owner, sec) != NULL
5920 && (xcoff_section_data (sec->owner, sec)->lineno_count
5921 > max_lineno_count))
5922 max_lineno_count =
5923 xcoff_section_data (sec->owner, sec)->lineno_count;
5924 if (sec->reloc_count > max_reloc_count)
5925 max_reloc_count = sec->reloc_count;
5927 else if (p->type == bfd_section_reloc_link_order
5928 || p->type == bfd_symbol_reloc_link_order)
5929 ++o->reloc_count;
5933 /* Compute the file positions for all the sections. */
5934 if (abfd->output_has_begun)
5936 if (xcoff_hash_table (info)->file_align != 0)
5937 abort ();
5939 else
5941 bfd_vma file_align;
5943 file_align = xcoff_hash_table (info)->file_align;
5944 if (file_align != 0)
5946 bfd_boolean saw_contents;
5947 int indx;
5948 file_ptr sofar;
5950 /* Insert .pad sections before every section which has
5951 contents and is loaded, if it is preceded by some other
5952 section which has contents and is loaded. */
5953 saw_contents = TRUE;
5954 for (o = abfd->sections; o != NULL; o = o->next)
5956 if (strcmp (o->name, ".pad") == 0)
5957 saw_contents = FALSE;
5958 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5959 && (o->flags & SEC_LOAD) != 0)
5961 if (! saw_contents)
5962 saw_contents = TRUE;
5963 else
5965 asection *n;
5967 /* Create a pad section and place it before the section
5968 that needs padding. This requires unlinking and
5969 relinking the bfd's section list. */
5971 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5972 SEC_HAS_CONTENTS);
5973 n->alignment_power = 0;
5975 bfd_section_list_remove (abfd, n);
5976 bfd_section_list_insert_before (abfd, o, n);
5977 saw_contents = FALSE;
5982 /* Reset the section indices after inserting the new
5983 sections. */
5984 indx = 0;
5985 for (o = abfd->sections; o != NULL; o = o->next)
5987 ++indx;
5988 o->target_index = indx;
5990 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5992 /* Work out appropriate sizes for the .pad sections to force
5993 each section to land on a page boundary. This bit of
5994 code knows what compute_section_file_positions is going
5995 to do. */
5996 sofar = bfd_coff_filhsz (abfd);
5997 sofar += bfd_coff_aoutsz (abfd);
5998 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5999 for (o = abfd->sections; o != NULL; o = o->next)
6000 if ((bfd_xcoff_is_reloc_count_overflow
6001 (abfd, (bfd_vma) o->reloc_count))
6002 || (bfd_xcoff_is_lineno_count_overflow
6003 (abfd, (bfd_vma) o->lineno_count)))
6004 /* 64 does not overflow, need to check if 32 does */
6005 sofar += bfd_coff_scnhsz (abfd);
6007 for (o = abfd->sections; o != NULL; o = o->next)
6009 if (strcmp (o->name, ".pad") == 0)
6011 bfd_vma pageoff;
6013 BFD_ASSERT (o->size == 0);
6014 pageoff = sofar & (file_align - 1);
6015 if (pageoff != 0)
6017 o->size = file_align - pageoff;
6018 sofar += file_align - pageoff;
6019 o->flags |= SEC_HAS_CONTENTS;
6022 else
6024 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6025 sofar += BFD_ALIGN (o->size,
6026 1 << o->alignment_power);
6031 if (! bfd_coff_compute_section_file_positions (abfd))
6032 goto error_return;
6035 /* Allocate space for the pointers we need to keep for the relocs. */
6037 unsigned int i;
6039 /* We use section_count + 1, rather than section_count, because
6040 the target_index fields are 1 based. */
6041 amt = abfd->section_count + 1;
6042 amt *= sizeof (struct xcoff_link_section_info);
6043 flinfo.section_info = bfd_malloc (amt);
6044 if (flinfo.section_info == NULL)
6045 goto error_return;
6046 for (i = 0; i <= abfd->section_count; i++)
6048 flinfo.section_info[i].relocs = NULL;
6049 flinfo.section_info[i].rel_hashes = NULL;
6050 flinfo.section_info[i].toc_rel_hashes = NULL;
6054 /* Set the file positions for the relocs. */
6055 rel_filepos = obj_relocbase (abfd);
6056 relsz = bfd_coff_relsz (abfd);
6057 max_output_reloc_count = 0;
6058 for (o = abfd->sections; o != NULL; o = o->next)
6060 if (o->reloc_count == 0)
6061 o->rel_filepos = 0;
6062 else
6064 /* A stripped file has no relocs. However, we still
6065 allocate the buffers, so that later code doesn't have to
6066 worry about whether we are stripping or not. */
6067 if (info->strip == strip_all)
6068 o->rel_filepos = 0;
6069 else
6071 o->flags |= SEC_RELOC;
6072 o->rel_filepos = rel_filepos;
6073 rel_filepos += o->reloc_count * relsz;
6076 /* We don't know the indices of global symbols until we have
6077 written out all the local symbols. For each section in
6078 the output file, we keep an array of pointers to hash
6079 table entries. Each entry in the array corresponds to a
6080 reloc. When we find a reloc against a global symbol, we
6081 set the corresponding entry in this array so that we can
6082 fix up the symbol index after we have written out all the
6083 local symbols.
6085 Because of this problem, we also keep the relocs in
6086 memory until the end of the link. This wastes memory.
6087 We could backpatch the file later, I suppose, although it
6088 would be slow. */
6089 amt = o->reloc_count;
6090 amt *= sizeof (struct internal_reloc);
6091 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6093 amt = o->reloc_count;
6094 amt *= sizeof (struct xcoff_link_hash_entry *);
6095 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6097 if (flinfo.section_info[o->target_index].relocs == NULL
6098 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6099 goto error_return;
6101 if (o->reloc_count > max_output_reloc_count)
6102 max_output_reloc_count = o->reloc_count;
6106 /* We now know the size of the relocs, so we can determine the file
6107 positions of the line numbers. */
6108 line_filepos = rel_filepos;
6109 flinfo.line_filepos = line_filepos;
6110 linesz = bfd_coff_linesz (abfd);
6111 for (o = abfd->sections; o != NULL; o = o->next)
6113 if (o->lineno_count == 0)
6114 o->line_filepos = 0;
6115 else
6117 o->line_filepos = line_filepos;
6118 line_filepos += o->lineno_count * linesz;
6121 /* Reset the reloc and lineno counts, so that we can use them to
6122 count the number of entries we have output so far. */
6123 o->reloc_count = 0;
6124 o->lineno_count = 0;
6127 obj_sym_filepos (abfd) = line_filepos;
6129 /* Figure out the largest number of symbols in an input BFD. Take
6130 the opportunity to clear the output_has_begun fields of all the
6131 input BFD's. We want at least 6 symbols, since that is the
6132 number which xcoff_write_global_symbol may need. */
6133 max_sym_count = 6;
6134 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6136 bfd_size_type sz;
6138 sub->output_has_begun = FALSE;
6139 sz = obj_raw_syment_count (sub);
6140 if (sz > max_sym_count)
6141 max_sym_count = sz;
6144 /* Allocate some buffers used while linking. */
6145 amt = max_sym_count * sizeof (struct internal_syment);
6146 flinfo.internal_syms = bfd_malloc (amt);
6148 amt = max_sym_count * sizeof (long);
6149 flinfo.sym_indices = bfd_malloc (amt);
6151 amt = (max_sym_count + 1) * symesz;
6152 flinfo.outsyms = bfd_malloc (amt);
6154 amt = max_lineno_count * bfd_coff_linesz (abfd);
6155 flinfo.linenos = bfd_malloc (amt);
6157 amt = max_contents_size;
6158 flinfo.contents = bfd_malloc (amt);
6160 amt = max_reloc_count * relsz;
6161 flinfo.external_relocs = bfd_malloc (amt);
6163 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6164 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6165 || flinfo.outsyms == NULL
6166 || (flinfo.linenos == NULL && max_lineno_count > 0)
6167 || (flinfo.contents == NULL && max_contents_size > 0)
6168 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6169 goto error_return;
6171 obj_raw_syment_count (abfd) = 0;
6173 /* Find a TOC symbol, if we need one. */
6174 if (!xcoff_find_tc0 (abfd, &flinfo))
6175 goto error_return;
6177 /* We now know the position of everything in the file, except that
6178 we don't know the size of the symbol table and therefore we don't
6179 know where the string table starts. We just build the string
6180 table in memory as we go along. We process all the relocations
6181 for a single input file at once. */
6182 for (o = abfd->sections; o != NULL; o = o->next)
6184 for (p = o->map_head.link_order; p != NULL; p = p->next)
6186 if (p->type == bfd_indirect_link_order
6187 && p->u.indirect.section->owner->xvec == abfd->xvec)
6189 sub = p->u.indirect.section->owner;
6190 if (! sub->output_has_begun)
6192 if (! xcoff_link_input_bfd (&flinfo, sub))
6193 goto error_return;
6194 sub->output_has_begun = TRUE;
6197 else if (p->type == bfd_section_reloc_link_order
6198 || p->type == bfd_symbol_reloc_link_order)
6200 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6201 goto error_return;
6203 else
6205 if (! _bfd_default_link_order (abfd, info, o, p))
6206 goto error_return;
6211 /* Free up the buffers used by xcoff_link_input_bfd. */
6212 free (flinfo.internal_syms);
6213 flinfo.internal_syms = NULL;
6214 free (flinfo.sym_indices);
6215 flinfo.sym_indices = NULL;
6216 free (flinfo.linenos);
6217 flinfo.linenos = NULL;
6218 free (flinfo.contents);
6219 flinfo.contents = NULL;
6220 free (flinfo.external_relocs);
6221 flinfo.external_relocs = NULL;
6223 /* The value of the last C_FILE symbol is supposed to be -1. Write
6224 it out again. */
6225 if (flinfo.last_file_index != -1)
6227 flinfo.last_file.n_value = -(bfd_vma) 1;
6228 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6229 (void *) flinfo.outsyms);
6230 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6231 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6232 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6233 goto error_return;
6236 /* Write out all the global symbols which do not come from XCOFF
6237 input files. */
6238 bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
6240 free (flinfo.outsyms);
6241 flinfo.outsyms = NULL;
6243 /* Now that we have written out all the global symbols, we know the
6244 symbol indices to use for relocs against them, and we can finally
6245 write out the relocs. */
6246 amt = max_output_reloc_count * relsz;
6247 external_relocs = bfd_malloc (amt);
6248 if (external_relocs == NULL && max_output_reloc_count != 0)
6249 goto error_return;
6251 for (o = abfd->sections; o != NULL; o = o->next)
6253 struct internal_reloc *irel;
6254 struct internal_reloc *irelend;
6255 struct xcoff_link_hash_entry **rel_hash;
6256 struct xcoff_toc_rel_hash *toc_rel_hash;
6257 bfd_byte *erel;
6258 bfd_size_type rel_size;
6260 /* A stripped file has no relocs. */
6261 if (info->strip == strip_all)
6263 o->reloc_count = 0;
6264 continue;
6267 if (o->reloc_count == 0)
6268 continue;
6270 irel = flinfo.section_info[o->target_index].relocs;
6271 irelend = irel + o->reloc_count;
6272 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6273 for (; irel < irelend; irel++, rel_hash++)
6275 if (*rel_hash != NULL)
6277 if ((*rel_hash)->indx < 0)
6279 (*info->callbacks->unattached_reloc)
6280 (info, (*rel_hash)->root.root.string,
6281 NULL, o, irel->r_vaddr);
6282 (*rel_hash)->indx = 0;
6284 irel->r_symndx = (*rel_hash)->indx;
6288 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6289 toc_rel_hash != NULL;
6290 toc_rel_hash = toc_rel_hash->next)
6292 if (toc_rel_hash->h->u.toc_indx < 0)
6294 (*info->callbacks->unattached_reloc)
6295 (info, toc_rel_hash->h->root.root.string,
6296 NULL, o, toc_rel_hash->rel->r_vaddr);
6297 toc_rel_hash->h->u.toc_indx = 0;
6299 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6302 /* XCOFF requires that the relocs be sorted by address. We tend
6303 to produce them in the order in which their containing csects
6304 appear in the symbol table, which is not necessarily by
6305 address. So we sort them here. There may be a better way to
6306 do this. */
6307 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6308 o->reloc_count, sizeof (struct internal_reloc),
6309 xcoff_sort_relocs);
6311 irel = flinfo.section_info[o->target_index].relocs;
6312 irelend = irel + o->reloc_count;
6313 erel = external_relocs;
6314 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6315 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6317 rel_size = relsz * o->reloc_count;
6318 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6319 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6320 goto error_return;
6323 free (external_relocs);
6324 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 free (flinfo.section_info[i].relocs);
6334 free (flinfo.section_info[i].rel_hashes);
6336 free (flinfo.section_info);
6337 flinfo.section_info = NULL;
6340 /* Write out the loader section contents. */
6341 o = xcoff_hash_table (info)->loader_section;
6342 if (o != NULL
6343 && o->size != 0
6344 && o->output_section != bfd_abs_section_ptr)
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 != NULL
6357 && o->size != 0
6358 && o->output_section != bfd_abs_section_ptr
6359 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6360 (file_ptr) o->output_offset,
6361 o->size))
6362 goto error_return;
6363 o = xcoff_hash_table (info)->toc_section;
6364 if (o != NULL
6365 && o->size != 0
6366 && o->output_section != bfd_abs_section_ptr
6367 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6368 (file_ptr) o->output_offset,
6369 o->size))
6370 goto error_return;
6371 o = xcoff_hash_table (info)->descriptor_section;
6372 if (o != NULL
6373 && o->size != 0
6374 && o->output_section != bfd_abs_section_ptr
6375 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6376 (file_ptr) o->output_offset,
6377 o->size))
6378 goto error_return;
6380 /* Write out the string table. */
6381 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6382 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6383 goto error_return;
6384 H_PUT_32 (abfd,
6385 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6386 strbuf);
6387 amt = STRING_SIZE_SIZE;
6388 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6389 goto error_return;
6390 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6391 goto error_return;
6393 _bfd_stringtab_free (flinfo.strtab);
6395 /* Write out the debugging string table. */
6396 o = xcoff_hash_table (info)->debug_section;
6397 if (o != NULL
6398 && o->size != 0
6399 && o->output_section != bfd_abs_section_ptr)
6401 struct bfd_strtab_hash *debug_strtab;
6403 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6404 BFD_ASSERT (o->output_section->size - o->output_offset
6405 >= _bfd_stringtab_size (debug_strtab));
6406 pos = o->output_section->filepos + o->output_offset;
6407 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6408 goto error_return;
6409 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6410 goto error_return;
6413 /* Setting symcount to 0 will cause write_object_contents to
6414 not try to write out the symbols. */
6415 abfd->symcount = 0;
6417 return TRUE;
6419 error_return:
6420 if (flinfo.strtab != NULL)
6421 _bfd_stringtab_free (flinfo.strtab);
6423 if (flinfo.section_info != NULL)
6425 unsigned int i;
6427 for (i = 0; i < abfd->section_count; i++)
6429 free (flinfo.section_info[i].relocs);
6430 free (flinfo.section_info[i].rel_hashes);
6432 free (flinfo.section_info);
6435 free (flinfo.internal_syms);
6436 free (flinfo.sym_indices);
6437 free (flinfo.outsyms);
6438 free (flinfo.linenos);
6439 free (flinfo.contents);
6440 free (flinfo.external_relocs);
6441 free (external_relocs);
6442 return FALSE;