home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / xcofflink.c < prev   
C/C++ Source or Header  |  1996-09-28  |  185KB  |  6,298 lines

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