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