* po/sr.po: New Serbian translation.
[binutils.git] / bfd / xcofflink.c
blob4adfb1746699af54da617d750331ae3d633182cc
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
29 #include "libcoff.h"
30 #include "libxcoff.h"
31 #include "libiberty.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file *next;
49 /* The path. */
50 const char *path;
51 /* The file name. */
52 const char *file;
53 /* The member name. */
54 const char *member;
57 /* Information we keep for each section in the output file during the
58 final link phase. */
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc *relocs;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry **rel_hashes;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash *next;
74 struct xcoff_link_hash_entry *h;
75 struct internal_reloc *rel;
76 } *toc_rel_hashes;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
83 bfd *archive;
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
87 const char *imppath;
88 const char *impfile;
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p : 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p : 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash *debug_strtab;
106 /* The .debug section we will use for the final output. */
107 asection *debug_section;
109 /* The .loader section we will use for the final output. */
110 asection *loader_section;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
114 size_t ldrel_count;
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr;
119 /* The .gl section we use to hold global linkage code. */
120 asection *linkage_section;
122 /* The .tc section we use to hold toc entries we build for global
123 linkage code. */
124 asection *toc_section;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection *descriptor_section;
130 /* The list of import files. */
131 struct xcoff_import_file *imports;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align;
136 /* Whether the .text section must be read-only. */
137 bfd_boolean textro;
139 /* Whether -brtl was specified. */
140 bfd_boolean rtld;
142 /* Whether garbage collection was done. */
143 bfd_boolean gc;
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list *next;
149 struct xcoff_link_hash_entry *h;
150 bfd_size_type size;
152 *size_list;
154 /* Information about archives. */
155 htab_t archive_info;
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info *info;
167 /* Output BFD. */
168 bfd *output_bfd;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash *strtab;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info *section_info;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file;
178 /* Symbol index of TOC symbol. */
179 long toc_symindx;
180 /* Start of .loader symbols. */
181 bfd_byte *ldsym;
182 /* Next .loader reloc to swap out. */
183 bfd_byte *ldrel;
184 /* File position of start of line numbers. */
185 file_ptr line_filepos;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment *internal_syms;
188 /* Buffer large enough to hold output indices of symbols of any
189 input file. */
190 long *sym_indices;
191 /* Buffer large enough to hold output symbols for any input file. */
192 bfd_byte *outsyms;
193 /* Buffer large enough to hold external line numbers for any input
194 section. */
195 bfd_byte *linenos;
196 /* Buffer large enough to hold any input section. */
197 bfd_byte *contents;
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte *external_relocs;
202 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
208 here. */
210 /* Read the contents of a section. */
212 static bfd_boolean
213 xcoff_get_section_contents (bfd *abfd, asection *sec)
215 if (coff_section_data (abfd, sec) == NULL)
217 bfd_size_type amt = sizeof (struct coff_section_tdata);
219 sec->used_by_bfd = bfd_zalloc (abfd, amt);
220 if (sec->used_by_bfd == NULL)
221 return FALSE;
224 if (coff_section_data (abfd, sec)->contents == NULL)
226 bfd_byte *contents;
228 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
230 if (contents != NULL)
231 free (contents);
232 return FALSE;
234 coff_section_data (abfd, sec)->contents = contents;
237 return TRUE;
240 /* Get the size required to hold the dynamic symbols. */
242 long
243 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
245 asection *lsec;
246 bfd_byte *contents;
247 struct internal_ldhdr ldhdr;
249 if ((abfd->flags & DYNAMIC) == 0)
251 bfd_set_error (bfd_error_invalid_operation);
252 return -1;
255 lsec = bfd_get_section_by_name (abfd, ".loader");
256 if (lsec == NULL)
258 bfd_set_error (bfd_error_no_symbols);
259 return -1;
262 if (! xcoff_get_section_contents (abfd, lsec))
263 return -1;
264 contents = coff_section_data (abfd, lsec)->contents;
266 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
268 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
271 /* Get the dynamic symbols. */
273 long
274 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
276 asection *lsec;
277 bfd_byte *contents;
278 struct internal_ldhdr ldhdr;
279 const char *strings;
280 bfd_byte *elsym, *elsymend;
281 coff_symbol_type *symbuf;
283 if ((abfd->flags & DYNAMIC) == 0)
285 bfd_set_error (bfd_error_invalid_operation);
286 return -1;
289 lsec = bfd_get_section_by_name (abfd, ".loader");
290 if (lsec == NULL)
292 bfd_set_error (bfd_error_no_symbols);
293 return -1;
296 if (! xcoff_get_section_contents (abfd, lsec))
297 return -1;
298 contents = coff_section_data (abfd, lsec)->contents;
300 coff_section_data (abfd, lsec)->keep_contents = TRUE;
302 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
304 strings = (char *) contents + ldhdr.l_stoff;
306 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
307 if (symbuf == NULL)
308 return -1;
310 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
312 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
313 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
315 struct internal_ldsym ldsym;
317 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
319 symbuf->symbol.the_bfd = abfd;
321 if (ldsym._l._l_l._l_zeroes == 0)
322 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
323 else
325 char *c;
327 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
328 if (c == NULL)
329 return -1;
330 memcpy (c, ldsym._l._l_name, SYMNMLEN);
331 c[SYMNMLEN] = '\0';
332 symbuf->symbol.name = c;
335 if (ldsym.l_smclas == XMC_XO)
336 symbuf->symbol.section = bfd_abs_section_ptr;
337 else
338 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
339 ldsym.l_scnum);
340 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
342 symbuf->symbol.flags = BSF_NO_FLAGS;
343 if ((ldsym.l_smtype & L_EXPORT) != 0)
345 if ((ldsym.l_smtype & L_WEAK) != 0)
346 symbuf->symbol.flags |= BSF_WEAK;
347 else
348 symbuf->symbol.flags |= BSF_GLOBAL;
351 /* FIXME: We have no way to record the other information stored
352 with the loader symbol. */
353 *psyms = (asymbol *) symbuf;
356 *psyms = NULL;
358 return ldhdr.l_nsyms;
361 /* Get the size required to hold the dynamic relocs. */
363 long
364 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
366 asection *lsec;
367 bfd_byte *contents;
368 struct internal_ldhdr ldhdr;
370 if ((abfd->flags & DYNAMIC) == 0)
372 bfd_set_error (bfd_error_invalid_operation);
373 return -1;
376 lsec = bfd_get_section_by_name (abfd, ".loader");
377 if (lsec == NULL)
379 bfd_set_error (bfd_error_no_symbols);
380 return -1;
383 if (! xcoff_get_section_contents (abfd, lsec))
384 return -1;
385 contents = coff_section_data (abfd, lsec)->contents;
387 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
389 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
392 /* Get the dynamic relocs. */
394 long
395 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
396 arelent **prelocs,
397 asymbol **syms)
399 asection *lsec;
400 bfd_byte *contents;
401 struct internal_ldhdr ldhdr;
402 arelent *relbuf;
403 bfd_byte *elrel, *elrelend;
405 if ((abfd->flags & DYNAMIC) == 0)
407 bfd_set_error (bfd_error_invalid_operation);
408 return -1;
411 lsec = bfd_get_section_by_name (abfd, ".loader");
412 if (lsec == NULL)
414 bfd_set_error (bfd_error_no_symbols);
415 return -1;
418 if (! xcoff_get_section_contents (abfd, lsec))
419 return -1;
420 contents = coff_section_data (abfd, lsec)->contents;
422 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
424 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
425 if (relbuf == NULL)
426 return -1;
428 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
430 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
431 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
432 prelocs++)
434 struct internal_ldrel ldrel;
436 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
438 if (ldrel.l_symndx >= 3)
439 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
440 else
442 const char *name;
443 asection *sec;
445 switch (ldrel.l_symndx)
447 case 0:
448 name = ".text";
449 break;
450 case 1:
451 name = ".data";
452 break;
453 case 2:
454 name = ".bss";
455 break;
456 default:
457 abort ();
458 break;
461 sec = bfd_get_section_by_name (abfd, name);
462 if (sec == NULL)
464 bfd_set_error (bfd_error_bad_value);
465 return -1;
468 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
471 relbuf->address = ldrel.l_vaddr;
472 relbuf->addend = 0;
474 /* Most dynamic relocs have the same type. FIXME: This is only
475 correct if ldrel.l_rtype == 0. In other cases, we should use
476 a different howto. */
477 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
479 /* FIXME: We have no way to record the l_rsecnm field. */
481 *prelocs = relbuf;
484 *prelocs = NULL;
486 return ldhdr.l_nreloc;
489 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 static hashval_t
492 xcoff_archive_info_hash (const void *data)
494 const struct xcoff_archive_info *info;
496 info = (const struct xcoff_archive_info *) data;
497 return htab_hash_pointer (info->archive);
500 static int
501 xcoff_archive_info_eq (const void *data1, const void *data2)
503 const struct xcoff_archive_info *info1;
504 const struct xcoff_archive_info *info2;
506 info1 = (const struct xcoff_archive_info *) data1;
507 info2 = (const struct xcoff_archive_info *) data2;
508 return info1->archive == info2->archive;
511 /* Return information about archive ARCHIVE. Return NULL on error. */
513 static struct xcoff_archive_info *
514 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
516 struct xcoff_link_hash_table *htab;
517 struct xcoff_archive_info *entryp, entry;
518 void **slot;
520 htab = xcoff_hash_table (info);
521 entry.archive = archive;
522 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
523 if (!slot)
524 return NULL;
526 entryp = *slot;
527 if (!entryp)
529 entryp = bfd_zalloc (archive, sizeof (entry));
530 if (!entryp)
531 return NULL;
533 entryp->archive = archive;
534 *slot = entryp;
536 return entryp;
539 /* Routine to create an entry in an XCOFF link hash table. */
541 static struct bfd_hash_entry *
542 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
543 struct bfd_hash_table *table,
544 const char *string)
546 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
548 /* Allocate the structure if it has not already been allocated by a
549 subclass. */
550 if (ret == NULL)
551 ret = bfd_hash_allocate (table, sizeof (* ret));
552 if (ret == NULL)
553 return NULL;
555 /* Call the allocation method of the superclass. */
556 ret = ((struct xcoff_link_hash_entry *)
557 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
558 table, string));
559 if (ret != NULL)
561 /* Set local fields. */
562 ret->indx = -1;
563 ret->toc_section = NULL;
564 ret->u.toc_indx = -1;
565 ret->descriptor = NULL;
566 ret->ldsym = NULL;
567 ret->ldindx = -1;
568 ret->flags = 0;
569 ret->smclas = XMC_UA;
572 return (struct bfd_hash_entry *) ret;
575 /* Create a XCOFF link hash table. */
577 struct bfd_link_hash_table *
578 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
580 struct xcoff_link_hash_table *ret;
581 bfd_size_type amt = sizeof (* ret);
583 ret = bfd_malloc (amt);
584 if (ret == NULL)
585 return NULL;
586 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
587 sizeof (struct xcoff_link_hash_entry)))
589 free (ret);
590 return NULL;
593 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
594 ret->debug_section = NULL;
595 ret->loader_section = NULL;
596 ret->ldrel_count = 0;
597 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
598 ret->linkage_section = NULL;
599 ret->toc_section = NULL;
600 ret->descriptor_section = NULL;
601 ret->imports = NULL;
602 ret->file_align = 0;
603 ret->textro = FALSE;
604 ret->gc = FALSE;
605 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
606 xcoff_archive_info_eq, NULL);
607 memset (ret->special_sections, 0, sizeof ret->special_sections);
609 /* The linker will always generate a full a.out header. We need to
610 record that fact now, before the sizeof_headers routine could be
611 called. */
612 xcoff_data (abfd)->full_aouthdr = TRUE;
614 return &ret->root;
617 /* Free a XCOFF link hash table. */
619 void
620 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
622 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
624 _bfd_stringtab_free (ret->debug_strtab);
625 bfd_hash_table_free (&ret->root.table);
626 free (ret);
629 /* Read internal relocs for an XCOFF csect. This is a wrapper around
630 _bfd_coff_read_internal_relocs which tries to take advantage of any
631 relocs which may have been cached for the enclosing section. */
633 static struct internal_reloc *
634 xcoff_read_internal_relocs (bfd *abfd,
635 asection *sec,
636 bfd_boolean cache,
637 bfd_byte *external_relocs,
638 bfd_boolean require_internal,
639 struct internal_reloc *internal_relocs)
641 if (coff_section_data (abfd, sec) != NULL
642 && coff_section_data (abfd, sec)->relocs == NULL
643 && xcoff_section_data (abfd, sec) != NULL)
645 asection *enclosing;
647 enclosing = xcoff_section_data (abfd, sec)->enclosing;
649 if (enclosing != NULL
650 && (coff_section_data (abfd, enclosing) == NULL
651 || coff_section_data (abfd, enclosing)->relocs == NULL)
652 && cache
653 && enclosing->reloc_count > 0)
655 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
656 external_relocs, FALSE, NULL)
657 == NULL)
658 return NULL;
661 if (enclosing != NULL
662 && coff_section_data (abfd, enclosing) != NULL
663 && coff_section_data (abfd, enclosing)->relocs != NULL)
665 size_t off;
667 off = ((sec->rel_filepos - enclosing->rel_filepos)
668 / bfd_coff_relsz (abfd));
670 if (! require_internal)
671 return coff_section_data (abfd, enclosing)->relocs + off;
672 memcpy (internal_relocs,
673 coff_section_data (abfd, enclosing)->relocs + off,
674 sec->reloc_count * sizeof (struct internal_reloc));
675 return internal_relocs;
679 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
680 require_internal, internal_relocs);
683 /* Split FILENAME into an import path and an import filename,
684 storing them in *IMPPATH and *IMPFILE respectively. */
686 bfd_boolean
687 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
688 const char **imppath, const char **impfile)
690 const char *base;
691 size_t length;
692 char *path;
694 base = lbasename (filename);
695 length = base - filename;
696 if (length == 0)
697 /* The filename has no directory component, so use an empty path. */
698 *imppath = "";
699 else if (length == 1)
700 /* The filename is in the root directory. */
701 *imppath = "/";
702 else
704 /* Extract the (non-empty) directory part. Note that we don't
705 need to strip duplicate directory separators from any part
706 of the string; the native linker doesn't do that either. */
707 path = bfd_alloc (abfd, length);
708 if (path == NULL)
709 return FALSE;
710 memcpy (path, filename, length - 1);
711 path[length - 1] = 0;
712 *imppath = path;
714 *impfile = base;
715 return TRUE;
718 /* Set ARCHIVE's import path as though its filename had been given
719 as FILENAME. */
721 bfd_boolean
722 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
723 bfd *archive, const char *filename)
725 struct xcoff_archive_info *archive_info;
727 archive_info = xcoff_get_archive_info (info, archive);
728 return (archive_info != NULL
729 && bfd_xcoff_split_import_path (archive, filename,
730 &archive_info->imppath,
731 &archive_info->impfile));
734 /* H is an imported symbol. Set the import module's path, file and member
735 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
736 no specific import module is specified. */
738 static bfd_boolean
739 xcoff_set_import_path (struct bfd_link_info *info,
740 struct xcoff_link_hash_entry *h,
741 const char *imppath, const char *impfile,
742 const char *impmember)
744 unsigned int c;
745 struct xcoff_import_file **pp;
747 /* We overload the ldindx field to hold the l_ifile value for this
748 symbol. */
749 BFD_ASSERT (h->ldsym == NULL);
750 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
751 if (imppath == NULL)
752 h->ldindx = -1;
753 else
755 /* We start c at 1 because the first entry in the import list is
756 reserved for the library search path. */
757 for (pp = &xcoff_hash_table (info)->imports, c = 1;
758 *pp != NULL;
759 pp = &(*pp)->next, ++c)
761 if (filename_cmp ((*pp)->path, imppath) == 0
762 && filename_cmp ((*pp)->file, impfile) == 0
763 && filename_cmp ((*pp)->member, impmember) == 0)
764 break;
767 if (*pp == NULL)
769 struct xcoff_import_file *n;
770 bfd_size_type amt = sizeof (* n);
772 n = bfd_alloc (info->output_bfd, amt);
773 if (n == NULL)
774 return FALSE;
775 n->next = NULL;
776 n->path = imppath;
777 n->file = impfile;
778 n->member = impmember;
779 *pp = n;
781 h->ldindx = c;
783 return TRUE;
786 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
787 Return true if LDSYM defines H. */
789 static bfd_boolean
790 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
791 struct internal_ldsym *ldsym)
793 /* If we didn't know about H before processing LDSYM, LDSYM
794 definitely defines H. */
795 if (h->root.type == bfd_link_hash_new)
796 return TRUE;
798 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
799 dynamic symbol, LDSYM trumps the current definition of H. */
800 if ((ldsym->l_smtype & L_WEAK) == 0
801 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
802 && (h->flags & XCOFF_DEF_REGULAR) == 0
803 && (h->root.type == bfd_link_hash_defweak
804 || h->root.type == bfd_link_hash_undefweak))
805 return TRUE;
807 /* If H is currently undefined, LDSYM defines it. */
808 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
809 && (h->root.type == bfd_link_hash_undefined
810 || h->root.type == bfd_link_hash_undefweak))
811 return TRUE;
813 return FALSE;
816 /* This function is used to add symbols from a dynamic object to the
817 global symbol table. */
819 static bfd_boolean
820 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
822 asection *lsec;
823 bfd_byte *contents;
824 struct internal_ldhdr ldhdr;
825 const char *strings;
826 bfd_byte *elsym, *elsymend;
827 struct xcoff_import_file *n;
828 unsigned int c;
829 struct xcoff_import_file **pp;
831 /* We can only handle a dynamic object if we are generating an XCOFF
832 output file. */
833 if (info->output_bfd->xvec != abfd->xvec)
835 (*_bfd_error_handler)
836 (_("%s: XCOFF shared object when not producing XCOFF output"),
837 bfd_get_filename (abfd));
838 bfd_set_error (bfd_error_invalid_operation);
839 return FALSE;
842 /* The symbols we use from a dynamic object are not the symbols in
843 the normal symbol table, but, rather, the symbols in the export
844 table. If there is a global symbol in a dynamic object which is
845 not in the export table, the loader will not be able to find it,
846 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
847 libc.a has symbols in the export table which are not in the
848 symbol table. */
850 /* Read in the .loader section. FIXME: We should really use the
851 o_snloader field in the a.out header, rather than grabbing the
852 section by name. */
853 lsec = bfd_get_section_by_name (abfd, ".loader");
854 if (lsec == NULL)
856 (*_bfd_error_handler)
857 (_("%s: dynamic object with no .loader section"),
858 bfd_get_filename (abfd));
859 bfd_set_error (bfd_error_no_symbols);
860 return FALSE;
863 if (! xcoff_get_section_contents (abfd, lsec))
864 return FALSE;
865 contents = coff_section_data (abfd, lsec)->contents;
867 /* Remove the sections from this object, so that they do not get
868 included in the link. */
869 bfd_section_list_clear (abfd);
871 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
873 strings = (char *) contents + ldhdr.l_stoff;
875 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
877 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
879 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
881 struct internal_ldsym ldsym;
882 char nambuf[SYMNMLEN + 1];
883 const char *name;
884 struct xcoff_link_hash_entry *h;
886 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
888 /* We are only interested in exported symbols. */
889 if ((ldsym.l_smtype & L_EXPORT) == 0)
890 continue;
892 if (ldsym._l._l_l._l_zeroes == 0)
893 name = strings + ldsym._l._l_l._l_offset;
894 else
896 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
897 nambuf[SYMNMLEN] = '\0';
898 name = nambuf;
901 /* Normally we could not call xcoff_link_hash_lookup in an add
902 symbols routine, since we might not be using an XCOFF hash
903 table. However, we verified above that we are using an XCOFF
904 hash table. */
906 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
907 TRUE, TRUE);
908 if (h == NULL)
909 return FALSE;
911 if (!xcoff_dynamic_definition_p (h, &ldsym))
912 continue;
914 h->flags |= XCOFF_DEF_DYNAMIC;
915 h->smclas = ldsym.l_smclas;
916 if (h->smclas == XMC_XO)
918 /* This symbol has an absolute value. */
919 if ((ldsym.l_smtype & L_WEAK) != 0)
920 h->root.type = bfd_link_hash_defweak;
921 else
922 h->root.type = bfd_link_hash_defined;
923 h->root.u.def.section = bfd_abs_section_ptr;
924 h->root.u.def.value = ldsym.l_value;
926 else
928 /* Otherwise, we don't bother to actually define the symbol,
929 since we don't have a section to put it in anyhow.
930 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
931 should be imported from the symbol's undef.abfd. */
932 if ((ldsym.l_smtype & L_WEAK) != 0)
933 h->root.type = bfd_link_hash_undefweak;
934 else
935 h->root.type = bfd_link_hash_undefined;
936 h->root.u.undef.abfd = abfd;
939 /* If this symbol defines a function descriptor, then it
940 implicitly defines the function code as well. */
941 if (h->smclas == XMC_DS
942 || (h->smclas == XMC_XO && name[0] != '.'))
943 h->flags |= XCOFF_DESCRIPTOR;
944 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
946 struct xcoff_link_hash_entry *hds;
948 hds = h->descriptor;
949 if (hds == NULL)
951 char *dsnm;
953 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
954 if (dsnm == NULL)
955 return FALSE;
956 dsnm[0] = '.';
957 strcpy (dsnm + 1, name);
958 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
959 TRUE, TRUE, TRUE);
960 free (dsnm);
961 if (hds == NULL)
962 return FALSE;
964 hds->descriptor = h;
965 h->descriptor = hds;
968 if (xcoff_dynamic_definition_p (hds, &ldsym))
970 hds->root.type = h->root.type;
971 hds->flags |= XCOFF_DEF_DYNAMIC;
972 if (h->smclas == XMC_XO)
974 /* An absolute symbol appears to actually define code, not a
975 function descriptor. This is how some math functions are
976 implemented on AIX 4.1. */
977 hds->smclas = XMC_XO;
978 hds->root.u.def.section = bfd_abs_section_ptr;
979 hds->root.u.def.value = ldsym.l_value;
981 else
983 hds->smclas = XMC_PR;
984 hds->root.u.undef.abfd = abfd;
985 /* We do not want to add this to the undefined
986 symbol list. */
992 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
994 free (coff_section_data (abfd, lsec)->contents);
995 coff_section_data (abfd, lsec)->contents = NULL;
998 /* Record this file in the import files. */
999 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
1000 if (n == NULL)
1001 return FALSE;
1002 n->next = NULL;
1004 if (abfd->my_archive == NULL)
1006 if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
1007 &n->path, &n->file))
1008 return FALSE;
1009 n->member = "";
1011 else
1013 struct xcoff_archive_info *archive_info;
1015 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1016 if (!archive_info->impfile)
1018 if (!bfd_xcoff_split_import_path (archive_info->archive,
1019 archive_info->archive->filename,
1020 &archive_info->imppath,
1021 &archive_info->impfile))
1022 return FALSE;
1024 n->path = archive_info->imppath;
1025 n->file = archive_info->impfile;
1026 n->member = bfd_get_filename (abfd);
1029 /* We start c at 1 because the first import file number is reserved
1030 for LIBPATH. */
1031 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1032 *pp != NULL;
1033 pp = &(*pp)->next, ++c)
1035 *pp = n;
1037 xcoff_data (abfd)->import_file_id = c;
1039 return TRUE;
1042 /* xcoff_link_create_extra_sections
1044 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1046 static bfd_boolean
1047 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1049 bfd_boolean return_value = FALSE;
1051 if (info->output_bfd->xvec == abfd->xvec)
1053 /* We need to build a .loader section, so we do it here. This
1054 won't work if we're producing an XCOFF output file with no
1055 XCOFF input files. FIXME. */
1057 if (!info->relocatable
1058 && xcoff_hash_table (info)->loader_section == NULL)
1060 asection *lsec;
1061 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1063 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1064 if (lsec == NULL)
1065 goto end_return;
1067 xcoff_hash_table (info)->loader_section = lsec;
1070 /* Likewise for the linkage section. */
1071 if (xcoff_hash_table (info)->linkage_section == NULL)
1073 asection *lsec;
1074 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1075 | SEC_IN_MEMORY);
1077 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1078 if (lsec == NULL)
1079 goto end_return;
1081 xcoff_hash_table (info)->linkage_section = lsec;
1082 lsec->alignment_power = 2;
1085 /* Likewise for the TOC section. */
1086 if (xcoff_hash_table (info)->toc_section == NULL)
1088 asection *tsec;
1089 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1090 | SEC_IN_MEMORY);
1092 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1093 if (tsec == NULL)
1094 goto end_return;
1096 xcoff_hash_table (info)->toc_section = tsec;
1097 tsec->alignment_power = 2;
1100 /* Likewise for the descriptor section. */
1101 if (xcoff_hash_table (info)->descriptor_section == NULL)
1103 asection *dsec;
1104 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1105 | SEC_IN_MEMORY);
1107 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1108 if (dsec == NULL)
1109 goto end_return;
1111 xcoff_hash_table (info)->descriptor_section = dsec;
1112 dsec->alignment_power = 2;
1115 /* Likewise for the .debug section. */
1116 if (xcoff_hash_table (info)->debug_section == NULL
1117 && info->strip != strip_all)
1119 asection *dsec;
1120 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1122 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1123 if (dsec == NULL)
1124 goto end_return;
1126 xcoff_hash_table (info)->debug_section = dsec;
1130 return_value = TRUE;
1132 end_return:
1134 return return_value;
1137 /* Returns the index of reloc in RELOCS with the least address greater
1138 than or equal to ADDRESS. The relocs are sorted by address. */
1140 static bfd_size_type
1141 xcoff_find_reloc (struct internal_reloc *relocs,
1142 bfd_size_type count,
1143 bfd_vma address)
1145 bfd_size_type min, max, this;
1147 if (count < 2)
1149 if (count == 1 && relocs[0].r_vaddr < address)
1150 return 1;
1151 else
1152 return 0;
1155 min = 0;
1156 max = count;
1158 /* Do a binary search over (min,max]. */
1159 while (min + 1 < max)
1161 bfd_vma raddr;
1163 this = (max + min) / 2;
1164 raddr = relocs[this].r_vaddr;
1165 if (raddr > address)
1166 max = this;
1167 else if (raddr < address)
1168 min = this;
1169 else
1171 min = this;
1172 break;
1176 if (relocs[min].r_vaddr < address)
1177 return min + 1;
1179 while (min > 0
1180 && relocs[min - 1].r_vaddr == address)
1181 --min;
1183 return min;
1186 /* Add all the symbols from an object file to the hash table.
1188 XCOFF is a weird format. A normal XCOFF .o files will have three
1189 COFF sections--.text, .data, and .bss--but each COFF section will
1190 contain many csects. These csects are described in the symbol
1191 table. From the linker's point of view, each csect must be
1192 considered a section in its own right. For example, a TOC entry is
1193 handled as a small XMC_TC csect. The linker must be able to merge
1194 different TOC entries together, which means that it must be able to
1195 extract the XMC_TC csects from the .data section of the input .o
1196 file.
1198 From the point of view of our linker, this is, of course, a hideous
1199 nightmare. We cope by actually creating sections for each csect,
1200 and discarding the original sections. We then have to handle the
1201 relocation entries carefully, since the only way to tell which
1202 csect they belong to is to examine the address. */
1204 static bfd_boolean
1205 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1207 unsigned int n_tmask;
1208 unsigned int n_btshft;
1209 bfd_boolean default_copy;
1210 bfd_size_type symcount;
1211 struct xcoff_link_hash_entry **sym_hash;
1212 asection **csect_cache;
1213 unsigned int *lineno_counts;
1214 bfd_size_type linesz;
1215 asection *o;
1216 asection *last_real;
1217 bfd_boolean keep_syms;
1218 asection *csect;
1219 unsigned int csect_index;
1220 asection *first_csect;
1221 bfd_size_type symesz;
1222 bfd_byte *esym;
1223 bfd_byte *esym_end;
1224 struct reloc_info_struct
1226 struct internal_reloc *relocs;
1227 asection **csects;
1228 bfd_byte *linenos;
1229 } *reloc_info = NULL;
1230 bfd_size_type amt;
1232 keep_syms = obj_coff_keep_syms (abfd);
1234 if ((abfd->flags & DYNAMIC) != 0
1235 && ! info->static_link)
1237 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1238 return FALSE;
1241 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1242 if (! xcoff_link_create_extra_sections (abfd, info))
1243 goto error_return;
1245 if ((abfd->flags & DYNAMIC) != 0
1246 && ! info->static_link)
1247 return TRUE;
1249 n_tmask = coff_data (abfd)->local_n_tmask;
1250 n_btshft = coff_data (abfd)->local_n_btshft;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1256 if (info->keep_memory)
1257 default_copy = FALSE;
1258 else
1259 default_copy = TRUE;
1261 symcount = obj_raw_syment_count (abfd);
1263 /* We keep a list of the linker hash table entries that correspond
1264 to each external symbol. */
1265 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1266 sym_hash = bfd_zalloc (abfd, amt);
1267 if (sym_hash == NULL && symcount != 0)
1268 goto error_return;
1269 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1271 /* Because of the weird stuff we are doing with XCOFF csects, we can
1272 not easily determine which section a symbol is in, so we store
1273 the information in the tdata for the input file. */
1274 amt = symcount * sizeof (asection *);
1275 csect_cache = bfd_zalloc (abfd, amt);
1276 if (csect_cache == NULL && symcount != 0)
1277 goto error_return;
1278 xcoff_data (abfd)->csects = csect_cache;
1280 /* We garbage-collect line-number information on a symbol-by-symbol
1281 basis, so we need to have quick access to the number of entries
1282 per symbol. */
1283 amt = symcount * sizeof (unsigned int);
1284 lineno_counts = bfd_zalloc (abfd, amt);
1285 if (lineno_counts == NULL && symcount != 0)
1286 goto error_return;
1287 xcoff_data (abfd)->lineno_counts = lineno_counts;
1289 /* While splitting sections into csects, we need to assign the
1290 relocs correctly. The relocs and the csects must both be in
1291 order by VMA within a given section, so we handle this by
1292 scanning along the relocs as we process the csects. We index
1293 into reloc_info using the section target_index. */
1294 amt = abfd->section_count + 1;
1295 amt *= sizeof (struct reloc_info_struct);
1296 reloc_info = bfd_zmalloc (amt);
1297 if (reloc_info == NULL)
1298 goto error_return;
1300 /* Read in the relocs and line numbers for each section. */
1301 linesz = bfd_coff_linesz (abfd);
1302 last_real = NULL;
1303 for (o = abfd->sections; o != NULL; o = o->next)
1305 last_real = o;
1307 if ((o->flags & SEC_RELOC) != 0)
1309 reloc_info[o->target_index].relocs =
1310 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1311 amt = o->reloc_count;
1312 amt *= sizeof (asection *);
1313 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1314 if (reloc_info[o->target_index].csects == NULL)
1315 goto error_return;
1318 if ((info->strip == strip_none || info->strip == strip_some)
1319 && o->lineno_count > 0)
1321 bfd_byte *linenos;
1323 amt = linesz * o->lineno_count;
1324 linenos = bfd_malloc (amt);
1325 if (linenos == NULL)
1326 goto error_return;
1327 reloc_info[o->target_index].linenos = linenos;
1328 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1329 || bfd_bread (linenos, amt, abfd) != amt)
1330 goto error_return;
1334 /* Don't let the linker relocation routines discard the symbols. */
1335 obj_coff_keep_syms (abfd) = TRUE;
1337 csect = NULL;
1338 csect_index = 0;
1339 first_csect = NULL;
1341 symesz = bfd_coff_symesz (abfd);
1342 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1343 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1344 esym_end = esym + symcount * symesz;
1346 while (esym < esym_end)
1348 struct internal_syment sym;
1349 union internal_auxent aux;
1350 const char *name;
1351 char buf[SYMNMLEN + 1];
1352 int smtyp;
1353 asection *section;
1354 bfd_vma value;
1355 struct xcoff_link_hash_entry *set_toc;
1357 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1359 /* In this pass we are only interested in symbols with csect
1360 information. */
1361 if (!CSECT_SYM_P (sym.n_sclass))
1363 /* Set csect_cache,
1364 Normally csect is a .pr, .rw etc. created in the loop
1365 If C_FILE or first time, handle special
1367 Advance esym, sym_hash, csect_hash ptrs. */
1368 if (sym.n_sclass == C_FILE || 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 (_("%B: `%s' has line numbers but no enclosing section"),
1417 abfd, name);
1418 bfd_set_error (bfd_error_bad_value);
1419 goto error_return;
1421 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1422 - enclosing->line_filepos);
1423 /* Explicit cast to bfd_signed_vma for compiler. */
1424 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1426 struct internal_lineno lin;
1427 bfd_byte *linpstart;
1429 linpstart = (reloc_info[enclosing->target_index].linenos
1430 + linoff);
1431 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1432 if (lin.l_lnno == 0
1433 && ((bfd_size_type) lin.l_addr.l_symndx
1434 == ((esym
1435 - (bfd_byte *) obj_coff_external_syms (abfd))
1436 / symesz)))
1438 bfd_byte *linpend, *linp;
1440 linpend = (reloc_info[enclosing->target_index].linenos
1441 + enclosing->lineno_count * linesz);
1442 for (linp = linpstart + linesz;
1443 linp < linpend;
1444 linp += linesz)
1446 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1447 (void *) &lin);
1448 if (lin.l_lnno == 0)
1449 break;
1451 *lineno_counts = (linp - linpstart) / linesz;
1452 /* The setting of line_filepos will only be
1453 useful if all the line number entries for a
1454 csect are contiguous; this only matters for
1455 error reporting. */
1456 if (csect->line_filepos == 0)
1457 csect->line_filepos =
1458 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1464 /* Pick up the csect auxiliary information. */
1465 if (sym.n_numaux == 0)
1467 (*_bfd_error_handler)
1468 (_("%B: class %d symbol `%s' has no aux entries"),
1469 abfd, sym.n_sclass, name);
1470 bfd_set_error (bfd_error_bad_value);
1471 goto error_return;
1474 bfd_coff_swap_aux_in (abfd,
1475 (void *) (esym + symesz * sym.n_numaux),
1476 sym.n_type, sym.n_sclass,
1477 sym.n_numaux - 1, sym.n_numaux,
1478 (void *) &aux);
1480 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1482 section = NULL;
1483 value = 0;
1484 set_toc = NULL;
1486 switch (smtyp)
1488 default:
1489 (*_bfd_error_handler)
1490 (_("%B: symbol `%s' has unrecognized csect type %d"),
1491 abfd, name, smtyp);
1492 bfd_set_error (bfd_error_bad_value);
1493 goto error_return;
1495 case XTY_ER:
1496 /* This is an external reference. */
1497 if (sym.n_sclass == C_HIDEXT
1498 || sym.n_scnum != N_UNDEF
1499 || aux.x_csect.x_scnlen.l != 0)
1501 (*_bfd_error_handler)
1502 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1503 abfd, name, sym.n_sclass, sym.n_scnum,
1504 aux.x_csect.x_scnlen.l);
1505 bfd_set_error (bfd_error_bad_value);
1506 goto error_return;
1509 /* An XMC_XO external reference is actually a reference to
1510 an absolute location. */
1511 if (aux.x_csect.x_smclas != XMC_XO)
1512 section = bfd_und_section_ptr;
1513 else
1515 section = bfd_abs_section_ptr;
1516 value = sym.n_value;
1518 break;
1520 case XTY_SD:
1521 csect = NULL;
1522 csect_index = -(unsigned) 1;
1524 /* When we see a TOC anchor, we record the TOC value. */
1525 if (aux.x_csect.x_smclas == XMC_TC0)
1527 if (sym.n_sclass != C_HIDEXT
1528 || aux.x_csect.x_scnlen.l != 0)
1530 (*_bfd_error_handler)
1531 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1532 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1533 bfd_set_error (bfd_error_bad_value);
1534 goto error_return;
1536 xcoff_data (abfd)->toc = sym.n_value;
1539 /* We must merge TOC entries for the same symbol. We can
1540 merge two TOC entries if they are both C_HIDEXT, they
1541 both have the same name, they are both 4 or 8 bytes long, and
1542 they both have a relocation table entry for an external
1543 symbol with the same name. Unfortunately, this means
1544 that we must look through the relocations. Ick.
1546 Logic for 32 bit vs 64 bit.
1547 32 bit has a csect length of 4 for TOC
1548 64 bit has a csect length of 8 for TOC
1550 The conditions to get past the if-check are not that bad.
1551 They are what is used to create the TOC csects in the first
1552 place. */
1553 if (aux.x_csect.x_smclas == XMC_TC
1554 && sym.n_sclass == C_HIDEXT
1555 && info->output_bfd->xvec == abfd->xvec
1556 && ((bfd_xcoff_is_xcoff32 (abfd)
1557 && aux.x_csect.x_scnlen.l == 4)
1558 || (bfd_xcoff_is_xcoff64 (abfd)
1559 && aux.x_csect.x_scnlen.l == 8)))
1561 asection *enclosing;
1562 struct internal_reloc *relocs;
1563 bfd_size_type relindx;
1564 struct internal_reloc *rel;
1566 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1567 if (enclosing == NULL)
1568 goto error_return;
1570 relocs = reloc_info[enclosing->target_index].relocs;
1571 amt = enclosing->reloc_count;
1572 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1573 rel = relocs + relindx;
1575 /* 32 bit R_POS r_size is 31
1576 64 bit R_POS r_size is 63 */
1577 if (relindx < enclosing->reloc_count
1578 && rel->r_vaddr == (bfd_vma) sym.n_value
1579 && rel->r_type == R_POS
1580 && ((bfd_xcoff_is_xcoff32 (abfd)
1581 && rel->r_size == 31)
1582 || (bfd_xcoff_is_xcoff64 (abfd)
1583 && rel->r_size == 63)))
1585 bfd_byte *erelsym;
1587 struct internal_syment relsym;
1589 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1590 + rel->r_symndx * symesz);
1591 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1592 if (EXTERN_SYM_P (relsym.n_sclass))
1594 const char *relname;
1595 char relbuf[SYMNMLEN + 1];
1596 bfd_boolean copy;
1597 struct xcoff_link_hash_entry *h;
1599 /* At this point we know that the TOC entry is
1600 for an externally visible symbol. */
1601 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1602 relbuf);
1603 if (relname == NULL)
1604 goto error_return;
1606 /* We only merge TOC entries if the TC name is
1607 the same as the symbol name. This handles
1608 the normal case, but not common cases like
1609 SYM.P4 which gcc generates to store SYM + 4
1610 in the TOC. FIXME. */
1611 if (strcmp (name, relname) == 0)
1613 copy = (! info->keep_memory
1614 || relsym._n._n_n._n_zeroes != 0
1615 || relsym._n._n_n._n_offset == 0);
1616 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1617 relname, TRUE, copy,
1618 FALSE);
1619 if (h == NULL)
1620 goto error_return;
1622 /* At this point h->root.type could be
1623 bfd_link_hash_new. That should be OK,
1624 since we know for sure that we will come
1625 across this symbol as we step through the
1626 file. */
1628 /* We store h in *sym_hash for the
1629 convenience of the relocate_section
1630 function. */
1631 *sym_hash = h;
1633 if (h->toc_section != NULL)
1635 asection **rel_csects;
1637 /* We already have a TOC entry for this
1638 symbol, so we can just ignore this
1639 one. */
1640 rel_csects =
1641 reloc_info[enclosing->target_index].csects;
1642 rel_csects[relindx] = bfd_und_section_ptr;
1643 break;
1646 /* We are about to create a TOC entry for
1647 this symbol. */
1648 set_toc = h;
1655 asection *enclosing;
1657 /* We need to create a new section. We get the name from
1658 the csect storage mapping class, so that the linker can
1659 accumulate similar csects together. */
1661 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1662 if (NULL == csect)
1663 goto error_return;
1665 /* The enclosing section is the main section : .data, .text
1666 or .bss that the csect is coming from. */
1667 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1668 if (enclosing == NULL)
1669 goto error_return;
1671 if (! bfd_is_abs_section (enclosing)
1672 && ((bfd_vma) sym.n_value < enclosing->vma
1673 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1674 > enclosing->vma + enclosing->size)))
1676 (*_bfd_error_handler)
1677 (_("%B: csect `%s' not in enclosing section"),
1678 abfd, name);
1679 bfd_set_error (bfd_error_bad_value);
1680 goto error_return;
1682 csect->vma = sym.n_value;
1683 csect->filepos = (enclosing->filepos
1684 + sym.n_value
1685 - enclosing->vma);
1686 csect->size = aux.x_csect.x_scnlen.l;
1687 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1688 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1690 /* Record the enclosing section in the tdata for this new
1691 section. */
1692 amt = sizeof (struct coff_section_tdata);
1693 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1694 if (csect->used_by_bfd == NULL)
1695 goto error_return;
1696 amt = sizeof (struct xcoff_section_tdata);
1697 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1698 if (coff_section_data (abfd, csect)->tdata == NULL)
1699 goto error_return;
1700 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1701 xcoff_section_data (abfd, csect)->lineno_count =
1702 enclosing->lineno_count;
1704 if (enclosing->owner == abfd)
1706 struct internal_reloc *relocs;
1707 bfd_size_type relindx;
1708 struct internal_reloc *rel;
1709 asection **rel_csect;
1711 relocs = reloc_info[enclosing->target_index].relocs;
1712 amt = enclosing->reloc_count;
1713 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1715 rel = relocs + relindx;
1716 rel_csect = (reloc_info[enclosing->target_index].csects
1717 + relindx);
1719 csect->rel_filepos = (enclosing->rel_filepos
1720 + relindx * bfd_coff_relsz (abfd));
1721 while (relindx < enclosing->reloc_count
1722 && *rel_csect == NULL
1723 && rel->r_vaddr < csect->vma + csect->size)
1726 *rel_csect = csect;
1727 csect->flags |= SEC_RELOC;
1728 ++csect->reloc_count;
1729 ++relindx;
1730 ++rel;
1731 ++rel_csect;
1735 /* There are a number of other fields and section flags
1736 which we do not bother to set. */
1738 csect_index = ((esym
1739 - (bfd_byte *) obj_coff_external_syms (abfd))
1740 / symesz);
1742 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1744 if (first_csect == NULL)
1745 first_csect = csect;
1747 /* If this symbol is external, we treat it as starting at the
1748 beginning of the newly created section. */
1749 if (EXTERN_SYM_P (sym.n_sclass))
1751 section = csect;
1752 value = 0;
1755 /* If this is a TOC section for a symbol, record it. */
1756 if (set_toc != NULL)
1757 set_toc->toc_section = csect;
1759 break;
1761 case XTY_LD:
1762 /* This is a label definition. The x_scnlen field is the
1763 symbol index of the csect. Usually the XTY_LD symbol will
1764 follow its appropriate XTY_SD symbol. The .set pseudo op can
1765 cause the XTY_LD to not follow the XTY_SD symbol. */
1767 bfd_boolean bad;
1769 bad = FALSE;
1770 if (aux.x_csect.x_scnlen.l < 0
1771 || (aux.x_csect.x_scnlen.l
1772 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1773 bad = TRUE;
1774 if (! bad)
1776 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1777 if (section == NULL
1778 || (section->flags & SEC_HAS_CONTENTS) == 0)
1779 bad = TRUE;
1781 if (bad)
1783 (*_bfd_error_handler)
1784 (_("%B: misplaced XTY_LD `%s'"),
1785 abfd, name);
1786 bfd_set_error (bfd_error_bad_value);
1787 goto error_return;
1789 csect = section;
1790 value = sym.n_value - csect->vma;
1792 break;
1794 case XTY_CM:
1795 /* This is an unitialized csect. We could base the name on
1796 the storage mapping class, but we don't bother except for
1797 an XMC_TD symbol. If this csect is externally visible,
1798 it is a common symbol. We put XMC_TD symbols in sections
1799 named .tocbss, and rely on the linker script to put that
1800 in the TOC area. */
1802 if (aux.x_csect.x_smclas == XMC_TD)
1804 /* The linker script puts the .td section in the data
1805 section after the .tc section. */
1806 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1807 SEC_ALLOC);
1809 else
1810 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1811 SEC_ALLOC);
1813 if (csect == NULL)
1814 goto error_return;
1815 csect->vma = sym.n_value;
1816 csect->size = aux.x_csect.x_scnlen.l;
1817 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1818 /* There are a number of other fields and section flags
1819 which we do not bother to set. */
1821 csect_index = ((esym
1822 - (bfd_byte *) obj_coff_external_syms (abfd))
1823 / symesz);
1825 amt = sizeof (struct coff_section_tdata);
1826 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1827 if (csect->used_by_bfd == NULL)
1828 goto error_return;
1829 amt = sizeof (struct xcoff_section_tdata);
1830 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1831 if (coff_section_data (abfd, csect)->tdata == NULL)
1832 goto error_return;
1833 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1835 if (first_csect == NULL)
1836 first_csect = csect;
1838 if (EXTERN_SYM_P (sym.n_sclass))
1840 csect->flags |= SEC_IS_COMMON;
1841 csect->size = 0;
1842 section = csect;
1843 value = aux.x_csect.x_scnlen.l;
1846 break;
1849 /* Check for magic symbol names. */
1850 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1851 && aux.x_csect.x_smclas != XMC_TC
1852 && aux.x_csect.x_smclas != XMC_TD)
1854 int i = -1;
1856 if (name[0] == '_')
1858 if (strcmp (name, "_text") == 0)
1859 i = XCOFF_SPECIAL_SECTION_TEXT;
1860 else if (strcmp (name, "_etext") == 0)
1861 i = XCOFF_SPECIAL_SECTION_ETEXT;
1862 else if (strcmp (name, "_data") == 0)
1863 i = XCOFF_SPECIAL_SECTION_DATA;
1864 else if (strcmp (name, "_edata") == 0)
1865 i = XCOFF_SPECIAL_SECTION_EDATA;
1866 else if (strcmp (name, "_end") == 0)
1867 i = XCOFF_SPECIAL_SECTION_END;
1869 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1870 i = XCOFF_SPECIAL_SECTION_END2;
1872 if (i != -1)
1873 xcoff_hash_table (info)->special_sections[i] = csect;
1876 /* Now we have enough information to add the symbol to the
1877 linker hash table. */
1879 if (EXTERN_SYM_P (sym.n_sclass))
1881 bfd_boolean copy;
1882 flagword flags;
1884 BFD_ASSERT (section != NULL);
1886 /* We must copy the name into memory if we got it from the
1887 syment itself, rather than the string table. */
1888 copy = default_copy;
1889 if (sym._n._n_n._n_zeroes != 0
1890 || sym._n._n_n._n_offset == 0)
1891 copy = TRUE;
1893 /* Ignore global linkage code when linking statically. */
1894 if (info->static_link
1895 && (smtyp == XTY_SD || smtyp == XTY_LD)
1896 && aux.x_csect.x_smclas == XMC_GL)
1898 section = bfd_und_section_ptr;
1899 value = 0;
1902 /* The AIX linker appears to only detect multiple symbol
1903 definitions when there is a reference to the symbol. If
1904 a symbol is defined multiple times, and the only
1905 references are from the same object file, the AIX linker
1906 appears to permit it. It does not merge the different
1907 definitions, but handles them independently. On the
1908 other hand, if there is a reference, the linker reports
1909 an error.
1911 This matters because the AIX <net/net_globals.h> header
1912 file actually defines an initialized array, so we have to
1913 actually permit that to work.
1915 Just to make matters even more confusing, the AIX linker
1916 appears to permit multiple symbol definitions whenever
1917 the second definition is in an archive rather than an
1918 object file. This may be a consequence of the manner in
1919 which it handles archives: I think it may load the entire
1920 archive in as separate csects, and then let garbage
1921 collection discard symbols.
1923 We also have to handle the case of statically linking a
1924 shared object, which will cause symbol redefinitions,
1925 although this is an easier case to detect. */
1926 else if (info->output_bfd->xvec == abfd->xvec)
1928 if (! bfd_is_und_section (section))
1929 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1930 name, TRUE, copy, FALSE);
1931 else
1932 /* Make a copy of the symbol name to prevent problems with
1933 merging symbols. */
1934 *sym_hash = ((struct xcoff_link_hash_entry *)
1935 bfd_wrapped_link_hash_lookup (abfd, info, name,
1936 TRUE, TRUE, FALSE));
1938 if (*sym_hash == NULL)
1939 goto error_return;
1940 if (((*sym_hash)->root.type == bfd_link_hash_defined
1941 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1942 && ! bfd_is_und_section (section)
1943 && ! bfd_is_com_section (section))
1945 /* This is a second definition of a defined symbol. */
1946 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1947 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1949 /* The existing symbol is from a shared library.
1950 Replace it. */
1951 (*sym_hash)->root.type = bfd_link_hash_undefined;
1952 (*sym_hash)->root.u.undef.abfd =
1953 (*sym_hash)->root.u.def.section->owner;
1955 else if (abfd->my_archive != NULL)
1957 /* This is a redefinition in an object contained
1958 in an archive. Just ignore it. See the
1959 comment above. */
1960 section = bfd_und_section_ptr;
1961 value = 0;
1963 else if (sym.n_sclass == C_AIX_WEAKEXT
1964 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1966 /* At least one of the definitions is weak.
1967 Allow the normal rules to take effect. */
1969 else if ((*sym_hash)->root.u.undef.next != NULL
1970 || info->hash->undefs_tail == &(*sym_hash)->root)
1972 /* This symbol has been referenced. In this
1973 case, we just continue and permit the
1974 multiple definition error. See the comment
1975 above about the behaviour of the AIX linker. */
1977 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1979 /* The symbols are both csects of the same
1980 class. There is at least a chance that this
1981 is a semi-legitimate redefinition. */
1982 section = bfd_und_section_ptr;
1983 value = 0;
1984 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1987 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1988 && (*sym_hash)->root.type == bfd_link_hash_defined
1989 && (bfd_is_und_section (section)
1990 || bfd_is_com_section (section)))
1992 /* This is a reference to a multiply defined symbol.
1993 Report the error now. See the comment above
1994 about the behaviour of the AIX linker. We could
1995 also do this with warning symbols, but I'm not
1996 sure the XCOFF linker is wholly prepared to
1997 handle them, and that would only be a warning,
1998 not an error. */
1999 if (! ((*info->callbacks->multiple_definition)
2000 (info, &(*sym_hash)->root, NULL, NULL, (bfd_vma) 0)))
2001 goto error_return;
2002 /* Try not to give this error too many times. */
2003 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2007 /* _bfd_generic_link_add_one_symbol may call the linker to
2008 generate an error message, and the linker may try to read
2009 the symbol table to give a good error. Right now, the
2010 line numbers are in an inconsistent state, since they are
2011 counted both in the real sections and in the new csects.
2012 We need to leave the count in the real sections so that
2013 the linker can report the line number of the error
2014 correctly, so temporarily clobber the link to the csects
2015 so that the linker will not try to read the line numbers
2016 a second time from the csects. */
2017 BFD_ASSERT (last_real->next == first_csect);
2018 last_real->next = NULL;
2019 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2020 if (! (_bfd_generic_link_add_one_symbol
2021 (info, abfd, name, flags, section, value,
2022 NULL, copy, TRUE,
2023 (struct bfd_link_hash_entry **) sym_hash)))
2024 goto error_return;
2025 last_real->next = first_csect;
2027 if (smtyp == XTY_CM)
2029 if ((*sym_hash)->root.type != bfd_link_hash_common
2030 || (*sym_hash)->root.u.c.p->section != csect)
2031 /* We don't need the common csect we just created. */
2032 csect->size = 0;
2033 else
2034 (*sym_hash)->root.u.c.p->alignment_power
2035 = csect->alignment_power;
2038 if (info->output_bfd->xvec == abfd->xvec)
2040 int flag;
2042 if (smtyp == XTY_ER
2043 || smtyp == XTY_CM
2044 || section == bfd_und_section_ptr)
2045 flag = XCOFF_REF_REGULAR;
2046 else
2047 flag = XCOFF_DEF_REGULAR;
2048 (*sym_hash)->flags |= flag;
2050 if ((*sym_hash)->smclas == XMC_UA
2051 || flag == XCOFF_DEF_REGULAR)
2052 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2056 if (smtyp == XTY_ER)
2057 *csect_cache = section;
2058 else
2060 *csect_cache = csect;
2061 if (csect != NULL)
2062 xcoff_section_data (abfd, csect)->last_symndx
2063 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2066 esym += (sym.n_numaux + 1) * symesz;
2067 sym_hash += sym.n_numaux + 1;
2068 csect_cache += sym.n_numaux + 1;
2069 lineno_counts += sym.n_numaux + 1;
2072 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2074 /* Make sure that we have seen all the relocs. */
2075 for (o = abfd->sections; o != first_csect; o = o->next)
2077 /* Debugging sections have no csects. */
2078 if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING)
2079 continue;
2081 /* Reset the section size and the line number count, since the
2082 data is now attached to the csects. Don't reset the size of
2083 the .debug section, since we need to read it below in
2084 bfd_xcoff_size_dynamic_sections. */
2085 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2086 o->size = 0;
2087 o->lineno_count = 0;
2089 if ((o->flags & SEC_RELOC) != 0)
2091 bfd_size_type i;
2092 struct internal_reloc *rel;
2093 asection **rel_csect;
2095 rel = reloc_info[o->target_index].relocs;
2096 rel_csect = reloc_info[o->target_index].csects;
2098 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2100 if (*rel_csect == NULL)
2102 (*_bfd_error_handler)
2103 (_("%B: reloc %s:%d not in csect"),
2104 abfd, o->name, i);
2105 bfd_set_error (bfd_error_bad_value);
2106 goto error_return;
2109 /* We identify all function symbols that are the target
2110 of a relocation, so that we can create glue code for
2111 functions imported from dynamic objects. */
2112 if (info->output_bfd->xvec == abfd->xvec
2113 && *rel_csect != bfd_und_section_ptr
2114 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2116 struct xcoff_link_hash_entry *h;
2118 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2119 /* If the symbol name starts with a period, it is
2120 the code of a function. If the symbol is
2121 currently undefined, then add an undefined symbol
2122 for the function descriptor. This should do no
2123 harm, because any regular object that defines the
2124 function should also define the function
2125 descriptor. It helps, because it means that we
2126 will identify the function descriptor with a
2127 dynamic object if a dynamic object defines it. */
2128 if (h->root.root.string[0] == '.'
2129 && h->descriptor == NULL)
2131 struct xcoff_link_hash_entry *hds;
2132 struct bfd_link_hash_entry *bh;
2134 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2135 h->root.root.string + 1,
2136 TRUE, FALSE, TRUE);
2137 if (hds == NULL)
2138 goto error_return;
2139 if (hds->root.type == bfd_link_hash_new)
2141 bh = &hds->root;
2142 if (! (_bfd_generic_link_add_one_symbol
2143 (info, abfd, hds->root.root.string,
2144 (flagword) 0, bfd_und_section_ptr,
2145 (bfd_vma) 0, NULL, FALSE,
2146 TRUE, &bh)))
2147 goto error_return;
2148 hds = (struct xcoff_link_hash_entry *) bh;
2150 hds->flags |= XCOFF_DESCRIPTOR;
2151 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2152 hds->descriptor = h;
2153 h->descriptor = hds;
2155 if (h->root.root.string[0] == '.')
2156 h->flags |= XCOFF_CALLED;
2160 free (reloc_info[o->target_index].csects);
2161 reloc_info[o->target_index].csects = NULL;
2163 /* Reset SEC_RELOC and the reloc_count, since the reloc
2164 information is now attached to the csects. */
2165 o->flags &=~ SEC_RELOC;
2166 o->reloc_count = 0;
2168 /* If we are not keeping memory, free the reloc information. */
2169 if (! info->keep_memory
2170 && coff_section_data (abfd, o) != NULL
2171 && coff_section_data (abfd, o)->relocs != NULL
2172 && ! coff_section_data (abfd, o)->keep_relocs)
2174 free (coff_section_data (abfd, o)->relocs);
2175 coff_section_data (abfd, o)->relocs = NULL;
2179 /* Free up the line numbers. FIXME: We could cache these
2180 somewhere for the final link, to avoid reading them again. */
2181 if (reloc_info[o->target_index].linenos != NULL)
2183 free (reloc_info[o->target_index].linenos);
2184 reloc_info[o->target_index].linenos = NULL;
2188 free (reloc_info);
2190 obj_coff_keep_syms (abfd) = keep_syms;
2192 return TRUE;
2194 error_return:
2195 if (reloc_info != NULL)
2197 for (o = abfd->sections; o != NULL; o = o->next)
2199 if (reloc_info[o->target_index].csects != NULL)
2200 free (reloc_info[o->target_index].csects);
2201 if (reloc_info[o->target_index].linenos != NULL)
2202 free (reloc_info[o->target_index].linenos);
2204 free (reloc_info);
2206 obj_coff_keep_syms (abfd) = keep_syms;
2207 return FALSE;
2210 #undef N_TMASK
2211 #undef N_BTSHFT
2213 /* Add symbols from an XCOFF object file. */
2215 static bfd_boolean
2216 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2218 if (! _bfd_coff_get_external_symbols (abfd))
2219 return FALSE;
2220 if (! xcoff_link_add_symbols (abfd, info))
2221 return FALSE;
2222 if (! info->keep_memory)
2224 if (! _bfd_coff_free_symbols (abfd))
2225 return FALSE;
2227 return TRUE;
2230 /* Look through the loader symbols to see if this dynamic object
2231 should be included in the link. The native linker uses the loader
2232 symbols, not the normal symbol table, so we do too. */
2234 static bfd_boolean
2235 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2236 struct bfd_link_info *info,
2237 bfd_boolean *pneeded,
2238 bfd **subsbfd)
2240 asection *lsec;
2241 bfd_byte *contents;
2242 struct internal_ldhdr ldhdr;
2243 const char *strings;
2244 bfd_byte *elsym, *elsymend;
2246 *pneeded = FALSE;
2248 lsec = bfd_get_section_by_name (abfd, ".loader");
2249 if (lsec == NULL)
2250 /* There are no symbols, so don't try to include it. */
2251 return TRUE;
2253 if (! xcoff_get_section_contents (abfd, lsec))
2254 return FALSE;
2255 contents = coff_section_data (abfd, lsec)->contents;
2257 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2259 strings = (char *) contents + ldhdr.l_stoff;
2261 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2263 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2264 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2266 struct internal_ldsym ldsym;
2267 char nambuf[SYMNMLEN + 1];
2268 const char *name;
2269 struct bfd_link_hash_entry *h;
2271 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2273 /* We are only interested in exported symbols. */
2274 if ((ldsym.l_smtype & L_EXPORT) == 0)
2275 continue;
2277 if (ldsym._l._l_l._l_zeroes == 0)
2278 name = strings + ldsym._l._l_l._l_offset;
2279 else
2281 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2282 nambuf[SYMNMLEN] = '\0';
2283 name = nambuf;
2286 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2288 /* We are only interested in symbols that are currently
2289 undefined. At this point we know that we are using an XCOFF
2290 hash table. */
2291 if (h != NULL
2292 && h->type == bfd_link_hash_undefined
2293 && (((struct xcoff_link_hash_entry *) h)->flags
2294 & XCOFF_DEF_DYNAMIC) == 0)
2296 if (!(*info->callbacks
2297 ->add_archive_element) (info, abfd, name, subsbfd))
2298 return FALSE;
2299 *pneeded = TRUE;
2300 return TRUE;
2304 /* We do not need this shared object. */
2305 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2307 free (coff_section_data (abfd, lsec)->contents);
2308 coff_section_data (abfd, lsec)->contents = NULL;
2311 return TRUE;
2314 /* Look through the symbols to see if this object file should be
2315 included in the link. */
2317 static bfd_boolean
2318 xcoff_link_check_ar_symbols (bfd *abfd,
2319 struct bfd_link_info *info,
2320 bfd_boolean *pneeded,
2321 bfd **subsbfd)
2323 bfd_size_type symesz;
2324 bfd_byte *esym;
2325 bfd_byte *esym_end;
2327 *pneeded = FALSE;
2329 if ((abfd->flags & DYNAMIC) != 0
2330 && ! info->static_link
2331 && info->output_bfd->xvec == abfd->xvec)
2332 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2334 symesz = bfd_coff_symesz (abfd);
2335 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2336 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2337 while (esym < esym_end)
2339 struct internal_syment sym;
2341 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2343 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2345 const char *name;
2346 char buf[SYMNMLEN + 1];
2347 struct bfd_link_hash_entry *h;
2349 /* This symbol is externally visible, and is defined by this
2350 object file. */
2351 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2353 if (name == NULL)
2354 return FALSE;
2355 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2357 /* We are only interested in symbols that are currently
2358 undefined. If a symbol is currently known to be common,
2359 XCOFF linkers do not bring in an object file which
2360 defines it. We also don't bring in symbols to satisfy
2361 undefined references in shared objects. */
2362 if (h != NULL
2363 && h->type == bfd_link_hash_undefined
2364 && (info->output_bfd->xvec != abfd->xvec
2365 || (((struct xcoff_link_hash_entry *) h)->flags
2366 & XCOFF_DEF_DYNAMIC) == 0))
2368 if (!(*info->callbacks
2369 ->add_archive_element) (info, abfd, name, subsbfd))
2370 return FALSE;
2371 *pneeded = TRUE;
2372 return TRUE;
2376 esym += (sym.n_numaux + 1) * symesz;
2379 /* We do not need this object file. */
2380 return TRUE;
2383 /* Check a single archive element to see if we need to include it in
2384 the link. *PNEEDED is set according to whether this element is
2385 needed in the link or not. This is called via
2386 _bfd_generic_link_add_archive_symbols. */
2388 static bfd_boolean
2389 xcoff_link_check_archive_element (bfd *abfd,
2390 struct bfd_link_info *info,
2391 bfd_boolean *pneeded)
2393 bfd_boolean keep_syms_p;
2394 bfd *oldbfd;
2396 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2397 if (!_bfd_coff_get_external_symbols (abfd))
2398 return FALSE;
2400 oldbfd = abfd;
2401 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2402 return FALSE;
2404 if (*pneeded)
2406 /* Potentially, the add_archive_element hook may have set a
2407 substitute BFD for us. */
2408 if (abfd != oldbfd)
2410 if (!keep_syms_p
2411 && !_bfd_coff_free_symbols (oldbfd))
2412 return FALSE;
2413 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2414 if (!_bfd_coff_get_external_symbols (abfd))
2415 return FALSE;
2417 if (!xcoff_link_add_symbols (abfd, info))
2418 return FALSE;
2419 if (info->keep_memory)
2420 keep_syms_p = TRUE;
2423 if (!keep_syms_p)
2425 if (!_bfd_coff_free_symbols (abfd))
2426 return FALSE;
2429 return TRUE;
2432 /* Given an XCOFF BFD, add symbols to the global hash table as
2433 appropriate. */
2435 bfd_boolean
2436 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2438 switch (bfd_get_format (abfd))
2440 case bfd_object:
2441 return xcoff_link_add_object_symbols (abfd, info);
2443 case bfd_archive:
2444 /* If the archive has a map, do the usual search. We then need
2445 to check the archive for dynamic objects, because they may not
2446 appear in the archive map even though they should, perhaps, be
2447 included. If the archive has no map, we just consider each object
2448 file in turn, since that apparently is what the AIX native linker
2449 does. */
2450 if (bfd_has_map (abfd))
2452 if (! (_bfd_generic_link_add_archive_symbols
2453 (abfd, info, xcoff_link_check_archive_element)))
2454 return FALSE;
2458 bfd *member;
2460 member = bfd_openr_next_archived_file (abfd, NULL);
2461 while (member != NULL)
2463 if (bfd_check_format (member, bfd_object)
2464 && (info->output_bfd->xvec == member->xvec)
2465 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2467 bfd_boolean needed;
2469 if (! xcoff_link_check_archive_element (member, info,
2470 &needed))
2471 return FALSE;
2472 if (needed)
2473 member->archive_pass = -1;
2475 member = bfd_openr_next_archived_file (abfd, member);
2479 return TRUE;
2481 default:
2482 bfd_set_error (bfd_error_wrong_format);
2483 return FALSE;
2487 bfd_boolean
2488 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2489 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2490 struct bfd_link_hash_entry *harg)
2492 struct xcoff_link_hash_entry *h;
2494 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2495 return FALSE;
2497 h = (struct xcoff_link_hash_entry *) harg;
2498 h->flags |= XCOFF_DEF_REGULAR;
2499 return TRUE;
2502 /* If symbol H has not been interpreted as a function descriptor,
2503 see whether it should be. Set up its descriptor information if so. */
2505 static bfd_boolean
2506 xcoff_find_function (struct bfd_link_info *info,
2507 struct xcoff_link_hash_entry *h)
2509 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2510 && h->root.root.string[0] != '.')
2512 char *fnname;
2513 struct xcoff_link_hash_entry *hfn;
2514 bfd_size_type amt;
2516 amt = strlen (h->root.root.string) + 2;
2517 fnname = bfd_malloc (amt);
2518 if (fnname == NULL)
2519 return FALSE;
2520 fnname[0] = '.';
2521 strcpy (fnname + 1, h->root.root.string);
2522 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2523 fnname, FALSE, FALSE, TRUE);
2524 free (fnname);
2525 if (hfn != NULL
2526 && hfn->smclas == XMC_PR
2527 && (hfn->root.type == bfd_link_hash_defined
2528 || hfn->root.type == bfd_link_hash_defweak))
2530 h->flags |= XCOFF_DESCRIPTOR;
2531 h->descriptor = hfn;
2532 hfn->descriptor = h;
2535 return TRUE;
2538 /* Return true if the given bfd contains at least one shared object. */
2540 static bfd_boolean
2541 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2542 bfd *archive)
2544 struct xcoff_archive_info *archive_info;
2545 bfd *member;
2547 archive_info = xcoff_get_archive_info (info, archive);
2548 if (!archive_info->know_contains_shared_object_p)
2550 member = bfd_openr_next_archived_file (archive, NULL);
2551 while (member != NULL && (member->flags & DYNAMIC) == 0)
2552 member = bfd_openr_next_archived_file (archive, member);
2554 archive_info->contains_shared_object_p = (member != NULL);
2555 archive_info->know_contains_shared_object_p = 1;
2557 return archive_info->contains_shared_object_p;
2560 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2561 qualifies for export by -bexpall. */
2563 static bfd_boolean
2564 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2566 /* Exclude symbols beginning with '_'. */
2567 if (h->root.root.string[0] == '_')
2568 return FALSE;
2570 /* Exclude archive members that would otherwise be unreferenced. */
2571 if ((h->flags & XCOFF_MARK) == 0
2572 && (h->root.type == bfd_link_hash_defined
2573 || h->root.type == bfd_link_hash_defweak)
2574 && h->root.u.def.section->owner != NULL
2575 && h->root.u.def.section->owner->my_archive != NULL)
2576 return FALSE;
2578 return TRUE;
2581 /* Return true if symbol H qualifies for the forms of automatic export
2582 specified by AUTO_EXPORT_FLAGS. */
2584 static bfd_boolean
2585 xcoff_auto_export_p (struct bfd_link_info *info,
2586 struct xcoff_link_hash_entry *h,
2587 unsigned int auto_export_flags)
2589 /* Don't automatically export things that were explicitly exported. */
2590 if ((h->flags & XCOFF_EXPORT) != 0)
2591 return FALSE;
2593 /* Don't export things that we don't define. */
2594 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2595 return FALSE;
2597 /* Don't export functions; export their descriptors instead. */
2598 if (h->root.root.string[0] == '.')
2599 return FALSE;
2601 /* We don't export a symbol which is being defined by an object
2602 included from an archive which contains a shared object. The
2603 rationale is that if an archive contains both an unshared and
2604 a shared object, then there must be some reason that the
2605 unshared object is unshared, and we don't want to start
2606 providing a shared version of it. In particular, this solves
2607 a bug involving the _savefNN set of functions. gcc will call
2608 those functions without providing a slot to restore the TOC,
2609 so it is essential that these functions be linked in directly
2610 and not from a shared object, which means that a shared
2611 object which also happens to link them in must not export
2612 them. This is confusing, but I haven't been able to think of
2613 a different approach. Note that the symbols can, of course,
2614 be exported explicitly. */
2615 if (h->root.type == bfd_link_hash_defined
2616 || h->root.type == bfd_link_hash_defweak)
2618 bfd *owner;
2620 owner = h->root.u.def.section->owner;
2621 if (owner != NULL
2622 && owner->my_archive != NULL
2623 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2624 return FALSE;
2627 /* Otherwise, all symbols are exported by -bexpfull. */
2628 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2629 return TRUE;
2631 /* Despite its name, -bexpall exports most but not all symbols. */
2632 if ((auto_export_flags & XCOFF_EXPALL) != 0
2633 && xcoff_covered_by_expall_p (h))
2634 return TRUE;
2636 return FALSE;
2639 /* Return true if relocation REL needs to be copied to the .loader section.
2640 If REL is against a global symbol, H is that symbol, otherwise it
2641 is null. */
2643 static bfd_boolean
2644 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2645 struct xcoff_link_hash_entry *h)
2647 if (!xcoff_hash_table (info)->loader_section)
2648 return FALSE;
2650 switch (rel->r_type)
2652 case R_TOC:
2653 case R_GL:
2654 case R_TCL:
2655 case R_TRL:
2656 case R_TRLA:
2657 /* We should never need a .loader reloc for a TOC-relative reloc. */
2658 return FALSE;
2660 default:
2661 /* In this case, relocations against defined symbols can be resolved
2662 statically. */
2663 if (h == NULL
2664 || h->root.type == bfd_link_hash_defined
2665 || h->root.type == bfd_link_hash_defweak
2666 || h->root.type == bfd_link_hash_common)
2667 return FALSE;
2669 /* We will always provide a local definition of function symbols,
2670 even if we don't have one yet. */
2671 if ((h->flags & XCOFF_CALLED) != 0)
2672 return FALSE;
2674 return TRUE;
2676 case R_POS:
2677 case R_NEG:
2678 case R_RL:
2679 case R_RLA:
2680 /* Absolute relocations against absolute symbols can be
2681 resolved statically. */
2682 if (h != NULL
2683 && (h->root.type == bfd_link_hash_defined
2684 || h->root.type == bfd_link_hash_defweak)
2685 && bfd_is_abs_section (h->root.u.def.section))
2686 return FALSE;
2688 return TRUE;
2692 /* Mark a symbol as not being garbage, including the section in which
2693 it is defined. */
2695 static inline bfd_boolean
2696 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2698 if ((h->flags & XCOFF_MARK) != 0)
2699 return TRUE;
2701 h->flags |= XCOFF_MARK;
2703 /* If we're marking an undefined symbol, try find some way of
2704 defining it. */
2705 if (!info->relocatable
2706 && (h->flags & XCOFF_IMPORT) == 0
2707 && (h->flags & XCOFF_DEF_REGULAR) == 0
2708 && (h->root.type == bfd_link_hash_undefined
2709 || h->root.type == bfd_link_hash_undefweak))
2711 /* First check whether this symbol can be interpreted as an
2712 undefined function descriptor for a defined function symbol. */
2713 if (!xcoff_find_function (info, h))
2714 return FALSE;
2716 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2717 && (h->descriptor->root.type == bfd_link_hash_defined
2718 || h->descriptor->root.type == bfd_link_hash_defweak))
2720 /* This is a descriptor for a defined symbol, but the input
2721 objects have not defined the descriptor itself. Fill in
2722 the definition automatically.
2724 Note that we do this even if we found a dynamic definition
2725 of H. The local function definition logically overrides
2726 the dynamic one. */
2727 asection *sec;
2729 sec = xcoff_hash_table (info)->descriptor_section;
2730 h->root.type = bfd_link_hash_defined;
2731 h->root.u.def.section = sec;
2732 h->root.u.def.value = sec->size;
2733 h->smclas = XMC_DS;
2734 h->flags |= XCOFF_DEF_REGULAR;
2736 /* The size of the function descriptor depends on whether this
2737 is xcoff32 (12) or xcoff64 (24). */
2738 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2740 /* A function descriptor uses two relocs: one for the
2741 associated code, and one for the TOC address. */
2742 xcoff_hash_table (info)->ldrel_count += 2;
2743 sec->reloc_count += 2;
2745 /* Mark the function itself. */
2746 if (!xcoff_mark_symbol (info, h->descriptor))
2747 return FALSE;
2749 /* Mark the TOC section, so that we get an anchor
2750 to relocate against. */
2751 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2752 return FALSE;
2754 /* We handle writing out the contents of the descriptor in
2755 xcoff_write_global_symbol. */
2757 else if (info->static_link)
2758 /* We can't get a symbol value dynamically, so just assume
2759 that it's undefined. */
2760 h->flags |= XCOFF_WAS_UNDEFINED;
2761 else if ((h->flags & XCOFF_CALLED) != 0)
2763 /* This is a function symbol for which we need to create
2764 linkage code. */
2765 asection *sec;
2766 struct xcoff_link_hash_entry *hds;
2768 /* Mark the descriptor (and its TOC section). */
2769 hds = h->descriptor;
2770 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2771 || hds->root.type == bfd_link_hash_undefweak)
2772 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2773 if (!xcoff_mark_symbol (info, hds))
2774 return FALSE;
2776 /* Treat this symbol as undefined if the descriptor was. */
2777 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2778 h->flags |= XCOFF_WAS_UNDEFINED;
2780 /* Allocate room for the global linkage code itself. */
2781 sec = xcoff_hash_table (info)->linkage_section;
2782 h->root.type = bfd_link_hash_defined;
2783 h->root.u.def.section = sec;
2784 h->root.u.def.value = sec->size;
2785 h->smclas = XMC_GL;
2786 h->flags |= XCOFF_DEF_REGULAR;
2787 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2789 /* The global linkage code requires a TOC entry for the
2790 descriptor. */
2791 if (hds->toc_section == NULL)
2793 int byte_size;
2795 /* 32 vs 64
2796 xcoff32 uses 4 bytes in the toc.
2797 xcoff64 uses 8 bytes in the toc. */
2798 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2799 byte_size = 8;
2800 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2801 byte_size = 4;
2802 else
2803 return FALSE;
2805 /* Allocate room in the fallback TOC section. */
2806 hds->toc_section = xcoff_hash_table (info)->toc_section;
2807 hds->u.toc_offset = hds->toc_section->size;
2808 hds->toc_section->size += byte_size;
2809 if (!xcoff_mark (info, hds->toc_section))
2810 return FALSE;
2812 /* Allocate room for a static and dynamic R_TOC
2813 relocation. */
2814 ++xcoff_hash_table (info)->ldrel_count;
2815 ++hds->toc_section->reloc_count;
2817 /* Set the index to -2 to force this symbol to
2818 get written out. */
2819 hds->indx = -2;
2820 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2823 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2825 /* Record that the symbol was undefined, then import it.
2826 -brtl links use a special fake import file. */
2827 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2828 if (xcoff_hash_table (info)->rtld)
2830 if (!xcoff_set_import_path (info, h, "", "..", ""))
2831 return FALSE;
2833 else
2835 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2836 return FALSE;
2841 if (h->root.type == bfd_link_hash_defined
2842 || h->root.type == bfd_link_hash_defweak)
2844 asection *hsec;
2846 hsec = h->root.u.def.section;
2847 if (! bfd_is_abs_section (hsec)
2848 && (hsec->flags & SEC_MARK) == 0)
2850 if (! xcoff_mark (info, hsec))
2851 return FALSE;
2855 if (h->toc_section != NULL
2856 && (h->toc_section->flags & SEC_MARK) == 0)
2858 if (! xcoff_mark (info, h->toc_section))
2859 return FALSE;
2862 return TRUE;
2865 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2866 If the symbol exists, set FLAGS. */
2868 static bfd_boolean
2869 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2870 const char *name, unsigned int flags)
2872 struct xcoff_link_hash_entry *h;
2874 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2875 FALSE, FALSE, TRUE);
2876 if (h != NULL)
2878 h->flags |= flags;
2879 if (h->root.type == bfd_link_hash_defined
2880 || h->root.type == bfd_link_hash_defweak)
2882 if (!xcoff_mark (info, h->root.u.def.section))
2883 return FALSE;
2886 return TRUE;
2889 /* The mark phase of garbage collection. For a given section, mark
2890 it, and all the sections which define symbols to which it refers.
2891 Because this function needs to look at the relocs, we also count
2892 the number of relocs which need to be copied into the .loader
2893 section. */
2895 static bfd_boolean
2896 xcoff_mark (struct bfd_link_info *info, asection *sec)
2898 if (bfd_is_abs_section (sec)
2899 || (sec->flags & SEC_MARK) != 0)
2900 return TRUE;
2902 sec->flags |= SEC_MARK;
2904 if (sec->owner->xvec == info->output_bfd->xvec
2905 && coff_section_data (sec->owner, sec) != NULL
2906 && xcoff_section_data (sec->owner, sec) != NULL)
2908 struct xcoff_link_hash_entry **syms;
2909 struct internal_reloc *rel, *relend;
2910 asection **csects;
2911 unsigned long i, first, last;
2913 /* Mark all the symbols in this section. */
2914 syms = obj_xcoff_sym_hashes (sec->owner);
2915 csects = xcoff_data (sec->owner)->csects;
2916 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2917 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2918 for (i = first; i <= last; i++)
2919 if (csects[i] == sec
2920 && syms[i] != NULL
2921 && (syms[i]->flags & XCOFF_MARK) == 0)
2923 if (!xcoff_mark_symbol (info, syms[i]))
2924 return FALSE;
2927 /* Look through the section relocs. */
2928 if ((sec->flags & SEC_RELOC) != 0
2929 && sec->reloc_count > 0)
2931 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2932 NULL, FALSE, NULL);
2933 if (rel == NULL)
2934 return FALSE;
2935 relend = rel + sec->reloc_count;
2936 for (; rel < relend; rel++)
2938 struct xcoff_link_hash_entry *h;
2940 if ((unsigned int) rel->r_symndx
2941 > obj_raw_syment_count (sec->owner))
2942 continue;
2944 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2945 if (h != NULL)
2947 if ((h->flags & XCOFF_MARK) == 0)
2949 if (!xcoff_mark_symbol (info, h))
2950 return FALSE;
2953 else
2955 asection *rsec;
2957 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2958 if (rsec != NULL
2959 && (rsec->flags & SEC_MARK) == 0)
2961 if (!xcoff_mark (info, rsec))
2962 return FALSE;
2966 /* See if this reloc needs to be copied into the .loader
2967 section. */
2968 if (xcoff_need_ldrel_p (info, rel, h))
2970 ++xcoff_hash_table (info)->ldrel_count;
2971 if (h != NULL)
2972 h->flags |= XCOFF_LDREL;
2976 if (! info->keep_memory
2977 && coff_section_data (sec->owner, sec) != NULL
2978 && coff_section_data (sec->owner, sec)->relocs != NULL
2979 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2981 free (coff_section_data (sec->owner, sec)->relocs);
2982 coff_section_data (sec->owner, sec)->relocs = NULL;
2987 return TRUE;
2990 /* Routines that are called after all the input files have been
2991 handled, but before the sections are laid out in memory. */
2993 /* The sweep phase of garbage collection. Remove all garbage
2994 sections. */
2996 static void
2997 xcoff_sweep (struct bfd_link_info *info)
2999 bfd *sub;
3001 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3003 asection *o;
3005 for (o = sub->sections; o != NULL; o = o->next)
3007 if ((o->flags & SEC_MARK) == 0)
3009 /* Keep all sections from non-XCOFF input files. Keep
3010 special sections. Keep .debug sections for the
3011 moment. */
3012 if (sub->xvec != info->output_bfd->xvec
3013 || o == xcoff_hash_table (info)->debug_section
3014 || o == xcoff_hash_table (info)->loader_section
3015 || o == xcoff_hash_table (info)->linkage_section
3016 || o == xcoff_hash_table (info)->descriptor_section
3017 || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING)
3018 || strcmp (o->name, ".debug") == 0)
3019 o->flags |= SEC_MARK;
3020 else
3022 o->size = 0;
3023 o->reloc_count = 0;
3030 /* Record the number of elements in a set. This is used to output the
3031 correct csect length. */
3033 bfd_boolean
3034 bfd_xcoff_link_record_set (bfd *output_bfd,
3035 struct bfd_link_info *info,
3036 struct bfd_link_hash_entry *harg,
3037 bfd_size_type size)
3039 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3040 struct xcoff_link_size_list *n;
3041 bfd_size_type amt;
3043 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3044 return TRUE;
3046 /* This will hardly ever be called. I don't want to burn four bytes
3047 per global symbol, so instead the size is kept on a linked list
3048 attached to the hash table. */
3049 amt = sizeof (* n);
3050 n = bfd_alloc (output_bfd, amt);
3051 if (n == NULL)
3052 return FALSE;
3053 n->next = xcoff_hash_table (info)->size_list;
3054 n->h = h;
3055 n->size = size;
3056 xcoff_hash_table (info)->size_list = n;
3058 h->flags |= XCOFF_HAS_SIZE;
3060 return TRUE;
3063 /* Import a symbol. */
3065 bfd_boolean
3066 bfd_xcoff_import_symbol (bfd *output_bfd,
3067 struct bfd_link_info *info,
3068 struct bfd_link_hash_entry *harg,
3069 bfd_vma val,
3070 const char *imppath,
3071 const char *impfile,
3072 const char *impmember,
3073 unsigned int syscall_flag)
3075 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3077 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3078 return TRUE;
3080 /* A symbol name which starts with a period is the code for a
3081 function. If the symbol is undefined, then add an undefined
3082 symbol for the function descriptor, and import that instead. */
3083 if (h->root.root.string[0] == '.'
3084 && h->root.type == bfd_link_hash_undefined
3085 && val == (bfd_vma) -1)
3087 struct xcoff_link_hash_entry *hds;
3089 hds = h->descriptor;
3090 if (hds == NULL)
3092 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3093 h->root.root.string + 1,
3094 TRUE, FALSE, TRUE);
3095 if (hds == NULL)
3096 return FALSE;
3097 if (hds->root.type == bfd_link_hash_new)
3099 hds->root.type = bfd_link_hash_undefined;
3100 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3102 hds->flags |= XCOFF_DESCRIPTOR;
3103 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3104 hds->descriptor = h;
3105 h->descriptor = hds;
3108 /* Now, if the descriptor is undefined, import the descriptor
3109 rather than the symbol we were told to import. FIXME: Is
3110 this correct in all cases? */
3111 if (hds->root.type == bfd_link_hash_undefined)
3112 h = hds;
3115 h->flags |= (XCOFF_IMPORT | syscall_flag);
3117 if (val != (bfd_vma) -1)
3119 if (h->root.type == bfd_link_hash_defined
3120 && (! bfd_is_abs_section (h->root.u.def.section)
3121 || h->root.u.def.value != val))
3123 if (! ((*info->callbacks->multiple_definition)
3124 (info, &h->root, output_bfd, bfd_abs_section_ptr, val)))
3125 return FALSE;
3128 h->root.type = bfd_link_hash_defined;
3129 h->root.u.def.section = bfd_abs_section_ptr;
3130 h->root.u.def.value = val;
3131 h->smclas = XMC_XO;
3134 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3135 return FALSE;
3137 return TRUE;
3140 /* Export a symbol. */
3142 bfd_boolean
3143 bfd_xcoff_export_symbol (bfd *output_bfd,
3144 struct bfd_link_info *info,
3145 struct bfd_link_hash_entry *harg)
3147 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3149 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3150 return TRUE;
3152 h->flags |= XCOFF_EXPORT;
3154 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3155 I'm just going to ignore it until somebody explains it. */
3157 /* Make sure we don't garbage collect this symbol. */
3158 if (! xcoff_mark_symbol (info, h))
3159 return FALSE;
3161 /* If this is a function descriptor, make sure we don't garbage
3162 collect the associated function code. We normally don't have to
3163 worry about this, because the descriptor will be attached to a
3164 section with relocs, but if we are creating the descriptor
3165 ourselves those relocs will not be visible to the mark code. */
3166 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3168 if (! xcoff_mark_symbol (info, h->descriptor))
3169 return FALSE;
3172 return TRUE;
3175 /* Count a reloc against a symbol. This is called for relocs
3176 generated by the linker script, typically for global constructors
3177 and destructors. */
3179 bfd_boolean
3180 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3181 struct bfd_link_info *info,
3182 const char *name)
3184 struct xcoff_link_hash_entry *h;
3186 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3187 return TRUE;
3189 h = ((struct xcoff_link_hash_entry *)
3190 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3191 FALSE));
3192 if (h == NULL)
3194 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3195 bfd_set_error (bfd_error_no_symbols);
3196 return FALSE;
3199 h->flags |= XCOFF_REF_REGULAR;
3200 if (xcoff_hash_table (info)->loader_section)
3202 h->flags |= XCOFF_LDREL;
3203 ++xcoff_hash_table (info)->ldrel_count;
3206 /* Mark the symbol to avoid garbage collection. */
3207 if (! xcoff_mark_symbol (info, h))
3208 return FALSE;
3210 return TRUE;
3213 /* This function is called for each symbol to which the linker script
3214 assigns a value. */
3216 bfd_boolean
3217 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3218 struct bfd_link_info *info,
3219 const char *name)
3221 struct xcoff_link_hash_entry *h;
3223 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3224 return TRUE;
3226 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3227 FALSE);
3228 if (h == NULL)
3229 return FALSE;
3231 h->flags |= XCOFF_DEF_REGULAR;
3233 return TRUE;
3236 /* An xcoff_link_hash_traverse callback for which DATA points to an
3237 xcoff_loader_info. Mark all symbols that should be automatically
3238 exported. */
3240 static bfd_boolean
3241 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3243 struct xcoff_loader_info *ldinfo;
3245 ldinfo = (struct xcoff_loader_info *) data;
3246 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3248 if (!xcoff_mark_symbol (ldinfo->info, h))
3249 ldinfo->failed = TRUE;
3251 return TRUE;
3254 /* Add a symbol to the .loader symbols, if necessary. */
3256 /* INPUT_BFD has an external symbol associated with hash table entry H
3257 and csect CSECT. Return true if INPUT_BFD defines H. */
3259 static bfd_boolean
3260 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3261 asection *csect)
3263 switch (h->root.type)
3265 case bfd_link_hash_defined:
3266 case bfd_link_hash_defweak:
3267 /* No input bfd owns absolute symbols. They are written by
3268 xcoff_write_global_symbol instead. */
3269 return (!bfd_is_abs_section (csect)
3270 && h->root.u.def.section == csect);
3272 case bfd_link_hash_common:
3273 return h->root.u.c.p->section->owner == input_bfd;
3275 case bfd_link_hash_undefined:
3276 case bfd_link_hash_undefweak:
3277 /* We can't treat undef.abfd as the owner because that bfd
3278 might be a dynamic object. Allow any bfd to claim it. */
3279 return TRUE;
3281 default:
3282 abort ();
3286 /* See if H should have a loader symbol associated with it. */
3288 static bfd_boolean
3289 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3290 struct xcoff_link_hash_entry *h)
3292 bfd_size_type amt;
3294 /* Warn if this symbol is exported but not defined. */
3295 if ((h->flags & XCOFF_EXPORT) != 0
3296 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3298 (*_bfd_error_handler)
3299 (_("warning: attempt to export undefined symbol `%s'"),
3300 h->root.root.string);
3301 return TRUE;
3304 /* We need to add a symbol to the .loader section if it is mentioned
3305 in a reloc which we are copying to the .loader section and it was
3306 not defined or common, or if it is the entry point, or if it is
3307 being exported. */
3308 if (((h->flags & XCOFF_LDREL) == 0
3309 || h->root.type == bfd_link_hash_defined
3310 || h->root.type == bfd_link_hash_defweak
3311 || h->root.type == bfd_link_hash_common)
3312 && (h->flags & XCOFF_ENTRY) == 0
3313 && (h->flags & XCOFF_EXPORT) == 0)
3314 return TRUE;
3316 /* We need to add this symbol to the .loader symbols. */
3318 BFD_ASSERT (h->ldsym == NULL);
3319 amt = sizeof (struct internal_ldsym);
3320 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3321 if (h->ldsym == NULL)
3323 ldinfo->failed = TRUE;
3324 return FALSE;
3327 if ((h->flags & XCOFF_IMPORT) != 0)
3329 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3330 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3331 h->smclas = XMC_DS;
3332 h->ldsym->l_ifile = h->ldindx;
3335 /* The first 3 symbol table indices are reserved to indicate the
3336 data, text and bss sections. */
3337 h->ldindx = ldinfo->ldsym_count + 3;
3339 ++ldinfo->ldsym_count;
3341 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3342 h->ldsym, h->root.root.string))
3343 return FALSE;
3345 h->flags |= XCOFF_BUILT_LDSYM;
3346 return TRUE;
3349 /* An xcoff_htab_traverse callback that is called for each symbol
3350 once garbage collection is complete. */
3352 static bfd_boolean
3353 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3355 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3357 /* __rtinit, this symbol has special handling. */
3358 if (h->flags & XCOFF_RTINIT)
3359 return TRUE;
3361 /* We don't want to garbage collect symbols which are not defined in
3362 XCOFF files. This is a convenient place to mark them. */
3363 if (xcoff_hash_table (ldinfo->info)->gc
3364 && (h->flags & XCOFF_MARK) == 0
3365 && (h->root.type == bfd_link_hash_defined
3366 || h->root.type == bfd_link_hash_defweak)
3367 && (h->root.u.def.section->owner == NULL
3368 || (h->root.u.def.section->owner->xvec
3369 != ldinfo->info->output_bfd->xvec)))
3370 h->flags |= XCOFF_MARK;
3372 /* Skip discarded symbols. */
3373 if (xcoff_hash_table (ldinfo->info)->gc
3374 && (h->flags & XCOFF_MARK) == 0)
3375 return TRUE;
3377 /* If this is still a common symbol, and it wasn't garbage
3378 collected, we need to actually allocate space for it in the .bss
3379 section. */
3380 if (h->root.type == bfd_link_hash_common
3381 && h->root.u.c.p->section->size == 0)
3383 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3384 h->root.u.c.p->section->size = h->root.u.c.size;
3387 if (xcoff_hash_table (ldinfo->info)->loader_section)
3389 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3390 h->flags |= XCOFF_EXPORT;
3392 if (!xcoff_build_ldsym (ldinfo, h))
3393 return FALSE;
3396 return TRUE;
3399 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3400 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3401 csect information, if any. NAME is the function's name if the name
3402 is stored in the .debug section, otherwise it is null.
3404 Return 1 if we should include an appropriately-adjusted ISYM
3405 in the output file, 0 if we should discard ISYM, or -1 if an
3406 error occured. */
3408 static int
3409 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3410 struct internal_syment *isym,
3411 union internal_auxent *aux,
3412 struct xcoff_link_hash_entry *h,
3413 asection *csect, const char *name)
3415 int smtyp;
3417 /* If we are skipping this csect, we want to strip the symbol too. */
3418 if (csect == NULL)
3419 return 0;
3421 /* Likewise if we garbage-collected the csect. */
3422 if (xcoff_hash_table (info)->gc
3423 && !bfd_is_abs_section (csect)
3424 && !bfd_is_und_section (csect)
3425 && (csect->flags & SEC_MARK) == 0)
3426 return 0;
3428 /* An XCOFF linker always removes C_STAT symbols. */
3429 if (isym->n_sclass == C_STAT)
3430 return 0;
3432 /* We generate the TOC anchor separately. */
3433 if (isym->n_sclass == C_HIDEXT
3434 && aux->x_csect.x_smclas == XMC_TC0)
3435 return 0;
3437 /* If we are stripping all symbols, we want to discard this one. */
3438 if (info->strip == strip_all)
3439 return 0;
3441 /* Discard symbols that are defined elsewhere. */
3442 if (EXTERN_SYM_P (isym->n_sclass))
3444 if ((h->flags & XCOFF_ALLOCATED) != 0)
3445 return 0;
3446 if (!xcoff_final_definition_p (input_bfd, h, csect))
3447 return 0;
3450 /* If we're discarding local symbols, check whether ISYM is local. */
3451 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3452 if (info->discard == discard_all
3453 && !EXTERN_SYM_P (isym->n_sclass)
3454 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3455 return 0;
3457 /* If we're stripping debugging symbols, check whether ISYM is one. */
3458 if (info->strip == strip_debugger
3459 && isym->n_scnum == N_DEBUG)
3460 return 0;
3462 /* If we are stripping symbols based on name, check how ISYM's
3463 name should be handled. */
3464 if (info->strip == strip_some
3465 || info->discard == discard_l)
3467 char buf[SYMNMLEN + 1];
3469 if (name == NULL)
3471 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3472 if (name == NULL)
3473 return -1;
3476 if (info->strip == strip_some
3477 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3478 return 0;
3480 if (info->discard == discard_l
3481 && !EXTERN_SYM_P (isym->n_sclass)
3482 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3483 && bfd_is_local_label_name (input_bfd, name))
3484 return 0;
3487 return 1;
3490 /* Lay out the .loader section, filling in the header and the import paths.
3491 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3493 static bfd_boolean
3494 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3495 const char *libpath)
3497 bfd *output_bfd;
3498 struct xcoff_link_hash_table *htab;
3499 struct internal_ldhdr *ldhdr;
3500 struct xcoff_import_file *fl;
3501 bfd_size_type stoff;
3502 size_t impsize, impcount;
3503 asection *lsec;
3504 char *out;
3506 /* Work out the size of the import file names. Each import file ID
3507 consists of three null terminated strings: the path, the file
3508 name, and the archive member name. The first entry in the list
3509 of names is the path to use to find objects, which the linker has
3510 passed in as the libpath argument. For some reason, the path
3511 entry in the other import file names appears to always be empty. */
3512 output_bfd = ldinfo->output_bfd;
3513 htab = xcoff_hash_table (ldinfo->info);
3514 impsize = strlen (libpath) + 3;
3515 impcount = 1;
3516 for (fl = htab->imports; fl != NULL; fl = fl->next)
3518 ++impcount;
3519 impsize += (strlen (fl->path)
3520 + strlen (fl->file)
3521 + strlen (fl->member)
3522 + 3);
3525 /* Set up the .loader section header. */
3526 ldhdr = &htab->ldhdr;
3527 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3528 ldhdr->l_nsyms = ldinfo->ldsym_count;
3529 ldhdr->l_nreloc = htab->ldrel_count;
3530 ldhdr->l_istlen = impsize;
3531 ldhdr->l_nimpid = impcount;
3532 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3533 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3534 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3535 ldhdr->l_stlen = ldinfo->string_size;
3536 stoff = ldhdr->l_impoff + impsize;
3537 if (ldinfo->string_size == 0)
3538 ldhdr->l_stoff = 0;
3539 else
3540 ldhdr->l_stoff = stoff;
3542 /* 64 bit elements to ldhdr
3543 The swap out routine for 32 bit will ignore them.
3544 Nothing fancy, symbols come after the header and relocs come
3545 after symbols. */
3546 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3547 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3548 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3550 /* We now know the final size of the .loader section. Allocate
3551 space for it. */
3552 lsec = htab->loader_section;
3553 lsec->size = stoff + ldhdr->l_stlen;
3554 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3555 if (lsec->contents == NULL)
3556 return FALSE;
3558 /* Set up the header. */
3559 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3561 /* Set up the import file names. */
3562 out = (char *) lsec->contents + ldhdr->l_impoff;
3563 strcpy (out, libpath);
3564 out += strlen (libpath) + 1;
3565 *out++ = '\0';
3566 *out++ = '\0';
3567 for (fl = htab->imports; fl != NULL; fl = fl->next)
3569 const char *s;
3571 s = fl->path;
3572 while ((*out++ = *s++) != '\0')
3574 s = fl->file;
3575 while ((*out++ = *s++) != '\0')
3577 s = fl->member;
3578 while ((*out++ = *s++) != '\0')
3582 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3584 /* Set up the symbol string table. */
3585 if (ldinfo->string_size > 0)
3587 memcpy (out, ldinfo->strings, ldinfo->string_size);
3588 free (ldinfo->strings);
3589 ldinfo->strings = NULL;
3592 /* We can't set up the symbol table or the relocs yet, because we
3593 don't yet know the final position of the various sections. The
3594 .loader symbols are written out when the corresponding normal
3595 symbols are written out in xcoff_link_input_bfd or
3596 xcoff_write_global_symbol. The .loader relocs are written out
3597 when the corresponding normal relocs are handled in
3598 xcoff_link_input_bfd. */
3600 return TRUE;
3603 /* Build the .loader section. This is called by the XCOFF linker
3604 emulation before_allocation routine. We must set the size of the
3605 .loader section before the linker lays out the output file.
3606 LIBPATH is the library path to search for shared objects; this is
3607 normally built from the -L arguments passed to the linker. ENTRY
3608 is the name of the entry point symbol (the -e linker option).
3609 FILE_ALIGN is the alignment to use for sections within the file
3610 (the -H linker option). MAXSTACK is the maximum stack size (the
3611 -bmaxstack linker option). MAXDATA is the maximum data size (the
3612 -bmaxdata linker option). GC is whether to do garbage collection
3613 (the -bgc linker option). MODTYPE is the module type (the
3614 -bmodtype linker option). TEXTRO is whether the text section must
3615 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3616 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3617 is set by this routine to csects with magic names like _end. */
3619 bfd_boolean
3620 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3621 struct bfd_link_info *info,
3622 const char *libpath,
3623 const char *entry,
3624 unsigned long file_align,
3625 unsigned long maxstack,
3626 unsigned long maxdata,
3627 bfd_boolean gc,
3628 int modtype,
3629 bfd_boolean textro,
3630 unsigned int auto_export_flags,
3631 asection **special_sections,
3632 bfd_boolean rtld)
3634 struct xcoff_loader_info ldinfo;
3635 int i;
3636 asection *sec;
3637 bfd *sub;
3638 struct bfd_strtab_hash *debug_strtab;
3639 bfd_byte *debug_contents = NULL;
3640 bfd_size_type amt;
3642 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3644 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3645 special_sections[i] = NULL;
3646 return TRUE;
3649 ldinfo.failed = FALSE;
3650 ldinfo.output_bfd = output_bfd;
3651 ldinfo.info = info;
3652 ldinfo.auto_export_flags = auto_export_flags;
3653 ldinfo.ldsym_count = 0;
3654 ldinfo.string_size = 0;
3655 ldinfo.strings = NULL;
3656 ldinfo.string_alc = 0;
3658 xcoff_data (output_bfd)->maxstack = maxstack;
3659 xcoff_data (output_bfd)->maxdata = maxdata;
3660 xcoff_data (output_bfd)->modtype = modtype;
3662 xcoff_hash_table (info)->file_align = file_align;
3663 xcoff_hash_table (info)->textro = textro;
3664 xcoff_hash_table (info)->rtld = rtld;
3666 /* __rtinit */
3667 if (xcoff_hash_table (info)->loader_section
3668 && (info->init_function || info->fini_function || rtld))
3670 struct xcoff_link_hash_entry *hsym;
3671 struct internal_ldsym *ldsym;
3673 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3674 "__rtinit", FALSE, FALSE, TRUE);
3675 if (hsym == NULL)
3677 (*_bfd_error_handler)
3678 (_("error: undefined symbol __rtinit"));
3679 return FALSE;
3682 xcoff_mark_symbol (info, hsym);
3683 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3685 /* __rtinit initialized. */
3686 amt = sizeof (* ldsym);
3687 ldsym = bfd_malloc (amt);
3689 ldsym->l_value = 0; /* Will be filled in later. */
3690 ldsym->l_scnum = 2; /* Data section. */
3691 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3692 ldsym->l_smclas = 5; /* .rw. */
3693 ldsym->l_ifile = 0; /* Special system loader symbol. */
3694 ldsym->l_parm = 0; /* NA. */
3696 /* Force __rtinit to be the first symbol in the loader symbol table
3697 See xcoff_build_ldsyms
3699 The first 3 symbol table indices are reserved to indicate the data,
3700 text and bss sections. */
3701 BFD_ASSERT (0 == ldinfo.ldsym_count);
3703 hsym->ldindx = 3;
3704 ldinfo.ldsym_count = 1;
3705 hsym->ldsym = ldsym;
3707 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3708 hsym->ldsym, hsym->root.root.string))
3709 return FALSE;
3711 /* This symbol is written out by xcoff_write_global_symbol
3712 Set stuff up so xcoff_write_global_symbol logic works. */
3713 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3714 hsym->root.type = bfd_link_hash_defined;
3715 hsym->root.u.def.value = 0;
3718 /* Garbage collect unused sections. */
3719 if (info->relocatable || !gc)
3721 gc = FALSE;
3722 xcoff_hash_table (info)->gc = FALSE;
3724 /* We still need to call xcoff_mark, in order to set ldrel_count
3725 correctly. */
3726 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3728 asection *o;
3730 for (o = sub->sections; o != NULL; o = o->next)
3732 /* We shouldn't unconditionaly mark the TOC section.
3733 The output file should only have a TOC if either
3734 (a) one of the input files did or (b) we end up
3735 creating TOC references as part of the link process. */
3736 if (o != xcoff_hash_table (info)->toc_section
3737 && (o->flags & SEC_MARK) == 0)
3739 if (! xcoff_mark (info, o))
3740 goto error_return;
3745 else
3747 if (entry != NULL
3748 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3749 goto error_return;
3750 if (info->init_function != NULL
3751 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3752 goto error_return;
3753 if (info->fini_function != NULL
3754 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3755 goto error_return;
3756 if (auto_export_flags != 0)
3758 xcoff_link_hash_traverse (xcoff_hash_table (info),
3759 xcoff_mark_auto_exports, &ldinfo);
3760 if (ldinfo.failed)
3761 goto error_return;
3763 xcoff_sweep (info);
3764 xcoff_hash_table (info)->gc = TRUE;
3767 /* Return special sections to the caller. */
3768 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3770 sec = xcoff_hash_table (info)->special_sections[i];
3772 if (sec != NULL
3773 && gc
3774 && (sec->flags & SEC_MARK) == 0)
3775 sec = NULL;
3777 special_sections[i] = sec;
3780 if (info->input_bfds == NULL)
3781 /* I'm not sure what to do in this bizarre case. */
3782 return TRUE;
3784 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3785 (void *) &ldinfo);
3786 if (ldinfo.failed)
3787 goto error_return;
3789 if (xcoff_hash_table (info)->loader_section
3790 && !xcoff_build_loader_section (&ldinfo, libpath))
3791 goto error_return;
3793 /* Allocate space for the magic sections. */
3794 sec = xcoff_hash_table (info)->linkage_section;
3795 if (sec->size > 0)
3797 sec->contents = bfd_zalloc (output_bfd, sec->size);
3798 if (sec->contents == NULL)
3799 goto error_return;
3801 sec = xcoff_hash_table (info)->toc_section;
3802 if (sec->size > 0)
3804 sec->contents = bfd_zalloc (output_bfd, sec->size);
3805 if (sec->contents == NULL)
3806 goto error_return;
3808 sec = xcoff_hash_table (info)->descriptor_section;
3809 if (sec->size > 0)
3811 sec->contents = bfd_zalloc (output_bfd, sec->size);
3812 if (sec->contents == NULL)
3813 goto error_return;
3816 /* Now that we've done garbage collection, decide which symbols to keep,
3817 and figure out the contents of the .debug section. */
3818 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3820 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3822 asection *subdeb;
3823 bfd_size_type symcount;
3824 long *debug_index;
3825 asection **csectpp;
3826 unsigned int *lineno_counts;
3827 struct xcoff_link_hash_entry **sym_hash;
3828 bfd_byte *esym, *esymend;
3829 bfd_size_type symesz;
3831 if (sub->xvec != info->output_bfd->xvec)
3832 continue;
3834 if ((sub->flags & DYNAMIC) != 0
3835 && !info->static_link)
3836 continue;
3838 if (! _bfd_coff_get_external_symbols (sub))
3839 goto error_return;
3841 symcount = obj_raw_syment_count (sub);
3842 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3843 if (debug_index == NULL)
3844 goto error_return;
3845 xcoff_data (sub)->debug_indices = debug_index;
3847 if (info->strip == strip_all
3848 || info->strip == strip_debugger
3849 || info->discard == discard_all)
3850 /* We're stripping all debugging information, so there's no need
3851 to read SUB's .debug section. */
3852 subdeb = NULL;
3853 else
3855 /* Grab the contents of SUB's .debug section, if any. */
3856 subdeb = bfd_get_section_by_name (sub, ".debug");
3857 if (subdeb != NULL && subdeb->size > 0)
3859 /* We use malloc and copy the names into the debug
3860 stringtab, rather than bfd_alloc, because I expect
3861 that, when linking many files together, many of the
3862 strings will be the same. Storing the strings in the
3863 hash table should save space in this case. */
3864 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3865 goto error_return;
3869 csectpp = xcoff_data (sub)->csects;
3870 lineno_counts = xcoff_data (sub)->lineno_counts;
3871 sym_hash = obj_xcoff_sym_hashes (sub);
3872 symesz = bfd_coff_symesz (sub);
3873 esym = (bfd_byte *) obj_coff_external_syms (sub);
3874 esymend = esym + symcount * symesz;
3876 while (esym < esymend)
3878 struct internal_syment sym;
3879 union internal_auxent aux;
3880 asection *csect;
3881 const char *name;
3882 int keep_p;
3884 bfd_coff_swap_sym_in (sub, esym, &sym);
3886 /* Read in the csect information, if any. */
3887 if (CSECT_SYM_P (sym.n_sclass))
3889 BFD_ASSERT (sym.n_numaux > 0);
3890 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3891 sym.n_type, sym.n_sclass,
3892 sym.n_numaux - 1, sym.n_numaux, &aux);
3895 /* If this symbol's name is stored in the debug section,
3896 get a pointer to it. */
3897 if (debug_contents != NULL
3898 && sym._n._n_n._n_zeroes == 0
3899 && bfd_coff_symname_in_debug (sub, &sym))
3900 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3901 else
3902 name = NULL;
3904 /* Decide whether to copy this symbol to the output file. */
3905 csect = *csectpp;
3906 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3907 *sym_hash, csect, name);
3908 if (keep_p < 0)
3909 return FALSE;
3911 if (!keep_p)
3912 /* Use a debug_index of -2 to record that a symbol should
3913 be stripped. */
3914 *debug_index = -2;
3915 else
3917 /* See whether we should store the symbol name in the
3918 output .debug section. */
3919 if (name != NULL)
3921 bfd_size_type indx;
3923 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3924 if (indx == (bfd_size_type) -1)
3925 goto error_return;
3926 *debug_index = indx;
3928 else
3929 *debug_index = -1;
3930 if (*sym_hash != 0)
3931 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3932 if (*lineno_counts > 0)
3933 csect->output_section->lineno_count += *lineno_counts;
3936 esym += (sym.n_numaux + 1) * symesz;
3937 csectpp += sym.n_numaux + 1;
3938 sym_hash += sym.n_numaux + 1;
3939 lineno_counts += sym.n_numaux + 1;
3940 debug_index += sym.n_numaux + 1;
3943 if (debug_contents)
3945 free (debug_contents);
3946 debug_contents = NULL;
3948 /* Clear the size of subdeb, so that it is not included directly
3949 in the output file. */
3950 subdeb->size = 0;
3953 if (! info->keep_memory)
3955 if (! _bfd_coff_free_symbols (sub))
3956 goto error_return;
3960 if (info->strip != strip_all)
3961 xcoff_hash_table (info)->debug_section->size =
3962 _bfd_stringtab_size (debug_strtab);
3964 return TRUE;
3966 error_return:
3967 if (ldinfo.strings != NULL)
3968 free (ldinfo.strings);
3969 if (debug_contents != NULL)
3970 free (debug_contents);
3971 return FALSE;
3974 bfd_boolean
3975 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3976 const char *init,
3977 const char *fini,
3978 bfd_boolean rtld)
3980 struct bfd_in_memory *bim;
3982 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3983 if (bim == NULL)
3984 return FALSE;
3986 bim->size = 0;
3987 bim->buffer = 0;
3989 abfd->link_next = 0;
3990 abfd->format = bfd_object;
3991 abfd->iostream = (void *) bim;
3992 abfd->flags = BFD_IN_MEMORY;
3993 abfd->iovec = &_bfd_memory_iovec;
3994 abfd->direction = write_direction;
3995 abfd->origin = 0;
3996 abfd->where = 0;
3998 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3999 return FALSE;
4001 /* need to reset to unknown or it will not be read back in correctly */
4002 abfd->format = bfd_unknown;
4003 abfd->direction = read_direction;
4004 abfd->where = 0;
4006 return TRUE;
4009 /* Return the section that defines H. Return null if no section does. */
4011 static asection *
4012 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4014 switch (h->root.type)
4016 case bfd_link_hash_defined:
4017 case bfd_link_hash_defweak:
4018 return h->root.u.def.section;
4020 case bfd_link_hash_common:
4021 return h->root.u.c.p->section;
4023 default:
4024 return NULL;
4028 /* Add a .loader relocation for input relocation IREL. If the loader
4029 relocation should be against an output section, HSEC points to the
4030 input section that IREL is against, otherwise HSEC is null. H is the
4031 symbol that IREL is against, or null if it isn't against a global symbol.
4032 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4034 static bfd_boolean
4035 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4036 asection *output_section, bfd *reference_bfd,
4037 struct internal_reloc *irel, asection *hsec,
4038 struct xcoff_link_hash_entry *h)
4040 struct internal_ldrel ldrel;
4042 ldrel.l_vaddr = irel->r_vaddr;
4043 if (hsec != NULL)
4045 const char *secname;
4047 secname = hsec->output_section->name;
4048 if (strcmp (secname, ".text") == 0)
4049 ldrel.l_symndx = 0;
4050 else if (strcmp (secname, ".data") == 0)
4051 ldrel.l_symndx = 1;
4052 else if (strcmp (secname, ".bss") == 0)
4053 ldrel.l_symndx = 2;
4054 else
4056 (*_bfd_error_handler)
4057 (_("%B: loader reloc in unrecognized section `%s'"),
4058 reference_bfd, secname);
4059 bfd_set_error (bfd_error_nonrepresentable_section);
4060 return FALSE;
4063 else if (h != NULL)
4065 if (h->ldindx < 0)
4067 (*_bfd_error_handler)
4068 (_("%B: `%s' in loader reloc but not loader sym"),
4069 reference_bfd, h->root.root.string);
4070 bfd_set_error (bfd_error_bad_value);
4071 return FALSE;
4073 ldrel.l_symndx = h->ldindx;
4075 else
4076 ldrel.l_symndx = -(bfd_size_type) 1;
4078 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4079 ldrel.l_rsecnm = output_section->target_index;
4080 if (xcoff_hash_table (flinfo->info)->textro
4081 && strcmp (output_section->name, ".text") == 0)
4083 (*_bfd_error_handler)
4084 (_("%B: loader reloc in read-only section %A"),
4085 reference_bfd, output_section);
4086 bfd_set_error (bfd_error_invalid_operation);
4087 return FALSE;
4089 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4090 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4091 return TRUE;
4094 /* Link an input file into the linker output file. This function
4095 handles all the sections and relocations of the input file at once. */
4097 static bfd_boolean
4098 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4099 bfd *input_bfd)
4101 bfd *output_bfd;
4102 const char *strings;
4103 bfd_size_type syment_base;
4104 unsigned int n_tmask;
4105 unsigned int n_btshft;
4106 bfd_boolean copy, hash;
4107 bfd_size_type isymesz;
4108 bfd_size_type osymesz;
4109 bfd_size_type linesz;
4110 bfd_byte *esym;
4111 bfd_byte *esym_end;
4112 struct xcoff_link_hash_entry **sym_hash;
4113 struct internal_syment *isymp;
4114 asection **csectpp;
4115 unsigned int *lineno_counts;
4116 long *debug_index;
4117 long *indexp;
4118 unsigned long output_index;
4119 bfd_byte *outsym;
4120 unsigned int incls;
4121 asection *oline;
4122 bfd_boolean keep_syms;
4123 asection *o;
4125 /* We can just skip DYNAMIC files, unless this is a static link. */
4126 if ((input_bfd->flags & DYNAMIC) != 0
4127 && ! flinfo->info->static_link)
4128 return TRUE;
4130 /* Move all the symbols to the output file. */
4131 output_bfd = flinfo->output_bfd;
4132 strings = NULL;
4133 syment_base = obj_raw_syment_count (output_bfd);
4134 isymesz = bfd_coff_symesz (input_bfd);
4135 osymesz = bfd_coff_symesz (output_bfd);
4136 linesz = bfd_coff_linesz (input_bfd);
4137 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4139 n_tmask = coff_data (input_bfd)->local_n_tmask;
4140 n_btshft = coff_data (input_bfd)->local_n_btshft;
4142 /* Define macros so that ISFCN, et. al., macros work correctly. */
4143 #define N_TMASK n_tmask
4144 #define N_BTSHFT n_btshft
4146 copy = FALSE;
4147 if (! flinfo->info->keep_memory)
4148 copy = TRUE;
4149 hash = TRUE;
4150 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4151 hash = FALSE;
4153 if (! _bfd_coff_get_external_symbols (input_bfd))
4154 return FALSE;
4156 /* Make one pass over the symbols and assign indices to symbols that
4157 we have decided to keep. Also use create .loader symbol information
4158 and update information in hash table entries. */
4159 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4160 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4161 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4162 csectpp = xcoff_data (input_bfd)->csects;
4163 debug_index = xcoff_data (input_bfd)->debug_indices;
4164 isymp = flinfo->internal_syms;
4165 indexp = flinfo->sym_indices;
4166 output_index = syment_base;
4167 while (esym < esym_end)
4169 union internal_auxent aux;
4170 int smtyp = 0;
4171 int add;
4173 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4175 /* Read in the csect information, if any. */
4176 if (CSECT_SYM_P (isymp->n_sclass))
4178 BFD_ASSERT (isymp->n_numaux > 0);
4179 bfd_coff_swap_aux_in (input_bfd,
4180 (void *) (esym + isymesz * isymp->n_numaux),
4181 isymp->n_type, isymp->n_sclass,
4182 isymp->n_numaux - 1, isymp->n_numaux,
4183 (void *) &aux);
4185 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4188 /* If this symbol is in the .loader section, swap out the
4189 .loader symbol information. If this is an external symbol
4190 reference to a defined symbol, though, then wait until we get
4191 to the definition. */
4192 if (EXTERN_SYM_P (isymp->n_sclass)
4193 && *sym_hash != NULL
4194 && (*sym_hash)->ldsym != NULL
4195 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4197 struct xcoff_link_hash_entry *h;
4198 struct internal_ldsym *ldsym;
4200 h = *sym_hash;
4201 ldsym = h->ldsym;
4202 if (isymp->n_scnum > 0)
4204 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4205 ldsym->l_value = (isymp->n_value
4206 + (*csectpp)->output_section->vma
4207 + (*csectpp)->output_offset
4208 - (*csectpp)->vma);
4210 else
4212 ldsym->l_scnum = isymp->n_scnum;
4213 ldsym->l_value = isymp->n_value;
4216 ldsym->l_smtype = smtyp;
4217 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4218 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4219 || (h->flags & XCOFF_IMPORT) != 0)
4220 ldsym->l_smtype |= L_IMPORT;
4221 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4222 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4223 || (h->flags & XCOFF_EXPORT) != 0)
4224 ldsym->l_smtype |= L_EXPORT;
4225 if ((h->flags & XCOFF_ENTRY) != 0)
4226 ldsym->l_smtype |= L_ENTRY;
4227 if (isymp->n_sclass == C_AIX_WEAKEXT)
4228 ldsym->l_smtype |= L_WEAK;
4230 ldsym->l_smclas = aux.x_csect.x_smclas;
4232 if (ldsym->l_ifile == (bfd_size_type) -1)
4233 ldsym->l_ifile = 0;
4234 else if (ldsym->l_ifile == 0)
4236 if ((ldsym->l_smtype & L_IMPORT) == 0)
4237 ldsym->l_ifile = 0;
4238 else
4240 bfd *impbfd;
4242 if (h->root.type == bfd_link_hash_defined
4243 || h->root.type == bfd_link_hash_defweak)
4244 impbfd = h->root.u.def.section->owner;
4245 else if (h->root.type == bfd_link_hash_undefined
4246 || h->root.type == bfd_link_hash_undefweak)
4247 impbfd = h->root.u.undef.abfd;
4248 else
4249 impbfd = NULL;
4251 if (impbfd == NULL)
4252 ldsym->l_ifile = 0;
4253 else
4255 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4256 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4261 ldsym->l_parm = 0;
4263 BFD_ASSERT (h->ldindx >= 0);
4264 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4265 (flinfo->ldsym
4266 + ((h->ldindx - 3)
4267 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4268 h->ldsym = NULL;
4270 /* Fill in snentry now that we know the target_index. */
4271 if ((h->flags & XCOFF_ENTRY) != 0
4272 && (h->root.type == bfd_link_hash_defined
4273 || h->root.type == bfd_link_hash_defweak))
4275 xcoff_data (output_bfd)->snentry =
4276 h->root.u.def.section->output_section->target_index;
4280 add = 1 + isymp->n_numaux;
4282 if (*debug_index == -2)
4283 /* We've decided to strip this symbol. */
4284 *indexp = -1;
4285 else
4287 /* Assign the next unused index to this symbol. */
4288 *indexp = output_index;
4290 if (EXTERN_SYM_P (isymp->n_sclass))
4292 BFD_ASSERT (*sym_hash != NULL);
4293 (*sym_hash)->indx = output_index;
4296 /* If this is a symbol in the TOC which we may have merged
4297 (class XMC_TC), remember the symbol index of the TOC
4298 symbol. */
4299 if (isymp->n_sclass == C_HIDEXT
4300 && aux.x_csect.x_smclas == XMC_TC
4301 && *sym_hash != NULL)
4303 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4304 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4305 (*sym_hash)->u.toc_indx = output_index;
4308 output_index += add;
4311 esym += add * isymesz;
4312 isymp += add;
4313 csectpp += add;
4314 sym_hash += add;
4315 debug_index += add;
4316 ++indexp;
4317 for (--add; add > 0; --add)
4318 *indexp++ = -1;
4321 /* Now write out the symbols that we decided to keep. */
4323 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4324 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4325 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4326 isymp = flinfo->internal_syms;
4327 indexp = flinfo->sym_indices;
4328 csectpp = xcoff_data (input_bfd)->csects;
4329 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4330 debug_index = xcoff_data (input_bfd)->debug_indices;
4331 outsym = flinfo->outsyms;
4332 incls = 0;
4333 oline = NULL;
4334 while (esym < esym_end)
4336 int add;
4338 add = 1 + isymp->n_numaux;
4340 if (*indexp < 0)
4341 esym += add * isymesz;
4342 else
4344 struct internal_syment isym;
4345 int i;
4347 /* Adjust the symbol in order to output it. */
4348 isym = *isymp;
4349 if (isym._n._n_n._n_zeroes == 0
4350 && isym._n._n_n._n_offset != 0)
4352 /* This symbol has a long name. Enter it in the string
4353 table we are building. If *debug_index != -1, the
4354 name has already been entered in the .debug section. */
4355 if (*debug_index >= 0)
4356 isym._n._n_n._n_offset = *debug_index;
4357 else
4359 const char *name;
4360 bfd_size_type indx;
4362 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4364 if (name == NULL)
4365 return FALSE;
4366 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4367 if (indx == (bfd_size_type) -1)
4368 return FALSE;
4369 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4373 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4374 multiple definition problems when linking a shared object
4375 statically. (The native linker doesn't enter __rtinit into
4376 the normal table at all, but having a local symbol can make
4377 the objdump output easier to read.) */
4378 if (isym.n_sclass == C_EXT
4379 && *sym_hash
4380 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4381 isym.n_sclass = C_HIDEXT;
4383 /* The value of a C_FILE symbol is the symbol index of the
4384 next C_FILE symbol. The value of the last C_FILE symbol
4385 is -1. We try to get this right, below, just before we
4386 write the symbols out, but in the general case we may
4387 have to write the symbol out twice. */
4388 if (isym.n_sclass == C_FILE)
4390 if (flinfo->last_file_index != -1
4391 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4393 /* We must correct the value of the last C_FILE entry. */
4394 flinfo->last_file.n_value = *indexp;
4395 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4397 /* The last C_FILE symbol is in this input file. */
4398 bfd_coff_swap_sym_out (output_bfd,
4399 (void *) &flinfo->last_file,
4400 (void *) (flinfo->outsyms
4401 + ((flinfo->last_file_index
4402 - syment_base)
4403 * osymesz)));
4405 else
4407 /* We have already written out the last C_FILE
4408 symbol. We need to write it out again. We
4409 borrow *outsym temporarily. */
4410 file_ptr pos;
4412 bfd_coff_swap_sym_out (output_bfd,
4413 (void *) &flinfo->last_file,
4414 (void *) outsym);
4416 pos = obj_sym_filepos (output_bfd);
4417 pos += flinfo->last_file_index * osymesz;
4418 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4419 || (bfd_bwrite (outsym, osymesz, output_bfd)
4420 != osymesz))
4421 return FALSE;
4425 flinfo->last_file_index = *indexp;
4426 flinfo->last_file = isym;
4429 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4430 into the line numbers. We update the symbol values when
4431 we handle the line numbers. */
4432 if (isym.n_sclass == C_BINCL
4433 || isym.n_sclass == C_EINCL)
4435 isym.n_value = flinfo->line_filepos;
4436 ++incls;
4438 /* The value of a C_BSTAT symbol is the symbol table
4439 index of the containing csect. */
4440 else if (isym.n_sclass == C_BSTAT)
4442 bfd_vma indx;
4444 indx = isym.n_value;
4445 if (indx < obj_raw_syment_count (input_bfd))
4447 long symindx;
4449 symindx = flinfo->sym_indices[indx];
4450 if (symindx < 0)
4451 isym.n_value = 0;
4452 else
4453 isym.n_value = symindx;
4456 else if (isym.n_sclass != C_ESTAT
4457 && isym.n_sclass != C_DECL
4458 && isym.n_scnum > 0)
4460 isym.n_scnum = (*csectpp)->output_section->target_index;
4461 isym.n_value += ((*csectpp)->output_section->vma
4462 + (*csectpp)->output_offset
4463 - (*csectpp)->vma);
4466 /* Output the symbol. */
4467 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4469 esym += isymesz;
4470 outsym += osymesz;
4472 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4474 union internal_auxent aux;
4476 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4477 isymp->n_sclass, i, isymp->n_numaux,
4478 (void *) &aux);
4480 if (isymp->n_sclass == C_FILE)
4482 /* This is the file name (or some comment put in by
4483 the compiler). If it is long, we must put it in
4484 the string table. */
4485 if (aux.x_file.x_n.x_zeroes == 0
4486 && aux.x_file.x_n.x_offset != 0)
4488 const char *filename;
4489 bfd_size_type indx;
4491 BFD_ASSERT (aux.x_file.x_n.x_offset
4492 >= STRING_SIZE_SIZE);
4493 if (strings == NULL)
4495 strings = _bfd_coff_read_string_table (input_bfd);
4496 if (strings == NULL)
4497 return FALSE;
4499 filename = strings + aux.x_file.x_n.x_offset;
4500 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4501 hash, copy);
4502 if (indx == (bfd_size_type) -1)
4503 return FALSE;
4504 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4507 else if (CSECT_SYM_P (isymp->n_sclass)
4508 && i + 1 == isymp->n_numaux)
4511 /* We don't support type checking. I don't know if
4512 anybody does. */
4513 aux.x_csect.x_parmhash = 0;
4514 /* I don't think anybody uses these fields, but we'd
4515 better clobber them just in case. */
4516 aux.x_csect.x_stab = 0;
4517 aux.x_csect.x_snstab = 0;
4519 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4521 unsigned long indx;
4523 indx = aux.x_csect.x_scnlen.l;
4524 if (indx < obj_raw_syment_count (input_bfd))
4526 long symindx;
4528 symindx = flinfo->sym_indices[indx];
4529 if (symindx < 0)
4531 aux.x_csect.x_scnlen.l = 0;
4533 else
4535 aux.x_csect.x_scnlen.l = symindx;
4540 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4542 unsigned long indx;
4544 if (ISFCN (isymp->n_type)
4545 || ISTAG (isymp->n_sclass)
4546 || isymp->n_sclass == C_BLOCK
4547 || isymp->n_sclass == C_FCN)
4549 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4550 if (indx > 0
4551 && indx < obj_raw_syment_count (input_bfd))
4553 /* We look forward through the symbol for
4554 the index of the next symbol we are going
4555 to include. I don't know if this is
4556 entirely right. */
4557 while (flinfo->sym_indices[indx] < 0
4558 && indx < obj_raw_syment_count (input_bfd))
4559 ++indx;
4560 if (indx >= obj_raw_syment_count (input_bfd))
4561 indx = output_index;
4562 else
4563 indx = flinfo->sym_indices[indx];
4564 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4569 indx = aux.x_sym.x_tagndx.l;
4570 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4572 long symindx;
4574 symindx = flinfo->sym_indices[indx];
4575 if (symindx < 0)
4576 aux.x_sym.x_tagndx.l = 0;
4577 else
4578 aux.x_sym.x_tagndx.l = symindx;
4583 /* Copy over the line numbers, unless we are stripping
4584 them. We do this on a symbol by symbol basis in
4585 order to more easily handle garbage collection. */
4586 if (CSECT_SYM_P (isymp->n_sclass)
4587 && i == 0
4588 && isymp->n_numaux > 1
4589 && ISFCN (isymp->n_type)
4590 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4592 if (*lineno_counts == 0)
4593 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4594 else
4596 asection *enclosing;
4597 unsigned int enc_count;
4598 bfd_signed_vma linoff;
4599 struct internal_lineno lin;
4600 bfd_byte *linp;
4601 bfd_byte *linpend;
4602 bfd_vma offset;
4603 file_ptr pos;
4604 bfd_size_type amt;
4606 /* Read in the enclosing section's line-number
4607 information, if we haven't already. */
4608 o = *csectpp;
4609 enclosing = xcoff_section_data (abfd, o)->enclosing;
4610 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4611 if (oline != enclosing)
4613 pos = enclosing->line_filepos;
4614 amt = linesz * enc_count;
4615 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4616 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4617 != amt))
4618 return FALSE;
4619 oline = enclosing;
4622 /* Copy across the first entry, adjusting its
4623 symbol index. */
4624 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4625 - enclosing->line_filepos);
4626 linp = flinfo->linenos + linoff;
4627 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4628 lin.l_addr.l_symndx = *indexp;
4629 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4631 /* Copy the other entries, adjusting their addresses. */
4632 linpend = linp + *lineno_counts * linesz;
4633 offset = (o->output_section->vma
4634 + o->output_offset
4635 - o->vma);
4636 for (linp += linesz; linp < linpend; linp += linesz)
4638 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4639 lin.l_addr.l_paddr += offset;
4640 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4643 /* Write out the entries we've just processed. */
4644 pos = (o->output_section->line_filepos
4645 + o->output_section->lineno_count * linesz);
4646 amt = linesz * *lineno_counts;
4647 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4648 || bfd_bwrite (flinfo->linenos + linoff,
4649 amt, output_bfd) != amt)
4650 return FALSE;
4651 o->output_section->lineno_count += *lineno_counts;
4653 /* Record the offset of the symbol's line numbers
4654 in the output file. */
4655 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4657 if (incls > 0)
4659 struct internal_syment *iisp, *iispend;
4660 long *iindp;
4661 bfd_byte *oos;
4662 bfd_vma range_start, range_end;
4663 int iiadd;
4665 /* Update any C_BINCL or C_EINCL symbols
4666 that refer to a line number in the
4667 range we just output. */
4668 iisp = flinfo->internal_syms;
4669 iispend = iisp + obj_raw_syment_count (input_bfd);
4670 iindp = flinfo->sym_indices;
4671 oos = flinfo->outsyms;
4672 range_start = enclosing->line_filepos + linoff;
4673 range_end = range_start + *lineno_counts * linesz;
4674 while (iisp < iispend)
4676 if (*iindp >= 0
4677 && (iisp->n_sclass == C_BINCL
4678 || iisp->n_sclass == C_EINCL)
4679 && iisp->n_value >= range_start
4680 && iisp->n_value < range_end)
4682 struct internal_syment iis;
4684 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4685 iis.n_value = (iisp->n_value
4686 - range_start
4687 + pos);
4688 bfd_coff_swap_sym_out (output_bfd,
4689 &iis, oos);
4690 --incls;
4693 iiadd = 1 + iisp->n_numaux;
4694 if (*iindp >= 0)
4695 oos += iiadd * osymesz;
4696 iisp += iiadd;
4697 iindp += iiadd;
4703 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4704 isymp->n_sclass, i, isymp->n_numaux,
4705 (void *) outsym);
4706 outsym += osymesz;
4707 esym += isymesz;
4711 sym_hash += add;
4712 indexp += add;
4713 isymp += add;
4714 csectpp += add;
4715 lineno_counts += add;
4716 debug_index += add;
4719 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4720 symbol will be the first symbol in the next input file. In the
4721 normal case, this will save us from writing out the C_FILE symbol
4722 again. */
4723 if (flinfo->last_file_index != -1
4724 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4726 flinfo->last_file.n_value = output_index;
4727 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4728 (void *) (flinfo->outsyms
4729 + ((flinfo->last_file_index - syment_base)
4730 * osymesz)));
4733 /* Write the modified symbols to the output file. */
4734 if (outsym > flinfo->outsyms)
4736 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4737 bfd_size_type amt = outsym - flinfo->outsyms;
4738 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4739 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4740 return FALSE;
4742 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4743 + (outsym - flinfo->outsyms) / osymesz)
4744 == output_index);
4746 obj_raw_syment_count (output_bfd) = output_index;
4749 /* Don't let the linker relocation routines discard the symbols. */
4750 keep_syms = obj_coff_keep_syms (input_bfd);
4751 obj_coff_keep_syms (input_bfd) = TRUE;
4753 /* Relocate the contents of each section. */
4754 for (o = input_bfd->sections; o != NULL; o = o->next)
4756 bfd_byte *contents;
4758 if (! o->linker_mark)
4759 /* This section was omitted from the link. */
4760 continue;
4762 if ((o->flags & SEC_HAS_CONTENTS) == 0
4763 || o->size == 0
4764 || (o->flags & SEC_IN_MEMORY) != 0)
4765 continue;
4767 /* We have set filepos correctly for the sections we created to
4768 represent csects, so bfd_get_section_contents should work. */
4769 if (coff_section_data (input_bfd, o) != NULL
4770 && coff_section_data (input_bfd, o)->contents != NULL)
4771 contents = coff_section_data (input_bfd, o)->contents;
4772 else
4774 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4775 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4776 return FALSE;
4777 contents = flinfo->contents;
4780 if ((o->flags & SEC_RELOC) != 0)
4782 int target_index;
4783 struct internal_reloc *internal_relocs;
4784 struct internal_reloc *irel;
4785 bfd_vma offset;
4786 struct internal_reloc *irelend;
4787 struct xcoff_link_hash_entry **rel_hash;
4788 long r_symndx;
4790 /* Read in the relocs. */
4791 target_index = o->output_section->target_index;
4792 internal_relocs = (xcoff_read_internal_relocs
4793 (input_bfd, o, FALSE, flinfo->external_relocs,
4794 TRUE,
4795 (flinfo->section_info[target_index].relocs
4796 + o->output_section->reloc_count)));
4797 if (internal_relocs == NULL)
4798 return FALSE;
4800 /* Call processor specific code to relocate the section
4801 contents. */
4802 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4803 input_bfd, o,
4804 contents,
4805 internal_relocs,
4806 flinfo->internal_syms,
4807 xcoff_data (input_bfd)->csects))
4808 return FALSE;
4810 offset = o->output_section->vma + o->output_offset - o->vma;
4811 irel = internal_relocs;
4812 irelend = irel + o->reloc_count;
4813 rel_hash = (flinfo->section_info[target_index].rel_hashes
4814 + o->output_section->reloc_count);
4815 for (; irel < irelend; irel++, rel_hash++)
4817 struct xcoff_link_hash_entry *h = NULL;
4819 *rel_hash = NULL;
4821 /* Adjust the reloc address and symbol index. */
4823 irel->r_vaddr += offset;
4825 r_symndx = irel->r_symndx;
4827 if (r_symndx == -1)
4828 h = NULL;
4829 else
4830 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4832 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4834 if (h != NULL
4835 && h->smclas != XMC_TD
4836 && (irel->r_type == R_TOC
4837 || irel->r_type == R_GL
4838 || irel->r_type == R_TCL
4839 || irel->r_type == R_TRL
4840 || irel->r_type == R_TRLA))
4842 /* This is a TOC relative reloc with a symbol
4843 attached. The symbol should be the one which
4844 this reloc is for. We want to make this
4845 reloc against the TOC address of the symbol,
4846 not the symbol itself. */
4847 BFD_ASSERT (h->toc_section != NULL);
4848 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4849 if (h->u.toc_indx != -1)
4850 irel->r_symndx = h->u.toc_indx;
4851 else
4853 struct xcoff_toc_rel_hash *n;
4854 struct xcoff_link_section_info *si;
4855 bfd_size_type amt;
4857 amt = sizeof (* n);
4858 n = bfd_alloc (flinfo->output_bfd, amt);
4859 if (n == NULL)
4860 return FALSE;
4861 si = flinfo->section_info + target_index;
4862 n->next = si->toc_rel_hashes;
4863 n->h = h;
4864 n->rel = irel;
4865 si->toc_rel_hashes = n;
4868 else if (h != NULL)
4870 /* This is a global symbol. */
4871 if (h->indx >= 0)
4872 irel->r_symndx = h->indx;
4873 else
4875 /* This symbol is being written at the end
4876 of the file, and we do not yet know the
4877 symbol index. We save the pointer to the
4878 hash table entry in the rel_hash list.
4879 We set the indx field to -2 to indicate
4880 that this symbol must not be stripped. */
4881 *rel_hash = h;
4882 h->indx = -2;
4885 else
4887 long indx;
4889 indx = flinfo->sym_indices[r_symndx];
4891 if (indx == -1)
4893 struct internal_syment *is;
4895 /* Relocations against a TC0 TOC anchor are
4896 automatically transformed to be against
4897 the TOC anchor in the output file. */
4898 is = flinfo->internal_syms + r_symndx;
4899 if (is->n_sclass == C_HIDEXT
4900 && is->n_numaux > 0)
4902 void * auxptr;
4903 union internal_auxent aux;
4905 auxptr = ((void *)
4906 (((bfd_byte *)
4907 obj_coff_external_syms (input_bfd))
4908 + ((r_symndx + is->n_numaux)
4909 * isymesz)));
4910 bfd_coff_swap_aux_in (input_bfd, auxptr,
4911 is->n_type, is->n_sclass,
4912 is->n_numaux - 1,
4913 is->n_numaux,
4914 (void *) &aux);
4915 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4916 && aux.x_csect.x_smclas == XMC_TC0)
4917 indx = flinfo->toc_symindx;
4921 if (indx != -1)
4922 irel->r_symndx = indx;
4923 else
4926 struct internal_syment *is;
4928 const char *name;
4929 char buf[SYMNMLEN + 1];
4931 /* This reloc is against a symbol we are
4932 stripping. It would be possible to handle
4933 this case, but I don't think it's worth it. */
4934 is = flinfo->internal_syms + r_symndx;
4936 if (is->n_sclass != C_DWARF)
4938 name = (_bfd_coff_internal_syment_name
4939 (input_bfd, is, buf));
4941 if (name == NULL)
4942 return FALSE;
4944 if (!(*flinfo->info->callbacks->unattached_reloc)
4945 (flinfo->info, name, input_bfd, o,
4946 irel->r_vaddr))
4947 return FALSE;
4953 if ((o->flags & SEC_DEBUGGING) == 0
4954 && xcoff_need_ldrel_p (flinfo->info, irel, h))
4956 asection *sec;
4958 if (r_symndx == -1)
4959 sec = NULL;
4960 else if (h == NULL)
4961 sec = xcoff_data (input_bfd)->csects[r_symndx];
4962 else
4963 sec = xcoff_symbol_section (h);
4964 if (!xcoff_create_ldrel (output_bfd, flinfo,
4965 o->output_section, input_bfd,
4966 irel, sec, h))
4967 return FALSE;
4971 o->output_section->reloc_count += o->reloc_count;
4974 /* Write out the modified section contents. */
4975 if (! bfd_set_section_contents (output_bfd, o->output_section,
4976 contents, (file_ptr) o->output_offset,
4977 o->size))
4978 return FALSE;
4981 obj_coff_keep_syms (input_bfd) = keep_syms;
4983 if (! flinfo->info->keep_memory)
4985 if (! _bfd_coff_free_symbols (input_bfd))
4986 return FALSE;
4989 return TRUE;
4992 #undef N_TMASK
4993 #undef N_BTSHFT
4995 /* Sort relocs by VMA. This is called via qsort. */
4997 static int
4998 xcoff_sort_relocs (const void * p1, const void * p2)
5000 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5001 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5003 if (r1->r_vaddr > r2->r_vaddr)
5004 return 1;
5005 else if (r1->r_vaddr < r2->r_vaddr)
5006 return -1;
5007 else
5008 return 0;
5011 /* Return true if section SEC is a TOC section. */
5013 static inline bfd_boolean
5014 xcoff_toc_section_p (asection *sec)
5016 const char *name;
5018 name = sec->name;
5019 if (name[0] == '.' && name[1] == 't')
5021 if (name[2] == 'c')
5023 if (name[3] == '0' && name[4] == 0)
5024 return TRUE;
5025 if (name[3] == 0)
5026 return TRUE;
5028 if (name[2] == 'd' && name[3] == 0)
5029 return TRUE;
5031 return FALSE;
5034 /* See if the link requires a TOC (it usually does!). If so, find a
5035 good place to put the TOC anchor csect, and write out the associated
5036 symbol. */
5038 static bfd_boolean
5039 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5041 bfd_vma toc_start, toc_end, start, end, best_address;
5042 asection *sec;
5043 bfd *input_bfd;
5044 int section_index;
5045 struct internal_syment irsym;
5046 union internal_auxent iraux;
5047 file_ptr pos;
5048 size_t size;
5050 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5051 index of a csect at the beginning of the TOC. */
5052 toc_start = ~(bfd_vma) 0;
5053 toc_end = 0;
5054 section_index = -1;
5055 for (input_bfd = flinfo->info->input_bfds;
5056 input_bfd != NULL;
5057 input_bfd = input_bfd->link_next)
5058 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5059 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5061 start = sec->output_section->vma + sec->output_offset;
5062 if (toc_start > start)
5064 toc_start = start;
5065 section_index = sec->output_section->target_index;
5068 end = start + sec->size;
5069 if (toc_end < end)
5070 toc_end = end;
5073 /* There's no need for a TC0 symbol if we don't have a TOC. */
5074 if (toc_end < toc_start)
5076 xcoff_data (output_bfd)->toc = toc_start;
5077 return TRUE;
5080 if (toc_end - toc_start < 0x8000)
5081 /* Every TOC csect can be accessed from TOC_START. */
5082 best_address = toc_start;
5083 else
5085 /* Find the lowest TOC csect that is still within range of TOC_END. */
5086 best_address = toc_end;
5087 for (input_bfd = flinfo->info->input_bfds;
5088 input_bfd != NULL;
5089 input_bfd = input_bfd->link_next)
5090 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5091 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5093 start = sec->output_section->vma + sec->output_offset;
5094 if (start < best_address
5095 && start + 0x8000 >= toc_end)
5097 best_address = start;
5098 section_index = sec->output_section->target_index;
5102 /* Make sure that the start of the TOC is also within range. */
5103 if (best_address > toc_start + 0x8000)
5105 (*_bfd_error_handler)
5106 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5107 "when compiling"),
5108 (unsigned long) (toc_end - toc_start));
5109 bfd_set_error (bfd_error_file_too_big);
5110 return FALSE;
5114 /* Record the chosen TOC value. */
5115 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5116 xcoff_data (output_bfd)->toc = best_address;
5117 xcoff_data (output_bfd)->sntoc = section_index;
5119 /* Fill out the TC0 symbol. */
5120 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &irsym, "TOC"))
5121 return FALSE;
5122 irsym.n_value = best_address;
5123 irsym.n_scnum = section_index;
5124 irsym.n_sclass = C_HIDEXT;
5125 irsym.n_type = T_NULL;
5126 irsym.n_numaux = 1;
5127 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5129 /* Fill out the auxillary csect information. */
5130 memset (&iraux, 0, sizeof iraux);
5131 iraux.x_csect.x_smtyp = XTY_SD;
5132 iraux.x_csect.x_smclas = XMC_TC0;
5133 iraux.x_csect.x_scnlen.l = 0;
5134 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5135 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5137 /* Write the contents to the file. */
5138 pos = obj_sym_filepos (output_bfd);
5139 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5140 size = 2 * bfd_coff_symesz (output_bfd);
5141 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5142 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5143 return FALSE;
5144 obj_raw_syment_count (output_bfd) += 2;
5146 return TRUE;
5149 /* Write out a non-XCOFF global symbol. */
5151 static bfd_boolean
5152 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
5154 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
5155 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5156 bfd *output_bfd;
5157 bfd_byte *outsym;
5158 struct internal_syment isym;
5159 union internal_auxent aux;
5160 bfd_boolean result;
5161 file_ptr pos;
5162 bfd_size_type amt;
5164 output_bfd = flinfo->output_bfd;
5165 outsym = flinfo->outsyms;
5167 if (h->root.type == bfd_link_hash_warning)
5169 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5170 if (h->root.type == bfd_link_hash_new)
5171 return TRUE;
5174 /* If this symbol was garbage collected, just skip it. */
5175 if (xcoff_hash_table (flinfo->info)->gc
5176 && (h->flags & XCOFF_MARK) == 0)
5177 return TRUE;
5179 /* If we need a .loader section entry, write it out. */
5180 if (h->ldsym != NULL)
5182 struct internal_ldsym *ldsym;
5183 bfd *impbfd;
5185 ldsym = h->ldsym;
5187 if (h->root.type == bfd_link_hash_undefined
5188 || h->root.type == bfd_link_hash_undefweak)
5191 ldsym->l_value = 0;
5192 ldsym->l_scnum = N_UNDEF;
5193 ldsym->l_smtype = XTY_ER;
5194 impbfd = h->root.u.undef.abfd;
5197 else if (h->root.type == bfd_link_hash_defined
5198 || h->root.type == bfd_link_hash_defweak)
5200 asection *sec;
5202 sec = h->root.u.def.section;
5203 ldsym->l_value = (sec->output_section->vma
5204 + sec->output_offset
5205 + h->root.u.def.value);
5206 ldsym->l_scnum = sec->output_section->target_index;
5207 ldsym->l_smtype = XTY_SD;
5208 impbfd = sec->owner;
5211 else
5212 abort ();
5214 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5215 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5216 || (h->flags & XCOFF_IMPORT) != 0)
5217 /* Clear l_smtype
5218 Import symbols are defined so the check above will make
5219 the l_smtype XTY_SD. But this is not correct, it should
5220 be cleared. */
5221 ldsym->l_smtype |= L_IMPORT;
5223 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5224 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5225 || (h->flags & XCOFF_EXPORT) != 0)
5226 ldsym->l_smtype |= L_EXPORT;
5228 if ((h->flags & XCOFF_ENTRY) != 0)
5229 ldsym->l_smtype |= L_ENTRY;
5231 if ((h->flags & XCOFF_RTINIT) != 0)
5232 ldsym->l_smtype = XTY_SD;
5234 ldsym->l_smclas = h->smclas;
5236 if (ldsym->l_smtype & L_IMPORT)
5238 if ((h->root.type == bfd_link_hash_defined
5239 || h->root.type == bfd_link_hash_defweak)
5240 && (h->root.u.def.value != 0))
5241 ldsym->l_smclas = XMC_XO;
5243 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5244 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5245 ldsym->l_smclas = XMC_SV3264;
5247 else if (h->flags & XCOFF_SYSCALL32)
5248 ldsym->l_smclas = XMC_SV;
5250 else if (h->flags & XCOFF_SYSCALL64)
5251 ldsym->l_smclas = XMC_SV64;
5254 if (ldsym->l_ifile == -(bfd_size_type) 1)
5256 ldsym->l_ifile = 0;
5258 else if (ldsym->l_ifile == 0)
5260 if ((ldsym->l_smtype & L_IMPORT) == 0)
5261 ldsym->l_ifile = 0;
5262 else if (impbfd == NULL)
5263 ldsym->l_ifile = 0;
5264 else
5266 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5267 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5271 ldsym->l_parm = 0;
5273 BFD_ASSERT (h->ldindx >= 0);
5275 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5276 (flinfo->ldsym +
5277 (h->ldindx - 3)
5278 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5279 h->ldsym = NULL;
5282 /* If this symbol needs global linkage code, write it out. */
5283 if (h->root.type == bfd_link_hash_defined
5284 && (h->root.u.def.section
5285 == xcoff_hash_table (flinfo->info)->linkage_section))
5287 bfd_byte *p;
5288 bfd_vma tocoff;
5289 unsigned int i;
5291 p = h->root.u.def.section->contents + h->root.u.def.value;
5293 /* The first instruction in the global linkage code loads a
5294 specific TOC element. */
5295 tocoff = (h->descriptor->toc_section->output_section->vma
5296 + h->descriptor->toc_section->output_offset
5297 - xcoff_data (output_bfd)->toc);
5299 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5300 tocoff += h->descriptor->u.toc_offset;
5302 /* The first instruction in the glink code needs to be
5303 cooked to to hold the correct offset in the toc. The
5304 rest are just output raw. */
5305 bfd_put_32 (output_bfd,
5306 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5308 /* Start with i == 1 to get past the first instruction done above
5309 The /4 is because the glink code is in bytes and we are going
5310 4 at a pop. */
5311 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5312 bfd_put_32 (output_bfd,
5313 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5314 &p[4 * i]);
5317 /* If we created a TOC entry for this symbol, write out the required
5318 relocs. */
5319 if ((h->flags & XCOFF_SET_TOC) != 0)
5321 asection *tocsec;
5322 asection *osec;
5323 int oindx;
5324 struct internal_reloc *irel;
5325 struct internal_syment irsym;
5326 union internal_auxent iraux;
5328 tocsec = h->toc_section;
5329 osec = tocsec->output_section;
5330 oindx = osec->target_index;
5331 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5332 irel->r_vaddr = (osec->vma
5333 + tocsec->output_offset
5334 + h->u.toc_offset);
5336 if (h->indx >= 0)
5337 irel->r_symndx = h->indx;
5338 else
5340 h->indx = -2;
5341 irel->r_symndx = obj_raw_syment_count (output_bfd);
5344 BFD_ASSERT (h->ldindx >= 0);
5346 /* Initialize the aux union here instead of closer to when it is
5347 written out below because the length of the csect depends on
5348 whether the output is 32 or 64 bit. */
5349 memset (&iraux, 0, sizeof iraux);
5350 iraux.x_csect.x_smtyp = XTY_SD;
5351 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5352 iraux.x_csect.x_smclas = XMC_TC;
5354 /* 32 bit uses a 32 bit R_POS to do the relocations
5355 64 bit uses a 64 bit R_POS to do the relocations
5357 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5359 Which one is determined by the backend. */
5360 if (bfd_xcoff_is_xcoff64 (output_bfd))
5362 irel->r_size = 63;
5363 iraux.x_csect.x_scnlen.l = 8;
5365 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5367 irel->r_size = 31;
5368 iraux.x_csect.x_scnlen.l = 4;
5370 else
5371 return FALSE;
5373 irel->r_type = R_POS;
5374 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5375 ++osec->reloc_count;
5377 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5378 output_bfd, irel, NULL, h))
5379 return FALSE;
5381 /* We need to emit a symbol to define a csect which holds
5382 the reloc. */
5383 if (flinfo->info->strip != strip_all)
5385 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab,
5386 &irsym, h->root.root.string);
5387 if (!result)
5388 return FALSE;
5390 irsym.n_value = irel->r_vaddr;
5391 irsym.n_scnum = osec->target_index;
5392 irsym.n_sclass = C_HIDEXT;
5393 irsym.n_type = T_NULL;
5394 irsym.n_numaux = 1;
5396 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5397 outsym += bfd_coff_symesz (output_bfd);
5399 /* Note : iraux is initialized above. */
5400 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5401 0, 1, (void *) outsym);
5402 outsym += bfd_coff_auxesz (output_bfd);
5404 if (h->indx >= 0)
5406 /* We aren't going to write out the symbols below, so we
5407 need to write them out now. */
5408 pos = obj_sym_filepos (output_bfd);
5409 pos += (obj_raw_syment_count (output_bfd)
5410 * bfd_coff_symesz (output_bfd));
5411 amt = outsym - flinfo->outsyms;
5412 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5413 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5414 return FALSE;
5415 obj_raw_syment_count (output_bfd) +=
5416 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5418 outsym = flinfo->outsyms;
5423 /* If this symbol is a specially defined function descriptor, write
5424 it out. The first word is the address of the function code
5425 itself, the second word is the address of the TOC, and the third
5426 word is zero.
5428 32 bit vs 64 bit
5429 The addresses for the 32 bit will take 4 bytes and the addresses
5430 for 64 bit will take 8 bytes. Similar for the relocs. This type
5431 of logic was also done above to create a TOC entry in
5432 xcoff_write_global_symbol. */
5433 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5434 && h->root.type == bfd_link_hash_defined
5435 && (h->root.u.def.section
5436 == xcoff_hash_table (flinfo->info)->descriptor_section))
5438 asection *sec;
5439 asection *osec;
5440 int oindx;
5441 bfd_byte *p;
5442 struct xcoff_link_hash_entry *hentry;
5443 asection *esec;
5444 struct internal_reloc *irel;
5445 asection *tsec;
5446 unsigned int reloc_size, byte_size;
5448 if (bfd_xcoff_is_xcoff64 (output_bfd))
5450 reloc_size = 63;
5451 byte_size = 8;
5453 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5455 reloc_size = 31;
5456 byte_size = 4;
5458 else
5459 return FALSE;
5461 sec = h->root.u.def.section;
5462 osec = sec->output_section;
5463 oindx = osec->target_index;
5464 p = sec->contents + h->root.u.def.value;
5466 hentry = h->descriptor;
5467 BFD_ASSERT (hentry != NULL
5468 && (hentry->root.type == bfd_link_hash_defined
5469 || hentry->root.type == bfd_link_hash_defweak));
5470 esec = hentry->root.u.def.section;
5472 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5473 irel->r_vaddr = (osec->vma
5474 + sec->output_offset
5475 + h->root.u.def.value);
5476 irel->r_symndx = esec->output_section->target_index;
5477 irel->r_type = R_POS;
5478 irel->r_size = reloc_size;
5479 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5480 ++osec->reloc_count;
5482 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5483 output_bfd, irel, esec, NULL))
5484 return FALSE;
5486 /* There are three items to write out,
5487 the address of the code
5488 the address of the toc anchor
5489 the environment pointer.
5490 We are ignoring the environment pointer. So set it to zero. */
5491 if (bfd_xcoff_is_xcoff64 (output_bfd))
5493 bfd_put_64 (output_bfd,
5494 (esec->output_section->vma + esec->output_offset
5495 + hentry->root.u.def.value),
5497 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5498 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5500 else
5502 /* 32 bit backend
5503 This logic was already called above so the error case where
5504 the backend is neither has already been checked. */
5505 bfd_put_32 (output_bfd,
5506 (esec->output_section->vma + esec->output_offset
5507 + hentry->root.u.def.value),
5509 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5510 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5513 tsec = coff_section_from_bfd_index (output_bfd,
5514 xcoff_data (output_bfd)->sntoc);
5516 ++irel;
5517 irel->r_vaddr = (osec->vma
5518 + sec->output_offset
5519 + h->root.u.def.value
5520 + byte_size);
5521 irel->r_symndx = tsec->output_section->target_index;
5522 irel->r_type = R_POS;
5523 irel->r_size = reloc_size;
5524 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5525 ++osec->reloc_count;
5527 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5528 output_bfd, irel, tsec, NULL))
5529 return FALSE;
5532 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5534 BFD_ASSERT (outsym == flinfo->outsyms);
5535 return TRUE;
5538 if (h->indx != -2
5539 && (flinfo->info->strip == strip_all
5540 || (flinfo->info->strip == strip_some
5541 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5542 FALSE, FALSE) == NULL)))
5544 BFD_ASSERT (outsym == flinfo->outsyms);
5545 return TRUE;
5548 if (h->indx != -2
5549 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5551 BFD_ASSERT (outsym == flinfo->outsyms);
5552 return TRUE;
5555 memset (&aux, 0, sizeof aux);
5557 h->indx = obj_raw_syment_count (output_bfd);
5559 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &isym,
5560 h->root.root.string);
5561 if (!result)
5562 return FALSE;
5564 if (h->root.type == bfd_link_hash_undefined
5565 || h->root.type == bfd_link_hash_undefweak)
5567 isym.n_value = 0;
5568 isym.n_scnum = N_UNDEF;
5569 if (h->root.type == bfd_link_hash_undefweak
5570 && C_WEAKEXT == C_AIX_WEAKEXT)
5571 isym.n_sclass = C_WEAKEXT;
5572 else
5573 isym.n_sclass = C_EXT;
5574 aux.x_csect.x_smtyp = XTY_ER;
5576 else if ((h->root.type == bfd_link_hash_defined
5577 || h->root.type == bfd_link_hash_defweak)
5578 && h->smclas == XMC_XO)
5580 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5581 isym.n_value = h->root.u.def.value;
5582 isym.n_scnum = N_UNDEF;
5583 if (h->root.type == bfd_link_hash_undefweak
5584 && C_WEAKEXT == C_AIX_WEAKEXT)
5585 isym.n_sclass = C_WEAKEXT;
5586 else
5587 isym.n_sclass = C_EXT;
5588 aux.x_csect.x_smtyp = XTY_ER;
5590 else if (h->root.type == bfd_link_hash_defined
5591 || h->root.type == bfd_link_hash_defweak)
5593 struct xcoff_link_size_list *l;
5595 isym.n_value = (h->root.u.def.section->output_section->vma
5596 + h->root.u.def.section->output_offset
5597 + h->root.u.def.value);
5598 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5599 isym.n_scnum = N_ABS;
5600 else
5601 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5602 isym.n_sclass = C_HIDEXT;
5603 aux.x_csect.x_smtyp = XTY_SD;
5605 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5607 for (l = xcoff_hash_table (flinfo->info)->size_list;
5608 l != NULL;
5609 l = l->next)
5611 if (l->h == h)
5613 aux.x_csect.x_scnlen.l = l->size;
5614 break;
5619 else if (h->root.type == bfd_link_hash_common)
5621 isym.n_value = (h->root.u.c.p->section->output_section->vma
5622 + h->root.u.c.p->section->output_offset);
5623 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5624 isym.n_sclass = C_EXT;
5625 aux.x_csect.x_smtyp = XTY_CM;
5626 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5628 else
5629 abort ();
5631 isym.n_type = T_NULL;
5632 isym.n_numaux = 1;
5634 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5635 outsym += bfd_coff_symesz (output_bfd);
5637 aux.x_csect.x_smclas = h->smclas;
5638 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5639 (void *) outsym);
5640 outsym += bfd_coff_auxesz (output_bfd);
5642 if ((h->root.type == bfd_link_hash_defined
5643 || h->root.type == bfd_link_hash_defweak)
5644 && h->smclas != XMC_XO)
5646 /* We just output an SD symbol. Now output an LD symbol. */
5647 h->indx += 2;
5649 if (h->root.type == bfd_link_hash_undefweak
5650 && C_WEAKEXT == C_AIX_WEAKEXT)
5651 isym.n_sclass = C_WEAKEXT;
5652 else
5653 isym.n_sclass = C_EXT;
5654 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5655 outsym += bfd_coff_symesz (output_bfd);
5657 aux.x_csect.x_smtyp = XTY_LD;
5658 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5659 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5660 (void *) outsym);
5661 outsym += bfd_coff_auxesz (output_bfd);
5664 pos = obj_sym_filepos (output_bfd);
5665 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5666 amt = outsym - flinfo->outsyms;
5667 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5668 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5669 return FALSE;
5670 obj_raw_syment_count (output_bfd) +=
5671 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5673 return TRUE;
5676 /* Handle a link order which is supposed to generate a reloc. */
5678 static bfd_boolean
5679 xcoff_reloc_link_order (bfd *output_bfd,
5680 struct xcoff_final_link_info *flinfo,
5681 asection *output_section,
5682 struct bfd_link_order *link_order)
5684 reloc_howto_type *howto;
5685 struct xcoff_link_hash_entry *h;
5686 asection *hsec;
5687 bfd_vma hval;
5688 bfd_vma addend;
5689 struct internal_reloc *irel;
5690 struct xcoff_link_hash_entry **rel_hash_ptr;
5692 if (link_order->type == bfd_section_reloc_link_order)
5693 /* We need to somehow locate a symbol in the right section. The
5694 symbol must either have a value of zero, or we must adjust
5695 the addend by the value of the symbol. FIXME: Write this
5696 when we need it. The old linker couldn't handle this anyhow. */
5697 abort ();
5699 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5700 if (howto == NULL)
5702 bfd_set_error (bfd_error_bad_value);
5703 return FALSE;
5706 h = ((struct xcoff_link_hash_entry *)
5707 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5708 link_order->u.reloc.p->u.name,
5709 FALSE, FALSE, TRUE));
5710 if (h == NULL)
5712 if (! ((*flinfo->info->callbacks->unattached_reloc)
5713 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5714 return FALSE;
5715 return TRUE;
5718 hsec = xcoff_symbol_section (h);
5719 if (h->root.type == bfd_link_hash_defined
5720 || h->root.type == bfd_link_hash_defweak)
5721 hval = h->root.u.def.value;
5722 else
5723 hval = 0;
5725 addend = link_order->u.reloc.p->addend;
5726 if (hsec != NULL)
5727 addend += (hsec->output_section->vma
5728 + hsec->output_offset
5729 + hval);
5731 if (addend != 0)
5733 bfd_size_type size;
5734 bfd_byte *buf;
5735 bfd_reloc_status_type rstat;
5736 bfd_boolean ok;
5738 size = bfd_get_reloc_size (howto);
5739 buf = bfd_zmalloc (size);
5740 if (buf == NULL)
5741 return FALSE;
5743 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5744 switch (rstat)
5746 case bfd_reloc_ok:
5747 break;
5748 default:
5749 case bfd_reloc_outofrange:
5750 abort ();
5751 case bfd_reloc_overflow:
5752 if (! ((*flinfo->info->callbacks->reloc_overflow)
5753 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5754 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5756 free (buf);
5757 return FALSE;
5759 break;
5761 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5762 (file_ptr) link_order->offset, size);
5763 free (buf);
5764 if (! ok)
5765 return FALSE;
5768 /* Store the reloc information in the right place. It will get
5769 swapped and written out at the end of the final_link routine. */
5770 irel = (flinfo->section_info[output_section->target_index].relocs
5771 + output_section->reloc_count);
5772 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5773 + output_section->reloc_count);
5775 memset (irel, 0, sizeof (struct internal_reloc));
5776 *rel_hash_ptr = NULL;
5778 irel->r_vaddr = output_section->vma + link_order->offset;
5780 if (h->indx >= 0)
5781 irel->r_symndx = h->indx;
5782 else
5784 /* Set the index to -2 to force this symbol to get written out. */
5785 h->indx = -2;
5786 *rel_hash_ptr = h;
5787 irel->r_symndx = 0;
5790 irel->r_type = howto->type;
5791 irel->r_size = howto->bitsize - 1;
5792 if (howto->complain_on_overflow == complain_overflow_signed)
5793 irel->r_size |= 0x80;
5795 ++output_section->reloc_count;
5797 /* Now output the reloc to the .loader section. */
5798 if (xcoff_hash_table (flinfo->info)->loader_section)
5800 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5801 output_bfd, irel, hsec, h))
5802 return FALSE;
5805 return TRUE;
5808 /* Do the final link step. */
5810 bfd_boolean
5811 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5813 bfd_size_type symesz;
5814 struct xcoff_final_link_info flinfo;
5815 asection *o;
5816 struct bfd_link_order *p;
5817 bfd_size_type max_contents_size;
5818 bfd_size_type max_sym_count;
5819 bfd_size_type max_lineno_count;
5820 bfd_size_type max_reloc_count;
5821 bfd_size_type max_output_reloc_count;
5822 file_ptr rel_filepos;
5823 unsigned int relsz;
5824 file_ptr line_filepos;
5825 unsigned int linesz;
5826 bfd *sub;
5827 bfd_byte *external_relocs = NULL;
5828 char strbuf[STRING_SIZE_SIZE];
5829 file_ptr pos;
5830 bfd_size_type amt;
5832 if (info->shared)
5833 abfd->flags |= DYNAMIC;
5835 symesz = bfd_coff_symesz (abfd);
5837 flinfo.info = info;
5838 flinfo.output_bfd = abfd;
5839 flinfo.strtab = NULL;
5840 flinfo.section_info = NULL;
5841 flinfo.last_file_index = -1;
5842 flinfo.toc_symindx = -1;
5843 flinfo.internal_syms = NULL;
5844 flinfo.sym_indices = NULL;
5845 flinfo.outsyms = NULL;
5846 flinfo.linenos = NULL;
5847 flinfo.contents = NULL;
5848 flinfo.external_relocs = NULL;
5850 if (xcoff_hash_table (info)->loader_section)
5852 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5853 + bfd_xcoff_ldhdrsz (abfd));
5854 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5855 + bfd_xcoff_ldhdrsz (abfd)
5856 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5857 * bfd_xcoff_ldsymsz (abfd)));
5859 else
5861 flinfo.ldsym = NULL;
5862 flinfo.ldrel = NULL;
5865 xcoff_data (abfd)->coff.link_info = info;
5867 flinfo.strtab = _bfd_stringtab_init ();
5868 if (flinfo.strtab == NULL)
5869 goto error_return;
5871 /* Count the relocation entries required for the output file.
5872 (We've already counted the line numbers.) Determine a few
5873 maximum sizes. */
5874 max_contents_size = 0;
5875 max_lineno_count = 0;
5876 max_reloc_count = 0;
5877 for (o = abfd->sections; o != NULL; o = o->next)
5879 o->reloc_count = 0;
5880 for (p = o->map_head.link_order; p != NULL; p = p->next)
5882 if (p->type == bfd_indirect_link_order)
5884 asection *sec;
5886 sec = p->u.indirect.section;
5888 /* Mark all sections which are to be included in the
5889 link. This will normally be every section. We need
5890 to do this so that we can identify any sections which
5891 the linker has decided to not include. */
5892 sec->linker_mark = TRUE;
5894 o->reloc_count += sec->reloc_count;
5896 if ((sec->flags & SEC_IN_MEMORY) == 0)
5898 if (sec->rawsize > max_contents_size)
5899 max_contents_size = sec->rawsize;
5900 if (sec->size > max_contents_size)
5901 max_contents_size = sec->size;
5903 if (coff_section_data (sec->owner, sec) != NULL
5904 && xcoff_section_data (sec->owner, sec) != NULL
5905 && (xcoff_section_data (sec->owner, sec)->lineno_count
5906 > max_lineno_count))
5907 max_lineno_count =
5908 xcoff_section_data (sec->owner, sec)->lineno_count;
5909 if (sec->reloc_count > max_reloc_count)
5910 max_reloc_count = sec->reloc_count;
5912 else if (p->type == bfd_section_reloc_link_order
5913 || p->type == bfd_symbol_reloc_link_order)
5914 ++o->reloc_count;
5918 /* Compute the file positions for all the sections. */
5919 if (abfd->output_has_begun)
5921 if (xcoff_hash_table (info)->file_align != 0)
5922 abort ();
5924 else
5926 bfd_vma file_align;
5928 file_align = xcoff_hash_table (info)->file_align;
5929 if (file_align != 0)
5931 bfd_boolean saw_contents;
5932 int indx;
5933 file_ptr sofar;
5935 /* Insert .pad sections before every section which has
5936 contents and is loaded, if it is preceded by some other
5937 section which has contents and is loaded. */
5938 saw_contents = TRUE;
5939 for (o = abfd->sections; o != NULL; o = o->next)
5941 if (strcmp (o->name, ".pad") == 0)
5942 saw_contents = FALSE;
5943 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5944 && (o->flags & SEC_LOAD) != 0)
5946 if (! saw_contents)
5947 saw_contents = TRUE;
5948 else
5950 asection *n;
5952 /* Create a pad section and place it before the section
5953 that needs padding. This requires unlinking and
5954 relinking the bfd's section list. */
5956 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5957 SEC_HAS_CONTENTS);
5958 n->alignment_power = 0;
5960 bfd_section_list_remove (abfd, n);
5961 bfd_section_list_insert_before (abfd, o, n);
5962 saw_contents = FALSE;
5967 /* Reset the section indices after inserting the new
5968 sections. */
5969 indx = 0;
5970 for (o = abfd->sections; o != NULL; o = o->next)
5972 ++indx;
5973 o->target_index = indx;
5975 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5977 /* Work out appropriate sizes for the .pad sections to force
5978 each section to land on a page boundary. This bit of
5979 code knows what compute_section_file_positions is going
5980 to do. */
5981 sofar = bfd_coff_filhsz (abfd);
5982 sofar += bfd_coff_aoutsz (abfd);
5983 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5984 for (o = abfd->sections; o != NULL; o = o->next)
5985 if ((bfd_xcoff_is_reloc_count_overflow
5986 (abfd, (bfd_vma) o->reloc_count))
5987 || (bfd_xcoff_is_lineno_count_overflow
5988 (abfd, (bfd_vma) o->lineno_count)))
5989 /* 64 does not overflow, need to check if 32 does */
5990 sofar += bfd_coff_scnhsz (abfd);
5992 for (o = abfd->sections; o != NULL; o = o->next)
5994 if (strcmp (o->name, ".pad") == 0)
5996 bfd_vma pageoff;
5998 BFD_ASSERT (o->size == 0);
5999 pageoff = sofar & (file_align - 1);
6000 if (pageoff != 0)
6002 o->size = file_align - pageoff;
6003 sofar += file_align - pageoff;
6004 o->flags |= SEC_HAS_CONTENTS;
6007 else
6009 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6010 sofar += BFD_ALIGN (o->size,
6011 1 << o->alignment_power);
6016 if (! bfd_coff_compute_section_file_positions (abfd))
6017 goto error_return;
6020 /* Allocate space for the pointers we need to keep for the relocs. */
6022 unsigned int i;
6024 /* We use section_count + 1, rather than section_count, because
6025 the target_index fields are 1 based. */
6026 amt = abfd->section_count + 1;
6027 amt *= sizeof (struct xcoff_link_section_info);
6028 flinfo.section_info = bfd_malloc (amt);
6029 if (flinfo.section_info == NULL)
6030 goto error_return;
6031 for (i = 0; i <= abfd->section_count; i++)
6033 flinfo.section_info[i].relocs = NULL;
6034 flinfo.section_info[i].rel_hashes = NULL;
6035 flinfo.section_info[i].toc_rel_hashes = NULL;
6039 /* Set the file positions for the relocs. */
6040 rel_filepos = obj_relocbase (abfd);
6041 relsz = bfd_coff_relsz (abfd);
6042 max_output_reloc_count = 0;
6043 for (o = abfd->sections; o != NULL; o = o->next)
6045 if (o->reloc_count == 0)
6046 o->rel_filepos = 0;
6047 else
6049 /* A stripped file has no relocs. However, we still
6050 allocate the buffers, so that later code doesn't have to
6051 worry about whether we are stripping or not. */
6052 if (info->strip == strip_all)
6053 o->rel_filepos = 0;
6054 else
6056 o->flags |= SEC_RELOC;
6057 o->rel_filepos = rel_filepos;
6058 rel_filepos += o->reloc_count * relsz;
6061 /* We don't know the indices of global symbols until we have
6062 written out all the local symbols. For each section in
6063 the output file, we keep an array of pointers to hash
6064 table entries. Each entry in the array corresponds to a
6065 reloc. When we find a reloc against a global symbol, we
6066 set the corresponding entry in this array so that we can
6067 fix up the symbol index after we have written out all the
6068 local symbols.
6070 Because of this problem, we also keep the relocs in
6071 memory until the end of the link. This wastes memory.
6072 We could backpatch the file later, I suppose, although it
6073 would be slow. */
6074 amt = o->reloc_count;
6075 amt *= sizeof (struct internal_reloc);
6076 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6078 amt = o->reloc_count;
6079 amt *= sizeof (struct xcoff_link_hash_entry *);
6080 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6082 if (flinfo.section_info[o->target_index].relocs == NULL
6083 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6084 goto error_return;
6086 if (o->reloc_count > max_output_reloc_count)
6087 max_output_reloc_count = o->reloc_count;
6091 /* We now know the size of the relocs, so we can determine the file
6092 positions of the line numbers. */
6093 line_filepos = rel_filepos;
6094 flinfo.line_filepos = line_filepos;
6095 linesz = bfd_coff_linesz (abfd);
6096 for (o = abfd->sections; o != NULL; o = o->next)
6098 if (o->lineno_count == 0)
6099 o->line_filepos = 0;
6100 else
6102 o->line_filepos = line_filepos;
6103 line_filepos += o->lineno_count * linesz;
6106 /* Reset the reloc and lineno counts, so that we can use them to
6107 count the number of entries we have output so far. */
6108 o->reloc_count = 0;
6109 o->lineno_count = 0;
6112 obj_sym_filepos (abfd) = line_filepos;
6114 /* Figure out the largest number of symbols in an input BFD. Take
6115 the opportunity to clear the output_has_begun fields of all the
6116 input BFD's. We want at least 6 symbols, since that is the
6117 number which xcoff_write_global_symbol may need. */
6118 max_sym_count = 6;
6119 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6121 bfd_size_type sz;
6123 sub->output_has_begun = FALSE;
6124 sz = obj_raw_syment_count (sub);
6125 if (sz > max_sym_count)
6126 max_sym_count = sz;
6129 /* Allocate some buffers used while linking. */
6130 amt = max_sym_count * sizeof (struct internal_syment);
6131 flinfo.internal_syms = bfd_malloc (amt);
6133 amt = max_sym_count * sizeof (long);
6134 flinfo.sym_indices = bfd_malloc (amt);
6136 amt = (max_sym_count + 1) * symesz;
6137 flinfo.outsyms = bfd_malloc (amt);
6139 amt = max_lineno_count * bfd_coff_linesz (abfd);
6140 flinfo.linenos = bfd_malloc (amt);
6142 amt = max_contents_size;
6143 flinfo.contents = bfd_malloc (amt);
6145 amt = max_reloc_count * relsz;
6146 flinfo.external_relocs = bfd_malloc (amt);
6148 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6149 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6150 || flinfo.outsyms == NULL
6151 || (flinfo.linenos == NULL && max_lineno_count > 0)
6152 || (flinfo.contents == NULL && max_contents_size > 0)
6153 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6154 goto error_return;
6156 obj_raw_syment_count (abfd) = 0;
6158 /* Find a TOC symbol, if we need one. */
6159 if (!xcoff_find_tc0 (abfd, &flinfo))
6160 goto error_return;
6162 /* We now know the position of everything in the file, except that
6163 we don't know the size of the symbol table and therefore we don't
6164 know where the string table starts. We just build the string
6165 table in memory as we go along. We process all the relocations
6166 for a single input file at once. */
6167 for (o = abfd->sections; o != NULL; o = o->next)
6169 for (p = o->map_head.link_order; p != NULL; p = p->next)
6171 if (p->type == bfd_indirect_link_order
6172 && p->u.indirect.section->owner->xvec == abfd->xvec)
6174 sub = p->u.indirect.section->owner;
6175 if (! sub->output_has_begun)
6177 if (! xcoff_link_input_bfd (&flinfo, sub))
6178 goto error_return;
6179 sub->output_has_begun = TRUE;
6182 else if (p->type == bfd_section_reloc_link_order
6183 || p->type == bfd_symbol_reloc_link_order)
6185 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6186 goto error_return;
6188 else
6190 if (! _bfd_default_link_order (abfd, info, o, p))
6191 goto error_return;
6196 /* Free up the buffers used by xcoff_link_input_bfd. */
6197 if (flinfo.internal_syms != NULL)
6199 free (flinfo.internal_syms);
6200 flinfo.internal_syms = NULL;
6202 if (flinfo.sym_indices != NULL)
6204 free (flinfo.sym_indices);
6205 flinfo.sym_indices = NULL;
6207 if (flinfo.linenos != NULL)
6209 free (flinfo.linenos);
6210 flinfo.linenos = NULL;
6212 if (flinfo.contents != NULL)
6214 free (flinfo.contents);
6215 flinfo.contents = NULL;
6217 if (flinfo.external_relocs != NULL)
6219 free (flinfo.external_relocs);
6220 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 if (flinfo.outsyms != NULL)
6242 free (flinfo.outsyms);
6243 flinfo.outsyms = NULL;
6246 /* Now that we have written out all the global symbols, we know the
6247 symbol indices to use for relocs against them, and we can finally
6248 write out the relocs. */
6249 amt = max_output_reloc_count * relsz;
6250 external_relocs = bfd_malloc (amt);
6251 if (external_relocs == NULL && max_output_reloc_count != 0)
6252 goto error_return;
6254 for (o = abfd->sections; o != NULL; o = o->next)
6256 struct internal_reloc *irel;
6257 struct internal_reloc *irelend;
6258 struct xcoff_link_hash_entry **rel_hash;
6259 struct xcoff_toc_rel_hash *toc_rel_hash;
6260 bfd_byte *erel;
6261 bfd_size_type rel_size;
6263 /* A stripped file has no relocs. */
6264 if (info->strip == strip_all)
6266 o->reloc_count = 0;
6267 continue;
6270 if (o->reloc_count == 0)
6271 continue;
6273 irel = flinfo.section_info[o->target_index].relocs;
6274 irelend = irel + o->reloc_count;
6275 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6276 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6278 if (*rel_hash != NULL)
6280 if ((*rel_hash)->indx < 0)
6282 if (! ((*info->callbacks->unattached_reloc)
6283 (info, (*rel_hash)->root.root.string,
6284 NULL, o, irel->r_vaddr)))
6285 goto error_return;
6286 (*rel_hash)->indx = 0;
6288 irel->r_symndx = (*rel_hash)->indx;
6292 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6293 toc_rel_hash != NULL;
6294 toc_rel_hash = toc_rel_hash->next)
6296 if (toc_rel_hash->h->u.toc_indx < 0)
6298 if (! ((*info->callbacks->unattached_reloc)
6299 (info, toc_rel_hash->h->root.root.string,
6300 NULL, o, toc_rel_hash->rel->r_vaddr)))
6301 goto error_return;
6302 toc_rel_hash->h->u.toc_indx = 0;
6304 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6307 /* XCOFF requires that the relocs be sorted by address. We tend
6308 to produce them in the order in which their containing csects
6309 appear in the symbol table, which is not necessarily by
6310 address. So we sort them here. There may be a better way to
6311 do this. */
6312 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6313 o->reloc_count, sizeof (struct internal_reloc),
6314 xcoff_sort_relocs);
6316 irel = flinfo.section_info[o->target_index].relocs;
6317 irelend = irel + o->reloc_count;
6318 erel = external_relocs;
6319 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6320 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6322 rel_size = relsz * o->reloc_count;
6323 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6324 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6325 goto error_return;
6328 if (external_relocs != NULL)
6330 free (external_relocs);
6331 external_relocs = NULL;
6334 /* Free up the section information. */
6335 if (flinfo.section_info != NULL)
6337 unsigned int i;
6339 for (i = 0; i < abfd->section_count; i++)
6341 if (flinfo.section_info[i].relocs != NULL)
6342 free (flinfo.section_info[i].relocs);
6343 if (flinfo.section_info[i].rel_hashes != NULL)
6344 free (flinfo.section_info[i].rel_hashes);
6346 free (flinfo.section_info);
6347 flinfo.section_info = NULL;
6350 /* Write out the loader section contents. */
6351 o = xcoff_hash_table (info)->loader_section;
6352 if (o)
6354 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6355 == (xcoff_hash_table (info)->loader_section->contents
6356 + xcoff_hash_table (info)->ldhdr.l_impoff));
6357 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6358 (file_ptr) o->output_offset, o->size))
6359 goto error_return;
6362 /* Write out the magic sections. */
6363 o = xcoff_hash_table (info)->linkage_section;
6364 if (o->size > 0
6365 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6366 (file_ptr) o->output_offset,
6367 o->size))
6368 goto error_return;
6369 o = xcoff_hash_table (info)->toc_section;
6370 if (o->size > 0
6371 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6372 (file_ptr) o->output_offset,
6373 o->size))
6374 goto error_return;
6375 o = xcoff_hash_table (info)->descriptor_section;
6376 if (o->size > 0
6377 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6378 (file_ptr) o->output_offset,
6379 o->size))
6380 goto error_return;
6382 /* Write out the string table. */
6383 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6384 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6385 goto error_return;
6386 H_PUT_32 (abfd,
6387 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6388 strbuf);
6389 amt = STRING_SIZE_SIZE;
6390 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6391 goto error_return;
6392 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6393 goto error_return;
6395 _bfd_stringtab_free (flinfo.strtab);
6397 /* Write out the debugging string table. */
6398 o = xcoff_hash_table (info)->debug_section;
6399 if (o != NULL)
6401 struct bfd_strtab_hash *debug_strtab;
6403 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6404 BFD_ASSERT (o->output_section->size - o->output_offset
6405 >= _bfd_stringtab_size (debug_strtab));
6406 pos = o->output_section->filepos + o->output_offset;
6407 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6408 goto error_return;
6409 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6410 goto error_return;
6413 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6414 not try to write out the symbols. */
6415 bfd_get_symcount (abfd) = 0;
6417 return TRUE;
6419 error_return:
6420 if (flinfo.strtab != NULL)
6421 _bfd_stringtab_free (flinfo.strtab);
6423 if (flinfo.section_info != NULL)
6425 unsigned int i;
6427 for (i = 0; i < abfd->section_count; i++)
6429 if (flinfo.section_info[i].relocs != NULL)
6430 free (flinfo.section_info[i].relocs);
6431 if (flinfo.section_info[i].rel_hashes != NULL)
6432 free (flinfo.section_info[i].rel_hashes);
6434 free (flinfo.section_info);
6437 if (flinfo.internal_syms != NULL)
6438 free (flinfo.internal_syms);
6439 if (flinfo.sym_indices != NULL)
6440 free (flinfo.sym_indices);
6441 if (flinfo.outsyms != NULL)
6442 free (flinfo.outsyms);
6443 if (flinfo.linenos != NULL)
6444 free (flinfo.linenos);
6445 if (flinfo.contents != NULL)
6446 free (flinfo.contents);
6447 if (flinfo.external_relocs != NULL)
6448 free (flinfo.external_relocs);
6449 if (external_relocs != NULL)
6450 free (external_relocs);
6451 return FALSE;