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