PR ld/11843
[binutils.git] / bfd / xcofflink.c
blob30923cfba435e7d578d3ce276b0df1a7c278b325
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
29 #include "libcoff.h"
30 #include "libxcoff.h"
31 #include "libiberty.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file *next;
49 /* The path. */
50 const char *path;
51 /* The file name. */
52 const char *file;
53 /* The member name. */
54 const char *member;
57 /* Information we keep for each section in the output file during the
58 final link phase. */
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc *relocs;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry **rel_hashes;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash *next;
74 struct xcoff_link_hash_entry *h;
75 struct internal_reloc *rel;
76 } *toc_rel_hashes;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
83 bfd *archive;
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
87 const char *imppath;
88 const char *impfile;
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p : 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p : 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash *debug_strtab;
106 /* The .debug section we will use for the final output. */
107 asection *debug_section;
109 /* The .loader section we will use for the final output. */
110 asection *loader_section;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
114 size_t ldrel_count;
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr;
119 /* The .gl section we use to hold global linkage code. */
120 asection *linkage_section;
122 /* The .tc section we use to hold toc entries we build for global
123 linkage code. */
124 asection *toc_section;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection *descriptor_section;
130 /* The list of import files. */
131 struct xcoff_import_file *imports;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align;
136 /* Whether the .text section must be read-only. */
137 bfd_boolean textro;
139 /* Whether -brtl was specified. */
140 bfd_boolean rtld;
142 /* Whether garbage collection was done. */
143 bfd_boolean gc;
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list *next;
149 struct xcoff_link_hash_entry *h;
150 bfd_size_type size;
152 *size_list;
154 /* Information about archives. */
155 htab_t archive_info;
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info *info;
167 /* Output BFD. */
168 bfd *output_bfd;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash *strtab;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info *section_info;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file;
178 /* Symbol index of TOC symbol. */
179 long toc_symindx;
180 /* Start of .loader symbols. */
181 bfd_byte *ldsym;
182 /* Next .loader reloc to swap out. */
183 bfd_byte *ldrel;
184 /* File position of start of line numbers. */
185 file_ptr line_filepos;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment *internal_syms;
188 /* Buffer large enough to hold output indices of symbols of any
189 input file. */
190 long *sym_indices;
191 /* Buffer large enough to hold output symbols for any input file. */
192 bfd_byte *outsyms;
193 /* Buffer large enough to hold external line numbers for any input
194 section. */
195 bfd_byte *linenos;
196 /* Buffer large enough to hold any input section. */
197 bfd_byte *contents;
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte *external_relocs;
202 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
208 here. */
210 /* Read the contents of a section. */
212 static bfd_boolean
213 xcoff_get_section_contents (bfd *abfd, asection *sec)
215 if (coff_section_data (abfd, sec) == NULL)
217 bfd_size_type amt = sizeof (struct coff_section_tdata);
219 sec->used_by_bfd = bfd_zalloc (abfd, amt);
220 if (sec->used_by_bfd == NULL)
221 return FALSE;
224 if (coff_section_data (abfd, sec)->contents == NULL)
226 bfd_byte *contents;
228 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
230 if (contents != NULL)
231 free (contents);
232 return FALSE;
234 coff_section_data (abfd, sec)->contents = contents;
237 return TRUE;
240 /* Get the size required to hold the dynamic symbols. */
242 long
243 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
245 asection *lsec;
246 bfd_byte *contents;
247 struct internal_ldhdr ldhdr;
249 if ((abfd->flags & DYNAMIC) == 0)
251 bfd_set_error (bfd_error_invalid_operation);
252 return -1;
255 lsec = bfd_get_section_by_name (abfd, ".loader");
256 if (lsec == NULL)
258 bfd_set_error (bfd_error_no_symbols);
259 return -1;
262 if (! xcoff_get_section_contents (abfd, lsec))
263 return -1;
264 contents = coff_section_data (abfd, lsec)->contents;
266 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
268 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
271 /* Get the dynamic symbols. */
273 long
274 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
276 asection *lsec;
277 bfd_byte *contents;
278 struct internal_ldhdr ldhdr;
279 const char *strings;
280 bfd_byte *elsym, *elsymend;
281 coff_symbol_type *symbuf;
283 if ((abfd->flags & DYNAMIC) == 0)
285 bfd_set_error (bfd_error_invalid_operation);
286 return -1;
289 lsec = bfd_get_section_by_name (abfd, ".loader");
290 if (lsec == NULL)
292 bfd_set_error (bfd_error_no_symbols);
293 return -1;
296 if (! xcoff_get_section_contents (abfd, lsec))
297 return -1;
298 contents = coff_section_data (abfd, lsec)->contents;
300 coff_section_data (abfd, lsec)->keep_contents = TRUE;
302 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
304 strings = (char *) contents + ldhdr.l_stoff;
306 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
307 if (symbuf == NULL)
308 return -1;
310 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
312 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
313 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
315 struct internal_ldsym ldsym;
317 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
319 symbuf->symbol.the_bfd = abfd;
321 if (ldsym._l._l_l._l_zeroes == 0)
322 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
323 else
325 char *c;
327 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
328 if (c == NULL)
329 return -1;
330 memcpy (c, ldsym._l._l_name, SYMNMLEN);
331 c[SYMNMLEN] = '\0';
332 symbuf->symbol.name = c;
335 if (ldsym.l_smclas == XMC_XO)
336 symbuf->symbol.section = bfd_abs_section_ptr;
337 else
338 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
339 ldsym.l_scnum);
340 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
342 symbuf->symbol.flags = BSF_NO_FLAGS;
343 if ((ldsym.l_smtype & L_EXPORT) != 0)
345 if ((ldsym.l_smtype & L_WEAK) != 0)
346 symbuf->symbol.flags |= BSF_WEAK;
347 else
348 symbuf->symbol.flags |= BSF_GLOBAL;
351 /* FIXME: We have no way to record the other information stored
352 with the loader symbol. */
353 *psyms = (asymbol *) symbuf;
356 *psyms = NULL;
358 return ldhdr.l_nsyms;
361 /* Get the size required to hold the dynamic relocs. */
363 long
364 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
366 asection *lsec;
367 bfd_byte *contents;
368 struct internal_ldhdr ldhdr;
370 if ((abfd->flags & DYNAMIC) == 0)
372 bfd_set_error (bfd_error_invalid_operation);
373 return -1;
376 lsec = bfd_get_section_by_name (abfd, ".loader");
377 if (lsec == NULL)
379 bfd_set_error (bfd_error_no_symbols);
380 return -1;
383 if (! xcoff_get_section_contents (abfd, lsec))
384 return -1;
385 contents = coff_section_data (abfd, lsec)->contents;
387 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
389 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
392 /* Get the dynamic relocs. */
394 long
395 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
396 arelent **prelocs,
397 asymbol **syms)
399 asection *lsec;
400 bfd_byte *contents;
401 struct internal_ldhdr ldhdr;
402 arelent *relbuf;
403 bfd_byte *elrel, *elrelend;
405 if ((abfd->flags & DYNAMIC) == 0)
407 bfd_set_error (bfd_error_invalid_operation);
408 return -1;
411 lsec = bfd_get_section_by_name (abfd, ".loader");
412 if (lsec == NULL)
414 bfd_set_error (bfd_error_no_symbols);
415 return -1;
418 if (! xcoff_get_section_contents (abfd, lsec))
419 return -1;
420 contents = coff_section_data (abfd, lsec)->contents;
422 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
424 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
425 if (relbuf == NULL)
426 return -1;
428 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
430 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
431 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
432 prelocs++)
434 struct internal_ldrel ldrel;
436 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
438 if (ldrel.l_symndx >= 3)
439 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
440 else
442 const char *name;
443 asection *sec;
445 switch (ldrel.l_symndx)
447 case 0:
448 name = ".text";
449 break;
450 case 1:
451 name = ".data";
452 break;
453 case 2:
454 name = ".bss";
455 break;
456 default:
457 abort ();
458 break;
461 sec = bfd_get_section_by_name (abfd, name);
462 if (sec == NULL)
464 bfd_set_error (bfd_error_bad_value);
465 return -1;
468 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
471 relbuf->address = ldrel.l_vaddr;
472 relbuf->addend = 0;
474 /* Most dynamic relocs have the same type. FIXME: This is only
475 correct if ldrel.l_rtype == 0. In other cases, we should use
476 a different howto. */
477 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
479 /* FIXME: We have no way to record the l_rsecnm field. */
481 *prelocs = relbuf;
484 *prelocs = NULL;
486 return ldhdr.l_nreloc;
489 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 static hashval_t
492 xcoff_archive_info_hash (const void *data)
494 const struct xcoff_archive_info *info;
496 info = (const struct xcoff_archive_info *) data;
497 return htab_hash_pointer (info->archive);
500 static int
501 xcoff_archive_info_eq (const void *data1, const void *data2)
503 const struct xcoff_archive_info *info1;
504 const struct xcoff_archive_info *info2;
506 info1 = (const struct xcoff_archive_info *) data1;
507 info2 = (const struct xcoff_archive_info *) data2;
508 return info1->archive == info2->archive;
511 /* Return information about archive ARCHIVE. Return NULL on error. */
513 static struct xcoff_archive_info *
514 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
516 struct xcoff_link_hash_table *htab;
517 struct xcoff_archive_info *entryp, entry;
518 void **slot;
520 htab = xcoff_hash_table (info);
521 entry.archive = archive;
522 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
523 if (!slot)
524 return NULL;
526 entryp = *slot;
527 if (!entryp)
529 entryp = bfd_zalloc (archive, sizeof (entry));
530 if (!entryp)
531 return NULL;
533 entryp->archive = archive;
534 *slot = entryp;
536 return entryp;
539 /* Routine to create an entry in an XCOFF link hash table. */
541 static struct bfd_hash_entry *
542 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
543 struct bfd_hash_table *table,
544 const char *string)
546 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
548 /* Allocate the structure if it has not already been allocated by a
549 subclass. */
550 if (ret == NULL)
551 ret = bfd_hash_allocate (table, sizeof (* ret));
552 if (ret == NULL)
553 return NULL;
555 /* Call the allocation method of the superclass. */
556 ret = ((struct xcoff_link_hash_entry *)
557 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
558 table, string));
559 if (ret != NULL)
561 /* Set local fields. */
562 ret->indx = -1;
563 ret->toc_section = NULL;
564 ret->u.toc_indx = -1;
565 ret->descriptor = NULL;
566 ret->ldsym = NULL;
567 ret->ldindx = -1;
568 ret->flags = 0;
569 ret->smclas = XMC_UA;
572 return (struct bfd_hash_entry *) ret;
575 /* Create a XCOFF link hash table. */
577 struct bfd_link_hash_table *
578 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
580 struct xcoff_link_hash_table *ret;
581 bfd_size_type amt = sizeof (* ret);
583 ret = bfd_malloc (amt);
584 if (ret == NULL)
585 return NULL;
586 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
587 sizeof (struct xcoff_link_hash_entry)))
589 free (ret);
590 return NULL;
593 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
594 ret->debug_section = NULL;
595 ret->loader_section = NULL;
596 ret->ldrel_count = 0;
597 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
598 ret->linkage_section = NULL;
599 ret->toc_section = NULL;
600 ret->descriptor_section = NULL;
601 ret->imports = NULL;
602 ret->file_align = 0;
603 ret->textro = FALSE;
604 ret->gc = FALSE;
605 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
606 xcoff_archive_info_eq, NULL);
607 memset (ret->special_sections, 0, sizeof ret->special_sections);
609 /* The linker will always generate a full a.out header. We need to
610 record that fact now, before the sizeof_headers routine could be
611 called. */
612 xcoff_data (abfd)->full_aouthdr = TRUE;
614 return &ret->root;
617 /* Free a XCOFF link hash table. */
619 void
620 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
622 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
624 _bfd_stringtab_free (ret->debug_strtab);
625 bfd_hash_table_free (&ret->root.table);
626 free (ret);
629 /* Read internal relocs for an XCOFF csect. This is a wrapper around
630 _bfd_coff_read_internal_relocs which tries to take advantage of any
631 relocs which may have been cached for the enclosing section. */
633 static struct internal_reloc *
634 xcoff_read_internal_relocs (bfd *abfd,
635 asection *sec,
636 bfd_boolean cache,
637 bfd_byte *external_relocs,
638 bfd_boolean require_internal,
639 struct internal_reloc *internal_relocs)
641 if (coff_section_data (abfd, sec) != NULL
642 && coff_section_data (abfd, sec)->relocs == NULL
643 && xcoff_section_data (abfd, sec) != NULL)
645 asection *enclosing;
647 enclosing = xcoff_section_data (abfd, sec)->enclosing;
649 if (enclosing != NULL
650 && (coff_section_data (abfd, enclosing) == NULL
651 || coff_section_data (abfd, enclosing)->relocs == NULL)
652 && cache
653 && enclosing->reloc_count > 0)
655 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
656 external_relocs, FALSE, NULL)
657 == NULL)
658 return NULL;
661 if (enclosing != NULL
662 && coff_section_data (abfd, enclosing) != NULL
663 && coff_section_data (abfd, enclosing)->relocs != NULL)
665 size_t off;
667 off = ((sec->rel_filepos - enclosing->rel_filepos)
668 / bfd_coff_relsz (abfd));
670 if (! require_internal)
671 return coff_section_data (abfd, enclosing)->relocs + off;
672 memcpy (internal_relocs,
673 coff_section_data (abfd, enclosing)->relocs + off,
674 sec->reloc_count * sizeof (struct internal_reloc));
675 return internal_relocs;
679 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
680 require_internal, internal_relocs);
683 /* Split FILENAME into an import path and an import filename,
684 storing them in *IMPPATH and *IMPFILE respectively. */
686 bfd_boolean
687 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
688 const char **imppath, const char **impfile)
690 const char *base;
691 size_t length;
692 char *path;
694 base = lbasename (filename);
695 length = base - filename;
696 if (length == 0)
697 /* The filename has no directory component, so use an empty path. */
698 *imppath = "";
699 else if (length == 1)
700 /* The filename is in the root directory. */
701 *imppath = "/";
702 else
704 /* Extract the (non-empty) directory part. Note that we don't
705 need to strip duplicate directory separators from any part
706 of the string; the native linker doesn't do that either. */
707 path = bfd_alloc (abfd, length);
708 if (path == NULL)
709 return FALSE;
710 memcpy (path, filename, length - 1);
711 path[length - 1] = 0;
712 *imppath = path;
714 *impfile = base;
715 return TRUE;
718 /* Set ARCHIVE's import path as though its filename had been given
719 as FILENAME. */
721 bfd_boolean
722 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
723 bfd *archive, const char *filename)
725 struct xcoff_archive_info *archive_info;
727 archive_info = xcoff_get_archive_info (info, archive);
728 return (archive_info != NULL
729 && bfd_xcoff_split_import_path (archive, filename,
730 &archive_info->imppath,
731 &archive_info->impfile));
734 /* H is an imported symbol. Set the import module's path, file and member
735 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
736 no specific import module is specified. */
738 static bfd_boolean
739 xcoff_set_import_path (struct bfd_link_info *info,
740 struct xcoff_link_hash_entry *h,
741 const char *imppath, const char *impfile,
742 const char *impmember)
744 unsigned int c;
745 struct xcoff_import_file **pp;
747 /* We overload the ldindx field to hold the l_ifile value for this
748 symbol. */
749 BFD_ASSERT (h->ldsym == NULL);
750 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
751 if (imppath == NULL)
752 h->ldindx = -1;
753 else
755 /* We start c at 1 because the first entry in the import list is
756 reserved for the library search path. */
757 for (pp = &xcoff_hash_table (info)->imports, c = 1;
758 *pp != NULL;
759 pp = &(*pp)->next, ++c)
761 if (strcmp ((*pp)->path, imppath) == 0
762 && strcmp ((*pp)->file, impfile) == 0
763 && strcmp ((*pp)->member, impmember) == 0)
764 break;
767 if (*pp == NULL)
769 struct xcoff_import_file *n;
770 bfd_size_type amt = sizeof (* n);
772 n = bfd_alloc (info->output_bfd, amt);
773 if (n == NULL)
774 return FALSE;
775 n->next = NULL;
776 n->path = imppath;
777 n->file = impfile;
778 n->member = impmember;
779 *pp = n;
781 h->ldindx = c;
783 return TRUE;
786 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
787 Return true if LDSYM defines H. */
789 static bfd_boolean
790 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
791 struct internal_ldsym *ldsym)
793 /* If we didn't know about H before processing LDSYM, LDSYM
794 definitely defines H. */
795 if (h->root.type == bfd_link_hash_new)
796 return TRUE;
798 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
799 dynamic symbol, LDSYM trumps the current definition of H. */
800 if ((ldsym->l_smtype & L_WEAK) == 0
801 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
802 && (h->flags & XCOFF_DEF_REGULAR) == 0
803 && (h->root.type == bfd_link_hash_defweak
804 || h->root.type == bfd_link_hash_undefweak))
805 return TRUE;
807 /* If H is currently undefined, LDSYM defines it. */
808 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
809 && (h->root.type == bfd_link_hash_undefined
810 || h->root.type == bfd_link_hash_undefweak))
811 return TRUE;
813 return FALSE;
816 /* This function is used to add symbols from a dynamic object to the
817 global symbol table. */
819 static bfd_boolean
820 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
822 asection *lsec;
823 bfd_byte *contents;
824 struct internal_ldhdr ldhdr;
825 const char *strings;
826 bfd_byte *elsym, *elsymend;
827 struct xcoff_import_file *n;
828 unsigned int c;
829 struct xcoff_import_file **pp;
831 /* We can only handle a dynamic object if we are generating an XCOFF
832 output file. */
833 if (info->output_bfd->xvec != abfd->xvec)
835 (*_bfd_error_handler)
836 (_("%s: XCOFF shared object when not producing XCOFF output"),
837 bfd_get_filename (abfd));
838 bfd_set_error (bfd_error_invalid_operation);
839 return FALSE;
842 /* The symbols we use from a dynamic object are not the symbols in
843 the normal symbol table, but, rather, the symbols in the export
844 table. If there is a global symbol in a dynamic object which is
845 not in the export table, the loader will not be able to find it,
846 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
847 libc.a has symbols in the export table which are not in the
848 symbol table. */
850 /* Read in the .loader section. FIXME: We should really use the
851 o_snloader field in the a.out header, rather than grabbing the
852 section by name. */
853 lsec = bfd_get_section_by_name (abfd, ".loader");
854 if (lsec == NULL)
856 (*_bfd_error_handler)
857 (_("%s: dynamic object with no .loader section"),
858 bfd_get_filename (abfd));
859 bfd_set_error (bfd_error_no_symbols);
860 return FALSE;
863 if (! xcoff_get_section_contents (abfd, lsec))
864 return FALSE;
865 contents = coff_section_data (abfd, lsec)->contents;
867 /* Remove the sections from this object, so that they do not get
868 included in the link. */
869 bfd_section_list_clear (abfd);
871 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
873 strings = (char *) contents + ldhdr.l_stoff;
875 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
877 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
879 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
881 struct internal_ldsym ldsym;
882 char nambuf[SYMNMLEN + 1];
883 const char *name;
884 struct xcoff_link_hash_entry *h;
886 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
888 /* We are only interested in exported symbols. */
889 if ((ldsym.l_smtype & L_EXPORT) == 0)
890 continue;
892 if (ldsym._l._l_l._l_zeroes == 0)
893 name = strings + ldsym._l._l_l._l_offset;
894 else
896 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
897 nambuf[SYMNMLEN] = '\0';
898 name = nambuf;
901 /* Normally we could not call xcoff_link_hash_lookup in an add
902 symbols routine, since we might not be using an XCOFF hash
903 table. However, we verified above that we are using an XCOFF
904 hash table. */
906 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
907 TRUE, TRUE);
908 if (h == NULL)
909 return FALSE;
911 if (!xcoff_dynamic_definition_p (h, &ldsym))
912 continue;
914 h->flags |= XCOFF_DEF_DYNAMIC;
915 h->smclas = ldsym.l_smclas;
916 if (h->smclas == XMC_XO)
918 /* This symbol has an absolute value. */
919 if ((ldsym.l_smtype & L_WEAK) != 0)
920 h->root.type = bfd_link_hash_defweak;
921 else
922 h->root.type = bfd_link_hash_defined;
923 h->root.u.def.section = bfd_abs_section_ptr;
924 h->root.u.def.value = ldsym.l_value;
926 else
928 /* Otherwise, we don't bother to actually define the symbol,
929 since we don't have a section to put it in anyhow.
930 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
931 should be imported from the symbol's undef.abfd. */
932 if ((ldsym.l_smtype & L_WEAK) != 0)
933 h->root.type = bfd_link_hash_undefweak;
934 else
935 h->root.type = bfd_link_hash_undefined;
936 h->root.u.undef.abfd = abfd;
939 /* If this symbol defines a function descriptor, then it
940 implicitly defines the function code as well. */
941 if (h->smclas == XMC_DS
942 || (h->smclas == XMC_XO && name[0] != '.'))
943 h->flags |= XCOFF_DESCRIPTOR;
944 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
946 struct xcoff_link_hash_entry *hds;
948 hds = h->descriptor;
949 if (hds == NULL)
951 char *dsnm;
953 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
954 if (dsnm == NULL)
955 return FALSE;
956 dsnm[0] = '.';
957 strcpy (dsnm + 1, name);
958 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
959 TRUE, TRUE, TRUE);
960 free (dsnm);
961 if (hds == NULL)
962 return FALSE;
964 hds->descriptor = h;
965 h->descriptor = hds;
968 if (xcoff_dynamic_definition_p (hds, &ldsym))
970 hds->root.type = h->root.type;
971 hds->flags |= XCOFF_DEF_DYNAMIC;
972 if (h->smclas == XMC_XO)
974 /* An absolute symbol appears to actually define code, not a
975 function descriptor. This is how some math functions are
976 implemented on AIX 4.1. */
977 hds->smclas = XMC_XO;
978 hds->root.u.def.section = bfd_abs_section_ptr;
979 hds->root.u.def.value = ldsym.l_value;
981 else
983 hds->smclas = XMC_PR;
984 hds->root.u.undef.abfd = abfd;
985 /* We do not want to add this to the undefined
986 symbol list. */
992 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
994 free (coff_section_data (abfd, lsec)->contents);
995 coff_section_data (abfd, lsec)->contents = NULL;
998 /* Record this file in the import files. */
999 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
1000 if (n == NULL)
1001 return FALSE;
1002 n->next = NULL;
1004 if (abfd->my_archive == NULL)
1006 if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
1007 &n->path, &n->file))
1008 return FALSE;
1009 n->member = "";
1011 else
1013 struct xcoff_archive_info *archive_info;
1015 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1016 if (!archive_info->impfile)
1018 if (!bfd_xcoff_split_import_path (archive_info->archive,
1019 archive_info->archive->filename,
1020 &archive_info->imppath,
1021 &archive_info->impfile))
1022 return FALSE;
1024 n->path = archive_info->imppath;
1025 n->file = archive_info->impfile;
1026 n->member = bfd_get_filename (abfd);
1029 /* We start c at 1 because the first import file number is reserved
1030 for LIBPATH. */
1031 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1032 *pp != NULL;
1033 pp = &(*pp)->next, ++c)
1035 *pp = n;
1037 xcoff_data (abfd)->import_file_id = c;
1039 return TRUE;
1042 /* xcoff_link_create_extra_sections
1044 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1046 static bfd_boolean
1047 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1049 bfd_boolean return_value = FALSE;
1051 if (info->output_bfd->xvec == abfd->xvec)
1053 /* We need to build a .loader section, so we do it here. This
1054 won't work if we're producing an XCOFF output file with no
1055 XCOFF input files. FIXME. */
1057 if (!info->relocatable
1058 && xcoff_hash_table (info)->loader_section == NULL)
1060 asection *lsec;
1061 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1063 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1064 if (lsec == NULL)
1065 goto end_return;
1067 xcoff_hash_table (info)->loader_section = lsec;
1070 /* Likewise for the linkage section. */
1071 if (xcoff_hash_table (info)->linkage_section == NULL)
1073 asection *lsec;
1074 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1075 | SEC_IN_MEMORY);
1077 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1078 if (lsec == NULL)
1079 goto end_return;
1081 xcoff_hash_table (info)->linkage_section = lsec;
1082 lsec->alignment_power = 2;
1085 /* Likewise for the TOC section. */
1086 if (xcoff_hash_table (info)->toc_section == NULL)
1088 asection *tsec;
1089 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1090 | SEC_IN_MEMORY);
1092 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1093 if (tsec == NULL)
1094 goto end_return;
1096 xcoff_hash_table (info)->toc_section = tsec;
1097 tsec->alignment_power = 2;
1100 /* Likewise for the descriptor section. */
1101 if (xcoff_hash_table (info)->descriptor_section == NULL)
1103 asection *dsec;
1104 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1105 | SEC_IN_MEMORY);
1107 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1108 if (dsec == NULL)
1109 goto end_return;
1111 xcoff_hash_table (info)->descriptor_section = dsec;
1112 dsec->alignment_power = 2;
1115 /* Likewise for the .debug section. */
1116 if (xcoff_hash_table (info)->debug_section == NULL
1117 && info->strip != strip_all)
1119 asection *dsec;
1120 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1122 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1123 if (dsec == NULL)
1124 goto end_return;
1126 xcoff_hash_table (info)->debug_section = dsec;
1130 return_value = TRUE;
1132 end_return:
1134 return return_value;
1137 /* Returns the index of reloc in RELOCS with the least address greater
1138 than or equal to ADDRESS. The relocs are sorted by address. */
1140 static bfd_size_type
1141 xcoff_find_reloc (struct internal_reloc *relocs,
1142 bfd_size_type count,
1143 bfd_vma address)
1145 bfd_size_type min, max, this;
1147 if (count < 2)
1149 if (count == 1 && relocs[0].r_vaddr < address)
1150 return 1;
1151 else
1152 return 0;
1155 min = 0;
1156 max = count;
1158 /* Do a binary search over (min,max]. */
1159 while (min + 1 < max)
1161 bfd_vma raddr;
1163 this = (max + min) / 2;
1164 raddr = relocs[this].r_vaddr;
1165 if (raddr > address)
1166 max = this;
1167 else if (raddr < address)
1168 min = this;
1169 else
1171 min = this;
1172 break;
1176 if (relocs[min].r_vaddr < address)
1177 return min + 1;
1179 while (min > 0
1180 && relocs[min - 1].r_vaddr == address)
1181 --min;
1183 return min;
1186 /* Add all the symbols from an object file to the hash table.
1188 XCOFF is a weird format. A normal XCOFF .o files will have three
1189 COFF sections--.text, .data, and .bss--but each COFF section will
1190 contain many csects. These csects are described in the symbol
1191 table. From the linker's point of view, each csect must be
1192 considered a section in its own right. For example, a TOC entry is
1193 handled as a small XMC_TC csect. The linker must be able to merge
1194 different TOC entries together, which means that it must be able to
1195 extract the XMC_TC csects from the .data section of the input .o
1196 file.
1198 From the point of view of our linker, this is, of course, a hideous
1199 nightmare. We cope by actually creating sections for each csect,
1200 and discarding the original sections. We then have to handle the
1201 relocation entries carefully, since the only way to tell which
1202 csect they belong to is to examine the address. */
1204 static bfd_boolean
1205 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1207 unsigned int n_tmask;
1208 unsigned int n_btshft;
1209 bfd_boolean default_copy;
1210 bfd_size_type symcount;
1211 struct xcoff_link_hash_entry **sym_hash;
1212 asection **csect_cache;
1213 unsigned int *lineno_counts;
1214 bfd_size_type linesz;
1215 asection *o;
1216 asection *last_real;
1217 bfd_boolean keep_syms;
1218 asection *csect;
1219 unsigned int csect_index;
1220 asection *first_csect;
1221 bfd_size_type symesz;
1222 bfd_byte *esym;
1223 bfd_byte *esym_end;
1224 struct reloc_info_struct
1226 struct internal_reloc *relocs;
1227 asection **csects;
1228 bfd_byte *linenos;
1229 } *reloc_info = NULL;
1230 bfd_size_type amt;
1232 keep_syms = obj_coff_keep_syms (abfd);
1234 if ((abfd->flags & DYNAMIC) != 0
1235 && ! info->static_link)
1237 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1238 return FALSE;
1241 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1242 if (! xcoff_link_create_extra_sections (abfd, info))
1243 goto error_return;
1245 if ((abfd->flags & DYNAMIC) != 0
1246 && ! info->static_link)
1247 return TRUE;
1249 n_tmask = coff_data (abfd)->local_n_tmask;
1250 n_btshft = coff_data (abfd)->local_n_btshft;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1256 if (info->keep_memory)
1257 default_copy = FALSE;
1258 else
1259 default_copy = TRUE;
1261 symcount = obj_raw_syment_count (abfd);
1263 /* We keep a list of the linker hash table entries that correspond
1264 to each external symbol. */
1265 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1266 sym_hash = bfd_zalloc (abfd, amt);
1267 if (sym_hash == NULL && symcount != 0)
1268 goto error_return;
1269 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1271 /* Because of the weird stuff we are doing with XCOFF csects, we can
1272 not easily determine which section a symbol is in, so we store
1273 the information in the tdata for the input file. */
1274 amt = symcount * sizeof (asection *);
1275 csect_cache = bfd_zalloc (abfd, amt);
1276 if (csect_cache == NULL && symcount != 0)
1277 goto error_return;
1278 xcoff_data (abfd)->csects = csect_cache;
1280 /* We garbage-collect line-number information on a symbol-by-symbol
1281 basis, so we need to have quick access to the number of entries
1282 per symbol. */
1283 amt = symcount * sizeof (unsigned int);
1284 lineno_counts = bfd_zalloc (abfd, amt);
1285 if (lineno_counts == NULL && symcount != 0)
1286 goto error_return;
1287 xcoff_data (abfd)->lineno_counts = lineno_counts;
1289 /* While splitting sections into csects, we need to assign the
1290 relocs correctly. The relocs and the csects must both be in
1291 order by VMA within a given section, so we handle this by
1292 scanning along the relocs as we process the csects. We index
1293 into reloc_info using the section target_index. */
1294 amt = abfd->section_count + 1;
1295 amt *= sizeof (struct reloc_info_struct);
1296 reloc_info = bfd_zmalloc (amt);
1297 if (reloc_info == NULL)
1298 goto error_return;
1300 /* Read in the relocs and line numbers for each section. */
1301 linesz = bfd_coff_linesz (abfd);
1302 last_real = NULL;
1303 for (o = abfd->sections; o != NULL; o = o->next)
1305 last_real = o;
1307 if ((o->flags & SEC_RELOC) != 0)
1309 reloc_info[o->target_index].relocs =
1310 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1311 amt = o->reloc_count;
1312 amt *= sizeof (asection *);
1313 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1314 if (reloc_info[o->target_index].csects == NULL)
1315 goto error_return;
1318 if ((info->strip == strip_none || info->strip == strip_some)
1319 && o->lineno_count > 0)
1321 bfd_byte *linenos;
1323 amt = linesz * o->lineno_count;
1324 linenos = bfd_malloc (amt);
1325 if (linenos == NULL)
1326 goto error_return;
1327 reloc_info[o->target_index].linenos = linenos;
1328 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1329 || bfd_bread (linenos, amt, abfd) != amt)
1330 goto error_return;
1334 /* Don't let the linker relocation routines discard the symbols. */
1335 obj_coff_keep_syms (abfd) = TRUE;
1337 csect = NULL;
1338 csect_index = 0;
1339 first_csect = NULL;
1341 symesz = bfd_coff_symesz (abfd);
1342 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1343 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1344 esym_end = esym + symcount * symesz;
1346 while (esym < esym_end)
1348 struct internal_syment sym;
1349 union internal_auxent aux;
1350 const char *name;
1351 char buf[SYMNMLEN + 1];
1352 int smtyp;
1353 asection *section;
1354 bfd_vma value;
1355 struct xcoff_link_hash_entry *set_toc;
1357 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1359 /* In this pass we are only interested in symbols with csect
1360 information. */
1361 if (!CSECT_SYM_P (sym.n_sclass))
1363 /* Set csect_cache,
1364 Normally csect is a .pr, .rw etc. created in the loop
1365 If C_FILE or first time, handle special
1367 Advance esym, sym_hash, csect_hash ptrs. */
1368 if (sym.n_sclass == C_FILE)
1369 csect = NULL;
1370 if (csect != NULL)
1371 *csect_cache = csect;
1372 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1373 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1374 else
1375 *csect_cache = NULL;
1376 esym += (sym.n_numaux + 1) * symesz;
1377 sym_hash += sym.n_numaux + 1;
1378 csect_cache += sym.n_numaux + 1;
1379 lineno_counts += sym.n_numaux + 1;
1381 continue;
1384 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1386 if (name == NULL)
1387 goto error_return;
1389 /* If this symbol has line number information attached to it,
1390 and we're not stripping it, count the number of entries and
1391 add them to the count for this csect. In the final link pass
1392 we are going to attach line number information by symbol,
1393 rather than by section, in order to more easily handle
1394 garbage collection. */
1395 if ((info->strip == strip_none || info->strip == strip_some)
1396 && sym.n_numaux > 1
1397 && csect != NULL
1398 && ISFCN (sym.n_type))
1400 union internal_auxent auxlin;
1402 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1403 sym.n_type, sym.n_sclass,
1404 0, sym.n_numaux, (void *) &auxlin);
1406 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1408 asection *enclosing;
1409 bfd_signed_vma linoff;
1411 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1412 if (enclosing == NULL)
1414 (*_bfd_error_handler)
1415 (_("%B: `%s' has line numbers but no enclosing section"),
1416 abfd, name);
1417 bfd_set_error (bfd_error_bad_value);
1418 goto error_return;
1420 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1421 - enclosing->line_filepos);
1422 /* Explicit cast to bfd_signed_vma for compiler. */
1423 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1425 struct internal_lineno lin;
1426 bfd_byte *linpstart;
1428 linpstart = (reloc_info[enclosing->target_index].linenos
1429 + linoff);
1430 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1431 if (lin.l_lnno == 0
1432 && ((bfd_size_type) lin.l_addr.l_symndx
1433 == ((esym
1434 - (bfd_byte *) obj_coff_external_syms (abfd))
1435 / symesz)))
1437 bfd_byte *linpend, *linp;
1439 linpend = (reloc_info[enclosing->target_index].linenos
1440 + enclosing->lineno_count * linesz);
1441 for (linp = linpstart + linesz;
1442 linp < linpend;
1443 linp += linesz)
1445 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1446 (void *) &lin);
1447 if (lin.l_lnno == 0)
1448 break;
1450 *lineno_counts = (linp - linpstart) / linesz;
1451 /* The setting of line_filepos will only be
1452 useful if all the line number entries for a
1453 csect are contiguous; this only matters for
1454 error reporting. */
1455 if (csect->line_filepos == 0)
1456 csect->line_filepos =
1457 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1463 /* Pick up the csect auxiliary information. */
1464 if (sym.n_numaux == 0)
1466 (*_bfd_error_handler)
1467 (_("%B: class %d symbol `%s' has no aux entries"),
1468 abfd, sym.n_sclass, name);
1469 bfd_set_error (bfd_error_bad_value);
1470 goto error_return;
1473 bfd_coff_swap_aux_in (abfd,
1474 (void *) (esym + symesz * sym.n_numaux),
1475 sym.n_type, sym.n_sclass,
1476 sym.n_numaux - 1, sym.n_numaux,
1477 (void *) &aux);
1479 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1481 section = NULL;
1482 value = 0;
1483 set_toc = NULL;
1485 switch (smtyp)
1487 default:
1488 (*_bfd_error_handler)
1489 (_("%B: symbol `%s' has unrecognized csect type %d"),
1490 abfd, name, smtyp);
1491 bfd_set_error (bfd_error_bad_value);
1492 goto error_return;
1494 case XTY_ER:
1495 /* This is an external reference. */
1496 if (sym.n_sclass == C_HIDEXT
1497 || sym.n_scnum != N_UNDEF
1498 || aux.x_csect.x_scnlen.l != 0)
1500 (*_bfd_error_handler)
1501 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1502 abfd, name, sym.n_sclass, sym.n_scnum,
1503 aux.x_csect.x_scnlen.l);
1504 bfd_set_error (bfd_error_bad_value);
1505 goto error_return;
1508 /* An XMC_XO external reference is actually a reference to
1509 an absolute location. */
1510 if (aux.x_csect.x_smclas != XMC_XO)
1511 section = bfd_und_section_ptr;
1512 else
1514 section = bfd_abs_section_ptr;
1515 value = sym.n_value;
1517 break;
1519 case XTY_SD:
1520 csect = NULL;
1521 csect_index = -(unsigned) 1;
1523 /* When we see a TOC anchor, we record the TOC value. */
1524 if (aux.x_csect.x_smclas == XMC_TC0)
1526 if (sym.n_sclass != C_HIDEXT
1527 || aux.x_csect.x_scnlen.l != 0)
1529 (*_bfd_error_handler)
1530 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1531 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1532 bfd_set_error (bfd_error_bad_value);
1533 goto error_return;
1535 xcoff_data (abfd)->toc = sym.n_value;
1538 /* We must merge TOC entries for the same symbol. We can
1539 merge two TOC entries if they are both C_HIDEXT, they
1540 both have the same name, they are both 4 or 8 bytes long, and
1541 they both have a relocation table entry for an external
1542 symbol with the same name. Unfortunately, this means
1543 that we must look through the relocations. Ick.
1545 Logic for 32 bit vs 64 bit.
1546 32 bit has a csect length of 4 for TOC
1547 64 bit has a csect length of 8 for TOC
1549 The conditions to get past the if-check are not that bad.
1550 They are what is used to create the TOC csects in the first
1551 place. */
1552 if (aux.x_csect.x_smclas == XMC_TC
1553 && sym.n_sclass == C_HIDEXT
1554 && info->output_bfd->xvec == abfd->xvec
1555 && ((bfd_xcoff_is_xcoff32 (abfd)
1556 && aux.x_csect.x_scnlen.l == 4)
1557 || (bfd_xcoff_is_xcoff64 (abfd)
1558 && aux.x_csect.x_scnlen.l == 8)))
1560 asection *enclosing;
1561 struct internal_reloc *relocs;
1562 bfd_size_type relindx;
1563 struct internal_reloc *rel;
1565 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1566 if (enclosing == NULL)
1567 goto error_return;
1569 relocs = reloc_info[enclosing->target_index].relocs;
1570 amt = enclosing->reloc_count;
1571 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1572 rel = relocs + relindx;
1574 /* 32 bit R_POS r_size is 31
1575 64 bit R_POS r_size is 63 */
1576 if (relindx < enclosing->reloc_count
1577 && rel->r_vaddr == (bfd_vma) sym.n_value
1578 && rel->r_type == R_POS
1579 && ((bfd_xcoff_is_xcoff32 (abfd)
1580 && rel->r_size == 31)
1581 || (bfd_xcoff_is_xcoff64 (abfd)
1582 && rel->r_size == 63)))
1584 bfd_byte *erelsym;
1586 struct internal_syment relsym;
1588 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1589 + rel->r_symndx * symesz);
1590 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1591 if (EXTERN_SYM_P (relsym.n_sclass))
1593 const char *relname;
1594 char relbuf[SYMNMLEN + 1];
1595 bfd_boolean copy;
1596 struct xcoff_link_hash_entry *h;
1598 /* At this point we know that the TOC entry is
1599 for an externally visible symbol. */
1600 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1601 relbuf);
1602 if (relname == NULL)
1603 goto error_return;
1605 /* We only merge TOC entries if the TC name is
1606 the same as the symbol name. This handles
1607 the normal case, but not common cases like
1608 SYM.P4 which gcc generates to store SYM + 4
1609 in the TOC. FIXME. */
1610 if (strcmp (name, relname) == 0)
1612 copy = (! info->keep_memory
1613 || relsym._n._n_n._n_zeroes != 0
1614 || relsym._n._n_n._n_offset == 0);
1615 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1616 relname, TRUE, copy,
1617 FALSE);
1618 if (h == NULL)
1619 goto error_return;
1621 /* At this point h->root.type could be
1622 bfd_link_hash_new. That should be OK,
1623 since we know for sure that we will come
1624 across this symbol as we step through the
1625 file. */
1627 /* We store h in *sym_hash for the
1628 convenience of the relocate_section
1629 function. */
1630 *sym_hash = h;
1632 if (h->toc_section != NULL)
1634 asection **rel_csects;
1636 /* We already have a TOC entry for this
1637 symbol, so we can just ignore this
1638 one. */
1639 rel_csects =
1640 reloc_info[enclosing->target_index].csects;
1641 rel_csects[relindx] = bfd_und_section_ptr;
1642 break;
1645 /* We are about to create a TOC entry for
1646 this symbol. */
1647 set_toc = h;
1654 asection *enclosing;
1656 /* We need to create a new section. We get the name from
1657 the csect storage mapping class, so that the linker can
1658 accumulate similar csects together. */
1660 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1661 if (NULL == csect)
1662 goto error_return;
1664 /* The enclosing section is the main section : .data, .text
1665 or .bss that the csect is coming from. */
1666 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1667 if (enclosing == NULL)
1668 goto error_return;
1670 if (! bfd_is_abs_section (enclosing)
1671 && ((bfd_vma) sym.n_value < enclosing->vma
1672 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1673 > enclosing->vma + enclosing->size)))
1675 (*_bfd_error_handler)
1676 (_("%B: csect `%s' not in enclosing section"),
1677 abfd, name);
1678 bfd_set_error (bfd_error_bad_value);
1679 goto error_return;
1681 csect->vma = sym.n_value;
1682 csect->filepos = (enclosing->filepos
1683 + sym.n_value
1684 - enclosing->vma);
1685 csect->size = aux.x_csect.x_scnlen.l;
1686 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1687 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1689 /* Record the enclosing section in the tdata for this new
1690 section. */
1691 amt = sizeof (struct coff_section_tdata);
1692 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1693 if (csect->used_by_bfd == NULL)
1694 goto error_return;
1695 amt = sizeof (struct xcoff_section_tdata);
1696 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1697 if (coff_section_data (abfd, csect)->tdata == NULL)
1698 goto error_return;
1699 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1700 xcoff_section_data (abfd, csect)->lineno_count =
1701 enclosing->lineno_count;
1703 if (enclosing->owner == abfd)
1705 struct internal_reloc *relocs;
1706 bfd_size_type relindx;
1707 struct internal_reloc *rel;
1708 asection **rel_csect;
1710 relocs = reloc_info[enclosing->target_index].relocs;
1711 amt = enclosing->reloc_count;
1712 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1714 rel = relocs + relindx;
1715 rel_csect = (reloc_info[enclosing->target_index].csects
1716 + relindx);
1718 csect->rel_filepos = (enclosing->rel_filepos
1719 + relindx * bfd_coff_relsz (abfd));
1720 while (relindx < enclosing->reloc_count
1721 && *rel_csect == NULL
1722 && rel->r_vaddr < csect->vma + csect->size)
1725 *rel_csect = csect;
1726 csect->flags |= SEC_RELOC;
1727 ++csect->reloc_count;
1728 ++relindx;
1729 ++rel;
1730 ++rel_csect;
1734 /* There are a number of other fields and section flags
1735 which we do not bother to set. */
1737 csect_index = ((esym
1738 - (bfd_byte *) obj_coff_external_syms (abfd))
1739 / symesz);
1741 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1743 if (first_csect == NULL)
1744 first_csect = csect;
1746 /* If this symbol is external, we treat it as starting at the
1747 beginning of the newly created section. */
1748 if (EXTERN_SYM_P (sym.n_sclass))
1750 section = csect;
1751 value = 0;
1754 /* If this is a TOC section for a symbol, record it. */
1755 if (set_toc != NULL)
1756 set_toc->toc_section = csect;
1758 break;
1760 case XTY_LD:
1761 /* This is a label definition. The x_scnlen field is the
1762 symbol index of the csect. Usually the XTY_LD symbol will
1763 follow its appropriate XTY_SD symbol. The .set pseudo op can
1764 cause the XTY_LD to not follow the XTY_SD symbol. */
1766 bfd_boolean bad;
1768 bad = FALSE;
1769 if (aux.x_csect.x_scnlen.l < 0
1770 || (aux.x_csect.x_scnlen.l
1771 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1772 bad = TRUE;
1773 if (! bad)
1775 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1776 if (section == NULL
1777 || (section->flags & SEC_HAS_CONTENTS) == 0)
1778 bad = TRUE;
1780 if (bad)
1782 (*_bfd_error_handler)
1783 (_("%B: misplaced XTY_LD `%s'"),
1784 abfd, name);
1785 bfd_set_error (bfd_error_bad_value);
1786 goto error_return;
1788 csect = section;
1789 value = sym.n_value - csect->vma;
1791 break;
1793 case XTY_CM:
1794 /* This is an unitialized csect. We could base the name on
1795 the storage mapping class, but we don't bother except for
1796 an XMC_TD symbol. If this csect is externally visible,
1797 it is a common symbol. We put XMC_TD symbols in sections
1798 named .tocbss, and rely on the linker script to put that
1799 in the TOC area. */
1801 if (aux.x_csect.x_smclas == XMC_TD)
1803 /* The linker script puts the .td section in the data
1804 section after the .tc section. */
1805 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1806 SEC_ALLOC);
1808 else
1809 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1810 SEC_ALLOC);
1812 if (csect == NULL)
1813 goto error_return;
1814 csect->vma = sym.n_value;
1815 csect->size = aux.x_csect.x_scnlen.l;
1816 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1817 /* There are a number of other fields and section flags
1818 which we do not bother to set. */
1820 csect_index = ((esym
1821 - (bfd_byte *) obj_coff_external_syms (abfd))
1822 / symesz);
1824 amt = sizeof (struct coff_section_tdata);
1825 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1826 if (csect->used_by_bfd == NULL)
1827 goto error_return;
1828 amt = sizeof (struct xcoff_section_tdata);
1829 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1830 if (coff_section_data (abfd, csect)->tdata == NULL)
1831 goto error_return;
1832 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1834 if (first_csect == NULL)
1835 first_csect = csect;
1837 if (EXTERN_SYM_P (sym.n_sclass))
1839 csect->flags |= SEC_IS_COMMON;
1840 csect->size = 0;
1841 section = csect;
1842 value = aux.x_csect.x_scnlen.l;
1845 break;
1848 /* Check for magic symbol names. */
1849 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1850 && aux.x_csect.x_smclas != XMC_TC
1851 && aux.x_csect.x_smclas != XMC_TD)
1853 int i = -1;
1855 if (name[0] == '_')
1857 if (strcmp (name, "_text") == 0)
1858 i = XCOFF_SPECIAL_SECTION_TEXT;
1859 else if (strcmp (name, "_etext") == 0)
1860 i = XCOFF_SPECIAL_SECTION_ETEXT;
1861 else if (strcmp (name, "_data") == 0)
1862 i = XCOFF_SPECIAL_SECTION_DATA;
1863 else if (strcmp (name, "_edata") == 0)
1864 i = XCOFF_SPECIAL_SECTION_EDATA;
1865 else if (strcmp (name, "_end") == 0)
1866 i = XCOFF_SPECIAL_SECTION_END;
1868 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1869 i = XCOFF_SPECIAL_SECTION_END2;
1871 if (i != -1)
1872 xcoff_hash_table (info)->special_sections[i] = csect;
1875 /* Now we have enough information to add the symbol to the
1876 linker hash table. */
1878 if (EXTERN_SYM_P (sym.n_sclass))
1880 bfd_boolean copy;
1881 flagword flags;
1883 BFD_ASSERT (section != NULL);
1885 /* We must copy the name into memory if we got it from the
1886 syment itself, rather than the string table. */
1887 copy = default_copy;
1888 if (sym._n._n_n._n_zeroes != 0
1889 || sym._n._n_n._n_offset == 0)
1890 copy = TRUE;
1892 /* Ignore global linkage code when linking statically. */
1893 if (info->static_link
1894 && (smtyp == XTY_SD || smtyp == XTY_LD)
1895 && aux.x_csect.x_smclas == XMC_GL)
1897 section = bfd_und_section_ptr;
1898 value = 0;
1901 /* The AIX linker appears to only detect multiple symbol
1902 definitions when there is a reference to the symbol. If
1903 a symbol is defined multiple times, and the only
1904 references are from the same object file, the AIX linker
1905 appears to permit it. It does not merge the different
1906 definitions, but handles them independently. On the
1907 other hand, if there is a reference, the linker reports
1908 an error.
1910 This matters because the AIX <net/net_globals.h> header
1911 file actually defines an initialized array, so we have to
1912 actually permit that to work.
1914 Just to make matters even more confusing, the AIX linker
1915 appears to permit multiple symbol definitions whenever
1916 the second definition is in an archive rather than an
1917 object file. This may be a consequence of the manner in
1918 which it handles archives: I think it may load the entire
1919 archive in as separate csects, and then let garbage
1920 collection discard symbols.
1922 We also have to handle the case of statically linking a
1923 shared object, which will cause symbol redefinitions,
1924 although this is an easier case to detect. */
1925 else if (info->output_bfd->xvec == abfd->xvec)
1927 if (! bfd_is_und_section (section))
1928 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1929 name, TRUE, copy, FALSE);
1930 else
1931 /* Make a copy of the symbol name to prevent problems with
1932 merging symbols. */
1933 *sym_hash = ((struct xcoff_link_hash_entry *)
1934 bfd_wrapped_link_hash_lookup (abfd, info, name,
1935 TRUE, TRUE, FALSE));
1937 if (*sym_hash == NULL)
1938 goto error_return;
1939 if (((*sym_hash)->root.type == bfd_link_hash_defined
1940 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1941 && ! bfd_is_und_section (section)
1942 && ! bfd_is_com_section (section))
1944 /* This is a second definition of a defined symbol. */
1945 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1946 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1948 /* The existing symbol is from a shared library.
1949 Replace it. */
1950 (*sym_hash)->root.type = bfd_link_hash_undefined;
1951 (*sym_hash)->root.u.undef.abfd =
1952 (*sym_hash)->root.u.def.section->owner;
1954 else if (abfd->my_archive != NULL)
1956 /* This is a redefinition in an object contained
1957 in an archive. Just ignore it. See the
1958 comment above. */
1959 section = bfd_und_section_ptr;
1960 value = 0;
1962 else if (sym.n_sclass == C_AIX_WEAKEXT
1963 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1965 /* At least one of the definitions is weak.
1966 Allow the normal rules to take effect. */
1968 else if ((*sym_hash)->root.u.undef.next != NULL
1969 || info->hash->undefs_tail == &(*sym_hash)->root)
1971 /* This symbol has been referenced. In this
1972 case, we just continue and permit the
1973 multiple definition error. See the comment
1974 above about the behaviour of the AIX linker. */
1976 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1978 /* The symbols are both csects of the same
1979 class. There is at least a chance that this
1980 is a semi-legitimate redefinition. */
1981 section = bfd_und_section_ptr;
1982 value = 0;
1983 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1986 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1987 && (*sym_hash)->root.type == bfd_link_hash_defined
1988 && (bfd_is_und_section (section)
1989 || bfd_is_com_section (section)))
1991 /* This is a reference to a multiply defined symbol.
1992 Report the error now. See the comment above
1993 about the behaviour of the AIX linker. We could
1994 also do this with warning symbols, but I'm not
1995 sure the XCOFF linker is wholly prepared to
1996 handle them, and that would only be a warning,
1997 not an error. */
1998 if (! ((*info->callbacks->multiple_definition)
1999 (info, (*sym_hash)->root.root.string,
2000 NULL, NULL, (bfd_vma) 0,
2001 (*sym_hash)->root.u.def.section->owner,
2002 (*sym_hash)->root.u.def.section,
2003 (*sym_hash)->root.u.def.value)))
2004 goto error_return;
2005 /* Try not to give this error too many times. */
2006 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2010 /* _bfd_generic_link_add_one_symbol may call the linker to
2011 generate an error message, and the linker may try to read
2012 the symbol table to give a good error. Right now, the
2013 line numbers are in an inconsistent state, since they are
2014 counted both in the real sections and in the new csects.
2015 We need to leave the count in the real sections so that
2016 the linker can report the line number of the error
2017 correctly, so temporarily clobber the link to the csects
2018 so that the linker will not try to read the line numbers
2019 a second time from the csects. */
2020 BFD_ASSERT (last_real->next == first_csect);
2021 last_real->next = NULL;
2022 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2023 if (! (_bfd_generic_link_add_one_symbol
2024 (info, abfd, name, flags, section, value,
2025 NULL, copy, TRUE,
2026 (struct bfd_link_hash_entry **) sym_hash)))
2027 goto error_return;
2028 last_real->next = first_csect;
2030 if (smtyp == XTY_CM)
2032 if ((*sym_hash)->root.type != bfd_link_hash_common
2033 || (*sym_hash)->root.u.c.p->section != csect)
2034 /* We don't need the common csect we just created. */
2035 csect->size = 0;
2036 else
2037 (*sym_hash)->root.u.c.p->alignment_power
2038 = csect->alignment_power;
2041 if (info->output_bfd->xvec == abfd->xvec)
2043 int flag;
2045 if (smtyp == XTY_ER
2046 || smtyp == XTY_CM
2047 || section == bfd_und_section_ptr)
2048 flag = XCOFF_REF_REGULAR;
2049 else
2050 flag = XCOFF_DEF_REGULAR;
2051 (*sym_hash)->flags |= flag;
2053 if ((*sym_hash)->smclas == XMC_UA
2054 || flag == XCOFF_DEF_REGULAR)
2055 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2059 if (smtyp == XTY_ER)
2060 *csect_cache = section;
2061 else
2063 *csect_cache = csect;
2064 if (csect != NULL)
2065 xcoff_section_data (abfd, csect)->last_symndx
2066 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2069 esym += (sym.n_numaux + 1) * symesz;
2070 sym_hash += sym.n_numaux + 1;
2071 csect_cache += sym.n_numaux + 1;
2072 lineno_counts += sym.n_numaux + 1;
2075 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2077 /* Make sure that we have seen all the relocs. */
2078 for (o = abfd->sections; o != first_csect; o = o->next)
2080 /* Reset the section size and the line number count, since the
2081 data is now attached to the csects. Don't reset the size of
2082 the .debug section, since we need to read it below in
2083 bfd_xcoff_size_dynamic_sections. */
2084 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2085 o->size = 0;
2086 o->lineno_count = 0;
2088 if ((o->flags & SEC_RELOC) != 0)
2090 bfd_size_type i;
2091 struct internal_reloc *rel;
2092 asection **rel_csect;
2094 rel = reloc_info[o->target_index].relocs;
2095 rel_csect = reloc_info[o->target_index].csects;
2097 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2099 if (*rel_csect == NULL)
2101 (*_bfd_error_handler)
2102 (_("%B: reloc %s:%d not in csect"),
2103 abfd, o->name, i);
2104 bfd_set_error (bfd_error_bad_value);
2105 goto error_return;
2108 /* We identify all function symbols that are the target
2109 of a relocation, so that we can create glue code for
2110 functions imported from dynamic objects. */
2111 if (info->output_bfd->xvec == abfd->xvec
2112 && *rel_csect != bfd_und_section_ptr
2113 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2115 struct xcoff_link_hash_entry *h;
2117 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2118 /* If the symbol name starts with a period, it is
2119 the code of a function. If the symbol is
2120 currently undefined, then add an undefined symbol
2121 for the function descriptor. This should do no
2122 harm, because any regular object that defines the
2123 function should also define the function
2124 descriptor. It helps, because it means that we
2125 will identify the function descriptor with a
2126 dynamic object if a dynamic object defines it. */
2127 if (h->root.root.string[0] == '.'
2128 && h->descriptor == NULL)
2130 struct xcoff_link_hash_entry *hds;
2131 struct bfd_link_hash_entry *bh;
2133 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2134 h->root.root.string + 1,
2135 TRUE, FALSE, TRUE);
2136 if (hds == NULL)
2137 goto error_return;
2138 if (hds->root.type == bfd_link_hash_new)
2140 bh = &hds->root;
2141 if (! (_bfd_generic_link_add_one_symbol
2142 (info, abfd, hds->root.root.string,
2143 (flagword) 0, bfd_und_section_ptr,
2144 (bfd_vma) 0, NULL, FALSE,
2145 TRUE, &bh)))
2146 goto error_return;
2147 hds = (struct xcoff_link_hash_entry *) bh;
2149 hds->flags |= XCOFF_DESCRIPTOR;
2150 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2151 hds->descriptor = h;
2152 h->descriptor = hds;
2154 if (h->root.root.string[0] == '.')
2155 h->flags |= XCOFF_CALLED;
2159 free (reloc_info[o->target_index].csects);
2160 reloc_info[o->target_index].csects = NULL;
2162 /* Reset SEC_RELOC and the reloc_count, since the reloc
2163 information is now attached to the csects. */
2164 o->flags &=~ SEC_RELOC;
2165 o->reloc_count = 0;
2167 /* If we are not keeping memory, free the reloc information. */
2168 if (! info->keep_memory
2169 && coff_section_data (abfd, o) != NULL
2170 && coff_section_data (abfd, o)->relocs != NULL
2171 && ! coff_section_data (abfd, o)->keep_relocs)
2173 free (coff_section_data (abfd, o)->relocs);
2174 coff_section_data (abfd, o)->relocs = NULL;
2178 /* Free up the line numbers. FIXME: We could cache these
2179 somewhere for the final link, to avoid reading them again. */
2180 if (reloc_info[o->target_index].linenos != NULL)
2182 free (reloc_info[o->target_index].linenos);
2183 reloc_info[o->target_index].linenos = NULL;
2187 free (reloc_info);
2189 obj_coff_keep_syms (abfd) = keep_syms;
2191 return TRUE;
2193 error_return:
2194 if (reloc_info != NULL)
2196 for (o = abfd->sections; o != NULL; o = o->next)
2198 if (reloc_info[o->target_index].csects != NULL)
2199 free (reloc_info[o->target_index].csects);
2200 if (reloc_info[o->target_index].linenos != NULL)
2201 free (reloc_info[o->target_index].linenos);
2203 free (reloc_info);
2205 obj_coff_keep_syms (abfd) = keep_syms;
2206 return FALSE;
2209 #undef N_TMASK
2210 #undef N_BTSHFT
2212 /* Add symbols from an XCOFF object file. */
2214 static bfd_boolean
2215 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2217 if (! _bfd_coff_get_external_symbols (abfd))
2218 return FALSE;
2219 if (! xcoff_link_add_symbols (abfd, info))
2220 return FALSE;
2221 if (! info->keep_memory)
2223 if (! _bfd_coff_free_symbols (abfd))
2224 return FALSE;
2226 return TRUE;
2229 /* Look through the loader symbols to see if this dynamic object
2230 should be included in the link. The native linker uses the loader
2231 symbols, not the normal symbol table, so we do too. */
2233 static bfd_boolean
2234 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2235 struct bfd_link_info *info,
2236 bfd_boolean *pneeded)
2238 asection *lsec;
2239 bfd_byte *contents;
2240 struct internal_ldhdr ldhdr;
2241 const char *strings;
2242 bfd_byte *elsym, *elsymend;
2244 *pneeded = FALSE;
2246 lsec = bfd_get_section_by_name (abfd, ".loader");
2247 if (lsec == NULL)
2248 /* There are no symbols, so don't try to include it. */
2249 return TRUE;
2251 if (! xcoff_get_section_contents (abfd, lsec))
2252 return FALSE;
2253 contents = coff_section_data (abfd, lsec)->contents;
2255 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2257 strings = (char *) contents + ldhdr.l_stoff;
2259 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2261 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2262 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2264 struct internal_ldsym ldsym;
2265 char nambuf[SYMNMLEN + 1];
2266 const char *name;
2267 struct bfd_link_hash_entry *h;
2269 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2271 /* We are only interested in exported symbols. */
2272 if ((ldsym.l_smtype & L_EXPORT) == 0)
2273 continue;
2275 if (ldsym._l._l_l._l_zeroes == 0)
2276 name = strings + ldsym._l._l_l._l_offset;
2277 else
2279 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2280 nambuf[SYMNMLEN] = '\0';
2281 name = nambuf;
2284 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2286 /* We are only interested in symbols that are currently
2287 undefined. At this point we know that we are using an XCOFF
2288 hash table. */
2289 if (h != NULL
2290 && h->type == bfd_link_hash_undefined
2291 && (((struct xcoff_link_hash_entry *) h)->flags
2292 & XCOFF_DEF_DYNAMIC) == 0)
2294 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2295 return FALSE;
2296 *pneeded = TRUE;
2297 return TRUE;
2301 /* We do not need this shared object. */
2302 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2304 free (coff_section_data (abfd, lsec)->contents);
2305 coff_section_data (abfd, lsec)->contents = NULL;
2308 return TRUE;
2311 /* Look through the symbols to see if this object file should be
2312 included in the link. */
2314 static bfd_boolean
2315 xcoff_link_check_ar_symbols (bfd *abfd,
2316 struct bfd_link_info *info,
2317 bfd_boolean *pneeded)
2319 bfd_size_type symesz;
2320 bfd_byte *esym;
2321 bfd_byte *esym_end;
2323 *pneeded = FALSE;
2325 if ((abfd->flags & DYNAMIC) != 0
2326 && ! info->static_link
2327 && info->output_bfd->xvec == abfd->xvec)
2328 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2330 symesz = bfd_coff_symesz (abfd);
2331 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2332 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2333 while (esym < esym_end)
2335 struct internal_syment sym;
2337 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2339 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2341 const char *name;
2342 char buf[SYMNMLEN + 1];
2343 struct bfd_link_hash_entry *h;
2345 /* This symbol is externally visible, and is defined by this
2346 object file. */
2347 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2349 if (name == NULL)
2350 return FALSE;
2351 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2353 /* We are only interested in symbols that are currently
2354 undefined. If a symbol is currently known to be common,
2355 XCOFF linkers do not bring in an object file which
2356 defines it. We also don't bring in symbols to satisfy
2357 undefined references in shared objects. */
2358 if (h != NULL
2359 && h->type == bfd_link_hash_undefined
2360 && (info->output_bfd->xvec != abfd->xvec
2361 || (((struct xcoff_link_hash_entry *) h)->flags
2362 & XCOFF_DEF_DYNAMIC) == 0))
2364 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2365 return FALSE;
2366 *pneeded = TRUE;
2367 return TRUE;
2371 esym += (sym.n_numaux + 1) * symesz;
2374 /* We do not need this object file. */
2375 return TRUE;
2378 /* Check a single archive element to see if we need to include it in
2379 the link. *PNEEDED is set according to whether this element is
2380 needed in the link or not. This is called via
2381 _bfd_generic_link_add_archive_symbols. */
2383 static bfd_boolean
2384 xcoff_link_check_archive_element (bfd *abfd,
2385 struct bfd_link_info *info,
2386 bfd_boolean *pneeded)
2388 bfd_boolean keep_syms_p;
2390 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2391 if (! _bfd_coff_get_external_symbols (abfd))
2392 return FALSE;
2394 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2395 return FALSE;
2397 if (*pneeded)
2399 if (! xcoff_link_add_symbols (abfd, info))
2400 return FALSE;
2401 if (info->keep_memory)
2402 keep_syms_p = TRUE;
2405 if (!keep_syms_p)
2407 if (! _bfd_coff_free_symbols (abfd))
2408 return FALSE;
2411 return TRUE;
2414 /* Given an XCOFF BFD, add symbols to the global hash table as
2415 appropriate. */
2417 bfd_boolean
2418 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2420 switch (bfd_get_format (abfd))
2422 case bfd_object:
2423 return xcoff_link_add_object_symbols (abfd, info);
2425 case bfd_archive:
2426 /* If the archive has a map, do the usual search. We then need
2427 to check the archive for dynamic objects, because they may not
2428 appear in the archive map even though they should, perhaps, be
2429 included. If the archive has no map, we just consider each object
2430 file in turn, since that apparently is what the AIX native linker
2431 does. */
2432 if (bfd_has_map (abfd))
2434 if (! (_bfd_generic_link_add_archive_symbols
2435 (abfd, info, xcoff_link_check_archive_element)))
2436 return FALSE;
2440 bfd *member;
2442 member = bfd_openr_next_archived_file (abfd, NULL);
2443 while (member != NULL)
2445 if (bfd_check_format (member, bfd_object)
2446 && (info->output_bfd->xvec == member->xvec)
2447 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2449 bfd_boolean needed;
2451 if (! xcoff_link_check_archive_element (member, info,
2452 &needed))
2453 return FALSE;
2454 if (needed)
2455 member->archive_pass = -1;
2457 member = bfd_openr_next_archived_file (abfd, member);
2461 return TRUE;
2463 default:
2464 bfd_set_error (bfd_error_wrong_format);
2465 return FALSE;
2469 bfd_boolean
2470 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2471 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2472 struct bfd_link_hash_entry *harg)
2474 struct xcoff_link_hash_entry *h;
2476 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2477 return FALSE;
2479 h = (struct xcoff_link_hash_entry *) harg;
2480 h->flags |= XCOFF_DEF_REGULAR;
2481 return TRUE;
2484 /* If symbol H has not been interpreted as a function descriptor,
2485 see whether it should be. Set up its descriptor information if so. */
2487 static bfd_boolean
2488 xcoff_find_function (struct bfd_link_info *info,
2489 struct xcoff_link_hash_entry *h)
2491 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2492 && h->root.root.string[0] != '.')
2494 char *fnname;
2495 struct xcoff_link_hash_entry *hfn;
2496 bfd_size_type amt;
2498 amt = strlen (h->root.root.string) + 2;
2499 fnname = bfd_malloc (amt);
2500 if (fnname == NULL)
2501 return FALSE;
2502 fnname[0] = '.';
2503 strcpy (fnname + 1, h->root.root.string);
2504 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2505 fnname, FALSE, FALSE, TRUE);
2506 free (fnname);
2507 if (hfn != NULL
2508 && hfn->smclas == XMC_PR
2509 && (hfn->root.type == bfd_link_hash_defined
2510 || hfn->root.type == bfd_link_hash_defweak))
2512 h->flags |= XCOFF_DESCRIPTOR;
2513 h->descriptor = hfn;
2514 hfn->descriptor = h;
2517 return TRUE;
2520 /* Return true if the given bfd contains at least one shared object. */
2522 static bfd_boolean
2523 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2524 bfd *archive)
2526 struct xcoff_archive_info *archive_info;
2527 bfd *member;
2529 archive_info = xcoff_get_archive_info (info, archive);
2530 if (!archive_info->know_contains_shared_object_p)
2532 member = bfd_openr_next_archived_file (archive, NULL);
2533 while (member != NULL && (member->flags & DYNAMIC) == 0)
2534 member = bfd_openr_next_archived_file (archive, member);
2536 archive_info->contains_shared_object_p = (member != NULL);
2537 archive_info->know_contains_shared_object_p = 1;
2539 return archive_info->contains_shared_object_p;
2542 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2543 qualifies for export by -bexpall. */
2545 static bfd_boolean
2546 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2548 /* Exclude symbols beginning with '_'. */
2549 if (h->root.root.string[0] == '_')
2550 return FALSE;
2552 /* Exclude archive members that would otherwise be unreferenced. */
2553 if ((h->flags & XCOFF_MARK) == 0
2554 && (h->root.type == bfd_link_hash_defined
2555 || h->root.type == bfd_link_hash_defweak)
2556 && h->root.u.def.section->owner != NULL
2557 && h->root.u.def.section->owner->my_archive != NULL)
2558 return FALSE;
2560 return TRUE;
2563 /* Return true if symbol H qualifies for the forms of automatic export
2564 specified by AUTO_EXPORT_FLAGS. */
2566 static bfd_boolean
2567 xcoff_auto_export_p (struct bfd_link_info *info,
2568 struct xcoff_link_hash_entry *h,
2569 unsigned int auto_export_flags)
2571 /* Don't automatically export things that were explicitly exported. */
2572 if ((h->flags & XCOFF_EXPORT) != 0)
2573 return FALSE;
2575 /* Don't export things that we don't define. */
2576 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2577 return FALSE;
2579 /* Don't export functions; export their descriptors instead. */
2580 if (h->root.root.string[0] == '.')
2581 return FALSE;
2583 /* We don't export a symbol which is being defined by an object
2584 included from an archive which contains a shared object. The
2585 rationale is that if an archive contains both an unshared and
2586 a shared object, then there must be some reason that the
2587 unshared object is unshared, and we don't want to start
2588 providing a shared version of it. In particular, this solves
2589 a bug involving the _savefNN set of functions. gcc will call
2590 those functions without providing a slot to restore the TOC,
2591 so it is essential that these functions be linked in directly
2592 and not from a shared object, which means that a shared
2593 object which also happens to link them in must not export
2594 them. This is confusing, but I haven't been able to think of
2595 a different approach. Note that the symbols can, of course,
2596 be exported explicitly. */
2597 if (h->root.type == bfd_link_hash_defined
2598 || h->root.type == bfd_link_hash_defweak)
2600 bfd *owner;
2602 owner = h->root.u.def.section->owner;
2603 if (owner != NULL
2604 && owner->my_archive != NULL
2605 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2606 return FALSE;
2609 /* Otherwise, all symbols are exported by -bexpfull. */
2610 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2611 return TRUE;
2613 /* Despite its name, -bexpall exports most but not all symbols. */
2614 if ((auto_export_flags & XCOFF_EXPALL) != 0
2615 && xcoff_covered_by_expall_p (h))
2616 return TRUE;
2618 return FALSE;
2621 /* Return true if relocation REL needs to be copied to the .loader section.
2622 If REL is against a global symbol, H is that symbol, otherwise it
2623 is null. */
2625 static bfd_boolean
2626 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2627 struct xcoff_link_hash_entry *h)
2629 if (!xcoff_hash_table (info)->loader_section)
2630 return FALSE;
2632 switch (rel->r_type)
2634 case R_TOC:
2635 case R_GL:
2636 case R_TCL:
2637 case R_TRL:
2638 case R_TRLA:
2639 /* We should never need a .loader reloc for a TOC-relative reloc. */
2640 return FALSE;
2642 default:
2643 /* In this case, relocations against defined symbols can be resolved
2644 statically. */
2645 if (h == NULL
2646 || h->root.type == bfd_link_hash_defined
2647 || h->root.type == bfd_link_hash_defweak
2648 || h->root.type == bfd_link_hash_common)
2649 return FALSE;
2651 /* We will always provide a local definition of function symbols,
2652 even if we don't have one yet. */
2653 if ((h->flags & XCOFF_CALLED) != 0)
2654 return FALSE;
2656 return TRUE;
2658 case R_POS:
2659 case R_NEG:
2660 case R_RL:
2661 case R_RLA:
2662 /* Absolute relocations against absolute symbols can be
2663 resolved statically. */
2664 if (h != NULL
2665 && (h->root.type == bfd_link_hash_defined
2666 || h->root.type == bfd_link_hash_defweak)
2667 && bfd_is_abs_section (h->root.u.def.section))
2668 return FALSE;
2670 return TRUE;
2674 /* Mark a symbol as not being garbage, including the section in which
2675 it is defined. */
2677 static inline bfd_boolean
2678 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2680 if ((h->flags & XCOFF_MARK) != 0)
2681 return TRUE;
2683 h->flags |= XCOFF_MARK;
2685 /* If we're marking an undefined symbol, try find some way of
2686 defining it. */
2687 if (!info->relocatable
2688 && (h->flags & XCOFF_IMPORT) == 0
2689 && (h->flags & XCOFF_DEF_REGULAR) == 0
2690 && (h->root.type == bfd_link_hash_undefined
2691 || h->root.type == bfd_link_hash_undefweak))
2693 /* First check whether this symbol can be interpreted as an
2694 undefined function descriptor for a defined function symbol. */
2695 if (!xcoff_find_function (info, h))
2696 return FALSE;
2698 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2699 && (h->descriptor->root.type == bfd_link_hash_defined
2700 || h->descriptor->root.type == bfd_link_hash_defweak))
2702 /* This is a descriptor for a defined symbol, but the input
2703 objects have not defined the descriptor itself. Fill in
2704 the definition automatically.
2706 Note that we do this even if we found a dynamic definition
2707 of H. The local function definition logically overrides
2708 the dynamic one. */
2709 asection *sec;
2711 sec = xcoff_hash_table (info)->descriptor_section;
2712 h->root.type = bfd_link_hash_defined;
2713 h->root.u.def.section = sec;
2714 h->root.u.def.value = sec->size;
2715 h->smclas = XMC_DS;
2716 h->flags |= XCOFF_DEF_REGULAR;
2718 /* The size of the function descriptor depends on whether this
2719 is xcoff32 (12) or xcoff64 (24). */
2720 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2722 /* A function descriptor uses two relocs: one for the
2723 associated code, and one for the TOC address. */
2724 xcoff_hash_table (info)->ldrel_count += 2;
2725 sec->reloc_count += 2;
2727 /* Mark the function itself. */
2728 if (!xcoff_mark_symbol (info, h->descriptor))
2729 return FALSE;
2731 /* Mark the TOC section, so that we get an anchor
2732 to relocate against. */
2733 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2734 return FALSE;
2736 /* We handle writing out the contents of the descriptor in
2737 xcoff_write_global_symbol. */
2739 else if (info->static_link)
2740 /* We can't get a symbol value dynamically, so just assume
2741 that it's undefined. */
2742 h->flags |= XCOFF_WAS_UNDEFINED;
2743 else if ((h->flags & XCOFF_CALLED) != 0)
2745 /* This is a function symbol for which we need to create
2746 linkage code. */
2747 asection *sec;
2748 struct xcoff_link_hash_entry *hds;
2750 /* Mark the descriptor (and its TOC section). */
2751 hds = h->descriptor;
2752 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2753 || hds->root.type == bfd_link_hash_undefweak)
2754 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2755 if (!xcoff_mark_symbol (info, hds))
2756 return FALSE;
2758 /* Treat this symbol as undefined if the descriptor was. */
2759 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2760 h->flags |= XCOFF_WAS_UNDEFINED;
2762 /* Allocate room for the global linkage code itself. */
2763 sec = xcoff_hash_table (info)->linkage_section;
2764 h->root.type = bfd_link_hash_defined;
2765 h->root.u.def.section = sec;
2766 h->root.u.def.value = sec->size;
2767 h->smclas = XMC_GL;
2768 h->flags |= XCOFF_DEF_REGULAR;
2769 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2771 /* The global linkage code requires a TOC entry for the
2772 descriptor. */
2773 if (hds->toc_section == NULL)
2775 int byte_size;
2777 /* 32 vs 64
2778 xcoff32 uses 4 bytes in the toc.
2779 xcoff64 uses 8 bytes in the toc. */
2780 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2781 byte_size = 8;
2782 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2783 byte_size = 4;
2784 else
2785 return FALSE;
2787 /* Allocate room in the fallback TOC section. */
2788 hds->toc_section = xcoff_hash_table (info)->toc_section;
2789 hds->u.toc_offset = hds->toc_section->size;
2790 hds->toc_section->size += byte_size;
2791 if (!xcoff_mark (info, hds->toc_section))
2792 return FALSE;
2794 /* Allocate room for a static and dynamic R_TOC
2795 relocation. */
2796 ++xcoff_hash_table (info)->ldrel_count;
2797 ++hds->toc_section->reloc_count;
2799 /* Set the index to -2 to force this symbol to
2800 get written out. */
2801 hds->indx = -2;
2802 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2805 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2807 /* Record that the symbol was undefined, then import it.
2808 -brtl links use a special fake import file. */
2809 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2810 if (xcoff_hash_table (info)->rtld)
2812 if (!xcoff_set_import_path (info, h, "", "..", ""))
2813 return FALSE;
2815 else
2817 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2818 return FALSE;
2823 if (h->root.type == bfd_link_hash_defined
2824 || h->root.type == bfd_link_hash_defweak)
2826 asection *hsec;
2828 hsec = h->root.u.def.section;
2829 if (! bfd_is_abs_section (hsec)
2830 && (hsec->flags & SEC_MARK) == 0)
2832 if (! xcoff_mark (info, hsec))
2833 return FALSE;
2837 if (h->toc_section != NULL
2838 && (h->toc_section->flags & SEC_MARK) == 0)
2840 if (! xcoff_mark (info, h->toc_section))
2841 return FALSE;
2844 return TRUE;
2847 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2848 If the symbol exists, set FLAGS. */
2850 static bfd_boolean
2851 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2852 const char *name, unsigned int flags)
2854 struct xcoff_link_hash_entry *h;
2856 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2857 FALSE, FALSE, TRUE);
2858 if (h != NULL)
2860 h->flags |= flags;
2861 if (h->root.type == bfd_link_hash_defined
2862 || h->root.type == bfd_link_hash_defweak)
2864 if (!xcoff_mark (info, h->root.u.def.section))
2865 return FALSE;
2868 return TRUE;
2871 /* The mark phase of garbage collection. For a given section, mark
2872 it, and all the sections which define symbols to which it refers.
2873 Because this function needs to look at the relocs, we also count
2874 the number of relocs which need to be copied into the .loader
2875 section. */
2877 static bfd_boolean
2878 xcoff_mark (struct bfd_link_info *info, asection *sec)
2880 if (bfd_is_abs_section (sec)
2881 || (sec->flags & SEC_MARK) != 0)
2882 return TRUE;
2884 sec->flags |= SEC_MARK;
2886 if (sec->owner->xvec == info->output_bfd->xvec
2887 && coff_section_data (sec->owner, sec) != NULL
2888 && xcoff_section_data (sec->owner, sec) != NULL)
2890 struct xcoff_link_hash_entry **syms;
2891 struct internal_reloc *rel, *relend;
2892 asection **csects;
2893 unsigned long i, first, last;
2895 /* Mark all the symbols in this section. */
2896 syms = obj_xcoff_sym_hashes (sec->owner);
2897 csects = xcoff_data (sec->owner)->csects;
2898 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2899 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2900 for (i = first; i <= last; i++)
2901 if (csects[i] == sec
2902 && syms[i] != NULL
2903 && (syms[i]->flags & XCOFF_MARK) == 0)
2905 if (!xcoff_mark_symbol (info, syms[i]))
2906 return FALSE;
2909 /* Look through the section relocs. */
2910 if ((sec->flags & SEC_RELOC) != 0
2911 && sec->reloc_count > 0)
2913 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2914 NULL, FALSE, NULL);
2915 if (rel == NULL)
2916 return FALSE;
2917 relend = rel + sec->reloc_count;
2918 for (; rel < relend; rel++)
2920 struct xcoff_link_hash_entry *h;
2922 if ((unsigned int) rel->r_symndx
2923 > obj_raw_syment_count (sec->owner))
2924 continue;
2926 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2927 if (h != NULL)
2929 if ((h->flags & XCOFF_MARK) == 0)
2931 if (!xcoff_mark_symbol (info, h))
2932 return FALSE;
2935 else
2937 asection *rsec;
2939 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2940 if (rsec != NULL
2941 && (rsec->flags & SEC_MARK) == 0)
2943 if (!xcoff_mark (info, rsec))
2944 return FALSE;
2948 /* See if this reloc needs to be copied into the .loader
2949 section. */
2950 if (xcoff_need_ldrel_p (info, rel, h))
2952 ++xcoff_hash_table (info)->ldrel_count;
2953 if (h != NULL)
2954 h->flags |= XCOFF_LDREL;
2958 if (! info->keep_memory
2959 && coff_section_data (sec->owner, sec) != NULL
2960 && coff_section_data (sec->owner, sec)->relocs != NULL
2961 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2963 free (coff_section_data (sec->owner, sec)->relocs);
2964 coff_section_data (sec->owner, sec)->relocs = NULL;
2969 return TRUE;
2972 /* Routines that are called after all the input files have been
2973 handled, but before the sections are laid out in memory. */
2975 /* The sweep phase of garbage collection. Remove all garbage
2976 sections. */
2978 static void
2979 xcoff_sweep (struct bfd_link_info *info)
2981 bfd *sub;
2983 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2985 asection *o;
2987 for (o = sub->sections; o != NULL; o = o->next)
2989 if ((o->flags & SEC_MARK) == 0)
2991 /* Keep all sections from non-XCOFF input files. Keep
2992 special sections. Keep .debug sections for the
2993 moment. */
2994 if (sub->xvec != info->output_bfd->xvec
2995 || o == xcoff_hash_table (info)->debug_section
2996 || o == xcoff_hash_table (info)->loader_section
2997 || o == xcoff_hash_table (info)->linkage_section
2998 || o == xcoff_hash_table (info)->descriptor_section
2999 || strcmp (o->name, ".debug") == 0)
3000 o->flags |= SEC_MARK;
3001 else
3003 o->size = 0;
3004 o->reloc_count = 0;
3011 /* Record the number of elements in a set. This is used to output the
3012 correct csect length. */
3014 bfd_boolean
3015 bfd_xcoff_link_record_set (bfd *output_bfd,
3016 struct bfd_link_info *info,
3017 struct bfd_link_hash_entry *harg,
3018 bfd_size_type size)
3020 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3021 struct xcoff_link_size_list *n;
3022 bfd_size_type amt;
3024 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3025 return TRUE;
3027 /* This will hardly ever be called. I don't want to burn four bytes
3028 per global symbol, so instead the size is kept on a linked list
3029 attached to the hash table. */
3030 amt = sizeof (* n);
3031 n = bfd_alloc (output_bfd, amt);
3032 if (n == NULL)
3033 return FALSE;
3034 n->next = xcoff_hash_table (info)->size_list;
3035 n->h = h;
3036 n->size = size;
3037 xcoff_hash_table (info)->size_list = n;
3039 h->flags |= XCOFF_HAS_SIZE;
3041 return TRUE;
3044 /* Import a symbol. */
3046 bfd_boolean
3047 bfd_xcoff_import_symbol (bfd *output_bfd,
3048 struct bfd_link_info *info,
3049 struct bfd_link_hash_entry *harg,
3050 bfd_vma val,
3051 const char *imppath,
3052 const char *impfile,
3053 const char *impmember,
3054 unsigned int syscall_flag)
3056 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3058 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3059 return TRUE;
3061 /* A symbol name which starts with a period is the code for a
3062 function. If the symbol is undefined, then add an undefined
3063 symbol for the function descriptor, and import that instead. */
3064 if (h->root.root.string[0] == '.'
3065 && h->root.type == bfd_link_hash_undefined
3066 && val == (bfd_vma) -1)
3068 struct xcoff_link_hash_entry *hds;
3070 hds = h->descriptor;
3071 if (hds == NULL)
3073 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3074 h->root.root.string + 1,
3075 TRUE, FALSE, TRUE);
3076 if (hds == NULL)
3077 return FALSE;
3078 if (hds->root.type == bfd_link_hash_new)
3080 hds->root.type = bfd_link_hash_undefined;
3081 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3083 hds->flags |= XCOFF_DESCRIPTOR;
3084 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3085 hds->descriptor = h;
3086 h->descriptor = hds;
3089 /* Now, if the descriptor is undefined, import the descriptor
3090 rather than the symbol we were told to import. FIXME: Is
3091 this correct in all cases? */
3092 if (hds->root.type == bfd_link_hash_undefined)
3093 h = hds;
3096 h->flags |= (XCOFF_IMPORT | syscall_flag);
3098 if (val != (bfd_vma) -1)
3100 if (h->root.type == bfd_link_hash_defined
3101 && (! bfd_is_abs_section (h->root.u.def.section)
3102 || h->root.u.def.value != val))
3104 if (! ((*info->callbacks->multiple_definition)
3105 (info, h->root.root.string, h->root.u.def.section->owner,
3106 h->root.u.def.section, h->root.u.def.value,
3107 output_bfd, bfd_abs_section_ptr, val)))
3108 return FALSE;
3111 h->root.type = bfd_link_hash_defined;
3112 h->root.u.def.section = bfd_abs_section_ptr;
3113 h->root.u.def.value = val;
3114 h->smclas = XMC_XO;
3117 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3118 return FALSE;
3120 return TRUE;
3123 /* Export a symbol. */
3125 bfd_boolean
3126 bfd_xcoff_export_symbol (bfd *output_bfd,
3127 struct bfd_link_info *info,
3128 struct bfd_link_hash_entry *harg)
3130 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3132 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3133 return TRUE;
3135 h->flags |= XCOFF_EXPORT;
3137 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3138 I'm just going to ignore it until somebody explains it. */
3140 /* Make sure we don't garbage collect this symbol. */
3141 if (! xcoff_mark_symbol (info, h))
3142 return FALSE;
3144 /* If this is a function descriptor, make sure we don't garbage
3145 collect the associated function code. We normally don't have to
3146 worry about this, because the descriptor will be attached to a
3147 section with relocs, but if we are creating the descriptor
3148 ourselves those relocs will not be visible to the mark code. */
3149 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3151 if (! xcoff_mark_symbol (info, h->descriptor))
3152 return FALSE;
3155 return TRUE;
3158 /* Count a reloc against a symbol. This is called for relocs
3159 generated by the linker script, typically for global constructors
3160 and destructors. */
3162 bfd_boolean
3163 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3164 struct bfd_link_info *info,
3165 const char *name)
3167 struct xcoff_link_hash_entry *h;
3169 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3170 return TRUE;
3172 h = ((struct xcoff_link_hash_entry *)
3173 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3174 FALSE));
3175 if (h == NULL)
3177 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3178 bfd_set_error (bfd_error_no_symbols);
3179 return FALSE;
3182 h->flags |= XCOFF_REF_REGULAR;
3183 if (xcoff_hash_table (info)->loader_section)
3185 h->flags |= XCOFF_LDREL;
3186 ++xcoff_hash_table (info)->ldrel_count;
3189 /* Mark the symbol to avoid garbage collection. */
3190 if (! xcoff_mark_symbol (info, h))
3191 return FALSE;
3193 return TRUE;
3196 /* This function is called for each symbol to which the linker script
3197 assigns a value. */
3199 bfd_boolean
3200 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3201 struct bfd_link_info *info,
3202 const char *name)
3204 struct xcoff_link_hash_entry *h;
3206 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3207 return TRUE;
3209 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3210 FALSE);
3211 if (h == NULL)
3212 return FALSE;
3214 h->flags |= XCOFF_DEF_REGULAR;
3216 return TRUE;
3219 /* An xcoff_link_hash_traverse callback for which DATA points to an
3220 xcoff_loader_info. Mark all symbols that should be automatically
3221 exported. */
3223 static bfd_boolean
3224 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3226 struct xcoff_loader_info *ldinfo;
3228 ldinfo = (struct xcoff_loader_info *) data;
3229 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3231 if (!xcoff_mark_symbol (ldinfo->info, h))
3232 ldinfo->failed = TRUE;
3234 return TRUE;
3237 /* Add a symbol to the .loader symbols, if necessary. */
3239 /* INPUT_BFD has an external symbol associated with hash table entry H
3240 and csect CSECT. Return true if INPUT_BFD defines H. */
3242 static bfd_boolean
3243 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3244 asection *csect)
3246 switch (h->root.type)
3248 case bfd_link_hash_defined:
3249 case bfd_link_hash_defweak:
3250 /* No input bfd owns absolute symbols. They are written by
3251 xcoff_write_global_symbol instead. */
3252 return (!bfd_is_abs_section (csect)
3253 && h->root.u.def.section == csect);
3255 case bfd_link_hash_common:
3256 return h->root.u.c.p->section->owner == input_bfd;
3258 case bfd_link_hash_undefined:
3259 case bfd_link_hash_undefweak:
3260 /* We can't treat undef.abfd as the owner because that bfd
3261 might be a dynamic object. Allow any bfd to claim it. */
3262 return TRUE;
3264 default:
3265 abort ();
3269 /* See if H should have a loader symbol associated with it. */
3271 static bfd_boolean
3272 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3273 struct xcoff_link_hash_entry *h)
3275 bfd_size_type amt;
3277 /* Warn if this symbol is exported but not defined. */
3278 if ((h->flags & XCOFF_EXPORT) != 0
3279 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3281 (*_bfd_error_handler)
3282 (_("warning: attempt to export undefined symbol `%s'"),
3283 h->root.root.string);
3284 return TRUE;
3287 /* We need to add a symbol to the .loader section if it is mentioned
3288 in a reloc which we are copying to the .loader section and it was
3289 not defined or common, or if it is the entry point, or if it is
3290 being exported. */
3291 if (((h->flags & XCOFF_LDREL) == 0
3292 || h->root.type == bfd_link_hash_defined
3293 || h->root.type == bfd_link_hash_defweak
3294 || h->root.type == bfd_link_hash_common)
3295 && (h->flags & XCOFF_ENTRY) == 0
3296 && (h->flags & XCOFF_EXPORT) == 0)
3297 return TRUE;
3299 /* We need to add this symbol to the .loader symbols. */
3301 BFD_ASSERT (h->ldsym == NULL);
3302 amt = sizeof (struct internal_ldsym);
3303 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3304 if (h->ldsym == NULL)
3306 ldinfo->failed = TRUE;
3307 return FALSE;
3310 if ((h->flags & XCOFF_IMPORT) != 0)
3312 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3313 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3314 h->smclas = XMC_DS;
3315 h->ldsym->l_ifile = h->ldindx;
3318 /* The first 3 symbol table indices are reserved to indicate the
3319 data, text and bss sections. */
3320 h->ldindx = ldinfo->ldsym_count + 3;
3322 ++ldinfo->ldsym_count;
3324 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3325 h->ldsym, h->root.root.string))
3326 return FALSE;
3328 h->flags |= XCOFF_BUILT_LDSYM;
3329 return TRUE;
3332 /* An xcoff_htab_traverse callback that is called for each symbol
3333 once garbage collection is complete. */
3335 static bfd_boolean
3336 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3338 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3340 if (h->root.type == bfd_link_hash_warning)
3341 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3343 /* __rtinit, this symbol has special handling. */
3344 if (h->flags & XCOFF_RTINIT)
3345 return TRUE;
3347 /* We don't want to garbage collect symbols which are not defined in
3348 XCOFF files. This is a convenient place to mark them. */
3349 if (xcoff_hash_table (ldinfo->info)->gc
3350 && (h->flags & XCOFF_MARK) == 0
3351 && (h->root.type == bfd_link_hash_defined
3352 || h->root.type == bfd_link_hash_defweak)
3353 && (h->root.u.def.section->owner == NULL
3354 || (h->root.u.def.section->owner->xvec
3355 != ldinfo->info->output_bfd->xvec)))
3356 h->flags |= XCOFF_MARK;
3358 /* Skip discarded symbols. */
3359 if (xcoff_hash_table (ldinfo->info)->gc
3360 && (h->flags & XCOFF_MARK) == 0)
3361 return TRUE;
3363 /* If this is still a common symbol, and it wasn't garbage
3364 collected, we need to actually allocate space for it in the .bss
3365 section. */
3366 if (h->root.type == bfd_link_hash_common
3367 && h->root.u.c.p->section->size == 0)
3369 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3370 h->root.u.c.p->section->size = h->root.u.c.size;
3373 if (xcoff_hash_table (ldinfo->info)->loader_section)
3375 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3376 h->flags |= XCOFF_EXPORT;
3378 if (!xcoff_build_ldsym (ldinfo, h))
3379 return FALSE;
3382 return TRUE;
3385 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3386 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3387 csect information, if any. NAME is the function's name if the name
3388 is stored in the .debug section, otherwise it is null.
3390 Return 1 if we should include an appropriately-adjusted ISYM
3391 in the output file, 0 if we should discard ISYM, or -1 if an
3392 error occured. */
3394 static int
3395 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3396 struct internal_syment *isym,
3397 union internal_auxent *aux,
3398 struct xcoff_link_hash_entry *h,
3399 asection *csect, const char *name)
3401 int smtyp;
3403 /* If we are skipping this csect, we want to strip the symbol too. */
3404 if (csect == NULL)
3405 return 0;
3407 /* Likewise if we garbage-collected the csect. */
3408 if (xcoff_hash_table (info)->gc
3409 && !bfd_is_abs_section (csect)
3410 && !bfd_is_und_section (csect)
3411 && (csect->flags & SEC_MARK) == 0)
3412 return 0;
3414 /* An XCOFF linker always removes C_STAT symbols. */
3415 if (isym->n_sclass == C_STAT)
3416 return 0;
3418 /* We generate the TOC anchor separately. */
3419 if (isym->n_sclass == C_HIDEXT
3420 && aux->x_csect.x_smclas == XMC_TC0)
3421 return 0;
3423 /* If we are stripping all symbols, we want to discard this one. */
3424 if (info->strip == strip_all)
3425 return 0;
3427 /* Discard symbols that are defined elsewhere. */
3428 if (EXTERN_SYM_P (isym->n_sclass))
3430 if ((h->flags & XCOFF_ALLOCATED) != 0)
3431 return 0;
3432 if (!xcoff_final_definition_p (input_bfd, h, csect))
3433 return 0;
3436 /* If we're discarding local symbols, check whether ISYM is local. */
3437 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3438 if (info->discard == discard_all
3439 && !EXTERN_SYM_P (isym->n_sclass)
3440 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3441 return 0;
3443 /* If we're stripping debugging symbols, check whether ISYM is one. */
3444 if (info->strip == strip_debugger
3445 && isym->n_scnum == N_DEBUG)
3446 return 0;
3448 /* If we are stripping symbols based on name, check how ISYM's
3449 name should be handled. */
3450 if (info->strip == strip_some
3451 || info->discard == discard_l)
3453 char buf[SYMNMLEN + 1];
3455 if (name == NULL)
3457 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3458 if (name == NULL)
3459 return -1;
3462 if (info->strip == strip_some
3463 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3464 return 0;
3466 if (info->discard == discard_l
3467 && !EXTERN_SYM_P (isym->n_sclass)
3468 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3469 && bfd_is_local_label_name (input_bfd, name))
3470 return 0;
3473 return 1;
3476 /* Lay out the .loader section, filling in the header and the import paths.
3477 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3479 static bfd_boolean
3480 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3481 const char *libpath)
3483 bfd *output_bfd;
3484 struct xcoff_link_hash_table *htab;
3485 struct internal_ldhdr *ldhdr;
3486 struct xcoff_import_file *fl;
3487 bfd_size_type stoff;
3488 size_t impsize, impcount;
3489 asection *lsec;
3490 char *out;
3492 /* Work out the size of the import file names. Each import file ID
3493 consists of three null terminated strings: the path, the file
3494 name, and the archive member name. The first entry in the list
3495 of names is the path to use to find objects, which the linker has
3496 passed in as the libpath argument. For some reason, the path
3497 entry in the other import file names appears to always be empty. */
3498 output_bfd = ldinfo->output_bfd;
3499 htab = xcoff_hash_table (ldinfo->info);
3500 impsize = strlen (libpath) + 3;
3501 impcount = 1;
3502 for (fl = htab->imports; fl != NULL; fl = fl->next)
3504 ++impcount;
3505 impsize += (strlen (fl->path)
3506 + strlen (fl->file)
3507 + strlen (fl->member)
3508 + 3);
3511 /* Set up the .loader section header. */
3512 ldhdr = &htab->ldhdr;
3513 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3514 ldhdr->l_nsyms = ldinfo->ldsym_count;
3515 ldhdr->l_nreloc = htab->ldrel_count;
3516 ldhdr->l_istlen = impsize;
3517 ldhdr->l_nimpid = impcount;
3518 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3519 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3520 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3521 ldhdr->l_stlen = ldinfo->string_size;
3522 stoff = ldhdr->l_impoff + impsize;
3523 if (ldinfo->string_size == 0)
3524 ldhdr->l_stoff = 0;
3525 else
3526 ldhdr->l_stoff = stoff;
3528 /* 64 bit elements to ldhdr
3529 The swap out routine for 32 bit will ignore them.
3530 Nothing fancy, symbols come after the header and relocs come
3531 after symbols. */
3532 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3533 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3534 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3536 /* We now know the final size of the .loader section. Allocate
3537 space for it. */
3538 lsec = htab->loader_section;
3539 lsec->size = stoff + ldhdr->l_stlen;
3540 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3541 if (lsec->contents == NULL)
3542 return FALSE;
3544 /* Set up the header. */
3545 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3547 /* Set up the import file names. */
3548 out = (char *) lsec->contents + ldhdr->l_impoff;
3549 strcpy (out, libpath);
3550 out += strlen (libpath) + 1;
3551 *out++ = '\0';
3552 *out++ = '\0';
3553 for (fl = htab->imports; fl != NULL; fl = fl->next)
3555 const char *s;
3557 s = fl->path;
3558 while ((*out++ = *s++) != '\0')
3560 s = fl->file;
3561 while ((*out++ = *s++) != '\0')
3563 s = fl->member;
3564 while ((*out++ = *s++) != '\0')
3568 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3570 /* Set up the symbol string table. */
3571 if (ldinfo->string_size > 0)
3573 memcpy (out, ldinfo->strings, ldinfo->string_size);
3574 free (ldinfo->strings);
3575 ldinfo->strings = NULL;
3578 /* We can't set up the symbol table or the relocs yet, because we
3579 don't yet know the final position of the various sections. The
3580 .loader symbols are written out when the corresponding normal
3581 symbols are written out in xcoff_link_input_bfd or
3582 xcoff_write_global_symbol. The .loader relocs are written out
3583 when the corresponding normal relocs are handled in
3584 xcoff_link_input_bfd. */
3586 return TRUE;
3589 /* Build the .loader section. This is called by the XCOFF linker
3590 emulation before_allocation routine. We must set the size of the
3591 .loader section before the linker lays out the output file.
3592 LIBPATH is the library path to search for shared objects; this is
3593 normally built from the -L arguments passed to the linker. ENTRY
3594 is the name of the entry point symbol (the -e linker option).
3595 FILE_ALIGN is the alignment to use for sections within the file
3596 (the -H linker option). MAXSTACK is the maximum stack size (the
3597 -bmaxstack linker option). MAXDATA is the maximum data size (the
3598 -bmaxdata linker option). GC is whether to do garbage collection
3599 (the -bgc linker option). MODTYPE is the module type (the
3600 -bmodtype linker option). TEXTRO is whether the text section must
3601 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3602 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3603 is set by this routine to csects with magic names like _end. */
3605 bfd_boolean
3606 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3607 struct bfd_link_info *info,
3608 const char *libpath,
3609 const char *entry,
3610 unsigned long file_align,
3611 unsigned long maxstack,
3612 unsigned long maxdata,
3613 bfd_boolean gc,
3614 int modtype,
3615 bfd_boolean textro,
3616 unsigned int auto_export_flags,
3617 asection **special_sections,
3618 bfd_boolean rtld)
3620 struct xcoff_loader_info ldinfo;
3621 int i;
3622 asection *sec;
3623 bfd *sub;
3624 struct bfd_strtab_hash *debug_strtab;
3625 bfd_byte *debug_contents = NULL;
3626 bfd_size_type amt;
3628 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3630 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3631 special_sections[i] = NULL;
3632 return TRUE;
3635 ldinfo.failed = FALSE;
3636 ldinfo.output_bfd = output_bfd;
3637 ldinfo.info = info;
3638 ldinfo.auto_export_flags = auto_export_flags;
3639 ldinfo.ldsym_count = 0;
3640 ldinfo.string_size = 0;
3641 ldinfo.strings = NULL;
3642 ldinfo.string_alc = 0;
3644 xcoff_data (output_bfd)->maxstack = maxstack;
3645 xcoff_data (output_bfd)->maxdata = maxdata;
3646 xcoff_data (output_bfd)->modtype = modtype;
3648 xcoff_hash_table (info)->file_align = file_align;
3649 xcoff_hash_table (info)->textro = textro;
3650 xcoff_hash_table (info)->rtld = rtld;
3652 /* __rtinit */
3653 if (xcoff_hash_table (info)->loader_section
3654 && (info->init_function || info->fini_function || rtld))
3656 struct xcoff_link_hash_entry *hsym;
3657 struct internal_ldsym *ldsym;
3659 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3660 "__rtinit", FALSE, FALSE, TRUE);
3661 if (hsym == NULL)
3663 (*_bfd_error_handler)
3664 (_("error: undefined symbol __rtinit"));
3665 return FALSE;
3668 xcoff_mark_symbol (info, hsym);
3669 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3671 /* __rtinit initialized. */
3672 amt = sizeof (* ldsym);
3673 ldsym = bfd_malloc (amt);
3675 ldsym->l_value = 0; /* Will be filled in later. */
3676 ldsym->l_scnum = 2; /* Data section. */
3677 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3678 ldsym->l_smclas = 5; /* .rw. */
3679 ldsym->l_ifile = 0; /* Special system loader symbol. */
3680 ldsym->l_parm = 0; /* NA. */
3682 /* Force __rtinit to be the first symbol in the loader symbol table
3683 See xcoff_build_ldsyms
3685 The first 3 symbol table indices are reserved to indicate the data,
3686 text and bss sections. */
3687 BFD_ASSERT (0 == ldinfo.ldsym_count);
3689 hsym->ldindx = 3;
3690 ldinfo.ldsym_count = 1;
3691 hsym->ldsym = ldsym;
3693 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3694 hsym->ldsym, hsym->root.root.string))
3695 return FALSE;
3697 /* This symbol is written out by xcoff_write_global_symbol
3698 Set stuff up so xcoff_write_global_symbol logic works. */
3699 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3700 hsym->root.type = bfd_link_hash_defined;
3701 hsym->root.u.def.value = 0;
3704 /* Garbage collect unused sections. */
3705 if (info->relocatable || !gc)
3707 gc = FALSE;
3708 xcoff_hash_table (info)->gc = FALSE;
3710 /* We still need to call xcoff_mark, in order to set ldrel_count
3711 correctly. */
3712 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3714 asection *o;
3716 for (o = sub->sections; o != NULL; o = o->next)
3718 /* We shouldn't unconditionaly mark the TOC section.
3719 The output file should only have a TOC if either
3720 (a) one of the input files did or (b) we end up
3721 creating TOC references as part of the link process. */
3722 if (o != xcoff_hash_table (info)->toc_section
3723 && (o->flags & SEC_MARK) == 0)
3725 if (! xcoff_mark (info, o))
3726 goto error_return;
3731 else
3733 if (entry != NULL
3734 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3735 goto error_return;
3736 if (info->init_function != NULL
3737 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3738 goto error_return;
3739 if (info->fini_function != NULL
3740 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3741 goto error_return;
3742 if (auto_export_flags != 0)
3744 xcoff_link_hash_traverse (xcoff_hash_table (info),
3745 xcoff_mark_auto_exports, &ldinfo);
3746 if (ldinfo.failed)
3747 goto error_return;
3749 xcoff_sweep (info);
3750 xcoff_hash_table (info)->gc = TRUE;
3753 /* Return special sections to the caller. */
3754 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3756 sec = xcoff_hash_table (info)->special_sections[i];
3758 if (sec != NULL
3759 && gc
3760 && (sec->flags & SEC_MARK) == 0)
3761 sec = NULL;
3763 special_sections[i] = sec;
3766 if (info->input_bfds == NULL)
3767 /* I'm not sure what to do in this bizarre case. */
3768 return TRUE;
3770 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3771 (void *) &ldinfo);
3772 if (ldinfo.failed)
3773 goto error_return;
3775 if (xcoff_hash_table (info)->loader_section
3776 && !xcoff_build_loader_section (&ldinfo, libpath))
3777 goto error_return;
3779 /* Allocate space for the magic sections. */
3780 sec = xcoff_hash_table (info)->linkage_section;
3781 if (sec->size > 0)
3783 sec->contents = bfd_zalloc (output_bfd, sec->size);
3784 if (sec->contents == NULL)
3785 goto error_return;
3787 sec = xcoff_hash_table (info)->toc_section;
3788 if (sec->size > 0)
3790 sec->contents = bfd_zalloc (output_bfd, sec->size);
3791 if (sec->contents == NULL)
3792 goto error_return;
3794 sec = xcoff_hash_table (info)->descriptor_section;
3795 if (sec->size > 0)
3797 sec->contents = bfd_zalloc (output_bfd, sec->size);
3798 if (sec->contents == NULL)
3799 goto error_return;
3802 /* Now that we've done garbage collection, decide which symbols to keep,
3803 and figure out the contents of the .debug section. */
3804 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3806 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3808 asection *subdeb;
3809 bfd_size_type symcount;
3810 long *debug_index;
3811 asection **csectpp;
3812 unsigned int *lineno_counts;
3813 struct xcoff_link_hash_entry **sym_hash;
3814 bfd_byte *esym, *esymend;
3815 bfd_size_type symesz;
3817 if (sub->xvec != info->output_bfd->xvec)
3818 continue;
3820 if ((sub->flags & DYNAMIC) != 0
3821 && !info->static_link)
3822 continue;
3824 if (! _bfd_coff_get_external_symbols (sub))
3825 goto error_return;
3827 symcount = obj_raw_syment_count (sub);
3828 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3829 if (debug_index == NULL)
3830 goto error_return;
3831 xcoff_data (sub)->debug_indices = debug_index;
3833 if (info->strip == strip_all
3834 || info->strip == strip_debugger
3835 || info->discard == discard_all)
3836 /* We're stripping all debugging information, so there's no need
3837 to read SUB's .debug section. */
3838 subdeb = NULL;
3839 else
3841 /* Grab the contents of SUB's .debug section, if any. */
3842 subdeb = bfd_get_section_by_name (sub, ".debug");
3843 if (subdeb != NULL && subdeb->size > 0)
3845 /* We use malloc and copy the names into the debug
3846 stringtab, rather than bfd_alloc, because I expect
3847 that, when linking many files together, many of the
3848 strings will be the same. Storing the strings in the
3849 hash table should save space in this case. */
3850 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3851 goto error_return;
3855 csectpp = xcoff_data (sub)->csects;
3856 lineno_counts = xcoff_data (sub)->lineno_counts;
3857 sym_hash = obj_xcoff_sym_hashes (sub);
3858 symesz = bfd_coff_symesz (sub);
3859 esym = (bfd_byte *) obj_coff_external_syms (sub);
3860 esymend = esym + symcount * symesz;
3862 while (esym < esymend)
3864 struct internal_syment sym;
3865 union internal_auxent aux;
3866 asection *csect;
3867 const char *name;
3868 int keep_p;
3870 bfd_coff_swap_sym_in (sub, esym, &sym);
3872 /* Read in the csect information, if any. */
3873 if (CSECT_SYM_P (sym.n_sclass))
3875 BFD_ASSERT (sym.n_numaux > 0);
3876 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3877 sym.n_type, sym.n_sclass,
3878 sym.n_numaux - 1, sym.n_numaux, &aux);
3881 /* If this symbol's name is stored in the debug section,
3882 get a pointer to it. */
3883 if (debug_contents != NULL
3884 && sym._n._n_n._n_zeroes == 0
3885 && bfd_coff_symname_in_debug (sub, &sym))
3886 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3887 else
3888 name = NULL;
3890 /* Decide whether to copy this symbol to the output file. */
3891 csect = *csectpp;
3892 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3893 *sym_hash, csect, name);
3894 if (keep_p < 0)
3895 return FALSE;
3897 if (!keep_p)
3898 /* Use a debug_index of -2 to record that a symbol should
3899 be stripped. */
3900 *debug_index = -2;
3901 else
3903 /* See whether we should store the symbol name in the
3904 output .debug section. */
3905 if (name != NULL)
3907 bfd_size_type indx;
3909 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3910 if (indx == (bfd_size_type) -1)
3911 goto error_return;
3912 *debug_index = indx;
3914 else
3915 *debug_index = -1;
3916 if (*sym_hash != 0)
3917 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3918 if (*lineno_counts > 0)
3919 csect->output_section->lineno_count += *lineno_counts;
3922 esym += (sym.n_numaux + 1) * symesz;
3923 csectpp += sym.n_numaux + 1;
3924 sym_hash += sym.n_numaux + 1;
3925 lineno_counts += sym.n_numaux + 1;
3926 debug_index += sym.n_numaux + 1;
3929 if (debug_contents)
3931 free (debug_contents);
3932 debug_contents = NULL;
3934 /* Clear the size of subdeb, so that it is not included directly
3935 in the output file. */
3936 subdeb->size = 0;
3939 if (! info->keep_memory)
3941 if (! _bfd_coff_free_symbols (sub))
3942 goto error_return;
3946 if (info->strip != strip_all)
3947 xcoff_hash_table (info)->debug_section->size =
3948 _bfd_stringtab_size (debug_strtab);
3950 return TRUE;
3952 error_return:
3953 if (ldinfo.strings != NULL)
3954 free (ldinfo.strings);
3955 if (debug_contents != NULL)
3956 free (debug_contents);
3957 return FALSE;
3960 bfd_boolean
3961 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3962 const char *init,
3963 const char *fini,
3964 bfd_boolean rtld)
3966 struct bfd_in_memory *bim;
3968 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3969 if (bim == NULL)
3970 return FALSE;
3972 bim->size = 0;
3973 bim->buffer = 0;
3975 abfd->link_next = 0;
3976 abfd->format = bfd_object;
3977 abfd->iostream = (void *) bim;
3978 abfd->flags = BFD_IN_MEMORY;
3979 abfd->iovec = &_bfd_memory_iovec;
3980 abfd->direction = write_direction;
3981 abfd->origin = 0;
3982 abfd->where = 0;
3984 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3985 return FALSE;
3987 /* need to reset to unknown or it will not be read back in correctly */
3988 abfd->format = bfd_unknown;
3989 abfd->direction = read_direction;
3990 abfd->where = 0;
3992 return TRUE;
3995 /* Return the section that defines H. Return null if no section does. */
3997 static asection *
3998 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4000 switch (h->root.type)
4002 case bfd_link_hash_defined:
4003 case bfd_link_hash_defweak:
4004 return h->root.u.def.section;
4006 case bfd_link_hash_common:
4007 return h->root.u.c.p->section;
4009 default:
4010 return NULL;
4014 /* Add a .loader relocation for input relocation IREL. If the loader
4015 relocation should be against an output section, HSEC points to the
4016 input section that IREL is against, otherwise HSEC is null. H is the
4017 symbol that IREL is against, or null if it isn't against a global symbol.
4018 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4020 static bfd_boolean
4021 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4022 asection *output_section, bfd *reference_bfd,
4023 struct internal_reloc *irel, asection *hsec,
4024 struct xcoff_link_hash_entry *h)
4026 struct internal_ldrel ldrel;
4028 ldrel.l_vaddr = irel->r_vaddr;
4029 if (hsec != NULL)
4031 const char *secname;
4033 secname = hsec->output_section->name;
4034 if (strcmp (secname, ".text") == 0)
4035 ldrel.l_symndx = 0;
4036 else if (strcmp (secname, ".data") == 0)
4037 ldrel.l_symndx = 1;
4038 else if (strcmp (secname, ".bss") == 0)
4039 ldrel.l_symndx = 2;
4040 else
4042 (*_bfd_error_handler)
4043 (_("%B: loader reloc in unrecognized section `%s'"),
4044 reference_bfd, secname);
4045 bfd_set_error (bfd_error_nonrepresentable_section);
4046 return FALSE;
4049 else if (h != NULL)
4051 if (h->ldindx < 0)
4053 (*_bfd_error_handler)
4054 (_("%B: `%s' in loader reloc but not loader sym"),
4055 reference_bfd, h->root.root.string);
4056 bfd_set_error (bfd_error_bad_value);
4057 return FALSE;
4059 ldrel.l_symndx = h->ldindx;
4061 else
4062 ldrel.l_symndx = -(bfd_size_type) 1;
4064 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4065 ldrel.l_rsecnm = output_section->target_index;
4066 if (xcoff_hash_table (flinfo->info)->textro
4067 && strcmp (output_section->name, ".text") == 0)
4069 (*_bfd_error_handler)
4070 (_("%B: loader reloc in read-only section %A"),
4071 reference_bfd, output_section);
4072 bfd_set_error (bfd_error_invalid_operation);
4073 return FALSE;
4075 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4076 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4077 return TRUE;
4080 /* Link an input file into the linker output file. This function
4081 handles all the sections and relocations of the input file at once. */
4083 static bfd_boolean
4084 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4085 bfd *input_bfd)
4087 bfd *output_bfd;
4088 const char *strings;
4089 bfd_size_type syment_base;
4090 unsigned int n_tmask;
4091 unsigned int n_btshft;
4092 bfd_boolean copy, hash;
4093 bfd_size_type isymesz;
4094 bfd_size_type osymesz;
4095 bfd_size_type linesz;
4096 bfd_byte *esym;
4097 bfd_byte *esym_end;
4098 struct xcoff_link_hash_entry **sym_hash;
4099 struct internal_syment *isymp;
4100 asection **csectpp;
4101 unsigned int *lineno_counts;
4102 long *debug_index;
4103 long *indexp;
4104 unsigned long output_index;
4105 bfd_byte *outsym;
4106 unsigned int incls;
4107 asection *oline;
4108 bfd_boolean keep_syms;
4109 asection *o;
4111 /* We can just skip DYNAMIC files, unless this is a static link. */
4112 if ((input_bfd->flags & DYNAMIC) != 0
4113 && ! flinfo->info->static_link)
4114 return TRUE;
4116 /* Move all the symbols to the output file. */
4117 output_bfd = flinfo->output_bfd;
4118 strings = NULL;
4119 syment_base = obj_raw_syment_count (output_bfd);
4120 isymesz = bfd_coff_symesz (input_bfd);
4121 osymesz = bfd_coff_symesz (output_bfd);
4122 linesz = bfd_coff_linesz (input_bfd);
4123 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4125 n_tmask = coff_data (input_bfd)->local_n_tmask;
4126 n_btshft = coff_data (input_bfd)->local_n_btshft;
4128 /* Define macros so that ISFCN, et. al., macros work correctly. */
4129 #define N_TMASK n_tmask
4130 #define N_BTSHFT n_btshft
4132 copy = FALSE;
4133 if (! flinfo->info->keep_memory)
4134 copy = TRUE;
4135 hash = TRUE;
4136 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4137 hash = FALSE;
4139 if (! _bfd_coff_get_external_symbols (input_bfd))
4140 return FALSE;
4142 /* Make one pass over the symbols and assign indices to symbols that
4143 we have decided to keep. Also use create .loader symbol information
4144 and update information in hash table entries. */
4145 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4146 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4147 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4148 csectpp = xcoff_data (input_bfd)->csects;
4149 debug_index = xcoff_data (input_bfd)->debug_indices;
4150 isymp = flinfo->internal_syms;
4151 indexp = flinfo->sym_indices;
4152 output_index = syment_base;
4153 while (esym < esym_end)
4155 union internal_auxent aux;
4156 int smtyp = 0;
4157 int add;
4159 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4161 /* Read in the csect information, if any. */
4162 if (CSECT_SYM_P (isymp->n_sclass))
4164 BFD_ASSERT (isymp->n_numaux > 0);
4165 bfd_coff_swap_aux_in (input_bfd,
4166 (void *) (esym + isymesz * isymp->n_numaux),
4167 isymp->n_type, isymp->n_sclass,
4168 isymp->n_numaux - 1, isymp->n_numaux,
4169 (void *) &aux);
4171 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4174 /* If this symbol is in the .loader section, swap out the
4175 .loader symbol information. If this is an external symbol
4176 reference to a defined symbol, though, then wait until we get
4177 to the definition. */
4178 if (EXTERN_SYM_P (isymp->n_sclass)
4179 && *sym_hash != NULL
4180 && (*sym_hash)->ldsym != NULL
4181 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4183 struct xcoff_link_hash_entry *h;
4184 struct internal_ldsym *ldsym;
4186 h = *sym_hash;
4187 ldsym = h->ldsym;
4188 if (isymp->n_scnum > 0)
4190 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4191 ldsym->l_value = (isymp->n_value
4192 + (*csectpp)->output_section->vma
4193 + (*csectpp)->output_offset
4194 - (*csectpp)->vma);
4196 else
4198 ldsym->l_scnum = isymp->n_scnum;
4199 ldsym->l_value = isymp->n_value;
4202 ldsym->l_smtype = smtyp;
4203 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4204 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4205 || (h->flags & XCOFF_IMPORT) != 0)
4206 ldsym->l_smtype |= L_IMPORT;
4207 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4208 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4209 || (h->flags & XCOFF_EXPORT) != 0)
4210 ldsym->l_smtype |= L_EXPORT;
4211 if ((h->flags & XCOFF_ENTRY) != 0)
4212 ldsym->l_smtype |= L_ENTRY;
4213 if (isymp->n_sclass == C_AIX_WEAKEXT)
4214 ldsym->l_smtype |= L_WEAK;
4216 ldsym->l_smclas = aux.x_csect.x_smclas;
4218 if (ldsym->l_ifile == (bfd_size_type) -1)
4219 ldsym->l_ifile = 0;
4220 else if (ldsym->l_ifile == 0)
4222 if ((ldsym->l_smtype & L_IMPORT) == 0)
4223 ldsym->l_ifile = 0;
4224 else
4226 bfd *impbfd;
4228 if (h->root.type == bfd_link_hash_defined
4229 || h->root.type == bfd_link_hash_defweak)
4230 impbfd = h->root.u.def.section->owner;
4231 else if (h->root.type == bfd_link_hash_undefined
4232 || h->root.type == bfd_link_hash_undefweak)
4233 impbfd = h->root.u.undef.abfd;
4234 else
4235 impbfd = NULL;
4237 if (impbfd == NULL)
4238 ldsym->l_ifile = 0;
4239 else
4241 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4242 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4247 ldsym->l_parm = 0;
4249 BFD_ASSERT (h->ldindx >= 0);
4250 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4251 (flinfo->ldsym
4252 + ((h->ldindx - 3)
4253 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4254 h->ldsym = NULL;
4256 /* Fill in snentry now that we know the target_index. */
4257 if ((h->flags & XCOFF_ENTRY) != 0
4258 && (h->root.type == bfd_link_hash_defined
4259 || h->root.type == bfd_link_hash_defweak))
4261 xcoff_data (output_bfd)->snentry =
4262 h->root.u.def.section->output_section->target_index;
4266 add = 1 + isymp->n_numaux;
4268 if (*debug_index == -2)
4269 /* We've decided to strip this symbol. */
4270 *indexp = -1;
4271 else
4273 /* Assign the next unused index to this symbol. */
4274 *indexp = output_index;
4276 if (EXTERN_SYM_P (isymp->n_sclass))
4278 BFD_ASSERT (*sym_hash != NULL);
4279 (*sym_hash)->indx = output_index;
4282 /* If this is a symbol in the TOC which we may have merged
4283 (class XMC_TC), remember the symbol index of the TOC
4284 symbol. */
4285 if (isymp->n_sclass == C_HIDEXT
4286 && aux.x_csect.x_smclas == XMC_TC
4287 && *sym_hash != NULL)
4289 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4290 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4291 (*sym_hash)->u.toc_indx = output_index;
4294 output_index += add;
4297 esym += add * isymesz;
4298 isymp += add;
4299 csectpp += add;
4300 sym_hash += add;
4301 debug_index += add;
4302 ++indexp;
4303 for (--add; add > 0; --add)
4304 *indexp++ = -1;
4307 /* Now write out the symbols that we decided to keep. */
4309 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4310 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4311 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4312 isymp = flinfo->internal_syms;
4313 indexp = flinfo->sym_indices;
4314 csectpp = xcoff_data (input_bfd)->csects;
4315 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4316 debug_index = xcoff_data (input_bfd)->debug_indices;
4317 outsym = flinfo->outsyms;
4318 incls = 0;
4319 oline = NULL;
4320 while (esym < esym_end)
4322 int add;
4324 add = 1 + isymp->n_numaux;
4326 if (*indexp < 0)
4327 esym += add * isymesz;
4328 else
4330 struct internal_syment isym;
4331 int i;
4333 /* Adjust the symbol in order to output it. */
4334 isym = *isymp;
4335 if (isym._n._n_n._n_zeroes == 0
4336 && isym._n._n_n._n_offset != 0)
4338 /* This symbol has a long name. Enter it in the string
4339 table we are building. If *debug_index != -1, the
4340 name has already been entered in the .debug section. */
4341 if (*debug_index >= 0)
4342 isym._n._n_n._n_offset = *debug_index;
4343 else
4345 const char *name;
4346 bfd_size_type indx;
4348 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4350 if (name == NULL)
4351 return FALSE;
4352 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4353 if (indx == (bfd_size_type) -1)
4354 return FALSE;
4355 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4359 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4360 multiple definition problems when linking a shared object
4361 statically. (The native linker doesn't enter __rtinit into
4362 the normal table at all, but having a local symbol can make
4363 the objdump output easier to read.) */
4364 if (isym.n_sclass == C_EXT
4365 && *sym_hash
4366 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4367 isym.n_sclass = C_HIDEXT;
4369 /* The value of a C_FILE symbol is the symbol index of the
4370 next C_FILE symbol. The value of the last C_FILE symbol
4371 is -1. We try to get this right, below, just before we
4372 write the symbols out, but in the general case we may
4373 have to write the symbol out twice. */
4374 if (isym.n_sclass == C_FILE)
4376 if (flinfo->last_file_index != -1
4377 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4379 /* We must correct the value of the last C_FILE entry. */
4380 flinfo->last_file.n_value = *indexp;
4381 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4383 /* The last C_FILE symbol is in this input file. */
4384 bfd_coff_swap_sym_out (output_bfd,
4385 (void *) &flinfo->last_file,
4386 (void *) (flinfo->outsyms
4387 + ((flinfo->last_file_index
4388 - syment_base)
4389 * osymesz)));
4391 else
4393 /* We have already written out the last C_FILE
4394 symbol. We need to write it out again. We
4395 borrow *outsym temporarily. */
4396 file_ptr pos;
4398 bfd_coff_swap_sym_out (output_bfd,
4399 (void *) &flinfo->last_file,
4400 (void *) outsym);
4402 pos = obj_sym_filepos (output_bfd);
4403 pos += flinfo->last_file_index * osymesz;
4404 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4405 || (bfd_bwrite (outsym, osymesz, output_bfd)
4406 != osymesz))
4407 return FALSE;
4411 flinfo->last_file_index = *indexp;
4412 flinfo->last_file = isym;
4415 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4416 into the line numbers. We update the symbol values when
4417 we handle the line numbers. */
4418 if (isym.n_sclass == C_BINCL
4419 || isym.n_sclass == C_EINCL)
4421 isym.n_value = flinfo->line_filepos;
4422 ++incls;
4424 /* The value of a C_BSTAT symbol is the symbol table
4425 index of the containing csect. */
4426 else if (isym.n_sclass == C_BSTAT)
4428 bfd_vma indx;
4430 indx = isym.n_value;
4431 if (indx < obj_raw_syment_count (input_bfd))
4433 long symindx;
4435 symindx = flinfo->sym_indices[indx];
4436 if (symindx < 0)
4437 isym.n_value = 0;
4438 else
4439 isym.n_value = symindx;
4442 else if (isym.n_sclass != C_ESTAT
4443 && isym.n_sclass != C_DECL
4444 && isym.n_scnum > 0)
4446 isym.n_scnum = (*csectpp)->output_section->target_index;
4447 isym.n_value += ((*csectpp)->output_section->vma
4448 + (*csectpp)->output_offset
4449 - (*csectpp)->vma);
4452 /* Output the symbol. */
4453 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4455 esym += isymesz;
4456 outsym += osymesz;
4458 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4460 union internal_auxent aux;
4462 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4463 isymp->n_sclass, i, isymp->n_numaux,
4464 (void *) &aux);
4466 if (isymp->n_sclass == C_FILE)
4468 /* This is the file name (or some comment put in by
4469 the compiler). If it is long, we must put it in
4470 the string table. */
4471 if (aux.x_file.x_n.x_zeroes == 0
4472 && aux.x_file.x_n.x_offset != 0)
4474 const char *filename;
4475 bfd_size_type indx;
4477 BFD_ASSERT (aux.x_file.x_n.x_offset
4478 >= STRING_SIZE_SIZE);
4479 if (strings == NULL)
4481 strings = _bfd_coff_read_string_table (input_bfd);
4482 if (strings == NULL)
4483 return FALSE;
4485 filename = strings + aux.x_file.x_n.x_offset;
4486 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4487 hash, copy);
4488 if (indx == (bfd_size_type) -1)
4489 return FALSE;
4490 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4493 else if (CSECT_SYM_P (isymp->n_sclass)
4494 && i + 1 == isymp->n_numaux)
4497 /* We don't support type checking. I don't know if
4498 anybody does. */
4499 aux.x_csect.x_parmhash = 0;
4500 /* I don't think anybody uses these fields, but we'd
4501 better clobber them just in case. */
4502 aux.x_csect.x_stab = 0;
4503 aux.x_csect.x_snstab = 0;
4505 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4507 unsigned long indx;
4509 indx = aux.x_csect.x_scnlen.l;
4510 if (indx < obj_raw_syment_count (input_bfd))
4512 long symindx;
4514 symindx = flinfo->sym_indices[indx];
4515 if (symindx < 0)
4517 aux.x_csect.x_scnlen.l = 0;
4519 else
4521 aux.x_csect.x_scnlen.l = symindx;
4526 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4528 unsigned long indx;
4530 if (ISFCN (isymp->n_type)
4531 || ISTAG (isymp->n_sclass)
4532 || isymp->n_sclass == C_BLOCK
4533 || isymp->n_sclass == C_FCN)
4535 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4536 if (indx > 0
4537 && indx < obj_raw_syment_count (input_bfd))
4539 /* We look forward through the symbol for
4540 the index of the next symbol we are going
4541 to include. I don't know if this is
4542 entirely right. */
4543 while (flinfo->sym_indices[indx] < 0
4544 && indx < obj_raw_syment_count (input_bfd))
4545 ++indx;
4546 if (indx >= obj_raw_syment_count (input_bfd))
4547 indx = output_index;
4548 else
4549 indx = flinfo->sym_indices[indx];
4550 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4555 indx = aux.x_sym.x_tagndx.l;
4556 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4558 long symindx;
4560 symindx = flinfo->sym_indices[indx];
4561 if (symindx < 0)
4562 aux.x_sym.x_tagndx.l = 0;
4563 else
4564 aux.x_sym.x_tagndx.l = symindx;
4569 /* Copy over the line numbers, unless we are stripping
4570 them. We do this on a symbol by symbol basis in
4571 order to more easily handle garbage collection. */
4572 if (CSECT_SYM_P (isymp->n_sclass)
4573 && i == 0
4574 && isymp->n_numaux > 1
4575 && ISFCN (isymp->n_type)
4576 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4578 if (*lineno_counts == 0)
4579 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4580 else
4582 asection *enclosing;
4583 unsigned int enc_count;
4584 bfd_signed_vma linoff;
4585 struct internal_lineno lin;
4586 bfd_byte *linp;
4587 bfd_byte *linpend;
4588 bfd_vma offset;
4589 file_ptr pos;
4590 bfd_size_type amt;
4592 /* Read in the enclosing section's line-number
4593 information, if we haven't already. */
4594 o = *csectpp;
4595 enclosing = xcoff_section_data (abfd, o)->enclosing;
4596 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4597 if (oline != enclosing)
4599 pos = enclosing->line_filepos;
4600 amt = linesz * enc_count;
4601 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4602 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4603 != amt))
4604 return FALSE;
4605 oline = enclosing;
4608 /* Copy across the first entry, adjusting its
4609 symbol index. */
4610 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4611 - enclosing->line_filepos);
4612 linp = flinfo->linenos + linoff;
4613 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4614 lin.l_addr.l_symndx = *indexp;
4615 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4617 /* Copy the other entries, adjusting their addresses. */
4618 linpend = linp + *lineno_counts * linesz;
4619 offset = (o->output_section->vma
4620 + o->output_offset
4621 - o->vma);
4622 for (linp += linesz; linp < linpend; linp += linesz)
4624 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4625 lin.l_addr.l_paddr += offset;
4626 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4629 /* Write out the entries we've just processed. */
4630 pos = (o->output_section->line_filepos
4631 + o->output_section->lineno_count * linesz);
4632 amt = linesz * *lineno_counts;
4633 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4634 || bfd_bwrite (flinfo->linenos + linoff,
4635 amt, output_bfd) != amt)
4636 return FALSE;
4637 o->output_section->lineno_count += *lineno_counts;
4639 /* Record the offset of the symbol's line numbers
4640 in the output file. */
4641 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4643 if (incls > 0)
4645 struct internal_syment *iisp, *iispend;
4646 long *iindp;
4647 bfd_byte *oos;
4648 bfd_vma range_start, range_end;
4649 int iiadd;
4651 /* Update any C_BINCL or C_EINCL symbols
4652 that refer to a line number in the
4653 range we just output. */
4654 iisp = flinfo->internal_syms;
4655 iispend = iisp + obj_raw_syment_count (input_bfd);
4656 iindp = flinfo->sym_indices;
4657 oos = flinfo->outsyms;
4658 range_start = enclosing->line_filepos + linoff;
4659 range_end = range_start + *lineno_counts * linesz;
4660 while (iisp < iispend)
4662 if (*iindp >= 0
4663 && (iisp->n_sclass == C_BINCL
4664 || iisp->n_sclass == C_EINCL)
4665 && iisp->n_value >= range_start
4666 && iisp->n_value < range_end)
4668 struct internal_syment iis;
4670 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4671 iis.n_value = (iisp->n_value
4672 - range_start
4673 + pos);
4674 bfd_coff_swap_sym_out (output_bfd,
4675 &iis, oos);
4676 --incls;
4679 iiadd = 1 + iisp->n_numaux;
4680 if (*iindp >= 0)
4681 oos += iiadd * osymesz;
4682 iisp += iiadd;
4683 iindp += iiadd;
4689 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4690 isymp->n_sclass, i, isymp->n_numaux,
4691 (void *) outsym);
4692 outsym += osymesz;
4693 esym += isymesz;
4697 sym_hash += add;
4698 indexp += add;
4699 isymp += add;
4700 csectpp += add;
4701 lineno_counts += add;
4702 debug_index += add;
4705 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4706 symbol will be the first symbol in the next input file. In the
4707 normal case, this will save us from writing out the C_FILE symbol
4708 again. */
4709 if (flinfo->last_file_index != -1
4710 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4712 flinfo->last_file.n_value = output_index;
4713 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4714 (void *) (flinfo->outsyms
4715 + ((flinfo->last_file_index - syment_base)
4716 * osymesz)));
4719 /* Write the modified symbols to the output file. */
4720 if (outsym > flinfo->outsyms)
4722 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4723 bfd_size_type amt = outsym - flinfo->outsyms;
4724 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4725 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4726 return FALSE;
4728 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4729 + (outsym - flinfo->outsyms) / osymesz)
4730 == output_index);
4732 obj_raw_syment_count (output_bfd) = output_index;
4735 /* Don't let the linker relocation routines discard the symbols. */
4736 keep_syms = obj_coff_keep_syms (input_bfd);
4737 obj_coff_keep_syms (input_bfd) = TRUE;
4739 /* Relocate the contents of each section. */
4740 for (o = input_bfd->sections; o != NULL; o = o->next)
4742 bfd_byte *contents;
4744 if (! o->linker_mark)
4745 /* This section was omitted from the link. */
4746 continue;
4748 if ((o->flags & SEC_HAS_CONTENTS) == 0
4749 || o->size == 0
4750 || (o->flags & SEC_IN_MEMORY) != 0)
4751 continue;
4753 /* We have set filepos correctly for the sections we created to
4754 represent csects, so bfd_get_section_contents should work. */
4755 if (coff_section_data (input_bfd, o) != NULL
4756 && coff_section_data (input_bfd, o)->contents != NULL)
4757 contents = coff_section_data (input_bfd, o)->contents;
4758 else
4760 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4761 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4762 return FALSE;
4763 contents = flinfo->contents;
4766 if ((o->flags & SEC_RELOC) != 0)
4768 int target_index;
4769 struct internal_reloc *internal_relocs;
4770 struct internal_reloc *irel;
4771 bfd_vma offset;
4772 struct internal_reloc *irelend;
4773 struct xcoff_link_hash_entry **rel_hash;
4774 long r_symndx;
4776 /* Read in the relocs. */
4777 target_index = o->output_section->target_index;
4778 internal_relocs = (xcoff_read_internal_relocs
4779 (input_bfd, o, FALSE, flinfo->external_relocs,
4780 TRUE,
4781 (flinfo->section_info[target_index].relocs
4782 + o->output_section->reloc_count)));
4783 if (internal_relocs == NULL)
4784 return FALSE;
4786 /* Call processor specific code to relocate the section
4787 contents. */
4788 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4789 input_bfd, o,
4790 contents,
4791 internal_relocs,
4792 flinfo->internal_syms,
4793 xcoff_data (input_bfd)->csects))
4794 return FALSE;
4796 offset = o->output_section->vma + o->output_offset - o->vma;
4797 irel = internal_relocs;
4798 irelend = irel + o->reloc_count;
4799 rel_hash = (flinfo->section_info[target_index].rel_hashes
4800 + o->output_section->reloc_count);
4801 for (; irel < irelend; irel++, rel_hash++)
4803 struct xcoff_link_hash_entry *h = NULL;
4805 *rel_hash = NULL;
4807 /* Adjust the reloc address and symbol index. */
4809 irel->r_vaddr += offset;
4811 r_symndx = irel->r_symndx;
4813 if (r_symndx == -1)
4814 h = NULL;
4815 else
4816 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4818 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4820 if (h != NULL
4821 && h->smclas != XMC_TD
4822 && (irel->r_type == R_TOC
4823 || irel->r_type == R_GL
4824 || irel->r_type == R_TCL
4825 || irel->r_type == R_TRL
4826 || irel->r_type == R_TRLA))
4828 /* This is a TOC relative reloc with a symbol
4829 attached. The symbol should be the one which
4830 this reloc is for. We want to make this
4831 reloc against the TOC address of the symbol,
4832 not the symbol itself. */
4833 BFD_ASSERT (h->toc_section != NULL);
4834 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4835 if (h->u.toc_indx != -1)
4836 irel->r_symndx = h->u.toc_indx;
4837 else
4839 struct xcoff_toc_rel_hash *n;
4840 struct xcoff_link_section_info *si;
4841 bfd_size_type amt;
4843 amt = sizeof (* n);
4844 n = bfd_alloc (flinfo->output_bfd, amt);
4845 if (n == NULL)
4846 return FALSE;
4847 si = flinfo->section_info + target_index;
4848 n->next = si->toc_rel_hashes;
4849 n->h = h;
4850 n->rel = irel;
4851 si->toc_rel_hashes = n;
4854 else if (h != NULL)
4856 /* This is a global symbol. */
4857 if (h->indx >= 0)
4858 irel->r_symndx = h->indx;
4859 else
4861 /* This symbol is being written at the end
4862 of the file, and we do not yet know the
4863 symbol index. We save the pointer to the
4864 hash table entry in the rel_hash list.
4865 We set the indx field to -2 to indicate
4866 that this symbol must not be stripped. */
4867 *rel_hash = h;
4868 h->indx = -2;
4871 else
4873 long indx;
4875 indx = flinfo->sym_indices[r_symndx];
4877 if (indx == -1)
4879 struct internal_syment *is;
4881 /* Relocations against a TC0 TOC anchor are
4882 automatically transformed to be against
4883 the TOC anchor in the output file. */
4884 is = flinfo->internal_syms + r_symndx;
4885 if (is->n_sclass == C_HIDEXT
4886 && is->n_numaux > 0)
4888 void * auxptr;
4889 union internal_auxent aux;
4891 auxptr = ((void *)
4892 (((bfd_byte *)
4893 obj_coff_external_syms (input_bfd))
4894 + ((r_symndx + is->n_numaux)
4895 * isymesz)));
4896 bfd_coff_swap_aux_in (input_bfd, auxptr,
4897 is->n_type, is->n_sclass,
4898 is->n_numaux - 1,
4899 is->n_numaux,
4900 (void *) &aux);
4901 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4902 && aux.x_csect.x_smclas == XMC_TC0)
4903 indx = flinfo->toc_symindx;
4907 if (indx != -1)
4908 irel->r_symndx = indx;
4909 else
4912 struct internal_syment *is;
4914 const char *name;
4915 char buf[SYMNMLEN + 1];
4917 /* This reloc is against a symbol we are
4918 stripping. It would be possible to handle
4919 this case, but I don't think it's worth it. */
4920 is = flinfo->internal_syms + r_symndx;
4922 name = (_bfd_coff_internal_syment_name
4923 (input_bfd, is, buf));
4925 if (name == NULL)
4926 return FALSE;
4928 if (! ((*flinfo->info->callbacks->unattached_reloc)
4929 (flinfo->info, name, input_bfd, o,
4930 irel->r_vaddr)))
4931 return FALSE;
4936 if (xcoff_need_ldrel_p (flinfo->info, irel, h))
4938 asection *sec;
4940 if (r_symndx == -1)
4941 sec = NULL;
4942 else if (h == NULL)
4943 sec = xcoff_data (input_bfd)->csects[r_symndx];
4944 else
4945 sec = xcoff_symbol_section (h);
4946 if (!xcoff_create_ldrel (output_bfd, flinfo,
4947 o->output_section, input_bfd,
4948 irel, sec, h))
4949 return FALSE;
4953 o->output_section->reloc_count += o->reloc_count;
4956 /* Write out the modified section contents. */
4957 if (! bfd_set_section_contents (output_bfd, o->output_section,
4958 contents, (file_ptr) o->output_offset,
4959 o->size))
4960 return FALSE;
4963 obj_coff_keep_syms (input_bfd) = keep_syms;
4965 if (! flinfo->info->keep_memory)
4967 if (! _bfd_coff_free_symbols (input_bfd))
4968 return FALSE;
4971 return TRUE;
4974 #undef N_TMASK
4975 #undef N_BTSHFT
4977 /* Sort relocs by VMA. This is called via qsort. */
4979 static int
4980 xcoff_sort_relocs (const void * p1, const void * p2)
4982 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4983 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4985 if (r1->r_vaddr > r2->r_vaddr)
4986 return 1;
4987 else if (r1->r_vaddr < r2->r_vaddr)
4988 return -1;
4989 else
4990 return 0;
4993 /* Return true if section SEC is a TOC section. */
4995 static inline bfd_boolean
4996 xcoff_toc_section_p (asection *sec)
4998 const char *name;
5000 name = sec->name;
5001 if (name[0] == '.' && name[1] == 't')
5003 if (name[2] == 'c')
5005 if (name[3] == '0' && name[4] == 0)
5006 return TRUE;
5007 if (name[3] == 0)
5008 return TRUE;
5010 if (name[2] == 'd' && name[3] == 0)
5011 return TRUE;
5013 return FALSE;
5016 /* See if the link requires a TOC (it usually does!). If so, find a
5017 good place to put the TOC anchor csect, and write out the associated
5018 symbol. */
5020 static bfd_boolean
5021 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5023 bfd_vma toc_start, toc_end, start, end, best_address;
5024 asection *sec;
5025 bfd *input_bfd;
5026 int section_index;
5027 struct internal_syment irsym;
5028 union internal_auxent iraux;
5029 file_ptr pos;
5030 size_t size;
5032 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5033 index of a csect at the beginning of the TOC. */
5034 toc_start = ~(bfd_vma) 0;
5035 toc_end = 0;
5036 section_index = -1;
5037 for (input_bfd = flinfo->info->input_bfds;
5038 input_bfd != NULL;
5039 input_bfd = input_bfd->link_next)
5040 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5041 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5043 start = sec->output_section->vma + sec->output_offset;
5044 if (toc_start > start)
5046 toc_start = start;
5047 section_index = sec->output_section->target_index;
5050 end = start + sec->size;
5051 if (toc_end < end)
5052 toc_end = end;
5055 /* There's no need for a TC0 symbol if we don't have a TOC. */
5056 if (toc_end < toc_start)
5058 xcoff_data (output_bfd)->toc = toc_start;
5059 return TRUE;
5062 if (toc_end - toc_start < 0x8000)
5063 /* Every TOC csect can be accessed from TOC_START. */
5064 best_address = toc_start;
5065 else
5067 /* Find the lowest TOC csect that is still within range of TOC_END. */
5068 best_address = toc_end;
5069 for (input_bfd = flinfo->info->input_bfds;
5070 input_bfd != NULL;
5071 input_bfd = input_bfd->link_next)
5072 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5073 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5075 start = sec->output_section->vma + sec->output_offset;
5076 if (start < best_address
5077 && start + 0x8000 >= toc_end)
5079 best_address = start;
5080 section_index = sec->output_section->target_index;
5084 /* Make sure that the start of the TOC is also within range. */
5085 if (best_address > toc_start + 0x8000)
5087 (*_bfd_error_handler)
5088 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5089 "when compiling"),
5090 (unsigned long) (toc_end - toc_start));
5091 bfd_set_error (bfd_error_file_too_big);
5092 return FALSE;
5096 /* Record the chosen TOC value. */
5097 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5098 xcoff_data (output_bfd)->toc = best_address;
5099 xcoff_data (output_bfd)->sntoc = section_index;
5101 /* Fill out the TC0 symbol. */
5102 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &irsym, "TOC"))
5103 return FALSE;
5104 irsym.n_value = best_address;
5105 irsym.n_scnum = section_index;
5106 irsym.n_sclass = C_HIDEXT;
5107 irsym.n_type = T_NULL;
5108 irsym.n_numaux = 1;
5109 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5111 /* Fill out the auxillary csect information. */
5112 memset (&iraux, 0, sizeof iraux);
5113 iraux.x_csect.x_smtyp = XTY_SD;
5114 iraux.x_csect.x_smclas = XMC_TC0;
5115 iraux.x_csect.x_scnlen.l = 0;
5116 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5117 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5119 /* Write the contents to the file. */
5120 pos = obj_sym_filepos (output_bfd);
5121 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5122 size = 2 * bfd_coff_symesz (output_bfd);
5123 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5124 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5125 return FALSE;
5126 obj_raw_syment_count (output_bfd) += 2;
5128 return TRUE;
5131 /* Write out a non-XCOFF global symbol. */
5133 static bfd_boolean
5134 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
5136 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5137 bfd *output_bfd;
5138 bfd_byte *outsym;
5139 struct internal_syment isym;
5140 union internal_auxent aux;
5141 bfd_boolean result;
5142 file_ptr pos;
5143 bfd_size_type amt;
5145 output_bfd = flinfo->output_bfd;
5146 outsym = flinfo->outsyms;
5148 if (h->root.type == bfd_link_hash_warning)
5150 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5151 if (h->root.type == bfd_link_hash_new)
5152 return TRUE;
5155 /* If this symbol was garbage collected, just skip it. */
5156 if (xcoff_hash_table (flinfo->info)->gc
5157 && (h->flags & XCOFF_MARK) == 0)
5158 return TRUE;
5160 /* If we need a .loader section entry, write it out. */
5161 if (h->ldsym != NULL)
5163 struct internal_ldsym *ldsym;
5164 bfd *impbfd;
5166 ldsym = h->ldsym;
5168 if (h->root.type == bfd_link_hash_undefined
5169 || h->root.type == bfd_link_hash_undefweak)
5172 ldsym->l_value = 0;
5173 ldsym->l_scnum = N_UNDEF;
5174 ldsym->l_smtype = XTY_ER;
5175 impbfd = h->root.u.undef.abfd;
5178 else if (h->root.type == bfd_link_hash_defined
5179 || h->root.type == bfd_link_hash_defweak)
5181 asection *sec;
5183 sec = h->root.u.def.section;
5184 ldsym->l_value = (sec->output_section->vma
5185 + sec->output_offset
5186 + h->root.u.def.value);
5187 ldsym->l_scnum = sec->output_section->target_index;
5188 ldsym->l_smtype = XTY_SD;
5189 impbfd = sec->owner;
5192 else
5193 abort ();
5195 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5196 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5197 || (h->flags & XCOFF_IMPORT) != 0)
5198 /* Clear l_smtype
5199 Import symbols are defined so the check above will make
5200 the l_smtype XTY_SD. But this is not correct, it should
5201 be cleared. */
5202 ldsym->l_smtype |= L_IMPORT;
5204 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5205 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5206 || (h->flags & XCOFF_EXPORT) != 0)
5207 ldsym->l_smtype |= L_EXPORT;
5209 if ((h->flags & XCOFF_ENTRY) != 0)
5210 ldsym->l_smtype |= L_ENTRY;
5212 if ((h->flags & XCOFF_RTINIT) != 0)
5213 ldsym->l_smtype = XTY_SD;
5215 ldsym->l_smclas = h->smclas;
5217 if (ldsym->l_smtype & L_IMPORT)
5219 if ((h->root.type == bfd_link_hash_defined
5220 || h->root.type == bfd_link_hash_defweak)
5221 && (h->root.u.def.value != 0))
5222 ldsym->l_smclas = XMC_XO;
5224 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5225 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5226 ldsym->l_smclas = XMC_SV3264;
5228 else if (h->flags & XCOFF_SYSCALL32)
5229 ldsym->l_smclas = XMC_SV;
5231 else if (h->flags & XCOFF_SYSCALL64)
5232 ldsym->l_smclas = XMC_SV64;
5235 if (ldsym->l_ifile == -(bfd_size_type) 1)
5237 ldsym->l_ifile = 0;
5239 else if (ldsym->l_ifile == 0)
5241 if ((ldsym->l_smtype & L_IMPORT) == 0)
5242 ldsym->l_ifile = 0;
5243 else if (impbfd == NULL)
5244 ldsym->l_ifile = 0;
5245 else
5247 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5248 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5252 ldsym->l_parm = 0;
5254 BFD_ASSERT (h->ldindx >= 0);
5256 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5257 (flinfo->ldsym +
5258 (h->ldindx - 3)
5259 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5260 h->ldsym = NULL;
5263 /* If this symbol needs global linkage code, write it out. */
5264 if (h->root.type == bfd_link_hash_defined
5265 && (h->root.u.def.section
5266 == xcoff_hash_table (flinfo->info)->linkage_section))
5268 bfd_byte *p;
5269 bfd_vma tocoff;
5270 unsigned int i;
5272 p = h->root.u.def.section->contents + h->root.u.def.value;
5274 /* The first instruction in the global linkage code loads a
5275 specific TOC element. */
5276 tocoff = (h->descriptor->toc_section->output_section->vma
5277 + h->descriptor->toc_section->output_offset
5278 - xcoff_data (output_bfd)->toc);
5280 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5281 tocoff += h->descriptor->u.toc_offset;
5283 /* The first instruction in the glink code needs to be
5284 cooked to to hold the correct offset in the toc. The
5285 rest are just output raw. */
5286 bfd_put_32 (output_bfd,
5287 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5289 /* Start with i == 1 to get past the first instruction done above
5290 The /4 is because the glink code is in bytes and we are going
5291 4 at a pop. */
5292 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5293 bfd_put_32 (output_bfd,
5294 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5295 &p[4 * i]);
5298 /* If we created a TOC entry for this symbol, write out the required
5299 relocs. */
5300 if ((h->flags & XCOFF_SET_TOC) != 0)
5302 asection *tocsec;
5303 asection *osec;
5304 int oindx;
5305 struct internal_reloc *irel;
5306 struct internal_syment irsym;
5307 union internal_auxent iraux;
5309 tocsec = h->toc_section;
5310 osec = tocsec->output_section;
5311 oindx = osec->target_index;
5312 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5313 irel->r_vaddr = (osec->vma
5314 + tocsec->output_offset
5315 + h->u.toc_offset);
5317 if (h->indx >= 0)
5318 irel->r_symndx = h->indx;
5319 else
5321 h->indx = -2;
5322 irel->r_symndx = obj_raw_syment_count (output_bfd);
5325 BFD_ASSERT (h->ldindx >= 0);
5327 /* Initialize the aux union here instead of closer to when it is
5328 written out below because the length of the csect depends on
5329 whether the output is 32 or 64 bit. */
5330 memset (&iraux, 0, sizeof iraux);
5331 iraux.x_csect.x_smtyp = XTY_SD;
5332 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5333 iraux.x_csect.x_smclas = XMC_TC;
5335 /* 32 bit uses a 32 bit R_POS to do the relocations
5336 64 bit uses a 64 bit R_POS to do the relocations
5338 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5340 Which one is determined by the backend. */
5341 if (bfd_xcoff_is_xcoff64 (output_bfd))
5343 irel->r_size = 63;
5344 iraux.x_csect.x_scnlen.l = 8;
5346 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5348 irel->r_size = 31;
5349 iraux.x_csect.x_scnlen.l = 4;
5351 else
5352 return FALSE;
5354 irel->r_type = R_POS;
5355 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5356 ++osec->reloc_count;
5358 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5359 output_bfd, irel, NULL, h))
5360 return FALSE;
5362 /* We need to emit a symbol to define a csect which holds
5363 the reloc. */
5364 if (flinfo->info->strip != strip_all)
5366 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab,
5367 &irsym, h->root.root.string);
5368 if (!result)
5369 return FALSE;
5371 irsym.n_value = irel->r_vaddr;
5372 irsym.n_scnum = osec->target_index;
5373 irsym.n_sclass = C_HIDEXT;
5374 irsym.n_type = T_NULL;
5375 irsym.n_numaux = 1;
5377 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5378 outsym += bfd_coff_symesz (output_bfd);
5380 /* Note : iraux is initialized above. */
5381 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5382 0, 1, (void *) outsym);
5383 outsym += bfd_coff_auxesz (output_bfd);
5385 if (h->indx >= 0)
5387 /* We aren't going to write out the symbols below, so we
5388 need to write them out now. */
5389 pos = obj_sym_filepos (output_bfd);
5390 pos += (obj_raw_syment_count (output_bfd)
5391 * bfd_coff_symesz (output_bfd));
5392 amt = outsym - flinfo->outsyms;
5393 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5394 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5395 return FALSE;
5396 obj_raw_syment_count (output_bfd) +=
5397 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5399 outsym = flinfo->outsyms;
5404 /* If this symbol is a specially defined function descriptor, write
5405 it out. The first word is the address of the function code
5406 itself, the second word is the address of the TOC, and the third
5407 word is zero.
5409 32 bit vs 64 bit
5410 The addresses for the 32 bit will take 4 bytes and the addresses
5411 for 64 bit will take 8 bytes. Similar for the relocs. This type
5412 of logic was also done above to create a TOC entry in
5413 xcoff_write_global_symbol. */
5414 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5415 && h->root.type == bfd_link_hash_defined
5416 && (h->root.u.def.section
5417 == xcoff_hash_table (flinfo->info)->descriptor_section))
5419 asection *sec;
5420 asection *osec;
5421 int oindx;
5422 bfd_byte *p;
5423 struct xcoff_link_hash_entry *hentry;
5424 asection *esec;
5425 struct internal_reloc *irel;
5426 asection *tsec;
5427 unsigned int reloc_size, byte_size;
5429 if (bfd_xcoff_is_xcoff64 (output_bfd))
5431 reloc_size = 63;
5432 byte_size = 8;
5434 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5436 reloc_size = 31;
5437 byte_size = 4;
5439 else
5440 return FALSE;
5442 sec = h->root.u.def.section;
5443 osec = sec->output_section;
5444 oindx = osec->target_index;
5445 p = sec->contents + h->root.u.def.value;
5447 hentry = h->descriptor;
5448 BFD_ASSERT (hentry != NULL
5449 && (hentry->root.type == bfd_link_hash_defined
5450 || hentry->root.type == bfd_link_hash_defweak));
5451 esec = hentry->root.u.def.section;
5453 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5454 irel->r_vaddr = (osec->vma
5455 + sec->output_offset
5456 + h->root.u.def.value);
5457 irel->r_symndx = esec->output_section->target_index;
5458 irel->r_type = R_POS;
5459 irel->r_size = reloc_size;
5460 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5461 ++osec->reloc_count;
5463 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5464 output_bfd, irel, esec, NULL))
5465 return FALSE;
5467 /* There are three items to write out,
5468 the address of the code
5469 the address of the toc anchor
5470 the environment pointer.
5471 We are ignoring the environment pointer. So set it to zero. */
5472 if (bfd_xcoff_is_xcoff64 (output_bfd))
5474 bfd_put_64 (output_bfd,
5475 (esec->output_section->vma + esec->output_offset
5476 + hentry->root.u.def.value),
5478 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5479 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5481 else
5483 /* 32 bit backend
5484 This logic was already called above so the error case where
5485 the backend is neither has already been checked. */
5486 bfd_put_32 (output_bfd,
5487 (esec->output_section->vma + esec->output_offset
5488 + hentry->root.u.def.value),
5490 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5491 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5494 tsec = coff_section_from_bfd_index (output_bfd,
5495 xcoff_data (output_bfd)->sntoc);
5497 ++irel;
5498 irel->r_vaddr = (osec->vma
5499 + sec->output_offset
5500 + h->root.u.def.value
5501 + byte_size);
5502 irel->r_symndx = tsec->output_section->target_index;
5503 irel->r_type = R_POS;
5504 irel->r_size = reloc_size;
5505 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5506 ++osec->reloc_count;
5508 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5509 output_bfd, irel, tsec, NULL))
5510 return FALSE;
5513 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5515 BFD_ASSERT (outsym == flinfo->outsyms);
5516 return TRUE;
5519 if (h->indx != -2
5520 && (flinfo->info->strip == strip_all
5521 || (flinfo->info->strip == strip_some
5522 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5523 FALSE, FALSE) == NULL)))
5525 BFD_ASSERT (outsym == flinfo->outsyms);
5526 return TRUE;
5529 if (h->indx != -2
5530 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5532 BFD_ASSERT (outsym == flinfo->outsyms);
5533 return TRUE;
5536 memset (&aux, 0, sizeof aux);
5538 h->indx = obj_raw_syment_count (output_bfd);
5540 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &isym,
5541 h->root.root.string);
5542 if (!result)
5543 return FALSE;
5545 if (h->root.type == bfd_link_hash_undefined
5546 || h->root.type == bfd_link_hash_undefweak)
5548 isym.n_value = 0;
5549 isym.n_scnum = N_UNDEF;
5550 if (h->root.type == bfd_link_hash_undefweak
5551 && C_WEAKEXT == C_AIX_WEAKEXT)
5552 isym.n_sclass = C_WEAKEXT;
5553 else
5554 isym.n_sclass = C_EXT;
5555 aux.x_csect.x_smtyp = XTY_ER;
5557 else if ((h->root.type == bfd_link_hash_defined
5558 || h->root.type == bfd_link_hash_defweak)
5559 && h->smclas == XMC_XO)
5561 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5562 isym.n_value = h->root.u.def.value;
5563 isym.n_scnum = N_UNDEF;
5564 if (h->root.type == bfd_link_hash_undefweak
5565 && C_WEAKEXT == C_AIX_WEAKEXT)
5566 isym.n_sclass = C_WEAKEXT;
5567 else
5568 isym.n_sclass = C_EXT;
5569 aux.x_csect.x_smtyp = XTY_ER;
5571 else if (h->root.type == bfd_link_hash_defined
5572 || h->root.type == bfd_link_hash_defweak)
5574 struct xcoff_link_size_list *l;
5576 isym.n_value = (h->root.u.def.section->output_section->vma
5577 + h->root.u.def.section->output_offset
5578 + h->root.u.def.value);
5579 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5580 isym.n_scnum = N_ABS;
5581 else
5582 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5583 isym.n_sclass = C_HIDEXT;
5584 aux.x_csect.x_smtyp = XTY_SD;
5586 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5588 for (l = xcoff_hash_table (flinfo->info)->size_list;
5589 l != NULL;
5590 l = l->next)
5592 if (l->h == h)
5594 aux.x_csect.x_scnlen.l = l->size;
5595 break;
5600 else if (h->root.type == bfd_link_hash_common)
5602 isym.n_value = (h->root.u.c.p->section->output_section->vma
5603 + h->root.u.c.p->section->output_offset);
5604 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5605 isym.n_sclass = C_EXT;
5606 aux.x_csect.x_smtyp = XTY_CM;
5607 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5609 else
5610 abort ();
5612 isym.n_type = T_NULL;
5613 isym.n_numaux = 1;
5615 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5616 outsym += bfd_coff_symesz (output_bfd);
5618 aux.x_csect.x_smclas = h->smclas;
5619 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5620 (void *) outsym);
5621 outsym += bfd_coff_auxesz (output_bfd);
5623 if ((h->root.type == bfd_link_hash_defined
5624 || h->root.type == bfd_link_hash_defweak)
5625 && h->smclas != XMC_XO)
5627 /* We just output an SD symbol. Now output an LD symbol. */
5628 h->indx += 2;
5630 if (h->root.type == bfd_link_hash_undefweak
5631 && C_WEAKEXT == C_AIX_WEAKEXT)
5632 isym.n_sclass = C_WEAKEXT;
5633 else
5634 isym.n_sclass = C_EXT;
5635 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5636 outsym += bfd_coff_symesz (output_bfd);
5638 aux.x_csect.x_smtyp = XTY_LD;
5639 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5640 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5641 (void *) outsym);
5642 outsym += bfd_coff_auxesz (output_bfd);
5645 pos = obj_sym_filepos (output_bfd);
5646 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5647 amt = outsym - flinfo->outsyms;
5648 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5649 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5650 return FALSE;
5651 obj_raw_syment_count (output_bfd) +=
5652 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5654 return TRUE;
5657 /* Handle a link order which is supposed to generate a reloc. */
5659 static bfd_boolean
5660 xcoff_reloc_link_order (bfd *output_bfd,
5661 struct xcoff_final_link_info *flinfo,
5662 asection *output_section,
5663 struct bfd_link_order *link_order)
5665 reloc_howto_type *howto;
5666 struct xcoff_link_hash_entry *h;
5667 asection *hsec;
5668 bfd_vma hval;
5669 bfd_vma addend;
5670 struct internal_reloc *irel;
5671 struct xcoff_link_hash_entry **rel_hash_ptr;
5673 if (link_order->type == bfd_section_reloc_link_order)
5674 /* We need to somehow locate a symbol in the right section. The
5675 symbol must either have a value of zero, or we must adjust
5676 the addend by the value of the symbol. FIXME: Write this
5677 when we need it. The old linker couldn't handle this anyhow. */
5678 abort ();
5680 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5681 if (howto == NULL)
5683 bfd_set_error (bfd_error_bad_value);
5684 return FALSE;
5687 h = ((struct xcoff_link_hash_entry *)
5688 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5689 link_order->u.reloc.p->u.name,
5690 FALSE, FALSE, TRUE));
5691 if (h == NULL)
5693 if (! ((*flinfo->info->callbacks->unattached_reloc)
5694 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5695 return FALSE;
5696 return TRUE;
5699 hsec = xcoff_symbol_section (h);
5700 if (h->root.type == bfd_link_hash_defined
5701 || h->root.type == bfd_link_hash_defweak)
5702 hval = h->root.u.def.value;
5703 else
5704 hval = 0;
5706 addend = link_order->u.reloc.p->addend;
5707 if (hsec != NULL)
5708 addend += (hsec->output_section->vma
5709 + hsec->output_offset
5710 + hval);
5712 if (addend != 0)
5714 bfd_size_type size;
5715 bfd_byte *buf;
5716 bfd_reloc_status_type rstat;
5717 bfd_boolean ok;
5719 size = bfd_get_reloc_size (howto);
5720 buf = bfd_zmalloc (size);
5721 if (buf == NULL)
5722 return FALSE;
5724 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5725 switch (rstat)
5727 case bfd_reloc_ok:
5728 break;
5729 default:
5730 case bfd_reloc_outofrange:
5731 abort ();
5732 case bfd_reloc_overflow:
5733 if (! ((*flinfo->info->callbacks->reloc_overflow)
5734 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5735 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5737 free (buf);
5738 return FALSE;
5740 break;
5742 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5743 (file_ptr) link_order->offset, size);
5744 free (buf);
5745 if (! ok)
5746 return FALSE;
5749 /* Store the reloc information in the right place. It will get
5750 swapped and written out at the end of the final_link routine. */
5751 irel = (flinfo->section_info[output_section->target_index].relocs
5752 + output_section->reloc_count);
5753 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5754 + output_section->reloc_count);
5756 memset (irel, 0, sizeof (struct internal_reloc));
5757 *rel_hash_ptr = NULL;
5759 irel->r_vaddr = output_section->vma + link_order->offset;
5761 if (h->indx >= 0)
5762 irel->r_symndx = h->indx;
5763 else
5765 /* Set the index to -2 to force this symbol to get written out. */
5766 h->indx = -2;
5767 *rel_hash_ptr = h;
5768 irel->r_symndx = 0;
5771 irel->r_type = howto->type;
5772 irel->r_size = howto->bitsize - 1;
5773 if (howto->complain_on_overflow == complain_overflow_signed)
5774 irel->r_size |= 0x80;
5776 ++output_section->reloc_count;
5778 /* Now output the reloc to the .loader section. */
5779 if (xcoff_hash_table (flinfo->info)->loader_section)
5781 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5782 output_bfd, irel, hsec, h))
5783 return FALSE;
5786 return TRUE;
5789 /* Do the final link step. */
5791 bfd_boolean
5792 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5794 bfd_size_type symesz;
5795 struct xcoff_final_link_info flinfo;
5796 asection *o;
5797 struct bfd_link_order *p;
5798 bfd_size_type max_contents_size;
5799 bfd_size_type max_sym_count;
5800 bfd_size_type max_lineno_count;
5801 bfd_size_type max_reloc_count;
5802 bfd_size_type max_output_reloc_count;
5803 file_ptr rel_filepos;
5804 unsigned int relsz;
5805 file_ptr line_filepos;
5806 unsigned int linesz;
5807 bfd *sub;
5808 bfd_byte *external_relocs = NULL;
5809 char strbuf[STRING_SIZE_SIZE];
5810 file_ptr pos;
5811 bfd_size_type amt;
5813 if (info->shared)
5814 abfd->flags |= DYNAMIC;
5816 symesz = bfd_coff_symesz (abfd);
5818 flinfo.info = info;
5819 flinfo.output_bfd = abfd;
5820 flinfo.strtab = NULL;
5821 flinfo.section_info = NULL;
5822 flinfo.last_file_index = -1;
5823 flinfo.toc_symindx = -1;
5824 flinfo.internal_syms = NULL;
5825 flinfo.sym_indices = NULL;
5826 flinfo.outsyms = NULL;
5827 flinfo.linenos = NULL;
5828 flinfo.contents = NULL;
5829 flinfo.external_relocs = NULL;
5831 if (xcoff_hash_table (info)->loader_section)
5833 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5834 + bfd_xcoff_ldhdrsz (abfd));
5835 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5836 + bfd_xcoff_ldhdrsz (abfd)
5837 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5838 * bfd_xcoff_ldsymsz (abfd)));
5840 else
5842 flinfo.ldsym = NULL;
5843 flinfo.ldrel = NULL;
5846 xcoff_data (abfd)->coff.link_info = info;
5848 flinfo.strtab = _bfd_stringtab_init ();
5849 if (flinfo.strtab == NULL)
5850 goto error_return;
5852 /* Count the relocation entries required for the output file.
5853 (We've already counted the line numbers.) Determine a few
5854 maximum sizes. */
5855 max_contents_size = 0;
5856 max_lineno_count = 0;
5857 max_reloc_count = 0;
5858 for (o = abfd->sections; o != NULL; o = o->next)
5860 o->reloc_count = 0;
5861 for (p = o->map_head.link_order; p != NULL; p = p->next)
5863 if (p->type == bfd_indirect_link_order)
5865 asection *sec;
5867 sec = p->u.indirect.section;
5869 /* Mark all sections which are to be included in the
5870 link. This will normally be every section. We need
5871 to do this so that we can identify any sections which
5872 the linker has decided to not include. */
5873 sec->linker_mark = TRUE;
5875 o->reloc_count += sec->reloc_count;
5877 if ((sec->flags & SEC_IN_MEMORY) == 0)
5879 if (sec->rawsize > max_contents_size)
5880 max_contents_size = sec->rawsize;
5881 if (sec->size > max_contents_size)
5882 max_contents_size = sec->size;
5884 if (coff_section_data (sec->owner, sec) != NULL
5885 && xcoff_section_data (sec->owner, sec) != NULL
5886 && (xcoff_section_data (sec->owner, sec)->lineno_count
5887 > max_lineno_count))
5888 max_lineno_count =
5889 xcoff_section_data (sec->owner, sec)->lineno_count;
5890 if (sec->reloc_count > max_reloc_count)
5891 max_reloc_count = sec->reloc_count;
5893 else if (p->type == bfd_section_reloc_link_order
5894 || p->type == bfd_symbol_reloc_link_order)
5895 ++o->reloc_count;
5899 /* Compute the file positions for all the sections. */
5900 if (abfd->output_has_begun)
5902 if (xcoff_hash_table (info)->file_align != 0)
5903 abort ();
5905 else
5907 bfd_vma file_align;
5909 file_align = xcoff_hash_table (info)->file_align;
5910 if (file_align != 0)
5912 bfd_boolean saw_contents;
5913 int indx;
5914 file_ptr sofar;
5916 /* Insert .pad sections before every section which has
5917 contents and is loaded, if it is preceded by some other
5918 section which has contents and is loaded. */
5919 saw_contents = TRUE;
5920 for (o = abfd->sections; o != NULL; o = o->next)
5922 if (strcmp (o->name, ".pad") == 0)
5923 saw_contents = FALSE;
5924 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5925 && (o->flags & SEC_LOAD) != 0)
5927 if (! saw_contents)
5928 saw_contents = TRUE;
5929 else
5931 asection *n;
5933 /* Create a pad section and place it before the section
5934 that needs padding. This requires unlinking and
5935 relinking the bfd's section list. */
5937 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5938 SEC_HAS_CONTENTS);
5939 n->alignment_power = 0;
5941 bfd_section_list_remove (abfd, n);
5942 bfd_section_list_insert_before (abfd, o, n);
5943 saw_contents = FALSE;
5948 /* Reset the section indices after inserting the new
5949 sections. */
5950 indx = 0;
5951 for (o = abfd->sections; o != NULL; o = o->next)
5953 ++indx;
5954 o->target_index = indx;
5956 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5958 /* Work out appropriate sizes for the .pad sections to force
5959 each section to land on a page boundary. This bit of
5960 code knows what compute_section_file_positions is going
5961 to do. */
5962 sofar = bfd_coff_filhsz (abfd);
5963 sofar += bfd_coff_aoutsz (abfd);
5964 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5965 for (o = abfd->sections; o != NULL; o = o->next)
5966 if ((bfd_xcoff_is_reloc_count_overflow
5967 (abfd, (bfd_vma) o->reloc_count))
5968 || (bfd_xcoff_is_lineno_count_overflow
5969 (abfd, (bfd_vma) o->lineno_count)))
5970 /* 64 does not overflow, need to check if 32 does */
5971 sofar += bfd_coff_scnhsz (abfd);
5973 for (o = abfd->sections; o != NULL; o = o->next)
5975 if (strcmp (o->name, ".pad") == 0)
5977 bfd_vma pageoff;
5979 BFD_ASSERT (o->size == 0);
5980 pageoff = sofar & (file_align - 1);
5981 if (pageoff != 0)
5983 o->size = file_align - pageoff;
5984 sofar += file_align - pageoff;
5985 o->flags |= SEC_HAS_CONTENTS;
5988 else
5990 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5991 sofar += BFD_ALIGN (o->size,
5992 1 << o->alignment_power);
5997 if (! bfd_coff_compute_section_file_positions (abfd))
5998 goto error_return;
6001 /* Allocate space for the pointers we need to keep for the relocs. */
6003 unsigned int i;
6005 /* We use section_count + 1, rather than section_count, because
6006 the target_index fields are 1 based. */
6007 amt = abfd->section_count + 1;
6008 amt *= sizeof (struct xcoff_link_section_info);
6009 flinfo.section_info = bfd_malloc (amt);
6010 if (flinfo.section_info == NULL)
6011 goto error_return;
6012 for (i = 0; i <= abfd->section_count; i++)
6014 flinfo.section_info[i].relocs = NULL;
6015 flinfo.section_info[i].rel_hashes = NULL;
6016 flinfo.section_info[i].toc_rel_hashes = NULL;
6020 /* Set the file positions for the relocs. */
6021 rel_filepos = obj_relocbase (abfd);
6022 relsz = bfd_coff_relsz (abfd);
6023 max_output_reloc_count = 0;
6024 for (o = abfd->sections; o != NULL; o = o->next)
6026 if (o->reloc_count == 0)
6027 o->rel_filepos = 0;
6028 else
6030 /* A stripped file has no relocs. However, we still
6031 allocate the buffers, so that later code doesn't have to
6032 worry about whether we are stripping or not. */
6033 if (info->strip == strip_all)
6034 o->rel_filepos = 0;
6035 else
6037 o->flags |= SEC_RELOC;
6038 o->rel_filepos = rel_filepos;
6039 rel_filepos += o->reloc_count * relsz;
6042 /* We don't know the indices of global symbols until we have
6043 written out all the local symbols. For each section in
6044 the output file, we keep an array of pointers to hash
6045 table entries. Each entry in the array corresponds to a
6046 reloc. When we find a reloc against a global symbol, we
6047 set the corresponding entry in this array so that we can
6048 fix up the symbol index after we have written out all the
6049 local symbols.
6051 Because of this problem, we also keep the relocs in
6052 memory until the end of the link. This wastes memory.
6053 We could backpatch the file later, I suppose, although it
6054 would be slow. */
6055 amt = o->reloc_count;
6056 amt *= sizeof (struct internal_reloc);
6057 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6059 amt = o->reloc_count;
6060 amt *= sizeof (struct xcoff_link_hash_entry *);
6061 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6063 if (flinfo.section_info[o->target_index].relocs == NULL
6064 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6065 goto error_return;
6067 if (o->reloc_count > max_output_reloc_count)
6068 max_output_reloc_count = o->reloc_count;
6072 /* We now know the size of the relocs, so we can determine the file
6073 positions of the line numbers. */
6074 line_filepos = rel_filepos;
6075 flinfo.line_filepos = line_filepos;
6076 linesz = bfd_coff_linesz (abfd);
6077 for (o = abfd->sections; o != NULL; o = o->next)
6079 if (o->lineno_count == 0)
6080 o->line_filepos = 0;
6081 else
6083 o->line_filepos = line_filepos;
6084 line_filepos += o->lineno_count * linesz;
6087 /* Reset the reloc and lineno counts, so that we can use them to
6088 count the number of entries we have output so far. */
6089 o->reloc_count = 0;
6090 o->lineno_count = 0;
6093 obj_sym_filepos (abfd) = line_filepos;
6095 /* Figure out the largest number of symbols in an input BFD. Take
6096 the opportunity to clear the output_has_begun fields of all the
6097 input BFD's. We want at least 6 symbols, since that is the
6098 number which xcoff_write_global_symbol may need. */
6099 max_sym_count = 6;
6100 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6102 bfd_size_type sz;
6104 sub->output_has_begun = FALSE;
6105 sz = obj_raw_syment_count (sub);
6106 if (sz > max_sym_count)
6107 max_sym_count = sz;
6110 /* Allocate some buffers used while linking. */
6111 amt = max_sym_count * sizeof (struct internal_syment);
6112 flinfo.internal_syms = bfd_malloc (amt);
6114 amt = max_sym_count * sizeof (long);
6115 flinfo.sym_indices = bfd_malloc (amt);
6117 amt = (max_sym_count + 1) * symesz;
6118 flinfo.outsyms = bfd_malloc (amt);
6120 amt = max_lineno_count * bfd_coff_linesz (abfd);
6121 flinfo.linenos = bfd_malloc (amt);
6123 amt = max_contents_size;
6124 flinfo.contents = bfd_malloc (amt);
6126 amt = max_reloc_count * relsz;
6127 flinfo.external_relocs = bfd_malloc (amt);
6129 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6130 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6131 || flinfo.outsyms == NULL
6132 || (flinfo.linenos == NULL && max_lineno_count > 0)
6133 || (flinfo.contents == NULL && max_contents_size > 0)
6134 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6135 goto error_return;
6137 obj_raw_syment_count (abfd) = 0;
6139 /* Find a TOC symbol, if we need one. */
6140 if (!xcoff_find_tc0 (abfd, &flinfo))
6141 goto error_return;
6143 /* We now know the position of everything in the file, except that
6144 we don't know the size of the symbol table and therefore we don't
6145 know where the string table starts. We just build the string
6146 table in memory as we go along. We process all the relocations
6147 for a single input file at once. */
6148 for (o = abfd->sections; o != NULL; o = o->next)
6150 for (p = o->map_head.link_order; p != NULL; p = p->next)
6152 if (p->type == bfd_indirect_link_order
6153 && p->u.indirect.section->owner->xvec == abfd->xvec)
6155 sub = p->u.indirect.section->owner;
6156 if (! sub->output_has_begun)
6158 if (! xcoff_link_input_bfd (&flinfo, sub))
6159 goto error_return;
6160 sub->output_has_begun = TRUE;
6163 else if (p->type == bfd_section_reloc_link_order
6164 || p->type == bfd_symbol_reloc_link_order)
6166 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6167 goto error_return;
6169 else
6171 if (! _bfd_default_link_order (abfd, info, o, p))
6172 goto error_return;
6177 /* Free up the buffers used by xcoff_link_input_bfd. */
6178 if (flinfo.internal_syms != NULL)
6180 free (flinfo.internal_syms);
6181 flinfo.internal_syms = NULL;
6183 if (flinfo.sym_indices != NULL)
6185 free (flinfo.sym_indices);
6186 flinfo.sym_indices = NULL;
6188 if (flinfo.linenos != NULL)
6190 free (flinfo.linenos);
6191 flinfo.linenos = NULL;
6193 if (flinfo.contents != NULL)
6195 free (flinfo.contents);
6196 flinfo.contents = NULL;
6198 if (flinfo.external_relocs != NULL)
6200 free (flinfo.external_relocs);
6201 flinfo.external_relocs = NULL;
6204 /* The value of the last C_FILE symbol is supposed to be -1. Write
6205 it out again. */
6206 if (flinfo.last_file_index != -1)
6208 flinfo.last_file.n_value = -(bfd_vma) 1;
6209 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6210 (void *) flinfo.outsyms);
6211 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6212 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6213 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6214 goto error_return;
6217 /* Write out all the global symbols which do not come from XCOFF
6218 input files. */
6219 xcoff_link_hash_traverse (xcoff_hash_table (info),
6220 xcoff_write_global_symbol,
6221 (void *) &flinfo);
6223 if (flinfo.outsyms != NULL)
6225 free (flinfo.outsyms);
6226 flinfo.outsyms = NULL;
6229 /* Now that we have written out all the global symbols, we know the
6230 symbol indices to use for relocs against them, and we can finally
6231 write out the relocs. */
6232 amt = max_output_reloc_count * relsz;
6233 external_relocs = bfd_malloc (amt);
6234 if (external_relocs == NULL && max_output_reloc_count != 0)
6235 goto error_return;
6237 for (o = abfd->sections; o != NULL; o = o->next)
6239 struct internal_reloc *irel;
6240 struct internal_reloc *irelend;
6241 struct xcoff_link_hash_entry **rel_hash;
6242 struct xcoff_toc_rel_hash *toc_rel_hash;
6243 bfd_byte *erel;
6244 bfd_size_type rel_size;
6246 /* A stripped file has no relocs. */
6247 if (info->strip == strip_all)
6249 o->reloc_count = 0;
6250 continue;
6253 if (o->reloc_count == 0)
6254 continue;
6256 irel = flinfo.section_info[o->target_index].relocs;
6257 irelend = irel + o->reloc_count;
6258 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6259 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6261 if (*rel_hash != NULL)
6263 if ((*rel_hash)->indx < 0)
6265 if (! ((*info->callbacks->unattached_reloc)
6266 (info, (*rel_hash)->root.root.string,
6267 NULL, o, irel->r_vaddr)))
6268 goto error_return;
6269 (*rel_hash)->indx = 0;
6271 irel->r_symndx = (*rel_hash)->indx;
6275 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6276 toc_rel_hash != NULL;
6277 toc_rel_hash = toc_rel_hash->next)
6279 if (toc_rel_hash->h->u.toc_indx < 0)
6281 if (! ((*info->callbacks->unattached_reloc)
6282 (info, toc_rel_hash->h->root.root.string,
6283 NULL, o, toc_rel_hash->rel->r_vaddr)))
6284 goto error_return;
6285 toc_rel_hash->h->u.toc_indx = 0;
6287 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6290 /* XCOFF requires that the relocs be sorted by address. We tend
6291 to produce them in the order in which their containing csects
6292 appear in the symbol table, which is not necessarily by
6293 address. So we sort them here. There may be a better way to
6294 do this. */
6295 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6296 o->reloc_count, sizeof (struct internal_reloc),
6297 xcoff_sort_relocs);
6299 irel = flinfo.section_info[o->target_index].relocs;
6300 irelend = irel + o->reloc_count;
6301 erel = external_relocs;
6302 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6303 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6305 rel_size = relsz * o->reloc_count;
6306 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6307 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6308 goto error_return;
6311 if (external_relocs != NULL)
6313 free (external_relocs);
6314 external_relocs = NULL;
6317 /* Free up the section information. */
6318 if (flinfo.section_info != NULL)
6320 unsigned int i;
6322 for (i = 0; i < abfd->section_count; i++)
6324 if (flinfo.section_info[i].relocs != NULL)
6325 free (flinfo.section_info[i].relocs);
6326 if (flinfo.section_info[i].rel_hashes != NULL)
6327 free (flinfo.section_info[i].rel_hashes);
6329 free (flinfo.section_info);
6330 flinfo.section_info = NULL;
6333 /* Write out the loader section contents. */
6334 o = xcoff_hash_table (info)->loader_section;
6335 if (o)
6337 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6338 == (xcoff_hash_table (info)->loader_section->contents
6339 + xcoff_hash_table (info)->ldhdr.l_impoff));
6340 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6341 (file_ptr) o->output_offset, o->size))
6342 goto error_return;
6345 /* Write out the magic sections. */
6346 o = xcoff_hash_table (info)->linkage_section;
6347 if (o->size > 0
6348 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6349 (file_ptr) o->output_offset,
6350 o->size))
6351 goto error_return;
6352 o = xcoff_hash_table (info)->toc_section;
6353 if (o->size > 0
6354 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6355 (file_ptr) o->output_offset,
6356 o->size))
6357 goto error_return;
6358 o = xcoff_hash_table (info)->descriptor_section;
6359 if (o->size > 0
6360 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6361 (file_ptr) o->output_offset,
6362 o->size))
6363 goto error_return;
6365 /* Write out the string table. */
6366 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6367 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6368 goto error_return;
6369 H_PUT_32 (abfd,
6370 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6371 strbuf);
6372 amt = STRING_SIZE_SIZE;
6373 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6374 goto error_return;
6375 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6376 goto error_return;
6378 _bfd_stringtab_free (flinfo.strtab);
6380 /* Write out the debugging string table. */
6381 o = xcoff_hash_table (info)->debug_section;
6382 if (o != NULL)
6384 struct bfd_strtab_hash *debug_strtab;
6386 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6387 BFD_ASSERT (o->output_section->size - o->output_offset
6388 >= _bfd_stringtab_size (debug_strtab));
6389 pos = o->output_section->filepos + o->output_offset;
6390 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6391 goto error_return;
6392 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6393 goto error_return;
6396 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6397 not try to write out the symbols. */
6398 bfd_get_symcount (abfd) = 0;
6400 return TRUE;
6402 error_return:
6403 if (flinfo.strtab != NULL)
6404 _bfd_stringtab_free (flinfo.strtab);
6406 if (flinfo.section_info != NULL)
6408 unsigned int i;
6410 for (i = 0; i < abfd->section_count; i++)
6412 if (flinfo.section_info[i].relocs != NULL)
6413 free (flinfo.section_info[i].relocs);
6414 if (flinfo.section_info[i].rel_hashes != NULL)
6415 free (flinfo.section_info[i].rel_hashes);
6417 free (flinfo.section_info);
6420 if (flinfo.internal_syms != NULL)
6421 free (flinfo.internal_syms);
6422 if (flinfo.sym_indices != NULL)
6423 free (flinfo.sym_indices);
6424 if (flinfo.outsyms != NULL)
6425 free (flinfo.outsyms);
6426 if (flinfo.linenos != NULL)
6427 free (flinfo.linenos);
6428 if (flinfo.contents != NULL)
6429 free (flinfo.contents);
6430 if (flinfo.external_relocs != NULL)
6431 free (flinfo.external_relocs);
6432 if (external_relocs != NULL)
6433 free (external_relocs);
6434 return FALSE;