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