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