Mach-O: add objdump -P function_starts to display function starts.
[binutils-gdb.git] / bfd / xcofflink.c
blobee90151e8637fe965d727f2438cacc704875280a
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2014 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
28 #include "libcoff.h"
29 #include "libxcoff.h"
30 #include "libiberty.h"
32 /* This file holds the XCOFF linker code. */
34 #undef STRING_SIZE_SIZE
35 #define STRING_SIZE_SIZE 4
37 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
38 This flag will only be used on input sections. */
40 #define SEC_MARK (SEC_ROM)
42 /* The list of import files. */
44 struct xcoff_import_file
46 /* The next entry in the list. */
47 struct xcoff_import_file *next;
48 /* The path. */
49 const char *path;
50 /* The file name. */
51 const char *file;
52 /* The member name. */
53 const char *member;
56 /* Information we keep for each section in the output file during the
57 final link phase. */
59 struct xcoff_link_section_info
61 /* The relocs to be output. */
62 struct internal_reloc *relocs;
63 /* For each reloc against a global symbol whose index was not known
64 when the reloc was handled, the global hash table entry. */
65 struct xcoff_link_hash_entry **rel_hashes;
66 /* If there is a TOC relative reloc against a global symbol, and the
67 index of the TOC symbol is not known when the reloc was handled,
68 an entry is added to this linked list. This is not an array,
69 like rel_hashes, because this case is quite uncommon. */
70 struct xcoff_toc_rel_hash
72 struct xcoff_toc_rel_hash *next;
73 struct xcoff_link_hash_entry *h;
74 struct internal_reloc *rel;
75 } *toc_rel_hashes;
78 /* Information that the XCOFF linker collects about an archive. */
79 struct xcoff_archive_info
81 /* The archive described by this entry. */
82 bfd *archive;
84 /* The import path and import filename to use when referring to
85 this archive in the .loader section. */
86 const char *imppath;
87 const char *impfile;
89 /* True if the archive contains a dynamic object. */
90 unsigned int contains_shared_object_p : 1;
92 /* True if the previous field is valid. */
93 unsigned int know_contains_shared_object_p : 1;
96 struct xcoff_link_hash_table
98 struct bfd_link_hash_table root;
100 /* The .debug string hash table. We need to compute this while
101 reading the input files, so that we know how large the .debug
102 section will be before we assign section positions. */
103 struct bfd_strtab_hash *debug_strtab;
105 /* The .debug section we will use for the final output. */
106 asection *debug_section;
108 /* The .loader section we will use for the final output. */
109 asection *loader_section;
111 /* A count of non TOC relative relocs which will need to be
112 allocated in the .loader section. */
113 size_t ldrel_count;
115 /* The .loader section header. */
116 struct internal_ldhdr ldhdr;
118 /* The .gl section we use to hold global linkage code. */
119 asection *linkage_section;
121 /* The .tc section we use to hold toc entries we build for global
122 linkage code. */
123 asection *toc_section;
125 /* The .ds section we use to hold function descriptors which we
126 create for exported symbols. */
127 asection *descriptor_section;
129 /* The list of import files. */
130 struct xcoff_import_file *imports;
132 /* Required alignment of sections within the output file. */
133 unsigned long file_align;
135 /* Whether the .text section must be read-only. */
136 bfd_boolean textro;
138 /* Whether -brtl was specified. */
139 bfd_boolean rtld;
141 /* Whether garbage collection was done. */
142 bfd_boolean gc;
144 /* A linked list of symbols for which we have size information. */
145 struct xcoff_link_size_list
147 struct xcoff_link_size_list *next;
148 struct xcoff_link_hash_entry *h;
149 bfd_size_type size;
151 *size_list;
153 /* Information about archives. */
154 htab_t archive_info;
156 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
157 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
160 /* Information that we pass around while doing the final link step. */
162 struct xcoff_final_link_info
164 /* General link information. */
165 struct bfd_link_info *info;
166 /* Output BFD. */
167 bfd *output_bfd;
168 /* Hash table for long symbol names. */
169 struct bfd_strtab_hash *strtab;
170 /* Array of information kept for each output section, indexed by the
171 target_index field. */
172 struct xcoff_link_section_info *section_info;
173 /* Symbol index of last C_FILE symbol (-1 if none). */
174 long last_file_index;
175 /* Contents of last C_FILE symbol. */
176 struct internal_syment last_file;
177 /* Symbol index of TOC symbol. */
178 long toc_symindx;
179 /* Start of .loader symbols. */
180 bfd_byte *ldsym;
181 /* Next .loader reloc to swap out. */
182 bfd_byte *ldrel;
183 /* File position of start of line numbers. */
184 file_ptr line_filepos;
185 /* Buffer large enough to hold swapped symbols of any input file. */
186 struct internal_syment *internal_syms;
187 /* Buffer large enough to hold output indices of symbols of any
188 input file. */
189 long *sym_indices;
190 /* Buffer large enough to hold output symbols for any input file. */
191 bfd_byte *outsyms;
192 /* Buffer large enough to hold external line numbers for any input
193 section. */
194 bfd_byte *linenos;
195 /* Buffer large enough to hold any input section. */
196 bfd_byte *contents;
197 /* Buffer large enough to hold external relocs of any input section. */
198 bfd_byte *external_relocs;
201 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
205 /* Routines to read XCOFF dynamic information. This don't really
206 belong here, but we already have the ldsym manipulation routines
207 here. */
209 /* Read the contents of a section. */
211 static bfd_boolean
212 xcoff_get_section_contents (bfd *abfd, asection *sec)
214 if (coff_section_data (abfd, sec) == NULL)
216 bfd_size_type amt = sizeof (struct coff_section_tdata);
218 sec->used_by_bfd = bfd_zalloc (abfd, amt);
219 if (sec->used_by_bfd == NULL)
220 return FALSE;
223 if (coff_section_data (abfd, sec)->contents == NULL)
225 bfd_byte *contents;
227 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
229 if (contents != NULL)
230 free (contents);
231 return FALSE;
233 coff_section_data (abfd, sec)->contents = contents;
236 return TRUE;
239 /* Get the size required to hold the dynamic symbols. */
241 long
242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
244 asection *lsec;
245 bfd_byte *contents;
246 struct internal_ldhdr ldhdr;
248 if ((abfd->flags & DYNAMIC) == 0)
250 bfd_set_error (bfd_error_invalid_operation);
251 return -1;
254 lsec = bfd_get_section_by_name (abfd, ".loader");
255 if (lsec == NULL)
257 bfd_set_error (bfd_error_no_symbols);
258 return -1;
261 if (! xcoff_get_section_contents (abfd, lsec))
262 return -1;
263 contents = coff_section_data (abfd, lsec)->contents;
265 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
267 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
270 /* Get the dynamic symbols. */
272 long
273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
275 asection *lsec;
276 bfd_byte *contents;
277 struct internal_ldhdr ldhdr;
278 const char *strings;
279 bfd_byte *elsym, *elsymend;
280 coff_symbol_type *symbuf;
282 if ((abfd->flags & DYNAMIC) == 0)
284 bfd_set_error (bfd_error_invalid_operation);
285 return -1;
288 lsec = bfd_get_section_by_name (abfd, ".loader");
289 if (lsec == NULL)
291 bfd_set_error (bfd_error_no_symbols);
292 return -1;
295 if (! xcoff_get_section_contents (abfd, lsec))
296 return -1;
297 contents = coff_section_data (abfd, lsec)->contents;
299 coff_section_data (abfd, lsec)->keep_contents = TRUE;
301 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
303 strings = (char *) contents + ldhdr.l_stoff;
305 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
306 if (symbuf == NULL)
307 return -1;
309 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
311 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
312 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
314 struct internal_ldsym ldsym;
316 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
318 symbuf->symbol.the_bfd = abfd;
320 if (ldsym._l._l_l._l_zeroes == 0)
321 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
322 else
324 char *c;
326 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
327 if (c == NULL)
328 return -1;
329 memcpy (c, ldsym._l._l_name, SYMNMLEN);
330 c[SYMNMLEN] = '\0';
331 symbuf->symbol.name = c;
334 if (ldsym.l_smclas == XMC_XO)
335 symbuf->symbol.section = bfd_abs_section_ptr;
336 else
337 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
338 ldsym.l_scnum);
339 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
341 symbuf->symbol.flags = BSF_NO_FLAGS;
342 if ((ldsym.l_smtype & L_EXPORT) != 0)
344 if ((ldsym.l_smtype & L_WEAK) != 0)
345 symbuf->symbol.flags |= BSF_WEAK;
346 else
347 symbuf->symbol.flags |= BSF_GLOBAL;
350 /* FIXME: We have no way to record the other information stored
351 with the loader symbol. */
352 *psyms = (asymbol *) symbuf;
355 *psyms = NULL;
357 return ldhdr.l_nsyms;
360 /* Get the size required to hold the dynamic relocs. */
362 long
363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
365 asection *lsec;
366 bfd_byte *contents;
367 struct internal_ldhdr ldhdr;
369 if ((abfd->flags & DYNAMIC) == 0)
371 bfd_set_error (bfd_error_invalid_operation);
372 return -1;
375 lsec = bfd_get_section_by_name (abfd, ".loader");
376 if (lsec == NULL)
378 bfd_set_error (bfd_error_no_symbols);
379 return -1;
382 if (! xcoff_get_section_contents (abfd, lsec))
383 return -1;
384 contents = coff_section_data (abfd, lsec)->contents;
386 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
388 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
391 /* Get the dynamic relocs. */
393 long
394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
395 arelent **prelocs,
396 asymbol **syms)
398 asection *lsec;
399 bfd_byte *contents;
400 struct internal_ldhdr ldhdr;
401 arelent *relbuf;
402 bfd_byte *elrel, *elrelend;
404 if ((abfd->flags & DYNAMIC) == 0)
406 bfd_set_error (bfd_error_invalid_operation);
407 return -1;
410 lsec = bfd_get_section_by_name (abfd, ".loader");
411 if (lsec == NULL)
413 bfd_set_error (bfd_error_no_symbols);
414 return -1;
417 if (! xcoff_get_section_contents (abfd, lsec))
418 return -1;
419 contents = coff_section_data (abfd, lsec)->contents;
421 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
423 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
424 if (relbuf == NULL)
425 return -1;
427 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
429 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
430 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
431 prelocs++)
433 struct internal_ldrel ldrel;
435 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
437 if (ldrel.l_symndx >= 3)
438 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
439 else
441 const char *name;
442 asection *sec;
444 switch (ldrel.l_symndx)
446 case 0:
447 name = ".text";
448 break;
449 case 1:
450 name = ".data";
451 break;
452 case 2:
453 name = ".bss";
454 break;
455 default:
456 abort ();
457 break;
460 sec = bfd_get_section_by_name (abfd, name);
461 if (sec == NULL)
463 bfd_set_error (bfd_error_bad_value);
464 return -1;
467 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
470 relbuf->address = ldrel.l_vaddr;
471 relbuf->addend = 0;
473 /* Most dynamic relocs have the same type. FIXME: This is only
474 correct if ldrel.l_rtype == 0. In other cases, we should use
475 a different howto. */
476 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
478 /* FIXME: We have no way to record the l_rsecnm field. */
480 *prelocs = relbuf;
483 *prelocs = NULL;
485 return ldhdr.l_nreloc;
488 /* Hash functions for xcoff_link_hash_table's archive_info. */
490 static hashval_t
491 xcoff_archive_info_hash (const void *data)
493 const struct xcoff_archive_info *info;
495 info = (const struct xcoff_archive_info *) data;
496 return htab_hash_pointer (info->archive);
499 static int
500 xcoff_archive_info_eq (const void *data1, const void *data2)
502 const struct xcoff_archive_info *info1;
503 const struct xcoff_archive_info *info2;
505 info1 = (const struct xcoff_archive_info *) data1;
506 info2 = (const struct xcoff_archive_info *) data2;
507 return info1->archive == info2->archive;
510 /* Return information about archive ARCHIVE. Return NULL on error. */
512 static struct xcoff_archive_info *
513 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
515 struct xcoff_link_hash_table *htab;
516 struct xcoff_archive_info *entryp, entry;
517 void **slot;
519 htab = xcoff_hash_table (info);
520 entry.archive = archive;
521 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
522 if (!slot)
523 return NULL;
525 entryp = *slot;
526 if (!entryp)
528 entryp = bfd_zalloc (archive, sizeof (entry));
529 if (!entryp)
530 return NULL;
532 entryp->archive = archive;
533 *slot = entryp;
535 return entryp;
538 /* Routine to create an entry in an XCOFF link hash table. */
540 static struct bfd_hash_entry *
541 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
542 struct bfd_hash_table *table,
543 const char *string)
545 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
547 /* Allocate the structure if it has not already been allocated by a
548 subclass. */
549 if (ret == NULL)
550 ret = bfd_hash_allocate (table, sizeof (* ret));
551 if (ret == NULL)
552 return NULL;
554 /* Call the allocation method of the superclass. */
555 ret = ((struct xcoff_link_hash_entry *)
556 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
557 table, string));
558 if (ret != NULL)
560 /* Set local fields. */
561 ret->indx = -1;
562 ret->toc_section = NULL;
563 ret->u.toc_indx = -1;
564 ret->descriptor = NULL;
565 ret->ldsym = NULL;
566 ret->ldindx = -1;
567 ret->flags = 0;
568 ret->smclas = XMC_UA;
571 return (struct bfd_hash_entry *) ret;
574 /* Create a XCOFF link hash table. */
576 struct bfd_link_hash_table *
577 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
579 struct xcoff_link_hash_table *ret;
580 bfd_size_type amt = sizeof (* ret);
582 ret = bfd_zmalloc (amt);
583 if (ret == NULL)
584 return NULL;
585 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
586 sizeof (struct xcoff_link_hash_entry)))
588 free (ret);
589 return NULL;
592 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
593 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
594 xcoff_archive_info_eq, NULL);
596 /* The linker will always generate a full a.out header. We need to
597 record that fact now, before the sizeof_headers routine could be
598 called. */
599 xcoff_data (abfd)->full_aouthdr = TRUE;
601 return &ret->root;
604 /* Free a XCOFF link hash table. */
606 void
607 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
609 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
611 _bfd_stringtab_free (ret->debug_strtab);
612 bfd_hash_table_free (&ret->root.table);
613 free (ret);
616 /* Read internal relocs for an XCOFF csect. This is a wrapper around
617 _bfd_coff_read_internal_relocs which tries to take advantage of any
618 relocs which may have been cached for the enclosing section. */
620 static struct internal_reloc *
621 xcoff_read_internal_relocs (bfd *abfd,
622 asection *sec,
623 bfd_boolean cache,
624 bfd_byte *external_relocs,
625 bfd_boolean require_internal,
626 struct internal_reloc *internal_relocs)
628 if (coff_section_data (abfd, sec) != NULL
629 && coff_section_data (abfd, sec)->relocs == NULL
630 && xcoff_section_data (abfd, sec) != NULL)
632 asection *enclosing;
634 enclosing = xcoff_section_data (abfd, sec)->enclosing;
636 if (enclosing != NULL
637 && (coff_section_data (abfd, enclosing) == NULL
638 || coff_section_data (abfd, enclosing)->relocs == NULL)
639 && cache
640 && enclosing->reloc_count > 0)
642 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
643 external_relocs, FALSE, NULL)
644 == NULL)
645 return NULL;
648 if (enclosing != NULL
649 && coff_section_data (abfd, enclosing) != NULL
650 && coff_section_data (abfd, enclosing)->relocs != NULL)
652 size_t off;
654 off = ((sec->rel_filepos - enclosing->rel_filepos)
655 / bfd_coff_relsz (abfd));
657 if (! require_internal)
658 return coff_section_data (abfd, enclosing)->relocs + off;
659 memcpy (internal_relocs,
660 coff_section_data (abfd, enclosing)->relocs + off,
661 sec->reloc_count * sizeof (struct internal_reloc));
662 return internal_relocs;
666 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
667 require_internal, internal_relocs);
670 /* Split FILENAME into an import path and an import filename,
671 storing them in *IMPPATH and *IMPFILE respectively. */
673 bfd_boolean
674 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
675 const char **imppath, const char **impfile)
677 const char *base;
678 size_t length;
679 char *path;
681 base = lbasename (filename);
682 length = base - filename;
683 if (length == 0)
684 /* The filename has no directory component, so use an empty path. */
685 *imppath = "";
686 else if (length == 1)
687 /* The filename is in the root directory. */
688 *imppath = "/";
689 else
691 /* Extract the (non-empty) directory part. Note that we don't
692 need to strip duplicate directory separators from any part
693 of the string; the native linker doesn't do that either. */
694 path = bfd_alloc (abfd, length);
695 if (path == NULL)
696 return FALSE;
697 memcpy (path, filename, length - 1);
698 path[length - 1] = 0;
699 *imppath = path;
701 *impfile = base;
702 return TRUE;
705 /* Set ARCHIVE's import path as though its filename had been given
706 as FILENAME. */
708 bfd_boolean
709 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
710 bfd *archive, const char *filename)
712 struct xcoff_archive_info *archive_info;
714 archive_info = xcoff_get_archive_info (info, archive);
715 return (archive_info != NULL
716 && bfd_xcoff_split_import_path (archive, filename,
717 &archive_info->imppath,
718 &archive_info->impfile));
721 /* H is an imported symbol. Set the import module's path, file and member
722 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
723 no specific import module is specified. */
725 static bfd_boolean
726 xcoff_set_import_path (struct bfd_link_info *info,
727 struct xcoff_link_hash_entry *h,
728 const char *imppath, const char *impfile,
729 const char *impmember)
731 unsigned int c;
732 struct xcoff_import_file **pp;
734 /* We overload the ldindx field to hold the l_ifile value for this
735 symbol. */
736 BFD_ASSERT (h->ldsym == NULL);
737 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
738 if (imppath == NULL)
739 h->ldindx = -1;
740 else
742 /* We start c at 1 because the first entry in the import list is
743 reserved for the library search path. */
744 for (pp = &xcoff_hash_table (info)->imports, c = 1;
745 *pp != NULL;
746 pp = &(*pp)->next, ++c)
748 if (filename_cmp ((*pp)->path, imppath) == 0
749 && filename_cmp ((*pp)->file, impfile) == 0
750 && filename_cmp ((*pp)->member, impmember) == 0)
751 break;
754 if (*pp == NULL)
756 struct xcoff_import_file *n;
757 bfd_size_type amt = sizeof (* n);
759 n = bfd_alloc (info->output_bfd, amt);
760 if (n == NULL)
761 return FALSE;
762 n->next = NULL;
763 n->path = imppath;
764 n->file = impfile;
765 n->member = impmember;
766 *pp = n;
768 h->ldindx = c;
770 return TRUE;
773 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
774 Return true if LDSYM defines H. */
776 static bfd_boolean
777 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
778 struct internal_ldsym *ldsym)
780 /* If we didn't know about H before processing LDSYM, LDSYM
781 definitely defines H. */
782 if (h->root.type == bfd_link_hash_new)
783 return TRUE;
785 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
786 dynamic symbol, LDSYM trumps the current definition of H. */
787 if ((ldsym->l_smtype & L_WEAK) == 0
788 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
789 && (h->flags & XCOFF_DEF_REGULAR) == 0
790 && (h->root.type == bfd_link_hash_defweak
791 || h->root.type == bfd_link_hash_undefweak))
792 return TRUE;
794 /* If H is currently undefined, LDSYM defines it. */
795 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
796 && (h->root.type == bfd_link_hash_undefined
797 || h->root.type == bfd_link_hash_undefweak))
798 return TRUE;
800 return FALSE;
803 /* This function is used to add symbols from a dynamic object to the
804 global symbol table. */
806 static bfd_boolean
807 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
809 asection *lsec;
810 bfd_byte *contents;
811 struct internal_ldhdr ldhdr;
812 const char *strings;
813 bfd_byte *elsym, *elsymend;
814 struct xcoff_import_file *n;
815 unsigned int c;
816 struct xcoff_import_file **pp;
818 /* We can only handle a dynamic object if we are generating an XCOFF
819 output file. */
820 if (info->output_bfd->xvec != abfd->xvec)
822 (*_bfd_error_handler)
823 (_("%s: XCOFF shared object when not producing XCOFF output"),
824 bfd_get_filename (abfd));
825 bfd_set_error (bfd_error_invalid_operation);
826 return FALSE;
829 /* The symbols we use from a dynamic object are not the symbols in
830 the normal symbol table, but, rather, the symbols in the export
831 table. If there is a global symbol in a dynamic object which is
832 not in the export table, the loader will not be able to find it,
833 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
834 libc.a has symbols in the export table which are not in the
835 symbol table. */
837 /* Read in the .loader section. FIXME: We should really use the
838 o_snloader field in the a.out header, rather than grabbing the
839 section by name. */
840 lsec = bfd_get_section_by_name (abfd, ".loader");
841 if (lsec == NULL)
843 (*_bfd_error_handler)
844 (_("%s: dynamic object with no .loader section"),
845 bfd_get_filename (abfd));
846 bfd_set_error (bfd_error_no_symbols);
847 return FALSE;
850 if (! xcoff_get_section_contents (abfd, lsec))
851 return FALSE;
852 contents = coff_section_data (abfd, lsec)->contents;
854 /* Remove the sections from this object, so that they do not get
855 included in the link. */
856 bfd_section_list_clear (abfd);
858 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
860 strings = (char *) contents + ldhdr.l_stoff;
862 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
864 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
866 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
868 struct internal_ldsym ldsym;
869 char nambuf[SYMNMLEN + 1];
870 const char *name;
871 struct xcoff_link_hash_entry *h;
873 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
875 /* We are only interested in exported symbols. */
876 if ((ldsym.l_smtype & L_EXPORT) == 0)
877 continue;
879 if (ldsym._l._l_l._l_zeroes == 0)
880 name = strings + ldsym._l._l_l._l_offset;
881 else
883 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
884 nambuf[SYMNMLEN] = '\0';
885 name = nambuf;
888 /* Normally we could not call xcoff_link_hash_lookup in an add
889 symbols routine, since we might not be using an XCOFF hash
890 table. However, we verified above that we are using an XCOFF
891 hash table. */
893 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
894 TRUE, TRUE);
895 if (h == NULL)
896 return FALSE;
898 if (!xcoff_dynamic_definition_p (h, &ldsym))
899 continue;
901 h->flags |= XCOFF_DEF_DYNAMIC;
902 h->smclas = ldsym.l_smclas;
903 if (h->smclas == XMC_XO)
905 /* This symbol has an absolute value. */
906 if ((ldsym.l_smtype & L_WEAK) != 0)
907 h->root.type = bfd_link_hash_defweak;
908 else
909 h->root.type = bfd_link_hash_defined;
910 h->root.u.def.section = bfd_abs_section_ptr;
911 h->root.u.def.value = ldsym.l_value;
913 else
915 /* Otherwise, we don't bother to actually define the symbol,
916 since we don't have a section to put it in anyhow.
917 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
918 should be imported from the symbol's undef.abfd. */
919 if ((ldsym.l_smtype & L_WEAK) != 0)
920 h->root.type = bfd_link_hash_undefweak;
921 else
922 h->root.type = bfd_link_hash_undefined;
923 h->root.u.undef.abfd = abfd;
926 /* If this symbol defines a function descriptor, then it
927 implicitly defines the function code as well. */
928 if (h->smclas == XMC_DS
929 || (h->smclas == XMC_XO && name[0] != '.'))
930 h->flags |= XCOFF_DESCRIPTOR;
931 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
933 struct xcoff_link_hash_entry *hds;
935 hds = h->descriptor;
936 if (hds == NULL)
938 char *dsnm;
940 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
941 if (dsnm == NULL)
942 return FALSE;
943 dsnm[0] = '.';
944 strcpy (dsnm + 1, name);
945 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
946 TRUE, TRUE, TRUE);
947 free (dsnm);
948 if (hds == NULL)
949 return FALSE;
951 hds->descriptor = h;
952 h->descriptor = hds;
955 if (xcoff_dynamic_definition_p (hds, &ldsym))
957 hds->root.type = h->root.type;
958 hds->flags |= XCOFF_DEF_DYNAMIC;
959 if (h->smclas == XMC_XO)
961 /* An absolute symbol appears to actually define code, not a
962 function descriptor. This is how some math functions are
963 implemented on AIX 4.1. */
964 hds->smclas = XMC_XO;
965 hds->root.u.def.section = bfd_abs_section_ptr;
966 hds->root.u.def.value = ldsym.l_value;
968 else
970 hds->smclas = XMC_PR;
971 hds->root.u.undef.abfd = abfd;
972 /* We do not want to add this to the undefined
973 symbol list. */
979 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
981 free (coff_section_data (abfd, lsec)->contents);
982 coff_section_data (abfd, lsec)->contents = NULL;
985 /* Record this file in the import files. */
986 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
987 if (n == NULL)
988 return FALSE;
989 n->next = NULL;
991 if (abfd->my_archive == NULL)
993 if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
994 &n->path, &n->file))
995 return FALSE;
996 n->member = "";
998 else
1000 struct xcoff_archive_info *archive_info;
1002 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1003 if (!archive_info->impfile)
1005 if (!bfd_xcoff_split_import_path (archive_info->archive,
1006 archive_info->archive->filename,
1007 &archive_info->imppath,
1008 &archive_info->impfile))
1009 return FALSE;
1011 n->path = archive_info->imppath;
1012 n->file = archive_info->impfile;
1013 n->member = bfd_get_filename (abfd);
1016 /* We start c at 1 because the first import file number is reserved
1017 for LIBPATH. */
1018 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1019 *pp != NULL;
1020 pp = &(*pp)->next, ++c)
1022 *pp = n;
1024 xcoff_data (abfd)->import_file_id = c;
1026 return TRUE;
1029 /* xcoff_link_create_extra_sections
1031 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1033 static bfd_boolean
1034 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1036 bfd_boolean return_value = FALSE;
1038 if (info->output_bfd->xvec == abfd->xvec)
1040 /* We need to build a .loader section, so we do it here. This
1041 won't work if we're producing an XCOFF output file with no
1042 XCOFF input files. FIXME. */
1044 if (!info->relocatable
1045 && xcoff_hash_table (info)->loader_section == NULL)
1047 asection *lsec;
1048 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1050 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1051 if (lsec == NULL)
1052 goto end_return;
1054 xcoff_hash_table (info)->loader_section = lsec;
1057 /* Likewise for the linkage section. */
1058 if (xcoff_hash_table (info)->linkage_section == NULL)
1060 asection *lsec;
1061 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1062 | SEC_IN_MEMORY);
1064 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1065 if (lsec == NULL)
1066 goto end_return;
1068 xcoff_hash_table (info)->linkage_section = lsec;
1069 lsec->alignment_power = 2;
1072 /* Likewise for the TOC section. */
1073 if (xcoff_hash_table (info)->toc_section == NULL)
1075 asection *tsec;
1076 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1077 | SEC_IN_MEMORY);
1079 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1080 if (tsec == NULL)
1081 goto end_return;
1083 xcoff_hash_table (info)->toc_section = tsec;
1084 tsec->alignment_power = 2;
1087 /* Likewise for the descriptor section. */
1088 if (xcoff_hash_table (info)->descriptor_section == NULL)
1090 asection *dsec;
1091 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1092 | SEC_IN_MEMORY);
1094 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1095 if (dsec == NULL)
1096 goto end_return;
1098 xcoff_hash_table (info)->descriptor_section = dsec;
1099 dsec->alignment_power = 2;
1102 /* Likewise for the .debug section. */
1103 if (xcoff_hash_table (info)->debug_section == NULL
1104 && info->strip != strip_all)
1106 asection *dsec;
1107 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1109 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1110 if (dsec == NULL)
1111 goto end_return;
1113 xcoff_hash_table (info)->debug_section = dsec;
1117 return_value = TRUE;
1119 end_return:
1121 return return_value;
1124 /* Returns the index of reloc in RELOCS with the least address greater
1125 than or equal to ADDRESS. The relocs are sorted by address. */
1127 static bfd_size_type
1128 xcoff_find_reloc (struct internal_reloc *relocs,
1129 bfd_size_type count,
1130 bfd_vma address)
1132 bfd_size_type min, max, this;
1134 if (count < 2)
1136 if (count == 1 && relocs[0].r_vaddr < address)
1137 return 1;
1138 else
1139 return 0;
1142 min = 0;
1143 max = count;
1145 /* Do a binary search over (min,max]. */
1146 while (min + 1 < max)
1148 bfd_vma raddr;
1150 this = (max + min) / 2;
1151 raddr = relocs[this].r_vaddr;
1152 if (raddr > address)
1153 max = this;
1154 else if (raddr < address)
1155 min = this;
1156 else
1158 min = this;
1159 break;
1163 if (relocs[min].r_vaddr < address)
1164 return min + 1;
1166 while (min > 0
1167 && relocs[min - 1].r_vaddr == address)
1168 --min;
1170 return min;
1173 /* Add all the symbols from an object file to the hash table.
1175 XCOFF is a weird format. A normal XCOFF .o files will have three
1176 COFF sections--.text, .data, and .bss--but each COFF section will
1177 contain many csects. These csects are described in the symbol
1178 table. From the linker's point of view, each csect must be
1179 considered a section in its own right. For example, a TOC entry is
1180 handled as a small XMC_TC csect. The linker must be able to merge
1181 different TOC entries together, which means that it must be able to
1182 extract the XMC_TC csects from the .data section of the input .o
1183 file.
1185 From the point of view of our linker, this is, of course, a hideous
1186 nightmare. We cope by actually creating sections for each csect,
1187 and discarding the original sections. We then have to handle the
1188 relocation entries carefully, since the only way to tell which
1189 csect they belong to is to examine the address. */
1191 static bfd_boolean
1192 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1194 unsigned int n_tmask;
1195 unsigned int n_btshft;
1196 bfd_boolean default_copy;
1197 bfd_size_type symcount;
1198 struct xcoff_link_hash_entry **sym_hash;
1199 asection **csect_cache;
1200 unsigned int *lineno_counts;
1201 bfd_size_type linesz;
1202 asection *o;
1203 asection *last_real;
1204 bfd_boolean keep_syms;
1205 asection *csect;
1206 unsigned int csect_index;
1207 asection *first_csect;
1208 bfd_size_type symesz;
1209 bfd_byte *esym;
1210 bfd_byte *esym_end;
1211 struct reloc_info_struct
1213 struct internal_reloc *relocs;
1214 asection **csects;
1215 bfd_byte *linenos;
1216 } *reloc_info = NULL;
1217 bfd_size_type amt;
1219 keep_syms = obj_coff_keep_syms (abfd);
1221 if ((abfd->flags & DYNAMIC) != 0
1222 && ! info->static_link)
1224 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1225 return FALSE;
1228 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1229 if (! xcoff_link_create_extra_sections (abfd, info))
1230 goto error_return;
1232 if ((abfd->flags & DYNAMIC) != 0
1233 && ! info->static_link)
1234 return TRUE;
1236 n_tmask = coff_data (abfd)->local_n_tmask;
1237 n_btshft = coff_data (abfd)->local_n_btshft;
1239 /* Define macros so that ISFCN, et. al., macros work correctly. */
1240 #define N_TMASK n_tmask
1241 #define N_BTSHFT n_btshft
1243 if (info->keep_memory)
1244 default_copy = FALSE;
1245 else
1246 default_copy = TRUE;
1248 symcount = obj_raw_syment_count (abfd);
1250 /* We keep a list of the linker hash table entries that correspond
1251 to each external symbol. */
1252 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1253 sym_hash = bfd_zalloc (abfd, amt);
1254 if (sym_hash == NULL && symcount != 0)
1255 goto error_return;
1256 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1258 /* Because of the weird stuff we are doing with XCOFF csects, we can
1259 not easily determine which section a symbol is in, so we store
1260 the information in the tdata for the input file. */
1261 amt = symcount * sizeof (asection *);
1262 csect_cache = bfd_zalloc (abfd, amt);
1263 if (csect_cache == NULL && symcount != 0)
1264 goto error_return;
1265 xcoff_data (abfd)->csects = csect_cache;
1267 /* We garbage-collect line-number information on a symbol-by-symbol
1268 basis, so we need to have quick access to the number of entries
1269 per symbol. */
1270 amt = symcount * sizeof (unsigned int);
1271 lineno_counts = bfd_zalloc (abfd, amt);
1272 if (lineno_counts == NULL && symcount != 0)
1273 goto error_return;
1274 xcoff_data (abfd)->lineno_counts = lineno_counts;
1276 /* While splitting sections into csects, we need to assign the
1277 relocs correctly. The relocs and the csects must both be in
1278 order by VMA within a given section, so we handle this by
1279 scanning along the relocs as we process the csects. We index
1280 into reloc_info using the section target_index. */
1281 amt = abfd->section_count + 1;
1282 amt *= sizeof (struct reloc_info_struct);
1283 reloc_info = bfd_zmalloc (amt);
1284 if (reloc_info == NULL)
1285 goto error_return;
1287 /* Read in the relocs and line numbers for each section. */
1288 linesz = bfd_coff_linesz (abfd);
1289 last_real = NULL;
1290 for (o = abfd->sections; o != NULL; o = o->next)
1292 last_real = o;
1294 if ((o->flags & SEC_RELOC) != 0)
1296 reloc_info[o->target_index].relocs =
1297 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1298 amt = o->reloc_count;
1299 amt *= sizeof (asection *);
1300 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1301 if (reloc_info[o->target_index].csects == NULL)
1302 goto error_return;
1305 if ((info->strip == strip_none || info->strip == strip_some)
1306 && o->lineno_count > 0)
1308 bfd_byte *linenos;
1310 amt = linesz * o->lineno_count;
1311 linenos = bfd_malloc (amt);
1312 if (linenos == NULL)
1313 goto error_return;
1314 reloc_info[o->target_index].linenos = linenos;
1315 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1316 || bfd_bread (linenos, amt, abfd) != amt)
1317 goto error_return;
1321 /* Don't let the linker relocation routines discard the symbols. */
1322 obj_coff_keep_syms (abfd) = TRUE;
1324 csect = NULL;
1325 csect_index = 0;
1326 first_csect = NULL;
1328 symesz = bfd_coff_symesz (abfd);
1329 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1330 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1331 esym_end = esym + symcount * symesz;
1333 while (esym < esym_end)
1335 struct internal_syment sym;
1336 union internal_auxent aux;
1337 const char *name;
1338 char buf[SYMNMLEN + 1];
1339 int smtyp;
1340 asection *section;
1341 bfd_vma value;
1342 struct xcoff_link_hash_entry *set_toc;
1344 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1346 /* In this pass we are only interested in symbols with csect
1347 information. */
1348 if (!CSECT_SYM_P (sym.n_sclass))
1350 /* Set csect_cache,
1351 Normally csect is a .pr, .rw etc. created in the loop
1352 If C_FILE or first time, handle special
1354 Advance esym, sym_hash, csect_hash ptrs. */
1355 if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1356 csect = NULL;
1357 if (csect != NULL)
1358 *csect_cache = csect;
1359 else if (first_csect == NULL
1360 || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1361 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1362 else
1363 *csect_cache = NULL;
1364 esym += (sym.n_numaux + 1) * symesz;
1365 sym_hash += sym.n_numaux + 1;
1366 csect_cache += sym.n_numaux + 1;
1367 lineno_counts += sym.n_numaux + 1;
1369 continue;
1372 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1374 if (name == NULL)
1375 goto error_return;
1377 /* If this symbol has line number information attached to it,
1378 and we're not stripping it, count the number of entries and
1379 add them to the count for this csect. In the final link pass
1380 we are going to attach line number information by symbol,
1381 rather than by section, in order to more easily handle
1382 garbage collection. */
1383 if ((info->strip == strip_none || info->strip == strip_some)
1384 && sym.n_numaux > 1
1385 && csect != NULL
1386 && ISFCN (sym.n_type))
1388 union internal_auxent auxlin;
1390 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1391 sym.n_type, sym.n_sclass,
1392 0, sym.n_numaux, (void *) &auxlin);
1394 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1396 asection *enclosing;
1397 bfd_signed_vma linoff;
1399 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1400 if (enclosing == NULL)
1402 (*_bfd_error_handler)
1403 (_("%B: `%s' has line numbers but no enclosing section"),
1404 abfd, name);
1405 bfd_set_error (bfd_error_bad_value);
1406 goto error_return;
1408 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1409 - enclosing->line_filepos);
1410 /* Explicit cast to bfd_signed_vma for compiler. */
1411 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1413 struct internal_lineno lin;
1414 bfd_byte *linpstart;
1416 linpstart = (reloc_info[enclosing->target_index].linenos
1417 + linoff);
1418 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1419 if (lin.l_lnno == 0
1420 && ((bfd_size_type) lin.l_addr.l_symndx
1421 == ((esym
1422 - (bfd_byte *) obj_coff_external_syms (abfd))
1423 / symesz)))
1425 bfd_byte *linpend, *linp;
1427 linpend = (reloc_info[enclosing->target_index].linenos
1428 + enclosing->lineno_count * linesz);
1429 for (linp = linpstart + linesz;
1430 linp < linpend;
1431 linp += linesz)
1433 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1434 (void *) &lin);
1435 if (lin.l_lnno == 0)
1436 break;
1438 *lineno_counts = (linp - linpstart) / linesz;
1439 /* The setting of line_filepos will only be
1440 useful if all the line number entries for a
1441 csect are contiguous; this only matters for
1442 error reporting. */
1443 if (csect->line_filepos == 0)
1444 csect->line_filepos =
1445 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1451 /* Pick up the csect auxiliary information. */
1452 if (sym.n_numaux == 0)
1454 (*_bfd_error_handler)
1455 (_("%B: class %d symbol `%s' has no aux entries"),
1456 abfd, sym.n_sclass, name);
1457 bfd_set_error (bfd_error_bad_value);
1458 goto error_return;
1461 bfd_coff_swap_aux_in (abfd,
1462 (void *) (esym + symesz * sym.n_numaux),
1463 sym.n_type, sym.n_sclass,
1464 sym.n_numaux - 1, sym.n_numaux,
1465 (void *) &aux);
1467 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1469 section = NULL;
1470 value = 0;
1471 set_toc = NULL;
1473 switch (smtyp)
1475 default:
1476 (*_bfd_error_handler)
1477 (_("%B: symbol `%s' has unrecognized csect type %d"),
1478 abfd, name, smtyp);
1479 bfd_set_error (bfd_error_bad_value);
1480 goto error_return;
1482 case XTY_ER:
1483 /* This is an external reference. */
1484 if (sym.n_sclass == C_HIDEXT
1485 || sym.n_scnum != N_UNDEF
1486 || aux.x_csect.x_scnlen.l != 0)
1488 (*_bfd_error_handler)
1489 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1490 abfd, name, sym.n_sclass, sym.n_scnum,
1491 aux.x_csect.x_scnlen.l);
1492 bfd_set_error (bfd_error_bad_value);
1493 goto error_return;
1496 /* An XMC_XO external reference is actually a reference to
1497 an absolute location. */
1498 if (aux.x_csect.x_smclas != XMC_XO)
1499 section = bfd_und_section_ptr;
1500 else
1502 section = bfd_abs_section_ptr;
1503 value = sym.n_value;
1505 break;
1507 case XTY_SD:
1508 csect = NULL;
1509 csect_index = -(unsigned) 1;
1511 /* When we see a TOC anchor, we record the TOC value. */
1512 if (aux.x_csect.x_smclas == XMC_TC0)
1514 if (sym.n_sclass != C_HIDEXT
1515 || aux.x_csect.x_scnlen.l != 0)
1517 (*_bfd_error_handler)
1518 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1519 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1520 bfd_set_error (bfd_error_bad_value);
1521 goto error_return;
1523 xcoff_data (abfd)->toc = sym.n_value;
1526 /* We must merge TOC entries for the same symbol. We can
1527 merge two TOC entries if they are both C_HIDEXT, they
1528 both have the same name, they are both 4 or 8 bytes long, and
1529 they both have a relocation table entry for an external
1530 symbol with the same name. Unfortunately, this means
1531 that we must look through the relocations. Ick.
1533 Logic for 32 bit vs 64 bit.
1534 32 bit has a csect length of 4 for TOC
1535 64 bit has a csect length of 8 for TOC
1537 The conditions to get past the if-check are not that bad.
1538 They are what is used to create the TOC csects in the first
1539 place. */
1540 if (aux.x_csect.x_smclas == XMC_TC
1541 && sym.n_sclass == C_HIDEXT
1542 && info->output_bfd->xvec == abfd->xvec
1543 && ((bfd_xcoff_is_xcoff32 (abfd)
1544 && aux.x_csect.x_scnlen.l == 4)
1545 || (bfd_xcoff_is_xcoff64 (abfd)
1546 && aux.x_csect.x_scnlen.l == 8)))
1548 asection *enclosing;
1549 struct internal_reloc *relocs;
1550 bfd_size_type relindx;
1551 struct internal_reloc *rel;
1553 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1554 if (enclosing == NULL)
1555 goto error_return;
1557 relocs = reloc_info[enclosing->target_index].relocs;
1558 amt = enclosing->reloc_count;
1559 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1560 rel = relocs + relindx;
1562 /* 32 bit R_POS r_size is 31
1563 64 bit R_POS r_size is 63 */
1564 if (relindx < enclosing->reloc_count
1565 && rel->r_vaddr == (bfd_vma) sym.n_value
1566 && rel->r_type == R_POS
1567 && ((bfd_xcoff_is_xcoff32 (abfd)
1568 && rel->r_size == 31)
1569 || (bfd_xcoff_is_xcoff64 (abfd)
1570 && rel->r_size == 63)))
1572 bfd_byte *erelsym;
1574 struct internal_syment relsym;
1576 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1577 + rel->r_symndx * symesz);
1578 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1579 if (EXTERN_SYM_P (relsym.n_sclass))
1581 const char *relname;
1582 char relbuf[SYMNMLEN + 1];
1583 bfd_boolean copy;
1584 struct xcoff_link_hash_entry *h;
1586 /* At this point we know that the TOC entry is
1587 for an externally visible symbol. */
1588 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1589 relbuf);
1590 if (relname == NULL)
1591 goto error_return;
1593 /* We only merge TOC entries if the TC name is
1594 the same as the symbol name. This handles
1595 the normal case, but not common cases like
1596 SYM.P4 which gcc generates to store SYM + 4
1597 in the TOC. FIXME. */
1598 if (strcmp (name, relname) == 0)
1600 copy = (! info->keep_memory
1601 || relsym._n._n_n._n_zeroes != 0
1602 || relsym._n._n_n._n_offset == 0);
1603 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1604 relname, TRUE, copy,
1605 FALSE);
1606 if (h == NULL)
1607 goto error_return;
1609 /* At this point h->root.type could be
1610 bfd_link_hash_new. That should be OK,
1611 since we know for sure that we will come
1612 across this symbol as we step through the
1613 file. */
1615 /* We store h in *sym_hash for the
1616 convenience of the relocate_section
1617 function. */
1618 *sym_hash = h;
1620 if (h->toc_section != NULL)
1622 asection **rel_csects;
1624 /* We already have a TOC entry for this
1625 symbol, so we can just ignore this
1626 one. */
1627 rel_csects =
1628 reloc_info[enclosing->target_index].csects;
1629 rel_csects[relindx] = bfd_und_section_ptr;
1630 break;
1633 /* We are about to create a TOC entry for
1634 this symbol. */
1635 set_toc = h;
1642 asection *enclosing;
1644 /* We need to create a new section. We get the name from
1645 the csect storage mapping class, so that the linker can
1646 accumulate similar csects together. */
1648 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1649 if (NULL == csect)
1650 goto error_return;
1652 /* The enclosing section is the main section : .data, .text
1653 or .bss that the csect is coming from. */
1654 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1655 if (enclosing == NULL)
1656 goto error_return;
1658 if (! bfd_is_abs_section (enclosing)
1659 && ((bfd_vma) sym.n_value < enclosing->vma
1660 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1661 > enclosing->vma + enclosing->size)))
1663 (*_bfd_error_handler)
1664 (_("%B: csect `%s' not in enclosing section"),
1665 abfd, name);
1666 bfd_set_error (bfd_error_bad_value);
1667 goto error_return;
1669 csect->vma = sym.n_value;
1670 csect->filepos = (enclosing->filepos
1671 + sym.n_value
1672 - enclosing->vma);
1673 csect->size = aux.x_csect.x_scnlen.l;
1674 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1675 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1677 /* Record the enclosing section in the tdata for this new
1678 section. */
1679 amt = sizeof (struct coff_section_tdata);
1680 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1681 if (csect->used_by_bfd == NULL)
1682 goto error_return;
1683 amt = sizeof (struct xcoff_section_tdata);
1684 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1685 if (coff_section_data (abfd, csect)->tdata == NULL)
1686 goto error_return;
1687 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1688 xcoff_section_data (abfd, csect)->lineno_count =
1689 enclosing->lineno_count;
1691 if (enclosing->owner == abfd)
1693 struct internal_reloc *relocs;
1694 bfd_size_type relindx;
1695 struct internal_reloc *rel;
1696 asection **rel_csect;
1698 relocs = reloc_info[enclosing->target_index].relocs;
1699 amt = enclosing->reloc_count;
1700 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1702 rel = relocs + relindx;
1703 rel_csect = (reloc_info[enclosing->target_index].csects
1704 + relindx);
1706 csect->rel_filepos = (enclosing->rel_filepos
1707 + relindx * bfd_coff_relsz (abfd));
1708 while (relindx < enclosing->reloc_count
1709 && *rel_csect == NULL
1710 && rel->r_vaddr < csect->vma + csect->size)
1713 *rel_csect = csect;
1714 csect->flags |= SEC_RELOC;
1715 ++csect->reloc_count;
1716 ++relindx;
1717 ++rel;
1718 ++rel_csect;
1722 /* There are a number of other fields and section flags
1723 which we do not bother to set. */
1725 csect_index = ((esym
1726 - (bfd_byte *) obj_coff_external_syms (abfd))
1727 / symesz);
1729 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1731 if (first_csect == NULL)
1732 first_csect = csect;
1734 /* If this symbol is external, we treat it as starting at the
1735 beginning of the newly created section. */
1736 if (EXTERN_SYM_P (sym.n_sclass))
1738 section = csect;
1739 value = 0;
1742 /* If this is a TOC section for a symbol, record it. */
1743 if (set_toc != NULL)
1744 set_toc->toc_section = csect;
1746 break;
1748 case XTY_LD:
1749 /* This is a label definition. The x_scnlen field is the
1750 symbol index of the csect. Usually the XTY_LD symbol will
1751 follow its appropriate XTY_SD symbol. The .set pseudo op can
1752 cause the XTY_LD to not follow the XTY_SD symbol. */
1754 bfd_boolean bad;
1756 bad = FALSE;
1757 if (aux.x_csect.x_scnlen.l < 0
1758 || (aux.x_csect.x_scnlen.l
1759 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1760 bad = TRUE;
1761 if (! bad)
1763 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1764 if (section == NULL
1765 || (section->flags & SEC_HAS_CONTENTS) == 0)
1766 bad = TRUE;
1768 if (bad)
1770 (*_bfd_error_handler)
1771 (_("%B: misplaced XTY_LD `%s'"),
1772 abfd, name);
1773 bfd_set_error (bfd_error_bad_value);
1774 goto error_return;
1776 csect = section;
1777 value = sym.n_value - csect->vma;
1779 break;
1781 case XTY_CM:
1782 /* This is an unitialized csect. We could base the name on
1783 the storage mapping class, but we don't bother except for
1784 an XMC_TD symbol. If this csect is externally visible,
1785 it is a common symbol. We put XMC_TD symbols in sections
1786 named .tocbss, and rely on the linker script to put that
1787 in the TOC area. */
1789 if (aux.x_csect.x_smclas == XMC_TD)
1791 /* The linker script puts the .td section in the data
1792 section after the .tc section. */
1793 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1794 SEC_ALLOC);
1796 else
1797 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1798 SEC_ALLOC);
1800 if (csect == NULL)
1801 goto error_return;
1802 csect->vma = sym.n_value;
1803 csect->size = aux.x_csect.x_scnlen.l;
1804 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1805 /* There are a number of other fields and section flags
1806 which we do not bother to set. */
1808 csect_index = ((esym
1809 - (bfd_byte *) obj_coff_external_syms (abfd))
1810 / symesz);
1812 amt = sizeof (struct coff_section_tdata);
1813 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1814 if (csect->used_by_bfd == NULL)
1815 goto error_return;
1816 amt = sizeof (struct xcoff_section_tdata);
1817 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1818 if (coff_section_data (abfd, csect)->tdata == NULL)
1819 goto error_return;
1820 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1822 if (first_csect == NULL)
1823 first_csect = csect;
1825 if (EXTERN_SYM_P (sym.n_sclass))
1827 csect->flags |= SEC_IS_COMMON;
1828 csect->size = 0;
1829 section = csect;
1830 value = aux.x_csect.x_scnlen.l;
1833 break;
1836 /* Check for magic symbol names. */
1837 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1838 && aux.x_csect.x_smclas != XMC_TC
1839 && aux.x_csect.x_smclas != XMC_TD)
1841 int i = -1;
1843 if (name[0] == '_')
1845 if (strcmp (name, "_text") == 0)
1846 i = XCOFF_SPECIAL_SECTION_TEXT;
1847 else if (strcmp (name, "_etext") == 0)
1848 i = XCOFF_SPECIAL_SECTION_ETEXT;
1849 else if (strcmp (name, "_data") == 0)
1850 i = XCOFF_SPECIAL_SECTION_DATA;
1851 else if (strcmp (name, "_edata") == 0)
1852 i = XCOFF_SPECIAL_SECTION_EDATA;
1853 else if (strcmp (name, "_end") == 0)
1854 i = XCOFF_SPECIAL_SECTION_END;
1856 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1857 i = XCOFF_SPECIAL_SECTION_END2;
1859 if (i != -1)
1860 xcoff_hash_table (info)->special_sections[i] = csect;
1863 /* Now we have enough information to add the symbol to the
1864 linker hash table. */
1866 if (EXTERN_SYM_P (sym.n_sclass))
1868 bfd_boolean copy;
1869 flagword flags;
1871 BFD_ASSERT (section != NULL);
1873 /* We must copy the name into memory if we got it from the
1874 syment itself, rather than the string table. */
1875 copy = default_copy;
1876 if (sym._n._n_n._n_zeroes != 0
1877 || sym._n._n_n._n_offset == 0)
1878 copy = TRUE;
1880 /* Ignore global linkage code when linking statically. */
1881 if (info->static_link
1882 && (smtyp == XTY_SD || smtyp == XTY_LD)
1883 && aux.x_csect.x_smclas == XMC_GL)
1885 section = bfd_und_section_ptr;
1886 value = 0;
1889 /* The AIX linker appears to only detect multiple symbol
1890 definitions when there is a reference to the symbol. If
1891 a symbol is defined multiple times, and the only
1892 references are from the same object file, the AIX linker
1893 appears to permit it. It does not merge the different
1894 definitions, but handles them independently. On the
1895 other hand, if there is a reference, the linker reports
1896 an error.
1898 This matters because the AIX <net/net_globals.h> header
1899 file actually defines an initialized array, so we have to
1900 actually permit that to work.
1902 Just to make matters even more confusing, the AIX linker
1903 appears to permit multiple symbol definitions whenever
1904 the second definition is in an archive rather than an
1905 object file. This may be a consequence of the manner in
1906 which it handles archives: I think it may load the entire
1907 archive in as separate csects, and then let garbage
1908 collection discard symbols.
1910 We also have to handle the case of statically linking a
1911 shared object, which will cause symbol redefinitions,
1912 although this is an easier case to detect. */
1913 else if (info->output_bfd->xvec == abfd->xvec)
1915 if (! bfd_is_und_section (section))
1916 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1917 name, TRUE, copy, FALSE);
1918 else
1919 /* Make a copy of the symbol name to prevent problems with
1920 merging symbols. */
1921 *sym_hash = ((struct xcoff_link_hash_entry *)
1922 bfd_wrapped_link_hash_lookup (abfd, info, name,
1923 TRUE, TRUE, FALSE));
1925 if (*sym_hash == NULL)
1926 goto error_return;
1927 if (((*sym_hash)->root.type == bfd_link_hash_defined
1928 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1929 && ! bfd_is_und_section (section)
1930 && ! bfd_is_com_section (section))
1932 /* This is a second definition of a defined symbol. */
1933 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1934 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1936 /* The existing symbol is from a shared library.
1937 Replace it. */
1938 (*sym_hash)->root.type = bfd_link_hash_undefined;
1939 (*sym_hash)->root.u.undef.abfd =
1940 (*sym_hash)->root.u.def.section->owner;
1942 else if (abfd->my_archive != NULL)
1944 /* This is a redefinition in an object contained
1945 in an archive. Just ignore it. See the
1946 comment above. */
1947 section = bfd_und_section_ptr;
1948 value = 0;
1950 else if (sym.n_sclass == C_AIX_WEAKEXT
1951 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1953 /* At least one of the definitions is weak.
1954 Allow the normal rules to take effect. */
1956 else if ((*sym_hash)->root.u.undef.next != NULL
1957 || info->hash->undefs_tail == &(*sym_hash)->root)
1959 /* This symbol has been referenced. In this
1960 case, we just continue and permit the
1961 multiple definition error. See the comment
1962 above about the behaviour of the AIX linker. */
1964 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1966 /* The symbols are both csects of the same
1967 class. There is at least a chance that this
1968 is a semi-legitimate redefinition. */
1969 section = bfd_und_section_ptr;
1970 value = 0;
1971 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1974 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1975 && (*sym_hash)->root.type == bfd_link_hash_defined
1976 && (bfd_is_und_section (section)
1977 || bfd_is_com_section (section)))
1979 /* This is a reference to a multiply defined symbol.
1980 Report the error now. See the comment above
1981 about the behaviour of the AIX linker. We could
1982 also do this with warning symbols, but I'm not
1983 sure the XCOFF linker is wholly prepared to
1984 handle them, and that would only be a warning,
1985 not an error. */
1986 if (! ((*info->callbacks->multiple_definition)
1987 (info, &(*sym_hash)->root, NULL, NULL, (bfd_vma) 0)))
1988 goto error_return;
1989 /* Try not to give this error too many times. */
1990 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1994 /* _bfd_generic_link_add_one_symbol may call the linker to
1995 generate an error message, and the linker may try to read
1996 the symbol table to give a good error. Right now, the
1997 line numbers are in an inconsistent state, since they are
1998 counted both in the real sections and in the new csects.
1999 We need to leave the count in the real sections so that
2000 the linker can report the line number of the error
2001 correctly, so temporarily clobber the link to the csects
2002 so that the linker will not try to read the line numbers
2003 a second time from the csects. */
2004 BFD_ASSERT (last_real->next == first_csect);
2005 last_real->next = NULL;
2006 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2007 if (! (_bfd_generic_link_add_one_symbol
2008 (info, abfd, name, flags, section, value,
2009 NULL, copy, TRUE,
2010 (struct bfd_link_hash_entry **) sym_hash)))
2011 goto error_return;
2012 last_real->next = first_csect;
2014 if (smtyp == XTY_CM)
2016 if ((*sym_hash)->root.type != bfd_link_hash_common
2017 || (*sym_hash)->root.u.c.p->section != csect)
2018 /* We don't need the common csect we just created. */
2019 csect->size = 0;
2020 else
2021 (*sym_hash)->root.u.c.p->alignment_power
2022 = csect->alignment_power;
2025 if (info->output_bfd->xvec == abfd->xvec)
2027 int flag;
2029 if (smtyp == XTY_ER
2030 || smtyp == XTY_CM
2031 || section == bfd_und_section_ptr)
2032 flag = XCOFF_REF_REGULAR;
2033 else
2034 flag = XCOFF_DEF_REGULAR;
2035 (*sym_hash)->flags |= flag;
2037 if ((*sym_hash)->smclas == XMC_UA
2038 || flag == XCOFF_DEF_REGULAR)
2039 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2043 if (smtyp == XTY_ER)
2044 *csect_cache = section;
2045 else
2047 *csect_cache = csect;
2048 if (csect != NULL)
2049 xcoff_section_data (abfd, csect)->last_symndx
2050 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2053 esym += (sym.n_numaux + 1) * symesz;
2054 sym_hash += sym.n_numaux + 1;
2055 csect_cache += sym.n_numaux + 1;
2056 lineno_counts += sym.n_numaux + 1;
2059 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2061 /* Make sure that we have seen all the relocs. */
2062 for (o = abfd->sections; o != first_csect; o = o->next)
2064 /* Debugging sections have no csects. */
2065 if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING)
2066 continue;
2068 /* Reset the section size and the line number count, since the
2069 data is now attached to the csects. Don't reset the size of
2070 the .debug section, since we need to read it below in
2071 bfd_xcoff_size_dynamic_sections. */
2072 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2073 o->size = 0;
2074 o->lineno_count = 0;
2076 if ((o->flags & SEC_RELOC) != 0)
2078 bfd_size_type i;
2079 struct internal_reloc *rel;
2080 asection **rel_csect;
2082 rel = reloc_info[o->target_index].relocs;
2083 rel_csect = reloc_info[o->target_index].csects;
2085 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2087 if (*rel_csect == NULL)
2089 (*_bfd_error_handler)
2090 (_("%B: reloc %s:%d not in csect"),
2091 abfd, o->name, i);
2092 bfd_set_error (bfd_error_bad_value);
2093 goto error_return;
2096 /* We identify all function symbols that are the target
2097 of a relocation, so that we can create glue code for
2098 functions imported from dynamic objects. */
2099 if (info->output_bfd->xvec == abfd->xvec
2100 && *rel_csect != bfd_und_section_ptr
2101 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2103 struct xcoff_link_hash_entry *h;
2105 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2106 /* If the symbol name starts with a period, it is
2107 the code of a function. If the symbol is
2108 currently undefined, then add an undefined symbol
2109 for the function descriptor. This should do no
2110 harm, because any regular object that defines the
2111 function should also define the function
2112 descriptor. It helps, because it means that we
2113 will identify the function descriptor with a
2114 dynamic object if a dynamic object defines it. */
2115 if (h->root.root.string[0] == '.'
2116 && h->descriptor == NULL)
2118 struct xcoff_link_hash_entry *hds;
2119 struct bfd_link_hash_entry *bh;
2121 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2122 h->root.root.string + 1,
2123 TRUE, FALSE, TRUE);
2124 if (hds == NULL)
2125 goto error_return;
2126 if (hds->root.type == bfd_link_hash_new)
2128 bh = &hds->root;
2129 if (! (_bfd_generic_link_add_one_symbol
2130 (info, abfd, hds->root.root.string,
2131 (flagword) 0, bfd_und_section_ptr,
2132 (bfd_vma) 0, NULL, FALSE,
2133 TRUE, &bh)))
2134 goto error_return;
2135 hds = (struct xcoff_link_hash_entry *) bh;
2137 hds->flags |= XCOFF_DESCRIPTOR;
2138 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2139 hds->descriptor = h;
2140 h->descriptor = hds;
2142 if (h->root.root.string[0] == '.')
2143 h->flags |= XCOFF_CALLED;
2147 free (reloc_info[o->target_index].csects);
2148 reloc_info[o->target_index].csects = NULL;
2150 /* Reset SEC_RELOC and the reloc_count, since the reloc
2151 information is now attached to the csects. */
2152 o->flags &=~ SEC_RELOC;
2153 o->reloc_count = 0;
2155 /* If we are not keeping memory, free the reloc information. */
2156 if (! info->keep_memory
2157 && coff_section_data (abfd, o) != NULL
2158 && coff_section_data (abfd, o)->relocs != NULL
2159 && ! coff_section_data (abfd, o)->keep_relocs)
2161 free (coff_section_data (abfd, o)->relocs);
2162 coff_section_data (abfd, o)->relocs = NULL;
2166 /* Free up the line numbers. FIXME: We could cache these
2167 somewhere for the final link, to avoid reading them again. */
2168 if (reloc_info[o->target_index].linenos != NULL)
2170 free (reloc_info[o->target_index].linenos);
2171 reloc_info[o->target_index].linenos = NULL;
2175 free (reloc_info);
2177 obj_coff_keep_syms (abfd) = keep_syms;
2179 return TRUE;
2181 error_return:
2182 if (reloc_info != NULL)
2184 for (o = abfd->sections; o != NULL; o = o->next)
2186 if (reloc_info[o->target_index].csects != NULL)
2187 free (reloc_info[o->target_index].csects);
2188 if (reloc_info[o->target_index].linenos != NULL)
2189 free (reloc_info[o->target_index].linenos);
2191 free (reloc_info);
2193 obj_coff_keep_syms (abfd) = keep_syms;
2194 return FALSE;
2197 #undef N_TMASK
2198 #undef N_BTSHFT
2200 /* Add symbols from an XCOFF object file. */
2202 static bfd_boolean
2203 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2205 if (! _bfd_coff_get_external_symbols (abfd))
2206 return FALSE;
2207 if (! xcoff_link_add_symbols (abfd, info))
2208 return FALSE;
2209 if (! info->keep_memory)
2211 if (! _bfd_coff_free_symbols (abfd))
2212 return FALSE;
2214 return TRUE;
2217 /* Look through the loader symbols to see if this dynamic object
2218 should be included in the link. The native linker uses the loader
2219 symbols, not the normal symbol table, so we do too. */
2221 static bfd_boolean
2222 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2223 struct bfd_link_info *info,
2224 bfd_boolean *pneeded,
2225 bfd **subsbfd)
2227 asection *lsec;
2228 bfd_byte *contents;
2229 struct internal_ldhdr ldhdr;
2230 const char *strings;
2231 bfd_byte *elsym, *elsymend;
2233 *pneeded = FALSE;
2235 lsec = bfd_get_section_by_name (abfd, ".loader");
2236 if (lsec == NULL)
2237 /* There are no symbols, so don't try to include it. */
2238 return TRUE;
2240 if (! xcoff_get_section_contents (abfd, lsec))
2241 return FALSE;
2242 contents = coff_section_data (abfd, lsec)->contents;
2244 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2246 strings = (char *) contents + ldhdr.l_stoff;
2248 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2250 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2251 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2253 struct internal_ldsym ldsym;
2254 char nambuf[SYMNMLEN + 1];
2255 const char *name;
2256 struct bfd_link_hash_entry *h;
2258 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2260 /* We are only interested in exported symbols. */
2261 if ((ldsym.l_smtype & L_EXPORT) == 0)
2262 continue;
2264 if (ldsym._l._l_l._l_zeroes == 0)
2265 name = strings + ldsym._l._l_l._l_offset;
2266 else
2268 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2269 nambuf[SYMNMLEN] = '\0';
2270 name = nambuf;
2273 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2275 /* We are only interested in symbols that are currently
2276 undefined. At this point we know that we are using an XCOFF
2277 hash table. */
2278 if (h != NULL
2279 && h->type == bfd_link_hash_undefined
2280 && (((struct xcoff_link_hash_entry *) h)->flags
2281 & XCOFF_DEF_DYNAMIC) == 0)
2283 if (!(*info->callbacks
2284 ->add_archive_element) (info, abfd, name, subsbfd))
2285 return FALSE;
2286 *pneeded = TRUE;
2287 return TRUE;
2291 /* We do not need this shared object. */
2292 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2294 free (coff_section_data (abfd, lsec)->contents);
2295 coff_section_data (abfd, lsec)->contents = NULL;
2298 return TRUE;
2301 /* Look through the symbols to see if this object file should be
2302 included in the link. */
2304 static bfd_boolean
2305 xcoff_link_check_ar_symbols (bfd *abfd,
2306 struct bfd_link_info *info,
2307 bfd_boolean *pneeded,
2308 bfd **subsbfd)
2310 bfd_size_type symesz;
2311 bfd_byte *esym;
2312 bfd_byte *esym_end;
2314 *pneeded = FALSE;
2316 if ((abfd->flags & DYNAMIC) != 0
2317 && ! info->static_link
2318 && info->output_bfd->xvec == abfd->xvec)
2319 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2321 symesz = bfd_coff_symesz (abfd);
2322 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2323 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2324 while (esym < esym_end)
2326 struct internal_syment sym;
2328 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2330 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2332 const char *name;
2333 char buf[SYMNMLEN + 1];
2334 struct bfd_link_hash_entry *h;
2336 /* This symbol is externally visible, and is defined by this
2337 object file. */
2338 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2340 if (name == NULL)
2341 return FALSE;
2342 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2344 /* We are only interested in symbols that are currently
2345 undefined. If a symbol is currently known to be common,
2346 XCOFF linkers do not bring in an object file which
2347 defines it. We also don't bring in symbols to satisfy
2348 undefined references in shared objects. */
2349 if (h != NULL
2350 && h->type == bfd_link_hash_undefined
2351 && (info->output_bfd->xvec != abfd->xvec
2352 || (((struct xcoff_link_hash_entry *) h)->flags
2353 & XCOFF_DEF_DYNAMIC) == 0))
2355 if (!(*info->callbacks
2356 ->add_archive_element) (info, abfd, name, subsbfd))
2357 return FALSE;
2358 *pneeded = TRUE;
2359 return TRUE;
2363 esym += (sym.n_numaux + 1) * symesz;
2366 /* We do not need this object file. */
2367 return TRUE;
2370 /* Check a single archive element to see if we need to include it in
2371 the link. *PNEEDED is set according to whether this element is
2372 needed in the link or not. This is called via
2373 _bfd_generic_link_add_archive_symbols. */
2375 static bfd_boolean
2376 xcoff_link_check_archive_element (bfd *abfd,
2377 struct bfd_link_info *info,
2378 bfd_boolean *pneeded)
2380 bfd_boolean keep_syms_p;
2381 bfd *oldbfd;
2383 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2384 if (!_bfd_coff_get_external_symbols (abfd))
2385 return FALSE;
2387 oldbfd = abfd;
2388 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2389 return FALSE;
2391 if (*pneeded)
2393 /* Potentially, the add_archive_element hook may have set a
2394 substitute BFD for us. */
2395 if (abfd != oldbfd)
2397 if (!keep_syms_p
2398 && !_bfd_coff_free_symbols (oldbfd))
2399 return FALSE;
2400 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2401 if (!_bfd_coff_get_external_symbols (abfd))
2402 return FALSE;
2404 if (!xcoff_link_add_symbols (abfd, info))
2405 return FALSE;
2406 if (info->keep_memory)
2407 keep_syms_p = TRUE;
2410 if (!keep_syms_p)
2412 if (!_bfd_coff_free_symbols (abfd))
2413 return FALSE;
2416 return TRUE;
2419 /* Given an XCOFF BFD, add symbols to the global hash table as
2420 appropriate. */
2422 bfd_boolean
2423 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2425 switch (bfd_get_format (abfd))
2427 case bfd_object:
2428 return xcoff_link_add_object_symbols (abfd, info);
2430 case bfd_archive:
2431 /* If the archive has a map, do the usual search. We then need
2432 to check the archive for dynamic objects, because they may not
2433 appear in the archive map even though they should, perhaps, be
2434 included. If the archive has no map, we just consider each object
2435 file in turn, since that apparently is what the AIX native linker
2436 does. */
2437 if (bfd_has_map (abfd))
2439 if (! (_bfd_generic_link_add_archive_symbols
2440 (abfd, info, xcoff_link_check_archive_element)))
2441 return FALSE;
2445 bfd *member;
2447 member = bfd_openr_next_archived_file (abfd, NULL);
2448 while (member != NULL)
2450 if (bfd_check_format (member, bfd_object)
2451 && (info->output_bfd->xvec == member->xvec)
2452 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2454 bfd_boolean needed;
2456 if (! xcoff_link_check_archive_element (member, info,
2457 &needed))
2458 return FALSE;
2459 if (needed)
2460 member->archive_pass = -1;
2462 member = bfd_openr_next_archived_file (abfd, member);
2466 return TRUE;
2468 default:
2469 bfd_set_error (bfd_error_wrong_format);
2470 return FALSE;
2474 bfd_boolean
2475 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2476 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2477 struct bfd_link_hash_entry *harg)
2479 struct xcoff_link_hash_entry *h;
2481 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2482 return FALSE;
2484 h = (struct xcoff_link_hash_entry *) harg;
2485 h->flags |= XCOFF_DEF_REGULAR;
2486 return TRUE;
2489 /* If symbol H has not been interpreted as a function descriptor,
2490 see whether it should be. Set up its descriptor information if so. */
2492 static bfd_boolean
2493 xcoff_find_function (struct bfd_link_info *info,
2494 struct xcoff_link_hash_entry *h)
2496 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2497 && h->root.root.string[0] != '.')
2499 char *fnname;
2500 struct xcoff_link_hash_entry *hfn;
2501 bfd_size_type amt;
2503 amt = strlen (h->root.root.string) + 2;
2504 fnname = bfd_malloc (amt);
2505 if (fnname == NULL)
2506 return FALSE;
2507 fnname[0] = '.';
2508 strcpy (fnname + 1, h->root.root.string);
2509 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2510 fnname, FALSE, FALSE, TRUE);
2511 free (fnname);
2512 if (hfn != NULL
2513 && hfn->smclas == XMC_PR
2514 && (hfn->root.type == bfd_link_hash_defined
2515 || hfn->root.type == bfd_link_hash_defweak))
2517 h->flags |= XCOFF_DESCRIPTOR;
2518 h->descriptor = hfn;
2519 hfn->descriptor = h;
2522 return TRUE;
2525 /* Return true if the given bfd contains at least one shared object. */
2527 static bfd_boolean
2528 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2529 bfd *archive)
2531 struct xcoff_archive_info *archive_info;
2532 bfd *member;
2534 archive_info = xcoff_get_archive_info (info, archive);
2535 if (!archive_info->know_contains_shared_object_p)
2537 member = bfd_openr_next_archived_file (archive, NULL);
2538 while (member != NULL && (member->flags & DYNAMIC) == 0)
2539 member = bfd_openr_next_archived_file (archive, member);
2541 archive_info->contains_shared_object_p = (member != NULL);
2542 archive_info->know_contains_shared_object_p = 1;
2544 return archive_info->contains_shared_object_p;
2547 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2548 qualifies for export by -bexpall. */
2550 static bfd_boolean
2551 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2553 /* Exclude symbols beginning with '_'. */
2554 if (h->root.root.string[0] == '_')
2555 return FALSE;
2557 /* Exclude archive members that would otherwise be unreferenced. */
2558 if ((h->flags & XCOFF_MARK) == 0
2559 && (h->root.type == bfd_link_hash_defined
2560 || h->root.type == bfd_link_hash_defweak)
2561 && h->root.u.def.section->owner != NULL
2562 && h->root.u.def.section->owner->my_archive != NULL)
2563 return FALSE;
2565 return TRUE;
2568 /* Return true if symbol H qualifies for the forms of automatic export
2569 specified by AUTO_EXPORT_FLAGS. */
2571 static bfd_boolean
2572 xcoff_auto_export_p (struct bfd_link_info *info,
2573 struct xcoff_link_hash_entry *h,
2574 unsigned int auto_export_flags)
2576 /* Don't automatically export things that were explicitly exported. */
2577 if ((h->flags & XCOFF_EXPORT) != 0)
2578 return FALSE;
2580 /* Don't export things that we don't define. */
2581 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2582 return FALSE;
2584 /* Don't export functions; export their descriptors instead. */
2585 if (h->root.root.string[0] == '.')
2586 return FALSE;
2588 /* We don't export a symbol which is being defined by an object
2589 included from an archive which contains a shared object. The
2590 rationale is that if an archive contains both an unshared and
2591 a shared object, then there must be some reason that the
2592 unshared object is unshared, and we don't want to start
2593 providing a shared version of it. In particular, this solves
2594 a bug involving the _savefNN set of functions. gcc will call
2595 those functions without providing a slot to restore the TOC,
2596 so it is essential that these functions be linked in directly
2597 and not from a shared object, which means that a shared
2598 object which also happens to link them in must not export
2599 them. This is confusing, but I haven't been able to think of
2600 a different approach. Note that the symbols can, of course,
2601 be exported explicitly. */
2602 if (h->root.type == bfd_link_hash_defined
2603 || h->root.type == bfd_link_hash_defweak)
2605 bfd *owner;
2607 owner = h->root.u.def.section->owner;
2608 if (owner != NULL
2609 && owner->my_archive != NULL
2610 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2611 return FALSE;
2614 /* Otherwise, all symbols are exported by -bexpfull. */
2615 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2616 return TRUE;
2618 /* Despite its name, -bexpall exports most but not all symbols. */
2619 if ((auto_export_flags & XCOFF_EXPALL) != 0
2620 && xcoff_covered_by_expall_p (h))
2621 return TRUE;
2623 return FALSE;
2626 /* Return true if relocation REL needs to be copied to the .loader section.
2627 If REL is against a global symbol, H is that symbol, otherwise it
2628 is null. */
2630 static bfd_boolean
2631 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2632 struct xcoff_link_hash_entry *h)
2634 if (!xcoff_hash_table (info)->loader_section)
2635 return FALSE;
2637 switch (rel->r_type)
2639 case R_TOC:
2640 case R_GL:
2641 case R_TCL:
2642 case R_TRL:
2643 case R_TRLA:
2644 /* We should never need a .loader reloc for a TOC-relative reloc. */
2645 return FALSE;
2647 default:
2648 /* In this case, relocations against defined symbols can be resolved
2649 statically. */
2650 if (h == NULL
2651 || h->root.type == bfd_link_hash_defined
2652 || h->root.type == bfd_link_hash_defweak
2653 || h->root.type == bfd_link_hash_common)
2654 return FALSE;
2656 /* We will always provide a local definition of function symbols,
2657 even if we don't have one yet. */
2658 if ((h->flags & XCOFF_CALLED) != 0)
2659 return FALSE;
2661 return TRUE;
2663 case R_POS:
2664 case R_NEG:
2665 case R_RL:
2666 case R_RLA:
2667 /* Absolute relocations against absolute symbols can be
2668 resolved statically. */
2669 if (h != NULL
2670 && (h->root.type == bfd_link_hash_defined
2671 || h->root.type == bfd_link_hash_defweak)
2672 && bfd_is_abs_section (h->root.u.def.section))
2673 return FALSE;
2675 return TRUE;
2679 /* Mark a symbol as not being garbage, including the section in which
2680 it is defined. */
2682 static inline bfd_boolean
2683 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2685 if ((h->flags & XCOFF_MARK) != 0)
2686 return TRUE;
2688 h->flags |= XCOFF_MARK;
2690 /* If we're marking an undefined symbol, try find some way of
2691 defining it. */
2692 if (!info->relocatable
2693 && (h->flags & XCOFF_IMPORT) == 0
2694 && (h->flags & XCOFF_DEF_REGULAR) == 0
2695 && (h->root.type == bfd_link_hash_undefined
2696 || h->root.type == bfd_link_hash_undefweak))
2698 /* First check whether this symbol can be interpreted as an
2699 undefined function descriptor for a defined function symbol. */
2700 if (!xcoff_find_function (info, h))
2701 return FALSE;
2703 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2704 && (h->descriptor->root.type == bfd_link_hash_defined
2705 || h->descriptor->root.type == bfd_link_hash_defweak))
2707 /* This is a descriptor for a defined symbol, but the input
2708 objects have not defined the descriptor itself. Fill in
2709 the definition automatically.
2711 Note that we do this even if we found a dynamic definition
2712 of H. The local function definition logically overrides
2713 the dynamic one. */
2714 asection *sec;
2716 sec = xcoff_hash_table (info)->descriptor_section;
2717 h->root.type = bfd_link_hash_defined;
2718 h->root.u.def.section = sec;
2719 h->root.u.def.value = sec->size;
2720 h->smclas = XMC_DS;
2721 h->flags |= XCOFF_DEF_REGULAR;
2723 /* The size of the function descriptor depends on whether this
2724 is xcoff32 (12) or xcoff64 (24). */
2725 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2727 /* A function descriptor uses two relocs: one for the
2728 associated code, and one for the TOC address. */
2729 xcoff_hash_table (info)->ldrel_count += 2;
2730 sec->reloc_count += 2;
2732 /* Mark the function itself. */
2733 if (!xcoff_mark_symbol (info, h->descriptor))
2734 return FALSE;
2736 /* Mark the TOC section, so that we get an anchor
2737 to relocate against. */
2738 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2739 return FALSE;
2741 /* We handle writing out the contents of the descriptor in
2742 xcoff_write_global_symbol. */
2744 else if (info->static_link)
2745 /* We can't get a symbol value dynamically, so just assume
2746 that it's undefined. */
2747 h->flags |= XCOFF_WAS_UNDEFINED;
2748 else if ((h->flags & XCOFF_CALLED) != 0)
2750 /* This is a function symbol for which we need to create
2751 linkage code. */
2752 asection *sec;
2753 struct xcoff_link_hash_entry *hds;
2755 /* Mark the descriptor (and its TOC section). */
2756 hds = h->descriptor;
2757 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2758 || hds->root.type == bfd_link_hash_undefweak)
2759 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2760 if (!xcoff_mark_symbol (info, hds))
2761 return FALSE;
2763 /* Treat this symbol as undefined if the descriptor was. */
2764 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2765 h->flags |= XCOFF_WAS_UNDEFINED;
2767 /* Allocate room for the global linkage code itself. */
2768 sec = xcoff_hash_table (info)->linkage_section;
2769 h->root.type = bfd_link_hash_defined;
2770 h->root.u.def.section = sec;
2771 h->root.u.def.value = sec->size;
2772 h->smclas = XMC_GL;
2773 h->flags |= XCOFF_DEF_REGULAR;
2774 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2776 /* The global linkage code requires a TOC entry for the
2777 descriptor. */
2778 if (hds->toc_section == NULL)
2780 int byte_size;
2782 /* 32 vs 64
2783 xcoff32 uses 4 bytes in the toc.
2784 xcoff64 uses 8 bytes in the toc. */
2785 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2786 byte_size = 8;
2787 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2788 byte_size = 4;
2789 else
2790 return FALSE;
2792 /* Allocate room in the fallback TOC section. */
2793 hds->toc_section = xcoff_hash_table (info)->toc_section;
2794 hds->u.toc_offset = hds->toc_section->size;
2795 hds->toc_section->size += byte_size;
2796 if (!xcoff_mark (info, hds->toc_section))
2797 return FALSE;
2799 /* Allocate room for a static and dynamic R_TOC
2800 relocation. */
2801 ++xcoff_hash_table (info)->ldrel_count;
2802 ++hds->toc_section->reloc_count;
2804 /* Set the index to -2 to force this symbol to
2805 get written out. */
2806 hds->indx = -2;
2807 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2810 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2812 /* Record that the symbol was undefined, then import it.
2813 -brtl links use a special fake import file. */
2814 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2815 if (xcoff_hash_table (info)->rtld)
2817 if (!xcoff_set_import_path (info, h, "", "..", ""))
2818 return FALSE;
2820 else
2822 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2823 return FALSE;
2828 if (h->root.type == bfd_link_hash_defined
2829 || h->root.type == bfd_link_hash_defweak)
2831 asection *hsec;
2833 hsec = h->root.u.def.section;
2834 if (! bfd_is_abs_section (hsec)
2835 && (hsec->flags & SEC_MARK) == 0)
2837 if (! xcoff_mark (info, hsec))
2838 return FALSE;
2842 if (h->toc_section != NULL
2843 && (h->toc_section->flags & SEC_MARK) == 0)
2845 if (! xcoff_mark (info, h->toc_section))
2846 return FALSE;
2849 return TRUE;
2852 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2853 If the symbol exists, set FLAGS. */
2855 static bfd_boolean
2856 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2857 const char *name, unsigned int flags)
2859 struct xcoff_link_hash_entry *h;
2861 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2862 FALSE, FALSE, TRUE);
2863 if (h != NULL)
2865 h->flags |= flags;
2866 if (h->root.type == bfd_link_hash_defined
2867 || h->root.type == bfd_link_hash_defweak)
2869 if (!xcoff_mark (info, h->root.u.def.section))
2870 return FALSE;
2873 return TRUE;
2876 /* The mark phase of garbage collection. For a given section, mark
2877 it, and all the sections which define symbols to which it refers.
2878 Because this function needs to look at the relocs, we also count
2879 the number of relocs which need to be copied into the .loader
2880 section. */
2882 static bfd_boolean
2883 xcoff_mark (struct bfd_link_info *info, asection *sec)
2885 if (bfd_is_abs_section (sec)
2886 || (sec->flags & SEC_MARK) != 0)
2887 return TRUE;
2889 sec->flags |= SEC_MARK;
2891 if (sec->owner->xvec == info->output_bfd->xvec
2892 && coff_section_data (sec->owner, sec) != NULL
2893 && xcoff_section_data (sec->owner, sec) != NULL)
2895 struct xcoff_link_hash_entry **syms;
2896 struct internal_reloc *rel, *relend;
2897 asection **csects;
2898 unsigned long i, first, last;
2900 /* Mark all the symbols in this section. */
2901 syms = obj_xcoff_sym_hashes (sec->owner);
2902 csects = xcoff_data (sec->owner)->csects;
2903 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2904 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2905 for (i = first; i <= last; i++)
2906 if (csects[i] == sec
2907 && syms[i] != NULL
2908 && (syms[i]->flags & XCOFF_MARK) == 0)
2910 if (!xcoff_mark_symbol (info, syms[i]))
2911 return FALSE;
2914 /* Look through the section relocs. */
2915 if ((sec->flags & SEC_RELOC) != 0
2916 && sec->reloc_count > 0)
2918 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2919 NULL, FALSE, NULL);
2920 if (rel == NULL)
2921 return FALSE;
2922 relend = rel + sec->reloc_count;
2923 for (; rel < relend; rel++)
2925 struct xcoff_link_hash_entry *h;
2927 if ((unsigned int) rel->r_symndx
2928 > obj_raw_syment_count (sec->owner))
2929 continue;
2931 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2932 if (h != NULL)
2934 if ((h->flags & XCOFF_MARK) == 0)
2936 if (!xcoff_mark_symbol (info, h))
2937 return FALSE;
2940 else
2942 asection *rsec;
2944 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2945 if (rsec != NULL
2946 && (rsec->flags & SEC_MARK) == 0)
2948 if (!xcoff_mark (info, rsec))
2949 return FALSE;
2953 /* See if this reloc needs to be copied into the .loader
2954 section. */
2955 if (xcoff_need_ldrel_p (info, rel, h))
2957 ++xcoff_hash_table (info)->ldrel_count;
2958 if (h != NULL)
2959 h->flags |= XCOFF_LDREL;
2963 if (! info->keep_memory
2964 && coff_section_data (sec->owner, sec) != NULL
2965 && coff_section_data (sec->owner, sec)->relocs != NULL
2966 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2968 free (coff_section_data (sec->owner, sec)->relocs);
2969 coff_section_data (sec->owner, sec)->relocs = NULL;
2974 return TRUE;
2977 /* Routines that are called after all the input files have been
2978 handled, but before the sections are laid out in memory. */
2980 /* The sweep phase of garbage collection. Remove all garbage
2981 sections. */
2983 static void
2984 xcoff_sweep (struct bfd_link_info *info)
2986 bfd *sub;
2988 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2990 asection *o;
2992 for (o = sub->sections; o != NULL; o = o->next)
2994 if ((o->flags & SEC_MARK) == 0)
2996 /* Keep all sections from non-XCOFF input files. Keep
2997 special sections. Keep .debug sections for the
2998 moment. */
2999 if (sub->xvec != info->output_bfd->xvec
3000 || o == xcoff_hash_table (info)->debug_section
3001 || o == xcoff_hash_table (info)->loader_section
3002 || o == xcoff_hash_table (info)->linkage_section
3003 || o == xcoff_hash_table (info)->descriptor_section
3004 || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING)
3005 || strcmp (o->name, ".debug") == 0)
3006 o->flags |= SEC_MARK;
3007 else
3009 o->size = 0;
3010 o->reloc_count = 0;
3017 /* Record the number of elements in a set. This is used to output the
3018 correct csect length. */
3020 bfd_boolean
3021 bfd_xcoff_link_record_set (bfd *output_bfd,
3022 struct bfd_link_info *info,
3023 struct bfd_link_hash_entry *harg,
3024 bfd_size_type size)
3026 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3027 struct xcoff_link_size_list *n;
3028 bfd_size_type amt;
3030 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3031 return TRUE;
3033 /* This will hardly ever be called. I don't want to burn four bytes
3034 per global symbol, so instead the size is kept on a linked list
3035 attached to the hash table. */
3036 amt = sizeof (* n);
3037 n = bfd_alloc (output_bfd, amt);
3038 if (n == NULL)
3039 return FALSE;
3040 n->next = xcoff_hash_table (info)->size_list;
3041 n->h = h;
3042 n->size = size;
3043 xcoff_hash_table (info)->size_list = n;
3045 h->flags |= XCOFF_HAS_SIZE;
3047 return TRUE;
3050 /* Import a symbol. */
3052 bfd_boolean
3053 bfd_xcoff_import_symbol (bfd *output_bfd,
3054 struct bfd_link_info *info,
3055 struct bfd_link_hash_entry *harg,
3056 bfd_vma val,
3057 const char *imppath,
3058 const char *impfile,
3059 const char *impmember,
3060 unsigned int syscall_flag)
3062 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3064 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3065 return TRUE;
3067 /* A symbol name which starts with a period is the code for a
3068 function. If the symbol is undefined, then add an undefined
3069 symbol for the function descriptor, and import that instead. */
3070 if (h->root.root.string[0] == '.'
3071 && h->root.type == bfd_link_hash_undefined
3072 && val == (bfd_vma) -1)
3074 struct xcoff_link_hash_entry *hds;
3076 hds = h->descriptor;
3077 if (hds == NULL)
3079 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3080 h->root.root.string + 1,
3081 TRUE, FALSE, TRUE);
3082 if (hds == NULL)
3083 return FALSE;
3084 if (hds->root.type == bfd_link_hash_new)
3086 hds->root.type = bfd_link_hash_undefined;
3087 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3089 hds->flags |= XCOFF_DESCRIPTOR;
3090 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3091 hds->descriptor = h;
3092 h->descriptor = hds;
3095 /* Now, if the descriptor is undefined, import the descriptor
3096 rather than the symbol we were told to import. FIXME: Is
3097 this correct in all cases? */
3098 if (hds->root.type == bfd_link_hash_undefined)
3099 h = hds;
3102 h->flags |= (XCOFF_IMPORT | syscall_flag);
3104 if (val != (bfd_vma) -1)
3106 if (h->root.type == bfd_link_hash_defined
3107 && (! bfd_is_abs_section (h->root.u.def.section)
3108 || h->root.u.def.value != val))
3110 if (! ((*info->callbacks->multiple_definition)
3111 (info, &h->root, output_bfd, bfd_abs_section_ptr, val)))
3112 return FALSE;
3115 h->root.type = bfd_link_hash_defined;
3116 h->root.u.def.section = bfd_abs_section_ptr;
3117 h->root.u.def.value = val;
3118 h->smclas = XMC_XO;
3121 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3122 return FALSE;
3124 return TRUE;
3127 /* Export a symbol. */
3129 bfd_boolean
3130 bfd_xcoff_export_symbol (bfd *output_bfd,
3131 struct bfd_link_info *info,
3132 struct bfd_link_hash_entry *harg)
3134 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3136 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3137 return TRUE;
3139 h->flags |= XCOFF_EXPORT;
3141 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3142 I'm just going to ignore it until somebody explains it. */
3144 /* Make sure we don't garbage collect this symbol. */
3145 if (! xcoff_mark_symbol (info, h))
3146 return FALSE;
3148 /* If this is a function descriptor, make sure we don't garbage
3149 collect the associated function code. We normally don't have to
3150 worry about this, because the descriptor will be attached to a
3151 section with relocs, but if we are creating the descriptor
3152 ourselves those relocs will not be visible to the mark code. */
3153 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3155 if (! xcoff_mark_symbol (info, h->descriptor))
3156 return FALSE;
3159 return TRUE;
3162 /* Count a reloc against a symbol. This is called for relocs
3163 generated by the linker script, typically for global constructors
3164 and destructors. */
3166 bfd_boolean
3167 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3168 struct bfd_link_info *info,
3169 const char *name)
3171 struct xcoff_link_hash_entry *h;
3173 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3174 return TRUE;
3176 h = ((struct xcoff_link_hash_entry *)
3177 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3178 FALSE));
3179 if (h == NULL)
3181 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3182 bfd_set_error (bfd_error_no_symbols);
3183 return FALSE;
3186 h->flags |= XCOFF_REF_REGULAR;
3187 if (xcoff_hash_table (info)->loader_section)
3189 h->flags |= XCOFF_LDREL;
3190 ++xcoff_hash_table (info)->ldrel_count;
3193 /* Mark the symbol to avoid garbage collection. */
3194 if (! xcoff_mark_symbol (info, h))
3195 return FALSE;
3197 return TRUE;
3200 /* This function is called for each symbol to which the linker script
3201 assigns a value. */
3203 bfd_boolean
3204 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3205 struct bfd_link_info *info,
3206 const char *name)
3208 struct xcoff_link_hash_entry *h;
3210 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3211 return TRUE;
3213 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3214 FALSE);
3215 if (h == NULL)
3216 return FALSE;
3218 h->flags |= XCOFF_DEF_REGULAR;
3220 return TRUE;
3223 /* An xcoff_link_hash_traverse callback for which DATA points to an
3224 xcoff_loader_info. Mark all symbols that should be automatically
3225 exported. */
3227 static bfd_boolean
3228 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3230 struct xcoff_loader_info *ldinfo;
3232 ldinfo = (struct xcoff_loader_info *) data;
3233 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3235 if (!xcoff_mark_symbol (ldinfo->info, h))
3236 ldinfo->failed = TRUE;
3238 return TRUE;
3241 /* Add a symbol to the .loader symbols, if necessary. */
3243 /* INPUT_BFD has an external symbol associated with hash table entry H
3244 and csect CSECT. Return true if INPUT_BFD defines H. */
3246 static bfd_boolean
3247 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3248 asection *csect)
3250 switch (h->root.type)
3252 case bfd_link_hash_defined:
3253 case bfd_link_hash_defweak:
3254 /* No input bfd owns absolute symbols. They are written by
3255 xcoff_write_global_symbol instead. */
3256 return (!bfd_is_abs_section (csect)
3257 && h->root.u.def.section == csect);
3259 case bfd_link_hash_common:
3260 return h->root.u.c.p->section->owner == input_bfd;
3262 case bfd_link_hash_undefined:
3263 case bfd_link_hash_undefweak:
3264 /* We can't treat undef.abfd as the owner because that bfd
3265 might be a dynamic object. Allow any bfd to claim it. */
3266 return TRUE;
3268 default:
3269 abort ();
3273 /* See if H should have a loader symbol associated with it. */
3275 static bfd_boolean
3276 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3277 struct xcoff_link_hash_entry *h)
3279 bfd_size_type amt;
3281 /* Warn if this symbol is exported but not defined. */
3282 if ((h->flags & XCOFF_EXPORT) != 0
3283 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3285 (*_bfd_error_handler)
3286 (_("warning: attempt to export undefined symbol `%s'"),
3287 h->root.root.string);
3288 return TRUE;
3291 /* We need to add a symbol to the .loader section if it is mentioned
3292 in a reloc which we are copying to the .loader section and it was
3293 not defined or common, or if it is the entry point, or if it is
3294 being exported. */
3295 if (((h->flags & XCOFF_LDREL) == 0
3296 || h->root.type == bfd_link_hash_defined
3297 || h->root.type == bfd_link_hash_defweak
3298 || h->root.type == bfd_link_hash_common)
3299 && (h->flags & XCOFF_ENTRY) == 0
3300 && (h->flags & XCOFF_EXPORT) == 0)
3301 return TRUE;
3303 /* We need to add this symbol to the .loader symbols. */
3305 BFD_ASSERT (h->ldsym == NULL);
3306 amt = sizeof (struct internal_ldsym);
3307 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3308 if (h->ldsym == NULL)
3310 ldinfo->failed = TRUE;
3311 return FALSE;
3314 if ((h->flags & XCOFF_IMPORT) != 0)
3316 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3317 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3318 h->smclas = XMC_DS;
3319 h->ldsym->l_ifile = h->ldindx;
3322 /* The first 3 symbol table indices are reserved to indicate the
3323 data, text and bss sections. */
3324 h->ldindx = ldinfo->ldsym_count + 3;
3326 ++ldinfo->ldsym_count;
3328 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3329 h->ldsym, h->root.root.string))
3330 return FALSE;
3332 h->flags |= XCOFF_BUILT_LDSYM;
3333 return TRUE;
3336 /* An xcoff_htab_traverse callback that is called for each symbol
3337 once garbage collection is complete. */
3339 static bfd_boolean
3340 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3342 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3344 /* __rtinit, this symbol has special handling. */
3345 if (h->flags & XCOFF_RTINIT)
3346 return TRUE;
3348 /* We don't want to garbage collect symbols which are not defined in
3349 XCOFF files. This is a convenient place to mark them. */
3350 if (xcoff_hash_table (ldinfo->info)->gc
3351 && (h->flags & XCOFF_MARK) == 0
3352 && (h->root.type == bfd_link_hash_defined
3353 || h->root.type == bfd_link_hash_defweak)
3354 && (h->root.u.def.section->owner == NULL
3355 || (h->root.u.def.section->owner->xvec
3356 != ldinfo->info->output_bfd->xvec)))
3357 h->flags |= XCOFF_MARK;
3359 /* Skip discarded symbols. */
3360 if (xcoff_hash_table (ldinfo->info)->gc
3361 && (h->flags & XCOFF_MARK) == 0)
3362 return TRUE;
3364 /* If this is still a common symbol, and it wasn't garbage
3365 collected, we need to actually allocate space for it in the .bss
3366 section. */
3367 if (h->root.type == bfd_link_hash_common
3368 && h->root.u.c.p->section->size == 0)
3370 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3371 h->root.u.c.p->section->size = h->root.u.c.size;
3374 if (xcoff_hash_table (ldinfo->info)->loader_section)
3376 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3377 h->flags |= XCOFF_EXPORT;
3379 if (!xcoff_build_ldsym (ldinfo, h))
3380 return FALSE;
3383 return TRUE;
3386 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3387 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3388 csect information, if any. NAME is the function's name if the name
3389 is stored in the .debug section, otherwise it is null.
3391 Return 1 if we should include an appropriately-adjusted ISYM
3392 in the output file, 0 if we should discard ISYM, or -1 if an
3393 error occured. */
3395 static int
3396 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3397 struct internal_syment *isym,
3398 union internal_auxent *aux,
3399 struct xcoff_link_hash_entry *h,
3400 asection *csect, const char *name)
3402 int smtyp;
3404 /* If we are skipping this csect, we want to strip the symbol too. */
3405 if (csect == NULL)
3406 return 0;
3408 /* Likewise if we garbage-collected the csect. */
3409 if (xcoff_hash_table (info)->gc
3410 && !bfd_is_abs_section (csect)
3411 && !bfd_is_und_section (csect)
3412 && (csect->flags & SEC_MARK) == 0)
3413 return 0;
3415 /* An XCOFF linker always removes C_STAT symbols. */
3416 if (isym->n_sclass == C_STAT)
3417 return 0;
3419 /* We generate the TOC anchor separately. */
3420 if (isym->n_sclass == C_HIDEXT
3421 && aux->x_csect.x_smclas == XMC_TC0)
3422 return 0;
3424 /* If we are stripping all symbols, we want to discard this one. */
3425 if (info->strip == strip_all)
3426 return 0;
3428 /* Discard symbols that are defined elsewhere. */
3429 if (EXTERN_SYM_P (isym->n_sclass))
3431 if ((h->flags & XCOFF_ALLOCATED) != 0)
3432 return 0;
3433 if (!xcoff_final_definition_p (input_bfd, h, csect))
3434 return 0;
3437 /* If we're discarding local symbols, check whether ISYM is local. */
3438 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3439 if (info->discard == discard_all
3440 && !EXTERN_SYM_P (isym->n_sclass)
3441 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3442 return 0;
3444 /* If we're stripping debugging symbols, check whether ISYM is one. */
3445 if (info->strip == strip_debugger
3446 && isym->n_scnum == N_DEBUG)
3447 return 0;
3449 /* If we are stripping symbols based on name, check how ISYM's
3450 name should be handled. */
3451 if (info->strip == strip_some
3452 || info->discard == discard_l)
3454 char buf[SYMNMLEN + 1];
3456 if (name == NULL)
3458 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3459 if (name == NULL)
3460 return -1;
3463 if (info->strip == strip_some
3464 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3465 return 0;
3467 if (info->discard == discard_l
3468 && !EXTERN_SYM_P (isym->n_sclass)
3469 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3470 && bfd_is_local_label_name (input_bfd, name))
3471 return 0;
3474 return 1;
3477 /* Lay out the .loader section, filling in the header and the import paths.
3478 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3480 static bfd_boolean
3481 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3482 const char *libpath)
3484 bfd *output_bfd;
3485 struct xcoff_link_hash_table *htab;
3486 struct internal_ldhdr *ldhdr;
3487 struct xcoff_import_file *fl;
3488 bfd_size_type stoff;
3489 size_t impsize, impcount;
3490 asection *lsec;
3491 char *out;
3493 /* Work out the size of the import file names. Each import file ID
3494 consists of three null terminated strings: the path, the file
3495 name, and the archive member name. The first entry in the list
3496 of names is the path to use to find objects, which the linker has
3497 passed in as the libpath argument. For some reason, the path
3498 entry in the other import file names appears to always be empty. */
3499 output_bfd = ldinfo->output_bfd;
3500 htab = xcoff_hash_table (ldinfo->info);
3501 impsize = strlen (libpath) + 3;
3502 impcount = 1;
3503 for (fl = htab->imports; fl != NULL; fl = fl->next)
3505 ++impcount;
3506 impsize += (strlen (fl->path)
3507 + strlen (fl->file)
3508 + strlen (fl->member)
3509 + 3);
3512 /* Set up the .loader section header. */
3513 ldhdr = &htab->ldhdr;
3514 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3515 ldhdr->l_nsyms = ldinfo->ldsym_count;
3516 ldhdr->l_nreloc = htab->ldrel_count;
3517 ldhdr->l_istlen = impsize;
3518 ldhdr->l_nimpid = impcount;
3519 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3520 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3521 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3522 ldhdr->l_stlen = ldinfo->string_size;
3523 stoff = ldhdr->l_impoff + impsize;
3524 if (ldinfo->string_size == 0)
3525 ldhdr->l_stoff = 0;
3526 else
3527 ldhdr->l_stoff = stoff;
3529 /* 64 bit elements to ldhdr
3530 The swap out routine for 32 bit will ignore them.
3531 Nothing fancy, symbols come after the header and relocs come
3532 after symbols. */
3533 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3534 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3535 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3537 /* We now know the final size of the .loader section. Allocate
3538 space for it. */
3539 lsec = htab->loader_section;
3540 lsec->size = stoff + ldhdr->l_stlen;
3541 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3542 if (lsec->contents == NULL)
3543 return FALSE;
3545 /* Set up the header. */
3546 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3548 /* Set up the import file names. */
3549 out = (char *) lsec->contents + ldhdr->l_impoff;
3550 strcpy (out, libpath);
3551 out += strlen (libpath) + 1;
3552 *out++ = '\0';
3553 *out++ = '\0';
3554 for (fl = htab->imports; fl != NULL; fl = fl->next)
3556 const char *s;
3558 s = fl->path;
3559 while ((*out++ = *s++) != '\0')
3561 s = fl->file;
3562 while ((*out++ = *s++) != '\0')
3564 s = fl->member;
3565 while ((*out++ = *s++) != '\0')
3569 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3571 /* Set up the symbol string table. */
3572 if (ldinfo->string_size > 0)
3574 memcpy (out, ldinfo->strings, ldinfo->string_size);
3575 free (ldinfo->strings);
3576 ldinfo->strings = NULL;
3579 /* We can't set up the symbol table or the relocs yet, because we
3580 don't yet know the final position of the various sections. The
3581 .loader symbols are written out when the corresponding normal
3582 symbols are written out in xcoff_link_input_bfd or
3583 xcoff_write_global_symbol. The .loader relocs are written out
3584 when the corresponding normal relocs are handled in
3585 xcoff_link_input_bfd. */
3587 return TRUE;
3590 /* Build the .loader section. This is called by the XCOFF linker
3591 emulation before_allocation routine. We must set the size of the
3592 .loader section before the linker lays out the output file.
3593 LIBPATH is the library path to search for shared objects; this is
3594 normally built from the -L arguments passed to the linker. ENTRY
3595 is the name of the entry point symbol (the -e linker option).
3596 FILE_ALIGN is the alignment to use for sections within the file
3597 (the -H linker option). MAXSTACK is the maximum stack size (the
3598 -bmaxstack linker option). MAXDATA is the maximum data size (the
3599 -bmaxdata linker option). GC is whether to do garbage collection
3600 (the -bgc linker option). MODTYPE is the module type (the
3601 -bmodtype linker option). TEXTRO is whether the text section must
3602 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3603 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3604 is set by this routine to csects with magic names like _end. */
3606 bfd_boolean
3607 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3608 struct bfd_link_info *info,
3609 const char *libpath,
3610 const char *entry,
3611 unsigned long file_align,
3612 unsigned long maxstack,
3613 unsigned long maxdata,
3614 bfd_boolean gc,
3615 int modtype,
3616 bfd_boolean textro,
3617 unsigned int auto_export_flags,
3618 asection **special_sections,
3619 bfd_boolean rtld)
3621 struct xcoff_loader_info ldinfo;
3622 int i;
3623 asection *sec;
3624 bfd *sub;
3625 struct bfd_strtab_hash *debug_strtab;
3626 bfd_byte *debug_contents = NULL;
3627 bfd_size_type amt;
3629 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3631 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3632 special_sections[i] = NULL;
3633 return TRUE;
3636 ldinfo.failed = FALSE;
3637 ldinfo.output_bfd = output_bfd;
3638 ldinfo.info = info;
3639 ldinfo.auto_export_flags = auto_export_flags;
3640 ldinfo.ldsym_count = 0;
3641 ldinfo.string_size = 0;
3642 ldinfo.strings = NULL;
3643 ldinfo.string_alc = 0;
3645 xcoff_data (output_bfd)->maxstack = maxstack;
3646 xcoff_data (output_bfd)->maxdata = maxdata;
3647 xcoff_data (output_bfd)->modtype = modtype;
3649 xcoff_hash_table (info)->file_align = file_align;
3650 xcoff_hash_table (info)->textro = textro;
3651 xcoff_hash_table (info)->rtld = rtld;
3653 /* __rtinit */
3654 if (xcoff_hash_table (info)->loader_section
3655 && (info->init_function || info->fini_function || rtld))
3657 struct xcoff_link_hash_entry *hsym;
3658 struct internal_ldsym *ldsym;
3660 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3661 "__rtinit", FALSE, FALSE, TRUE);
3662 if (hsym == NULL)
3664 (*_bfd_error_handler)
3665 (_("error: undefined symbol __rtinit"));
3666 return FALSE;
3669 xcoff_mark_symbol (info, hsym);
3670 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3672 /* __rtinit initialized. */
3673 amt = sizeof (* ldsym);
3674 ldsym = bfd_malloc (amt);
3676 ldsym->l_value = 0; /* Will be filled in later. */
3677 ldsym->l_scnum = 2; /* Data section. */
3678 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3679 ldsym->l_smclas = 5; /* .rw. */
3680 ldsym->l_ifile = 0; /* Special system loader symbol. */
3681 ldsym->l_parm = 0; /* NA. */
3683 /* Force __rtinit to be the first symbol in the loader symbol table
3684 See xcoff_build_ldsyms
3686 The first 3 symbol table indices are reserved to indicate the data,
3687 text and bss sections. */
3688 BFD_ASSERT (0 == ldinfo.ldsym_count);
3690 hsym->ldindx = 3;
3691 ldinfo.ldsym_count = 1;
3692 hsym->ldsym = ldsym;
3694 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3695 hsym->ldsym, hsym->root.root.string))
3696 return FALSE;
3698 /* This symbol is written out by xcoff_write_global_symbol
3699 Set stuff up so xcoff_write_global_symbol logic works. */
3700 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3701 hsym->root.type = bfd_link_hash_defined;
3702 hsym->root.u.def.value = 0;
3705 /* Garbage collect unused sections. */
3706 if (info->relocatable || !gc)
3708 gc = FALSE;
3709 xcoff_hash_table (info)->gc = FALSE;
3711 /* We still need to call xcoff_mark, in order to set ldrel_count
3712 correctly. */
3713 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3715 asection *o;
3717 for (o = sub->sections; o != NULL; o = o->next)
3719 /* We shouldn't unconditionaly mark the TOC section.
3720 The output file should only have a TOC if either
3721 (a) one of the input files did or (b) we end up
3722 creating TOC references as part of the link process. */
3723 if (o != xcoff_hash_table (info)->toc_section
3724 && (o->flags & SEC_MARK) == 0)
3726 if (! xcoff_mark (info, o))
3727 goto error_return;
3732 else
3734 if (entry != NULL
3735 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3736 goto error_return;
3737 if (info->init_function != NULL
3738 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3739 goto error_return;
3740 if (info->fini_function != NULL
3741 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3742 goto error_return;
3743 if (auto_export_flags != 0)
3745 xcoff_link_hash_traverse (xcoff_hash_table (info),
3746 xcoff_mark_auto_exports, &ldinfo);
3747 if (ldinfo.failed)
3748 goto error_return;
3750 xcoff_sweep (info);
3751 xcoff_hash_table (info)->gc = TRUE;
3754 /* Return special sections to the caller. */
3755 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3757 sec = xcoff_hash_table (info)->special_sections[i];
3759 if (sec != NULL
3760 && gc
3761 && (sec->flags & SEC_MARK) == 0)
3762 sec = NULL;
3764 special_sections[i] = sec;
3767 if (info->input_bfds == NULL)
3768 /* I'm not sure what to do in this bizarre case. */
3769 return TRUE;
3771 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3772 (void *) &ldinfo);
3773 if (ldinfo.failed)
3774 goto error_return;
3776 if (xcoff_hash_table (info)->loader_section
3777 && !xcoff_build_loader_section (&ldinfo, libpath))
3778 goto error_return;
3780 /* Allocate space for the magic sections. */
3781 sec = xcoff_hash_table (info)->linkage_section;
3782 if (sec->size > 0)
3784 sec->contents = bfd_zalloc (output_bfd, sec->size);
3785 if (sec->contents == NULL)
3786 goto error_return;
3788 sec = xcoff_hash_table (info)->toc_section;
3789 if (sec->size > 0)
3791 sec->contents = bfd_zalloc (output_bfd, sec->size);
3792 if (sec->contents == NULL)
3793 goto error_return;
3795 sec = xcoff_hash_table (info)->descriptor_section;
3796 if (sec->size > 0)
3798 sec->contents = bfd_zalloc (output_bfd, sec->size);
3799 if (sec->contents == NULL)
3800 goto error_return;
3803 /* Now that we've done garbage collection, decide which symbols to keep,
3804 and figure out the contents of the .debug section. */
3805 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3807 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3809 asection *subdeb;
3810 bfd_size_type symcount;
3811 long *debug_index;
3812 asection **csectpp;
3813 unsigned int *lineno_counts;
3814 struct xcoff_link_hash_entry **sym_hash;
3815 bfd_byte *esym, *esymend;
3816 bfd_size_type symesz;
3818 if (sub->xvec != info->output_bfd->xvec)
3819 continue;
3821 if ((sub->flags & DYNAMIC) != 0
3822 && !info->static_link)
3823 continue;
3825 if (! _bfd_coff_get_external_symbols (sub))
3826 goto error_return;
3828 symcount = obj_raw_syment_count (sub);
3829 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3830 if (debug_index == NULL)
3831 goto error_return;
3832 xcoff_data (sub)->debug_indices = debug_index;
3834 if (info->strip == strip_all
3835 || info->strip == strip_debugger
3836 || info->discard == discard_all)
3837 /* We're stripping all debugging information, so there's no need
3838 to read SUB's .debug section. */
3839 subdeb = NULL;
3840 else
3842 /* Grab the contents of SUB's .debug section, if any. */
3843 subdeb = bfd_get_section_by_name (sub, ".debug");
3844 if (subdeb != NULL && subdeb->size > 0)
3846 /* We use malloc and copy the names into the debug
3847 stringtab, rather than bfd_alloc, because I expect
3848 that, when linking many files together, many of the
3849 strings will be the same. Storing the strings in the
3850 hash table should save space in this case. */
3851 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3852 goto error_return;
3856 csectpp = xcoff_data (sub)->csects;
3857 lineno_counts = xcoff_data (sub)->lineno_counts;
3858 sym_hash = obj_xcoff_sym_hashes (sub);
3859 symesz = bfd_coff_symesz (sub);
3860 esym = (bfd_byte *) obj_coff_external_syms (sub);
3861 esymend = esym + symcount * symesz;
3863 while (esym < esymend)
3865 struct internal_syment sym;
3866 union internal_auxent aux;
3867 asection *csect;
3868 const char *name;
3869 int keep_p;
3871 bfd_coff_swap_sym_in (sub, esym, &sym);
3873 /* Read in the csect information, if any. */
3874 if (CSECT_SYM_P (sym.n_sclass))
3876 BFD_ASSERT (sym.n_numaux > 0);
3877 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3878 sym.n_type, sym.n_sclass,
3879 sym.n_numaux - 1, sym.n_numaux, &aux);
3882 /* If this symbol's name is stored in the debug section,
3883 get a pointer to it. */
3884 if (debug_contents != NULL
3885 && sym._n._n_n._n_zeroes == 0
3886 && bfd_coff_symname_in_debug (sub, &sym))
3887 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3888 else
3889 name = NULL;
3891 /* Decide whether to copy this symbol to the output file. */
3892 csect = *csectpp;
3893 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3894 *sym_hash, csect, name);
3895 if (keep_p < 0)
3896 return FALSE;
3898 if (!keep_p)
3899 /* Use a debug_index of -2 to record that a symbol should
3900 be stripped. */
3901 *debug_index = -2;
3902 else
3904 /* See whether we should store the symbol name in the
3905 output .debug section. */
3906 if (name != NULL)
3908 bfd_size_type indx;
3910 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3911 if (indx == (bfd_size_type) -1)
3912 goto error_return;
3913 *debug_index = indx;
3915 else
3916 *debug_index = -1;
3917 if (*sym_hash != 0)
3918 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3919 if (*lineno_counts > 0)
3920 csect->output_section->lineno_count += *lineno_counts;
3923 esym += (sym.n_numaux + 1) * symesz;
3924 csectpp += sym.n_numaux + 1;
3925 sym_hash += sym.n_numaux + 1;
3926 lineno_counts += sym.n_numaux + 1;
3927 debug_index += sym.n_numaux + 1;
3930 if (debug_contents)
3932 free (debug_contents);
3933 debug_contents = NULL;
3935 /* Clear the size of subdeb, so that it is not included directly
3936 in the output file. */
3937 subdeb->size = 0;
3940 if (! info->keep_memory)
3942 if (! _bfd_coff_free_symbols (sub))
3943 goto error_return;
3947 if (info->strip != strip_all)
3948 xcoff_hash_table (info)->debug_section->size =
3949 _bfd_stringtab_size (debug_strtab);
3951 return TRUE;
3953 error_return:
3954 if (ldinfo.strings != NULL)
3955 free (ldinfo.strings);
3956 if (debug_contents != NULL)
3957 free (debug_contents);
3958 return FALSE;
3961 bfd_boolean
3962 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3963 const char *init,
3964 const char *fini,
3965 bfd_boolean rtld)
3967 struct bfd_in_memory *bim;
3969 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3970 if (bim == NULL)
3971 return FALSE;
3973 bim->size = 0;
3974 bim->buffer = 0;
3976 abfd->link_next = 0;
3977 abfd->format = bfd_object;
3978 abfd->iostream = (void *) bim;
3979 abfd->flags = BFD_IN_MEMORY;
3980 abfd->iovec = &_bfd_memory_iovec;
3981 abfd->direction = write_direction;
3982 abfd->origin = 0;
3983 abfd->where = 0;
3985 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3986 return FALSE;
3988 /* need to reset to unknown or it will not be read back in correctly */
3989 abfd->format = bfd_unknown;
3990 abfd->direction = read_direction;
3991 abfd->where = 0;
3993 return TRUE;
3996 /* Return the section that defines H. Return null if no section does. */
3998 static asection *
3999 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4001 switch (h->root.type)
4003 case bfd_link_hash_defined:
4004 case bfd_link_hash_defweak:
4005 return h->root.u.def.section;
4007 case bfd_link_hash_common:
4008 return h->root.u.c.p->section;
4010 default:
4011 return NULL;
4015 /* Add a .loader relocation for input relocation IREL. If the loader
4016 relocation should be against an output section, HSEC points to the
4017 input section that IREL is against, otherwise HSEC is null. H is the
4018 symbol that IREL is against, or null if it isn't against a global symbol.
4019 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4021 static bfd_boolean
4022 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4023 asection *output_section, bfd *reference_bfd,
4024 struct internal_reloc *irel, asection *hsec,
4025 struct xcoff_link_hash_entry *h)
4027 struct internal_ldrel ldrel;
4029 ldrel.l_vaddr = irel->r_vaddr;
4030 if (hsec != NULL)
4032 const char *secname;
4034 secname = hsec->output_section->name;
4035 if (strcmp (secname, ".text") == 0)
4036 ldrel.l_symndx = 0;
4037 else if (strcmp (secname, ".data") == 0)
4038 ldrel.l_symndx = 1;
4039 else if (strcmp (secname, ".bss") == 0)
4040 ldrel.l_symndx = 2;
4041 else
4043 (*_bfd_error_handler)
4044 (_("%B: loader reloc in unrecognized section `%s'"),
4045 reference_bfd, secname);
4046 bfd_set_error (bfd_error_nonrepresentable_section);
4047 return FALSE;
4050 else if (h != NULL)
4052 if (h->ldindx < 0)
4054 (*_bfd_error_handler)
4055 (_("%B: `%s' in loader reloc but not loader sym"),
4056 reference_bfd, h->root.root.string);
4057 bfd_set_error (bfd_error_bad_value);
4058 return FALSE;
4060 ldrel.l_symndx = h->ldindx;
4062 else
4063 ldrel.l_symndx = -(bfd_size_type) 1;
4065 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4066 ldrel.l_rsecnm = output_section->target_index;
4067 if (xcoff_hash_table (flinfo->info)->textro
4068 && strcmp (output_section->name, ".text") == 0)
4070 (*_bfd_error_handler)
4071 (_("%B: loader reloc in read-only section %A"),
4072 reference_bfd, output_section);
4073 bfd_set_error (bfd_error_invalid_operation);
4074 return FALSE;
4076 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4077 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4078 return TRUE;
4081 /* Link an input file into the linker output file. This function
4082 handles all the sections and relocations of the input file at once. */
4084 static bfd_boolean
4085 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4086 bfd *input_bfd)
4088 bfd *output_bfd;
4089 const char *strings;
4090 bfd_size_type syment_base;
4091 unsigned int n_tmask;
4092 unsigned int n_btshft;
4093 bfd_boolean copy, hash;
4094 bfd_size_type isymesz;
4095 bfd_size_type osymesz;
4096 bfd_size_type linesz;
4097 bfd_byte *esym;
4098 bfd_byte *esym_end;
4099 struct xcoff_link_hash_entry **sym_hash;
4100 struct internal_syment *isymp;
4101 asection **csectpp;
4102 unsigned int *lineno_counts;
4103 long *debug_index;
4104 long *indexp;
4105 unsigned long output_index;
4106 bfd_byte *outsym;
4107 unsigned int incls;
4108 asection *oline;
4109 bfd_boolean keep_syms;
4110 asection *o;
4112 /* We can just skip DYNAMIC files, unless this is a static link. */
4113 if ((input_bfd->flags & DYNAMIC) != 0
4114 && ! flinfo->info->static_link)
4115 return TRUE;
4117 /* Move all the symbols to the output file. */
4118 output_bfd = flinfo->output_bfd;
4119 strings = NULL;
4120 syment_base = obj_raw_syment_count (output_bfd);
4121 isymesz = bfd_coff_symesz (input_bfd);
4122 osymesz = bfd_coff_symesz (output_bfd);
4123 linesz = bfd_coff_linesz (input_bfd);
4124 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4126 n_tmask = coff_data (input_bfd)->local_n_tmask;
4127 n_btshft = coff_data (input_bfd)->local_n_btshft;
4129 /* Define macros so that ISFCN, et. al., macros work correctly. */
4130 #define N_TMASK n_tmask
4131 #define N_BTSHFT n_btshft
4133 copy = FALSE;
4134 if (! flinfo->info->keep_memory)
4135 copy = TRUE;
4136 hash = TRUE;
4137 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4138 hash = FALSE;
4140 if (! _bfd_coff_get_external_symbols (input_bfd))
4141 return FALSE;
4143 /* Make one pass over the symbols and assign indices to symbols that
4144 we have decided to keep. Also use create .loader symbol information
4145 and update information in hash table entries. */
4146 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4147 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4148 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4149 csectpp = xcoff_data (input_bfd)->csects;
4150 debug_index = xcoff_data (input_bfd)->debug_indices;
4151 isymp = flinfo->internal_syms;
4152 indexp = flinfo->sym_indices;
4153 output_index = syment_base;
4154 while (esym < esym_end)
4156 union internal_auxent aux;
4157 int smtyp = 0;
4158 int add;
4160 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4162 /* Read in the csect information, if any. */
4163 if (CSECT_SYM_P (isymp->n_sclass))
4165 BFD_ASSERT (isymp->n_numaux > 0);
4166 bfd_coff_swap_aux_in (input_bfd,
4167 (void *) (esym + isymesz * isymp->n_numaux),
4168 isymp->n_type, isymp->n_sclass,
4169 isymp->n_numaux - 1, isymp->n_numaux,
4170 (void *) &aux);
4172 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4175 /* If this symbol is in the .loader section, swap out the
4176 .loader symbol information. If this is an external symbol
4177 reference to a defined symbol, though, then wait until we get
4178 to the definition. */
4179 if (EXTERN_SYM_P (isymp->n_sclass)
4180 && *sym_hash != NULL
4181 && (*sym_hash)->ldsym != NULL
4182 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4184 struct xcoff_link_hash_entry *h;
4185 struct internal_ldsym *ldsym;
4187 h = *sym_hash;
4188 ldsym = h->ldsym;
4189 if (isymp->n_scnum > 0)
4191 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4192 ldsym->l_value = (isymp->n_value
4193 + (*csectpp)->output_section->vma
4194 + (*csectpp)->output_offset
4195 - (*csectpp)->vma);
4197 else
4199 ldsym->l_scnum = isymp->n_scnum;
4200 ldsym->l_value = isymp->n_value;
4203 ldsym->l_smtype = smtyp;
4204 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4205 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4206 || (h->flags & XCOFF_IMPORT) != 0)
4207 ldsym->l_smtype |= L_IMPORT;
4208 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4209 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4210 || (h->flags & XCOFF_EXPORT) != 0)
4211 ldsym->l_smtype |= L_EXPORT;
4212 if ((h->flags & XCOFF_ENTRY) != 0)
4213 ldsym->l_smtype |= L_ENTRY;
4214 if (isymp->n_sclass == C_AIX_WEAKEXT)
4215 ldsym->l_smtype |= L_WEAK;
4217 ldsym->l_smclas = aux.x_csect.x_smclas;
4219 if (ldsym->l_ifile == (bfd_size_type) -1)
4220 ldsym->l_ifile = 0;
4221 else if (ldsym->l_ifile == 0)
4223 if ((ldsym->l_smtype & L_IMPORT) == 0)
4224 ldsym->l_ifile = 0;
4225 else
4227 bfd *impbfd;
4229 if (h->root.type == bfd_link_hash_defined
4230 || h->root.type == bfd_link_hash_defweak)
4231 impbfd = h->root.u.def.section->owner;
4232 else if (h->root.type == bfd_link_hash_undefined
4233 || h->root.type == bfd_link_hash_undefweak)
4234 impbfd = h->root.u.undef.abfd;
4235 else
4236 impbfd = NULL;
4238 if (impbfd == NULL)
4239 ldsym->l_ifile = 0;
4240 else
4242 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4243 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4248 ldsym->l_parm = 0;
4250 BFD_ASSERT (h->ldindx >= 0);
4251 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4252 (flinfo->ldsym
4253 + ((h->ldindx - 3)
4254 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4255 h->ldsym = NULL;
4257 /* Fill in snentry now that we know the target_index. */
4258 if ((h->flags & XCOFF_ENTRY) != 0
4259 && (h->root.type == bfd_link_hash_defined
4260 || h->root.type == bfd_link_hash_defweak))
4262 xcoff_data (output_bfd)->snentry =
4263 h->root.u.def.section->output_section->target_index;
4267 add = 1 + isymp->n_numaux;
4269 if (*debug_index == -2)
4270 /* We've decided to strip this symbol. */
4271 *indexp = -1;
4272 else
4274 /* Assign the next unused index to this symbol. */
4275 *indexp = output_index;
4277 if (EXTERN_SYM_P (isymp->n_sclass))
4279 BFD_ASSERT (*sym_hash != NULL);
4280 (*sym_hash)->indx = output_index;
4283 /* If this is a symbol in the TOC which we may have merged
4284 (class XMC_TC), remember the symbol index of the TOC
4285 symbol. */
4286 if (isymp->n_sclass == C_HIDEXT
4287 && aux.x_csect.x_smclas == XMC_TC
4288 && *sym_hash != NULL)
4290 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4291 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4292 (*sym_hash)->u.toc_indx = output_index;
4295 output_index += add;
4298 esym += add * isymesz;
4299 isymp += add;
4300 csectpp += add;
4301 sym_hash += add;
4302 debug_index += add;
4303 ++indexp;
4304 for (--add; add > 0; --add)
4305 *indexp++ = -1;
4308 /* Now write out the symbols that we decided to keep. */
4310 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4311 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4312 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4313 isymp = flinfo->internal_syms;
4314 indexp = flinfo->sym_indices;
4315 csectpp = xcoff_data (input_bfd)->csects;
4316 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4317 debug_index = xcoff_data (input_bfd)->debug_indices;
4318 outsym = flinfo->outsyms;
4319 incls = 0;
4320 oline = NULL;
4321 while (esym < esym_end)
4323 int add;
4325 add = 1 + isymp->n_numaux;
4327 if (*indexp < 0)
4328 esym += add * isymesz;
4329 else
4331 struct internal_syment isym;
4332 int i;
4334 /* Adjust the symbol in order to output it. */
4335 isym = *isymp;
4336 if (isym._n._n_n._n_zeroes == 0
4337 && isym._n._n_n._n_offset != 0)
4339 /* This symbol has a long name. Enter it in the string
4340 table we are building. If *debug_index != -1, the
4341 name has already been entered in the .debug section. */
4342 if (*debug_index >= 0)
4343 isym._n._n_n._n_offset = *debug_index;
4344 else
4346 const char *name;
4347 bfd_size_type indx;
4349 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4351 if (name == NULL)
4352 return FALSE;
4353 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4354 if (indx == (bfd_size_type) -1)
4355 return FALSE;
4356 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4360 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4361 multiple definition problems when linking a shared object
4362 statically. (The native linker doesn't enter __rtinit into
4363 the normal table at all, but having a local symbol can make
4364 the objdump output easier to read.) */
4365 if (isym.n_sclass == C_EXT
4366 && *sym_hash
4367 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4368 isym.n_sclass = C_HIDEXT;
4370 /* The value of a C_FILE symbol is the symbol index of the
4371 next C_FILE symbol. The value of the last C_FILE symbol
4372 is -1. We try to get this right, below, just before we
4373 write the symbols out, but in the general case we may
4374 have to write the symbol out twice. */
4375 if (isym.n_sclass == C_FILE)
4377 if (flinfo->last_file_index != -1
4378 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4380 /* We must correct the value of the last C_FILE entry. */
4381 flinfo->last_file.n_value = *indexp;
4382 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4384 /* The last C_FILE symbol is in this input file. */
4385 bfd_coff_swap_sym_out (output_bfd,
4386 (void *) &flinfo->last_file,
4387 (void *) (flinfo->outsyms
4388 + ((flinfo->last_file_index
4389 - syment_base)
4390 * osymesz)));
4392 else
4394 /* We have already written out the last C_FILE
4395 symbol. We need to write it out again. We
4396 borrow *outsym temporarily. */
4397 file_ptr pos;
4399 bfd_coff_swap_sym_out (output_bfd,
4400 (void *) &flinfo->last_file,
4401 (void *) outsym);
4403 pos = obj_sym_filepos (output_bfd);
4404 pos += flinfo->last_file_index * osymesz;
4405 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4406 || (bfd_bwrite (outsym, osymesz, output_bfd)
4407 != osymesz))
4408 return FALSE;
4412 flinfo->last_file_index = *indexp;
4413 flinfo->last_file = isym;
4416 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4417 into the line numbers. We update the symbol values when
4418 we handle the line numbers. */
4419 if (isym.n_sclass == C_BINCL
4420 || isym.n_sclass == C_EINCL)
4422 isym.n_value = flinfo->line_filepos;
4423 ++incls;
4425 /* The value of a C_BSTAT symbol is the symbol table
4426 index of the containing csect. */
4427 else if (isym.n_sclass == C_BSTAT)
4429 bfd_vma indx;
4431 indx = isym.n_value;
4432 if (indx < obj_raw_syment_count (input_bfd))
4434 long symindx;
4436 symindx = flinfo->sym_indices[indx];
4437 if (symindx < 0)
4438 isym.n_value = 0;
4439 else
4440 isym.n_value = symindx;
4443 else if (isym.n_sclass != C_ESTAT
4444 && isym.n_sclass != C_DECL
4445 && isym.n_scnum > 0)
4447 isym.n_scnum = (*csectpp)->output_section->target_index;
4448 isym.n_value += ((*csectpp)->output_section->vma
4449 + (*csectpp)->output_offset
4450 - (*csectpp)->vma);
4453 /* Output the symbol. */
4454 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4456 esym += isymesz;
4457 outsym += osymesz;
4459 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4461 union internal_auxent aux;
4463 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4464 isymp->n_sclass, i, isymp->n_numaux,
4465 (void *) &aux);
4467 if (isymp->n_sclass == C_FILE)
4469 /* This is the file name (or some comment put in by
4470 the compiler). If it is long, we must put it in
4471 the string table. */
4472 if (aux.x_file.x_n.x_zeroes == 0
4473 && aux.x_file.x_n.x_offset != 0)
4475 const char *filename;
4476 bfd_size_type indx;
4478 BFD_ASSERT (aux.x_file.x_n.x_offset
4479 >= STRING_SIZE_SIZE);
4480 if (strings == NULL)
4482 strings = _bfd_coff_read_string_table (input_bfd);
4483 if (strings == NULL)
4484 return FALSE;
4486 filename = strings + aux.x_file.x_n.x_offset;
4487 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4488 hash, copy);
4489 if (indx == (bfd_size_type) -1)
4490 return FALSE;
4491 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4494 else if (CSECT_SYM_P (isymp->n_sclass)
4495 && i + 1 == isymp->n_numaux)
4498 /* We don't support type checking. I don't know if
4499 anybody does. */
4500 aux.x_csect.x_parmhash = 0;
4501 /* I don't think anybody uses these fields, but we'd
4502 better clobber them just in case. */
4503 aux.x_csect.x_stab = 0;
4504 aux.x_csect.x_snstab = 0;
4506 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4508 unsigned long indx;
4510 indx = aux.x_csect.x_scnlen.l;
4511 if (indx < obj_raw_syment_count (input_bfd))
4513 long symindx;
4515 symindx = flinfo->sym_indices[indx];
4516 if (symindx < 0)
4518 aux.x_csect.x_scnlen.l = 0;
4520 else
4522 aux.x_csect.x_scnlen.l = symindx;
4527 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4529 unsigned long indx;
4531 if (ISFCN (isymp->n_type)
4532 || ISTAG (isymp->n_sclass)
4533 || isymp->n_sclass == C_BLOCK
4534 || isymp->n_sclass == C_FCN)
4536 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4537 if (indx > 0
4538 && indx < obj_raw_syment_count (input_bfd))
4540 /* We look forward through the symbol for
4541 the index of the next symbol we are going
4542 to include. I don't know if this is
4543 entirely right. */
4544 while (flinfo->sym_indices[indx] < 0
4545 && indx < obj_raw_syment_count (input_bfd))
4546 ++indx;
4547 if (indx >= obj_raw_syment_count (input_bfd))
4548 indx = output_index;
4549 else
4550 indx = flinfo->sym_indices[indx];
4551 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4556 indx = aux.x_sym.x_tagndx.l;
4557 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4559 long symindx;
4561 symindx = flinfo->sym_indices[indx];
4562 if (symindx < 0)
4563 aux.x_sym.x_tagndx.l = 0;
4564 else
4565 aux.x_sym.x_tagndx.l = symindx;
4570 /* Copy over the line numbers, unless we are stripping
4571 them. We do this on a symbol by symbol basis in
4572 order to more easily handle garbage collection. */
4573 if (CSECT_SYM_P (isymp->n_sclass)
4574 && i == 0
4575 && isymp->n_numaux > 1
4576 && ISFCN (isymp->n_type)
4577 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4579 if (*lineno_counts == 0)
4580 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4581 else
4583 asection *enclosing;
4584 unsigned int enc_count;
4585 bfd_signed_vma linoff;
4586 struct internal_lineno lin;
4587 bfd_byte *linp;
4588 bfd_byte *linpend;
4589 bfd_vma offset;
4590 file_ptr pos;
4591 bfd_size_type amt;
4593 /* Read in the enclosing section's line-number
4594 information, if we haven't already. */
4595 o = *csectpp;
4596 enclosing = xcoff_section_data (abfd, o)->enclosing;
4597 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4598 if (oline != enclosing)
4600 pos = enclosing->line_filepos;
4601 amt = linesz * enc_count;
4602 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4603 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4604 != amt))
4605 return FALSE;
4606 oline = enclosing;
4609 /* Copy across the first entry, adjusting its
4610 symbol index. */
4611 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4612 - enclosing->line_filepos);
4613 linp = flinfo->linenos + linoff;
4614 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4615 lin.l_addr.l_symndx = *indexp;
4616 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4618 /* Copy the other entries, adjusting their addresses. */
4619 linpend = linp + *lineno_counts * linesz;
4620 offset = (o->output_section->vma
4621 + o->output_offset
4622 - o->vma);
4623 for (linp += linesz; linp < linpend; linp += linesz)
4625 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4626 lin.l_addr.l_paddr += offset;
4627 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4630 /* Write out the entries we've just processed. */
4631 pos = (o->output_section->line_filepos
4632 + o->output_section->lineno_count * linesz);
4633 amt = linesz * *lineno_counts;
4634 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4635 || bfd_bwrite (flinfo->linenos + linoff,
4636 amt, output_bfd) != amt)
4637 return FALSE;
4638 o->output_section->lineno_count += *lineno_counts;
4640 /* Record the offset of the symbol's line numbers
4641 in the output file. */
4642 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4644 if (incls > 0)
4646 struct internal_syment *iisp, *iispend;
4647 long *iindp;
4648 bfd_byte *oos;
4649 bfd_vma range_start, range_end;
4650 int iiadd;
4652 /* Update any C_BINCL or C_EINCL symbols
4653 that refer to a line number in the
4654 range we just output. */
4655 iisp = flinfo->internal_syms;
4656 iispend = iisp + obj_raw_syment_count (input_bfd);
4657 iindp = flinfo->sym_indices;
4658 oos = flinfo->outsyms;
4659 range_start = enclosing->line_filepos + linoff;
4660 range_end = range_start + *lineno_counts * linesz;
4661 while (iisp < iispend)
4663 if (*iindp >= 0
4664 && (iisp->n_sclass == C_BINCL
4665 || iisp->n_sclass == C_EINCL)
4666 && iisp->n_value >= range_start
4667 && iisp->n_value < range_end)
4669 struct internal_syment iis;
4671 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4672 iis.n_value = (iisp->n_value
4673 - range_start
4674 + pos);
4675 bfd_coff_swap_sym_out (output_bfd,
4676 &iis, oos);
4677 --incls;
4680 iiadd = 1 + iisp->n_numaux;
4681 if (*iindp >= 0)
4682 oos += iiadd * osymesz;
4683 iisp += iiadd;
4684 iindp += iiadd;
4690 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4691 isymp->n_sclass, i, isymp->n_numaux,
4692 (void *) outsym);
4693 outsym += osymesz;
4694 esym += isymesz;
4698 sym_hash += add;
4699 indexp += add;
4700 isymp += add;
4701 csectpp += add;
4702 lineno_counts += add;
4703 debug_index += add;
4706 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4707 symbol will be the first symbol in the next input file. In the
4708 normal case, this will save us from writing out the C_FILE symbol
4709 again. */
4710 if (flinfo->last_file_index != -1
4711 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4713 flinfo->last_file.n_value = output_index;
4714 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4715 (void *) (flinfo->outsyms
4716 + ((flinfo->last_file_index - syment_base)
4717 * osymesz)));
4720 /* Write the modified symbols to the output file. */
4721 if (outsym > flinfo->outsyms)
4723 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4724 bfd_size_type amt = outsym - flinfo->outsyms;
4725 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4726 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4727 return FALSE;
4729 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4730 + (outsym - flinfo->outsyms) / osymesz)
4731 == output_index);
4733 obj_raw_syment_count (output_bfd) = output_index;
4736 /* Don't let the linker relocation routines discard the symbols. */
4737 keep_syms = obj_coff_keep_syms (input_bfd);
4738 obj_coff_keep_syms (input_bfd) = TRUE;
4740 /* Relocate the contents of each section. */
4741 for (o = input_bfd->sections; o != NULL; o = o->next)
4743 bfd_byte *contents;
4745 if (! o->linker_mark)
4746 /* This section was omitted from the link. */
4747 continue;
4749 if ((o->flags & SEC_HAS_CONTENTS) == 0
4750 || o->size == 0
4751 || (o->flags & SEC_IN_MEMORY) != 0)
4752 continue;
4754 /* We have set filepos correctly for the sections we created to
4755 represent csects, so bfd_get_section_contents should work. */
4756 if (coff_section_data (input_bfd, o) != NULL
4757 && coff_section_data (input_bfd, o)->contents != NULL)
4758 contents = coff_section_data (input_bfd, o)->contents;
4759 else
4761 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4762 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4763 return FALSE;
4764 contents = flinfo->contents;
4767 if ((o->flags & SEC_RELOC) != 0)
4769 int target_index;
4770 struct internal_reloc *internal_relocs;
4771 struct internal_reloc *irel;
4772 bfd_vma offset;
4773 struct internal_reloc *irelend;
4774 struct xcoff_link_hash_entry **rel_hash;
4775 long r_symndx;
4777 /* Read in the relocs. */
4778 target_index = o->output_section->target_index;
4779 internal_relocs = (xcoff_read_internal_relocs
4780 (input_bfd, o, FALSE, flinfo->external_relocs,
4781 TRUE,
4782 (flinfo->section_info[target_index].relocs
4783 + o->output_section->reloc_count)));
4784 if (internal_relocs == NULL)
4785 return FALSE;
4787 /* Call processor specific code to relocate the section
4788 contents. */
4789 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4790 input_bfd, o,
4791 contents,
4792 internal_relocs,
4793 flinfo->internal_syms,
4794 xcoff_data (input_bfd)->csects))
4795 return FALSE;
4797 offset = o->output_section->vma + o->output_offset - o->vma;
4798 irel = internal_relocs;
4799 irelend = irel + o->reloc_count;
4800 rel_hash = (flinfo->section_info[target_index].rel_hashes
4801 + o->output_section->reloc_count);
4802 for (; irel < irelend; irel++, rel_hash++)
4804 struct xcoff_link_hash_entry *h = NULL;
4806 *rel_hash = NULL;
4808 /* Adjust the reloc address and symbol index. */
4810 irel->r_vaddr += offset;
4812 r_symndx = irel->r_symndx;
4814 if (r_symndx == -1)
4815 h = NULL;
4816 else
4817 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4819 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4821 if (h != NULL
4822 && h->smclas != XMC_TD
4823 && (irel->r_type == R_TOC
4824 || irel->r_type == R_GL
4825 || irel->r_type == R_TCL
4826 || irel->r_type == R_TRL
4827 || irel->r_type == R_TRLA))
4829 /* This is a TOC relative reloc with a symbol
4830 attached. The symbol should be the one which
4831 this reloc is for. We want to make this
4832 reloc against the TOC address of the symbol,
4833 not the symbol itself. */
4834 BFD_ASSERT (h->toc_section != NULL);
4835 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4836 if (h->u.toc_indx != -1)
4837 irel->r_symndx = h->u.toc_indx;
4838 else
4840 struct xcoff_toc_rel_hash *n;
4841 struct xcoff_link_section_info *si;
4842 bfd_size_type amt;
4844 amt = sizeof (* n);
4845 n = bfd_alloc (flinfo->output_bfd, amt);
4846 if (n == NULL)
4847 return FALSE;
4848 si = flinfo->section_info + target_index;
4849 n->next = si->toc_rel_hashes;
4850 n->h = h;
4851 n->rel = irel;
4852 si->toc_rel_hashes = n;
4855 else if (h != NULL)
4857 /* This is a global symbol. */
4858 if (h->indx >= 0)
4859 irel->r_symndx = h->indx;
4860 else
4862 /* This symbol is being written at the end
4863 of the file, and we do not yet know the
4864 symbol index. We save the pointer to the
4865 hash table entry in the rel_hash list.
4866 We set the indx field to -2 to indicate
4867 that this symbol must not be stripped. */
4868 *rel_hash = h;
4869 h->indx = -2;
4872 else
4874 long indx;
4876 indx = flinfo->sym_indices[r_symndx];
4878 if (indx == -1)
4880 struct internal_syment *is;
4882 /* Relocations against a TC0 TOC anchor are
4883 automatically transformed to be against
4884 the TOC anchor in the output file. */
4885 is = flinfo->internal_syms + r_symndx;
4886 if (is->n_sclass == C_HIDEXT
4887 && is->n_numaux > 0)
4889 void * auxptr;
4890 union internal_auxent aux;
4892 auxptr = ((void *)
4893 (((bfd_byte *)
4894 obj_coff_external_syms (input_bfd))
4895 + ((r_symndx + is->n_numaux)
4896 * isymesz)));
4897 bfd_coff_swap_aux_in (input_bfd, auxptr,
4898 is->n_type, is->n_sclass,
4899 is->n_numaux - 1,
4900 is->n_numaux,
4901 (void *) &aux);
4902 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4903 && aux.x_csect.x_smclas == XMC_TC0)
4904 indx = flinfo->toc_symindx;
4908 if (indx != -1)
4909 irel->r_symndx = indx;
4910 else
4913 struct internal_syment *is;
4915 const char *name;
4916 char buf[SYMNMLEN + 1];
4918 /* This reloc is against a symbol we are
4919 stripping. It would be possible to handle
4920 this case, but I don't think it's worth it. */
4921 is = flinfo->internal_syms + r_symndx;
4923 if (is->n_sclass != C_DWARF)
4925 name = (_bfd_coff_internal_syment_name
4926 (input_bfd, is, buf));
4928 if (name == NULL)
4929 return FALSE;
4931 if (!(*flinfo->info->callbacks->unattached_reloc)
4932 (flinfo->info, name, input_bfd, o,
4933 irel->r_vaddr))
4934 return FALSE;
4940 if ((o->flags & SEC_DEBUGGING) == 0
4941 && xcoff_need_ldrel_p (flinfo->info, irel, h))
4943 asection *sec;
4945 if (r_symndx == -1)
4946 sec = NULL;
4947 else if (h == NULL)
4948 sec = xcoff_data (input_bfd)->csects[r_symndx];
4949 else
4950 sec = xcoff_symbol_section (h);
4951 if (!xcoff_create_ldrel (output_bfd, flinfo,
4952 o->output_section, input_bfd,
4953 irel, sec, h))
4954 return FALSE;
4958 o->output_section->reloc_count += o->reloc_count;
4961 /* Write out the modified section contents. */
4962 if (! bfd_set_section_contents (output_bfd, o->output_section,
4963 contents, (file_ptr) o->output_offset,
4964 o->size))
4965 return FALSE;
4968 obj_coff_keep_syms (input_bfd) = keep_syms;
4970 if (! flinfo->info->keep_memory)
4972 if (! _bfd_coff_free_symbols (input_bfd))
4973 return FALSE;
4976 return TRUE;
4979 #undef N_TMASK
4980 #undef N_BTSHFT
4982 /* Sort relocs by VMA. This is called via qsort. */
4984 static int
4985 xcoff_sort_relocs (const void * p1, const void * p2)
4987 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4988 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4990 if (r1->r_vaddr > r2->r_vaddr)
4991 return 1;
4992 else if (r1->r_vaddr < r2->r_vaddr)
4993 return -1;
4994 else
4995 return 0;
4998 /* Return true if section SEC is a TOC section. */
5000 static inline bfd_boolean
5001 xcoff_toc_section_p (asection *sec)
5003 const char *name;
5005 name = sec->name;
5006 if (name[0] == '.' && name[1] == 't')
5008 if (name[2] == 'c')
5010 if (name[3] == '0' && name[4] == 0)
5011 return TRUE;
5012 if (name[3] == 0)
5013 return TRUE;
5015 if (name[2] == 'd' && name[3] == 0)
5016 return TRUE;
5018 return FALSE;
5021 /* See if the link requires a TOC (it usually does!). If so, find a
5022 good place to put the TOC anchor csect, and write out the associated
5023 symbol. */
5025 static bfd_boolean
5026 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5028 bfd_vma toc_start, toc_end, start, end, best_address;
5029 asection *sec;
5030 bfd *input_bfd;
5031 int section_index;
5032 struct internal_syment irsym;
5033 union internal_auxent iraux;
5034 file_ptr pos;
5035 size_t size;
5037 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5038 index of a csect at the beginning of the TOC. */
5039 toc_start = ~(bfd_vma) 0;
5040 toc_end = 0;
5041 section_index = -1;
5042 for (input_bfd = flinfo->info->input_bfds;
5043 input_bfd != NULL;
5044 input_bfd = input_bfd->link_next)
5045 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5046 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5048 start = sec->output_section->vma + sec->output_offset;
5049 if (toc_start > start)
5051 toc_start = start;
5052 section_index = sec->output_section->target_index;
5055 end = start + sec->size;
5056 if (toc_end < end)
5057 toc_end = end;
5060 /* There's no need for a TC0 symbol if we don't have a TOC. */
5061 if (toc_end < toc_start)
5063 xcoff_data (output_bfd)->toc = toc_start;
5064 return TRUE;
5067 if (toc_end - toc_start < 0x8000)
5068 /* Every TOC csect can be accessed from TOC_START. */
5069 best_address = toc_start;
5070 else
5072 /* Find the lowest TOC csect that is still within range of TOC_END. */
5073 best_address = toc_end;
5074 for (input_bfd = flinfo->info->input_bfds;
5075 input_bfd != NULL;
5076 input_bfd = input_bfd->link_next)
5077 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5078 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5080 start = sec->output_section->vma + sec->output_offset;
5081 if (start < best_address
5082 && start + 0x8000 >= toc_end)
5084 best_address = start;
5085 section_index = sec->output_section->target_index;
5089 /* Make sure that the start of the TOC is also within range. */
5090 if (best_address > toc_start + 0x8000)
5092 (*_bfd_error_handler)
5093 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5094 "when compiling"),
5095 (unsigned long) (toc_end - toc_start));
5096 bfd_set_error (bfd_error_file_too_big);
5097 return FALSE;
5101 /* Record the chosen TOC value. */
5102 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5103 xcoff_data (output_bfd)->toc = best_address;
5104 xcoff_data (output_bfd)->sntoc = section_index;
5106 /* Fill out the TC0 symbol. */
5107 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &irsym, "TOC"))
5108 return FALSE;
5109 irsym.n_value = best_address;
5110 irsym.n_scnum = section_index;
5111 irsym.n_sclass = C_HIDEXT;
5112 irsym.n_type = T_NULL;
5113 irsym.n_numaux = 1;
5114 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5116 /* Fill out the auxillary csect information. */
5117 memset (&iraux, 0, sizeof iraux);
5118 iraux.x_csect.x_smtyp = XTY_SD;
5119 iraux.x_csect.x_smclas = XMC_TC0;
5120 iraux.x_csect.x_scnlen.l = 0;
5121 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5122 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5124 /* Write the contents to the file. */
5125 pos = obj_sym_filepos (output_bfd);
5126 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5127 size = 2 * bfd_coff_symesz (output_bfd);
5128 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5129 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5130 return FALSE;
5131 obj_raw_syment_count (output_bfd) += 2;
5133 return TRUE;
5136 /* Write out a non-XCOFF global symbol. */
5138 static bfd_boolean
5139 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
5141 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
5142 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5143 bfd *output_bfd;
5144 bfd_byte *outsym;
5145 struct internal_syment isym;
5146 union internal_auxent aux;
5147 bfd_boolean result;
5148 file_ptr pos;
5149 bfd_size_type amt;
5151 output_bfd = flinfo->output_bfd;
5152 outsym = flinfo->outsyms;
5154 if (h->root.type == bfd_link_hash_warning)
5156 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5157 if (h->root.type == bfd_link_hash_new)
5158 return TRUE;
5161 /* If this symbol was garbage collected, just skip it. */
5162 if (xcoff_hash_table (flinfo->info)->gc
5163 && (h->flags & XCOFF_MARK) == 0)
5164 return TRUE;
5166 /* If we need a .loader section entry, write it out. */
5167 if (h->ldsym != NULL)
5169 struct internal_ldsym *ldsym;
5170 bfd *impbfd;
5172 ldsym = h->ldsym;
5174 if (h->root.type == bfd_link_hash_undefined
5175 || h->root.type == bfd_link_hash_undefweak)
5178 ldsym->l_value = 0;
5179 ldsym->l_scnum = N_UNDEF;
5180 ldsym->l_smtype = XTY_ER;
5181 impbfd = h->root.u.undef.abfd;
5184 else if (h->root.type == bfd_link_hash_defined
5185 || h->root.type == bfd_link_hash_defweak)
5187 asection *sec;
5189 sec = h->root.u.def.section;
5190 ldsym->l_value = (sec->output_section->vma
5191 + sec->output_offset
5192 + h->root.u.def.value);
5193 ldsym->l_scnum = sec->output_section->target_index;
5194 ldsym->l_smtype = XTY_SD;
5195 impbfd = sec->owner;
5198 else
5199 abort ();
5201 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5202 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5203 || (h->flags & XCOFF_IMPORT) != 0)
5204 /* Clear l_smtype
5205 Import symbols are defined so the check above will make
5206 the l_smtype XTY_SD. But this is not correct, it should
5207 be cleared. */
5208 ldsym->l_smtype |= L_IMPORT;
5210 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5211 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5212 || (h->flags & XCOFF_EXPORT) != 0)
5213 ldsym->l_smtype |= L_EXPORT;
5215 if ((h->flags & XCOFF_ENTRY) != 0)
5216 ldsym->l_smtype |= L_ENTRY;
5218 if ((h->flags & XCOFF_RTINIT) != 0)
5219 ldsym->l_smtype = XTY_SD;
5221 ldsym->l_smclas = h->smclas;
5223 if (ldsym->l_smtype & L_IMPORT)
5225 if ((h->root.type == bfd_link_hash_defined
5226 || h->root.type == bfd_link_hash_defweak)
5227 && (h->root.u.def.value != 0))
5228 ldsym->l_smclas = XMC_XO;
5230 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5231 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5232 ldsym->l_smclas = XMC_SV3264;
5234 else if (h->flags & XCOFF_SYSCALL32)
5235 ldsym->l_smclas = XMC_SV;
5237 else if (h->flags & XCOFF_SYSCALL64)
5238 ldsym->l_smclas = XMC_SV64;
5241 if (ldsym->l_ifile == -(bfd_size_type) 1)
5243 ldsym->l_ifile = 0;
5245 else if (ldsym->l_ifile == 0)
5247 if ((ldsym->l_smtype & L_IMPORT) == 0)
5248 ldsym->l_ifile = 0;
5249 else if (impbfd == NULL)
5250 ldsym->l_ifile = 0;
5251 else
5253 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5254 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5258 ldsym->l_parm = 0;
5260 BFD_ASSERT (h->ldindx >= 0);
5262 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5263 (flinfo->ldsym +
5264 (h->ldindx - 3)
5265 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5266 h->ldsym = NULL;
5269 /* If this symbol needs global linkage code, write it out. */
5270 if (h->root.type == bfd_link_hash_defined
5271 && (h->root.u.def.section
5272 == xcoff_hash_table (flinfo->info)->linkage_section))
5274 bfd_byte *p;
5275 bfd_vma tocoff;
5276 unsigned int i;
5278 p = h->root.u.def.section->contents + h->root.u.def.value;
5280 /* The first instruction in the global linkage code loads a
5281 specific TOC element. */
5282 tocoff = (h->descriptor->toc_section->output_section->vma
5283 + h->descriptor->toc_section->output_offset
5284 - xcoff_data (output_bfd)->toc);
5286 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5287 tocoff += h->descriptor->u.toc_offset;
5289 /* The first instruction in the glink code needs to be
5290 cooked to to hold the correct offset in the toc. The
5291 rest are just output raw. */
5292 bfd_put_32 (output_bfd,
5293 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5295 /* Start with i == 1 to get past the first instruction done above
5296 The /4 is because the glink code is in bytes and we are going
5297 4 at a pop. */
5298 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5299 bfd_put_32 (output_bfd,
5300 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5301 &p[4 * i]);
5304 /* If we created a TOC entry for this symbol, write out the required
5305 relocs. */
5306 if ((h->flags & XCOFF_SET_TOC) != 0)
5308 asection *tocsec;
5309 asection *osec;
5310 int oindx;
5311 struct internal_reloc *irel;
5312 struct internal_syment irsym;
5313 union internal_auxent iraux;
5315 tocsec = h->toc_section;
5316 osec = tocsec->output_section;
5317 oindx = osec->target_index;
5318 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5319 irel->r_vaddr = (osec->vma
5320 + tocsec->output_offset
5321 + h->u.toc_offset);
5323 if (h->indx >= 0)
5324 irel->r_symndx = h->indx;
5325 else
5327 h->indx = -2;
5328 irel->r_symndx = obj_raw_syment_count (output_bfd);
5331 BFD_ASSERT (h->ldindx >= 0);
5333 /* Initialize the aux union here instead of closer to when it is
5334 written out below because the length of the csect depends on
5335 whether the output is 32 or 64 bit. */
5336 memset (&iraux, 0, sizeof iraux);
5337 iraux.x_csect.x_smtyp = XTY_SD;
5338 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5339 iraux.x_csect.x_smclas = XMC_TC;
5341 /* 32 bit uses a 32 bit R_POS to do the relocations
5342 64 bit uses a 64 bit R_POS to do the relocations
5344 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5346 Which one is determined by the backend. */
5347 if (bfd_xcoff_is_xcoff64 (output_bfd))
5349 irel->r_size = 63;
5350 iraux.x_csect.x_scnlen.l = 8;
5352 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5354 irel->r_size = 31;
5355 iraux.x_csect.x_scnlen.l = 4;
5357 else
5358 return FALSE;
5360 irel->r_type = R_POS;
5361 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5362 ++osec->reloc_count;
5364 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5365 output_bfd, irel, NULL, h))
5366 return FALSE;
5368 /* We need to emit a symbol to define a csect which holds
5369 the reloc. */
5370 if (flinfo->info->strip != strip_all)
5372 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab,
5373 &irsym, h->root.root.string);
5374 if (!result)
5375 return FALSE;
5377 irsym.n_value = irel->r_vaddr;
5378 irsym.n_scnum = osec->target_index;
5379 irsym.n_sclass = C_HIDEXT;
5380 irsym.n_type = T_NULL;
5381 irsym.n_numaux = 1;
5383 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5384 outsym += bfd_coff_symesz (output_bfd);
5386 /* Note : iraux is initialized above. */
5387 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5388 0, 1, (void *) outsym);
5389 outsym += bfd_coff_auxesz (output_bfd);
5391 if (h->indx >= 0)
5393 /* We aren't going to write out the symbols below, so we
5394 need to write them out now. */
5395 pos = obj_sym_filepos (output_bfd);
5396 pos += (obj_raw_syment_count (output_bfd)
5397 * bfd_coff_symesz (output_bfd));
5398 amt = outsym - flinfo->outsyms;
5399 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5400 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5401 return FALSE;
5402 obj_raw_syment_count (output_bfd) +=
5403 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5405 outsym = flinfo->outsyms;
5410 /* If this symbol is a specially defined function descriptor, write
5411 it out. The first word is the address of the function code
5412 itself, the second word is the address of the TOC, and the third
5413 word is zero.
5415 32 bit vs 64 bit
5416 The addresses for the 32 bit will take 4 bytes and the addresses
5417 for 64 bit will take 8 bytes. Similar for the relocs. This type
5418 of logic was also done above to create a TOC entry in
5419 xcoff_write_global_symbol. */
5420 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5421 && h->root.type == bfd_link_hash_defined
5422 && (h->root.u.def.section
5423 == xcoff_hash_table (flinfo->info)->descriptor_section))
5425 asection *sec;
5426 asection *osec;
5427 int oindx;
5428 bfd_byte *p;
5429 struct xcoff_link_hash_entry *hentry;
5430 asection *esec;
5431 struct internal_reloc *irel;
5432 asection *tsec;
5433 unsigned int reloc_size, byte_size;
5435 if (bfd_xcoff_is_xcoff64 (output_bfd))
5437 reloc_size = 63;
5438 byte_size = 8;
5440 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5442 reloc_size = 31;
5443 byte_size = 4;
5445 else
5446 return FALSE;
5448 sec = h->root.u.def.section;
5449 osec = sec->output_section;
5450 oindx = osec->target_index;
5451 p = sec->contents + h->root.u.def.value;
5453 hentry = h->descriptor;
5454 BFD_ASSERT (hentry != NULL
5455 && (hentry->root.type == bfd_link_hash_defined
5456 || hentry->root.type == bfd_link_hash_defweak));
5457 esec = hentry->root.u.def.section;
5459 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5460 irel->r_vaddr = (osec->vma
5461 + sec->output_offset
5462 + h->root.u.def.value);
5463 irel->r_symndx = esec->output_section->target_index;
5464 irel->r_type = R_POS;
5465 irel->r_size = reloc_size;
5466 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5467 ++osec->reloc_count;
5469 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5470 output_bfd, irel, esec, NULL))
5471 return FALSE;
5473 /* There are three items to write out,
5474 the address of the code
5475 the address of the toc anchor
5476 the environment pointer.
5477 We are ignoring the environment pointer. So set it to zero. */
5478 if (bfd_xcoff_is_xcoff64 (output_bfd))
5480 bfd_put_64 (output_bfd,
5481 (esec->output_section->vma + esec->output_offset
5482 + hentry->root.u.def.value),
5484 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5485 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5487 else
5489 /* 32 bit backend
5490 This logic was already called above so the error case where
5491 the backend is neither has already been checked. */
5492 bfd_put_32 (output_bfd,
5493 (esec->output_section->vma + esec->output_offset
5494 + hentry->root.u.def.value),
5496 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5497 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5500 tsec = coff_section_from_bfd_index (output_bfd,
5501 xcoff_data (output_bfd)->sntoc);
5503 ++irel;
5504 irel->r_vaddr = (osec->vma
5505 + sec->output_offset
5506 + h->root.u.def.value
5507 + byte_size);
5508 irel->r_symndx = tsec->output_section->target_index;
5509 irel->r_type = R_POS;
5510 irel->r_size = reloc_size;
5511 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5512 ++osec->reloc_count;
5514 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5515 output_bfd, irel, tsec, NULL))
5516 return FALSE;
5519 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5521 BFD_ASSERT (outsym == flinfo->outsyms);
5522 return TRUE;
5525 if (h->indx != -2
5526 && (flinfo->info->strip == strip_all
5527 || (flinfo->info->strip == strip_some
5528 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5529 FALSE, FALSE) == NULL)))
5531 BFD_ASSERT (outsym == flinfo->outsyms);
5532 return TRUE;
5535 if (h->indx != -2
5536 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5538 BFD_ASSERT (outsym == flinfo->outsyms);
5539 return TRUE;
5542 memset (&aux, 0, sizeof aux);
5544 h->indx = obj_raw_syment_count (output_bfd);
5546 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &isym,
5547 h->root.root.string);
5548 if (!result)
5549 return FALSE;
5551 if (h->root.type == bfd_link_hash_undefined
5552 || h->root.type == bfd_link_hash_undefweak)
5554 isym.n_value = 0;
5555 isym.n_scnum = N_UNDEF;
5556 if (h->root.type == bfd_link_hash_undefweak
5557 && C_WEAKEXT == C_AIX_WEAKEXT)
5558 isym.n_sclass = C_WEAKEXT;
5559 else
5560 isym.n_sclass = C_EXT;
5561 aux.x_csect.x_smtyp = XTY_ER;
5563 else if ((h->root.type == bfd_link_hash_defined
5564 || h->root.type == bfd_link_hash_defweak)
5565 && h->smclas == XMC_XO)
5567 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5568 isym.n_value = h->root.u.def.value;
5569 isym.n_scnum = N_UNDEF;
5570 if (h->root.type == bfd_link_hash_undefweak
5571 && C_WEAKEXT == C_AIX_WEAKEXT)
5572 isym.n_sclass = C_WEAKEXT;
5573 else
5574 isym.n_sclass = C_EXT;
5575 aux.x_csect.x_smtyp = XTY_ER;
5577 else if (h->root.type == bfd_link_hash_defined
5578 || h->root.type == bfd_link_hash_defweak)
5580 struct xcoff_link_size_list *l;
5582 isym.n_value = (h->root.u.def.section->output_section->vma
5583 + h->root.u.def.section->output_offset
5584 + h->root.u.def.value);
5585 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5586 isym.n_scnum = N_ABS;
5587 else
5588 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5589 isym.n_sclass = C_HIDEXT;
5590 aux.x_csect.x_smtyp = XTY_SD;
5592 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5594 for (l = xcoff_hash_table (flinfo->info)->size_list;
5595 l != NULL;
5596 l = l->next)
5598 if (l->h == h)
5600 aux.x_csect.x_scnlen.l = l->size;
5601 break;
5606 else if (h->root.type == bfd_link_hash_common)
5608 isym.n_value = (h->root.u.c.p->section->output_section->vma
5609 + h->root.u.c.p->section->output_offset);
5610 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5611 isym.n_sclass = C_EXT;
5612 aux.x_csect.x_smtyp = XTY_CM;
5613 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5615 else
5616 abort ();
5618 isym.n_type = T_NULL;
5619 isym.n_numaux = 1;
5621 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5622 outsym += bfd_coff_symesz (output_bfd);
5624 aux.x_csect.x_smclas = h->smclas;
5625 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5626 (void *) outsym);
5627 outsym += bfd_coff_auxesz (output_bfd);
5629 if ((h->root.type == bfd_link_hash_defined
5630 || h->root.type == bfd_link_hash_defweak)
5631 && h->smclas != XMC_XO)
5633 /* We just output an SD symbol. Now output an LD symbol. */
5634 h->indx += 2;
5636 if (h->root.type == bfd_link_hash_undefweak
5637 && C_WEAKEXT == C_AIX_WEAKEXT)
5638 isym.n_sclass = C_WEAKEXT;
5639 else
5640 isym.n_sclass = C_EXT;
5641 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5642 outsym += bfd_coff_symesz (output_bfd);
5644 aux.x_csect.x_smtyp = XTY_LD;
5645 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5646 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5647 (void *) outsym);
5648 outsym += bfd_coff_auxesz (output_bfd);
5651 pos = obj_sym_filepos (output_bfd);
5652 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5653 amt = outsym - flinfo->outsyms;
5654 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5655 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5656 return FALSE;
5657 obj_raw_syment_count (output_bfd) +=
5658 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5660 return TRUE;
5663 /* Handle a link order which is supposed to generate a reloc. */
5665 static bfd_boolean
5666 xcoff_reloc_link_order (bfd *output_bfd,
5667 struct xcoff_final_link_info *flinfo,
5668 asection *output_section,
5669 struct bfd_link_order *link_order)
5671 reloc_howto_type *howto;
5672 struct xcoff_link_hash_entry *h;
5673 asection *hsec;
5674 bfd_vma hval;
5675 bfd_vma addend;
5676 struct internal_reloc *irel;
5677 struct xcoff_link_hash_entry **rel_hash_ptr;
5679 if (link_order->type == bfd_section_reloc_link_order)
5680 /* We need to somehow locate a symbol in the right section. The
5681 symbol must either have a value of zero, or we must adjust
5682 the addend by the value of the symbol. FIXME: Write this
5683 when we need it. The old linker couldn't handle this anyhow. */
5684 abort ();
5686 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5687 if (howto == NULL)
5689 bfd_set_error (bfd_error_bad_value);
5690 return FALSE;
5693 h = ((struct xcoff_link_hash_entry *)
5694 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5695 link_order->u.reloc.p->u.name,
5696 FALSE, FALSE, TRUE));
5697 if (h == NULL)
5699 if (! ((*flinfo->info->callbacks->unattached_reloc)
5700 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5701 return FALSE;
5702 return TRUE;
5705 hsec = xcoff_symbol_section (h);
5706 if (h->root.type == bfd_link_hash_defined
5707 || h->root.type == bfd_link_hash_defweak)
5708 hval = h->root.u.def.value;
5709 else
5710 hval = 0;
5712 addend = link_order->u.reloc.p->addend;
5713 if (hsec != NULL)
5714 addend += (hsec->output_section->vma
5715 + hsec->output_offset
5716 + hval);
5718 if (addend != 0)
5720 bfd_size_type size;
5721 bfd_byte *buf;
5722 bfd_reloc_status_type rstat;
5723 bfd_boolean ok;
5725 size = bfd_get_reloc_size (howto);
5726 buf = bfd_zmalloc (size);
5727 if (buf == NULL)
5728 return FALSE;
5730 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5731 switch (rstat)
5733 case bfd_reloc_ok:
5734 break;
5735 default:
5736 case bfd_reloc_outofrange:
5737 abort ();
5738 case bfd_reloc_overflow:
5739 if (! ((*flinfo->info->callbacks->reloc_overflow)
5740 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5741 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5743 free (buf);
5744 return FALSE;
5746 break;
5748 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5749 (file_ptr) link_order->offset, size);
5750 free (buf);
5751 if (! ok)
5752 return FALSE;
5755 /* Store the reloc information in the right place. It will get
5756 swapped and written out at the end of the final_link routine. */
5757 irel = (flinfo->section_info[output_section->target_index].relocs
5758 + output_section->reloc_count);
5759 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5760 + output_section->reloc_count);
5762 memset (irel, 0, sizeof (struct internal_reloc));
5763 *rel_hash_ptr = NULL;
5765 irel->r_vaddr = output_section->vma + link_order->offset;
5767 if (h->indx >= 0)
5768 irel->r_symndx = h->indx;
5769 else
5771 /* Set the index to -2 to force this symbol to get written out. */
5772 h->indx = -2;
5773 *rel_hash_ptr = h;
5774 irel->r_symndx = 0;
5777 irel->r_type = howto->type;
5778 irel->r_size = howto->bitsize - 1;
5779 if (howto->complain_on_overflow == complain_overflow_signed)
5780 irel->r_size |= 0x80;
5782 ++output_section->reloc_count;
5784 /* Now output the reloc to the .loader section. */
5785 if (xcoff_hash_table (flinfo->info)->loader_section)
5787 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5788 output_bfd, irel, hsec, h))
5789 return FALSE;
5792 return TRUE;
5795 /* Do the final link step. */
5797 bfd_boolean
5798 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5800 bfd_size_type symesz;
5801 struct xcoff_final_link_info flinfo;
5802 asection *o;
5803 struct bfd_link_order *p;
5804 bfd_size_type max_contents_size;
5805 bfd_size_type max_sym_count;
5806 bfd_size_type max_lineno_count;
5807 bfd_size_type max_reloc_count;
5808 bfd_size_type max_output_reloc_count;
5809 file_ptr rel_filepos;
5810 unsigned int relsz;
5811 file_ptr line_filepos;
5812 unsigned int linesz;
5813 bfd *sub;
5814 bfd_byte *external_relocs = NULL;
5815 char strbuf[STRING_SIZE_SIZE];
5816 file_ptr pos;
5817 bfd_size_type amt;
5819 if (info->shared)
5820 abfd->flags |= DYNAMIC;
5822 symesz = bfd_coff_symesz (abfd);
5824 flinfo.info = info;
5825 flinfo.output_bfd = abfd;
5826 flinfo.strtab = NULL;
5827 flinfo.section_info = NULL;
5828 flinfo.last_file_index = -1;
5829 flinfo.toc_symindx = -1;
5830 flinfo.internal_syms = NULL;
5831 flinfo.sym_indices = NULL;
5832 flinfo.outsyms = NULL;
5833 flinfo.linenos = NULL;
5834 flinfo.contents = NULL;
5835 flinfo.external_relocs = NULL;
5837 if (xcoff_hash_table (info)->loader_section)
5839 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5840 + bfd_xcoff_ldhdrsz (abfd));
5841 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5842 + bfd_xcoff_ldhdrsz (abfd)
5843 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5844 * bfd_xcoff_ldsymsz (abfd)));
5846 else
5848 flinfo.ldsym = NULL;
5849 flinfo.ldrel = NULL;
5852 xcoff_data (abfd)->coff.link_info = info;
5854 flinfo.strtab = _bfd_stringtab_init ();
5855 if (flinfo.strtab == NULL)
5856 goto error_return;
5858 /* Count the relocation entries required for the output file.
5859 (We've already counted the line numbers.) Determine a few
5860 maximum sizes. */
5861 max_contents_size = 0;
5862 max_lineno_count = 0;
5863 max_reloc_count = 0;
5864 for (o = abfd->sections; o != NULL; o = o->next)
5866 o->reloc_count = 0;
5867 for (p = o->map_head.link_order; p != NULL; p = p->next)
5869 if (p->type == bfd_indirect_link_order)
5871 asection *sec;
5873 sec = p->u.indirect.section;
5875 /* Mark all sections which are to be included in the
5876 link. This will normally be every section. We need
5877 to do this so that we can identify any sections which
5878 the linker has decided to not include. */
5879 sec->linker_mark = TRUE;
5881 o->reloc_count += sec->reloc_count;
5883 if ((sec->flags & SEC_IN_MEMORY) == 0)
5885 if (sec->rawsize > max_contents_size)
5886 max_contents_size = sec->rawsize;
5887 if (sec->size > max_contents_size)
5888 max_contents_size = sec->size;
5890 if (coff_section_data (sec->owner, sec) != NULL
5891 && xcoff_section_data (sec->owner, sec) != NULL
5892 && (xcoff_section_data (sec->owner, sec)->lineno_count
5893 > max_lineno_count))
5894 max_lineno_count =
5895 xcoff_section_data (sec->owner, sec)->lineno_count;
5896 if (sec->reloc_count > max_reloc_count)
5897 max_reloc_count = sec->reloc_count;
5899 else if (p->type == bfd_section_reloc_link_order
5900 || p->type == bfd_symbol_reloc_link_order)
5901 ++o->reloc_count;
5905 /* Compute the file positions for all the sections. */
5906 if (abfd->output_has_begun)
5908 if (xcoff_hash_table (info)->file_align != 0)
5909 abort ();
5911 else
5913 bfd_vma file_align;
5915 file_align = xcoff_hash_table (info)->file_align;
5916 if (file_align != 0)
5918 bfd_boolean saw_contents;
5919 int indx;
5920 file_ptr sofar;
5922 /* Insert .pad sections before every section which has
5923 contents and is loaded, if it is preceded by some other
5924 section which has contents and is loaded. */
5925 saw_contents = TRUE;
5926 for (o = abfd->sections; o != NULL; o = o->next)
5928 if (strcmp (o->name, ".pad") == 0)
5929 saw_contents = FALSE;
5930 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5931 && (o->flags & SEC_LOAD) != 0)
5933 if (! saw_contents)
5934 saw_contents = TRUE;
5935 else
5937 asection *n;
5939 /* Create a pad section and place it before the section
5940 that needs padding. This requires unlinking and
5941 relinking the bfd's section list. */
5943 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5944 SEC_HAS_CONTENTS);
5945 n->alignment_power = 0;
5947 bfd_section_list_remove (abfd, n);
5948 bfd_section_list_insert_before (abfd, o, n);
5949 saw_contents = FALSE;
5954 /* Reset the section indices after inserting the new
5955 sections. */
5956 indx = 0;
5957 for (o = abfd->sections; o != NULL; o = o->next)
5959 ++indx;
5960 o->target_index = indx;
5962 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5964 /* Work out appropriate sizes for the .pad sections to force
5965 each section to land on a page boundary. This bit of
5966 code knows what compute_section_file_positions is going
5967 to do. */
5968 sofar = bfd_coff_filhsz (abfd);
5969 sofar += bfd_coff_aoutsz (abfd);
5970 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5971 for (o = abfd->sections; o != NULL; o = o->next)
5972 if ((bfd_xcoff_is_reloc_count_overflow
5973 (abfd, (bfd_vma) o->reloc_count))
5974 || (bfd_xcoff_is_lineno_count_overflow
5975 (abfd, (bfd_vma) o->lineno_count)))
5976 /* 64 does not overflow, need to check if 32 does */
5977 sofar += bfd_coff_scnhsz (abfd);
5979 for (o = abfd->sections; o != NULL; o = o->next)
5981 if (strcmp (o->name, ".pad") == 0)
5983 bfd_vma pageoff;
5985 BFD_ASSERT (o->size == 0);
5986 pageoff = sofar & (file_align - 1);
5987 if (pageoff != 0)
5989 o->size = file_align - pageoff;
5990 sofar += file_align - pageoff;
5991 o->flags |= SEC_HAS_CONTENTS;
5994 else
5996 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5997 sofar += BFD_ALIGN (o->size,
5998 1 << o->alignment_power);
6003 if (! bfd_coff_compute_section_file_positions (abfd))
6004 goto error_return;
6007 /* Allocate space for the pointers we need to keep for the relocs. */
6009 unsigned int i;
6011 /* We use section_count + 1, rather than section_count, because
6012 the target_index fields are 1 based. */
6013 amt = abfd->section_count + 1;
6014 amt *= sizeof (struct xcoff_link_section_info);
6015 flinfo.section_info = bfd_malloc (amt);
6016 if (flinfo.section_info == NULL)
6017 goto error_return;
6018 for (i = 0; i <= abfd->section_count; i++)
6020 flinfo.section_info[i].relocs = NULL;
6021 flinfo.section_info[i].rel_hashes = NULL;
6022 flinfo.section_info[i].toc_rel_hashes = NULL;
6026 /* Set the file positions for the relocs. */
6027 rel_filepos = obj_relocbase (abfd);
6028 relsz = bfd_coff_relsz (abfd);
6029 max_output_reloc_count = 0;
6030 for (o = abfd->sections; o != NULL; o = o->next)
6032 if (o->reloc_count == 0)
6033 o->rel_filepos = 0;
6034 else
6036 /* A stripped file has no relocs. However, we still
6037 allocate the buffers, so that later code doesn't have to
6038 worry about whether we are stripping or not. */
6039 if (info->strip == strip_all)
6040 o->rel_filepos = 0;
6041 else
6043 o->flags |= SEC_RELOC;
6044 o->rel_filepos = rel_filepos;
6045 rel_filepos += o->reloc_count * relsz;
6048 /* We don't know the indices of global symbols until we have
6049 written out all the local symbols. For each section in
6050 the output file, we keep an array of pointers to hash
6051 table entries. Each entry in the array corresponds to a
6052 reloc. When we find a reloc against a global symbol, we
6053 set the corresponding entry in this array so that we can
6054 fix up the symbol index after we have written out all the
6055 local symbols.
6057 Because of this problem, we also keep the relocs in
6058 memory until the end of the link. This wastes memory.
6059 We could backpatch the file later, I suppose, although it
6060 would be slow. */
6061 amt = o->reloc_count;
6062 amt *= sizeof (struct internal_reloc);
6063 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6065 amt = o->reloc_count;
6066 amt *= sizeof (struct xcoff_link_hash_entry *);
6067 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6069 if (flinfo.section_info[o->target_index].relocs == NULL
6070 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6071 goto error_return;
6073 if (o->reloc_count > max_output_reloc_count)
6074 max_output_reloc_count = o->reloc_count;
6078 /* We now know the size of the relocs, so we can determine the file
6079 positions of the line numbers. */
6080 line_filepos = rel_filepos;
6081 flinfo.line_filepos = line_filepos;
6082 linesz = bfd_coff_linesz (abfd);
6083 for (o = abfd->sections; o != NULL; o = o->next)
6085 if (o->lineno_count == 0)
6086 o->line_filepos = 0;
6087 else
6089 o->line_filepos = line_filepos;
6090 line_filepos += o->lineno_count * linesz;
6093 /* Reset the reloc and lineno counts, so that we can use them to
6094 count the number of entries we have output so far. */
6095 o->reloc_count = 0;
6096 o->lineno_count = 0;
6099 obj_sym_filepos (abfd) = line_filepos;
6101 /* Figure out the largest number of symbols in an input BFD. Take
6102 the opportunity to clear the output_has_begun fields of all the
6103 input BFD's. We want at least 6 symbols, since that is the
6104 number which xcoff_write_global_symbol may need. */
6105 max_sym_count = 6;
6106 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6108 bfd_size_type sz;
6110 sub->output_has_begun = FALSE;
6111 sz = obj_raw_syment_count (sub);
6112 if (sz > max_sym_count)
6113 max_sym_count = sz;
6116 /* Allocate some buffers used while linking. */
6117 amt = max_sym_count * sizeof (struct internal_syment);
6118 flinfo.internal_syms = bfd_malloc (amt);
6120 amt = max_sym_count * sizeof (long);
6121 flinfo.sym_indices = bfd_malloc (amt);
6123 amt = (max_sym_count + 1) * symesz;
6124 flinfo.outsyms = bfd_malloc (amt);
6126 amt = max_lineno_count * bfd_coff_linesz (abfd);
6127 flinfo.linenos = bfd_malloc (amt);
6129 amt = max_contents_size;
6130 flinfo.contents = bfd_malloc (amt);
6132 amt = max_reloc_count * relsz;
6133 flinfo.external_relocs = bfd_malloc (amt);
6135 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6136 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6137 || flinfo.outsyms == NULL
6138 || (flinfo.linenos == NULL && max_lineno_count > 0)
6139 || (flinfo.contents == NULL && max_contents_size > 0)
6140 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6141 goto error_return;
6143 obj_raw_syment_count (abfd) = 0;
6145 /* Find a TOC symbol, if we need one. */
6146 if (!xcoff_find_tc0 (abfd, &flinfo))
6147 goto error_return;
6149 /* We now know the position of everything in the file, except that
6150 we don't know the size of the symbol table and therefore we don't
6151 know where the string table starts. We just build the string
6152 table in memory as we go along. We process all the relocations
6153 for a single input file at once. */
6154 for (o = abfd->sections; o != NULL; o = o->next)
6156 for (p = o->map_head.link_order; p != NULL; p = p->next)
6158 if (p->type == bfd_indirect_link_order
6159 && p->u.indirect.section->owner->xvec == abfd->xvec)
6161 sub = p->u.indirect.section->owner;
6162 if (! sub->output_has_begun)
6164 if (! xcoff_link_input_bfd (&flinfo, sub))
6165 goto error_return;
6166 sub->output_has_begun = TRUE;
6169 else if (p->type == bfd_section_reloc_link_order
6170 || p->type == bfd_symbol_reloc_link_order)
6172 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6173 goto error_return;
6175 else
6177 if (! _bfd_default_link_order (abfd, info, o, p))
6178 goto error_return;
6183 /* Free up the buffers used by xcoff_link_input_bfd. */
6184 if (flinfo.internal_syms != NULL)
6186 free (flinfo.internal_syms);
6187 flinfo.internal_syms = NULL;
6189 if (flinfo.sym_indices != NULL)
6191 free (flinfo.sym_indices);
6192 flinfo.sym_indices = NULL;
6194 if (flinfo.linenos != NULL)
6196 free (flinfo.linenos);
6197 flinfo.linenos = NULL;
6199 if (flinfo.contents != NULL)
6201 free (flinfo.contents);
6202 flinfo.contents = NULL;
6204 if (flinfo.external_relocs != NULL)
6206 free (flinfo.external_relocs);
6207 flinfo.external_relocs = NULL;
6210 /* The value of the last C_FILE symbol is supposed to be -1. Write
6211 it out again. */
6212 if (flinfo.last_file_index != -1)
6214 flinfo.last_file.n_value = -(bfd_vma) 1;
6215 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6216 (void *) flinfo.outsyms);
6217 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6218 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6219 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6220 goto error_return;
6223 /* Write out all the global symbols which do not come from XCOFF
6224 input files. */
6225 bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
6227 if (flinfo.outsyms != NULL)
6229 free (flinfo.outsyms);
6230 flinfo.outsyms = NULL;
6233 /* Now that we have written out all the global symbols, we know the
6234 symbol indices to use for relocs against them, and we can finally
6235 write out the relocs. */
6236 amt = max_output_reloc_count * relsz;
6237 external_relocs = bfd_malloc (amt);
6238 if (external_relocs == NULL && max_output_reloc_count != 0)
6239 goto error_return;
6241 for (o = abfd->sections; o != NULL; o = o->next)
6243 struct internal_reloc *irel;
6244 struct internal_reloc *irelend;
6245 struct xcoff_link_hash_entry **rel_hash;
6246 struct xcoff_toc_rel_hash *toc_rel_hash;
6247 bfd_byte *erel;
6248 bfd_size_type rel_size;
6250 /* A stripped file has no relocs. */
6251 if (info->strip == strip_all)
6253 o->reloc_count = 0;
6254 continue;
6257 if (o->reloc_count == 0)
6258 continue;
6260 irel = flinfo.section_info[o->target_index].relocs;
6261 irelend = irel + o->reloc_count;
6262 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6263 for (; irel < irelend; irel++, rel_hash++)
6265 if (*rel_hash != NULL)
6267 if ((*rel_hash)->indx < 0)
6269 if (! ((*info->callbacks->unattached_reloc)
6270 (info, (*rel_hash)->root.root.string,
6271 NULL, o, irel->r_vaddr)))
6272 goto error_return;
6273 (*rel_hash)->indx = 0;
6275 irel->r_symndx = (*rel_hash)->indx;
6279 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6280 toc_rel_hash != NULL;
6281 toc_rel_hash = toc_rel_hash->next)
6283 if (toc_rel_hash->h->u.toc_indx < 0)
6285 if (! ((*info->callbacks->unattached_reloc)
6286 (info, toc_rel_hash->h->root.root.string,
6287 NULL, o, toc_rel_hash->rel->r_vaddr)))
6288 goto error_return;
6289 toc_rel_hash->h->u.toc_indx = 0;
6291 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6294 /* XCOFF requires that the relocs be sorted by address. We tend
6295 to produce them in the order in which their containing csects
6296 appear in the symbol table, which is not necessarily by
6297 address. So we sort them here. There may be a better way to
6298 do this. */
6299 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6300 o->reloc_count, sizeof (struct internal_reloc),
6301 xcoff_sort_relocs);
6303 irel = flinfo.section_info[o->target_index].relocs;
6304 irelend = irel + o->reloc_count;
6305 erel = external_relocs;
6306 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6307 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6309 rel_size = relsz * o->reloc_count;
6310 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6311 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6312 goto error_return;
6315 if (external_relocs != NULL)
6317 free (external_relocs);
6318 external_relocs = NULL;
6321 /* Free up the section information. */
6322 if (flinfo.section_info != NULL)
6324 unsigned int i;
6326 for (i = 0; i < abfd->section_count; i++)
6328 if (flinfo.section_info[i].relocs != NULL)
6329 free (flinfo.section_info[i].relocs);
6330 if (flinfo.section_info[i].rel_hashes != NULL)
6331 free (flinfo.section_info[i].rel_hashes);
6333 free (flinfo.section_info);
6334 flinfo.section_info = NULL;
6337 /* Write out the loader section contents. */
6338 o = xcoff_hash_table (info)->loader_section;
6339 if (o)
6341 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6342 == (xcoff_hash_table (info)->loader_section->contents
6343 + xcoff_hash_table (info)->ldhdr.l_impoff));
6344 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6345 (file_ptr) o->output_offset, o->size))
6346 goto error_return;
6349 /* Write out the magic sections. */
6350 o = xcoff_hash_table (info)->linkage_section;
6351 if (o->size > 0
6352 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6353 (file_ptr) o->output_offset,
6354 o->size))
6355 goto error_return;
6356 o = xcoff_hash_table (info)->toc_section;
6357 if (o->size > 0
6358 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6359 (file_ptr) o->output_offset,
6360 o->size))
6361 goto error_return;
6362 o = xcoff_hash_table (info)->descriptor_section;
6363 if (o->size > 0
6364 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6365 (file_ptr) o->output_offset,
6366 o->size))
6367 goto error_return;
6369 /* Write out the string table. */
6370 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6371 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6372 goto error_return;
6373 H_PUT_32 (abfd,
6374 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6375 strbuf);
6376 amt = STRING_SIZE_SIZE;
6377 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6378 goto error_return;
6379 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6380 goto error_return;
6382 _bfd_stringtab_free (flinfo.strtab);
6384 /* Write out the debugging string table. */
6385 o = xcoff_hash_table (info)->debug_section;
6386 if (o != NULL)
6388 struct bfd_strtab_hash *debug_strtab;
6390 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6391 BFD_ASSERT (o->output_section->size - o->output_offset
6392 >= _bfd_stringtab_size (debug_strtab));
6393 pos = o->output_section->filepos + o->output_offset;
6394 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6395 goto error_return;
6396 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6397 goto error_return;
6400 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6401 not try to write out the symbols. */
6402 bfd_get_symcount (abfd) = 0;
6404 return TRUE;
6406 error_return:
6407 if (flinfo.strtab != NULL)
6408 _bfd_stringtab_free (flinfo.strtab);
6410 if (flinfo.section_info != NULL)
6412 unsigned int i;
6414 for (i = 0; i < abfd->section_count; i++)
6416 if (flinfo.section_info[i].relocs != NULL)
6417 free (flinfo.section_info[i].relocs);
6418 if (flinfo.section_info[i].rel_hashes != NULL)
6419 free (flinfo.section_info[i].rel_hashes);
6421 free (flinfo.section_info);
6424 if (flinfo.internal_syms != NULL)
6425 free (flinfo.internal_syms);
6426 if (flinfo.sym_indices != NULL)
6427 free (flinfo.sym_indices);
6428 if (flinfo.outsyms != NULL)
6429 free (flinfo.outsyms);
6430 if (flinfo.linenos != NULL)
6431 free (flinfo.linenos);
6432 if (flinfo.contents != NULL)
6433 free (flinfo.contents);
6434 if (flinfo.external_relocs != NULL)
6435 free (flinfo.external_relocs);
6436 if (external_relocs != NULL)
6437 free (external_relocs);
6438 return FALSE;