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