OSDN Git Service

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