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