]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-hppa.c
* som.c (hppa_som_gen_reloc_type): Use correct enum type for
[thirdparty/binutils-gdb.git] / bfd / elf32-hppa.c
1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
3
4 Written by
5
6 Center for Software Science
7 Department of Computer Science
8 University of Utah
9
10 This file is part of BFD, the Binary File Descriptor library.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "libbfd.h"
29 #include "obstack.h"
30 #include "bfdlink.h"
31 #include "libelf.h"
32
33 /* Note there isn't much error handling code in here yet. Unexpected
34 conditions are handled by just calling abort. FIXME damnit! */
35
36 /* ELF32/HPPA relocation support
37
38 This file contains ELF32/HPPA relocation support as specified
39 in the Stratus FTX/Golf Object File Format (SED-1762) dated
40 November 19, 1992. */
41
42 #include "elf32-hppa.h"
43 #include "libhppa.h"
44 #include "aout/aout64.h"
45 #include "hppa_stubs.h"
46
47 /* The basic stub types supported. If/when shared libraries are
48 implemented some form of IMPORT and EXPORT stubs will be needed. */
49 typedef enum
50 {
51 HPPA_STUB_ILLEGAL,
52 HPPA_STUB_ARG_RELOC,
53 HPPA_STUB_LONG_CALL,
54 } hppa_stub_type;
55
56 /* This is a list of all the stubs for a particular BFD. */
57
58 typedef struct elf32_hppa_stub_name_list_struct
59 {
60 /* The symbol associated with this stub. */
61 asymbol *sym;
62 /* Pointer to chain of all stub chains. */
63 struct elf32_hppa_stub_description_struct *stub_desc;
64 /* Pointer to the stub contents (eg instructions). */
65 int *stub_secp;
66 /* Size of this stub? (in what units? FIXME). */
67 unsigned size;
68 /* Pointer to the next stub entry in the chain. */
69 struct elf32_hppa_stub_name_list_struct *next;
70 } elf32_hppa_stub_name_list;
71
72 /* This is a linked list in which each entry describes all the
73 linker stubs for a particular bfd. */
74
75 typedef struct elf32_hppa_stub_description_struct
76 {
77 /* The next group of stubs. */
78 struct elf32_hppa_stub_description_struct *next;
79 /* Used to identify this group of stubs as belonging
80 to a particular bfd. */
81 bfd *this_bfd;
82 /* FIXME: The stub section for this group of stubs? Is
83 this redundant with stub_listP->sym->section? */
84 asection *stub_sec;
85 /* FIXME: what the hell is this? */
86 unsigned relocs_allocated_cnt;
87 /* The current real size of the stubs (in bytes?). */
88 unsigned real_size;
89 /* How much space we have allocated for stubs (in bytes?). */
90 unsigned allocated_size;
91 /* Pointer to the first available space for new stubs. */
92 int *stub_secp;
93 /* Pointer to the beginning of the stubs. FIXME: Why an int *
94 above and a char * here? */
95 char *stub_contents;
96 /* The list of stubs for this bfd. */
97 elf32_hppa_stub_name_list *stub_listP;
98 /* I guess we just carry this around for fun. */
99 struct bfd_link_info *link_info;
100 } elf32_hppa_stub_description;
101
102 /* FIXME. */
103 #define ARGUMENTS 0
104 #define RETURN_VALUE 1
105
106 /* The various argument relocations that may be performed.
107 Note GRX,GRY really means ARGX,ARGY. */
108 typedef enum
109 {
110 /* No relocation. */
111 NO_ARG_RELOC,
112 /* Relocate 32 bits from general to FP register. */
113 R_TO_FR,
114 /* Relocate 64 bits from arg0,arg1 to FParg1. */
115 R01_TO_FR,
116 /* Relocate 64 bits from arg2,arg3 to FParg3. */
117 R23_TO_FR,
118 /* Relocate 32 bits from FP to general register. */
119 FR_TO_R,
120 /* Relocate 64 bits from FParg1 to arg0,arg1. */
121 FR_TO_R01,
122 /* Relocate 64 bits from FParg3 to arg2,arg3. */
123 FR_TO_R23,
124 /* Death. */
125 ARG_RELOC_ERR,
126 } arg_reloc_type;
127
128 /* Where (what register type) is an argument comming from? */
129 typedef enum
130 {
131 /* Not in a register. */
132 AR_NO,
133 /* In a general argument register. */
134 AR_GR,
135 /* In right half of a FP argument register. */
136 AR_FR,
137 /* In upper (left) half of a FP argument register. */
138 AR_FU,
139 /* In general argument register pair 0 (arg0, arg1). */
140 AR_DBL01,
141 /* In general argument register pair 1 (arg2, arg3). */
142 AR_DBL23,
143 } arg_location;
144
145 /* What is being relocated (eg which argument or the return value). */
146 typedef enum
147 {
148 ARG0, ARG1, ARG2, ARG3, RETVAL,
149 } arg_reloc_location;
150
151 /* Horizontal represents callee's argument location information, vertical
152 represents caller's argument location information. Value at a particular
153 X, Y location represents what (if any) argument relocation needs to
154 be performed to make caller and callee agree. */
155 static CONST arg_reloc_type mismatches[6][6] =
156 {
157 {NO_ARG_RELOC, NO_ARG_RELOC, NO_ARG_RELOC, NO_ARG_RELOC,
158 NO_ARG_RELOC, NO_ARG_RELOC},
159 {NO_ARG_RELOC, NO_ARG_RELOC, R_TO_FR, ARG_RELOC_ERR,
160 R01_TO_FR, ARG_RELOC_ERR},
161 {NO_ARG_RELOC, FR_TO_R, NO_ARG_RELOC, ARG_RELOC_ERR,
162 ARG_RELOC_ERR, ARG_RELOC_ERR},
163 {ARG_RELOC_ERR, ARG_RELOC_ERR, ARG_RELOC_ERR, ARG_RELOC_ERR,
164 ARG_RELOC_ERR, ARG_RELOC_ERR},
165 {NO_ARG_RELOC, FR_TO_R01, NO_ARG_RELOC, ARG_RELOC_ERR,
166 NO_ARG_RELOC, ARG_RELOC_ERR},
167 {NO_ARG_RELOC, FR_TO_R23, NO_ARG_RELOC, ARG_RELOC_ERR,
168 ARG_RELOC_ERR, NO_ARG_RELOC},
169 };
170
171 /* Likewise for the return value. */
172 static CONST arg_reloc_type retval_mismatches[6][6] =
173 {
174 {NO_ARG_RELOC, NO_ARG_RELOC, NO_ARG_RELOC, NO_ARG_RELOC,
175 NO_ARG_RELOC, NO_ARG_RELOC},
176 {NO_ARG_RELOC, NO_ARG_RELOC, FR_TO_R, ARG_RELOC_ERR,
177 FR_TO_R01, ARG_RELOC_ERR},
178 {NO_ARG_RELOC, R_TO_FR, NO_ARG_RELOC, ARG_RELOC_ERR,
179 ARG_RELOC_ERR, ARG_RELOC_ERR},
180 {ARG_RELOC_ERR, ARG_RELOC_ERR, ARG_RELOC_ERR, ARG_RELOC_ERR,
181 ARG_RELOC_ERR, ARG_RELOC_ERR},
182 {NO_ARG_RELOC, R01_TO_FR, NO_ARG_RELOC, ARG_RELOC_ERR,
183 NO_ARG_RELOC, ARG_RELOC_ERR},
184 {NO_ARG_RELOC, R23_TO_FR, NO_ARG_RELOC, ARG_RELOC_ERR,
185 ARG_RELOC_ERR, NO_ARG_RELOC},
186 };
187
188 /* Used for index mapping in symbol-extension sections. */
189 struct elf32_hppa_symextn_map_struct
190 {
191 int old_index;
192 bfd *bfd;
193 asymbol *sym;
194 int new_index;
195 };
196
197 static bfd_reloc_status_type hppa_elf_reloc
198 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
199
200 static unsigned long hppa_elf_relocate_insn
201 PARAMS ((bfd *, asection *, unsigned long, unsigned long, long,
202 long, unsigned long, unsigned long, unsigned long));
203
204 static void hppa_elf_relocate_unwind_table
205 PARAMS ((bfd *, PTR, unsigned long, long, long,
206 unsigned long, unsigned long));
207
208 static long get_symbol_value PARAMS ((asymbol *));
209
210 static bfd_reloc_status_type hppa_elf_reloc
211 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd*, char **));
212
213 static CONST reloc_howto_type * elf_hppa_reloc_type_lookup
214 PARAMS ((bfd_arch_info_type *, bfd_reloc_code_real_type));
215
216 static symext_entryS elf32_hppa_get_sym_extn PARAMS ((bfd *, asymbol *, int));
217
218 static elf32_hppa_stub_description * find_stubs PARAMS ((bfd *, asection *));
219
220 static elf32_hppa_stub_description * new_stub
221 PARAMS ((bfd *, asection *, struct bfd_link_info *));
222
223 static arg_reloc_type type_of_mismatch PARAMS ((int, int, int));
224
225 static elf32_hppa_stub_name_list * find_stub_by_name
226 PARAMS ((bfd *, asection *, char *));
227
228 static elf32_hppa_stub_name_list * add_stub_by_name
229 PARAMS ((bfd *, asection *, asymbol *, struct bfd_link_info *));
230
231 static void hppa_elf_stub_finish PARAMS ((bfd *));
232
233 static void hppa_elf_stub_reloc
234 PARAMS ((elf32_hppa_stub_description *, bfd *, asymbol **, int,
235 elf32_hppa_reloc_type));
236
237 static int hppa_elf_arg_reloc_needed_p
238 PARAMS ((bfd *, arelent *, arg_reloc_type [5], symext_entryS));
239
240 static asymbol * hppa_elf_build_linker_stub
241 PARAMS ((bfd *, bfd *, struct bfd_link_info *, arelent *,
242 arg_reloc_type [5], int, unsigned *, hppa_stub_type));
243
244 static void hppa_elf_create_stub_sec
245 PARAMS ((bfd *, bfd *, asection **, struct bfd_link_info *));
246
247 static int hppa_elf_long_branch_needed_p
248 PARAMS ((bfd *, asection *, arelent *, asymbol *, unsigned));
249
250 static boolean hppa_elf_set_section_contents
251 PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
252
253 static void elf_info_to_howto
254 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
255
256 static void elf32_hppa_backend_symbol_processing PARAMS ((bfd *, asymbol *));
257
258 static boolean elf32_hppa_backend_section_processing
259 PARAMS ((bfd *, Elf32_Internal_Shdr *));
260
261 static boolean elf32_hppa_backend_symbol_table_processing
262 PARAMS ((bfd *, elf_symbol_type *, int));
263
264 static boolean elf32_hppa_backend_section_from_shdr
265 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
266
267 static boolean elf32_hppa_backend_fake_sections
268 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
269
270 static boolean elf32_hppa_backend_section_from_bfd_section
271 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
272
273 /* ELF/PA relocation howto entries. */
274
275 static reloc_howto_type elf_hppa_howto_table[ELF_HOWTO_TABLE_SIZE] =
276 {
277 {R_HPPA_NONE, 0, 3, 19, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_NONE"},
278 {R_HPPA_32, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_32"},
279 {R_HPPA_11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_11"},
280 {R_HPPA_14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_14"},
281 {R_HPPA_17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_17"},
282 {R_HPPA_L21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_L21"},
283 {R_HPPA_R11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_R11"},
284 {R_HPPA_R14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_R14"},
285 {R_HPPA_R17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_R17"},
286 {R_HPPA_LS21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_LS21"},
287 {R_HPPA_RS11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RS11"},
288 {R_HPPA_RS14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RS14"},
289 {R_HPPA_RS17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RS17"},
290 {R_HPPA_LD21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_LD21"},
291 {R_HPPA_RD11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RD11"},
292 {R_HPPA_RD14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RD14"},
293 {R_HPPA_RD17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RD17"},
294 {R_HPPA_LR21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_LR21"},
295 {R_HPPA_RR14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RR14"},
296 {R_HPPA_RR17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_RR17"},
297 {R_HPPA_GOTOFF_11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_11"},
298 {R_HPPA_GOTOFF_14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_14"},
299 {R_HPPA_GOTOFF_L21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_L21"},
300 {R_HPPA_GOTOFF_R11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_R11"},
301 {R_HPPA_GOTOFF_R14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_R14"},
302 {R_HPPA_GOTOFF_LS21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_LS21"},
303 {R_HPPA_GOTOFF_RS11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_RS11"},
304 {R_HPPA_GOTOFF_RS14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_RS14"},
305 {R_HPPA_GOTOFF_LD21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_LD21"},
306 {R_HPPA_GOTOFF_RD11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_RD11"},
307 {R_HPPA_GOTOFF_RD14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_RD14"},
308 {R_HPPA_GOTOFF_LR21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_LR21"},
309 {R_HPPA_GOTOFF_RR14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_GOTOFF_RR14"},
310 {R_HPPA_ABS_CALL_11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_11"},
311 {R_HPPA_ABS_CALL_14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_14"},
312 {R_HPPA_ABS_CALL_17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_17"},
313 {R_HPPA_ABS_CALL_L21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_L21"},
314 {R_HPPA_ABS_CALL_R11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_R11"},
315 {R_HPPA_ABS_CALL_R14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_R14"},
316 {R_HPPA_ABS_CALL_R17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_R17"},
317 {R_HPPA_ABS_CALL_LS21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_LS21"},
318 {R_HPPA_ABS_CALL_RS11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RS11"},
319 {R_HPPA_ABS_CALL_RS14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RS14"},
320 {R_HPPA_ABS_CALL_RS17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RS17"},
321 {R_HPPA_ABS_CALL_LD21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_LD21"},
322 {R_HPPA_ABS_CALL_RD11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RD11"},
323 {R_HPPA_ABS_CALL_RD14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RD14"},
324 {R_HPPA_ABS_CALL_RD17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RD17"},
325 {R_HPPA_ABS_CALL_LR21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_LR21"},
326 {R_HPPA_ABS_CALL_RR14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RR14"},
327 {R_HPPA_ABS_CALL_RR17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ABS_CALL_RR17"},
328 {R_HPPA_PCREL_CALL_11, 0, 3, 11, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_11"},
329 {R_HPPA_PCREL_CALL_14, 0, 3, 14, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_14"},
330 {R_HPPA_PCREL_CALL_17, 0, 3, 17, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_17"},
331 {R_HPPA_PCREL_CALL_12, 0, 3, 12, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_12"},
332 {R_HPPA_PCREL_CALL_L21, 0, 3, 21, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_L21"},
333 {R_HPPA_PCREL_CALL_R11, 0, 3, 11, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_R11"},
334 {R_HPPA_PCREL_CALL_R14, 0, 3, 14, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_R14"},
335 {R_HPPA_PCREL_CALL_R17, 0, 3, 17, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_R17"},
336 {R_HPPA_PCREL_CALL_LS21, 0, 3, 21, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_LS21"},
337 {R_HPPA_PCREL_CALL_RS11, 0, 3, 11, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RS11"},
338 {R_HPPA_PCREL_CALL_RS14, 0, 3, 14, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RS14"},
339 {R_HPPA_PCREL_CALL_RS17, 0, 3, 17, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RS17"},
340 {R_HPPA_PCREL_CALL_LD21, 0, 3, 21, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_LD21"},
341 {R_HPPA_PCREL_CALL_RD11, 0, 3, 11, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RD11"},
342 {R_HPPA_PCREL_CALL_RD14, 0, 3, 14, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RD14"},
343 {R_HPPA_PCREL_CALL_RD17, 0, 3, 17, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RD17"},
344 {R_HPPA_PCREL_CALL_LR21, 0, 3, 21, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_LR21"},
345 {R_HPPA_PCREL_CALL_RR14, 0, 3, 14, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RR14"},
346 {R_HPPA_PCREL_CALL_RR17, 0, 3, 17, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PCREL_CALL_RR17"},
347 {R_HPPA_PLABEL_32, 0, 3, 32, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PLABEL_32"},
348 {R_HPPA_PLABEL_11, 0, 3, 11, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PLABEL_11"},
349 {R_HPPA_PLABEL_14, 0, 3, 14, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PLABEL_14"},
350 {R_HPPA_PLABEL_L21, 0, 3, 21, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PLABEL_L21"},
351 {R_HPPA_PLABEL_R11, 0, 3, 11, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PLABEL_R11"},
352 {R_HPPA_PLABEL_R14, 0, 3, 14, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_PLABEL_R14"},
353 {R_HPPA_DLT_32, 0, 3, 32, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_DLT_32"},
354 {R_HPPA_DLT_11, 0, 3, 11, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_DLT_11"},
355 {R_HPPA_DLT_14, 0, 3, 14, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_DLT_14"},
356 {R_HPPA_DLT_L21, 0, 3, 21, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_DLT_L21"},
357 {R_HPPA_DLT_R11, 0, 3, 11, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_DLT_R11"},
358 {R_HPPA_DLT_R14, 0, 3, 14, false, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_DLT_R14"},
359 {R_HPPA_UNWIND_ENTRY, 0, 3, 32, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_UNWIND_ENTRY"},
360 {R_HPPA_UNWIND_ENTRIES, 0, 3, 32, true, 0, complain_overflow_signed, hppa_elf_reloc, "R_HPPA_UNWIND_ENTRIES"},
361 {R_HPPA_PUSH_CONST, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_CONST"},
362 {R_HPPA_PUSH_PC, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_PC"},
363 {R_HPPA_PUSH_SYM, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_SYM"},
364 {R_HPPA_PUSH_GOTOFF, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_GOTOFF"},
365 {R_HPPA_PUSH_ABS_CALL, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_ABS_CALL"},
366 {R_HPPA_PUSH_PCREL_CALL, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_PCREL_CALL"},
367 {R_HPPA_PUSH_PLABEL, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_PUSH_PLABEL"},
368 {R_HPPA_MAX, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_MAX"},
369 {R_HPPA_MIN, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_MIN"},
370 {R_HPPA_ADD, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ADD"},
371 {R_HPPA_SUB, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_SUB"},
372 {R_HPPA_MULT, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_MULT"},
373 {R_HPPA_DIV, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_DIV"},
374 {R_HPPA_MOD, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_MOD"},
375 {R_HPPA_AND, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_AND"},
376 {R_HPPA_OR, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_OR"},
377 {R_HPPA_XOR, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_XOR"},
378 {R_HPPA_NOT, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_NOT"},
379 {R_HPPA_LSHIFT, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_LSHIFT"},
380 {R_HPPA_ARITH_RSHIFT, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_ARITH_RSHIFT"},
381 {R_HPPA_LOGIC_RSHIFT, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_LOGIC_RSHIFT"},
382 {R_HPPA_EXPR_F, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_L"},
383 {R_HPPA_EXPR_L, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_L"},
384 {R_HPPA_EXPR_R, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_R"},
385 {R_HPPA_EXPR_LS, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_LS"},
386 {R_HPPA_EXPR_RS, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_RS"},
387 {R_HPPA_EXPR_LD, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_LD"},
388 {R_HPPA_EXPR_RD, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_RD"},
389 {R_HPPA_EXPR_LR, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_LR"},
390 {R_HPPA_EXPR_RR, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_RR"},
391 {R_HPPA_EXPR_32, 0, 3, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_32"},
392 {R_HPPA_EXPR_21, 0, 3, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_21"},
393 {R_HPPA_EXPR_11, 0, 3, 11, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_11"},
394 {R_HPPA_EXPR_14, 0, 3, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_14"},
395 {R_HPPA_EXPR_17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_17"},
396 {R_HPPA_EXPR_12, 0, 3, 12, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_EXPR_12"},
397 {R_HPPA_STUB_CALL_17, 0, 3, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_HPPA_STUB_CALL_17"},
398 {R_HPPA_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_dont, NULL, "R_HPPA_UNIMPLEMENTED"},
399 };
400
401 static symext_chainS *symext_rootP;
402 static symext_chainS *symext_lastP;
403 static boolean symext_chain_built;
404 static long global_value;
405 static long GOT_value;
406 static asymbol *global_symbol;
407 static int global_sym_defined;
408 static symext_entryS *symextn_contents;
409 static unsigned int symextn_contents_real_size;
410 static elf32_hppa_stub_description *elf_hppa_stub_rootP;
411 static boolean stubs_finished = false;
412 static struct elf32_hppa_symextn_map_struct *elf32_hppa_symextn_map;
413 static int elf32_hppa_symextn_map_size;
414
415 static char *linker_stubs = NULL;
416 static int linker_stubs_size = 0;
417 static int linker_stubs_max_size = 0;
418 #define STUB_ALLOC_INCR 100
419 #define STUB_SYM_BUFFER_INC 5
420
421 /* Relocate the given INSN given the various input parameters.
422
423 FIXME: endianness and sizeof (long) issues abound here. */
424
425 static unsigned long
426 hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
427 r_addend, r_format, r_field, pcrel)
428 bfd *abfd;
429 asection *input_sect;
430 unsigned long insn;
431 unsigned long address;
432 long sym_value;
433 long r_addend;
434 unsigned long r_format;
435 unsigned long r_field;
436 unsigned long pcrel;
437 {
438 unsigned char opcode = get_opcode (insn);
439 long constant_value;
440 unsigned arg_reloc;
441
442 switch (opcode)
443 {
444 case LDO:
445 case LDB:
446 case LDH:
447 case LDW:
448 case LDWM:
449 case STB:
450 case STH:
451 case STW:
452 case STWM:
453 case COMICLR:
454 case SUBI:
455 case ADDIT:
456 case ADDI:
457 case LDIL:
458 case ADDIL:
459 constant_value = HPPA_R_CONSTANT (r_addend);
460
461 if (pcrel)
462 sym_value -= address;
463
464 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
465 return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
466
467 case BL:
468 case BE:
469 case BLE:
470 arg_reloc = HPPA_R_ARG_RELOC (r_addend);
471
472 /* XXX computing constant_value is not needed??? */
473 constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
474 (insn & 0x00001ffc) >> 2,
475 insn & 1);
476
477 constant_value = (constant_value << 15) >> 15;
478 if (pcrel)
479 {
480 sym_value -=
481 address + input_sect->output_offset
482 + input_sect->output_section->vma;
483 sym_value = hppa_field_adjust (sym_value, -8, r_field);
484 }
485 else
486 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
487
488 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
489
490 default:
491 if (opcode == 0)
492 {
493 constant_value = HPPA_R_CONSTANT (r_addend);
494
495 if (pcrel)
496 sym_value -= address;
497
498 return hppa_field_adjust (sym_value, constant_value, r_field);
499 }
500 else
501 abort ();
502 }
503 }
504
505 /* Relocate a single unwind entry, or an entire table of them. */
506
507 static void
508 hppa_elf_relocate_unwind_table (abfd, data, address, sym_value,
509 r_addend, r_type, r_field)
510 bfd *abfd;
511 PTR data;
512 unsigned long address;
513 long sym_value;
514 long r_addend;
515 unsigned long r_type;
516 unsigned long r_field;
517 {
518 bfd_byte *hit_data = address + (bfd_byte *) data;
519 long start_offset;
520 long end_offset;
521 long relocated_value;
522 int i;
523
524 switch (r_type)
525 {
526 case R_HPPA_UNWIND_ENTRY:
527 /* Need to relocate the first two 32bit fields in the unwind. They
528 correspond to a function's start and end address. */
529 start_offset = bfd_get_32 (abfd, hit_data);
530 relocated_value = hppa_field_adjust (sym_value, start_offset, r_field);
531 bfd_put_32 (abfd, relocated_value, hit_data);
532
533 hit_data += sizeof (unsigned long);
534 end_offset = bfd_get_32 (abfd, hit_data);
535 relocated_value = hppa_field_adjust (sym_value, end_offset, r_field);
536 bfd_put_32 (abfd, relocated_value, hit_data);
537 break;
538
539 case R_HPPA_UNWIND_ENTRIES:
540 /* Relocate a mass of unwind entires. The count is passed in r_addend
541 (who's braindamaged idea was this anyway? */
542 for (i = 0; i < r_addend; i++, hit_data += 3 * sizeof (unsigned long))
543 {
544 unsigned int adjustment;
545 /* Adjust the first 32bit field in the unwind entry. It's
546 the starting offset of a function. */
547 start_offset = bfd_get_32 (abfd, hit_data);
548 bfd_put_32 (abfd, sym_value, hit_data);
549 adjustment = sym_value - start_offset;
550
551 /* Now adjust the second 32bit field, it's the ending offset
552 of a function. */
553 hit_data += sizeof (unsigned long);
554 end_offset = adjustment + bfd_get_32 (abfd, hit_data);
555 bfd_put_32 (abfd, end_offset, hit_data);
556
557 /* Prepare for the next iteration. */
558 start_offset = bfd_get_32 (abfd,
559 hit_data + 3 * sizeof (unsigned long));
560 sym_value = start_offset + adjustment;
561 }
562 break;
563
564 default:
565 abort ();
566 }
567 }
568
569 /* Return the relocated value of the given symbol. */
570
571 static long
572 get_symbol_value (symbol)
573 asymbol *symbol;
574 {
575 if (symbol == NULL
576 || symbol->section == &bfd_com_section)
577 return 0;
578 else
579 return symbol->value + symbol->section->output_section->vma
580 + symbol->section->output_offset;
581 }
582
583 /* Return one (or more) BFD relocations which implement the base
584 relocation with modifications based on format and field. */
585
586 elf32_hppa_reloc_type **
587 hppa_elf_gen_reloc_type (abfd, base_type, format, field)
588 bfd *abfd;
589 elf32_hppa_reloc_type base_type;
590 int format;
591 int field;
592 {
593 elf32_hppa_reloc_type *finaltype;
594 elf32_hppa_reloc_type **final_types;
595
596 /* Allocate slots for the BFD relocation. */
597 final_types = (elf32_hppa_reloc_type **)
598 bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type *) * 2);
599 BFD_ASSERT (final_types != 0); /* FIXME */
600
601 /* Allocate space for the relocation itself. */
602 finaltype = (elf32_hppa_reloc_type *)
603 bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type));
604 BFD_ASSERT (finaltype != 0); /* FIXME */
605
606 /* Some reasonable defaults. */
607 final_types[0] = finaltype;
608 final_types[1] = NULL;
609
610 #define final_type finaltype[0]
611
612 final_type = base_type;
613
614 /* Just a tangle of nested switch statements to deal with the braindamage
615 that a different field selector means a completely different relocation
616 for PA ELF. */
617 switch (base_type)
618 {
619 case R_HPPA:
620 switch (format)
621 {
622 case 11:
623 switch (field)
624 {
625 case e_fsel:
626 final_type = R_HPPA_11;
627 break;
628 case e_rsel:
629 final_type = R_HPPA_R11;
630 break;
631 case e_rssel:
632 final_type = R_HPPA_RS11;
633 break;
634 case e_rdsel:
635 final_type = R_HPPA_RD11;
636 break;
637 case e_psel:
638 final_type = R_HPPA_PLABEL_11;
639 break;
640 case e_rpsel:
641 final_type = R_HPPA_PLABEL_R11;
642 break;
643 case e_tsel:
644 final_type = R_HPPA_DLT_11;
645 break;
646 case e_rtsel:
647 final_type = R_HPPA_DLT_R11;
648 break;
649 default:
650 abort ();
651 break;
652 }
653 break;
654
655 case 14:
656 switch (field)
657 {
658 case e_rsel:
659 final_type = R_HPPA_R14;
660 break;
661 case e_rssel:
662 final_type = R_HPPA_RS14;
663 break;
664 case e_rdsel:
665 final_type = R_HPPA_RD14;
666 break;
667 case e_rrsel:
668 final_type = R_HPPA_RR14;
669 break;
670 case e_psel:
671 final_type = R_HPPA_PLABEL_14;
672 break;
673 case e_rpsel:
674 final_type = R_HPPA_PLABEL_R14;
675 break;
676 case e_tsel:
677 final_type = R_HPPA_DLT_14;
678 break;
679 case e_rtsel:
680 final_type = R_HPPA_DLT_R14;
681 break;
682 default:
683 abort ();
684 break;
685 }
686 break;
687
688 case 17:
689 switch (field)
690 {
691 case e_fsel:
692 final_type = R_HPPA_17;
693 break;
694 case e_rsel:
695 final_type = R_HPPA_R17;
696 break;
697 case e_rssel:
698 final_type = R_HPPA_RS17;
699 break;
700 case e_rdsel:
701 final_type = R_HPPA_RD17;
702 break;
703 case e_rrsel:
704 final_type = R_HPPA_RR17;
705 break;
706 default:
707 abort ();
708 break;
709 }
710 break;
711
712 case 21:
713 switch (field)
714 {
715 case e_lsel:
716 final_type = R_HPPA_L21;
717 break;
718 case e_lssel:
719 final_type = R_HPPA_LS21;
720 break;
721 case e_ldsel:
722 final_type = R_HPPA_LD21;
723 break;
724 case e_lrsel:
725 final_type = R_HPPA_LR21;
726 break;
727 case e_lpsel:
728 final_type = R_HPPA_PLABEL_L21;
729 break;
730 case e_ltsel:
731 final_type = R_HPPA_PLABEL_L21;
732 break;
733 default:
734 abort ();
735 break;
736 }
737 break;
738
739 case 32:
740 switch (field)
741 {
742 case e_fsel:
743 final_type = R_HPPA_32;
744 break;
745 case e_psel:
746 final_type = R_HPPA_PLABEL_32;
747 break;
748 case e_tsel:
749 final_type = R_HPPA_DLT_32;
750 break;
751 default:
752 abort ();
753 break;
754 }
755 break;
756
757 default:
758 abort ();
759 break;
760 }
761 break;
762
763
764 case R_HPPA_GOTOFF:
765 switch (format)
766 {
767 case 11:
768 switch (field)
769 {
770 case e_rsel:
771 final_type = R_HPPA_GOTOFF_R11;
772 break;
773 case e_rssel:
774 final_type = R_HPPA_GOTOFF_RS11;
775 break;
776 case e_rdsel:
777 final_type = R_HPPA_GOTOFF_RD11;
778 break;
779 case e_fsel:
780 final_type = R_HPPA_GOTOFF_11;
781 break;
782 default:
783 abort ();
784 break;
785 }
786 break;
787
788 case 14:
789 switch (field)
790 {
791 case e_rsel:
792 final_type = R_HPPA_GOTOFF_R14;
793 break;
794 case e_rssel:
795 final_type = R_HPPA_GOTOFF_RS14;
796 break;
797 case e_rdsel:
798 final_type = R_HPPA_GOTOFF_RD14;
799 break;
800 case e_rrsel:
801 final_type = R_HPPA_GOTOFF_RR14;
802 break;
803 case e_fsel:
804 final_type = R_HPPA_GOTOFF_14;
805 break;
806 default:
807 abort ();
808 break;
809 }
810 break;
811
812 case 21:
813 switch (field)
814 {
815 case e_lsel:
816 final_type = R_HPPA_GOTOFF_L21;
817 break;
818 case e_lssel:
819 final_type = R_HPPA_GOTOFF_LS21;
820 break;
821 case e_ldsel:
822 final_type = R_HPPA_GOTOFF_LD21;
823 break;
824 case e_lrsel:
825 final_type = R_HPPA_GOTOFF_LR21;
826 break;
827 default:
828 abort ();
829 break;
830 }
831 break;
832
833 default:
834 abort ();
835 break;
836 }
837 break;
838
839
840 case R_HPPA_PCREL_CALL:
841 switch (format)
842 {
843 case 11:
844 switch (field)
845 {
846 case e_rsel:
847 final_type = R_HPPA_PCREL_CALL_R11;
848 break;
849 case e_rssel:
850 final_type = R_HPPA_PCREL_CALL_RS11;
851 break;
852 case e_rdsel:
853 final_type = R_HPPA_PCREL_CALL_RD11;
854 break;
855 case e_fsel:
856 final_type = R_HPPA_PCREL_CALL_11;
857 break;
858 default:
859 abort ();
860 break;
861 }
862 break;
863
864 case 14:
865 switch (field)
866 {
867 case e_rsel:
868 final_type = R_HPPA_PCREL_CALL_R14;
869 break;
870 case e_rssel:
871 final_type = R_HPPA_PCREL_CALL_RS14;
872 break;
873 case e_rdsel:
874 final_type = R_HPPA_PCREL_CALL_RD14;
875 break;
876 case e_rrsel:
877 final_type = R_HPPA_PCREL_CALL_RR14;
878 break;
879 case e_fsel:
880 final_type = R_HPPA_PCREL_CALL_14;
881 break;
882 default:
883 abort ();
884 break;
885 }
886 break;
887
888 case 17:
889 switch (field)
890 {
891 case e_rsel:
892 final_type = R_HPPA_PCREL_CALL_R17;
893 break;
894 case e_rssel:
895 final_type = R_HPPA_PCREL_CALL_RS17;
896 break;
897 case e_rdsel:
898 final_type = R_HPPA_PCREL_CALL_RD17;
899 break;
900 case e_rrsel:
901 final_type = R_HPPA_PCREL_CALL_RR17;
902 break;
903 case e_fsel:
904 final_type = R_HPPA_PCREL_CALL_17;
905 break;
906 default:
907 abort ();
908 break;
909 }
910 break;
911
912 case 21:
913 switch (field)
914 {
915 case e_lsel:
916 final_type = R_HPPA_PCREL_CALL_L21;
917 break;
918 case e_lssel:
919 final_type = R_HPPA_PCREL_CALL_LS21;
920 break;
921 case e_ldsel:
922 final_type = R_HPPA_PCREL_CALL_LD21;
923 break;
924 case e_lrsel:
925 final_type = R_HPPA_PCREL_CALL_LR21;
926 break;
927 default:
928 abort ();
929 break;
930 }
931 break;
932
933 default:
934 abort ();
935 break;
936 }
937 break;
938
939
940 case R_HPPA_PLABEL:
941 switch (format)
942 {
943 case 11:
944 switch (field)
945 {
946 case e_fsel:
947 final_type = R_HPPA_PLABEL_11;
948 break;
949 case e_rsel:
950 final_type = R_HPPA_PLABEL_R11;
951 break;
952 default:
953 abort ();
954 break;
955 }
956 break;
957
958 case 14:
959 switch (field)
960 {
961 case e_fsel:
962 final_type = R_HPPA_PLABEL_14;
963 break;
964 case e_rsel:
965 final_type = R_HPPA_PLABEL_R14;
966 break;
967 default:
968 abort ();
969 break;
970 }
971 break;
972
973 case 21:
974 switch (field)
975 {
976 case e_lsel:
977 final_type = R_HPPA_PLABEL_L21;
978 break;
979 default:
980 abort ();
981 break;
982 }
983 break;
984
985 case 32:
986 switch (field)
987 {
988 case e_fsel:
989 final_type = R_HPPA_PLABEL_32;
990 break;
991 default:
992 abort ();
993 break;
994 }
995 break;
996
997 default:
998 abort ();
999 break;
1000 }
1001
1002
1003 case R_HPPA_ABS_CALL:
1004 switch (format)
1005 {
1006 case 11:
1007 switch (field)
1008 {
1009 case e_rsel:
1010 final_type = R_HPPA_ABS_CALL_R11;
1011 break;
1012 case e_rssel:
1013 final_type = R_HPPA_ABS_CALL_RS11;
1014 break;
1015 case e_rdsel:
1016 final_type = R_HPPA_ABS_CALL_RD11;
1017 break;
1018 case e_fsel:
1019 final_type = R_HPPA_ABS_CALL_11;
1020 break;
1021 default:
1022 abort ();
1023 break;
1024 }
1025 break;
1026
1027 case 14:
1028 switch (field)
1029 {
1030 case e_rsel:
1031 final_type = R_HPPA_ABS_CALL_R14;
1032 break;
1033 case e_rssel:
1034 final_type = R_HPPA_ABS_CALL_RS14;
1035 break;
1036 case e_rdsel:
1037 final_type = R_HPPA_ABS_CALL_RD14;
1038 break;
1039 case e_rrsel:
1040 final_type = R_HPPA_ABS_CALL_RR14;
1041 break;
1042 case e_fsel:
1043 final_type = R_HPPA_ABS_CALL_14;
1044 break;
1045 default:
1046 abort ();
1047 break;
1048 }
1049 break;
1050
1051 case 17:
1052 switch (field)
1053 {
1054 case e_rsel:
1055 final_type = R_HPPA_ABS_CALL_R17;
1056 break;
1057 case e_rssel:
1058 final_type = R_HPPA_ABS_CALL_RS17;
1059 break;
1060 case e_rdsel:
1061 final_type = R_HPPA_ABS_CALL_RD17;
1062 break;
1063 case e_rrsel:
1064 final_type = R_HPPA_ABS_CALL_RR17;
1065 break;
1066 case e_fsel:
1067 final_type = R_HPPA_ABS_CALL_17;
1068 break;
1069 default:
1070 abort ();
1071 break;
1072 }
1073 break;
1074
1075 case 21:
1076 switch (field)
1077 {
1078 case e_lsel:
1079 final_type = R_HPPA_ABS_CALL_L21;
1080 break;
1081 case e_lssel:
1082 final_type = R_HPPA_ABS_CALL_LS21;
1083 break;
1084 case e_ldsel:
1085 final_type = R_HPPA_ABS_CALL_LD21;
1086 break;
1087 case e_lrsel:
1088 final_type = R_HPPA_ABS_CALL_LR21;
1089 break;
1090 default:
1091 abort ();
1092 break;
1093 }
1094 break;
1095
1096 default:
1097 abort ();
1098 break;
1099 }
1100 break;
1101
1102
1103 case R_HPPA_UNWIND:
1104 final_type = R_HPPA_UNWIND_ENTRY;
1105 break;
1106
1107
1108 case R_HPPA_COMPLEX:
1109 case R_HPPA_COMPLEX_PCREL_CALL:
1110 case R_HPPA_COMPLEX_ABS_CALL:
1111 /* The code originally here was horribly broken, and apparently
1112 never used. Zap it. When we need complex relocations rewrite
1113 it correctly! */
1114 abort ();
1115 break;
1116
1117 default:
1118 final_type = base_type;
1119 break;
1120 }
1121
1122 return final_types;
1123 }
1124
1125 #undef final_type
1126
1127
1128 /* Actually perform a relocation. */
1129
1130 static bfd_reloc_status_type
1131 hppa_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
1132 error_message)
1133 bfd *abfd;
1134 arelent *reloc_entry;
1135 asymbol *symbol_in;
1136 PTR data;
1137 asection *input_section;
1138 bfd *output_bfd;
1139 char **error_message;
1140 {
1141 unsigned long insn;
1142 long sym_value = 0;
1143 unsigned long addr = reloc_entry->address;
1144 bfd_byte *hit_data = addr + (bfd_byte *) data;
1145 unsigned long r_type = reloc_entry->howto->type;
1146 unsigned long r_field = e_fsel;
1147 boolean r_pcrel = reloc_entry->howto->pc_relative;
1148 unsigned r_format = reloc_entry->howto->bitsize;
1149 long r_addend = reloc_entry->addend;
1150
1151 /* If only performing a partial link, get out early. */
1152 if (output_bfd)
1153 {
1154 reloc_entry->address += input_section->output_offset;
1155 return bfd_reloc_ok;
1156 }
1157
1158 /* If performing final link and the symbol we're relocating against
1159 is undefined, then return an error. */
1160 if (symbol_in && symbol_in->section == &bfd_und_section)
1161 return bfd_reloc_undefined;
1162
1163 /* Get the final relocated value. */
1164 sym_value = get_symbol_value (symbol_in);
1165
1166 /* Compute the value of $global$.
1167 FIXME: None of this should be necessary. $global$ is just a
1168 marker and shouldn't really figure into these computations.
1169
1170 Once that's fixed we'll need to teach this backend to change
1171 DP-relative relocations involving symbols in the text section
1172 to be simple absolute relocations. */
1173 if (!global_sym_defined)
1174 {
1175 if (global_symbol)
1176 {
1177 global_value = (global_symbol->value
1178 + global_symbol->section->output_section->vma
1179 + global_symbol->section->output_offset);
1180 GOT_value = global_value;
1181 global_sym_defined++;
1182 }
1183 }
1184
1185 /* Get the instruction word. */
1186 insn = bfd_get_32 (abfd, hit_data);
1187
1188 /* Relocate the value based on one of the basic relocation types
1189
1190 basic_type_1: relocation is relative to $global$
1191 basic_type_2: relocation is relative to the current GOT
1192 basic_type_3: relocation is an absolute call
1193 basic_type_4: relocation is an PC-relative call
1194 basic_type_5: relocation is plabel reference
1195 basic_type_6: relocation is an unwind table relocation
1196 extended_type: unimplemented */
1197
1198 switch (r_type)
1199 {
1200 case R_HPPA_NONE:
1201 break;
1202
1203 /* Handle all the basic type 1 relocations. */
1204 case R_HPPA_32:
1205 case R_HPPA_11:
1206 case R_HPPA_14:
1207 case R_HPPA_17:
1208 r_field = e_fsel;
1209 goto do_basic_type_1;
1210 case R_HPPA_L21:
1211 r_field = e_lsel;
1212 goto do_basic_type_1;
1213 case R_HPPA_R11:
1214 case R_HPPA_R14:
1215 case R_HPPA_R17:
1216 r_field = e_rsel;
1217 goto do_basic_type_1;
1218 case R_HPPA_LS21:
1219 r_field = e_lssel;
1220 goto do_basic_type_1;
1221 case R_HPPA_RS11:
1222 case R_HPPA_RS14:
1223 case R_HPPA_RS17:
1224 r_field = e_ldsel;
1225 goto do_basic_type_1;
1226 case R_HPPA_LD21:
1227 r_field = e_ldsel;
1228 goto do_basic_type_1;
1229 case R_HPPA_RD11:
1230 case R_HPPA_RD14:
1231 case R_HPPA_RD17:
1232 r_field = e_rdsel;
1233 goto do_basic_type_1;
1234 case R_HPPA_LR21:
1235 r_field = e_lrsel;
1236 goto do_basic_type_1;
1237 case R_HPPA_RR14:
1238 case R_HPPA_RR17:
1239 r_field = e_rrsel;
1240
1241 do_basic_type_1:
1242 insn = hppa_elf_relocate_insn (abfd, input_section, insn, addr,
1243 sym_value, r_addend, r_format,
1244 r_field, r_pcrel);
1245 break;
1246
1247 /* Handle all the basic type 2 relocations. */
1248 case R_HPPA_GOTOFF_11:
1249 case R_HPPA_GOTOFF_14:
1250 r_field = e_fsel;
1251 goto do_basic_type_2;
1252 case R_HPPA_GOTOFF_L21:
1253 r_field = e_lsel;
1254 goto do_basic_type_2;
1255 case R_HPPA_GOTOFF_R11:
1256 case R_HPPA_GOTOFF_R14:
1257 r_field = e_rsel;
1258 goto do_basic_type_2;
1259 case R_HPPA_GOTOFF_LS21:
1260 r_field = e_lssel;
1261 goto do_basic_type_2;
1262 case R_HPPA_GOTOFF_RS11:
1263 case R_HPPA_GOTOFF_RS14:
1264 r_field = e_rssel;
1265 goto do_basic_type_2;
1266 case R_HPPA_GOTOFF_LD21:
1267 r_field = e_ldsel;
1268 goto do_basic_type_2;
1269 case R_HPPA_GOTOFF_RD11:
1270 case R_HPPA_GOTOFF_RD14:
1271 r_field = e_rdsel;
1272 goto do_basic_type_2;
1273 case R_HPPA_GOTOFF_LR21:
1274 r_field = e_lrsel;
1275 goto do_basic_type_2;
1276 case R_HPPA_GOTOFF_RR14:
1277 r_field = e_rrsel;
1278
1279 do_basic_type_2:
1280 sym_value -= GOT_value;
1281 insn = hppa_elf_relocate_insn (abfd, input_section, insn, addr,
1282 sym_value, r_addend, r_format,
1283 r_field, r_pcrel);
1284 break;
1285
1286 /* Handle all the basic type 3 relocations. */
1287 case R_HPPA_ABS_CALL_11:
1288 case R_HPPA_ABS_CALL_14:
1289 case R_HPPA_ABS_CALL_17:
1290 r_field = e_fsel;
1291 goto do_basic_type_3;
1292 case R_HPPA_ABS_CALL_L21:
1293 r_field = e_lsel;
1294 goto do_basic_type_3;
1295 case R_HPPA_ABS_CALL_R11:
1296 case R_HPPA_ABS_CALL_R14:
1297 case R_HPPA_ABS_CALL_R17:
1298 r_field = e_rsel;
1299 goto do_basic_type_3;
1300 case R_HPPA_ABS_CALL_LS21:
1301 r_field = e_lssel;
1302 goto do_basic_type_3;
1303 case R_HPPA_ABS_CALL_RS11:
1304 case R_HPPA_ABS_CALL_RS14:
1305 case R_HPPA_ABS_CALL_RS17:
1306 r_field = e_rssel;
1307 goto do_basic_type_3;
1308 case R_HPPA_ABS_CALL_LD21:
1309 r_field = e_ldsel;
1310 goto do_basic_type_3;
1311 case R_HPPA_ABS_CALL_RD11:
1312 case R_HPPA_ABS_CALL_RD14:
1313 case R_HPPA_ABS_CALL_RD17:
1314 r_field = e_rdsel;
1315 goto do_basic_type_3;
1316 case R_HPPA_ABS_CALL_LR21:
1317 r_field = e_lrsel;
1318 goto do_basic_type_3;
1319 case R_HPPA_ABS_CALL_RR14:
1320 case R_HPPA_ABS_CALL_RR17:
1321 r_field = e_rrsel;
1322
1323 do_basic_type_3:
1324 insn = hppa_elf_relocate_insn (abfd, input_section, insn, addr,
1325 sym_value, r_addend, r_format,
1326 r_field, r_pcrel);
1327 break;
1328
1329 /* Handle all the basic type 4 relocations. */
1330 case R_HPPA_PCREL_CALL_11:
1331 case R_HPPA_PCREL_CALL_14:
1332 case R_HPPA_PCREL_CALL_17:
1333 r_field = e_fsel;
1334 goto do_basic_type_4;
1335 case R_HPPA_PCREL_CALL_L21:
1336 r_field = e_lsel;
1337 goto do_basic_type_4;
1338 case R_HPPA_PCREL_CALL_R11:
1339 case R_HPPA_PCREL_CALL_R14:
1340 case R_HPPA_PCREL_CALL_R17:
1341 r_field = e_rsel;
1342 goto do_basic_type_4;
1343 case R_HPPA_PCREL_CALL_LS21:
1344 r_field = e_lssel;
1345 goto do_basic_type_4;
1346 case R_HPPA_PCREL_CALL_RS11:
1347 case R_HPPA_PCREL_CALL_RS14:
1348 case R_HPPA_PCREL_CALL_RS17:
1349 r_field = e_rssel;
1350 goto do_basic_type_4;
1351 case R_HPPA_PCREL_CALL_LD21:
1352 r_field = e_ldsel;
1353 goto do_basic_type_4;
1354 case R_HPPA_PCREL_CALL_RD11:
1355 case R_HPPA_PCREL_CALL_RD14:
1356 case R_HPPA_PCREL_CALL_RD17:
1357 r_field = e_rdsel;
1358 goto do_basic_type_4;
1359 case R_HPPA_PCREL_CALL_LR21:
1360 r_field = e_lrsel;
1361 goto do_basic_type_4;
1362 case R_HPPA_PCREL_CALL_RR14:
1363 case R_HPPA_PCREL_CALL_RR17:
1364 r_field = e_rrsel;
1365
1366 do_basic_type_4:
1367 insn = hppa_elf_relocate_insn (abfd, input_section, insn, addr,
1368 sym_value, r_addend, r_format,
1369 r_field, r_pcrel);
1370 break;
1371
1372 /* Handle all the basic type 5 relocations. */
1373 case R_HPPA_PLABEL_32:
1374 case R_HPPA_PLABEL_11:
1375 case R_HPPA_PLABEL_14:
1376 r_field = e_fsel;
1377 goto do_basic_type_5;
1378 case R_HPPA_PLABEL_L21:
1379 r_field = e_lsel;
1380 goto do_basic_type_5;
1381 case R_HPPA_PLABEL_R11:
1382 case R_HPPA_PLABEL_R14:
1383 r_field = e_rsel;
1384 do_basic_type_5:
1385 insn = hppa_elf_relocate_insn (abfd, input_section, insn, addr,
1386 sym_value, r_addend, r_format,
1387 r_field, r_pcrel);
1388 break;
1389
1390 /* Handle all basic type 6 relocations. */
1391 case R_HPPA_UNWIND_ENTRY:
1392 case R_HPPA_UNWIND_ENTRIES:
1393 hppa_elf_relocate_unwind_table (abfd, data, addr,
1394 sym_value, r_addend,
1395 r_type, r_field);
1396 return bfd_reloc_ok;
1397
1398 /* This is a linker internal relocation. */
1399 case R_HPPA_STUB_CALL_17:
1400 /* This relocation is for a branch to a long branch stub.
1401 Change instruction to a BLE,N. It may also be necessary
1402 to interchange the branch and its delay slot.
1403 The original instruction stream is
1404
1405 bl <foo>,r ; call foo using register r as
1406 ; the return pointer
1407 XXX ; delay slot instruction
1408
1409 The new instruction stream will be:
1410
1411 XXX ; delay slot instruction
1412 ble <foo_stub> ; call the long call stub for foo
1413 ; using r31 as the return pointer
1414
1415 This braindamage is necessary because the compiler may put
1416 an instruction which uses %r31 in the delay slot of the original
1417 call. By changing the call instruction from a "bl" to a "ble"
1418 %r31 gets clobbered before the delay slot executes. This
1419 also means the stub has to play funny games to make sure
1420 we return to the instruction just after the BLE rather than
1421 two instructions after the BLE.
1422
1423 We do not interchange the branch and delay slot if the delay
1424 slot was already nullified, or if the instruction in the delay
1425 slot modifies the return pointer to avoid an unconditional
1426 jump after the call returns (GCC optimization).
1427
1428 None of this horseshit would be necessary if we put the
1429 stubs between functions and just redirected the "bl" to
1430 the stub. Live and learn. */
1431
1432 /* Is this instruction nullified? (does this ever happen?) */
1433 if (insn & 2)
1434 {
1435 insn = BLE_N_XXX_0_0;
1436 bfd_put_32 (abfd, insn, hit_data);
1437 r_type = R_HPPA_ABS_CALL_17;
1438 r_pcrel = 0;
1439 insn = hppa_elf_relocate_insn (abfd, input_section, insn,
1440 addr, sym_value, r_addend,
1441 r_format, r_field, r_pcrel);
1442 }
1443 else
1444 {
1445 /* So much for the trivial case... */
1446 unsigned long old_delay_slot_insn = bfd_get_32 (abfd, hit_data + 4);
1447 unsigned rtn_reg = (insn & 0x03e00000) >> 21;
1448
1449 if (get_opcode (old_delay_slot_insn) == LDO)
1450 {
1451 unsigned ldo_src_reg = (old_delay_slot_insn & 0x03e00000) >> 21;
1452 unsigned ldo_target_reg = (old_delay_slot_insn & 0x001f0000) >> 16;
1453
1454 /* If the target of the LDO is the same as the return
1455 register then there is no reordering. We can leave the
1456 instuction as a non-nullified BLE in this case.
1457
1458 FIXME: This test looks wrong. If we had a ble using
1459 ldo_target_reg as the *source* we'd fuck this up. */
1460 if (ldo_target_reg == rtn_reg)
1461 {
1462 unsigned long new_delay_slot_insn = old_delay_slot_insn;
1463
1464 BFD_ASSERT (ldo_src_reg == ldo_target_reg);
1465 new_delay_slot_insn &= 0xfc00ffff;
1466 new_delay_slot_insn |= ((31 << 21) | (31 << 16));
1467 bfd_put_32 (abfd, new_delay_slot_insn, hit_data + 4);
1468 insn = BLE_XXX_0_0;
1469 r_type = R_HPPA_ABS_CALL_17;
1470 r_pcrel = 0;
1471 insn = hppa_elf_relocate_insn (abfd, input_section, insn,
1472 addr, sym_value, r_addend,
1473 r_format, r_field, r_pcrel);
1474 bfd_put_32 (abfd, insn, hit_data);
1475 return bfd_reloc_ok;
1476 }
1477 else if (rtn_reg == 31)
1478 {
1479 /* The return register is r31, so this is a millicode
1480 call. Do not perform any instruction reordering. */
1481 insn = BLE_XXX_0_0;
1482 r_type = R_HPPA_ABS_CALL_17;
1483 r_pcrel = 0;
1484 insn = hppa_elf_relocate_insn (abfd, input_section, insn,
1485 addr, sym_value,
1486 r_addend, r_format,
1487 r_field, r_pcrel);
1488 bfd_put_32 (abfd, insn, hit_data);
1489 return bfd_reloc_ok;
1490 }
1491 else
1492 {
1493 /* Check to see if the delay slot instruction has a
1494 relocation. If so, we need to change the address
1495 field of it because the instruction it relocates
1496 is going to be moved. Oh what a mess. */
1497 arelent * next_reloc_entry = reloc_entry+1;
1498
1499 if (next_reloc_entry->address == reloc_entry->address + 4)
1500 next_reloc_entry->address -= 4;
1501
1502 insn = old_delay_slot_insn;
1503 bfd_put_32 (abfd, insn, hit_data);
1504 insn = BLE_N_XXX_0_0;
1505 bfd_put_32 (abfd, insn, hit_data + 4);
1506 r_type = R_HPPA_ABS_CALL_17;
1507 r_pcrel = 0;
1508 insn = hppa_elf_relocate_insn (abfd, input_section, insn,
1509 addr + 4,
1510 sym_value, r_addend,
1511 r_format, r_field, r_pcrel);
1512 bfd_put_32 (abfd, insn, hit_data + 4);
1513 return bfd_reloc_ok;
1514 }
1515 }
1516 /* Same comments as above regarding incorrect test. */
1517 else if (rtn_reg == 31)
1518 {
1519 /* The return register is r31, so this is a millicode call.
1520 Perform no instruction reordering in this case. */
1521 insn = BLE_XXX_0_0;
1522 r_type = R_HPPA_ABS_CALL_17;
1523 r_pcrel = 0;
1524 insn = hppa_elf_relocate_insn (abfd, input_section, insn,
1525 addr, sym_value,
1526 r_addend, r_format,
1527 r_field, r_pcrel);
1528 bfd_put_32 (abfd, insn, hit_data);
1529 return bfd_reloc_ok;
1530 }
1531 else
1532 {
1533 /* Check to see if the delay slot instruction has a
1534 relocation. If so, we need to change its address
1535 field because the instruction it relocates is going
1536 to be moved. */
1537 arelent * next_reloc_entry = reloc_entry+1;
1538
1539 if (next_reloc_entry->address == reloc_entry->address + 4)
1540 next_reloc_entry->address -= 4;
1541
1542 insn = old_delay_slot_insn;
1543 bfd_put_32 (abfd, insn, hit_data);
1544 insn = BLE_N_XXX_0_0;
1545 bfd_put_32 (abfd, insn, hit_data + 4);
1546 r_type = R_HPPA_ABS_CALL_17;
1547 r_pcrel = 0;
1548 insn = hppa_elf_relocate_insn (abfd, input_section, insn,
1549 addr + 4, sym_value,
1550 r_addend, r_format,
1551 r_field, r_pcrel);
1552 bfd_put_32 (abfd, insn, hit_data + 4);
1553 return bfd_reloc_ok;
1554 }
1555 }
1556 break;
1557
1558 /* Something we don't know how to handle. */
1559 default:
1560 *error_message = (char *) "Unrecognized reloc";
1561 return bfd_reloc_notsupported;
1562 }
1563
1564 /* Update the instruction word. */
1565 bfd_put_32 (abfd, insn, hit_data);
1566 return (bfd_reloc_ok);
1567 }
1568
1569 /* Return the address of the howto table entry to perform the CODE
1570 relocation for an ARCH machine. */
1571
1572 static CONST reloc_howto_type *
1573 elf_hppa_reloc_type_lookup (arch, code)
1574 bfd_arch_info_type *arch;
1575 bfd_reloc_code_real_type code;
1576 {
1577 if ((int) code < (int) R_HPPA_UNIMPLEMENTED)
1578 {
1579 BFD_ASSERT ((int) elf_hppa_howto_table[(int) code].type == (int) code);
1580 return &elf_hppa_howto_table[(int) code];
1581 }
1582 return NULL;
1583 }
1584
1585
1586 /* Update the symbol extention chain to include the symbol pointed to
1587 by SYMBOLP if SYMBOLP is a function symbol. Used internally and by GAS. */
1588
1589 void
1590 elf_hppa_tc_symbol (abfd, symbolP, sym_idx, symext_root, symext_last)
1591 bfd *abfd;
1592 elf_symbol_type *symbolP;
1593 int sym_idx;
1594 symext_chainS **symext_root;
1595 symext_chainS **symext_last;
1596 {
1597 symext_chainS *symextP;
1598 unsigned int arg_reloc;
1599
1600 /* Only functions can have argument relocations. */
1601 if (!(symbolP->symbol.flags & BSF_FUNCTION))
1602 return;
1603
1604 arg_reloc = symbolP->tc_data.hppa_arg_reloc;
1605
1606 /* If there are no argument relocation bits, then no relocation is
1607 necessary. Do not add this to the symextn section. */
1608 if (arg_reloc == 0)
1609 return;
1610
1611 /* Allocate memory and initialize this entry. */
1612 symextP = (symext_chainS *) bfd_alloc (abfd, sizeof (symext_chainS) * 2);
1613 if (!symextP)
1614 {
1615 bfd_set_error (bfd_error_no_memory);
1616 abort(); /* FIXME */
1617 }
1618
1619 symextP[0].entry = ELF32_HPPA_SX_WORD (HPPA_SXT_SYMNDX, sym_idx);
1620 symextP[0].next = &symextP[1];
1621
1622 symextP[1].entry = ELF32_HPPA_SX_WORD (HPPA_SXT_ARG_RELOC, arg_reloc);
1623 symextP[1].next = NULL;
1624
1625 /* Now update the chain itself so it can be walked later to build
1626 the symbol extension section. */
1627 if (*symext_root == NULL)
1628 {
1629 *symext_root = &symextP[0];
1630 *symext_last = &symextP[1];
1631 }
1632 else
1633 {
1634 (*symext_last)->next = &symextP[0];
1635 *symext_last = &symextP[1];
1636 }
1637 }
1638
1639 /* Build the symbol extension section. Used internally and by GAS. */
1640
1641 void
1642 elf_hppa_tc_make_sections (abfd, symext_root)
1643 bfd *abfd;
1644 symext_chainS *symext_root;
1645 {
1646 symext_chainS *symextP;
1647 int size, n, i;
1648 asection *symextn_sec;
1649
1650 /* FIXME: Huh? I don't see what this is supposed to do for us. */
1651 hppa_elf_stub_finish (abfd);
1652
1653 /* If there are no entries in the symbol extension chain, then
1654 there is no symbol extension section. */
1655 if (symext_root == NULL)
1656 return;
1657
1658 /* Count the number of entries on the chain. */
1659 for (n = 0, symextP = symext_root; symextP; symextP = symextP->next, ++n)
1660 ;
1661
1662 /* Create the symbol extension section and set some appropriate
1663 attributes. */
1664 size = sizeof (symext_entryS) * n;
1665 symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1666 if (symextn_sec == (asection *) 0)
1667 {
1668 symextn_sec = bfd_make_section (abfd, SYMEXTN_SECTION_NAME);
1669 bfd_set_section_flags (abfd,
1670 symextn_sec,
1671 SEC_LOAD | SEC_HAS_CONTENTS | SEC_DATA);
1672 symextn_sec->output_section = symextn_sec;
1673 symextn_sec->output_offset = 0;
1674 bfd_set_section_alignment (abfd, symextn_sec, 2);
1675 }
1676 bfd_set_section_size (abfd, symextn_sec, symextn_contents_real_size);
1677 symextn_contents_real_size = size;
1678
1679 /* Grab some memory for the contents of the symbol extension section
1680 itself. */
1681 symextn_contents = (symext_entryS *) bfd_alloc (abfd, size);
1682 if (!symextn_contents)
1683 {
1684 bfd_set_error (bfd_error_no_memory);
1685 abort(); /* FIXME */
1686 }
1687
1688 /* Fill in the contents of the symbol extension section. */
1689 for (i = 0, symextP = symext_root; symextP; symextP = symextP->next, ++i)
1690 symextn_contents[i] = symextP->entry;
1691
1692 return;
1693 }
1694
1695 /* Return the symbol extension record of type TYPE for the symbol SYM. */
1696
1697 static symext_entryS
1698 elf32_hppa_get_sym_extn (abfd, sym, type)
1699 bfd *abfd;
1700 asymbol *sym;
1701 int type;
1702 {
1703 switch (type)
1704 {
1705 case HPPA_SXT_SYMNDX:
1706 case HPPA_SXT_NULL:
1707 return (symext_entryS) 0;
1708 case HPPA_SXT_ARG_RELOC:
1709 {
1710 elf_symbol_type *esymP = (elf_symbol_type *) sym;
1711
1712 return (symext_entryS) esymP->tc_data.hppa_arg_reloc;
1713 }
1714 /* This should never happen. */
1715 default:
1716 abort();
1717 }
1718 }
1719
1720 /* Search the chain of stub descriptions and locate the stub
1721 description for this the given section within the given bfd.
1722
1723 FIXME: I see yet another wonderful linear linked list search
1724 here. This is probably bad. */
1725
1726 static elf32_hppa_stub_description *
1727 find_stubs (abfd, stub_sec)
1728 bfd *abfd;
1729 asection *stub_sec;
1730 {
1731 elf32_hppa_stub_description *stubP;
1732
1733 for (stubP = elf_hppa_stub_rootP; stubP; stubP = stubP->next)
1734 {
1735 /* Is this the right one? */
1736 if (stubP->this_bfd == abfd && stubP->stub_sec == stub_sec)
1737 return stubP;
1738 }
1739 return NULL;
1740 }
1741
1742 static elf32_hppa_stub_description *
1743 new_stub (abfd, stub_sec, link_info)
1744 bfd *abfd;
1745 asection *stub_sec;
1746 struct bfd_link_info *link_info;
1747 {
1748 elf32_hppa_stub_description *stub = find_stubs (abfd, stub_sec);
1749
1750 /* If we found a list for this bfd, then use it. */
1751 if (stub)
1752 return stub;
1753
1754 /* Nope, allocate and initialize a new entry in the stub list chain. */
1755 stub = (elf32_hppa_stub_description *)
1756 bfd_zalloc (abfd, sizeof (elf32_hppa_stub_description));
1757 if (stub)
1758 {
1759 stub->this_bfd = abfd;
1760 stub->stub_sec = stub_sec;
1761 stub->real_size = 0;
1762 stub->allocated_size = 0;
1763 stub->stub_contents = NULL;
1764 stub->stub_secp = NULL;
1765 stub->link_info = link_info;
1766
1767 stub->next = elf_hppa_stub_rootP;
1768 elf_hppa_stub_rootP = stub;
1769 }
1770 else
1771 {
1772 bfd_set_error (bfd_error_no_memory);
1773 abort(); /* FIXME */
1774 }
1775
1776 return stub;
1777 }
1778
1779 /* Try and locate a stub with the name NAME within the stubs
1780 associated with ABFD. More linked list searches. */
1781
1782 static elf32_hppa_stub_name_list *
1783 find_stub_by_name (abfd, stub_sec, name)
1784 bfd *abfd;
1785 asection *stub_sec;
1786 char *name;
1787 {
1788 /* Find the stubs associated with this bfd. */
1789 elf32_hppa_stub_description *stub = find_stubs (abfd, stub_sec);
1790
1791 /* If found, then we have to walk down them looking for a match. */
1792 if (stub)
1793 {
1794 elf32_hppa_stub_name_list *name_listP;
1795
1796 for (name_listP = stub->stub_listP;
1797 name_listP;
1798 name_listP = name_listP->next)
1799 {
1800 if (!strcmp (name_listP->sym->name, name))
1801 return name_listP;
1802 }
1803 }
1804
1805 /* Not found. */
1806 return 0;
1807 }
1808
1809 /* Add a new stub (SYM) to the list of stubs associated with the given BFD. */
1810 static elf32_hppa_stub_name_list *
1811 add_stub_by_name(abfd, stub_sec, sym, link_info)
1812 bfd *abfd;
1813 asection *stub_sec;
1814 asymbol *sym;
1815 struct bfd_link_info *link_info;
1816 {
1817 elf32_hppa_stub_description *stub = find_stubs (abfd, stub_sec);
1818 elf32_hppa_stub_name_list *stub_entry;
1819
1820 /* If no stubs are associated with this bfd, then we have to make
1821 a chain-of-stubs associated with this bfd. */
1822 if (!stub)
1823 stub = new_stub (abfd, stub_sec, link_info);
1824
1825 if (stub)
1826 {
1827 /* Allocate and initialize an entry in the stub chain. */
1828 stub_entry = (elf32_hppa_stub_name_list *)
1829 bfd_zalloc (abfd, sizeof (elf32_hppa_stub_name_list));
1830
1831 if (stub_entry)
1832 {
1833 stub_entry->size = 0;
1834 stub_entry->sym = sym;
1835 stub_entry->stub_desc = stub;
1836 /* First byte of this stub is the pointer to
1837 the next available location in the stub buffer. */
1838 stub_entry->stub_secp = stub->stub_secp;
1839 /* Add it to the chain. */
1840 if (stub->stub_listP)
1841 stub_entry->next = stub->stub_listP;
1842 else
1843 stub_entry->next = NULL;
1844 stub->stub_listP = stub_entry;
1845 return stub_entry;
1846 }
1847 else
1848 {
1849 bfd_set_error (bfd_error_no_memory);
1850 abort(); /* FIXME */
1851 }
1852 }
1853 /* Death by mis-adventure. */
1854 abort ();
1855 return (elf32_hppa_stub_name_list *)NULL;
1856 }
1857
1858 /* For the given caller/callee argument location information and the
1859 type of relocation (arguments or return value), return the type
1860 of argument relocation needed to make caller and callee happy. */
1861
1862 static arg_reloc_type
1863 type_of_mismatch (caller_bits, callee_bits, type)
1864 int caller_bits;
1865 int callee_bits;
1866 int type;
1867 {
1868 switch (type)
1869 {
1870 case ARGUMENTS:
1871 return mismatches[caller_bits][callee_bits];
1872 case RETURN_VALUE:
1873 return retval_mismatches[caller_bits][callee_bits];
1874 }
1875 return ARG_RELOC_ERR;
1876 }
1877
1878 /* Extract specific argument location bits for WHICH from the
1879 the full argument location information in AR. */
1880 #define EXTRACT_ARBITS(ar, which) ((ar) >> (8 - ((which) * 2))) & 3
1881
1882 /* Add the new instruction INSN into the stub area denoted by ENTRY.
1883 FIXME: Looks like more cases where we assume sizeof (int) ==
1884 sizeof (insn) which may not be true if building cross tools. */
1885 #define NEW_INSTRUCTION(entry, insn) \
1886 { \
1887 *((entry)->stub_desc->stub_secp)++ = (insn); \
1888 (entry)->stub_desc->real_size += sizeof (int); \
1889 (entry)->size += sizeof(int); \
1890 bfd_set_section_size((entry)->stub_desc->this_bfd, \
1891 (entry)->stub_desc->stub_sec, \
1892 (entry)->stub_desc->real_size); \
1893 }
1894
1895 /* Find the offset of the current stub? Looks more like it
1896 finds the offset of the last instruction to me. */
1897 #define CURRENT_STUB_OFFSET(entry) \
1898 ((char *)(entry)->stub_desc->stub_secp \
1899 - (char *)(entry)->stub_desc->stub_contents - 4)
1900
1901 /* All the stubs have already been built, finish up stub stuff
1902 by applying relocations to the stubs. */
1903
1904 static void
1905 hppa_elf_stub_finish (output_bfd)
1906 bfd *output_bfd;
1907 {
1908 elf32_hppa_stub_description *stub_list = elf_hppa_stub_rootP;
1909
1910 /* If the stubs have been finished, then we're already done. */
1911 if (stubs_finished)
1912 return;
1913
1914 /* Walk down the list of stub lists. */
1915 for (; stub_list; stub_list = stub_list->next)
1916 {
1917 /* If this list has stubs, then do something. */
1918 if (stub_list->real_size)
1919 {
1920 bfd *stub_bfd = stub_list->this_bfd;
1921 asection *stub_sec = bfd_get_section_by_name (stub_bfd,
1922 ".hppa_linker_stubs");
1923 bfd_size_type reloc_size;
1924 arelent **reloc_vector;
1925
1926 /* Some sanity checking. */
1927 BFD_ASSERT (stub_sec == stub_list->stub_sec);
1928 BFD_ASSERT (stub_sec);
1929
1930 /* For stub sections raw_size == cooked_size. Also update
1931 reloc_done as we're handling the relocs now. */
1932 stub_sec->_cooked_size = stub_sec->_raw_size;
1933 stub_sec->reloc_done = true;
1934
1935 /* Make space to hold the relocations for the stub section. */
1936 reloc_size = bfd_get_reloc_upper_bound (stub_bfd, stub_sec);
1937 reloc_vector = (arelent **) alloca (reloc_size);
1938
1939 /* If we have relocations, do them. */
1940 if (bfd_canonicalize_reloc (stub_bfd, stub_sec, reloc_vector,
1941 output_bfd->outsymbols))
1942 {
1943 arelent **parent;
1944 for (parent = reloc_vector; *parent != NULL; parent++)
1945 {
1946 char *err = NULL;
1947 bfd_reloc_status_type r =
1948 bfd_perform_relocation (stub_bfd, *parent,
1949 stub_list->stub_contents,
1950 stub_sec, (bfd *) NULL, &err);
1951
1952 /* If there was an error, tell someone about it. */
1953 if (r != bfd_reloc_ok)
1954 {
1955 struct bfd_link_info *link_info = stub_list->link_info;
1956
1957 switch (r)
1958 {
1959 case bfd_reloc_undefined:
1960 if (! ((*link_info->callbacks->undefined_symbol)
1961 (link_info,
1962 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1963 stub_bfd, stub_sec, (*parent)->address)))
1964 abort ();
1965 break;
1966 case bfd_reloc_dangerous:
1967 if (! ((*link_info->callbacks->reloc_dangerous)
1968 (link_info, err, stub_bfd, stub_sec,
1969 (*parent)->address)))
1970 abort ();
1971 break;
1972 case bfd_reloc_overflow:
1973 {
1974 if (! ((*link_info->callbacks->reloc_overflow)
1975 (link_info,
1976 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1977 (*parent)->howto->name,
1978 (*parent)->addend,
1979 stub_bfd, stub_sec,
1980 (*parent)->address)))
1981 abort ();
1982 }
1983 break;
1984 case bfd_reloc_outofrange:
1985 default:
1986 abort ();
1987 break;
1988 }
1989 }
1990 }
1991 }
1992
1993 /* All done with the relocations. Set the final contents
1994 of the stub section. FIXME: no check of return value! */
1995 bfd_set_section_contents (output_bfd, stub_sec,
1996 stub_list->stub_contents,
1997 0, stub_list->real_size);
1998 }
1999 }
2000 /* All done. */
2001 stubs_finished = true;
2002 }
2003
2004 /* Allocate a new relocation entry to be used in a linker stub. */
2005
2006 static void
2007 hppa_elf_stub_reloc (stub_desc, output_bfd, target_sym, offset, type)
2008 elf32_hppa_stub_description *stub_desc;
2009 bfd *output_bfd;
2010 asymbol **target_sym;
2011 int offset;
2012 elf32_hppa_reloc_type type;
2013 {
2014 arelent relent;
2015 int size;
2016 Elf_Internal_Shdr *rela_hdr;
2017
2018 /* I really don't like the realloc nonsense in here. FIXME. */
2019 if (stub_desc->relocs_allocated_cnt == stub_desc->stub_sec->reloc_count)
2020 {
2021 /* Allocate the first few relocation entries. */
2022 if (stub_desc->stub_sec->relocation == NULL)
2023 {
2024 stub_desc->relocs_allocated_cnt = STUB_RELOC_INCR;
2025 size = sizeof (arelent) * stub_desc->relocs_allocated_cnt;
2026 stub_desc->stub_sec->relocation = (arelent *) bfd_zmalloc (size);
2027 }
2028 else
2029 {
2030 /* We've used all the entries we've already allocated. So get
2031 some more. */
2032 stub_desc->relocs_allocated_cnt += STUB_RELOC_INCR;
2033 size = sizeof (arelent) * stub_desc->relocs_allocated_cnt;
2034 stub_desc->stub_sec->relocation = (arelent *)
2035 realloc (stub_desc->stub_sec->relocation, size);
2036 }
2037 if (!stub_desc->stub_sec->relocation)
2038 {
2039 bfd_set_error (bfd_error_no_memory);
2040 abort (); /* FIXME */
2041 }
2042 }
2043
2044 rela_hdr = &elf_section_data(stub_desc->stub_sec)->rel_hdr;
2045 rela_hdr->sh_size += sizeof(Elf32_External_Rela);
2046
2047 /* Fill in the details. */
2048 relent.address = offset;
2049 relent.addend = 0;
2050 relent.sym_ptr_ptr = target_sym;
2051 relent.howto = bfd_reloc_type_lookup (stub_desc->this_bfd, type);
2052
2053 /* Save it in the array of relocations for the stub section. */
2054 memcpy (&stub_desc->stub_sec->relocation[stub_desc->stub_sec->reloc_count++],
2055 &relent, sizeof (arelent));
2056 }
2057
2058 /* Build an argument relocation stub. RTN_ADJUST is a hint that an
2059 adjust to the return pointer from within the stub itself may be
2060 needed. */
2061
2062 static asymbol *
2063 hppa_elf_build_linker_stub (abfd, output_bfd, link_info, reloc_entry,
2064 stub_types, rtn_adjust, data, linker_stub_type)
2065 bfd *abfd;
2066 bfd *output_bfd;
2067 struct bfd_link_info *link_info;
2068 arelent *reloc_entry;
2069 arg_reloc_type stub_types[5];
2070 int rtn_adjust;
2071 unsigned *data;
2072 hppa_stub_type linker_stub_type;
2073 {
2074 int i;
2075 boolean milli, dyncall;
2076 char stub_sym_name[128];
2077 elf32_hppa_stub_name_list *stub_entry;
2078 /* Some initialization. */
2079 unsigned insn = data[0];
2080 asymbol *stub_sym = NULL;
2081 asymbol **orig_sym = reloc_entry->sym_ptr_ptr;
2082 asection *stub_sec = bfd_get_section_by_name (abfd, ".hppa_linker_stubs");
2083 elf32_hppa_stub_description *stub_desc = find_stubs (abfd, stub_sec);
2084
2085 /* Perform some additional checks on whether we should really do the
2086 return adjustment. For example, if the instruction is nullified
2087 or if the delay slot contains an instruction that modifies the return
2088 pointer, then the branch instructions should not be rearranged
2089 (rtn_adjust is false). */
2090 if (insn & 2 || insn == 0)
2091 rtn_adjust = false;
2092 else
2093 {
2094 unsigned delay_insn = data[1];
2095
2096 if (get_opcode (delay_insn) == LDO
2097 && (((insn & 0x03e00000) >> 21) == ((delay_insn & 0x001f0000) >> 16)))
2098 rtn_adjust = false;
2099 }
2100
2101 /* Some special code for long-call stubs. */
2102 if (linker_stub_type == HPPA_STUB_LONG_CALL)
2103 {
2104
2105 /* Is this a millicode call? If so, the return address
2106 comes in on r31 rather than r2 (rp) so a slightly
2107 different code sequence is needed. */
2108 unsigned rtn_reg = (insn & 0x03e00000) >> 21;
2109 if (rtn_reg == 31)
2110 milli = true;
2111
2112 /* Dyncall is special because the user code has already
2113 put the return pointer in %r2 (aka RP). Other millicode
2114 calls have the return pointer in %r31. */
2115 if (strcmp ((*orig_sym)->name, "$$dyncall") == 0)
2116 dyncall = true;
2117
2118 /* If we are creating a call from a stub to another stub, then
2119 never do the instruction reordering. We can tell if we are
2120 going to be calling one stub from another by the fact that
2121 the symbol name has '_stub_' (arg. reloc. stub) or '_lb_stub_'
2122 prepended to the name. Alternatively, the section of the
2123 symbol will be '.hppa_linker_stubs'. This is only an issue
2124 for long-calls; they are the only stubs allowed to call another
2125 stub. */
2126 if ((strncmp ((*orig_sym)->name, "_stub_", 6) == 0)
2127 || (strncmp ((*orig_sym)->name, "_lb_stub_", 9) == 0))
2128 {
2129 BFD_ASSERT (strcmp ((*orig_sym)->section->name, ".hppa_linker_stubs")
2130 == 0);
2131 rtn_adjust = false;
2132 }
2133 }
2134
2135 /* Create the stub section if necessary. */
2136 if (!stub_sec)
2137 {
2138 BFD_ASSERT (stub_desc == NULL);
2139 hppa_elf_create_stub_sec (abfd, output_bfd, &stub_sec, link_info);
2140 stub_desc = new_stub (abfd, stub_sec, link_info);
2141 }
2142
2143 /* Make the stub if we did not find one already. */
2144 if (!stub_desc)
2145 stub_desc = new_stub (abfd, stub_sec, link_info);
2146
2147 /* Allocate space to write the stub.
2148 FIXME: Why using realloc?!? */
2149 if (!stub_desc->stub_contents)
2150 {
2151 stub_desc->allocated_size = STUB_BUFFER_INCR;
2152 stub_desc->stub_contents = (char *) malloc (STUB_BUFFER_INCR);
2153 }
2154 else if ((stub_desc->allocated_size - stub_desc->real_size) < STUB_MAX_SIZE)
2155 {
2156 stub_desc->allocated_size = stub_desc->allocated_size + STUB_BUFFER_INCR;
2157 stub_desc->stub_contents = (char *) realloc (stub_desc->stub_contents,
2158 stub_desc->allocated_size);
2159 }
2160
2161 /* If no memory die. (I seriously doubt the other routines
2162 are prepared to get a NULL return value). */
2163 if (!stub_desc->stub_contents)
2164 {
2165 bfd_set_error (bfd_error_no_memory);
2166 abort ();
2167 }
2168
2169 /* Generate an appropriate name for this stub. */
2170 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2171 sprintf (stub_sym_name,
2172 "_stub_%s_%02d_%02d_%02d_%02d_%02d_%s",
2173 reloc_entry->sym_ptr_ptr[0]->name,
2174 stub_types[0], stub_types[1], stub_types[2],
2175 stub_types[3], stub_types[4],
2176 rtn_adjust ? "RA" : "");
2177 else
2178 sprintf (stub_sym_name,
2179 "_lb_stub_%s_%s", reloc_entry->sym_ptr_ptr[0]->name,
2180 rtn_adjust ? "RA" : "");
2181
2182
2183 stub_desc->stub_secp
2184 = (int *) (stub_desc->stub_contents + stub_desc->real_size);
2185 stub_entry = find_stub_by_name (abfd, stub_sec, stub_sym_name);
2186
2187 /* See if we already have one by this name. */
2188 if (stub_entry)
2189 {
2190 /* Yes, re-use it. Redirect the original relocation from the
2191 old symbol (a function symbol) to the stub (the stub will call
2192 the original function). */
2193 stub_sym = stub_entry->sym;
2194 reloc_entry->sym_ptr_ptr = (asymbol **) bfd_zalloc (abfd,
2195 sizeof (asymbol **));
2196 if (reloc_entry->sym_ptr_ptr == NULL)
2197 {
2198 bfd_set_error (bfd_error_no_memory);
2199 abort ();
2200 }
2201 reloc_entry->sym_ptr_ptr[0] = stub_sym;
2202 if (linker_stub_type == HPPA_STUB_LONG_CALL
2203 || (reloc_entry->howto->type != R_HPPA_PLABEL_32
2204 && (get_opcode(insn) == BLE
2205 || get_opcode (insn) == BE
2206 || get_opcode (insn) == BL)))
2207 reloc_entry->howto = bfd_reloc_type_lookup (abfd, R_HPPA_STUB_CALL_17);
2208 }
2209 else
2210 {
2211 /* Create a new symbol to point to this stub. */
2212 stub_sym = bfd_make_empty_symbol (abfd);
2213 if (!stub_sym)
2214 {
2215 bfd_set_error (bfd_error_no_memory);
2216 abort ();
2217 }
2218 stub_sym->name = bfd_zalloc (abfd, strlen (stub_sym_name) + 1);
2219 if (!stub_sym->name)
2220 {
2221 bfd_set_error (bfd_error_no_memory);
2222 abort ();
2223 }
2224 strcpy ((char *) stub_sym->name, stub_sym_name);
2225 stub_sym->value
2226 = (char *) stub_desc->stub_secp - (char *) stub_desc->stub_contents;
2227 stub_sym->section = stub_sec;
2228 stub_sym->flags = BSF_LOCAL | BSF_FUNCTION;
2229 stub_entry = add_stub_by_name (abfd, stub_sec, stub_sym, link_info);
2230
2231 /* Redirect the original relocation from the old symbol (a function)
2232 to the stub (the stub calls the function). */
2233 reloc_entry->sym_ptr_ptr = (asymbol **) bfd_zalloc (abfd,
2234 sizeof (asymbol **));
2235 if (reloc_entry->sym_ptr_ptr == NULL)
2236 {
2237 bfd_set_error (bfd_error_no_memory);
2238 abort ();
2239 }
2240 reloc_entry->sym_ptr_ptr[0] = stub_sym;
2241 if (linker_stub_type == HPPA_STUB_LONG_CALL
2242 || (reloc_entry->howto->type != R_HPPA_PLABEL_32
2243 && (get_opcode (insn) == BLE
2244 || get_opcode (insn) == BE
2245 || get_opcode (insn) == BL)))
2246 reloc_entry->howto = bfd_reloc_type_lookup (abfd, R_HPPA_STUB_CALL_17);
2247
2248 /* Now generate the code for the stub. Starting with two
2249 common instructions.
2250
2251 FIXME: Do we still need the SP adjustment?
2252 Do we still need to muck with space registers? */
2253 NEW_INSTRUCTION (stub_entry, LDSID_31_1)
2254 NEW_INSTRUCTION (stub_entry, MTSP_1_SR0)
2255
2256 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2257 {
2258 NEW_INSTRUCTION (stub_entry, ADDI_8_SP)
2259
2260 /* Examine each argument, generating code to relocate it
2261 into a different register if necessary. */
2262 for (i = ARG0; i < ARG3; i++)
2263 {
2264 switch (stub_types[i])
2265 {
2266
2267 case NO_ARG_RELOC:
2268 continue;
2269
2270 case R_TO_FR:
2271 switch (i)
2272 {
2273 case ARG0:
2274 NEW_INSTRUCTION (stub_entry, STWS_ARG0_M8SP)
2275 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG0)
2276 break;
2277 case ARG1:
2278 NEW_INSTRUCTION (stub_entry, STWS_ARG1_M8SP)
2279 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG1)
2280 break;
2281 case ARG2:
2282 NEW_INSTRUCTION (stub_entry, STWS_ARG2_M8SP)
2283 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG2)
2284 break;
2285 case ARG3:
2286 NEW_INSTRUCTION (stub_entry, STWS_ARG3_M8SP)
2287 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG3)
2288 break;
2289 }
2290 continue;
2291
2292 case R01_TO_FR:
2293 switch (i)
2294 {
2295 case ARG0:
2296 NEW_INSTRUCTION (stub_entry, STWS_ARG0_M4SP)
2297 NEW_INSTRUCTION (stub_entry, STWS_ARG1_M8SP)
2298 NEW_INSTRUCTION (stub_entry, FLDDS_M8SP_FARG1)
2299 break;
2300 default:
2301 abort ();
2302 break;
2303 }
2304 continue;
2305
2306 case R23_TO_FR:
2307 switch (i)
2308 {
2309 case ARG2:
2310 NEW_INSTRUCTION (stub_entry, STWS_ARG2_M4SP)
2311 NEW_INSTRUCTION (stub_entry, STWS_ARG3_M8SP)
2312 NEW_INSTRUCTION (stub_entry, FLDDS_M8SP_FARG3)
2313 break;
2314 default:
2315 abort ();
2316 break;
2317 }
2318 continue;
2319
2320 case FR_TO_R:
2321 switch (i)
2322 {
2323 case ARG0:
2324 NEW_INSTRUCTION (stub_entry, FSTWS_FARG0_M8SP)
2325 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG0)
2326 break;
2327 case ARG1:
2328 NEW_INSTRUCTION (stub_entry, FSTWS_FARG1_M8SP)
2329 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG1)
2330 break;
2331 case ARG2:
2332 NEW_INSTRUCTION (stub_entry, FSTWS_FARG2_M8SP)
2333 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG2)
2334 break;
2335 case ARG3:
2336 NEW_INSTRUCTION (stub_entry, FSTWS_FARG3_M8SP)
2337 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG3)
2338 break;
2339 }
2340 continue;
2341
2342 case FR_TO_R01:
2343 switch (i)
2344 {
2345 case ARG0:
2346 NEW_INSTRUCTION (stub_entry, FSTDS_FARG1_M8SP)
2347 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG0)
2348 NEW_INSTRUCTION (stub_entry, LDWS_M8SP_ARG1)
2349 break;
2350 default:
2351 abort ();
2352 break;
2353 }
2354 continue;
2355
2356 case FR_TO_R23:
2357 switch (i)
2358 {
2359 case ARG2:
2360 NEW_INSTRUCTION (stub_entry, FSTDS_FARG3_M8SP)
2361 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG2)
2362 NEW_INSTRUCTION (stub_entry, LDWS_M8SP_ARG3)
2363 break;
2364 default:
2365 abort ();
2366 break;
2367 }
2368 continue;
2369
2370 default:
2371 abort ();
2372 break;
2373 }
2374 }
2375
2376 /* Put the stack pointer back. FIXME: Is this still necessary? */
2377 NEW_INSTRUCTION (stub_entry, ADDI_M8_SP_SP)
2378 }
2379
2380 /* Common code again. Return pointer adjustment and the like. */
2381 if (!dyncall)
2382 {
2383 /* This isn't dyncall. */
2384 if (!milli)
2385 {
2386 /* It's not a millicode call, so get the correct return
2387 value into %r2 (aka RP). */
2388 if (rtn_adjust)
2389 NEW_INSTRUCTION (stub_entry, ADDI_M4_31_RP)
2390 else
2391 NEW_INSTRUCTION (stub_entry, COPY_31_2)
2392 }
2393 else
2394 {
2395 /* It is a millicode call, so get the correct return
2396 value into %r1?!?. FIXME: Shouldn't this be
2397 %r31? Yes, and a little re-arrangement of the
2398 code below would make that possible. */
2399 if (rtn_adjust)
2400 NEW_INSTRUCTION (stub_entry, ADDI_M4_31_1)
2401 else
2402 NEW_INSTRUCTION (stub_entry, COPY_31_1)
2403 }
2404 }
2405 else
2406 {
2407 /* This is dyncall, so the code is a little different as the
2408 return pointer is already in %r2 (aka RP). */
2409 if (rtn_adjust)
2410 NEW_INSTRUCTION (stub_entry, ADDI_M4_31_RP)
2411 }
2412
2413 /* Save the return address. */
2414 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2415 NEW_INSTRUCTION (stub_entry, STW_RP_M8SP)
2416
2417 /* Long branch to the target function. */
2418 NEW_INSTRUCTION (stub_entry, LDIL_XXX_31)
2419 hppa_elf_stub_reloc (stub_entry->stub_desc,
2420 abfd, orig_sym,
2421 CURRENT_STUB_OFFSET (stub_entry),
2422 R_HPPA_L21);
2423 NEW_INSTRUCTION (stub_entry, BLE_XXX_0_31)
2424 hppa_elf_stub_reloc (stub_entry->stub_desc,
2425 abfd, orig_sym,
2426 CURRENT_STUB_OFFSET (stub_entry),
2427 R_HPPA_ABS_CALL_R17);
2428
2429 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2430 {
2431 /* In delay slot of long-call, copy %r31 into %r2 so that
2432 the callee can return in the normal fashion. */
2433 NEW_INSTRUCTION (stub_entry, COPY_31_2)
2434
2435 /* Restore the return address. */
2436 NEW_INSTRUCTION (stub_entry, LDW_M8SP_RP)
2437
2438 /* Generate the code to move the return value around. */
2439 switch (stub_types[RETVAL])
2440 {
2441 case NO_ARG_RELOC:
2442 break;
2443
2444 case R_TO_FR:
2445 NEW_INSTRUCTION (stub_entry, STWS_RET0_M8SP)
2446 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FRET0)
2447 break;
2448
2449 case FR_TO_R:
2450 NEW_INSTRUCTION (stub_entry, FSTWS_FRET0_M8SP)
2451 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_RET0)
2452 break;
2453
2454 default:
2455 abort ();
2456 break;
2457 }
2458
2459 /* Return back to the main code stream. */
2460 NEW_INSTRUCTION (stub_entry, BV_N_0_RP)
2461 }
2462 else
2463 {
2464 if (!dyncall)
2465 {
2466 /* Get return address into %r31. Both variants may be necessary
2467 (I think) as we could be cascading into another stub. */
2468 if (!milli)
2469 NEW_INSTRUCTION (stub_entry, COPY_2_31)
2470 else
2471 NEW_INSTRUCTION (stub_entry, COPY_1_31)
2472 }
2473 else
2474 {
2475 /* Get the return address into %r31 too. Might be necessary
2476 (I think) as we could be cascading into another stub. */
2477 NEW_INSTRUCTION (stub_entry, COPY_2_31)
2478 }
2479
2480 /* No need for a return to the main stream. */
2481 }
2482 }
2483 return stub_sym;
2484 }
2485
2486 /* Return nonzero if an argument relocation will be needed to call
2487 the function (symbol in RELOC_ENTRY) assuming the caller has
2488 argument relocation bugs CALLER_AR. */
2489
2490 static int
2491 hppa_elf_arg_reloc_needed_p (abfd, reloc_entry, stub_types, caller_ar)
2492 bfd *abfd;
2493 arelent *reloc_entry;
2494 arg_reloc_type stub_types[5];
2495 symext_entryS caller_ar;
2496 {
2497 /* If the symbol is still undefined, then it's impossible to know
2498 if an argument relocation is needed. */
2499 if (reloc_entry->sym_ptr_ptr[0]
2500 && reloc_entry->sym_ptr_ptr[0]->section != &bfd_und_section)
2501 {
2502 symext_entryS callee_ar = elf32_hppa_get_sym_extn (abfd,
2503 reloc_entry->sym_ptr_ptr[0],
2504 HPPA_SXT_ARG_RELOC);
2505
2506 /* Now examine all the argument and return value location
2507 information to determine if a relocation stub will be needed. */
2508 if (caller_ar && callee_ar)
2509 {
2510 arg_location caller_loc[5];
2511 arg_location callee_loc[5];
2512
2513 /* Extract the location information for the return value
2514 and argument registers separately. */
2515 callee_loc[RETVAL] = EXTRACT_ARBITS (callee_ar, RETVAL);
2516 caller_loc[RETVAL] = EXTRACT_ARBITS (caller_ar, RETVAL);
2517 callee_loc[ARG0] = EXTRACT_ARBITS (callee_ar, ARG0);
2518 caller_loc[ARG0] = EXTRACT_ARBITS (caller_ar, ARG0);
2519 callee_loc[ARG1] = EXTRACT_ARBITS (callee_ar, ARG1);
2520 caller_loc[ARG1] = EXTRACT_ARBITS (caller_ar, ARG1);
2521 callee_loc[ARG2] = EXTRACT_ARBITS (callee_ar, ARG2);
2522 caller_loc[ARG2] = EXTRACT_ARBITS (caller_ar, ARG2);
2523 callee_loc[ARG3] = EXTRACT_ARBITS (callee_ar, ARG3);
2524 caller_loc[ARG3] = EXTRACT_ARBITS (caller_ar, ARG3);
2525
2526 /* Check some special combinations. For example, if FU
2527 appears in ARG1 or ARG3, we can move it to ARG0 or ARG2,
2528 respectively. (I guess this braindamage is correct? It'd
2529 take an hour or two of reading PA calling conventions to
2530 really know). */
2531
2532 if (caller_loc[ARG0] == AR_FU || caller_loc[ARG1] == AR_FU)
2533 {
2534 caller_loc[ARG0] = AR_DBL01;
2535 caller_loc[ARG1] = AR_NO;
2536 }
2537 if (caller_loc[ARG2] == AR_FU || caller_loc[ARG3] == AR_FU)
2538 {
2539 caller_loc[ARG2] = AR_DBL23;
2540 caller_loc[ARG3] = AR_NO;
2541 }
2542 if (callee_loc[ARG0] == AR_FU || callee_loc[ARG1] == AR_FU)
2543 {
2544 callee_loc[ARG0] = AR_DBL01;
2545 callee_loc[ARG1] = AR_NO;
2546 }
2547 if (callee_loc[ARG2] == AR_FU || callee_loc[ARG3] == AR_FU)
2548 {
2549 callee_loc[ARG2] = AR_DBL23;
2550 callee_loc[ARG3] = AR_NO;
2551 }
2552
2553 /* Now look up potential mismatches. */
2554 stub_types[ARG0] = type_of_mismatch (caller_loc[ARG0],
2555 callee_loc[ARG0],
2556 ARGUMENTS);
2557 stub_types[ARG1] = type_of_mismatch (caller_loc[ARG1],
2558 callee_loc[ARG1],
2559 ARGUMENTS);
2560 stub_types[ARG2] = type_of_mismatch (caller_loc[ARG2],
2561 callee_loc[ARG2],
2562 ARGUMENTS);
2563 stub_types[ARG3] = type_of_mismatch (caller_loc[ARG3],
2564 callee_loc[ARG3],
2565 ARGUMENTS);
2566 stub_types[RETVAL] = type_of_mismatch (caller_loc[RETVAL],
2567 callee_loc[RETVAL],
2568 RETURN_VALUE);
2569
2570 /* If any of the arguments or return value need an argument
2571 relocation, then we will need an argument relocation stub. */
2572 if (stub_types[ARG0] != NO_ARG_RELOC
2573 || stub_types[ARG1] != NO_ARG_RELOC
2574 || stub_types[ARG2] != NO_ARG_RELOC
2575 || stub_types[ARG3] != NO_ARG_RELOC
2576 || stub_types[RETVAL] != NO_ARG_RELOC)
2577 return 1;
2578 }
2579 }
2580 return 0;
2581 }
2582
2583 /* Create the linker stub section. */
2584
2585 static void
2586 hppa_elf_create_stub_sec (abfd, output_bfd, secptr, link_info)
2587 bfd *abfd;
2588 bfd *output_bfd;
2589 asection **secptr;
2590 struct bfd_link_info *link_info;
2591 {
2592 asection *output_text_section;
2593
2594 output_text_section = bfd_get_section_by_name (output_bfd, ".text");
2595 *secptr = bfd_make_section (abfd, ".hppa_linker_stubs");
2596 bfd_set_section_flags (abfd, *secptr,
2597 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
2598 | SEC_RELOC | SEC_CODE | SEC_READONLY);
2599 (*secptr)->output_section = output_text_section->output_section;
2600 (*secptr)->output_offset = 0;
2601
2602 /* Set up the ELF section header for this new section. This
2603 is basically the same processing as elf_make_sections().
2604 elf_make_sections is static and therefore not accessable
2605 here. */
2606 {
2607 Elf_Internal_Shdr *this_hdr;
2608 this_hdr = &elf_section_data ((*secptr))->this_hdr;
2609
2610 /* Set the sizes of this section. The contents have already
2611 been set up ?!? */
2612 this_hdr->sh_addr = (*secptr)->vma;
2613 this_hdr->sh_size = (*secptr)->_raw_size;
2614
2615 /* Set appropriate flags for sections with relocations. */
2616 if ((*secptr)->flags & SEC_RELOC)
2617 {
2618 Elf_Internal_Shdr *rela_hdr;
2619 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
2620
2621 rela_hdr = &elf_section_data ((*secptr))->rel_hdr;
2622
2623 if (use_rela_p)
2624 {
2625 rela_hdr->sh_type = SHT_RELA;
2626 rela_hdr->sh_entsize = sizeof (Elf32_External_Rela);
2627 }
2628 else
2629 {
2630 rela_hdr->sh_type = SHT_REL;
2631 rela_hdr->sh_entsize = sizeof (Elf32_External_Rel);
2632 }
2633 rela_hdr->sh_flags = 0;
2634 rela_hdr->sh_addr = 0;
2635 rela_hdr->sh_offset = 0;
2636 rela_hdr->sh_addralign = 0;
2637 rela_hdr->size = 0;
2638 }
2639
2640 if ((*secptr)->flags & SEC_ALLOC)
2641 this_hdr->sh_flags |= SHF_ALLOC;
2642
2643 if (!((*secptr)->flags & SEC_READONLY))
2644 this_hdr->sh_flags |= SHF_WRITE;
2645
2646 if ((*secptr)->flags & SEC_CODE)
2647 this_hdr->sh_flags |= SHF_EXECINSTR;
2648 }
2649
2650 bfd_set_section_alignment (abfd, *secptr, 2);
2651 }
2652
2653 /* Return nonzero if a long-call stub will be needed to call the
2654 function (symbol in RELOC_ENTRY). */
2655
2656 static int
2657 hppa_elf_long_branch_needed_p (abfd, asec, reloc_entry, symbol, insn)
2658 bfd *abfd;
2659 asection *asec;
2660 arelent *reloc_entry;
2661 asymbol *symbol;
2662 unsigned insn;
2663 {
2664 long sym_value = get_symbol_value (symbol);
2665 int fmt = reloc_entry->howto->bitsize;
2666 unsigned char op = get_opcode (insn);
2667 unsigned raddr;
2668
2669 #define too_far(val,num_bits) \
2670 ((int)(val) > (1 << (num_bits)) - 1) || ((int)(val) < (-1 << (num_bits)))
2671
2672 switch (op)
2673 {
2674 case BL:
2675 raddr =
2676 reloc_entry->address + asec->output_offset + asec->output_section->vma;
2677 /* If the symbol and raddr (relocated addr?) are too far away from
2678 each other, then a long-call stub will be needed. */
2679 if (too_far (sym_value - raddr, fmt + 1))
2680 return 1;
2681 break;
2682 }
2683 return 0;
2684 }
2685
2686 /* Search the given section and determine if linker stubs will be
2687 needed for any calls within that section.
2688
2689 Return any new stub symbols created.
2690
2691 Used out of hppaelf.em in the linker. */
2692
2693 asymbol *
2694 hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec,
2695 new_sym_cnt, link_info)
2696 bfd *stub_bfd;
2697 bfd *abfd;
2698 bfd *output_bfd;
2699 asection *asec;
2700 int *new_sym_cnt;
2701 struct bfd_link_info *link_info;
2702 {
2703 int i;
2704 arg_reloc_type stub_types[5];
2705 asymbol *new_syms = NULL;
2706 int new_cnt = 0;
2707 int new_max = 0;
2708
2709 /* Relocations are in different places depending on whether this is
2710 an output section or an input section. Also, the relocations are
2711 in different forms. Sigh. Luckily, we have bfd_canonicalize_reloc()
2712 to straighten this out for us . */
2713 if (asec->reloc_count > 0)
2714 {
2715 arelent **reloc_vector
2716 = (arelent **) alloca (asec->reloc_count * (sizeof (arelent *) + 1));
2717
2718 /* Make sure the canonical symbols are hanging around in a convient
2719 location. */
2720 if (bfd_get_outsymbols (abfd) == NULL)
2721 {
2722 size_t symsize;
2723
2724 symsize = get_symtab_upper_bound (abfd);
2725 abfd->outsymbols = (asymbol **) bfd_alloc (abfd, symsize);
2726 if (!abfd->outsymbols)
2727 {
2728 bfd_set_error (bfd_error_no_memory);
2729 abort ();
2730 }
2731 abfd->symcount = bfd_canonicalize_symtab (abfd, abfd->outsymbols);
2732 }
2733
2734 /* Now get the relocations. */
2735 bfd_canonicalize_reloc (abfd, asec, reloc_vector,
2736 bfd_get_outsymbols (abfd));
2737
2738 /* Examine each relocation entry in this section. */
2739 for (i = 0; i < asec->reloc_count; i++)
2740 {
2741 arelent *rle = reloc_vector[i];
2742
2743 switch (rle->howto->type)
2744 {
2745 /* Any call could need argument relocation stubs, and
2746 some may need long-call stubs. */
2747 case R_HPPA_ABS_CALL_11:
2748 case R_HPPA_ABS_CALL_14:
2749 case R_HPPA_ABS_CALL_17:
2750 case R_HPPA_ABS_CALL_L21:
2751 case R_HPPA_ABS_CALL_R11:
2752 case R_HPPA_ABS_CALL_R14:
2753 case R_HPPA_ABS_CALL_R17:
2754 case R_HPPA_ABS_CALL_LS21:
2755 case R_HPPA_ABS_CALL_RS11:
2756 case R_HPPA_ABS_CALL_RS14:
2757 case R_HPPA_ABS_CALL_RS17:
2758 case R_HPPA_ABS_CALL_LD21:
2759 case R_HPPA_ABS_CALL_RD11:
2760 case R_HPPA_ABS_CALL_RD14:
2761 case R_HPPA_ABS_CALL_RD17:
2762 case R_HPPA_ABS_CALL_LR21:
2763 case R_HPPA_ABS_CALL_RR14:
2764 case R_HPPA_ABS_CALL_RR17:
2765 case R_HPPA_PCREL_CALL_11:
2766 case R_HPPA_PCREL_CALL_14:
2767 case R_HPPA_PCREL_CALL_17:
2768 case R_HPPA_PCREL_CALL_12:
2769 case R_HPPA_PCREL_CALL_L21:
2770 case R_HPPA_PCREL_CALL_R11:
2771 case R_HPPA_PCREL_CALL_R14:
2772 case R_HPPA_PCREL_CALL_R17:
2773 case R_HPPA_PCREL_CALL_LS21:
2774 case R_HPPA_PCREL_CALL_RS11:
2775 case R_HPPA_PCREL_CALL_RS14:
2776 case R_HPPA_PCREL_CALL_RS17:
2777 case R_HPPA_PCREL_CALL_LD21:
2778 case R_HPPA_PCREL_CALL_RD11:
2779 case R_HPPA_PCREL_CALL_RD14:
2780 case R_HPPA_PCREL_CALL_RD17:
2781 case R_HPPA_PCREL_CALL_LR21:
2782 case R_HPPA_PCREL_CALL_RR14:
2783 case R_HPPA_PCREL_CALL_RR17:
2784 {
2785 symext_entryS caller_ar
2786 = (symext_entryS) HPPA_R_ARG_RELOC (rle->addend);
2787 unsigned insn[2];
2788
2789 /* We'll need this for the long-call checks. */
2790 bfd_get_section_contents (abfd, asec, insn, rle->address,
2791 sizeof(insn));
2792
2793 /* See if this call needs an argument relocation stub. */
2794 if (hppa_elf_arg_reloc_needed_p (abfd, rle, stub_types,
2795 caller_ar))
2796 {
2797 /* Generate a stub and keep track of the new symbol. */
2798 asymbol *r;
2799
2800 if (new_cnt == new_max)
2801 {
2802 new_max += STUB_SYM_BUFFER_INC;
2803 new_syms = (asymbol *)
2804 realloc (new_syms, new_max * sizeof (asymbol));
2805 if (new_syms == NULL)
2806 abort ();
2807 }
2808
2809 /* Build the argument relocation stub. */
2810 r = hppa_elf_build_linker_stub (stub_bfd, output_bfd,
2811 link_info, rle,
2812 stub_types, true, insn,
2813 HPPA_STUB_ARG_RELOC);
2814 new_syms[new_cnt++] = *r;
2815 }
2816
2817 /* See if this call needs a long-call stub. */
2818 if (hppa_elf_long_branch_needed_p (abfd, asec, rle,
2819 rle->sym_ptr_ptr[0],
2820 insn[0]))
2821 {
2822 /* Generate a stub and keep track of the new symbol. */
2823 asymbol *r;
2824
2825 if (new_cnt == new_max)
2826 {
2827 new_max += STUB_SYM_BUFFER_INC;
2828 new_syms = (asymbol *)
2829 realloc (new_syms, (new_max * sizeof (asymbol)));
2830 if (! new_syms)
2831 abort ();
2832 }
2833
2834 /* Build the long-call stub. */
2835 r = hppa_elf_build_linker_stub (stub_bfd, output_bfd,
2836 link_info, rle,
2837 NULL, true, insn,
2838 HPPA_STUB_LONG_CALL);
2839 new_syms[new_cnt++] = *r;
2840 }
2841 }
2842 break;
2843
2844 /* PLABELs may need argument relocation stubs. */
2845 case R_HPPA_PLABEL_32:
2846 case R_HPPA_PLABEL_11:
2847 case R_HPPA_PLABEL_14:
2848 case R_HPPA_PLABEL_L21:
2849 case R_HPPA_PLABEL_R11:
2850 case R_HPPA_PLABEL_R14:
2851 {
2852 /* On a plabel relocation, assume the arguments of the
2853 caller are set up in general registers (indirect
2854 calls only use general registers.
2855 NOTE: 0x155 = ARGW0=GR,ARGW1=GR,ARGW2=GR,RETVAL=GR. */
2856 symext_entryS caller_ar = (symext_entryS) 0x155;
2857 unsigned insn[2];
2858
2859 /* Do we really need this? */
2860 bfd_get_section_contents (abfd, asec, insn, rle->address,
2861 sizeof(insn));
2862
2863 /* See if this call needs an argument relocation stub. */
2864 if (hppa_elf_arg_reloc_needed_p (abfd, rle, stub_types,
2865 caller_ar))
2866 {
2867 /* Generate a plabel stub and keep track of the
2868 new symbol. */
2869 asymbol *r;
2870 int rtn_adjust;
2871
2872 if (new_cnt == new_max)
2873 {
2874 new_max += STUB_SYM_BUFFER_INC;
2875 new_syms = (asymbol *) realloc (new_syms, new_max
2876 * sizeof (asymbol));
2877 }
2878
2879 /* Determine whether a return adjustment
2880 (see the relocation code for relocation type
2881 R_HPPA_STUB_CALL_17) is possible. Basically,
2882 determine whether we are looking at a branch or not. */
2883 if (rle->howto->type == R_HPPA_PLABEL_32)
2884 rtn_adjust = false;
2885 else
2886 {
2887 switch (get_opcode(insn[0]))
2888 {
2889 case BLE:
2890 case BE:
2891 rtn_adjust = true;
2892 break;
2893 default:
2894 rtn_adjust = false;
2895 }
2896 }
2897
2898 /* Build the argument relocation stub. */
2899 r = hppa_elf_build_linker_stub (stub_bfd, output_bfd,
2900 link_info, rle, stub_types,
2901 rtn_adjust, insn,
2902 HPPA_STUB_ARG_RELOC);
2903 new_syms[new_cnt++] = *r;
2904 }
2905 }
2906 break;
2907
2908 default:
2909 break;
2910 }
2911 }
2912 }
2913
2914 /* Return the new symbols and update the counters. */
2915 *new_sym_cnt = new_cnt;
2916 return new_syms;
2917 }
2918
2919 /* Set the contents of a particular section at a particular location. */
2920
2921 static boolean
2922 hppa_elf_set_section_contents (abfd, section, location, offset, count)
2923 bfd *abfd;
2924 sec_ptr section;
2925 PTR location;
2926 file_ptr offset;
2927 bfd_size_type count;
2928 {
2929 /* Linker stubs are handled a little differently. */
2930 if (! strcmp (section->name, ".hppa_linker_stubs"))
2931 {
2932 if (linker_stubs_max_size < offset + count)
2933 {
2934 linker_stubs_max_size = offset + count + STUB_ALLOC_INCR;
2935 linker_stubs = (char *)realloc (linker_stubs, linker_stubs_max_size);
2936 if (! linker_stubs)
2937 abort ();
2938 }
2939
2940 if (offset + count > linker_stubs_size)
2941 linker_stubs_size = offset + count;
2942
2943 /* Set the contents. */
2944 memcpy(linker_stubs + offset, location, count);
2945 return (true);
2946 }
2947 else
2948 /* For everything but the linker stub section, use the generic
2949 code. */
2950 return bfd_elf32_set_section_contents (abfd, section, location,
2951 offset, count);
2952 }
2953
2954 /* Get the contents of the given section.
2955
2956 This is special for PA ELF because some sections (such as linker stubs)
2957 may reside in memory rather than on disk, or in the case of the symbol
2958 extension section, the contents may need to be generated from other
2959 information contained in the BFD. */
2960
2961 boolean
2962 hppa_elf_get_section_contents (abfd, section, location, offset, count)
2963 bfd *abfd;
2964 sec_ptr section;
2965 PTR location;
2966 file_ptr offset;
2967 bfd_size_type count;
2968 {
2969 /* If this is the linker stub section, then its contents are contained
2970 in memory rather than on disk. FIXME. Is that always right? What
2971 about the case where a final executable is read in and a user tries
2972 to get the contents of this section? In that case the contents would
2973 be on disk like everything else. */
2974 if (strcmp (section->name, ".hppa_linker_stubs") == 0)
2975 {
2976 elf32_hppa_stub_description *stub_desc = find_stubs (abfd, section);
2977
2978 if (count == 0)
2979 return true;
2980
2981 /* Sanity check our arguments. */
2982 if ((bfd_size_type) (offset + count) > section->_raw_size
2983 || (bfd_size_type) (offset + count) > stub_desc->real_size)
2984 return (false);
2985
2986 memcpy (location, stub_desc->stub_contents + offset, count);
2987 return (true);
2988 }
2989
2990 /* The symbol extension section also needs special handling. Its
2991 contents might be on the disk, in memory, or still need to
2992 be generated. */
2993 else if (strcmp (section->name, ".hppa_symextn") == 0)
2994 {
2995 /* If there are no output sections, then read the contents of the
2996 symbol extension section from disk. */
2997 if (section->output_section == NULL
2998 && abfd->direction == read_direction)
2999 {
3000 return bfd_generic_get_section_contents (abfd, section, location,
3001 offset, count);
3002 }
3003
3004 /* If this is the first time through, and there are output sections,
3005 then build the symbol extension section based on other information
3006 contained in the BFD. */
3007 else if (! symext_chain_built)
3008 {
3009 int i;
3010 int *symtab_map =
3011 (int *) elf_sym_extra (section->output_section->owner);
3012
3013 for (i = 0; i < section->output_section->owner->symcount; i++)
3014 {
3015 elf_hppa_tc_symbol (section->output_section->owner,
3016 ((elf_symbol_type *)
3017 section->output_section->owner->outsymbols[i]),
3018 symtab_map[i], &symext_rootP, &symext_lastP);
3019 }
3020 symext_chain_built++;
3021 elf_hppa_tc_make_sections (section->output_section->owner,
3022 symext_rootP);
3023 }
3024
3025 /* At this point we know that the symbol extension section has been
3026 built. We just need to copy it into the user's buffer. */
3027 if (count == 0)
3028 return true;
3029
3030 /* Sanity check our arguments. */
3031 if ((bfd_size_type) (offset + count) > section->_raw_size
3032 || (bfd_size_type) (offset + count) > symextn_contents_real_size)
3033 return (false);
3034
3035 memcpy (location,
3036 (char *)symextn_contents + section->output_offset + offset,
3037 count);
3038 return (true);
3039 }
3040 else
3041 /* It's not the symbol extension or linker stub sections, use
3042 the generic routines. */
3043 return bfd_generic_get_section_contents (abfd, section, location,
3044 offset, count);
3045 }
3046
3047 /* Translate from an elf into field into a howto relocation pointer. */
3048
3049 static void
3050 elf_info_to_howto (abfd, cache_ptr, dst)
3051 bfd *abfd;
3052 arelent *cache_ptr;
3053 Elf32_Internal_Rela *dst;
3054 {
3055 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_HPPA_UNIMPLEMENTED);
3056 cache_ptr->howto = &elf_hppa_howto_table[ELF32_R_TYPE (dst->r_info)];
3057 }
3058
3059 /* Do PA ELF specific processing for symbols. Needed to find the
3060 value of $global$. */
3061
3062 static void
3063 elf32_hppa_backend_symbol_processing (abfd, sym)
3064 bfd *abfd;
3065 asymbol *sym;
3066 {
3067 /* Is this a definition of $global$? If so, keep it because it will be
3068 needed if any relocations are performed. */
3069 if (!strcmp (sym->name, "$global$")
3070 && sym->section != &bfd_und_section)
3071 {
3072 global_symbol = sym;
3073 }
3074 }
3075
3076 /* Do some PA ELF specific work after reading in the symbol table.
3077 In particular attach the argument relocation from the
3078 symbol extension section to the appropriate symbols. */
3079 static boolean
3080 elf32_hppa_backend_symbol_table_processing (abfd, esyms,symcnt)
3081 bfd *abfd;
3082 elf_symbol_type *esyms;
3083 int symcnt;
3084 {
3085 Elf32_Internal_Shdr *symextn_hdr =
3086 bfd_elf_find_section (abfd, SYMEXTN_SECTION_NAME);
3087 int i, current_sym_idx = 0;
3088
3089 /* If no symbol extension existed, then all symbol extension information
3090 is assumed to be zero. */
3091 if (symextn_hdr == NULL)
3092 {
3093 for (i = 0; i < symcnt; i++)
3094 esyms[i].tc_data.hppa_arg_reloc = 0;
3095 return (true);
3096 }
3097
3098 /* Allocate a buffer of the appropriate size for the symextn section. */
3099 symextn_hdr->contents = bfd_zalloc(abfd,symextn_hdr->sh_size);
3100 if (!symextn_hdr->contents)
3101 {
3102 bfd_set_error (bfd_error_no_memory);
3103 return false;
3104 }
3105 symextn_hdr->size = symextn_hdr->sh_size;
3106
3107 /* Read in the symextn section. */
3108 if (bfd_seek (abfd, symextn_hdr->sh_offset, SEEK_SET) == -1)
3109 {
3110 bfd_set_error (bfd_error_system_call);
3111 return (false);
3112 }
3113 if (bfd_read ((PTR) symextn_hdr->contents, 1, symextn_hdr->size, abfd)
3114 != symextn_hdr->size)
3115 {
3116 bfd_set_error (bfd_error_system_call);
3117 return (false);
3118 }
3119
3120 /* Parse entries in the symbol extension section, updating the symtab
3121 entries as we go */
3122 for (i = 0; i < symextn_hdr->size / sizeof(symext_entryS); i++)
3123 {
3124 symext_entryS *seP = ((symext_entryS *)symextn_hdr->contents) + i;
3125 int se_value = ELF32_HPPA_SX_VAL (*seP);
3126 int se_type = ELF32_HPPA_SX_TYPE (*seP);
3127
3128 switch (se_type)
3129 {
3130 case HPPA_SXT_NULL:
3131 break;
3132
3133 case HPPA_SXT_SYMNDX:
3134 if (se_value >= symcnt)
3135 {
3136 bfd_set_error (bfd_error_bad_value);
3137 return (false);
3138 }
3139 current_sym_idx = se_value - 1;
3140 break;
3141
3142 case HPPA_SXT_ARG_RELOC:
3143 esyms[current_sym_idx].tc_data.hppa_arg_reloc = se_value;
3144 break;
3145
3146 default:
3147 bfd_set_error (bfd_error_bad_value);
3148 return (false);
3149 }
3150 }
3151 return (true);
3152 }
3153
3154 /* Perform on PA ELF specific processing once a section has been
3155 read in. In particular keep the symbol indexes correct for
3156 the symbol extension information. */
3157
3158 static boolean
3159 elf32_hppa_backend_section_processing (abfd, secthdr)
3160 bfd *abfd;
3161 Elf32_Internal_Shdr *secthdr;
3162 {
3163 int i, j, k;
3164
3165 if (secthdr->sh_type == SHT_HPPA_SYMEXTN)
3166 {
3167 for (i = 0; i < secthdr->size / sizeof (symext_entryS); i++)
3168 {
3169 symext_entryS *seP = ((symext_entryS *)secthdr->contents) + i;
3170 int se_value = ELF32_HPPA_SX_VAL (*seP);
3171 int se_type = ELF32_HPPA_SX_TYPE (*seP);
3172
3173 switch (se_type)
3174 {
3175 case HPPA_SXT_NULL:
3176 break;
3177
3178 case HPPA_SXT_SYMNDX:
3179 for (j = 0; j < abfd->symcount; j++)
3180 {
3181 /* Locate the map entry for this symbol and modify the
3182 symbol extension section symbol index entry to reflect
3183 the new symbol table index. */
3184 for (k = 0; k < elf32_hppa_symextn_map_size; k++)
3185 {
3186 if (elf32_hppa_symextn_map[k].old_index == se_value
3187 && elf32_hppa_symextn_map[k].bfd
3188 == abfd->outsymbols[j]->the_bfd
3189 && elf32_hppa_symextn_map[k].sym
3190 == abfd->outsymbols[j])
3191 {
3192 bfd_put_32(abfd,
3193 ELF32_HPPA_SX_WORD (HPPA_SXT_SYMNDX, j),
3194 (char *)seP);
3195 }
3196 }
3197 }
3198 break;
3199
3200 case HPPA_SXT_ARG_RELOC:
3201 break;
3202
3203 default:
3204 bfd_set_error (bfd_error_bad_value);
3205 return (false);
3206 }
3207 }
3208 }
3209 return true;
3210 }
3211
3212 /* What does this really do? Just determine if there is an appropriate
3213 mapping from ELF section headers to backend sections? More symbol
3214 extension braindamage. */
3215
3216 static boolean
3217 elf32_hppa_backend_section_from_shdr (abfd, hdr, name)
3218 bfd *abfd;
3219 Elf32_Internal_Shdr *hdr;
3220 char *name;
3221 {
3222 asection *newsect;
3223
3224 if (hdr->sh_type == SHT_HPPA_SYMEXTN)
3225 {
3226 BFD_ASSERT (strcmp (name, ".hppa_symextn") == 0);
3227
3228 /* Bits that get saved. This one is real. */
3229 if (!hdr->rawdata)
3230 {
3231 newsect = bfd_make_section (abfd, name);
3232 if (newsect != NULL)
3233 {
3234 newsect->vma = hdr->sh_addr;
3235 newsect->_raw_size = hdr->sh_size;
3236 newsect->filepos = hdr->sh_offset;
3237 newsect->flags |= SEC_HAS_CONTENTS;
3238 newsect->alignment_power = hdr->sh_addralign;
3239
3240 if (hdr->sh_flags & SHF_ALLOC)
3241 {
3242 newsect->flags |= SEC_ALLOC;
3243 newsect->flags |= SEC_LOAD;
3244 }
3245
3246 if (!(hdr->sh_flags & SHF_WRITE))
3247 newsect->flags |= SEC_READONLY;
3248
3249 if (hdr->sh_flags & SHF_EXECINSTR)
3250 newsect->flags |= SEC_CODE;
3251 else
3252 newsect->flags |= SEC_DATA;
3253
3254 hdr->rawdata = (void *) newsect;
3255 }
3256 }
3257 return true;
3258 }
3259 return false;
3260 }
3261
3262 /* Return true if the given section is a fake section. */
3263
3264 static boolean
3265 elf32_hppa_backend_fake_sections (abfd, secthdr, asect)
3266 bfd *abfd;
3267 Elf_Internal_Shdr *secthdr;
3268 asection *asect;
3269 {
3270
3271 if (strcmp(asect->name, ".hppa_symextn") == 0)
3272 {
3273 secthdr->sh_type = SHT_HPPA_SYMEXTN;
3274 secthdr->sh_flags = 0;
3275 secthdr->sh_info = elf_section_data(asect)->rel_hdr.sh_link;
3276 secthdr->sh_link = elf_onesymtab(abfd);
3277 return true;
3278 }
3279
3280 if (!strcmp (asect->name, ".hppa_unwind"))
3281 {
3282 secthdr->sh_type = SHT_PROGBITS;
3283 /* Unwind descriptors are not part of the program memory image. */
3284 secthdr->sh_flags = 0;
3285 secthdr->sh_info = 0;
3286 secthdr->sh_link = 0;
3287 secthdr->sh_entsize = 16;
3288 return true;
3289 }
3290
3291 /* @@ Should this be CPU specific?? KR */
3292 if (!strcmp (asect->name, ".stabstr"))
3293 {
3294 secthdr->sh_type = SHT_STRTAB;
3295 secthdr->sh_flags = 0;
3296 secthdr->sh_info = 0;
3297 secthdr->sh_link = 0;
3298 secthdr->sh_entsize = 0;
3299 return true;
3300 }
3301
3302 return false;
3303 }
3304
3305 /* Return true if there is a mapping from bfd section into a
3306 backend section. */
3307
3308 static boolean
3309 elf32_hppa_backend_section_from_bfd_section (abfd, hdr, asect, ignored)
3310 bfd *abfd;
3311 Elf32_Internal_Shdr *hdr;
3312 asection *asect;
3313 int *ignored;
3314 {
3315 if (hdr->sh_type == SHT_HPPA_SYMEXTN)
3316 {
3317 if (hdr->rawdata)
3318 {
3319 if (((struct sec *) (hdr->rawdata)) == asect)
3320 {
3321 BFD_ASSERT (strcmp (asect->name, ".hppa_symextn") == 0);
3322 return true;
3323 }
3324 }
3325 }
3326 else if (hdr->sh_type == SHT_STRTAB)
3327 {
3328 if (hdr->rawdata)
3329 {
3330 if (((struct sec *) (hdr->rawdata)) == asect)
3331 {
3332 BFD_ASSERT (strcmp (asect->name, ".stabstr") == 0);
3333 return true;
3334 }
3335 }
3336 }
3337
3338 return false;
3339 }
3340
3341 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3342 #define elf_backend_section_from_bfd_section elf32_hppa_backend_section_from_bfd_section
3343
3344 #define elf_backend_symbol_processing elf32_hppa_backend_symbol_processing
3345 #define elf_backend_symbol_table_processing elf32_hppa_backend_symbol_table_processing
3346
3347 #define bfd_generic_get_section_contents hppa_elf_get_section_contents
3348 #define bfd_elf32_set_section_contents hppa_elf_set_section_contents
3349
3350 #define elf_backend_section_processing elf32_hppa_backend_section_processing
3351
3352 #define elf_backend_section_from_shdr elf32_hppa_backend_section_from_shdr
3353 #define elf_backend_fake_sections elf32_hppa_backend_fake_sections
3354
3355 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
3356 #define TARGET_BIG_NAME "elf32-hppa"
3357 #define ELF_ARCH bfd_arch_hppa
3358 #define ELF_MACHINE_CODE EM_HPPA
3359 #define ELF_MAXPAGESIZE 0x1000
3360
3361 #include "elf32-target.h"