OSDN Git Service

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