* som.c (som_prep_for_fixups): New function.
[deliverable/binutils-gdb.git] / bfd / som.c
1 /* bfd back-end for HP PA-RISC SOM objects.
2 Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 Contributed by the Center for Software Science at the
5 University of Utah (pa-gdb-bugs@cs.utah.edu).
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25
26 #if defined (HOST_HPPAHPUX) || defined (HOST_HPPABSD)
27
28 #include "libbfd.h"
29 #include "som.h"
30 #include "libhppa.h"
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/dir.h>
36 #include <signal.h>
37 #include <machine/reg.h>
38 #include <sys/user.h> /* After a.out.h */
39 #include <sys/file.h>
40 #include <errno.h>
41
42 /* Magic not defined in standard HP-UX header files until 8.0 */
43
44 #ifndef CPU_PA_RISC1_0
45 #define CPU_PA_RISC1_0 0x20B
46 #endif /* CPU_PA_RISC1_0 */
47
48 #ifndef CPU_PA_RISC1_1
49 #define CPU_PA_RISC1_1 0x210
50 #endif /* CPU_PA_RISC1_1 */
51
52 #ifndef _PA_RISC1_0_ID
53 #define _PA_RISC1_0_ID CPU_PA_RISC1_0
54 #endif /* _PA_RISC1_0_ID */
55
56 #ifndef _PA_RISC1_1_ID
57 #define _PA_RISC1_1_ID CPU_PA_RISC1_1
58 #endif /* _PA_RISC1_1_ID */
59
60 #ifndef _PA_RISC_MAXID
61 #define _PA_RISC_MAXID 0x2FF
62 #endif /* _PA_RISC_MAXID */
63
64 #ifndef _PA_RISC_ID
65 #define _PA_RISC_ID(__m_num) \
66 (((__m_num) == _PA_RISC1_0_ID) || \
67 ((__m_num) >= _PA_RISC1_1_ID && (__m_num) <= _PA_RISC_MAXID))
68 #endif /* _PA_RISC_ID */
69
70 /* SOM allows any one of the four previous relocations to be reused
71 with a "R_PREV_FIXUP" relocation entry. Since R_PREV_FIXUP
72 relocations are always a single byte, using a R_PREV_FIXUP instead
73 of some multi-byte relocation makes object files smaller.
74
75 Note one side effect of using a R_PREV_FIXUP is the relocation that
76 is being repeated moves to the front of the queue. */
77 struct reloc_queue
78 {
79 unsigned char *reloc;
80 unsigned int size;
81 } reloc_queue[4];
82
83 /* This fully describes the symbol types which may be attached to
84 an EXPORT or IMPORT directive. Only SOM uses this formation
85 (ELF has no need for it). */
86 typedef enum
87 {
88 SYMBOL_TYPE_UNKNOWN,
89 SYMBOL_TYPE_ABSOLUTE,
90 SYMBOL_TYPE_CODE,
91 SYMBOL_TYPE_DATA,
92 SYMBOL_TYPE_ENTRY,
93 SYMBOL_TYPE_MILLICODE,
94 SYMBOL_TYPE_PLABEL,
95 SYMBOL_TYPE_PRI_PROG,
96 SYMBOL_TYPE_SEC_PROG,
97 } pa_symbol_type;
98
99 /* Forward declarations */
100
101 static boolean som_mkobject PARAMS ((bfd *));
102 static bfd_target * som_object_setup PARAMS ((bfd *,
103 struct header *,
104 struct som_exec_auxhdr *));
105 static asection * make_unique_section PARAMS ((bfd *, CONST char *, int));
106 static boolean setup_sections PARAMS ((bfd *, struct header *));
107 static bfd_target * som_object_p PARAMS ((bfd *));
108 static boolean som_write_object_contents PARAMS ((bfd *));
109 static boolean som_slurp_string_table PARAMS ((bfd *));
110 static unsigned int som_slurp_symbol_table PARAMS ((bfd *));
111 static unsigned int som_get_symtab_upper_bound PARAMS ((bfd *));
112 static unsigned int som_canonicalize_reloc PARAMS ((bfd *, sec_ptr,
113 arelent **, asymbol **));
114 static unsigned int som_get_reloc_upper_bound PARAMS ((bfd *, sec_ptr));
115 static unsigned int som_get_symtab PARAMS ((bfd *, asymbol **));
116 static asymbol * som_make_empty_symbol PARAMS ((bfd *));
117 static void som_print_symbol PARAMS ((bfd *, PTR,
118 asymbol *, bfd_print_symbol_type));
119 static boolean som_new_section_hook PARAMS ((bfd *, asection *));
120 static boolean som_set_section_contents PARAMS ((bfd *, sec_ptr, PTR,
121 file_ptr, bfd_size_type));
122 static boolean som_set_arch_mach PARAMS ((bfd *, enum bfd_architecture,
123 unsigned long));
124 static boolean som_find_nearest_line PARAMS ((bfd *, asection *,
125 asymbol **, bfd_vma,
126 CONST char **,
127 CONST char **,
128 unsigned int *));
129 static void som_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
130 static asection * som_section_from_subspace_index PARAMS ((bfd *,
131 unsigned int));
132 static int log2 PARAMS ((unsigned int));
133 static bfd_reloc_status_type hppa_som_reloc PARAMS ((bfd *, arelent *,
134 asymbol *, PTR,
135 asection *, bfd *));
136 static void som_initialize_reloc_queue PARAMS ((struct reloc_queue *));
137 static void som_reloc_queue_insert PARAMS ((unsigned char *, unsigned int,
138 struct reloc_queue *));
139 static void som_reloc_queue_fix PARAMS ((struct reloc_queue *, unsigned int));
140 static int som_reloc_queue_find PARAMS ((unsigned char *, unsigned int,
141 struct reloc_queue *));
142 static unsigned char * try_prev_fixup PARAMS ((bfd *, int *, unsigned char *,
143 unsigned int,
144 struct reloc_queue *));
145
146 static unsigned char * som_reloc_skip PARAMS ((bfd *, unsigned int,
147 unsigned char *, unsigned int *,
148 struct reloc_queue *));
149 static unsigned char * som_reloc_addend PARAMS ((bfd *, int, unsigned char *,
150 unsigned int *,
151 struct reloc_queue *));
152 static unsigned char * som_reloc_call PARAMS ((bfd *, unsigned char *,
153 unsigned int *,
154 arelent *, int,
155 struct reloc_queue *));
156 static unsigned long som_count_spaces PARAMS ((bfd *));
157 static unsigned long som_count_subspaces PARAMS ((bfd *));
158 static int compare_syms PARAMS ((asymbol **, asymbol **));
159 static unsigned long som_compute_checksum PARAMS ((bfd *));
160 static boolean som_prep_headers PARAMS ((bfd *));
161 static int som_sizeof_headers PARAMS ((bfd *, boolean));
162 static boolean som_write_headers PARAMS ((bfd *));
163 static boolean som_build_and_write_symbol_table PARAMS ((bfd *));
164 static void som_prep_for_fixups PARAMS ((bfd *, asymbol **, unsigned long));
165
166
167 static reloc_howto_type som_hppa_howto_table[] =
168 {
169 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
170 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
171 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
172 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
173 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
174 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
175 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
176 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
177 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
178 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
179 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
180 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
181 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
182 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
183 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
184 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
185 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
186 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
187 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
188 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
189 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
190 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
191 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
192 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
193 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
194 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
195 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
196 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
197 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
198 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
199 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
200 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
201 {R_ZEROES, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ZEROES"},
202 {R_ZEROES, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ZEROES"},
203 {R_UNINIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_UNINIT"},
204 {R_UNINIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_UNINIT"},
205 {R_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RELOCATION"},
206 {R_DATA_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_ONE_SYMBOL"},
207 {R_DATA_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_ONE_SYMBOL"},
208 {R_DATA_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_PLABEL"},
209 {R_DATA_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_PLABEL"},
210 {R_SPACE_REF, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_SPACE_REF"},
211 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
212 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
213 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
214 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
215 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
216 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
217 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
218 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
219 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
220 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
221 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
222 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
223 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
224 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
225 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
226 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
227 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
228 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
229 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
230 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
231 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
232 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
233 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
234 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
235 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
236 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
237 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
238 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
239 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
240 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
241 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
242 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
243 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
244 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
245 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
246 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
247 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
248 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
249 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
250 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
251 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
252 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
253 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
254 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
255 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
256 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
257 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
258 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
259 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
260 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
261 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
262 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
263 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
264 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
265 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
266 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
267 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
268 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
269 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
270 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
271 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
272 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
273 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
274 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
275 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
276 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
277 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
278 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
279 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
280 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
281 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
282 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
283 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
284 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
285 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
286 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
287 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
288 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
289 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
290 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
291 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
292 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
293 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
294 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
295 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
296 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
297 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
298 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
299 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
300 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
301 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
302 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
303 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
304 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
305 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
306 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
307 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
308 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
309 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
310 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
311 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
312 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
313 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
314 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
315 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
316 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
317 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
318 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
319 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
320 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
321 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
322 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
323 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
324 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
325 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
326 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
327 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
328 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
329 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
330 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
331 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
332 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
333 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
334 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
335 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
336 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
337 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
338 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
339 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
340 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
341 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
342 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
343 {R_MILLI_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_MILLI_REL"},
344 {R_MILLI_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_MILLI_REL"},
345 {R_CODE_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_PLABEL"},
346 {R_CODE_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_PLABEL"},
347 {R_BREAKPOINT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BREAKPOINT"},
348 {R_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ENTRY"},
349 {R_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ENTRY"},
350 {R_ALT_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ALT_ENTRY"},
351 {R_EXIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_EXIT"},
352 {R_BEGIN_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BEGIN_TRY"},
353 {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
354 {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
355 {R_BEGIN_BRTAB, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BEGIN_BRTAB"},
356 {R_END_BRTAB, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_BRTAB"},
357 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
358 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
359 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
360 {R_DATA_EXPR, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_EXPR"},
361 {R_CODE_EXPR, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_EXPR"},
362 {R_FSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_FSEL"},
363 {R_LSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_LSEL"},
364 {R_RSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RSEL"},
365 {R_N_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_N_MODE"},
366 {R_S_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_S_MODE"},
367 {R_D_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_D_MODE"},
368 {R_R_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_R_MODE"},
369 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
370 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
371 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
372 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
373 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
374 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
375 {R_TRANSLATED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_TRANSLATED"},
376 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
377 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
378 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
379 {R_COMP1, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP1"},
380 {R_COMP2, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP2"},
381 {R_COMP3, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP3"},
382 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
383 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
384 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
385 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
386 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
387 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
388 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
389 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
390 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
391 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
392 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
393 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
394 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
395 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
396 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
397 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
398 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
399 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
400 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
401 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
402 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
403 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
404 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
405 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
406 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
407 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
408 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
409 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
410 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
411 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
412 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
413 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
414 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
415 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
416 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
417 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
418 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
419 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
420 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
421 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
422 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
423 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
424 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
425 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
426 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"}};
427
428
429 /* Initialize the SOM relocation queue. By definition the queue holds
430 the last four multibyte fixups. */
431
432 static void
433 som_initialize_reloc_queue (queue)
434 struct reloc_queue *queue;
435 {
436 queue[0].reloc = NULL;
437 queue[0].size = 0;
438 queue[1].reloc = NULL;
439 queue[1].size = 0;
440 queue[2].reloc = NULL;
441 queue[2].size = 0;
442 queue[3].reloc = NULL;
443 queue[3].size = 0;
444 }
445
446 /* Insert a new relocation into the relocation queue. */
447
448 static void
449 som_reloc_queue_insert (p, size, queue)
450 unsigned char *p;
451 unsigned int size;
452 struct reloc_queue *queue;
453 {
454 queue[3].reloc = queue[2].reloc;
455 queue[3].size = queue[2].size;
456 queue[2].reloc = queue[1].reloc;
457 queue[2].size = queue[1].size;
458 queue[1].reloc = queue[0].reloc;
459 queue[1].size = queue[0].size;
460 queue[0].reloc = p;
461 queue[0].size = size;
462 }
463
464 /* When an entry in the relocation queue is reused, the entry moves
465 to the front of the queue. */
466
467 static void
468 som_reloc_queue_fix (queue, index)
469 struct reloc_queue *queue;
470 unsigned int index;
471 {
472 if (index == 0)
473 return;
474
475 if (index == 1)
476 {
477 unsigned char *tmp1 = queue[0].reloc;
478 unsigned int tmp2 = queue[0].size;
479 queue[0].reloc = queue[1].reloc;
480 queue[0].size = queue[1].size;
481 queue[1].reloc = tmp1;
482 queue[1].size = tmp2;
483 return;
484 }
485
486 if (index == 2)
487 {
488 unsigned char *tmp1 = queue[0].reloc;
489 unsigned int tmp2 = queue[0].size;
490 queue[0].reloc = queue[2].reloc;
491 queue[0].size = queue[2].size;
492 queue[2].reloc = queue[1].reloc;
493 queue[2].size = queue[1].size;
494 queue[1].reloc = tmp1;
495 queue[1].size = tmp2;
496 return;
497 }
498
499 if (index == 3)
500 {
501 unsigned char *tmp1 = queue[0].reloc;
502 unsigned int tmp2 = queue[0].size;
503 queue[0].reloc = queue[3].reloc;
504 queue[0].size = queue[3].size;
505 queue[3].reloc = queue[2].reloc;
506 queue[3].size = queue[2].size;
507 queue[2].reloc = queue[1].reloc;
508 queue[2].size = queue[1].size;
509 queue[1].reloc = tmp1;
510 queue[1].size = tmp2;
511 return;
512 }
513 abort();
514 }
515
516 /* Search for a particular relocation in the relocation queue. */
517
518 static int
519 som_reloc_queue_find (p, size, queue)
520 unsigned char *p;
521 unsigned int size;
522 struct reloc_queue *queue;
523 {
524 if (!bcmp (p, queue[0].reloc, size)
525 && size == queue[0].size)
526 return 0;
527 if (!bcmp (p, queue[1].reloc, size)
528 && size == queue[1].size)
529 return 1;
530 if (!bcmp (p, queue[2].reloc, size)
531 && size == queue[2].size)
532 return 2;
533 if (!bcmp (p, queue[3].reloc, size)
534 && size == queue[3].size)
535 return 3;
536 return -1;
537 }
538
539 static unsigned char *
540 try_prev_fixup (abfd, subspace_reloc_sizep, p, size, queue)
541 bfd *abfd;
542 int *subspace_reloc_sizep;
543 unsigned char *p;
544 unsigned int size;
545 struct reloc_queue *queue;
546 {
547 int queue_index = som_reloc_queue_find (p, size, queue);
548
549 if (queue_index != -1)
550 {
551 /* Found this in a previous fixup. Undo the fixup we
552 just built and use R_PREV_FIXUP instead. We saved
553 a total of size - 1 bytes in the fixup stream. */
554 bfd_put_8 (abfd, R_PREV_FIXUP + queue_index, p);
555 p += 1;
556 *subspace_reloc_sizep += 1;
557 som_reloc_queue_fix (queue, queue_index);
558 }
559 else
560 {
561 som_reloc_queue_insert (p, size, queue);
562 *subspace_reloc_sizep += size;
563 p += size;
564 }
565 return p;
566 }
567
568 /* Emit the proper R_NO_RELOCATION fixups to map the next SKIP
569 bytes without any relocation. Update the size of the subspace
570 relocation stream via SUBSPACE_RELOC_SIZE_P; also return the
571 current pointer into the relocation stream. */
572
573 static unsigned char *
574 som_reloc_skip (abfd, skip, p, subspace_reloc_sizep, queue)
575 bfd *abfd;
576 unsigned int skip;
577 unsigned char *p;
578 unsigned int *subspace_reloc_sizep;
579 struct reloc_queue *queue;
580 {
581 /* Use a 4 byte R_NO_RELOCATION entry with a maximal value
582 then R_PREV_FIXUPs to get the difference down to a
583 reasonable size. */
584 if (skip >= 0x1000000)
585 {
586 skip -= 0x1000000;
587 bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
588 bfd_put_8 (abfd, 0xff, p + 1);
589 bfd_put_16 (abfd, 0xffff, p + 2);
590 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
591 while (skip >= 0x1000000)
592 {
593 skip -= 0x1000000;
594 bfd_put_8 (abfd, R_PREV_FIXUP, p);
595 p++;
596 *subspace_reloc_sizep += 1;
597 /* No need to adjust queue here since we are repeating the
598 most recent fixup. */
599 }
600 }
601
602 /* The difference must be less than 0x1000000. Use one
603 more R_NO_RELOCATION entry to get to the right difference. */
604 if ((skip & 3) == 0 && skip <= 0xc0000 && skip > 0)
605 {
606 /* Difference can be handled in a simple single-byte
607 R_NO_RELOCATION entry. */
608 if (skip <= 0x60)
609 {
610 bfd_put_8 (abfd, R_NO_RELOCATION + (skip >> 2) - 1, p);
611 *subspace_reloc_sizep += 1;
612 p++;
613 }
614 /* Handle it with a two byte R_NO_RELOCATION entry. */
615 else if (skip <= 0x1000)
616 {
617 bfd_put_8 (abfd, R_NO_RELOCATION + 24 + (((skip >> 2) - 1) >> 8), p);
618 bfd_put_8 (abfd, (skip >> 2) - 1, p + 1);
619 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
620 }
621 /* Handle it with a three byte R_NO_RELOCATION entry. */
622 else
623 {
624 bfd_put_8 (abfd, R_NO_RELOCATION + 28 + (((skip >> 2) - 1) >> 16), p);
625 bfd_put_16 (abfd, (skip >> 2) - 1, p + 1);
626 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
627 }
628 }
629 /* Ugh. Punt and use a 4 byte entry. */
630 else if (skip > 0)
631 {
632 bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
633 bfd_put_8 (abfd, skip >> 16, p + 1);
634 bfd_put_16 (abfd, skip, p + 2);
635 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
636 }
637 return p;
638 }
639
640 /* Emit the proper R_DATA_OVERRIDE fixups to handle a nonzero addend
641 from a BFD relocation. Update the size of the subspace relocation
642 stream via SUBSPACE_RELOC_SIZE_P; also return the current pointer
643 into the relocation stream. */
644
645 static unsigned char *
646 som_reloc_addend (abfd, addend, p, subspace_reloc_sizep, queue)
647 bfd *abfd;
648 int addend;
649 unsigned char *p;
650 unsigned int *subspace_reloc_sizep;
651 struct reloc_queue *queue;
652 {
653 if ((unsigned)(addend) + 0x80 < 0x100)
654 {
655 bfd_put_8 (abfd, R_DATA_OVERRIDE + 1, p);
656 bfd_put_8 (abfd, addend, p + 1);
657 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
658 }
659 else if ((unsigned) (addend) + 0x8000 < 0x10000)
660 {
661 bfd_put_8 (abfd, R_DATA_OVERRIDE + 2, p);
662 bfd_put_16 (abfd, addend, p + 1);
663 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
664 }
665 else if ((unsigned) (addend) + 0x800000 < 0x1000000)
666 {
667 bfd_put_8 (abfd, R_DATA_OVERRIDE + 3, p);
668 bfd_put_8 (abfd, addend >> 16, p + 1);
669 bfd_put_16 (abfd, addend, p + 2);
670 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
671 }
672 else
673 {
674 bfd_put_8 (abfd, R_DATA_OVERRIDE + 4, p);
675 bfd_put_32 (abfd, addend, p + 1);
676 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
677 }
678 return p;
679 }
680
681 /* Handle a single function call relocation. */
682
683 static unsigned char *
684 som_reloc_call (abfd, p, subspace_reloc_sizep, bfd_reloc, sym_num, queue)
685 bfd *abfd;
686 unsigned char *p;
687 unsigned int *subspace_reloc_sizep;
688 arelent *bfd_reloc;
689 int sym_num;
690 struct reloc_queue *queue;
691 {
692 int arg_bits = HPPA_R_ARG_RELOC (bfd_reloc->addend);
693 int rtn_bits = arg_bits & 0x3;
694 int type, done = 0;
695
696 /* You'll never believe all this is necessary to handle relocations
697 for function calls. Having to compute and pack the argument
698 relocation bits is the real nightmare.
699
700 If you're interested in how this works, just forget it. You really
701 do not want to know about this braindamage. */
702
703 /* First see if this can be done with a "simple" relocation. Simple
704 relocations have a symbol number < 0x100 and have simple encodings
705 of argument relocations. */
706
707 if (sym_num < 0x100)
708 {
709 switch (arg_bits)
710 {
711 case 0:
712 case 1:
713 type = 0;
714 break;
715 case 1 << 8:
716 case 1 << 8 | 1:
717 type = 1;
718 break;
719 case 1 << 8 | 1 << 6:
720 case 1 << 8 | 1 << 6 | 1:
721 type = 2;
722 break;
723 case 1 << 8 | 1 << 6 | 1 << 4:
724 case 1 << 8 | 1 << 6 | 1 << 4 | 1:
725 type = 3;
726 break;
727 case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2:
728 case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2 | 1:
729 type = 4;
730 break;
731 default:
732 /* Not one of the easy encodings. This will have to be
733 handled by the more complex code below. */
734 type = -1;
735 break;
736 }
737 if (type != -1)
738 {
739 /* Account for the return value too. */
740 if (rtn_bits)
741 type += 5;
742
743 /* Emit a 2 byte relocation. Then see if it can be handled
744 with a relocation which is already in the relocation queue. */
745 bfd_put_8 (abfd, bfd_reloc->howto->type + type, p);
746 bfd_put_8 (abfd, sym_num, p + 1);
747 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
748 done = 1;
749 }
750 }
751
752 /* If this could not be handled with a simple relocation, then do a hard
753 one. Hard relocations occur if the symbol number was too high or if
754 the encoding of argument relocation bits is too complex. */
755 if (! done)
756 {
757 /* Don't ask about these magic sequences. I took them straight
758 from gas-1.36 which took them from the a.out man page. */
759 type = rtn_bits;
760 if ((arg_bits >> 6 & 0xf) == 0xe)
761 type += 9 * 40;
762 else
763 type += (3 * (arg_bits >> 8 & 3) + (arg_bits >> 6 & 3)) * 40;
764 if ((arg_bits >> 2 & 0xf) == 0xe)
765 type += 9 * 4;
766 else
767 type += (3 * (arg_bits >> 4 & 3) + (arg_bits >> 2 & 3)) * 4;
768
769 /* Output the first two bytes of the relocation. These describe
770 the length of the relocation and encoding style. */
771 bfd_put_8 (abfd, bfd_reloc->howto->type + 10
772 + 2 * (sym_num >= 0x100) + (type >= 0x100),
773 p);
774 bfd_put_8 (abfd, type, p + 1);
775
776 /* Now output the symbol index and see if this bizarre relocation
777 just happened to be in the relocation queue. */
778 if (sym_num < 0x100)
779 {
780 bfd_put_8 (abfd, sym_num, p + 2);
781 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
782 }
783 else
784 {
785 bfd_put_8 (abfd, sym_num >> 16, p + 2);
786 bfd_put_16 (abfd, sym_num, p + 3);
787 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
788 }
789 }
790 return p;
791 }
792
793
794 /* Return the logarithm of X, base 2, considering X unsigned.
795 Abort if X is not a power of two -- this should never happen (FIXME:
796 It will happen on corrupt executables. GDB should give an error, not
797 a coredump, in that case). */
798
799 static int
800 log2 (x)
801 unsigned int x;
802 {
803 int log = 0;
804
805 /* Test for 0 or a power of 2. */
806 if (x == 0 || x != (x & -x))
807 abort();
808
809 while ((x >>= 1) != 0)
810 log++;
811 return log;
812 }
813
814 static bfd_reloc_status_type
815 hppa_som_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd)
816 bfd *abfd;
817 arelent *reloc_entry;
818 asymbol *symbol_in;
819 PTR data;
820 asection *input_section;
821 bfd *output_bfd;
822 {
823 if (output_bfd)
824 {
825 reloc_entry->address += input_section->output_offset;
826 return bfd_reloc_ok;
827 }
828 return bfd_reloc_ok;
829 }
830
831 /* Given a generic HPPA relocation type, the instruction format,
832 and a field selector, return an appropriate SOM reloation.
833
834 FIXME. Need to handle %RR, %LR and the like as field selectors.
835 These will need to generate multiple SOM relocations. */
836
837 int **
838 hppa_som_gen_reloc_type (abfd, base_type, format, field)
839 bfd *abfd;
840 int base_type;
841 int format;
842 int field;
843 {
844 int *final_type, **final_types;
845
846 final_types = (int **) bfd_alloc_by_size_t (abfd, sizeof (int *) * 2);
847 final_type = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
848
849
850 final_types[0] = final_type;
851 final_types[1] = NULL;
852
853 /* Default to the basic relocation passed in. */
854 *final_type = base_type;
855
856 switch (base_type)
857 {
858 case R_HPPA:
859 /* PLABELs get their own relocation type. */
860 if (field == e_psel
861 || field == e_lpsel
862 || field == e_rpsel)
863 {
864 /* A PLABEL relocation that has a size of 32 bits must
865 be a R_DATA_PLABEL. All others are R_CODE_PLABELs. */
866 if (format == 32)
867 *final_type = R_DATA_PLABEL;
868 else
869 *final_type = R_CODE_PLABEL;
870 }
871 /* A relocatoin in the data space is always a full 32bits. */
872 else if (format == 32)
873 *final_type = R_DATA_ONE_SYMBOL;
874
875 break;
876
877 case R_HPPA_GOTOFF:
878 /* More PLABEL special cases. */
879 if (field == e_psel
880 || field == e_lpsel
881 || field == e_rpsel)
882 *final_type = R_DATA_PLABEL;
883 break;
884
885 case R_HPPA_NONE:
886 case R_HPPA_ABS_CALL:
887 case R_HPPA_PCREL_CALL:
888 case R_HPPA_COMPLEX:
889 case R_HPPA_COMPLEX_PCREL_CALL:
890 case R_HPPA_COMPLEX_ABS_CALL:
891 /* Right now we can default all these. */
892 break;
893 }
894 return final_types;
895 }
896
897 /* Return the address of the correct entry in the PA SOM relocation
898 howto table. */
899
900 static reloc_howto_type *
901 som_bfd_reloc_type_lookup (arch, code)
902 bfd_arch_info_type *arch;
903 bfd_reloc_code_real_type code;
904 {
905 if ((int) code < (int) R_NO_RELOCATION + 255)
906 {
907 BFD_ASSERT ((int) som_hppa_howto_table[(int) code].type == (int) code);
908 return &som_hppa_howto_table[(int) code];
909 }
910
911 return (reloc_howto_type *) 0;
912 }
913
914 /* Perform some initialization for an object. Save results of this
915 initialization in the BFD. */
916
917 static bfd_target *
918 som_object_setup (abfd, file_hdrp, aux_hdrp)
919 bfd *abfd;
920 struct header *file_hdrp;
921 struct som_exec_auxhdr *aux_hdrp;
922 {
923 asection *text, *data, *bss;
924
925 /* som_mkobject will set bfd_error if som_mkobject fails. */
926 if (som_mkobject (abfd) != true)
927 return 0;
928
929 /* Make the standard .text, .data, and .bss sections so that tools
930 which assume those names work (size for example). They will have
931 no contents, but the sizes and such will reflect those of the
932 $CODE$, $DATA$, and $BSS$ subspaces respectively.
933
934 FIXME: Should check return status from bfd_make_section calls below. */
935
936 text = bfd_make_section (abfd, ".text");
937 data = bfd_make_section (abfd, ".data");
938 bss = bfd_make_section (abfd, ".bss");
939
940 text->_raw_size = aux_hdrp->exec_tsize;
941 data->_raw_size = aux_hdrp->exec_dsize;
942 bss->_raw_size = aux_hdrp->exec_bsize;
943
944 text->flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_CODE);
945 data->flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS);
946 bss->flags = (SEC_ALLOC | SEC_IS_COMMON);
947
948 /* The virtual memory addresses of the sections */
949 text->vma = aux_hdrp->exec_tmem;
950 data->vma = aux_hdrp->exec_dmem;
951 bss->vma = aux_hdrp->exec_bfill;
952
953 /* The file offsets of the sections */
954 text->filepos = aux_hdrp->exec_tfile;
955 data->filepos = aux_hdrp->exec_dfile;
956
957 /* The file offsets of the relocation info */
958 text->rel_filepos = 0;
959 data->rel_filepos = 0;
960
961 /* Set BFD flags based on what information is available in the SOM. */
962 abfd->flags = NO_FLAGS;
963 if (! file_hdrp->entry_offset)
964 abfd->flags |= HAS_RELOC;
965 else
966 abfd->flags |= EXEC_P;
967 if (file_hdrp->symbol_total)
968 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
969
970 bfd_get_start_address (abfd) = aux_hdrp->exec_entry;
971 bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 0);
972 bfd_get_symcount (abfd) = file_hdrp->symbol_total;
973
974 /* Initialize the saved symbol table and string table to NULL.
975 Save important offsets and sizes from the SOM header into
976 the BFD. */
977 obj_som_stringtab (abfd) = (char *) NULL;
978 obj_som_symtab (abfd) = (som_symbol_type *) NULL;
979 obj_som_stringtab_size (abfd) = file_hdrp->symbol_strings_size;
980 obj_som_sym_filepos (abfd) = file_hdrp->symbol_location;
981 obj_som_str_filepos (abfd) = file_hdrp->symbol_strings_location;
982 obj_som_reloc_filepos (abfd) = file_hdrp->fixup_request_location;
983
984 return abfd->xvec;
985 }
986
987 /* Create a new BFD section for NAME. If NAME already exists, then create a
988 new unique name, with NAME as the prefix. This exists because SOM .o files
989 may have more than one $CODE$ subspace. */
990
991 static asection *
992 make_unique_section (abfd, name, num)
993 bfd *abfd;
994 CONST char *name;
995 int num;
996 {
997 asection *sect;
998 char *newname;
999 char altname[100];
1000
1001 sect = bfd_make_section (abfd, name);
1002 while (!sect)
1003 {
1004 sprintf (altname, "%s-%d", name, num++);
1005 sect = bfd_make_section (abfd, altname);
1006 }
1007
1008 newname = bfd_alloc (abfd, strlen (sect->name) + 1);
1009 strcpy (newname, sect->name);
1010
1011 sect->name = newname;
1012 return sect;
1013 }
1014
1015 /* Convert all of the space and subspace info into BFD sections. Each space
1016 contains a number of subspaces, which in turn describe the mapping between
1017 regions of the exec file, and the address space that the program runs in.
1018 BFD sections which correspond to spaces will overlap the sections for the
1019 associated subspaces. */
1020
1021 static boolean
1022 setup_sections (abfd, file_hdr)
1023 bfd *abfd;
1024 struct header *file_hdr;
1025 {
1026 char *space_strings;
1027 int space_index;
1028 unsigned int total_subspaces = 0;
1029
1030 /* First, read in space names */
1031
1032 space_strings = alloca (file_hdr->space_strings_size);
1033 if (!space_strings)
1034 return false;
1035
1036 if (bfd_seek (abfd, file_hdr->space_strings_location, SEEK_SET) < 0)
1037 return false;
1038 if (bfd_read (space_strings, 1, file_hdr->space_strings_size, abfd)
1039 != file_hdr->space_strings_size)
1040 return false;
1041
1042 /* Loop over all of the space dictionaries, building up sections */
1043 for (space_index = 0; space_index < file_hdr->space_total; space_index++)
1044 {
1045 struct space_dictionary_record space;
1046 struct subspace_dictionary_record subspace, save_subspace;
1047 int subspace_index;
1048 asection *space_asect;
1049
1050 /* Read the space dictionary element */
1051 if (bfd_seek (abfd, file_hdr->space_location
1052 + space_index * sizeof space, SEEK_SET) < 0)
1053 return false;
1054 if (bfd_read (&space, 1, sizeof space, abfd) != sizeof space)
1055 return false;
1056
1057 /* Setup the space name string */
1058 space.name.n_name = space.name.n_strx + space_strings;
1059
1060 /* Make a section out of it */
1061 space_asect = make_unique_section (abfd, space.name.n_name, space_index);
1062 if (!space_asect)
1063 return false;
1064
1065 /* Now, read in the first subspace for this space */
1066 if (bfd_seek (abfd, file_hdr->subspace_location
1067 + space.subspace_index * sizeof subspace,
1068 SEEK_SET) < 0)
1069 return false;
1070 if (bfd_read (&subspace, 1, sizeof subspace, abfd) != sizeof subspace)
1071 return false;
1072 /* Seek back to the start of the subspaces for loop below */
1073 if (bfd_seek (abfd, file_hdr->subspace_location
1074 + space.subspace_index * sizeof subspace,
1075 SEEK_SET) < 0)
1076 return false;
1077
1078 /* Setup the start address and file loc from the first subspace record */
1079 space_asect->vma = subspace.subspace_start;
1080 space_asect->filepos = subspace.file_loc_init_value;
1081 space_asect->alignment_power = log2 (subspace.alignment);
1082
1083 /* Initialize save_subspace so we can reliably determine if this
1084 loop placed any useful values into it. */
1085 bzero (&save_subspace, sizeof (struct subspace_dictionary_record));
1086
1087 /* Loop over the rest of the subspaces, building up more sections */
1088 for (subspace_index = 0; subspace_index < space.subspace_quantity;
1089 subspace_index++)
1090 {
1091 asection *subspace_asect;
1092
1093 /* Read in the next subspace */
1094 if (bfd_read (&subspace, 1, sizeof subspace, abfd)
1095 != sizeof subspace)
1096 return false;
1097
1098 /* Setup the subspace name string */
1099 subspace.name.n_name = subspace.name.n_strx + space_strings;
1100
1101 /* Make a section out of this subspace */
1102 subspace_asect = make_unique_section (abfd, subspace.name.n_name,
1103 space.subspace_index + subspace_index);
1104
1105 if (!subspace_asect)
1106 return false;
1107
1108 /* Keep an easy mapping between subspaces and sections. */
1109 som_section_data (subspace_asect)->subspace_index
1110 = total_subspaces++;
1111
1112 /* Set SEC_READONLY and SEC_CODE/SEC_DATA as specified
1113 by the access_control_bits in the subspace header. */
1114 switch (subspace.access_control_bits >> 4)
1115 {
1116 /* Readonly data. */
1117 case 0x0:
1118 subspace_asect->flags |= SEC_DATA | SEC_READONLY;
1119 break;
1120
1121 /* Normal data. */
1122 case 0x1:
1123 subspace_asect->flags |= SEC_DATA;
1124 break;
1125
1126 /* Readonly code and the gateways.
1127 Gateways have other attributes which do not map
1128 into anything BFD knows about. */
1129 case 0x2:
1130 case 0x4:
1131 case 0x5:
1132 case 0x6:
1133 case 0x7:
1134 subspace_asect->flags |= SEC_CODE | SEC_READONLY;
1135 break;
1136
1137 /* dynamic (writable) code. */
1138 case 0x3:
1139 subspace_asect->flags |= SEC_CODE;
1140 break;
1141 }
1142
1143 if (subspace.dup_common || subspace.is_common)
1144 subspace_asect->flags |= SEC_IS_COMMON;
1145 else
1146 subspace_asect->flags |= SEC_HAS_CONTENTS;
1147 if (subspace.is_loadable)
1148 subspace_asect->flags |= SEC_ALLOC | SEC_LOAD;
1149 if (subspace.code_only)
1150 subspace_asect->flags |= SEC_CODE;
1151
1152 /* This subspace has relocations.
1153 The fixup_request_quantity is a byte count for the number of
1154 entries in the relocation stream; it is not the actual number
1155 of relocations in the subspace. */
1156 if (subspace.fixup_request_quantity != 0)
1157 {
1158 subspace_asect->flags |= SEC_RELOC;
1159 subspace_asect->rel_filepos = subspace.fixup_request_index;
1160 som_section_data (subspace_asect)->reloc_size
1161 = subspace.fixup_request_quantity;
1162 /* We can not determine this yet. When we read in the
1163 relocation table the correct value will be filled in. */
1164 subspace_asect->reloc_count = -1;
1165 }
1166
1167 /* Update save_subspace if appropriate. */
1168 if (subspace.file_loc_init_value > save_subspace.file_loc_init_value)
1169 save_subspace = subspace;
1170
1171 subspace_asect->vma = subspace.subspace_start;
1172 subspace_asect->_cooked_size = subspace.subspace_length;
1173 subspace_asect->_raw_size = subspace.initialization_length;
1174 subspace_asect->alignment_power = log2 (subspace.alignment);
1175 subspace_asect->filepos = subspace.file_loc_init_value;
1176 }
1177
1178 /* Yow! there is no subspace within the space which actually
1179 has initialized information in it; this should never happen
1180 as far as I know. */
1181 if (!save_subspace.file_loc_init_value)
1182 abort ();
1183
1184 /* Setup the sizes for the space section based upon the info in the
1185 last subspace of the space. */
1186 space_asect->_cooked_size = save_subspace.subspace_start
1187 - space_asect->vma + save_subspace.subspace_length;
1188 space_asect->_raw_size = save_subspace.file_loc_init_value
1189 - space_asect->filepos + save_subspace.initialization_length;
1190 }
1191 return true;
1192 }
1193
1194 /* Read in a SOM object and make it into a BFD. */
1195
1196 static bfd_target *
1197 som_object_p (abfd)
1198 bfd *abfd;
1199 {
1200 struct header file_hdr;
1201 struct som_exec_auxhdr aux_hdr;
1202
1203 if (bfd_read ((PTR) & file_hdr, 1, FILE_HDR_SIZE, abfd) != FILE_HDR_SIZE)
1204 {
1205 bfd_error = system_call_error;
1206 return 0;
1207 }
1208
1209 if (!_PA_RISC_ID (file_hdr.system_id))
1210 {
1211 bfd_error = wrong_format;
1212 return 0;
1213 }
1214
1215 switch (file_hdr.a_magic)
1216 {
1217 case RELOC_MAGIC:
1218 case EXEC_MAGIC:
1219 case SHARE_MAGIC:
1220 case DEMAND_MAGIC:
1221 #ifdef DL_MAGIC
1222 case DL_MAGIC:
1223 #endif
1224 #ifdef SHL_MAGIC
1225 case SHL_MAGIC:
1226 #endif
1227 #ifdef EXECLIBMAGIC
1228 case EXECLIBMAGIC:
1229 #endif
1230 break;
1231 default:
1232 bfd_error = wrong_format;
1233 return 0;
1234 }
1235
1236 if (file_hdr.version_id != VERSION_ID
1237 && file_hdr.version_id != NEW_VERSION_ID)
1238 {
1239 bfd_error = wrong_format;
1240 return 0;
1241 }
1242
1243 /* If the aux_header_size field in the file header is zero, then this
1244 object is an incomplete executable (a .o file). Do not try to read
1245 a non-existant auxiliary header. */
1246 bzero (&aux_hdr, sizeof (struct som_exec_auxhdr));
1247 if (file_hdr.aux_header_size != 0)
1248 {
1249 if (bfd_read ((PTR) & aux_hdr, 1, AUX_HDR_SIZE, abfd) != AUX_HDR_SIZE)
1250 {
1251 bfd_error = wrong_format;
1252 return 0;
1253 }
1254 }
1255
1256 if (!setup_sections (abfd, &file_hdr))
1257 {
1258 /* setup_sections does not bubble up a bfd error code. */
1259 bfd_error = bad_value;
1260 return 0;
1261 }
1262
1263 /* This appears to be a valid SOM object. Do some initialization. */
1264 return som_object_setup (abfd, &file_hdr, &aux_hdr);
1265 }
1266
1267 /* Create a SOM object. */
1268
1269 static boolean
1270 som_mkobject (abfd)
1271 bfd *abfd;
1272 {
1273 /* Allocate memory to hold backend information. */
1274 abfd->tdata.som_data = (struct som_data_struct *)
1275 bfd_zalloc (abfd, sizeof (struct som_data_struct));
1276 if (abfd->tdata.som_data == NULL)
1277 {
1278 bfd_error = no_memory;
1279 return false;
1280 }
1281 obj_som_file_hdr (abfd) = bfd_zalloc (abfd, sizeof (struct header));
1282 if (obj_som_file_hdr (abfd) == NULL)
1283
1284 {
1285 bfd_error = no_memory;
1286 return false;
1287 }
1288 return true;
1289 }
1290
1291 /* Initialize some information in the file header. This routine makes
1292 not attempt at doing the right thing for a full executable; it
1293 is only meant to handle relocatable objects. */
1294
1295 static boolean
1296 som_prep_headers (abfd)
1297 bfd *abfd;
1298 {
1299 struct header *file_hdr = obj_som_file_hdr (abfd);
1300 asection *section;
1301
1302 /* FIXME. This should really be conditional based on whether or not
1303 PA1.1 instructions/registers have been used. */
1304 file_hdr->system_id = HP9000S800_ID;
1305
1306 /* FIXME. Only correct for building relocatable objects. */
1307 if (abfd->flags & EXEC_P)
1308 abort ();
1309 else
1310 file_hdr->a_magic = RELOC_MAGIC;
1311
1312 /* Only new format SOM is supported. */
1313 file_hdr->version_id = NEW_VERSION_ID;
1314
1315 /* These fields are optional, and embedding timestamps is not always
1316 a wise thing to do, it makes comparing objects during a multi-stage
1317 bootstrap difficult. */
1318 file_hdr->file_time.secs = 0;
1319 file_hdr->file_time.nanosecs = 0;
1320
1321 if (abfd->flags & EXEC_P)
1322 abort ();
1323 else
1324 {
1325 file_hdr->entry_space = 0;
1326 file_hdr->entry_subspace = 0;
1327 file_hdr->entry_offset = 0;
1328 }
1329
1330 /* FIXME. I do not know if we ever need to put anything other
1331 than zero in this field. */
1332 file_hdr->presumed_dp = 0;
1333
1334 /* Now iterate over the sections translating information from
1335 BFD sections to SOM spaces/subspaces. */
1336
1337 for (section = abfd->sections; section != NULL; section = section->next)
1338 {
1339 /* Ignore anything which has not been marked as a space or
1340 subspace. */
1341 if (som_section_data (section)->is_space == 0
1342
1343 && som_section_data (section)->is_subspace == 0)
1344 continue;
1345
1346 if (som_section_data (section)->is_space)
1347 {
1348 /* Set space attributes. Note most attributes of SOM spaces
1349 are set based on the subspaces it contains. */
1350 som_section_data (section)->space_dict.loader_fix_index = -1;
1351 som_section_data (section)->space_dict.init_pointer_index = -1;
1352 }
1353 else
1354 {
1355 /* Set subspace attributes. Basic stuff is done here, additional
1356 attributes are filled in later as more information becomes
1357 available. */
1358 if (section->flags & SEC_IS_COMMON)
1359 {
1360 som_section_data (section)->subspace_dict.dup_common = 1;
1361 som_section_data (section)->subspace_dict.is_common = 1;
1362 }
1363
1364 if (section->flags & SEC_ALLOC)
1365 som_section_data (section)->subspace_dict.is_loadable = 1;
1366
1367 if (section->flags & SEC_CODE)
1368 som_section_data (section)->subspace_dict.code_only = 1;
1369
1370 som_section_data (section)->subspace_dict.subspace_start =
1371 section->vma;
1372 som_section_data (section)->subspace_dict.subspace_length =
1373 bfd_section_size (abfd, section);
1374 som_section_data (section)->subspace_dict.initialization_length =
1375 bfd_section_size (abfd, section);
1376 som_section_data (section)->subspace_dict.alignment =
1377 1 << section->alignment_power;
1378 }
1379 }
1380 return true;
1381 }
1382
1383 /* Count and return the number of spaces attached to the given BFD. */
1384
1385 static unsigned long
1386 som_count_spaces (abfd)
1387 bfd *abfd;
1388 {
1389 int count = 0;
1390 asection *section;
1391
1392 for (section = abfd->sections; section != NULL; section = section->next)
1393 count += som_section_data (section)->is_space;
1394
1395 return count;
1396 }
1397
1398 /* Count the number of subspaces attached to the given BFD. */
1399
1400 static unsigned long
1401 som_count_subspaces (abfd)
1402 bfd *abfd;
1403 {
1404 int count = 0;
1405 asection *section;
1406
1407 for (section = abfd->sections; section != NULL; section = section->next)
1408 count += som_section_data (section)->is_subspace;
1409
1410 return count;
1411 }
1412
1413 /* Return -1, 0, 1 indicating the relative ordering of sym1 and sym2.
1414
1415 We desire symbols to be ordered starting with the symbol with the
1416 highest relocation count down to the symbol with the lowest relocation
1417 count. Doing so compacts the relocation stream. */
1418
1419 static int
1420 compare_syms (sym1, sym2)
1421 asymbol **sym1;
1422 asymbol **sym2;
1423
1424 {
1425 unsigned int count1, count2;
1426
1427 /* Get relocation count for each symbol. Note that the count
1428 is stored in the udata pointer for section symbols! */
1429 if ((*sym1)->flags & BSF_SECTION_SYM)
1430 count1 = (int)(*sym1)->udata;
1431 else
1432 count1 = (*som_symbol_data ((*sym1)))->reloc_count;
1433
1434 if ((*sym2)->flags & BSF_SECTION_SYM)
1435 count2 = (int)(*sym2)->udata;
1436 else
1437 count2 = (*som_symbol_data ((*sym2)))->reloc_count;
1438
1439 /* Return the appropriate value. */
1440 if (count1 < count2)
1441 return 1;
1442 else if (count1 > count2)
1443 return -1;
1444 return 0;
1445 }
1446
1447 /* Perform various work in preparation for emitting the fixup stream. */
1448
1449 static void
1450 som_prep_for_fixups (abfd, syms, num_syms)
1451 bfd *abfd;
1452 asymbol **syms;
1453 unsigned long num_syms;
1454 {
1455 int i;
1456 asection *section;
1457
1458 /* Most SOM relocations involving a symbol have a length which is
1459 dependent on the index of the symbol. So symbols which are
1460 used often in relocations should have a small index. */
1461
1462 /* First initialize the counters for each symbol. */
1463 for (i = 0; i < num_syms; i++)
1464 {
1465 /* Handle a section symbol; these have no pointers back to the
1466 SOM symbol info. So we just use the pointer field (udata)
1467 to hold the relocation count.
1468
1469 FIXME. While we're here set the name of any section symbol
1470 to something which will not screw GDB. How do other formats
1471 deal with this?!? */
1472 if (som_symbol_data (syms[i]) == NULL)
1473 {
1474 syms[i]->flags |= BSF_SECTION_SYM;
1475 syms[i]->name = "L$0\002";
1476 syms[i]->udata = (PTR) 0;
1477 }
1478 else
1479 (*som_symbol_data (syms[i]))->reloc_count = 0;
1480 }
1481
1482 /* Now that the counters are initialized, make a weighted count
1483 of how often a given symbol is used in a relocation. */
1484 for (section = abfd->sections; section != NULL; section = section->next)
1485 {
1486 int i;
1487
1488 /* Does this section have any relocations? */
1489 if (section->reloc_count <= 0)
1490 continue;
1491
1492 /* Walk through each relocation for this section. */
1493 for (i = 1; i < section->reloc_count; i++)
1494 {
1495 arelent *reloc = section->orelocation[i];
1496 int scale;
1497
1498 /* If no symbol, then there is no counter to increase. */
1499 if (reloc->sym_ptr_ptr == NULL)
1500 continue;
1501
1502 /* Scaling to encourage symbols involved in R_DP_RELATIVE
1503 and R_CODE_ONE_SYMBOL relocations to come first. These
1504 two relocations have single byte versions if the symbol
1505 index is very small. */
1506 if (reloc->howto->type == R_DP_RELATIVE
1507 || reloc->howto->type == R_CODE_ONE_SYMBOL)
1508 scale = 2;
1509 else
1510 scale = 1;
1511
1512 /* Handle section symbols by ramming the count in the udata
1513 field. It will not be used and the count is very important
1514 for these symbols. */
1515 if ((*reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
1516 {
1517 (*reloc->sym_ptr_ptr)->udata =
1518 (PTR) ((int) (*reloc->sym_ptr_ptr)->udata + scale);
1519 continue;
1520 }
1521
1522 /* A normal symbol. Increment the count. */
1523 (*som_symbol_data ((*reloc->sym_ptr_ptr)))->reloc_count += scale;
1524 }
1525 }
1526
1527 /* Now sort the symbols. */
1528 qsort (syms, num_syms, sizeof (asymbol *), compare_syms);
1529
1530 /* Compute the symbol indexes, they will be needed by the relocation
1531 code. */
1532 for (i = 0; i < num_syms; i++)
1533 {
1534 /* A section symbol. Again, there is no pointer to backend symbol
1535 information, so we reuse (abuse) the udata field again. */
1536 if (syms[i]->flags & BSF_SECTION_SYM)
1537 syms[i]->udata = (PTR) i;
1538 else
1539 (*som_symbol_data (syms[i]))->index = i;
1540 }
1541 }
1542
1543 /* Finally, scribble out the various headers to the disk. */
1544
1545 static boolean
1546 som_write_headers (abfd)
1547 bfd *abfd;
1548 {
1549 int num_spaces = som_count_spaces (abfd);
1550 int i;
1551 int subspace_index = 0;
1552 file_ptr location;
1553 asection *section;
1554
1555 /* Subspaces are written first so that we can set up information
1556 about them in their containing spaces as the subspace is written. */
1557
1558 /* Seek to the start of the subspace dictionary records. */
1559 location = obj_som_file_hdr (abfd)->subspace_location;
1560 bfd_seek (abfd, location, SEEK_SET);
1561 section = abfd->sections;
1562 /* Now for each loadable space write out records for its subspaces. */
1563 for (i = 0; i < num_spaces; i++)
1564 {
1565 asection *subsection;
1566
1567 /* Find a space. */
1568 while (som_section_data (section)->is_space == 0)
1569 section = section->next;
1570
1571 /* Now look for all its subspaces. */
1572 for (subsection = abfd->sections;
1573 subsection != NULL;
1574 subsection = subsection->next)
1575 {
1576
1577 /* Skip any section which does not correspond to a space
1578 or subspace. Or does not have SEC_ALLOC set (and therefore
1579 has no real bits on the disk). */
1580 if (som_section_data (subsection)->is_subspace == 0
1581 || som_section_data (subsection)->containing_space != section
1582 || (subsection->flags & SEC_ALLOC) == 0)
1583 continue;
1584
1585 /* If this is the first subspace for this space, then save
1586 the index of the subspace in its containing space. Also
1587 set "is_loadable" in the containing space. */
1588
1589 if (som_section_data (section)->space_dict.subspace_quantity == 0)
1590 {
1591 som_section_data (section)->space_dict.is_loadable = 1;
1592 som_section_data (section)->space_dict.subspace_index
1593 = subspace_index;
1594 }
1595
1596 /* Increment the number of subspaces seen and the number of
1597 subspaces contained within the current space. */
1598 subspace_index++;
1599 som_section_data (section)->space_dict.subspace_quantity++;
1600
1601 /* Mark the index of the current space within the subspace's
1602 dictionary record. */
1603 som_section_data (subsection)->subspace_dict.space_index = i;
1604
1605 /* Dump the current subspace header. */
1606 if (bfd_write ((PTR) &som_section_data (subsection)->subspace_dict,
1607 sizeof (struct subspace_dictionary_record), 1, abfd)
1608 != sizeof (struct subspace_dictionary_record))
1609 {
1610 bfd_error = system_call_error;
1611 return false;
1612 }
1613 }
1614 /* Goto the next section. */
1615 section = section->next;
1616 }
1617
1618 /* Now repeat the process for unloadable subspaces. */
1619 section = abfd->sections;
1620 /* Now for each space write out records for its subspaces. */
1621 for (i = 0; i < num_spaces; i++)
1622 {
1623 asection *subsection;
1624
1625 /* Find a space. */
1626 while (som_section_data (section)->is_space == 0)
1627 section = section->next;
1628
1629 /* Now look for all its subspaces. */
1630 for (subsection = abfd->sections;
1631 subsection != NULL;
1632 subsection = subsection->next)
1633 {
1634
1635 /* Skip any section which does not correspond to a space or
1636 subspace, or which SEC_ALLOC set (and therefore handled
1637 in the loadable spaces/subspaces code above. */
1638
1639 if (som_section_data (subsection)->is_subspace == 0
1640 || som_section_data (subsection)->containing_space != section
1641 || (subsection->flags & SEC_ALLOC) != 0)
1642 continue;
1643
1644 /* If this is the first subspace for this space, then save
1645 the index of the subspace in its containing space. Clear
1646 "is_loadable". */
1647
1648 if (som_section_data (section)->space_dict.subspace_quantity == 0)
1649 {
1650 som_section_data (section)->space_dict.is_loadable = 0;
1651 som_section_data (section)->space_dict.subspace_index
1652 = subspace_index;
1653 }
1654
1655 /* Increment the number of subspaces seen and the number of
1656 subspaces contained within the current space. */
1657 som_section_data (section)->space_dict.subspace_quantity++;
1658 subspace_index++;
1659
1660 /* Mark the index of the current space within the subspace's
1661 dictionary record. */
1662 som_section_data (subsection)->subspace_dict.space_index = i;
1663
1664 /* Dump this subspace header. */
1665 if (bfd_write ((PTR) &som_section_data (subsection)->subspace_dict,
1666 sizeof (struct subspace_dictionary_record), 1, abfd)
1667 != sizeof (struct subspace_dictionary_record))
1668 {
1669 bfd_error = system_call_error;
1670 return false;
1671 }
1672 }
1673 /* Goto the next section. */
1674 section = section->next;
1675 }
1676
1677 /* All the subspace dictiondary records are written, and all the
1678 fields are set up in the space dictionary records.
1679
1680 Seek to the right location and start writing the space
1681 dictionary records. */
1682 location = obj_som_file_hdr (abfd)->space_location;
1683 bfd_seek (abfd, location, SEEK_SET);
1684
1685 section = abfd->sections;
1686 for (i = 0; i < num_spaces; i++)
1687 {
1688
1689 /* Find a space. */
1690 while (som_section_data (section)->is_space == 0)
1691 section = section->next;
1692
1693 /* Dump its header */
1694 if (bfd_write ((PTR) &som_section_data (section)->space_dict,
1695 sizeof (struct space_dictionary_record), 1, abfd)
1696 != sizeof (struct space_dictionary_record))
1697 {
1698 bfd_error = system_call_error;
1699 return false;
1700 }
1701
1702 /* Goto the next section. */
1703 section = section->next;
1704 }
1705
1706 /* Only thing left to do is write out the file header. It is always
1707 at location zero. Seek there and write it. */
1708 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
1709 if (bfd_write ((PTR) obj_som_file_hdr (abfd),
1710 sizeof (struct header), 1, abfd)
1711 != sizeof (struct header))
1712 {
1713 bfd_error = system_call_error;
1714 return false;
1715 }
1716 return true;
1717 }
1718
1719 /* Compute and return the checksum for a SOM file header. */
1720
1721 static unsigned long
1722 som_compute_checksum (abfd)
1723 bfd *abfd;
1724 {
1725 unsigned long checksum, count, i;
1726 unsigned long *buffer = (unsigned long *) obj_som_file_hdr (abfd);
1727
1728 checksum = 0;
1729 count = sizeof (struct header) / sizeof (unsigned long);
1730 for (i = 0; i < count; i++)
1731 checksum ^= *(buffer + i);
1732
1733 return checksum;
1734 }
1735
1736 /* Build and write, in one big chunk, the entire symbol table for
1737 this BFD. */
1738
1739 static boolean
1740 som_build_and_write_symbol_table (abfd)
1741 bfd *abfd;
1742 {
1743 unsigned int num_syms = bfd_get_symcount (abfd);
1744 file_ptr symtab_location = obj_som_file_hdr (abfd)->symbol_location;
1745 asymbol **bfd_syms = bfd_get_outsymbols (abfd);
1746 struct symbol_dictionary_record *som_symtab;
1747 int i, symtab_size;
1748
1749 /* Compute total symbol table size and allocate a chunk of memory
1750 to hold the symbol table as we build it. */
1751 symtab_size = num_syms * sizeof (struct symbol_dictionary_record);
1752 som_symtab = (struct symbol_dictionary_record *) alloca (symtab_size);
1753 bzero (som_symtab, symtab_size);
1754
1755 /* Walk over each symbol. */
1756 for (i = 0; i < num_syms; i++)
1757 {
1758 /* This is really an index into the symbol strings table.
1759 By the time we get here, the index has already been
1760 computed and stored into the name field in the BFD symbol. */
1761 som_symtab[i].name.n_strx = (int) bfd_syms[i]->name;
1762
1763 /* The HP SOM linker requires detailed type information about
1764 all symbols (including undefined symbols!). Unfortunately,
1765 the type specified in an import/export statement does not
1766 always match what the linker wants. Severe braindamage. */
1767
1768 /* Section symbols will not have a SOM symbol type assigned to
1769 them yet. Assign all section symbols type ST_DATA. */
1770 if (bfd_syms[i]->flags & BSF_SECTION_SYM)
1771 som_symtab[i].symbol_type = ST_DATA;
1772 else
1773 {
1774 /* Common symbols must have scope SS_UNSAT and type
1775 ST_STORAGE or the linker will choke. */
1776 if (bfd_syms[i]->section == &bfd_com_section)
1777 {
1778 som_symtab[i].symbol_scope = SS_UNSAT;
1779 som_symtab[i].symbol_type = ST_STORAGE;
1780 }
1781
1782 /* It is possible to have a symbol without an associated
1783 type. This happens if the user imported the symbol
1784 without a type and the symbol was never defined
1785 locally. If BSF_FUNCTION is set for this symbol, then
1786 assign it type ST_CODE (the HP linker requires undefined
1787 external functions to have type ST_CODE rather than ST_ENTRY. */
1788 else if (((*som_symbol_data (bfd_syms[i]))->som_type
1789 == SYMBOL_TYPE_UNKNOWN)
1790 && (bfd_syms[i]->section == &bfd_und_section)
1791 && (bfd_syms[i]->flags & BSF_FUNCTION))
1792 som_symtab[i].symbol_type = ST_CODE;
1793
1794 /* Handle function symbols which were defined in this file.
1795 They should have type ST_ENTRY. Also retrieve the argument
1796 relocation bits from the SOM backend information. */
1797 else if (((*som_symbol_data (bfd_syms[i]))->som_type
1798 == SYMBOL_TYPE_ENTRY)
1799 || (((*som_symbol_data (bfd_syms[i]))->som_type
1800 == SYMBOL_TYPE_CODE)
1801 && (bfd_syms[i]->flags & BSF_FUNCTION))
1802 || (((*som_symbol_data (bfd_syms[i]))->som_type
1803 == SYMBOL_TYPE_UNKNOWN)
1804 && (bfd_syms[i]->flags & BSF_FUNCTION)))
1805 {
1806 som_symtab[i].symbol_type = ST_ENTRY;
1807 som_symtab[i].arg_reloc
1808 = (*som_symbol_data (bfd_syms[i]))->tc_data.hppa_arg_reloc;
1809 }
1810
1811 /* If the type is unknown at this point, it should be
1812 ST_DATA (functions were handled as special cases above). */
1813 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1814 == SYMBOL_TYPE_UNKNOWN)
1815 som_symtab[i].symbol_type = ST_DATA;
1816
1817 /* From now on it's a very simple mapping. */
1818 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1819 == SYMBOL_TYPE_ABSOLUTE)
1820 som_symtab[i].symbol_type = ST_ABSOLUTE;
1821 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1822 == SYMBOL_TYPE_CODE)
1823 som_symtab[i].symbol_type = ST_CODE;
1824 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1825 == SYMBOL_TYPE_DATA)
1826 som_symtab[i].symbol_type = ST_DATA;
1827 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1828 == SYMBOL_TYPE_MILLICODE)
1829 som_symtab[i].symbol_type = ST_MILLICODE;
1830 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1831 == SYMBOL_TYPE_PLABEL)
1832 som_symtab[i].symbol_type = ST_PLABEL;
1833 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1834 == SYMBOL_TYPE_PRI_PROG)
1835 som_symtab[i].symbol_type = ST_PRI_PROG;
1836 else if ((*som_symbol_data (bfd_syms[i]))->som_type
1837 == SYMBOL_TYPE_SEC_PROG)
1838 som_symtab[i].symbol_type = ST_SEC_PROG;
1839 }
1840
1841 /* Now handle the symbol's scope. Exported data which is not
1842 in the common section has scope SS_UNIVERSAL. Note scope
1843 of common symbols was handled earlier! */
1844 if (bfd_syms[i]->flags & BSF_EXPORT
1845 && bfd_syms[i]->section != &bfd_com_section)
1846 som_symtab[i].symbol_scope = SS_UNIVERSAL;
1847 /* Any undefined symbol at this point has a scope SS_UNSAT. */
1848 else if (bfd_syms[i]->section == &bfd_und_section)
1849 som_symtab[i].symbol_scope = SS_UNSAT;
1850 /* Anything else which is not in the common section has scope
1851 SS_LOCAL. */
1852 else if (bfd_syms[i]->section != &bfd_com_section)
1853 som_symtab[i].symbol_scope = SS_LOCAL;
1854
1855 /* Now set the symbol_info field. It has no real meaning
1856 for undefined or common symbols, but the HP linker will
1857 choke if it's not set to some "reasonable" value. We
1858 use zero as a reasonable value. */
1859 if (bfd_syms[i]->section == &bfd_com_section
1860 || bfd_syms[i]->section == &bfd_und_section)
1861 som_symtab[i].symbol_info = 0;
1862 /* For all other symbols, the symbol_info field contains the
1863 subspace index of the space this symbol is contained in. */
1864 else
1865 som_symtab[i].symbol_info
1866 = som_section_data (bfd_syms[i]->section)->subspace_index;
1867
1868 /* Set the symbol's value. */
1869 som_symtab[i].symbol_value
1870 = bfd_syms[i]->value + bfd_syms[i]->section->vma;
1871 }
1872
1873 /* Egad. Everything is ready, seek to the right location and
1874 scribble out the symbol table. */
1875 if (bfd_seek (abfd, symtab_location, SEEK_SET) != 0)
1876 {
1877 bfd_error = system_call_error;
1878 return false;
1879 }
1880
1881 if (bfd_write ((PTR) som_symtab, symtab_size, 1, abfd) != symtab_size)
1882 {
1883 bfd_error = system_call_error;
1884 return false;
1885 }
1886 return true;
1887 }
1888
1889 /* Write an object in SOM format. */
1890
1891 static boolean
1892 som_write_object_contents (abfd)
1893 bfd *abfd;
1894 {
1895 if (abfd->output_has_begun == false)
1896 {
1897 /* Set up fixed parts of the file, space, and subspace headers.
1898 Notify the world that output has begun. */
1899 som_prep_headers (abfd);
1900 abfd->output_has_begun = true;
1901 #if 0
1902 /* Not in Cygnus sources yet. */
1903 /* Start writing the object file. This include all the string
1904 tables, fixup streams, and other portions of the object file. */
1905 som_begin_writing (abfd);
1906 #endif
1907 }
1908
1909 /* Now that the symbol table information is complete, build and
1910 write the symbol table. */
1911 if (som_build_and_write_symbol_table (abfd) == false)
1912 return false;
1913
1914 /* Compute the checksum for the file header just before writing
1915 the header to disk. */
1916 obj_som_file_hdr (abfd)->checksum = som_compute_checksum (abfd);
1917 return (som_write_headers (abfd));
1918 }
1919
1920 \f
1921 /* Read and save the string table associated with the given BFD. */
1922
1923 static boolean
1924 som_slurp_string_table (abfd)
1925 bfd *abfd;
1926 {
1927 char *stringtab;
1928
1929 /* Use the saved version if its available. */
1930 if (obj_som_stringtab (abfd) != NULL)
1931 return true;
1932
1933 /* Allocate and read in the string table. */
1934 stringtab = bfd_zalloc (abfd, obj_som_stringtab_size (abfd));
1935 if (stringtab == NULL)
1936 {
1937 bfd_error = no_memory;
1938 return false;
1939 }
1940
1941 if (bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET) < 0)
1942 {
1943 bfd_error = system_call_error;
1944 return false;
1945 }
1946
1947 if (bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd)
1948 != obj_som_stringtab_size (abfd))
1949 {
1950 bfd_error = system_call_error;
1951 return false;
1952 }
1953
1954 /* Save our results and return success. */
1955 obj_som_stringtab (abfd) = stringtab;
1956 return true;
1957 }
1958
1959 /* Return the amount of data (in bytes) required to hold the symbol
1960 table for this object. */
1961
1962 static unsigned int
1963 som_get_symtab_upper_bound (abfd)
1964 bfd *abfd;
1965 {
1966 if (!som_slurp_symbol_table (abfd))
1967 return 0;
1968
1969 return (bfd_get_symcount (abfd) + 1) * (sizeof (som_symbol_type *));
1970 }
1971
1972 /* Convert from a SOM subspace index to a BFD section. */
1973
1974 static asection *
1975 som_section_from_subspace_index (abfd, index)
1976 bfd *abfd;
1977 unsigned int index;
1978 {
1979 asection *section;
1980
1981 for (section = abfd->sections; section != NULL; section = section->next)
1982 if (som_section_data (section)->subspace_index == index)
1983 return section;
1984
1985 /* Should never happen. */
1986 abort();
1987 }
1988
1989 /* Read and save the symbol table associated with the given BFD. */
1990
1991 static unsigned int
1992 som_slurp_symbol_table (abfd)
1993 bfd *abfd;
1994 {
1995 int symbol_count = bfd_get_symcount (abfd);
1996 int symsize = sizeof (struct symbol_dictionary_record);
1997 char *stringtab;
1998 struct symbol_dictionary_record *buf, *bufp, *endbufp;
1999 som_symbol_type *sym, *symbase;
2000
2001 /* Return saved value if it exists. */
2002 if (obj_som_symtab (abfd) != NULL)
2003 return true;
2004
2005 /* Sanity checking. Make sure there are some symbols and that
2006 we can read the string table too. */
2007 if (symbol_count == 0)
2008 {
2009 bfd_error = no_symbols;
2010 return false;
2011 }
2012
2013 if (!som_slurp_string_table (abfd))
2014 return false;
2015
2016 stringtab = obj_som_stringtab (abfd);
2017
2018 symbase = (som_symbol_type *)
2019 bfd_zalloc (abfd, symbol_count * sizeof (som_symbol_type));
2020 if (symbase == NULL)
2021 {
2022 bfd_error = no_memory;
2023 return false;
2024 }
2025
2026 /* Read in the external SOM representation. */
2027 buf = alloca (symbol_count * symsize);
2028 if (buf == NULL)
2029 {
2030 bfd_error = no_memory;
2031 return false;
2032 }
2033 if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) < 0)
2034 {
2035 bfd_error = system_call_error;
2036 return false;
2037 }
2038 if (bfd_read (buf, symbol_count * symsize, 1, abfd)
2039 != symbol_count * symsize)
2040 {
2041 bfd_error = no_symbols;
2042 return (false);
2043 }
2044
2045 /* Iterate over all the symbols and internalize them. */
2046 endbufp = buf + symbol_count;
2047 for (bufp = buf, sym = symbase; bufp < endbufp; ++bufp)
2048 {
2049
2050 /* I don't think we care about these. */
2051 if (bufp->symbol_type == ST_SYM_EXT
2052 || bufp->symbol_type == ST_ARG_EXT)
2053 continue;
2054
2055 /* Some reasonable defaults. */
2056 sym->symbol.the_bfd = abfd;
2057 sym->symbol.name = bufp->name.n_strx + stringtab;
2058 sym->symbol.value = bufp->symbol_value;
2059 sym->symbol.section = 0;
2060 sym->symbol.flags = 0;
2061
2062 switch (bufp->symbol_type)
2063 {
2064 case ST_ENTRY:
2065 sym->symbol.flags |= BSF_FUNCTION;
2066 sym->symbol.value &= ~0x3;
2067 break;
2068
2069 case ST_PRI_PROG:
2070 case ST_SEC_PROG:
2071 case ST_STUB:
2072 case ST_MILLICODE:
2073 case ST_CODE:
2074 sym->symbol.value &= ~0x3;
2075
2076 default:
2077 break;
2078 }
2079
2080 /* Handle scoping and section information. */
2081 switch (bufp->symbol_scope)
2082 {
2083 /* symbol_info field is undefined for SS_EXTERNAL and SS_UNSAT symbols,
2084 so the section associated with this symbol can't be known. */
2085 case SS_EXTERNAL:
2086 case SS_UNSAT:
2087 sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
2088 break;
2089
2090 case SS_UNIVERSAL:
2091 sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
2092 sym->symbol.section
2093 = som_section_from_subspace_index (abfd, bufp->symbol_info);
2094 sym->symbol.value -= sym->symbol.section->vma;
2095 break;
2096
2097 #if 0
2098 /* SS_GLOBAL and SS_LOCAL are two names for the same thing.
2099 Sound dumb? It is. */
2100 case SS_GLOBAL:
2101 #endif
2102 case SS_LOCAL:
2103 sym->symbol.flags |= BSF_LOCAL;
2104 sym->symbol.section
2105 = som_section_from_subspace_index (abfd, bufp->symbol_info);
2106 sym->symbol.value -= sym->symbol.section->vma;
2107 break;
2108 }
2109
2110 /* Mark symbols left around by the debugger. */
2111 if (strlen (sym->symbol.name) >= 3
2112 && sym->symbol.name[0] == 'L'
2113 && (sym->symbol.name[2] == '$' || sym->symbol.name[3] == '$'))
2114 sym->symbol.flags |= BSF_DEBUGGING;
2115
2116 /* Note increment at bottom of loop, since we skip some symbols
2117 we can not include it as part of the for statement. */
2118 sym++;
2119 }
2120
2121 /* Save our results and return success. */
2122 obj_som_symtab (abfd) = symbase;
2123 return (true);
2124 }
2125
2126 /* Canonicalize a SOM symbol table. Return the number of entries
2127 in the symbol table. */
2128
2129 static unsigned int
2130 som_get_symtab (abfd, location)
2131 bfd *abfd;
2132 asymbol **location;
2133 {
2134 int i;
2135 som_symbol_type *symbase;
2136
2137 if (!som_slurp_symbol_table (abfd))
2138 return 0;
2139
2140 i = bfd_get_symcount (abfd);
2141 symbase = obj_som_symtab (abfd);
2142
2143 for (; i > 0; i--, location++, symbase++)
2144 *location = &symbase->symbol;
2145
2146 /* Final null pointer. */
2147 *location = 0;
2148 return (bfd_get_symcount (abfd));
2149 }
2150
2151 /* Make a SOM symbol. There is nothing special to do here. */
2152
2153 static asymbol *
2154 som_make_empty_symbol (abfd)
2155 bfd *abfd;
2156 {
2157 som_symbol_type *new =
2158 (som_symbol_type *) bfd_zalloc (abfd, sizeof (som_symbol_type));
2159 if (new == NULL)
2160 {
2161 bfd_error = no_memory;
2162 return 0;
2163 }
2164 new->symbol.the_bfd = abfd;
2165
2166 return &new->symbol;
2167 }
2168
2169 /* Print symbol information. */
2170
2171 static void
2172 som_print_symbol (ignore_abfd, afile, symbol, how)
2173 bfd *ignore_abfd;
2174 PTR afile;
2175 asymbol *symbol;
2176 bfd_print_symbol_type how;
2177 {
2178 FILE *file = (FILE *) afile;
2179 switch (how)
2180 {
2181 case bfd_print_symbol_name:
2182 fprintf (file, "%s", symbol->name);
2183 break;
2184 case bfd_print_symbol_more:
2185 fprintf (file, "som ");
2186 fprintf_vma (file, symbol->value);
2187 fprintf (file, " %lx", (long) symbol->flags);
2188 break;
2189 case bfd_print_symbol_all:
2190 {
2191 CONST char *section_name;
2192 section_name = symbol->section ? symbol->section->name : "(*none*)";
2193 bfd_print_symbol_vandf ((PTR) file, symbol);
2194 fprintf (file, " %s\t%s", section_name, symbol->name);
2195 break;
2196 }
2197 }
2198 }
2199
2200 static unsigned int
2201 som_get_reloc_upper_bound (abfd, asect)
2202 bfd *abfd;
2203 sec_ptr asect;
2204 {
2205 fprintf (stderr, "som_get_reloc_upper_bound unimplemented\n");
2206 fflush (stderr);
2207 abort ();
2208 return (0);
2209 }
2210
2211 static unsigned int
2212 som_canonicalize_reloc (abfd, section, relptr, symbols)
2213 bfd *abfd;
2214 sec_ptr section;
2215 arelent **relptr;
2216 asymbol **symbols;
2217 {
2218 fprintf (stderr, "som_canonicalize_reloc unimplemented\n");
2219 fflush (stderr);
2220 abort ();
2221 }
2222
2223 extern bfd_target som_vec;
2224
2225 /* A hook to set up object file dependent section information. */
2226
2227 static boolean
2228 som_new_section_hook (abfd, newsect)
2229 bfd *abfd;
2230 asection *newsect;
2231 {
2232 newsect->used_by_bfd = (struct som_section_data_struct *)
2233 bfd_zalloc (abfd, sizeof (struct som_section_data_struct));
2234 newsect->alignment_power = 3;
2235
2236 /* Initialize the subspace_index field to -1 so that it does
2237 not match a subspace with an index of 0. */
2238 som_section_data (newsect)->subspace_index = -1;
2239
2240 /* We allow more than three sections internally */
2241 return true;
2242 }
2243
2244 /* Set backend info for sections which can not be described
2245 in the BFD data structures. */
2246
2247 void
2248 bfd_som_set_section_attributes (section, defined, private, sort_key, spnum)
2249 asection *section;
2250 char defined;
2251 char private;
2252 unsigned char sort_key;
2253 int spnum;
2254 {
2255 struct space_dictionary_record *space_dict;
2256
2257 som_section_data (section)->is_space = 1;
2258 space_dict = &som_section_data (section)->space_dict;
2259 space_dict->is_defined = defined;
2260 space_dict->is_private = private;
2261 space_dict->sort_key = sort_key;
2262 space_dict->space_number = spnum;
2263 }
2264
2265 /* Set backend info for subsections which can not be described
2266 in the BFD data structures. */
2267
2268 void
2269 bfd_som_set_subsection_attributes (section, container, access,
2270 sort_key, quadrant)
2271 asection *section;
2272 asection *container;
2273 int access;
2274 unsigned char sort_key;
2275 int quadrant;
2276 {
2277 struct subspace_dictionary_record *subspace_dict;
2278 som_section_data (section)->is_subspace = 1;
2279 subspace_dict = &som_section_data (section)->subspace_dict;
2280 subspace_dict->access_control_bits = access;
2281 subspace_dict->sort_key = sort_key;
2282 subspace_dict->quadrant = quadrant;
2283 som_section_data (section)->containing_space = container;
2284 }
2285
2286 /* Set the full SOM symbol type. SOM needs far more symbol information
2287 than any other object file format I'm aware of. It is mandatory
2288 to be able to know if a symbol is an entry point, millicode, data,
2289 code, absolute, storage request, or procedure label. If you get
2290 the symbol type wrong your program will not link. */
2291
2292 void
2293 bfd_som_set_symbol_type (symbol, type)
2294 asymbol *symbol;
2295 unsigned int type;
2296 {
2297 (*som_symbol_data (symbol))->som_type = type;
2298 }
2299
2300 /* Attach 64bits of unwind information to a symbol (which hopefully
2301 is a function of some kind!). It would be better to keep this
2302 in the R_ENTRY relocation, but there is not enough space. */
2303
2304 void
2305 bfd_som_attach_unwind_info (symbol, unwind_desc)
2306 asymbol *symbol;
2307 char *unwind_desc;
2308 {
2309 (*som_symbol_data (symbol))->unwind = unwind_desc;
2310 }
2311
2312 static boolean
2313 som_set_section_contents (abfd, section, location, offset, count)
2314 bfd *abfd;
2315 sec_ptr section;
2316 PTR location;
2317 file_ptr offset;
2318 bfd_size_type count;
2319 {
2320 if (abfd->output_has_begun == false)
2321 {
2322 /* Set up fixed parts of the file, space, and subspace headers.
2323 Notify the world that output has begun. */
2324 som_prep_headers (abfd);
2325 abfd->output_has_begun = true;
2326 #if 0
2327 /* Not in Cygnus sources yet. */
2328 /* Start writing the object file. This include all the string
2329 tables, fixup streams, and other portions of the object file. */
2330 som_begin_writing (abfd);
2331 #endif
2332 }
2333
2334 /* Only write subspaces which have "real" contents (eg. the contents
2335 are not generated at run time by the OS). */
2336 if (som_section_data (section)->is_subspace != 1
2337 || ((section->flags & (SEC_LOAD | SEC_DEBUGGING)) == 0))
2338 return true;
2339
2340 /* Seek to the proper offset within the object file and write the
2341 data. */
2342 offset += som_section_data (section)->subspace_dict.file_loc_init_value;
2343 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
2344 {
2345 bfd_error = system_call_error;
2346 return false;
2347 }
2348
2349 if (bfd_write ((PTR) location, 1, count, abfd) != count)
2350 {
2351 bfd_error = system_call_error;
2352 return false;
2353 }
2354 return true;
2355 }
2356
2357 static boolean
2358 som_set_arch_mach (abfd, arch, machine)
2359 bfd *abfd;
2360 enum bfd_architecture arch;
2361 unsigned long machine;
2362 {
2363 /* Allow any architecture to be supported by the SOM backend */
2364 return bfd_default_set_arch_mach (abfd, arch, machine);
2365 }
2366
2367 static boolean
2368 som_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2369 functionname_ptr, line_ptr)
2370 bfd *abfd;
2371 asection *section;
2372 asymbol **symbols;
2373 bfd_vma offset;
2374 CONST char **filename_ptr;
2375 CONST char **functionname_ptr;
2376 unsigned int *line_ptr;
2377 {
2378 fprintf (stderr, "som_find_nearest_line unimplemented\n");
2379 fflush (stderr);
2380 abort ();
2381 return (false);
2382 }
2383
2384 static int
2385 som_sizeof_headers (abfd, reloc)
2386 bfd *abfd;
2387 boolean reloc;
2388 {
2389 fprintf (stderr, "som_sizeof_headers unimplemented\n");
2390 fflush (stderr);
2391 abort ();
2392 return (0);
2393 }
2394
2395 /* Return information about SOM symbol SYMBOL in RET. */
2396
2397 static void
2398 som_get_symbol_info (ignore_abfd, symbol, ret)
2399 bfd *ignore_abfd; /* Ignored. */
2400 asymbol *symbol;
2401 symbol_info *ret;
2402 {
2403 bfd_symbol_info (symbol, ret);
2404 }
2405
2406 /* End of miscellaneous support functions. */
2407
2408 #define som_bfd_debug_info_start bfd_void
2409 #define som_bfd_debug_info_end bfd_void
2410 #define som_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
2411
2412 #define som_openr_next_archived_file bfd_generic_openr_next_archived_file
2413 #define som_generic_stat_arch_elt bfd_generic_stat_arch_elt
2414 #define som_slurp_armap bfd_false
2415 #define som_slurp_extended_name_table _bfd_slurp_extended_name_table
2416 #define som_truncate_arname (void (*)())bfd_nullvoidptr
2417 #define som_write_armap 0
2418
2419 #define som_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
2420 #define som_close_and_cleanup bfd_generic_close_and_cleanup
2421 #define som_get_section_contents bfd_generic_get_section_contents
2422
2423 #define som_bfd_get_relocated_section_contents \
2424 bfd_generic_get_relocated_section_contents
2425 #define som_bfd_relax_section bfd_generic_relax_section
2426 #define som_bfd_seclet_link bfd_generic_seclet_link
2427 #define som_bfd_reloc_type_lookup \
2428 ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
2429 #define som_bfd_make_debug_symbol \
2430 ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
2431
2432 /* Core file support is in the hpux-core backend. */
2433 #define som_core_file_failing_command _bfd_dummy_core_file_failing_command
2434 #define som_core_file_failing_signal _bfd_dummy_core_file_failing_signal
2435 #define som_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
2436
2437 bfd_target som_vec =
2438 {
2439 "som", /* name */
2440 bfd_target_som_flavour,
2441 true, /* target byte order */
2442 true, /* target headers byte order */
2443 (HAS_RELOC | EXEC_P | /* object flags */
2444 HAS_LINENO | HAS_DEBUG |
2445 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2446 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
2447 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2448
2449 /* leading_symbol_char: is the first char of a user symbol
2450 predictable, and if so what is it */
2451 0,
2452 ' ', /* ar_pad_char */
2453 16, /* ar_max_namelen */
2454 3, /* minimum alignment */
2455 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2456 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2457 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2458 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2459 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2460 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2461 {_bfd_dummy_target,
2462 som_object_p, /* bfd_check_format */
2463 bfd_generic_archive_p,
2464 _bfd_dummy_target
2465 },
2466 {
2467 bfd_false,
2468 som_mkobject,
2469 _bfd_generic_mkarchive,
2470 bfd_false
2471 },
2472 {
2473 bfd_false,
2474 som_write_object_contents,
2475 _bfd_write_archive_contents,
2476 bfd_false,
2477 },
2478 #undef som
2479 JUMP_TABLE (som),
2480 (PTR) 0
2481 };
2482
2483 #endif /* HOST_HPPAHPUX || HOST_HPPABSD */
This page took 0.141914 seconds and 4 git commands to generate.