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