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