]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-hppa.c
* elf32-hppa.c (mismatches, retval_mismatches): Fix mismatch
[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 = bfd_zalloc (abfd, sizeof (asymbol **));
2195 if (reloc_entry->sym_ptr_ptr == NULL)
2196 {
2197 bfd_set_error (bfd_error_no_memory);
2198 abort ();
2199 }
2200 reloc_entry->sym_ptr_ptr[0] = stub_sym;
2201 if (linker_stub_type == HPPA_STUB_LONG_CALL
2202 || (reloc_entry->howto->type != R_HPPA_PLABEL_32
2203 && (get_opcode(insn) == BLE
2204 || get_opcode (insn) == BE
2205 || get_opcode (insn) == BL)))
2206 reloc_entry->howto = bfd_reloc_type_lookup (abfd, R_HPPA_STUB_CALL_17);
2207 }
2208 else
2209 {
2210 /* Create a new symbol to point to this stub. */
2211 stub_sym = bfd_make_empty_symbol (abfd);
2212 if (!stub_sym)
2213 {
2214 bfd_set_error (bfd_error_no_memory);
2215 abort ();
2216 }
2217 stub_sym->name = bfd_zalloc (abfd, strlen (stub_sym_name) + 1);
2218 if (!stub_sym->name)
2219 {
2220 bfd_set_error (bfd_error_no_memory);
2221 abort ();
2222 }
2223 strcpy ((char *) stub_sym->name, stub_sym_name);
2224 stub_sym->value
2225 = (char *) stub_desc->stub_secp - (char *) stub_desc->stub_contents;
2226 stub_sym->section = stub_sec;
2227 stub_sym->flags = BSF_LOCAL | BSF_FUNCTION;
2228 stub_entry = add_stub_by_name (abfd, stub_sec, stub_sym, link_info);
2229
2230 /* Redirect the original relocation from the old symbol (a function)
2231 to the stub (the stub calls the function). */
2232 reloc_entry->sym_ptr_ptr = bfd_zalloc (abfd, sizeof (asymbol **));
2233 if (reloc_entry->sym_ptr_ptr == NULL)
2234 {
2235 bfd_set_error (bfd_error_no_memory);
2236 abort ();
2237 }
2238 reloc_entry->sym_ptr_ptr[0] = stub_sym;
2239 if (linker_stub_type == HPPA_STUB_LONG_CALL
2240 || (reloc_entry->howto->type != R_HPPA_PLABEL_32
2241 && (get_opcode (insn) == BLE
2242 || get_opcode (insn) == BE
2243 || get_opcode (insn) == BL)))
2244 reloc_entry->howto = bfd_reloc_type_lookup (abfd, R_HPPA_STUB_CALL_17);
2245
2246 /* Now generate the code for the stub. Starting with two
2247 common instructions.
2248
2249 FIXME: Do we still need the SP adjustment?
2250 Do we still need to muck with space registers? */
2251 NEW_INSTRUCTION (stub_entry, LDSID_31_1)
2252 NEW_INSTRUCTION (stub_entry, MTSP_1_SR0)
2253
2254 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2255 {
2256 NEW_INSTRUCTION (stub_entry, ADDI_8_SP)
2257
2258 /* Examine each argument, generating code to relocate it
2259 into a different register if necessary. */
2260 for (i = ARG0; i < ARG3; i++)
2261 {
2262 switch (stub_types[i])
2263 {
2264
2265 case NO_ARG_RELOC:
2266 continue;
2267
2268 case R_TO_FR:
2269 switch (i)
2270 {
2271 case ARG0:
2272 NEW_INSTRUCTION (stub_entry, STWS_ARG0_M8SP)
2273 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG0)
2274 break;
2275 case ARG1:
2276 NEW_INSTRUCTION (stub_entry, STWS_ARG1_M8SP)
2277 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG1)
2278 break;
2279 case ARG2:
2280 NEW_INSTRUCTION (stub_entry, STWS_ARG2_M8SP)
2281 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG2)
2282 break;
2283 case ARG3:
2284 NEW_INSTRUCTION (stub_entry, STWS_ARG3_M8SP)
2285 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FARG3)
2286 break;
2287 }
2288 continue;
2289
2290 case R01_TO_FR:
2291 switch (i)
2292 {
2293 case ARG0:
2294 NEW_INSTRUCTION (stub_entry, STWS_ARG0_M4SP)
2295 NEW_INSTRUCTION (stub_entry, STWS_ARG1_M8SP)
2296 NEW_INSTRUCTION (stub_entry, FLDDS_M8SP_FARG1)
2297 break;
2298 default:
2299 abort ();
2300 break;
2301 }
2302 continue;
2303
2304 case R23_TO_FR:
2305 switch (i)
2306 {
2307 case ARG2:
2308 NEW_INSTRUCTION (stub_entry, STWS_ARG2_M4SP)
2309 NEW_INSTRUCTION (stub_entry, STWS_ARG3_M8SP)
2310 NEW_INSTRUCTION (stub_entry, FLDDS_M8SP_FARG3)
2311 break;
2312 default:
2313 abort ();
2314 break;
2315 }
2316 continue;
2317
2318 case FR_TO_R:
2319 switch (i)
2320 {
2321 case ARG0:
2322 NEW_INSTRUCTION (stub_entry, FSTWS_FARG0_M8SP)
2323 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG0)
2324 break;
2325 case ARG1:
2326 NEW_INSTRUCTION (stub_entry, FSTWS_FARG1_M8SP)
2327 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG1)
2328 break;
2329 case ARG2:
2330 NEW_INSTRUCTION (stub_entry, FSTWS_FARG2_M8SP)
2331 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG2)
2332 break;
2333 case ARG3:
2334 NEW_INSTRUCTION (stub_entry, FSTWS_FARG3_M8SP)
2335 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG3)
2336 break;
2337 }
2338 continue;
2339
2340 case FR_TO_R01:
2341 switch (i)
2342 {
2343 case ARG0:
2344 NEW_INSTRUCTION (stub_entry, FSTDS_FARG1_M8SP)
2345 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG0)
2346 NEW_INSTRUCTION (stub_entry, LDWS_M8SP_ARG1)
2347 break;
2348 default:
2349 abort ();
2350 break;
2351 }
2352 continue;
2353
2354 case FR_TO_R23:
2355 switch (i)
2356 {
2357 case ARG2:
2358 NEW_INSTRUCTION (stub_entry, FSTDS_FARG3_M8SP)
2359 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_ARG2)
2360 NEW_INSTRUCTION (stub_entry, LDWS_M8SP_ARG3)
2361 break;
2362 default:
2363 abort ();
2364 break;
2365 }
2366 continue;
2367
2368 default:
2369 abort ();
2370 break;
2371 }
2372 }
2373
2374 /* Put the stack pointer back. FIXME: Is this still necessary? */
2375 NEW_INSTRUCTION (stub_entry, ADDI_M8_SP_SP)
2376 }
2377
2378 /* Common code again. Return pointer adjustment and the like. */
2379 if (!dyncall)
2380 {
2381 /* This isn't dyncall. */
2382 if (!milli)
2383 {
2384 /* It's not a millicode call, so get the correct return
2385 value into %r2 (aka RP). */
2386 if (rtn_adjust)
2387 NEW_INSTRUCTION (stub_entry, ADDI_M4_31_RP)
2388 else
2389 NEW_INSTRUCTION (stub_entry, COPY_31_2)
2390 }
2391 else
2392 {
2393 /* It is a millicode call, so get the correct return
2394 value into %r1?!?. FIXME: Shouldn't this be
2395 %r31? Yes, and a little re-arrangement of the
2396 code below would make that possible. */
2397 if (rtn_adjust)
2398 NEW_INSTRUCTION (stub_entry, ADDI_M4_31_1)
2399 else
2400 NEW_INSTRUCTION (stub_entry, COPY_31_1)
2401 }
2402 }
2403 else
2404 {
2405 /* This is dyncall, so the code is a little different as the
2406 return pointer is already in %r2 (aka RP). */
2407 if (rtn_adjust)
2408 NEW_INSTRUCTION (stub_entry, ADDI_M4_31_RP)
2409 }
2410
2411 /* Save the return address. */
2412 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2413 NEW_INSTRUCTION (stub_entry, STW_RP_M8SP)
2414
2415 /* Long branch to the target function. */
2416 NEW_INSTRUCTION (stub_entry, LDIL_XXX_31)
2417 hppa_elf_stub_reloc (stub_entry->stub_desc,
2418 abfd, orig_sym,
2419 CURRENT_STUB_OFFSET (stub_entry),
2420 R_HPPA_L21);
2421 NEW_INSTRUCTION (stub_entry, BLE_XXX_0_31)
2422 hppa_elf_stub_reloc (stub_entry->stub_desc,
2423 abfd, orig_sym,
2424 CURRENT_STUB_OFFSET (stub_entry),
2425 R_HPPA_ABS_CALL_R17);
2426
2427 if (linker_stub_type == HPPA_STUB_ARG_RELOC)
2428 {
2429 /* In delay slot of long-call, copy %r31 into %r2 so that
2430 the callee can return in the normal fashion. */
2431 NEW_INSTRUCTION (stub_entry, COPY_31_2)
2432
2433 /* Restore the return address. */
2434 NEW_INSTRUCTION (stub_entry, LDW_M8SP_RP)
2435
2436 /* Generate the code to move the return value around. */
2437 switch (stub_types[RETVAL])
2438 {
2439 case NO_ARG_RELOC:
2440 break;
2441
2442 case R_TO_FR:
2443 NEW_INSTRUCTION (stub_entry, STWS_RET0_M8SP)
2444 NEW_INSTRUCTION (stub_entry, FLDWS_M8SP_FRET0)
2445 break;
2446
2447 case FR_TO_R:
2448 NEW_INSTRUCTION (stub_entry, FSTWS_FRET0_M8SP)
2449 NEW_INSTRUCTION (stub_entry, LDWS_M4SP_RET0)
2450 break;
2451
2452 default:
2453 abort ();
2454 break;
2455 }
2456
2457 /* Return back to the main code stream. */
2458 NEW_INSTRUCTION (stub_entry, BV_N_0_RP)
2459 }
2460 else
2461 {
2462 if (!dyncall)
2463 {
2464 /* Get return address into %r31. Both variants may be necessary
2465 (I think) as we could be cascading into another stub. */
2466 if (!milli)
2467 NEW_INSTRUCTION (stub_entry, COPY_2_31)
2468 else
2469 NEW_INSTRUCTION (stub_entry, COPY_1_31)
2470 }
2471 else
2472 {
2473 /* Get the return address into %r31 too. Might be necessary
2474 (I think) as we could be cascading into another stub. */
2475 NEW_INSTRUCTION (stub_entry, COPY_2_31)
2476 }
2477
2478 /* No need for a return to the main stream. */
2479 }
2480 }
2481 return stub_sym;
2482 }
2483
2484 /* Return nonzero if an argument relocation will be needed to call
2485 the function (symbol in RELOC_ENTRY) assuming the caller has
2486 argument relocation bugs CALLER_AR. */
2487
2488 static int
2489 hppa_elf_arg_reloc_needed_p (abfd, reloc_entry, stub_types, caller_ar)
2490 bfd *abfd;
2491 arelent *reloc_entry;
2492 arg_reloc_type stub_types[5];
2493 symext_entryS caller_ar;
2494 {
2495 /* If the symbol is still undefined, then it's impossible to know
2496 if an argument relocation is needed. */
2497 if (reloc_entry->sym_ptr_ptr[0]
2498 && reloc_entry->sym_ptr_ptr[0]->section != &bfd_und_section)
2499 {
2500 symext_entryS callee_ar = elf32_hppa_get_sym_extn (abfd,
2501 reloc_entry->sym_ptr_ptr[0],
2502 HPPA_SXT_ARG_RELOC);
2503
2504 /* Now examine all the argument and return value location
2505 information to determine if a relocation stub will be needed. */
2506 if (caller_ar && callee_ar)
2507 {
2508 arg_location caller_loc[5];
2509 arg_location callee_loc[5];
2510
2511 /* Extract the location information for the return value
2512 and argument registers separately. */
2513 callee_loc[RETVAL] = EXTRACT_ARBITS (callee_ar, RETVAL);
2514 caller_loc[RETVAL] = EXTRACT_ARBITS (caller_ar, RETVAL);
2515 callee_loc[ARG0] = EXTRACT_ARBITS (callee_ar, ARG0);
2516 caller_loc[ARG0] = EXTRACT_ARBITS (caller_ar, ARG0);
2517 callee_loc[ARG1] = EXTRACT_ARBITS (callee_ar, ARG1);
2518 caller_loc[ARG1] = EXTRACT_ARBITS (caller_ar, ARG1);
2519 callee_loc[ARG2] = EXTRACT_ARBITS (callee_ar, ARG2);
2520 caller_loc[ARG2] = EXTRACT_ARBITS (caller_ar, ARG2);
2521 callee_loc[ARG3] = EXTRACT_ARBITS (callee_ar, ARG3);
2522 caller_loc[ARG3] = EXTRACT_ARBITS (caller_ar, ARG3);
2523
2524 /* Check some special combinations. For example, if FU
2525 appears in ARG1 or ARG3, we can move it to ARG0 or ARG2,
2526 respectively. (I guess this braindamage is correct? It'd
2527 take an hour or two of reading PA calling conventions to
2528 really know). */
2529
2530 if (caller_loc[ARG0] == AR_FU || caller_loc[ARG1] == AR_FU)
2531 {
2532 caller_loc[ARG0] = AR_DBL01;
2533 caller_loc[ARG1] = AR_NO;
2534 }
2535 if (caller_loc[ARG2] == AR_FU || caller_loc[ARG3] == AR_FU)
2536 {
2537 caller_loc[ARG2] = AR_DBL23;
2538 caller_loc[ARG3] = AR_NO;
2539 }
2540 if (callee_loc[ARG0] == AR_FU || callee_loc[ARG1] == AR_FU)
2541 {
2542 callee_loc[ARG0] = AR_DBL01;
2543 callee_loc[ARG1] = AR_NO;
2544 }
2545 if (callee_loc[ARG2] == AR_FU || callee_loc[ARG3] == AR_FU)
2546 {
2547 callee_loc[ARG2] = AR_DBL23;
2548 callee_loc[ARG3] = AR_NO;
2549 }
2550
2551 /* Now look up potential mismatches. */
2552 stub_types[ARG0] = type_of_mismatch (caller_loc[ARG0],
2553 callee_loc[ARG0],
2554 ARGUMENTS);
2555 stub_types[ARG1] = type_of_mismatch (caller_loc[ARG1],
2556 callee_loc[ARG1],
2557 ARGUMENTS);
2558 stub_types[ARG2] = type_of_mismatch (caller_loc[ARG2],
2559 callee_loc[ARG2],
2560 ARGUMENTS);
2561 stub_types[ARG3] = type_of_mismatch (caller_loc[ARG3],
2562 callee_loc[ARG3],
2563 ARGUMENTS);
2564 stub_types[RETVAL] = type_of_mismatch (caller_loc[RETVAL],
2565 callee_loc[RETVAL],
2566 RETURN_VALUE);
2567
2568 /* If any of the arguments or return value need an argument
2569 relocation, then we will need an argument relocation stub. */
2570 if (stub_types[ARG0] != NO_ARG_RELOC
2571 || stub_types[ARG1] != NO_ARG_RELOC
2572 || stub_types[ARG2] != NO_ARG_RELOC
2573 || stub_types[ARG3] != NO_ARG_RELOC
2574 || stub_types[RETVAL] != NO_ARG_RELOC)
2575 return 1;
2576 }
2577 }
2578 return 0;
2579 }
2580
2581 /* Create the linker stub section. */
2582
2583 static void
2584 hppa_elf_create_stub_sec (abfd, output_bfd, secptr, link_info)
2585 bfd *abfd;
2586 bfd *output_bfd;
2587 asection **secptr;
2588 struct bfd_link_info *link_info;
2589 {
2590 asection *output_text_section;
2591
2592 output_text_section = bfd_get_section_by_name (output_bfd, ".text");
2593 *secptr = bfd_make_section (abfd, ".hppa_linker_stubs");
2594 bfd_set_section_flags (abfd, *secptr,
2595 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
2596 | SEC_RELOC | SEC_CODE | SEC_READONLY);
2597 (*secptr)->output_section = output_text_section->output_section;
2598 (*secptr)->output_offset = 0;
2599
2600 /* Set up the ELF section header for this new section. This
2601 is basically the same processing as elf_make_sections().
2602 elf_make_sections is static and therefore not accessable
2603 here. */
2604 {
2605 Elf_Internal_Shdr *this_hdr;
2606 this_hdr = &elf_section_data ((*secptr))->this_hdr;
2607
2608 /* Set the sizes of this section. The contents have already
2609 been set up ?!? */
2610 this_hdr->sh_addr = (*secptr)->vma;
2611 this_hdr->sh_size = (*secptr)->_raw_size;
2612
2613 /* Set appropriate flags for sections with relocations. */
2614 if ((*secptr)->flags & SEC_RELOC)
2615 {
2616 Elf_Internal_Shdr *rela_hdr;
2617 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
2618
2619 rela_hdr = &elf_section_data ((*secptr))->rel_hdr;
2620
2621 if (use_rela_p)
2622 {
2623 rela_hdr->sh_type = SHT_RELA;
2624 rela_hdr->sh_entsize = sizeof (Elf32_External_Rela);
2625 }
2626 else
2627 {
2628 rela_hdr->sh_type = SHT_REL;
2629 rela_hdr->sh_entsize = sizeof (Elf32_External_Rel);
2630 }
2631 rela_hdr->sh_flags = 0;
2632 rela_hdr->sh_addr = 0;
2633 rela_hdr->sh_offset = 0;
2634 rela_hdr->sh_addralign = 0;
2635 rela_hdr->size = 0;
2636 }
2637
2638 if ((*secptr)->flags & SEC_ALLOC)
2639 this_hdr->sh_flags |= SHF_ALLOC;
2640
2641 if (!((*secptr)->flags & SEC_READONLY))
2642 this_hdr->sh_flags |= SHF_WRITE;
2643
2644 if ((*secptr)->flags & SEC_CODE)
2645 this_hdr->sh_flags |= SHF_EXECINSTR;
2646 }
2647
2648 bfd_set_section_alignment (abfd, *secptr, 2);
2649 }
2650
2651 /* Return nonzero if a long-call stub will be needed to call the
2652 function (symbol in RELOC_ENTRY). */
2653
2654 static int
2655 hppa_elf_long_branch_needed_p (abfd, asec, reloc_entry, symbol, insn)
2656 bfd *abfd;
2657 asection *asec;
2658 arelent *reloc_entry;
2659 asymbol *symbol;
2660 unsigned insn;
2661 {
2662 long sym_value = get_symbol_value (symbol);
2663 int fmt = reloc_entry->howto->bitsize;
2664 unsigned char op = get_opcode (insn);
2665 unsigned raddr;
2666
2667 #define too_far(val,num_bits) \
2668 ((int)(val) > (1 << (num_bits)) - 1) || ((int)(val) < (-1 << (num_bits)))
2669
2670 switch (op)
2671 {
2672 case BL:
2673 raddr =
2674 reloc_entry->address + asec->output_offset + asec->output_section->vma;
2675 /* If the symbol and raddr (relocated addr?) are too far away from
2676 each other, then a long-call stub will be needed. */
2677 if (too_far (sym_value - raddr, fmt + 1))
2678 return 1;
2679 break;
2680 }
2681 return 0;
2682 }
2683
2684 /* Search the given section and determine if linker stubs will be
2685 needed for any calls within that section.
2686
2687 Return any new stub symbols created.
2688
2689 Used out of hppaelf.em in the linker. */
2690
2691 asymbol *
2692 hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec,
2693 new_sym_cnt, link_info)
2694 bfd *stub_bfd;
2695 bfd *abfd;
2696 bfd *output_bfd;
2697 asection *asec;
2698 int *new_sym_cnt;
2699 struct bfd_link_info *link_info;
2700 {
2701 int i;
2702 arg_reloc_type stub_types[5];
2703 asymbol *new_syms = NULL;
2704 int new_cnt = 0;
2705 int new_max = 0;
2706
2707 /* Relocations are in different places depending on whether this is
2708 an output section or an input section. Also, the relocations are
2709 in different forms. Sigh. Luckily, we have bfd_canonicalize_reloc()
2710 to straighten this out for us . */
2711 if (asec->reloc_count > 0)
2712 {
2713 arelent **reloc_vector
2714 = (arelent **) alloca (asec->reloc_count * (sizeof (arelent *) + 1));
2715
2716 /* Make sure the canonical symbols are hanging around in a convient
2717 location. */
2718 if (bfd_get_outsymbols (abfd) == NULL)
2719 {
2720 size_t symsize;
2721
2722 symsize = get_symtab_upper_bound (abfd);
2723 abfd->outsymbols = (asymbol **) bfd_alloc (abfd, symsize);
2724 if (!abfd->outsymbols)
2725 {
2726 bfd_set_error (bfd_error_no_memory);
2727 return false;
2728 }
2729 abfd->symcount = bfd_canonicalize_symtab (abfd, abfd->outsymbols);
2730 }
2731
2732 /* Now get the relocations. */
2733 bfd_canonicalize_reloc (abfd, asec, reloc_vector,
2734 bfd_get_outsymbols (abfd));
2735
2736 /* Examine each relocation entry in this section. */
2737 for (i = 0; i < asec->reloc_count; i++)
2738 {
2739 arelent *rle = reloc_vector[i];
2740
2741 switch (rle->howto->type)
2742 {
2743 /* Any call could need argument relocation stubs, and
2744 some may need long-call stubs. */
2745 case R_HPPA_ABS_CALL_11:
2746 case R_HPPA_ABS_CALL_14:
2747 case R_HPPA_ABS_CALL_17:
2748 case R_HPPA_ABS_CALL_L21:
2749 case R_HPPA_ABS_CALL_R11:
2750 case R_HPPA_ABS_CALL_R14:
2751 case R_HPPA_ABS_CALL_R17:
2752 case R_HPPA_ABS_CALL_LS21:
2753 case R_HPPA_ABS_CALL_RS11:
2754 case R_HPPA_ABS_CALL_RS14:
2755 case R_HPPA_ABS_CALL_RS17:
2756 case R_HPPA_ABS_CALL_LD21:
2757 case R_HPPA_ABS_CALL_RD11:
2758 case R_HPPA_ABS_CALL_RD14:
2759 case R_HPPA_ABS_CALL_RD17:
2760 case R_HPPA_ABS_CALL_LR21:
2761 case R_HPPA_ABS_CALL_RR14:
2762 case R_HPPA_ABS_CALL_RR17:
2763 case R_HPPA_PCREL_CALL_11:
2764 case R_HPPA_PCREL_CALL_14:
2765 case R_HPPA_PCREL_CALL_17:
2766 case R_HPPA_PCREL_CALL_12:
2767 case R_HPPA_PCREL_CALL_L21:
2768 case R_HPPA_PCREL_CALL_R11:
2769 case R_HPPA_PCREL_CALL_R14:
2770 case R_HPPA_PCREL_CALL_R17:
2771 case R_HPPA_PCREL_CALL_LS21:
2772 case R_HPPA_PCREL_CALL_RS11:
2773 case R_HPPA_PCREL_CALL_RS14:
2774 case R_HPPA_PCREL_CALL_RS17:
2775 case R_HPPA_PCREL_CALL_LD21:
2776 case R_HPPA_PCREL_CALL_RD11:
2777 case R_HPPA_PCREL_CALL_RD14:
2778 case R_HPPA_PCREL_CALL_RD17:
2779 case R_HPPA_PCREL_CALL_LR21:
2780 case R_HPPA_PCREL_CALL_RR14:
2781 case R_HPPA_PCREL_CALL_RR17:
2782 {
2783 symext_entryS caller_ar
2784 = (symext_entryS) HPPA_R_ARG_RELOC (rle->addend);
2785 unsigned insn[2];
2786
2787 /* We'll need this for the long-call checks. */
2788 bfd_get_section_contents (abfd, asec, insn, rle->address,
2789 sizeof(insn));
2790
2791 /* See if this call needs an argument relocation stub. */
2792 if (hppa_elf_arg_reloc_needed_p (abfd, rle, stub_types,
2793 caller_ar))
2794 {
2795 /* Generate a stub and keep track of the new symbol. */
2796 asymbol *r;
2797
2798 if (new_cnt == new_max)
2799 {
2800 new_max += STUB_SYM_BUFFER_INC;
2801 new_syms = (asymbol *)
2802 realloc (new_syms, new_max * sizeof (asymbol));
2803 if (new_syms == NULL)
2804 abort ();
2805 }
2806
2807 /* Build the argument relocation stub. */
2808 r = hppa_elf_build_linker_stub (stub_bfd, output_bfd,
2809 link_info, rle,
2810 stub_types, true, insn,
2811 HPPA_STUB_ARG_RELOC);
2812 new_syms[new_cnt++] = *r;
2813 }
2814
2815 /* See if this call needs a long-call stub. */
2816 if (hppa_elf_long_branch_needed_p (abfd, asec, rle,
2817 rle->sym_ptr_ptr[0],
2818 insn[0]))
2819 {
2820 /* Generate a stub and keep track of the new symbol. */
2821 asymbol *r;
2822
2823 if (new_cnt == new_max)
2824 {
2825 new_max += STUB_SYM_BUFFER_INC;
2826 new_syms = (asymbol *)
2827 realloc (new_syms, (new_max * sizeof (asymbol)));
2828 if (! new_syms)
2829 abort ();
2830 }
2831
2832 /* Build the long-call stub. */
2833 r = hppa_elf_build_linker_stub (stub_bfd, output_bfd,
2834 link_info, rle,
2835 NULL, true, insn,
2836 HPPA_STUB_LONG_CALL);
2837 new_syms[new_cnt++] = *r;
2838 }
2839 }
2840 break;
2841
2842 /* PLABELs may need argument relocation stubs. */
2843 case R_HPPA_PLABEL_32:
2844 case R_HPPA_PLABEL_11:
2845 case R_HPPA_PLABEL_14:
2846 case R_HPPA_PLABEL_L21:
2847 case R_HPPA_PLABEL_R11:
2848 case R_HPPA_PLABEL_R14:
2849 {
2850 /* On a plabel relocation, assume the arguments of the
2851 caller are set up in general registers (indirect
2852 calls only use general registers.
2853 NOTE: 0x155 = ARGW0=GR,ARGW1=GR,ARGW2=GR,RETVAL=GR. */
2854 symext_entryS caller_ar = (symext_entryS) 0x155;
2855 unsigned insn[2];
2856
2857 /* Do we really need this? */
2858 bfd_get_section_contents (abfd, asec, insn, rle->address,
2859 sizeof(insn));
2860
2861 /* See if this call needs an argument relocation stub. */
2862 if (hppa_elf_arg_reloc_needed_p (abfd, rle, stub_types,
2863 caller_ar))
2864 {
2865 /* Generate a plabel stub and keep track of the
2866 new symbol. */
2867 asymbol *r;
2868 int rtn_adjust;
2869
2870 if (new_cnt == new_max)
2871 {
2872 new_max += STUB_SYM_BUFFER_INC;
2873 new_syms = (asymbol *) realloc (new_syms, new_max
2874 * sizeof (asymbol));
2875 }
2876
2877 /* Determine whether a return adjustment
2878 (see the relocation code for relocation type
2879 R_HPPA_STUB_CALL_17) is possible. Basically,
2880 determine whether we are looking at a branch or not. */
2881 if (rle->howto->type == R_HPPA_PLABEL_32)
2882 rtn_adjust = false;
2883 else
2884 {
2885 switch (get_opcode(insn[0]))
2886 {
2887 case BLE:
2888 case BE:
2889 rtn_adjust = true;
2890 break;
2891 default:
2892 rtn_adjust = false;
2893 }
2894 }
2895
2896 /* Build the argument relocation stub. */
2897 r = hppa_elf_build_linker_stub (stub_bfd, output_bfd,
2898 link_info, rle, stub_types,
2899 rtn_adjust, insn,
2900 HPPA_STUB_ARG_RELOC);
2901 new_syms[new_cnt++] = *r;
2902 }
2903 }
2904 break;
2905
2906 default:
2907 break;
2908 }
2909 }
2910 }
2911
2912 /* Return the new symbols and update the counters. */
2913 *new_sym_cnt = new_cnt;
2914 return new_syms;
2915 }
2916
2917 /* Set the contents of a particular section at a particular location. */
2918
2919 static boolean
2920 hppa_elf_set_section_contents (abfd, section, location, offset, count)
2921 bfd *abfd;
2922 sec_ptr section;
2923 PTR location;
2924 file_ptr offset;
2925 bfd_size_type count;
2926 {
2927 /* Linker stubs are handled a little differently. */
2928 if (! strcmp (section->name, ".hppa_linker_stubs"))
2929 {
2930 if (linker_stubs_max_size < offset + count)
2931 {
2932 linker_stubs_max_size = offset + count + STUB_ALLOC_INCR;
2933 linker_stubs = (char *)realloc (linker_stubs, linker_stubs_max_size);
2934 if (! linker_stubs)
2935 abort ();
2936 }
2937
2938 if (offset + count > linker_stubs_size)
2939 linker_stubs_size = offset + count;
2940
2941 /* Set the contents. */
2942 memcpy(linker_stubs + offset, location, count);
2943 return (true);
2944 }
2945 else
2946 /* For everything but the linker stub section, use the generic
2947 code. */
2948 return bfd_elf32_set_section_contents (abfd, section, location,
2949 offset, count);
2950 }
2951
2952 /* Get the contents of the given section.
2953
2954 This is special for PA ELF because some sections (such as linker stubs)
2955 may reside in memory rather than on disk, or in the case of the symbol
2956 extension section, the contents may need to be generated from other
2957 information contained in the BFD. */
2958
2959 boolean
2960 hppa_elf_get_section_contents (abfd, section, location, offset, count)
2961 bfd *abfd;
2962 sec_ptr section;
2963 PTR location;
2964 file_ptr offset;
2965 bfd_size_type count;
2966 {
2967 /* If this is the linker stub section, then its contents are contained
2968 in memory rather than on disk. FIXME. Is that always right? What
2969 about the case where a final executable is read in and a user tries
2970 to get the contents of this section? In that case the contents would
2971 be on disk like everything else. */
2972 if (strcmp (section->name, ".hppa_linker_stubs") == 0)
2973 {
2974 elf32_hppa_stub_description *stub_desc = find_stubs (abfd, section);
2975
2976 if (count == 0)
2977 return true;
2978
2979 /* Sanity check our arguments. */
2980 if ((bfd_size_type) (offset + count) > section->_raw_size
2981 || (bfd_size_type) (offset + count) > stub_desc->real_size)
2982 return (false);
2983
2984 memcpy (location, stub_desc->stub_contents + offset, count);
2985 return (true);
2986 }
2987
2988 /* The symbol extension section also needs special handling. Its
2989 contents might be on the disk, in memory, or still need to
2990 be generated. */
2991 else if (strcmp (section->name, ".hppa_symextn") == 0)
2992 {
2993 /* If there are no output sections, then read the contents of the
2994 symbol extension section from disk. */
2995 if (section->output_section == NULL
2996 && abfd->direction == read_direction)
2997 {
2998 return bfd_generic_get_section_contents (abfd, section, location,
2999 offset, count);
3000 }
3001
3002 /* If this is the first time through, and there are output sections,
3003 then build the symbol extension section based on other information
3004 contained in the BFD. */
3005 else if (! symext_chain_built)
3006 {
3007 int i;
3008 int *symtab_map =
3009 (int *) elf_sym_extra (section->output_section->owner);
3010
3011 for (i = 0; i < section->output_section->owner->symcount; i++)
3012 {
3013 elf_hppa_tc_symbol (section->output_section->owner,
3014 ((elf_symbol_type *)
3015 section->output_section->owner->outsymbols[i]),
3016 symtab_map[i], &symext_rootP, &symext_lastP);
3017 }
3018 symext_chain_built++;
3019 elf_hppa_tc_make_sections (section->output_section->owner,
3020 symext_rootP);
3021 }
3022
3023 /* At this point we know that the symbol extension section has been
3024 built. We just need to copy it into the user's buffer. */
3025 if (count == 0)
3026 return true;
3027
3028 /* Sanity check our arguments. */
3029 if ((bfd_size_type) (offset + count) > section->_raw_size
3030 || (bfd_size_type) (offset + count) > symextn_contents_real_size)
3031 return (false);
3032
3033 memcpy (location,
3034 (char *)symextn_contents + section->output_offset + offset,
3035 count);
3036 return (true);
3037 }
3038 else
3039 /* It's not the symbol extension or linker stub sections, use
3040 the generic routines. */
3041 return bfd_generic_get_section_contents (abfd, section, location,
3042 offset, count);
3043 }
3044
3045 /* Translate from an elf into field into a howto relocation pointer. */
3046
3047 static void
3048 elf_info_to_howto (abfd, cache_ptr, dst)
3049 bfd *abfd;
3050 arelent *cache_ptr;
3051 Elf32_Internal_Rela *dst;
3052 {
3053 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_HPPA_UNIMPLEMENTED);
3054 cache_ptr->howto = &elf_hppa_howto_table[ELF32_R_TYPE (dst->r_info)];
3055 }
3056
3057 /* Do PA ELF specific processing for symbols. Needed to find the
3058 value of $global$. */
3059
3060 static void
3061 elf32_hppa_backend_symbol_processing (abfd, sym)
3062 bfd *abfd;
3063 asymbol *sym;
3064 {
3065 /* Is this a definition of $global$? If so, keep it because it will be
3066 needed if any relocations are performed. */
3067 if (!strcmp (sym->name, "$global$")
3068 && sym->section != &bfd_und_section)
3069 {
3070 global_symbol = sym;
3071 }
3072 }
3073
3074 /* Do some PA ELF specific work after reading in the symbol table.
3075 In particular attach the argument relocation from the
3076 symbol extension section to the appropriate symbols. */
3077 static boolean
3078 elf32_hppa_backend_symbol_table_processing (abfd, esyms,symcnt)
3079 bfd *abfd;
3080 elf_symbol_type *esyms;
3081 int symcnt;
3082 {
3083 Elf32_Internal_Shdr *symextn_hdr =
3084 bfd_elf_find_section (abfd, SYMEXTN_SECTION_NAME);
3085 int i, current_sym_idx = 0;
3086
3087 /* If no symbol extension existed, then all symbol extension information
3088 is assumed to be zero. */
3089 if (symextn_hdr == NULL)
3090 {
3091 for (i = 0; i < symcnt; i++)
3092 esyms[i].tc_data.hppa_arg_reloc = 0;
3093 return (true);
3094 }
3095
3096 /* Allocate a buffer of the appropriate size for the symextn section. */
3097 symextn_hdr->contents = bfd_zalloc(abfd,symextn_hdr->sh_size);
3098 if (!symextn_hdr->contents)
3099 {
3100 bfd_set_error (bfd_error_no_memory);
3101 return false;
3102 }
3103 symextn_hdr->size = symextn_hdr->sh_size;
3104
3105 /* Read in the symextn section. */
3106 if (bfd_seek (abfd, symextn_hdr->sh_offset, SEEK_SET) == -1)
3107 {
3108 bfd_set_error (bfd_error_system_call);
3109 return (false);
3110 }
3111 if (bfd_read ((PTR) symextn_hdr->contents, 1, symextn_hdr->size, abfd)
3112 != symextn_hdr->size)
3113 {
3114 bfd_set_error (bfd_error_system_call);
3115 return (false);
3116 }
3117
3118 /* Parse entries in the symbol extension section, updating the symtab
3119 entries as we go */
3120 for (i = 0; i < symextn_hdr->size / sizeof(symext_entryS); i++)
3121 {
3122 symext_entryS *seP = ((symext_entryS *)symextn_hdr->contents) + i;
3123 int se_value = ELF32_HPPA_SX_VAL (*seP);
3124 int se_type = ELF32_HPPA_SX_TYPE (*seP);
3125
3126 switch (se_type)
3127 {
3128 case HPPA_SXT_NULL:
3129 break;
3130
3131 case HPPA_SXT_SYMNDX:
3132 if (se_value >= symcnt)
3133 {
3134 bfd_set_error (bfd_error_bad_value);
3135 return (false);
3136 }
3137 current_sym_idx = se_value - 1;
3138 break;
3139
3140 case HPPA_SXT_ARG_RELOC:
3141 esyms[current_sym_idx].tc_data.hppa_arg_reloc = se_value;
3142 break;
3143
3144 default:
3145 bfd_set_error (bfd_error_bad_value);
3146 return (false);
3147 }
3148 }
3149 return (true);
3150 }
3151
3152 /* Perform on PA ELF specific processing once a section has been
3153 read in. In particular keep the symbol indexes correct for
3154 the symbol extension information. */
3155
3156 static boolean
3157 elf32_hppa_backend_section_processing (abfd, secthdr)
3158 bfd *abfd;
3159 Elf32_Internal_Shdr *secthdr;
3160 {
3161 int i, j, k;
3162
3163 if (secthdr->sh_type == SHT_HPPA_SYMEXTN)
3164 {
3165 for (i = 0; i < secthdr->size / sizeof (symext_entryS); i++)
3166 {
3167 symext_entryS *seP = ((symext_entryS *)secthdr->contents) + i;
3168 int se_value = ELF32_HPPA_SX_VAL (*seP);
3169 int se_type = ELF32_HPPA_SX_TYPE (*seP);
3170
3171 switch (se_type)
3172 {
3173 case HPPA_SXT_NULL:
3174 break;
3175
3176 case HPPA_SXT_SYMNDX:
3177 for (j = 0; j < abfd->symcount; j++)
3178 {
3179 /* Locate the map entry for this symbol and modify the
3180 symbol extension section symbol index entry to reflect
3181 the new symbol table index. */
3182 for (k = 0; k < elf32_hppa_symextn_map_size; k++)
3183 {
3184 if (elf32_hppa_symextn_map[k].old_index == se_value
3185 && elf32_hppa_symextn_map[k].bfd
3186 == abfd->outsymbols[j]->the_bfd
3187 && elf32_hppa_symextn_map[k].sym
3188 == abfd->outsymbols[j])
3189 {
3190 bfd_put_32(abfd,
3191 ELF32_HPPA_SX_WORD (HPPA_SXT_SYMNDX, j),
3192 (char *)seP);
3193 }
3194 }
3195 }
3196 break;
3197
3198 case HPPA_SXT_ARG_RELOC:
3199 break;
3200
3201 default:
3202 bfd_set_error (bfd_error_bad_value);
3203 return (false);
3204 }
3205 }
3206 }
3207 return true;
3208 }
3209
3210 /* What does this really do? Just determine if there is an appropriate
3211 mapping from ELF section headers to backend sections? More symbol
3212 extension braindamage. */
3213
3214 static boolean
3215 elf32_hppa_backend_section_from_shdr (abfd, hdr, name)
3216 bfd *abfd;
3217 Elf32_Internal_Shdr *hdr;
3218 char *name;
3219 {
3220 asection *newsect;
3221
3222 if (hdr->sh_type == SHT_HPPA_SYMEXTN)
3223 {
3224 BFD_ASSERT (strcmp (name, ".hppa_symextn") == 0);
3225
3226 /* Bits that get saved. This one is real. */
3227 if (!hdr->rawdata)
3228 {
3229 newsect = bfd_make_section (abfd, name);
3230 if (newsect != NULL)
3231 {
3232 newsect->vma = hdr->sh_addr;
3233 newsect->_raw_size = hdr->sh_size;
3234 newsect->filepos = hdr->sh_offset;
3235 newsect->flags |= SEC_HAS_CONTENTS;
3236 newsect->alignment_power = hdr->sh_addralign;
3237
3238 if (hdr->sh_flags & SHF_ALLOC)
3239 {
3240 newsect->flags |= SEC_ALLOC;
3241 newsect->flags |= SEC_LOAD;
3242 }
3243
3244 if (!(hdr->sh_flags & SHF_WRITE))
3245 newsect->flags |= SEC_READONLY;
3246
3247 if (hdr->sh_flags & SHF_EXECINSTR)
3248 newsect->flags |= SEC_CODE;
3249 else
3250 newsect->flags |= SEC_DATA;
3251
3252 hdr->rawdata = (void *) newsect;
3253 }
3254 }
3255 return true;
3256 }
3257 return false;
3258 }
3259
3260 /* Return true if the given section is a fake section. */
3261
3262 static boolean
3263 elf32_hppa_backend_fake_sections (abfd, secthdr, asect)
3264 bfd *abfd;
3265 Elf_Internal_Shdr *secthdr;
3266 asection *asect;
3267 {
3268
3269 if (strcmp(asect->name, ".hppa_symextn") == 0)
3270 {
3271 secthdr->sh_type = SHT_HPPA_SYMEXTN;
3272 secthdr->sh_flags = 0;
3273 secthdr->sh_info = elf_section_data(asect)->rel_hdr.sh_link;
3274 secthdr->sh_link = elf_onesymtab(abfd);
3275 return true;
3276 }
3277
3278 if (!strcmp (asect->name, ".hppa_unwind"))
3279 {
3280 secthdr->sh_type = SHT_PROGBITS;
3281 /* Unwind descriptors are not part of the program memory image. */
3282 secthdr->sh_flags = 0;
3283 secthdr->sh_info = 0;
3284 secthdr->sh_link = 0;
3285 secthdr->sh_entsize = 16;
3286 return true;
3287 }
3288
3289 /* @@ Should this be CPU specific?? KR */
3290 if (!strcmp (asect->name, ".stabstr"))
3291 {
3292 secthdr->sh_type = SHT_STRTAB;
3293 secthdr->sh_flags = 0;
3294 secthdr->sh_info = 0;
3295 secthdr->sh_link = 0;
3296 secthdr->sh_entsize = 0;
3297 return true;
3298 }
3299
3300 return false;
3301 }
3302
3303 /* Return true if there is a mapping from bfd section into a
3304 backend section. */
3305
3306 static boolean
3307 elf32_hppa_backend_section_from_bfd_section (abfd, hdr, asect, ignored)
3308 bfd *abfd;
3309 Elf32_Internal_Shdr *hdr;
3310 asection *asect;
3311 int *ignored;
3312 {
3313 if (hdr->sh_type == SHT_HPPA_SYMEXTN)
3314 {
3315 if (hdr->rawdata)
3316 {
3317 if (((struct sec *) (hdr->rawdata)) == asect)
3318 {
3319 BFD_ASSERT (strcmp (asect->name, ".hppa_symextn") == 0);
3320 return true;
3321 }
3322 }
3323 }
3324 else if (hdr->sh_type == SHT_STRTAB)
3325 {
3326 if (hdr->rawdata)
3327 {
3328 if (((struct sec *) (hdr->rawdata)) == asect)
3329 {
3330 BFD_ASSERT (strcmp (asect->name, ".stabstr") == 0);
3331 return true;
3332 }
3333 }
3334 }
3335
3336 return false;
3337 }
3338
3339 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3340 #define elf_backend_section_from_bfd_section elf32_hppa_backend_section_from_bfd_section
3341
3342 #define elf_backend_symbol_processing elf32_hppa_backend_symbol_processing
3343 #define elf_backend_symbol_table_processing elf32_hppa_backend_symbol_table_processing
3344
3345 #define bfd_generic_get_section_contents hppa_elf_get_section_contents
3346 #define bfd_elf32_set_section_contents hppa_elf_set_section_contents
3347
3348 #define elf_backend_section_processing elf32_hppa_backend_section_processing
3349
3350 #define elf_backend_section_from_shdr elf32_hppa_backend_section_from_shdr
3351 #define elf_backend_fake_sections elf32_hppa_backend_fake_sections
3352
3353 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
3354 #define TARGET_BIG_NAME "elf32-hppa"
3355 #define ELF_ARCH bfd_arch_hppa
3356 #define ELF_MACHINE_CODE EM_HPPA
3357 #define ELF_MAXPAGESIZE 0x1000
3358
3359 #include "elf32-target.h"