* som.c (som_begin_writing): 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 /* Size (in chars) of the temporary buffers used during fixup and string
71 table writes. */
72
73 #define SOM_TMP_BUFSIZE 8192
74
75
76 /* SOM allows any one of the four previous relocations to be reused
77 with a "R_PREV_FIXUP" relocation entry. Since R_PREV_FIXUP
78 relocations are always a single byte, using a R_PREV_FIXUP instead
79 of some multi-byte relocation makes object files smaller.
80
81 Note one side effect of using a R_PREV_FIXUP is the relocation that
82 is being repeated moves to the front of the queue. */
83 struct reloc_queue
84 {
85 unsigned char *reloc;
86 unsigned int size;
87 } reloc_queue[4];
88
89 /* This fully describes the symbol types which may be attached to
90 an EXPORT or IMPORT directive. Only SOM uses this formation
91 (ELF has no need for it). */
92 typedef enum
93 {
94 SYMBOL_TYPE_UNKNOWN,
95 SYMBOL_TYPE_ABSOLUTE,
96 SYMBOL_TYPE_CODE,
97 SYMBOL_TYPE_DATA,
98 SYMBOL_TYPE_ENTRY,
99 SYMBOL_TYPE_MILLICODE,
100 SYMBOL_TYPE_PLABEL,
101 SYMBOL_TYPE_PRI_PROG,
102 SYMBOL_TYPE_SEC_PROG,
103 } pa_symbol_type;
104
105 /* Forward declarations */
106
107 static boolean som_mkobject PARAMS ((bfd *));
108 static bfd_target * som_object_setup PARAMS ((bfd *,
109 struct header *,
110 struct som_exec_auxhdr *));
111 static asection * make_unique_section PARAMS ((bfd *, CONST char *, int));
112 static boolean setup_sections PARAMS ((bfd *, struct header *));
113 static bfd_target * som_object_p PARAMS ((bfd *));
114 static boolean som_write_object_contents PARAMS ((bfd *));
115 static boolean som_slurp_string_table PARAMS ((bfd *));
116 static unsigned int som_slurp_symbol_table PARAMS ((bfd *));
117 static unsigned int som_get_symtab_upper_bound PARAMS ((bfd *));
118 static unsigned int som_canonicalize_reloc PARAMS ((bfd *, sec_ptr,
119 arelent **, asymbol **));
120 static unsigned int som_get_reloc_upper_bound PARAMS ((bfd *, sec_ptr));
121 static unsigned int som_get_symtab PARAMS ((bfd *, asymbol **));
122 static asymbol * som_make_empty_symbol PARAMS ((bfd *));
123 static void som_print_symbol PARAMS ((bfd *, PTR,
124 asymbol *, bfd_print_symbol_type));
125 static boolean som_new_section_hook PARAMS ((bfd *, asection *));
126 static boolean som_set_section_contents PARAMS ((bfd *, sec_ptr, PTR,
127 file_ptr, bfd_size_type));
128 static boolean som_set_arch_mach PARAMS ((bfd *, enum bfd_architecture,
129 unsigned long));
130 static boolean som_find_nearest_line PARAMS ((bfd *, asection *,
131 asymbol **, bfd_vma,
132 CONST char **,
133 CONST char **,
134 unsigned int *));
135 static void som_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
136 static asection * som_section_from_subspace_index PARAMS ((bfd *,
137 unsigned int));
138 static int log2 PARAMS ((unsigned int));
139 static bfd_reloc_status_type hppa_som_reloc PARAMS ((bfd *, arelent *,
140 asymbol *, PTR,
141 asection *, bfd *));
142 static void som_initialize_reloc_queue PARAMS ((struct reloc_queue *));
143 static void som_reloc_queue_insert PARAMS ((unsigned char *, unsigned int,
144 struct reloc_queue *));
145 static void som_reloc_queue_fix PARAMS ((struct reloc_queue *, unsigned int));
146 static int som_reloc_queue_find PARAMS ((unsigned char *, unsigned int,
147 struct reloc_queue *));
148 static unsigned char * try_prev_fixup PARAMS ((bfd *, int *, unsigned char *,
149 unsigned int,
150 struct reloc_queue *));
151
152 static unsigned char * som_reloc_skip PARAMS ((bfd *, unsigned int,
153 unsigned char *, unsigned int *,
154 struct reloc_queue *));
155 static unsigned char * som_reloc_addend PARAMS ((bfd *, int, unsigned char *,
156 unsigned int *,
157 struct reloc_queue *));
158 static unsigned char * som_reloc_call PARAMS ((bfd *, unsigned char *,
159 unsigned int *,
160 arelent *, int,
161 struct reloc_queue *));
162 static unsigned long som_count_spaces PARAMS ((bfd *));
163 static unsigned long som_count_subspaces PARAMS ((bfd *));
164 static int compare_syms PARAMS ((asymbol **, asymbol **));
165 static unsigned long som_compute_checksum PARAMS ((bfd *));
166 static boolean som_prep_headers PARAMS ((bfd *));
167 static int som_sizeof_headers PARAMS ((bfd *, boolean));
168 static boolean som_write_headers PARAMS ((bfd *));
169 static boolean som_build_and_write_symbol_table PARAMS ((bfd *));
170 static void som_prep_for_fixups PARAMS ((bfd *, asymbol **, unsigned long));
171 static boolean som_write_fixups PARAMS ((bfd *, unsigned long, unsigned int *));
172 static boolean som_write_space_strings PARAMS ((bfd *, unsigned long,
173 unsigned int *));
174 static boolean som_write_symbol_strings PARAMS ((bfd *, unsigned long,
175 asymbol **, unsigned int,
176 unsigned *));
177 static boolean som_begin_writing PARAMS ((bfd *));
178
179 static reloc_howto_type som_hppa_howto_table[] =
180 {
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_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
202 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
203 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
204 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
205 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
206 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
207 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
208 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
209 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
210 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
211 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
212 {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
213 {R_ZEROES, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ZEROES"},
214 {R_ZEROES, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ZEROES"},
215 {R_UNINIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_UNINIT"},
216 {R_UNINIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_UNINIT"},
217 {R_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RELOCATION"},
218 {R_DATA_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_ONE_SYMBOL"},
219 {R_DATA_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_ONE_SYMBOL"},
220 {R_DATA_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_PLABEL"},
221 {R_DATA_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_PLABEL"},
222 {R_SPACE_REF, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_SPACE_REF"},
223 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
224 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
225 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
226 {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
227 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
228 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
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_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
232 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
233 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
234 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
235 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
236 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
237 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
238 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
239 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
240 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
241 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
242 {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
243 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
244 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
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_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
248 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
249 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
250 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
251 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
252 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
253 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
254 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
255 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
256 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
257 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
258 {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
259 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
260 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
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_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
285 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
286 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
287 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
288 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
289 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
290 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
291 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
292 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
293 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
294 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
295 {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
296 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
297 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
298 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
299 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
300 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
301 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
302 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
303 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
304 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
305 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
306 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
307 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
308 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
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_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
333 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
334 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
335 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
336 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
337 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
338 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
339 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
340 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
341 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
342 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
343 {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
344 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
345 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
346 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
347 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
348 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
349 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
350 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
351 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
352 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
353 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
354 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
355 {R_MILLI_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_MILLI_REL"},
356 {R_MILLI_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_MILLI_REL"},
357 {R_CODE_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_PLABEL"},
358 {R_CODE_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_PLABEL"},
359 {R_BREAKPOINT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BREAKPOINT"},
360 {R_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ENTRY"},
361 {R_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ENTRY"},
362 {R_ALT_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ALT_ENTRY"},
363 {R_EXIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_EXIT"},
364 {R_BEGIN_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BEGIN_TRY"},
365 {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
366 {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
367 {R_BEGIN_BRTAB, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BEGIN_BRTAB"},
368 {R_END_BRTAB, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_BRTAB"},
369 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
370 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
371 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
372 {R_DATA_EXPR, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_EXPR"},
373 {R_CODE_EXPR, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_EXPR"},
374 {R_FSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_FSEL"},
375 {R_LSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_LSEL"},
376 {R_RSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RSEL"},
377 {R_N_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_N_MODE"},
378 {R_S_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_S_MODE"},
379 {R_D_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_D_MODE"},
380 {R_R_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_R_MODE"},
381 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
382 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
383 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
384 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
385 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
386 {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
387 {R_TRANSLATED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_TRANSLATED"},
388 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
389 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
390 {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
391 {R_COMP1, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP1"},
392 {R_COMP2, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP2"},
393 {R_COMP3, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP3"},
394 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
395 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
396 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
397 {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
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 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
428 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
429 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
430 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
431 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
432 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
433 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
434 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
435 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
436 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
437 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
438 {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"}};
439
440
441 /* Initialize the SOM relocation queue. By definition the queue holds
442 the last four multibyte fixups. */
443
444 static void
445 som_initialize_reloc_queue (queue)
446 struct reloc_queue *queue;
447 {
448 queue[0].reloc = NULL;
449 queue[0].size = 0;
450 queue[1].reloc = NULL;
451 queue[1].size = 0;
452 queue[2].reloc = NULL;
453 queue[2].size = 0;
454 queue[3].reloc = NULL;
455 queue[3].size = 0;
456 }
457
458 /* Insert a new relocation into the relocation queue. */
459
460 static void
461 som_reloc_queue_insert (p, size, queue)
462 unsigned char *p;
463 unsigned int size;
464 struct reloc_queue *queue;
465 {
466 queue[3].reloc = queue[2].reloc;
467 queue[3].size = queue[2].size;
468 queue[2].reloc = queue[1].reloc;
469 queue[2].size = queue[1].size;
470 queue[1].reloc = queue[0].reloc;
471 queue[1].size = queue[0].size;
472 queue[0].reloc = p;
473 queue[0].size = size;
474 }
475
476 /* When an entry in the relocation queue is reused, the entry moves
477 to the front of the queue. */
478
479 static void
480 som_reloc_queue_fix (queue, index)
481 struct reloc_queue *queue;
482 unsigned int index;
483 {
484 if (index == 0)
485 return;
486
487 if (index == 1)
488 {
489 unsigned char *tmp1 = queue[0].reloc;
490 unsigned int tmp2 = queue[0].size;
491 queue[0].reloc = queue[1].reloc;
492 queue[0].size = queue[1].size;
493 queue[1].reloc = tmp1;
494 queue[1].size = tmp2;
495 return;
496 }
497
498 if (index == 2)
499 {
500 unsigned char *tmp1 = queue[0].reloc;
501 unsigned int tmp2 = queue[0].size;
502 queue[0].reloc = queue[2].reloc;
503 queue[0].size = queue[2].size;
504 queue[2].reloc = queue[1].reloc;
505 queue[2].size = queue[1].size;
506 queue[1].reloc = tmp1;
507 queue[1].size = tmp2;
508 return;
509 }
510
511 if (index == 3)
512 {
513 unsigned char *tmp1 = queue[0].reloc;
514 unsigned int tmp2 = queue[0].size;
515 queue[0].reloc = queue[3].reloc;
516 queue[0].size = queue[3].size;
517 queue[3].reloc = queue[2].reloc;
518 queue[3].size = queue[2].size;
519 queue[2].reloc = queue[1].reloc;
520 queue[2].size = queue[1].size;
521 queue[1].reloc = tmp1;
522 queue[1].size = tmp2;
523 return;
524 }
525 abort();
526 }
527
528 /* Search for a particular relocation in the relocation queue. */
529
530 static int
531 som_reloc_queue_find (p, size, queue)
532 unsigned char *p;
533 unsigned int size;
534 struct reloc_queue *queue;
535 {
536 if (!bcmp (p, queue[0].reloc, size)
537 && size == queue[0].size)
538 return 0;
539 if (!bcmp (p, queue[1].reloc, size)
540 && size == queue[1].size)
541 return 1;
542 if (!bcmp (p, queue[2].reloc, size)
543 && size == queue[2].size)
544 return 2;
545 if (!bcmp (p, queue[3].reloc, size)
546 && size == queue[3].size)
547 return 3;
548 return -1;
549 }
550
551 static unsigned char *
552 try_prev_fixup (abfd, subspace_reloc_sizep, p, size, queue)
553 bfd *abfd;
554 int *subspace_reloc_sizep;
555 unsigned char *p;
556 unsigned int size;
557 struct reloc_queue *queue;
558 {
559 int queue_index = som_reloc_queue_find (p, size, queue);
560
561 if (queue_index != -1)
562 {
563 /* Found this in a previous fixup. Undo the fixup we
564 just built and use R_PREV_FIXUP instead. We saved
565 a total of size - 1 bytes in the fixup stream. */
566 bfd_put_8 (abfd, R_PREV_FIXUP + queue_index, p);
567 p += 1;
568 *subspace_reloc_sizep += 1;
569 som_reloc_queue_fix (queue, queue_index);
570 }
571 else
572 {
573 som_reloc_queue_insert (p, size, queue);
574 *subspace_reloc_sizep += size;
575 p += size;
576 }
577 return p;
578 }
579
580 /* Emit the proper R_NO_RELOCATION fixups to map the next SKIP
581 bytes without any relocation. Update the size of the subspace
582 relocation stream via SUBSPACE_RELOC_SIZE_P; also return the
583 current pointer into the relocation stream. */
584
585 static unsigned char *
586 som_reloc_skip (abfd, skip, p, subspace_reloc_sizep, queue)
587 bfd *abfd;
588 unsigned int skip;
589 unsigned char *p;
590 unsigned int *subspace_reloc_sizep;
591 struct reloc_queue *queue;
592 {
593 /* Use a 4 byte R_NO_RELOCATION entry with a maximal value
594 then R_PREV_FIXUPs to get the difference down to a
595 reasonable size. */
596 if (skip >= 0x1000000)
597 {
598 skip -= 0x1000000;
599 bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
600 bfd_put_8 (abfd, 0xff, p + 1);
601 bfd_put_16 (abfd, 0xffff, p + 2);
602 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
603 while (skip >= 0x1000000)
604 {
605 skip -= 0x1000000;
606 bfd_put_8 (abfd, R_PREV_FIXUP, p);
607 p++;
608 *subspace_reloc_sizep += 1;
609 /* No need to adjust queue here since we are repeating the
610 most recent fixup. */
611 }
612 }
613
614 /* The difference must be less than 0x1000000. Use one
615 more R_NO_RELOCATION entry to get to the right difference. */
616 if ((skip & 3) == 0 && skip <= 0xc0000 && skip > 0)
617 {
618 /* Difference can be handled in a simple single-byte
619 R_NO_RELOCATION entry. */
620 if (skip <= 0x60)
621 {
622 bfd_put_8 (abfd, R_NO_RELOCATION + (skip >> 2) - 1, p);
623 *subspace_reloc_sizep += 1;
624 p++;
625 }
626 /* Handle it with a two byte R_NO_RELOCATION entry. */
627 else if (skip <= 0x1000)
628 {
629 bfd_put_8 (abfd, R_NO_RELOCATION + 24 + (((skip >> 2) - 1) >> 8), p);
630 bfd_put_8 (abfd, (skip >> 2) - 1, p + 1);
631 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
632 }
633 /* Handle it with a three byte R_NO_RELOCATION entry. */
634 else
635 {
636 bfd_put_8 (abfd, R_NO_RELOCATION + 28 + (((skip >> 2) - 1) >> 16), p);
637 bfd_put_16 (abfd, (skip >> 2) - 1, p + 1);
638 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
639 }
640 }
641 /* Ugh. Punt and use a 4 byte entry. */
642 else if (skip > 0)
643 {
644 bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
645 bfd_put_8 (abfd, skip >> 16, p + 1);
646 bfd_put_16 (abfd, skip, p + 2);
647 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
648 }
649 return p;
650 }
651
652 /* Emit the proper R_DATA_OVERRIDE fixups to handle a nonzero addend
653 from a BFD relocation. Update the size of the subspace relocation
654 stream via SUBSPACE_RELOC_SIZE_P; also return the current pointer
655 into the relocation stream. */
656
657 static unsigned char *
658 som_reloc_addend (abfd, addend, p, subspace_reloc_sizep, queue)
659 bfd *abfd;
660 int addend;
661 unsigned char *p;
662 unsigned int *subspace_reloc_sizep;
663 struct reloc_queue *queue;
664 {
665 if ((unsigned)(addend) + 0x80 < 0x100)
666 {
667 bfd_put_8 (abfd, R_DATA_OVERRIDE + 1, p);
668 bfd_put_8 (abfd, addend, p + 1);
669 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
670 }
671 else if ((unsigned) (addend) + 0x8000 < 0x10000)
672 {
673 bfd_put_8 (abfd, R_DATA_OVERRIDE + 2, p);
674 bfd_put_16 (abfd, addend, p + 1);
675 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
676 }
677 else if ((unsigned) (addend) + 0x800000 < 0x1000000)
678 {
679 bfd_put_8 (abfd, R_DATA_OVERRIDE + 3, p);
680 bfd_put_8 (abfd, addend >> 16, p + 1);
681 bfd_put_16 (abfd, addend, p + 2);
682 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
683 }
684 else
685 {
686 bfd_put_8 (abfd, R_DATA_OVERRIDE + 4, p);
687 bfd_put_32 (abfd, addend, p + 1);
688 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
689 }
690 return p;
691 }
692
693 /* Handle a single function call relocation. */
694
695 static unsigned char *
696 som_reloc_call (abfd, p, subspace_reloc_sizep, bfd_reloc, sym_num, queue)
697 bfd *abfd;
698 unsigned char *p;
699 unsigned int *subspace_reloc_sizep;
700 arelent *bfd_reloc;
701 int sym_num;
702 struct reloc_queue *queue;
703 {
704 int arg_bits = HPPA_R_ARG_RELOC (bfd_reloc->addend);
705 int rtn_bits = arg_bits & 0x3;
706 int type, done = 0;
707
708 /* You'll never believe all this is necessary to handle relocations
709 for function calls. Having to compute and pack the argument
710 relocation bits is the real nightmare.
711
712 If you're interested in how this works, just forget it. You really
713 do not want to know about this braindamage. */
714
715 /* First see if this can be done with a "simple" relocation. Simple
716 relocations have a symbol number < 0x100 and have simple encodings
717 of argument relocations. */
718
719 if (sym_num < 0x100)
720 {
721 switch (arg_bits)
722 {
723 case 0:
724 case 1:
725 type = 0;
726 break;
727 case 1 << 8:
728 case 1 << 8 | 1:
729 type = 1;
730 break;
731 case 1 << 8 | 1 << 6:
732 case 1 << 8 | 1 << 6 | 1:
733 type = 2;
734 break;
735 case 1 << 8 | 1 << 6 | 1 << 4:
736 case 1 << 8 | 1 << 6 | 1 << 4 | 1:
737 type = 3;
738 break;
739 case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2:
740 case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2 | 1:
741 type = 4;
742 break;
743 default:
744 /* Not one of the easy encodings. This will have to be
745 handled by the more complex code below. */
746 type = -1;
747 break;
748 }
749 if (type != -1)
750 {
751 /* Account for the return value too. */
752 if (rtn_bits)
753 type += 5;
754
755 /* Emit a 2 byte relocation. Then see if it can be handled
756 with a relocation which is already in the relocation queue. */
757 bfd_put_8 (abfd, bfd_reloc->howto->type + type, p);
758 bfd_put_8 (abfd, sym_num, p + 1);
759 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
760 done = 1;
761 }
762 }
763
764 /* If this could not be handled with a simple relocation, then do a hard
765 one. Hard relocations occur if the symbol number was too high or if
766 the encoding of argument relocation bits is too complex. */
767 if (! done)
768 {
769 /* Don't ask about these magic sequences. I took them straight
770 from gas-1.36 which took them from the a.out man page. */
771 type = rtn_bits;
772 if ((arg_bits >> 6 & 0xf) == 0xe)
773 type += 9 * 40;
774 else
775 type += (3 * (arg_bits >> 8 & 3) + (arg_bits >> 6 & 3)) * 40;
776 if ((arg_bits >> 2 & 0xf) == 0xe)
777 type += 9 * 4;
778 else
779 type += (3 * (arg_bits >> 4 & 3) + (arg_bits >> 2 & 3)) * 4;
780
781 /* Output the first two bytes of the relocation. These describe
782 the length of the relocation and encoding style. */
783 bfd_put_8 (abfd, bfd_reloc->howto->type + 10
784 + 2 * (sym_num >= 0x100) + (type >= 0x100),
785 p);
786 bfd_put_8 (abfd, type, p + 1);
787
788 /* Now output the symbol index and see if this bizarre relocation
789 just happened to be in the relocation queue. */
790 if (sym_num < 0x100)
791 {
792 bfd_put_8 (abfd, sym_num, p + 2);
793 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
794 }
795 else
796 {
797 bfd_put_8 (abfd, sym_num >> 16, p + 2);
798 bfd_put_16 (abfd, sym_num, p + 3);
799 p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
800 }
801 }
802 return p;
803 }
804
805
806 /* Return the logarithm of X, base 2, considering X unsigned.
807 Abort if X is not a power of two -- this should never happen (FIXME:
808 It will happen on corrupt executables. GDB should give an error, not
809 a coredump, in that case). */
810
811 static int
812 log2 (x)
813 unsigned int x;
814 {
815 int log = 0;
816
817 /* Test for 0 or a power of 2. */
818 if (x == 0 || x != (x & -x))
819 abort();
820
821 while ((x >>= 1) != 0)
822 log++;
823 return log;
824 }
825
826 static bfd_reloc_status_type
827 hppa_som_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd)
828 bfd *abfd;
829 arelent *reloc_entry;
830 asymbol *symbol_in;
831 PTR data;
832 asection *input_section;
833 bfd *output_bfd;
834 {
835 if (output_bfd)
836 {
837 reloc_entry->address += input_section->output_offset;
838 return bfd_reloc_ok;
839 }
840 return bfd_reloc_ok;
841 }
842
843 /* Given a generic HPPA relocation type, the instruction format,
844 and a field selector, return an appropriate SOM reloation.
845
846 FIXME. Need to handle %RR, %LR and the like as field selectors.
847 These will need to generate multiple SOM relocations. */
848
849 int **
850 hppa_som_gen_reloc_type (abfd, base_type, format, field)
851 bfd *abfd;
852 int base_type;
853 int format;
854 int field;
855 {
856 int *final_type, **final_types;
857
858 final_types = (int **) bfd_alloc_by_size_t (abfd, sizeof (int *) * 2);
859 final_type = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
860
861
862 final_types[0] = final_type;
863 final_types[1] = NULL;
864
865 /* Default to the basic relocation passed in. */
866 *final_type = base_type;
867
868 switch (base_type)
869 {
870 case R_HPPA:
871 /* PLABELs get their own relocation type. */
872 if (field == e_psel
873 || field == e_lpsel
874 || field == e_rpsel)
875 {
876 /* A PLABEL relocation that has a size of 32 bits must
877 be a R_DATA_PLABEL. All others are R_CODE_PLABELs. */
878 if (format == 32)
879 *final_type = R_DATA_PLABEL;
880 else
881 *final_type = R_CODE_PLABEL;
882 }
883 /* A relocatoin in the data space is always a full 32bits. */
884 else if (format == 32)
885 *final_type = R_DATA_ONE_SYMBOL;
886
887 break;
888
889 case R_HPPA_GOTOFF:
890 /* More PLABEL special cases. */
891 if (field == e_psel
892 || field == e_lpsel
893 || field == e_rpsel)
894 *final_type = R_DATA_PLABEL;
895 break;
896
897 case R_HPPA_NONE:
898 case R_HPPA_ABS_CALL:
899 case R_HPPA_PCREL_CALL:
900 case R_HPPA_COMPLEX:
901 case R_HPPA_COMPLEX_PCREL_CALL:
902 case R_HPPA_COMPLEX_ABS_CALL:
903 /* Right now we can default all these. */
904 break;
905 }
906 return final_types;
907 }
908
909 /* Return the address of the correct entry in the PA SOM relocation
910 howto table. */
911
912 static reloc_howto_type *
913 som_bfd_reloc_type_lookup (arch, code)
914 bfd_arch_info_type *arch;
915 bfd_reloc_code_real_type code;
916 {
917 if ((int) code < (int) R_NO_RELOCATION + 255)
918 {
919 BFD_ASSERT ((int) som_hppa_howto_table[(int) code].type == (int) code);
920 return &som_hppa_howto_table[(int) code];
921 }
922
923 return (reloc_howto_type *) 0;
924 }
925
926 /* Perform some initialization for an object. Save results of this
927 initialization in the BFD. */
928
929 static bfd_target *
930 som_object_setup (abfd, file_hdrp, aux_hdrp)
931 bfd *abfd;
932 struct header *file_hdrp;
933 struct som_exec_auxhdr *aux_hdrp;
934 {
935 asection *text, *data, *bss;
936
937 /* som_mkobject will set bfd_error if som_mkobject fails. */
938 if (som_mkobject (abfd) != true)
939 return 0;
940
941 /* Make the standard .text, .data, and .bss sections so that tools
942 which assume those names work (size for example). They will have
943 no contents, but the sizes and such will reflect those of the
944 $CODE$, $DATA$, and $BSS$ subspaces respectively.
945
946 FIXME: Should check return status from bfd_make_section calls below. */
947
948 text = bfd_make_section (abfd, ".text");
949 data = bfd_make_section (abfd, ".data");
950 bss = bfd_make_section (abfd, ".bss");
951
952 text->_raw_size = aux_hdrp->exec_tsize;
953 data->_raw_size = aux_hdrp->exec_dsize;
954 bss->_raw_size = aux_hdrp->exec_bsize;
955
956 text->flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_CODE);
957 data->flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS);
958 bss->flags = (SEC_ALLOC | SEC_IS_COMMON);
959
960 /* The virtual memory addresses of the sections */
961 text->vma = aux_hdrp->exec_tmem;
962 data->vma = aux_hdrp->exec_dmem;
963 bss->vma = aux_hdrp->exec_bfill;
964
965 /* The file offsets of the sections */
966 text->filepos = aux_hdrp->exec_tfile;
967 data->filepos = aux_hdrp->exec_dfile;
968
969 /* The file offsets of the relocation info */
970 text->rel_filepos = 0;
971 data->rel_filepos = 0;
972
973 /* Set BFD flags based on what information is available in the SOM. */
974 abfd->flags = NO_FLAGS;
975 if (! file_hdrp->entry_offset)
976 abfd->flags |= HAS_RELOC;
977 else
978 abfd->flags |= EXEC_P;
979 if (file_hdrp->symbol_total)
980 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
981
982 bfd_get_start_address (abfd) = aux_hdrp->exec_entry;
983 bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 0);
984 bfd_get_symcount (abfd) = file_hdrp->symbol_total;
985
986 /* Initialize the saved symbol table and string table to NULL.
987 Save important offsets and sizes from the SOM header into
988 the BFD. */
989 obj_som_stringtab (abfd) = (char *) NULL;
990 obj_som_symtab (abfd) = (som_symbol_type *) NULL;
991 obj_som_stringtab_size (abfd) = file_hdrp->symbol_strings_size;
992 obj_som_sym_filepos (abfd) = file_hdrp->symbol_location;
993 obj_som_str_filepos (abfd) = file_hdrp->symbol_strings_location;
994 obj_som_reloc_filepos (abfd) = file_hdrp->fixup_request_location;
995
996 return abfd->xvec;
997 }
998
999 /* Create a new BFD section for NAME. If NAME already exists, then create a
1000 new unique name, with NAME as the prefix. This exists because SOM .o files
1001 may have more than one $CODE$ subspace. */
1002
1003 static asection *
1004 make_unique_section (abfd, name, num)
1005 bfd *abfd;
1006 CONST char *name;
1007 int num;
1008 {
1009 asection *sect;
1010 char *newname;
1011 char altname[100];
1012
1013 sect = bfd_make_section (abfd, name);
1014 while (!sect)
1015 {
1016 sprintf (altname, "%s-%d", name, num++);
1017 sect = bfd_make_section (abfd, altname);
1018 }
1019
1020 newname = bfd_alloc (abfd, strlen (sect->name) + 1);
1021 strcpy (newname, sect->name);
1022
1023 sect->name = newname;
1024 return sect;
1025 }
1026
1027 /* Convert all of the space and subspace info into BFD sections. Each space
1028 contains a number of subspaces, which in turn describe the mapping between
1029 regions of the exec file, and the address space that the program runs in.
1030 BFD sections which correspond to spaces will overlap the sections for the
1031 associated subspaces. */
1032
1033 static boolean
1034 setup_sections (abfd, file_hdr)
1035 bfd *abfd;
1036 struct header *file_hdr;
1037 {
1038 char *space_strings;
1039 int space_index;
1040 unsigned int total_subspaces = 0;
1041
1042 /* First, read in space names */
1043
1044 space_strings = alloca (file_hdr->space_strings_size);
1045 if (!space_strings)
1046 return false;
1047
1048 if (bfd_seek (abfd, file_hdr->space_strings_location, SEEK_SET) < 0)
1049 return false;
1050 if (bfd_read (space_strings, 1, file_hdr->space_strings_size, abfd)
1051 != file_hdr->space_strings_size)
1052 return false;
1053
1054 /* Loop over all of the space dictionaries, building up sections */
1055 for (space_index = 0; space_index < file_hdr->space_total; space_index++)
1056 {
1057 struct space_dictionary_record space;
1058 struct subspace_dictionary_record subspace, save_subspace;
1059 int subspace_index;
1060 asection *space_asect;
1061
1062 /* Read the space dictionary element */
1063 if (bfd_seek (abfd, file_hdr->space_location
1064 + space_index * sizeof space, SEEK_SET) < 0)
1065 return false;
1066 if (bfd_read (&space, 1, sizeof space, abfd) != sizeof space)
1067 return false;
1068
1069 /* Setup the space name string */
1070 space.name.n_name = space.name.n_strx + space_strings;
1071
1072 /* Make a section out of it */
1073 space_asect = make_unique_section (abfd, space.name.n_name, space_index);
1074 if (!space_asect)
1075 return false;
1076
1077 /* Now, read in the first subspace for this space */
1078 if (bfd_seek (abfd, file_hdr->subspace_location
1079 + space.subspace_index * sizeof subspace,
1080 SEEK_SET) < 0)
1081 return false;
1082 if (bfd_read (&subspace, 1, sizeof subspace, abfd) != sizeof subspace)
1083 return false;
1084 /* Seek back to the start of the subspaces for loop below */
1085 if (bfd_seek (abfd, file_hdr->subspace_location
1086 + space.subspace_index * sizeof subspace,
1087 SEEK_SET) < 0)
1088 return false;
1089
1090 /* Setup the start address and file loc from the first subspace record */
1091 space_asect->vma = subspace.subspace_start;
1092 space_asect->filepos = subspace.file_loc_init_value;
1093 space_asect->alignment_power = log2 (subspace.alignment);
1094
1095 /* Initialize save_subspace so we can reliably determine if this
1096 loop placed any useful values into it. */
1097 bzero (&save_subspace, sizeof (struct subspace_dictionary_record));
1098
1099 /* Loop over the rest of the subspaces, building up more sections */
1100 for (subspace_index = 0; subspace_index < space.subspace_quantity;
1101 subspace_index++)
1102 {
1103 asection *subspace_asect;
1104
1105 /* Read in the next subspace */
1106 if (bfd_read (&subspace, 1, sizeof subspace, abfd)
1107 != sizeof subspace)
1108 return false;
1109
1110 /* Setup the subspace name string */
1111 subspace.name.n_name = subspace.name.n_strx + space_strings;
1112
1113 /* Make a section out of this subspace */
1114 subspace_asect = make_unique_section (abfd, subspace.name.n_name,
1115 space.subspace_index + subspace_index);
1116
1117 if (!subspace_asect)
1118 return false;
1119
1120 /* Keep an easy mapping between subspaces and sections. */
1121 som_section_data (subspace_asect)->subspace_index
1122 = total_subspaces++;
1123
1124 /* Set SEC_READONLY and SEC_CODE/SEC_DATA as specified
1125 by the access_control_bits in the subspace header. */
1126 switch (subspace.access_control_bits >> 4)
1127 {
1128 /* Readonly data. */
1129 case 0x0:
1130 subspace_asect->flags |= SEC_DATA | SEC_READONLY;
1131 break;
1132
1133 /* Normal data. */
1134 case 0x1:
1135 subspace_asect->flags |= SEC_DATA;
1136 break;
1137
1138 /* Readonly code and the gateways.
1139 Gateways have other attributes which do not map
1140 into anything BFD knows about. */
1141 case 0x2:
1142 case 0x4:
1143 case 0x5:
1144 case 0x6:
1145 case 0x7:
1146 subspace_asect->flags |= SEC_CODE | SEC_READONLY;
1147 break;
1148
1149 /* dynamic (writable) code. */
1150 case 0x3:
1151 subspace_asect->flags |= SEC_CODE;
1152 break;
1153 }
1154
1155 if (subspace.dup_common || subspace.is_common)
1156 subspace_asect->flags |= SEC_IS_COMMON;
1157 else
1158 subspace_asect->flags |= SEC_HAS_CONTENTS;
1159 if (subspace.is_loadable)
1160 subspace_asect->flags |= SEC_ALLOC | SEC_LOAD;
1161 if (subspace.code_only)
1162 subspace_asect->flags |= SEC_CODE;
1163
1164 /* This subspace has relocations.
1165 The fixup_request_quantity is a byte count for the number of
1166 entries in the relocation stream; it is not the actual number
1167 of relocations in the subspace. */
1168 if (subspace.fixup_request_quantity != 0)
1169 {
1170 subspace_asect->flags |= SEC_RELOC;
1171 subspace_asect->rel_filepos = subspace.fixup_request_index;
1172 som_section_data (subspace_asect)->reloc_size
1173 = subspace.fixup_request_quantity;
1174 /* We can not determine this yet. When we read in the
1175 relocation table the correct value will be filled in. */
1176 subspace_asect->reloc_count = -1;
1177 }
1178
1179 /* Update save_subspace if appropriate. */
1180 if (subspace.file_loc_init_value > save_subspace.file_loc_init_value)
1181 save_subspace = subspace;
1182
1183 subspace_asect->vma = subspace.subspace_start;
1184 subspace_asect->_cooked_size = subspace.subspace_length;
1185 subspace_asect->_raw_size = subspace.initialization_length;
1186 subspace_asect->alignment_power = log2 (subspace.alignment);
1187 subspace_asect->filepos = subspace.file_loc_init_value;
1188 }
1189
1190 /* Yow! there is no subspace within the space which actually
1191 has initialized information in it; this should never happen
1192 as far as I know. */
1193 if (!save_subspace.file_loc_init_value)
1194 abort ();
1195
1196 /* Setup the sizes for the space section based upon the info in the
1197 last subspace of the space. */
1198 space_asect->_cooked_size = save_subspace.subspace_start
1199 - space_asect->vma + save_subspace.subspace_length;
1200 space_asect->_raw_size = save_subspace.file_loc_init_value
1201 - space_asect->filepos + save_subspace.initialization_length;
1202 }
1203 return true;
1204 }
1205
1206 /* Read in a SOM object and make it into a BFD. */
1207
1208 static bfd_target *
1209 som_object_p (abfd)
1210 bfd *abfd;
1211 {
1212 struct header file_hdr;
1213 struct som_exec_auxhdr aux_hdr;
1214
1215 if (bfd_read ((PTR) & file_hdr, 1, FILE_HDR_SIZE, abfd) != FILE_HDR_SIZE)
1216 {
1217 bfd_error = system_call_error;
1218 return 0;
1219 }
1220
1221 if (!_PA_RISC_ID (file_hdr.system_id))
1222 {
1223 bfd_error = wrong_format;
1224 return 0;
1225 }
1226
1227 switch (file_hdr.a_magic)
1228 {
1229 case RELOC_MAGIC:
1230 case EXEC_MAGIC:
1231 case SHARE_MAGIC:
1232 case DEMAND_MAGIC:
1233 #ifdef DL_MAGIC
1234 case DL_MAGIC:
1235 #endif
1236 #ifdef SHL_MAGIC
1237 case SHL_MAGIC:
1238 #endif
1239 #ifdef EXECLIBMAGIC
1240 case EXECLIBMAGIC:
1241 #endif
1242 break;
1243 default:
1244 bfd_error = wrong_format;
1245 return 0;
1246 }
1247
1248 if (file_hdr.version_id != VERSION_ID
1249 && file_hdr.version_id != NEW_VERSION_ID)
1250 {
1251 bfd_error = wrong_format;
1252 return 0;
1253 }
1254
1255 /* If the aux_header_size field in the file header is zero, then this
1256 object is an incomplete executable (a .o file). Do not try to read
1257 a non-existant auxiliary header. */
1258 bzero (&aux_hdr, sizeof (struct som_exec_auxhdr));
1259 if (file_hdr.aux_header_size != 0)
1260 {
1261 if (bfd_read ((PTR) & aux_hdr, 1, AUX_HDR_SIZE, abfd) != AUX_HDR_SIZE)
1262 {
1263 bfd_error = wrong_format;
1264 return 0;
1265 }
1266 }
1267
1268 if (!setup_sections (abfd, &file_hdr))
1269 {
1270 /* setup_sections does not bubble up a bfd error code. */
1271 bfd_error = bad_value;
1272 return 0;
1273 }
1274
1275 /* This appears to be a valid SOM object. Do some initialization. */
1276 return som_object_setup (abfd, &file_hdr, &aux_hdr);
1277 }
1278
1279 /* Create a SOM object. */
1280
1281 static boolean
1282 som_mkobject (abfd)
1283 bfd *abfd;
1284 {
1285 /* Allocate memory to hold backend information. */
1286 abfd->tdata.som_data = (struct som_data_struct *)
1287 bfd_zalloc (abfd, sizeof (struct som_data_struct));
1288 if (abfd->tdata.som_data == NULL)
1289 {
1290 bfd_error = no_memory;
1291 return false;
1292 }
1293 obj_som_file_hdr (abfd) = bfd_zalloc (abfd, sizeof (struct header));
1294 if (obj_som_file_hdr (abfd) == NULL)
1295
1296 {
1297 bfd_error = no_memory;
1298 return false;
1299 }
1300 return true;
1301 }
1302
1303 /* Initialize some information in the file header. This routine makes
1304 not attempt at doing the right thing for a full executable; it
1305 is only meant to handle relocatable objects. */
1306
1307 static boolean
1308 som_prep_headers (abfd)
1309 bfd *abfd;
1310 {
1311 struct header *file_hdr = obj_som_file_hdr (abfd);
1312 asection *section;
1313
1314 /* FIXME. This should really be conditional based on whether or not
1315 PA1.1 instructions/registers have been used. */
1316 file_hdr->system_id = HP9000S800_ID;
1317
1318 /* FIXME. Only correct for building relocatable objects. */
1319 if (abfd->flags & EXEC_P)
1320 abort ();
1321 else
1322 file_hdr->a_magic = RELOC_MAGIC;
1323
1324 /* Only new format SOM is supported. */
1325 file_hdr->version_id = NEW_VERSION_ID;
1326
1327 /* These fields are optional, and embedding timestamps is not always
1328 a wise thing to do, it makes comparing objects during a multi-stage
1329 bootstrap difficult. */
1330 file_hdr->file_time.secs = 0;
1331 file_hdr->file_time.nanosecs = 0;
1332
1333 if (abfd->flags & EXEC_P)
1334 abort ();
1335 else
1336 {
1337 file_hdr->entry_space = 0;
1338 file_hdr->entry_subspace = 0;
1339 file_hdr->entry_offset = 0;
1340 }
1341
1342 /* FIXME. I do not know if we ever need to put anything other
1343 than zero in this field. */
1344 file_hdr->presumed_dp = 0;
1345
1346 /* Now iterate over the sections translating information from
1347 BFD sections to SOM spaces/subspaces. */
1348
1349 for (section = abfd->sections; section != NULL; section = section->next)
1350 {
1351 /* Ignore anything which has not been marked as a space or
1352 subspace. */
1353 if (som_section_data (section)->is_space == 0
1354
1355 && som_section_data (section)->is_subspace == 0)
1356 continue;
1357
1358 if (som_section_data (section)->is_space)
1359 {
1360 /* Set space attributes. Note most attributes of SOM spaces
1361 are set based on the subspaces it contains. */
1362 som_section_data (section)->space_dict.loader_fix_index = -1;
1363 som_section_data (section)->space_dict.init_pointer_index = -1;
1364 }
1365 else
1366 {
1367 /* Set subspace attributes. Basic stuff is done here, additional
1368 attributes are filled in later as more information becomes
1369 available. */
1370 if (section->flags & SEC_IS_COMMON)
1371 {
1372 som_section_data (section)->subspace_dict.dup_common = 1;
1373 som_section_data (section)->subspace_dict.is_common = 1;
1374 }
1375
1376 if (section->flags & SEC_ALLOC)
1377 som_section_data (section)->subspace_dict.is_loadable = 1;
1378
1379 if (section->flags & SEC_CODE)
1380 som_section_data (section)->subspace_dict.code_only = 1;
1381
1382 som_section_data (section)->subspace_dict.subspace_start =
1383 section->vma;
1384 som_section_data (section)->subspace_dict.subspace_length =
1385 bfd_section_size (abfd, section);
1386 som_section_data (section)->subspace_dict.initialization_length =
1387 bfd_section_size (abfd, section);
1388 som_section_data (section)->subspace_dict.alignment =
1389 1 << section->alignment_power;
1390 }
1391 }
1392 return true;
1393 }
1394
1395 /* Count and return the number of spaces attached to the given BFD. */
1396
1397 static unsigned long
1398 som_count_spaces (abfd)
1399 bfd *abfd;
1400 {
1401 int count = 0;
1402 asection *section;
1403
1404 for (section = abfd->sections; section != NULL; section = section->next)
1405 count += som_section_data (section)->is_space;
1406
1407 return count;
1408 }
1409
1410 /* Count the number of subspaces attached to the given BFD. */
1411
1412 static unsigned long
1413 som_count_subspaces (abfd)
1414 bfd *abfd;
1415 {
1416 int count = 0;
1417 asection *section;
1418
1419 for (section = abfd->sections; section != NULL; section = section->next)
1420 count += som_section_data (section)->is_subspace;
1421
1422 return count;
1423 }
1424
1425 /* Return -1, 0, 1 indicating the relative ordering of sym1 and sym2.
1426
1427 We desire symbols to be ordered starting with the symbol with the
1428 highest relocation count down to the symbol with the lowest relocation
1429 count. Doing so compacts the relocation stream. */
1430
1431 static int
1432 compare_syms (sym1, sym2)
1433 asymbol **sym1;
1434 asymbol **sym2;
1435
1436 {
1437 unsigned int count1, count2;
1438
1439 /* Get relocation count for each symbol. Note that the count
1440 is stored in the udata pointer for section symbols! */
1441 if ((*sym1)->flags & BSF_SECTION_SYM)
1442 count1 = (int)(*sym1)->udata;
1443 else
1444 count1 = (*som_symbol_data ((*sym1)))->reloc_count;
1445
1446 if ((*sym2)->flags & BSF_SECTION_SYM)
1447 count2 = (int)(*sym2)->udata;
1448 else
1449 count2 = (*som_symbol_data ((*sym2)))->reloc_count;
1450
1451 /* Return the appropriate value. */
1452 if (count1 < count2)
1453 return 1;
1454 else if (count1 > count2)
1455 return -1;
1456 return 0;
1457 }
1458
1459 /* Perform various work in preparation for emitting the fixup stream. */
1460
1461 static void
1462 som_prep_for_fixups (abfd, syms, num_syms)
1463 bfd *abfd;
1464 asymbol **syms;
1465 unsigned long num_syms;
1466 {
1467 int i;
1468 asection *section;
1469
1470 /* Most SOM relocations involving a symbol have a length which is
1471 dependent on the index of the symbol. So symbols which are
1472 used often in relocations should have a small index. */
1473
1474 /* First initialize the counters for each symbol. */
1475 for (i = 0; i < num_syms; i++)
1476 {
1477 /* Handle a section symbol; these have no pointers back to the
1478 SOM symbol info. So we just use the pointer field (udata)
1479 to hold the relocation count.
1480
1481 FIXME. While we're here set the name of any section symbol
1482 to something which will not screw GDB. How do other formats
1483 deal with this?!? */
1484 if (som_symbol_data (syms[i]) == NULL)
1485 {
1486 syms[i]->flags |= BSF_SECTION_SYM;
1487 syms[i]->name = "L$0\002";
1488 syms[i]->udata = (PTR) 0;
1489 }
1490 else
1491 (*som_symbol_data (syms[i]))->reloc_count = 0;
1492 }
1493
1494 /* Now that the counters are initialized, make a weighted count
1495 of how often a given symbol is used in a relocation. */
1496 for (section = abfd->sections; section != NULL; section = section->next)
1497 {
1498 int i;
1499
1500 /* Does this section have any relocations? */
1501 if (section->reloc_count <= 0)
1502 continue;
1503
1504 /* Walk through each relocation for this section. */
1505 for (i = 1; i < section->reloc_count; i++)
1506 {
1507 arelent *reloc = section->orelocation[i];
1508 int scale;
1509
1510 /* If no symbol, then there is no counter to increase. */
1511 if (reloc->sym_ptr_ptr == NULL)
1512 continue;
1513
1514 /* Scaling to encourage symbols involved in R_DP_RELATIVE
1515 and R_CODE_ONE_SYMBOL relocations to come first. These
1516 two relocations have single byte versions if the symbol
1517 index is very small. */
1518 if (reloc->howto->type == R_DP_RELATIVE
1519 || reloc->howto->type == R_CODE_ONE_SYMBOL)
1520 scale = 2;
1521 else
1522 scale = 1;
1523
1524 /* Handle section symbols by ramming the count in the udata
1525 field. It will not be used and the count is very important
1526 for these symbols. */
1527 if ((*reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
1528 {
1529 (*reloc->sym_ptr_ptr)->udata =
1530 (PTR) ((int) (*reloc->sym_ptr_ptr)->udata + scale);
1531 continue;
1532 }
1533
1534 /* A normal symbol. Increment the count. */
1535 (*som_symbol_data ((*reloc->sym_ptr_ptr)))->reloc_count += scale;
1536 }
1537 }
1538
1539 /* Now sort the symbols. */
1540 qsort (syms, num_syms, sizeof (asymbol *), compare_syms);
1541
1542 /* Compute the symbol indexes, they will be needed by the relocation
1543 code. */
1544 for (i = 0; i < num_syms; i++)
1545 {
1546 /* A section symbol. Again, there is no pointer to backend symbol
1547 information, so we reuse (abuse) the udata field again. */
1548 if (syms[i]->flags & BSF_SECTION_SYM)
1549 syms[i]->udata = (PTR) i;
1550 else
1551 (*som_symbol_data (syms[i]))->index = i;
1552 }
1553 }
1554
1555 static boolean
1556 som_write_fixups (abfd, current_offset, total_reloc_sizep)
1557 bfd *abfd;
1558 unsigned long current_offset;
1559 unsigned int *total_reloc_sizep;
1560 {
1561 unsigned int i, j;
1562 unsigned char *tmp_space, *p;
1563 unsigned int total_reloc_size = 0;
1564 unsigned int subspace_reloc_size = 0;
1565 unsigned int num_spaces = obj_som_file_hdr (abfd)->space_total;
1566 asection *section = abfd->sections;
1567
1568 /* Get a chunk of memory that we can use as buffer space, then throw
1569 away. */
1570 tmp_space = alloca (SOM_TMP_BUFSIZE);
1571 bzero (tmp_space, SOM_TMP_BUFSIZE);
1572 p = tmp_space;
1573
1574 /* All the fixups for a particular subspace are emitted in a single
1575 stream. All the subspaces for a particular space are emitted
1576 as a single stream.
1577
1578 So, to get all the locations correct one must iterate through all the
1579 spaces, for each space iterate through its subspaces and output a
1580 fixups stream. */
1581 for (i = 0; i < num_spaces; i++)
1582 {
1583 asection *subsection;
1584
1585 /* Find a space. */
1586 while (som_section_data (section)->is_space == 0)
1587 section = section->next;
1588
1589 /* Now iterate through each of its subspaces. */
1590 for (subsection = abfd->sections;
1591 subsection != NULL;
1592 subsection = subsection->next)
1593 {
1594 int reloc_offset;
1595
1596 /* Find a subspace of this space. */
1597 if (som_section_data (subsection)->is_subspace == 0
1598 || som_section_data (subsection)->containing_space != section)
1599 continue;
1600
1601 /* If this subspace had no relocations, then we're finished
1602 with it. */
1603 if (subsection->reloc_count <= 0)
1604 {
1605 som_section_data (subsection)->subspace_dict.fixup_request_index
1606 = -1;
1607 continue;
1608 }
1609
1610 /* This subspace has some relocations. Put the relocation stream
1611 index into the subspace record. */
1612 som_section_data (subsection)->subspace_dict.fixup_request_index
1613 = total_reloc_size;
1614
1615 /* To make life easier start over with a clean slate for
1616 each subspace. Seek to the start of the relocation stream
1617 for this subspace in preparation for writing out its fixup
1618 stream. */
1619 if (bfd_seek (abfd, current_offset + total_reloc_size, SEEK_SET) != 0)
1620 {
1621 bfd_error = system_call_error;
1622 return false;
1623 }
1624
1625 /* Buffer space has already been allocated. Just perform some
1626 initialization here. */
1627 p = tmp_space;
1628 subspace_reloc_size = 0;
1629 reloc_offset = 0;
1630 som_initialize_reloc_queue (reloc_queue);
1631
1632 /* Translate each BFD relocation into one or more SOM
1633 relocations. */
1634 for (j = 0; j < subsection->reloc_count; j++)
1635 {
1636 arelent *bfd_reloc = subsection->orelocation[j];
1637 unsigned int skip;
1638 int sym_num;
1639
1640 /* Get the symbol number. Remember it's stored in a
1641 special place for section symbols. */
1642 if ((*bfd_reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
1643 sym_num = (int) (*bfd_reloc->sym_ptr_ptr)->udata;
1644 else
1645 sym_num = (*som_symbol_data ((*bfd_reloc->sym_ptr_ptr)))->index;
1646
1647 /* If there is not enough room for the next couple relocations,
1648 then dump the current buffer contents now. Also reinitialize
1649 the relocation queue.
1650
1651 FIXME. We assume here that no BFD relocation will expand
1652 to more than 100 bytes of SOM relocations. This should (?!?)
1653 be quite safe. */
1654 if (p - tmp_space + 100 > SOM_TMP_BUFSIZE)
1655 {
1656 if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
1657 != p - tmp_space)
1658 {
1659 bfd_error = system_call_error;
1660 return false;
1661 }
1662 p = tmp_space;
1663 som_initialize_reloc_queue (reloc_queue);
1664 }
1665
1666 /* Emit R_NO_RELOCATION fixups to map any bytes which were
1667 skipped. */
1668 skip = bfd_reloc->address - reloc_offset;
1669 p = som_reloc_skip (abfd, skip, p,
1670 &subspace_reloc_size, reloc_queue);
1671
1672 /* Update reloc_offset for the next iteration.
1673
1674 Note R_ENTRY and R_EXIT relocations are just markers,
1675 they do not consume input bytes. */
1676 if (bfd_reloc->howto->type != R_ENTRY
1677 && bfd_reloc->howto->type != R_EXIT)
1678 reloc_offset = bfd_reloc->address + 4;
1679 else
1680 reloc_offset = bfd_reloc->address;
1681
1682
1683 /* Now the actual relocation we care about. */
1684 switch (bfd_reloc->howto->type)
1685 {
1686 case R_PCREL_CALL:
1687 case R_ABS_CALL:
1688 p = som_reloc_call (abfd, p, &subspace_reloc_size,
1689 bfd_reloc, sym_num, reloc_queue);
1690 break;
1691
1692 case R_CODE_ONE_SYMBOL:
1693 case R_DP_RELATIVE:
1694 /* Account for any addend. */
1695 if (bfd_reloc->addend)
1696 p = som_reloc_addend (abfd, bfd_reloc->addend, p,
1697 &subspace_reloc_size, reloc_queue);
1698
1699 if (sym_num < 0x20)
1700 {
1701 bfd_put_8 (abfd, bfd_reloc->howto->type + sym_num, p);
1702 subspace_reloc_size += 1;
1703 p += 1;
1704 }
1705 else if (sym_num < 0x100)
1706 {
1707 bfd_put_8 (abfd, bfd_reloc->howto->type + 32, p);
1708 bfd_put_8 (abfd, sym_num, p + 1);
1709 p = try_prev_fixup (abfd, &subspace_reloc_size, p,
1710 2, reloc_queue);
1711 }
1712 else if (sym_num < 0x10000000)
1713 {
1714 bfd_put_8 (abfd, bfd_reloc->howto->type + 33, p);
1715 bfd_put_8 (abfd, sym_num >> 16, p + 1);
1716 bfd_put_16 (abfd, sym_num, p + 2);
1717 p = try_prev_fixup (abfd, &subspace_reloc_size,
1718 p, 4, reloc_queue);
1719 }
1720 else
1721 abort ();
1722 break;
1723
1724 case R_DATA_ONE_SYMBOL:
1725 case R_DATA_PLABEL:
1726 case R_CODE_PLABEL:
1727 /* Account for any addend. */
1728 if (bfd_reloc->addend)
1729 p = som_reloc_addend (abfd, bfd_reloc->addend, p,
1730 &subspace_reloc_size, reloc_queue);
1731
1732 if (sym_num < 0x100)
1733 {
1734 bfd_put_8 (abfd, bfd_reloc->howto->type, p);
1735 bfd_put_8 (abfd, sym_num, p + 1);
1736 p = try_prev_fixup (abfd, &subspace_reloc_size, p,
1737 2, reloc_queue);
1738 }
1739 else if (sym_num < 0x10000000)
1740 {
1741 bfd_put_8 (abfd, bfd_reloc->howto->type + 1, p);
1742 bfd_put_8 (abfd, sym_num >> 16, p + 1);
1743 bfd_put_16 (abfd, sym_num, p + 2);
1744 p = try_prev_fixup (abfd, &subspace_reloc_size,
1745 p, 4, reloc_queue);
1746 }
1747 else
1748 abort ();
1749 break;
1750
1751 case R_ENTRY:
1752 {
1753 int *descp
1754 = (int *) (*som_symbol_data ((*bfd_reloc->sym_ptr_ptr)))->unwind;
1755 bfd_put_8 (abfd, R_ENTRY, p);
1756 bfd_put_32 (abfd, descp[0], p + 1);
1757 bfd_put_32 (abfd, descp[1], p + 5);
1758 p = try_prev_fixup (abfd, &subspace_reloc_size,
1759 p, 9, reloc_queue);
1760 break;
1761 }
1762
1763 case R_EXIT:
1764 bfd_put_8 (abfd, R_EXIT, p);
1765 subspace_reloc_size += 1;
1766 p += 1;
1767 break;
1768
1769 /* Put a "R_RESERVED" relocation in the stream if
1770 we hit something we do not understand. The linker
1771 will complain loudly if this ever happens. */
1772 default:
1773 bfd_put_8 (abfd, 0xff, p);
1774 subspace_reloc_size += 1;
1775 p += 1;
1776 }
1777 }
1778
1779 /* Last BFD relocation for a subspace has been processed.
1780 Map the rest of the subspace with R_NO_RELOCATION fixups. */
1781 p = som_reloc_skip (abfd, bfd_section_size (abfd, subsection)
1782 - reloc_offset,
1783 p, &subspace_reloc_size, reloc_queue);
1784
1785 /* Scribble out the relocations. */
1786 if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
1787 != p - tmp_space)
1788 {
1789 bfd_error = system_call_error;
1790 return false;
1791 }
1792 p = tmp_space;
1793
1794 total_reloc_size += subspace_reloc_size;
1795 som_section_data (subsection)->subspace_dict.fixup_request_quantity
1796 = subspace_reloc_size;
1797 }
1798 section = section->next;
1799 }
1800 *total_reloc_sizep = total_reloc_size;
1801 return true;
1802 }
1803
1804 /* Write out the space/subspace string table. */
1805
1806 static boolean
1807 som_write_space_strings (abfd, current_offset, string_sizep)
1808 bfd *abfd;
1809 unsigned long current_offset;
1810 unsigned int *string_sizep;
1811 {
1812 unsigned char *tmp_space, *p;
1813 unsigned int strings_size = 0;
1814 asection *section;
1815
1816 /* Get a chunk of memory that we can use as buffer space, then throw
1817 away. */
1818 tmp_space = alloca (SOM_TMP_BUFSIZE);
1819 bzero (tmp_space, SOM_TMP_BUFSIZE);
1820 p = tmp_space;
1821
1822 /* Seek to the start of the space strings in preparation for writing
1823 them out. */
1824 if (bfd_seek (abfd, current_offset, SEEK_SET) != 0)
1825 {
1826 bfd_error = system_call_error;
1827 return false;
1828 }
1829
1830 /* Walk through all the spaces and subspaces (order is not important)
1831 building up and writing string table entries for their names. */
1832 for (section = abfd->sections; section != NULL; section = section->next)
1833 {
1834 int length;
1835
1836 /* Only work with space/subspaces; avoid any other sections
1837 which might have been made (.text for example). */
1838 if (som_section_data (section)->is_space == 0
1839 && som_section_data (section)->is_subspace == 0)
1840 continue;
1841
1842 /* Get the length of the space/subspace name. */
1843 length = strlen (section->name);
1844
1845 /* If there is not enough room for the next entry, then dump the
1846 current buffer contents now. Each entry will take 4 bytes to
1847 hold the string length + the string itself + null terminator. */
1848 if (p - tmp_space + 5 + length > SOM_TMP_BUFSIZE)
1849 {
1850 if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
1851 != p - tmp_space)
1852 {
1853 bfd_error = system_call_error;
1854 return false;
1855 }
1856 /* Reset to beginning of the buffer space. */
1857 p = tmp_space;
1858 }
1859
1860 /* First element in a string table entry is the length of the
1861 string. Alignment issues are already handled. */
1862 bfd_put_32 (abfd, length, p);
1863 p += 4;
1864 strings_size += 4;
1865
1866 /* Record the index in the space/subspace records. */
1867 if (som_section_data (section)->is_space)
1868 som_section_data (section)->space_dict.name.n_strx = strings_size;
1869 else
1870 som_section_data (section)->subspace_dict.name.n_strx = strings_size;
1871
1872 /* Next comes the string itself + a null terminator. */
1873 strcpy (p, section->name);
1874 p += length + 1;
1875 strings_size += length + 1;
1876
1877 /* Always align up to the next word boundary. */
1878 while (strings_size % 4)
1879 {
1880 bfd_put_8 (abfd, 0, p);
1881 p++;
1882 strings_size++;
1883 }
1884 }
1885
1886 /* Done with the space/subspace strings. Write out any information
1887 contained in a partial block. */
1888 if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) != p - tmp_space)
1889 {
1890 bfd_error = system_call_error;
1891 return false;
1892 }
1893 *string_sizep = strings_size;
1894 return true;
1895 }
1896
1897 /* Write out the symbol string table. */
1898
1899 static boolean
1900 som_write_symbol_strings (abfd, current_offset, syms, num_syms, string_sizep)
1901 bfd *abfd;
1902 unsigned long current_offset;
1903 asymbol **syms;
1904 unsigned int num_syms;
1905 unsigned int *string_sizep;
1906 {
1907 unsigned int i;
1908 unsigned char *tmp_space, *p;
1909 unsigned int strings_size = 0;
1910
1911 /* Get a chunk of memory that we can use as buffer space, then throw
1912 away. */
1913 tmp_space = alloca (SOM_TMP_BUFSIZE);
1914 bzero (tmp_space, SOM_TMP_BUFSIZE);
1915 p = tmp_space;
1916
1917 /* Seek to the start of the space strings in preparation for writing
1918 them out. */
1919 if (bfd_seek (abfd, current_offset, SEEK_SET) != 0)
1920 {
1921 bfd_error = system_call_error;
1922 return false;
1923 }
1924
1925 for (i = 0; i < num_syms; i++)
1926 {
1927 int length = strlen (syms[i]->name);
1928
1929 /* If there is not enough room for the next entry, then dump the
1930 current buffer contents now. */
1931 if (p - tmp_space + 5 + length > SOM_TMP_BUFSIZE)
1932 {
1933 if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
1934 != p - tmp_space)
1935 {
1936 bfd_error = system_call_error;
1937 return false;
1938 }
1939 /* Reset to beginning of the buffer space. */
1940 p = tmp_space;
1941 }
1942
1943 /* First element in a string table entry is the length of the
1944 string. This must always be 4 byte aligned. This is also
1945 an appropriate time to fill in the string index field in the
1946 symbol table entry. */
1947 bfd_put_32 (abfd, length, p);
1948 strings_size += 4;
1949 p += 4;
1950
1951 /* Next comes the string itself + a null terminator. */
1952 strcpy (p, syms[i]->name);
1953
1954 /* ACK. FIXME. */
1955 syms[i]->name = (char *)strings_size;
1956 p += length + 1;
1957 strings_size += length + 1;
1958
1959 /* Always align up to the next word boundary. */
1960 while (strings_size % 4)
1961 {
1962 bfd_put_8 (abfd, 0, p);
1963 strings_size++;
1964 p++;
1965 }
1966 }
1967
1968 /* Scribble out any partial block. */
1969 if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) != p - tmp_space)
1970 {
1971 bfd_error = system_call_error;
1972 return false;
1973 }
1974
1975 *string_sizep = strings_size;
1976 return true;
1977 }
1978
1979 /* Compute variable information to be placed in the SOM headers,
1980 space/subspace dictionaries, relocation streams, etc. Begin
1981 writing parts of the object file. */
1982
1983 static boolean
1984 som_begin_writing (abfd)
1985 bfd *abfd;
1986 {
1987 unsigned long current_offset = 0;
1988 int strings_size = 0;
1989 unsigned int total_reloc_size = 0;
1990 unsigned long num_spaces, num_subspaces, num_syms, i;
1991 asection *section;
1992 asymbol **syms = bfd_get_outsymbols (abfd);
1993 unsigned int total_subspaces = 0;
1994
1995 /* The file header will always be first in an object file,
1996 everything else can be in random locations. To keep things
1997 "simple" BFD will lay out the object file in the manner suggested
1998 by the PRO ABI for PA-RISC Systems. */
1999
2000 /* Before any output can really begin offsets for all the major
2001 portions of the object file must be computed. So, starting
2002 with the initial file header compute (and sometimes write)
2003 each portion of the object file. */
2004
2005 /* Make room for the file header, it's contents are not complete
2006 yet, so it can not be written at this time. */
2007 current_offset += sizeof (struct header);
2008
2009 /* Any auxiliary headers will follow the file header. Right now
2010 we have no auxiliary headers, so current_offset does not change. */
2011 obj_som_file_hdr (abfd)->aux_header_location = current_offset;
2012 obj_som_file_hdr (abfd)->aux_header_size = 0;
2013
2014 /* Next comes the initialization pointers; again we have no
2015 initialization pointers, so current offset does not change. */
2016 obj_som_file_hdr (abfd)->init_array_location = current_offset;
2017 obj_som_file_hdr (abfd)->init_array_total = 0;
2018
2019 /* Next are the space records. These are fixed length records.
2020
2021 Count the number of spaces to determine how much room is needed
2022 in the object file for the space records.
2023
2024 The names of the spaces are stored in a separate string table,
2025 and the index for each space into the string table is computed
2026 below. Therefore, it is not possible to write the space headers
2027 at this time. */
2028 num_spaces = som_count_spaces (abfd);
2029 obj_som_file_hdr (abfd)->space_location = current_offset;
2030 obj_som_file_hdr (abfd)->space_total = num_spaces;
2031 current_offset += num_spaces * sizeof (struct space_dictionary_record);
2032
2033 /* Next are the subspace records. These are fixed length records.
2034
2035 Count the number of subspaes to determine how much room is needed
2036 in the object file for the subspace records.
2037
2038 A variety if fields in the subspace record are still unknown at
2039 this time (index into string table, fixup stream location/size, etc). */
2040 num_subspaces = som_count_subspaces (abfd);
2041 obj_som_file_hdr (abfd)->subspace_location = current_offset;
2042 obj_som_file_hdr (abfd)->subspace_total = num_subspaces;
2043 current_offset += num_subspaces * sizeof (struct subspace_dictionary_record);
2044
2045 /* Next is the string table for the space/subspace names. We will
2046 build and write the string table on the fly. At the same time
2047 we will fill in the space/subspace name index fields. */
2048
2049 /* The string table needs to be aligned on a word boundary. */
2050 if (current_offset % 4)
2051 current_offset += (4 - (current_offset % 4));
2052
2053 /* Mark the offset of the space/subspace string table in the
2054 file header. */
2055 obj_som_file_hdr (abfd)->space_strings_location = current_offset;
2056
2057 /* Scribble out the space strings. */
2058 if (som_write_space_strings (abfd, current_offset, &strings_size) == false)
2059 return false;
2060
2061 /* Record total string table size in the header and update the
2062 current offset. */
2063 obj_som_file_hdr (abfd)->space_strings_size = strings_size;
2064 current_offset += strings_size;
2065
2066 /* Next is the symbol table. These are fixed length records.
2067
2068 Count the number of symbols to determine how much room is needed
2069 in the object file for the symbol table.
2070
2071 The names of the symbols are stored in a separate string table,
2072 and the index for each symbol name into the string table is computed
2073 below. Therefore, it is not possible to write the symobl table
2074 at this time. */
2075 num_syms = bfd_get_symcount (abfd);
2076 obj_som_file_hdr (abfd)->symbol_location = current_offset;
2077 obj_som_file_hdr (abfd)->symbol_total = num_syms;
2078 current_offset += num_syms * sizeof (struct symbol_dictionary_record);
2079
2080 /* Do prep work before handling fixups. */
2081 som_prep_for_fixups (abfd, syms, num_syms);
2082
2083 /* Next comes the fixup stream which starts on a word boundary. */
2084 if (current_offset % 4)
2085 current_offset += (4 - (current_offset % 4));
2086 obj_som_file_hdr (abfd)->fixup_request_location = current_offset;
2087
2088 /* Write the fixups and update fields in subspace headers which
2089 relate to the fixup stream. */
2090 if (som_write_fixups (abfd, current_offset, &total_reloc_size) == false)
2091 return false;
2092
2093 /* Record the total size of the fixup stream in the file header. */
2094 obj_som_file_hdr (abfd)->fixup_request_total = total_reloc_size;
2095 current_offset += total_reloc_size;
2096
2097 /* Next are the symbol strings.
2098 Align them to a word boundary. */
2099 if (current_offset % 4)
2100 current_offset += (4 - (current_offset % 4));
2101 obj_som_file_hdr (abfd)->symbol_strings_location = current_offset;
2102
2103 /* Scribble out the symbol strings. */
2104 if (som_write_symbol_strings (abfd, current_offset, syms,
2105 num_syms, &strings_size)
2106 == false)
2107 return false;
2108
2109 /* Record total string table size in header and update the
2110 current offset. */
2111 obj_som_file_hdr (abfd)->symbol_strings_size = strings_size;
2112 current_offset += strings_size;
2113
2114 /* Next is the compiler records. We do not use these. */
2115 obj_som_file_hdr (abfd)->compiler_location = current_offset;
2116 obj_som_file_hdr (abfd)->compiler_total = 0;
2117
2118 /* Now compute the file positions for the loadable subspaces. */
2119
2120 section = abfd->sections;
2121 for (i = 0; i < num_spaces; i++)
2122 {
2123 asection *subsection;
2124
2125 /* Find a space. */
2126 while (som_section_data (section)->is_space == 0)
2127 section = section->next;
2128
2129 /* Now look for all its subspaces. */
2130 for (subsection = abfd->sections;
2131 subsection != NULL;
2132 subsection = subsection->next)
2133 {
2134
2135 if (som_section_data (subsection)->is_subspace == 0
2136 || som_section_data (subsection)->containing_space != section
2137 || (subsection->flags & SEC_ALLOC) == 0)
2138 continue;
2139
2140 som_section_data (subsection)->subspace_index = total_subspaces++;
2141 /* This is real data to be loaded from the file. */
2142 if (subsection->flags & SEC_LOAD)
2143 {
2144 som_section_data (subsection)->subspace_dict.file_loc_init_value
2145 = current_offset;
2146 section->filepos = current_offset;
2147 current_offset += bfd_section_size (abfd, subsection);
2148 }
2149 /* Looks like uninitialized data. */
2150 else
2151 {
2152 som_section_data (subsection)->subspace_dict.file_loc_init_value
2153 = 0;
2154 som_section_data (subsection)->subspace_dict.
2155 initialization_length = 0;
2156 }
2157 }
2158 /* Goto the next section. */
2159 section = section->next;
2160 }
2161
2162 /* Finally compute the file positions for unloadable subspaces. */
2163
2164 obj_som_file_hdr (abfd)->unloadable_sp_location = current_offset;
2165 section = abfd->sections;
2166 for (i = 0; i < num_spaces; i++)
2167 {
2168 asection *subsection;
2169
2170 /* Find a space. */
2171 while (som_section_data (section)->is_space == 0)
2172 section = section->next;
2173
2174 /* Now look for all its subspaces. */
2175 for (subsection = abfd->sections;
2176 subsection != NULL;
2177 subsection = subsection->next)
2178 {
2179
2180 if (som_section_data (subsection)->is_subspace == 0
2181 || som_section_data (subsection)->containing_space != section
2182 || (subsection->flags & SEC_ALLOC) != 0)
2183 continue;
2184
2185 som_section_data (subsection)->subspace_index = total_subspaces++;
2186 /* This is real data to be loaded from the file. */
2187 if ((subsection->flags & SEC_LOAD) == 0)
2188 {
2189 som_section_data (subsection)->subspace_dict.file_loc_init_value
2190 = current_offset;
2191 section->filepos = current_offset;
2192 current_offset += bfd_section_size (abfd, subsection);
2193 }
2194 /* Looks like uninitialized data. */
2195 else
2196 {
2197 som_section_data (subsection)->subspace_dict.file_loc_init_value
2198 = 0;
2199 som_section_data (subsection)->subspace_dict.
2200 initialization_length = bfd_section_size (abfd, subsection);
2201 }
2202 }
2203 /* Goto the next section. */
2204 section = section->next;
2205 }
2206
2207 obj_som_file_hdr (abfd)->unloadable_sp_size
2208 = current_offset - obj_som_file_hdr (abfd)->unloadable_sp_location;
2209
2210 /* Loader fixups are not supported in any way shape or form. */
2211 obj_som_file_hdr (abfd)->loader_fixup_location = 0;
2212 obj_som_file_hdr (abfd)->loader_fixup_total = 0;
2213
2214 /* Done. Store the total size of the SOM. */
2215 obj_som_file_hdr (abfd)->som_length = current_offset;
2216 return true;
2217 }
2218
2219 /* Finally, scribble out the various headers to the disk. */
2220
2221 static boolean
2222 som_write_headers (abfd)
2223 bfd *abfd;
2224 {
2225 int num_spaces = som_count_spaces (abfd);
2226 int i;
2227 int subspace_index = 0;
2228 file_ptr location;
2229 asection *section;
2230
2231 /* Subspaces are written first so that we can set up information
2232 about them in their containing spaces as the subspace is written. */
2233
2234 /* Seek to the start of the subspace dictionary records. */
2235 location = obj_som_file_hdr (abfd)->subspace_location;
2236 bfd_seek (abfd, location, SEEK_SET);
2237 section = abfd->sections;
2238 /* Now for each loadable space write out records for its subspaces. */
2239 for (i = 0; i < num_spaces; i++)
2240 {
2241 asection *subsection;
2242
2243 /* Find a space. */
2244 while (som_section_data (section)->is_space == 0)
2245 section = section->next;
2246
2247 /* Now look for all its subspaces. */
2248 for (subsection = abfd->sections;
2249 subsection != NULL;
2250 subsection = subsection->next)
2251 {
2252
2253 /* Skip any section which does not correspond to a space
2254 or subspace. Or does not have SEC_ALLOC set (and therefore
2255 has no real bits on the disk). */
2256 if (som_section_data (subsection)->is_subspace == 0
2257 || som_section_data (subsection)->containing_space != section
2258 || (subsection->flags & SEC_ALLOC) == 0)
2259 continue;
2260
2261 /* If this is the first subspace for this space, then save
2262 the index of the subspace in its containing space. Also
2263 set "is_loadable" in the containing space. */
2264
2265 if (som_section_data (section)->space_dict.subspace_quantity == 0)
2266 {
2267 som_section_data (section)->space_dict.is_loadable = 1;
2268 som_section_data (section)->space_dict.subspace_index
2269 = subspace_index;
2270 }
2271
2272 /* Increment the number of subspaces seen and the number of
2273 subspaces contained within the current space. */
2274 subspace_index++;
2275 som_section_data (section)->space_dict.subspace_quantity++;
2276
2277 /* Mark the index of the current space within the subspace's
2278 dictionary record. */
2279 som_section_data (subsection)->subspace_dict.space_index = i;
2280
2281 /* Dump the current subspace header. */
2282 if (bfd_write ((PTR) &som_section_data (subsection)->subspace_dict,
2283 sizeof (struct subspace_dictionary_record), 1, abfd)
2284 != sizeof (struct subspace_dictionary_record))
2285 {
2286 bfd_error = system_call_error;
2287 return false;
2288 }
2289 }
2290 /* Goto the next section. */
2291 section = section->next;
2292 }
2293
2294 /* Now repeat the process for unloadable subspaces. */
2295 section = abfd->sections;
2296 /* Now for each space write out records for its subspaces. */
2297 for (i = 0; i < num_spaces; i++)
2298 {
2299 asection *subsection;
2300
2301 /* Find a space. */
2302 while (som_section_data (section)->is_space == 0)
2303 section = section->next;
2304
2305 /* Now look for all its subspaces. */
2306 for (subsection = abfd->sections;
2307 subsection != NULL;
2308 subsection = subsection->next)
2309 {
2310
2311 /* Skip any section which does not correspond to a space or
2312 subspace, or which SEC_ALLOC set (and therefore handled
2313 in the loadable spaces/subspaces code above. */
2314
2315 if (som_section_data (subsection)->is_subspace == 0
2316 || som_section_data (subsection)->containing_space != section
2317 || (subsection->flags & SEC_ALLOC) != 0)
2318 continue;
2319
2320 /* If this is the first subspace for this space, then save
2321 the index of the subspace in its containing space. Clear
2322 "is_loadable". */
2323
2324 if (som_section_data (section)->space_dict.subspace_quantity == 0)
2325 {
2326 som_section_data (section)->space_dict.is_loadable = 0;
2327 som_section_data (section)->space_dict.subspace_index
2328 = subspace_index;
2329 }
2330
2331 /* Increment the number of subspaces seen and the number of
2332 subspaces contained within the current space. */
2333 som_section_data (section)->space_dict.subspace_quantity++;
2334 subspace_index++;
2335
2336 /* Mark the index of the current space within the subspace's
2337 dictionary record. */
2338 som_section_data (subsection)->subspace_dict.space_index = i;
2339
2340 /* Dump this subspace header. */
2341 if (bfd_write ((PTR) &som_section_data (subsection)->subspace_dict,
2342 sizeof (struct subspace_dictionary_record), 1, abfd)
2343 != sizeof (struct subspace_dictionary_record))
2344 {
2345 bfd_error = system_call_error;
2346 return false;
2347 }
2348 }
2349 /* Goto the next section. */
2350 section = section->next;
2351 }
2352
2353 /* All the subspace dictiondary records are written, and all the
2354 fields are set up in the space dictionary records.
2355
2356 Seek to the right location and start writing the space
2357 dictionary records. */
2358 location = obj_som_file_hdr (abfd)->space_location;
2359 bfd_seek (abfd, location, SEEK_SET);
2360
2361 section = abfd->sections;
2362 for (i = 0; i < num_spaces; i++)
2363 {
2364
2365 /* Find a space. */
2366 while (som_section_data (section)->is_space == 0)
2367 section = section->next;
2368
2369 /* Dump its header */
2370 if (bfd_write ((PTR) &som_section_data (section)->space_dict,
2371 sizeof (struct space_dictionary_record), 1, abfd)
2372 != sizeof (struct space_dictionary_record))
2373 {
2374 bfd_error = system_call_error;
2375 return false;
2376 }
2377
2378 /* Goto the next section. */
2379 section = section->next;
2380 }
2381
2382 /* Only thing left to do is write out the file header. It is always
2383 at location zero. Seek there and write it. */
2384 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
2385 if (bfd_write ((PTR) obj_som_file_hdr (abfd),
2386 sizeof (struct header), 1, abfd)
2387 != sizeof (struct header))
2388 {
2389 bfd_error = system_call_error;
2390 return false;
2391 }
2392 return true;
2393 }
2394
2395 /* Compute and return the checksum for a SOM file header. */
2396
2397 static unsigned long
2398 som_compute_checksum (abfd)
2399 bfd *abfd;
2400 {
2401 unsigned long checksum, count, i;
2402 unsigned long *buffer = (unsigned long *) obj_som_file_hdr (abfd);
2403
2404 checksum = 0;
2405 count = sizeof (struct header) / sizeof (unsigned long);
2406 for (i = 0; i < count; i++)
2407 checksum ^= *(buffer + i);
2408
2409 return checksum;
2410 }
2411
2412 /* Build and write, in one big chunk, the entire symbol table for
2413 this BFD. */
2414
2415 static boolean
2416 som_build_and_write_symbol_table (abfd)
2417 bfd *abfd;
2418 {
2419 unsigned int num_syms = bfd_get_symcount (abfd);
2420 file_ptr symtab_location = obj_som_file_hdr (abfd)->symbol_location;
2421 asymbol **bfd_syms = bfd_get_outsymbols (abfd);
2422 struct symbol_dictionary_record *som_symtab;
2423 int i, symtab_size;
2424
2425 /* Compute total symbol table size and allocate a chunk of memory
2426 to hold the symbol table as we build it. */
2427 symtab_size = num_syms * sizeof (struct symbol_dictionary_record);
2428 som_symtab = (struct symbol_dictionary_record *) alloca (symtab_size);
2429 bzero (som_symtab, symtab_size);
2430
2431 /* Walk over each symbol. */
2432 for (i = 0; i < num_syms; i++)
2433 {
2434 /* This is really an index into the symbol strings table.
2435 By the time we get here, the index has already been
2436 computed and stored into the name field in the BFD symbol. */
2437 som_symtab[i].name.n_strx = (int) bfd_syms[i]->name;
2438
2439 /* The HP SOM linker requires detailed type information about
2440 all symbols (including undefined symbols!). Unfortunately,
2441 the type specified in an import/export statement does not
2442 always match what the linker wants. Severe braindamage. */
2443
2444 /* Section symbols will not have a SOM symbol type assigned to
2445 them yet. Assign all section symbols type ST_DATA. */
2446 if (bfd_syms[i]->flags & BSF_SECTION_SYM)
2447 som_symtab[i].symbol_type = ST_DATA;
2448 else
2449 {
2450 /* Common symbols must have scope SS_UNSAT and type
2451 ST_STORAGE or the linker will choke. */
2452 if (bfd_syms[i]->section == &bfd_com_section)
2453 {
2454 som_symtab[i].symbol_scope = SS_UNSAT;
2455 som_symtab[i].symbol_type = ST_STORAGE;
2456 }
2457
2458 /* It is possible to have a symbol without an associated
2459 type. This happens if the user imported the symbol
2460 without a type and the symbol was never defined
2461 locally. If BSF_FUNCTION is set for this symbol, then
2462 assign it type ST_CODE (the HP linker requires undefined
2463 external functions to have type ST_CODE rather than ST_ENTRY. */
2464 else if (((*som_symbol_data (bfd_syms[i]))->som_type
2465 == SYMBOL_TYPE_UNKNOWN)
2466 && (bfd_syms[i]->section == &bfd_und_section)
2467 && (bfd_syms[i]->flags & BSF_FUNCTION))
2468 som_symtab[i].symbol_type = ST_CODE;
2469
2470 /* Handle function symbols which were defined in this file.
2471 They should have type ST_ENTRY. Also retrieve the argument
2472 relocation bits from the SOM backend information. */
2473 else if (((*som_symbol_data (bfd_syms[i]))->som_type
2474 == SYMBOL_TYPE_ENTRY)
2475 || (((*som_symbol_data (bfd_syms[i]))->som_type
2476 == SYMBOL_TYPE_CODE)
2477 && (bfd_syms[i]->flags & BSF_FUNCTION))
2478 || (((*som_symbol_data (bfd_syms[i]))->som_type
2479 == SYMBOL_TYPE_UNKNOWN)
2480 && (bfd_syms[i]->flags & BSF_FUNCTION)))
2481 {
2482 som_symtab[i].symbol_type = ST_ENTRY;
2483 som_symtab[i].arg_reloc
2484 = (*som_symbol_data (bfd_syms[i]))->tc_data.hppa_arg_reloc;
2485 }
2486
2487 /* If the type is unknown at this point, it should be
2488 ST_DATA (functions were handled as special cases above). */
2489 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2490 == SYMBOL_TYPE_UNKNOWN)
2491 som_symtab[i].symbol_type = ST_DATA;
2492
2493 /* From now on it's a very simple mapping. */
2494 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2495 == SYMBOL_TYPE_ABSOLUTE)
2496 som_symtab[i].symbol_type = ST_ABSOLUTE;
2497 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2498 == SYMBOL_TYPE_CODE)
2499 som_symtab[i].symbol_type = ST_CODE;
2500 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2501 == SYMBOL_TYPE_DATA)
2502 som_symtab[i].symbol_type = ST_DATA;
2503 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2504 == SYMBOL_TYPE_MILLICODE)
2505 som_symtab[i].symbol_type = ST_MILLICODE;
2506 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2507 == SYMBOL_TYPE_PLABEL)
2508 som_symtab[i].symbol_type = ST_PLABEL;
2509 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2510 == SYMBOL_TYPE_PRI_PROG)
2511 som_symtab[i].symbol_type = ST_PRI_PROG;
2512 else if ((*som_symbol_data (bfd_syms[i]))->som_type
2513 == SYMBOL_TYPE_SEC_PROG)
2514 som_symtab[i].symbol_type = ST_SEC_PROG;
2515 }
2516
2517 /* Now handle the symbol's scope. Exported data which is not
2518 in the common section has scope SS_UNIVERSAL. Note scope
2519 of common symbols was handled earlier! */
2520 if (bfd_syms[i]->flags & BSF_EXPORT
2521 && bfd_syms[i]->section != &bfd_com_section)
2522 som_symtab[i].symbol_scope = SS_UNIVERSAL;
2523 /* Any undefined symbol at this point has a scope SS_UNSAT. */
2524 else if (bfd_syms[i]->section == &bfd_und_section)
2525 som_symtab[i].symbol_scope = SS_UNSAT;
2526 /* Anything else which is not in the common section has scope
2527 SS_LOCAL. */
2528 else if (bfd_syms[i]->section != &bfd_com_section)
2529 som_symtab[i].symbol_scope = SS_LOCAL;
2530
2531 /* Now set the symbol_info field. It has no real meaning
2532 for undefined or common symbols, but the HP linker will
2533 choke if it's not set to some "reasonable" value. We
2534 use zero as a reasonable value. */
2535 if (bfd_syms[i]->section == &bfd_com_section
2536 || bfd_syms[i]->section == &bfd_und_section)
2537 som_symtab[i].symbol_info = 0;
2538 /* For all other symbols, the symbol_info field contains the
2539 subspace index of the space this symbol is contained in. */
2540 else
2541 som_symtab[i].symbol_info
2542 = som_section_data (bfd_syms[i]->section)->subspace_index;
2543
2544 /* Set the symbol's value. */
2545 som_symtab[i].symbol_value
2546 = bfd_syms[i]->value + bfd_syms[i]->section->vma;
2547 }
2548
2549 /* Egad. Everything is ready, seek to the right location and
2550 scribble out the symbol table. */
2551 if (bfd_seek (abfd, symtab_location, SEEK_SET) != 0)
2552 {
2553 bfd_error = system_call_error;
2554 return false;
2555 }
2556
2557 if (bfd_write ((PTR) som_symtab, symtab_size, 1, abfd) != symtab_size)
2558 {
2559 bfd_error = system_call_error;
2560 return false;
2561 }
2562 return true;
2563 }
2564
2565 /* Write an object in SOM format. */
2566
2567 static boolean
2568 som_write_object_contents (abfd)
2569 bfd *abfd;
2570 {
2571 if (abfd->output_has_begun == false)
2572 {
2573 /* Set up fixed parts of the file, space, and subspace headers.
2574 Notify the world that output has begun. */
2575 som_prep_headers (abfd);
2576 abfd->output_has_begun = true;
2577 /* Start writing the object file. This include all the string
2578 tables, fixup streams, and other portions of the object file. */
2579 som_begin_writing (abfd);
2580 }
2581
2582 /* Now that the symbol table information is complete, build and
2583 write the symbol table. */
2584 if (som_build_and_write_symbol_table (abfd) == false)
2585 return false;
2586
2587 /* Compute the checksum for the file header just before writing
2588 the header to disk. */
2589 obj_som_file_hdr (abfd)->checksum = som_compute_checksum (abfd);
2590 return (som_write_headers (abfd));
2591 }
2592
2593 \f
2594 /* Read and save the string table associated with the given BFD. */
2595
2596 static boolean
2597 som_slurp_string_table (abfd)
2598 bfd *abfd;
2599 {
2600 char *stringtab;
2601
2602 /* Use the saved version if its available. */
2603 if (obj_som_stringtab (abfd) != NULL)
2604 return true;
2605
2606 /* Allocate and read in the string table. */
2607 stringtab = bfd_zalloc (abfd, obj_som_stringtab_size (abfd));
2608 if (stringtab == NULL)
2609 {
2610 bfd_error = no_memory;
2611 return false;
2612 }
2613
2614 if (bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET) < 0)
2615 {
2616 bfd_error = system_call_error;
2617 return false;
2618 }
2619
2620 if (bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd)
2621 != obj_som_stringtab_size (abfd))
2622 {
2623 bfd_error = system_call_error;
2624 return false;
2625 }
2626
2627 /* Save our results and return success. */
2628 obj_som_stringtab (abfd) = stringtab;
2629 return true;
2630 }
2631
2632 /* Return the amount of data (in bytes) required to hold the symbol
2633 table for this object. */
2634
2635 static unsigned int
2636 som_get_symtab_upper_bound (abfd)
2637 bfd *abfd;
2638 {
2639 if (!som_slurp_symbol_table (abfd))
2640 return 0;
2641
2642 return (bfd_get_symcount (abfd) + 1) * (sizeof (som_symbol_type *));
2643 }
2644
2645 /* Convert from a SOM subspace index to a BFD section. */
2646
2647 static asection *
2648 som_section_from_subspace_index (abfd, index)
2649 bfd *abfd;
2650 unsigned int index;
2651 {
2652 asection *section;
2653
2654 for (section = abfd->sections; section != NULL; section = section->next)
2655 if (som_section_data (section)->subspace_index == index)
2656 return section;
2657
2658 /* Should never happen. */
2659 abort();
2660 }
2661
2662 /* Read and save the symbol table associated with the given BFD. */
2663
2664 static unsigned int
2665 som_slurp_symbol_table (abfd)
2666 bfd *abfd;
2667 {
2668 int symbol_count = bfd_get_symcount (abfd);
2669 int symsize = sizeof (struct symbol_dictionary_record);
2670 char *stringtab;
2671 struct symbol_dictionary_record *buf, *bufp, *endbufp;
2672 som_symbol_type *sym, *symbase;
2673
2674 /* Return saved value if it exists. */
2675 if (obj_som_symtab (abfd) != NULL)
2676 return true;
2677
2678 /* Sanity checking. Make sure there are some symbols and that
2679 we can read the string table too. */
2680 if (symbol_count == 0)
2681 {
2682 bfd_error = no_symbols;
2683 return false;
2684 }
2685
2686 if (!som_slurp_string_table (abfd))
2687 return false;
2688
2689 stringtab = obj_som_stringtab (abfd);
2690
2691 symbase = (som_symbol_type *)
2692 bfd_zalloc (abfd, symbol_count * sizeof (som_symbol_type));
2693 if (symbase == NULL)
2694 {
2695 bfd_error = no_memory;
2696 return false;
2697 }
2698
2699 /* Read in the external SOM representation. */
2700 buf = alloca (symbol_count * symsize);
2701 if (buf == NULL)
2702 {
2703 bfd_error = no_memory;
2704 return false;
2705 }
2706 if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) < 0)
2707 {
2708 bfd_error = system_call_error;
2709 return false;
2710 }
2711 if (bfd_read (buf, symbol_count * symsize, 1, abfd)
2712 != symbol_count * symsize)
2713 {
2714 bfd_error = no_symbols;
2715 return (false);
2716 }
2717
2718 /* Iterate over all the symbols and internalize them. */
2719 endbufp = buf + symbol_count;
2720 for (bufp = buf, sym = symbase; bufp < endbufp; ++bufp)
2721 {
2722
2723 /* I don't think we care about these. */
2724 if (bufp->symbol_type == ST_SYM_EXT
2725 || bufp->symbol_type == ST_ARG_EXT)
2726 continue;
2727
2728 /* Some reasonable defaults. */
2729 sym->symbol.the_bfd = abfd;
2730 sym->symbol.name = bufp->name.n_strx + stringtab;
2731 sym->symbol.value = bufp->symbol_value;
2732 sym->symbol.section = 0;
2733 sym->symbol.flags = 0;
2734
2735 switch (bufp->symbol_type)
2736 {
2737 case ST_ENTRY:
2738 sym->symbol.flags |= BSF_FUNCTION;
2739 sym->symbol.value &= ~0x3;
2740 break;
2741
2742 case ST_PRI_PROG:
2743 case ST_SEC_PROG:
2744 case ST_STUB:
2745 case ST_MILLICODE:
2746 case ST_CODE:
2747 sym->symbol.value &= ~0x3;
2748
2749 default:
2750 break;
2751 }
2752
2753 /* Handle scoping and section information. */
2754 switch (bufp->symbol_scope)
2755 {
2756 /* symbol_info field is undefined for SS_EXTERNAL and SS_UNSAT symbols,
2757 so the section associated with this symbol can't be known. */
2758 case SS_EXTERNAL:
2759 case SS_UNSAT:
2760 sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
2761 break;
2762
2763 case SS_UNIVERSAL:
2764 sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
2765 sym->symbol.section
2766 = som_section_from_subspace_index (abfd, bufp->symbol_info);
2767 sym->symbol.value -= sym->symbol.section->vma;
2768 break;
2769
2770 #if 0
2771 /* SS_GLOBAL and SS_LOCAL are two names for the same thing.
2772 Sound dumb? It is. */
2773 case SS_GLOBAL:
2774 #endif
2775 case SS_LOCAL:
2776 sym->symbol.flags |= BSF_LOCAL;
2777 sym->symbol.section
2778 = som_section_from_subspace_index (abfd, bufp->symbol_info);
2779 sym->symbol.value -= sym->symbol.section->vma;
2780 break;
2781 }
2782
2783 /* Mark symbols left around by the debugger. */
2784 if (strlen (sym->symbol.name) >= 3
2785 && sym->symbol.name[0] == 'L'
2786 && (sym->symbol.name[2] == '$' || sym->symbol.name[3] == '$'))
2787 sym->symbol.flags |= BSF_DEBUGGING;
2788
2789 /* Note increment at bottom of loop, since we skip some symbols
2790 we can not include it as part of the for statement. */
2791 sym++;
2792 }
2793
2794 /* Save our results and return success. */
2795 obj_som_symtab (abfd) = symbase;
2796 return (true);
2797 }
2798
2799 /* Canonicalize a SOM symbol table. Return the number of entries
2800 in the symbol table. */
2801
2802 static unsigned int
2803 som_get_symtab (abfd, location)
2804 bfd *abfd;
2805 asymbol **location;
2806 {
2807 int i;
2808 som_symbol_type *symbase;
2809
2810 if (!som_slurp_symbol_table (abfd))
2811 return 0;
2812
2813 i = bfd_get_symcount (abfd);
2814 symbase = obj_som_symtab (abfd);
2815
2816 for (; i > 0; i--, location++, symbase++)
2817 *location = &symbase->symbol;
2818
2819 /* Final null pointer. */
2820 *location = 0;
2821 return (bfd_get_symcount (abfd));
2822 }
2823
2824 /* Make a SOM symbol. There is nothing special to do here. */
2825
2826 static asymbol *
2827 som_make_empty_symbol (abfd)
2828 bfd *abfd;
2829 {
2830 som_symbol_type *new =
2831 (som_symbol_type *) bfd_zalloc (abfd, sizeof (som_symbol_type));
2832 if (new == NULL)
2833 {
2834 bfd_error = no_memory;
2835 return 0;
2836 }
2837 new->symbol.the_bfd = abfd;
2838
2839 return &new->symbol;
2840 }
2841
2842 /* Print symbol information. */
2843
2844 static void
2845 som_print_symbol (ignore_abfd, afile, symbol, how)
2846 bfd *ignore_abfd;
2847 PTR afile;
2848 asymbol *symbol;
2849 bfd_print_symbol_type how;
2850 {
2851 FILE *file = (FILE *) afile;
2852 switch (how)
2853 {
2854 case bfd_print_symbol_name:
2855 fprintf (file, "%s", symbol->name);
2856 break;
2857 case bfd_print_symbol_more:
2858 fprintf (file, "som ");
2859 fprintf_vma (file, symbol->value);
2860 fprintf (file, " %lx", (long) symbol->flags);
2861 break;
2862 case bfd_print_symbol_all:
2863 {
2864 CONST char *section_name;
2865 section_name = symbol->section ? symbol->section->name : "(*none*)";
2866 bfd_print_symbol_vandf ((PTR) file, symbol);
2867 fprintf (file, " %s\t%s", section_name, symbol->name);
2868 break;
2869 }
2870 }
2871 }
2872
2873 static unsigned int
2874 som_get_reloc_upper_bound (abfd, asect)
2875 bfd *abfd;
2876 sec_ptr asect;
2877 {
2878 fprintf (stderr, "som_get_reloc_upper_bound unimplemented\n");
2879 fflush (stderr);
2880 abort ();
2881 return (0);
2882 }
2883
2884 static unsigned int
2885 som_canonicalize_reloc (abfd, section, relptr, symbols)
2886 bfd *abfd;
2887 sec_ptr section;
2888 arelent **relptr;
2889 asymbol **symbols;
2890 {
2891 fprintf (stderr, "som_canonicalize_reloc unimplemented\n");
2892 fflush (stderr);
2893 abort ();
2894 }
2895
2896 extern bfd_target som_vec;
2897
2898 /* A hook to set up object file dependent section information. */
2899
2900 static boolean
2901 som_new_section_hook (abfd, newsect)
2902 bfd *abfd;
2903 asection *newsect;
2904 {
2905 newsect->used_by_bfd = (struct som_section_data_struct *)
2906 bfd_zalloc (abfd, sizeof (struct som_section_data_struct));
2907 newsect->alignment_power = 3;
2908
2909 /* Initialize the subspace_index field to -1 so that it does
2910 not match a subspace with an index of 0. */
2911 som_section_data (newsect)->subspace_index = -1;
2912
2913 /* We allow more than three sections internally */
2914 return true;
2915 }
2916
2917 /* Set backend info for sections which can not be described
2918 in the BFD data structures. */
2919
2920 void
2921 bfd_som_set_section_attributes (section, defined, private, sort_key, spnum)
2922 asection *section;
2923 char defined;
2924 char private;
2925 unsigned char sort_key;
2926 int spnum;
2927 {
2928 struct space_dictionary_record *space_dict;
2929
2930 som_section_data (section)->is_space = 1;
2931 space_dict = &som_section_data (section)->space_dict;
2932 space_dict->is_defined = defined;
2933 space_dict->is_private = private;
2934 space_dict->sort_key = sort_key;
2935 space_dict->space_number = spnum;
2936 }
2937
2938 /* Set backend info for subsections which can not be described
2939 in the BFD data structures. */
2940
2941 void
2942 bfd_som_set_subsection_attributes (section, container, access,
2943 sort_key, quadrant)
2944 asection *section;
2945 asection *container;
2946 int access;
2947 unsigned char sort_key;
2948 int quadrant;
2949 {
2950 struct subspace_dictionary_record *subspace_dict;
2951 som_section_data (section)->is_subspace = 1;
2952 subspace_dict = &som_section_data (section)->subspace_dict;
2953 subspace_dict->access_control_bits = access;
2954 subspace_dict->sort_key = sort_key;
2955 subspace_dict->quadrant = quadrant;
2956 som_section_data (section)->containing_space = container;
2957 }
2958
2959 /* Set the full SOM symbol type. SOM needs far more symbol information
2960 than any other object file format I'm aware of. It is mandatory
2961 to be able to know if a symbol is an entry point, millicode, data,
2962 code, absolute, storage request, or procedure label. If you get
2963 the symbol type wrong your program will not link. */
2964
2965 void
2966 bfd_som_set_symbol_type (symbol, type)
2967 asymbol *symbol;
2968 unsigned int type;
2969 {
2970 (*som_symbol_data (symbol))->som_type = type;
2971 }
2972
2973 /* Attach 64bits of unwind information to a symbol (which hopefully
2974 is a function of some kind!). It would be better to keep this
2975 in the R_ENTRY relocation, but there is not enough space. */
2976
2977 void
2978 bfd_som_attach_unwind_info (symbol, unwind_desc)
2979 asymbol *symbol;
2980 char *unwind_desc;
2981 {
2982 (*som_symbol_data (symbol))->unwind = unwind_desc;
2983 }
2984
2985 static boolean
2986 som_set_section_contents (abfd, section, location, offset, count)
2987 bfd *abfd;
2988 sec_ptr section;
2989 PTR location;
2990 file_ptr offset;
2991 bfd_size_type count;
2992 {
2993 if (abfd->output_has_begun == false)
2994 {
2995 /* Set up fixed parts of the file, space, and subspace headers.
2996 Notify the world that output has begun. */
2997 som_prep_headers (abfd);
2998 abfd->output_has_begun = true;
2999 /* Start writing the object file. This include all the string
3000 tables, fixup streams, and other portions of the object file. */
3001 som_begin_writing (abfd);
3002 }
3003
3004 /* Only write subspaces which have "real" contents (eg. the contents
3005 are not generated at run time by the OS). */
3006 if (som_section_data (section)->is_subspace != 1
3007 || ((section->flags & (SEC_LOAD | SEC_DEBUGGING)) == 0))
3008 return true;
3009
3010 /* Seek to the proper offset within the object file and write the
3011 data. */
3012 offset += som_section_data (section)->subspace_dict.file_loc_init_value;
3013 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
3014 {
3015 bfd_error = system_call_error;
3016 return false;
3017 }
3018
3019 if (bfd_write ((PTR) location, 1, count, abfd) != count)
3020 {
3021 bfd_error = system_call_error;
3022 return false;
3023 }
3024 return true;
3025 }
3026
3027 static boolean
3028 som_set_arch_mach (abfd, arch, machine)
3029 bfd *abfd;
3030 enum bfd_architecture arch;
3031 unsigned long machine;
3032 {
3033 /* Allow any architecture to be supported by the SOM backend */
3034 return bfd_default_set_arch_mach (abfd, arch, machine);
3035 }
3036
3037 static boolean
3038 som_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3039 functionname_ptr, line_ptr)
3040 bfd *abfd;
3041 asection *section;
3042 asymbol **symbols;
3043 bfd_vma offset;
3044 CONST char **filename_ptr;
3045 CONST char **functionname_ptr;
3046 unsigned int *line_ptr;
3047 {
3048 fprintf (stderr, "som_find_nearest_line unimplemented\n");
3049 fflush (stderr);
3050 abort ();
3051 return (false);
3052 }
3053
3054 static int
3055 som_sizeof_headers (abfd, reloc)
3056 bfd *abfd;
3057 boolean reloc;
3058 {
3059 fprintf (stderr, "som_sizeof_headers unimplemented\n");
3060 fflush (stderr);
3061 abort ();
3062 return (0);
3063 }
3064
3065 /* Return information about SOM symbol SYMBOL in RET. */
3066
3067 static void
3068 som_get_symbol_info (ignore_abfd, symbol, ret)
3069 bfd *ignore_abfd; /* Ignored. */
3070 asymbol *symbol;
3071 symbol_info *ret;
3072 {
3073 bfd_symbol_info (symbol, ret);
3074 }
3075
3076 /* End of miscellaneous support functions. */
3077
3078 #define som_bfd_debug_info_start bfd_void
3079 #define som_bfd_debug_info_end bfd_void
3080 #define som_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
3081
3082 #define som_openr_next_archived_file bfd_generic_openr_next_archived_file
3083 #define som_generic_stat_arch_elt bfd_generic_stat_arch_elt
3084 #define som_slurp_armap bfd_false
3085 #define som_slurp_extended_name_table _bfd_slurp_extended_name_table
3086 #define som_truncate_arname (void (*)())bfd_nullvoidptr
3087 #define som_write_armap 0
3088
3089 #define som_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
3090 #define som_close_and_cleanup bfd_generic_close_and_cleanup
3091 #define som_get_section_contents bfd_generic_get_section_contents
3092
3093 #define som_bfd_get_relocated_section_contents \
3094 bfd_generic_get_relocated_section_contents
3095 #define som_bfd_relax_section bfd_generic_relax_section
3096 #define som_bfd_seclet_link bfd_generic_seclet_link
3097 #define som_bfd_reloc_type_lookup \
3098 ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
3099 #define som_bfd_make_debug_symbol \
3100 ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
3101
3102 /* Core file support is in the hpux-core backend. */
3103 #define som_core_file_failing_command _bfd_dummy_core_file_failing_command
3104 #define som_core_file_failing_signal _bfd_dummy_core_file_failing_signal
3105 #define som_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
3106
3107 bfd_target som_vec =
3108 {
3109 "som", /* name */
3110 bfd_target_som_flavour,
3111 true, /* target byte order */
3112 true, /* target headers byte order */
3113 (HAS_RELOC | EXEC_P | /* object flags */
3114 HAS_LINENO | HAS_DEBUG |
3115 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3116 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
3117 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3118
3119 /* leading_symbol_char: is the first char of a user symbol
3120 predictable, and if so what is it */
3121 0,
3122 ' ', /* ar_pad_char */
3123 16, /* ar_max_namelen */
3124 3, /* minimum alignment */
3125 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3126 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3127 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3128 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3129 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3130 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3131 {_bfd_dummy_target,
3132 som_object_p, /* bfd_check_format */
3133 bfd_generic_archive_p,
3134 _bfd_dummy_target
3135 },
3136 {
3137 bfd_false,
3138 som_mkobject,
3139 _bfd_generic_mkarchive,
3140 bfd_false
3141 },
3142 {
3143 bfd_false,
3144 som_write_object_contents,
3145 _bfd_write_archive_contents,
3146 bfd_false,
3147 },
3148 #undef som
3149 JUMP_TABLE (som),
3150 (PTR) 0
3151 };
3152
3153 #endif /* HOST_HPPAHPUX || HOST_HPPABSD */
This page took 0.123377 seconds and 5 git commands to generate.