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