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