home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / elf32-hppa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-12  |  93.1 KB  |  2,912 lines

  1. /* BFD back-end for HP PA-RISC ELF files.
  2.    Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4.    Written by
  5.  
  6.     Center for Software Science
  7.     Department of Computer Science
  8.     University of Utah
  9.  
  10. This file is part of BFD, the Binary File Descriptor library.
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  25.  
  26. #include "bfd.h"
  27. #include "sysdep.h"
  28. #include "bfdlink.h"
  29. #include "libbfd.h"
  30. #include "obstack.h"
  31. #include "libelf.h"
  32.  
  33.  
  34. /* HPPA symbol table extension entry types */
  35. enum elf32_hppa_symextn_types
  36. {
  37.   PARISC_SXT_NULL,
  38.   PARISC_SXT_SYMNDX,
  39.   PARISC_SXT_ARG_RELOC,
  40. };
  41.  
  42. /* These macros compose and decompose the value of a symextn entry:
  43.  
  44.    entry_type = ELF32_PARISC_SX_TYPE(word);
  45.    entry_value = ELF32_PARISC_SX_VAL(word);
  46.    word = ELF32_PARISC_SX_WORD(type,val);  */
  47.  
  48. #define ELF32_PARISC_SX_TYPE(p)        ((p) >> 24)
  49. #define ELF32_PARISC_SX_VAL(p)        ((p) & 0xFFFFFF)
  50. #define ELF32_PARISC_SX_WORD(type,val)    (((type) << 24) + (val & 0xFFFFFF))
  51.  
  52. /* The following was added facilitate implementation of the .hppa_symextn
  53.    section.  This section is built after the symbol table is built in the
  54.    elf_write_object_contents routine (called from bfd_close).  It is built
  55.    so late because it requires information that is not known until
  56.    the symbol and string table sections have been allocated, and
  57.    the symbol table has been built. */
  58.  
  59. #define SYMEXTN_SECTION_NAME ".PARISC.symext"
  60.  
  61. /* FIXME.  Are these external?  (For example used by GAS?).  If so the
  62.    names need to change to avoid namespace pollution, if not they should
  63.    be moved into elf32-hppa.c.  */
  64. typedef unsigned long symext_entryS;
  65. struct symext_chain
  66.   {
  67.     symext_entryS entry;
  68.     struct symext_chain *next;
  69.   };
  70.  
  71. typedef struct symext_chain symext_chainS;
  72.  
  73. /* We use three different hash tables to hold information for
  74.    linking PA ELF objects.
  75.  
  76.    The first is the elf32_hppa_link_hash_table which is derived
  77.    from the standard ELF linker hash table.  We use this as a place to
  78.    attach other hash tables and static information.
  79.  
  80.    The second is the stub hash table which is derived from the
  81.    base BFD hash table.  The stub hash table holds the information
  82.    necessary to build the linker stubs during a link.
  83.  
  84.    The last hash table keeps track of argument location information needed
  85.    to build hash tables.  Each function with nonzero argument location
  86.    bits will have an entry in this table.  */
  87.  
  88. /* Hash table for linker stubs.  */
  89.  
  90. struct elf32_hppa_stub_hash_entry
  91. {
  92.   /* Base hash table entry structure, we can get the name of the stub
  93.      (and thus know exactly what actions it performs) from the base
  94.      hash table entry.  */
  95.   struct bfd_hash_entry root;
  96.  
  97.   /* Offset of the beginning of this stub.  */
  98.   bfd_vma offset;
  99.  
  100.   /* Given the symbol's value and its section we can determine its final
  101.      value when building the stubs (so the stub knows where to jump.  */
  102.   symvalue target_value;
  103.   asection *target_section;
  104. };
  105.  
  106. struct elf32_hppa_stub_hash_table
  107. {
  108.   /* The hash table itself.  */
  109.   struct bfd_hash_table root;
  110.  
  111.   /* The stub BFD.  */
  112.   bfd *stub_bfd;
  113.  
  114.   /* Where to place the next stub.  */
  115.   bfd_byte *location;
  116.  
  117.   /* Current offset in the stub section.  */
  118.   unsigned int offset;
  119.  
  120. };
  121.  
  122. /* Hash table for argument location information.  */
  123.  
  124. struct elf32_hppa_args_hash_entry
  125. {
  126.   /* Base hash table entry structure.  */
  127.   struct bfd_hash_entry root;
  128.  
  129.   /* The argument location bits for this entry.  */
  130.   int arg_bits;
  131. };
  132.  
  133. struct elf32_hppa_args_hash_table
  134. {
  135.   /* The hash table itself.  */
  136.   struct bfd_hash_table root;
  137. };
  138.  
  139. struct elf32_hppa_link_hash_entry
  140. {
  141.   struct elf_link_hash_entry root;
  142. };
  143.  
  144. struct elf32_hppa_link_hash_table
  145. {
  146.   /* The main hash table.  */
  147.   struct elf_link_hash_table root;
  148.  
  149.   /* The stub hash table.  */
  150.   struct elf32_hppa_stub_hash_table *stub_hash_table;
  151.  
  152.   /* The argument relocation bits hash table.  */
  153.   struct elf32_hppa_args_hash_table *args_hash_table;
  154.  
  155.   /* A count of the number of output symbols.  */
  156.   unsigned int output_symbol_count;
  157.  
  158.   /* Stuff so we can handle DP relative relocations.  */
  159.   long global_value;
  160.   int global_sym_defined;
  161. };
  162.  
  163. typedef unsigned int symextn_entry;
  164.  
  165. /* FIXME.  */
  166. #define ARGUMENTS    0
  167. #define RETURN_VALUE    1
  168.  
  169. /* The various argument relocations that may be performed.  */
  170. typedef enum
  171. {
  172.   /* No relocation.  */
  173.   NO,
  174.   /* Relocate 32 bits from GR to FP register.  */
  175.   GF,
  176.   /* Relocate 64 bits from a GR pair to FP pair.  */
  177.   GD,
  178.   /* Relocate 32 bits from FP to GR.  */
  179.   FG,
  180.   /* Relocate 64 bits from FP pair to GR pair.  */
  181.   DG,
  182. } arg_reloc_type;
  183.  
  184. /* What is being relocated (eg which argument or the return value).  */
  185. typedef enum
  186. {
  187.   ARG0, ARG1, ARG2, ARG3, RET,
  188. } arg_reloc_location;
  189.  
  190.  
  191. /* ELF32/HPPA relocation support
  192.  
  193.     This file contains ELF32/HPPA relocation support as specified
  194.     in the Stratus FTX/Golf Object File Format (SED-1762) dated
  195.     November 19, 1992.  */
  196.  
  197. #include "elf32-hppa.h"
  198. #include "hppa_stubs.h"
  199.  
  200. static bfd_reloc_status_type hppa_elf_reloc
  201.   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  202.  
  203. static unsigned long hppa_elf_relocate_insn
  204.   PARAMS ((bfd *, asection *, unsigned long, unsigned long, long,
  205.        long, unsigned long, unsigned long, unsigned long));
  206.  
  207. static bfd_reloc_status_type hppa_elf_reloc
  208.   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd*, char **));
  209.  
  210. static CONST reloc_howto_type * elf_hppa_reloc_type_lookup
  211.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  212.  
  213. static boolean elf32_hppa_set_section_contents
  214.   PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
  215.  
  216. static void elf_info_to_howto
  217.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
  218.  
  219. static boolean elf32_hppa_backend_symbol_table_processing
  220.   PARAMS ((bfd *, elf_symbol_type *, int));
  221.  
  222. static void elf32_hppa_backend_begin_write_processing
  223.   PARAMS ((bfd *, struct bfd_link_info *));
  224.  
  225. static void elf32_hppa_backend_final_write_processing
  226.   PARAMS ((bfd *, struct bfd_link_info *));
  227.  
  228. static void add_entry_to_symext_chain
  229.   PARAMS ((bfd *, unsigned int, unsigned int, symext_chainS **,
  230.        symext_chainS **));
  231.  
  232. static void
  233. elf_hppa_tc_make_sections PARAMS ((bfd *, symext_chainS *));
  234.  
  235. static boolean hppa_elf_is_local_label PARAMS ((bfd *, asymbol *));
  236.  
  237. static boolean elf32_hppa_add_symbol_hook
  238.   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
  239.        const char **, flagword *, asection **, bfd_vma *));
  240.  
  241. static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
  242.   PARAMS ((const reloc_howto_type *, bfd *, bfd *, asection *,
  243.        bfd_byte *, bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
  244.        asection *, const char *, int));
  245.  
  246. static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
  247.   PARAMS ((bfd *));
  248.  
  249. static struct bfd_hash_entry *
  250. elf32_hppa_stub_hash_newfunc
  251.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  252.  
  253. static struct bfd_hash_entry *
  254. elf32_hppa_args_hash_newfunc
  255.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  256.  
  257. static boolean
  258. elf32_hppa_relocate_section
  259.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
  260.        bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **,
  261.        char *));
  262.  
  263. static boolean
  264. elf32_hppa_stub_hash_table_init
  265.   PARAMS ((struct elf32_hppa_stub_hash_table *, bfd *,
  266.        struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
  267.                            struct bfd_hash_table *,
  268.                            const char *))));
  269.  
  270. static boolean
  271. elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR));
  272.  
  273. static boolean
  274. elf32_hppa_read_symext_info
  275.   PARAMS ((bfd *, Elf_Internal_Shdr *, struct elf32_hppa_args_hash_table *,
  276.        Elf_Internal_Sym *, boolean, boolean));
  277.  
  278. static unsigned int elf32_hppa_size_of_stub
  279.   PARAMS ((unsigned int, unsigned int, bfd_vma, bfd_vma, const char *));
  280.  
  281. static boolean elf32_hppa_arg_reloc_needed
  282.   PARAMS ((unsigned int, unsigned int, arg_reloc_type []));
  283.  
  284. static void elf32_hppa_name_of_stub
  285.   PARAMS ((unsigned int, unsigned int, bfd_vma, bfd_vma, char *));
  286.  
  287. static boolean elf32_hppa_size_symext PARAMS ((struct bfd_hash_entry *, PTR));
  288.  
  289. static boolean elf32_hppa_link_output_symbol_hook
  290.   PARAMS ((bfd *, struct bfd_link_info *, const char *,
  291.        Elf_Internal_Sym *, asection *));
  292.  
  293. /* ELF/PA relocation howto entries.  */
  294.  
  295. static reloc_howto_type elf_hppa_howto_table[ELF_HOWTO_TABLE_SIZE] =
  296. {
  297.   {R_PARISC_NONE, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_NONE"},
  298.   {R_PARISC_DIR32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR32"},
  299.   {R_PARISC_DIR21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR21L"},
  300.   {R_PARISC_DIR17R, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR17R"},
  301.   {R_PARISC_DIR17F, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR17F"},
  302.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  303.   {R_PARISC_DIR14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR14R"},
  304.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  305.  
  306.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  307.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  308.   {R_PARISC_PCREL21L, 0, 0, 21, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL21L"},
  309.   {R_PARISC_PCREL17R, 0, 0, 17, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL17R"},
  310.   {R_PARISC_PCREL17F, 0, 0, 17, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL17F"},
  311.   {R_PARISC_PCREL17C, 0, 0, 17, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL17C"},
  312.   {R_PARISC_PCREL14R, 0, 0, 14, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL14R"},
  313.   {R_PARISC_PCREL14F, 0, 0, 14, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL14F"},
  314.  
  315.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  316.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  317.   {R_PARISC_DPREL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DPREL21L"},
  318.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  319.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  320.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  321.   {R_PARISC_DPREL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DPREL14R"},
  322.   {R_PARISC_DPREL14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DPREL14F"},
  323.  
  324.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  325.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  326.   {R_PARISC_DLTREL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTREL21L"},
  327.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  328.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  329.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  330.   {R_PARISC_DLTREL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTREL14R"},
  331.   {R_PARISC_DLTREL14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTREL14F"},
  332.  
  333.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  334.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  335.   {R_PARISC_DLTIND21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTIND21L"},
  336.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  337.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  338.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  339.   {R_PARISC_DLTIND14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTIND14R"},
  340.   {R_PARISC_DLTIND14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTIND14F"},
  341.  
  342.   {R_PARISC_SETBASE, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_SETBASE"},
  343.   {R_PARISC_BASEREL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL32"},
  344.   {R_PARISC_BASEREL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL21L"},
  345.   {R_PARISC_BASEREL17R, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL17R"},
  346.   {R_PARISC_BASEREL17F, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL17F"},
  347.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  348.   {R_PARISC_BASEREL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL14R"},
  349.   {R_PARISC_BASEREL14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL14F"},
  350.  
  351.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  352.   {R_PARISC_TEXTREL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_TEXTREL32"},
  353.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  354.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  355.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  356.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  357.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  358.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  359.  
  360.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  361.   {R_PARISC_DATAREL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  362.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  363.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  364.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  365.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  366.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  367.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  368.  
  369.  
  370.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  371.   {R_PARISC_PLABEL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLABEL32"},
  372.   {R_PARISC_PLABEL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLABEL21L"},
  373.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  374.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  375.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  376.   {R_PARISC_PLABEL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLABEL14R"},
  377.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  378.  
  379.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  380.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  381.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  382.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  383.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  384.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  385.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  386.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  387.  
  388.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  389.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  390.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  391.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  392.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  393.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  394.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  395.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  396.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  397.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  398.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  399.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  400.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  401.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  402.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  403.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  404.  
  405.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  406.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  407.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  408.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  409.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  410.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  411.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  412.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  413.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  414.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  415.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  416.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  417.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  418.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  419.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  420.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  421.  
  422.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  423.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  424.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  425.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  426.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  427.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  428.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  429.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  430.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  431.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  432.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  433.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  434.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  435.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  436.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  437.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  438.  
  439.  
  440.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  441.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  442.   {R_PARISC_PLTIND21L, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLTIND21L"},
  443.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  444.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  445.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  446.   {R_PARISC_PLTIND14R, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLTIND14R"},
  447.   {R_PARISC_PLTIND14F, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLTIND14F"},
  448.  
  449.  
  450.   {R_PARISC_COPY, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_COPY"},
  451.   {R_PARISC_GLOB_DAT, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_GLOB_DAT"},
  452.   {R_PARISC_JMP_SLOT, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_JMP_SLOT"},
  453.   {R_PARISC_RELATIVE, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_RELATIVE"},
  454.  
  455.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_dont, NULL, "R_PARISC_UNIMPLEMENTED"},
  456. };
  457.  
  458. /* Where (what register type) is an argument comming from?  */
  459. typedef enum
  460. {
  461.   AR_NO,
  462.   AR_GR,
  463.   AR_FR,
  464.   AR_FU,
  465.   AR_FPDBL1,
  466.   AR_FPDBL2,
  467. } arg_location;
  468.  
  469. /* Horizontal represents the callee's argument location information,
  470.    vertical represents caller's argument location information.  Value at a
  471.    particular X,Y location represents what (if any) argument relocation
  472.    needs to be performed to make caller and callee agree.  */
  473.  
  474. static CONST arg_reloc_type arg_mismatches[6][6] =
  475. {
  476.   {NO, NO, NO, NO, NO, NO},
  477.   {NO, NO, GF, NO, GD, NO},
  478.   {NO, FG, NO, NO, NO, NO},
  479.   {NO, NO, NO, NO, NO, NO},
  480.   {NO, DG, NO, NO, NO, NO},
  481.   {NO, DG, NO, NO, NO, NO},
  482. };
  483.  
  484. /* Likewise, but reversed for the return value.  */
  485. static CONST arg_reloc_type ret_mismatches[6][6] =
  486. {
  487.   {NO, NO, NO, NO, NO, NO},
  488.   {NO, NO, FG, NO, DG, NO},
  489.   {NO, GF, NO, NO, NO, NO},
  490.   {NO, NO, NO, NO, NO, NO},
  491.   {NO, GD, NO, NO, NO, NO},
  492.   {NO, GD, NO, NO, NO, NO},
  493. };
  494.  
  495. /* Misc static crud for symbol extension records.  */
  496. static symext_chainS *symext_rootP;
  497. static symext_chainS *symext_lastP;
  498. static int symext_chain_size;
  499.  
  500. /* FIXME: We should be able to try this static variable!  */
  501. static symext_entryS *symextn_contents;
  502.  
  503.  
  504. /* For linker stub hash tables.  */
  505. #define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
  506.   ((struct elf32_hppa_stub_hash_entry *) \
  507.    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  508.  
  509. #define elf32_hppa_stub_hash_traverse(table, func, info) \
  510.   (bfd_hash_traverse \
  511.    (&(table)->root, \
  512.     (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
  513.     (info)))
  514.  
  515. /* For linker args hash tables.  */
  516. #define elf32_hppa_args_hash_lookup(table, string, create, copy) \
  517.   ((struct elf32_hppa_args_hash_entry *) \
  518.    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  519.  
  520. #define elf32_hppa_args_hash_traverse(table, func, info) \
  521.   (bfd_hash_traverse \
  522.    (&(table)->root, \
  523.     (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
  524.     (info)))
  525.  
  526. #define elf32_hppa_args_hash_table_init(table, newfunc) \
  527.   (bfd_hash_table_init \
  528.    (&(table)->root, \
  529.     (struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *, \
  530.                      struct bfd_hash_table *, \
  531.                      const char *))) (newfunc)))
  532.  
  533. /* For HPPA linker hash table.  */
  534.  
  535. #define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
  536.   ((struct elf32_hppa_link_hash_entry *)                \
  537.    elf_link_hash_lookup (&(table)->root, (string), (create),        \
  538.              (copy), (follow)))
  539.  
  540. #define elf32_hppa_link_hash_traverse(table, func, info)        \
  541.   (elf_link_hash_traverse                        \
  542.    (&(table)->root,                            \
  543.     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),    \
  544.     (info)))
  545.  
  546. /* Get the PA ELF linker hash table from a link_info structure.  */
  547.  
  548. #define elf32_hppa_hash_table(p) \
  549.   ((struct elf32_hppa_link_hash_table *) ((p)->hash))
  550.  
  551.  
  552. /* Extract specific argument location bits for WHICH from
  553.    the full argument location in AR.  */
  554. #define EXTRACT_ARBITS(ar, which) ((ar) >> (8 - ((which) * 2))) & 3
  555.  
  556. /* Assorted hash table functions.  */
  557.  
  558. /* Initialize an entry in the stub hash table.  */
  559.  
  560. static struct bfd_hash_entry *
  561. elf32_hppa_stub_hash_newfunc (entry, table, string)
  562.      struct bfd_hash_entry *entry;
  563.      struct bfd_hash_table *table;
  564.      const char *string;
  565. {
  566.   struct elf32_hppa_stub_hash_entry *ret;
  567.  
  568.   ret = (struct elf32_hppa_stub_hash_entry *) entry;
  569.  
  570.   /* Allocate the structure if it has not already been allocated by a
  571.      subclass.  */
  572.   if (ret == NULL)
  573.     ret = ((struct elf32_hppa_stub_hash_entry *)
  574.        bfd_hash_allocate (table,
  575.                   sizeof (struct elf32_hppa_stub_hash_entry)));
  576.   if (ret == NULL)
  577.     {
  578.       bfd_set_error (bfd_error_no_memory);
  579.       return NULL;
  580.     }
  581.  
  582.   /* Call the allocation method of the superclass.  */
  583.   ret = ((struct elf32_hppa_stub_hash_entry *)
  584.      bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  585.  
  586.   if (ret)
  587.     {
  588.       /* Initialize the local fields.  */
  589.       ret->offset = 0;
  590.       ret->target_value = 0;
  591.       ret->target_section = NULL;
  592.     }
  593.  
  594.   return (struct bfd_hash_entry *) ret;
  595. }
  596.  
  597. /* Initialize a stub hash table.  */
  598.  
  599. static boolean
  600. elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
  601.      struct elf32_hppa_stub_hash_table *table;
  602.      bfd *stub_bfd;
  603.      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
  604.                         struct bfd_hash_table *,
  605.                         const char *));
  606. {
  607.   table->offset = 0;
  608.   table->location = 0;
  609.   table->stub_bfd = stub_bfd;
  610.   return (bfd_hash_table_init (&table->root, newfunc));
  611. }
  612.  
  613. /* Initialize an entry in the argument location hash table.  */
  614.  
  615. static struct bfd_hash_entry *
  616. elf32_hppa_args_hash_newfunc (entry, table, string)
  617.      struct bfd_hash_entry *entry;
  618.      struct bfd_hash_table *table;
  619.      const char *string;
  620. {
  621.   struct elf32_hppa_args_hash_entry *ret;
  622.  
  623.   ret = (struct elf32_hppa_args_hash_entry *) entry;
  624.  
  625.   /* Allocate the structure if it has not already been allocated by a
  626.      subclass.  */
  627.   if (ret == NULL)
  628.     ret = ((struct elf32_hppa_args_hash_entry *)
  629.        bfd_hash_allocate (table,
  630.                   sizeof (struct elf32_hppa_args_hash_entry)));
  631.   if (ret == NULL)
  632.     {
  633.       bfd_set_error (bfd_error_no_memory);
  634.       return NULL;
  635.     }
  636.  
  637.   /* Call the allocation method of the superclass.  */
  638.   ret = ((struct elf32_hppa_args_hash_entry *)
  639.      bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  640.  
  641.   /* Initialize the local fields.  */
  642.   if (ret)
  643.     ret->arg_bits = 0;
  644.  
  645.   return (struct bfd_hash_entry *) ret;
  646. }
  647.  
  648. /* Create the derived linker hash table.  The PA ELF port uses the derived
  649.    hash table to keep information specific to the PA ELF linker (without
  650.    using static variables).  */
  651.  
  652. static struct bfd_link_hash_table *
  653. elf32_hppa_link_hash_table_create (abfd)
  654.      bfd *abfd;
  655. {
  656.   struct elf32_hppa_link_hash_table *ret;
  657.  
  658.   ret = ((struct elf32_hppa_link_hash_table *)
  659.      bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table)));
  660.   if (ret == NULL)
  661.     {
  662.       bfd_set_error (bfd_error_no_memory);
  663.       return NULL;
  664.     }
  665.   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  666.                       _bfd_elf_link_hash_newfunc))
  667.     {
  668.       bfd_release (abfd, ret);
  669.       return NULL;
  670.     }
  671.   ret->stub_hash_table = NULL;
  672.   ret->args_hash_table = NULL;
  673.   ret->output_symbol_count = 0;
  674.   ret->global_value = 0;
  675.   ret->global_sym_defined = 0;
  676.  
  677.   return &ret->root.root;
  678. }
  679.  
  680. /* Relocate the given INSN given the various input parameters.
  681.  
  682.    FIXME: endianness and sizeof (long) issues abound here.  */
  683.  
  684. static unsigned long
  685. hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
  686.             r_addend, r_format, r_field, pcrel)
  687.      bfd *abfd;
  688.      asection *input_sect;
  689.      unsigned long insn;
  690.      unsigned long address;
  691.      long sym_value;
  692.      long r_addend;
  693.      unsigned long r_format;
  694.      unsigned long r_field;
  695.      unsigned long pcrel;
  696. {
  697.   unsigned char opcode = get_opcode (insn);
  698.   long constant_value;
  699.  
  700.   switch (opcode)
  701.     {
  702.     case LDO:
  703.     case LDB:
  704.     case LDH:
  705.     case LDW:
  706.     case LDWM:
  707.     case STB:
  708.     case STH:
  709.     case STW:
  710.     case STWM:
  711.     case COMICLR:
  712.     case SUBI:
  713.     case ADDIT:
  714.     case ADDI:
  715.     case LDIL:
  716.     case ADDIL:
  717.       constant_value = HPPA_R_CONSTANT (r_addend);
  718.  
  719.       if (pcrel)
  720.     sym_value -= address;
  721.  
  722.       sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
  723.       return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
  724.  
  725.     case BL:
  726.     case BE:
  727.     case BLE:
  728.       /* XXX computing constant_value is not needed??? */
  729.       constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
  730.                     (insn & 0x00001ffc) >> 2,
  731.                     insn & 1);
  732.  
  733.       constant_value = (constant_value << 15) >> 15;
  734.       if (pcrel)
  735.     {
  736.       sym_value -=
  737.         address + input_sect->output_offset
  738.         + input_sect->output_section->vma;
  739.       sym_value = hppa_field_adjust (sym_value, -8, r_field);
  740.     }
  741.       else
  742.     sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
  743.  
  744.       return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
  745.  
  746.     default:
  747.       if (opcode == 0)
  748.     {
  749.       constant_value = HPPA_R_CONSTANT (r_addend);
  750.  
  751.       if (pcrel)
  752.         sym_value -= address;
  753.  
  754.       return hppa_field_adjust (sym_value, constant_value, r_field);
  755.     }
  756.       else
  757.     abort ();
  758.     }
  759. }
  760.  
  761. /* Relocate an HPPA ELF section.  */
  762.  
  763. static boolean
  764. elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
  765.                  contents, relocs, local_syms, local_sections,
  766.                  output_names)
  767.      bfd *output_bfd;
  768.      struct bfd_link_info *info;
  769.      bfd *input_bfd;
  770.      asection *input_section;
  771.      bfd_byte *contents;
  772.      Elf_Internal_Rela *relocs;
  773.      Elf_Internal_Sym *local_syms;
  774.      asection **local_sections;
  775.      char *output_names;
  776. {
  777.   Elf_Internal_Shdr *symtab_hdr;
  778.   Elf_Internal_Rela *rel;
  779.   Elf_Internal_Rela *relend;
  780.  
  781.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  782.  
  783.   rel = relocs;
  784.   relend = relocs + input_section->reloc_count;
  785.   for (; rel < relend; rel++)
  786.     {
  787.       int r_type;
  788.       const reloc_howto_type *howto;
  789.       long r_symndx;
  790.       struct elf_link_hash_entry *h;
  791.       Elf_Internal_Sym *sym;
  792.       asection *sym_sec;
  793.       bfd_vma relocation;
  794.       bfd_reloc_status_type r;
  795.       const char *sym_name;
  796.  
  797.       r_type = ELF32_R_TYPE (rel->r_info);
  798.       if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
  799.     {
  800.       bfd_set_error (bfd_error_bad_value);
  801.       return false;
  802.     }
  803.       howto = elf_hppa_howto_table + r_type;
  804.  
  805.       r_symndx = ELF32_R_SYM (rel->r_info);
  806.  
  807.       if (info->relocateable)
  808.     {
  809.       /* This is a relocateable link.  We don't have to change
  810.          anything, unless the reloc is against a section symbol,
  811.          in which case we have to adjust according to where the
  812.          section symbol winds up in the output section.  */
  813.       if (r_symndx < symtab_hdr->sh_info)
  814.         {
  815.           sym = local_syms + r_symndx;
  816.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  817.         {
  818.           sym_sec = local_sections[r_symndx];
  819.           rel->r_addend += sym_sec->output_offset;
  820.         }
  821.         }
  822.  
  823.       continue;
  824.     }
  825.  
  826.       /* This is a final link.  */
  827.       h = NULL;
  828.       sym = NULL;
  829.       sym_sec = NULL;
  830.       if (r_symndx < symtab_hdr->sh_info)
  831.     {
  832.       sym = local_syms + r_symndx;
  833.       sym_sec = local_sections[r_symndx];
  834.       relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
  835.                ? 0 : sym->st_value)
  836.              + sym_sec->output_offset
  837.              + sym_sec->output_section->vma);
  838.     }
  839.       else
  840.     {
  841.       long indx;
  842.  
  843.       indx = r_symndx - symtab_hdr->sh_info;
  844.       h = elf_sym_hashes (input_bfd)[indx];
  845.       if (h->root.type == bfd_link_hash_defined)
  846.         {
  847.           sym_sec = h->root.u.def.section;
  848.           relocation = (h->root.u.def.value
  849.                 + sym_sec->output_offset
  850.                 + sym_sec->output_section->vma);
  851.         }
  852.       else if (h->root.type == bfd_link_hash_weak)
  853.         relocation = 0;
  854.       else
  855.         {
  856.           if (!((*info->callbacks->undefined_symbol)
  857.             (info, h->root.root.string, input_bfd,
  858.              input_section, rel->r_offset)))
  859.         return false;
  860.           relocation = 0;
  861.         }
  862.     }
  863.  
  864.       if (h != NULL)
  865.     sym_name = h->root.root.string;
  866.       else
  867.     {
  868.       sym_name = output_names + sym->st_name;
  869.       if (sym_name == NULL)
  870.         return false;
  871.       if (*sym_name == '\0')
  872.         sym_name = bfd_section_name (input_bfd, sym_sec);
  873.     }
  874.  
  875.       r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
  876.                           input_section, contents,
  877.                           rel->r_offset, relocation,
  878.                           rel->r_addend, info, sym_sec,
  879.                           sym_name, h == NULL);
  880.  
  881.       if (r != bfd_reloc_ok)
  882.     {
  883.       switch (r)
  884.         {
  885.         case bfd_reloc_dangerous:
  886.           {
  887.         /* We use this return value to indicate that we performed
  888.            a "dangerous" relocation.  This doesn't mean we did
  889.            the wrong thing, it just means there may be some cleanup
  890.            that needs to be done here.
  891.  
  892.            In particular we had to swap the last call insn and its
  893.            delay slot.  If the delay slot insn needed a relocation,
  894.            then we'll need to adjust the next relocation entry's
  895.            offset to account for the fact that the insn moved.
  896.  
  897.            This hair wouldn't be necessary if we inserted stubs
  898.            between procedures and used a "bl" to get to the stub.  */
  899.         if (rel != relend)
  900.           {
  901.             Elf_Internal_Rela *next_rel = rel + 1;
  902.  
  903.             if (rel->r_offset + 4 == next_rel->r_offset)
  904.               next_rel->r_offset -= 4;
  905.           }
  906.         break;
  907.           }
  908.         default:
  909.         case bfd_reloc_outofrange:
  910.         case bfd_reloc_overflow:
  911.           {
  912.         if (!((*info->callbacks->reloc_overflow)
  913.               (info, sym_name, howto->name, (bfd_vma) 0,
  914.             input_bfd, input_section, rel->r_offset)))
  915.           return false;
  916.           }
  917.           break;
  918.         }
  919.     }
  920.     }
  921.  
  922.   return true;
  923. }
  924.  
  925. /* Return one (or more) BFD relocations which implement the base
  926.    relocation with modifications based on format and field.  */
  927.  
  928. elf32_hppa_reloc_type **
  929. hppa_elf_gen_reloc_type (abfd, base_type, format, field)
  930.      bfd *abfd;
  931.      elf32_hppa_reloc_type base_type;
  932.      int format;
  933.      int field;
  934. {
  935.   elf32_hppa_reloc_type *finaltype;
  936.   elf32_hppa_reloc_type **final_types;
  937.  
  938.   /* Allocate slots for the BFD relocation.  */
  939.   final_types = (elf32_hppa_reloc_type **)
  940.     bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type *) * 2);
  941.   if (final_types == NULL)
  942.     return NULL;
  943.  
  944.   /* Allocate space for the relocation itself.  */
  945.   finaltype = (elf32_hppa_reloc_type *)
  946.     bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type));
  947.   if (finaltype == NULL)
  948.     return NULL;
  949.  
  950.   /* Some reasonable defaults.  */
  951.   final_types[0] = finaltype;
  952.   final_types[1] = NULL;
  953.  
  954. #define final_type finaltype[0]
  955.  
  956.   final_type = base_type;
  957.  
  958.   /* Just a tangle of nested switch statements to deal with the braindamage
  959.      that a different field selector means a completely different relocation
  960.      for PA ELF.  */
  961.   switch (base_type)
  962.     {
  963.     case R_HPPA:
  964.     case R_HPPA_ABS_CALL:
  965.       switch (format)
  966.     {
  967.     case 14:
  968.       switch (field)
  969.         {
  970.         case e_rsel:
  971.         case e_rrsel:
  972.           final_type = R_PARISC_DIR14R;
  973.           break;
  974.         case e_rtsel:
  975.           final_type = R_PARISC_DLTREL14R;
  976.           break;
  977.         case e_tsel:
  978.           final_type = R_PARISC_DLTREL14F;
  979.           break;
  980.         case e_rpsel:
  981.           final_type = R_PARISC_PLABEL14R;
  982.           break;
  983.         default:
  984.           return NULL;
  985.         }
  986.       break;
  987.  
  988.     case 17:
  989.       switch (field)
  990.         {
  991.         case e_fsel:
  992.           final_type = R_PARISC_DIR17F;
  993.           break;
  994.         case e_rsel:
  995.         case e_rrsel:
  996.           final_type = R_PARISC_DIR17R;
  997.           break;
  998.         default:
  999.           return NULL;
  1000.         }
  1001.       break;
  1002.  
  1003.     case 21:
  1004.       switch (field)
  1005.         {
  1006.         case e_lsel:
  1007.         case e_lrsel:
  1008.           final_type = R_PARISC_DIR21L;
  1009.           break;
  1010.         case e_ltsel:
  1011.           final_type = R_PARISC_DLTREL21L;
  1012.           break;
  1013.         case e_lpsel:
  1014.           final_type = R_PARISC_PLABEL21L;
  1015.           break;
  1016.         default:
  1017.           return NULL;
  1018.         }
  1019.       break;
  1020.  
  1021.     case 32:
  1022.       switch (field)
  1023.         {
  1024.         case e_fsel:
  1025.           final_type = R_PARISC_DIR32;
  1026.           break;
  1027.         case e_psel:
  1028.           final_type = R_PARISC_PLABEL32;
  1029.           break;
  1030.         default:
  1031.           return NULL;
  1032.         }
  1033.       break;
  1034.  
  1035.     default:
  1036.       return NULL;
  1037.     }
  1038.       break;
  1039.  
  1040.  
  1041.     case R_HPPA_GOTOFF:
  1042.       switch (format)
  1043.     {
  1044.     case 14:
  1045.       switch (field)
  1046.         {
  1047.         case e_rsel:
  1048.         case e_rrsel:
  1049.           final_type = R_PARISC_DPREL14R;
  1050.           break;
  1051.         case e_fsel:
  1052.           final_type = R_PARISC_DPREL14F;
  1053.           break;
  1054.         default:
  1055.           return NULL;
  1056.         }
  1057.       break;
  1058.  
  1059.     case 21:
  1060.       switch (field)
  1061.         {
  1062.         case e_lrsel:
  1063.         case e_lsel:
  1064.           final_type = R_PARISC_DPREL21L;
  1065.           break;
  1066.         default:
  1067.           return NULL;
  1068.         }
  1069.       break;
  1070.  
  1071.     default:
  1072.       return NULL;
  1073.     }
  1074.       break;
  1075.  
  1076.  
  1077.     case R_HPPA_PCREL_CALL:
  1078.       switch (format)
  1079.     {
  1080.     case 14:
  1081.       switch (field)
  1082.         {
  1083.         case e_rsel:
  1084.         case e_rrsel:
  1085.           final_type = R_PARISC_PCREL14R;
  1086.           break;
  1087.         case e_fsel:
  1088.           final_type = R_PARISC_PCREL14F;
  1089.           break;
  1090.         default:
  1091.           return NULL;
  1092.         }
  1093.       break;
  1094.  
  1095.     case 17:
  1096.       switch (field)
  1097.         {
  1098.         case e_rsel:
  1099.         case e_rrsel:
  1100.           final_type = R_PARISC_PCREL17R;
  1101.           break;
  1102.         case e_fsel:
  1103.           final_type = R_PARISC_PCREL17F;
  1104.           break;
  1105.         default:
  1106.           return NULL;
  1107.         }
  1108.       break;
  1109.  
  1110.     case 21:
  1111.       switch (field)
  1112.         {
  1113.         case e_lsel:
  1114.         case e_lrsel:
  1115.           final_type = R_PARISC_PCREL21L;
  1116.           break;
  1117.         default:
  1118.           return NULL;
  1119.         }
  1120.       break;
  1121.  
  1122.     default:
  1123.       return NULL;
  1124.     }
  1125.       break;
  1126.  
  1127.     default:
  1128.       return NULL;
  1129.     }
  1130.  
  1131.   return final_types;
  1132. }
  1133.  
  1134. #undef final_type
  1135.  
  1136. /* Set the contents of a particular section at a particular location.  */
  1137.  
  1138. static boolean
  1139. elf32_hppa_set_section_contents (abfd, section, location, offset, count)
  1140.      bfd *abfd;
  1141.      sec_ptr section;
  1142.      PTR location;
  1143.      file_ptr offset;
  1144.      bfd_size_type count;
  1145. {
  1146.   /* Ignore write requests for the symbol extension section until we've
  1147.      had the chance to rebuild it ourselves.  */
  1148.   if (!strcmp (section->name, ".PARISC.symextn") && !symext_chain_size)
  1149.     return true;
  1150.   else
  1151.     return bfd_elf32_set_section_contents (abfd, section, location,
  1152.                        offset, count);
  1153. }
  1154.  
  1155. /* Translate from an elf into field into a howto relocation pointer.  */
  1156.  
  1157. static void
  1158. elf_info_to_howto (abfd, cache_ptr, dst)
  1159.      bfd *abfd;
  1160.      arelent *cache_ptr;
  1161.      Elf32_Internal_Rela *dst;
  1162. {
  1163.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_PARISC_UNIMPLEMENTED);
  1164.   cache_ptr->howto = &elf_hppa_howto_table[ELF32_R_TYPE (dst->r_info)];
  1165. }
  1166.  
  1167.  
  1168. /* Actually perform a relocation.  NOTE this is (mostly) superceeded
  1169.    by elf32_hppa_bfd_final_link_relocate which is called by the new
  1170.    fast linker.  */
  1171.  
  1172. static bfd_reloc_status_type
  1173. hppa_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
  1174.         error_message)
  1175.      bfd *abfd;
  1176.      arelent *reloc_entry;
  1177.      asymbol *symbol_in;
  1178.      PTR data;
  1179.      asection *input_section;
  1180.      bfd *output_bfd;
  1181.      char **error_message;
  1182. {
  1183.   /* It is no longer valid to call hppa_elf_reloc when creating
  1184.      a final executable.  */
  1185.   if (output_bfd)
  1186.     {
  1187.       reloc_entry->address += input_section->output_offset;
  1188.  
  1189.       /* Work around lossage in generic elf code to write relocations.
  1190.      (maps different section symbols into the same symbol index).  */
  1191.       if ((symbol_in->flags & BSF_SECTION_SYM)
  1192.       && symbol_in->section)
  1193.     reloc_entry->addend += symbol_in->section->output_offset;
  1194.       return bfd_reloc_ok;
  1195.     }
  1196.   else
  1197.     {
  1198.       *error_message = (char *) "Unsupported call to hppa_elf_reloc";
  1199.       return bfd_reloc_notsupported;
  1200.     }
  1201. }
  1202.  
  1203. /* Actually perform a relocation as part of a final link.  This can get
  1204.    rather hairy when linker stubs are needed.  */
  1205.  
  1206. static bfd_reloc_status_type
  1207. elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
  1208.                     input_section, contents, offset, value,
  1209.                     addend, info, sym_sec, sym_name, is_local)
  1210.      const reloc_howto_type *howto;
  1211.      bfd *input_bfd;
  1212.      bfd *output_bfd;
  1213.      asection *input_section;
  1214.      bfd_byte *contents;
  1215.      bfd_vma offset;
  1216.      bfd_vma value;
  1217.      bfd_vma addend;
  1218.      struct bfd_link_info *info;
  1219.      asection *sym_sec;
  1220.      const char *sym_name;
  1221.      int is_local;
  1222. {
  1223.   unsigned long insn;
  1224.   unsigned long r_type = howto->type;
  1225.   unsigned long r_format = howto->bitsize;
  1226.   unsigned long r_field = e_fsel;
  1227.   bfd_byte *hit_data = contents + offset + input_section->vma;
  1228.   boolean r_pcrel = howto->pc_relative;
  1229.  
  1230.   insn = bfd_get_32 (input_bfd, hit_data);
  1231.  
  1232.   /* Make sure we have a value for $global$.  FIXME isn't this effectively
  1233.      just like the gp pointer on MIPS?  Can we use those routines for this
  1234.      purpose?  */
  1235.   if (!elf32_hppa_hash_table (info)->global_sym_defined)
  1236.     {
  1237.       struct elf_link_hash_entry *h;
  1238.       asection *sec;
  1239.  
  1240.       h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
  1241.                  false, false);
  1242.  
  1243.       /* If there isn't a $global$, then we're in deep trouble.  */
  1244.       if (h == NULL)
  1245.     return bfd_reloc_notsupported;
  1246.  
  1247.       sec = h->root.u.def.section;
  1248.       elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
  1249.                             + sec->vma
  1250.                             + sec->output_offset);
  1251.       elf32_hppa_hash_table (info)->global_sym_defined = 1;
  1252.     }
  1253.  
  1254.   switch (r_type)
  1255.     {
  1256.     case R_PARISC_NONE:
  1257.       break;
  1258.  
  1259.     case R_PARISC_DIR32:
  1260.     case R_PARISC_DIR17F:
  1261.     case R_PARISC_PCREL17C:
  1262.       r_field = e_fsel;
  1263.       goto do_basic_type_1;
  1264.     case R_PARISC_DIR21L:
  1265.     case R_PARISC_PCREL21L:
  1266.       r_field = e_lrsel;
  1267.       goto do_basic_type_1;
  1268.     case R_PARISC_DIR17R:
  1269.     case R_PARISC_PCREL17R:
  1270.     case R_PARISC_DIR14R:
  1271.     case R_PARISC_PCREL14R:
  1272.       r_field = e_rrsel;
  1273.       goto do_basic_type_1;
  1274.  
  1275.     /* For all the DP relative relocations, we need to examine the symbol's
  1276.        section.  If it's a code section, then "data pointer relative" makes
  1277.        no sense.  In that case we don't adjust the "value", and for 21 bit
  1278.        addil instructions, we change the source addend register from %dp to
  1279.        %r0.  */
  1280.     case R_PARISC_DPREL21L:
  1281.       r_field = e_lrsel;
  1282.       if (sym_sec->flags & SEC_CODE)
  1283.     insn &= ~0x03e00000;
  1284.       else
  1285.     value -= elf32_hppa_hash_table (info)->global_value;
  1286.       goto do_basic_type_1;
  1287.     case R_PARISC_DPREL14R:
  1288.       r_field = e_rrsel;
  1289.       if ((sym_sec->flags & SEC_CODE) == 0)
  1290.     value -= elf32_hppa_hash_table (info)->global_value;
  1291.       goto do_basic_type_1;
  1292.     case R_PARISC_DPREL14F:
  1293.       r_field = e_fsel;
  1294.       if ((sym_sec->flags & SEC_CODE) == 0)
  1295.     value -= elf32_hppa_hash_table (info)->global_value;
  1296.       goto do_basic_type_1;
  1297.  
  1298.     /* These cases are separate as they may involve a lot more work
  1299.        to deal with linker stubs.  */
  1300.     case R_PARISC_PLABEL32:
  1301.     case R_PARISC_PLABEL21L:
  1302.     case R_PARISC_PLABEL14R:
  1303.     case R_PARISC_PCREL17F:
  1304.       {
  1305.     bfd_vma location;
  1306.     unsigned int len, caller_args, callee_args;
  1307.     arg_reloc_type arg_reloc_types[5];
  1308.     struct elf32_hppa_args_hash_table *args_hash_table;
  1309.     struct elf32_hppa_args_hash_entry *args_hash;
  1310.     char *new_name, *stub_name;
  1311.  
  1312.     /* Get the field selector right.  We'll need it in a minute.  */
  1313.     if (r_type == R_PARISC_PCREL17F
  1314.         || r_type == R_PARISC_PLABEL32)
  1315.       r_field = e_fsel;
  1316.     else if (r_type == R_PARISC_PLABEL21L)
  1317.       r_field = e_lrsel;
  1318.     else if (r_type == R_PARISC_PLABEL14R)
  1319.       r_field = e_rrsel;
  1320.  
  1321.     /* Find out where we are and where we're going.  */
  1322.     location = (offset +
  1323.             input_section->output_offset +
  1324.             input_section->output_section->vma);
  1325.  
  1326.     /* Now look for the argument relocation bits associated with the
  1327.        target.  */
  1328.     len = strlen (sym_name) + 1;
  1329.     if (is_local)
  1330.       len += 9;
  1331.     new_name = malloc (len);
  1332.     if (!new_name)
  1333.       {
  1334.         bfd_set_error (bfd_error_no_memory);
  1335.         return bfd_reloc_notsupported;
  1336.       }
  1337.     strcpy (new_name, sym_name);
  1338.  
  1339.     /* Local symbols have unique IDs.  */
  1340.     if (is_local)
  1341.       sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
  1342.  
  1343.     args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
  1344.  
  1345.     args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  1346.                          new_name, false, false);
  1347.     if (args_hash == NULL)
  1348.       callee_args = 0;
  1349.     else
  1350.       callee_args = args_hash->arg_bits;
  1351.  
  1352.     /* If this is a CALL relocation, then get the caller's bits
  1353.        from the addend.  Else use the magic 0x155 value for PLABELS.
  1354.  
  1355.        Also we don't care about the destination (value) for PLABELS.  */
  1356.     if (r_type == R_PARISC_PCREL17F)
  1357.       caller_args = HPPA_R_ARG_RELOC (addend);
  1358.     else
  1359.       {
  1360.         caller_args = 0x155;
  1361.         location = value;
  1362.       }
  1363.  
  1364.     /* Any kind of linker stub needed?  */
  1365.     if (((int)(value - location) > 0x3ffff)
  1366.         || ((int)(value - location) < (int)0xfffc0000)
  1367.         || elf32_hppa_arg_reloc_needed (caller_args, callee_args,
  1368.                         arg_reloc_types))
  1369.       {
  1370.         struct elf32_hppa_stub_hash_table *stub_hash_table;
  1371.         struct elf32_hppa_stub_hash_entry *stub_hash;
  1372.         asection *stub_section;
  1373.  
  1374.         /* Build a name for the stub.  */
  1375.  
  1376.         len = strlen (new_name);
  1377.         len += 23;
  1378.         stub_name = malloc (len);
  1379.         if (!stub_name)
  1380.           {
  1381.         bfd_set_error (bfd_error_no_memory);
  1382.         return bfd_reloc_notsupported;
  1383.           }
  1384.         elf32_hppa_name_of_stub (caller_args, callee_args,
  1385.                      location, value, stub_name);
  1386.         strcat (stub_name, new_name);
  1387.         free (new_name);
  1388.  
  1389.         stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
  1390.  
  1391.         stub_hash
  1392.           = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
  1393.                          false, false);
  1394.  
  1395.         /* We're done with that name.  */
  1396.         free (stub_name);
  1397.  
  1398.         /* The stub BFD only has one section.  */
  1399.         stub_section = stub_hash_table->stub_bfd->sections;
  1400.  
  1401.         if (stub_hash != NULL)
  1402.           {
  1403.  
  1404.         if (r_type == R_PARISC_PCREL17F)
  1405.           {
  1406.             unsigned long delay_insn;
  1407.             unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
  1408.  
  1409.             /* We'll need to peek at the next insn.  */
  1410.             delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
  1411.             opcode = get_opcode (delay_insn);
  1412.  
  1413.             /* We also need to know the return register for this
  1414.                call.  */
  1415.             rtn_reg = (insn & 0x03e00000) >> 21;
  1416.  
  1417.             ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
  1418.             ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
  1419.  
  1420.             /* Munge up the value and other parameters for
  1421.                hppa_elf_relocate_insn.  */
  1422.  
  1423.             value = (stub_hash->offset
  1424.                  + stub_section->output_offset
  1425.                  + stub_section->output_section->vma);
  1426.  
  1427.             r_format = 17;
  1428.             r_field = e_fsel;
  1429.             r_pcrel = 0;
  1430.             addend = 0;
  1431.  
  1432.             /* We need to peek at the delay insn and determine if
  1433.                we'll need to swap the branch and its delay insn.  */
  1434.             if ((insn & 2)
  1435.             || (opcode == LDO
  1436.                 && ldo_target_reg == rtn_reg)
  1437.             || (delay_insn == 0x08000240))
  1438.               {
  1439.             /* No need to swap the branch and its delay slot, but
  1440.                we do need to make sure to jump past the return
  1441.                pointer update in the stub.  */
  1442.             value += 4;
  1443.  
  1444.             /* If the delay insn does a return pointer adjustment,
  1445.                then we have to make sure it stays valid.  */
  1446.             if (opcode == LDO
  1447.                 && ldo_target_reg == rtn_reg)
  1448.               {
  1449.                 delay_insn &= 0xfc00ffff;
  1450.                 delay_insn |= ((31 << 21) | (31 << 16));
  1451.                 bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
  1452.               }
  1453.             /* Use a BLE to reach the stub.  */
  1454.             insn = BLE_SR4_R0;
  1455.               }
  1456.             else
  1457.               {
  1458.             /* Wonderful, we have to swap the call insn and its
  1459.                delay slot.  */
  1460.             bfd_put_32 (input_bfd, delay_insn, hit_data);
  1461.             /* Use a BLE,n to reach the stub.  */
  1462.             insn = (BLE_SR4_R0 | 0x2);
  1463.             bfd_put_32 (input_bfd, insn, hit_data + 4);
  1464.             insn = hppa_elf_relocate_insn (input_bfd,
  1465.                                input_section,
  1466.                                insn, offset + 4,
  1467.                                value, addend,
  1468.                                r_format, r_field,
  1469.                                r_pcrel);
  1470.             /* Update the instruction word.  */
  1471.             bfd_put_32 (input_bfd, insn, hit_data + 4);
  1472.             return bfd_reloc_dangerous;
  1473.               }
  1474.           }
  1475.         else
  1476.           {
  1477.             /* PLABEL stuff is easy.  */
  1478.  
  1479.             value = (stub_hash->offset
  1480.                  + stub_section->output_offset
  1481.                  + stub_section->output_section->vma);
  1482.             /* We don't need the RP adjustment for PLABELs.  */
  1483.             value += 4;
  1484.             if (r_type == R_PARISC_PLABEL32)
  1485.               r_format = 32;
  1486.             else if (r_type == R_PARISC_PLABEL21L)
  1487.               r_format = 21;
  1488.             else if (r_type == R_PARISC_PLABEL14R)
  1489.               r_format = 14;
  1490.  
  1491.             r_pcrel = 0;
  1492.             addend = 0;
  1493.           }
  1494.         }
  1495.           else
  1496.         return bfd_reloc_notsupported;
  1497.       }
  1498.     goto do_basic_type_1;
  1499.       }
  1500.  
  1501. do_basic_type_1:
  1502.       insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
  1503.                      offset, value, addend, r_format,
  1504.                      r_field, r_pcrel);
  1505.       break;
  1506.  
  1507.     /* Something we don't know how to handle.  */
  1508.     default:
  1509.       return bfd_reloc_notsupported;
  1510.     }
  1511.  
  1512.   /* Update the instruction word.  */
  1513.   bfd_put_32 (input_bfd, insn, hit_data);
  1514.   return (bfd_reloc_ok);
  1515. }
  1516.  
  1517. /* Return the address of the howto table entry to perform the CODE
  1518.    relocation for an ARCH machine.  */
  1519.  
  1520. static CONST reloc_howto_type *
  1521. elf_hppa_reloc_type_lookup (abfd, code)
  1522.      bfd *abfd;
  1523.      bfd_reloc_code_real_type code;
  1524. {
  1525.   if ((int) code < (int) R_PARISC_UNIMPLEMENTED)
  1526.     {
  1527.       BFD_ASSERT ((int) elf_hppa_howto_table[(int) code].type == (int) code);
  1528.       return &elf_hppa_howto_table[(int) code];
  1529.     }
  1530.   return NULL;
  1531. }
  1532.  
  1533. /* Return true if SYM represents a local label symbol.  */
  1534.  
  1535. static boolean
  1536. hppa_elf_is_local_label (abfd, sym)
  1537.      bfd *abfd;
  1538.      asymbol *sym;
  1539. {
  1540.   return (sym->name[0] == 'L' && sym->name[1] == '$');
  1541. }
  1542.  
  1543. /* Do any backend specific processing when beginning to write an object
  1544.    file.  For PA ELF we need to determine the size of the symbol extension
  1545.    section *before* any other output processing happens.  */
  1546.  
  1547. static void
  1548. elf32_hppa_backend_begin_write_processing (abfd, info)
  1549.      bfd *abfd;
  1550.      struct bfd_link_info *info;
  1551. {
  1552.   unsigned int i;
  1553.   asection *symextn_sec;
  1554.  
  1555.   /* Size up the symbol extension section.  */
  1556.   if ((abfd->outsymbols == NULL
  1557.        && info == NULL)
  1558.       || symext_chain_size != 0)
  1559.     return;
  1560.  
  1561.   if (info == NULL)
  1562.     {
  1563.       /* We were not called from the BFD ELF linker code, so we need
  1564.      to examine the output BFD's outsymbols.
  1565.  
  1566.      Note we can not build the symbol extensions now as the symbol
  1567.      map hasn't been set up.  */
  1568.       for (i = 0; i < abfd->symcount; i++)
  1569.     {
  1570.       elf_symbol_type *symbol = (elf_symbol_type *)abfd->outsymbols[i];
  1571.  
  1572.       /* Only functions ever need an entry in the symbol extension
  1573.          section.  */
  1574.       if (!(symbol->symbol.flags & BSF_FUNCTION))
  1575.         continue;
  1576.  
  1577.       /* And only if they specify the locations of their arguments.  */
  1578.       if (symbol->tc_data.hppa_arg_reloc == 0)
  1579.         continue;
  1580.  
  1581.       /* Yup.  This function symbol needs an entry.  */
  1582.       symext_chain_size += 2 * sizeof (symext_entryS);
  1583.     }
  1584.     }
  1585.   else
  1586.     {
  1587.       struct elf32_hppa_args_hash_table *table;
  1588.       table = elf32_hppa_hash_table (info)->args_hash_table;
  1589.  
  1590.       /* Determine the size of the symbol extension section.  */
  1591.       elf32_hppa_args_hash_traverse (table,
  1592.                      elf32_hppa_size_symext,
  1593.                      &symext_chain_size);
  1594.     }
  1595.  
  1596.   /* Now create the section and set its size.  We'll fill in the
  1597.      contents later.  */
  1598.   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
  1599.   if (symextn_sec == NULL)
  1600.     symextn_sec = bfd_make_section (abfd, SYMEXTN_SECTION_NAME);
  1601.  
  1602.   bfd_set_section_flags (abfd, symextn_sec,
  1603.              SEC_LOAD | SEC_HAS_CONTENTS | SEC_DATA);
  1604.   symextn_sec->output_section = symextn_sec;
  1605.   symextn_sec->output_offset = 0;
  1606.   bfd_set_section_alignment (abfd, symextn_sec, 2);
  1607.   bfd_set_section_size (abfd, symextn_sec, symext_chain_size);
  1608. }
  1609.  
  1610. /* Called for each entry in the args location hash table.  For each
  1611.    entry we bump the size pointer by 2 records (16 bytes).  */
  1612.  
  1613. static boolean
  1614. elf32_hppa_size_symext (gen_entry, in_args)
  1615.      struct bfd_hash_entry *gen_entry;
  1616.      PTR in_args;
  1617. {
  1618.   unsigned int *sizep = (unsigned int *)in_args;
  1619.  
  1620.   *sizep += 2 * sizeof (symext_entryS);
  1621.   return true;
  1622. }
  1623.  
  1624. /* Backend routine called by the linker for each output symbol.
  1625.  
  1626.    For PA ELF we use this opportunity to add an appropriate entry
  1627.    to the symbol extension chain for function symbols.  */
  1628.  
  1629. static boolean
  1630. elf32_hppa_link_output_symbol_hook (abfd, info, name, sym, section)
  1631.      bfd *abfd;
  1632.      struct bfd_link_info *info;
  1633.      const char *name;
  1634.      Elf_Internal_Sym *sym;
  1635.      asection *section;
  1636. {
  1637.   char *new_name;
  1638.   unsigned int len, index;
  1639.   struct elf32_hppa_args_hash_table *args_hash_table;
  1640.   struct elf32_hppa_args_hash_entry *args_hash;
  1641.  
  1642.   index = elf32_hppa_hash_table (info)->output_symbol_count++;
  1643.  
  1644.   /* We need to look up this symbol in the args hash table to see if
  1645.      it has argument relocation bits.  */
  1646.   if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
  1647.     return true;
  1648.  
  1649.   /* We know it's a function symbol of some kind.  */
  1650.   len = strlen (name) + 1;
  1651.   if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  1652.     len += 9;
  1653.  
  1654.   new_name = malloc (len);
  1655.   if (new_name == NULL)
  1656.     {
  1657.       bfd_set_error (bfd_error_no_memory);
  1658.       return false;
  1659.     }
  1660.  
  1661.   strcpy (new_name, name);
  1662.   if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  1663.     sprintf (new_name + len - 10, "_%08x", (int)section);
  1664.  
  1665.   /* Now that we have the unique name, we can look it up in the
  1666.      args hash table.  */
  1667.   args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
  1668.   args_hash = elf32_hppa_args_hash_lookup (args_hash_table, new_name,
  1669.                        false, false);
  1670.   free (new_name);
  1671.   if (args_hash == NULL)
  1672.     return true;
  1673.  
  1674.   /* We know this symbol has arg reloc bits.  */
  1675.   add_entry_to_symext_chain (abfd, args_hash->arg_bits,
  1676.                  index, &symext_rootP, &symext_lastP);
  1677.   return true;
  1678. }
  1679.  
  1680. /* Perform any processing needed late in the object file writing process.
  1681.    For PA ELF we build and set the contents of the symbol extension
  1682.    section.  */
  1683.  
  1684. static void
  1685. elf32_hppa_backend_final_write_processing (abfd, info)
  1686.      bfd *abfd;
  1687.      struct bfd_link_info *info;
  1688. {
  1689.   asection *symextn_sec;
  1690.   unsigned int i, *symtab_map = (int *) elf_sym_extra (abfd);
  1691.  
  1692.   /* Now build the symbol extension section.  */
  1693.   if (symext_chain_size == 0)
  1694.     return;
  1695.  
  1696.   if (info == NULL)
  1697.     {
  1698.       /* We were not called from the backend linker, so we still need
  1699.      to build the symbol extension chain.
  1700.  
  1701.          Look at each symbol, adding the appropriate information to the
  1702.      symbol extension section list as necessary.  */
  1703.       for (i = 0; i < abfd->symcount; i++)
  1704.     {
  1705.       elf_symbol_type *symbol = (elf_symbol_type *) abfd->outsymbols[i];
  1706.  
  1707.       /* Only functions ever need an entry in the symbol extension
  1708.          section.  */
  1709.       if (!(symbol->symbol.flags & BSF_FUNCTION))
  1710.         continue;
  1711.  
  1712.       /* And only if they specify the locations of their arguments.  */
  1713.       if (symbol->tc_data.hppa_arg_reloc == 0)
  1714.         continue;
  1715.  
  1716.       /* Add this symbol's information to the chain.  */
  1717.       add_entry_to_symext_chain (abfd, symbol->tc_data.hppa_arg_reloc,
  1718.                      symtab_map[i], &symext_rootP,
  1719.                      &symext_lastP);
  1720.     }
  1721.     }
  1722.  
  1723.   /* Now fill in the contents of the symbol extension section.  */
  1724.   elf_hppa_tc_make_sections (abfd, symext_rootP);
  1725.  
  1726.   /* And attach that as the section's contents.  */
  1727.   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
  1728.   if (symextn_sec == (asection *) 0)
  1729.     abort();
  1730.  
  1731.   symextn_sec->contents = (void *)symextn_contents;
  1732.  
  1733.   bfd_set_section_contents (abfd, symextn_sec, symextn_sec->contents,
  1734.                 symextn_sec->output_offset, symextn_sec->_raw_size);
  1735. }
  1736.  
  1737. /* Update the symbol extention chain to include the symbol pointed to
  1738.    by SYMBOLP if SYMBOLP is a function symbol.  Used internally and by GAS.  */
  1739.  
  1740. static void
  1741. add_entry_to_symext_chain (abfd, arg_reloc, sym_idx, symext_root, symext_last)
  1742.      bfd *abfd;
  1743.      unsigned int arg_reloc;
  1744.      unsigned int sym_idx;
  1745.      symext_chainS **symext_root;
  1746.      symext_chainS **symext_last;
  1747. {
  1748.   symext_chainS *symextP;
  1749.  
  1750.   /* Allocate memory and initialize this entry.  */
  1751.   symextP = (symext_chainS *) bfd_alloc (abfd, sizeof (symext_chainS) * 2);
  1752.   if (!symextP)
  1753.     {
  1754.       bfd_set_error (bfd_error_no_memory);
  1755.       abort();            /* FIXME */
  1756.     }
  1757.  
  1758.   symextP[0].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_SYMNDX, sym_idx);
  1759.   symextP[0].next = &symextP[1];
  1760.  
  1761.   symextP[1].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_ARG_RELOC, arg_reloc);
  1762.   symextP[1].next = NULL;
  1763.  
  1764.   /* Now update the chain itself so it can be walked later to build
  1765.      the symbol extension section.  */
  1766.   if (*symext_root == NULL)
  1767.     {
  1768.       *symext_root = &symextP[0];
  1769.       *symext_last = &symextP[1];
  1770.     }
  1771.   else
  1772.     {
  1773.       (*symext_last)->next = &symextP[0];
  1774.       *symext_last = &symextP[1];
  1775.     }
  1776. }
  1777.  
  1778. /* Build the symbol extension section.  */
  1779.  
  1780. static void
  1781. elf_hppa_tc_make_sections (abfd, symext_root)
  1782.      bfd *abfd;
  1783.      symext_chainS *symext_root;
  1784. {
  1785.   symext_chainS *symextP;
  1786.   unsigned int i;
  1787.   asection *symextn_sec;
  1788.  
  1789.   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
  1790.  
  1791.   /* Grab some memory for the contents of the symbol extension section
  1792.      itself.  */
  1793.   symextn_contents = (symext_entryS *) bfd_zalloc (abfd,
  1794.                            symextn_sec->_raw_size);
  1795.   if (!symextn_contents)
  1796.     {
  1797.       bfd_set_error (bfd_error_no_memory);
  1798.       abort();            /* FIXME */
  1799.     }
  1800.  
  1801.   /* Fill in the contents of the symbol extension chain.  */
  1802.   for (i = 0, symextP = symext_root; symextP; symextP = symextP->next, ++i)
  1803.     symextn_contents[i] = symextP->entry;
  1804.  
  1805.   return;
  1806. }
  1807.  
  1808. /* Do some PA ELF specific work after reading in the symbol table.
  1809.    In particular attach the argument relocation from the
  1810.    symbol extension section to the appropriate symbols.  */
  1811.  
  1812. static boolean
  1813. elf32_hppa_backend_symbol_table_processing (abfd, esyms,symcnt)
  1814.      bfd *abfd;
  1815.      elf_symbol_type *esyms;
  1816.      int symcnt;
  1817. {
  1818.   Elf32_Internal_Shdr *symextn_hdr =
  1819.     bfd_elf_find_section (abfd, SYMEXTN_SECTION_NAME);
  1820.   unsigned int i, current_sym_idx = 0;
  1821.  
  1822.   /* If no symbol extension existed, then all symbol extension information
  1823.      is assumed to be zero.  */
  1824.   if (symextn_hdr == NULL)
  1825.     {
  1826.       for (i = 0; i < symcnt; i++)
  1827.     esyms[i].tc_data.hppa_arg_reloc = 0;
  1828.       return (true);
  1829.     }
  1830.  
  1831.   /* FIXME:  Why not use bfd_get_section_contents here?  Also should give
  1832.      memory back when we're done.  */
  1833.   /* Allocate a buffer of the appropriate size for the symextn section.  */
  1834.   symextn_hdr->contents = bfd_zalloc(abfd,symextn_hdr->sh_size);
  1835.   if (!symextn_hdr->contents)
  1836.     {
  1837.       bfd_set_error (bfd_error_no_memory);
  1838.       return false;
  1839.     }
  1840.   symextn_hdr->size = symextn_hdr->sh_size;
  1841.  
  1842.   /* Read in the symextn section.  */
  1843.   if (bfd_seek (abfd, symextn_hdr->sh_offset, SEEK_SET) == -1)
  1844.     return false;
  1845.   if (bfd_read ((PTR) symextn_hdr->contents, 1, symextn_hdr->size, abfd)
  1846.       != symextn_hdr->size)
  1847.     return false;
  1848.  
  1849.   /* Parse entries in the symbol extension section, updating the symtab
  1850.      entries as we go */
  1851.   for (i = 0; i < symextn_hdr->size / sizeof(symext_entryS); i++)
  1852.     {
  1853.       symext_entryS *seP = ((symext_entryS *)symextn_hdr->contents) + i;
  1854.       unsigned int se_value = ELF32_PARISC_SX_VAL (*seP);
  1855.       unsigned int se_type = ELF32_PARISC_SX_TYPE (*seP);
  1856.  
  1857.       switch (se_type)
  1858.     {
  1859.     case PARISC_SXT_NULL:
  1860.       break;
  1861.  
  1862.     case PARISC_SXT_SYMNDX:
  1863.       if (se_value >= symcnt)
  1864.         {
  1865.           bfd_set_error (bfd_error_bad_value);
  1866.           return (false);
  1867.         }
  1868.       current_sym_idx = se_value - 1;
  1869.       break;
  1870.  
  1871.     case PARISC_SXT_ARG_RELOC:
  1872.       esyms[current_sym_idx].tc_data.hppa_arg_reloc = se_value;
  1873.       break;
  1874.  
  1875.     default:
  1876.       bfd_set_error (bfd_error_bad_value);
  1877.       return (false);
  1878.     }
  1879.     }
  1880.   return (true);
  1881. }
  1882.  
  1883. /* Read and attach the symbol extension information for the symbols
  1884.    in INPUT_BFD to the argument location hash table.  Handle locals
  1885.    if DO_LOCALS is true; likewise for globals when DO_GLOBALS is true.  */
  1886.  
  1887. static boolean
  1888. elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
  1889.                  local_syms, do_locals, do_globals)
  1890.      bfd *input_bfd;
  1891.      Elf_Internal_Shdr *symtab_hdr;
  1892.      struct elf32_hppa_args_hash_table *args_hash_table;
  1893.      Elf_Internal_Sym *local_syms;
  1894.      boolean do_locals;
  1895.      boolean do_globals;
  1896. {
  1897.   asection *symextn_sec;
  1898.   symextn_entry *contents;
  1899.   unsigned int i, n_entries, current_index = 0;
  1900.  
  1901.   /* Get the symbol extension section for this BFD.  If no section exists
  1902.      then there's nothing to do.  Likewise if the section exists, but
  1903.      has no contents.  */
  1904.   symextn_sec = bfd_get_section_by_name (input_bfd, SYMEXTN_SECTION_NAME);
  1905.   if (symextn_sec == NULL
  1906.       || symextn_sec->_raw_size == 0)
  1907.     return true;
  1908.  
  1909.   contents = (symextn_entry *) malloc (symextn_sec->_raw_size);
  1910.   if (contents == NULL)
  1911.     {
  1912.       bfd_set_error (bfd_error_no_memory);
  1913.       return false;
  1914.     }
  1915.  
  1916.   if (bfd_get_section_contents (input_bfd, symextn_sec, contents,
  1917.                 0, symextn_sec->_raw_size) == false)
  1918.     {
  1919.       free (contents);
  1920.       return false;
  1921.     }
  1922.  
  1923.   n_entries = symextn_sec->_raw_size / sizeof (symextn_entry);
  1924.   for (i = 0; i < n_entries; i++)
  1925.     {
  1926.       symextn_entry entry = contents[i];
  1927.       unsigned int value = ELF32_PARISC_SX_VAL (entry);
  1928.       unsigned int type = ELF32_PARISC_SX_TYPE (entry);
  1929.       struct elf32_hppa_args_hash_entry *args_hash;
  1930.  
  1931.       switch (type)
  1932.     {
  1933.     case PARISC_SXT_NULL:
  1934.       break;
  1935.  
  1936.     case PARISC_SXT_SYMNDX:
  1937.       if (value >= symtab_hdr->sh_size / sizeof (Elf32_External_Sym))
  1938.         {
  1939.           bfd_set_error (bfd_error_bad_value);
  1940.           free (contents);
  1941.           return false;
  1942.         }
  1943.       current_index = value;
  1944.       break;
  1945.  
  1946.     case PARISC_SXT_ARG_RELOC:
  1947.       if (current_index < symtab_hdr->sh_info
  1948.           && do_locals)
  1949.         {
  1950.           Elf_Internal_Shdr *hdr;
  1951.           char *new_name;
  1952.           const char *sym_name;
  1953.           asection *sym_sec;
  1954.           unsigned int len;
  1955.  
  1956.           hdr = elf_elfsections (input_bfd)[local_syms[current_index].st_shndx];
  1957.           sym_sec = (asection *)hdr->rawdata;
  1958.           sym_name = elf_string_from_elf_section (input_bfd,
  1959.                               symtab_hdr->sh_link,
  1960.                          local_syms[current_index].st_name);
  1961.           len = strlen (sym_name) + 10;
  1962.           new_name = malloc (len);
  1963.           if (new_name == NULL)
  1964.         {
  1965.           bfd_set_error (bfd_error_no_memory);
  1966.           free (contents);
  1967.           return false;
  1968.         }
  1969.           strcpy (new_name, sym_name);
  1970.           sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
  1971.  
  1972.           /* This is a global symbol with argument location info.
  1973.          We need to enter it into the hash table.  */
  1974.           args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  1975.                                new_name, true,
  1976.                                true);
  1977.           free (new_name);
  1978.           if (args_hash == NULL)
  1979.         {
  1980.           free (contents);
  1981.           return false;
  1982.         }
  1983.           args_hash->arg_bits = value;
  1984.           break;
  1985.         }
  1986.       else if (current_index >= symtab_hdr->sh_info
  1987.            && do_globals)
  1988.         {
  1989.           struct elf_link_hash_entry *h;
  1990.  
  1991.           current_index -= symtab_hdr->sh_info;
  1992.           h = elf_sym_hashes(input_bfd)[current_index];
  1993.           /* This is a global symbol with argument location
  1994.          information.  We need to enter it into the hash table.  */
  1995.           args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  1996.                                h->root.root.string,
  1997.                                true, true);
  1998.           if (args_hash == NULL)
  1999.         {
  2000.           bfd_set_error (bfd_error_bad_value);
  2001.           free (contents);
  2002.           return false;
  2003.         }
  2004.           args_hash->arg_bits = value;
  2005.           break;
  2006.         }
  2007.       else
  2008.         break;
  2009.  
  2010.     default:
  2011.       bfd_set_error (bfd_error_bad_value);
  2012.       free (contents);
  2013.       return false;
  2014.     }
  2015.     }
  2016.   free (contents);
  2017.   return true;
  2018. }
  2019.  
  2020. /* Undo the generic ELF code's subtraction of section->vma from the
  2021.    value of each external symbol.  */
  2022.  
  2023. static boolean
  2024. elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
  2025.      bfd *abfd;
  2026.      struct bfd_link_info *info;
  2027.      const Elf_Internal_Sym *sym;
  2028.      const char **namep;
  2029.      flagword *flagsp;
  2030.      asection **secp;
  2031.      bfd_vma *valp;
  2032. {
  2033.   *valp += (*secp)->vma;
  2034.   return true;
  2035. }
  2036.  
  2037. /* Determine the name of the stub needed to perform a call assuming the
  2038.    argument relocation bits for caller and callee are in CALLER and CALLEE
  2039.    for a call from LOCATION to DESTINATION.  Copy the name into STUB_NAME.  */
  2040.  
  2041. static void
  2042. elf32_hppa_name_of_stub (caller, callee, location, destination, stub_name)
  2043.      unsigned int caller, callee;
  2044.      bfd_vma location, destination;
  2045.      char *stub_name;
  2046. {
  2047.   arg_reloc_type arg_reloc_types[5];
  2048.  
  2049.   if (elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types))
  2050.     {
  2051.       arg_reloc_location i;
  2052.       /* Fill in the basic template.  */
  2053.       strcpy (stub_name, "__XX_XX_XX_XX_XX_stub_");
  2054.  
  2055.       /* Now fix the specifics.  */
  2056.       for (i = ARG0; i <= RET; i++)
  2057.     switch (arg_reloc_types[i])
  2058.       {
  2059.         case NO:
  2060.           stub_name[3 * i + 2] = 'N';
  2061.           stub_name[3 * i + 3] = 'O';
  2062.           break;
  2063.         case GF:
  2064.           stub_name[3 * i + 2] = 'G';
  2065.           stub_name[3 * i + 3] = 'F';
  2066.           break;
  2067.         case FG:
  2068.           stub_name[3 * i + 2] = 'F';
  2069.           stub_name[3 * i + 3] = 'G';
  2070.           break;
  2071.         case GD:
  2072.           stub_name[3 * i + 2] = 'G';
  2073.           stub_name[3 * i + 3] = 'D';
  2074.           break;
  2075.         case DG:
  2076.           stub_name[3 * i + 2] = 'D';
  2077.           stub_name[3 * i + 3] = 'G';
  2078.           break;
  2079.       }
  2080.     }
  2081.   else
  2082.     strcpy (stub_name, "_____long_branch_stub_");
  2083. }
  2084.  
  2085. /* Determine if an argument relocation stub is needed to perform a
  2086.    call assuming the argument relocation bits for caller and callee
  2087.    are in CALLER and CALLEE.  Place the type of relocations (if any)
  2088.    into stub_types_p.  */
  2089.  
  2090. static boolean
  2091. elf32_hppa_arg_reloc_needed (caller, callee, stub_types)
  2092.      unsigned int caller, callee;
  2093.      arg_reloc_type stub_types[5];
  2094. {
  2095.   /* Special case for no relocations.  */
  2096.   if (caller == 0 || callee == 0)
  2097.     return 0;
  2098.   else
  2099.     {
  2100.       arg_location caller_loc[5];
  2101.       arg_location callee_loc[5];
  2102.  
  2103.       /* Extract the location information for the argument and return
  2104.      value on both the caller and callee sides.  */
  2105.       caller_loc[ARG0] = EXTRACT_ARBITS (caller, ARG0);
  2106.       callee_loc[ARG0] = EXTRACT_ARBITS (callee, ARG0);
  2107.       caller_loc[ARG1] = EXTRACT_ARBITS (caller, ARG1);
  2108.       callee_loc[ARG1] = EXTRACT_ARBITS (callee, ARG1);
  2109.       caller_loc[ARG2] = EXTRACT_ARBITS (caller, ARG2);
  2110.       callee_loc[ARG2] = EXTRACT_ARBITS (callee, ARG2);
  2111.       caller_loc[ARG3] = EXTRACT_ARBITS (caller, ARG3);
  2112.       callee_loc[ARG3] = EXTRACT_ARBITS (callee, ARG3);
  2113.       caller_loc[RET] = EXTRACT_ARBITS (caller, RET);
  2114.       callee_loc[RET] = EXTRACT_ARBITS (callee, RET);
  2115.  
  2116.       /* Check some special combinations.  This is necessary to
  2117.      deal with double precision FP arguments.  */
  2118.       if (caller_loc[ARG0] == AR_FU || caller_loc[ARG1] == AR_FU)
  2119.     {
  2120.       caller_loc[ARG0] = AR_FPDBL1;
  2121.       caller_loc[ARG1] = AR_NO;
  2122.     }
  2123.       if (caller_loc[ARG2] == AR_FU || caller_loc[ARG3] == AR_FU)
  2124.     {
  2125.       caller_loc[ARG2] = AR_FPDBL2;
  2126.       caller_loc[ARG3] = AR_NO;
  2127.     }
  2128.       if (callee_loc[ARG0] == AR_FU || callee_loc[ARG1] == AR_FU)
  2129.     {
  2130.       callee_loc[ARG0] = AR_FPDBL1;
  2131.       callee_loc[ARG1] = AR_NO;
  2132.     }
  2133.       if (callee_loc[ARG2] == AR_FU || callee_loc[ARG3] == AR_FU)
  2134.     {
  2135.       callee_loc[ARG2] = AR_FPDBL2;
  2136.       callee_loc[ARG3] = AR_NO;
  2137.     }
  2138.  
  2139.       /* Now look up any relocation needed for each argument and the
  2140.      return value.  */
  2141.       stub_types[ARG0] = arg_mismatches[caller_loc[ARG0]][callee_loc[ARG0]];
  2142.       stub_types[ARG1] = arg_mismatches[caller_loc[ARG1]][callee_loc[ARG1]];
  2143.       stub_types[ARG2] = arg_mismatches[caller_loc[ARG2]][callee_loc[ARG2]];
  2144.       stub_types[ARG3] = arg_mismatches[caller_loc[ARG3]][callee_loc[ARG3]];
  2145.       stub_types[RET] = ret_mismatches[caller_loc[RET]][callee_loc[RET]];
  2146.  
  2147.       return (stub_types[ARG0] != NO
  2148.           || stub_types[ARG1] != NO
  2149.           || stub_types[ARG2] != NO
  2150.           || stub_types[ARG3] != NO
  2151.           || stub_types[RET] != NO);
  2152.     }
  2153. }
  2154.  
  2155. /* Compute the size of the stub needed to call from LOCATION to DESTINATION
  2156.    (a function named SYM_NAME), with argument relocation bits CALLER and
  2157.    CALLEE.  Return zero if no stub is needed to perform such a call.  */
  2158.  
  2159. static unsigned int
  2160. elf32_hppa_size_of_stub (callee, caller, location, destination, sym_name)
  2161.      unsigned int callee, caller;
  2162.      bfd_vma location, destination;
  2163.      const char *sym_name;
  2164. {
  2165.   arg_reloc_type arg_reloc_types[5];
  2166.  
  2167.   /* Determine if a long branch or argument relocation stub is needed.
  2168.      If an argument relocation stub is needed, the relocation will be
  2169.      stored into arg_reloc_types.  */
  2170.   if (!(((int)(location - destination) > 0x3ffff)
  2171.     || ((int)(location - destination) < (int)0xfffc0000)
  2172.     || elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types)))
  2173.     return 0;
  2174.  
  2175.   /* Some kind of stub is needed.  Determine how big it needs to be.
  2176.      First check for argument relocation stubs as they also handle
  2177.      long calls.  Then check for long calls to millicode and finally
  2178.      the normal long calls.  */
  2179.   if (arg_reloc_types[ARG0] != NO
  2180.       || arg_reloc_types[ARG1] != NO
  2181.       || arg_reloc_types[ARG2] != NO
  2182.       || arg_reloc_types[ARG3] != NO
  2183.       || arg_reloc_types[RET] != NO)
  2184.     {
  2185.       /* Some kind of argument relocation stub is needed.  */
  2186.       unsigned int len = 16;
  2187.       arg_reloc_location i;
  2188.  
  2189.       /* Each GR or FG relocation takes 2 insns, each GD or DG
  2190.      relocation takes 3 insns.  Plus 4 more insns for the
  2191.          RP adjustment, ldil & (be | ble) and copy.  */
  2192.       for (i = ARG0; i <= RET; i++)
  2193.     switch (arg_reloc_types[i])
  2194.       {
  2195.         case GF:
  2196.         case FG:
  2197.           len += 8;
  2198.           break;
  2199.  
  2200.         case GD:
  2201.         case DG:
  2202.           len += 12;
  2203.           break;
  2204.  
  2205.         default:
  2206.           break;
  2207.       }
  2208.  
  2209.       /* Extra instructions are needed if we're relocating a return value.  */
  2210.       if (arg_reloc_types[RET] != NO)
  2211.     len += 12;
  2212.  
  2213.       return len;
  2214.     }
  2215.   else if (!strncmp ("$$", sym_name, 2)
  2216.       && strcmp ("$$dyncall", sym_name))
  2217.     return 12;
  2218.   else
  2219.     return 16;
  2220. }
  2221.  
  2222. /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
  2223.    IN_ARGS contains the stub BFD and link info pointers.  */
  2224.  
  2225. static boolean
  2226. elf32_hppa_build_one_stub (gen_entry, in_args)
  2227.      struct bfd_hash_entry *gen_entry;
  2228.      PTR in_args;
  2229. {
  2230.   void **args = (void **)in_args;
  2231.   bfd *stub_bfd = (bfd *)args[0];
  2232.   struct bfd_link_info *info = (struct bfd_link_info *)args[1];
  2233.   struct elf32_hppa_stub_hash_entry *entry;
  2234.   struct elf32_hppa_stub_hash_table *stub_hash_table;
  2235.   bfd_byte *loc;
  2236.   symvalue sym_value;
  2237.   const char *sym_name;
  2238.  
  2239.   /* Initialize pointers to the stub hash table, the particular entry we
  2240.      are building a stub for, and where (in memory) we should place the stub
  2241.      instructions.  */
  2242.   entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
  2243.   stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
  2244.   loc = stub_hash_table->location;
  2245.  
  2246.   /* Make a note of the offset within the stubs for this entry.  */
  2247.   entry->offset = stub_hash_table->offset;
  2248.  
  2249.   /* The symbol's name starts at offset 22.  */
  2250.   sym_name = entry->root.string + 22;
  2251.  
  2252.   sym_value = (entry->target_value
  2253.            + entry->target_section->output_offset
  2254.            + entry->target_section->output_section->vma);
  2255.  
  2256.   if (strncmp ("_____long_branch_stub_", entry->root.string, 22))
  2257.     {
  2258.       /* This must be an argument or return value relocation stub.  */
  2259.       unsigned long insn;
  2260.       arg_reloc_location i;
  2261.       bfd_byte *begin_loc = loc;
  2262.  
  2263.       /* First the return pointer adjustment.  Depending on exact calling
  2264.      sequence this instruction may be skipped.  */
  2265.       bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
  2266.       loc += 4;
  2267.  
  2268.       /* If we are relocating a return value, then we're going to have
  2269.      to return into the stub.  So we have to save off the user's
  2270.      return pointer into the stack at RP'.  */
  2271.       if (strncmp (entry->root.string + 14, "NO", 2))
  2272.     {
  2273.       bfd_put_32 (stub_bfd, STW_R31_M8R30, loc);
  2274.       loc += 4;
  2275.     }
  2276.  
  2277.       /* Iterate over the argument relocations, emitting instructions
  2278.      to move them around as necessary.  */
  2279.       for (i = ARG0; i <= ARG3; i++)
  2280.     {
  2281.       if (!strncmp (entry->root.string + 3 * i + 2, "GF", 2))
  2282.         {
  2283.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((26 - i) << 16), loc);
  2284.           bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | (4 + i), loc + 4);
  2285.           loc += 8;
  2286.         }
  2287.       else if (!strncmp (entry->root.string + 3 * i + 2, "FG", 2))
  2288.         {
  2289.           bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | (4 + i), loc);
  2290.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((26 - i) << 16), loc + 4);
  2291.           loc += 8;
  2292.         }
  2293.       else if (!strncmp (entry->root.string + 3 * i + 2, "GD", 2))
  2294.         {
  2295.           bfd_put_32 (stub_bfd, STW_ARG_M12R30 | ((26 - i) << 16), loc);
  2296.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((25 - i) << 16), loc + 4);
  2297.           bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | (5 + i), loc + 8);
  2298.           loc += 12;
  2299.         }
  2300.       else if (!strncmp (entry->root.string + 3 * i + 2, "DG", 2))
  2301.         {
  2302.           bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | (5 + i), loc);
  2303.           bfd_put_32 (stub_bfd, LDW_M12R30_ARG | ((26 - i) << 16), loc + 4);
  2304.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((25 - i) << 16), loc + 8);
  2305.           loc += 12;
  2306.         }
  2307.     }
  2308.  
  2309.       /* Load the high bits of the target address into %r1.  */
  2310.       insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
  2311.                 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
  2312.       bfd_put_32 (stub_bfd, insn, loc);
  2313.       loc += 4;
  2314.  
  2315.       /* If we are relocating a return value, then we're going to have
  2316.      to return into the stub, then perform the return value relocation.  */
  2317.       if (strncmp (entry->root.string + 14, "NO", 2))
  2318.     {
  2319.       /* To return to the stub we "ble" to the target and copy the return
  2320.          pointer from %r31 into %r2.  */
  2321.       insn = hppa_rebuild_insn (stub_bfd,
  2322.                     BLE_SR4_R1,
  2323.                     hppa_field_adjust (sym_value, 0,
  2324.                                e_rrsel) >> 2,
  2325.                     17);
  2326.       bfd_put_32 (stub_bfd, insn, loc);
  2327.       bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
  2328.  
  2329.       /* Reload the return pointer for our caller from the stack.  */
  2330.       bfd_put_32 (stub_bfd, LDW_M8R30_R31, loc + 8);
  2331.       loc += 12;
  2332.  
  2333.       /* Perform the return value relocation.  */
  2334.       if (!strncmp (entry->root.string + 14, "GF", 2))
  2335.         {
  2336.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (28 << 16), loc);
  2337.           bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | 4, loc + 4);
  2338.           loc += 8;
  2339.         }
  2340.       else if (!strncmp (entry->root.string + 14, "FG", 2))
  2341.         {
  2342.           bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | 4, loc);
  2343.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (28 << 16), loc + 4);
  2344.           loc += 8;
  2345.         }
  2346.       else if (!strncmp (entry->root.string + 2, "GD", 2))
  2347.         {
  2348.           bfd_put_32 (stub_bfd, STW_ARG_M12R30 | (28 << 16), loc);
  2349.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (29 << 16), loc + 4);
  2350.           bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | 4, loc + 8);
  2351.           loc += 12;
  2352.         }
  2353.       else if (!strncmp (entry->root.string + 2, "DG", 2))
  2354.         {
  2355.           bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | 4, loc);
  2356.           bfd_put_32 (stub_bfd, LDW_M12R30_ARG | (28 << 16), loc + 4);
  2357.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (29 << 16), loc + 8);
  2358.           loc += 12;
  2359.         }
  2360.       /* Branch back to the user's code now.  */
  2361.       bfd_put_32 (stub_bfd, BV_N_0_R31, loc);
  2362.       loc += 4;
  2363.     }
  2364.       else
  2365.     {
  2366.       /* No return value relocation, so we can simply "be" to the
  2367.          target and copy out return pointer into %r2.  */
  2368.       insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1,
  2369.                     hppa_field_adjust (sym_value, 0,
  2370.                                e_rrsel) >> 2, 17);
  2371.       bfd_put_32 (stub_bfd, insn, loc);
  2372.       bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
  2373.       loc += 8;
  2374.     }
  2375.  
  2376.       /* Update the location and offsets.  */
  2377.       stub_hash_table->location += (loc - begin_loc);
  2378.       stub_hash_table->offset += (loc - begin_loc);
  2379.     }
  2380.   else
  2381.     {
  2382.       /* Create one of two variant long branch stubs.  One for $$dyncall and
  2383.      normal calls, the other for calls to millicode.  */
  2384.       unsigned long insn;
  2385.       int millicode_call = 0;
  2386.  
  2387.       if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
  2388.     millicode_call = 1;
  2389.  
  2390.       /* First the return pointer adjustment.  Depending on exact calling
  2391.      sequence this instruction may be skipped.  */
  2392.       bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
  2393.  
  2394.       /* The next two instructions are the long branch itself.  A long branch
  2395.      is formed with "ldil" loading the upper bits of the target address
  2396.      into a register, then branching with "be" which adds in the lower bits.
  2397.      Long branches to millicode nullify the delay slot of the "be".  */
  2398.       insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
  2399.                 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
  2400.       bfd_put_32 (stub_bfd, insn, loc + 4);
  2401.       insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
  2402.                 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
  2403.                 17);
  2404.       bfd_put_32 (stub_bfd, insn, loc + 8);
  2405.  
  2406.       if (!millicode_call)
  2407.     {
  2408.       /* The sequence to call this stub places the return pointer into %r31,
  2409.          the final target expects the return pointer in %r2, so copy the
  2410.           return pointer into the proper register.  */
  2411.       bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
  2412.  
  2413.       /* Update the location and offsets.  */
  2414.       stub_hash_table->location += 16;
  2415.       stub_hash_table->offset += 16;
  2416.     }
  2417.       else
  2418.     {
  2419.       /* Update the location and offsets.  */
  2420.       stub_hash_table->location += 12;
  2421.       stub_hash_table->offset += 12;
  2422.     }
  2423.  
  2424.     }
  2425.   return true;
  2426. }
  2427.  
  2428. /* External entry points for sizing and building linker stubs.  */
  2429.  
  2430. /* Build all the stubs associated with the current output file.  The
  2431.    stubs are kept in a hash table attached to the main linker hash
  2432.    table.  This is called via hppaelf_finish in the linker.  */
  2433.  
  2434. boolean
  2435. elf32_hppa_build_stubs (stub_bfd, info)
  2436.      bfd *stub_bfd;
  2437.      struct bfd_link_info *info;
  2438. {
  2439.   /* The stub BFD only has one section.  */
  2440.   asection *stub_sec = stub_bfd->sections;
  2441.   struct elf32_hppa_stub_hash_table *table;
  2442.   unsigned int size;
  2443.   void *args[2];
  2444.  
  2445.   /* So we can pass both the BFD for the stubs and the link info
  2446.      structure to the routine which actually builds stubs.  */
  2447.   args[0] = stub_bfd;
  2448.   args[1] = info;
  2449.  
  2450.   /* Allocate memory to hold the linker stubs.  */
  2451.   size = bfd_section_size (stub_bfd, stub_sec);
  2452.   stub_sec->contents = bfd_zalloc (stub_bfd, size);
  2453.   if (stub_sec->contents == NULL)
  2454.     {
  2455.       bfd_set_error (bfd_error_no_memory);
  2456.       return false;
  2457.     }
  2458.   table = elf32_hppa_hash_table(info)->stub_hash_table;
  2459.   table->location = stub_sec->contents;
  2460.  
  2461.   /* Build the stubs as directed by the stub hash table.  */
  2462.   elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
  2463.  
  2464.   return true;
  2465. }
  2466.  
  2467. /* Determine and set the size of the stub section for a final link.
  2468.  
  2469.    The basic idea here is to examine all the relocations looking for
  2470.    PC-relative calls to a target that is unreachable with a "bl"
  2471.    instruction or calls where the caller and callee disagree on the
  2472.    location of their arguments or return value.  */
  2473.  
  2474. boolean
  2475. elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
  2476.      bfd *stub_bfd;
  2477.      bfd *output_bfd;
  2478.      struct bfd_link_info *link_info;
  2479. {
  2480.   bfd *input_bfd;
  2481.   asection *section, *stub_sec;
  2482.   Elf_Internal_Shdr *symtab_hdr;
  2483.   Elf_Internal_Sym *local_syms, *isym;
  2484.   Elf32_External_Sym *ext_syms, *esym;
  2485.   struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
  2486.   struct elf32_hppa_args_hash_table *args_hash_table = 0;
  2487.  
  2488.   /* Create and initialize the stub hash table.  */
  2489.   stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
  2490.              malloc (sizeof (struct elf32_hppa_stub_hash_table)));
  2491.   if (!stub_hash_table)
  2492.     {
  2493.       bfd_set_error (bfd_error_no_memory);
  2494.       goto error_return;
  2495.     }
  2496.  
  2497.   if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
  2498.                     elf32_hppa_stub_hash_newfunc))
  2499.     goto error_return;
  2500.  
  2501.   /* Likewise for the argument location hash table.  */
  2502.   args_hash_table = ((struct elf32_hppa_args_hash_table *)
  2503.              malloc (sizeof (struct elf32_hppa_args_hash_table)));
  2504.   if (!args_hash_table)
  2505.     {
  2506.       bfd_set_error (bfd_error_no_memory);
  2507.       goto error_return;
  2508.     }
  2509.  
  2510.   if (!elf32_hppa_args_hash_table_init (args_hash_table,
  2511.                     elf32_hppa_args_hash_newfunc))
  2512.     goto error_return;
  2513.  
  2514.   /* Attach the hash tables to the main hash table.  */
  2515.   elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
  2516.   elf32_hppa_hash_table(link_info)->args_hash_table = args_hash_table;
  2517.  
  2518.   /* Walk over all the input BFDs adding entries to the args hash table
  2519.      for all the external functions.  */
  2520.   for (input_bfd = link_info->input_bfds;
  2521.        input_bfd != NULL;
  2522.        input_bfd = input_bfd->link_next)
  2523.     {
  2524.       /* We'll need the symbol table in a second.  */
  2525.       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2526.       if (symtab_hdr->sh_info == 0)
  2527.     continue;
  2528.  
  2529.       if (elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
  2530.                        NULL, false, true) == false)
  2531.     goto error_return;
  2532.     }
  2533.  
  2534.   /* Magic as we know the stub bfd only has one section.  */
  2535.   stub_sec = stub_bfd->sections;
  2536.  
  2537.   /* Now that we have argument location information for all the global
  2538.      functions we can start looking for stubs.  */
  2539.   for (input_bfd = link_info->input_bfds;
  2540.        input_bfd != NULL;
  2541.        input_bfd = input_bfd->link_next)
  2542.     {
  2543.       unsigned int i;
  2544.  
  2545.       /* We'll need the symbol table in a second.  */
  2546.       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2547.       if (symtab_hdr->sh_info == 0)
  2548.     continue;
  2549.  
  2550.       /* We need an array of the local symbols attached to the input bfd.
  2551.      Unfortunately, we're going to have to read & swap them in.  */
  2552.       local_syms
  2553.     = (Elf_Internal_Sym *)malloc (symtab_hdr->sh_info
  2554.                       * sizeof (Elf_Internal_Sym));
  2555.       if (local_syms == NULL)
  2556.     {
  2557.       bfd_set_error (bfd_error_no_memory);
  2558.       goto error_return;
  2559.     }
  2560.  
  2561.       ext_syms
  2562.     = (Elf32_External_Sym *)malloc (symtab_hdr->sh_info
  2563.                     * sizeof (Elf32_External_Sym));
  2564.       if (ext_syms == NULL)
  2565.     {
  2566.       bfd_set_error (bfd_error_no_memory);
  2567.       free (local_syms);
  2568.       goto error_return;
  2569.     }
  2570.  
  2571.       if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
  2572.       || bfd_read (ext_syms, 1,
  2573.                (symtab_hdr->sh_info
  2574.             * sizeof (Elf32_External_Sym)), input_bfd)
  2575.       != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))
  2576.     {
  2577.       free (local_syms);
  2578.       free (ext_syms);
  2579.       goto error_return;
  2580.     }
  2581.  
  2582.       /* Swap the local symbols in.  */
  2583.       isym = local_syms;
  2584.       esym = ext_syms;
  2585.       for (i = 0; i < symtab_hdr->sh_info; i++, esym++, isym++)
  2586.      bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
  2587.  
  2588.       /* Now we can free the external symbols.  */
  2589.       free (ext_syms);
  2590.  
  2591.       if (elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
  2592.                        local_syms, true, false) == false)
  2593.     {
  2594.       free (local_syms);
  2595.       goto error_return;
  2596.     }
  2597.  
  2598.       /* If generating a relocateable output file, then we don't
  2599.      have to examine the relocs.  */
  2600.       if (link_info->relocateable)
  2601.     {
  2602.       free (local_syms);
  2603.       return true;
  2604.     }
  2605.  
  2606.       /* Walk over each section attached to the input bfd.  */
  2607.       for (section = input_bfd->sections;
  2608.        section != NULL;
  2609.        section = section->next)
  2610.     {
  2611.       Elf_Internal_Shdr *input_rel_hdr;
  2612.       Elf32_External_Rela *external_relocs, *erelaend, *erela;
  2613.       Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  2614.  
  2615.       /* If there aren't any relocs, then there's nothing to do.  */
  2616.       if ((section->flags & SEC_RELOC) == 0
  2617.           || section->reloc_count == 0)
  2618.         continue;
  2619.  
  2620.       /* Allocate space for the external relocations.  */
  2621.       external_relocs
  2622.         = (PTR) malloc (section->reloc_count * sizeof (Elf32_External_Rela));
  2623.       if (external_relocs == NULL)
  2624.         {
  2625.           bfd_set_error (bfd_error_no_memory);
  2626.           free (local_syms);
  2627.           goto error_return;
  2628.         }
  2629.  
  2630.       /* Likewise for the internal relocations.  */
  2631.       internal_relocs
  2632.         = (PTR) malloc (section->reloc_count * sizeof (Elf_Internal_Rela));
  2633.       if (internal_relocs == NULL)
  2634.         {
  2635.           bfd_set_error (bfd_error_no_memory);
  2636.           free (external_relocs);
  2637.           free (local_syms);
  2638.           goto error_return;
  2639.         }
  2640.  
  2641.       /* Read in the external relocs.  */
  2642.       input_rel_hdr = &elf_section_data (section)->rel_hdr;
  2643.       if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
  2644.           || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
  2645.                input_bfd) != input_rel_hdr->sh_size)
  2646.         {
  2647.           free (external_relocs);
  2648.           free (internal_relocs);
  2649.           free (local_syms);
  2650.           goto error_return;
  2651.         }
  2652.  
  2653.       /* Swap in the relocs.  */
  2654.       erela = external_relocs;
  2655.       erelaend = erela + section->reloc_count;
  2656.       irela = internal_relocs;
  2657.       for (; erela < erelaend; erela++, irela++)
  2658.         bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
  2659.  
  2660.       /* We're done with the external relocs, free them.  */
  2661.       free (external_relocs);
  2662.  
  2663.       /* Now examine each relocation.  */
  2664.       irela = internal_relocs;
  2665.       irelaend = irela + section->reloc_count;
  2666.       for (; irela < irelaend; irela++)
  2667.         {
  2668.           long r_type, callee_args, caller_args, r_index, size_of_stub;
  2669.           struct elf_link_hash_entry *hash;
  2670.           struct elf32_hppa_stub_hash_entry *stub_hash;
  2671.           struct elf32_hppa_args_hash_entry *args_hash;
  2672.           Elf_Internal_Sym *sym;
  2673.           asection *sym_sec;
  2674.           const char *sym_name;
  2675.           symvalue sym_value;
  2676.           bfd_vma location, destination;
  2677.           char *new_name = NULL;
  2678.  
  2679.           r_type = ELF32_R_TYPE (irela->r_info);
  2680.           r_index = ELF32_R_SYM (irela->r_info);
  2681.  
  2682.           if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
  2683.         {
  2684.           bfd_set_error (bfd_error_bad_value);
  2685.           free (internal_relocs);
  2686.           free (local_syms);
  2687.           goto error_return;
  2688.         }
  2689.  
  2690.           /* Only look for stubs on call instructions or plabel
  2691.          references.  */
  2692.           if (r_type != R_PARISC_PCREL17F
  2693.           && r_type != R_PARISC_PLABEL32
  2694.           && r_type != R_PARISC_PLABEL21L
  2695.           && r_type != R_PARISC_PLABEL14R)
  2696.         continue;
  2697.  
  2698.           /* Now determine the call target, its name, value, section
  2699.          and argument relocation bits.  */
  2700.           hash = NULL;
  2701.           sym = NULL;
  2702.           sym_sec = NULL;
  2703.           if (r_index < symtab_hdr->sh_info)
  2704.         {
  2705.           /* It's a local symbol.  */
  2706.           Elf_Internal_Shdr *hdr;
  2707.  
  2708.           sym = local_syms + r_index;
  2709.           hdr = elf_elfsections (input_bfd)[sym->st_shndx];
  2710.           sym_sec = (asection *)hdr->rawdata;
  2711.           sym_name = elf_string_from_elf_section (input_bfd,
  2712.                               symtab_hdr->sh_link,
  2713.                               sym->st_name);
  2714.           sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
  2715.                    ? 0 : sym->st_value);
  2716.           destination = (sym_value
  2717.                  + sym_sec->output_offset
  2718.                  + sym_sec->output_section->vma);
  2719.  
  2720.           /* Tack on an ID so we can uniquely identify this local
  2721.              symbol in the stub or arg info hash tables.  */
  2722.           new_name = malloc (strlen (sym_name) + 10);
  2723.           if (new_name == 0)
  2724.             {
  2725.               bfd_set_error (bfd_error_bad_value);
  2726.               free (internal_relocs);
  2727.               free (local_syms);
  2728.               goto error_return;
  2729.             }
  2730.           sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
  2731.           sym_name = new_name;
  2732.         }
  2733.           else
  2734.         {
  2735.           /* It's an external symbol.  */
  2736.           long index;
  2737.  
  2738.           index = r_index - symtab_hdr->sh_info;
  2739.           hash = elf_sym_hashes (input_bfd)[index];
  2740.           if (hash->root.type == bfd_link_hash_defined)
  2741.             {
  2742.               sym_sec = hash->root.u.def.section;
  2743.               sym_name = hash->root.root.string;
  2744.               sym_value = hash->root.u.def.value;
  2745.               destination = (sym_value
  2746.                      + sym_sec->output_offset
  2747.                      + sym_sec->output_section->vma);
  2748.             }
  2749.           else
  2750.             {
  2751.               bfd_set_error (bfd_error_bad_value);
  2752.               free (internal_relocs);
  2753.               free (local_syms);
  2754.               goto error_return;
  2755.             }
  2756.         }
  2757.  
  2758.           args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  2759.                                sym_name, false, false);
  2760.  
  2761.           /* Get both caller and callee argument information.  */
  2762.           if (args_hash == NULL)
  2763.         callee_args = 0;
  2764.           else
  2765.         callee_args = args_hash->arg_bits;
  2766.  
  2767.           /* For calls get the caller's bits from the addend of
  2768.          the call relocation.  For PLABELS the caller's bits
  2769.          are assumed to have all args & return values in general
  2770.          registers (0x155).  */
  2771.           if (r_type == R_PARISC_PCREL17F)
  2772.         caller_args = HPPA_R_ARG_RELOC (irela->r_addend);
  2773.           else
  2774.         caller_args = 0x155;
  2775.  
  2776.           /* Now determine where the call point is.  */
  2777.           location = (section->output_offset
  2778.               + section->output_section->vma
  2779.               + irela->r_offset);
  2780.  
  2781.           /* We only care about the destination for PCREL function
  2782.          calls (eg. we don't care for PLABELS).  */
  2783.           if (r_type != R_PARISC_PCREL17F)
  2784.         location = destination;
  2785.  
  2786.           /* Determine what (if any) linker stub is needed and its
  2787.          size (in bytes).  */
  2788.           size_of_stub = elf32_hppa_size_of_stub (callee_args,
  2789.                               caller_args,
  2790.                               location,
  2791.                               destination,
  2792.                               sym_name);
  2793.           if (size_of_stub != 0)
  2794.         {
  2795.           char *stub_name;
  2796.           unsigned int len;
  2797.  
  2798.           /* Get the name of this stub.  */
  2799.           len = strlen (sym_name);
  2800.           len += 23;
  2801.  
  2802.           stub_name = malloc (len);
  2803.           if (!stub_name)
  2804.             {
  2805.               bfd_set_error (bfd_error_no_memory);
  2806.  
  2807.               /* Because sym_name was mallocd above for local
  2808.              symbols.  */
  2809.               if (r_index < symtab_hdr->sh_info)
  2810.             free (new_name);
  2811.  
  2812.               free (internal_relocs);
  2813.               free (local_syms);
  2814.               goto error_return;
  2815.             }
  2816.           elf32_hppa_name_of_stub (caller_args, callee_args,
  2817.                        location, destination, stub_name);
  2818.           strcat (stub_name + 22, sym_name);
  2819.  
  2820.           /* Because sym_name was malloced above for local symbols.  */
  2821.           if (r_index < symtab_hdr->sh_info)
  2822.             free (new_name);
  2823.  
  2824.           stub_hash
  2825.             = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
  2826.                            false, false);
  2827.           if (stub_hash != NULL)
  2828.             {
  2829.               /* The proper stub has already been created, nothing
  2830.              else to do.  */
  2831.               free (stub_name);
  2832.             }
  2833.           else
  2834.             {
  2835.               bfd_set_section_size (stub_bfd, stub_sec,
  2836.                         (bfd_section_size (stub_bfd,
  2837.                                    stub_sec)
  2838.                          + size_of_stub));
  2839.  
  2840.               /* Enter this entry into the linker stub hash table.  */
  2841.               stub_hash
  2842.             = elf32_hppa_stub_hash_lookup (stub_hash_table,
  2843.                                stub_name, true, true);
  2844.               if (stub_hash == NULL)
  2845.             {
  2846.               bfd_set_error (bfd_error_no_memory);
  2847.               free (stub_name);
  2848.               free (internal_relocs);
  2849.               free (local_syms);
  2850.               goto error_return;
  2851.             }
  2852.  
  2853.               /* We'll need these to determine the address that the
  2854.              stub will branch to.  */
  2855.               stub_hash->target_value = sym_value;
  2856.               stub_hash->target_section = sym_sec;
  2857.             }
  2858.           free (stub_name);
  2859.         }
  2860.         }
  2861.       /* We're done with the internal relocs, free them.  */
  2862.       free (internal_relocs);
  2863.     }
  2864.       /* We're done with the local symbols, free them.  */
  2865.       free (local_syms);
  2866.     }
  2867.   return true;
  2868.  
  2869. error_return:
  2870.   /* Return gracefully, avoiding dangling references to the hash tables.  */
  2871.   if (stub_hash_table)
  2872.     {
  2873.       elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
  2874.       free (stub_hash_table);
  2875.     }
  2876.   if (args_hash_table)
  2877.     {
  2878.       elf32_hppa_hash_table(link_info)->args_hash_table = NULL;
  2879.       free (args_hash_table);
  2880.     }
  2881.   return false;
  2882. }
  2883.  
  2884. /* Misc BFD support code.  */
  2885. #define bfd_elf32_bfd_reloc_type_lookup        elf_hppa_reloc_type_lookup
  2886. #define bfd_elf32_bfd_is_local_label        hppa_elf_is_local_label
  2887.  
  2888. /* Symbol extension stuff.  */
  2889. #define bfd_elf32_set_section_contents        elf32_hppa_set_section_contents
  2890. #define elf_backend_symbol_table_processing \
  2891.   elf32_hppa_backend_symbol_table_processing
  2892. #define elf_backend_begin_write_processing \
  2893.   elf32_hppa_backend_begin_write_processing
  2894. #define elf_backend_final_write_processing \
  2895.   elf32_hppa_backend_final_write_processing
  2896.  
  2897. /* Stuff for the BFD linker.  */
  2898. #define elf_backend_relocate_section        elf32_hppa_relocate_section
  2899. #define elf_backend_add_symbol_hook        elf32_hppa_add_symbol_hook
  2900. #define elf_backend_link_output_symbol_hook \
  2901.   elf32_hppa_link_output_symbol_hook
  2902. #define bfd_elf32_bfd_link_hash_table_create \
  2903.   elf32_hppa_link_hash_table_create
  2904.  
  2905. #define TARGET_BIG_SYM        bfd_elf32_hppa_vec
  2906. #define TARGET_BIG_NAME        "elf32-hppa"
  2907. #define ELF_ARCH        bfd_arch_hppa
  2908. #define ELF_MACHINE_CODE    EM_PARISC
  2909. #define ELF_MAXPAGESIZE        0x1000
  2910.  
  2911. #include "elf32-target.h"
  2912.