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