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