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