home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / bfd / elf32-hppa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-25  |  96.5 KB  |  3,002 lines

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