xref: /openbsd/gnu/usr.bin/binutils/bfd/elf32-hppa.c (revision 2159047f)
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
25 
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "bfdlink.h"
29 #include "libbfd.h"
30 #include "obstack.h"
31 #include "elf-bfd.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 *, unsigned 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       unsigned 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 	      || h->root.type == bfd_link_hash_defweak)
846 	    {
847 	      sym_sec = h->root.u.def.section;
848 	      relocation = (h->root.u.def.value
849 			    + sym_sec->output_offset
850 			    + sym_sec->output_section->vma);
851 	    }
852 	  else if (h->root.type == bfd_link_hash_undefweak)
853 	    relocation = 0;
854 	  else
855 	    {
856 	      if (!((*info->callbacks->undefined_symbol)
857 		    (info, h->root.root.string, input_bfd,
858 		     input_section, rel->r_offset)))
859 		return false;
860 	      break;
861 	    }
862 	}
863 
864       if (h != NULL)
865 	sym_name = h->root.root.string;
866       else
867 	{
868 	  sym_name = bfd_elf_string_from_elf_section (input_bfd,
869 						      symtab_hdr->sh_link,
870 						      sym->st_name);
871 	  if (sym_name == NULL)
872 	    return false;
873 	  if (*sym_name == '\0')
874 	    sym_name = bfd_section_name (input_bfd, sym_sec);
875 	}
876 
877       /* If args_hash_table is NULL, then we have encountered some
878 	 kind of link error (ex. undefined symbols).  Do not try to
879 	 apply any relocations, continue the loop so we can notify
880 	 the user of several errors in a single attempted link.  */
881       if (elf32_hppa_hash_table (info)->args_hash_table == NULL)
882 	continue;
883 
884       r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
885 					      input_section, contents,
886 					      rel->r_offset, relocation,
887 					      rel->r_addend, info, sym_sec,
888 					      sym_name, h == NULL);
889 
890       if (r != bfd_reloc_ok)
891 	{
892 	  switch (r)
893 	    {
894 	    /* This can happen for DP relative relocs if $global$ is
895 	       undefined.  This is a panic situation so we don't try
896 	       to continue.  */
897 	    case bfd_reloc_undefined:
898 	    case bfd_reloc_notsupported:
899 	      if (!((*info->callbacks->undefined_symbol)
900 		    (info, "$global$", input_bfd,
901 		     input_section, rel->r_offset)))
902 		return false;
903 	      return false;
904 	    case bfd_reloc_dangerous:
905 	      {
906 		/* We use this return value to indicate that we performed
907 		   a "dangerous" relocation.  This doesn't mean we did
908 		   the wrong thing, it just means there may be some cleanup
909 		   that needs to be done here.
910 
911 		   In particular we had to swap the last call insn and its
912 		   delay slot.  If the delay slot insn needed a relocation,
913 		   then we'll need to adjust the next relocation entry's
914 		   offset to account for the fact that the insn moved.
915 
916 		   This hair wouldn't be necessary if we inserted stubs
917 		   between procedures and used a "bl" to get to the stub.  */
918 		if (rel != relend)
919 		  {
920 		    Elf_Internal_Rela *next_rel = rel + 1;
921 
922 		    if (rel->r_offset + 4 == next_rel->r_offset)
923 		      next_rel->r_offset -= 4;
924 		  }
925 		break;
926 	      }
927 	    default:
928 	    case bfd_reloc_outofrange:
929 	    case bfd_reloc_overflow:
930 	      {
931 		if (!((*info->callbacks->reloc_overflow)
932 		      (info, sym_name, howto->name, (bfd_vma) 0,
933 			input_bfd, input_section, rel->r_offset)))
934 		  return false;
935 	      }
936 	      break;
937 	    }
938 	}
939     }
940 
941   return true;
942 }
943 
944 /* Return one (or more) BFD relocations which implement the base
945    relocation with modifications based on format and field.  */
946 
947 elf32_hppa_reloc_type **
948 hppa_elf_gen_reloc_type (abfd, base_type, format, field, ignore)
949      bfd *abfd;
950      elf32_hppa_reloc_type base_type;
951      int format;
952      int field;
953      int ignore;
954 {
955   elf32_hppa_reloc_type *finaltype;
956   elf32_hppa_reloc_type **final_types;
957 
958   /* Allocate slots for the BFD relocation.  */
959   final_types = (elf32_hppa_reloc_type **)
960     bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type *) * 2);
961   if (final_types == NULL)
962     return NULL;
963 
964   /* Allocate space for the relocation itself.  */
965   finaltype = (elf32_hppa_reloc_type *)
966     bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type));
967   if (finaltype == NULL)
968     return NULL;
969 
970   /* Some reasonable defaults.  */
971   final_types[0] = finaltype;
972   final_types[1] = NULL;
973 
974 #define final_type finaltype[0]
975 
976   final_type = base_type;
977 
978   /* Just a tangle of nested switch statements to deal with the braindamage
979      that a different field selector means a completely different relocation
980      for PA ELF.  */
981   switch (base_type)
982     {
983     case R_HPPA:
984     case R_HPPA_ABS_CALL:
985       switch (format)
986 	{
987 	case 14:
988 	  switch (field)
989 	    {
990 	    case e_rsel:
991 	    case e_rrsel:
992 	      final_type = R_PARISC_DIR14R;
993 	      break;
994 	    case e_rtsel:
995 	      final_type = R_PARISC_DLTREL14R;
996 	      break;
997 	    case e_tsel:
998 	      final_type = R_PARISC_DLTREL14F;
999 	      break;
1000 	    case e_rpsel:
1001 	      final_type = R_PARISC_PLABEL14R;
1002 	      break;
1003 	    default:
1004 	      return NULL;
1005 	    }
1006 	  break;
1007 
1008 	case 17:
1009 	  switch (field)
1010 	    {
1011 	    case e_fsel:
1012 	      final_type = R_PARISC_DIR17F;
1013 	      break;
1014 	    case e_rsel:
1015 	    case e_rrsel:
1016 	      final_type = R_PARISC_DIR17R;
1017 	      break;
1018 	    default:
1019 	      return NULL;
1020 	    }
1021 	  break;
1022 
1023 	case 21:
1024 	  switch (field)
1025 	    {
1026 	    case e_lsel:
1027 	    case e_lrsel:
1028 	      final_type = R_PARISC_DIR21L;
1029 	      break;
1030 	    case e_ltsel:
1031 	      final_type = R_PARISC_DLTREL21L;
1032 	      break;
1033 	    case e_lpsel:
1034 	      final_type = R_PARISC_PLABEL21L;
1035 	      break;
1036 	    default:
1037 	      return NULL;
1038 	    }
1039 	  break;
1040 
1041 	case 32:
1042 	  switch (field)
1043 	    {
1044 	    case e_fsel:
1045 	      final_type = R_PARISC_DIR32;
1046 	      break;
1047 	    case e_psel:
1048 	      final_type = R_PARISC_PLABEL32;
1049 	      break;
1050 	    default:
1051 	      return NULL;
1052 	    }
1053 	  break;
1054 
1055 	default:
1056 	  return NULL;
1057 	}
1058       break;
1059 
1060 
1061     case R_HPPA_GOTOFF:
1062       switch (format)
1063 	{
1064 	case 14:
1065 	  switch (field)
1066 	    {
1067 	    case e_rsel:
1068 	    case e_rrsel:
1069 	      final_type = R_PARISC_DPREL14R;
1070 	      break;
1071 	    case e_fsel:
1072 	      final_type = R_PARISC_DPREL14F;
1073 	      break;
1074 	    default:
1075 	      return NULL;
1076 	    }
1077 	  break;
1078 
1079 	case 21:
1080 	  switch (field)
1081 	    {
1082 	    case e_lrsel:
1083 	    case e_lsel:
1084 	      final_type = R_PARISC_DPREL21L;
1085 	      break;
1086 	    default:
1087 	      return NULL;
1088 	    }
1089 	  break;
1090 
1091 	default:
1092 	  return NULL;
1093 	}
1094       break;
1095 
1096 
1097     case R_HPPA_PCREL_CALL:
1098       switch (format)
1099 	{
1100 	case 14:
1101 	  switch (field)
1102 	    {
1103 	    case e_rsel:
1104 	    case e_rrsel:
1105 	      final_type = R_PARISC_PCREL14R;
1106 	      break;
1107 	    case e_fsel:
1108 	      final_type = R_PARISC_PCREL14F;
1109 	      break;
1110 	    default:
1111 	      return NULL;
1112 	    }
1113 	  break;
1114 
1115 	case 17:
1116 	  switch (field)
1117 	    {
1118 	    case e_rsel:
1119 	    case e_rrsel:
1120 	      final_type = R_PARISC_PCREL17R;
1121 	      break;
1122 	    case e_fsel:
1123 	      final_type = R_PARISC_PCREL17F;
1124 	      break;
1125 	    default:
1126 	      return NULL;
1127 	    }
1128 	  break;
1129 
1130 	case 21:
1131 	  switch (field)
1132 	    {
1133 	    case e_lsel:
1134 	    case e_lrsel:
1135 	      final_type = R_PARISC_PCREL21L;
1136 	      break;
1137 	    default:
1138 	      return NULL;
1139 	    }
1140 	  break;
1141 
1142 	default:
1143 	  return NULL;
1144 	}
1145       break;
1146 
1147     default:
1148       return NULL;
1149     }
1150 
1151   return final_types;
1152 }
1153 
1154 #undef final_type
1155 
1156 /* Set the contents of a particular section at a particular location.  */
1157 
1158 static boolean
1159 elf32_hppa_set_section_contents (abfd, section, location, offset, count)
1160      bfd *abfd;
1161      sec_ptr section;
1162      PTR location;
1163      file_ptr offset;
1164      bfd_size_type count;
1165 {
1166   /* Ignore write requests for the symbol extension section until we've
1167      had the chance to rebuild it ourselves.  */
1168   if (!strcmp (section->name, ".PARISC.symextn") && !symext_chain_size)
1169     return true;
1170   else
1171     return _bfd_elf_set_section_contents (abfd, section, location,
1172 					  offset, count);
1173 }
1174 
1175 /* Translate from an elf into field into a howto relocation pointer.  */
1176 
1177 static void
1178 elf_info_to_howto (abfd, cache_ptr, dst)
1179      bfd *abfd;
1180      arelent *cache_ptr;
1181      Elf32_Internal_Rela *dst;
1182 {
1183   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_PARISC_UNIMPLEMENTED);
1184   cache_ptr->howto = &elf_hppa_howto_table[ELF32_R_TYPE (dst->r_info)];
1185 }
1186 
1187 
1188 /* Actually perform a relocation.  NOTE this is (mostly) superceeded
1189    by elf32_hppa_bfd_final_link_relocate which is called by the new
1190    fast linker.  */
1191 
1192 static bfd_reloc_status_type
1193 hppa_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
1194 		error_message)
1195      bfd *abfd;
1196      arelent *reloc_entry;
1197      asymbol *symbol_in;
1198      PTR data;
1199      asection *input_section;
1200      bfd *output_bfd;
1201      char **error_message;
1202 {
1203   /* It is no longer valid to call hppa_elf_reloc when creating
1204      a final executable.  */
1205   if (output_bfd)
1206     {
1207       reloc_entry->address += input_section->output_offset;
1208 
1209       /* Work around lossage in generic elf code to write relocations.
1210 	 (maps different section symbols into the same symbol index).  */
1211       if ((symbol_in->flags & BSF_SECTION_SYM)
1212 	  && symbol_in->section)
1213 	reloc_entry->addend += symbol_in->section->output_offset;
1214       return bfd_reloc_ok;
1215     }
1216   else
1217     {
1218       *error_message = (char *) "Unsupported call to hppa_elf_reloc";
1219       return bfd_reloc_notsupported;
1220     }
1221 }
1222 
1223 /* Actually perform a relocation as part of a final link.  This can get
1224    rather hairy when linker stubs are needed.  */
1225 
1226 static bfd_reloc_status_type
1227 elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
1228 				    input_section, contents, offset, value,
1229 				    addend, info, sym_sec, sym_name, is_local)
1230      reloc_howto_type *howto;
1231      bfd *input_bfd;
1232      bfd *output_bfd;
1233      asection *input_section;
1234      bfd_byte *contents;
1235      bfd_vma offset;
1236      bfd_vma value;
1237      bfd_vma addend;
1238      struct bfd_link_info *info;
1239      asection *sym_sec;
1240      const char *sym_name;
1241      int is_local;
1242 {
1243   unsigned long insn;
1244   unsigned long r_type = howto->type;
1245   unsigned long r_format = howto->bitsize;
1246   unsigned long r_field = e_fsel;
1247   bfd_byte *hit_data = contents + offset;
1248   boolean r_pcrel = howto->pc_relative;
1249 
1250   insn = bfd_get_32 (input_bfd, hit_data);
1251 
1252   /* Make sure we have a value for $global$.  FIXME isn't this effectively
1253      just like the gp pointer on MIPS?  Can we use those routines for this
1254      purpose?  */
1255   if (!elf32_hppa_hash_table (info)->global_sym_defined)
1256     {
1257       struct elf_link_hash_entry *h;
1258       asection *sec;
1259 
1260       h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
1261 				 false, false);
1262 
1263       /* If there isn't a $global$, then we're in deep trouble.  */
1264       if (h == NULL)
1265 	return bfd_reloc_notsupported;
1266 
1267       /* If $global$ isn't a defined symbol, then we're still in deep
1268 	 trouble.  */
1269       if (h->root.type != bfd_link_hash_defined)
1270 	return bfd_reloc_undefined;
1271 
1272       sec = h->root.u.def.section;
1273       elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
1274 						    + sec->output_section->vma
1275 						    + sec->output_offset);
1276       elf32_hppa_hash_table (info)->global_sym_defined = 1;
1277     }
1278 
1279   switch (r_type)
1280     {
1281     case R_PARISC_NONE:
1282       break;
1283 
1284     case R_PARISC_DIR32:
1285     case R_PARISC_DIR17F:
1286     case R_PARISC_PCREL17C:
1287       r_field = e_fsel;
1288       goto do_basic_type_1;
1289     case R_PARISC_DIR21L:
1290     case R_PARISC_PCREL21L:
1291       r_field = e_lrsel;
1292       goto do_basic_type_1;
1293     case R_PARISC_DIR17R:
1294     case R_PARISC_PCREL17R:
1295     case R_PARISC_DIR14R:
1296     case R_PARISC_PCREL14R:
1297       r_field = e_rrsel;
1298       goto do_basic_type_1;
1299 
1300     /* For all the DP relative relocations, we need to examine the symbol's
1301        section.  If it's a code section, then "data pointer relative" makes
1302        no sense.  In that case we don't adjust the "value", and for 21 bit
1303        addil instructions, we change the source addend register from %dp to
1304        %r0.  */
1305     case R_PARISC_DPREL21L:
1306       r_field = e_lrsel;
1307       if (sym_sec->flags & SEC_CODE)
1308 	{
1309 	  if ((insn & 0xfc000000) >> 26 == 0xa
1310 	       && (insn & 0x03e00000) >> 21 == 0x1b)
1311 	    insn &= ~0x03e00000;
1312 	}
1313       else
1314 	value -= elf32_hppa_hash_table (info)->global_value;
1315       goto do_basic_type_1;
1316     case R_PARISC_DPREL14R:
1317       r_field = e_rrsel;
1318       if ((sym_sec->flags & SEC_CODE) == 0)
1319 	value -= elf32_hppa_hash_table (info)->global_value;
1320       goto do_basic_type_1;
1321     case R_PARISC_DPREL14F:
1322       r_field = e_fsel;
1323       if ((sym_sec->flags & SEC_CODE) == 0)
1324 	value -= elf32_hppa_hash_table (info)->global_value;
1325       goto do_basic_type_1;
1326 
1327     /* These cases are separate as they may involve a lot more work
1328        to deal with linker stubs.  */
1329     case R_PARISC_PLABEL32:
1330     case R_PARISC_PLABEL21L:
1331     case R_PARISC_PLABEL14R:
1332     case R_PARISC_PCREL17F:
1333       {
1334 	bfd_vma location;
1335 	unsigned int len, caller_args, callee_args;
1336 	arg_reloc_type arg_reloc_types[5];
1337 	struct elf32_hppa_args_hash_table *args_hash_table;
1338 	struct elf32_hppa_args_hash_entry *args_hash;
1339 	char *new_name, *stub_name;
1340 
1341 	/* Get the field selector right.  We'll need it in a minute.  */
1342 	if (r_type == R_PARISC_PCREL17F
1343 	    || r_type == R_PARISC_PLABEL32)
1344 	  r_field = e_fsel;
1345 	else if (r_type == R_PARISC_PLABEL21L)
1346 	  r_field = e_lrsel;
1347 	else if (r_type == R_PARISC_PLABEL14R)
1348 	  r_field = e_rrsel;
1349 
1350 	/* Find out where we are and where we're going.  */
1351 	location = (offset +
1352 		    input_section->output_offset +
1353 		    input_section->output_section->vma);
1354 
1355 	/* Now look for the argument relocation bits associated with the
1356 	   target.  */
1357 	len = strlen (sym_name) + 1;
1358 	if (is_local)
1359 	  len += 9;
1360 	new_name = malloc (len);
1361 	if (!new_name)
1362 	  {
1363 	    bfd_set_error (bfd_error_no_memory);
1364 	    return bfd_reloc_notsupported;
1365 	  }
1366 	strcpy (new_name, sym_name);
1367 
1368 	/* Local symbols have unique IDs.  */
1369 	if (is_local)
1370 	  sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
1371 
1372 	args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
1373 
1374 	args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
1375 						 new_name, false, false);
1376 	if (args_hash == NULL)
1377 	  callee_args = 0;
1378 	else
1379 	  callee_args = args_hash->arg_bits;
1380 
1381 	/* If this is a CALL relocation, then get the caller's bits
1382 	   from the addend.  Else use the magic 0x155 value for PLABELS.
1383 
1384 	   Also we don't care about the destination (value) for PLABELS.  */
1385 	if (r_type == R_PARISC_PCREL17F)
1386 	  caller_args = HPPA_R_ARG_RELOC (addend);
1387 	else
1388 	  {
1389 	    caller_args = 0x155;
1390 	    location = value;
1391 	  }
1392 
1393 	/* Any kind of linker stub needed?  */
1394 	if (((int)(value - location) > 0x3ffff)
1395 	    || ((int)(value - location) < (int)0xfffc0000)
1396 	    || elf32_hppa_arg_reloc_needed (caller_args, callee_args,
1397 					    arg_reloc_types))
1398 	  {
1399 	    struct elf32_hppa_stub_hash_table *stub_hash_table;
1400 	    struct elf32_hppa_stub_hash_entry *stub_hash;
1401 	    asection *stub_section;
1402 
1403 	    /* Build a name for the stub.  */
1404 
1405 	    len = strlen (new_name);
1406 	    len += 23;
1407 	    stub_name = malloc (len);
1408 	    if (!stub_name)
1409 	      {
1410 		bfd_set_error (bfd_error_no_memory);
1411 		return bfd_reloc_notsupported;
1412 	      }
1413 	    elf32_hppa_name_of_stub (caller_args, callee_args,
1414 				     location, value, stub_name);
1415 	    strcat (stub_name, new_name);
1416 	    free (new_name);
1417 
1418 	    stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
1419 
1420 	    stub_hash
1421 	      = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
1422 					     false, false);
1423 
1424 	    /* We're done with that name.  */
1425 	    free (stub_name);
1426 
1427 	    /* The stub BFD only has one section.  */
1428 	    stub_section = stub_hash_table->stub_bfd->sections;
1429 
1430 	    if (stub_hash != NULL)
1431 	      {
1432 
1433 		if (r_type == R_PARISC_PCREL17F)
1434 		  {
1435 		    unsigned long delay_insn;
1436 		    unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
1437 
1438 		    /* We'll need to peek at the next insn.  */
1439 		    delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
1440 		    opcode = get_opcode (delay_insn);
1441 
1442 		    /* We also need to know the return register for this
1443 		       call.  */
1444 		    rtn_reg = (insn & 0x03e00000) >> 21;
1445 
1446 		    ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
1447 		    ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
1448 
1449 		    /* Munge up the value and other parameters for
1450 		       hppa_elf_relocate_insn.  */
1451 
1452 		    value = (stub_hash->offset
1453 			     + stub_section->output_offset
1454 			     + stub_section->output_section->vma);
1455 
1456 		    r_format = 17;
1457 		    r_field = e_fsel;
1458 		    r_pcrel = 0;
1459 		    addend = 0;
1460 
1461 		    /* We need to peek at the delay insn and determine if
1462 		       we'll need to swap the branch and its delay insn.  */
1463 		    if ((insn & 2)
1464 			|| (opcode == LDO
1465 			    && ldo_target_reg == rtn_reg)
1466 			|| (delay_insn == 0x08000240))
1467 		      {
1468 			/* No need to swap the branch and its delay slot, but
1469 			   we do need to make sure to jump past the return
1470 			   pointer update in the stub.  */
1471 			value += 4;
1472 
1473 			/* If the delay insn does a return pointer adjustment,
1474 			   then we have to make sure it stays valid.  */
1475 			if (opcode == LDO
1476 			    && ldo_target_reg == rtn_reg)
1477 			  {
1478 			    delay_insn &= 0xfc00ffff;
1479 			    delay_insn |= ((31 << 21) | (31 << 16));
1480 			    bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
1481 			  }
1482 			/* Use a BLE to reach the stub.  */
1483 			insn = BLE_SR4_R0;
1484 		      }
1485 		    else
1486 		      {
1487 			/* Wonderful, we have to swap the call insn and its
1488 			   delay slot.  */
1489 			bfd_put_32 (input_bfd, delay_insn, hit_data);
1490 			/* Use a BLE,n to reach the stub.  */
1491 			insn = (BLE_SR4_R0 | 0x2);
1492 			bfd_put_32 (input_bfd, insn, hit_data + 4);
1493 			insn = hppa_elf_relocate_insn (input_bfd,
1494 						       input_section,
1495 						       insn, offset + 4,
1496 						       value, addend,
1497 						       r_format, r_field,
1498 						       r_pcrel);
1499 			/* Update the instruction word.  */
1500 			bfd_put_32 (input_bfd, insn, hit_data + 4);
1501 			return bfd_reloc_dangerous;
1502 		      }
1503 		  }
1504 		else
1505 		  {
1506 		    /* PLABEL stuff is easy.  */
1507 
1508 		    value = (stub_hash->offset
1509 			     + stub_section->output_offset
1510 			     + stub_section->output_section->vma);
1511 		    /* We don't need the RP adjustment for PLABELs.  */
1512 		    value += 4;
1513 		    if (r_type == R_PARISC_PLABEL32)
1514 		      r_format = 32;
1515 		    else if (r_type == R_PARISC_PLABEL21L)
1516 		      r_format = 21;
1517 		    else if (r_type == R_PARISC_PLABEL14R)
1518 		      r_format = 14;
1519 
1520 		    r_pcrel = 0;
1521 		    addend = 0;
1522 		  }
1523 		}
1524 	      else
1525 		return bfd_reloc_notsupported;
1526 	  }
1527 	goto do_basic_type_1;
1528       }
1529 
1530 do_basic_type_1:
1531       insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
1532 				     offset, value, addend, r_format,
1533 				     r_field, r_pcrel);
1534       break;
1535 
1536     /* Something we don't know how to handle.  */
1537     default:
1538       return bfd_reloc_notsupported;
1539     }
1540 
1541   /* Update the instruction word.  */
1542   bfd_put_32 (input_bfd, insn, hit_data);
1543   return (bfd_reloc_ok);
1544 }
1545 
1546 /* Return the address of the howto table entry to perform the CODE
1547    relocation for an ARCH machine.  */
1548 
1549 static reloc_howto_type *
1550 elf_hppa_reloc_type_lookup (abfd, code)
1551      bfd *abfd;
1552      bfd_reloc_code_real_type code;
1553 {
1554   if ((int) code < (int) R_PARISC_UNIMPLEMENTED)
1555     {
1556       BFD_ASSERT ((int) elf_hppa_howto_table[(int) code].type == (int) code);
1557       return &elf_hppa_howto_table[(int) code];
1558     }
1559   return NULL;
1560 }
1561 
1562 /* Return true if SYM represents a local label symbol.  */
1563 
1564 static boolean
1565 hppa_elf_is_local_label (abfd, sym)
1566      bfd *abfd;
1567      asymbol *sym;
1568 {
1569   return (sym->name[0] == 'L' && sym->name[1] == '$');
1570 }
1571 
1572 /* Do any backend specific processing when beginning to write an object
1573    file.  For PA ELF we need to determine the size of the symbol extension
1574    section *before* any other output processing happens.  */
1575 
1576 static void
1577 elf32_hppa_backend_begin_write_processing (abfd, info)
1578      bfd *abfd;
1579      struct bfd_link_info *info;
1580 {
1581   unsigned int i;
1582   asection *symextn_sec;
1583 
1584   /* Size up the symbol extension section.  */
1585   if ((abfd->outsymbols == NULL
1586        && info == NULL)
1587       || symext_chain_size != 0)
1588     return;
1589 
1590   if (info == NULL)
1591     {
1592       /* We were not called from the BFD ELF linker code, so we need
1593 	 to examine the output BFD's outsymbols.
1594 
1595 	 Note we can not build the symbol extensions now as the symbol
1596 	 map hasn't been set up.  */
1597       for (i = 0; i < abfd->symcount; i++)
1598 	{
1599 	  elf_symbol_type *symbol = (elf_symbol_type *)abfd->outsymbols[i];
1600 
1601 	  /* Only functions ever need an entry in the symbol extension
1602 	     section.  */
1603 	  if (!(symbol->symbol.flags & BSF_FUNCTION))
1604 	    continue;
1605 
1606 	  /* And only if they specify the locations of their arguments.  */
1607 	  if (symbol->tc_data.hppa_arg_reloc == 0)
1608 	    continue;
1609 
1610 	  /* Yup.  This function symbol needs an entry.  */
1611 	  symext_chain_size += 2 * ELF32_PARISC_SX_SIZE;
1612 	}
1613     }
1614   else if (info->relocateable == true)
1615     {
1616       struct elf32_hppa_args_hash_table *table;
1617       table = elf32_hppa_hash_table (info)->args_hash_table;
1618 
1619       /* Determine the size of the symbol extension section.  */
1620       elf32_hppa_args_hash_traverse (table,
1621 				     elf32_hppa_size_symext,
1622 				     &symext_chain_size);
1623     }
1624 
1625   /* Now create the section and set its size.  We'll fill in the
1626      contents later.  */
1627   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1628   if (symextn_sec == NULL)
1629     symextn_sec = bfd_make_section (abfd, SYMEXTN_SECTION_NAME);
1630 
1631   bfd_set_section_flags (abfd, symextn_sec,
1632 			 SEC_LOAD | SEC_HAS_CONTENTS | SEC_DATA);
1633   symextn_sec->output_section = symextn_sec;
1634   symextn_sec->output_offset = 0;
1635   bfd_set_section_alignment (abfd, symextn_sec, 2);
1636   bfd_set_section_size (abfd, symextn_sec, symext_chain_size);
1637 }
1638 
1639 /* Called for each entry in the args location hash table.  For each
1640    entry we bump the size pointer by 2 records (16 bytes).  */
1641 
1642 static boolean
1643 elf32_hppa_size_symext (gen_entry, in_args)
1644      struct bfd_hash_entry *gen_entry;
1645      PTR in_args;
1646 {
1647   bfd_size_type *sizep = (bfd_size_type *)in_args;
1648 
1649   *sizep += 2 * ELF32_PARISC_SX_SIZE;
1650   return true;
1651 }
1652 
1653 /* Backend routine called by the linker for each output symbol.
1654 
1655    For PA ELF we use this opportunity to add an appropriate entry
1656    to the symbol extension chain for function symbols.  */
1657 
1658 static boolean
1659 elf32_hppa_link_output_symbol_hook (abfd, info, name, sym, section)
1660      bfd *abfd;
1661      struct bfd_link_info *info;
1662      const char *name;
1663      Elf_Internal_Sym *sym;
1664      asection *section;
1665 {
1666   char *new_name;
1667   unsigned int len, index;
1668   struct elf32_hppa_args_hash_table *args_hash_table;
1669   struct elf32_hppa_args_hash_entry *args_hash;
1670 
1671   /* If the args hash table is NULL, then we've encountered an error
1672      of some sorts (for example, an undefined symbol).  In that case
1673      we've got nothing else to do.
1674 
1675      NOTE: elf_link_output_symbol will abort if we return false here!  */
1676   if (elf32_hppa_hash_table (info)->args_hash_table == NULL)
1677     return true;
1678 
1679   index = elf32_hppa_hash_table (info)->output_symbol_count++;
1680 
1681   /* We need to look up this symbol in the args hash table to see if
1682      it has argument relocation bits.  */
1683   if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1684     return true;
1685 
1686   /* We know it's a function symbol of some kind.  */
1687   len = strlen (name) + 1;
1688   if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
1689     len += 9;
1690 
1691   new_name = malloc (len);
1692   if (new_name == NULL)
1693     {
1694       bfd_set_error (bfd_error_no_memory);
1695       return false;
1696     }
1697 
1698   strcpy (new_name, name);
1699   if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
1700     sprintf (new_name + len - 10, "_%08x", (int)section);
1701 
1702   /* Now that we have the unique name, we can look it up in the
1703      args hash table.  */
1704   args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
1705   args_hash = elf32_hppa_args_hash_lookup (args_hash_table, new_name,
1706 					   false, false);
1707   free (new_name);
1708   if (args_hash == NULL)
1709     return true;
1710 
1711   /* We know this symbol has arg reloc bits.  */
1712   add_entry_to_symext_chain (abfd, args_hash->arg_bits,
1713 			     index, &symext_rootP, &symext_lastP);
1714   return true;
1715 }
1716 
1717 /* Perform any processing needed late in the object file writing process.
1718    For PA ELF we build and set the contents of the symbol extension
1719    section.  */
1720 
1721 static void
1722 elf32_hppa_backend_final_write_processing (abfd, linker)
1723      bfd *abfd;
1724      boolean linker;
1725 {
1726   asection *symextn_sec;
1727   unsigned int i;
1728 
1729   /* Now build the symbol extension section.  */
1730   if (symext_chain_size == 0)
1731     return;
1732 
1733   if (! linker)
1734     {
1735       /* We were not called from the backend linker, so we still need
1736 	 to build the symbol extension chain.
1737 
1738          Look at each symbol, adding the appropriate information to the
1739 	 symbol extension section list as necessary.  */
1740       for (i = 0; i < abfd->symcount; i++)
1741 	{
1742 	  elf_symbol_type *symbol = (elf_symbol_type *) abfd->outsymbols[i];
1743 
1744 	  /* Only functions ever need an entry in the symbol extension
1745 	     section.  */
1746 	  if (!(symbol->symbol.flags & BSF_FUNCTION))
1747 	    continue;
1748 
1749 	  /* And only if they specify the locations of their arguments.  */
1750 	  if (symbol->tc_data.hppa_arg_reloc == 0)
1751 	    continue;
1752 
1753 	  /* Add this symbol's information to the chain.  */
1754 	  add_entry_to_symext_chain (abfd, symbol->tc_data.hppa_arg_reloc,
1755 				     symbol->symbol.udata.i, &symext_rootP,
1756 				     &symext_lastP);
1757 	}
1758     }
1759 
1760   /* Now fill in the contents of the symbol extension section.  */
1761   elf_hppa_tc_make_sections (abfd, symext_rootP);
1762 
1763   /* And attach that as the section's contents.  */
1764   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1765   if (symextn_sec == (asection *) 0)
1766     abort();
1767 
1768   symextn_sec->contents = (void *)symextn_contents;
1769 
1770   bfd_set_section_contents (abfd, symextn_sec, symextn_sec->contents,
1771 			    symextn_sec->output_offset, symextn_sec->_raw_size);
1772 }
1773 
1774 /* Update the symbol extention chain to include the symbol pointed to
1775    by SYMBOLP if SYMBOLP is a function symbol.  Used internally and by GAS.  */
1776 
1777 static void
1778 add_entry_to_symext_chain (abfd, arg_reloc, sym_idx, symext_root, symext_last)
1779      bfd *abfd;
1780      unsigned int arg_reloc;
1781      unsigned int sym_idx;
1782      symext_chainS **symext_root;
1783      symext_chainS **symext_last;
1784 {
1785   symext_chainS *symextP;
1786 
1787   /* Allocate memory and initialize this entry.  */
1788   symextP = (symext_chainS *) bfd_alloc (abfd, sizeof (symext_chainS) * 2);
1789   if (!symextP)
1790     {
1791       bfd_set_error (bfd_error_no_memory);
1792       abort();			/* FIXME */
1793     }
1794 
1795   symextP[0].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_SYMNDX, sym_idx);
1796   symextP[0].next = &symextP[1];
1797 
1798   symextP[1].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_ARG_RELOC, arg_reloc);
1799   symextP[1].next = NULL;
1800 
1801   /* Now update the chain itself so it can be walked later to build
1802      the symbol extension section.  */
1803   if (*symext_root == NULL)
1804     {
1805       *symext_root = &symextP[0];
1806       *symext_last = &symextP[1];
1807     }
1808   else
1809     {
1810       (*symext_last)->next = &symextP[0];
1811       *symext_last = &symextP[1];
1812     }
1813 }
1814 
1815 /* Build the symbol extension section.  */
1816 
1817 static void
1818 elf_hppa_tc_make_sections (abfd, symext_root)
1819      bfd *abfd;
1820      symext_chainS *symext_root;
1821 {
1822   symext_chainS *symextP;
1823   unsigned int i;
1824   asection *symextn_sec;
1825 
1826   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1827 
1828   /* Grab some memory for the contents of the symbol extension section
1829      itself.  */
1830   symextn_contents = (bfd_byte *) bfd_zalloc (abfd,
1831 					      symextn_sec->_raw_size);
1832   if (!symextn_contents)
1833     {
1834       bfd_set_error (bfd_error_no_memory);
1835       abort();			/* FIXME */
1836     }
1837 
1838   /* Fill in the contents of the symbol extension chain.  */
1839   for (i = 0, symextP = symext_root; symextP; symextP = symextP->next, ++i)
1840     ELF32_PARISC_SX_PUT (abfd, (bfd_vma) symextP->entry,
1841 			 symextn_contents + i * ELF32_PARISC_SX_SIZE);
1842 
1843   return;
1844 }
1845 
1846 /* Do some PA ELF specific work after reading in the symbol table.
1847    In particular attach the argument relocation from the
1848    symbol extension section to the appropriate symbols.  */
1849 
1850 static boolean
1851 elf32_hppa_backend_symbol_table_processing (abfd, esyms,symcnt)
1852      bfd *abfd;
1853      elf_symbol_type *esyms;
1854      unsigned int symcnt;
1855 {
1856   Elf32_Internal_Shdr *symextn_hdr =
1857     bfd_elf_find_section (abfd, SYMEXTN_SECTION_NAME);
1858   unsigned int i, current_sym_idx = 0;
1859 
1860   /* If no symbol extension existed, then all symbol extension information
1861      is assumed to be zero.  */
1862   if (symextn_hdr == NULL)
1863     {
1864       for (i = 0; i < symcnt; i++)
1865 	esyms[i].tc_data.hppa_arg_reloc = 0;
1866       return (true);
1867     }
1868 
1869   /* FIXME:  Why not use bfd_get_section_contents here?  Also should give
1870      memory back when we're done.  */
1871   /* Allocate a buffer of the appropriate size for the symextn section.  */
1872   symextn_hdr->contents = bfd_zalloc(abfd,symextn_hdr->sh_size);
1873   if (!symextn_hdr->contents)
1874     {
1875       bfd_set_error (bfd_error_no_memory);
1876       return false;
1877     }
1878 
1879   /* Read in the symextn section.  */
1880   if (bfd_seek (abfd, symextn_hdr->sh_offset, SEEK_SET) == -1)
1881     return false;
1882   if (bfd_read ((PTR) symextn_hdr->contents, 1, symextn_hdr->sh_size, abfd)
1883       != symextn_hdr->sh_size)
1884     return false;
1885 
1886   /* Parse entries in the symbol extension section, updating the symtab
1887      entries as we go */
1888   for (i = 0; i < symextn_hdr->sh_size / ELF32_PARISC_SX_SIZE; i++)
1889     {
1890       symext_entryS se =
1891 	ELF32_PARISC_SX_GET (abfd,
1892 			     (symextn_hdr->contents
1893 			      + i * ELF32_PARISC_SX_SIZE));
1894       unsigned int se_value = ELF32_PARISC_SX_VAL (se);
1895       unsigned int se_type = ELF32_PARISC_SX_TYPE (se);
1896 
1897       switch (se_type)
1898 	{
1899 	case PARISC_SXT_NULL:
1900 	  break;
1901 
1902 	case PARISC_SXT_SYMNDX:
1903 	  if (se_value >= symcnt)
1904 	    {
1905 	      bfd_set_error (bfd_error_bad_value);
1906 	      return (false);
1907 	    }
1908 	  current_sym_idx = se_value - 1;
1909 	  break;
1910 
1911 	case PARISC_SXT_ARG_RELOC:
1912 	  esyms[current_sym_idx].tc_data.hppa_arg_reloc = se_value;
1913 	  break;
1914 
1915 	default:
1916 	  bfd_set_error (bfd_error_bad_value);
1917 	  return (false);
1918 	}
1919     }
1920   return (true);
1921 }
1922 
1923 /* Read and attach the symbol extension information for the symbols
1924    in INPUT_BFD to the argument location hash table.  Handle locals
1925    if DO_LOCALS is true; likewise for globals when DO_GLOBALS is true.  */
1926 
1927 static boolean
1928 elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table, local_syms)
1929      bfd *input_bfd;
1930      Elf_Internal_Shdr *symtab_hdr;
1931      struct elf32_hppa_args_hash_table *args_hash_table;
1932      Elf_Internal_Sym *local_syms;
1933 {
1934   asection *symextn_sec;
1935   bfd_byte *contents;
1936   unsigned int i, n_entries, current_index = 0;
1937 
1938   /* Get the symbol extension section for this BFD.  If no section exists
1939      then there's nothing to do.  Likewise if the section exists, but
1940      has no contents.  */
1941   symextn_sec = bfd_get_section_by_name (input_bfd, SYMEXTN_SECTION_NAME);
1942   if (symextn_sec == NULL)
1943     return true;
1944 
1945   /* Done separately so we can turn off SEC_HAS_CONTENTS (see below).  */
1946   if (symextn_sec->_raw_size == 0)
1947     {
1948       symextn_sec->flags &= ~SEC_HAS_CONTENTS;
1949       return true;
1950     }
1951 
1952   contents = (bfd_byte *) malloc ((size_t) symextn_sec->_raw_size);
1953   if (contents == NULL)
1954     {
1955       bfd_set_error (bfd_error_no_memory);
1956       return false;
1957     }
1958 
1959   /* How gross.  We turn off SEC_HAS_CONTENTS for the input symbol extension
1960      sections to keep the generic ELF/BFD code from trying to do anything
1961      with them.  We have to undo that hack temporarily so that we can read
1962      in the contents with the generic code.  */
1963   symextn_sec->flags |= SEC_HAS_CONTENTS;
1964   if (bfd_get_section_contents (input_bfd, symextn_sec, contents,
1965 				0, symextn_sec->_raw_size) == false)
1966     {
1967       symextn_sec->flags &= ~SEC_HAS_CONTENTS;
1968       free (contents);
1969       return false;
1970     }
1971 
1972   /* Gross.  Turn off SEC_HAS_CONTENTS for the input symbol extension
1973      sections (see above).  */
1974   symextn_sec->flags &= ~SEC_HAS_CONTENTS;
1975 
1976   n_entries = symextn_sec->_raw_size / ELF32_PARISC_SX_SIZE;
1977   for (i = 0; i < n_entries; i++)
1978     {
1979       symext_entryS entry =
1980 	ELF32_PARISC_SX_GET (input_bfd, contents + i * ELF32_PARISC_SX_SIZE);
1981       unsigned int value = ELF32_PARISC_SX_VAL (entry);
1982       unsigned int type = ELF32_PARISC_SX_TYPE (entry);
1983       struct elf32_hppa_args_hash_entry *args_hash;
1984 
1985       switch (type)
1986 	{
1987 	case PARISC_SXT_NULL:
1988 	  break;
1989 
1990 	case PARISC_SXT_SYMNDX:
1991 	  if (value >= symtab_hdr->sh_size / sizeof (Elf32_External_Sym))
1992 	    {
1993 	      bfd_set_error (bfd_error_bad_value);
1994 	      free (contents);
1995 	      return false;
1996 	    }
1997 	  current_index = value;
1998 	  break;
1999 
2000 	case PARISC_SXT_ARG_RELOC:
2001 	  if (current_index < symtab_hdr->sh_info)
2002 	    {
2003 	      Elf_Internal_Shdr *hdr;
2004 	      char *new_name;
2005 	      const char *sym_name;
2006 	      asection *sym_sec;
2007 	      unsigned int len;
2008 
2009 	      hdr = elf_elfsections (input_bfd)[local_syms[current_index].st_shndx];
2010 	      sym_sec = hdr->bfd_section;
2011 	      sym_name = bfd_elf_string_from_elf_section (input_bfd,
2012 						      symtab_hdr->sh_link,
2013 	 			        local_syms[current_index].st_name);
2014 	      len = strlen (sym_name) + 10;
2015 	      new_name = malloc (len);
2016 	      if (new_name == NULL)
2017 		{
2018 		  bfd_set_error (bfd_error_no_memory);
2019 		  free (contents);
2020 		  return false;
2021 		}
2022 	      strcpy (new_name, sym_name);
2023 	      sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
2024 
2025 	      /* This is a global symbol with argument location info.
2026 		 We need to enter it into the hash table.  */
2027 	      args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
2028 						       new_name, true,
2029 						       true);
2030 	      free (new_name);
2031 	      if (args_hash == NULL)
2032 		{
2033 		  free (contents);
2034 		  return false;
2035 		}
2036 	      args_hash->arg_bits = value;
2037 	      break;
2038 	    }
2039 	  else if (current_index >= symtab_hdr->sh_info)
2040 	    {
2041 	      struct elf_link_hash_entry *h;
2042 
2043 	      current_index -= symtab_hdr->sh_info;
2044 	      h = elf_sym_hashes(input_bfd)[current_index];
2045 	      /* This is a global symbol with argument location
2046 		 information.  We need to enter it into the hash table.  */
2047 	      args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
2048 						       h->root.root.string,
2049 						       true, true);
2050 	      if (args_hash == NULL)
2051 		{
2052 		  bfd_set_error (bfd_error_bad_value);
2053 		  free (contents);
2054 		  return false;
2055 		}
2056 	      args_hash->arg_bits = value;
2057 	      break;
2058 	    }
2059 	  else
2060 	    break;
2061 
2062 	default:
2063 	  bfd_set_error (bfd_error_bad_value);
2064 	  free (contents);
2065 	  return false;
2066 	}
2067     }
2068   free (contents);
2069   return true;
2070 }
2071 
2072 /* Undo the generic ELF code's subtraction of section->vma from the
2073    value of each external symbol.  */
2074 
2075 static boolean
2076 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2077      bfd *abfd;
2078      struct bfd_link_info *info;
2079      const Elf_Internal_Sym *sym;
2080      const char **namep;
2081      flagword *flagsp;
2082      asection **secp;
2083      bfd_vma *valp;
2084 {
2085   *valp += (*secp)->vma;
2086   return true;
2087 }
2088 
2089 /* Determine the name of the stub needed to perform a call assuming the
2090    argument relocation bits for caller and callee are in CALLER and CALLEE
2091    for a call from LOCATION to DESTINATION.  Copy the name into STUB_NAME.  */
2092 
2093 static void
2094 elf32_hppa_name_of_stub (caller, callee, location, destination, stub_name)
2095      unsigned int caller, callee;
2096      bfd_vma location, destination;
2097      char *stub_name;
2098 {
2099   arg_reloc_type arg_reloc_types[5];
2100 
2101   if (elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types))
2102     {
2103       arg_reloc_location i;
2104       /* Fill in the basic template.  */
2105       strcpy (stub_name, "__XX_XX_XX_XX_XX_stub_");
2106 
2107       /* Now fix the specifics.  */
2108       for (i = ARG0; i <= RET; i++)
2109 	switch (arg_reloc_types[i])
2110 	  {
2111 	    case NO:
2112 	      stub_name[3 * i + 2] = 'N';
2113 	      stub_name[3 * i + 3] = 'O';
2114 	      break;
2115 	    case GF:
2116 	      stub_name[3 * i + 2] = 'G';
2117 	      stub_name[3 * i + 3] = 'F';
2118 	      break;
2119 	    case FG:
2120 	      stub_name[3 * i + 2] = 'F';
2121 	      stub_name[3 * i + 3] = 'G';
2122 	      break;
2123 	    case GD:
2124 	      stub_name[3 * i + 2] = 'G';
2125 	      stub_name[3 * i + 3] = 'D';
2126 	      break;
2127 	    case DG:
2128 	      stub_name[3 * i + 2] = 'D';
2129 	      stub_name[3 * i + 3] = 'G';
2130 	      break;
2131 	  }
2132     }
2133   else
2134     strcpy (stub_name, "_____long_branch_stub_");
2135 }
2136 
2137 /* Determine if an argument relocation stub is needed to perform a
2138    call assuming the argument relocation bits for caller and callee
2139    are in CALLER and CALLEE.  Place the type of relocations (if any)
2140    into stub_types_p.  */
2141 
2142 static boolean
2143 elf32_hppa_arg_reloc_needed (caller, callee, stub_types)
2144      unsigned int caller, callee;
2145      arg_reloc_type stub_types[5];
2146 {
2147   /* Special case for no relocations.  */
2148   if (caller == 0 || callee == 0)
2149     return 0;
2150   else
2151     {
2152       arg_location caller_loc[5];
2153       arg_location callee_loc[5];
2154 
2155       /* Extract the location information for the argument and return
2156 	 value on both the caller and callee sides.  */
2157       caller_loc[ARG0] = EXTRACT_ARBITS (caller, ARG0);
2158       callee_loc[ARG0] = EXTRACT_ARBITS (callee, ARG0);
2159       caller_loc[ARG1] = EXTRACT_ARBITS (caller, ARG1);
2160       callee_loc[ARG1] = EXTRACT_ARBITS (callee, ARG1);
2161       caller_loc[ARG2] = EXTRACT_ARBITS (caller, ARG2);
2162       callee_loc[ARG2] = EXTRACT_ARBITS (callee, ARG2);
2163       caller_loc[ARG3] = EXTRACT_ARBITS (caller, ARG3);
2164       callee_loc[ARG3] = EXTRACT_ARBITS (callee, ARG3);
2165       caller_loc[RET] = EXTRACT_ARBITS (caller, RET);
2166       callee_loc[RET] = EXTRACT_ARBITS (callee, RET);
2167 
2168       /* Check some special combinations.  This is necessary to
2169 	 deal with double precision FP arguments.  */
2170       if (caller_loc[ARG0] == AR_FU || caller_loc[ARG1] == AR_FU)
2171 	{
2172 	  caller_loc[ARG0] = AR_FPDBL1;
2173 	  caller_loc[ARG1] = AR_NO;
2174 	}
2175       if (caller_loc[ARG2] == AR_FU || caller_loc[ARG3] == AR_FU)
2176 	{
2177 	  caller_loc[ARG2] = AR_FPDBL2;
2178 	  caller_loc[ARG3] = AR_NO;
2179 	}
2180       if (callee_loc[ARG0] == AR_FU || callee_loc[ARG1] == AR_FU)
2181 	{
2182 	  callee_loc[ARG0] = AR_FPDBL1;
2183 	  callee_loc[ARG1] = AR_NO;
2184 	}
2185       if (callee_loc[ARG2] == AR_FU || callee_loc[ARG3] == AR_FU)
2186 	{
2187 	  callee_loc[ARG2] = AR_FPDBL2;
2188 	  callee_loc[ARG3] = AR_NO;
2189 	}
2190 
2191       /* Now look up any relocation needed for each argument and the
2192 	 return value.  */
2193       stub_types[ARG0] = arg_mismatches[caller_loc[ARG0]][callee_loc[ARG0]];
2194       stub_types[ARG1] = arg_mismatches[caller_loc[ARG1]][callee_loc[ARG1]];
2195       stub_types[ARG2] = arg_mismatches[caller_loc[ARG2]][callee_loc[ARG2]];
2196       stub_types[ARG3] = arg_mismatches[caller_loc[ARG3]][callee_loc[ARG3]];
2197       stub_types[RET] = ret_mismatches[caller_loc[RET]][callee_loc[RET]];
2198 
2199       return (stub_types[ARG0] != NO
2200 	      || stub_types[ARG1] != NO
2201 	      || stub_types[ARG2] != NO
2202 	      || stub_types[ARG3] != NO
2203 	      || stub_types[RET] != NO);
2204     }
2205 }
2206 
2207 /* Compute the size of the stub needed to call from LOCATION to DESTINATION
2208    (a function named SYM_NAME), with argument relocation bits CALLER and
2209    CALLEE.  Return zero if no stub is needed to perform such a call.  */
2210 
2211 static unsigned int
2212 elf32_hppa_size_of_stub (callee, caller, location, destination, sym_name)
2213      unsigned int callee, caller;
2214      bfd_vma location, destination;
2215      const char *sym_name;
2216 {
2217   arg_reloc_type arg_reloc_types[5];
2218 
2219   /* Determine if a long branch or argument relocation stub is needed.
2220      If an argument relocation stub is needed, the relocation will be
2221      stored into arg_reloc_types.  */
2222   if (!(((int)(location - destination) > 0x3ffff)
2223 	|| ((int)(location - destination) < (int)0xfffc0000)
2224 	|| elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types)))
2225     return 0;
2226 
2227   /* Some kind of stub is needed.  Determine how big it needs to be.
2228      First check for argument relocation stubs as they also handle
2229      long calls.  Then check for long calls to millicode and finally
2230      the normal long calls.  */
2231   if (arg_reloc_types[ARG0] != NO
2232       || arg_reloc_types[ARG1] != NO
2233       || arg_reloc_types[ARG2] != NO
2234       || arg_reloc_types[ARG3] != NO
2235       || arg_reloc_types[RET] != NO)
2236     {
2237       /* Some kind of argument relocation stub is needed.  */
2238       unsigned int len = 16;
2239       arg_reloc_location i;
2240 
2241       /* Each GR or FG relocation takes 2 insns, each GD or DG
2242 	 relocation takes 3 insns.  Plus 4 more insns for the
2243          RP adjustment, ldil & (be | ble) and copy.  */
2244       for (i = ARG0; i <= RET; i++)
2245 	switch (arg_reloc_types[i])
2246 	  {
2247 	    case GF:
2248 	    case FG:
2249 	      len += 8;
2250 	      break;
2251 
2252 	    case GD:
2253 	    case DG:
2254 	      len += 12;
2255 	      break;
2256 
2257 	    default:
2258 	      break;
2259 	  }
2260 
2261       /* Extra instructions are needed if we're relocating a return value.  */
2262       if (arg_reloc_types[RET] != NO)
2263 	len += 12;
2264 
2265       return len;
2266     }
2267   else if (!strncmp ("$$", sym_name, 2)
2268       && strcmp ("$$dyncall", sym_name))
2269     return 12;
2270   else
2271     return 16;
2272 }
2273 
2274 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
2275    IN_ARGS contains the stub BFD and link info pointers.  */
2276 
2277 static boolean
2278 elf32_hppa_build_one_stub (gen_entry, in_args)
2279      struct bfd_hash_entry *gen_entry;
2280      PTR in_args;
2281 {
2282   void **args = (void **)in_args;
2283   bfd *stub_bfd = (bfd *)args[0];
2284   struct bfd_link_info *info = (struct bfd_link_info *)args[1];
2285   struct elf32_hppa_stub_hash_entry *entry;
2286   struct elf32_hppa_stub_hash_table *stub_hash_table;
2287   bfd_byte *loc;
2288   symvalue sym_value;
2289   const char *sym_name;
2290 
2291   /* Initialize pointers to the stub hash table, the particular entry we
2292      are building a stub for, and where (in memory) we should place the stub
2293      instructions.  */
2294   entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
2295   stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
2296   loc = stub_hash_table->location;
2297 
2298   /* Make a note of the offset within the stubs for this entry.  */
2299   entry->offset = stub_hash_table->offset;
2300 
2301   /* The symbol's name starts at offset 22.  */
2302   sym_name = entry->root.string + 22;
2303 
2304   sym_value = (entry->target_value
2305 	       + entry->target_section->output_offset
2306 	       + entry->target_section->output_section->vma);
2307 
2308   if (strncmp ("_____long_branch_stub_", entry->root.string, 22))
2309     {
2310       /* This must be an argument or return value relocation stub.  */
2311       unsigned long insn;
2312       arg_reloc_location i;
2313       bfd_byte *begin_loc = loc;
2314 
2315       /* First the return pointer adjustment.  Depending on exact calling
2316 	 sequence this instruction may be skipped.  */
2317       bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
2318       loc += 4;
2319 
2320       /* If we are relocating a return value, then we're going to have
2321 	 to return into the stub.  So we have to save off the user's
2322 	 return pointer into the stack at RP'.  */
2323       if (strncmp (entry->root.string + 14, "NO", 2))
2324 	{
2325 	  bfd_put_32 (stub_bfd, STW_R31_M8R30, loc);
2326 	  loc += 4;
2327 	}
2328 
2329       /* Iterate over the argument relocations, emitting instructions
2330 	 to move them around as necessary.  */
2331       for (i = ARG0; i <= ARG3; i++)
2332 	{
2333 	  if (!strncmp (entry->root.string + 3 * i + 2, "GF", 2))
2334 	    {
2335 	      bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((26 - i) << 16), loc);
2336 	      bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | (4 + i), loc + 4);
2337 	      loc += 8;
2338 	    }
2339 	  else if (!strncmp (entry->root.string + 3 * i + 2, "FG", 2))
2340 	    {
2341 	      bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | (4 + i), loc);
2342 	      bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((26 - i) << 16), loc + 4);
2343 	      loc += 8;
2344 	    }
2345 	  else if (!strncmp (entry->root.string + 3 * i + 2, "GD", 2))
2346 	    {
2347 	      bfd_put_32 (stub_bfd, STW_ARG_M12R30 | ((26 - i) << 16), loc);
2348 	      bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((25 - i) << 16), loc + 4);
2349 	      bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | (5 + i), loc + 8);
2350 	      loc += 12;
2351 	    }
2352 	  else if (!strncmp (entry->root.string + 3 * i + 2, "DG", 2))
2353 	    {
2354 	      bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | (5 + i), loc);
2355 	      bfd_put_32 (stub_bfd, LDW_M12R30_ARG | ((26 - i) << 16), loc + 4);
2356 	      bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((25 - i) << 16), loc + 8);
2357 	      loc += 12;
2358 	    }
2359 	}
2360 
2361       /* Load the high bits of the target address into %r1.  */
2362       insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
2363 				hppa_field_adjust (sym_value, 0, e_lrsel), 21);
2364       bfd_put_32 (stub_bfd, insn, loc);
2365       loc += 4;
2366 
2367       /* If we are relocating a return value, then we're going to have
2368 	 to return into the stub, then perform the return value relocation.  */
2369       if (strncmp (entry->root.string + 14, "NO", 2))
2370 	{
2371 	  /* To return to the stub we "ble" to the target and copy the return
2372 	     pointer from %r31 into %r2.  */
2373 	  insn = hppa_rebuild_insn (stub_bfd,
2374 				    BLE_SR4_R1,
2375 				    hppa_field_adjust (sym_value, 0,
2376 						       e_rrsel) >> 2,
2377 				    17);
2378 	  bfd_put_32 (stub_bfd, insn, loc);
2379 	  bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
2380 
2381 	  /* Reload the return pointer for our caller from the stack.  */
2382 	  bfd_put_32 (stub_bfd, LDW_M8R30_R31, loc + 8);
2383 	  loc += 12;
2384 
2385 	  /* Perform the return value relocation.  */
2386 	  if (!strncmp (entry->root.string + 14, "GF", 2))
2387 	    {
2388 	      bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (28 << 16), loc);
2389 	      bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | 4, loc + 4);
2390 	      loc += 8;
2391 	    }
2392 	  else if (!strncmp (entry->root.string + 14, "FG", 2))
2393 	    {
2394 	      bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | 4, loc);
2395 	      bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (28 << 16), loc + 4);
2396 	      loc += 8;
2397 	    }
2398 	  else if (!strncmp (entry->root.string + 2, "GD", 2))
2399 	    {
2400 	      bfd_put_32 (stub_bfd, STW_ARG_M12R30 | (28 << 16), loc);
2401 	      bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (29 << 16), loc + 4);
2402 	      bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | 4, loc + 8);
2403 	      loc += 12;
2404 	    }
2405 	  else if (!strncmp (entry->root.string + 2, "DG", 2))
2406 	    {
2407 	      bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | 4, loc);
2408 	      bfd_put_32 (stub_bfd, LDW_M12R30_ARG | (28 << 16), loc + 4);
2409 	      bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (29 << 16), loc + 8);
2410 	      loc += 12;
2411 	    }
2412 	  /* Branch back to the user's code now.  */
2413 	  bfd_put_32 (stub_bfd, BV_N_0_R31, loc);
2414 	  loc += 4;
2415 	}
2416       else
2417 	{
2418 	  /* No return value relocation, so we can simply "be" to the
2419 	     target and copy out return pointer into %r2.  */
2420 	  insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1,
2421 				    hppa_field_adjust (sym_value, 0,
2422 						       e_rrsel) >> 2, 17);
2423 	  bfd_put_32 (stub_bfd, insn, loc);
2424 	  bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
2425 	  loc += 8;
2426 	}
2427 
2428       /* Update the location and offsets.  */
2429       stub_hash_table->location += (loc - begin_loc);
2430       stub_hash_table->offset += (loc - begin_loc);
2431     }
2432   else
2433     {
2434       /* Create one of two variant long branch stubs.  One for $$dyncall and
2435 	 normal calls, the other for calls to millicode.  */
2436       unsigned long insn;
2437       int millicode_call = 0;
2438 
2439       if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
2440 	millicode_call = 1;
2441 
2442       /* First the return pointer adjustment.  Depending on exact calling
2443 	 sequence this instruction may be skipped.  */
2444       bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
2445 
2446       /* The next two instructions are the long branch itself.  A long branch
2447 	 is formed with "ldil" loading the upper bits of the target address
2448 	 into a register, then branching with "be" which adds in the lower bits.
2449 	 Long branches to millicode nullify the delay slot of the "be".  */
2450       insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
2451 				hppa_field_adjust (sym_value, 0, e_lrsel), 21);
2452       bfd_put_32 (stub_bfd, insn, loc + 4);
2453       insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
2454 				hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
2455 				17);
2456       bfd_put_32 (stub_bfd, insn, loc + 8);
2457 
2458       if (!millicode_call)
2459 	{
2460 	  /* The sequence to call this stub places the return pointer into %r31,
2461 	     the final target expects the return pointer in %r2, so copy the
2462 	      return pointer into the proper register.  */
2463 	  bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
2464 
2465 	  /* Update the location and offsets.  */
2466 	  stub_hash_table->location += 16;
2467 	  stub_hash_table->offset += 16;
2468 	}
2469       else
2470 	{
2471 	  /* Update the location and offsets.  */
2472 	  stub_hash_table->location += 12;
2473 	  stub_hash_table->offset += 12;
2474 	}
2475 
2476     }
2477   return true;
2478 }
2479 
2480 /* External entry points for sizing and building linker stubs.  */
2481 
2482 /* Build all the stubs associated with the current output file.  The
2483    stubs are kept in a hash table attached to the main linker hash
2484    table.  This is called via hppaelf_finish in the linker.  */
2485 
2486 boolean
2487 elf32_hppa_build_stubs (stub_bfd, info)
2488      bfd *stub_bfd;
2489      struct bfd_link_info *info;
2490 {
2491   /* The stub BFD only has one section.  */
2492   asection *stub_sec = stub_bfd->sections;
2493   struct elf32_hppa_stub_hash_table *table;
2494   unsigned int size;
2495   void *args[2];
2496 
2497   /* So we can pass both the BFD for the stubs and the link info
2498      structure to the routine which actually builds stubs.  */
2499   args[0] = stub_bfd;
2500   args[1] = info;
2501 
2502   /* Allocate memory to hold the linker stubs.  */
2503   size = bfd_section_size (stub_bfd, stub_sec);
2504   stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
2505   if (stub_sec->contents == NULL)
2506     {
2507       bfd_set_error (bfd_error_no_memory);
2508       return false;
2509     }
2510   table = elf32_hppa_hash_table(info)->stub_hash_table;
2511   table->location = stub_sec->contents;
2512 
2513   /* Build the stubs as directed by the stub hash table.  */
2514   elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
2515 
2516   return true;
2517 }
2518 
2519 /* Determine and set the size of the stub section for a final link.
2520 
2521    The basic idea here is to examine all the relocations looking for
2522    PC-relative calls to a target that is unreachable with a "bl"
2523    instruction or calls where the caller and callee disagree on the
2524    location of their arguments or return value.  */
2525 
2526 boolean
2527 elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
2528      bfd *stub_bfd;
2529      bfd *output_bfd;
2530      struct bfd_link_info *link_info;
2531 {
2532   bfd *input_bfd;
2533   asection *section, *stub_sec = 0;
2534   Elf_Internal_Shdr *symtab_hdr;
2535   Elf_Internal_Sym *local_syms, *isym, **all_local_syms;
2536   Elf32_External_Sym *ext_syms, *esym;
2537   unsigned int i, index, bfd_count = 0;
2538   struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
2539   struct elf32_hppa_args_hash_table *args_hash_table = 0;
2540 
2541   /* Create and initialize the stub hash table.  */
2542   stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
2543 		     malloc (sizeof (struct elf32_hppa_stub_hash_table)));
2544   if (!stub_hash_table)
2545     {
2546       bfd_set_error (bfd_error_no_memory);
2547       goto error_return;
2548     }
2549 
2550   if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
2551 					elf32_hppa_stub_hash_newfunc))
2552     goto error_return;
2553 
2554   /* Likewise for the argument location hash table.  */
2555   args_hash_table = ((struct elf32_hppa_args_hash_table *)
2556 		     malloc (sizeof (struct elf32_hppa_args_hash_table)));
2557   if (!args_hash_table)
2558     {
2559       bfd_set_error (bfd_error_no_memory);
2560       goto error_return;
2561     }
2562 
2563   if (!elf32_hppa_args_hash_table_init (args_hash_table,
2564 					elf32_hppa_args_hash_newfunc))
2565     goto error_return;
2566 
2567   /* Attach the hash tables to the main hash table.  */
2568   elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
2569   elf32_hppa_hash_table(link_info)->args_hash_table = args_hash_table;
2570 
2571   /* Count the number of input BFDs.  */
2572   for (input_bfd = link_info->input_bfds;
2573        input_bfd != NULL;
2574        input_bfd = input_bfd->link_next)
2575      bfd_count++;
2576 
2577   /* We want to read in symbol extension records only once.  To do this
2578      we need to read in the local symbols in parallel and save them for
2579      later use; so hold pointers to the local symbols in an array.  */
2580   all_local_syms
2581     = (Elf_Internal_Sym **) malloc (sizeof (Elf_Internal_Sym *) * bfd_count);
2582   if (all_local_syms == NULL)
2583     {
2584       bfd_set_error (bfd_error_no_memory);
2585       goto error_return;
2586     }
2587   memset (all_local_syms, 0, sizeof (Elf_Internal_Sym *) * bfd_count);
2588 
2589   /* Walk over all the input BFDs adding entries to the args hash table
2590      for all the external functions.  */
2591   for (input_bfd = link_info->input_bfds, index = 0;
2592        input_bfd != NULL;
2593        input_bfd = input_bfd->link_next, index++)
2594     {
2595       /* We'll need the symbol table in a second.  */
2596       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2597       if (symtab_hdr->sh_info == 0)
2598 	continue;
2599 
2600       /* We need an array of the local symbols attached to the input bfd.
2601 	 Unfortunately, we're going to have to read & swap them in.  */
2602       local_syms
2603 	= (Elf_Internal_Sym *)malloc (symtab_hdr->sh_info
2604 				      * sizeof (Elf_Internal_Sym));
2605       if (local_syms == NULL)
2606 	{
2607 	  bfd_set_error (bfd_error_no_memory);
2608 	  for (i = 0; i < bfd_count; i++)
2609 	    if (all_local_syms[i])
2610 	      free (all_local_syms[i]);
2611 	  free (all_local_syms);
2612 	  goto error_return;
2613 	}
2614       all_local_syms[index] = local_syms;
2615 
2616       ext_syms
2617 	= (Elf32_External_Sym *)malloc (symtab_hdr->sh_info
2618 					* sizeof (Elf32_External_Sym));
2619       if (ext_syms == NULL)
2620 	{
2621 	  bfd_set_error (bfd_error_no_memory);
2622 	  for (i = 0; i < bfd_count; i++)
2623 	    if (all_local_syms[i])
2624 	      free (all_local_syms[i]);
2625 	  free (all_local_syms);
2626 	  goto error_return;
2627 	}
2628 
2629       if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2630 	  || bfd_read (ext_syms, 1,
2631 		       (symtab_hdr->sh_info
2632 			* sizeof (Elf32_External_Sym)), input_bfd)
2633 	  != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))
2634 	{
2635 	  for (i = 0; i < bfd_count; i++)
2636 	    if (all_local_syms[i])
2637 	      free (all_local_syms[i]);
2638 	  free (all_local_syms);
2639 	  free (ext_syms);
2640 	  goto error_return;
2641 	}
2642 
2643       /* Swap the local symbols in.  */
2644       isym = local_syms;
2645       esym = ext_syms;
2646       for (i = 0; i < symtab_hdr->sh_info; i++, esym++, isym++)
2647 	 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2648 
2649       /* Now we can free the external symbols.  */
2650       free (ext_syms);
2651 
2652       if (elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
2653 				       local_syms) == false)
2654 	{
2655 	  for (i = 0; i < bfd_count; i++)
2656 	    if (all_local_syms[i])
2657 	      free (all_local_syms[i]);
2658 	  free (all_local_syms);
2659 	  goto error_return;
2660 	}
2661     }
2662 
2663   /* Magic as we know the stub bfd only has one section.  */
2664   stub_sec = stub_bfd->sections;
2665 
2666   /* If generating a relocateable output file, then we don't
2667      have to examine the relocs.  */
2668   if (link_info->relocateable)
2669     {
2670       for (i = 0; i < bfd_count; i++)
2671 	if (all_local_syms[i])
2672 	  free (all_local_syms[i]);
2673       free (all_local_syms);
2674       return true;
2675     }
2676 
2677   /* Now that we have argument location information for all the global
2678      functions we can start looking for stubs.  */
2679   for (input_bfd = link_info->input_bfds, index = 0;
2680        input_bfd != NULL;
2681        input_bfd = input_bfd->link_next, index++)
2682     {
2683       /* We'll need the symbol table in a second.  */
2684       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2685       if (symtab_hdr->sh_info == 0)
2686 	continue;
2687 
2688       local_syms = all_local_syms[index];
2689 
2690       /* Walk over each section attached to the input bfd.  */
2691       for (section = input_bfd->sections;
2692 	   section != NULL;
2693 	   section = section->next)
2694 	{
2695 	  Elf_Internal_Shdr *input_rel_hdr;
2696 	  Elf32_External_Rela *external_relocs, *erelaend, *erela;
2697 	  Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2698 
2699 	  /* If there aren't any relocs, then there's nothing to do.  */
2700 	  if ((section->flags & SEC_RELOC) == 0
2701 	      || section->reloc_count == 0)
2702 	    continue;
2703 
2704 	  /* Allocate space for the external relocations.  */
2705 	  external_relocs
2706 	    = (Elf32_External_Rela *) malloc (section->reloc_count * sizeof (Elf32_External_Rela));
2707 	  if (external_relocs == NULL)
2708 	    {
2709 	      bfd_set_error (bfd_error_no_memory);
2710 	      for (i = 0; i < bfd_count; i++)
2711 		if (all_local_syms[i])
2712 		  free (all_local_syms[i]);
2713 	      free (all_local_syms);
2714 	      goto error_return;
2715 	    }
2716 
2717 	  /* Likewise for the internal relocations.  */
2718 	  internal_relocs
2719 	    = (Elf_Internal_Rela *) malloc (section->reloc_count * sizeof (Elf_Internal_Rela));
2720 	  if (internal_relocs == NULL)
2721 	    {
2722 	      bfd_set_error (bfd_error_no_memory);
2723 	      free (external_relocs);
2724 	      for (i = 0; i < bfd_count; i++)
2725 		if (all_local_syms[i])
2726 		  free (all_local_syms[i]);
2727 	      free (all_local_syms);
2728 	      goto error_return;
2729 	    }
2730 
2731 	  /* Read in the external relocs.  */
2732 	  input_rel_hdr = &elf_section_data (section)->rel_hdr;
2733 	  if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2734 	      || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
2735 			   input_bfd) != input_rel_hdr->sh_size)
2736 	    {
2737 	      free (external_relocs);
2738 	      free (internal_relocs);
2739 	      for (i = 0; i < bfd_count; i++)
2740 		if (all_local_syms[i])
2741 		  free (all_local_syms[i]);
2742 	      free (all_local_syms);
2743 	      goto error_return;
2744 	    }
2745 
2746 	  /* Swap in the relocs.  */
2747 	  erela = external_relocs;
2748 	  erelaend = erela + section->reloc_count;
2749 	  irela = internal_relocs;
2750 	  for (; erela < erelaend; erela++, irela++)
2751 	    bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2752 
2753 	  /* We're done with the external relocs, free them.  */
2754 	  free (external_relocs);
2755 
2756 	  /* Now examine each relocation.  */
2757 	  irela = internal_relocs;
2758 	  irelaend = irela + section->reloc_count;
2759 	  for (; irela < irelaend; irela++)
2760 	    {
2761 	      long r_type, callee_args, caller_args, size_of_stub;
2762 	      unsigned long r_index;
2763 	      struct elf_link_hash_entry *hash;
2764 	      struct elf32_hppa_stub_hash_entry *stub_hash;
2765 	      struct elf32_hppa_args_hash_entry *args_hash;
2766 	      Elf_Internal_Sym *sym;
2767 	      asection *sym_sec;
2768 	      const char *sym_name;
2769 	      symvalue sym_value;
2770 	      bfd_vma location, destination;
2771 	      char *new_name = NULL;
2772 
2773 	      r_type = ELF32_R_TYPE (irela->r_info);
2774 	      r_index = ELF32_R_SYM (irela->r_info);
2775 
2776 	      if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
2777 		{
2778 		  bfd_set_error (bfd_error_bad_value);
2779 		  free (internal_relocs);
2780 		  for (i = 0; i < bfd_count; i++)
2781 		    if (all_local_syms[i])
2782 		      free (all_local_syms[i]);
2783 		  free (all_local_syms);
2784 		  goto error_return;
2785 		}
2786 
2787 	      /* Only look for stubs on call instructions or plabel
2788 		 references.  */
2789 	      if (r_type != R_PARISC_PCREL17F
2790 		  && r_type != R_PARISC_PLABEL32
2791 		  && r_type != R_PARISC_PLABEL21L
2792 		  && r_type != R_PARISC_PLABEL14R)
2793 		continue;
2794 
2795 	      /* Now determine the call target, its name, value, section
2796 		 and argument relocation bits.  */
2797 	      hash = NULL;
2798 	      sym = NULL;
2799 	      sym_sec = NULL;
2800 	      if (r_index < symtab_hdr->sh_info)
2801 		{
2802 		  /* It's a local symbol.  */
2803 		  Elf_Internal_Shdr *hdr;
2804 
2805 		  sym = local_syms + r_index;
2806 		  hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2807 		  sym_sec = hdr->bfd_section;
2808 		  sym_name = bfd_elf_string_from_elf_section (input_bfd,
2809 							      symtab_hdr->sh_link,
2810 							      sym->st_name);
2811 		  sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2812 			       ? 0 : sym->st_value);
2813 		  destination = (sym_value
2814 				 + sym_sec->output_offset
2815 				 + sym_sec->output_section->vma);
2816 
2817 		  /* Tack on an ID so we can uniquely identify this local
2818 		     symbol in the stub or arg info hash tables.  */
2819 		  new_name = malloc (strlen (sym_name) + 10);
2820 		  if (new_name == 0)
2821 		    {
2822 		      bfd_set_error (bfd_error_bad_value);
2823 		      free (internal_relocs);
2824 		      for (i = 0; i < bfd_count; i++)
2825 			if (all_local_syms[i])
2826 			  free (all_local_syms[i]);
2827 		      free (all_local_syms);
2828 		      goto error_return;
2829 		    }
2830 		  sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
2831 		  sym_name = new_name;
2832 		}
2833 	      else
2834 		{
2835 		  /* It's an external symbol.  */
2836 		  long index;
2837 
2838 		  index = r_index - symtab_hdr->sh_info;
2839 		  hash = elf_sym_hashes (input_bfd)[index];
2840 		  if (hash->root.type == bfd_link_hash_defined
2841 		      || hash->root.type == bfd_link_hash_defweak)
2842 		    {
2843 		      sym_sec = hash->root.u.def.section;
2844 		      sym_name = hash->root.root.string;
2845 		      sym_value = hash->root.u.def.value;
2846 		      destination = (sym_value
2847 				     + sym_sec->output_offset
2848 				     + sym_sec->output_section->vma);
2849 		    }
2850 		  else
2851 		    {
2852 		      bfd_set_error (bfd_error_bad_value);
2853 		      free (internal_relocs);
2854 		      for (i = 0; i < bfd_count; i++)
2855 			if (all_local_syms[i])
2856 			  free (all_local_syms[i]);
2857 		      free (all_local_syms);
2858 		      goto error_return;
2859 		    }
2860 		}
2861 
2862 	      args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
2863 						       sym_name, false, false);
2864 
2865 	      /* Get both caller and callee argument information.  */
2866 	      if (args_hash == NULL)
2867 		callee_args = 0;
2868 	      else
2869 		callee_args = args_hash->arg_bits;
2870 
2871 	      /* For calls get the caller's bits from the addend of
2872 		 the call relocation.  For PLABELS the caller's bits
2873 		 are assumed to have all args & return values in general
2874 		 registers (0x155).  */
2875 	      if (r_type == R_PARISC_PCREL17F)
2876 		caller_args = HPPA_R_ARG_RELOC (irela->r_addend);
2877 	      else
2878 		caller_args = 0x155;
2879 
2880 	      /* Now determine where the call point is.  */
2881 	      location = (section->output_offset
2882 			  + section->output_section->vma
2883 			  + irela->r_offset);
2884 
2885 	      /* We only care about the destination for PCREL function
2886 		 calls (eg. we don't care for PLABELS).  */
2887 	      if (r_type != R_PARISC_PCREL17F)
2888 		location = destination;
2889 
2890 	      /* Determine what (if any) linker stub is needed and its
2891 		 size (in bytes).  */
2892 	      size_of_stub = elf32_hppa_size_of_stub (callee_args,
2893 						      caller_args,
2894 						      location,
2895 						      destination,
2896 						      sym_name);
2897 	      if (size_of_stub != 0)
2898 		{
2899 		  char *stub_name;
2900 		  unsigned int len;
2901 
2902 		  /* Get the name of this stub.  */
2903 		  len = strlen (sym_name);
2904 		  len += 23;
2905 
2906 		  stub_name = malloc (len);
2907 		  if (!stub_name)
2908 		    {
2909 		      bfd_set_error (bfd_error_no_memory);
2910 
2911 		      /* Because sym_name was mallocd above for local
2912 			 symbols.  */
2913 		      if (r_index < symtab_hdr->sh_info)
2914 			free (new_name);
2915 
2916 		      free (internal_relocs);
2917 		      for (i = 0; i < bfd_count; i++)
2918 			if (all_local_syms[i])
2919 			  free (all_local_syms[i]);
2920 		      free (all_local_syms);
2921 		      goto error_return;
2922 		    }
2923 		  elf32_hppa_name_of_stub (caller_args, callee_args,
2924 					   location, destination, stub_name);
2925 		  strcat (stub_name + 22, sym_name);
2926 
2927 		  /* Because sym_name was malloced above for local symbols.  */
2928 		  if (r_index < symtab_hdr->sh_info)
2929 		    free (new_name);
2930 
2931 		  stub_hash
2932 		    = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
2933 						   false, false);
2934 		  if (stub_hash != NULL)
2935 		    {
2936 		      /* The proper stub has already been created, nothing
2937 			 else to do.  */
2938 		      free (stub_name);
2939 		    }
2940 		  else
2941 		    {
2942 		      bfd_set_section_size (stub_bfd, stub_sec,
2943 					    (bfd_section_size (stub_bfd,
2944 							       stub_sec)
2945 					     + size_of_stub));
2946 
2947 		      /* Enter this entry into the linker stub hash table.  */
2948 		      stub_hash
2949 			= elf32_hppa_stub_hash_lookup (stub_hash_table,
2950 						       stub_name, true, true);
2951 		      if (stub_hash == NULL)
2952 			{
2953 			  bfd_set_error (bfd_error_no_memory);
2954 			  free (stub_name);
2955 			  free (internal_relocs);
2956 			  for (i = 0; i < bfd_count; i++)
2957 			    if (all_local_syms[i])
2958 			      free (all_local_syms[i]);
2959 			  free (all_local_syms);
2960 			  goto error_return;
2961 			}
2962 
2963 		      /* We'll need these to determine the address that the
2964 			 stub will branch to.  */
2965 		      stub_hash->target_value = sym_value;
2966 		      stub_hash->target_section = sym_sec;
2967 		    }
2968 		  free (stub_name);
2969 		}
2970 	    }
2971 	  /* We're done with the internal relocs, free them.  */
2972 	  free (internal_relocs);
2973 	}
2974     }
2975   /* We're done with the local symbols, free them.  */
2976   for (i = 0; i < bfd_count; i++)
2977     if (all_local_syms[i])
2978       free (all_local_syms[i]);
2979   free (all_local_syms);
2980   return true;
2981 
2982 error_return:
2983   /* Return gracefully, avoiding dangling references to the hash tables.  */
2984   if (stub_hash_table)
2985     {
2986       elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
2987       free (stub_hash_table);
2988     }
2989   if (args_hash_table)
2990     {
2991       elf32_hppa_hash_table(link_info)->args_hash_table = NULL;
2992       free (args_hash_table);
2993     }
2994   /* Set the size of the stub section to zero since we're never going
2995      to create them.   Avoids losing when we try to get its contents
2996      too.  */
2997   bfd_set_section_size (stub_bfd, stub_sec, 0);
2998   return false;
2999 }
3000 
3001 /* Misc BFD support code.  */
3002 #define bfd_elf32_bfd_reloc_type_lookup		elf_hppa_reloc_type_lookup
3003 #define bfd_elf32_bfd_is_local_label		hppa_elf_is_local_label
3004 
3005 /* Symbol extension stuff.  */
3006 #define bfd_elf32_set_section_contents		elf32_hppa_set_section_contents
3007 #define elf_backend_symbol_table_processing \
3008   elf32_hppa_backend_symbol_table_processing
3009 #define elf_backend_begin_write_processing \
3010   elf32_hppa_backend_begin_write_processing
3011 #define elf_backend_final_write_processing \
3012   elf32_hppa_backend_final_write_processing
3013 
3014 /* Stuff for the BFD linker.  */
3015 #define elf_backend_relocate_section		elf32_hppa_relocate_section
3016 #define elf_backend_add_symbol_hook		elf32_hppa_add_symbol_hook
3017 #define elf_backend_link_output_symbol_hook \
3018   elf32_hppa_link_output_symbol_hook
3019 #define bfd_elf32_bfd_link_hash_table_create \
3020   elf32_hppa_link_hash_table_create
3021 
3022 #define TARGET_BIG_SYM		bfd_elf32_hppa_vec
3023 #define TARGET_BIG_NAME		"elf32-hppa"
3024 #define ELF_ARCH		bfd_arch_hppa
3025 #define ELF_MACHINE_CODE	EM_PARISC
3026 #define ELF_MAXPAGESIZE		0x1000
3027 
3028 #include "elf32-target.h"
3029