Back to index

cell-binutils  2.17cvs20070401
xcofflink.c
Go to the documentation of this file.
00001 /* POWER/PowerPC XCOFF linker support.
00002    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
00003    2005, 2006 Free Software Foundation, Inc.
00004    Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "bfdlink.h"
00025 #include "libbfd.h"
00026 #include "coff/internal.h"
00027 #include "coff/xcoff.h"
00028 #include "libcoff.h"
00029 #include "libxcoff.h"
00030 
00031 /* This file holds the XCOFF linker code.  */
00032 
00033 #undef  STRING_SIZE_SIZE
00034 #define STRING_SIZE_SIZE 4
00035 
00036 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
00037    This flag will only be used on input sections.  */
00038 
00039 #define SEC_MARK (SEC_ROM)
00040 
00041 /* The list of import files.  */
00042 
00043 struct xcoff_import_file
00044 {
00045   /* The next entry in the list.  */
00046   struct xcoff_import_file *next;
00047   /* The path.  */
00048   const char *path;
00049   /* The file name.  */
00050   const char *file;
00051   /* The member name.  */
00052   const char *member;
00053 };
00054 
00055 /* Information we keep for each section in the output file during the
00056    final link phase.  */
00057 
00058 struct xcoff_link_section_info
00059 {
00060   /* The relocs to be output.  */
00061   struct internal_reloc *relocs;
00062   /* For each reloc against a global symbol whose index was not known
00063      when the reloc was handled, the global hash table entry.  */
00064   struct xcoff_link_hash_entry **rel_hashes;
00065   /* If there is a TOC relative reloc against a global symbol, and the
00066      index of the TOC symbol is not known when the reloc was handled,
00067      an entry is added to this linked list.  This is not an array,
00068      like rel_hashes, because this case is quite uncommon.  */
00069   struct xcoff_toc_rel_hash
00070   {
00071     struct xcoff_toc_rel_hash *next;
00072     struct xcoff_link_hash_entry *h;
00073     struct internal_reloc *rel;
00074   } *toc_rel_hashes;
00075 };
00076 
00077 /* Information that we pass around while doing the final link step.  */
00078 
00079 struct xcoff_final_link_info
00080 {
00081   /* General link information.  */
00082   struct bfd_link_info *info;
00083   /* Output BFD.  */
00084   bfd *output_bfd;
00085   /* Hash table for long symbol names.  */
00086   struct bfd_strtab_hash *strtab;
00087   /* Array of information kept for each output section, indexed by the
00088      target_index field.  */
00089   struct xcoff_link_section_info *section_info;
00090   /* Symbol index of last C_FILE symbol (-1 if none).  */
00091   long last_file_index;
00092   /* Contents of last C_FILE symbol.  */
00093   struct internal_syment last_file;
00094   /* Symbol index of TOC symbol.  */
00095   long toc_symindx;
00096   /* Start of .loader symbols.  */
00097   bfd_byte *ldsym;
00098   /* Next .loader reloc to swap out.  */
00099   bfd_byte *ldrel;
00100   /* File position of start of line numbers.  */
00101   file_ptr line_filepos;
00102   /* Buffer large enough to hold swapped symbols of any input file.  */
00103   struct internal_syment *internal_syms;
00104   /* Buffer large enough to hold output indices of symbols of any
00105      input file.  */
00106   long *sym_indices;
00107   /* Buffer large enough to hold output symbols for any input file.  */
00108   bfd_byte *outsyms;
00109   /* Buffer large enough to hold external line numbers for any input
00110      section.  */
00111   bfd_byte *linenos;
00112   /* Buffer large enough to hold any input section.  */
00113   bfd_byte *contents;
00114   /* Buffer large enough to hold external relocs of any input section.  */
00115   bfd_byte *external_relocs;
00116 };
00117 
00118 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
00119 
00120 
00121 
00122 /* Routines to read XCOFF dynamic information.  This don't really
00123    belong here, but we already have the ldsym manipulation routines
00124    here.  */
00125 
00126 /* Read the contents of a section.  */
00127 
00128 static bfd_boolean
00129 xcoff_get_section_contents (bfd *abfd, asection *sec)
00130 {
00131   if (coff_section_data (abfd, sec) == NULL)
00132     {
00133       bfd_size_type amt = sizeof (struct coff_section_tdata);
00134 
00135       sec->used_by_bfd = bfd_zalloc (abfd, amt);
00136       if (sec->used_by_bfd == NULL)
00137        return FALSE;
00138     }
00139 
00140   if (coff_section_data (abfd, sec)->contents == NULL)
00141     {
00142       bfd_byte *contents;
00143 
00144       if (! bfd_malloc_and_get_section (abfd, sec, &contents))
00145        {
00146          if (contents != NULL)
00147            free (contents);
00148          return FALSE;
00149        }
00150       coff_section_data (abfd, sec)->contents = contents;
00151     }
00152 
00153   return TRUE;
00154 }
00155 
00156 /* Get the size required to hold the dynamic symbols.  */
00157 
00158 long
00159 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
00160 {
00161   asection *lsec;
00162   bfd_byte *contents;
00163   struct internal_ldhdr ldhdr;
00164 
00165   if ((abfd->flags & DYNAMIC) == 0)
00166     {
00167       bfd_set_error (bfd_error_invalid_operation);
00168       return -1;
00169     }
00170 
00171   lsec = bfd_get_section_by_name (abfd, ".loader");
00172   if (lsec == NULL)
00173     {
00174       bfd_set_error (bfd_error_no_symbols);
00175       return -1;
00176     }
00177 
00178   if (! xcoff_get_section_contents (abfd, lsec))
00179     return -1;
00180   contents = coff_section_data (abfd, lsec)->contents;
00181 
00182   bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
00183 
00184   return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
00185 }
00186 
00187 /* Get the dynamic symbols.  */
00188 
00189 long
00190 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
00191 {
00192   asection *lsec;
00193   bfd_byte *contents;
00194   struct internal_ldhdr ldhdr;
00195   const char *strings;
00196   bfd_byte *elsym, *elsymend;
00197   coff_symbol_type *symbuf;
00198 
00199   if ((abfd->flags & DYNAMIC) == 0)
00200     {
00201       bfd_set_error (bfd_error_invalid_operation);
00202       return -1;
00203     }
00204 
00205   lsec = bfd_get_section_by_name (abfd, ".loader");
00206   if (lsec == NULL)
00207     {
00208       bfd_set_error (bfd_error_no_symbols);
00209       return -1;
00210     }
00211 
00212   if (! xcoff_get_section_contents (abfd, lsec))
00213     return -1;
00214   contents = coff_section_data (abfd, lsec)->contents;
00215 
00216   coff_section_data (abfd, lsec)->keep_contents = TRUE;
00217 
00218   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
00219 
00220   strings = (char *) contents + ldhdr.l_stoff;
00221 
00222   symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
00223   if (symbuf == NULL)
00224     return -1;
00225 
00226   elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
00227 
00228   elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
00229   for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
00230     {
00231       struct internal_ldsym ldsym;
00232 
00233       bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
00234 
00235       symbuf->symbol.the_bfd = abfd;
00236 
00237       if (ldsym._l._l_l._l_zeroes == 0)
00238        symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
00239       else
00240        {
00241          char *c;
00242 
00243          c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
00244          if (c == NULL)
00245            return -1;
00246          memcpy (c, ldsym._l._l_name, SYMNMLEN);
00247          c[SYMNMLEN] = '\0';
00248          symbuf->symbol.name = c;
00249        }
00250 
00251       if (ldsym.l_smclas == XMC_XO)
00252        symbuf->symbol.section = bfd_abs_section_ptr;
00253       else
00254        symbuf->symbol.section = coff_section_from_bfd_index (abfd,
00255                                                        ldsym.l_scnum);
00256       symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
00257 
00258       symbuf->symbol.flags = BSF_NO_FLAGS;
00259       if ((ldsym.l_smtype & L_EXPORT) != 0)
00260        symbuf->symbol.flags |= BSF_GLOBAL;
00261 
00262       /* FIXME: We have no way to record the other information stored
00263         with the loader symbol.  */
00264       *psyms = (asymbol *) symbuf;
00265     }
00266 
00267   *psyms = NULL;
00268 
00269   return ldhdr.l_nsyms;
00270 }
00271 
00272 /* Get the size required to hold the dynamic relocs.  */
00273 
00274 long
00275 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
00276 {
00277   asection *lsec;
00278   bfd_byte *contents;
00279   struct internal_ldhdr ldhdr;
00280 
00281   if ((abfd->flags & DYNAMIC) == 0)
00282     {
00283       bfd_set_error (bfd_error_invalid_operation);
00284       return -1;
00285     }
00286 
00287   lsec = bfd_get_section_by_name (abfd, ".loader");
00288   if (lsec == NULL)
00289     {
00290       bfd_set_error (bfd_error_no_symbols);
00291       return -1;
00292     }
00293 
00294   if (! xcoff_get_section_contents (abfd, lsec))
00295     return -1;
00296   contents = coff_section_data (abfd, lsec)->contents;
00297 
00298   bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
00299 
00300   return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
00301 }
00302 
00303 /* Get the dynamic relocs.  */
00304 
00305 long
00306 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
00307                                    arelent **prelocs,
00308                                    asymbol **syms)
00309 {
00310   asection *lsec;
00311   bfd_byte *contents;
00312   struct internal_ldhdr ldhdr;
00313   arelent *relbuf;
00314   bfd_byte *elrel, *elrelend;
00315 
00316   if ((abfd->flags & DYNAMIC) == 0)
00317     {
00318       bfd_set_error (bfd_error_invalid_operation);
00319       return -1;
00320     }
00321 
00322   lsec = bfd_get_section_by_name (abfd, ".loader");
00323   if (lsec == NULL)
00324     {
00325       bfd_set_error (bfd_error_no_symbols);
00326       return -1;
00327     }
00328 
00329   if (! xcoff_get_section_contents (abfd, lsec))
00330     return -1;
00331   contents = coff_section_data (abfd, lsec)->contents;
00332 
00333   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
00334 
00335   relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
00336   if (relbuf == NULL)
00337     return -1;
00338 
00339   elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
00340 
00341   elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
00342   for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
00343         prelocs++)
00344     {
00345       struct internal_ldrel ldrel;
00346 
00347       bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
00348 
00349       if (ldrel.l_symndx >= 3)
00350        relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
00351       else
00352        {
00353          const char *name;
00354          asection *sec;
00355 
00356          switch (ldrel.l_symndx)
00357            {
00358            case 0:
00359              name = ".text";
00360              break;
00361            case 1:
00362              name = ".data";
00363              break;
00364            case 2:
00365              name = ".bss";
00366              break;
00367            default:
00368              abort ();
00369              break;
00370            }
00371 
00372          sec = bfd_get_section_by_name (abfd, name);
00373          if (sec == NULL)
00374            {
00375              bfd_set_error (bfd_error_bad_value);
00376              return -1;
00377            }
00378 
00379          relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
00380        }
00381 
00382       relbuf->address = ldrel.l_vaddr;
00383       relbuf->addend = 0;
00384 
00385       /* Most dynamic relocs have the same type.  FIXME: This is only
00386         correct if ldrel.l_rtype == 0.  In other cases, we should use
00387         a different howto.  */
00388       relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
00389 
00390       /* FIXME: We have no way to record the l_rsecnm field.  */
00391 
00392       *prelocs = relbuf;
00393     }
00394 
00395   *prelocs = NULL;
00396 
00397   return ldhdr.l_nreloc;
00398 }
00399 
00400 /* Routine to create an entry in an XCOFF link hash table.  */
00401 
00402 static struct bfd_hash_entry *
00403 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
00404                       struct bfd_hash_table *table,
00405                       const char *string)
00406 {
00407   struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
00408 
00409   /* Allocate the structure if it has not already been allocated by a
00410      subclass.  */
00411   if (ret == NULL)
00412     ret = bfd_hash_allocate (table, sizeof (* ret));
00413   if (ret == NULL)
00414     return NULL;
00415 
00416   /* Call the allocation method of the superclass.  */
00417   ret = ((struct xcoff_link_hash_entry *)
00418         _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
00419                              table, string));
00420   if (ret != NULL)
00421     {
00422       /* Set local fields.  */
00423       ret->indx = -1;
00424       ret->toc_section = NULL;
00425       ret->u.toc_indx = -1;
00426       ret->descriptor = NULL;
00427       ret->ldsym = NULL;
00428       ret->ldindx = -1;
00429       ret->flags = 0;
00430       ret->smclas = XMC_UA;
00431     }
00432 
00433   return (struct bfd_hash_entry *) ret;
00434 }
00435 
00436 /* Create a XCOFF link hash table.  */
00437 
00438 struct bfd_link_hash_table *
00439 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
00440 {
00441   struct xcoff_link_hash_table *ret;
00442   bfd_size_type amt = sizeof (* ret);
00443 
00444   ret = bfd_malloc (amt);
00445   if (ret == NULL)
00446     return NULL;
00447   if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
00448                               sizeof (struct xcoff_link_hash_entry)))
00449     {
00450       free (ret);
00451       return NULL;
00452     }
00453 
00454   ret->debug_strtab = _bfd_xcoff_stringtab_init ();
00455   ret->debug_section = NULL;
00456   ret->loader_section = NULL;
00457   ret->ldrel_count = 0;
00458   memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
00459   ret->linkage_section = NULL;
00460   ret->toc_section = NULL;
00461   ret->descriptor_section = NULL;
00462   ret->imports = NULL;
00463   ret->file_align = 0;
00464   ret->textro = FALSE;
00465   ret->gc = FALSE;
00466   memset (ret->special_sections, 0, sizeof ret->special_sections);
00467 
00468   /* The linker will always generate a full a.out header.  We need to
00469      record that fact now, before the sizeof_headers routine could be
00470      called.  */
00471   xcoff_data (abfd)->full_aouthdr = TRUE;
00472 
00473   return &ret->root;
00474 }
00475 
00476 /* Free a XCOFF link hash table.  */
00477 
00478 void
00479 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
00480 {
00481   struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
00482 
00483   _bfd_stringtab_free (ret->debug_strtab);
00484   bfd_hash_table_free (&ret->root.table);
00485   free (ret);
00486 }
00487 
00488 /* Read internal relocs for an XCOFF csect.  This is a wrapper around
00489    _bfd_coff_read_internal_relocs which tries to take advantage of any
00490    relocs which may have been cached for the enclosing section.  */
00491 
00492 static struct internal_reloc *
00493 xcoff_read_internal_relocs (bfd *abfd,
00494                          asection *sec,
00495                          bfd_boolean cache,
00496                          bfd_byte *external_relocs,
00497                          bfd_boolean require_internal,
00498                          struct internal_reloc *internal_relocs)
00499 {
00500   if (coff_section_data (abfd, sec) != NULL
00501       && coff_section_data (abfd, sec)->relocs == NULL
00502       && xcoff_section_data (abfd, sec) != NULL)
00503     {
00504       asection *enclosing;
00505 
00506       enclosing = xcoff_section_data (abfd, sec)->enclosing;
00507 
00508       if (enclosing != NULL
00509          && (coff_section_data (abfd, enclosing) == NULL
00510              || coff_section_data (abfd, enclosing)->relocs == NULL)
00511          && cache
00512          && enclosing->reloc_count > 0)
00513        {
00514          if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
00515                                          external_relocs, FALSE, NULL)
00516              == NULL)
00517            return NULL;
00518        }
00519 
00520       if (enclosing != NULL
00521          && coff_section_data (abfd, enclosing) != NULL
00522          && coff_section_data (abfd, enclosing)->relocs != NULL)
00523        {
00524          size_t off;
00525 
00526          off = ((sec->rel_filepos - enclosing->rel_filepos)
00527                / bfd_coff_relsz (abfd));
00528 
00529          if (! require_internal)
00530            return coff_section_data (abfd, enclosing)->relocs + off;
00531          memcpy (internal_relocs,
00532                 coff_section_data (abfd, enclosing)->relocs + off,
00533                 sec->reloc_count * sizeof (struct internal_reloc));
00534          return internal_relocs;
00535        }
00536     }
00537 
00538   return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
00539                                     require_internal, internal_relocs);
00540 }
00541 
00542 /* This function is used to add symbols from a dynamic object to the
00543    global symbol table.  */
00544 
00545 static bfd_boolean
00546 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
00547 {
00548   asection *lsec;
00549   bfd_byte *contents;
00550   struct internal_ldhdr ldhdr;
00551   const char *strings;
00552   bfd_byte *elsym, *elsymend;
00553   struct xcoff_import_file *n;
00554   const char *bname;
00555   const char *mname;
00556   const char *s;
00557   unsigned int c;
00558   struct xcoff_import_file **pp;
00559 
00560   /* We can only handle a dynamic object if we are generating an XCOFF
00561      output file.  */
00562    if (info->hash->creator != abfd->xvec)
00563     {
00564       (*_bfd_error_handler)
00565        (_("%s: XCOFF shared object when not producing XCOFF output"),
00566         bfd_get_filename (abfd));
00567       bfd_set_error (bfd_error_invalid_operation);
00568       return FALSE;
00569     }
00570 
00571   /* The symbols we use from a dynamic object are not the symbols in
00572      the normal symbol table, but, rather, the symbols in the export
00573      table.  If there is a global symbol in a dynamic object which is
00574      not in the export table, the loader will not be able to find it,
00575      so we don't want to find it either.  Also, on AIX 4.1.3, shr.o in
00576      libc.a has symbols in the export table which are not in the
00577      symbol table.  */
00578 
00579   /* Read in the .loader section.  FIXME: We should really use the
00580      o_snloader field in the a.out header, rather than grabbing the
00581      section by name.  */
00582   lsec = bfd_get_section_by_name (abfd, ".loader");
00583   if (lsec == NULL)
00584     {
00585       (*_bfd_error_handler)
00586        (_("%s: dynamic object with no .loader section"),
00587         bfd_get_filename (abfd));
00588       bfd_set_error (bfd_error_no_symbols);
00589       return FALSE;
00590     }
00591 
00592   if (! xcoff_get_section_contents (abfd, lsec))
00593     return FALSE;
00594   contents = coff_section_data (abfd, lsec)->contents;
00595 
00596   /* Remove the sections from this object, so that they do not get
00597      included in the link.  */
00598   bfd_section_list_clear (abfd);
00599 
00600   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
00601 
00602   strings = (char *) contents + ldhdr.l_stoff;
00603 
00604   elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
00605 
00606   elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
00607 
00608   for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
00609     {
00610       struct internal_ldsym ldsym;
00611       char nambuf[SYMNMLEN + 1];
00612       const char *name;
00613       struct xcoff_link_hash_entry *h;
00614 
00615       bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
00616 
00617       /* We are only interested in exported symbols.  */
00618       if ((ldsym.l_smtype & L_EXPORT) == 0)
00619        continue;
00620 
00621       if (ldsym._l._l_l._l_zeroes == 0)
00622        name = strings + ldsym._l._l_l._l_offset;
00623       else
00624        {
00625          memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
00626          nambuf[SYMNMLEN] = '\0';
00627          name = nambuf;
00628        }
00629 
00630       /* Normally we could not call xcoff_link_hash_lookup in an add
00631         symbols routine, since we might not be using an XCOFF hash
00632         table.  However, we verified above that we are using an XCOFF
00633         hash table.  */
00634 
00635       h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
00636                               TRUE, TRUE);
00637       if (h == NULL)
00638        return FALSE;
00639 
00640       h->flags |= XCOFF_DEF_DYNAMIC;
00641 
00642       /* If the symbol is undefined, and the BFD it was found in is
00643         not a dynamic object, change the BFD to this dynamic object,
00644         so that we can get the correct import file ID.  */
00645       if ((h->root.type == bfd_link_hash_undefined
00646           || h->root.type == bfd_link_hash_undefweak)
00647          && (h->root.u.undef.abfd == NULL
00648              || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
00649        h->root.u.undef.abfd = abfd;
00650 
00651       if (h->root.type == bfd_link_hash_new)
00652        {
00653          h->root.type = bfd_link_hash_undefined;
00654          h->root.u.undef.abfd = abfd;
00655          /* We do not want to add this to the undefined symbol list.  */
00656        }
00657 
00658       if (h->smclas == XMC_UA
00659          || h->root.type == bfd_link_hash_undefined
00660          || h->root.type == bfd_link_hash_undefweak)
00661        h->smclas = ldsym.l_smclas;
00662 
00663       /* Unless this is an XMC_XO symbol, we don't bother to actually
00664         define it, since we don't have a section to put it in anyhow.
00665         Instead, the relocation routines handle the DEF_DYNAMIC flag
00666         correctly.  */
00667 
00668       if (h->smclas == XMC_XO
00669          && (h->root.type == bfd_link_hash_undefined
00670              || h->root.type == bfd_link_hash_undefweak))
00671        {
00672          /* This symbol has an absolute value.  */
00673          h->root.type = bfd_link_hash_defined;
00674          h->root.u.def.section = bfd_abs_section_ptr;
00675          h->root.u.def.value = ldsym.l_value;
00676        }
00677 
00678       /* If this symbol defines a function descriptor, then it
00679         implicitly defines the function code as well.  */
00680       if (h->smclas == XMC_DS
00681          || (h->smclas == XMC_XO && name[0] != '.'))
00682        h->flags |= XCOFF_DESCRIPTOR;
00683       if ((h->flags & XCOFF_DESCRIPTOR) != 0)
00684        {
00685          struct xcoff_link_hash_entry *hds;
00686 
00687          hds = h->descriptor;
00688          if (hds == NULL)
00689            {
00690              char *dsnm;
00691 
00692              dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
00693              if (dsnm == NULL)
00694               return FALSE;
00695              dsnm[0] = '.';
00696              strcpy (dsnm + 1, name);
00697              hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
00698                                        TRUE, TRUE, TRUE);
00699              free (dsnm);
00700              if (hds == NULL)
00701               return FALSE;
00702 
00703              if (hds->root.type == bfd_link_hash_new)
00704               {
00705                 hds->root.type = bfd_link_hash_undefined;
00706                 hds->root.u.undef.abfd = abfd;
00707                 /* We do not want to add this to the undefined
00708                    symbol list.  */
00709               }
00710 
00711              hds->descriptor = h;
00712              h->descriptor = hds;
00713            }
00714 
00715          hds->flags |= XCOFF_DEF_DYNAMIC;
00716          if (hds->smclas == XMC_UA)
00717            hds->smclas = XMC_PR;
00718 
00719          /* An absolute symbol appears to actually define code, not a
00720             function descriptor.  This is how some math functions are
00721             implemented on AIX 4.1.  */
00722          if (h->smclas == XMC_XO
00723              && (hds->root.type == bfd_link_hash_undefined
00724                 || hds->root.type == bfd_link_hash_undefweak))
00725            {
00726              hds->smclas = XMC_XO;
00727              hds->root.type = bfd_link_hash_defined;
00728              hds->root.u.def.section = bfd_abs_section_ptr;
00729              hds->root.u.def.value = ldsym.l_value;
00730            }
00731        }
00732     }
00733 
00734   if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
00735     {
00736       free (coff_section_data (abfd, lsec)->contents);
00737       coff_section_data (abfd, lsec)->contents = NULL;
00738     }
00739 
00740   /* Record this file in the import files.  */
00741   n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
00742   if (n == NULL)
00743     return FALSE;
00744   n->next = NULL;
00745 
00746   /* For some reason, the path entry in the import file list for a
00747      shared object appears to always be empty.  The file name is the
00748      base name.  */
00749   n->path = "";
00750   if (abfd->my_archive == NULL)
00751     {
00752       bname = bfd_get_filename (abfd);
00753       mname = "";
00754     }
00755   else
00756     {
00757       bname = bfd_get_filename (abfd->my_archive);
00758       mname = bfd_get_filename (abfd);
00759     }
00760   s = strrchr (bname, '/');
00761   if (s != NULL)
00762     bname = s + 1;
00763   n->file = bname;
00764   n->member = mname;
00765 
00766   /* We start c at 1 because the first import file number is reserved
00767      for LIBPATH.  */
00768   for (pp = &xcoff_hash_table (info)->imports, c = 1;
00769        *pp != NULL;
00770        pp = &(*pp)->next, ++c)
00771     ;
00772   *pp = n;
00773 
00774   xcoff_data (abfd)->import_file_id = c;
00775 
00776   return TRUE;
00777 }
00778 
00779 /* xcoff_link_create_extra_sections
00780 
00781    Takes care of creating the .loader, .gl, .ds, .debug and sections.  */
00782 
00783 static bfd_boolean
00784 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
00785 {
00786   bfd_boolean return_value = FALSE;
00787 
00788   if (info->hash->creator == abfd->xvec)
00789     {
00790       /* We need to build a .loader section, so we do it here.  This
00791         won't work if we're producing an XCOFF output file with no
00792         XCOFF input files.  FIXME.  */
00793 
00794       if (xcoff_hash_table (info)->loader_section == NULL)
00795        {
00796          asection *lsec;
00797          flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
00798 
00799          lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
00800          if (lsec == NULL)
00801            goto end_return;
00802 
00803          xcoff_hash_table (info)->loader_section = lsec;
00804        }
00805 
00806       /* Likewise for the linkage section.  */
00807       if (xcoff_hash_table (info)->linkage_section == NULL)
00808        {
00809          asection *lsec;
00810          flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
00811                          | SEC_IN_MEMORY);
00812 
00813          lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
00814          if (lsec == NULL)
00815            goto end_return;
00816 
00817          xcoff_hash_table (info)->linkage_section = lsec;
00818          lsec->alignment_power = 2;
00819        }
00820 
00821       /* Likewise for the TOC section.  */
00822       if (xcoff_hash_table (info)->toc_section == NULL)
00823        {
00824          asection *tsec;
00825          flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
00826                          | SEC_IN_MEMORY);
00827 
00828          tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
00829          if (tsec == NULL)
00830            goto end_return;
00831 
00832          xcoff_hash_table (info)->toc_section = tsec;
00833          tsec->alignment_power = 2;
00834        }
00835 
00836       /* Likewise for the descriptor section.  */
00837       if (xcoff_hash_table (info)->descriptor_section == NULL)
00838        {
00839          asection *dsec;
00840          flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
00841                          | SEC_IN_MEMORY);
00842 
00843          dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
00844          if (dsec == NULL)
00845            goto end_return;
00846 
00847          xcoff_hash_table (info)->descriptor_section = dsec;
00848          dsec->alignment_power = 2;
00849        }
00850 
00851       /* Likewise for the .debug section.  */
00852       if (xcoff_hash_table (info)->debug_section == NULL
00853          && info->strip != strip_all)
00854        {
00855          asection *dsec;
00856          flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
00857 
00858          dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
00859          if (dsec == NULL)
00860            goto end_return;
00861 
00862          xcoff_hash_table (info)->debug_section = dsec;
00863        }
00864     }
00865 
00866   return_value = TRUE;
00867 
00868  end_return:
00869 
00870   return return_value;
00871 }
00872 
00873 /* Returns the index of reloc in RELOCS with the least address greater
00874    than or equal to ADDRESS.  The relocs are sorted by address.  */
00875 
00876 static bfd_size_type
00877 xcoff_find_reloc (struct internal_reloc *relocs,
00878                 bfd_size_type count,
00879                 bfd_vma address)
00880 {
00881   bfd_size_type min, max, this;
00882 
00883   if (count < 2)
00884     {
00885       if (count == 1 && relocs[0].r_vaddr < address)
00886        return 1;
00887       else
00888        return 0;
00889     }
00890 
00891   min = 0;
00892   max = count;
00893 
00894   /* Do a binary search over (min,max].  */
00895   while (min + 1 < max)
00896     {
00897       bfd_vma raddr;
00898 
00899       this = (max + min) / 2;
00900       raddr = relocs[this].r_vaddr;
00901       if (raddr > address)
00902        max = this;
00903       else if (raddr < address)
00904        min = this;
00905       else
00906        {
00907          min = this;
00908          break;
00909        }
00910     }
00911 
00912   if (relocs[min].r_vaddr < address)
00913     return min + 1;
00914 
00915   while (min > 0
00916         && relocs[min - 1].r_vaddr == address)
00917     --min;
00918 
00919   return min;
00920 }
00921 
00922 /* Add all the symbols from an object file to the hash table.
00923 
00924    XCOFF is a weird format.  A normal XCOFF .o files will have three
00925    COFF sections--.text, .data, and .bss--but each COFF section will
00926    contain many csects.  These csects are described in the symbol
00927    table.  From the linker's point of view, each csect must be
00928    considered a section in its own right.  For example, a TOC entry is
00929    handled as a small XMC_TC csect.  The linker must be able to merge
00930    different TOC entries together, which means that it must be able to
00931    extract the XMC_TC csects from the .data section of the input .o
00932    file.
00933 
00934    From the point of view of our linker, this is, of course, a hideous
00935    nightmare.  We cope by actually creating sections for each csect,
00936    and discarding the original sections.  We then have to handle the
00937    relocation entries carefully, since the only way to tell which
00938    csect they belong to is to examine the address.  */
00939 
00940 static bfd_boolean
00941 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
00942 {
00943   unsigned int n_tmask;
00944   unsigned int n_btshft;
00945   bfd_boolean default_copy;
00946   bfd_size_type symcount;
00947   struct xcoff_link_hash_entry **sym_hash;
00948   asection **csect_cache;
00949   bfd_size_type linesz;
00950   asection *o;
00951   asection *last_real;
00952   bfd_boolean keep_syms;
00953   asection *csect;
00954   unsigned int csect_index;
00955   asection *first_csect;
00956   bfd_size_type symesz;
00957   bfd_byte *esym;
00958   bfd_byte *esym_end;
00959   struct reloc_info_struct
00960   {
00961     struct internal_reloc *relocs;
00962     asection **csects;
00963     bfd_byte *linenos;
00964   } *reloc_info = NULL;
00965   bfd_size_type amt;
00966 
00967   keep_syms = obj_coff_keep_syms (abfd);
00968 
00969   if ((abfd->flags & DYNAMIC) != 0
00970       && ! info->static_link)
00971     {
00972       if (! xcoff_link_add_dynamic_symbols (abfd, info))
00973        return FALSE;
00974     }
00975 
00976   /* Create the loader, toc, gl, ds and debug sections, if needed.  */
00977   if (! xcoff_link_create_extra_sections (abfd, info))
00978     goto error_return;
00979 
00980   if ((abfd->flags & DYNAMIC) != 0
00981       && ! info->static_link)
00982     return TRUE;
00983 
00984   n_tmask = coff_data (abfd)->local_n_tmask;
00985   n_btshft = coff_data (abfd)->local_n_btshft;
00986 
00987   /* Define macros so that ISFCN, et. al., macros work correctly.  */
00988 #define N_TMASK n_tmask
00989 #define N_BTSHFT n_btshft
00990 
00991   if (info->keep_memory)
00992     default_copy = FALSE;
00993   else
00994     default_copy = TRUE;
00995 
00996   symcount = obj_raw_syment_count (abfd);
00997 
00998   /* We keep a list of the linker hash table entries that correspond
00999      to each external symbol.  */
01000   amt = symcount * sizeof (struct xcoff_link_hash_entry *);
01001   sym_hash = bfd_zalloc (abfd, amt);
01002   if (sym_hash == NULL && symcount != 0)
01003     goto error_return;
01004   coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
01005 
01006   /* Because of the weird stuff we are doing with XCOFF csects, we can
01007      not easily determine which section a symbol is in, so we store
01008      the information in the tdata for the input file.  */
01009   amt = symcount * sizeof (asection *);
01010   csect_cache = bfd_zalloc (abfd, amt);
01011   if (csect_cache == NULL && symcount != 0)
01012     goto error_return;
01013   xcoff_data (abfd)->csects = csect_cache;
01014 
01015   /* While splitting sections into csects, we need to assign the
01016      relocs correctly.  The relocs and the csects must both be in
01017      order by VMA within a given section, so we handle this by
01018      scanning along the relocs as we process the csects.  We index
01019      into reloc_info using the section target_index.  */
01020   amt = abfd->section_count + 1;
01021   amt *= sizeof (struct reloc_info_struct);
01022   reloc_info = bfd_zmalloc (amt);
01023   if (reloc_info == NULL)
01024     goto error_return;
01025 
01026   /* Read in the relocs and line numbers for each section.  */
01027   linesz = bfd_coff_linesz (abfd);
01028   last_real = NULL;
01029   for (o = abfd->sections; o != NULL; o = o->next)
01030     {
01031       last_real = o;
01032 
01033       if ((o->flags & SEC_RELOC) != 0)
01034        {
01035          reloc_info[o->target_index].relocs =
01036            xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
01037          amt = o->reloc_count;
01038          amt *= sizeof (asection *);
01039          reloc_info[o->target_index].csects = bfd_zmalloc (amt);
01040          if (reloc_info[o->target_index].csects == NULL)
01041            goto error_return;
01042        }
01043 
01044       if ((info->strip == strip_none || info->strip == strip_some)
01045          && o->lineno_count > 0)
01046        {
01047          bfd_byte *linenos;
01048 
01049          amt = linesz * o->lineno_count;
01050          linenos = bfd_malloc (amt);
01051          if (linenos == NULL)
01052            goto error_return;
01053          reloc_info[o->target_index].linenos = linenos;
01054          if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
01055              || bfd_bread (linenos, amt, abfd) != amt)
01056            goto error_return;
01057        }
01058     }
01059 
01060   /* Don't let the linker relocation routines discard the symbols.  */
01061   obj_coff_keep_syms (abfd) = TRUE;
01062 
01063   csect = NULL;
01064   csect_index = 0;
01065   first_csect = NULL;
01066 
01067   symesz = bfd_coff_symesz (abfd);
01068   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
01069   esym = (bfd_byte *) obj_coff_external_syms (abfd);
01070   esym_end = esym + symcount * symesz;
01071 
01072   while (esym < esym_end)
01073     {
01074       struct internal_syment sym;
01075       union internal_auxent aux;
01076       const char *name;
01077       char buf[SYMNMLEN + 1];
01078       int smtyp;
01079       flagword flags;
01080       asection *section;
01081       bfd_vma value;
01082       struct xcoff_link_hash_entry *set_toc;
01083 
01084       bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
01085 
01086       /* In this pass we are only interested in symbols with csect
01087         information.  */
01088       if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
01089        {
01090          /* Set csect_cache,
01091             Normally csect is a .pr, .rw  etc. created in the loop
01092             If C_FILE or first time, handle special
01093 
01094             Advance esym, sym_hash, csect_hash ptr's
01095             Keep track of the last_symndx for the current file.  */
01096          if (sym.n_sclass == C_FILE && csect != NULL)
01097            {
01098              xcoff_section_data (abfd, csect)->last_symndx =
01099               ((esym
01100                 - (bfd_byte *) obj_coff_external_syms (abfd))
01101                / symesz);
01102              csect = NULL;
01103            }
01104 
01105          if (csect != NULL)
01106            *csect_cache = csect;
01107          else if (first_csect == NULL || sym.n_sclass == C_FILE)
01108            *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
01109          else
01110            *csect_cache = NULL;
01111          esym += (sym.n_numaux + 1) * symesz;
01112          sym_hash += sym.n_numaux + 1;
01113          csect_cache += sym.n_numaux + 1;
01114 
01115          continue;
01116        }
01117 
01118       name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
01119 
01120       if (name == NULL)
01121        goto error_return;
01122 
01123       /* If this symbol has line number information attached to it,
01124         and we're not stripping it, count the number of entries and
01125         add them to the count for this csect.  In the final link pass
01126         we are going to attach line number information by symbol,
01127         rather than by section, in order to more easily handle
01128         garbage collection.  */
01129       if ((info->strip == strip_none || info->strip == strip_some)
01130          && sym.n_numaux > 1
01131          && csect != NULL
01132          && ISFCN (sym.n_type))
01133        {
01134          union internal_auxent auxlin;
01135 
01136          bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
01137                             sym.n_type, sym.n_sclass,
01138                             0, sym.n_numaux, (void *) &auxlin);
01139 
01140          if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
01141            {
01142              asection *enclosing;
01143              bfd_signed_vma linoff;
01144 
01145              enclosing = xcoff_section_data (abfd, csect)->enclosing;
01146              if (enclosing == NULL)
01147               {
01148                 (*_bfd_error_handler)
01149                   (_("%B: `%s' has line numbers but no enclosing section"),
01150                    abfd, name);
01151                 bfd_set_error (bfd_error_bad_value);
01152                 goto error_return;
01153               }
01154              linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
01155                      - enclosing->line_filepos);
01156              /* Explicit cast to bfd_signed_vma for compiler.  */
01157              if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
01158               {
01159                 struct internal_lineno lin;
01160                 bfd_byte *linpstart;
01161 
01162                 linpstart = (reloc_info[enclosing->target_index].linenos
01163                             + linoff);
01164                 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
01165                 if (lin.l_lnno == 0
01166                     && ((bfd_size_type) lin.l_addr.l_symndx
01167                        == ((esym
01168                             - (bfd_byte *) obj_coff_external_syms (abfd))
01169                            / symesz)))
01170                   {
01171                     bfd_byte *linpend, *linp;
01172 
01173                     linpend = (reloc_info[enclosing->target_index].linenos
01174                              + enclosing->lineno_count * linesz);
01175                     for (linp = linpstart + linesz;
01176                         linp < linpend;
01177                         linp += linesz)
01178                      {
01179                        bfd_coff_swap_lineno_in (abfd, (void *) linp,
01180                                              (void *) &lin);
01181                        if (lin.l_lnno == 0)
01182                          break;
01183                      }
01184                     csect->lineno_count += (linp - linpstart) / linesz;
01185                     /* The setting of line_filepos will only be
01186                       useful if all the line number entries for a
01187                       csect are contiguous; this only matters for
01188                       error reporting.  */
01189                     if (csect->line_filepos == 0)
01190                      csect->line_filepos =
01191                        auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
01192                   }
01193               }
01194            }
01195        }
01196 
01197       /* Pick up the csect auxiliary information.  */
01198       if (sym.n_numaux == 0)
01199        {
01200          (*_bfd_error_handler)
01201            (_("%B: class %d symbol `%s' has no aux entries"),
01202             abfd, sym.n_sclass, name);
01203          bfd_set_error (bfd_error_bad_value);
01204          goto error_return;
01205        }
01206 
01207       bfd_coff_swap_aux_in (abfd,
01208                          (void *) (esym + symesz * sym.n_numaux),
01209                          sym.n_type, sym.n_sclass,
01210                          sym.n_numaux - 1, sym.n_numaux,
01211                          (void *) &aux);
01212 
01213       smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
01214 
01215       flags = BSF_GLOBAL;
01216       section = NULL;
01217       value = 0;
01218       set_toc = NULL;
01219 
01220       switch (smtyp)
01221        {
01222        default:
01223          (*_bfd_error_handler)
01224            (_("%B: symbol `%s' has unrecognized csect type %d"),
01225             abfd, name, smtyp);
01226          bfd_set_error (bfd_error_bad_value);
01227          goto error_return;
01228 
01229        case XTY_ER:
01230          /* This is an external reference.  */
01231          if (sym.n_sclass == C_HIDEXT
01232              || sym.n_scnum != N_UNDEF
01233              || aux.x_csect.x_scnlen.l != 0)
01234            {
01235              (*_bfd_error_handler)
01236               (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
01237                abfd, name, sym.n_sclass, sym.n_scnum,
01238                aux.x_csect.x_scnlen.l);
01239              bfd_set_error (bfd_error_bad_value);
01240              goto error_return;
01241            }
01242 
01243          /* An XMC_XO external reference is actually a reference to
01244             an absolute location.  */
01245          if (aux.x_csect.x_smclas != XMC_XO)
01246            section = bfd_und_section_ptr;
01247          else
01248            {
01249              section = bfd_abs_section_ptr;
01250              value = sym.n_value;
01251            }
01252          break;
01253 
01254        case XTY_SD:
01255          /* This is a csect definition.  */
01256          if (csect != NULL)
01257            {
01258              xcoff_section_data (abfd, csect)->last_symndx =
01259               ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
01260            }
01261 
01262          csect = NULL;
01263          csect_index = -(unsigned) 1;
01264 
01265          /* When we see a TOC anchor, we record the TOC value.  */
01266          if (aux.x_csect.x_smclas == XMC_TC0)
01267            {
01268              if (sym.n_sclass != C_HIDEXT
01269                 || aux.x_csect.x_scnlen.l != 0)
01270               {
01271                 (*_bfd_error_handler)
01272                   (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
01273                    abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
01274                 bfd_set_error (bfd_error_bad_value);
01275                 goto error_return;
01276               }
01277              xcoff_data (abfd)->toc = sym.n_value;
01278            }
01279 
01280          /* We must merge TOC entries for the same symbol.  We can
01281             merge two TOC entries if they are both C_HIDEXT, they
01282             both have the same name, they are both 4 or 8 bytes long, and
01283             they both have a relocation table entry for an external
01284             symbol with the same name.  Unfortunately, this means
01285             that we must look through the relocations.  Ick.
01286 
01287             Logic for 32 bit vs 64 bit.
01288             32 bit has a csect length of 4 for TOC
01289             64 bit has a csect length of 8 for TOC
01290 
01291             The conditions to get past the if-check are not that bad.
01292             They are what is used to create the TOC csects in the first
01293             place.  */
01294          if (aux.x_csect.x_smclas == XMC_TC
01295              && sym.n_sclass == C_HIDEXT
01296              && info->hash->creator == abfd->xvec
01297              && ((bfd_xcoff_is_xcoff32 (abfd)
01298                  && aux.x_csect.x_scnlen.l == 4)
01299                 || (bfd_xcoff_is_xcoff64 (abfd)
01300                     && aux.x_csect.x_scnlen.l == 8)))
01301            {
01302              asection *enclosing;
01303              struct internal_reloc *relocs;
01304              bfd_size_type relindx;
01305              struct internal_reloc *rel;
01306 
01307              enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
01308              if (enclosing == NULL)
01309               goto error_return;
01310 
01311              relocs = reloc_info[enclosing->target_index].relocs;
01312              amt = enclosing->reloc_count;
01313              relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
01314              rel = relocs + relindx;
01315 
01316              /* 32 bit R_POS r_size is 31
01317                64 bit R_POS r_size is 63  */
01318              if (relindx < enclosing->reloc_count
01319                 && rel->r_vaddr == (bfd_vma) sym.n_value
01320                 && rel->r_type == R_POS
01321                 && ((bfd_xcoff_is_xcoff32 (abfd)
01322                      && rel->r_size == 31)
01323                     || (bfd_xcoff_is_xcoff64 (abfd)
01324                        && rel->r_size == 63)))
01325               {
01326                 bfd_byte *erelsym;
01327 
01328                 struct internal_syment relsym;
01329 
01330                 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
01331                           + rel->r_symndx * symesz);
01332                 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
01333                 if (relsym.n_sclass == C_EXT)
01334                   {
01335                     const char *relname;
01336                     char relbuf[SYMNMLEN + 1];
01337                     bfd_boolean copy;
01338                     struct xcoff_link_hash_entry *h;
01339 
01340                     /* At this point we know that the TOC entry is
01341                       for an externally visible symbol.  */
01342                     relname = _bfd_coff_internal_syment_name (abfd, &relsym,
01343                                                         relbuf);
01344                     if (relname == NULL)
01345                      goto error_return;
01346 
01347                     /* We only merge TOC entries if the TC name is
01348                       the same as the symbol name.  This handles
01349                       the normal case, but not common cases like
01350                       SYM.P4 which gcc generates to store SYM + 4
01351                       in the TOC.  FIXME.  */
01352                     if (strcmp (name, relname) == 0)
01353                      {
01354                        copy = (! info->keep_memory
01355                               || relsym._n._n_n._n_zeroes != 0
01356                               || relsym._n._n_n._n_offset == 0);
01357                        h = xcoff_link_hash_lookup (xcoff_hash_table (info),
01358                                                 relname, TRUE, copy,
01359                                                 FALSE);
01360                        if (h == NULL)
01361                          goto error_return;
01362 
01363                        /* At this point h->root.type could be
01364                           bfd_link_hash_new.  That should be OK,
01365                           since we know for sure that we will come
01366                           across this symbol as we step through the
01367                           file.  */
01368 
01369                        /* We store h in *sym_hash for the
01370                           convenience of the relocate_section
01371                           function.  */
01372                        *sym_hash = h;
01373 
01374                        if (h->toc_section != NULL)
01375                          {
01376                            asection **rel_csects;
01377 
01378                            /* We already have a TOC entry for this
01379                              symbol, so we can just ignore this
01380                              one.  */
01381                            rel_csects =
01382                             reloc_info[enclosing->target_index].csects;
01383                            rel_csects[relindx] = bfd_und_section_ptr;
01384                            break;
01385                          }
01386 
01387                        /* We are about to create a TOC entry for
01388                           this symbol.  */
01389                        set_toc = h;
01390                      }
01391                   }
01392               }
01393            }
01394 
01395          {
01396            asection *enclosing;
01397 
01398            /* We need to create a new section.  We get the name from
01399               the csect storage mapping class, so that the linker can
01400               accumulate similar csects together.  */
01401 
01402            csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
01403            if (NULL == csect)
01404              goto error_return;
01405 
01406            /* The enclosing section is the main section : .data, .text
01407               or .bss that the csect is coming from.  */
01408            enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
01409            if (enclosing == NULL)
01410              goto error_return;
01411 
01412            if (! bfd_is_abs_section (enclosing)
01413               && ((bfd_vma) sym.n_value < enclosing->vma
01414                   || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
01415                      > enclosing->vma + enclosing->size)))
01416              {
01417               (*_bfd_error_handler)
01418                 (_("%B: csect `%s' not in enclosing section"),
01419                  abfd, name);
01420               bfd_set_error (bfd_error_bad_value);
01421               goto error_return;
01422              }
01423            csect->vma = sym.n_value;
01424            csect->filepos = (enclosing->filepos
01425                            + sym.n_value
01426                            - enclosing->vma);
01427            csect->size = aux.x_csect.x_scnlen.l;
01428            csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
01429            csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
01430 
01431            /* Record the enclosing section in the tdata for this new
01432               section.  */
01433            amt = sizeof (struct coff_section_tdata);
01434            csect->used_by_bfd = bfd_zalloc (abfd, amt);
01435            if (csect->used_by_bfd == NULL)
01436              goto error_return;
01437            amt = sizeof (struct xcoff_section_tdata);
01438            coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
01439            if (coff_section_data (abfd, csect)->tdata == NULL)
01440              goto error_return;
01441            xcoff_section_data (abfd, csect)->enclosing = enclosing;
01442            xcoff_section_data (abfd, csect)->lineno_count =
01443              enclosing->lineno_count;
01444 
01445            if (enclosing->owner == abfd)
01446              {
01447               struct internal_reloc *relocs;
01448               bfd_size_type relindx;
01449               struct internal_reloc *rel;
01450               asection **rel_csect;
01451 
01452               relocs = reloc_info[enclosing->target_index].relocs;
01453               amt = enclosing->reloc_count;
01454               relindx = xcoff_find_reloc (relocs, amt, csect->vma);
01455 
01456               rel = relocs + relindx;
01457               rel_csect = (reloc_info[enclosing->target_index].csects
01458                           + relindx);
01459 
01460               csect->rel_filepos = (enclosing->rel_filepos
01461                                   + relindx * bfd_coff_relsz (abfd));
01462               while (relindx < enclosing->reloc_count
01463                      && *rel_csect == NULL
01464                      && rel->r_vaddr < csect->vma + csect->size)
01465                 {
01466 
01467                   *rel_csect = csect;
01468                   csect->flags |= SEC_RELOC;
01469                   ++csect->reloc_count;
01470                   ++relindx;
01471                   ++rel;
01472                   ++rel_csect;
01473                 }
01474              }
01475 
01476            /* There are a number of other fields and section flags
01477               which we do not bother to set.  */
01478 
01479            csect_index = ((esym
01480                          - (bfd_byte *) obj_coff_external_syms (abfd))
01481                         / symesz);
01482 
01483            xcoff_section_data (abfd, csect)->first_symndx = csect_index;
01484 
01485            if (first_csect == NULL)
01486              first_csect = csect;
01487 
01488            /* If this symbol is C_EXT, we treat it as starting at the
01489               beginning of the newly created section.  */
01490            if (sym.n_sclass == C_EXT)
01491              {
01492               section = csect;
01493               value = 0;
01494              }
01495 
01496            /* If this is a TOC section for a symbol, record it.  */
01497            if (set_toc != NULL)
01498              set_toc->toc_section = csect;
01499          }
01500          break;
01501 
01502        case XTY_LD:
01503          /* This is a label definition.  The x_scnlen field is the
01504             symbol index of the csect.  Usually the XTY_LD symbol will
01505             follow its appropriate XTY_SD symbol.  The .set pseudo op can
01506             cause the XTY_LD to not follow the XTY_SD symbol. */
01507          {
01508            bfd_boolean bad;
01509 
01510            bad = FALSE;
01511            if (aux.x_csect.x_scnlen.l < 0
01512               || (aux.x_csect.x_scnlen.l
01513                   >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
01514              bad = TRUE;
01515            if (! bad)
01516              {
01517               section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
01518               if (section == NULL
01519                   || (section->flags & SEC_HAS_CONTENTS) == 0)
01520                 bad = TRUE;
01521              }
01522            if (bad)
01523              {
01524               (*_bfd_error_handler)
01525                 (_("%B: misplaced XTY_LD `%s'"),
01526                  abfd, name);
01527               bfd_set_error (bfd_error_bad_value);
01528               goto error_return;
01529              }
01530            csect = section;
01531            value = sym.n_value - csect->vma;
01532          }
01533          break;
01534 
01535        case XTY_CM:
01536          /* This is an unitialized csect.  We could base the name on
01537             the storage mapping class, but we don't bother except for
01538             an XMC_TD symbol.  If this csect is externally visible,
01539             it is a common symbol.  We put XMC_TD symbols in sections
01540             named .tocbss, and rely on the linker script to put that
01541             in the TOC area.  */
01542 
01543          if (csect != NULL)
01544            {
01545              xcoff_section_data (abfd, csect)->last_symndx =
01546               ((esym
01547                 - (bfd_byte *) obj_coff_external_syms (abfd))
01548                / symesz);
01549            }
01550 
01551          if (aux.x_csect.x_smclas == XMC_TD)
01552            {
01553              /* The linker script puts the .td section in the data
01554                section after the .tc section.  */
01555              csect = bfd_make_section_anyway_with_flags (abfd, ".td",
01556                                                    SEC_ALLOC);
01557            }
01558          else
01559            csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
01560                                                  SEC_ALLOC);
01561 
01562          if (csect == NULL)
01563            goto error_return;
01564          csect->vma = sym.n_value;
01565          csect->size = aux.x_csect.x_scnlen.l;
01566          csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
01567          /* There are a number of other fields and section flags
01568             which we do not bother to set.  */
01569 
01570          csect_index = ((esym
01571                        - (bfd_byte *) obj_coff_external_syms (abfd))
01572                       / symesz);
01573 
01574          amt = sizeof (struct coff_section_tdata);
01575          csect->used_by_bfd = bfd_zalloc (abfd, amt);
01576          if (csect->used_by_bfd == NULL)
01577            goto error_return;
01578          amt = sizeof (struct xcoff_section_tdata);
01579          coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
01580          if (coff_section_data (abfd, csect)->tdata == NULL)
01581            goto error_return;
01582          xcoff_section_data (abfd, csect)->first_symndx = csect_index;
01583 
01584          if (first_csect == NULL)
01585            first_csect = csect;
01586 
01587          if (sym.n_sclass == C_EXT)
01588            {
01589              csect->flags |= SEC_IS_COMMON;
01590              csect->size = 0;
01591              section = csect;
01592              value = aux.x_csect.x_scnlen.l;
01593            }
01594 
01595          break;
01596        }
01597 
01598       /* Check for magic symbol names.  */
01599       if ((smtyp == XTY_SD || smtyp == XTY_CM)
01600          && aux.x_csect.x_smclas != XMC_TC
01601          && aux.x_csect.x_smclas != XMC_TD)
01602        {
01603          int i = -1;
01604 
01605          if (name[0] == '_')
01606            {
01607              if (strcmp (name, "_text") == 0)
01608               i = XCOFF_SPECIAL_SECTION_TEXT;
01609              else if (strcmp (name, "_etext") == 0)
01610               i = XCOFF_SPECIAL_SECTION_ETEXT;
01611              else if (strcmp (name, "_data") == 0)
01612               i = XCOFF_SPECIAL_SECTION_DATA;
01613              else if (strcmp (name, "_edata") == 0)
01614               i = XCOFF_SPECIAL_SECTION_EDATA;
01615              else if (strcmp (name, "_end") == 0)
01616               i = XCOFF_SPECIAL_SECTION_END;
01617            }
01618          else if (name[0] == 'e' && strcmp (name, "end") == 0)
01619            i = XCOFF_SPECIAL_SECTION_END2;
01620 
01621          if (i != -1)
01622            xcoff_hash_table (info)->special_sections[i] = csect;
01623        }
01624 
01625       /* Now we have enough information to add the symbol to the
01626         linker hash table.  */
01627 
01628       if (sym.n_sclass == C_EXT)
01629        {
01630          bfd_boolean copy;
01631 
01632          BFD_ASSERT (section != NULL);
01633 
01634          /* We must copy the name into memory if we got it from the
01635             syment itself, rather than the string table.  */
01636          copy = default_copy;
01637          if (sym._n._n_n._n_zeroes != 0
01638              || sym._n._n_n._n_offset == 0)
01639            copy = TRUE;
01640 
01641          /* The AIX linker appears to only detect multiple symbol
01642             definitions when there is a reference to the symbol.  If
01643             a symbol is defined multiple times, and the only
01644             references are from the same object file, the AIX linker
01645             appears to permit it.  It does not merge the different
01646             definitions, but handles them independently.  On the
01647             other hand, if there is a reference, the linker reports
01648             an error.
01649 
01650             This matters because the AIX <net/net_globals.h> header
01651             file actually defines an initialized array, so we have to
01652             actually permit that to work.
01653 
01654             Just to make matters even more confusing, the AIX linker
01655             appears to permit multiple symbol definitions whenever
01656             the second definition is in an archive rather than an
01657             object file.  This may be a consequence of the manner in
01658             which it handles archives: I think it may load the entire
01659             archive in as separate csects, and then let garbage
01660             collection discard symbols.
01661 
01662             We also have to handle the case of statically linking a
01663             shared object, which will cause symbol redefinitions,
01664             although this is an easier case to detect.  */
01665 
01666          if (info->hash->creator == abfd->xvec)
01667            {
01668              if (! bfd_is_und_section (section))
01669               *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
01670                                               name, TRUE, copy, FALSE);
01671              else
01672               /* Make a copy of the symbol name to prevent problems with
01673                  merging symbols.  */
01674               *sym_hash = ((struct xcoff_link_hash_entry *)
01675                           bfd_wrapped_link_hash_lookup (abfd, info, name,
01676                                                     TRUE, TRUE, FALSE));
01677 
01678              if (*sym_hash == NULL)
01679               goto error_return;
01680              if (((*sym_hash)->root.type == bfd_link_hash_defined
01681                  || (*sym_hash)->root.type == bfd_link_hash_defweak)
01682                 && ! bfd_is_und_section (section)
01683                 && ! bfd_is_com_section (section))
01684               {
01685                 /* This is a second definition of a defined symbol.  */
01686                 if ((abfd->flags & DYNAMIC) != 0
01687                     && ((*sym_hash)->smclas != XMC_GL
01688                        || aux.x_csect.x_smclas == XMC_GL
01689                        || ((*sym_hash)->root.u.def.section->owner->flags
01690                            & DYNAMIC) == 0))
01691                   {
01692                     /* The new symbol is from a shared library, and
01693                       either the existing symbol is not global
01694                       linkage code or this symbol is global linkage
01695                       code.  If the existing symbol is global
01696                       linkage code and the new symbol is not, then
01697                       we want to use the new symbol.  */
01698                     section = bfd_und_section_ptr;
01699                     value = 0;
01700                   }
01701                 else if (((*sym_hash)->root.u.def.section->owner->flags
01702                          & DYNAMIC) != 0)
01703                   {
01704                     /* The existing symbol is from a shared library.
01705                       Replace it.  */
01706                     (*sym_hash)->root.type = bfd_link_hash_undefined;
01707                     (*sym_hash)->root.u.undef.abfd =
01708                      (*sym_hash)->root.u.def.section->owner;
01709                   }
01710                 else if (abfd->my_archive != NULL)
01711                   {
01712                     /* This is a redefinition in an object contained
01713                       in an archive.  Just ignore it.  See the
01714                       comment above.  */
01715                     section = bfd_und_section_ptr;
01716                     value = 0;
01717                   }
01718                 else if ((*sym_hash)->root.u.undef.next != NULL
01719                         || info->hash->undefs_tail == &(*sym_hash)->root)
01720                   {
01721                     /* This symbol has been referenced.  In this
01722                       case, we just continue and permit the
01723                       multiple definition error.  See the comment
01724                       above about the behaviour of the AIX linker.  */
01725                   }
01726                 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
01727                   {
01728                     /* The symbols are both csects of the same
01729                       class.  There is at least a chance that this
01730                       is a semi-legitimate redefinition.  */
01731                     section = bfd_und_section_ptr;
01732                     value = 0;
01733                     (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
01734                   }
01735               }
01736              else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
01737                      && ((*sym_hash)->root.type == bfd_link_hash_defined
01738                         || (*sym_hash)->root.type == bfd_link_hash_defweak)
01739                      && (bfd_is_und_section (section)
01740                         || bfd_is_com_section (section)))
01741               {
01742                 /* This is a reference to a multiply defined symbol.
01743                    Report the error now.  See the comment above
01744                    about the behaviour of the AIX linker.  We could
01745                    also do this with warning symbols, but I'm not
01746                    sure the XCOFF linker is wholly prepared to
01747                    handle them, and that would only be a warning,
01748                    not an error.  */
01749                 if (! ((*info->callbacks->multiple_definition)
01750                       (info, (*sym_hash)->root.root.string,
01751                        NULL, NULL, (bfd_vma) 0,
01752                        (*sym_hash)->root.u.def.section->owner,
01753                        (*sym_hash)->root.u.def.section,
01754                        (*sym_hash)->root.u.def.value)))
01755                   goto error_return;
01756                 /* Try not to give this error too many times.  */
01757                 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
01758               }
01759            }
01760 
01761          /* _bfd_generic_link_add_one_symbol may call the linker to
01762             generate an error message, and the linker may try to read
01763             the symbol table to give a good error.  Right now, the
01764             line numbers are in an inconsistent state, since they are
01765             counted both in the real sections and in the new csects.
01766             We need to leave the count in the real sections so that
01767             the linker can report the line number of the error
01768             correctly, so temporarily clobber the link to the csects
01769             so that the linker will not try to read the line numbers
01770             a second time from the csects.  */
01771          BFD_ASSERT (last_real->next == first_csect);
01772          last_real->next = NULL;
01773          if (! (_bfd_generic_link_add_one_symbol
01774                (info, abfd, name, flags, section, value,
01775                 NULL, copy, TRUE,
01776                 (struct bfd_link_hash_entry **) sym_hash)))
01777            goto error_return;
01778          last_real->next = first_csect;
01779 
01780          if (smtyp == XTY_CM)
01781            {
01782              if ((*sym_hash)->root.type != bfd_link_hash_common
01783                 || (*sym_hash)->root.u.c.p->section != csect)
01784               /* We don't need the common csect we just created.  */
01785               csect->size = 0;
01786              else
01787               (*sym_hash)->root.u.c.p->alignment_power
01788                 = csect->alignment_power;
01789            }
01790 
01791          if (info->hash->creator == abfd->xvec)
01792            {
01793              int flag;
01794 
01795              if (smtyp == XTY_ER || smtyp == XTY_CM)
01796               flag = XCOFF_REF_REGULAR;
01797              else
01798               flag = XCOFF_DEF_REGULAR;
01799              (*sym_hash)->flags |= flag;
01800 
01801              if ((*sym_hash)->smclas == XMC_UA
01802                 || flag == XCOFF_DEF_REGULAR)
01803               (*sym_hash)->smclas = aux.x_csect.x_smclas;
01804            }
01805        }
01806 
01807       *csect_cache = csect;
01808 
01809       esym += (sym.n_numaux + 1) * symesz;
01810       sym_hash += sym.n_numaux + 1;
01811       csect_cache += sym.n_numaux + 1;
01812     }
01813 
01814   BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
01815 
01816   /* Make sure that we have seen all the relocs.  */
01817   for (o = abfd->sections; o != first_csect; o = o->next)
01818     {
01819       /* Reset the section size and the line number count, since the
01820         data is now attached to the csects.  Don't reset the size of
01821         the .debug section, since we need to read it below in
01822         bfd_xcoff_size_dynamic_sections.  */
01823       if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
01824        o->size = 0;
01825       o->lineno_count = 0;
01826 
01827       if ((o->flags & SEC_RELOC) != 0)
01828        {
01829          bfd_size_type i;
01830          struct internal_reloc *rel;
01831          asection **rel_csect;
01832 
01833          rel = reloc_info[o->target_index].relocs;
01834          rel_csect = reloc_info[o->target_index].csects;
01835 
01836          for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
01837            {
01838              if (*rel_csect == NULL)
01839               {
01840                 (*_bfd_error_handler)
01841                   (_("%B: reloc %s:%d not in csect"),
01842                    abfd, o->name, i);
01843                 bfd_set_error (bfd_error_bad_value);
01844                 goto error_return;
01845               }
01846 
01847              /* We identify all symbols which are called, so that we
01848                can create glue code for calls to functions imported
01849                from dynamic objects.  */
01850              if (info->hash->creator == abfd->xvec
01851                 && *rel_csect != bfd_und_section_ptr
01852                 && (rel->r_type == R_BR
01853                     || rel->r_type == R_RBR)
01854                 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
01855               {
01856                 struct xcoff_link_hash_entry *h;
01857 
01858                 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
01859                 h->flags |= XCOFF_CALLED;
01860                 /* If the symbol name starts with a period, it is
01861                    the code of a function.  If the symbol is
01862                    currently undefined, then add an undefined symbol
01863                    for the function descriptor.  This should do no
01864                    harm, because any regular object that defines the
01865                    function should also define the function
01866                    descriptor.  It helps, because it means that we
01867                    will identify the function descriptor with a
01868                    dynamic object if a dynamic object defines it.  */
01869                 if (h->root.root.string[0] == '.'
01870                     && h->descriptor == NULL)
01871                   {
01872                     struct xcoff_link_hash_entry *hds;
01873                     struct bfd_link_hash_entry *bh;
01874 
01875                     hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
01876                                               h->root.root.string + 1,
01877                                               TRUE, FALSE, TRUE);
01878                     if (hds == NULL)
01879                      goto error_return;
01880                     if (hds->root.type == bfd_link_hash_new)
01881                      {
01882                        bh = &hds->root;
01883                        if (! (_bfd_generic_link_add_one_symbol
01884                              (info, abfd, hds->root.root.string,
01885                               (flagword) 0, bfd_und_section_ptr,
01886                               (bfd_vma) 0, NULL, FALSE,
01887                               TRUE, &bh)))
01888                          goto error_return;
01889                        hds = (struct xcoff_link_hash_entry *) bh;
01890                      }
01891                     hds->flags |= XCOFF_DESCRIPTOR;
01892                     BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
01893                               && (h->flags & XCOFF_DESCRIPTOR) == 0);
01894                     hds->descriptor = h;
01895                     h->descriptor = hds;
01896                   }
01897               }
01898            }
01899 
01900          free (reloc_info[o->target_index].csects);
01901          reloc_info[o->target_index].csects = NULL;
01902 
01903          /* Reset SEC_RELOC and the reloc_count, since the reloc
01904             information is now attached to the csects.  */
01905          o->flags &=~ SEC_RELOC;
01906          o->reloc_count = 0;
01907 
01908          /* If we are not keeping memory, free the reloc information.  */
01909          if (! info->keep_memory
01910              && coff_section_data (abfd, o) != NULL
01911              && coff_section_data (abfd, o)->relocs != NULL
01912              && ! coff_section_data (abfd, o)->keep_relocs)
01913            {
01914              free (coff_section_data (abfd, o)->relocs);
01915              coff_section_data (abfd, o)->relocs = NULL;
01916            }
01917        }
01918 
01919       /* Free up the line numbers.  FIXME: We could cache these
01920         somewhere for the final link, to avoid reading them again.  */
01921       if (reloc_info[o->target_index].linenos != NULL)
01922        {
01923          free (reloc_info[o->target_index].linenos);
01924          reloc_info[o->target_index].linenos = NULL;
01925        }
01926     }
01927 
01928   free (reloc_info);
01929 
01930   obj_coff_keep_syms (abfd) = keep_syms;
01931 
01932   return TRUE;
01933 
01934  error_return:
01935   if (reloc_info != NULL)
01936     {
01937       for (o = abfd->sections; o != NULL; o = o->next)
01938        {
01939          if (reloc_info[o->target_index].csects != NULL)
01940            free (reloc_info[o->target_index].csects);
01941          if (reloc_info[o->target_index].linenos != NULL)
01942            free (reloc_info[o->target_index].linenos);
01943        }
01944       free (reloc_info);
01945     }
01946   obj_coff_keep_syms (abfd) = keep_syms;
01947   return FALSE;
01948 }
01949 
01950 #undef N_TMASK
01951 #undef N_BTSHFT
01952 
01953 /* Add symbols from an XCOFF object file.  */
01954 
01955 static bfd_boolean
01956 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
01957 {
01958   if (! _bfd_coff_get_external_symbols (abfd))
01959     return FALSE;
01960   if (! xcoff_link_add_symbols (abfd, info))
01961     return FALSE;
01962   if (! info->keep_memory)
01963     {
01964       if (! _bfd_coff_free_symbols (abfd))
01965        return FALSE;
01966     }
01967   return TRUE;
01968 }
01969 
01970 /* Look through the loader symbols to see if this dynamic object
01971    should be included in the link.  The native linker uses the loader
01972    symbols, not the normal symbol table, so we do too.  */
01973 
01974 static bfd_boolean
01975 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
01976                                  struct bfd_link_info *info,
01977                                  bfd_boolean *pneeded)
01978 {
01979   asection *lsec;
01980   bfd_byte *contents;
01981   struct internal_ldhdr ldhdr;
01982   const char *strings;
01983   bfd_byte *elsym, *elsymend;
01984 
01985   *pneeded = FALSE;
01986 
01987   lsec = bfd_get_section_by_name (abfd, ".loader");
01988   if (lsec == NULL)
01989     /* There are no symbols, so don't try to include it.  */
01990     return TRUE;
01991 
01992   if (! xcoff_get_section_contents (abfd, lsec))
01993     return FALSE;
01994   contents = coff_section_data (abfd, lsec)->contents;
01995 
01996   bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
01997 
01998   strings = (char *) contents + ldhdr.l_stoff;
01999 
02000   elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
02001 
02002   elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
02003   for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
02004     {
02005       struct internal_ldsym ldsym;
02006       char nambuf[SYMNMLEN + 1];
02007       const char *name;
02008       struct bfd_link_hash_entry *h;
02009 
02010       bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
02011 
02012       /* We are only interested in exported symbols.  */
02013       if ((ldsym.l_smtype & L_EXPORT) == 0)
02014        continue;
02015 
02016       if (ldsym._l._l_l._l_zeroes == 0)
02017        name = strings + ldsym._l._l_l._l_offset;
02018       else
02019        {
02020          memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
02021          nambuf[SYMNMLEN] = '\0';
02022          name = nambuf;
02023        }
02024 
02025       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
02026 
02027       /* We are only interested in symbols that are currently
02028         undefined.  At this point we know that we are using an XCOFF
02029         hash table.  */
02030       if (h != NULL
02031          && h->type == bfd_link_hash_undefined
02032          && (((struct xcoff_link_hash_entry *) h)->flags
02033              & XCOFF_DEF_DYNAMIC) == 0)
02034        {
02035          if (! (*info->callbacks->add_archive_element) (info, abfd, name))
02036            return FALSE;
02037          *pneeded = TRUE;
02038          return TRUE;
02039        }
02040     }
02041 
02042   /* We do not need this shared object.  */
02043   if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
02044     {
02045       free (coff_section_data (abfd, lsec)->contents);
02046       coff_section_data (abfd, lsec)->contents = NULL;
02047     }
02048 
02049   return TRUE;
02050 }
02051 
02052 /* Look through the symbols to see if this object file should be
02053    included in the link.  */
02054 
02055 static bfd_boolean
02056 xcoff_link_check_ar_symbols (bfd *abfd,
02057                           struct bfd_link_info *info,
02058                           bfd_boolean *pneeded)
02059 {
02060   bfd_size_type symesz;
02061   bfd_byte *esym;
02062   bfd_byte *esym_end;
02063 
02064   *pneeded = FALSE;
02065 
02066   if ((abfd->flags & DYNAMIC) != 0
02067       && ! info->static_link
02068       && info->hash->creator == abfd->xvec)
02069     return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
02070 
02071   symesz = bfd_coff_symesz (abfd);
02072   esym = (bfd_byte *) obj_coff_external_syms (abfd);
02073   esym_end = esym + obj_raw_syment_count (abfd) * symesz;
02074   while (esym < esym_end)
02075     {
02076       struct internal_syment sym;
02077 
02078       bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
02079 
02080       if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
02081        {
02082          const char *name;
02083          char buf[SYMNMLEN + 1];
02084          struct bfd_link_hash_entry *h;
02085 
02086          /* This symbol is externally visible, and is defined by this
02087             object file.  */
02088          name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
02089 
02090          if (name == NULL)
02091            return FALSE;
02092          h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
02093 
02094          /* We are only interested in symbols that are currently
02095             undefined.  If a symbol is currently known to be common,
02096             XCOFF linkers do not bring in an object file which
02097             defines it.  We also don't bring in symbols to satisfy
02098             undefined references in shared objects.  */
02099          if (h != NULL
02100              && h->type == bfd_link_hash_undefined
02101              && (info->hash->creator != abfd->xvec
02102                 || (((struct xcoff_link_hash_entry *) h)->flags
02103                     & XCOFF_DEF_DYNAMIC) == 0))
02104            {
02105              if (! (*info->callbacks->add_archive_element) (info, abfd, name))
02106               return FALSE;
02107              *pneeded = TRUE;
02108              return TRUE;
02109            }
02110        }
02111 
02112       esym += (sym.n_numaux + 1) * symesz;
02113     }
02114 
02115   /* We do not need this object file.  */
02116   return TRUE;
02117 }
02118 
02119 /* Check a single archive element to see if we need to include it in
02120    the link.  *PNEEDED is set according to whether this element is
02121    needed in the link or not.  This is called via
02122    _bfd_generic_link_add_archive_symbols.  */
02123 
02124 static bfd_boolean
02125 xcoff_link_check_archive_element (bfd *abfd,
02126                               struct bfd_link_info *info,
02127                               bfd_boolean *pneeded)
02128 {
02129   if (! _bfd_coff_get_external_symbols (abfd))
02130     return FALSE;
02131 
02132   if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
02133     return FALSE;
02134 
02135   if (*pneeded)
02136     {
02137       if (! xcoff_link_add_symbols (abfd, info))
02138        return FALSE;
02139     }
02140 
02141   if (! info->keep_memory || ! *pneeded)
02142     {
02143       if (! _bfd_coff_free_symbols (abfd))
02144        return FALSE;
02145     }
02146 
02147   return TRUE;
02148 }
02149 
02150 /* Given an XCOFF BFD, add symbols to the global hash table as
02151    appropriate.  */
02152 
02153 bfd_boolean
02154 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
02155 {
02156   switch (bfd_get_format (abfd))
02157     {
02158     case bfd_object:
02159       return xcoff_link_add_object_symbols (abfd, info);
02160 
02161     case bfd_archive:
02162       /* If the archive has a map, do the usual search.  We then need
02163         to check the archive for dynamic objects, because they may not
02164         appear in the archive map even though they should, perhaps, be
02165         included.  If the archive has no map, we just consider each object
02166         file in turn, since that apparently is what the AIX native linker
02167         does.  */
02168       if (bfd_has_map (abfd))
02169        {
02170          if (! (_bfd_generic_link_add_archive_symbols
02171                (abfd, info, xcoff_link_check_archive_element)))
02172            return FALSE;
02173        }
02174 
02175       {
02176        bfd *member;
02177 
02178        member = bfd_openr_next_archived_file (abfd, NULL);
02179        while (member != NULL)
02180          {
02181            if (bfd_check_format (member, bfd_object)
02182               && (info->hash->creator == member->xvec)
02183               && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
02184              {
02185               bfd_boolean needed;
02186 
02187               if (! xcoff_link_check_archive_element (member, info,
02188                                                  &needed))
02189                 return FALSE;
02190               if (needed)
02191                 member->archive_pass = -1;
02192              }
02193            member = bfd_openr_next_archived_file (abfd, member);
02194          }
02195       }
02196 
02197       return TRUE;
02198 
02199     default:
02200       bfd_set_error (bfd_error_wrong_format);
02201       return FALSE;
02202     }
02203 }
02204 
02205 /* Mark a symbol as not being garbage, including the section in which
02206    it is defined.  */
02207 
02208 static inline bfd_boolean
02209 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
02210 {
02211   if ((h->flags & XCOFF_MARK) != 0)
02212     return TRUE;
02213 
02214   h->flags |= XCOFF_MARK;
02215   if (h->root.type == bfd_link_hash_defined
02216       || h->root.type == bfd_link_hash_defweak)
02217     {
02218       asection *hsec;
02219 
02220       hsec = h->root.u.def.section;
02221       if (! bfd_is_abs_section (hsec)
02222          && (hsec->flags & SEC_MARK) == 0)
02223        {
02224          if (! xcoff_mark (info, hsec))
02225            return FALSE;
02226        }
02227     }
02228 
02229   if (h->toc_section != NULL
02230       && (h->toc_section->flags & SEC_MARK) == 0)
02231     {
02232       if (! xcoff_mark (info, h->toc_section))
02233        return FALSE;
02234     }
02235 
02236   return TRUE;
02237 }
02238 
02239 /* The mark phase of garbage collection.  For a given section, mark
02240    it, and all the sections which define symbols to which it refers.
02241    Because this function needs to look at the relocs, we also count
02242    the number of relocs which need to be copied into the .loader
02243    section.  */
02244 
02245 static bfd_boolean
02246 xcoff_mark (struct bfd_link_info *info, asection *sec)
02247 {
02248   if (bfd_is_abs_section (sec)
02249       || (sec->flags & SEC_MARK) != 0)
02250     return TRUE;
02251 
02252   sec->flags |= SEC_MARK;
02253 
02254   if (sec->owner->xvec == info->hash->creator
02255       && coff_section_data (sec->owner, sec) != NULL
02256       && xcoff_section_data (sec->owner, sec) != NULL)
02257     {
02258       struct xcoff_link_hash_entry **hp, **hpend;
02259       struct internal_reloc *rel, *relend;
02260 
02261       /* Mark all the symbols in this section.  */
02262       hp = (obj_xcoff_sym_hashes (sec->owner)
02263            + xcoff_section_data (sec->owner, sec)->first_symndx);
02264       hpend = (obj_xcoff_sym_hashes (sec->owner)
02265               + xcoff_section_data (sec->owner, sec)->last_symndx);
02266       for (; hp < hpend; hp++)
02267        {
02268          struct xcoff_link_hash_entry *h;
02269 
02270          h = *hp;
02271          if (h != NULL
02272              && (h->flags & XCOFF_MARK) == 0)
02273            {
02274              if (! xcoff_mark_symbol (info, h))
02275               return FALSE;
02276            }
02277        }
02278 
02279       /* Look through the section relocs.  */
02280       if ((sec->flags & SEC_RELOC) != 0
02281          && sec->reloc_count > 0)
02282        {
02283          rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
02284                                        NULL, FALSE, NULL);
02285          if (rel == NULL)
02286            return FALSE;
02287          relend = rel + sec->reloc_count;
02288          for (; rel < relend; rel++)
02289            {
02290              asection *rsec;
02291              struct xcoff_link_hash_entry *h;
02292 
02293              if ((unsigned int) rel->r_symndx
02294                 > obj_raw_syment_count (sec->owner))
02295               continue;
02296 
02297              h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
02298              if (h != NULL
02299                 && (h->flags & XCOFF_MARK) == 0)
02300               {
02301                 if (! xcoff_mark_symbol (info, h))
02302                   return FALSE;
02303               }
02304 
02305              rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
02306              if (rsec != NULL
02307                 && (rsec->flags & SEC_MARK) == 0)
02308               {
02309                 if (! xcoff_mark (info, rsec))
02310                   return FALSE;
02311               }
02312 
02313              /* See if this reloc needs to be copied into the .loader
02314                section.  */
02315              switch (rel->r_type)
02316               {
02317               default:
02318                 if (h == NULL
02319                     || h->root.type == bfd_link_hash_defined
02320                     || h->root.type == bfd_link_hash_defweak
02321                     || h->root.type == bfd_link_hash_common
02322                     || ((h->flags & XCOFF_CALLED) != 0
02323                        && (h->root.type == bfd_link_hash_undefined
02324                            || h->root.type == bfd_link_hash_undefweak)
02325                        && h->root.root.string[0] == '.'
02326                        && h->descriptor != NULL
02327                        && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
02328                            || ((h->descriptor->flags & XCOFF_IMPORT) != 0
02329                               && (h->descriptor->flags
02330                                   & XCOFF_DEF_REGULAR) == 0))))
02331                   break;
02332                 /* Fall through.  */
02333               case R_POS:
02334               case R_NEG:
02335               case R_RL:
02336               case R_RLA:
02337                 ++xcoff_hash_table (info)->ldrel_count;
02338                 if (h != NULL)
02339                   h->flags |= XCOFF_LDREL;
02340                 break;
02341               case R_TOC:
02342               case R_GL:
02343               case R_TCL:
02344               case R_TRL:
02345               case R_TRLA:
02346                 /* We should never need a .loader reloc for a TOC
02347                    relative reloc.  */
02348                 break;
02349               }
02350            }
02351 
02352          if (! info->keep_memory
02353              && coff_section_data (sec->owner, sec) != NULL
02354              && coff_section_data (sec->owner, sec)->relocs != NULL
02355              && ! coff_section_data (sec->owner, sec)->keep_relocs)
02356            {
02357              free (coff_section_data (sec->owner, sec)->relocs);
02358              coff_section_data (sec->owner, sec)->relocs = NULL;
02359            }
02360        }
02361     }
02362 
02363   return TRUE;
02364 }
02365 
02366 /* Routines that are called after all the input files have been
02367    handled, but before the sections are laid out in memory.  */
02368 
02369 /* The sweep phase of garbage collection.  Remove all garbage
02370    sections.  */
02371 
02372 static void
02373 xcoff_sweep (struct bfd_link_info *info)
02374 {
02375   bfd *sub;
02376 
02377   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
02378     {
02379       asection *o;
02380 
02381       for (o = sub->sections; o != NULL; o = o->next)
02382        {
02383          if ((o->flags & SEC_MARK) == 0)
02384            {
02385              /* Keep all sections from non-XCOFF input files.  Keep
02386                special sections.  Keep .debug sections for the
02387                moment.  */
02388              if (sub->xvec != info->hash->creator
02389                 || o == xcoff_hash_table (info)->debug_section
02390                 || o == xcoff_hash_table (info)->loader_section
02391                 || o == xcoff_hash_table (info)->linkage_section
02392                 || o == xcoff_hash_table (info)->toc_section
02393                 || o == xcoff_hash_table (info)->descriptor_section
02394                 || strcmp (o->name, ".debug") == 0)
02395               o->flags |= SEC_MARK;
02396              else
02397               {
02398                 o->size = 0;
02399                 o->reloc_count = 0;
02400                 o->lineno_count = 0;
02401               }
02402            }
02403        }
02404     }
02405 }
02406 
02407 /* Record the number of elements in a set.  This is used to output the
02408    correct csect length.  */
02409 
02410 bfd_boolean
02411 bfd_xcoff_link_record_set (bfd *output_bfd,
02412                         struct bfd_link_info *info,
02413                         struct bfd_link_hash_entry *harg,
02414                         bfd_size_type size)
02415 {
02416   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
02417   struct xcoff_link_size_list *n;
02418   bfd_size_type amt;
02419 
02420   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
02421     return TRUE;
02422 
02423   /* This will hardly ever be called.  I don't want to burn four bytes
02424      per global symbol, so instead the size is kept on a linked list
02425      attached to the hash table.  */
02426   amt = sizeof (* n);
02427   n = bfd_alloc (output_bfd, amt);
02428   if (n == NULL)
02429     return FALSE;
02430   n->next = xcoff_hash_table (info)->size_list;
02431   n->h = h;
02432   n->size = size;
02433   xcoff_hash_table (info)->size_list = n;
02434 
02435   h->flags |= XCOFF_HAS_SIZE;
02436 
02437   return TRUE;
02438 }
02439 
02440 /* Import a symbol.  */
02441 
02442 bfd_boolean
02443 bfd_xcoff_import_symbol (bfd *output_bfd,
02444                       struct bfd_link_info *info,
02445                       struct bfd_link_hash_entry *harg,
02446                       bfd_vma val,
02447                       const char *imppath,
02448                       const char *impfile,
02449                       const char *impmember,
02450                       unsigned int syscall_flag)
02451 {
02452   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
02453 
02454   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
02455     return TRUE;
02456 
02457   /* A symbol name which starts with a period is the code for a
02458      function.  If the symbol is undefined, then add an undefined
02459      symbol for the function descriptor, and import that instead.  */
02460   if (h->root.root.string[0] == '.'
02461       && h->root.type == bfd_link_hash_undefined
02462       && val == (bfd_vma) -1)
02463     {
02464       struct xcoff_link_hash_entry *hds;
02465 
02466       hds = h->descriptor;
02467       if (hds == NULL)
02468        {
02469          hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
02470                                    h->root.root.string + 1,
02471                                    TRUE, FALSE, TRUE);
02472          if (hds == NULL)
02473            return FALSE;
02474          if (hds->root.type == bfd_link_hash_new)
02475            {
02476              hds->root.type = bfd_link_hash_undefined;
02477              hds->root.u.undef.abfd = h->root.u.undef.abfd;
02478            }
02479          hds->flags |= XCOFF_DESCRIPTOR;
02480          BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
02481                     && (h->flags & XCOFF_DESCRIPTOR) == 0);
02482          hds->descriptor = h;
02483          h->descriptor = hds;
02484        }
02485 
02486       /* Now, if the descriptor is undefined, import the descriptor
02487         rather than the symbol we were told to import.  FIXME: Is
02488         this correct in all cases?  */
02489       if (hds->root.type == bfd_link_hash_undefined)
02490        h = hds;
02491     }
02492 
02493   h->flags |= (XCOFF_IMPORT | syscall_flag);
02494 
02495   if (val != (bfd_vma) -1)
02496     {
02497       if (h->root.type == bfd_link_hash_defined
02498          && (! bfd_is_abs_section (h->root.u.def.section)
02499              || h->root.u.def.value != val))
02500        {
02501          if (! ((*info->callbacks->multiple_definition)
02502                (info, h->root.root.string, h->root.u.def.section->owner,
02503                 h->root.u.def.section, h->root.u.def.value,
02504                 output_bfd, bfd_abs_section_ptr, val)))
02505            return FALSE;
02506        }
02507 
02508       h->root.type = bfd_link_hash_defined;
02509       h->root.u.def.section = bfd_abs_section_ptr;
02510       h->root.u.def.value = val;
02511     }
02512 
02513   /* We overload the ldindx field to hold the l_ifile value for this
02514      symbol.  */
02515   BFD_ASSERT (h->ldsym == NULL);
02516   BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
02517   if (imppath == NULL)
02518     h->ldindx = -1;
02519   else
02520     {
02521       unsigned int c;
02522       struct xcoff_import_file **pp;
02523 
02524       /* We start c at 1 because the first entry in the import list is
02525         reserved for the library search path.  */
02526       for (pp = &xcoff_hash_table (info)->imports, c = 1;
02527           *pp != NULL;
02528           pp = &(*pp)->next, ++c)
02529        {
02530          if (strcmp ((*pp)->path, imppath) == 0
02531              && strcmp ((*pp)->file, impfile) == 0
02532              && strcmp ((*pp)->member, impmember) == 0)
02533            break;
02534        }
02535 
02536       if (*pp == NULL)
02537        {
02538          struct xcoff_import_file *n;
02539          bfd_size_type amt = sizeof (* n);
02540 
02541          n = bfd_alloc (output_bfd, amt);
02542          if (n == NULL)
02543            return FALSE;
02544          n->next = NULL;
02545          n->path = imppath;
02546          n->file = impfile;
02547          n->member = impmember;
02548          *pp = n;
02549        }
02550 
02551       h->ldindx = c;
02552     }
02553 
02554   return TRUE;
02555 }
02556 
02557 /* Export a symbol.  */
02558 
02559 bfd_boolean
02560 bfd_xcoff_export_symbol (bfd *output_bfd,
02561                       struct bfd_link_info *info,
02562                       struct bfd_link_hash_entry *harg)
02563 {
02564   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
02565 
02566   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
02567     return TRUE;
02568 
02569   h->flags |= XCOFF_EXPORT;
02570 
02571   /* FIXME: I'm not at all sure what syscall is supposed to mean, so
02572      I'm just going to ignore it until somebody explains it.  */
02573 
02574   /* See if this is a function descriptor.  It may be one even though
02575      it is not so marked.  */
02576   if ((h->flags & XCOFF_DESCRIPTOR) == 0
02577       && h->root.root.string[0] != '.')
02578     {
02579       char *fnname;
02580       struct xcoff_link_hash_entry *hfn;
02581       bfd_size_type amt = strlen (h->root.root.string) + 2;
02582 
02583       fnname = bfd_malloc (amt);
02584       if (fnname == NULL)
02585        return FALSE;
02586       fnname[0] = '.';
02587       strcpy (fnname + 1, h->root.root.string);
02588       hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
02589                                 fnname, FALSE, FALSE, TRUE);
02590       free (fnname);
02591       if (hfn != NULL
02592          && hfn->smclas == XMC_PR
02593          && (hfn->root.type == bfd_link_hash_defined
02594              || hfn->root.type == bfd_link_hash_defweak))
02595        {
02596          h->flags |= XCOFF_DESCRIPTOR;
02597          h->descriptor = hfn;
02598          hfn->descriptor = h;
02599        }
02600     }
02601 
02602   /* Make sure we don't garbage collect this symbol.  */
02603   if (! xcoff_mark_symbol (info, h))
02604     return FALSE;
02605 
02606   /* If this is a function descriptor, make sure we don't garbage
02607      collect the associated function code.  We normally don't have to
02608      worry about this, because the descriptor will be attached to a
02609      section with relocs, but if we are creating the descriptor
02610      ourselves those relocs will not be visible to the mark code.  */
02611   if ((h->flags & XCOFF_DESCRIPTOR) != 0)
02612     {
02613       if (! xcoff_mark_symbol (info, h->descriptor))
02614        return FALSE;
02615     }
02616 
02617   return TRUE;
02618 }
02619 
02620 /* Count a reloc against a symbol.  This is called for relocs
02621    generated by the linker script, typically for global constructors
02622    and destructors.  */
02623 
02624 bfd_boolean
02625 bfd_xcoff_link_count_reloc (bfd *output_bfd,
02626                          struct bfd_link_info *info,
02627                          const char *name)
02628 {
02629   struct xcoff_link_hash_entry *h;
02630 
02631   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
02632     return TRUE;
02633 
02634   h = ((struct xcoff_link_hash_entry *)
02635        bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
02636                                  FALSE));
02637   if (h == NULL)
02638     {
02639       (*_bfd_error_handler) (_("%s: no such symbol"), name);
02640       bfd_set_error (bfd_error_no_symbols);
02641       return FALSE;
02642     }
02643 
02644   h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
02645   ++xcoff_hash_table (info)->ldrel_count;
02646 
02647   /* Mark the symbol to avoid garbage collection.  */
02648   if (! xcoff_mark_symbol (info, h))
02649     return FALSE;
02650 
02651   return TRUE;
02652 }
02653 
02654 /* This function is called for each symbol to which the linker script
02655    assigns a value.  */
02656 
02657 bfd_boolean
02658 bfd_xcoff_record_link_assignment (bfd *output_bfd,
02659                               struct bfd_link_info *info,
02660                               const char *name)
02661 {
02662   struct xcoff_link_hash_entry *h;
02663 
02664   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
02665     return TRUE;
02666 
02667   h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
02668                            FALSE);
02669   if (h == NULL)
02670     return FALSE;
02671 
02672   h->flags |= XCOFF_DEF_REGULAR;
02673 
02674   return TRUE;
02675 }
02676 
02677 /* Add a symbol to the .loader symbols, if necessary.  */
02678 
02679 static bfd_boolean
02680 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
02681 {
02682   struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
02683   bfd_size_type amt;
02684 
02685   if (h->root.type == bfd_link_hash_warning)
02686     h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
02687 
02688   /* __rtinit, this symbol has special handling. */
02689   if (h->flags & XCOFF_RTINIT)
02690       return TRUE;
02691 
02692   /* If this is a final link, and the symbol was defined as a common
02693      symbol in a regular object file, and there was no definition in
02694      any dynamic object, then the linker will have allocated space for
02695      the symbol in a common section but the XCOFF_DEF_REGULAR flag
02696      will not have been set.  */
02697   if (h->root.type == bfd_link_hash_defined
02698       && (h->flags & XCOFF_DEF_REGULAR) == 0
02699       && (h->flags & XCOFF_REF_REGULAR) != 0
02700       && (h->flags & XCOFF_DEF_DYNAMIC) == 0
02701       && (bfd_is_abs_section (h->root.u.def.section)
02702          || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
02703     h->flags |= XCOFF_DEF_REGULAR;
02704 
02705   /* If all defined symbols should be exported, mark them now.  We
02706      don't want to export the actual functions, just the function
02707      descriptors.  */
02708   if (ldinfo->export_defineds
02709       && (h->flags & XCOFF_DEF_REGULAR) != 0
02710       && h->root.root.string[0] != '.')
02711     {
02712       bfd_boolean export;
02713 
02714       /* We don't export a symbol which is being defined by an object
02715         included from an archive which contains a shared object.  The
02716         rationale is that if an archive contains both an unshared and
02717         a shared object, then there must be some reason that the
02718         unshared object is unshared, and we don't want to start
02719         providing a shared version of it.  In particular, this solves
02720         a bug involving the _savefNN set of functions.  gcc will call
02721         those functions without providing a slot to restore the TOC,
02722         so it is essential that these functions be linked in directly
02723         and not from a shared object, which means that a shared
02724         object which also happens to link them in must not export
02725         them.  This is confusing, but I haven't been able to think of
02726         a different approach.  Note that the symbols can, of course,
02727         be exported explicitly.  */
02728       export = TRUE;
02729       if ((h->root.type == bfd_link_hash_defined
02730           || h->root.type == bfd_link_hash_defweak)
02731          && h->root.u.def.section->owner != NULL
02732          && h->root.u.def.section->owner->my_archive != NULL)
02733        {
02734          bfd *arbfd, *member;
02735 
02736          arbfd = h->root.u.def.section->owner->my_archive;
02737          member = bfd_openr_next_archived_file (arbfd, NULL);
02738          while (member != NULL)
02739            {
02740              if ((member->flags & DYNAMIC) != 0)
02741               {
02742                 export = FALSE;
02743                 break;
02744               }
02745              member = bfd_openr_next_archived_file (arbfd, member);
02746            }
02747        }
02748 
02749       if (export)
02750        h->flags |= XCOFF_EXPORT;
02751     }
02752 
02753   /* We don't want to garbage collect symbols which are not defined in
02754      XCOFF files.  This is a convenient place to mark them.  */
02755   if (xcoff_hash_table (ldinfo->info)->gc
02756       && (h->flags & XCOFF_MARK) == 0
02757       && (h->root.type == bfd_link_hash_defined
02758          || h->root.type == bfd_link_hash_defweak)
02759       && (h->root.u.def.section->owner == NULL
02760          || (h->root.u.def.section->owner->xvec
02761              != ldinfo->info->hash->creator)))
02762     h->flags |= XCOFF_MARK;
02763 
02764   /* If this symbol is called and defined in a dynamic object, or it
02765      is imported, then we need to set up global linkage code for it.
02766      (Unless we did garbage collection and we didn't need this
02767      symbol.)  */
02768   if ((h->flags & XCOFF_CALLED) != 0
02769       && (h->root.type == bfd_link_hash_undefined
02770          || h->root.type == bfd_link_hash_undefweak)
02771       && h->root.root.string[0] == '.'
02772       && h->descriptor != NULL
02773       && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
02774          || ((h->descriptor->flags & XCOFF_IMPORT) != 0
02775              && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
02776       && (! xcoff_hash_table (ldinfo->info)->gc
02777          || (h->flags & XCOFF_MARK) != 0))
02778     {
02779       asection *sec;
02780       struct xcoff_link_hash_entry *hds;
02781 
02782       sec = xcoff_hash_table (ldinfo->info)->linkage_section;
02783       h->root.type = bfd_link_hash_defined;
02784       h->root.u.def.section = sec;
02785       h->root.u.def.value = sec->size;
02786       h->smclas = XMC_GL;
02787       h->flags |= XCOFF_DEF_REGULAR;
02788       sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
02789 
02790       /* The global linkage code requires a TOC entry for the
02791         descriptor.  */
02792       hds = h->descriptor;
02793       BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
02794                  || hds->root.type == bfd_link_hash_undefweak)
02795                 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
02796       hds->flags |= XCOFF_MARK;
02797       if (hds->toc_section == NULL)
02798        {
02799          int byte_size;
02800 
02801          /* 32 vs 64
02802             xcoff32 uses 4 bytes in the toc.
02803             xcoff64 uses 8 bytes in the toc.  */
02804          if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
02805            byte_size = 8;
02806          else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
02807            byte_size = 4;
02808          else
02809            return FALSE;
02810 
02811          hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
02812          hds->u.toc_offset = hds->toc_section->size;
02813          hds->toc_section->size += byte_size;
02814          ++xcoff_hash_table (ldinfo->info)->ldrel_count;
02815          ++hds->toc_section->reloc_count;
02816          hds->indx = -2;
02817          hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
02818 
02819          /* We need to call xcoff_build_ldsyms recursively here,
02820             because we may already have passed hds on the traversal.  */
02821          xcoff_build_ldsyms (hds, p);
02822        }
02823     }
02824 
02825   /* If this symbol is exported, but not defined, we need to try to
02826      define it.  */
02827   if ((h->flags & XCOFF_EXPORT) != 0
02828       && (h->flags & XCOFF_IMPORT) == 0
02829       && (h->flags & XCOFF_DEF_REGULAR) == 0
02830       && (h->flags & XCOFF_DEF_DYNAMIC) == 0
02831       && (h->root.type == bfd_link_hash_undefined
02832          || h->root.type == bfd_link_hash_undefweak))
02833     {
02834       if ((h->flags & XCOFF_DESCRIPTOR) != 0
02835          && (h->descriptor->root.type == bfd_link_hash_defined
02836              || h->descriptor->root.type == bfd_link_hash_defweak))
02837        {
02838          asection *sec;
02839 
02840          /* This is an undefined function descriptor associated with
02841             a defined entry point.  We can build up a function
02842             descriptor ourselves.  Believe it or not, the AIX linker
02843             actually does this, and there are cases where we need to
02844             do it as well.  */
02845          sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
02846          h->root.type = bfd_link_hash_defined;
02847          h->root.u.def.section = sec;
02848          h->root.u.def.value = sec->size;
02849          h->smclas = XMC_DS;
02850          h->flags |= XCOFF_DEF_REGULAR;
02851 
02852          /* The size of the function descriptor depends if this is an
02853             xcoff32 (12) or xcoff64 (24).  */
02854          sec->size +=
02855            bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
02856 
02857          /* A function descriptor uses two relocs: one for the
02858             associated code, and one for the TOC address.  */
02859          xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
02860          sec->reloc_count += 2;
02861 
02862          /* We handle writing out the contents of the descriptor in
02863             xcoff_write_global_symbol.  */
02864        }
02865       else
02866        {
02867          (*_bfd_error_handler)
02868            (_("warning: attempt to export undefined symbol `%s'"),
02869             h->root.root.string);
02870          h->ldsym = NULL;
02871          return TRUE;
02872        }
02873     }
02874 
02875   /* If this is still a common symbol, and it wasn't garbage
02876      collected, we need to actually allocate space for it in the .bss
02877      section.  */
02878   if (h->root.type == bfd_link_hash_common
02879       && (! xcoff_hash_table (ldinfo->info)->gc
02880          || (h->flags & XCOFF_MARK) != 0)
02881       && h->root.u.c.p->section->size == 0)
02882     {
02883       BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
02884       h->root.u.c.p->section->size = h->root.u.c.size;
02885     }
02886 
02887   /* We need to add a symbol to the .loader section if it is mentioned
02888      in a reloc which we are copying to the .loader section and it was
02889      not defined or common, or if it is the entry point, or if it is
02890      being exported.  */
02891 
02892   if (((h->flags & XCOFF_LDREL) == 0
02893        || h->root.type == bfd_link_hash_defined
02894        || h->root.type == bfd_link_hash_defweak
02895        || h->root.type == bfd_link_hash_common)
02896       && (h->flags & XCOFF_ENTRY) == 0
02897       && (h->flags & XCOFF_EXPORT) == 0)
02898     {
02899       h->ldsym = NULL;
02900       return TRUE;
02901     }
02902 
02903   /* We don't need to add this symbol if we did garbage collection and
02904      we did not mark this symbol.  */
02905   if (xcoff_hash_table (ldinfo->info)->gc
02906       && (h->flags & XCOFF_MARK) == 0)
02907     {
02908       h->ldsym = NULL;
02909       return TRUE;
02910     }
02911 
02912   /* We may have already processed this symbol due to the recursive
02913      call above.  */
02914   if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
02915     return TRUE;
02916 
02917   /* We need to add this symbol to the .loader symbols.  */
02918 
02919   BFD_ASSERT (h->ldsym == NULL);
02920   amt = sizeof (struct internal_ldsym);
02921   h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
02922   if (h->ldsym == NULL)
02923     {
02924       ldinfo->failed = TRUE;
02925       return FALSE;
02926     }
02927 
02928   if ((h->flags & XCOFF_IMPORT) != 0)
02929     h->ldsym->l_ifile = h->ldindx;
02930 
02931   /* The first 3 symbol table indices are reserved to indicate the
02932      data, text and bss sections.  */
02933   h->ldindx = ldinfo->ldsym_count + 3;
02934 
02935   ++ldinfo->ldsym_count;
02936 
02937   if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
02938                                  h->ldsym, h->root.root.string))
02939     return FALSE;
02940 
02941   h->flags |= XCOFF_BUILT_LDSYM;
02942 
02943   return TRUE;
02944 }
02945 /* Build the .loader section.  This is called by the XCOFF linker
02946    emulation before_allocation routine.  We must set the size of the
02947    .loader section before the linker lays out the output file.
02948    LIBPATH is the library path to search for shared objects; this is
02949    normally built from the -L arguments passed to the linker.  ENTRY
02950    is the name of the entry point symbol (the -e linker option).
02951    FILE_ALIGN is the alignment to use for sections within the file
02952    (the -H linker option).  MAXSTACK is the maximum stack size (the
02953    -bmaxstack linker option).  MAXDATA is the maximum data size (the
02954    -bmaxdata linker option).  GC is whether to do garbage collection
02955    (the -bgc linker option).  MODTYPE is the module type (the
02956    -bmodtype linker option).  TEXTRO is whether the text section must
02957    be read only (the -btextro linker option).  EXPORT_DEFINEDS is
02958    whether all defined symbols should be exported (the -unix linker
02959    option).  SPECIAL_SECTIONS is set by this routine to csects with
02960    magic names like _end.  */
02961 
02962 bfd_boolean
02963 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
02964                              struct bfd_link_info *info,
02965                              const char *libpath,
02966                              const char *entry,
02967                              unsigned long file_align,
02968                              unsigned long maxstack,
02969                              unsigned long maxdata,
02970                              bfd_boolean gc,
02971                              int modtype,
02972                              bfd_boolean textro,
02973                              bfd_boolean export_defineds,
02974                              asection **special_sections,
02975                              bfd_boolean rtld)
02976 {
02977   struct xcoff_link_hash_entry *hentry;
02978   asection *lsec;
02979   struct xcoff_loader_info ldinfo;
02980   int i;
02981   size_t impsize, impcount;
02982   struct xcoff_import_file *fl;
02983   struct internal_ldhdr *ldhdr;
02984   bfd_size_type stoff;
02985   char *out;
02986   asection *sec;
02987   bfd *sub;
02988   struct bfd_strtab_hash *debug_strtab;
02989   bfd_byte *debug_contents = NULL;
02990   bfd_size_type amt;
02991 
02992   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
02993     {
02994       for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
02995        special_sections[i] = NULL;
02996       return TRUE;
02997     }
02998 
02999   ldinfo.failed = FALSE;
03000   ldinfo.output_bfd = output_bfd;
03001   ldinfo.info = info;
03002   ldinfo.export_defineds = export_defineds;
03003   ldinfo.ldsym_count = 0;
03004   ldinfo.string_size = 0;
03005   ldinfo.strings = NULL;
03006   ldinfo.string_alc = 0;
03007 
03008   xcoff_data (output_bfd)->maxstack = maxstack;
03009   xcoff_data (output_bfd)->maxdata = maxdata;
03010   xcoff_data (output_bfd)->modtype = modtype;
03011 
03012   xcoff_hash_table (info)->file_align = file_align;
03013   xcoff_hash_table (info)->textro = textro;
03014 
03015   hentry = NULL;
03016   if (entry != NULL)
03017     {
03018       hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
03019                                    FALSE, FALSE, TRUE);
03020       if (hentry != NULL)
03021        hentry->flags |= XCOFF_ENTRY;
03022     }
03023 
03024   /* __rtinit */
03025   if (info->init_function || info->fini_function || rtld)
03026     {
03027       struct xcoff_link_hash_entry *hsym;
03028       struct internal_ldsym *ldsym;
03029 
03030       hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
03031                                  "__rtinit", FALSE, FALSE, TRUE);
03032       if (hsym == NULL)
03033        {
03034          (*_bfd_error_handler)
03035            (_("error: undefined symbol __rtinit"));
03036          return FALSE;
03037        }
03038 
03039       xcoff_mark_symbol (info, hsym);
03040       hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
03041 
03042       /* __rtinit initialized.  */
03043       amt = sizeof (* ldsym);
03044       ldsym = bfd_malloc (amt);
03045 
03046       ldsym->l_value = 0;          /* Will be filled in later.  */
03047       ldsym->l_scnum = 2;          /* Data section.  */
03048       ldsym->l_smtype = XTY_SD;           /* Csect section definition.  */
03049       ldsym->l_smclas = 5;         /* .rw.  */
03050       ldsym->l_ifile = 0;          /* Special system loader symbol.  */
03051       ldsym->l_parm = 0;           /* NA.  */
03052 
03053       /* Force __rtinit to be the first symbol in the loader symbol table
03054         See xcoff_build_ldsyms
03055 
03056         The first 3 symbol table indices are reserved to indicate the data,
03057         text and bss sections.  */
03058       BFD_ASSERT (0 == ldinfo.ldsym_count);
03059 
03060       hsym->ldindx = 3;
03061       ldinfo.ldsym_count = 1;
03062       hsym->ldsym = ldsym;
03063 
03064       if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
03065                                     hsym->ldsym, hsym->root.root.string))
03066        return FALSE;
03067 
03068       /* This symbol is written out by xcoff_write_global_symbol
03069         Set stuff up so xcoff_write_global_symbol logic works.  */
03070       hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
03071       hsym->root.type = bfd_link_hash_defined;
03072       hsym->root.u.def.value = 0;
03073     }
03074 
03075   /* Garbage collect unused sections.  */
03076   if (info->relocatable
03077       || ! gc
03078       || hentry == NULL
03079       || (hentry->root.type != bfd_link_hash_defined
03080          && hentry->root.type != bfd_link_hash_defweak))
03081     {
03082       gc = FALSE;
03083       xcoff_hash_table (info)->gc = FALSE;
03084 
03085       /* We still need to call xcoff_mark, in order to set ldrel_count
03086         correctly.  */
03087       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
03088        {
03089          asection *o;
03090 
03091          for (o = sub->sections; o != NULL; o = o->next)
03092            {
03093              if ((o->flags & SEC_MARK) == 0)
03094               {
03095                 if (! xcoff_mark (info, o))
03096                   goto error_return;
03097               }
03098            }
03099        }
03100     }
03101   else
03102     {
03103       if (! xcoff_mark (info, hentry->root.u.def.section))
03104        goto error_return;
03105       xcoff_sweep (info);
03106       xcoff_hash_table (info)->gc = TRUE;
03107     }
03108 
03109   /* Return special sections to the caller.  */
03110   for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
03111     {
03112       sec = xcoff_hash_table (info)->special_sections[i];
03113 
03114       if (sec != NULL
03115          && gc
03116          && (sec->flags & SEC_MARK) == 0)
03117        sec = NULL;
03118 
03119       special_sections[i] = sec;
03120     }
03121 
03122   if (info->input_bfds == NULL)
03123     /* I'm not sure what to do in this bizarre case.  */
03124     return TRUE;
03125 
03126   xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
03127                          (void *) &ldinfo);
03128   if (ldinfo.failed)
03129     goto error_return;
03130 
03131   /* Work out the size of the import file names.  Each import file ID
03132      consists of three null terminated strings: the path, the file
03133      name, and the archive member name.  The first entry in the list
03134      of names is the path to use to find objects, which the linker has
03135      passed in as the libpath argument.  For some reason, the path
03136      entry in the other import file names appears to always be empty.  */
03137   impsize = strlen (libpath) + 3;
03138   impcount = 1;
03139   for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
03140     {
03141       ++impcount;
03142       impsize += (strlen (fl->path)
03143                 + strlen (fl->file)
03144                 + strlen (fl->member)
03145                 + 3);
03146     }
03147 
03148   /* Set up the .loader section header.  */
03149   ldhdr = &xcoff_hash_table (info)->ldhdr;
03150   ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
03151   ldhdr->l_nsyms = ldinfo.ldsym_count;
03152   ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
03153   ldhdr->l_istlen = impsize;
03154   ldhdr->l_nimpid = impcount;
03155   ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
03156                    + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
03157                    + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
03158   ldhdr->l_stlen = ldinfo.string_size;
03159   stoff = ldhdr->l_impoff + impsize;
03160   if (ldinfo.string_size == 0)
03161     ldhdr->l_stoff = 0;
03162   else
03163     ldhdr->l_stoff = stoff;
03164 
03165   /* 64 bit elements to ldhdr
03166      The swap out routine for 32 bit will ignore them.
03167      Nothing fancy, symbols come after the header and relocs come
03168      after symbols.  */
03169   ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
03170   ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
03171                    + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
03172 
03173   /* We now know the final size of the .loader section.  Allocate
03174      space for it.  */
03175   lsec = xcoff_hash_table (info)->loader_section;
03176   lsec->size = stoff + ldhdr->l_stlen;
03177   lsec->contents = bfd_zalloc (output_bfd, lsec->size);
03178   if (lsec->contents == NULL)
03179     goto error_return;
03180 
03181   /* Set up the header.  */
03182   bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
03183 
03184   /* Set up the import file names.  */
03185   out = (char *) lsec->contents + ldhdr->l_impoff;
03186   strcpy (out, libpath);
03187   out += strlen (libpath) + 1;
03188   *out++ = '\0';
03189   *out++ = '\0';
03190   for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
03191     {
03192       const char *s;
03193 
03194       s = fl->path;
03195       while ((*out++ = *s++) != '\0')
03196        ;
03197       s = fl->file;
03198       while ((*out++ = *s++) != '\0')
03199        ;
03200       s = fl->member;
03201       while ((*out++ = *s++) != '\0')
03202        ;
03203     }
03204 
03205   BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
03206 
03207   /* Set up the symbol string table.  */
03208   if (ldinfo.string_size > 0)
03209     {
03210       memcpy (out, ldinfo.strings, ldinfo.string_size);
03211       free (ldinfo.strings);
03212       ldinfo.strings = NULL;
03213     }
03214 
03215   /* We can't set up the symbol table or the relocs yet, because we
03216      don't yet know the final position of the various sections.  The
03217      .loader symbols are written out when the corresponding normal
03218      symbols are written out in xcoff_link_input_bfd or
03219      xcoff_write_global_symbol.  The .loader relocs are written out
03220      when the corresponding normal relocs are handled in
03221      xcoff_link_input_bfd.  */
03222 
03223   /* Allocate space for the magic sections.  */
03224   sec = xcoff_hash_table (info)->linkage_section;
03225   if (sec->size > 0)
03226     {
03227       sec->contents = bfd_zalloc (output_bfd, sec->size);
03228       if (sec->contents == NULL)
03229        goto error_return;
03230     }
03231   sec = xcoff_hash_table (info)->toc_section;
03232   if (sec->size > 0)
03233     {
03234       sec->contents = bfd_zalloc (output_bfd, sec->size);
03235       if (sec->contents == NULL)
03236        goto error_return;
03237     }
03238   sec = xcoff_hash_table (info)->descriptor_section;
03239   if (sec->size > 0)
03240     {
03241       sec->contents = bfd_zalloc (output_bfd, sec->size);
03242       if (sec->contents == NULL)
03243        goto error_return;
03244     }
03245 
03246   /* Now that we've done garbage collection, figure out the contents
03247      of the .debug section.  */
03248   debug_strtab = xcoff_hash_table (info)->debug_strtab;
03249 
03250   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
03251     {
03252       asection *subdeb;
03253       bfd_size_type symcount;
03254       unsigned long *debug_index;
03255       asection **csectpp;
03256       bfd_byte *esym, *esymend;
03257       bfd_size_type symesz;
03258 
03259       if (sub->xvec != info->hash->creator)
03260        continue;
03261       subdeb = bfd_get_section_by_name (sub, ".debug");
03262       if (subdeb == NULL || subdeb->size == 0)
03263        continue;
03264 
03265       if (info->strip == strip_all
03266          || info->strip == strip_debugger
03267          || info->discard == discard_all)
03268        {
03269          subdeb->size = 0;
03270          continue;
03271        }
03272 
03273       if (! _bfd_coff_get_external_symbols (sub))
03274        goto error_return;
03275 
03276       symcount = obj_raw_syment_count (sub);
03277       debug_index = bfd_zalloc (sub, symcount * sizeof (unsigned long));
03278       if (debug_index == NULL)
03279        goto error_return;
03280       xcoff_data (sub)->debug_indices = debug_index;
03281 
03282       /* Grab the contents of the .debug section.  We use malloc and
03283         copy the names into the debug stringtab, rather than
03284         bfd_alloc, because I expect that, when linking many files
03285         together, many of the strings will be the same.  Storing the
03286         strings in the hash table should save space in this case.  */
03287       if (! bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
03288        goto error_return;
03289 
03290       csectpp = xcoff_data (sub)->csects;
03291 
03292       /* Dynamic object do not have csectpp's.  */
03293       if (NULL != csectpp)
03294        {
03295          symesz = bfd_coff_symesz (sub);
03296          esym = (bfd_byte *) obj_coff_external_syms (sub);
03297          esymend = esym + symcount * symesz;
03298 
03299          while (esym < esymend)
03300            {
03301              struct internal_syment sym;
03302 
03303              bfd_coff_swap_sym_in (sub, (void *) esym, (void *) &sym);
03304 
03305              *debug_index = (unsigned long) -1;
03306 
03307              if (sym._n._n_n._n_zeroes == 0
03308                 && *csectpp != NULL
03309                 && (! gc
03310                     || ((*csectpp)->flags & SEC_MARK) != 0
03311                     || *csectpp == bfd_abs_section_ptr)
03312                 && bfd_coff_symname_in_debug (sub, &sym))
03313               {
03314                 char *name;
03315                 bfd_size_type indx;
03316 
03317                 name = (char *) debug_contents + sym._n._n_n._n_offset;
03318                 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
03319                 if (indx == (bfd_size_type) -1)
03320                   goto error_return;
03321                 *debug_index = indx;
03322               }
03323 
03324              esym += (sym.n_numaux + 1) * symesz;
03325              csectpp += sym.n_numaux + 1;
03326              debug_index += sym.n_numaux + 1;
03327            }
03328        }
03329 
03330       free (debug_contents);
03331       debug_contents = NULL;
03332 
03333       /* Clear the size of subdeb, so that it is not included directly
03334         in the output file.  */
03335       subdeb->size = 0;
03336 
03337       if (! info->keep_memory)
03338        {
03339          if (! _bfd_coff_free_symbols (sub))
03340            goto error_return;
03341        }
03342     }
03343 
03344   if (info->strip != strip_all)
03345     xcoff_hash_table (info)->debug_section->size =
03346       _bfd_stringtab_size (debug_strtab);
03347 
03348   return TRUE;
03349 
03350  error_return:
03351   if (ldinfo.strings != NULL)
03352     free (ldinfo.strings);
03353   if (debug_contents != NULL)
03354     free (debug_contents);
03355   return FALSE;
03356 }
03357 
03358 bfd_boolean
03359 bfd_xcoff_link_generate_rtinit (bfd *abfd,
03360                             const char *init,
03361                             const char *fini,
03362                             bfd_boolean rtld)
03363 {
03364   struct bfd_in_memory *bim;
03365 
03366   bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
03367   if (bim == NULL)
03368     return FALSE;
03369 
03370   bim->size = 0;
03371   bim->buffer = 0;
03372 
03373   abfd->link_next = 0;
03374   abfd->format = bfd_object;
03375   abfd->iostream = (void *) bim;
03376   abfd->flags = BFD_IN_MEMORY;
03377   abfd->direction = write_direction;
03378   abfd->where = 0;
03379 
03380   if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
03381     return FALSE;
03382 
03383   /* need to reset to unknown or it will not be read back in correctly */
03384   abfd->format = bfd_unknown;
03385   abfd->direction = read_direction;
03386   abfd->where = 0;
03387 
03388   return TRUE;
03389 }
03390 
03391 /* Link an input file into the linker output file.  This function
03392    handles all the sections and relocations of the input file at once.  */
03393 
03394 static bfd_boolean
03395 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
03396                     bfd *input_bfd)
03397 {
03398   bfd *output_bfd;
03399   const char *strings;
03400   bfd_size_type syment_base;
03401   unsigned int n_tmask;
03402   unsigned int n_btshft;
03403   bfd_boolean copy, hash;
03404   bfd_size_type isymesz;
03405   bfd_size_type osymesz;
03406   bfd_size_type linesz;
03407   bfd_byte *esym;
03408   bfd_byte *esym_end;
03409   struct xcoff_link_hash_entry **sym_hash;
03410   struct internal_syment *isymp;
03411   asection **csectpp;
03412   unsigned long *debug_index;
03413   long *indexp;
03414   unsigned long output_index;
03415   bfd_byte *outsym;
03416   unsigned int incls;
03417   asection *oline;
03418   bfd_boolean keep_syms;
03419   asection *o;
03420 
03421   /* We can just skip DYNAMIC files, unless this is a static link.  */
03422   if ((input_bfd->flags & DYNAMIC) != 0
03423       && ! finfo->info->static_link)
03424     return TRUE;
03425 
03426   /* Move all the symbols to the output file.  */
03427   output_bfd = finfo->output_bfd;
03428   strings = NULL;
03429   syment_base = obj_raw_syment_count (output_bfd);
03430   isymesz = bfd_coff_symesz (input_bfd);
03431   osymesz = bfd_coff_symesz (output_bfd);
03432   linesz = bfd_coff_linesz (input_bfd);
03433   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
03434 
03435   n_tmask = coff_data (input_bfd)->local_n_tmask;
03436   n_btshft = coff_data (input_bfd)->local_n_btshft;
03437 
03438   /* Define macros so that ISFCN, et. al., macros work correctly.  */
03439 #define N_TMASK n_tmask
03440 #define N_BTSHFT n_btshft
03441 
03442   copy = FALSE;
03443   if (! finfo->info->keep_memory)
03444     copy = TRUE;
03445   hash = TRUE;
03446   if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
03447     hash = FALSE;
03448 
03449   if (! _bfd_coff_get_external_symbols (input_bfd))
03450     return FALSE;
03451 
03452   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
03453   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
03454   sym_hash = obj_xcoff_sym_hashes (input_bfd);
03455   csectpp = xcoff_data (input_bfd)->csects;
03456   debug_index = xcoff_data (input_bfd)->debug_indices;
03457   isymp = finfo->internal_syms;
03458   indexp = finfo->sym_indices;
03459   output_index = syment_base;
03460   outsym = finfo->outsyms;
03461   incls = 0;
03462   oline = NULL;
03463 
03464   while (esym < esym_end)
03465     {
03466       struct internal_syment isym;
03467       union internal_auxent aux;
03468       int smtyp = 0;
03469       bfd_boolean skip;
03470       bfd_boolean require;
03471       int add;
03472 
03473       bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
03474 
03475       /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
03476         information.  */
03477       if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
03478        {
03479          BFD_ASSERT (isymp->n_numaux > 0);
03480          bfd_coff_swap_aux_in (input_bfd,
03481                             (void *) (esym + isymesz * isymp->n_numaux),
03482                             isymp->n_type, isymp->n_sclass,
03483                             isymp->n_numaux - 1, isymp->n_numaux,
03484                             (void *) &aux);
03485 
03486          smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
03487        }
03488 
03489       /* Make a copy of *isymp so that the relocate_section function
03490         always sees the original values.  This is more reliable than
03491         always recomputing the symbol value even if we are stripping
03492         the symbol.  */
03493       isym = *isymp;
03494 
03495       /* If this symbol is in the .loader section, swap out the
03496         .loader symbol information.  If this is an external symbol
03497         reference to a defined symbol, though, then wait until we get
03498         to the definition.  */
03499       if (isym.n_sclass == C_EXT
03500          && *sym_hash != NULL
03501          && (*sym_hash)->ldsym != NULL
03502          && (smtyp != XTY_ER
03503              || (*sym_hash)->root.type == bfd_link_hash_undefined))
03504        {
03505          struct xcoff_link_hash_entry *h;
03506          struct internal_ldsym *ldsym;
03507 
03508          h = *sym_hash;
03509          ldsym = h->ldsym;
03510          if (isym.n_scnum > 0)
03511            {
03512              ldsym->l_scnum = (*csectpp)->output_section->target_index;
03513              ldsym->l_value = (isym.n_value
03514                             + (*csectpp)->output_section->vma
03515                             + (*csectpp)->output_offset
03516                             - (*csectpp)->vma);
03517            }
03518          else
03519            {
03520              ldsym->l_scnum = isym.n_scnum;
03521              ldsym->l_value = isym.n_value;
03522            }
03523 
03524          ldsym->l_smtype = smtyp;
03525          if (((h->flags & XCOFF_DEF_REGULAR) == 0
03526               && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
03527              || (h->flags & XCOFF_IMPORT) != 0)
03528            ldsym->l_smtype |= L_IMPORT;
03529          if (((h->flags & XCOFF_DEF_REGULAR) != 0
03530               && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
03531              || (h->flags & XCOFF_EXPORT) != 0)
03532            ldsym->l_smtype |= L_EXPORT;
03533          if ((h->flags & XCOFF_ENTRY) != 0)
03534            ldsym->l_smtype |= L_ENTRY;
03535 
03536          ldsym->l_smclas = aux.x_csect.x_smclas;
03537 
03538          if (ldsym->l_ifile == (bfd_size_type) -1)
03539            ldsym->l_ifile = 0;
03540          else if (ldsym->l_ifile == 0)
03541            {
03542              if ((ldsym->l_smtype & L_IMPORT) == 0)
03543               ldsym->l_ifile = 0;
03544              else
03545               {
03546                 bfd *impbfd;
03547 
03548                 if (h->root.type == bfd_link_hash_defined
03549                     || h->root.type == bfd_link_hash_defweak)
03550                   impbfd = h->root.u.def.section->owner;
03551                 else if (h->root.type == bfd_link_hash_undefined
03552                         || h->root.type == bfd_link_hash_undefweak)
03553                   impbfd = h->root.u.undef.abfd;
03554                 else
03555                   impbfd = NULL;
03556 
03557                 if (impbfd == NULL)
03558                   ldsym->l_ifile = 0;
03559                 else
03560                   {
03561                     BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
03562                     ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
03563                   }
03564               }
03565            }
03566 
03567          ldsym->l_parm = 0;
03568 
03569          BFD_ASSERT (h->ldindx >= 0);
03570          bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
03571                                 (finfo->ldsym
03572                                  + ((h->ldindx - 3)
03573                                    * bfd_xcoff_ldsymsz (finfo->output_bfd))));
03574          h->ldsym = NULL;
03575 
03576          /* Fill in snentry now that we know the target_index.  */
03577          if ((h->flags & XCOFF_ENTRY) != 0
03578              && (h->root.type == bfd_link_hash_defined
03579                 || h->root.type == bfd_link_hash_defweak))
03580            {
03581              xcoff_data (output_bfd)->snentry =
03582               h->root.u.def.section->output_section->target_index;
03583            }
03584        }
03585 
03586       *indexp = -1;
03587 
03588       skip = FALSE;
03589       require = FALSE;
03590       add = 1 + isym.n_numaux;
03591 
03592       /* If we are skipping this csect, we want to skip this symbol.  */
03593       if (*csectpp == NULL)
03594        skip = TRUE;
03595 
03596       /* If we garbage collected this csect, we want to skip this
03597         symbol.  */
03598       if (! skip
03599          && xcoff_hash_table (finfo->info)->gc
03600          && ((*csectpp)->flags & SEC_MARK) == 0
03601          && *csectpp != bfd_abs_section_ptr)
03602        skip = TRUE;
03603 
03604       /* An XCOFF linker always skips C_STAT symbols.  */
03605       if (! skip
03606          && isymp->n_sclass == C_STAT)
03607        skip = TRUE;
03608 
03609       /* We skip all but the first TOC anchor.  */
03610       if (! skip
03611          && isymp->n_sclass == C_HIDEXT
03612          && aux.x_csect.x_smclas == XMC_TC0)
03613        {
03614          if (finfo->toc_symindx != -1)
03615            skip = TRUE;
03616          else
03617            {
03618              bfd_vma tocval, tocend;
03619              bfd *inp;
03620 
03621              tocval = ((*csectpp)->output_section->vma
03622                      + (*csectpp)->output_offset
03623                      + isym.n_value
03624                      - (*csectpp)->vma);
03625 
03626              /* We want to find out if tocval is a good value to use
03627                as the TOC anchor--that is, whether we can access all
03628                of the TOC using a 16 bit offset from tocval.  This
03629                test assumes that the TOC comes at the end of the
03630                output section, as it does in the default linker
03631                script.  */
03632              tocend = ((*csectpp)->output_section->vma
03633                      + (*csectpp)->output_section->size);
03634              for (inp = finfo->info->input_bfds;
03635                  inp != NULL;
03636                  inp = inp->link_next)
03637               {
03638 
03639                 for (o = inp->sections; o != NULL; o = o->next)
03640                   if (strcmp (o->name, ".tocbss") == 0)
03641                     {
03642                      bfd_vma new_toc_end;
03643                      new_toc_end = (o->output_section->vma
03644                                    + o->output_offset
03645                                    + o->size);
03646                      if (new_toc_end > tocend)
03647                        tocend = new_toc_end;
03648                     }
03649 
03650               }
03651 
03652              if (tocval + 0x10000 < tocend)
03653               {
03654                 (*_bfd_error_handler)
03655                   (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
03656                    (unsigned long) (tocend - tocval));
03657                 bfd_set_error (bfd_error_file_too_big);
03658                 return FALSE;
03659               }
03660 
03661              if (tocval + 0x8000 < tocend)
03662               {
03663                 bfd_vma tocadd;
03664 
03665                 tocadd = tocend - (tocval + 0x8000);
03666                 tocval += tocadd;
03667                 isym.n_value += tocadd;
03668               }
03669 
03670              finfo->toc_symindx = output_index;
03671              xcoff_data (finfo->output_bfd)->toc = tocval;
03672              xcoff_data (finfo->output_bfd)->sntoc =
03673               (*csectpp)->output_section->target_index;
03674              require = TRUE;
03675 
03676            }
03677        }
03678 
03679       /* If we are stripping all symbols, we want to skip this one.  */
03680       if (! skip
03681          && finfo->info->strip == strip_all)
03682        skip = TRUE;
03683 
03684       /* We can skip resolved external references.  */
03685       if (! skip
03686          && isym.n_sclass == C_EXT
03687          && smtyp == XTY_ER
03688          && (*sym_hash)->root.type != bfd_link_hash_undefined)
03689        skip = TRUE;
03690 
03691       /* We can skip common symbols if they got defined somewhere
03692         else.  */
03693       if (! skip
03694          && isym.n_sclass == C_EXT
03695          && smtyp == XTY_CM
03696          && ((*sym_hash)->root.type != bfd_link_hash_common
03697              || (*sym_hash)->root.u.c.p->section != *csectpp)
03698          && ((*sym_hash)->root.type != bfd_link_hash_defined
03699              || (*sym_hash)->root.u.def.section != *csectpp))
03700        skip = TRUE;
03701 
03702       /* Skip local symbols if we are discarding them.  */
03703       if (! skip
03704          && finfo->info->discard == discard_all
03705          && isym.n_sclass != C_EXT
03706          && (isym.n_sclass != C_HIDEXT
03707              || smtyp != XTY_SD))
03708        skip = TRUE;
03709 
03710       /* If we stripping debugging symbols, and this is a debugging
03711         symbol, then skip it.  */
03712       if (! skip
03713          && finfo->info->strip == strip_debugger
03714          && isym.n_scnum == N_DEBUG)
03715        skip = TRUE;
03716 
03717       /* If some symbols are stripped based on the name, work out the
03718         name and decide whether to skip this symbol.  We don't handle
03719         this correctly for symbols whose names are in the .debug
03720         section; to get it right we would need a new bfd_strtab_hash
03721         function to return the string given the index.  */
03722       if (! skip
03723          && (finfo->info->strip == strip_some
03724              || finfo->info->discard == discard_l)
03725          && (debug_index == NULL || *debug_index == (unsigned long) -1))
03726        {
03727          const char *name;
03728          char buf[SYMNMLEN + 1];
03729 
03730          name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
03731 
03732          if (name == NULL)
03733            return FALSE;
03734 
03735          if ((finfo->info->strip == strip_some
03736               && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
03737                                 FALSE) == NULL))
03738              || (finfo->info->discard == discard_l
03739                 && (isym.n_sclass != C_EXT
03740                     && (isym.n_sclass != C_HIDEXT
03741                        || smtyp != XTY_SD))
03742                 && bfd_is_local_label_name (input_bfd, name)))
03743            skip = TRUE;
03744        }
03745 
03746       /* We can not skip the first TOC anchor.  */
03747       if (skip
03748          && require
03749          && finfo->info->strip != strip_all)
03750        skip = FALSE;
03751 
03752       /* We now know whether we are to skip this symbol or not.  */
03753       if (! skip)
03754        {
03755          /* Adjust the symbol in order to output it.  */
03756 
03757          if (isym._n._n_n._n_zeroes == 0
03758              && isym._n._n_n._n_offset != 0)
03759            {
03760              /* This symbol has a long name.  Enter it in the string
03761                table we are building.  If *debug_index != -1, the
03762                name has already been entered in the .debug section.  */
03763              if (debug_index != NULL && *debug_index != (unsigned long) -1)
03764               isym._n._n_n._n_offset = *debug_index;
03765              else
03766               {
03767                 const char *name;
03768                 bfd_size_type indx;
03769 
03770                 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
03771 
03772                 if (name == NULL)
03773                   return FALSE;
03774                 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
03775                 if (indx == (bfd_size_type) -1)
03776                   return FALSE;
03777                 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
03778               }
03779            }
03780 
03781          if (isym.n_sclass != C_BSTAT
03782              && isym.n_sclass != C_ESTAT
03783              && isym.n_sclass != C_DECL
03784              && isym.n_scnum > 0)
03785            {
03786              isym.n_scnum = (*csectpp)->output_section->target_index;
03787              isym.n_value += ((*csectpp)->output_section->vma
03788                             + (*csectpp)->output_offset
03789                             - (*csectpp)->vma);
03790            }
03791 
03792          /* The value of a C_FILE symbol is the symbol index of the
03793             next C_FILE symbol.  The value of the last C_FILE symbol
03794             is -1.  We try to get this right, below, just before we
03795             write the symbols out, but in the general case we may
03796             have to write the symbol out twice.  */
03797          if (isym.n_sclass == C_FILE)
03798            {
03799              if (finfo->last_file_index != -1
03800                 && finfo->last_file.n_value != (bfd_vma) output_index)
03801               {
03802                 /* We must correct the value of the last C_FILE entry.  */
03803                 finfo->last_file.n_value = output_index;
03804                 if ((bfd_size_type) finfo->last_file_index >= syment_base)
03805                   {
03806                     /* The last C_FILE symbol is in this input file.  */
03807                     bfd_coff_swap_sym_out (output_bfd,
03808                                         (void *) &finfo->last_file,
03809                                         (void *) (finfo->outsyms
03810                                               + ((finfo->last_file_index
03811                                                  - syment_base)
03812                                                  * osymesz)));
03813                   }
03814                 else
03815                   {
03816                     /* We have already written out the last C_FILE
03817                       symbol.  We need to write it out again.  We
03818                       borrow *outsym temporarily.  */
03819                     file_ptr pos;
03820 
03821                     bfd_coff_swap_sym_out (output_bfd,
03822                                         (void *) &finfo->last_file,
03823                                         (void *) outsym);
03824 
03825                     pos = obj_sym_filepos (output_bfd);
03826                     pos += finfo->last_file_index * osymesz;
03827                     if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
03828                        || (bfd_bwrite (outsym, osymesz, output_bfd)
03829                            != osymesz))
03830                      return FALSE;
03831                   }
03832               }
03833 
03834              finfo->last_file_index = output_index;
03835              finfo->last_file = isym;
03836            }
03837 
03838          /* The value of a C_BINCL or C_EINCL symbol is a file offset
03839             into the line numbers.  We update the symbol values when
03840             we handle the line numbers.  */
03841          if (isym.n_sclass == C_BINCL
03842              || isym.n_sclass == C_EINCL)
03843            {
03844              isym.n_value = finfo->line_filepos;
03845              ++incls;
03846            }
03847 
03848          /* Output the symbol.  */
03849 
03850          bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
03851 
03852          *indexp = output_index;
03853 
03854          if (isym.n_sclass == C_EXT)
03855            {
03856              long indx;
03857              struct xcoff_link_hash_entry *h;
03858 
03859              indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
03860                     / isymesz);
03861              h = obj_xcoff_sym_hashes (input_bfd)[indx];
03862              BFD_ASSERT (h != NULL);
03863              h->indx = output_index;
03864            }
03865 
03866          /* If this is a symbol in the TOC which we may have merged
03867             (class XMC_TC), remember the symbol index of the TOC
03868             symbol.  */
03869          if (isym.n_sclass == C_HIDEXT
03870              && aux.x_csect.x_smclas == XMC_TC
03871              && *sym_hash != NULL)
03872            {
03873              BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
03874              BFD_ASSERT ((*sym_hash)->toc_section != NULL);
03875              (*sym_hash)->u.toc_indx = output_index;
03876            }
03877 
03878          output_index += add;
03879          outsym += add * osymesz;
03880        }
03881 
03882       esym += add * isymesz;
03883       isymp += add;
03884       csectpp += add;
03885       sym_hash += add;
03886       if (debug_index != NULL)
03887        debug_index += add;
03888       ++indexp;
03889       for (--add; add > 0; --add)
03890        *indexp++ = -1;
03891     }
03892 
03893   /* Fix up the aux entries and the C_BSTAT symbols.  This must be
03894      done in a separate pass, because we don't know the correct symbol
03895      indices until we have already decided which symbols we are going
03896      to keep.  */
03897 
03898   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
03899   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
03900   isymp = finfo->internal_syms;
03901   indexp = finfo->sym_indices;
03902   csectpp = xcoff_data (input_bfd)->csects;
03903   outsym = finfo->outsyms;
03904   while (esym < esym_end)
03905     {
03906       int add;
03907 
03908       add = 1 + isymp->n_numaux;
03909 
03910       if (*indexp < 0)
03911        esym += add * isymesz;
03912       else
03913        {
03914          int i;
03915 
03916          if (isymp->n_sclass == C_BSTAT)
03917            {
03918              struct internal_syment isym;
03919 
03920              bfd_vma indx;
03921 
03922              /* The value of a C_BSTAT symbol is the symbol table
03923                index of the containing csect.  */
03924              bfd_coff_swap_sym_in (output_bfd, (void *) outsym, (void *) &isym);
03925              indx = isym.n_value;
03926              if (indx < obj_raw_syment_count (input_bfd))
03927               {
03928                 long symindx;
03929 
03930                 symindx = finfo->sym_indices[indx];
03931                 if (symindx < 0)
03932                   isym.n_value = 0;
03933                 else
03934                   isym.n_value = symindx;
03935                 bfd_coff_swap_sym_out (output_bfd, (void *) &isym,
03936                                     (void *) outsym);
03937               }
03938            }
03939 
03940          esym += isymesz;
03941          outsym += osymesz;
03942 
03943          for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
03944            {
03945              union internal_auxent aux;
03946 
03947              bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
03948                                 isymp->n_sclass, i, isymp->n_numaux,
03949                                 (void *) &aux);
03950 
03951              if (isymp->n_sclass == C_FILE)
03952               {
03953                 /* This is the file name (or some comment put in by
03954                    the compiler).  If it is long, we must put it in
03955                    the string table.  */
03956                 if (aux.x_file.x_n.x_zeroes == 0
03957                     && aux.x_file.x_n.x_offset != 0)
03958                   {
03959                     const char *filename;
03960                     bfd_size_type indx;
03961 
03962                     BFD_ASSERT (aux.x_file.x_n.x_offset
03963                               >= STRING_SIZE_SIZE);
03964                     if (strings == NULL)
03965                      {
03966                        strings = _bfd_coff_read_string_table (input_bfd);
03967                        if (strings == NULL)
03968                          return FALSE;
03969                      }
03970                     filename = strings + aux.x_file.x_n.x_offset;
03971                     indx = _bfd_stringtab_add (finfo->strtab, filename,
03972                                            hash, copy);
03973                     if (indx == (bfd_size_type) -1)
03974                      return FALSE;
03975                     aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
03976                   }
03977               }
03978              else if ((isymp->n_sclass == C_EXT
03979                      || isymp->n_sclass == C_HIDEXT)
03980                      && i + 1 == isymp->n_numaux)
03981               {
03982 
03983                 /* We don't support type checking.  I don't know if
03984                    anybody does.  */
03985                 aux.x_csect.x_parmhash = 0;
03986                 /* I don't think anybody uses these fields, but we'd
03987                    better clobber them just in case.  */
03988                 aux.x_csect.x_stab = 0;
03989                 aux.x_csect.x_snstab = 0;
03990 
03991                 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
03992                   {
03993                     unsigned long indx;
03994 
03995                     indx = aux.x_csect.x_scnlen.l;
03996                     if (indx < obj_raw_syment_count (input_bfd))
03997                      {
03998                        long symindx;
03999 
04000                        symindx = finfo->sym_indices[indx];
04001                        if (symindx < 0)
04002                          {
04003                            aux.x_csect.x_scnlen.l = 0;
04004                          }
04005                        else
04006                          {
04007                            aux.x_csect.x_scnlen.l = symindx;
04008                          }
04009                      }
04010                   }
04011               }
04012              else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
04013               {
04014                 unsigned long indx;
04015 
04016                 if (ISFCN (isymp->n_type)
04017                     || ISTAG (isymp->n_sclass)
04018                     || isymp->n_sclass == C_BLOCK
04019                     || isymp->n_sclass == C_FCN)
04020                   {
04021                     indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
04022                     if (indx > 0
04023                        && indx < obj_raw_syment_count (input_bfd))
04024                      {
04025                        /* We look forward through the symbol for
04026                           the index of the next symbol we are going
04027                           to include.  I don't know if this is
04028                           entirely right.  */
04029                        while (finfo->sym_indices[indx] < 0
04030                              && indx < obj_raw_syment_count (input_bfd))
04031                          ++indx;
04032                        if (indx >= obj_raw_syment_count (input_bfd))
04033                          indx = output_index;
04034                        else
04035                          indx = finfo->sym_indices[indx];
04036                        aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
04037 
04038                      }
04039                   }
04040 
04041                 indx = aux.x_sym.x_tagndx.l;
04042                 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
04043                   {
04044                     long symindx;
04045 
04046                     symindx = finfo->sym_indices[indx];
04047                     if (symindx < 0)
04048                      aux.x_sym.x_tagndx.l = 0;
04049                     else
04050                      aux.x_sym.x_tagndx.l = symindx;
04051                   }
04052 
04053               }
04054 
04055              /* Copy over the line numbers, unless we are stripping
04056                them.  We do this on a symbol by symbol basis in
04057                order to more easily handle garbage collection.  */
04058              if ((isymp->n_sclass == C_EXT
04059                  || isymp->n_sclass == C_HIDEXT)
04060                 && i == 0
04061                 && isymp->n_numaux > 1
04062                 && ISFCN (isymp->n_type)
04063                 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
04064               {
04065                 if (finfo->info->strip != strip_none
04066                     && finfo->info->strip != strip_some)
04067                   aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
04068                 else
04069                   {
04070                     asection *enclosing;
04071                     unsigned int enc_count;
04072                     bfd_signed_vma linoff;
04073                     struct internal_lineno lin;
04074 
04075                     o = *csectpp;
04076                     enclosing = xcoff_section_data (abfd, o)->enclosing;
04077                     enc_count = xcoff_section_data (abfd, o)->lineno_count;
04078                     if (oline != enclosing)
04079                      {
04080                        file_ptr pos = enclosing->line_filepos;
04081                        bfd_size_type amt = linesz * enc_count;
04082                        if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
04083                            || (bfd_bread (finfo->linenos, amt, input_bfd)
04084                               != amt))
04085                          return FALSE;
04086                        oline = enclosing;
04087                      }
04088 
04089                     linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
04090                             - enclosing->line_filepos);
04091 
04092                     bfd_coff_swap_lineno_in (input_bfd,
04093                                           (void *) (finfo->linenos + linoff),
04094                                           (void *) &lin);
04095                     if (lin.l_lnno != 0
04096                        || ((bfd_size_type) lin.l_addr.l_symndx
04097                            != ((esym
04098                                - isymesz
04099                                - ((bfd_byte *)
04100                                   obj_coff_external_syms (input_bfd)))
04101                               / isymesz)))
04102                      aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
04103                     else
04104                      {
04105                        bfd_byte *linpend, *linp;
04106                        bfd_vma offset;
04107                        bfd_size_type count;
04108 
04109                        lin.l_addr.l_symndx = *indexp;
04110                        bfd_coff_swap_lineno_out (output_bfd, (void *) &lin,
04111                                               (void *) (finfo->linenos
04112                                                     + linoff));
04113 
04114                        linpend = (finfo->linenos
04115                                  + enc_count * linesz);
04116                        offset = (o->output_section->vma
04117                                 + o->output_offset
04118                                 - o->vma);
04119                        for (linp = finfo->linenos + linoff + linesz;
04120                             linp < linpend;
04121                             linp += linesz)
04122                          {
04123                            bfd_coff_swap_lineno_in (input_bfd, (void *) linp,
04124                                                  (void *) &lin);
04125                            if (lin.l_lnno == 0)
04126                             break;
04127                            lin.l_addr.l_paddr += offset;
04128                            bfd_coff_swap_lineno_out (output_bfd,
04129                                                  (void *) &lin,
04130                                                  (void *) linp);
04131                          }
04132 
04133                        count = (linp - (finfo->linenos + linoff)) / linesz;
04134 
04135                        aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
04136                          (o->output_section->line_filepos
04137                           + o->output_section->lineno_count * linesz);
04138 
04139                        if (bfd_seek (output_bfd,
04140                                    aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
04141                                    SEEK_SET) != 0
04142                            || (bfd_bwrite (finfo->linenos + linoff,
04143                                         linesz * count, output_bfd)
04144                               != linesz * count))
04145                          return FALSE;
04146 
04147                        o->output_section->lineno_count += count;
04148 
04149                        if (incls > 0)
04150                          {
04151                            struct internal_syment *iisp, *iispend;
04152                            long *iindp;
04153                            bfd_byte *oos;
04154                            int iiadd;
04155 
04156                            /* Update any C_BINCL or C_EINCL symbols
04157                              that refer to a line number in the
04158                              range we just output.  */
04159                            iisp = finfo->internal_syms;
04160                            iispend = (iisp
04161                                     + obj_raw_syment_count (input_bfd));
04162                            iindp = finfo->sym_indices;
04163                            oos = finfo->outsyms;
04164                            while (iisp < iispend)
04165                             {
04166                               if (*iindp >= 0
04167                                   && (iisp->n_sclass == C_BINCL
04168                                      || iisp->n_sclass == C_EINCL)
04169                                   && ((bfd_size_type) iisp->n_value
04170                                      >= (bfd_size_type)(enclosing->line_filepos + linoff))
04171                                   && ((bfd_size_type) iisp->n_value
04172                                      < (enclosing->line_filepos
04173                                         + enc_count * linesz)))
04174                                 {
04175                                   struct internal_syment iis;
04176 
04177                                   bfd_coff_swap_sym_in (output_bfd,
04178                                                      (void *) oos,
04179                                                      (void *) &iis);
04180                                   iis.n_value =
04181                                    (iisp->n_value
04182                                     - enclosing->line_filepos
04183                                     - linoff
04184                                     + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
04185                                   bfd_coff_swap_sym_out (output_bfd,
04186                                                       (void *) &iis,
04187                                                       (void *) oos);
04188                                   --incls;
04189                                 }
04190 
04191                               iiadd = 1 + iisp->n_numaux;
04192                               if (*iindp >= 0)
04193                                 oos += iiadd * osymesz;
04194                               iisp += iiadd;
04195                               iindp += iiadd;
04196                             }
04197                          }
04198                      }
04199                   }
04200               }
04201 
04202              bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
04203                                  isymp->n_sclass, i, isymp->n_numaux,
04204                                  (void *) outsym);
04205              outsym += osymesz;
04206              esym += isymesz;
04207            }
04208        }
04209 
04210       indexp += add;
04211       isymp += add;
04212       csectpp += add;
04213     }
04214 
04215   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
04216      symbol will be the first symbol in the next input file.  In the
04217      normal case, this will save us from writing out the C_FILE symbol
04218      again.  */
04219   if (finfo->last_file_index != -1
04220       && (bfd_size_type) finfo->last_file_index >= syment_base)
04221     {
04222       finfo->last_file.n_value = output_index;
04223       bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
04224                           (void *) (finfo->outsyms
04225                                 + ((finfo->last_file_index - syment_base)
04226                                    * osymesz)));
04227     }
04228 
04229   /* Write the modified symbols to the output file.  */
04230   if (outsym > finfo->outsyms)
04231     {
04232       file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
04233       bfd_size_type amt = outsym - finfo->outsyms;
04234       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
04235          || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
04236        return FALSE;
04237 
04238       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
04239                  + (outsym - finfo->outsyms) / osymesz)
04240                 == output_index);
04241 
04242       obj_raw_syment_count (output_bfd) = output_index;
04243     }
04244 
04245   /* Don't let the linker relocation routines discard the symbols.  */
04246   keep_syms = obj_coff_keep_syms (input_bfd);
04247   obj_coff_keep_syms (input_bfd) = TRUE;
04248 
04249   /* Relocate the contents of each section.  */
04250   for (o = input_bfd->sections; o != NULL; o = o->next)
04251     {
04252       bfd_byte *contents;
04253 
04254       if (! o->linker_mark)
04255        /* This section was omitted from the link.  */
04256        continue;
04257 
04258       if ((o->flags & SEC_HAS_CONTENTS) == 0
04259          || o->size == 0
04260          || (o->flags & SEC_IN_MEMORY) != 0)
04261        continue;
04262 
04263       /* We have set filepos correctly for the sections we created to
04264         represent csects, so bfd_get_section_contents should work.  */
04265       if (coff_section_data (input_bfd, o) != NULL
04266          && coff_section_data (input_bfd, o)->contents != NULL)
04267        contents = coff_section_data (input_bfd, o)->contents;
04268       else
04269        {
04270          bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
04271          if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
04272            return FALSE;
04273          contents = finfo->contents;
04274        }
04275 
04276       if ((o->flags & SEC_RELOC) != 0)
04277        {
04278          int target_index;
04279          struct internal_reloc *internal_relocs;
04280          struct internal_reloc *irel;
04281          bfd_vma offset;
04282          struct internal_reloc *irelend;
04283          struct xcoff_link_hash_entry **rel_hash;
04284          long r_symndx;
04285 
04286          /* Read in the relocs.  */
04287          target_index = o->output_section->target_index;
04288          internal_relocs = (xcoff_read_internal_relocs
04289                           (input_bfd, o, FALSE, finfo->external_relocs,
04290                            TRUE,
04291                            (finfo->section_info[target_index].relocs
04292                             + o->output_section->reloc_count)));
04293          if (internal_relocs == NULL)
04294            return FALSE;
04295 
04296          /* Call processor specific code to relocate the section
04297             contents.  */
04298          if (! bfd_coff_relocate_section (output_bfd, finfo->info,
04299                                       input_bfd, o,
04300                                       contents,
04301                                       internal_relocs,
04302                                       finfo->internal_syms,
04303                                       xcoff_data (input_bfd)->csects))
04304            return FALSE;
04305 
04306          offset = o->output_section->vma + o->output_offset - o->vma;
04307          irel = internal_relocs;
04308          irelend = irel + o->reloc_count;
04309          rel_hash = (finfo->section_info[target_index].rel_hashes
04310                     + o->output_section->reloc_count);
04311          for (; irel < irelend; irel++, rel_hash++)
04312            {
04313              struct xcoff_link_hash_entry *h = NULL;
04314              struct internal_ldrel ldrel;
04315              bfd_boolean quiet;
04316 
04317              *rel_hash = NULL;
04318 
04319              /* Adjust the reloc address and symbol index.  */
04320 
04321              irel->r_vaddr += offset;
04322 
04323              r_symndx = irel->r_symndx;
04324 
04325              if (r_symndx == -1)
04326               h = NULL;
04327              else
04328               h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
04329 
04330              if (r_symndx != -1 && finfo->info->strip != strip_all)
04331               {
04332                 if (h != NULL
04333                     && h->smclas != XMC_TD
04334                     && (irel->r_type == R_TOC
04335                        || irel->r_type == R_GL
04336                        || irel->r_type == R_TCL
04337                        || irel->r_type == R_TRL
04338                        || irel->r_type == R_TRLA))
04339                   {
04340                     /* This is a TOC relative reloc with a symbol
04341                       attached.  The symbol should be the one which
04342                       this reloc is for.  We want to make this
04343                       reloc against the TOC address of the symbol,
04344                       not the symbol itself.  */
04345                     BFD_ASSERT (h->toc_section != NULL);
04346                     BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
04347                     if (h->u.toc_indx != -1)
04348                      irel->r_symndx = h->u.toc_indx;
04349                     else
04350                      {
04351                        struct xcoff_toc_rel_hash *n;
04352                        struct xcoff_link_section_info *si;
04353                        bfd_size_type amt;
04354 
04355                        amt = sizeof (* n);
04356                        n = bfd_alloc (finfo->output_bfd, amt);
04357                        if (n == NULL)
04358                          return FALSE;
04359                        si = finfo->section_info + target_index;
04360                        n->next = si->toc_rel_hashes;
04361                        n->h = h;
04362                        n->rel = irel;
04363                        si->toc_rel_hashes = n;
04364                      }
04365                   }
04366                 else if (h != NULL)
04367                   {
04368                     /* This is a global symbol.  */
04369                     if (h->indx >= 0)
04370                      irel->r_symndx = h->indx;
04371                     else
04372                      {
04373                        /* This symbol is being written at the end
04374                           of the file, and we do not yet know the
04375                           symbol index.  We save the pointer to the
04376                           hash table entry in the rel_hash list.
04377                           We set the indx field to -2 to indicate
04378                           that this symbol must not be stripped.  */
04379                        *rel_hash = h;
04380                        h->indx = -2;
04381                      }
04382                   }
04383                 else
04384                   {
04385                     long indx;
04386 
04387                     indx = finfo->sym_indices[r_symndx];
04388 
04389                     if (indx == -1)
04390                      {
04391                        struct internal_syment *is;
04392 
04393                        /* Relocations against a TC0 TOC anchor are
04394                           automatically transformed to be against
04395                           the TOC anchor in the output file.  */
04396                        is = finfo->internal_syms + r_symndx;
04397                        if (is->n_sclass == C_HIDEXT
04398                            && is->n_numaux > 0)
04399                          {
04400                            void * auxptr;
04401                            union internal_auxent aux;
04402 
04403                            auxptr = ((void *)
04404                                    (((bfd_byte *)
04405                                      obj_coff_external_syms (input_bfd))
04406                                     + ((r_symndx + is->n_numaux)
04407                                        * isymesz)));
04408                            bfd_coff_swap_aux_in (input_bfd, auxptr,
04409                                               is->n_type, is->n_sclass,
04410                                               is->n_numaux - 1,
04411                                               is->n_numaux,
04412                                               (void *) &aux);
04413                            if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
04414                               && aux.x_csect.x_smclas == XMC_TC0)
04415                             indx = finfo->toc_symindx;
04416                          }
04417                      }
04418 
04419                     if (indx != -1)
04420                      irel->r_symndx = indx;
04421                     else
04422                      {
04423 
04424                        struct internal_syment *is;
04425 
04426                        const char *name;
04427                        char buf[SYMNMLEN + 1];
04428 
04429                        /* This reloc is against a symbol we are
04430                           stripping.  It would be possible to handle
04431                           this case, but I don't think it's worth it.  */
04432                        is = finfo->internal_syms + r_symndx;
04433 
04434                        name = (_bfd_coff_internal_syment_name
04435                               (input_bfd, is, buf));
04436 
04437                        if (name == NULL)
04438                          return FALSE;
04439 
04440                        if (! ((*finfo->info->callbacks->unattached_reloc)
04441                              (finfo->info, name, input_bfd, o,
04442                               irel->r_vaddr)))
04443                          return FALSE;
04444                      }
04445                   }
04446               }
04447 
04448              quiet = FALSE;
04449              switch (irel->r_type)
04450               {
04451               default:
04452                 if (h == NULL
04453                     || h->root.type == bfd_link_hash_defined
04454                     || h->root.type == bfd_link_hash_defweak
04455                     || h->root.type == bfd_link_hash_common)
04456                   break;
04457                 /* Fall through.  */
04458               case R_POS:
04459               case R_NEG:
04460               case R_RL:
04461               case R_RLA:
04462                 /* This reloc needs to be copied into the .loader
04463                    section.  */
04464                 ldrel.l_vaddr = irel->r_vaddr;
04465                 if (r_symndx == -1)
04466                   ldrel.l_symndx = -(bfd_size_type ) 1;
04467                 else if (h == NULL
04468                         || (h->root.type == bfd_link_hash_defined
04469                             || h->root.type == bfd_link_hash_defweak
04470                             || h->root.type == bfd_link_hash_common))
04471                   {
04472                     asection *sec;
04473 
04474                     if (h == NULL)
04475                      sec = xcoff_data (input_bfd)->csects[r_symndx];
04476                     else if (h->root.type == bfd_link_hash_common)
04477                      sec = h->root.u.c.p->section;
04478                     else
04479                      sec = h->root.u.def.section;
04480                     sec = sec->output_section;
04481 
04482                     if (strcmp (sec->name, ".text") == 0)
04483                      ldrel.l_symndx = 0;
04484                     else if (strcmp (sec->name, ".data") == 0)
04485                      ldrel.l_symndx = 1;
04486                     else if (strcmp (sec->name, ".bss") == 0)
04487                      ldrel.l_symndx = 2;
04488                     else
04489                      {
04490                        (*_bfd_error_handler)
04491                          (_("%B: loader reloc in unrecognized section `%A'"),
04492                           input_bfd, sec);
04493                        bfd_set_error (bfd_error_nonrepresentable_section);
04494                        return FALSE;
04495                      }
04496                   }
04497                 else
04498                   {
04499                     if (! finfo->info->relocatable
04500                        && (h->flags & XCOFF_DEF_DYNAMIC) == 0
04501                        && (h->flags & XCOFF_IMPORT) == 0)
04502                      {
04503                        /* We already called the undefined_symbol
04504                           callback for this relocation, in
04505                           _bfd_ppc_xcoff_relocate_section.  Don't
04506                           issue any more warnings.  */
04507                        quiet = TRUE;
04508                      }
04509                     if (h->ldindx < 0 && ! quiet)
04510                      {
04511                        (*_bfd_error_handler)
04512                          (_("%B: `%s' in loader reloc but not loader sym"),
04513                           input_bfd,
04514                           h->root.root.string);
04515                        bfd_set_error (bfd_error_bad_value);
04516                        return FALSE;
04517                      }
04518                     ldrel.l_symndx = h->ldindx;
04519                   }
04520                 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
04521                 ldrel.l_rsecnm = o->output_section->target_index;
04522                 if (xcoff_hash_table (finfo->info)->textro
04523                     && strcmp (o->output_section->name, ".text") == 0
04524                     && ! quiet)
04525                   {
04526                     (*_bfd_error_handler)
04527                      (_("%B: loader reloc in read-only section %A"),
04528                       input_bfd, o->output_section);
04529                     bfd_set_error (bfd_error_invalid_operation);
04530                     return FALSE;
04531                   }
04532                 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
04533                                        finfo->ldrel);
04534 
04535                 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
04536                 break;
04537 
04538               case R_TOC:
04539               case R_GL:
04540               case R_TCL:
04541               case R_TRL:
04542               case R_TRLA:
04543                 /* We should never need a .loader reloc for a TOC
04544                    relative reloc.  */
04545                 break;
04546               }
04547            }
04548 
04549          o->output_section->reloc_count += o->reloc_count;
04550        }
04551 
04552       /* Write out the modified section contents.  */
04553       if (! bfd_set_section_contents (output_bfd, o->output_section,
04554                                   contents, (file_ptr) o->output_offset,
04555                                   o->size))
04556        return FALSE;
04557     }
04558 
04559   obj_coff_keep_syms (input_bfd) = keep_syms;
04560 
04561   if (! finfo->info->keep_memory)
04562     {
04563       if (! _bfd_coff_free_symbols (input_bfd))
04564        return FALSE;
04565     }
04566 
04567   return TRUE;
04568 }
04569 
04570 #undef N_TMASK
04571 #undef N_BTSHFT
04572 
04573 /* Sort relocs by VMA.  This is called via qsort.  */
04574 
04575 static int
04576 xcoff_sort_relocs (const void * p1, const void * p2)
04577 {
04578   const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
04579   const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
04580 
04581   if (r1->r_vaddr > r2->r_vaddr)
04582     return 1;
04583   else if (r1->r_vaddr < r2->r_vaddr)
04584     return -1;
04585   else
04586     return 0;
04587 }
04588 
04589 /* Write out a non-XCOFF global symbol.  */
04590 
04591 static bfd_boolean
04592 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
04593 {
04594   struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
04595   bfd *output_bfd;
04596   bfd_byte *outsym;
04597   struct internal_syment isym;
04598   union internal_auxent aux;
04599   bfd_boolean result;
04600   file_ptr pos;
04601   bfd_size_type amt;
04602 
04603   output_bfd = finfo->output_bfd;
04604   outsym = finfo->outsyms;
04605 
04606   if (h->root.type == bfd_link_hash_warning)
04607     {
04608       h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
04609       if (h->root.type == bfd_link_hash_new)
04610        return TRUE;
04611     }
04612 
04613   /* If this symbol was garbage collected, just skip it.  */
04614   if (xcoff_hash_table (finfo->info)->gc
04615       && (h->flags & XCOFF_MARK) == 0)
04616     return TRUE;
04617 
04618   /* If we need a .loader section entry, write it out.  */
04619   if (h->ldsym != NULL)
04620     {
04621       struct internal_ldsym *ldsym;
04622       bfd *impbfd;
04623 
04624       ldsym = h->ldsym;
04625 
04626       if (h->root.type == bfd_link_hash_undefined
04627          || h->root.type == bfd_link_hash_undefweak)
04628        {
04629 
04630          ldsym->l_value = 0;
04631          ldsym->l_scnum = N_UNDEF;
04632          ldsym->l_smtype = XTY_ER;
04633          impbfd = h->root.u.undef.abfd;
04634 
04635        }
04636       else if (h->root.type == bfd_link_hash_defined
04637               || h->root.type == bfd_link_hash_defweak)
04638        {
04639          asection *sec;
04640 
04641          sec = h->root.u.def.section;
04642          ldsym->l_value = (sec->output_section->vma
04643                          + sec->output_offset
04644                          + h->root.u.def.value);
04645          ldsym->l_scnum = sec->output_section->target_index;
04646          ldsym->l_smtype = XTY_SD;
04647          impbfd = sec->owner;
04648 
04649        }
04650       else
04651        abort ();
04652 
04653       if (((h->flags & XCOFF_DEF_REGULAR) == 0
04654           && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
04655          || (h->flags & XCOFF_IMPORT) != 0)
04656        /* Clear l_smtype
04657           Import symbols are defined so the check above will make
04658           the l_smtype XTY_SD.  But this is not correct, it should
04659           be cleared.  */
04660        ldsym->l_smtype |= L_IMPORT;
04661 
04662       if (((h->flags & XCOFF_DEF_REGULAR) != 0
04663           && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
04664          || (h->flags & XCOFF_EXPORT) != 0)
04665        ldsym->l_smtype |= L_EXPORT;
04666 
04667       if ((h->flags & XCOFF_ENTRY) != 0)
04668        ldsym->l_smtype |= L_ENTRY;
04669 
04670       if ((h->flags & XCOFF_RTINIT) != 0)
04671        ldsym->l_smtype = XTY_SD;
04672 
04673       ldsym->l_smclas = h->smclas;
04674 
04675       if (ldsym->l_smtype & L_IMPORT)
04676        {
04677          if ((h->root.type == bfd_link_hash_defined
04678               || h->root.type == bfd_link_hash_defweak)
04679              && (h->root.u.def.value != 0))
04680            ldsym->l_smclas = XMC_XO;
04681 
04682          else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
04683                  (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
04684            ldsym->l_smclas = XMC_SV3264;
04685 
04686          else if (h->flags & XCOFF_SYSCALL32)
04687            ldsym->l_smclas = XMC_SV;
04688 
04689          else if (h->flags & XCOFF_SYSCALL64)
04690            ldsym->l_smclas = XMC_SV64;
04691        }
04692 
04693       if (ldsym->l_ifile == -(bfd_size_type) 1)
04694        {
04695          ldsym->l_ifile = 0;
04696        }
04697       else if (ldsym->l_ifile == 0)
04698        {
04699          if ((ldsym->l_smtype & L_IMPORT) == 0)
04700            ldsym->l_ifile = 0;
04701          else if (impbfd == NULL)
04702            ldsym->l_ifile = 0;
04703          else
04704            {
04705              BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
04706              ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
04707            }
04708        }
04709 
04710       ldsym->l_parm = 0;
04711 
04712       BFD_ASSERT (h->ldindx >= 0);
04713 
04714       bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
04715                             (finfo->ldsym +
04716                              (h->ldindx - 3)
04717                              * bfd_xcoff_ldsymsz(finfo->output_bfd)));
04718       h->ldsym = NULL;
04719     }
04720 
04721   /* If this symbol needs global linkage code, write it out.  */
04722   if (h->root.type == bfd_link_hash_defined
04723       && (h->root.u.def.section
04724          == xcoff_hash_table (finfo->info)->linkage_section))
04725     {
04726       bfd_byte *p;
04727       bfd_vma tocoff;
04728       unsigned int i;
04729 
04730       p = h->root.u.def.section->contents + h->root.u.def.value;
04731 
04732       /* The first instruction in the global linkage code loads a
04733         specific TOC element.  */
04734       tocoff = (h->descriptor->toc_section->output_section->vma
04735               + h->descriptor->toc_section->output_offset
04736               - xcoff_data (output_bfd)->toc);
04737 
04738       if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
04739        tocoff += h->descriptor->u.toc_offset;
04740 
04741       /* The first instruction in the glink code needs to be
04742         cooked to to hold the correct offset in the toc.  The
04743         rest are just output raw.  */
04744       bfd_put_32 (output_bfd,
04745                 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
04746 
04747       /* Start with i == 1 to get past the first instruction done above
04748         The /4 is because the glink code is in bytes and we are going
04749         4 at a pop.  */
04750       for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
04751        bfd_put_32 (output_bfd,
04752                   (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
04753                   &p[4 * i]);
04754     }
04755 
04756   /* If we created a TOC entry for this symbol, write out the required
04757      relocs.  */
04758   if ((h->flags & XCOFF_SET_TOC) != 0)
04759     {
04760       asection *tocsec;
04761       asection *osec;
04762       int oindx;
04763       struct internal_reloc *irel;
04764       struct internal_ldrel ldrel;
04765       struct internal_syment irsym;
04766       union internal_auxent iraux;
04767 
04768       tocsec = h->toc_section;
04769       osec = tocsec->output_section;
04770       oindx = osec->target_index;
04771       irel = finfo->section_info[oindx].relocs + osec->reloc_count;
04772       irel->r_vaddr = (osec->vma
04773                      + tocsec->output_offset
04774                      + h->u.toc_offset);
04775 
04776       if (h->indx >= 0)
04777        irel->r_symndx = h->indx;
04778       else
04779        {
04780          h->indx = -2;
04781          irel->r_symndx = obj_raw_syment_count (output_bfd);
04782        }
04783 
04784       BFD_ASSERT (h->ldindx >= 0);
04785 
04786       /* Initialize the aux union here instead of closer to when it is
04787         written out below because the length of the csect depends on
04788         whether the output is 32 or 64 bit.  */
04789       memset (&iraux, 0, sizeof iraux);
04790       iraux.x_csect.x_smtyp = XTY_SD;
04791       /* iraux.x_csect.x_scnlen.l = 4 or 8, see below.  */
04792       iraux.x_csect.x_smclas = XMC_TC;
04793 
04794       /* 32 bit uses a 32 bit R_POS to do the relocations
04795         64 bit uses a 64 bit R_POS to do the relocations
04796 
04797         Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
04798 
04799         Which one is determined by the backend.  */
04800       if (bfd_xcoff_is_xcoff64 (output_bfd))
04801        {
04802          irel->r_size = 63;
04803          iraux.x_csect.x_scnlen.l = 8;
04804        }
04805       else if (bfd_xcoff_is_xcoff32 (output_bfd))
04806        {
04807          irel->r_size = 31;
04808          iraux.x_csect.x_scnlen.l = 4;
04809        }
04810       else
04811        return FALSE;
04812 
04813       irel->r_type = R_POS;
04814       finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
04815       ++osec->reloc_count;
04816 
04817       ldrel.l_vaddr = irel->r_vaddr;
04818       ldrel.l_symndx = h->ldindx;
04819       ldrel.l_rtype = (irel->r_size << 8) | R_POS;
04820       ldrel.l_rsecnm = oindx;
04821       bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
04822       finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
04823 
04824       /* We need to emit a symbol to define a csect which holds
04825         the reloc.  */
04826       if (finfo->info->strip != strip_all)
04827        {
04828          result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
04829                                          &irsym, h->root.root.string);
04830          if (!result)
04831            return FALSE;
04832 
04833          irsym.n_value = irel->r_vaddr;
04834          irsym.n_scnum = osec->target_index;
04835          irsym.n_sclass = C_HIDEXT;
04836          irsym.n_type = T_NULL;
04837          irsym.n_numaux = 1;
04838 
04839          bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
04840          outsym += bfd_coff_symesz (output_bfd);
04841 
04842          /* Note : iraux is initialized above.  */
04843          bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
04844                              0, 1, (void *) outsym);
04845          outsym += bfd_coff_auxesz (output_bfd);
04846 
04847          if (h->indx >= 0)
04848            {
04849              /* We aren't going to write out the symbols below, so we
04850                need to write them out now.  */
04851              pos = obj_sym_filepos (output_bfd);
04852              pos += (obj_raw_syment_count (output_bfd)
04853                     * bfd_coff_symesz (output_bfd));
04854              amt = outsym - finfo->outsyms;
04855              if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
04856                 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
04857               return FALSE;
04858              obj_raw_syment_count (output_bfd) +=
04859               (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
04860 
04861              outsym = finfo->outsyms;
04862            }
04863        }
04864     }
04865 
04866   /* If this symbol is a specially defined function descriptor, write
04867      it out.  The first word is the address of the function code
04868      itself, the second word is the address of the TOC, and the third
04869      word is zero.
04870 
04871      32 bit vs 64 bit
04872      The addresses for the 32 bit will take 4 bytes and the addresses
04873      for 64 bit will take 8 bytes.  Similar for the relocs.  This type
04874      of logic was also done above to create a TOC entry in
04875      xcoff_write_global_symbol.  */
04876   if ((h->flags & XCOFF_DESCRIPTOR) != 0
04877       && h->root.type == bfd_link_hash_defined
04878       && (h->root.u.def.section
04879          == xcoff_hash_table (finfo->info)->descriptor_section))
04880     {
04881       asection *sec;
04882       asection *osec;
04883       int oindx;
04884       bfd_byte *p;
04885       struct xcoff_link_hash_entry *hentry;
04886       asection *esec;
04887       struct internal_reloc *irel;
04888       struct internal_ldrel ldrel;
04889       asection *tsec;
04890       unsigned int reloc_size, byte_size;
04891 
04892       if (bfd_xcoff_is_xcoff64 (output_bfd))
04893        {
04894          reloc_size = 63;
04895          byte_size = 8;
04896        }
04897       else if (bfd_xcoff_is_xcoff32 (output_bfd))
04898        {
04899          reloc_size = 31;
04900          byte_size = 4;
04901        }
04902       else
04903        return FALSE;
04904 
04905       sec = h->root.u.def.section;
04906       osec = sec->output_section;
04907       oindx = osec->target_index;
04908       p = sec->contents + h->root.u.def.value;
04909 
04910       hentry = h->descriptor;
04911       BFD_ASSERT (hentry != NULL
04912                 && (hentry->root.type == bfd_link_hash_defined
04913                     || hentry->root.type == bfd_link_hash_defweak));
04914       esec = hentry->root.u.def.section;
04915 
04916       irel = finfo->section_info[oindx].relocs + osec->reloc_count;
04917       irel->r_vaddr = (osec->vma
04918                      + sec->output_offset
04919                      + h->root.u.def.value);
04920       irel->r_symndx = esec->output_section->target_index;
04921       irel->r_type = R_POS;
04922       irel->r_size = reloc_size;
04923       finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
04924       ++osec->reloc_count;
04925 
04926       ldrel.l_vaddr = irel->r_vaddr;
04927       if (strcmp (esec->output_section->name, ".text") == 0)
04928        ldrel.l_symndx = 0;
04929       else if (strcmp (esec->output_section->name, ".data") == 0)
04930        ldrel.l_symndx = 1;
04931       else if (strcmp (esec->output_section->name, ".bss") == 0)
04932        ldrel.l_symndx = 2;
04933       else
04934        {
04935          (*_bfd_error_handler)
04936            (_("%s: loader reloc in unrecognized section `%s'"),
04937             bfd_get_filename (output_bfd),
04938             esec->output_section->name);
04939          bfd_set_error (bfd_error_nonrepresentable_section);
04940          return FALSE;
04941        }
04942       ldrel.l_rtype = (reloc_size << 8) | R_POS;
04943       ldrel.l_rsecnm = oindx;
04944       bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
04945       finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
04946 
04947       /* There are three items to write out,
04948         the address of the code
04949         the address of the toc anchor
04950         the environment pointer.
04951         We are ignoring the environment pointer.  So set it to zero.  */
04952       if (bfd_xcoff_is_xcoff64 (output_bfd))
04953        {
04954          bfd_put_64 (output_bfd,
04955                     (esec->output_section->vma + esec->output_offset
04956                      + hentry->root.u.def.value),
04957                     p);
04958          bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
04959          bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
04960        }
04961       else
04962        {
04963          /* 32 bit backend
04964             This logic was already called above so the error case where
04965             the backend is neither has already been checked.  */
04966          bfd_put_32 (output_bfd,
04967                     (esec->output_section->vma + esec->output_offset
04968                      + hentry->root.u.def.value),
04969                     p);
04970          bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
04971          bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
04972        }
04973 
04974       tsec = coff_section_from_bfd_index (output_bfd,
04975                                      xcoff_data (output_bfd)->sntoc);
04976 
04977       ++irel;
04978       irel->r_vaddr = (osec->vma
04979                      + sec->output_offset
04980                      + h->root.u.def.value
04981                      + byte_size);
04982       irel->r_symndx = tsec->output_section->target_index;
04983       irel->r_type = R_POS;
04984       irel->r_size = reloc_size;
04985       finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
04986       ++osec->reloc_count;
04987 
04988       ldrel.l_vaddr = irel->r_vaddr;
04989       if (strcmp (tsec->output_section->name, ".text") == 0)
04990        ldrel.l_symndx = 0;
04991       else if (strcmp (tsec->output_section->name, ".data") == 0)
04992        ldrel.l_symndx = 1;
04993       else if (strcmp (tsec->output_section->name, ".bss") == 0)
04994        ldrel.l_symndx = 2;
04995       else
04996        {
04997          (*_bfd_error_handler)
04998            (_("%s: loader reloc in unrecognized section `%s'"),
04999             bfd_get_filename (output_bfd),
05000             tsec->output_section->name);
05001          bfd_set_error (bfd_error_nonrepresentable_section);
05002          return FALSE;
05003        }
05004       ldrel.l_rtype = (reloc_size << 8) | R_POS;
05005       ldrel.l_rsecnm = oindx;
05006       bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
05007       finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
05008     }
05009 
05010   if (h->indx >= 0 || finfo->info->strip == strip_all)
05011     {
05012       BFD_ASSERT (outsym == finfo->outsyms);
05013       return TRUE;
05014     }
05015 
05016   if (h->indx != -2
05017       && (finfo->info->strip == strip_all
05018          || (finfo->info->strip == strip_some
05019              && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
05020                               FALSE, FALSE) == NULL)))
05021     {
05022       BFD_ASSERT (outsym == finfo->outsyms);
05023       return TRUE;
05024     }
05025 
05026   if (h->indx != -2
05027       && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
05028     {
05029       BFD_ASSERT (outsym == finfo->outsyms);
05030       return TRUE;
05031     }
05032 
05033   memset (&aux, 0, sizeof aux);
05034 
05035   h->indx = obj_raw_syment_count (output_bfd);
05036 
05037   result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
05038                                   h->root.root.string);
05039   if (!result)
05040     return FALSE;
05041 
05042   if (h->root.type == bfd_link_hash_undefined
05043       || h->root.type == bfd_link_hash_undefweak)
05044     {
05045       isym.n_value = 0;
05046       isym.n_scnum = N_UNDEF;
05047       isym.n_sclass = C_EXT;
05048       aux.x_csect.x_smtyp = XTY_ER;
05049     }
05050   else if ((h->root.type == bfd_link_hash_defined
05051            || h->root.type == bfd_link_hash_defweak)
05052           && h->smclas == XMC_XO)
05053     {
05054       BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
05055       isym.n_value = h->root.u.def.value;
05056       isym.n_scnum = N_UNDEF;
05057       isym.n_sclass = C_EXT;
05058       aux.x_csect.x_smtyp = XTY_ER;
05059     }
05060   else if (h->root.type == bfd_link_hash_defined
05061           || h->root.type == bfd_link_hash_defweak)
05062     {
05063       struct xcoff_link_size_list *l;
05064 
05065       isym.n_value = (h->root.u.def.section->output_section->vma
05066                     + h->root.u.def.section->output_offset
05067                     + h->root.u.def.value);
05068       if (bfd_is_abs_section (h->root.u.def.section->output_section))
05069        isym.n_scnum = N_ABS;
05070       else
05071        isym.n_scnum = h->root.u.def.section->output_section->target_index;
05072       isym.n_sclass = C_HIDEXT;
05073       aux.x_csect.x_smtyp = XTY_SD;
05074 
05075       if ((h->flags & XCOFF_HAS_SIZE) != 0)
05076        {
05077          for (l = xcoff_hash_table (finfo->info)->size_list;
05078               l != NULL;
05079               l = l->next)
05080            {
05081              if (l->h == h)
05082               {
05083                 aux.x_csect.x_scnlen.l = l->size;
05084                 break;
05085               }
05086            }
05087        }
05088     }
05089   else if (h->root.type == bfd_link_hash_common)
05090     {
05091       isym.n_value = (h->root.u.c.p->section->output_section->vma
05092                     + h->root.u.c.p->section->output_offset);
05093       isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
05094       isym.n_sclass = C_EXT;
05095       aux.x_csect.x_smtyp = XTY_CM;
05096       aux.x_csect.x_scnlen.l = h->root.u.c.size;
05097     }
05098   else
05099     abort ();
05100 
05101   isym.n_type = T_NULL;
05102   isym.n_numaux = 1;
05103 
05104   bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
05105   outsym += bfd_coff_symesz (output_bfd);
05106 
05107   aux.x_csect.x_smclas = h->smclas;
05108   bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
05109                       (void *) outsym);
05110   outsym += bfd_coff_auxesz (output_bfd);
05111 
05112   if ((h->root.type == bfd_link_hash_defined
05113        || h->root.type == bfd_link_hash_defweak)
05114       && h->smclas != XMC_XO)
05115     {
05116       /* We just output an SD symbol.  Now output an LD symbol.  */
05117       h->indx += 2;
05118 
05119       isym.n_sclass = C_EXT;
05120       bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
05121       outsym += bfd_coff_symesz (output_bfd);
05122 
05123       aux.x_csect.x_smtyp = XTY_LD;
05124       aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
05125       bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
05126                           (void *) outsym);
05127       outsym += bfd_coff_auxesz (output_bfd);
05128     }
05129 
05130   pos = obj_sym_filepos (output_bfd);
05131   pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
05132   amt = outsym - finfo->outsyms;
05133   if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
05134       || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
05135     return FALSE;
05136   obj_raw_syment_count (output_bfd) +=
05137     (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
05138 
05139   return TRUE;
05140 }
05141 
05142 /* Handle a link order which is supposed to generate a reloc.  */
05143 
05144 static bfd_boolean
05145 xcoff_reloc_link_order (bfd *output_bfd,
05146                      struct xcoff_final_link_info *finfo,
05147                      asection *output_section,
05148                      struct bfd_link_order *link_order)
05149 {
05150   reloc_howto_type *howto;
05151   struct xcoff_link_hash_entry *h;
05152   asection *hsec;
05153   bfd_vma hval;
05154   bfd_vma addend;
05155   struct internal_reloc *irel;
05156   struct xcoff_link_hash_entry **rel_hash_ptr;
05157   struct internal_ldrel ldrel;
05158 
05159   if (link_order->type == bfd_section_reloc_link_order)
05160     /* We need to somehow locate a symbol in the right section.  The
05161        symbol must either have a value of zero, or we must adjust
05162        the addend by the value of the symbol.  FIXME: Write this
05163        when we need it.  The old linker couldn't handle this anyhow.  */
05164     abort ();
05165 
05166   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
05167   if (howto == NULL)
05168     {
05169       bfd_set_error (bfd_error_bad_value);
05170       return FALSE;
05171     }
05172 
05173   h = ((struct xcoff_link_hash_entry *)
05174        bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
05175                                  link_order->u.reloc.p->u.name,
05176                                  FALSE, FALSE, TRUE));
05177   if (h == NULL)
05178     {
05179       if (! ((*finfo->info->callbacks->unattached_reloc)
05180             (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
05181        return FALSE;
05182       return TRUE;
05183     }
05184 
05185   if (h->root.type == bfd_link_hash_common)
05186     {
05187       hsec = h->root.u.c.p->section;
05188       hval = 0;
05189     }
05190   else if (h->root.type == bfd_link_hash_defined
05191           || h->root.type == bfd_link_hash_defweak)
05192     {
05193       hsec = h->root.u.def.section;
05194       hval = h->root.u.def.value;
05195     }
05196   else
05197     {
05198       hsec = NULL;
05199       hval = 0;
05200     }
05201 
05202   addend = link_order->u.reloc.p->addend;
05203   if (hsec != NULL)
05204     addend += (hsec->output_section->vma
05205               + hsec->output_offset
05206               + hval);
05207 
05208   if (addend != 0)
05209     {
05210       bfd_size_type size;
05211       bfd_byte *buf;
05212       bfd_reloc_status_type rstat;
05213       bfd_boolean ok;
05214 
05215       size = bfd_get_reloc_size (howto);
05216       buf = bfd_zmalloc (size);
05217       if (buf == NULL)
05218        return FALSE;
05219 
05220       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
05221       switch (rstat)
05222        {
05223        case bfd_reloc_ok:
05224          break;
05225        default:
05226        case bfd_reloc_outofrange:
05227          abort ();
05228        case bfd_reloc_overflow:
05229          if (! ((*finfo->info->callbacks->reloc_overflow)
05230                (finfo->info, NULL, link_order->u.reloc.p->u.name,
05231                 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
05232            {
05233              free (buf);
05234              return FALSE;
05235            }
05236          break;
05237        }
05238       ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
05239                                  (file_ptr) link_order->offset, size);
05240       free (buf);
05241       if (! ok)
05242        return FALSE;
05243     }
05244 
05245   /* Store the reloc information in the right place.  It will get
05246      swapped and written out at the end of the final_link routine.  */
05247   irel = (finfo->section_info[output_section->target_index].relocs
05248          + output_section->reloc_count);
05249   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
05250                 + output_section->reloc_count);
05251 
05252   memset (irel, 0, sizeof (struct internal_reloc));
05253   *rel_hash_ptr = NULL;
05254 
05255   irel->r_vaddr = output_section->vma + link_order->offset;
05256 
05257   if (h->indx >= 0)
05258     irel->r_symndx = h->indx;
05259   else
05260     {
05261       /* Set the index to -2 to force this symbol to get written out.  */
05262       h->indx = -2;
05263       *rel_hash_ptr = h;
05264       irel->r_symndx = 0;
05265     }
05266 
05267   irel->r_type = howto->type;
05268   irel->r_size = howto->bitsize - 1;
05269   if (howto->complain_on_overflow == complain_overflow_signed)
05270     irel->r_size |= 0x80;
05271 
05272   ++output_section->reloc_count;
05273 
05274   /* Now output the reloc to the .loader section.  */
05275 
05276   ldrel.l_vaddr = irel->r_vaddr;
05277 
05278   if (hsec != NULL)
05279     {
05280       const char *secname;
05281 
05282       secname = hsec->output_section->name;
05283 
05284       if (strcmp (secname, ".text") == 0)
05285        ldrel.l_symndx = 0;
05286       else if (strcmp (secname, ".data") == 0)
05287        ldrel.l_symndx = 1;
05288       else if (strcmp (secname, ".bss") == 0)
05289        ldrel.l_symndx = 2;
05290       else
05291        {
05292          (*_bfd_error_handler)
05293            (_("%s: loader reloc in unrecognized section `%s'"),
05294             bfd_get_filename (output_bfd), secname);
05295          bfd_set_error (bfd_error_nonrepresentable_section);
05296          return FALSE;
05297        }
05298     }
05299   else
05300     {
05301       if (h->ldindx < 0)
05302        {
05303          (*_bfd_error_handler)
05304            (_("%s: `%s' in loader reloc but not loader sym"),
05305             bfd_get_filename (output_bfd),
05306             h->root.root.string);
05307          bfd_set_error (bfd_error_bad_value);
05308          return FALSE;
05309        }
05310       ldrel.l_symndx = h->ldindx;
05311     }
05312 
05313   ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
05314   ldrel.l_rsecnm = output_section->target_index;
05315   bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
05316   finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
05317 
05318   return TRUE;
05319 }
05320 
05321 /* Do the final link step.  */
05322 
05323 bfd_boolean
05324 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
05325 {
05326   bfd_size_type symesz;
05327   struct xcoff_final_link_info finfo;
05328   asection *o;
05329   struct bfd_link_order *p;
05330   bfd_size_type max_contents_size;
05331   bfd_size_type max_sym_count;
05332   bfd_size_type max_lineno_count;
05333   bfd_size_type max_reloc_count;
05334   bfd_size_type max_output_reloc_count;
05335   file_ptr rel_filepos;
05336   unsigned int relsz;
05337   file_ptr line_filepos;
05338   unsigned int linesz;
05339   bfd *sub;
05340   bfd_byte *external_relocs = NULL;
05341   char strbuf[STRING_SIZE_SIZE];
05342   file_ptr pos;
05343   bfd_size_type amt;
05344 
05345   if (info->shared)
05346     abfd->flags |= DYNAMIC;
05347 
05348   symesz = bfd_coff_symesz (abfd);
05349 
05350   finfo.info = info;
05351   finfo.output_bfd = abfd;
05352   finfo.strtab = NULL;
05353   finfo.section_info = NULL;
05354   finfo.last_file_index = -1;
05355   finfo.toc_symindx = -1;
05356   finfo.internal_syms = NULL;
05357   finfo.sym_indices = NULL;
05358   finfo.outsyms = NULL;
05359   finfo.linenos = NULL;
05360   finfo.contents = NULL;
05361   finfo.external_relocs = NULL;
05362 
05363   finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
05364                + bfd_xcoff_ldhdrsz (abfd));
05365   finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
05366                + bfd_xcoff_ldhdrsz(abfd)
05367                + (xcoff_hash_table (info)->ldhdr.l_nsyms
05368                   * bfd_xcoff_ldsymsz(abfd)));
05369 
05370   xcoff_data (abfd)->coff.link_info = info;
05371 
05372   finfo.strtab = _bfd_stringtab_init ();
05373   if (finfo.strtab == NULL)
05374     goto error_return;
05375 
05376   /* Count the line number and relocation entries required for the
05377      output file.  Determine a few maximum sizes.  */
05378   max_contents_size = 0;
05379   max_lineno_count = 0;
05380   max_reloc_count = 0;
05381   for (o = abfd->sections; o != NULL; o = o->next)
05382     {
05383       o->reloc_count = 0;
05384       o->lineno_count = 0;
05385       for (p = o->map_head.link_order; p != NULL; p = p->next)
05386        {
05387          if (p->type == bfd_indirect_link_order)
05388            {
05389              asection *sec;
05390 
05391              sec = p->u.indirect.section;
05392 
05393              /* Mark all sections which are to be included in the
05394                link.  This will normally be every section.  We need
05395                to do this so that we can identify any sections which
05396                the linker has decided to not include.  */
05397              sec->linker_mark = TRUE;
05398 
05399              if (info->strip == strip_none
05400                 || info->strip == strip_some)
05401               o->lineno_count += sec->lineno_count;
05402 
05403              o->reloc_count += sec->reloc_count;
05404 
05405              if (sec->rawsize > max_contents_size)
05406               max_contents_size = sec->rawsize;
05407              if (sec->size > max_contents_size)
05408               max_contents_size = sec->size;
05409              if (sec->lineno_count > max_lineno_count)
05410               max_lineno_count = sec->lineno_count;
05411              if (coff_section_data (sec->owner, sec) != NULL
05412                 && xcoff_section_data (sec->owner, sec) != NULL
05413                 && (xcoff_section_data (sec->owner, sec)->lineno_count
05414                     > max_lineno_count))
05415               max_lineno_count =
05416                 xcoff_section_data (sec->owner, sec)->lineno_count;
05417              if (sec->reloc_count > max_reloc_count)
05418               max_reloc_count = sec->reloc_count;
05419            }
05420          else if (p->type == bfd_section_reloc_link_order
05421                  || p->type == bfd_symbol_reloc_link_order)
05422            ++o->reloc_count;
05423        }
05424     }
05425 
05426   /* Compute the file positions for all the sections.  */
05427   if (abfd->output_has_begun)
05428     {
05429       if (xcoff_hash_table (info)->file_align != 0)
05430        abort ();
05431     }
05432   else
05433     {
05434       bfd_vma file_align;
05435 
05436       file_align = xcoff_hash_table (info)->file_align;
05437       if (file_align != 0)
05438        {
05439          bfd_boolean saw_contents;
05440          int indx;
05441          file_ptr sofar;
05442 
05443          /* Insert .pad sections before every section which has
05444             contents and is loaded, if it is preceded by some other
05445             section which has contents and is loaded.  */
05446          saw_contents = TRUE;
05447          for (o = abfd->sections; o != NULL; o = o->next)
05448            {
05449              if (strcmp (o->name, ".pad") == 0)
05450               saw_contents = FALSE;
05451              else if ((o->flags & SEC_HAS_CONTENTS) != 0
05452                      && (o->flags & SEC_LOAD) != 0)
05453               {
05454                 if (! saw_contents)
05455                   saw_contents = TRUE;
05456                 else
05457                   {
05458                     asection *n;
05459 
05460                     /* Create a pad section and place it before the section
05461                       that needs padding.  This requires unlinking and
05462                       relinking the bfd's section list.  */
05463 
05464                     n = bfd_make_section_anyway_with_flags (abfd, ".pad",
05465                                                        SEC_HAS_CONTENTS);
05466                     n->alignment_power = 0;
05467 
05468                     bfd_section_list_remove (abfd, n);
05469                     bfd_section_list_insert_before (abfd, o, n);
05470                     saw_contents = FALSE;
05471                   }
05472               }
05473            }
05474 
05475          /* Reset the section indices after inserting the new
05476             sections.  */
05477          indx = 0;
05478          for (o = abfd->sections; o != NULL; o = o->next)
05479            {
05480              ++indx;
05481              o->target_index = indx;
05482            }
05483          BFD_ASSERT ((unsigned int) indx == abfd->section_count);
05484 
05485          /* Work out appropriate sizes for the .pad sections to force
05486             each section to land on a page boundary.  This bit of
05487             code knows what compute_section_file_positions is going
05488             to do.  */
05489          sofar = bfd_coff_filhsz (abfd);
05490          sofar += bfd_coff_aoutsz (abfd);
05491          sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
05492          for (o = abfd->sections; o != NULL; o = o->next)
05493            if ((bfd_xcoff_is_reloc_count_overflow
05494                (abfd, (bfd_vma) o->reloc_count))
05495               || (bfd_xcoff_is_lineno_count_overflow
05496                   (abfd, (bfd_vma) o->lineno_count)))
05497              /* 64 does not overflow, need to check if 32 does */
05498              sofar += bfd_coff_scnhsz (abfd);
05499 
05500          for (o = abfd->sections; o != NULL; o = o->next)
05501            {
05502              if (strcmp (o->name, ".pad") == 0)
05503               {
05504                 bfd_vma pageoff;
05505 
05506                 BFD_ASSERT (o->size == 0);
05507                 pageoff = sofar & (file_align - 1);
05508                 if (pageoff != 0)
05509                   {
05510                     o->size = file_align - pageoff;
05511                     sofar += file_align - pageoff;
05512                     o->flags |= SEC_HAS_CONTENTS;
05513                   }
05514               }
05515              else
05516               {
05517                 if ((o->flags & SEC_HAS_CONTENTS) != 0)
05518                   sofar += BFD_ALIGN (o->size,
05519                                    1 << o->alignment_power);
05520               }
05521            }
05522        }
05523 
05524       if (! bfd_coff_compute_section_file_positions (abfd))
05525        goto error_return;
05526     }
05527 
05528   /* Allocate space for the pointers we need to keep for the relocs.  */
05529   {
05530     unsigned int i;
05531 
05532     /* We use section_count + 1, rather than section_count, because
05533        the target_index fields are 1 based.  */
05534     amt = abfd->section_count + 1;
05535     amt *= sizeof (struct xcoff_link_section_info);
05536     finfo.section_info = bfd_malloc (amt);
05537     if (finfo.section_info == NULL)
05538       goto error_return;
05539     for (i = 0; i <= abfd->section_count; i++)
05540       {
05541        finfo.section_info[i].relocs = NULL;
05542        finfo.section_info[i].rel_hashes = NULL;
05543        finfo.section_info[i].toc_rel_hashes = NULL;
05544       }
05545   }
05546 
05547   /* Set the file positions for the relocs.  */
05548   rel_filepos = obj_relocbase (abfd);
05549   relsz = bfd_coff_relsz (abfd);
05550   max_output_reloc_count = 0;
05551   for (o = abfd->sections; o != NULL; o = o->next)
05552     {
05553       if (o->reloc_count == 0)
05554        o->rel_filepos = 0;
05555       else
05556        {
05557          /* A stripped file has no relocs.  However, we still
05558             allocate the buffers, so that later code doesn't have to
05559             worry about whether we are stripping or not.  */
05560          if (info->strip == strip_all)
05561            o->rel_filepos = 0;
05562          else
05563            {
05564              o->flags |= SEC_RELOC;
05565              o->rel_filepos = rel_filepos;
05566              rel_filepos += o->reloc_count * relsz;
05567            }
05568 
05569          /* We don't know the indices of global symbols until we have
05570             written out all the local symbols.  For each section in
05571             the output file, we keep an array of pointers to hash
05572             table entries.  Each entry in the array corresponds to a
05573             reloc.  When we find a reloc against a global symbol, we
05574             set the corresponding entry in this array so that we can
05575             fix up the symbol index after we have written out all the
05576             local symbols.
05577 
05578             Because of this problem, we also keep the relocs in
05579             memory until the end of the link.  This wastes memory.
05580             We could backpatch the file later, I suppose, although it
05581             would be slow.  */
05582          amt = o->reloc_count;
05583          amt *= sizeof (struct internal_reloc);
05584          finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
05585 
05586          amt = o->reloc_count;
05587          amt *= sizeof (struct xcoff_link_hash_entry *);
05588          finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
05589 
05590          if (finfo.section_info[o->target_index].relocs == NULL
05591              || finfo.section_info[o->target_index].rel_hashes == NULL)
05592            goto error_return;
05593 
05594          if (o->reloc_count > max_output_reloc_count)
05595            max_output_reloc_count = o->reloc_count;
05596        }
05597     }
05598 
05599   /* We now know the size of the relocs, so we can determine the file
05600      positions of the line numbers.  */
05601   line_filepos = rel_filepos;
05602   finfo.line_filepos = line_filepos;
05603   linesz = bfd_coff_linesz (abfd);
05604   for (o = abfd->sections; o != NULL; o = o->next)
05605     {
05606       if (o->lineno_count == 0)
05607        o->line_filepos = 0;
05608       else
05609        {
05610          o->line_filepos = line_filepos;
05611          line_filepos += o->lineno_count * linesz;
05612        }
05613 
05614       /* Reset the reloc and lineno counts, so that we can use them to
05615         count the number of entries we have output so far.  */
05616       o->reloc_count = 0;
05617       o->lineno_count = 0;
05618     }
05619 
05620   obj_sym_filepos (abfd) = line_filepos;
05621 
05622   /* Figure out the largest number of symbols in an input BFD.  Take
05623      the opportunity to clear the output_has_begun fields of all the
05624      input BFD's.  We want at least 6 symbols, since that is the
05625      number which xcoff_write_global_symbol may need.  */
05626   max_sym_count = 6;
05627   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
05628     {
05629       bfd_size_type sz;
05630 
05631       sub->output_has_begun = FALSE;
05632       sz = obj_raw_syment_count (sub);
05633       if (sz > max_sym_count)
05634        max_sym_count = sz;
05635     }
05636 
05637   /* Allocate some buffers used while linking.  */
05638   amt = max_sym_count * sizeof (struct internal_syment);
05639   finfo.internal_syms = bfd_malloc (amt);
05640 
05641   amt = max_sym_count * sizeof (long);
05642   finfo.sym_indices = bfd_malloc (amt);
05643 
05644   amt = (max_sym_count + 1) * symesz;
05645   finfo.outsyms = bfd_malloc (amt);
05646 
05647   amt = max_lineno_count * bfd_coff_linesz (abfd);
05648   finfo.linenos = bfd_malloc (amt);
05649 
05650   amt = max_contents_size;
05651   finfo.contents = bfd_malloc (amt);
05652 
05653   amt = max_reloc_count * relsz;
05654   finfo.external_relocs = bfd_malloc (amt);
05655 
05656   if ((finfo.internal_syms == NULL && max_sym_count > 0)
05657       || (finfo.sym_indices == NULL && max_sym_count > 0)
05658       || finfo.outsyms == NULL
05659       || (finfo.linenos == NULL && max_lineno_count > 0)
05660       || (finfo.contents == NULL && max_contents_size > 0)
05661       || (finfo.external_relocs == NULL && max_reloc_count > 0))
05662     goto error_return;
05663 
05664   obj_raw_syment_count (abfd) = 0;
05665   xcoff_data (abfd)->toc = (bfd_vma) -1;
05666 
05667   /* We now know the position of everything in the file, except that
05668      we don't know the size of the symbol table and therefore we don't
05669      know where the string table starts.  We just build the string
05670      table in memory as we go along.  We process all the relocations
05671      for a single input file at once.  */
05672   for (o = abfd->sections; o != NULL; o = o->next)
05673     {
05674       for (p = o->map_head.link_order; p != NULL; p = p->next)
05675        {
05676          if (p->type == bfd_indirect_link_order
05677              && p->u.indirect.section->owner->xvec == abfd->xvec)
05678            {
05679              sub = p->u.indirect.section->owner;
05680              if (! sub->output_has_begun)
05681               {
05682                 if (! xcoff_link_input_bfd (&finfo, sub))
05683                   goto error_return;
05684                 sub->output_has_begun = TRUE;
05685               }
05686            }
05687          else if (p->type == bfd_section_reloc_link_order
05688                  || p->type == bfd_symbol_reloc_link_order)
05689            {
05690              if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
05691               goto error_return;
05692            }
05693          else
05694            {
05695              if (! _bfd_default_link_order (abfd, info, o, p))
05696               goto error_return;
05697            }
05698        }
05699     }
05700 
05701   /* Free up the buffers used by xcoff_link_input_bfd.  */
05702   if (finfo.internal_syms != NULL)
05703     {
05704       free (finfo.internal_syms);
05705       finfo.internal_syms = NULL;
05706     }
05707   if (finfo.sym_indices != NULL)
05708     {
05709       free (finfo.sym_indices);
05710       finfo.sym_indices = NULL;
05711     }
05712   if (finfo.linenos != NULL)
05713     {
05714       free (finfo.linenos);
05715       finfo.linenos = NULL;
05716     }
05717   if (finfo.contents != NULL)
05718     {
05719       free (finfo.contents);
05720       finfo.contents = NULL;
05721     }
05722   if (finfo.external_relocs != NULL)
05723     {
05724       free (finfo.external_relocs);
05725       finfo.external_relocs = NULL;
05726     }
05727 
05728   /* The value of the last C_FILE symbol is supposed to be -1.  Write
05729      it out again.  */
05730   if (finfo.last_file_index != -1)
05731     {
05732       finfo.last_file.n_value = -(bfd_vma) 1;
05733       bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
05734                           (void *) finfo.outsyms);
05735       pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
05736       if (bfd_seek (abfd, pos, SEEK_SET) != 0
05737          || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
05738        goto error_return;
05739     }
05740 
05741   /* Write out all the global symbols which do not come from XCOFF
05742      input files.  */
05743   xcoff_link_hash_traverse (xcoff_hash_table (info),
05744                          xcoff_write_global_symbol,
05745                          (void *) &finfo);
05746 
05747   if (finfo.outsyms != NULL)
05748     {
05749       free (finfo.outsyms);
05750       finfo.outsyms = NULL;
05751     }
05752 
05753   /* Now that we have written out all the global symbols, we know the
05754      symbol indices to use for relocs against them, and we can finally
05755      write out the relocs.  */
05756   amt = max_output_reloc_count * relsz;
05757   external_relocs = bfd_malloc (amt);
05758   if (external_relocs == NULL && max_output_reloc_count != 0)
05759     goto error_return;
05760 
05761   for (o = abfd->sections; o != NULL; o = o->next)
05762     {
05763       struct internal_reloc *irel;
05764       struct internal_reloc *irelend;
05765       struct xcoff_link_hash_entry **rel_hash;
05766       struct xcoff_toc_rel_hash *toc_rel_hash;
05767       bfd_byte *erel;
05768       bfd_size_type rel_size;
05769 
05770       /* A stripped file has no relocs.  */
05771       if (info->strip == strip_all)
05772        {
05773          o->reloc_count = 0;
05774          continue;
05775        }
05776 
05777       if (o->reloc_count == 0)
05778        continue;
05779 
05780       irel = finfo.section_info[o->target_index].relocs;
05781       irelend = irel + o->reloc_count;
05782       rel_hash = finfo.section_info[o->target_index].rel_hashes;
05783       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
05784        {
05785          if (*rel_hash != NULL)
05786            {
05787              if ((*rel_hash)->indx < 0)
05788               {
05789                 if (! ((*info->callbacks->unattached_reloc)
05790                       (info, (*rel_hash)->root.root.string,
05791                        NULL, o, irel->r_vaddr)))
05792                   goto error_return;
05793                 (*rel_hash)->indx = 0;
05794               }
05795              irel->r_symndx = (*rel_hash)->indx;
05796            }
05797        }
05798 
05799       for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
05800           toc_rel_hash != NULL;
05801           toc_rel_hash = toc_rel_hash->next)
05802        {
05803          if (toc_rel_hash->h->u.toc_indx < 0)
05804            {
05805              if (! ((*info->callbacks->unattached_reloc)
05806                    (info, toc_rel_hash->h->root.root.string,
05807                     NULL, o, toc_rel_hash->rel->r_vaddr)))
05808               goto error_return;
05809              toc_rel_hash->h->u.toc_indx = 0;
05810            }
05811          toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
05812        }
05813 
05814       /* XCOFF requires that the relocs be sorted by address.  We tend
05815         to produce them in the order in which their containing csects
05816         appear in the symbol table, which is not necessarily by
05817         address.  So we sort them here.  There may be a better way to
05818         do this.  */
05819       qsort ((void *) finfo.section_info[o->target_index].relocs,
05820             o->reloc_count, sizeof (struct internal_reloc),
05821             xcoff_sort_relocs);
05822 
05823       irel = finfo.section_info[o->target_index].relocs;
05824       irelend = irel + o->reloc_count;
05825       erel = external_relocs;
05826       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
05827        bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
05828 
05829       rel_size = relsz * o->reloc_count;
05830       if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
05831          || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
05832        goto error_return;
05833     }
05834 
05835   if (external_relocs != NULL)
05836     {
05837       free (external_relocs);
05838       external_relocs = NULL;
05839     }
05840 
05841   /* Free up the section information.  */
05842   if (finfo.section_info != NULL)
05843     {
05844       unsigned int i;
05845 
05846       for (i = 0; i < abfd->section_count; i++)
05847        {
05848          if (finfo.section_info[i].relocs != NULL)
05849            free (finfo.section_info[i].relocs);
05850          if (finfo.section_info[i].rel_hashes != NULL)
05851            free (finfo.section_info[i].rel_hashes);
05852        }
05853       free (finfo.section_info);
05854       finfo.section_info = NULL;
05855     }
05856 
05857   /* Write out the loader section contents.  */
05858   BFD_ASSERT ((bfd_byte *) finfo.ldrel
05859              == (xcoff_hash_table (info)->loader_section->contents
05860                 + xcoff_hash_table (info)->ldhdr.l_impoff));
05861   o = xcoff_hash_table (info)->loader_section;
05862   if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
05863                               (file_ptr) o->output_offset, o->size))
05864     goto error_return;
05865 
05866   /* Write out the magic sections.  */
05867   o = xcoff_hash_table (info)->linkage_section;
05868   if (o->size > 0
05869       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
05870                                  (file_ptr) o->output_offset,
05871                                  o->size))
05872     goto error_return;
05873   o = xcoff_hash_table (info)->toc_section;
05874   if (o->size > 0
05875       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
05876                                  (file_ptr) o->output_offset,
05877                                  o->size))
05878     goto error_return;
05879   o = xcoff_hash_table (info)->descriptor_section;
05880   if (o->size > 0
05881       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
05882                                  (file_ptr) o->output_offset,
05883                                  o->size))
05884     goto error_return;
05885 
05886   /* Write out the string table.  */
05887   pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
05888   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
05889     goto error_return;
05890   H_PUT_32 (abfd,
05891            _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
05892            strbuf);
05893   amt = STRING_SIZE_SIZE;
05894   if (bfd_bwrite (strbuf, amt, abfd) != amt)
05895     goto error_return;
05896   if (! _bfd_stringtab_emit (abfd, finfo.strtab))
05897     goto error_return;
05898 
05899   _bfd_stringtab_free (finfo.strtab);
05900 
05901   /* Write out the debugging string table.  */
05902   o = xcoff_hash_table (info)->debug_section;
05903   if (o != NULL)
05904     {
05905       struct bfd_strtab_hash *debug_strtab;
05906 
05907       debug_strtab = xcoff_hash_table (info)->debug_strtab;
05908       BFD_ASSERT (o->output_section->size - o->output_offset
05909                 >= _bfd_stringtab_size (debug_strtab));
05910       pos = o->output_section->filepos + o->output_offset;
05911       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
05912        goto error_return;
05913       if (! _bfd_stringtab_emit (abfd, debug_strtab))
05914        goto error_return;
05915     }
05916 
05917   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
05918      not try to write out the symbols.  */
05919   bfd_get_symcount (abfd) = 0;
05920 
05921   return TRUE;
05922 
05923  error_return:
05924   if (finfo.strtab != NULL)
05925     _bfd_stringtab_free (finfo.strtab);
05926 
05927   if (finfo.section_info != NULL)
05928     {
05929       unsigned int i;
05930 
05931       for (i = 0; i < abfd->section_count; i++)
05932        {
05933          if (finfo.section_info[i].relocs != NULL)
05934            free (finfo.section_info[i].relocs);
05935          if (finfo.section_info[i].rel_hashes != NULL)
05936            free (finfo.section_info[i].rel_hashes);
05937        }
05938       free (finfo.section_info);
05939     }
05940 
05941   if (finfo.internal_syms != NULL)
05942     free (finfo.internal_syms);
05943   if (finfo.sym_indices != NULL)
05944     free (finfo.sym_indices);
05945   if (finfo.outsyms != NULL)
05946     free (finfo.outsyms);
05947   if (finfo.linenos != NULL)
05948     free (finfo.linenos);
05949   if (finfo.contents != NULL)
05950     free (finfo.contents);
05951   if (finfo.external_relocs != NULL)
05952     free (finfo.external_relocs);
05953   if (external_relocs != NULL)
05954     free (external_relocs);
05955   return FALSE;
05956 }