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