* opncls.c (bfd_make_readable): Call bfd_section_list_clear.
[external/binutils.git] / bfd / xcofflink.c
1 /* POWER/PowerPC XCOFF linker support.
2    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.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
31 /* This file holds the XCOFF linker code.  */
32
33 #define STRING_SIZE_SIZE (4)
34
35 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
36    This flag will only be used on input sections.  */
37
38 #define SEC_MARK (SEC_ROM)
39
40 /* The list of import files.  */
41
42 struct xcoff_import_file
43 {
44   /* The next entry in the list.  */
45   struct xcoff_import_file *next;
46   /* The path.  */
47   const char *path;
48   /* The file name.  */
49   const char *file;
50   /* The member name.  */
51   const char *member;
52 };
53
54 /* Information we keep for each section in the output file during the
55    final link phase.  */
56
57 struct xcoff_link_section_info
58 {
59   /* The relocs to be output.  */
60   struct internal_reloc *relocs;
61   /* For each reloc against a global symbol whose index was not known
62      when the reloc was handled, the global hash table entry.  */
63   struct xcoff_link_hash_entry **rel_hashes;
64   /* If there is a TOC relative reloc against a global symbol, and the
65      index of the TOC symbol is not known when the reloc was handled,
66      an entry is added to this linked list.  This is not an array,
67      like rel_hashes, because this case is quite uncommon.  */
68   struct xcoff_toc_rel_hash {
69     struct xcoff_toc_rel_hash *next;
70     struct xcoff_link_hash_entry *h;
71     struct internal_reloc *rel;
72   } *toc_rel_hashes;
73 };
74
75 /* Information that we pass around while doing the final link step.  */
76
77 struct xcoff_final_link_info
78 {
79   /* General link information.  */
80   struct bfd_link_info *info;
81   /* Output BFD.  */
82   bfd *output_bfd;
83   /* Hash table for long symbol names.  */
84   struct bfd_strtab_hash *strtab;
85   /* Array of information kept for each output section, indexed by the
86      target_index field.  */
87   struct xcoff_link_section_info *section_info;
88   /* Symbol index of last C_FILE symbol (-1 if none).  */
89   long last_file_index;
90   /* Contents of last C_FILE symbol.  */
91   struct internal_syment last_file;
92   /* Symbol index of TOC symbol.  */
93   long toc_symindx;
94   /* Start of .loader symbols.  */
95   bfd_byte *ldsym;
96   /* Next .loader reloc to swap out.  */
97   bfd_byte *ldrel;
98   /* File position of start of line numbers.  */
99   file_ptr line_filepos;
100   /* Buffer large enough to hold swapped symbols of any input file.  */
101   struct internal_syment *internal_syms;
102   /* Buffer large enough to hold output indices of symbols of any
103      input file.  */
104   long *sym_indices;
105   /* Buffer large enough to hold output symbols for any input file.  */
106   bfd_byte *outsyms;
107   /* Buffer large enough to hold external line numbers for any input
108      section.  */
109   bfd_byte *linenos;
110   /* Buffer large enough to hold any input section.  */
111   bfd_byte *contents;
112   /* Buffer large enough to hold external relocs of any input section.  */
113   bfd_byte *external_relocs;
114 };
115
116 static struct bfd_hash_entry *xcoff_link_hash_newfunc
117   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
118 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
119 static struct internal_reloc *xcoff_read_internal_relocs
120   PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
121            struct internal_reloc *));
122 static boolean xcoff_link_add_object_symbols
123   PARAMS ((bfd *, struct bfd_link_info *));
124 static boolean xcoff_link_check_archive_element
125   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
126 static boolean xcoff_link_check_ar_symbols
127   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
128 static boolean xcoff_link_check_dynamic_ar_symbols
129   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
130 static bfd_size_type xcoff_find_reloc
131   PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
132 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
133 static boolean xcoff_link_add_dynamic_symbols
134   PARAMS ((bfd *, struct bfd_link_info *));
135 static boolean xcoff_mark_symbol
136   PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
137 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
138 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
139 static boolean xcoff_build_ldsyms
140   PARAMS ((struct xcoff_link_hash_entry *, PTR));
141 static boolean xcoff_link_input_bfd
142   PARAMS ((struct xcoff_final_link_info *, bfd *));
143 static boolean xcoff_write_global_symbol
144   PARAMS ((struct xcoff_link_hash_entry *, PTR));
145 static boolean xcoff_reloc_link_order
146   PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
147            struct bfd_link_order *));
148 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
149 \f
150
151 /* Routines to read XCOFF dynamic information.  This don't really
152    belong here, but we already have the ldsym manipulation routines
153    here.  */
154
155 /* Read the contents of a section.  */
156
157 static boolean
158 xcoff_get_section_contents (abfd, sec)
159      bfd *abfd;
160      asection *sec;
161 {
162
163   if (coff_section_data (abfd, sec) == NULL)
164     {
165       bfd_size_type amt = sizeof (struct coff_section_tdata);
166       sec->used_by_bfd = bfd_zalloc (abfd, amt);
167       if (sec->used_by_bfd == NULL)
168         return false;
169     }
170
171   if (coff_section_data (abfd, sec)->contents == NULL)
172     {
173       coff_section_data (abfd, sec)->contents = ((bfd_byte *)
174                                                  bfd_malloc (sec->_raw_size));
175       if (coff_section_data (abfd, sec)->contents == NULL)
176         return false;
177
178       if (! bfd_get_section_contents (abfd, sec,
179                                       coff_section_data (abfd, sec)->contents,
180                                       (file_ptr) 0, sec->_raw_size))
181         return false;
182     }
183
184   return true;
185 }
186
187 /* Get the size required to hold the dynamic symbols.  */
188
189 long
190 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
191      bfd *abfd;
192 {
193   asection *lsec;
194   bfd_byte *contents;
195   struct internal_ldhdr ldhdr;
196
197   if ((abfd->flags & DYNAMIC) == 0)
198     {
199       bfd_set_error (bfd_error_invalid_operation);
200       return -1;
201     }
202
203   lsec = bfd_get_section_by_name (abfd, ".loader");
204   if (lsec == NULL)
205     {
206       bfd_set_error (bfd_error_no_symbols);
207       return -1;
208     }
209
210   if (! xcoff_get_section_contents (abfd, lsec))
211     return -1;
212   contents = coff_section_data (abfd, lsec)->contents;
213
214   bfd_xcoff_swap_ldhdr_in (abfd, (PTR) contents, &ldhdr);
215
216   return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
217 }
218
219 /* Get the dynamic symbols.  */
220
221 long
222 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
223      bfd *abfd;
224      asymbol **psyms;
225 {
226   asection *lsec;
227   bfd_byte *contents;
228   struct internal_ldhdr ldhdr;
229   const char *strings;
230   bfd_byte *elsym, *elsymend;
231   coff_symbol_type *symbuf;
232
233   if ((abfd->flags & DYNAMIC) == 0)
234     {
235       bfd_set_error (bfd_error_invalid_operation);
236       return -1;
237     }
238
239   lsec = bfd_get_section_by_name (abfd, ".loader");
240   if (lsec == NULL)
241     {
242       bfd_set_error (bfd_error_no_symbols);
243       return -1;
244     }
245
246   if (! xcoff_get_section_contents (abfd, lsec))
247     return -1;
248   contents = coff_section_data (abfd, lsec)->contents;
249
250   coff_section_data (abfd, lsec)->keep_contents = true;
251
252   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
253
254   strings = (char *) contents + ldhdr.l_stoff;
255
256   symbuf = ((coff_symbol_type *)
257             bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
258   if (symbuf == NULL)
259     return -1;
260
261   elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
262
263   elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
264   for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
265     {
266       struct internal_ldsym ldsym;
267
268       bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
269
270       symbuf->symbol.the_bfd = abfd;
271
272       if (ldsym._l._l_l._l_zeroes == 0)
273         symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
274       else
275         {
276           char *c;
277
278           c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
279           if (c == NULL)
280             return -1;
281           memcpy (c, ldsym._l._l_name, SYMNMLEN);
282           c[SYMNMLEN] = '\0';
283           symbuf->symbol.name = c;
284         }
285
286       if (ldsym.l_smclas == XMC_XO)
287         symbuf->symbol.section = bfd_abs_section_ptr;
288       else
289         symbuf->symbol.section = coff_section_from_bfd_index (abfd,
290                                                               ldsym.l_scnum);
291       symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
292
293       symbuf->symbol.flags = BSF_NO_FLAGS;
294       if ((ldsym.l_smtype & L_EXPORT) != 0)
295         symbuf->symbol.flags |= BSF_GLOBAL;
296
297       /* FIXME: We have no way to record the other information stored
298          with the loader symbol.  */
299
300       *psyms = (asymbol *) symbuf;
301     }
302
303   *psyms = NULL;
304
305   return ldhdr.l_nsyms;
306 }
307
308 /* Get the size required to hold the dynamic relocs.  */
309
310 long
311 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
312      bfd *abfd;
313 {
314   asection *lsec;
315   bfd_byte *contents;
316   struct internal_ldhdr ldhdr;
317
318   if ((abfd->flags & DYNAMIC) == 0)
319     {
320       bfd_set_error (bfd_error_invalid_operation);
321       return -1;
322     }
323
324   lsec = bfd_get_section_by_name (abfd, ".loader");
325   if (lsec == NULL)
326     {
327       bfd_set_error (bfd_error_no_symbols);
328       return -1;
329     }
330
331   if (! xcoff_get_section_contents (abfd, lsec))
332     return -1;
333   contents = coff_section_data (abfd, lsec)->contents;
334
335   bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
336
337   return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
338 }
339
340 /* Get the dynamic relocs.  */
341
342 long
343 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
344      bfd *abfd;
345      arelent **prelocs;
346      asymbol **syms;
347 {
348   asection *lsec;
349   bfd_byte *contents;
350   struct internal_ldhdr ldhdr;
351   arelent *relbuf;
352   bfd_byte *elrel, *elrelend;
353
354   if ((abfd->flags & DYNAMIC) == 0)
355     {
356       bfd_set_error (bfd_error_invalid_operation);
357       return -1;
358     }
359
360   lsec = bfd_get_section_by_name (abfd, ".loader");
361   if (lsec == NULL)
362     {
363       bfd_set_error (bfd_error_no_symbols);
364       return -1;
365     }
366
367   if (! xcoff_get_section_contents (abfd, lsec))
368     return -1;
369   contents = coff_section_data (abfd, lsec)->contents;
370
371   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
372
373   relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
374   if (relbuf == NULL)
375     return -1;
376
377   elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
378
379   elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
380   for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
381          prelocs++)
382     {
383       struct internal_ldrel ldrel;
384
385       bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
386
387       if (ldrel.l_symndx >= 3)
388         relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
389       else
390         {
391           const char *name;
392           asection *sec;
393
394           switch (ldrel.l_symndx)
395             {
396             case 0:
397               name = ".text";
398               break;
399             case 1:
400               name = ".data";
401               break;
402             case 2:
403               name = ".bss";
404               break;
405             default:
406               abort ();
407               break;
408             }
409
410           sec = bfd_get_section_by_name (abfd, name);
411           if (sec == NULL)
412             {
413               bfd_set_error (bfd_error_bad_value);
414               return -1;
415             }
416
417           relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
418         }
419
420       relbuf->address = ldrel.l_vaddr;
421       relbuf->addend = 0;
422
423       /* Most dynamic relocs have the same type.  FIXME: This is only
424          correct if ldrel.l_rtype == 0.  In other cases, we should use
425          a different howto.  */
426       relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
427
428       /* FIXME: We have no way to record the l_rsecnm field.  */
429
430       *prelocs = relbuf;
431     }
432
433   *prelocs = NULL;
434
435   return ldhdr.l_nreloc;
436 }
437 \f
438 /* Routine to create an entry in an XCOFF link hash table.  */
439
440 static struct bfd_hash_entry *
441 xcoff_link_hash_newfunc (entry, table, string)
442      struct bfd_hash_entry *entry;
443      struct bfd_hash_table *table;
444      const char *string;
445 {
446   struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
447
448   /* Allocate the structure if it has not already been allocated by a
449      subclass.  */
450   if (ret == (struct xcoff_link_hash_entry *) NULL)
451     ret = ((struct xcoff_link_hash_entry *)
452            bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
453   if (ret == (struct xcoff_link_hash_entry *) NULL)
454     return (struct bfd_hash_entry *) ret;
455
456   /* Call the allocation method of the superclass.  */
457   ret = ((struct xcoff_link_hash_entry *)
458          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
459                                  table, string));
460   if (ret != NULL)
461     {
462       /* Set local fields.  */
463       ret->indx = -1;
464       ret->toc_section = NULL;
465       ret->u.toc_indx = -1;
466       ret->descriptor = NULL;
467       ret->ldsym = NULL;
468       ret->ldindx = -1;
469       ret->flags = 0;
470       ret->smclas = XMC_UA;
471     }
472
473   return (struct bfd_hash_entry *) ret;
474 }
475
476 /* Create a XCOFF link hash table.  */
477
478 struct bfd_link_hash_table *
479 _bfd_xcoff_bfd_link_hash_table_create (abfd)
480      bfd *abfd;
481 {
482   struct xcoff_link_hash_table *ret;
483   bfd_size_type amt = sizeof (struct xcoff_link_hash_table);
484
485   ret = (struct xcoff_link_hash_table *) bfd_malloc (amt);
486   if (ret == (struct xcoff_link_hash_table *) NULL)
487     return (struct bfd_link_hash_table *) NULL;
488   if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
489     {
490       free (ret);
491       return (struct bfd_link_hash_table *) NULL;
492     }
493
494   ret->debug_strtab = _bfd_xcoff_stringtab_init ();
495   ret->debug_section = NULL;
496   ret->loader_section = NULL;
497   ret->ldrel_count = 0;
498   memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
499   ret->linkage_section = NULL;
500   ret->toc_section = NULL;
501   ret->descriptor_section = NULL;
502   ret->imports = NULL;
503   ret->file_align = 0;
504   ret->textro = false;
505   ret->gc = false;
506   memset (ret->special_sections, 0, sizeof ret->special_sections);
507
508   /* The linker will always generate a full a.out header.  We need to
509      record that fact now, before the sizeof_headers routine could be
510      called.  */
511   xcoff_data (abfd)->full_aouthdr = true;
512
513   return &ret->root;
514 }
515
516 /* Free a XCOFF link hash table.  */
517
518 void
519 _bfd_xcoff_bfd_link_hash_table_free (hash)
520      struct bfd_link_hash_table *hash;
521 {
522   struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
523
524   _bfd_stringtab_free (ret->debug_strtab);
525   bfd_hash_table_free (&ret->root.table);
526   free (ret);
527 }
528 \f
529 /* Read internal relocs for an XCOFF csect.  This is a wrapper around
530    _bfd_coff_read_internal_relocs which tries to take advantage of any
531    relocs which may have been cached for the enclosing section.  */
532
533 static struct internal_reloc *
534 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
535                             require_internal, internal_relocs)
536      bfd *abfd;
537      asection *sec;
538      boolean cache;
539      bfd_byte *external_relocs;
540      boolean require_internal;
541      struct internal_reloc *internal_relocs;
542 {
543
544   if (coff_section_data (abfd, sec) != NULL
545       && coff_section_data (abfd, sec)->relocs == NULL
546       && xcoff_section_data (abfd, sec) != NULL)
547     {
548       asection *enclosing;
549
550       enclosing = xcoff_section_data (abfd, sec)->enclosing;
551
552       if (enclosing != NULL
553           && (coff_section_data (abfd, enclosing) == NULL
554               || coff_section_data (abfd, enclosing)->relocs == NULL)
555           && cache
556           && enclosing->reloc_count > 0)
557         {
558           if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
559                                               external_relocs, false,
560                                               (struct internal_reloc *) NULL)
561               == NULL)
562             return NULL;
563         }
564
565       if (enclosing != NULL
566           && coff_section_data (abfd, enclosing) != NULL
567           && coff_section_data (abfd, enclosing)->relocs != NULL)
568         {
569           size_t off;
570
571           off = ((sec->rel_filepos - enclosing->rel_filepos)
572                  / bfd_coff_relsz (abfd));
573
574           if (! require_internal)
575             return coff_section_data (abfd, enclosing)->relocs + off;
576           memcpy (internal_relocs,
577                   coff_section_data (abfd, enclosing)->relocs + off,
578                   sec->reloc_count * sizeof (struct internal_reloc));
579           return internal_relocs;
580         }
581     }
582
583   return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
584                                          require_internal, internal_relocs);
585 }
586 \f
587 /* Given an XCOFF BFD, add symbols to the global hash table as
588    appropriate.  */
589
590 boolean
591 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
592      bfd *abfd;
593      struct bfd_link_info *info;
594 {
595
596   switch (bfd_get_format (abfd))
597     {
598     case bfd_object:
599       return xcoff_link_add_object_symbols (abfd, info);
600
601     case bfd_archive:
602       /* If the archive has a map, do the usual search.  We then need
603          to check the archive for dynamic objects, because they may not 
604          appear in the archive map even though they should, perhaps, be 
605          included.  If the archive has no map, we just consider each object 
606          file in turn, since that apparently is what the AIX native linker 
607          does.  */
608       if (bfd_has_map (abfd))
609         {
610           if (! (_bfd_generic_link_add_archive_symbols
611                  (abfd, info, xcoff_link_check_archive_element)))
612             return false;
613         }
614
615       {
616         bfd *member;
617         
618         member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
619         while (member != NULL)
620           {
621             if (bfd_check_format (member, bfd_object)
622                 && (info->hash->creator == member->xvec)
623                 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
624               {
625                 boolean needed;
626                 
627                 if (! xcoff_link_check_archive_element (member, info, 
628                                                         &needed))
629                   return false;
630                 if (needed)
631                   member->archive_pass = -1;
632               }
633             member = bfd_openr_next_archived_file (abfd, member);
634           }
635       }
636
637       return true;
638
639     default:
640       bfd_set_error (bfd_error_wrong_format);
641       return false;
642     }
643 }
644
645 /* Add symbols from an XCOFF object file.  */
646
647 static boolean
648 xcoff_link_add_object_symbols (abfd, info)
649      bfd *abfd;
650      struct bfd_link_info *info;
651 {
652
653   if (! _bfd_coff_get_external_symbols (abfd))
654     return false;
655   if (! xcoff_link_add_symbols (abfd, info))
656     return false;
657   if (! info->keep_memory)
658     {
659       if (! _bfd_coff_free_symbols (abfd))
660         return false;
661     }
662   return true;
663 }
664
665 /* Check a single archive element to see if we need to include it in
666    the link.  *PNEEDED is set according to whether this element is
667    needed in the link or not.  This is called via
668    _bfd_generic_link_add_archive_symbols.  */
669
670 static boolean
671 xcoff_link_check_archive_element (abfd, info, pneeded)
672      bfd *abfd;
673      struct bfd_link_info *info;
674      boolean *pneeded;
675 {
676
677   if (! _bfd_coff_get_external_symbols (abfd))
678     return false;
679
680   if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
681     return false;
682
683   if (*pneeded)
684     {
685       if (! xcoff_link_add_symbols (abfd, info))
686         return false;
687     }
688
689   if (! info->keep_memory || ! *pneeded)
690     {
691       if (! _bfd_coff_free_symbols (abfd))
692         return false;
693     }
694
695   return true;
696 }
697
698 /* Look through the symbols to see if this object file should be
699    included in the link.  */
700
701 static boolean
702 xcoff_link_check_ar_symbols (abfd, info, pneeded)
703      bfd *abfd;
704      struct bfd_link_info *info;
705      boolean *pneeded;
706 {
707   bfd_size_type symesz;
708   bfd_byte *esym;
709   bfd_byte *esym_end;
710
711   *pneeded = false;
712
713   if ((abfd->flags & DYNAMIC) != 0
714       && ! info->static_link
715       && info->hash->creator == abfd->xvec)
716     return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
717
718   symesz = bfd_coff_symesz (abfd);
719   esym = (bfd_byte *) obj_coff_external_syms (abfd);
720   esym_end = esym + obj_raw_syment_count (abfd) * symesz;
721   while (esym < esym_end)
722     {
723       struct internal_syment sym;
724
725       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
726
727       if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
728         {
729           const char *name;
730           char buf[SYMNMLEN + 1];
731           struct bfd_link_hash_entry *h;
732
733           /* This symbol is externally visible, and is defined by this
734              object file.  */
735
736           name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
737
738           if (name == NULL)
739             return false;
740           h = bfd_link_hash_lookup (info->hash, name, false, false, true);
741
742           /* We are only interested in symbols that are currently
743              undefined.  If a symbol is currently known to be common,
744              XCOFF linkers do not bring in an object file which
745              defines it.  We also don't bring in symbols to satisfy
746              undefined references in shared objects.  */
747           if (h != (struct bfd_link_hash_entry *) NULL
748               && h->type == bfd_link_hash_undefined
749               && (info->hash->creator != abfd->xvec
750                   || (((struct xcoff_link_hash_entry *) h)->flags
751                       & XCOFF_DEF_DYNAMIC) == 0))
752             {
753               if (! (*info->callbacks->add_archive_element) (info, abfd, name))
754                 return false;
755               *pneeded = true;
756               return true;
757             }
758         }
759
760       esym += (sym.n_numaux + 1) * symesz;
761     }
762
763   /* We do not need this object file.  */
764   return true;
765 }
766
767 /* Look through the loader symbols to see if this dynamic object
768    should be included in the link.  The native linker uses the loader
769    symbols, not the normal symbol table, so we do too.  */
770
771 static boolean
772 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
773      bfd *abfd;
774      struct bfd_link_info *info;
775      boolean *pneeded;
776 {
777   asection *lsec;
778   bfd_byte *contents;
779   struct internal_ldhdr ldhdr;
780   const char *strings;
781   bfd_byte *elsym, *elsymend;
782
783   *pneeded = false;
784
785   lsec = bfd_get_section_by_name (abfd, ".loader");
786   if (lsec == NULL)
787     {
788       /* There are no symbols, so don't try to include it.  */
789       return true;
790     }
791
792   if (! xcoff_get_section_contents (abfd, lsec))
793     return false;
794   contents = coff_section_data (abfd, lsec)->contents;
795
796   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
797
798   strings = (char *) contents + ldhdr.l_stoff;
799
800   elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
801
802   elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
803   for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
804     {
805       struct internal_ldsym ldsym;
806       char nambuf[SYMNMLEN + 1];
807       const char *name;
808       struct bfd_link_hash_entry *h;
809
810       bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
811
812       /* We are only interested in exported symbols.  */
813       if ((ldsym.l_smtype & L_EXPORT) == 0)
814         continue;
815
816       if (ldsym._l._l_l._l_zeroes == 0)
817         name = strings + ldsym._l._l_l._l_offset;
818       else
819         {
820           memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
821           nambuf[SYMNMLEN] = '\0';
822           name = nambuf;
823         }
824
825       h = bfd_link_hash_lookup (info->hash, name, false, false, true);
826
827       /* We are only interested in symbols that are currently
828          undefined.  At this point we know that we are using an XCOFF
829          hash table.  */
830       if (h != NULL
831           && h->type == bfd_link_hash_undefined
832           && (((struct xcoff_link_hash_entry *) h)->flags
833               & XCOFF_DEF_DYNAMIC) == 0)
834         {
835           if (! (*info->callbacks->add_archive_element) (info, abfd, name))
836             return false;
837           *pneeded = true;
838           return true;
839         }
840     }
841
842   /* We do not need this shared object.  */
843
844   if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
845     {
846       free (coff_section_data (abfd, lsec)->contents);
847       coff_section_data (abfd, lsec)->contents = NULL;
848     }
849
850   return true;
851 }
852
853 /* Returns the index of reloc in RELOCS with the least address greater
854    than or equal to ADDRESS.  The relocs are sorted by address.  */
855
856 static bfd_size_type
857 xcoff_find_reloc (relocs, count, address)
858      struct internal_reloc *relocs;
859      bfd_size_type count;
860      bfd_vma address;
861 {
862   bfd_size_type min, max, this;
863
864   if (count < 2)
865     {
866       if (count == 1 && relocs[0].r_vaddr < address)
867         return 1;
868       else
869         return 0;
870     }
871
872   min = 0;
873   max = count;
874
875   /* Do a binary search over (min,max].  */
876   while (min + 1 < max)
877     {
878       bfd_vma raddr;
879
880       this = (max + min) / 2;
881       raddr = relocs[this].r_vaddr;
882       if (raddr > address)
883         max = this;
884       else if (raddr < address)
885         min = this;
886       else
887         {
888           min = this;
889           break;
890         }
891     }
892
893   if (relocs[min].r_vaddr < address)
894     return min + 1;
895
896   while (min > 0
897          && relocs[min - 1].r_vaddr == address)
898     --min;
899
900   return min;
901 }
902
903
904 /* xcoff_link_create_extra_sections
905
906    Takes care of creating the .loader, .gl, .ds, .debug and sections.  */
907
908 static boolean
909 xcoff_link_create_extra_sections(bfd * abfd, struct bfd_link_info *info)
910 {
911
912   boolean return_value = false;
913
914   if (info->hash->creator == abfd->xvec)
915     {
916
917       /* We need to build a .loader section, so we do it here.  This
918          won't work if we're producing an XCOFF output file with no
919          XCOFF input files.  FIXME.  */
920
921       if (xcoff_hash_table (info)->loader_section == NULL)
922         {
923           asection *lsec;
924
925           lsec = bfd_make_section_anyway (abfd, ".loader");
926           if (lsec == NULL)
927             {
928               goto end_return;
929             }
930           xcoff_hash_table (info)->loader_section = lsec;
931           lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
932         }
933
934       /* Likewise for the linkage section.  */
935       if (xcoff_hash_table (info)->linkage_section == NULL)
936         {
937           asection *lsec;
938
939           lsec = bfd_make_section_anyway (abfd, ".gl");
940           if (lsec == NULL)
941             {
942               goto end_return;
943             }
944
945           xcoff_hash_table (info)->linkage_section = lsec;
946           lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
947                           | SEC_IN_MEMORY);
948           lsec->alignment_power = 2;
949         }
950
951       /* Likewise for the TOC section.  */
952       if (xcoff_hash_table (info)->toc_section == NULL)
953         {
954           asection *tsec;
955
956           tsec = bfd_make_section_anyway (abfd, ".tc");
957           if (tsec == NULL)
958             {
959               goto end_return;
960             }
961
962           xcoff_hash_table (info)->toc_section = tsec;
963           tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
964                           | SEC_IN_MEMORY);
965           tsec->alignment_power = 2;
966         }
967
968       /* Likewise for the descriptor section.  */
969       if (xcoff_hash_table (info)->descriptor_section == NULL)
970         {
971           asection *dsec;
972
973           dsec = bfd_make_section_anyway (abfd, ".ds");
974           if (dsec == NULL)
975             {
976               goto end_return;
977             }
978
979           xcoff_hash_table (info)->descriptor_section = dsec;
980           dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
981                           | SEC_IN_MEMORY);
982           dsec->alignment_power = 2;
983         }
984
985       /* Likewise for the .debug section.  */
986       if (xcoff_hash_table (info)->debug_section == NULL
987           && info->strip != strip_all)
988         {
989           asection *dsec;
990
991           dsec = bfd_make_section_anyway (abfd, ".debug");
992           if (dsec == NULL)
993             {
994               goto end_return;
995             }
996           xcoff_hash_table (info)->debug_section = dsec;
997           dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
998         }
999     }
1000
1001   return_value = true;
1002
1003  end_return:
1004
1005   return return_value;
1006 }
1007
1008 /* Add all the symbols from an object file to the hash table.
1009
1010    XCOFF is a weird format.  A normal XCOFF .o files will have three
1011    COFF sections--.text, .data, and .bss--but each COFF section will
1012    contain many csects.  These csects are described in the symbol
1013    table.  From the linker's point of view, each csect must be
1014    considered a section in its own right.  For example, a TOC entry is
1015    handled as a small XMC_TC csect.  The linker must be able to merge
1016    different TOC entries together, which means that it must be able to
1017    extract the XMC_TC csects from the .data section of the input .o
1018    file.
1019
1020    From the point of view of our linker, this is, of course, a hideous
1021    nightmare.  We cope by actually creating sections for each csect,
1022    and discarding the original sections.  We then have to handle the
1023    relocation entries carefully, since the only way to tell which
1024    csect they belong to is to examine the address.  */
1025
1026 static boolean
1027 xcoff_link_add_symbols (abfd, info)
1028      bfd *abfd;
1029      struct bfd_link_info *info;
1030 {
1031   unsigned int n_tmask;
1032   unsigned int n_btshft;
1033   boolean default_copy;
1034   bfd_size_type symcount;
1035   struct xcoff_link_hash_entry **sym_hash;
1036   asection **csect_cache;
1037   bfd_size_type linesz;
1038   asection *o;
1039   asection *last_real;
1040   boolean keep_syms;
1041   asection *csect;
1042   unsigned int csect_index;
1043   asection *first_csect;
1044   bfd_size_type symesz;
1045   bfd_byte *esym;
1046   bfd_byte *esym_end;
1047   struct reloc_info_struct
1048   {
1049     struct internal_reloc *relocs;
1050     asection **csects;
1051     bfd_byte *linenos;
1052   } *reloc_info = NULL;
1053   bfd_size_type amt;
1054
1055   keep_syms = obj_coff_keep_syms (abfd);
1056
1057   if ((abfd->flags & DYNAMIC) != 0
1058       && ! info->static_link)
1059     {
1060       if (! xcoff_link_add_dynamic_symbols (abfd, info))
1061         return false;
1062     }
1063
1064   /* create the loader, toc, gl, ds and debug sections, if needed */
1065   if (false == xcoff_link_create_extra_sections(abfd, info))
1066     goto error_return;
1067
1068   if ((abfd->flags & DYNAMIC) != 0
1069       && ! info->static_link)
1070     return true;
1071
1072   n_tmask = coff_data (abfd)->local_n_tmask;
1073   n_btshft = coff_data (abfd)->local_n_btshft;
1074
1075   /* Define macros so that ISFCN, et. al., macros work correctly.  */
1076 #define N_TMASK n_tmask
1077 #define N_BTSHFT n_btshft
1078
1079   if (info->keep_memory)
1080     default_copy = false;
1081   else
1082     default_copy = true;
1083
1084   symcount = obj_raw_syment_count (abfd);
1085
1086   /* We keep a list of the linker hash table entries that correspond
1087      to each external symbol.  */
1088   amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1089   sym_hash = (struct xcoff_link_hash_entry **) bfd_alloc (abfd, amt);
1090   if (sym_hash == NULL && symcount != 0)
1091     goto error_return;
1092   coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1093   memset (sym_hash, 0, (size_t) amt);
1094
1095   /* Because of the weird stuff we are doing with XCOFF csects, we can
1096      not easily determine which section a symbol is in, so we store
1097      the information in the tdata for the input file.  */
1098   amt = symcount * sizeof (asection *);
1099   csect_cache = (asection **) bfd_alloc (abfd, amt);
1100   if (csect_cache == NULL && symcount != 0)
1101     goto error_return;
1102   xcoff_data (abfd)->csects = csect_cache;
1103   memset (csect_cache, 0, (size_t) amt);
1104
1105   /* While splitting sections into csects, we need to assign the
1106      relocs correctly.  The relocs and the csects must both be in
1107      order by VMA within a given section, so we handle this by
1108      scanning along the relocs as we process the csects.  We index
1109      into reloc_info using the section target_index.  */
1110   amt = abfd->section_count + 1;
1111   amt *= sizeof (struct reloc_info_struct);
1112   reloc_info = (struct reloc_info_struct *) bfd_malloc (amt);
1113   if (reloc_info == NULL)
1114     goto error_return;
1115   memset ((PTR) reloc_info, 0, (size_t) amt);
1116
1117   /* Read in the relocs and line numbers for each section.  */
1118   linesz = bfd_coff_linesz (abfd);
1119   last_real = NULL;
1120   for (o = abfd->sections; o != NULL; o = o->next)
1121     {
1122
1123       last_real = o;
1124       if ((o->flags & SEC_RELOC) != 0)
1125         {
1126
1127           reloc_info[o->target_index].relocs =
1128             xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1129                                         false, (struct internal_reloc *) NULL);
1130           amt = o->reloc_count;
1131           amt *= sizeof (asection *);
1132           reloc_info[o->target_index].csects = (asection **) bfd_malloc (amt);
1133           if (reloc_info[o->target_index].csects == NULL)
1134             goto error_return;
1135           memset (reloc_info[o->target_index].csects, 0, (size_t) amt);
1136
1137         }
1138
1139       if ((info->strip == strip_none || info->strip == strip_some)
1140           && o->lineno_count > 0)
1141         {
1142
1143           bfd_byte *linenos;
1144
1145           amt = linesz * o->lineno_count;
1146           linenos = (bfd_byte *) bfd_malloc (amt);
1147           if (linenos == NULL)
1148             goto error_return;
1149           reloc_info[o->target_index].linenos = linenos;
1150           if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1151               || bfd_bread (linenos, amt, abfd) != amt)
1152             goto error_return;
1153
1154         }
1155     }
1156
1157   /* Don't let the linker relocation routines discard the symbols.  */
1158   obj_coff_keep_syms (abfd) = true;
1159
1160   csect = NULL;
1161   csect_index = 0;
1162   first_csect = NULL;
1163
1164   symesz = bfd_coff_symesz (abfd);
1165   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1166   esym = (bfd_byte *) obj_coff_external_syms (abfd);
1167   esym_end = esym + symcount * symesz;
1168
1169   while (esym < esym_end)
1170     {
1171       struct internal_syment sym;
1172       union internal_auxent aux;
1173       const char *name;
1174       char buf[SYMNMLEN + 1];
1175       int smtyp;
1176       flagword flags;
1177       asection *section;
1178       bfd_vma value;
1179       struct xcoff_link_hash_entry *set_toc;
1180
1181       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1182
1183       /* In this pass we are only interested in symbols with csect
1184          information.  */
1185       if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1186         {
1187
1188           /* Set csect_cache,
1189              Normally csect is a .pr, .rw  etc. created in the loop
1190              If C_FILE or first time, handle special
1191
1192              Advance esym, sym_hash, csect_hash ptr's
1193              Keep track of the last_symndx for the current file.  */
1194           if (sym.n_sclass == C_FILE && csect != NULL)
1195             {
1196               xcoff_section_data (abfd, csect)->last_symndx =
1197                 ((esym
1198                   - (bfd_byte *) obj_coff_external_syms (abfd))
1199                  / symesz);
1200               csect = NULL;
1201             }
1202
1203           if (csect != NULL)
1204             *csect_cache = csect;
1205           else if (first_csect == NULL || sym.n_sclass == C_FILE)
1206             *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1207           else
1208             *csect_cache = NULL;
1209           esym += (sym.n_numaux + 1) * symesz;
1210           sym_hash += sym.n_numaux + 1;
1211           csect_cache += sym.n_numaux + 1;
1212
1213           continue;
1214         }
1215
1216       name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1217
1218       if (name == NULL)
1219         goto error_return;
1220
1221       /* If this symbol has line number information attached to it,
1222          and we're not stripping it, count the number of entries and
1223          add them to the count for this csect.  In the final link pass
1224          we are going to attach line number information by symbol,
1225          rather than by section, in order to more easily handle
1226          garbage collection.  */
1227       if ((info->strip == strip_none || info->strip == strip_some)
1228           && sym.n_numaux > 1
1229           && csect != NULL
1230           && ISFCN (sym.n_type))
1231         {
1232
1233           union internal_auxent auxlin;
1234
1235           bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1236                                 sym.n_type, sym.n_sclass,
1237                                 0, sym.n_numaux, (PTR) &auxlin);
1238
1239           if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1240             {
1241               asection *enclosing;
1242               bfd_signed_vma linoff;
1243
1244               enclosing = xcoff_section_data (abfd, csect)->enclosing;
1245               if (enclosing == NULL)
1246                 {
1247                   (*_bfd_error_handler)
1248                     (_("%s: `%s' has line numbers but no enclosing section"),
1249                      bfd_archive_filename (abfd), name);
1250                   bfd_set_error (bfd_error_bad_value);
1251                   goto error_return;
1252                 }
1253               linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1254                         - enclosing->line_filepos);
1255               /* explict cast to bfd_signed_vma for compiler */
1256               if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1257                 {
1258                   struct internal_lineno lin;
1259                   bfd_byte *linpstart;
1260
1261                   linpstart = (reloc_info[enclosing->target_index].linenos
1262                                + linoff);
1263                   bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1264                   if (lin.l_lnno == 0
1265                       && ((bfd_size_type) lin.l_addr.l_symndx
1266                           == ((esym
1267                                - (bfd_byte *) obj_coff_external_syms (abfd))
1268                               / symesz)))
1269                     {
1270                       bfd_byte *linpend, *linp;
1271
1272                       linpend = (reloc_info[enclosing->target_index].linenos
1273                                  + enclosing->lineno_count * linesz);
1274                       for (linp = linpstart + linesz;
1275                            linp < linpend;
1276                            linp += linesz)
1277                         {
1278                           bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1279                                                    (PTR) &lin);
1280                           if (lin.l_lnno == 0)
1281                             break;
1282                         }
1283                       csect->lineno_count += (linp - linpstart) / linesz;
1284                       /* The setting of line_filepos will only be
1285                          useful if all the line number entries for a
1286                          csect are contiguous; this only matters for
1287                          error reporting.  */
1288                       if (csect->line_filepos == 0)
1289                         csect->line_filepos =
1290                           auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1291                     }
1292                 }
1293             }
1294         }
1295
1296       /* Pick up the csect auxiliary information.  */
1297
1298       if (sym.n_numaux == 0)
1299         {
1300           (*_bfd_error_handler)
1301             (_("%s: class %d symbol `%s' has no aux entries"),
1302              bfd_archive_filename (abfd), sym.n_sclass, name);
1303           bfd_set_error (bfd_error_bad_value);
1304           goto error_return;
1305         }
1306
1307       bfd_coff_swap_aux_in (abfd,
1308                             (PTR) (esym + symesz * sym.n_numaux),
1309                             sym.n_type, sym.n_sclass,
1310                             sym.n_numaux - 1, sym.n_numaux,
1311                             (PTR) &aux);
1312
1313       smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1314
1315       flags = BSF_GLOBAL;
1316       section = NULL;
1317       value = 0;
1318       set_toc = NULL;
1319
1320       switch (smtyp)
1321         {
1322         default:
1323           (*_bfd_error_handler)
1324             (_("%s: symbol `%s' has unrecognized csect type %d"),
1325              bfd_archive_filename (abfd), name, smtyp);
1326           bfd_set_error (bfd_error_bad_value);
1327           goto error_return;
1328
1329         case XTY_ER:
1330           /* This is an external reference.  */
1331           if (sym.n_sclass == C_HIDEXT
1332               || sym.n_scnum != N_UNDEF
1333               || aux.x_csect.x_scnlen.l != 0)
1334             {
1335               (*_bfd_error_handler)
1336                 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1337                  bfd_archive_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1338                  aux.x_csect.x_scnlen.l);
1339               bfd_set_error (bfd_error_bad_value);
1340               goto error_return;
1341             }
1342
1343           /* An XMC_XO external reference is actually a reference to
1344              an absolute location.  */
1345           if (aux.x_csect.x_smclas != XMC_XO)
1346             section = bfd_und_section_ptr;
1347           else
1348             {
1349               section = bfd_abs_section_ptr;
1350               value = sym.n_value;
1351             }
1352           break;
1353
1354         case XTY_SD:
1355           /* This is a csect definition.  */
1356           if (csect != NULL)
1357             {
1358               xcoff_section_data (abfd, csect)->last_symndx =
1359                 ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1360             }
1361
1362           csect = NULL;
1363           csect_index = -(unsigned) 1;
1364
1365           /* When we see a TOC anchor, we record the TOC value.  */
1366           if (aux.x_csect.x_smclas == XMC_TC0)
1367             {
1368               if (sym.n_sclass != C_HIDEXT
1369                   || aux.x_csect.x_scnlen.l != 0)
1370                 {
1371                   (*_bfd_error_handler)
1372                     (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1373                      bfd_archive_filename (abfd), name, sym.n_sclass,
1374                      aux.x_csect.x_scnlen.l);
1375                   bfd_set_error (bfd_error_bad_value);
1376                   goto error_return;
1377                 }
1378               xcoff_data (abfd)->toc = sym.n_value;
1379             }
1380
1381           /* We must merge TOC entries for the same symbol.  We can
1382              merge two TOC entries if they are both C_HIDEXT, they
1383              both have the same name, they are both 4 or 8 bytes long, and
1384              they both have a relocation table entry for an external
1385              symbol with the same name.  Unfortunately, this means
1386              that we must look through the relocations.  Ick.
1387
1388              Logic for 32 bit vs 64 bit.
1389              32 bit has a csect length of 4 for TOC
1390              64 bit has a csect length of 8 for TOC
1391
1392              The conditions to get past the if-check are not that bad.
1393              They are what is used to create the TOC csects in the first
1394              place.  */
1395           if (aux.x_csect.x_smclas == XMC_TC
1396               && sym.n_sclass == C_HIDEXT
1397               && info->hash->creator == abfd->xvec
1398               && ((bfd_xcoff_is_xcoff32 (abfd)
1399                    && aux.x_csect.x_scnlen.l == 4)
1400                   || (bfd_xcoff_is_xcoff64 (abfd)
1401                       && aux.x_csect.x_scnlen.l == 8)))
1402             {
1403               asection *enclosing;
1404               struct internal_reloc *relocs;
1405               bfd_size_type relindx;
1406               struct internal_reloc *rel;
1407
1408               enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1409               if (enclosing == NULL)
1410                 goto error_return;
1411
1412               relocs = reloc_info[enclosing->target_index].relocs;
1413               amt = enclosing->reloc_count;
1414               relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1415               rel = relocs + relindx;
1416
1417               /* 32 bit R_POS r_size is 31
1418                  64 bit R_POS r_size is 63  */
1419               if (relindx < enclosing->reloc_count
1420                   && rel->r_vaddr == (bfd_vma) sym.n_value
1421                   && rel->r_type == R_POS
1422                   && ((bfd_xcoff_is_xcoff32 (abfd)
1423                        && rel->r_size == 31)
1424                       || (bfd_xcoff_is_xcoff64 (abfd)
1425                           && rel->r_size == 63)))
1426                 {
1427                   bfd_byte *erelsym;
1428
1429                   struct internal_syment relsym;
1430
1431                   erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1432                              + rel->r_symndx * symesz);
1433                   bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1434                   if (relsym.n_sclass == C_EXT)
1435                     {
1436                       const char *relname;
1437                       char relbuf[SYMNMLEN + 1];
1438                       boolean copy;
1439                       struct xcoff_link_hash_entry *h;
1440
1441                       /* At this point we know that the TOC entry is
1442                          for an externally visible symbol.  */
1443
1444                       relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1445                                                                 relbuf);
1446                       if (relname == NULL)
1447                         goto error_return;
1448
1449                       /* We only merge TOC entries if the TC name is
1450                          the same as the symbol name.  This handles
1451                          the normal case, but not common cases like
1452                          SYM.P4 which gcc generates to store SYM + 4
1453                          in the TOC.  FIXME.  */
1454
1455                       if (strcmp (name, relname) == 0)
1456                         {
1457                           copy = (! info->keep_memory
1458                                   || relsym._n._n_n._n_zeroes != 0
1459                                   || relsym._n._n_n._n_offset == 0);
1460                           h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1461                                                       relname, true, copy,
1462                                                       false);
1463                           if (h == NULL)
1464                             goto error_return;
1465
1466                           /* At this point h->root.type could be
1467                              bfd_link_hash_new.  That should be OK,
1468                              since we know for sure that we will come
1469                              across this symbol as we step through the
1470                              file.  */
1471
1472                           /* We store h in *sym_hash for the
1473                              convenience of the relocate_section
1474                              function.  */
1475                           *sym_hash = h;
1476
1477                           if (h->toc_section != NULL)
1478                             {
1479                               asection **rel_csects;
1480
1481                               /* We already have a TOC entry for this
1482                                  symbol, so we can just ignore this
1483                                  one.  */
1484                               rel_csects =
1485                                 reloc_info[enclosing->target_index].csects;
1486                               rel_csects[relindx] = bfd_und_section_ptr;
1487                               break;
1488                             }
1489
1490                           /* We are about to create a TOC entry for
1491                              this symbol.  */
1492                           set_toc = h;
1493                         } /* merge toc reloc */
1494                     } /* c_ext */
1495                 } /* reloc */
1496             } /* merge toc */
1497
1498           {
1499
1500             asection *enclosing;
1501
1502             /* We need to create a new section.  We get the name from
1503                the csect storage mapping class, so that the linker can
1504                accumulate similar csects together.  */
1505
1506             csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1507             if (NULL == csect)
1508               {
1509                 goto error_return;
1510               }
1511
1512             /* The enclosing section is the main section : .data, .text
1513                or .bss that the csect is coming from.  */
1514             enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1515             if (enclosing == NULL)
1516               goto error_return;
1517
1518             if (! bfd_is_abs_section (enclosing)
1519                 && ((bfd_vma) sym.n_value < enclosing->vma
1520                     || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1521                         > enclosing->vma + enclosing->_raw_size)))
1522               {
1523                 (*_bfd_error_handler)
1524                   (_("%s: csect `%s' not in enclosing section"),
1525                    bfd_archive_filename (abfd), name);
1526                 bfd_set_error (bfd_error_bad_value);
1527                 goto error_return;
1528               }
1529             csect->vma = sym.n_value;
1530             csect->filepos = (enclosing->filepos
1531                               + sym.n_value
1532                               - enclosing->vma);
1533             csect->_raw_size = aux.x_csect.x_scnlen.l;
1534             csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1535             csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1536
1537             /* Record the enclosing section in the tdata for this new
1538                section.  */
1539             amt = sizeof (struct coff_section_tdata);
1540             csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1541             if (csect->used_by_bfd == NULL)
1542               goto error_return;
1543             amt = sizeof (struct xcoff_section_tdata);
1544             coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1545             if (coff_section_data (abfd, csect)->tdata == NULL)
1546               goto error_return;
1547             xcoff_section_data (abfd, csect)->enclosing = enclosing;
1548             xcoff_section_data (abfd, csect)->lineno_count =
1549               enclosing->lineno_count;
1550
1551             if (enclosing->owner == abfd)
1552               {
1553                 struct internal_reloc *relocs;
1554                 bfd_size_type relindx;
1555                 struct internal_reloc *rel;
1556                 asection **rel_csect;
1557
1558                 relocs = reloc_info[enclosing->target_index].relocs;
1559                 amt = enclosing->reloc_count;
1560                 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1561
1562                 rel = relocs + relindx;
1563                 rel_csect = (reloc_info[enclosing->target_index].csects
1564                              + relindx);
1565
1566                 csect->rel_filepos = (enclosing->rel_filepos
1567                                       + relindx * bfd_coff_relsz (abfd));
1568                 while (relindx < enclosing->reloc_count
1569                        && *rel_csect == NULL
1570                        && rel->r_vaddr < csect->vma + csect->_raw_size)
1571                   {
1572
1573                     *rel_csect = csect;
1574                     csect->flags |= SEC_RELOC;
1575                     ++csect->reloc_count;
1576                     ++relindx;
1577                     ++rel;
1578                     ++rel_csect;
1579                   }
1580               }
1581
1582             /* There are a number of other fields and section flags
1583                which we do not bother to set.  */
1584
1585             csect_index = ((esym
1586                             - (bfd_byte *) obj_coff_external_syms (abfd))
1587                            / symesz);
1588
1589             xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1590
1591             if (first_csect == NULL)
1592               first_csect = csect;
1593
1594             /* If this symbol is C_EXT, we treat it as starting at the
1595                beginning of the newly created section.  */
1596             if (sym.n_sclass == C_EXT)
1597               {
1598                 section = csect;
1599                 value = 0;
1600               }
1601
1602             /* If this is a TOC section for a symbol, record it.  */
1603             if (set_toc != NULL)
1604               set_toc->toc_section = csect;
1605           }
1606           break;
1607
1608         case XTY_LD:
1609           /* This is a label definition.  The x_scnlen field is the
1610              symbol index of the csect.  Usually the XTY_LD symbol will
1611              follow its appropriate XTY_SD symbol.  The .set pseudo op can
1612              cause the XTY_LD to not follow the XTY_SD symbol. */
1613           {
1614             boolean bad;
1615
1616             bad = false;
1617             if (aux.x_csect.x_scnlen.l < 0
1618                 || (aux.x_csect.x_scnlen.l
1619                     >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1620               bad = true;
1621             if (! bad)
1622               {
1623                 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1624                 if (section == NULL
1625                     || (section->flags & SEC_HAS_CONTENTS) == 0)
1626                   bad = true;
1627               }
1628             if (bad)
1629               {
1630                 (*_bfd_error_handler)
1631                   (_("%s: misplaced XTY_LD `%s'"),
1632                    bfd_archive_filename (abfd), name);
1633                 bfd_set_error (bfd_error_bad_value);
1634                 goto error_return;
1635               }
1636             csect = section;
1637             value = sym.n_value - csect->vma;
1638           }
1639           break;
1640
1641         case XTY_CM:
1642           /* This is an unitialized csect.  We could base the name on
1643              the storage mapping class, but we don't bother except for
1644              an XMC_TD symbol.  If this csect is externally visible,
1645              it is a common symbol.  We put XMC_TD symbols in sections
1646              named .tocbss, and rely on the linker script to put that
1647              in the TOC area.  */
1648
1649           if (csect != NULL)
1650             {
1651               xcoff_section_data (abfd, csect)->last_symndx =
1652                 ((esym
1653                   - (bfd_byte *) obj_coff_external_syms (abfd))
1654                  / symesz);
1655             }
1656
1657           if (aux.x_csect.x_smclas == XMC_TD)
1658             {
1659               /* The linker script puts the .td section in the data
1660                  section after the .tc section.  */
1661               csect = bfd_make_section_anyway (abfd, ".td");
1662
1663             }
1664           else
1665             {
1666               csect = bfd_make_section_anyway (abfd, ".bss");
1667             }
1668           if (csect == NULL)
1669             goto error_return;
1670           csect->vma = sym.n_value;
1671           csect->_raw_size = aux.x_csect.x_scnlen.l;
1672           csect->flags |= SEC_ALLOC;
1673           csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1674           /* There are a number of other fields and section flags
1675              which we do not bother to set.  */
1676
1677           csect_index = ((esym
1678                           - (bfd_byte *) obj_coff_external_syms (abfd))
1679                          / symesz);
1680
1681           amt = sizeof (struct coff_section_tdata);
1682           csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1683           if (csect->used_by_bfd == NULL)
1684             goto error_return;
1685           amt = sizeof (struct xcoff_section_tdata);
1686           coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1687           if (coff_section_data (abfd, csect)->tdata == NULL)
1688             goto error_return;
1689           xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1690
1691           if (first_csect == NULL)
1692             first_csect = csect;
1693
1694           if (sym.n_sclass == C_EXT)
1695             {
1696               csect->flags |= SEC_IS_COMMON;
1697               csect->_raw_size = 0;
1698               section = csect;
1699               value = aux.x_csect.x_scnlen.l;
1700             }
1701
1702           break;
1703         }
1704
1705       /* Check for magic symbol names.  */
1706       if ((smtyp == XTY_SD || smtyp == XTY_CM)
1707           && aux.x_csect.x_smclas != XMC_TC
1708           && aux.x_csect.x_smclas != XMC_TD)
1709         {
1710
1711           int i = -1;
1712
1713           if (name[0] == '_')
1714             {
1715               if (strcmp (name, "_text") == 0)
1716                 i = XCOFF_SPECIAL_SECTION_TEXT;
1717               else if (strcmp (name, "_etext") == 0)
1718                 i = XCOFF_SPECIAL_SECTION_ETEXT;
1719               else if (strcmp (name, "_data") == 0)
1720                 i = XCOFF_SPECIAL_SECTION_DATA;
1721               else if (strcmp (name, "_edata") == 0)
1722                 i = XCOFF_SPECIAL_SECTION_EDATA;
1723               else if (strcmp (name, "_end") == 0)
1724                 i = XCOFF_SPECIAL_SECTION_END;
1725             }
1726           else if (name[0] == 'e' && strcmp (name, "end") == 0)
1727             {
1728               i = XCOFF_SPECIAL_SECTION_END2;
1729             }
1730
1731           if (i != -1)
1732             {
1733               xcoff_hash_table (info)->special_sections[i] = csect;
1734             }
1735         }
1736
1737       /* Now we have enough information to add the symbol to the
1738          linker hash table.  */
1739
1740       if (sym.n_sclass == C_EXT)
1741         {
1742           boolean copy;
1743
1744           BFD_ASSERT (section != NULL);
1745
1746           /* We must copy the name into memory if we got it from the
1747              syment itself, rather than the string table.  */
1748           copy = default_copy;
1749           if (sym._n._n_n._n_zeroes != 0
1750               || sym._n._n_n._n_offset == 0)
1751             copy = true;
1752
1753           /* The AIX linker appears to only detect multiple symbol
1754              definitions when there is a reference to the symbol.  If
1755              a symbol is defined multiple times, and the only
1756              references are from the same object file, the AIX linker
1757              appears to permit it.  It does not merge the different
1758              definitions, but handles them independently.  On the
1759              other hand, if there is a reference, the linker reports
1760              an error.
1761
1762              This matters because the AIX <net/net_globals.h> header
1763              file actually defines an initialized array, so we have to
1764              actually permit that to work.
1765
1766              Just to make matters even more confusing, the AIX linker
1767              appears to permit multiple symbol definitions whenever
1768              the second definition is in an archive rather than an
1769              object file.  This may be a consequence of the manner in
1770              which it handles archives: I think it may load the entire
1771              archive in as separate csects, and then let garbage
1772              collection discard symbols.
1773
1774              We also have to handle the case of statically linking a
1775              shared object, which will cause symbol redefinitions,
1776              although this is an easier case to detect.  */
1777
1778           if (info->hash->creator == abfd->xvec)
1779             {
1780               if (! bfd_is_und_section (section))
1781                 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1782                                                     name, true, copy, false);
1783               else
1784                 *sym_hash = ((struct xcoff_link_hash_entry *)
1785                              bfd_wrapped_link_hash_lookup (abfd, info, name,
1786                                                            true, copy, false));
1787               if (*sym_hash == NULL)
1788                 goto error_return;
1789               if (((*sym_hash)->root.type == bfd_link_hash_defined
1790                    || (*sym_hash)->root.type == bfd_link_hash_defweak)
1791                   && ! bfd_is_und_section (section)
1792                   && ! bfd_is_com_section (section))
1793                 {
1794                   /* This is a second definition of a defined symbol.  */
1795                   if ((abfd->flags & DYNAMIC) != 0
1796                       && ((*sym_hash)->smclas != XMC_GL
1797                           || aux.x_csect.x_smclas == XMC_GL
1798                           || ((*sym_hash)->root.u.def.section->owner->flags
1799                               & DYNAMIC) == 0))
1800                     {
1801                       /* The new symbol is from a shared library, and
1802                          either the existing symbol is not global
1803                          linkage code or this symbol is global linkage
1804                          code.  If the existing symbol is global
1805                          linkage code and the new symbol is not, then
1806                          we want to use the new symbol.  */
1807                       section = bfd_und_section_ptr;
1808                       value = 0;
1809                     }
1810                   else if (((*sym_hash)->root.u.def.section->owner->flags
1811                             & DYNAMIC) != 0)
1812                     {
1813                       /* The existing symbol is from a shared library.
1814                          Replace it.  */
1815                       (*sym_hash)->root.type = bfd_link_hash_undefined;
1816                       (*sym_hash)->root.u.undef.abfd =
1817                         (*sym_hash)->root.u.def.section->owner;
1818                     }
1819                   else if (abfd->my_archive != NULL)
1820                     {
1821                       /* This is a redefinition in an object contained
1822                          in an archive.  Just ignore it.  See the
1823                          comment above.  */
1824                       section = bfd_und_section_ptr;
1825                       value = 0;
1826                     }
1827                   else if ((*sym_hash)->root.next != NULL
1828                            || info->hash->undefs_tail == &(*sym_hash)->root)
1829                     {
1830                       /* This symbol has been referenced.  In this
1831                          case, we just continue and permit the
1832                          multiple definition error.  See the comment
1833                          above about the behaviour of the AIX linker.  */
1834                     }
1835                   else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1836                     {
1837                       /* The symbols are both csects of the same
1838                          class.  There is at least a chance that this
1839                          is a semi-legitimate redefinition.  */
1840                       section = bfd_und_section_ptr;
1841                       value = 0;
1842                       (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1843                     }
1844                 }
1845               else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1846                        && ((*sym_hash)->root.type == bfd_link_hash_defined
1847                            || (*sym_hash)->root.type == bfd_link_hash_defweak)
1848                        && (bfd_is_und_section (section)
1849                            || bfd_is_com_section (section)))
1850                 {
1851                   /* This is a reference to a multiply defined symbol.
1852                      Report the error now.  See the comment above
1853                      about the behaviour of the AIX linker.  We could
1854                      also do this with warning symbols, but I'm not
1855                      sure the XCOFF linker is wholly prepared to
1856                      handle them, and that would only be a warning,
1857                      not an error.  */
1858                   if (! ((*info->callbacks->multiple_definition)
1859                          (info, (*sym_hash)->root.root.string,
1860                           (bfd *) NULL, (asection *) NULL, (bfd_vma) 0,
1861                           (*sym_hash)->root.u.def.section->owner,
1862                           (*sym_hash)->root.u.def.section,
1863                           (*sym_hash)->root.u.def.value)))
1864                     goto error_return;
1865                   /* Try not to give this error too many times.  */
1866                   (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1867                 }
1868             }
1869
1870           /* _bfd_generic_link_add_one_symbol may call the linker to
1871              generate an error message, and the linker may try to read
1872              the symbol table to give a good error.  Right now, the
1873              line numbers are in an inconsistent state, since they are
1874              counted both in the real sections and in the new csects.
1875              We need to leave the count in the real sections so that
1876              the linker can report the line number of the error
1877              correctly, so temporarily clobber the link to the csects
1878              so that the linker will not try to read the line numbers
1879              a second time from the csects.  */
1880           BFD_ASSERT (last_real->next == first_csect);
1881           last_real->next = NULL;
1882           if (! (_bfd_generic_link_add_one_symbol
1883                  (info, abfd, name, flags, section, value,
1884                   (const char *) NULL, copy, true,
1885                   (struct bfd_link_hash_entry **) sym_hash)))
1886             goto error_return;
1887           last_real->next = first_csect;
1888
1889           if (smtyp == XTY_CM)
1890             {
1891               if ((*sym_hash)->root.type != bfd_link_hash_common
1892                   || (*sym_hash)->root.u.c.p->section != csect)
1893                 {
1894                   /* We don't need the common csect we just created.  */
1895                   csect->_raw_size = 0;
1896                 }
1897               else
1898                 {
1899                   (*sym_hash)->root.u.c.p->alignment_power
1900                     = csect->alignment_power;
1901                 }
1902             }
1903
1904           if (info->hash->creator == abfd->xvec)
1905             {
1906               int flag;
1907
1908               if (smtyp == XTY_ER || smtyp == XTY_CM)
1909                 flag = XCOFF_REF_REGULAR;
1910               else
1911                 flag = XCOFF_DEF_REGULAR;
1912               (*sym_hash)->flags |= flag;
1913
1914               if ((*sym_hash)->smclas == XMC_UA
1915                   || flag == XCOFF_DEF_REGULAR)
1916                 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1917             }
1918         }
1919
1920       *csect_cache = csect;
1921
1922       esym += (sym.n_numaux + 1) * symesz;
1923       sym_hash += sym.n_numaux + 1;
1924       csect_cache += sym.n_numaux + 1;
1925     }
1926
1927   BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1928
1929   /* Make sure that we have seen all the relocs.  */
1930   for (o = abfd->sections; o != first_csect; o = o->next)
1931     {
1932       /* Reset the section size and the line number count, since the
1933          data is now attached to the csects.  Don't reset the size of
1934          the .debug section, since we need to read it below in
1935          bfd_xcoff_size_dynamic_sections.  */
1936       if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1937         o->_raw_size = 0;
1938       o->lineno_count = 0;
1939
1940       if ((o->flags & SEC_RELOC) != 0)
1941         {
1942           bfd_size_type i;
1943           struct internal_reloc *rel;
1944           asection **rel_csect;
1945
1946           rel = reloc_info[o->target_index].relocs;
1947           rel_csect = reloc_info[o->target_index].csects;
1948
1949           for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1950             {
1951
1952               if (*rel_csect == NULL)
1953                 {
1954                   (*_bfd_error_handler)
1955                     (_("%s: reloc %s:%d not in csect"),
1956                      bfd_archive_filename (abfd), o->name, i);
1957                   bfd_set_error (bfd_error_bad_value);
1958                   goto error_return;
1959                 }
1960
1961               /* We identify all symbols which are called, so that we
1962                  can create glue code for calls to functions imported
1963                  from dynamic objects.  */
1964               if (info->hash->creator == abfd->xvec
1965                   && *rel_csect != bfd_und_section_ptr
1966                   && (rel->r_type == R_BR
1967                       || rel->r_type == R_RBR)
1968                   && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1969                 {
1970                   struct xcoff_link_hash_entry *h;
1971
1972                   h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1973                   h->flags |= XCOFF_CALLED;
1974                   /* If the symbol name starts with a period, it is
1975                      the code of a function.  If the symbol is
1976                      currently undefined, then add an undefined symbol
1977                      for the function descriptor.  This should do no
1978                      harm, because any regular object that defines the
1979                      function should also define the function
1980                      descriptor.  It helps, because it means that we
1981                      will identify the function descriptor with a
1982                      dynamic object if a dynamic object defines it.  */
1983                   if (h->root.root.string[0] == '.'
1984                       && h->descriptor == NULL)
1985                     {
1986                       struct xcoff_link_hash_entry *hds;
1987
1988                       hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1989                                                     h->root.root.string + 1,
1990                                                     true, false, true);
1991                       if (hds == NULL)
1992                         goto error_return;
1993                       if (hds->root.type == bfd_link_hash_new)
1994                         {
1995                           if (! (_bfd_generic_link_add_one_symbol
1996                                  (info, abfd, hds->root.root.string,
1997                                   (flagword) 0, bfd_und_section_ptr,
1998                                   (bfd_vma) 0, (const char *) NULL, false,
1999                                   true,
2000                                   (struct bfd_link_hash_entry **) &hds)))
2001                             goto error_return;
2002                         }
2003                       hds->flags |= XCOFF_DESCRIPTOR;
2004                       BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2005                                   && (h->flags & XCOFF_DESCRIPTOR) == 0);
2006                       hds->descriptor = h;
2007                       h->descriptor = hds;
2008                     }
2009                 }
2010             }
2011
2012           free (reloc_info[o->target_index].csects);
2013           reloc_info[o->target_index].csects = NULL;
2014
2015           /* Reset SEC_RELOC and the reloc_count, since the reloc
2016              information is now attached to the csects.  */
2017           o->flags &=~ SEC_RELOC;
2018           o->reloc_count = 0;
2019
2020           /* If we are not keeping memory, free the reloc information.  */
2021           if (! info->keep_memory
2022               && coff_section_data (abfd, o) != NULL
2023               && coff_section_data (abfd, o)->relocs != NULL
2024               && ! coff_section_data (abfd, o)->keep_relocs)
2025             {
2026               free (coff_section_data (abfd, o)->relocs);
2027               coff_section_data (abfd, o)->relocs = NULL;
2028             }
2029         }
2030
2031       /* Free up the line numbers.  FIXME: We could cache these
2032          somewhere for the final link, to avoid reading them again.  */
2033       if (reloc_info[o->target_index].linenos != NULL)
2034         {
2035           free (reloc_info[o->target_index].linenos);
2036           reloc_info[o->target_index].linenos = NULL;
2037         }
2038     }
2039
2040   free (reloc_info);
2041
2042   obj_coff_keep_syms (abfd) = keep_syms;
2043
2044   return true;
2045
2046  error_return:
2047   if (reloc_info != NULL)
2048     {
2049       for (o = abfd->sections; o != NULL; o = o->next)
2050         {
2051           if (reloc_info[o->target_index].csects != NULL)
2052             free (reloc_info[o->target_index].csects);
2053           if (reloc_info[o->target_index].linenos != NULL)
2054             free (reloc_info[o->target_index].linenos);
2055         }
2056       free (reloc_info);
2057     }
2058   obj_coff_keep_syms (abfd) = keep_syms;
2059   return false;
2060 }
2061
2062 #undef N_TMASK
2063 #undef N_BTSHFT
2064
2065 /* This function is used to add symbols from a dynamic object to the
2066    global symbol table.  */
2067
2068 static boolean
2069 xcoff_link_add_dynamic_symbols (abfd, info)
2070      bfd *abfd;
2071      struct bfd_link_info *info;
2072 {
2073   asection *lsec;
2074   bfd_byte *contents;
2075   struct internal_ldhdr ldhdr;
2076   const char *strings;
2077   bfd_byte *elsym, *elsymend;
2078   struct xcoff_import_file *n;
2079   const char *bname;
2080   const char *mname;
2081   const char *s;
2082   unsigned int c;
2083   struct xcoff_import_file **pp;
2084
2085   /* We can only handle a dynamic object if we are generating an XCOFF
2086      output file.  */
2087    if (info->hash->creator != abfd->xvec)
2088     {
2089       (*_bfd_error_handler)
2090         (_("%s: XCOFF shared object when not producing XCOFF output"),
2091          bfd_get_filename (abfd));
2092       bfd_set_error (bfd_error_invalid_operation);
2093       return false;
2094     }
2095
2096   /* The symbols we use from a dynamic object are not the symbols in
2097      the normal symbol table, but, rather, the symbols in the export
2098      table.  If there is a global symbol in a dynamic object which is
2099      not in the export table, the loader will not be able to find it,
2100      so we don't want to find it either.  Also, on AIX 4.1.3, shr.o in
2101      libc.a has symbols in the export table which are not in the
2102      symbol table.  */
2103
2104   /* Read in the .loader section.  FIXME: We should really use the
2105      o_snloader field in the a.out header, rather than grabbing the
2106      section by name.  */
2107   lsec = bfd_get_section_by_name (abfd, ".loader");
2108   if (lsec == NULL)
2109     {
2110       (*_bfd_error_handler)
2111         (_("%s: dynamic object with no .loader section"),
2112          bfd_get_filename (abfd));
2113       bfd_set_error (bfd_error_no_symbols);
2114       return false;
2115     }
2116
2117
2118   if (! xcoff_get_section_contents (abfd, lsec))
2119     return false;
2120   contents = coff_section_data (abfd, lsec)->contents;
2121
2122   /* Remove the sections from this object, so that they do not get
2123      included in the link.  */
2124   bfd_section_list_clear (abfd);
2125
2126   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2127
2128   strings = (char *) contents + ldhdr.l_stoff;
2129
2130   elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
2131
2132   elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
2133
2134   for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
2135     {
2136       struct internal_ldsym ldsym;
2137       char nambuf[SYMNMLEN + 1];
2138       const char *name;
2139       struct xcoff_link_hash_entry *h;
2140
2141       bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2142
2143       /* We are only interested in exported symbols.  */
2144       if ((ldsym.l_smtype & L_EXPORT) == 0)
2145         continue;
2146
2147       if (ldsym._l._l_l._l_zeroes == 0)
2148         name = strings + ldsym._l._l_l._l_offset;
2149       else
2150         {
2151           memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2152           nambuf[SYMNMLEN] = '\0';
2153           name = nambuf;
2154         }
2155
2156       /* Normally we could not call xcoff_link_hash_lookup in an add
2157          symbols routine, since we might not be using an XCOFF hash
2158          table.  However, we verified above that we are using an XCOFF
2159          hash table.  */
2160
2161       h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2162                                   true, true);
2163       if (h == NULL)
2164         return false;
2165
2166       h->flags |= XCOFF_DEF_DYNAMIC;
2167
2168       /* If the symbol is undefined, and the BFD it was found in is
2169          not a dynamic object, change the BFD to this dynamic object,
2170          so that we can get the correct import file ID.  */
2171       if ((h->root.type == bfd_link_hash_undefined
2172            || h->root.type == bfd_link_hash_undefweak)
2173           && (h->root.u.undef.abfd == NULL
2174               || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2175         h->root.u.undef.abfd = abfd;
2176
2177       if (h->root.type == bfd_link_hash_new)
2178         {
2179           h->root.type = bfd_link_hash_undefined;
2180           h->root.u.undef.abfd = abfd;
2181           /* We do not want to add this to the undefined symbol list.  */
2182         }
2183
2184       if (h->smclas == XMC_UA
2185           || h->root.type == bfd_link_hash_undefined
2186           || h->root.type == bfd_link_hash_undefweak)
2187         h->smclas = ldsym.l_smclas;
2188
2189       /* Unless this is an XMC_XO symbol, we don't bother to actually
2190          define it, since we don't have a section to put it in anyhow.
2191          Instead, the relocation routines handle the DEF_DYNAMIC flag
2192          correctly.  */
2193
2194       if (h->smclas == XMC_XO
2195           && (h->root.type == bfd_link_hash_undefined
2196               || h->root.type == bfd_link_hash_undefweak))
2197         {
2198           /* This symbol has an absolute value.  */
2199           h->root.type = bfd_link_hash_defined;
2200           h->root.u.def.section = bfd_abs_section_ptr;
2201           h->root.u.def.value = ldsym.l_value;
2202         }
2203
2204       /* If this symbol defines a function descriptor, then it
2205          implicitly defines the function code as well.  */
2206       if (h->smclas == XMC_DS
2207           || (h->smclas == XMC_XO && name[0] != '.'))
2208         h->flags |= XCOFF_DESCRIPTOR;
2209       if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2210         {
2211           struct xcoff_link_hash_entry *hds;
2212
2213           hds = h->descriptor;
2214           if (hds == NULL)
2215             {
2216               char *dsnm;
2217
2218               dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
2219               if (dsnm == NULL)
2220                 return false;
2221               dsnm[0] = '.';
2222               strcpy (dsnm + 1, name);
2223               hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2224                                             true, true, true);
2225               free (dsnm);
2226               if (hds == NULL)
2227                 return false;
2228
2229               if (hds->root.type == bfd_link_hash_new)
2230                 {
2231                   hds->root.type = bfd_link_hash_undefined;
2232                   hds->root.u.undef.abfd = abfd;
2233                   /* We do not want to add this to the undefined
2234                      symbol list.  */
2235                 }
2236
2237               hds->descriptor = h;
2238               h->descriptor = hds;
2239             }
2240
2241           hds->flags |= XCOFF_DEF_DYNAMIC;
2242           if (hds->smclas == XMC_UA)
2243             hds->smclas = XMC_PR;
2244
2245           /* An absolute symbol appears to actually define code, not a
2246              function descriptor.  This is how some math functions are
2247              implemented on AIX 4.1.  */
2248           if (h->smclas == XMC_XO
2249               && (hds->root.type == bfd_link_hash_undefined
2250                   || hds->root.type == bfd_link_hash_undefweak))
2251             {
2252               hds->smclas = XMC_XO;
2253               hds->root.type = bfd_link_hash_defined;
2254               hds->root.u.def.section = bfd_abs_section_ptr;
2255               hds->root.u.def.value = ldsym.l_value;
2256             }
2257         }
2258     }
2259
2260   if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2261     {
2262       free (coff_section_data (abfd, lsec)->contents);
2263       coff_section_data (abfd, lsec)->contents = NULL;
2264     }
2265
2266   /* Record this file in the import files.  */
2267
2268   n = ((struct xcoff_import_file *)
2269        bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file)));
2270   if (n == NULL)
2271     return false;
2272   n->next = NULL;
2273
2274   /* For some reason, the path entry in the import file list for a
2275      shared object appears to always be empty.  The file name is the
2276      base name.  */
2277   n->path = "";
2278   if (abfd->my_archive == NULL)
2279     {
2280       bname = bfd_get_filename (abfd);
2281       mname = "";
2282     }
2283   else
2284     {
2285       bname = bfd_get_filename (abfd->my_archive);
2286       mname = bfd_get_filename (abfd);
2287     }
2288   s = strrchr (bname, '/');
2289   if (s != NULL)
2290     bname = s + 1;
2291   n->file = bname;
2292   n->member = mname;
2293
2294   /* We start c at 1 because the first import file number is reserved
2295      for LIBPATH.  */
2296   for (pp = &xcoff_hash_table (info)->imports, c = 1;
2297        *pp != NULL;
2298        pp = &(*pp)->next, ++c)
2299     ;
2300   *pp = n;
2301
2302   xcoff_data (abfd)->import_file_id = c;
2303
2304   return true;
2305 }
2306 \f
2307 /* Routines that are called after all the input files have been
2308    handled, but before the sections are laid out in memory.  */
2309
2310 /* Mark a symbol as not being garbage, including the section in which
2311    it is defined.  */
2312
2313 static INLINE boolean
2314 xcoff_mark_symbol (info, h)
2315      struct bfd_link_info *info;
2316      struct xcoff_link_hash_entry *h;
2317 {
2318
2319   if ((h->flags & XCOFF_MARK) != 0)
2320     return true;
2321
2322   h->flags |= XCOFF_MARK;
2323   if (h->root.type == bfd_link_hash_defined
2324       || h->root.type == bfd_link_hash_defweak)
2325     {
2326       asection *hsec;
2327
2328       hsec = h->root.u.def.section;
2329       if (! bfd_is_abs_section (hsec)
2330           && (hsec->flags & SEC_MARK) == 0)
2331         {
2332           if (! xcoff_mark (info, hsec))
2333             return false;
2334         }
2335     }
2336
2337   if (h->toc_section != NULL
2338       && (h->toc_section->flags & SEC_MARK) == 0)
2339     {
2340       if (! xcoff_mark (info, h->toc_section))
2341         return false;
2342     }
2343
2344   return true;
2345 }
2346
2347 /* The mark phase of garbage collection.  For a given section, mark
2348    it, and all the sections which define symbols to which it refers.
2349    Because this function needs to look at the relocs, we also count
2350    the number of relocs which need to be copied into the .loader
2351    section.  */
2352
2353 static boolean
2354 xcoff_mark (info, sec)
2355      struct bfd_link_info *info;
2356      asection *sec;
2357 {
2358   if (bfd_is_abs_section (sec)
2359       || (sec->flags & SEC_MARK) != 0)
2360     return true;
2361
2362   sec->flags |= SEC_MARK;
2363
2364   if (sec->owner->xvec == info->hash->creator
2365       && coff_section_data (sec->owner, sec) != NULL
2366       && xcoff_section_data (sec->owner, sec) != NULL)
2367     {
2368       register struct xcoff_link_hash_entry **hp, **hpend;
2369       struct internal_reloc *rel, *relend;
2370
2371       /* Mark all the symbols in this section.  */
2372
2373       hp = (obj_xcoff_sym_hashes (sec->owner)
2374             + xcoff_section_data (sec->owner, sec)->first_symndx);
2375       hpend = (obj_xcoff_sym_hashes (sec->owner)
2376                + xcoff_section_data (sec->owner, sec)->last_symndx);
2377       for (; hp < hpend; hp++)
2378         {
2379           register struct xcoff_link_hash_entry *h;
2380
2381           h = *hp;
2382           if (h != NULL
2383               && (h->flags & XCOFF_MARK) == 0)
2384             {
2385               if (! xcoff_mark_symbol (info, h))
2386                 return false;
2387             }
2388         }
2389
2390       /* Look through the section relocs.  */
2391
2392       if ((sec->flags & SEC_RELOC) != 0
2393           && sec->reloc_count > 0)
2394         {
2395           rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2396                                             (bfd_byte *) NULL, false,
2397                                             (struct internal_reloc *) NULL);
2398           if (rel == NULL)
2399             return false;
2400           relend = rel + sec->reloc_count;
2401           for (; rel < relend; rel++)
2402             {
2403               asection *rsec;
2404               struct xcoff_link_hash_entry *h;
2405
2406               if ((unsigned int) rel->r_symndx
2407                   > obj_raw_syment_count (sec->owner))
2408                 continue;
2409
2410               h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2411               if (h != NULL
2412                   && (h->flags & XCOFF_MARK) == 0)
2413                 {
2414                   if (! xcoff_mark_symbol (info, h))
2415                     return false;
2416                 }
2417
2418               rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2419               if (rsec != NULL
2420                   && (rsec->flags & SEC_MARK) == 0)
2421                 {
2422                   if (! xcoff_mark (info, rsec))
2423                     return false;
2424                 }
2425
2426               /* See if this reloc needs to be copied into the .loader
2427                  section.  */
2428               switch (rel->r_type)
2429                 {
2430                 default:
2431                   if (h == NULL
2432                       || h->root.type == bfd_link_hash_defined
2433                       || h->root.type == bfd_link_hash_defweak
2434                       || h->root.type == bfd_link_hash_common
2435                       || ((h->flags & XCOFF_CALLED) != 0
2436                           && (h->root.type == bfd_link_hash_undefined
2437                               || h->root.type == bfd_link_hash_undefweak)
2438                           && h->root.root.string[0] == '.'
2439                           && h->descriptor != NULL
2440                           && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2441                               || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2442                                   && (h->descriptor->flags
2443                                       & XCOFF_DEF_REGULAR) == 0))))
2444                     break;
2445                   /* Fall through.  */
2446                 case R_POS:
2447                 case R_NEG:
2448                 case R_RL:
2449                 case R_RLA:
2450                   ++xcoff_hash_table (info)->ldrel_count;
2451                   if (h != NULL)
2452                     h->flags |= XCOFF_LDREL;
2453                   break;
2454                 case R_TOC:
2455                 case R_GL:
2456                 case R_TCL:
2457                 case R_TRL:
2458                 case R_TRLA:
2459                   /* We should never need a .loader reloc for a TOC
2460                      relative reloc.  */
2461                   break;
2462                 }
2463             }
2464
2465           if (! info->keep_memory
2466               && coff_section_data (sec->owner, sec) != NULL
2467               && coff_section_data (sec->owner, sec)->relocs != NULL
2468               && ! coff_section_data (sec->owner, sec)->keep_relocs)
2469             {
2470               free (coff_section_data (sec->owner, sec)->relocs);
2471               coff_section_data (sec->owner, sec)->relocs = NULL;
2472             }
2473         }
2474     }
2475
2476   return true;
2477 }
2478
2479 /* The sweep phase of garbage collection.  Remove all garbage
2480    sections.  */
2481
2482 static void
2483 xcoff_sweep (info)
2484      struct bfd_link_info *info;
2485 {
2486   bfd *sub;
2487
2488   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2489     {
2490       asection *o;
2491
2492       for (o = sub->sections; o != NULL; o = o->next)
2493         {
2494           if ((o->flags & SEC_MARK) == 0)
2495             {
2496               /* Keep all sections from non-XCOFF input files.  Keep
2497                  special sections.  Keep .debug sections for the
2498                  moment.  */
2499               if (sub->xvec != info->hash->creator
2500                   || o == xcoff_hash_table (info)->debug_section
2501                   || o == xcoff_hash_table (info)->loader_section
2502                   || o == xcoff_hash_table (info)->linkage_section
2503                   || o == xcoff_hash_table (info)->toc_section
2504                   || o == xcoff_hash_table (info)->descriptor_section
2505                   || strcmp (o->name, ".debug") == 0)
2506                 o->flags |= SEC_MARK;
2507               else
2508                 {
2509                   o->_raw_size = 0;
2510                   o->reloc_count = 0;
2511                   o->lineno_count = 0;
2512                 }
2513             }
2514         }
2515     }
2516 }
2517
2518 /* Record the number of elements in a set.  This is used to output the
2519    correct csect length.  */
2520
2521 boolean
2522 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2523      bfd *output_bfd;
2524      struct bfd_link_info *info;
2525      struct bfd_link_hash_entry *harg;
2526      bfd_size_type size;
2527 {
2528   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2529   struct xcoff_link_size_list *n;
2530   bfd_size_type amt;
2531
2532   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2533     return true;
2534
2535   /* This will hardly ever be called.  I don't want to burn four bytes
2536      per global symbol, so instead the size is kept on a linked list
2537      attached to the hash table.  */
2538
2539   amt = sizeof (struct xcoff_link_size_list);
2540   n = (struct xcoff_link_size_list *) bfd_alloc (output_bfd, amt);
2541   if (n == NULL)
2542     return false;
2543   n->next = xcoff_hash_table (info)->size_list;
2544   n->h = h;
2545   n->size = size;
2546   xcoff_hash_table (info)->size_list = n;
2547
2548   h->flags |= XCOFF_HAS_SIZE;
2549
2550   return true;
2551 }
2552
2553 /* Import a symbol.  */
2554
2555 boolean
2556 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2557                          impmember, syscall_flag)
2558      bfd *output_bfd;
2559      struct bfd_link_info *info;
2560      struct bfd_link_hash_entry *harg;
2561      bfd_vma val;
2562      const char *imppath;
2563      const char *impfile;
2564      const char *impmember;
2565      unsigned int syscall_flag;
2566 {
2567   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2568
2569   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2570     return true;
2571
2572   /* A symbol name which starts with a period is the code for a
2573      function.  If the symbol is undefined, then add an undefined
2574      symbol for the function descriptor, and import that instead.  */
2575   if (h->root.root.string[0] == '.'
2576       && h->root.type == bfd_link_hash_undefined
2577       && val == (bfd_vma) -1)
2578     {
2579       struct xcoff_link_hash_entry *hds;
2580
2581       hds = h->descriptor;
2582       if (hds == NULL)
2583         {
2584           hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2585                                         h->root.root.string + 1,
2586                                         true, false, true);
2587           if (hds == NULL)
2588             return false;
2589           if (hds->root.type == bfd_link_hash_new)
2590             {
2591               hds->root.type = bfd_link_hash_undefined;
2592               hds->root.u.undef.abfd = h->root.u.undef.abfd;
2593             }
2594           hds->flags |= XCOFF_DESCRIPTOR;
2595           BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2596                       && (h->flags & XCOFF_DESCRIPTOR) == 0);
2597           hds->descriptor = h;
2598           h->descriptor = hds;
2599         }
2600
2601       /* Now, if the descriptor is undefined, import the descriptor
2602          rather than the symbol we were told to import.  FIXME: Is
2603          this correct in all cases?  */
2604       if (hds->root.type == bfd_link_hash_undefined)
2605         h = hds;
2606     }
2607
2608   h->flags |= (XCOFF_IMPORT | syscall_flag);
2609
2610   if (val != (bfd_vma) -1)
2611     {
2612       if (h->root.type == bfd_link_hash_defined
2613           && (! bfd_is_abs_section (h->root.u.def.section)
2614               || h->root.u.def.value != val))
2615         {
2616           if (! ((*info->callbacks->multiple_definition)
2617                  (info, h->root.root.string, h->root.u.def.section->owner,
2618                   h->root.u.def.section, h->root.u.def.value,
2619                   output_bfd, bfd_abs_section_ptr, val)))
2620             return false;
2621         }
2622
2623       h->root.type = bfd_link_hash_defined;
2624       h->root.u.def.section = bfd_abs_section_ptr;
2625       h->root.u.def.value = val;
2626     }
2627
2628   /* We overload the ldindx field to hold the l_ifile value for this
2629      symbol.  */
2630   BFD_ASSERT (h->ldsym == NULL);
2631   BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2632   if (imppath == NULL)
2633     h->ldindx = -1;
2634   else
2635     {
2636       unsigned int c;
2637       struct xcoff_import_file **pp;
2638
2639       /* We start c at 1 because the first entry in the import list is
2640          reserved for the library search path.  */
2641       for (pp = &xcoff_hash_table (info)->imports, c = 1;
2642            *pp != NULL;
2643            pp = &(*pp)->next, ++c)
2644         {
2645           if (strcmp ((*pp)->path, imppath) == 0
2646               && strcmp ((*pp)->file, impfile) == 0
2647               && strcmp ((*pp)->member, impmember) == 0)
2648             break;
2649         }
2650
2651       if (*pp == NULL)
2652         {
2653           struct xcoff_import_file *n;
2654           bfd_size_type amt = sizeof (struct xcoff_import_file);
2655
2656           n = (struct xcoff_import_file *) bfd_alloc (output_bfd, amt);
2657           if (n == NULL)
2658             return false;
2659           n->next = NULL;
2660           n->path = imppath;
2661           n->file = impfile;
2662           n->member = impmember;
2663           *pp = n;
2664         }
2665
2666       h->ldindx = c;
2667     }
2668
2669   return true;
2670 }
2671
2672 /* Export a symbol.  */
2673
2674 boolean
2675 bfd_xcoff_export_symbol (output_bfd, info, harg)
2676      bfd *output_bfd;
2677      struct bfd_link_info *info;
2678      struct bfd_link_hash_entry *harg;
2679 {
2680   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2681
2682   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2683     return true;
2684
2685   h->flags |= XCOFF_EXPORT;
2686
2687   /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2688      I'm just going to ignore it until somebody explains it.  */
2689
2690   /* See if this is a function descriptor.  It may be one even though
2691      it is not so marked.  */
2692   if ((h->flags & XCOFF_DESCRIPTOR) == 0
2693       && h->root.root.string[0] != '.')
2694     {
2695       char *fnname;
2696       struct xcoff_link_hash_entry *hfn;
2697       bfd_size_type amt = strlen (h->root.root.string) + 2;
2698
2699       fnname = (char *) bfd_malloc (amt);
2700       if (fnname == NULL)
2701         return false;
2702       fnname[0] = '.';
2703       strcpy (fnname + 1, h->root.root.string);
2704       hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2705                                     fnname, false, false, true);
2706       free (fnname);
2707       if (hfn != NULL
2708           && hfn->smclas == XMC_PR
2709           && (hfn->root.type == bfd_link_hash_defined
2710               || hfn->root.type == bfd_link_hash_defweak))
2711         {
2712           h->flags |= XCOFF_DESCRIPTOR;
2713           h->descriptor = hfn;
2714           hfn->descriptor = h;
2715         }
2716     }
2717
2718   /* Make sure we don't garbage collect this symbol.  */
2719   if (! xcoff_mark_symbol (info, h))
2720     return false;
2721
2722   /* If this is a function descriptor, make sure we don't garbage
2723      collect the associated function code.  We normally don't have to
2724      worry about this, because the descriptor will be attached to a
2725      section with relocs, but if we are creating the descriptor
2726      ourselves those relocs will not be visible to the mark code.  */
2727   if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2728     {
2729       if (! xcoff_mark_symbol (info, h->descriptor))
2730         return false;
2731     }
2732
2733   return true;
2734 }
2735
2736 /* Count a reloc against a symbol.  This is called for relocs
2737    generated by the linker script, typically for global constructors
2738    and destructors.  */
2739
2740 boolean
2741 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2742      bfd *output_bfd;
2743      struct bfd_link_info *info;
2744      const char *name;
2745 {
2746   struct xcoff_link_hash_entry *h;
2747
2748   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2749     return true;
2750
2751   h = ((struct xcoff_link_hash_entry *)
2752        bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
2753                                      false));
2754   if (h == NULL)
2755     {
2756       (*_bfd_error_handler) (_("%s: no such symbol"), name);
2757       bfd_set_error (bfd_error_no_symbols);
2758       return false;
2759     }
2760
2761   h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2762   ++xcoff_hash_table (info)->ldrel_count;
2763
2764   /* Mark the symbol to avoid garbage collection.  */
2765   if (! xcoff_mark_symbol (info, h))
2766     return false;
2767
2768   return true;
2769 }
2770
2771 /* This function is called for each symbol to which the linker script
2772    assigns a value.  */
2773
2774 boolean
2775 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2776      bfd *output_bfd;
2777      struct bfd_link_info *info;
2778      const char *name;
2779 {
2780   struct xcoff_link_hash_entry *h;
2781
2782   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2783     return true;
2784
2785   h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2786                               false);
2787   if (h == NULL)
2788     return false;
2789
2790   h->flags |= XCOFF_DEF_REGULAR;
2791
2792   return true;
2793 }
2794
2795 /* Build the .loader section.  This is called by the XCOFF linker
2796    emulation before_allocation routine.  We must set the size of the
2797    .loader section before the linker lays out the output file.
2798    LIBPATH is the library path to search for shared objects; this is
2799    normally built from the -L arguments passed to the linker.  ENTRY
2800    is the name of the entry point symbol (the -e linker option).
2801    FILE_ALIGN is the alignment to use for sections within the file
2802    (the -H linker option).  MAXSTACK is the maximum stack size (the
2803    -bmaxstack linker option).  MAXDATA is the maximum data size (the
2804    -bmaxdata linker option).  GC is whether to do garbage collection
2805    (the -bgc linker option).  MODTYPE is the module type (the
2806    -bmodtype linker option).  TEXTRO is whether the text section must
2807    be read only (the -btextro linker option).  EXPORT_DEFINEDS is
2808    whether all defined symbols should be exported (the -unix linker
2809    option).  SPECIAL_SECTIONS is set by this routine to csects with
2810    magic names like _end.  */
2811
2812 boolean
2813 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2814                                  file_align, maxstack, maxdata, gc,
2815                                  modtype, textro, export_defineds,
2816                                  special_sections, rtld)
2817      bfd *output_bfd;
2818      struct bfd_link_info *info;
2819      const char *libpath;
2820      const char *entry;
2821      unsigned long file_align;
2822      unsigned long maxstack;
2823      unsigned long maxdata;
2824      boolean gc;
2825      int modtype;
2826      boolean textro;
2827      boolean export_defineds;
2828      asection **special_sections;
2829      boolean rtld;
2830 {
2831   struct xcoff_link_hash_entry *hentry;
2832   asection *lsec;
2833   struct xcoff_loader_info ldinfo;
2834   int i;
2835   size_t impsize, impcount;
2836   struct xcoff_import_file *fl;
2837   struct internal_ldhdr *ldhdr;
2838   bfd_size_type stoff;
2839   register char *out;
2840   asection *sec;
2841   bfd *sub;
2842   struct bfd_strtab_hash *debug_strtab;
2843   bfd_byte *debug_contents = NULL;
2844   bfd_size_type amt;
2845
2846   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2847     {
2848       for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2849         special_sections[i] = NULL;
2850       return true;
2851     }
2852
2853   ldinfo.failed = false;
2854   ldinfo.output_bfd = output_bfd;
2855   ldinfo.info = info;
2856   ldinfo.export_defineds = export_defineds;
2857   ldinfo.ldsym_count = 0;
2858   ldinfo.string_size = 0;
2859   ldinfo.strings = NULL;
2860   ldinfo.string_alc = 0;
2861
2862   xcoff_data (output_bfd)->maxstack = maxstack;
2863   xcoff_data (output_bfd)->maxdata = maxdata;
2864   xcoff_data (output_bfd)->modtype = modtype;
2865
2866   xcoff_hash_table (info)->file_align = file_align;
2867   xcoff_hash_table (info)->textro = textro;
2868
2869   hentry = NULL;
2870   if (entry != NULL)
2871     {
2872       hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2873                                        false, false, true);
2874       if (hentry != NULL)
2875         hentry->flags |= XCOFF_ENTRY;
2876     }
2877
2878   /* __rtinit */
2879   if (info->init_function || info->fini_function || rtld == true) 
2880     {
2881       struct xcoff_link_hash_entry *hsym;
2882       struct internal_ldsym *ldsym;
2883       
2884       hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
2885                                      "__rtinit", false, false, true);
2886       if (hsym == NULL)
2887         {
2888           (*_bfd_error_handler)
2889             (_("error: undefined symbol __rtinit"));
2890           return false;
2891         }
2892       
2893       xcoff_mark_symbol (info, hsym);
2894       hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
2895
2896       /* __rtinit initalized */
2897       amt = sizeof (struct internal_ldsym);
2898       ldsym = (struct internal_ldsym *) bfd_malloc (amt);
2899       
2900       ldsym->l_value = 0;                  /* will be filled in later */
2901       ldsym->l_scnum = 2;                  /* data section */
2902       ldsym->l_smtype = XTY_SD;            /* csect section definition */
2903       ldsym->l_smclas = 5;                 /* .rw */
2904       ldsym->l_ifile = 0;                  /* special system loader symbol */
2905       ldsym->l_parm = 0;                   /* NA */
2906       
2907       /* Force __rtinit to be the first symbol in the loader symbol table
2908          See xcoff_build_ldsyms
2909          
2910          The first 3 symbol table indices are reserved to indicate the data,
2911          text and bss sections.  */
2912       BFD_ASSERT (0 == ldinfo.ldsym_count);
2913       
2914       hsym->ldindx = 3;
2915       ldinfo.ldsym_count = 1;
2916       hsym->ldsym = ldsym;
2917       
2918       if (false == bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
2919                                                 hsym->ldsym,
2920                                                 hsym->root.root.string))
2921         return false;
2922       
2923       /* This symbol is written out by xcoff_write_global_symbol
2924          Set stuff up so xcoff_write_global_symbol logic works.  */
2925       hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
2926       hsym->root.type = bfd_link_hash_defined;
2927       hsym->root.u.def.value = 0;
2928     }
2929
2930   /* Garbage collect unused sections.  */
2931   if (info->relocateable
2932       || ! gc
2933       || hentry == NULL
2934       || (hentry->root.type != bfd_link_hash_defined
2935           && hentry->root.type != bfd_link_hash_defweak))
2936     {
2937       gc = false;
2938       xcoff_hash_table (info)->gc = false;
2939
2940       /* We still need to call xcoff_mark, in order to set ldrel_count
2941          correctly.  */
2942       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2943         {
2944           asection *o;
2945
2946           for (o = sub->sections; o != NULL; o = o->next)
2947             {
2948               if ((o->flags & SEC_MARK) == 0)
2949                 {
2950                   if (! xcoff_mark (info, o))
2951                     goto error_return;
2952                 }
2953             }
2954         }
2955     }
2956   else
2957     {
2958       if (! xcoff_mark (info, hentry->root.u.def.section))
2959         goto error_return;
2960       xcoff_sweep (info);
2961       xcoff_hash_table (info)->gc = true;
2962     }
2963
2964   /* Return special sections to the caller.  */
2965   for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2966     {
2967       sec = xcoff_hash_table (info)->special_sections[i];
2968
2969       if (sec != NULL
2970           && gc
2971           && (sec->flags & SEC_MARK) == 0)
2972         {
2973           sec = NULL;
2974         }
2975       special_sections[i] = sec;
2976     }
2977
2978   if (info->input_bfds == NULL)
2979     {
2980       /* I'm not sure what to do in this bizarre case.  */
2981       return true;
2982     }
2983
2984   xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2985                             (PTR) &ldinfo);
2986   if (ldinfo.failed)
2987     goto error_return;
2988
2989   /* Work out the size of the import file names.  Each import file ID
2990      consists of three null terminated strings: the path, the file
2991      name, and the archive member name.  The first entry in the list
2992      of names is the path to use to find objects, which the linker has
2993      passed in as the libpath argument.  For some reason, the path
2994      entry in the other import file names appears to always be empty.  */
2995   impsize = strlen (libpath) + 3;
2996   impcount = 1;
2997   for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2998     {
2999       ++impcount;
3000       impsize += (strlen (fl->path)
3001                   + strlen (fl->file)
3002                   + strlen (fl->member)
3003                   + 3);
3004     }
3005
3006   /* Set up the .loader section header.  */
3007   ldhdr = &xcoff_hash_table (info)->ldhdr;
3008   ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3009   ldhdr->l_nsyms = ldinfo.ldsym_count;
3010   ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3011   ldhdr->l_istlen = impsize;
3012   ldhdr->l_nimpid = impcount;
3013   ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3014                      + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3015                      + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3016   ldhdr->l_stlen = ldinfo.string_size;
3017   stoff = ldhdr->l_impoff + impsize;
3018   if (ldinfo.string_size == 0)
3019     ldhdr->l_stoff = 0;
3020   else
3021     ldhdr->l_stoff = stoff;
3022
3023   /* 64 bit elements to ldhdr
3024      The swap out routine for 32 bit will ignore them.
3025      Nothing fancy, symbols come after the header and relocs come
3026      after symbols.  */
3027   ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3028   ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3029                      + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3030
3031   /* We now know the final size of the .loader section.  Allocate
3032      space for it.  */
3033   lsec = xcoff_hash_table (info)->loader_section;
3034   lsec->_raw_size = stoff + ldhdr->l_stlen;
3035   lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3036   if (lsec->contents == NULL)
3037     goto error_return;
3038
3039   /* Set up the header.  */
3040   bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3041
3042   /* Set up the import file names.  */
3043   out = (char *) lsec->contents + ldhdr->l_impoff;
3044   strcpy (out, libpath);
3045   out += strlen (libpath) + 1;
3046   *out++ = '\0';
3047   *out++ = '\0';
3048   for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3049     {
3050       register const char *s;
3051
3052       s = fl->path;
3053       while ((*out++ = *s++) != '\0')
3054         ;
3055       s = fl->file;
3056       while ((*out++ = *s++) != '\0')
3057         ;
3058       s = fl->member;
3059       while ((*out++ = *s++) != '\0')
3060         ;
3061     }
3062
3063   BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3064
3065   /* Set up the symbol string table.  */
3066   if (ldinfo.string_size > 0)
3067     {
3068       memcpy (out, ldinfo.strings, ldinfo.string_size);
3069       free (ldinfo.strings);
3070       ldinfo.strings = NULL;
3071     }
3072
3073   /* We can't set up the symbol table or the relocs yet, because we
3074      don't yet know the final position of the various sections.  The
3075      .loader symbols are written out when the corresponding normal
3076      symbols are written out in xcoff_link_input_bfd or
3077      xcoff_write_global_symbol.  The .loader relocs are written out
3078      when the corresponding normal relocs are handled in
3079      xcoff_link_input_bfd.
3080   */
3081
3082   /* Allocate space for the magic sections.  */
3083   sec = xcoff_hash_table (info)->linkage_section;
3084   if (sec->_raw_size > 0)
3085     {
3086       sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3087       if (sec->contents == NULL)
3088         goto error_return;
3089     }
3090   sec = xcoff_hash_table (info)->toc_section;
3091   if (sec->_raw_size > 0)
3092     {
3093       sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3094       if (sec->contents == NULL)
3095         goto error_return;
3096     }
3097   sec = xcoff_hash_table (info)->descriptor_section;
3098   if (sec->_raw_size > 0)
3099     {
3100       sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3101       if (sec->contents == NULL)
3102         goto error_return;
3103     }
3104
3105   /* Now that we've done garbage collection, figure out the contents
3106      of the .debug section.  */
3107   debug_strtab = xcoff_hash_table (info)->debug_strtab;
3108
3109   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3110     {
3111       asection *subdeb;
3112       bfd_size_type symcount;
3113       unsigned long *debug_index;
3114       asection **csectpp;
3115       bfd_byte *esym, *esymend;
3116       bfd_size_type symesz;
3117
3118       if (sub->xvec != info->hash->creator)
3119         continue;
3120       subdeb = bfd_get_section_by_name (sub, ".debug");
3121       if (subdeb == NULL || subdeb->_raw_size == 0)
3122         continue;
3123
3124       if (info->strip == strip_all
3125           || info->strip == strip_debugger
3126           || info->discard == discard_all)
3127         {
3128           subdeb->_raw_size = 0;
3129           continue;
3130         }
3131
3132       if (! _bfd_coff_get_external_symbols (sub))
3133         goto error_return;
3134
3135       symcount = obj_raw_syment_count (sub);
3136       debug_index = ((unsigned long *)
3137                      bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3138       if (debug_index == NULL)
3139         goto error_return;
3140       xcoff_data (sub)->debug_indices = debug_index;
3141
3142       /* Grab the contents of the .debug section.  We use malloc and
3143          copy the names into the debug stringtab, rather than
3144          bfd_alloc, because I expect that, when linking many files
3145          together, many of the strings will be the same.  Storing the
3146          strings in the hash table should save space in this case.  */
3147       debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3148       if (debug_contents == NULL)
3149         goto error_return;
3150       if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3151                                       (file_ptr) 0, subdeb->_raw_size))
3152         goto error_return;
3153
3154       csectpp = xcoff_data (sub)->csects;
3155
3156       /* Dynamic object do not have csectpp's.  */
3157       if (NULL != csectpp) 
3158         {
3159           symesz = bfd_coff_symesz (sub);
3160           esym = (bfd_byte *) obj_coff_external_syms (sub);
3161           esymend = esym + symcount * symesz;
3162
3163           while (esym < esymend)
3164             {
3165               struct internal_syment sym;
3166
3167               bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3168
3169               *debug_index = (unsigned long) -1;
3170
3171               if (sym._n._n_n._n_zeroes == 0
3172                   && *csectpp != NULL
3173                   && (! gc
3174                       || ((*csectpp)->flags & SEC_MARK) != 0
3175                       || *csectpp == bfd_abs_section_ptr)
3176                   && bfd_coff_symname_in_debug (sub, &sym))
3177                 {
3178                   char *name;
3179                   bfd_size_type indx;
3180
3181                   name = (char *) debug_contents + sym._n._n_n._n_offset;
3182                   indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3183                   if (indx == (bfd_size_type) -1)
3184                     goto error_return;
3185                   *debug_index = indx;
3186                 }
3187
3188               esym += (sym.n_numaux + 1) * symesz;
3189               csectpp += sym.n_numaux + 1;
3190               debug_index += sym.n_numaux + 1;
3191             }
3192         }
3193
3194       free (debug_contents);
3195       debug_contents = NULL;
3196
3197       /* Clear the size of subdeb, so that it is not included directly
3198          in the output file.  */
3199       subdeb->_raw_size = 0;
3200
3201       if (! info->keep_memory)
3202         {
3203           if (! _bfd_coff_free_symbols (sub))
3204             goto error_return;
3205         }
3206     }
3207
3208   if (info->strip != strip_all)
3209     xcoff_hash_table (info)->debug_section->_raw_size =
3210       _bfd_stringtab_size (debug_strtab);
3211
3212   return true;
3213
3214  error_return:
3215   if (ldinfo.strings != NULL)
3216     free (ldinfo.strings);
3217   if (debug_contents != NULL)
3218     free (debug_contents);
3219   return false;
3220 }
3221
3222 boolean 
3223 bfd_xcoff_link_generate_rtinit (abfd, init, fini, rtld)
3224      bfd *abfd;
3225      const char *init;
3226      const char *fini;
3227      boolean rtld;
3228 {
3229   struct bfd_in_memory *bim;
3230   
3231   bim = ((struct bfd_in_memory *)
3232          bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
3233   if (bim == NULL)
3234     return false;
3235
3236   bim->size = 0;
3237   bim->buffer = 0;
3238
3239   abfd->link_next = 0;
3240   abfd->format = bfd_object;
3241   abfd->iostream = (PTR) bim;
3242   abfd->flags = BFD_IN_MEMORY;
3243   abfd->direction = write_direction;
3244   abfd->where = 0;
3245
3246   if (false == bfd_xcoff_generate_rtinit (abfd, init, fini, rtld)) 
3247     return false;
3248
3249   /* need to reset to unknown or it will not be read back in correctly */
3250   abfd->format = bfd_unknown;
3251   abfd->direction = read_direction;
3252   abfd->where = 0;
3253
3254   return true;
3255 }
3256
3257
3258 /* Add a symbol to the .loader symbols, if necessary.  */
3259
3260 static boolean
3261 xcoff_build_ldsyms (h, p)
3262      struct xcoff_link_hash_entry *h;
3263      PTR p;
3264 {
3265   struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3266   bfd_size_type amt;
3267
3268   if (h->root.type == bfd_link_hash_warning)
3269     h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3270
3271   /* __rtinit, this symbol has special handling. */
3272   if (h->flags & XCOFF_RTINIT)
3273       return true;
3274
3275   /* If this is a final link, and the symbol was defined as a common
3276      symbol in a regular object file, and there was no definition in
3277      any dynamic object, then the linker will have allocated space for
3278      the symbol in a common section but the XCOFF_DEF_REGULAR flag
3279      will not have been set.  */
3280   if (h->root.type == bfd_link_hash_defined
3281       && (h->flags & XCOFF_DEF_REGULAR) == 0
3282       && (h->flags & XCOFF_REF_REGULAR) != 0
3283       && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3284       && (bfd_is_abs_section (h->root.u.def.section)
3285           || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3286     h->flags |= XCOFF_DEF_REGULAR;
3287
3288   /* If all defined symbols should be exported, mark them now.  We
3289      don't want to export the actual functions, just the function
3290      descriptors.  */
3291   if (ldinfo->export_defineds
3292       && (h->flags & XCOFF_DEF_REGULAR) != 0
3293       && h->root.root.string[0] != '.')
3294     {
3295       boolean export;
3296
3297       /* We don't export a symbol which is being defined by an object
3298          included from an archive which contains a shared object.  The
3299          rationale is that if an archive contains both an unshared and
3300          a shared object, then there must be some reason that the
3301          unshared object is unshared, and we don't want to start
3302          providing a shared version of it.  In particular, this solves
3303          a bug involving the _savefNN set of functions.  gcc will call
3304          those functions without providing a slot to restore the TOC,
3305          so it is essential that these functions be linked in directly
3306          and not from a shared object, which means that a shared
3307          object which also happens to link them in must not export
3308          them.  This is confusing, but I haven't been able to think of
3309          a different approach.  Note that the symbols can, of course,
3310          be exported explicitly.  */
3311       export = true;
3312       if ((h->root.type == bfd_link_hash_defined
3313            || h->root.type == bfd_link_hash_defweak)
3314           && h->root.u.def.section->owner != NULL
3315           && h->root.u.def.section->owner->my_archive != NULL)
3316         {
3317           bfd *arbfd, *member;
3318
3319           arbfd = h->root.u.def.section->owner->my_archive;
3320           member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3321           while (member != NULL)
3322             {
3323               if ((member->flags & DYNAMIC) != 0)
3324                 {
3325                   export = false;
3326                   break;
3327                 }
3328               member = bfd_openr_next_archived_file (arbfd, member);
3329             }
3330         }
3331
3332       if (export)
3333         h->flags |= XCOFF_EXPORT;
3334     }
3335
3336   /* We don't want to garbage collect symbols which are not defined in
3337      XCOFF files.  This is a convenient place to mark them.  */
3338   if (xcoff_hash_table (ldinfo->info)->gc
3339       && (h->flags & XCOFF_MARK) == 0
3340       && (h->root.type == bfd_link_hash_defined
3341           || h->root.type == bfd_link_hash_defweak)
3342       && (h->root.u.def.section->owner == NULL
3343           || (h->root.u.def.section->owner->xvec
3344               != ldinfo->info->hash->creator)))
3345     h->flags |= XCOFF_MARK;
3346
3347   /* If this symbol is called and defined in a dynamic object, or it
3348      is imported, then we need to set up global linkage code for it.
3349      (Unless we did garbage collection and we didn't need this
3350      symbol.)  */
3351   if ((h->flags & XCOFF_CALLED) != 0
3352       && (h->root.type == bfd_link_hash_undefined
3353           || h->root.type == bfd_link_hash_undefweak)
3354       && h->root.root.string[0] == '.'
3355       && h->descriptor != NULL
3356       && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3357           || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3358               && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3359       && (! xcoff_hash_table (ldinfo->info)->gc
3360           || (h->flags & XCOFF_MARK) != 0))
3361     {
3362       asection *sec;
3363       struct xcoff_link_hash_entry *hds;
3364
3365       sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3366       h->root.type = bfd_link_hash_defined;
3367       h->root.u.def.section = sec;
3368       h->root.u.def.value = sec->_raw_size;
3369       h->smclas = XMC_GL;
3370       h->flags |= XCOFF_DEF_REGULAR;
3371       sec->_raw_size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
3372
3373       /* The global linkage code requires a TOC entry for the
3374          descriptor.  */
3375       hds = h->descriptor;
3376       BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3377                    || hds->root.type == bfd_link_hash_undefweak)
3378                   && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3379       hds->flags |= XCOFF_MARK;
3380       if (hds->toc_section == NULL)
3381         {
3382           int byte_size;
3383
3384           /* 32 vs 64
3385              xcoff32 uses 4 bytes in the toc.
3386              xcoff64 uses 8 bytes in the toc.  */
3387           if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
3388             byte_size = 8;
3389           else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
3390             byte_size = 4;
3391           else
3392             return false;
3393
3394           hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3395           hds->u.toc_offset = hds->toc_section->_raw_size;
3396           hds->toc_section->_raw_size += byte_size;
3397           ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3398           ++hds->toc_section->reloc_count;
3399           hds->indx = -2;
3400           hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3401
3402           /* We need to call xcoff_build_ldsyms recursively here,
3403              because we may already have passed hds on the traversal.  */
3404           xcoff_build_ldsyms (hds, p);
3405         }
3406     }
3407
3408   /* If this symbol is exported, but not defined, we need to try to
3409      define it.  */
3410   if ((h->flags & XCOFF_EXPORT) != 0
3411       && (h->flags & XCOFF_IMPORT) == 0
3412       && (h->flags & XCOFF_DEF_REGULAR) == 0
3413       && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3414       && (h->root.type == bfd_link_hash_undefined
3415           || h->root.type == bfd_link_hash_undefweak))
3416     {
3417       if ((h->flags & XCOFF_DESCRIPTOR) != 0
3418           && (h->descriptor->root.type == bfd_link_hash_defined
3419               || h->descriptor->root.type == bfd_link_hash_defweak))
3420         {
3421           asection *sec;
3422
3423           /* This is an undefined function descriptor associated with
3424              a defined entry point.  We can build up a function
3425              descriptor ourselves.  Believe it or not, the AIX linker
3426              actually does this, and there are cases where we need to
3427              do it as well.  */
3428           sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3429           h->root.type = bfd_link_hash_defined;
3430           h->root.u.def.section = sec;
3431           h->root.u.def.value = sec->_raw_size;
3432           h->smclas = XMC_DS;
3433           h->flags |= XCOFF_DEF_REGULAR;
3434
3435           /* The size of the function descriptor depends if this is an
3436              xcoff32 (12) or xcoff64 (24).  */
3437           sec->_raw_size +=
3438             bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
3439
3440           /* A function descriptor uses two relocs: one for the
3441              associated code, and one for the TOC address.  */
3442           xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3443           sec->reloc_count += 2;
3444
3445           /* We handle writing out the contents of the descriptor in
3446              xcoff_write_global_symbol.  */
3447         }
3448       else
3449         {
3450           (*_bfd_error_handler)
3451             (_("warning: attempt to export undefined symbol `%s'"),
3452              h->root.root.string);
3453           h->ldsym = NULL;
3454           return true;
3455         }
3456     }
3457
3458   /* If this is still a common symbol, and it wasn't garbage
3459      collected, we need to actually allocate space for it in the .bss
3460      section.  */
3461   if (h->root.type == bfd_link_hash_common
3462       && (! xcoff_hash_table (ldinfo->info)->gc
3463           || (h->flags & XCOFF_MARK) != 0)
3464       && h->root.u.c.p->section->_raw_size == 0)
3465     {
3466       BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3467       h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3468     }
3469
3470   /* We need to add a symbol to the .loader section if it is mentioned
3471      in a reloc which we are copying to the .loader section and it was
3472      not defined or common, or if it is the entry point, or if it is
3473      being exported.  */
3474
3475   if (((h->flags & XCOFF_LDREL) == 0
3476        || h->root.type == bfd_link_hash_defined
3477        || h->root.type == bfd_link_hash_defweak
3478        || h->root.type == bfd_link_hash_common)
3479       && (h->flags & XCOFF_ENTRY) == 0
3480       && (h->flags & XCOFF_EXPORT) == 0)
3481     {
3482       h->ldsym = NULL;
3483       return true;
3484     }
3485
3486   /* We don't need to add this symbol if we did garbage collection and
3487      we did not mark this symbol.  */
3488   if (xcoff_hash_table (ldinfo->info)->gc
3489       && (h->flags & XCOFF_MARK) == 0)
3490     {
3491       h->ldsym = NULL;
3492       return true;
3493     }
3494
3495   /* We may have already processed this symbol due to the recursive
3496      call above.  */
3497   if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3498     return true;
3499
3500   /* We need to add this symbol to the .loader symbols.  */
3501
3502   BFD_ASSERT (h->ldsym == NULL);
3503   amt = sizeof (struct internal_ldsym);
3504   h->ldsym = (struct internal_ldsym *) bfd_zalloc (ldinfo->output_bfd, amt);
3505   if (h->ldsym == NULL)
3506     {
3507       ldinfo->failed = true;
3508       return false;
3509     }
3510
3511   if ((h->flags & XCOFF_IMPORT) != 0)
3512     h->ldsym->l_ifile = h->ldindx;
3513
3514   /* The first 3 symbol table indices are reserved to indicate the
3515      data, text and bss sections.  */
3516   h->ldindx = ldinfo->ldsym_count + 3;
3517
3518   ++ldinfo->ldsym_count;
3519
3520   if (false == bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3521                                             h->ldsym,
3522                                             h->root.root.string))
3523     {
3524       return false;
3525     }
3526
3527   h->flags |= XCOFF_BUILT_LDSYM;
3528
3529   return true;
3530 }
3531 \f
3532 /* Do the final link step.  */
3533
3534 boolean
3535 _bfd_xcoff_bfd_final_link (abfd, info)
3536      bfd *abfd;
3537      struct bfd_link_info *info;
3538 {
3539   bfd_size_type symesz;
3540   struct xcoff_final_link_info finfo;
3541   asection *o;
3542   struct bfd_link_order *p;
3543   bfd_size_type max_contents_size;
3544   bfd_size_type max_sym_count;
3545   bfd_size_type max_lineno_count;
3546   bfd_size_type max_reloc_count;
3547   bfd_size_type max_output_reloc_count;
3548   file_ptr rel_filepos;
3549   unsigned int relsz;
3550   file_ptr line_filepos;
3551   unsigned int linesz;
3552   bfd *sub;
3553   bfd_byte *external_relocs = NULL;
3554   char strbuf[STRING_SIZE_SIZE];
3555   file_ptr pos;
3556   bfd_size_type amt;
3557
3558   if (info->shared)
3559     abfd->flags |= DYNAMIC;
3560
3561   symesz = bfd_coff_symesz (abfd);
3562
3563   finfo.info = info;
3564   finfo.output_bfd = abfd;
3565   finfo.strtab = NULL;
3566   finfo.section_info = NULL;
3567   finfo.last_file_index = -1;
3568   finfo.toc_symindx = -1;
3569   finfo.internal_syms = NULL;
3570   finfo.sym_indices = NULL;
3571   finfo.outsyms = NULL;
3572   finfo.linenos = NULL;
3573   finfo.contents = NULL;
3574   finfo.external_relocs = NULL;
3575
3576   finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
3577                  + bfd_xcoff_ldhdrsz (abfd));
3578   finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
3579                  + bfd_xcoff_ldhdrsz(abfd)
3580                  + (xcoff_hash_table (info)->ldhdr.l_nsyms
3581                     * bfd_xcoff_ldsymsz(abfd)));
3582
3583   xcoff_data (abfd)->coff.link_info = info;
3584
3585   finfo.strtab = _bfd_stringtab_init ();
3586   if (finfo.strtab == NULL)
3587     goto error_return;
3588
3589   /* Count the line number and relocation entries required for the
3590      output file.  Determine a few maximum sizes.  */
3591   max_contents_size = 0;
3592   max_lineno_count = 0;
3593   max_reloc_count = 0;
3594   for (o = abfd->sections; o != NULL; o = o->next)
3595     {
3596       o->reloc_count = 0;
3597       o->lineno_count = 0;
3598       for (p = o->link_order_head; p != NULL; p = p->next)
3599         {
3600           if (p->type == bfd_indirect_link_order)
3601             {
3602               asection *sec;
3603
3604               sec = p->u.indirect.section;
3605
3606               /* Mark all sections which are to be included in the
3607                  link.  This will normally be every section.  We need
3608                  to do this so that we can identify any sections which
3609                  the linker has decided to not include.  */
3610               sec->linker_mark = true;
3611
3612               if (info->strip == strip_none
3613                   || info->strip == strip_some)
3614                 o->lineno_count += sec->lineno_count;
3615
3616               o->reloc_count += sec->reloc_count;
3617
3618               if (sec->_raw_size > max_contents_size)
3619                 max_contents_size = sec->_raw_size;
3620               if (sec->lineno_count > max_lineno_count)
3621                 max_lineno_count = sec->lineno_count;
3622               if (coff_section_data (sec->owner, sec) != NULL
3623                   && xcoff_section_data (sec->owner, sec) != NULL
3624                   && (xcoff_section_data (sec->owner, sec)->lineno_count
3625                       > max_lineno_count))
3626                 max_lineno_count =
3627                   xcoff_section_data (sec->owner, sec)->lineno_count;
3628               if (sec->reloc_count > max_reloc_count)
3629                 max_reloc_count = sec->reloc_count;
3630             }
3631           else if (p->type == bfd_section_reloc_link_order
3632                    || p->type == bfd_symbol_reloc_link_order)
3633             ++o->reloc_count;
3634         }
3635     }
3636
3637   /* Compute the file positions for all the sections.  */
3638   if (abfd->output_has_begun)
3639     {
3640       if (xcoff_hash_table (info)->file_align != 0)
3641         abort ();
3642     }
3643   else
3644     {
3645       bfd_vma file_align;
3646
3647       file_align = xcoff_hash_table (info)->file_align;
3648       if (file_align != 0)
3649         {
3650           boolean saw_contents;
3651           int indx;
3652           asection **op;
3653           file_ptr sofar;
3654           
3655           /* Insert .pad sections before every section which has
3656              contents and is loaded, if it is preceded by some other
3657              section which has contents and is loaded.  */
3658           saw_contents = true;
3659           for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3660             {
3661               if (strcmp ((*op)->name, ".pad") == 0)
3662                 saw_contents = false;
3663               else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3664                        && ((*op)->flags & SEC_LOAD) != 0)
3665                 {
3666                   if (! saw_contents)
3667                     saw_contents = true;
3668                   else
3669                     {
3670                       asection *n, **st;
3671                       
3672                       /* Create a pad section and place it before the section
3673                          that needs padding.  This requires unlinking and 
3674                          relinking the bfd's section list.  */
3675                       
3676                       st = abfd->section_tail;
3677                       n = bfd_make_section_anyway (abfd, ".pad");
3678                       n->flags = SEC_HAS_CONTENTS;
3679                       n->alignment_power = 0; 
3680
3681                       BFD_ASSERT (*st == n);
3682                       bfd_section_list_remove (abfd, st);
3683                       bfd_section_list_insert (abfd, op, n);
3684
3685                       op = &n->next;
3686                       saw_contents = false;
3687                     }
3688                 }
3689             }
3690           
3691           /* Reset the section indices after inserting the new
3692              sections.  */
3693           indx = 0;
3694           for (o = abfd->sections; o != NULL; o = o->next)
3695             {
3696               ++indx;
3697               o->target_index = indx;
3698             }
3699           BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3700
3701           /* Work out appropriate sizes for the .pad sections to force
3702              each section to land on a page boundary.  This bit of
3703              code knows what compute_section_file_positions is going
3704              to do.  */
3705           sofar = bfd_coff_filhsz (abfd);
3706           sofar += bfd_coff_aoutsz (abfd);
3707           sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3708           for (o = abfd->sections; o != NULL; o = o->next)
3709             if ((bfd_xcoff_is_reloc_count_overflow
3710                  (abfd, (bfd_vma) o->reloc_count))
3711                 || (bfd_xcoff_is_lineno_count_overflow
3712                     (abfd, (bfd_vma) o->lineno_count)))
3713               /* 64 does not overflow, need to check if 32 does */
3714               sofar += bfd_coff_scnhsz (abfd);
3715
3716           for (o = abfd->sections; o != NULL; o = o->next)
3717             {
3718               if (strcmp (o->name, ".pad") == 0)
3719                 {
3720                   bfd_vma pageoff;
3721
3722                   BFD_ASSERT (o->_raw_size == 0);
3723                   pageoff = sofar & (file_align - 1);
3724                   if (pageoff != 0)
3725                     {
3726                       o->_raw_size = file_align - pageoff;
3727                       sofar += file_align - pageoff;
3728                       o->flags |= SEC_HAS_CONTENTS;
3729                     }
3730                 }
3731               else
3732                 {
3733                   if ((o->flags & SEC_HAS_CONTENTS) != 0)
3734                     sofar += BFD_ALIGN (o->_raw_size,
3735                                         1 << o->alignment_power);
3736                 }
3737             }
3738         }
3739
3740       if (! bfd_coff_compute_section_file_positions (abfd))
3741         goto error_return;
3742     }
3743
3744   /* Allocate space for the pointers we need to keep for the relocs.  */
3745   {
3746     unsigned int i;
3747
3748     /* We use section_count + 1, rather than section_count, because
3749        the target_index fields are 1 based.  */
3750     amt = abfd->section_count + 1;
3751     amt *= sizeof (struct xcoff_link_section_info);
3752     finfo.section_info = (struct xcoff_link_section_info *) bfd_malloc (amt);
3753     if (finfo.section_info == NULL)
3754       goto error_return;
3755     for (i = 0; i <= abfd->section_count; i++)
3756       {
3757         finfo.section_info[i].relocs = NULL;
3758         finfo.section_info[i].rel_hashes = NULL;
3759         finfo.section_info[i].toc_rel_hashes = NULL;
3760       }
3761   }
3762
3763   /* Set the file positions for the relocs.  */
3764   rel_filepos = obj_relocbase (abfd);
3765   relsz = bfd_coff_relsz (abfd);
3766   max_output_reloc_count = 0;
3767   for (o = abfd->sections; o != NULL; o = o->next)
3768     {
3769       if (o->reloc_count == 0)
3770         o->rel_filepos = 0;
3771       else
3772         {
3773           /* A stripped file has no relocs.  However, we still
3774              allocate the buffers, so that later code doesn't have to
3775              worry about whether we are stripping or not.  */
3776           if (info->strip == strip_all)
3777             o->rel_filepos = 0;
3778           else
3779             {
3780               o->flags |= SEC_RELOC;
3781               o->rel_filepos = rel_filepos;
3782               rel_filepos += o->reloc_count * relsz;
3783             }
3784
3785           /* We don't know the indices of global symbols until we have
3786              written out all the local symbols.  For each section in
3787              the output file, we keep an array of pointers to hash
3788              table entries.  Each entry in the array corresponds to a
3789              reloc.  When we find a reloc against a global symbol, we
3790              set the corresponding entry in this array so that we can
3791              fix up the symbol index after we have written out all the
3792              local symbols.
3793
3794              Because of this problem, we also keep the relocs in
3795              memory until the end of the link.  This wastes memory.
3796              We could backpatch the file later, I suppose, although it
3797              would be slow.  */
3798           amt = o->reloc_count;
3799           amt *= sizeof (struct internal_reloc);
3800           finfo.section_info[o->target_index].relocs =
3801             (struct internal_reloc *) bfd_malloc (amt);
3802
3803           amt = o->reloc_count;
3804           amt *= sizeof (struct xcoff_link_hash_entry *);
3805           finfo.section_info[o->target_index].rel_hashes =
3806             (struct xcoff_link_hash_entry **) bfd_malloc (amt);
3807
3808           if (finfo.section_info[o->target_index].relocs == NULL
3809               || finfo.section_info[o->target_index].rel_hashes == NULL)
3810             goto error_return;
3811
3812           if (o->reloc_count > max_output_reloc_count)
3813             max_output_reloc_count = o->reloc_count;
3814         }
3815     }
3816
3817   /* We now know the size of the relocs, so we can determine the file
3818      positions of the line numbers.  */
3819   line_filepos = rel_filepos;
3820   finfo.line_filepos = line_filepos;
3821   linesz = bfd_coff_linesz (abfd);
3822   for (o = abfd->sections; o != NULL; o = o->next)
3823     {
3824       if (o->lineno_count == 0)
3825         o->line_filepos = 0;
3826       else
3827         {
3828           o->line_filepos = line_filepos;
3829           line_filepos += o->lineno_count * linesz;
3830         }
3831
3832       /* Reset the reloc and lineno counts, so that we can use them to
3833          count the number of entries we have output so far.  */
3834       o->reloc_count = 0;
3835       o->lineno_count = 0;
3836     }
3837
3838   obj_sym_filepos (abfd) = line_filepos;
3839
3840   /* Figure out the largest number of symbols in an input BFD.  Take
3841      the opportunity to clear the output_has_begun fields of all the
3842      input BFD's.  We want at least 6 symbols, since that is the
3843      number which xcoff_write_global_symbol may need.  */
3844   max_sym_count = 6;
3845   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3846     {
3847       bfd_size_type sz;
3848
3849       sub->output_has_begun = false;
3850       sz = obj_raw_syment_count (sub);
3851       if (sz > max_sym_count)
3852         max_sym_count = sz;
3853     }
3854
3855   /* Allocate some buffers used while linking.  */
3856   amt = max_sym_count * sizeof (struct internal_syment);
3857   finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
3858
3859   amt = max_sym_count * sizeof (long);
3860   finfo.sym_indices = (long *) bfd_malloc (amt);
3861
3862   amt = (max_sym_count + 1) * symesz;
3863   finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
3864
3865   amt = max_lineno_count * bfd_coff_linesz (abfd);
3866   finfo.linenos = (bfd_byte *) bfd_malloc (amt);
3867
3868   amt = max_contents_size;
3869   finfo.contents = (bfd_byte *) bfd_malloc (amt);
3870
3871   amt = max_reloc_count * relsz;
3872   finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
3873
3874   if ((finfo.internal_syms == NULL && max_sym_count > 0)
3875       || (finfo.sym_indices == NULL && max_sym_count > 0)
3876       || finfo.outsyms == NULL
3877       || (finfo.linenos == NULL && max_lineno_count > 0)
3878       || (finfo.contents == NULL && max_contents_size > 0)
3879       || (finfo.external_relocs == NULL && max_reloc_count > 0))
3880     goto error_return;
3881
3882   obj_raw_syment_count (abfd) = 0;
3883   xcoff_data (abfd)->toc = (bfd_vma) -1;
3884
3885   /* We now know the position of everything in the file, except that
3886      we don't know the size of the symbol table and therefore we don't
3887      know where the string table starts.  We just build the string
3888      table in memory as we go along.  We process all the relocations
3889      for a single input file at once.  */
3890   for (o = abfd->sections; o != NULL; o = o->next)
3891     {
3892       for (p = o->link_order_head; p != NULL; p = p->next)
3893         {
3894           if (p->type == bfd_indirect_link_order
3895               && p->u.indirect.section->owner->xvec == abfd->xvec)
3896             {
3897               sub = p->u.indirect.section->owner;
3898               if (! sub->output_has_begun)
3899                 {
3900                   if (! xcoff_link_input_bfd (&finfo, sub))
3901                     goto error_return;
3902                   sub->output_has_begun = true;
3903                 }
3904             }
3905           else if (p->type == bfd_section_reloc_link_order
3906                    || p->type == bfd_symbol_reloc_link_order)
3907             {
3908               if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3909                 goto error_return;
3910             }
3911           else
3912             {
3913               if (! _bfd_default_link_order (abfd, info, o, p))
3914                 goto error_return;
3915             }
3916         }
3917     }
3918
3919
3920   /* Free up the buffers used by xcoff_link_input_bfd.  */
3921
3922   if (finfo.internal_syms != NULL)
3923     {
3924       free (finfo.internal_syms);
3925       finfo.internal_syms = NULL;
3926     }
3927   if (finfo.sym_indices != NULL)
3928     {
3929       free (finfo.sym_indices);
3930       finfo.sym_indices = NULL;
3931     }
3932   if (finfo.linenos != NULL)
3933     {
3934       free (finfo.linenos);
3935       finfo.linenos = NULL;
3936     }
3937   if (finfo.contents != NULL)
3938     {
3939       free (finfo.contents);
3940       finfo.contents = NULL;
3941     }
3942   if (finfo.external_relocs != NULL)
3943     {
3944       free (finfo.external_relocs);
3945       finfo.external_relocs = NULL;
3946     }
3947
3948   /* The value of the last C_FILE symbol is supposed to be -1.  Write
3949      it out again.  */
3950   if (finfo.last_file_index != -1)
3951     {
3952       finfo.last_file.n_value = -(bfd_vma) 1;
3953       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3954                              (PTR) finfo.outsyms);
3955       pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
3956       if (bfd_seek (abfd, pos, SEEK_SET) != 0
3957           || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
3958         goto error_return;
3959     }
3960
3961   /* Write out all the global symbols which do not come from XCOFF
3962      input files.  */
3963   xcoff_link_hash_traverse (xcoff_hash_table (info),
3964                             xcoff_write_global_symbol,
3965                             (PTR) &finfo);
3966
3967   if (finfo.outsyms != NULL)
3968     {
3969       free (finfo.outsyms);
3970       finfo.outsyms = NULL;
3971     }
3972
3973   /* Now that we have written out all the global symbols, we know the
3974      symbol indices to use for relocs against them, and we can finally
3975      write out the relocs.  */
3976   amt = max_output_reloc_count * relsz;
3977   external_relocs = (bfd_byte *) bfd_malloc (amt);
3978   if (external_relocs == NULL && max_output_reloc_count != 0)
3979     goto error_return;
3980
3981   for (o = abfd->sections; o != NULL; o = o->next)
3982     {
3983       struct internal_reloc *irel;
3984       struct internal_reloc *irelend;
3985       struct xcoff_link_hash_entry **rel_hash;
3986       struct xcoff_toc_rel_hash *toc_rel_hash;
3987       bfd_byte *erel;
3988       bfd_size_type rel_size;
3989
3990       /* A stripped file has no relocs.  */
3991       if (info->strip == strip_all)
3992         {
3993           o->reloc_count = 0;
3994           continue;
3995         }
3996
3997       if (o->reloc_count == 0)
3998         continue;
3999
4000       irel = finfo.section_info[o->target_index].relocs;
4001       irelend = irel + o->reloc_count;
4002       rel_hash = finfo.section_info[o->target_index].rel_hashes;
4003       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4004         {
4005           if (*rel_hash != NULL)
4006             {
4007               if ((*rel_hash)->indx < 0)
4008                 {
4009                   if (! ((*info->callbacks->unattached_reloc)
4010                          (info, (*rel_hash)->root.root.string,
4011                           (bfd *) NULL, o, irel->r_vaddr)))
4012                     goto error_return;
4013                   (*rel_hash)->indx = 0;
4014                 }
4015               irel->r_symndx = (*rel_hash)->indx;
4016             }
4017         }
4018
4019       for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4020            toc_rel_hash != NULL;
4021            toc_rel_hash = toc_rel_hash->next)
4022         {
4023           if (toc_rel_hash->h->u.toc_indx < 0)
4024             {
4025               if (! ((*info->callbacks->unattached_reloc)
4026                      (info, toc_rel_hash->h->root.root.string,
4027                       (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4028                 goto error_return;
4029               toc_rel_hash->h->u.toc_indx = 0;
4030             }
4031           toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4032         }
4033
4034       /* XCOFF requires that the relocs be sorted by address.  We tend
4035          to produce them in the order in which their containing csects
4036          appear in the symbol table, which is not necessarily by
4037          address.  So we sort them here.  There may be a better way to
4038          do this.  */
4039       qsort ((PTR) finfo.section_info[o->target_index].relocs,
4040              o->reloc_count, sizeof (struct internal_reloc),
4041              xcoff_sort_relocs);
4042
4043       irel = finfo.section_info[o->target_index].relocs;
4044       irelend = irel + o->reloc_count;
4045       erel = external_relocs;
4046       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4047         bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4048
4049       rel_size = relsz * o->reloc_count;
4050       if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4051           || bfd_bwrite ((PTR) external_relocs, rel_size, abfd) != rel_size)
4052         goto error_return;
4053     }
4054
4055   if (external_relocs != NULL)
4056     {
4057       free (external_relocs);
4058       external_relocs = NULL;
4059     }
4060
4061   /* Free up the section information.  */
4062   if (finfo.section_info != NULL)
4063     {
4064       unsigned int i;
4065
4066       for (i = 0; i < abfd->section_count; i++)
4067         {
4068           if (finfo.section_info[i].relocs != NULL)
4069             free (finfo.section_info[i].relocs);
4070           if (finfo.section_info[i].rel_hashes != NULL)
4071             free (finfo.section_info[i].rel_hashes);
4072         }
4073       free (finfo.section_info);
4074       finfo.section_info = NULL;
4075     }
4076
4077   /* Write out the loader section contents.  */
4078   BFD_ASSERT ((bfd_byte *) finfo.ldrel
4079               == (xcoff_hash_table (info)->loader_section->contents
4080                   + xcoff_hash_table (info)->ldhdr.l_impoff));
4081   o = xcoff_hash_table (info)->loader_section;
4082   if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
4083                                   (file_ptr) o->output_offset, o->_raw_size))
4084     goto error_return;
4085
4086   /* Write out the magic sections.  */
4087   o = xcoff_hash_table (info)->linkage_section;
4088   if (o->_raw_size > 0
4089       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4090                                      (file_ptr) o->output_offset,
4091                                      o->_raw_size))
4092     goto error_return;
4093   o = xcoff_hash_table (info)->toc_section;
4094   if (o->_raw_size > 0
4095       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4096                                      (file_ptr) o->output_offset,
4097                                      o->_raw_size))
4098     goto error_return;
4099   o = xcoff_hash_table (info)->descriptor_section;
4100   if (o->_raw_size > 0
4101       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4102                                      (file_ptr) o->output_offset,
4103                                      o->_raw_size))
4104     goto error_return;
4105
4106   /* Write out the string table.  */
4107   pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
4108   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4109     goto error_return;
4110   H_PUT_32 (abfd,
4111             _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4112             strbuf);
4113   amt = STRING_SIZE_SIZE;
4114   if (bfd_bwrite (strbuf, amt, abfd) != amt)
4115     goto error_return;
4116   if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4117     goto error_return;
4118
4119   _bfd_stringtab_free (finfo.strtab);
4120
4121   /* Write out the debugging string table.  */
4122   o = xcoff_hash_table (info)->debug_section;
4123   if (o != NULL)
4124     {
4125       struct bfd_strtab_hash *debug_strtab;
4126
4127       debug_strtab = xcoff_hash_table (info)->debug_strtab;
4128       BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4129                   >= _bfd_stringtab_size (debug_strtab));
4130       pos = o->output_section->filepos + o->output_offset;
4131       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4132         goto error_return;
4133       if (! _bfd_stringtab_emit (abfd, debug_strtab))
4134         goto error_return;
4135     }
4136
4137   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4138      not try to write out the symbols.  */
4139   bfd_get_symcount (abfd) = 0;
4140
4141   return true;
4142
4143  error_return:
4144   if (finfo.strtab != NULL)
4145     _bfd_stringtab_free (finfo.strtab);
4146
4147   if (finfo.section_info != NULL)
4148     {
4149       unsigned int i;
4150
4151       for (i = 0; i < abfd->section_count; i++)
4152         {
4153           if (finfo.section_info[i].relocs != NULL)
4154             free (finfo.section_info[i].relocs);
4155           if (finfo.section_info[i].rel_hashes != NULL)
4156             free (finfo.section_info[i].rel_hashes);
4157         }
4158       free (finfo.section_info);
4159     }
4160
4161   if (finfo.internal_syms != NULL)
4162     free (finfo.internal_syms);
4163   if (finfo.sym_indices != NULL)
4164     free (finfo.sym_indices);
4165   if (finfo.outsyms != NULL)
4166     free (finfo.outsyms);
4167   if (finfo.linenos != NULL)
4168     free (finfo.linenos);
4169   if (finfo.contents != NULL)
4170     free (finfo.contents);
4171   if (finfo.external_relocs != NULL)
4172     free (finfo.external_relocs);
4173   if (external_relocs != NULL)
4174     free (external_relocs);
4175   return false;
4176 }
4177
4178 /* Link an input file into the linker output file.  This function
4179    handles all the sections and relocations of the input file at once.  */
4180
4181 static boolean
4182 xcoff_link_input_bfd (finfo, input_bfd)
4183      struct xcoff_final_link_info *finfo;
4184      bfd *input_bfd;
4185 {
4186   bfd *output_bfd;
4187   const char *strings;
4188   bfd_size_type syment_base;
4189   unsigned int n_tmask;
4190   unsigned int n_btshft;
4191   boolean copy, hash;
4192   bfd_size_type isymesz;
4193   bfd_size_type osymesz;
4194   bfd_size_type linesz;
4195   bfd_byte *esym;
4196   bfd_byte *esym_end;
4197   struct xcoff_link_hash_entry **sym_hash;
4198   struct internal_syment *isymp;
4199   asection **csectpp;
4200   unsigned long *debug_index;
4201   long *indexp;
4202   unsigned long output_index;
4203   bfd_byte *outsym;
4204   unsigned int incls;
4205   asection *oline;
4206   boolean keep_syms;
4207   asection *o;
4208
4209   /* We can just skip DYNAMIC files, unless this is a static link.  */
4210   if ((input_bfd->flags & DYNAMIC) != 0
4211       && ! finfo->info->static_link)
4212     return true;
4213
4214   /* Move all the symbols to the output file.  */
4215
4216   output_bfd = finfo->output_bfd;
4217   strings = NULL;
4218   syment_base = obj_raw_syment_count (output_bfd);
4219   isymesz = bfd_coff_symesz (input_bfd);
4220   osymesz = bfd_coff_symesz (output_bfd);
4221   linesz = bfd_coff_linesz (input_bfd);
4222   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4223
4224   n_tmask = coff_data (input_bfd)->local_n_tmask;
4225   n_btshft = coff_data (input_bfd)->local_n_btshft;
4226
4227   /* Define macros so that ISFCN, et. al., macros work correctly.  */
4228 #define N_TMASK n_tmask
4229 #define N_BTSHFT n_btshft
4230
4231   copy = false;
4232   if (! finfo->info->keep_memory)
4233     copy = true;
4234   hash = true;
4235   if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4236     hash = false;
4237
4238   if (! _bfd_coff_get_external_symbols (input_bfd))
4239     return false;
4240
4241   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4242   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4243   sym_hash = obj_xcoff_sym_hashes (input_bfd);
4244   csectpp = xcoff_data (input_bfd)->csects;
4245   debug_index = xcoff_data (input_bfd)->debug_indices;
4246   isymp = finfo->internal_syms;
4247   indexp = finfo->sym_indices;
4248   output_index = syment_base;
4249   outsym = finfo->outsyms;
4250   incls = 0;
4251   oline = NULL;
4252
4253   while (esym < esym_end)
4254     {
4255
4256       struct internal_syment isym;
4257       union internal_auxent aux;
4258       int smtyp = 0;
4259       boolean skip;
4260       boolean require;
4261       int add;
4262
4263       bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4264
4265       /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4266          information.  */
4267       if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4268         {
4269           BFD_ASSERT (isymp->n_numaux > 0);
4270           bfd_coff_swap_aux_in (input_bfd,
4271                                 (PTR) (esym + isymesz * isymp->n_numaux),
4272                                 isymp->n_type, isymp->n_sclass,
4273                                 isymp->n_numaux - 1, isymp->n_numaux,
4274                                 (PTR) &aux);
4275
4276           smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4277         }
4278
4279       /* Make a copy of *isymp so that the relocate_section function
4280          always sees the original values.  This is more reliable than
4281          always recomputing the symbol value even if we are stripping
4282          the symbol.  */
4283       isym = *isymp;
4284
4285       /* If this symbol is in the .loader section, swap out the
4286          .loader symbol information.  If this is an external symbol
4287          reference to a defined symbol, though, then wait until we get
4288          to the definition.  */
4289       if (isym.n_sclass == C_EXT
4290           && *sym_hash != NULL
4291           && (*sym_hash)->ldsym != NULL
4292           && (smtyp != XTY_ER
4293               || (*sym_hash)->root.type == bfd_link_hash_undefined))
4294         {
4295           struct xcoff_link_hash_entry *h;
4296           struct internal_ldsym *ldsym;
4297
4298           h = *sym_hash;
4299           ldsym = h->ldsym;
4300           if (isym.n_scnum > 0)
4301             {
4302               ldsym->l_scnum = (*csectpp)->output_section->target_index;
4303               ldsym->l_value = (isym.n_value
4304                                 + (*csectpp)->output_section->vma
4305                                 + (*csectpp)->output_offset
4306                                 - (*csectpp)->vma);
4307             }
4308           else
4309             {
4310               ldsym->l_scnum = isym.n_scnum;
4311               ldsym->l_value = isym.n_value;
4312             }
4313
4314           ldsym->l_smtype = smtyp;
4315           if (((h->flags & XCOFF_DEF_REGULAR) == 0
4316                && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4317               || (h->flags & XCOFF_IMPORT) != 0)
4318             ldsym->l_smtype |= L_IMPORT;
4319           if (((h->flags & XCOFF_DEF_REGULAR) != 0
4320                && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4321               || (h->flags & XCOFF_EXPORT) != 0)
4322             ldsym->l_smtype |= L_EXPORT;
4323           if ((h->flags & XCOFF_ENTRY) != 0)
4324             ldsym->l_smtype |= L_ENTRY;
4325
4326           ldsym->l_smclas = aux.x_csect.x_smclas;
4327
4328           if (ldsym->l_ifile == (bfd_size_type) -1)
4329             ldsym->l_ifile = 0;
4330           else if (ldsym->l_ifile == 0)
4331             {
4332               if ((ldsym->l_smtype & L_IMPORT) == 0)
4333                 ldsym->l_ifile = 0;
4334               else
4335                 {
4336                   bfd *impbfd;
4337
4338                   if (h->root.type == bfd_link_hash_defined
4339                       || h->root.type == bfd_link_hash_defweak)
4340                     impbfd = h->root.u.def.section->owner;
4341                   else if (h->root.type == bfd_link_hash_undefined
4342                            || h->root.type == bfd_link_hash_undefweak)
4343                     impbfd = h->root.u.undef.abfd;
4344                   else
4345                     impbfd = NULL;
4346
4347                   if (impbfd == NULL)
4348                     ldsym->l_ifile = 0;
4349                   else
4350                     {
4351                       BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4352                       ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4353                     }
4354                 }
4355             }
4356
4357           ldsym->l_parm = 0;
4358
4359           BFD_ASSERT (h->ldindx >= 0);
4360           bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4361                                     (finfo->ldsym
4362                                      + ((h->ldindx - 3)
4363                                         * bfd_xcoff_ldsymsz (finfo->output_bfd))));
4364           h->ldsym = NULL;
4365
4366           /* Fill in snentry now that we know the target_index.  */
4367           if ((h->flags & XCOFF_ENTRY) != 0
4368               && (h->root.type == bfd_link_hash_defined
4369                   || h->root.type == bfd_link_hash_defweak))
4370             {
4371               xcoff_data (output_bfd)->snentry =
4372                 h->root.u.def.section->output_section->target_index;
4373             }
4374         }
4375
4376       *indexp = -1;
4377
4378       skip = false;
4379       require = false;
4380       add = 1 + isym.n_numaux;
4381
4382       /* If we are skipping this csect, we want to skip this symbol.  */
4383       if (*csectpp == NULL)
4384         skip = true;
4385
4386       /* If we garbage collected this csect, we want to skip this
4387          symbol.  */
4388       if (! skip
4389           && xcoff_hash_table (finfo->info)->gc
4390           && ((*csectpp)->flags & SEC_MARK) == 0
4391           && *csectpp != bfd_abs_section_ptr)
4392         skip = true;
4393
4394       /* An XCOFF linker always skips C_STAT symbols.  */
4395       if (! skip
4396           && isymp->n_sclass == C_STAT)
4397         skip = true;
4398
4399       /* We skip all but the first TOC anchor.  */
4400       if (! skip
4401           && isymp->n_sclass == C_HIDEXT
4402           && aux.x_csect.x_smclas == XMC_TC0)
4403         {
4404           if (finfo->toc_symindx != -1)
4405             skip = true;
4406           else
4407             {
4408               bfd_vma tocval, tocend;
4409               bfd *inp;
4410
4411               tocval = ((*csectpp)->output_section->vma
4412                         + (*csectpp)->output_offset
4413                         + isym.n_value
4414                         - (*csectpp)->vma);
4415
4416               /* We want to find out if tocval is a good value to use
4417                  as the TOC anchor--that is, whether we can access all
4418                  of the TOC using a 16 bit offset from tocval.  This
4419                  test assumes that the TOC comes at the end of the
4420                  output section, as it does in the default linker
4421                  script.  */
4422               tocend = ((*csectpp)->output_section->vma
4423                         + (*csectpp)->output_section->_raw_size);
4424               for (inp = finfo->info->input_bfds;
4425                    inp != NULL;
4426                    inp = inp->link_next)
4427                 {
4428
4429                   for (o = inp->sections; o != NULL; o = o->next)
4430                     if (strcmp (o->name, ".tocbss") == 0)
4431                       {
4432                         bfd_vma new_toc_end;
4433                         new_toc_end = (o->output_section->vma
4434                                        + o->output_offset
4435                                        + o->_cooked_size);
4436                         if (new_toc_end > tocend)
4437                           tocend = new_toc_end;
4438                       }
4439
4440                 }
4441
4442               if (tocval + 0x10000 < tocend)
4443                 {
4444                   (*_bfd_error_handler)
4445                     (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4446                      (unsigned long) (tocend - tocval));
4447                   bfd_set_error (bfd_error_file_too_big);
4448                   return false;
4449                 }
4450
4451               if (tocval + 0x8000 < tocend)
4452                 {
4453                   bfd_vma tocadd;
4454
4455                   tocadd = tocend - (tocval + 0x8000);
4456                   tocval += tocadd;
4457                   isym.n_value += tocadd;
4458                 }
4459
4460               finfo->toc_symindx = output_index;
4461               xcoff_data (finfo->output_bfd)->toc = tocval;
4462               xcoff_data (finfo->output_bfd)->sntoc =
4463                 (*csectpp)->output_section->target_index;
4464               require = true;
4465
4466             }
4467         }
4468
4469       /* If we are stripping all symbols, we want to skip this one.  */
4470       if (! skip
4471           && finfo->info->strip == strip_all)
4472         skip = true;
4473
4474       /* We can skip resolved external references.  */
4475       if (! skip
4476           && isym.n_sclass == C_EXT
4477           && smtyp == XTY_ER
4478           && (*sym_hash)->root.type != bfd_link_hash_undefined)
4479         skip = true;
4480
4481       /* We can skip common symbols if they got defined somewhere
4482          else.  */
4483       if (! skip
4484           && isym.n_sclass == C_EXT
4485           && smtyp == XTY_CM
4486           && ((*sym_hash)->root.type != bfd_link_hash_common
4487               || (*sym_hash)->root.u.c.p->section != *csectpp)
4488           && ((*sym_hash)->root.type != bfd_link_hash_defined
4489               || (*sym_hash)->root.u.def.section != *csectpp))
4490         skip = true;
4491
4492       /* Skip local symbols if we are discarding them.  */
4493       if (! skip
4494           && finfo->info->discard == discard_all
4495           && isym.n_sclass != C_EXT
4496           && (isym.n_sclass != C_HIDEXT
4497               || smtyp != XTY_SD))
4498         skip = true;
4499
4500       /* If we stripping debugging symbols, and this is a debugging
4501          symbol, then skip it.  */
4502       if (! skip
4503           && finfo->info->strip == strip_debugger
4504           && isym.n_scnum == N_DEBUG)
4505         skip = true;
4506
4507       /* If some symbols are stripped based on the name, work out the
4508          name and decide whether to skip this symbol.  We don't handle
4509          this correctly for symbols whose names are in the .debug
4510          section; to get it right we would need a new bfd_strtab_hash
4511          function to return the string given the index.  */
4512       if (! skip
4513           && (finfo->info->strip == strip_some
4514               || finfo->info->discard == discard_l)
4515           && (debug_index == NULL || *debug_index == (unsigned long) -1))
4516         {
4517           const char *name;
4518           char buf[SYMNMLEN + 1];
4519
4520           name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4521
4522           if (name == NULL)
4523             return false;
4524
4525           if ((finfo->info->strip == strip_some
4526                && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4527                                     false) == NULL))
4528               || (finfo->info->discard == discard_l
4529                   && (isym.n_sclass != C_EXT
4530                       && (isym.n_sclass != C_HIDEXT
4531                           || smtyp != XTY_SD))
4532                   && bfd_is_local_label_name (input_bfd, name)))
4533             skip = true;
4534         }
4535
4536       /* We can not skip the first TOC anchor.  */
4537       if (skip
4538           && require
4539           && finfo->info->strip != strip_all)
4540         skip = false;
4541
4542       /* We now know whether we are to skip this symbol or not.  */
4543       if (! skip)
4544         {
4545           /* Adjust the symbol in order to output it.  */
4546
4547           if (isym._n._n_n._n_zeroes == 0
4548               && isym._n._n_n._n_offset != 0)
4549             {
4550               /* This symbol has a long name.  Enter it in the string
4551                  table we are building.  If *debug_index != -1, the
4552                  name has already been entered in the .debug section.  */
4553               if (debug_index != NULL && *debug_index != (unsigned long) -1)
4554                 isym._n._n_n._n_offset = *debug_index;
4555               else
4556                 {
4557                   const char *name;
4558                   bfd_size_type indx;
4559
4560                   name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4561                                                          (char *) NULL);
4562
4563                   if (name == NULL)
4564                     return false;
4565                   indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4566                   if (indx == (bfd_size_type) -1)
4567                     return false;
4568                   isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4569                 }
4570             }
4571
4572           if (isym.n_sclass != C_BSTAT
4573               && isym.n_sclass != C_ESTAT
4574               && isym.n_sclass != C_DECL
4575               && isym.n_scnum > 0)
4576             {
4577               isym.n_scnum = (*csectpp)->output_section->target_index;
4578               isym.n_value += ((*csectpp)->output_section->vma
4579                                + (*csectpp)->output_offset
4580                                - (*csectpp)->vma);
4581             }
4582
4583           /* The value of a C_FILE symbol is the symbol index of the
4584              next C_FILE symbol.  The value of the last C_FILE symbol
4585              is -1.  We try to get this right, below, just before we
4586              write the symbols out, but in the general case we may
4587              have to write the symbol out twice.  */
4588           if (isym.n_sclass == C_FILE)
4589             {
4590               if (finfo->last_file_index != -1
4591                   && finfo->last_file.n_value != (bfd_vma) output_index)
4592                 {
4593                   /* We must correct the value of the last C_FILE entry.  */
4594                   finfo->last_file.n_value = output_index;
4595                   if ((bfd_size_type) finfo->last_file_index >= syment_base)
4596                     {
4597                       /* The last C_FILE symbol is in this input file.  */
4598                       bfd_coff_swap_sym_out (output_bfd,
4599                                              (PTR) &finfo->last_file,
4600                                              (PTR) (finfo->outsyms
4601                                                     + ((finfo->last_file_index
4602                                                         - syment_base)
4603                                                        * osymesz)));
4604                     }
4605                   else
4606                     {
4607                       /* We have already written out the last C_FILE
4608                          symbol.  We need to write it out again.  We
4609                          borrow *outsym temporarily.  */
4610                       file_ptr pos;
4611
4612                       bfd_coff_swap_sym_out (output_bfd,
4613                                              (PTR) &finfo->last_file,
4614                                              (PTR) outsym);
4615
4616                       pos = obj_sym_filepos (output_bfd);
4617                       pos += finfo->last_file_index * osymesz;
4618                       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4619                           || (bfd_bwrite (outsym, osymesz, output_bfd)
4620                               != osymesz))
4621                         return false;
4622                     }
4623                 }
4624
4625               finfo->last_file_index = output_index;
4626               finfo->last_file = isym;
4627             }
4628
4629           /* The value of a C_BINCL or C_EINCL symbol is a file offset
4630              into the line numbers.  We update the symbol values when
4631              we handle the line numbers.  */
4632           if (isym.n_sclass == C_BINCL
4633               || isym.n_sclass == C_EINCL)
4634             {
4635               isym.n_value = finfo->line_filepos;
4636               ++incls;
4637             }
4638
4639           /* Output the symbol.  */
4640
4641           bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4642
4643           *indexp = output_index;
4644
4645           if (isym.n_sclass == C_EXT)
4646             {
4647               long indx;
4648               struct xcoff_link_hash_entry *h;
4649
4650               indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4651                       / isymesz);
4652               h = obj_xcoff_sym_hashes (input_bfd)[indx];
4653               BFD_ASSERT (h != NULL);
4654               h->indx = output_index;
4655             }
4656
4657           /* If this is a symbol in the TOC which we may have merged
4658              (class XMC_TC), remember the symbol index of the TOC
4659              symbol.  */
4660           if (isym.n_sclass == C_HIDEXT
4661               && aux.x_csect.x_smclas == XMC_TC
4662               && *sym_hash != NULL)
4663             {
4664               BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4665               BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4666               (*sym_hash)->u.toc_indx = output_index;
4667             }
4668
4669           output_index += add;
4670           outsym += add * osymesz;
4671         }
4672
4673       esym += add * isymesz;
4674       isymp += add;
4675       csectpp += add;
4676       sym_hash += add;
4677       if (debug_index != NULL)
4678         debug_index += add;
4679       ++indexp;
4680       for (--add; add > 0; --add)
4681         *indexp++ = -1;
4682     }
4683
4684   /* Fix up the aux entries and the C_BSTAT symbols.  This must be
4685      done in a separate pass, because we don't know the correct symbol
4686      indices until we have already decided which symbols we are going
4687      to keep.  */
4688
4689   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4690   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4691   isymp = finfo->internal_syms;
4692   indexp = finfo->sym_indices;
4693   csectpp = xcoff_data (input_bfd)->csects;
4694   outsym = finfo->outsyms;
4695   while (esym < esym_end)
4696     {
4697       int add;
4698
4699       add = 1 + isymp->n_numaux;
4700
4701       if (*indexp < 0)
4702         esym += add * isymesz;
4703       else
4704         {
4705           int i;
4706
4707           if (isymp->n_sclass == C_BSTAT)
4708             {
4709               struct internal_syment isym;
4710
4711               bfd_vma indx;
4712
4713               /* The value of a C_BSTAT symbol is the symbol table
4714                  index of the containing csect.  */
4715               bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4716               indx = isym.n_value;
4717               if (indx < obj_raw_syment_count (input_bfd))
4718                 {
4719                   long symindx;
4720
4721                   symindx = finfo->sym_indices[indx];
4722                   if (symindx < 0)
4723                     isym.n_value = 0;
4724                   else
4725                     isym.n_value = symindx;
4726                   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4727                                          (PTR) outsym);
4728                 }
4729             }
4730
4731           esym += isymesz;
4732           outsym += osymesz;
4733
4734           for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4735             {
4736               union internal_auxent aux;
4737
4738               bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4739                                     isymp->n_sclass, i, isymp->n_numaux,
4740                                     (PTR) &aux);
4741
4742               if (isymp->n_sclass == C_FILE)
4743                 {
4744                   /* This is the file name (or some comment put in by
4745                      the compiler).  If it is long, we must put it in
4746                      the string table.  */
4747                   if (aux.x_file.x_n.x_zeroes == 0
4748                       && aux.x_file.x_n.x_offset != 0)
4749                     {
4750                       const char *filename;
4751                       bfd_size_type indx;
4752
4753                       BFD_ASSERT (aux.x_file.x_n.x_offset
4754                                   >= STRING_SIZE_SIZE);
4755                       if (strings == NULL)
4756                         {
4757                           strings = _bfd_coff_read_string_table (input_bfd);
4758                           if (strings == NULL)
4759                             return false;
4760                         }
4761                       filename = strings + aux.x_file.x_n.x_offset;
4762                       indx = _bfd_stringtab_add (finfo->strtab, filename,
4763                                                  hash, copy);
4764                       if (indx == (bfd_size_type) -1)
4765                         return false;
4766                       aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4767                     }
4768                 }
4769               else if ((isymp->n_sclass == C_EXT
4770                         || isymp->n_sclass == C_HIDEXT)
4771                        && i + 1 == isymp->n_numaux)
4772                 {
4773
4774                   /* We don't support type checking.  I don't know if
4775                      anybody does.  */
4776                   aux.x_csect.x_parmhash = 0;
4777                   /* I don't think anybody uses these fields, but we'd
4778                      better clobber them just in case.  */
4779                   aux.x_csect.x_stab = 0;
4780                   aux.x_csect.x_snstab = 0;
4781
4782                   if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4783                     {
4784                       unsigned long indx;
4785
4786                       indx = aux.x_csect.x_scnlen.l;
4787                       if (indx < obj_raw_syment_count (input_bfd))
4788                         {
4789                           long symindx;
4790
4791                           symindx = finfo->sym_indices[indx];
4792                           if (symindx < 0)
4793                             {
4794                               aux.x_csect.x_scnlen.l = 0;
4795                             }
4796                           else
4797                             {
4798                               aux.x_csect.x_scnlen.l = symindx;
4799                             }
4800                         }
4801                     }
4802                 }
4803               else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4804                 {
4805                   unsigned long indx;
4806
4807                   if (ISFCN (isymp->n_type)
4808                       || ISTAG (isymp->n_sclass)
4809                       || isymp->n_sclass == C_BLOCK
4810                       || isymp->n_sclass == C_FCN)
4811                     {
4812                       indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4813                       if (indx > 0
4814                           && indx < obj_raw_syment_count (input_bfd))
4815                         {
4816                           /* We look forward through the symbol for
4817                              the index of the next symbol we are going
4818                              to include.  I don't know if this is
4819                              entirely right.  */
4820                           while (finfo->sym_indices[indx] < 0
4821                                  && indx < obj_raw_syment_count (input_bfd))
4822                             ++indx;
4823                           if (indx >= obj_raw_syment_count (input_bfd))
4824                             indx = output_index;
4825                           else
4826                             indx = finfo->sym_indices[indx];
4827                           aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4828
4829                         }
4830                     }
4831
4832                   indx = aux.x_sym.x_tagndx.l;
4833                   if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4834                     {
4835                       long symindx;
4836
4837                       symindx = finfo->sym_indices[indx];
4838                       if (symindx < 0)
4839                         aux.x_sym.x_tagndx.l = 0;
4840                       else
4841                         aux.x_sym.x_tagndx.l = symindx;
4842                     }
4843
4844                 }
4845
4846               /* Copy over the line numbers, unless we are stripping
4847                  them.  We do this on a symbol by symbol basis in
4848                  order to more easily handle garbage collection.  */
4849               if ((isymp->n_sclass == C_EXT
4850                    || isymp->n_sclass == C_HIDEXT)
4851                   && i == 0
4852                   && isymp->n_numaux > 1
4853                   && ISFCN (isymp->n_type)
4854                   && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4855                 {
4856                   if (finfo->info->strip != strip_none
4857                       && finfo->info->strip != strip_some)
4858                     aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4859                   else
4860                     {
4861                       asection *enclosing;
4862                       unsigned int enc_count;
4863                       bfd_signed_vma linoff;
4864                       struct internal_lineno lin;
4865
4866                       o = *csectpp;
4867                       enclosing = xcoff_section_data (abfd, o)->enclosing;
4868                       enc_count = xcoff_section_data (abfd, o)->lineno_count;
4869                       if (oline != enclosing)
4870                         {
4871                           file_ptr pos = enclosing->line_filepos;
4872                           bfd_size_type amt = linesz * enc_count;
4873                           if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4874                               || (bfd_bread (finfo->linenos, amt, input_bfd)
4875                                   != amt))
4876                             return false;
4877                           oline = enclosing;
4878                         }
4879
4880                       linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4881                                 - enclosing->line_filepos);
4882
4883                       bfd_coff_swap_lineno_in (input_bfd,
4884                                                (PTR) (finfo->linenos + linoff),
4885                                                (PTR) &lin);
4886                       if (lin.l_lnno != 0
4887                           || ((bfd_size_type) lin.l_addr.l_symndx
4888                               != ((esym
4889                                    - isymesz
4890                                    - ((bfd_byte *)
4891                                       obj_coff_external_syms (input_bfd)))
4892                                   / isymesz)))
4893                         aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4894                       else
4895                         {
4896                           bfd_byte *linpend, *linp;
4897                           bfd_vma offset;
4898                           bfd_size_type count;
4899
4900                           lin.l_addr.l_symndx = *indexp;
4901                           bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4902                                                     (PTR) (finfo->linenos
4903                                                            + linoff));
4904
4905                           linpend = (finfo->linenos
4906                                      + enc_count * linesz);
4907                           offset = (o->output_section->vma
4908                                     + o->output_offset
4909                                     - o->vma);
4910                           for (linp = finfo->linenos + linoff + linesz;
4911                                linp < linpend;
4912                                linp += linesz)
4913                             {
4914                               bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4915                                                        (PTR) &lin);
4916                               if (lin.l_lnno == 0)
4917                                 break;
4918                               lin.l_addr.l_paddr += offset;
4919                               bfd_coff_swap_lineno_out (output_bfd,
4920                                                         (PTR) &lin,
4921                                                         (PTR) linp);
4922                             }
4923
4924                           count = (linp - (finfo->linenos + linoff)) / linesz;
4925
4926                           aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4927                             (o->output_section->line_filepos
4928                              + o->output_section->lineno_count * linesz);
4929
4930                           if (bfd_seek (output_bfd,
4931                                         aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4932                                         SEEK_SET) != 0
4933                               || (bfd_bwrite (finfo->linenos + linoff,
4934                                              linesz * count, output_bfd)
4935                                   != linesz * count))
4936                             return false;
4937
4938                           o->output_section->lineno_count += count;
4939
4940                           if (incls > 0)
4941                             {
4942                               struct internal_syment *iisp, *iispend;
4943                               long *iindp;
4944                               bfd_byte *oos;
4945                               int iiadd;
4946
4947                               /* Update any C_BINCL or C_EINCL symbols
4948                                  that refer to a line number in the
4949                                  range we just output.  */
4950                               iisp = finfo->internal_syms;
4951                               iispend = (iisp
4952                                          + obj_raw_syment_count (input_bfd));
4953                               iindp = finfo->sym_indices;
4954                               oos = finfo->outsyms;
4955                               while (iisp < iispend)
4956                                 {
4957                                   if (*iindp >= 0
4958                                       && (iisp->n_sclass == C_BINCL
4959                                           || iisp->n_sclass == C_EINCL)
4960                                       && ((bfd_size_type) iisp->n_value
4961                                           >= (bfd_size_type)(enclosing->line_filepos + linoff))
4962                                       && ((bfd_size_type) iisp->n_value
4963                                           < (enclosing->line_filepos
4964                                              + enc_count * linesz)))
4965                                     {
4966                                       struct internal_syment iis;
4967
4968                                       bfd_coff_swap_sym_in (output_bfd,
4969                                                             (PTR) oos,
4970                                                             (PTR) &iis);
4971                                       iis.n_value =
4972                                         (iisp->n_value
4973                                          - enclosing->line_filepos
4974                                          - linoff
4975                                          + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4976                                       bfd_coff_swap_sym_out (output_bfd,
4977                                                              (PTR) &iis,
4978                                                              (PTR) oos);
4979                                       --incls;
4980                                     }
4981
4982                                   iiadd = 1 + iisp->n_numaux;
4983                                   if (*iindp >= 0)
4984                                     oos += iiadd * osymesz;
4985                                   iisp += iiadd;
4986                                   iindp += iiadd;
4987                                 }
4988                             }
4989                         }
4990                     }
4991                 }
4992
4993               bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4994                                      isymp->n_sclass, i, isymp->n_numaux,
4995                                      (PTR) outsym);
4996               outsym += osymesz;
4997               esym += isymesz;
4998             }
4999         }
5000
5001       indexp += add;
5002       isymp += add;
5003       csectpp += add;
5004     }
5005
5006   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5007      symbol will be the first symbol in the next input file.  In the
5008      normal case, this will save us from writing out the C_FILE symbol
5009      again.  */
5010   if (finfo->last_file_index != -1
5011       && (bfd_size_type) finfo->last_file_index >= syment_base)
5012     {
5013       finfo->last_file.n_value = output_index;
5014       bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5015                              (PTR) (finfo->outsyms
5016                                     + ((finfo->last_file_index - syment_base)
5017                                        * osymesz)));
5018     }
5019
5020   /* Write the modified symbols to the output file.  */
5021   if (outsym > finfo->outsyms)
5022     {
5023       file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
5024       bfd_size_type amt = outsym - finfo->outsyms;
5025       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5026           || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5027         return false;
5028
5029       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5030                    + (outsym - finfo->outsyms) / osymesz)
5031                   == output_index);
5032
5033       obj_raw_syment_count (output_bfd) = output_index;
5034     }
5035
5036   /* Don't let the linker relocation routines discard the symbols.  */
5037   keep_syms = obj_coff_keep_syms (input_bfd);
5038   obj_coff_keep_syms (input_bfd) = true;
5039
5040   /* Relocate the contents of each section.  */
5041   for (o = input_bfd->sections; o != NULL; o = o->next)
5042     {
5043
5044       bfd_byte *contents;
5045
5046       if (! o->linker_mark)
5047         {
5048           /* This section was omitted from the link.  */
5049           continue;
5050         }
5051
5052       if ((o->flags & SEC_HAS_CONTENTS) == 0
5053           || o->_raw_size == 0
5054           || (o->flags & SEC_IN_MEMORY) != 0)
5055         continue;
5056
5057       /* We have set filepos correctly for the sections we created to
5058          represent csects, so bfd_get_section_contents should work.  */
5059       if (coff_section_data (input_bfd, o) != NULL
5060           && coff_section_data (input_bfd, o)->contents != NULL)
5061         contents = coff_section_data (input_bfd, o)->contents;
5062       else {
5063         if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5064                                         (file_ptr) 0, o->_raw_size))
5065           return false;
5066         contents = finfo->contents;
5067       }
5068
5069       if ((o->flags & SEC_RELOC) != 0)
5070         {
5071           int target_index;
5072           struct internal_reloc *internal_relocs;
5073           struct internal_reloc *irel;
5074           bfd_vma offset;
5075           struct internal_reloc *irelend;
5076           struct xcoff_link_hash_entry **rel_hash;
5077           long r_symndx;
5078
5079           /* Read in the relocs.  */
5080           target_index = o->output_section->target_index;
5081           internal_relocs = (xcoff_read_internal_relocs
5082                              (input_bfd, o, false, finfo->external_relocs,
5083                               true,
5084                               (finfo->section_info[target_index].relocs
5085                                + o->output_section->reloc_count)));
5086           if (internal_relocs == NULL)
5087             return false;
5088
5089           /* Call processor specific code to relocate the section
5090              contents.  */
5091           if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5092                                            input_bfd, o,
5093                                            contents,
5094                                            internal_relocs,
5095                                            finfo->internal_syms,
5096                                            xcoff_data (input_bfd)->csects))
5097             return false;
5098
5099           offset = o->output_section->vma + o->output_offset - o->vma;
5100           irel = internal_relocs;
5101           irelend = irel + o->reloc_count;
5102           rel_hash = (finfo->section_info[target_index].rel_hashes
5103                       + o->output_section->reloc_count);
5104           for (; irel < irelend; irel++, rel_hash++)
5105             {
5106               struct xcoff_link_hash_entry *h = NULL;
5107               struct internal_ldrel ldrel;
5108               boolean quiet;
5109
5110               *rel_hash = NULL;
5111
5112               /* Adjust the reloc address and symbol index.  */
5113
5114               irel->r_vaddr += offset;
5115
5116               r_symndx = irel->r_symndx;
5117
5118               if (r_symndx == -1)
5119                 h = NULL;
5120               else
5121                 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5122
5123               if (r_symndx != -1 && finfo->info->strip != strip_all)
5124                 {
5125                   if (h != NULL
5126                       && h->smclas != XMC_TD
5127                       && (irel->r_type == R_TOC
5128                           || irel->r_type == R_GL
5129                           || irel->r_type == R_TCL
5130                           || irel->r_type == R_TRL
5131                           || irel->r_type == R_TRLA))
5132                     {
5133                       /* This is a TOC relative reloc with a symbol
5134                          attached.  The symbol should be the one which
5135                          this reloc is for.  We want to make this
5136                          reloc against the TOC address of the symbol,
5137                          not the symbol itself.  */
5138                       BFD_ASSERT (h->toc_section != NULL);
5139                       BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5140                       if (h->u.toc_indx != -1)
5141                         irel->r_symndx = h->u.toc_indx;
5142                       else
5143                         {
5144                           struct xcoff_toc_rel_hash *n;
5145                           struct xcoff_link_section_info *si;
5146                           bfd_size_type amt;
5147
5148                           amt = sizeof (struct xcoff_toc_rel_hash);
5149                           n = ((struct xcoff_toc_rel_hash *)
5150                                bfd_alloc (finfo->output_bfd, amt));
5151                           if (n == NULL)
5152                             return false;
5153                           si = finfo->section_info + target_index;
5154                           n->next = si->toc_rel_hashes;
5155                           n->h = h;
5156                           n->rel = irel;
5157                           si->toc_rel_hashes = n;
5158                         }
5159                     }
5160                   else if (h != NULL)
5161                     {
5162                       /* This is a global symbol.  */
5163                       if (h->indx >= 0)
5164                         irel->r_symndx = h->indx;
5165                       else
5166                         {
5167                           /* This symbol is being written at the end
5168                              of the file, and we do not yet know the
5169                              symbol index.  We save the pointer to the
5170                              hash table entry in the rel_hash list.
5171                              We set the indx field to -2 to indicate
5172                              that this symbol must not be stripped.  */
5173                           *rel_hash = h;
5174                           h->indx = -2;
5175                         }
5176                     }
5177                   else
5178                     {
5179                       long indx;
5180
5181                       indx = finfo->sym_indices[r_symndx];
5182
5183                       if (indx == -1)
5184                         {
5185                           struct internal_syment *is;
5186
5187                           /* Relocations against a TC0 TOC anchor are
5188                              automatically transformed to be against
5189                              the TOC anchor in the output file.  */
5190                           is = finfo->internal_syms + r_symndx;
5191                           if (is->n_sclass == C_HIDEXT
5192                               && is->n_numaux > 0)
5193                             {
5194                               PTR auxptr;
5195                               union internal_auxent aux;
5196
5197                               auxptr = ((PTR)
5198                                         (((bfd_byte *)
5199                                           obj_coff_external_syms (input_bfd))
5200                                          + ((r_symndx + is->n_numaux)
5201                                             * isymesz)));
5202                               bfd_coff_swap_aux_in (input_bfd, auxptr,
5203                                                     is->n_type, is->n_sclass,
5204                                                     is->n_numaux - 1,
5205                                                     is->n_numaux,
5206                                                     (PTR) &aux);
5207                               if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5208                                   && aux.x_csect.x_smclas == XMC_TC0)
5209                                 indx = finfo->toc_symindx;
5210                             }
5211                         }
5212
5213                       if (indx != -1)
5214                         irel->r_symndx = indx;
5215                       else
5216                         {
5217
5218                           struct internal_syment *is;
5219
5220                           const char *name;
5221                           char buf[SYMNMLEN + 1];
5222
5223                           /* This reloc is against a symbol we are
5224                              stripping.  It would be possible to handle
5225                              this case, but I don't think it's worth it.  */
5226                           is = finfo->internal_syms + r_symndx;
5227
5228                           name = (_bfd_coff_internal_syment_name
5229                                   (input_bfd, is, buf));
5230
5231                           if (name == NULL)
5232                             return false;
5233
5234                           if (! ((*finfo->info->callbacks->unattached_reloc)
5235                                  (finfo->info, name, input_bfd, o,
5236                                   irel->r_vaddr)))
5237                             return false;
5238                         }
5239                     }
5240                 }
5241
5242               quiet = false;
5243               switch (irel->r_type)
5244                 {
5245                 default:
5246                   if (h == NULL
5247                       || h->root.type == bfd_link_hash_defined
5248                       || h->root.type == bfd_link_hash_defweak
5249                       || h->root.type == bfd_link_hash_common)
5250                     break;
5251                   /* Fall through.  */
5252                 case R_POS:
5253                 case R_NEG:
5254                 case R_RL:
5255                 case R_RLA:
5256                   /* This reloc needs to be copied into the .loader
5257                      section.  */
5258                   ldrel.l_vaddr = irel->r_vaddr;
5259                   if (r_symndx == -1)
5260                     ldrel.l_symndx = -(bfd_size_type ) 1;
5261                   else if (h == NULL
5262                            || (h->root.type == bfd_link_hash_defined
5263                                || h->root.type == bfd_link_hash_defweak
5264                                || h->root.type == bfd_link_hash_common))
5265                     {
5266                       asection *sec;
5267
5268                       if (h == NULL)
5269                         sec = xcoff_data (input_bfd)->csects[r_symndx];
5270                       else if (h->root.type == bfd_link_hash_common)
5271                         sec = h->root.u.c.p->section;
5272                       else
5273                         sec = h->root.u.def.section;
5274                       sec = sec->output_section;
5275
5276                       if (strcmp (sec->name, ".text") == 0)
5277                         ldrel.l_symndx = 0;
5278                       else if (strcmp (sec->name, ".data") == 0)
5279                         ldrel.l_symndx = 1;
5280                       else if (strcmp (sec->name, ".bss") == 0)
5281                         ldrel.l_symndx = 2;
5282                       else
5283                         {
5284                           (*_bfd_error_handler)
5285                             (_("%s: loader reloc in unrecognized section `%s'"),
5286                              bfd_archive_filename (input_bfd),
5287                              sec->name);
5288                           bfd_set_error (bfd_error_nonrepresentable_section);
5289                           return false;
5290                         }
5291                     }
5292                   else
5293                     {
5294                       if (! finfo->info->relocateable
5295                           && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5296                           && (h->flags & XCOFF_IMPORT) == 0)
5297                         {
5298                           /* We already called the undefined_symbol
5299                              callback for this relocation, in
5300                              _bfd_ppc_xcoff_relocate_section.  Don't
5301                              issue any more warnings.  */
5302                           quiet = true;
5303                         }
5304                       if (h->ldindx < 0 && ! quiet)
5305                         {
5306                           (*_bfd_error_handler)
5307                             (_("%s: `%s' in loader reloc but not loader sym"),
5308                              bfd_archive_filename (input_bfd),
5309                              h->root.root.string);
5310                           bfd_set_error (bfd_error_bad_value);
5311                           return false;
5312                         }
5313                       ldrel.l_symndx = h->ldindx;
5314                     }
5315                   ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5316                   ldrel.l_rsecnm = o->output_section->target_index;
5317                   if (xcoff_hash_table (finfo->info)->textro
5318                       && strcmp (o->output_section->name, ".text") == 0
5319                       && ! quiet)
5320                     {
5321                       (*_bfd_error_handler)
5322                         (_("%s: loader reloc in read-only section %s"),
5323                          bfd_archive_filename (input_bfd),
5324                          bfd_get_section_name (finfo->output_bfd,
5325                                                o->output_section));
5326                       bfd_set_error (bfd_error_invalid_operation);
5327                       return false;
5328                     }
5329                   bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
5330                                             finfo->ldrel);
5331
5332                   finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5333                   break;
5334
5335                 case R_TOC:
5336                 case R_GL:
5337                 case R_TCL:
5338                 case R_TRL:
5339                 case R_TRLA:
5340                   /* We should never need a .loader reloc for a TOC
5341                      relative reloc.  */
5342                   break;
5343                 }
5344             }
5345
5346           o->output_section->reloc_count += o->reloc_count;
5347         }
5348
5349       /* Write out the modified section contents.  */
5350       if (! bfd_set_section_contents (output_bfd, o->output_section,
5351                                       contents, (file_ptr) o->output_offset,
5352                                       (o->_cooked_size != 0
5353                                        ? o->_cooked_size
5354                                        : o->_raw_size)))
5355         return false;
5356     }
5357
5358   obj_coff_keep_syms (input_bfd) = keep_syms;
5359
5360   if (! finfo->info->keep_memory)
5361     {
5362       if (! _bfd_coff_free_symbols (input_bfd))
5363         return false;
5364     }
5365
5366   return true;
5367 }
5368
5369 #undef N_TMASK
5370 #undef N_BTSHFT
5371
5372 /* Write out a non-XCOFF global symbol.  */
5373
5374
5375 static boolean
5376 xcoff_write_global_symbol (h, inf)
5377      struct xcoff_link_hash_entry *h;
5378      PTR inf;
5379 {
5380   struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
5381   bfd *output_bfd;
5382   bfd_byte *outsym;
5383   struct internal_syment isym;
5384   union internal_auxent aux;
5385   boolean result;
5386   file_ptr pos;
5387   bfd_size_type amt;
5388
5389   output_bfd = finfo->output_bfd;
5390   outsym = finfo->outsyms;
5391
5392   if (h->root.type == bfd_link_hash_warning)
5393     {
5394       h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5395       if (h->root.type == bfd_link_hash_new)
5396         return true;
5397     }
5398
5399   /* If this symbol was garbage collected, just skip it.  */
5400   if (xcoff_hash_table (finfo->info)->gc
5401       && (h->flags & XCOFF_MARK) == 0)
5402     return true;
5403
5404   /* If we need a .loader section entry, write it out.  */
5405   if (h->ldsym != NULL)
5406     {
5407       struct internal_ldsym *ldsym;
5408       bfd *impbfd;
5409
5410       ldsym = h->ldsym;
5411
5412       if (h->root.type == bfd_link_hash_undefined
5413           || h->root.type == bfd_link_hash_undefweak)
5414         {
5415
5416           ldsym->l_value = 0;
5417           ldsym->l_scnum = N_UNDEF;
5418           ldsym->l_smtype = XTY_ER;
5419           impbfd = h->root.u.undef.abfd;
5420
5421         }
5422       else if (h->root.type == bfd_link_hash_defined
5423                || h->root.type == bfd_link_hash_defweak)
5424         {
5425
5426           asection *sec;
5427
5428           sec = h->root.u.def.section;
5429           ldsym->l_value = (sec->output_section->vma
5430                             + sec->output_offset
5431                             + h->root.u.def.value);
5432           ldsym->l_scnum = sec->output_section->target_index;
5433           ldsym->l_smtype = XTY_SD;
5434           impbfd = sec->owner;
5435
5436         }
5437       else
5438         abort ();
5439
5440       if (((h->flags & XCOFF_DEF_REGULAR) == 0
5441            && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5442           || (h->flags & XCOFF_IMPORT) != 0)
5443         {
5444           /* Clear l_smtype
5445              Import symbols are defined so the check above will make
5446              the l_smtype XTY_SD.  But this is not correct, it should
5447              be cleared.  */
5448           ldsym->l_smtype |= L_IMPORT;
5449         }
5450
5451       if (((h->flags & XCOFF_DEF_REGULAR) != 0
5452            && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5453           || (h->flags & XCOFF_EXPORT) != 0)
5454         {
5455           ldsym->l_smtype |= L_EXPORT;
5456         }
5457
5458       if ((h->flags & XCOFF_ENTRY) != 0)
5459         {
5460           ldsym->l_smtype |= L_ENTRY;
5461         }
5462
5463       if ((h->flags & XCOFF_RTINIT) != 0)
5464         {
5465           ldsym->l_smtype = XTY_SD;
5466         }
5467
5468       ldsym->l_smclas = h->smclas;
5469
5470       if (ldsym->l_smtype & L_IMPORT)
5471         {
5472           if ((h->root.type == bfd_link_hash_defined
5473                || h->root.type == bfd_link_hash_defweak)
5474               && (h->root.u.def.value != 0))
5475             {
5476               ldsym->l_smclas = XMC_XO;
5477             }
5478           else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5479                    (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5480             {
5481               ldsym->l_smclas = XMC_SV3264;
5482             }
5483           else if (h->flags & XCOFF_SYSCALL32)
5484             {
5485               ldsym->l_smclas = XMC_SV;
5486             }
5487           else if (h->flags & XCOFF_SYSCALL64)
5488             {
5489               ldsym->l_smclas = XMC_SV64;
5490             }
5491         }
5492
5493       if (ldsym->l_ifile == -(bfd_size_type) 1)
5494         {
5495           ldsym->l_ifile = 0;
5496         }
5497       else if (ldsym->l_ifile == 0)
5498         {
5499           if ((ldsym->l_smtype & L_IMPORT) == 0)
5500             {
5501               ldsym->l_ifile = 0;
5502             }
5503           else if (impbfd == NULL)
5504             {
5505               ldsym->l_ifile = 0;
5506             }
5507           else
5508             {
5509               BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5510               ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5511             }
5512         }
5513
5514       ldsym->l_parm = 0;
5515
5516       BFD_ASSERT (h->ldindx >= 0);
5517
5518       bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5519                                 (finfo->ldsym +
5520                                  (h->ldindx - 3)
5521                                  * bfd_xcoff_ldsymsz(finfo->output_bfd)));
5522       h->ldsym = NULL;
5523     }
5524
5525   /* If this symbol needs global linkage code, write it out.  */
5526   if (h->root.type == bfd_link_hash_defined
5527       && (h->root.u.def.section
5528           == xcoff_hash_table (finfo->info)->linkage_section))
5529     {
5530       bfd_byte *p;
5531       bfd_vma tocoff;
5532       unsigned int i;
5533
5534       p = h->root.u.def.section->contents + h->root.u.def.value;
5535
5536       /* The first instruction in the global linkage code loads a
5537          specific TOC element.  */
5538       tocoff = (h->descriptor->toc_section->output_section->vma
5539                 + h->descriptor->toc_section->output_offset
5540                 - xcoff_data (output_bfd)->toc);
5541
5542       if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5543         {
5544           tocoff += h->descriptor->u.toc_offset;
5545         }
5546
5547
5548       /* The first instruction in the glink code needs to be
5549          cooked to to hold the correct offset in the toc.  The
5550          rest are just output raw.  */
5551       bfd_put_32 (output_bfd,
5552                   bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5553
5554       /* Start with i == 1 to get past the first instruction done above
5555          The /4 is because the glink code is in bytes and we are going
5556          4 at a pop.  */
5557       for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5558         {
5559           bfd_put_32 (output_bfd,
5560                       (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5561                       &p[4 * i]);
5562         }
5563     }
5564
5565   /* If we created a TOC entry for this symbol, write out the required
5566      relocs.  */
5567   if ((h->flags & XCOFF_SET_TOC) != 0)
5568     {
5569       asection *tocsec;
5570       asection *osec;
5571       int oindx;
5572       struct internal_reloc *irel;
5573       struct internal_ldrel ldrel;
5574       struct internal_syment irsym;
5575       union internal_auxent iraux;
5576
5577       tocsec = h->toc_section;
5578       osec = tocsec->output_section;
5579       oindx = osec->target_index;
5580       irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5581       irel->r_vaddr = (osec->vma
5582                        + tocsec->output_offset
5583                        + h->u.toc_offset);
5584
5585
5586       if (h->indx >= 0)
5587         {
5588           irel->r_symndx = h->indx;
5589         }
5590       else
5591         {
5592           h->indx = -2;
5593           irel->r_symndx = obj_raw_syment_count (output_bfd);
5594         }
5595
5596       BFD_ASSERT (h->ldindx >= 0);
5597
5598       /* Initialize the aux union here instead of closer to when it is
5599          written out below because the length of the csect depends on
5600          whether the output is 32 or 64 bit.  */
5601       memset (&iraux, 0, sizeof iraux);
5602       iraux.x_csect.x_smtyp = XTY_SD;
5603       /* iraux.x_csect.x_scnlen.l = 4 or 8, see below */
5604       iraux.x_csect.x_smclas = XMC_TC;
5605
5606       /* 32 bit uses a 32 bit R_POS to do the relocations
5607          64 bit uses a 64 bit R_POS to do the relocations
5608
5609          Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5610
5611          Which one is determined by the backend.  */
5612       if (bfd_xcoff_is_xcoff64 (output_bfd))
5613         {
5614           irel->r_size = 63;
5615           iraux.x_csect.x_scnlen.l = 8;
5616         }
5617       else if (bfd_xcoff_is_xcoff32 (output_bfd))
5618         {
5619           irel->r_size = 31;
5620           iraux.x_csect.x_scnlen.l = 4;
5621         }
5622       else
5623         {
5624           return false;
5625         }
5626       irel->r_type = R_POS;
5627       finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5628       ++osec->reloc_count;
5629
5630       ldrel.l_vaddr = irel->r_vaddr;
5631       ldrel.l_symndx = h->ldindx;
5632       ldrel.l_rtype = (irel->r_size << 8) | R_POS;
5633       ldrel.l_rsecnm = oindx;
5634       bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5635       finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5636
5637       /* We need to emit a symbol to define a csect which holds
5638          the reloc.  */
5639       if (finfo->info->strip != strip_all)
5640         {
5641
5642           result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
5643                                               &irsym, h->root.root.string);
5644           if (false == result)
5645             {
5646               return false;
5647             }
5648
5649           irsym.n_value = irel->r_vaddr;
5650           irsym.n_scnum = osec->target_index;
5651           irsym.n_sclass = C_HIDEXT;
5652           irsym.n_type = T_NULL;
5653           irsym.n_numaux = 1;
5654
5655           bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5656           outsym += bfd_coff_symesz (output_bfd);
5657
5658           /* note : iraux is initialized above */
5659           bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5660                                  0, 1, (PTR) outsym);
5661           outsym += bfd_coff_auxesz (output_bfd);
5662
5663           if (h->indx >= 0)
5664             {
5665               /* We aren't going to write out the symbols below, so we
5666                  need to write them out now.  */
5667               pos = obj_sym_filepos (output_bfd);
5668               pos += (obj_raw_syment_count (output_bfd)
5669                       * bfd_coff_symesz (output_bfd));
5670               amt = outsym - finfo->outsyms;
5671               if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5672                   || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5673                 return false;
5674               obj_raw_syment_count (output_bfd) +=
5675                 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5676
5677               outsym = finfo->outsyms;
5678             }
5679         }
5680     }
5681
5682   /* If this symbol is a specially defined function descriptor, write
5683      it out.  The first word is the address of the function code
5684      itself, the second word is the address of the TOC, and the third
5685      word is zero.
5686
5687      32 bit vs 64 bit
5688      The addresses for the 32 bit will take 4 bytes and the addresses
5689      for 64 bit will take 8 bytes.  Similar for the relocs.  This type
5690      of logic was also done above to create a TOC entry in
5691      xcoff_write_global_symbol.  */
5692   if ((h->flags & XCOFF_DESCRIPTOR) != 0
5693       && h->root.type == bfd_link_hash_defined
5694       && (h->root.u.def.section
5695           == xcoff_hash_table (finfo->info)->descriptor_section))
5696     {
5697       asection *sec;
5698       asection *osec;
5699       int oindx;
5700       bfd_byte *p;
5701       struct xcoff_link_hash_entry *hentry;
5702       asection *esec;
5703       struct internal_reloc *irel;
5704       struct internal_ldrel ldrel;
5705       asection *tsec;
5706       unsigned int reloc_size, byte_size;
5707
5708       if (bfd_xcoff_is_xcoff64 (output_bfd))
5709         {
5710           reloc_size = 63;
5711           byte_size = 8;
5712         }
5713       else if (bfd_xcoff_is_xcoff32 (output_bfd))
5714         {
5715           reloc_size = 31;
5716           byte_size = 4;
5717         }
5718       else
5719         {
5720           return false;
5721         }
5722
5723       sec = h->root.u.def.section;
5724       osec = sec->output_section;
5725       oindx = osec->target_index;
5726       p = sec->contents + h->root.u.def.value;
5727
5728       hentry = h->descriptor;
5729       BFD_ASSERT (hentry != NULL
5730                   && (hentry->root.type == bfd_link_hash_defined
5731                       || hentry->root.type == bfd_link_hash_defweak));
5732       esec = hentry->root.u.def.section;
5733
5734       irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5735       irel->r_vaddr = (osec->vma
5736                        + sec->output_offset
5737                        + h->root.u.def.value);
5738       irel->r_symndx = esec->output_section->target_index;
5739       irel->r_type = R_POS;
5740       irel->r_size = reloc_size;
5741       finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5742       ++osec->reloc_count;
5743
5744       ldrel.l_vaddr = irel->r_vaddr;
5745       if (strcmp (esec->output_section->name, ".text") == 0)
5746         ldrel.l_symndx = 0;
5747       else if (strcmp (esec->output_section->name, ".data") == 0)
5748         ldrel.l_symndx = 1;
5749       else if (strcmp (esec->output_section->name, ".bss") == 0)
5750         ldrel.l_symndx = 2;
5751       else
5752         {
5753           (*_bfd_error_handler)
5754             (_("%s: loader reloc in unrecognized section `%s'"),
5755              bfd_get_filename (output_bfd),
5756              esec->output_section->name);
5757           bfd_set_error (bfd_error_nonrepresentable_section);
5758           return false;
5759         }
5760       ldrel.l_rtype = (reloc_size << 8) | R_POS;
5761       ldrel.l_rsecnm = oindx;
5762       bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5763       finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5764
5765       /* There are three items to write out,
5766          the address of the code
5767          the address of the toc anchor
5768          the environment pointer.
5769          We are ignoring the environment pointer.  So set it to zero.  */
5770       if (bfd_xcoff_is_xcoff64 (output_bfd))
5771         {
5772           bfd_put_64 (output_bfd,
5773                       (esec->output_section->vma + esec->output_offset
5774                        + hentry->root.u.def.value),
5775                       p);
5776           bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5777           bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5778         }
5779       else
5780         {
5781           /* 32 bit backend
5782              This logic was already called above so the error case where
5783              the backend is neither has already been checked.  */
5784           bfd_put_32 (output_bfd,
5785                       (esec->output_section->vma + esec->output_offset
5786                        + hentry->root.u.def.value),
5787                       p);
5788           bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5789           bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5790         }
5791
5792       tsec = coff_section_from_bfd_index (output_bfd,
5793                                           xcoff_data (output_bfd)->sntoc);
5794
5795       ++irel;
5796       irel->r_vaddr = (osec->vma
5797                        + sec->output_offset
5798                        + h->root.u.def.value
5799                        + byte_size);
5800       irel->r_symndx = tsec->output_section->target_index;
5801       irel->r_type = R_POS;
5802       irel->r_size = reloc_size;
5803       finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5804       ++osec->reloc_count;
5805
5806       ldrel.l_vaddr = irel->r_vaddr;
5807       if (strcmp (tsec->output_section->name, ".text") == 0)
5808         ldrel.l_symndx = 0;
5809       else if (strcmp (tsec->output_section->name, ".data") == 0)
5810         ldrel.l_symndx = 1;
5811       else if (strcmp (tsec->output_section->name, ".bss") == 0)
5812         ldrel.l_symndx = 2;
5813       else
5814         {
5815           (*_bfd_error_handler)
5816             (_("%s: loader reloc in unrecognized section `%s'"),
5817              bfd_get_filename (output_bfd),
5818              tsec->output_section->name);
5819           bfd_set_error (bfd_error_nonrepresentable_section);
5820           return false;
5821         }
5822       ldrel.l_rtype = (reloc_size << 8) | R_POS;
5823       ldrel.l_rsecnm = oindx;
5824       bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5825       finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5826     }
5827
5828   if (h->indx >= 0 || finfo->info->strip == strip_all)
5829     {
5830       BFD_ASSERT (outsym == finfo->outsyms);
5831       return true;
5832     }
5833
5834   if (h->indx != -2
5835       && (finfo->info->strip == strip_all
5836           || (finfo->info->strip == strip_some
5837               && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5838                                   false, false) == NULL)))
5839     {
5840       BFD_ASSERT (outsym == finfo->outsyms);
5841       return true;
5842     }
5843
5844   if (h->indx != -2
5845       && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5846     {
5847       BFD_ASSERT (outsym == finfo->outsyms);
5848       return true;
5849     }
5850
5851   memset (&aux, 0, sizeof aux);
5852
5853   h->indx = obj_raw_syment_count (output_bfd);
5854
5855   result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5856                                       h->root.root.string);
5857   if (false == result)
5858     {
5859       return false;
5860     }
5861
5862   if (h->root.type == bfd_link_hash_undefined
5863       || h->root.type == bfd_link_hash_undefweak)
5864     {
5865       isym.n_value = 0;
5866       isym.n_scnum = N_UNDEF;
5867       isym.n_sclass = C_EXT;
5868       aux.x_csect.x_smtyp = XTY_ER;
5869     }
5870   else if ((h->root.type == bfd_link_hash_defined
5871             || h->root.type == bfd_link_hash_defweak)
5872            && h->smclas == XMC_XO)
5873     {
5874       BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5875       isym.n_value = h->root.u.def.value;
5876       isym.n_scnum = N_UNDEF;
5877       isym.n_sclass = C_EXT;
5878       aux.x_csect.x_smtyp = XTY_ER;
5879     }
5880   else if (h->root.type == bfd_link_hash_defined
5881            || h->root.type == bfd_link_hash_defweak)
5882     {
5883       struct xcoff_link_size_list *l;
5884
5885       isym.n_value = (h->root.u.def.section->output_section->vma
5886                       + h->root.u.def.section->output_offset
5887                       + h->root.u.def.value);
5888       isym.n_scnum = h->root.u.def.section->output_section->target_index;
5889       isym.n_sclass = C_HIDEXT;
5890       aux.x_csect.x_smtyp = XTY_SD;
5891
5892       if ((h->flags & XCOFF_HAS_SIZE) != 0)
5893         {
5894           for (l = xcoff_hash_table (finfo->info)->size_list;
5895                l != NULL;
5896                l = l->next)
5897             {
5898               if (l->h == h)
5899                 {
5900                   aux.x_csect.x_scnlen.l = l->size;
5901                   break;
5902                 }
5903             }
5904         }
5905     }
5906   else if (h->root.type == bfd_link_hash_common)
5907     {
5908       isym.n_value = (h->root.u.c.p->section->output_section->vma
5909                       + h->root.u.c.p->section->output_offset);
5910       isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5911       isym.n_sclass = C_EXT;
5912       aux.x_csect.x_smtyp = XTY_CM;
5913       aux.x_csect.x_scnlen.l = h->root.u.c.size;
5914     }
5915   else
5916     abort ();
5917
5918   isym.n_type = T_NULL;
5919   isym.n_numaux = 1;
5920
5921   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5922   outsym += bfd_coff_symesz (output_bfd);
5923
5924   aux.x_csect.x_smclas = h->smclas;
5925   bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5926                          (PTR) outsym);
5927   outsym += bfd_coff_auxesz (output_bfd);
5928
5929   if ((h->root.type == bfd_link_hash_defined
5930        || h->root.type == bfd_link_hash_defweak)
5931       && h->smclas != XMC_XO)
5932     {
5933       /* We just output an SD symbol.  Now output an LD symbol.  */
5934
5935       h->indx += 2;
5936
5937       isym.n_sclass = C_EXT;
5938       bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5939       outsym += bfd_coff_symesz (output_bfd);
5940
5941       aux.x_csect.x_smtyp = XTY_LD;
5942       aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5943       bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5944                              (PTR) outsym);
5945       outsym += bfd_coff_auxesz (output_bfd);
5946     }
5947
5948   pos = obj_sym_filepos (output_bfd);
5949   pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5950   amt = outsym - finfo->outsyms;
5951   if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5952       || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5953     return false;
5954   obj_raw_syment_count (output_bfd) +=
5955     (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5956
5957   return true;
5958 }
5959
5960 /* Handle a link order which is supposed to generate a reloc.  */
5961
5962 static boolean
5963 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5964      bfd *output_bfd;
5965      struct xcoff_final_link_info *finfo;
5966      asection *output_section;
5967      struct bfd_link_order *link_order;
5968 {
5969   reloc_howto_type *howto;
5970   struct xcoff_link_hash_entry *h;
5971   asection *hsec;
5972   bfd_vma hval;
5973   bfd_vma addend;
5974   struct internal_reloc *irel;
5975   struct xcoff_link_hash_entry **rel_hash_ptr;
5976   struct internal_ldrel ldrel;
5977
5978   if (link_order->type == bfd_section_reloc_link_order)
5979     {
5980       /* We need to somehow locate a symbol in the right section.  The
5981          symbol must either have a value of zero, or we must adjust
5982          the addend by the value of the symbol.  FIXME: Write this
5983          when we need it.  The old linker couldn't handle this anyhow.  */
5984       abort ();
5985     }
5986
5987   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5988   if (howto == NULL)
5989     {
5990       bfd_set_error (bfd_error_bad_value);
5991       return false;
5992     }
5993
5994   h = ((struct xcoff_link_hash_entry *)
5995        bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5996                                      link_order->u.reloc.p->u.name,
5997                                      false, false, true));
5998   if (h == NULL)
5999     {
6000       if (! ((*finfo->info->callbacks->unattached_reloc)
6001              (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6002               (asection *) NULL, (bfd_vma) 0)))
6003         return false;
6004       return true;
6005     }
6006
6007   if (h->root.type == bfd_link_hash_common)
6008     {
6009       hsec = h->root.u.c.p->section;
6010       hval = 0;
6011     }
6012   else if (h->root.type == bfd_link_hash_defined
6013            || h->root.type == bfd_link_hash_defweak)
6014     {
6015       hsec = h->root.u.def.section;
6016       hval = h->root.u.def.value;
6017     }
6018   else
6019     {
6020       hsec = NULL;
6021       hval = 0;
6022     }
6023
6024   addend = link_order->u.reloc.p->addend;
6025   if (hsec != NULL)
6026     addend += (hsec->output_section->vma
6027                + hsec->output_offset
6028                + hval);
6029
6030   if (addend != 0)
6031     {
6032       bfd_size_type size;
6033       bfd_byte *buf;
6034       bfd_reloc_status_type rstat;
6035       boolean ok;
6036
6037       size = bfd_get_reloc_size (howto);
6038       buf = (bfd_byte *) bfd_zmalloc (size);
6039       if (buf == NULL)
6040         return false;
6041
6042       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6043       switch (rstat)
6044         {
6045         case bfd_reloc_ok:
6046           break;
6047         default:
6048         case bfd_reloc_outofrange:
6049           abort ();
6050         case bfd_reloc_overflow:
6051           if (! ((*finfo->info->callbacks->reloc_overflow)
6052                  (finfo->info, link_order->u.reloc.p->u.name,
6053                   howto->name, addend, (bfd *) NULL, (asection *) NULL,
6054                   (bfd_vma) 0)))
6055             {
6056               free (buf);
6057               return false;
6058             }
6059           break;
6060         }
6061       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6062                                      (file_ptr) link_order->offset, size);
6063       free (buf);
6064       if (! ok)
6065         return false;
6066     }
6067
6068   /* Store the reloc information in the right place.  It will get
6069      swapped and written out at the end of the final_link routine.  */
6070
6071   irel = (finfo->section_info[output_section->target_index].relocs
6072           + output_section->reloc_count);
6073   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6074                   + output_section->reloc_count);
6075
6076   memset (irel, 0, sizeof (struct internal_reloc));
6077   *rel_hash_ptr = NULL;
6078
6079   irel->r_vaddr = output_section->vma + link_order->offset;
6080
6081   if (h->indx >= 0)
6082     irel->r_symndx = h->indx;
6083   else
6084     {
6085       /* Set the index to -2 to force this symbol to get written out.  */
6086       h->indx = -2;
6087       *rel_hash_ptr = h;
6088       irel->r_symndx = 0;
6089     }
6090
6091   irel->r_type = howto->type;
6092   irel->r_size = howto->bitsize - 1;
6093   if (howto->complain_on_overflow == complain_overflow_signed)
6094     irel->r_size |= 0x80;
6095
6096   ++output_section->reloc_count;
6097
6098   /* Now output the reloc to the .loader section.  */
6099
6100   ldrel.l_vaddr = irel->r_vaddr;
6101
6102   if (hsec != NULL)
6103     {
6104       const char *secname;
6105
6106       secname = hsec->output_section->name;
6107
6108       if (strcmp (secname, ".text") == 0)
6109         ldrel.l_symndx = 0;
6110       else if (strcmp (secname, ".data") == 0)
6111         ldrel.l_symndx = 1;
6112       else if (strcmp (secname, ".bss") == 0)
6113         ldrel.l_symndx = 2;
6114       else
6115         {
6116           (*_bfd_error_handler)
6117             (_("%s: loader reloc in unrecognized section `%s'"),
6118              bfd_get_filename (output_bfd), secname);
6119           bfd_set_error (bfd_error_nonrepresentable_section);
6120           return false;
6121         }
6122     }
6123   else
6124     {
6125       if (h->ldindx < 0)
6126         {
6127           (*_bfd_error_handler)
6128             (_("%s: `%s' in loader reloc but not loader sym"),
6129              bfd_get_filename (output_bfd),
6130              h->root.root.string);
6131           bfd_set_error (bfd_error_bad_value);
6132           return false;
6133         }
6134       ldrel.l_symndx = h->ldindx;
6135     }
6136
6137   ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6138   ldrel.l_rsecnm = output_section->target_index;
6139   bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6140   finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
6141
6142   return true;
6143 }
6144
6145 /* Sort relocs by VMA.  This is called via qsort.  */
6146
6147 static int
6148 xcoff_sort_relocs (p1, p2)
6149      const PTR p1;
6150      const PTR p2;
6151 {
6152   const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6153   const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6154
6155   if (r1->r_vaddr > r2->r_vaddr)
6156     return 1;
6157   else if (r1->r_vaddr < r2->r_vaddr)
6158     return -1;
6159   else
6160     return 0;
6161 }
6162
6163
6164
6165