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