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