* som.[ch]: Do not include libhppa.h in som.c, instead include
[external/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 (pa-gdb-bugs@cs.utah.edu).
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25
26 #if defined (HOST_HPPAHPUX) || defined (HOST_HPPABSD) || defined (HOST_HPPAOSF)
27
28 #include "libbfd.h"
29 #include "som.h"
30
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/dir.h>
35 #include <signal.h>
36 #include <machine/reg.h>
37 #include <sys/user.h>           /* After a.out.h  */
38 #include <sys/file.h>
39 #include <errno.h>
40
41 /* Magic not defined in standard HP-UX header files until 8.0 */
42
43 #ifndef CPU_PA_RISC1_0
44 #define CPU_PA_RISC1_0 0x20B
45 #endif /* CPU_PA_RISC1_0 */
46
47 #ifndef CPU_PA_RISC1_1
48 #define CPU_PA_RISC1_1 0x210
49 #endif /* CPU_PA_RISC1_1 */
50
51 #ifndef _PA_RISC1_0_ID
52 #define _PA_RISC1_0_ID CPU_PA_RISC1_0
53 #endif /* _PA_RISC1_0_ID */
54
55 #ifndef _PA_RISC1_1_ID
56 #define _PA_RISC1_1_ID CPU_PA_RISC1_1
57 #endif /* _PA_RISC1_1_ID */
58
59 #ifndef _PA_RISC_MAXID
60 #define _PA_RISC_MAXID  0x2FF
61 #endif /* _PA_RISC_MAXID */
62
63 #ifndef _PA_RISC_ID
64 #define _PA_RISC_ID(__m_num)            \
65     (((__m_num) == _PA_RISC1_0_ID) ||   \
66      ((__m_num) >= _PA_RISC1_1_ID && (__m_num) <= _PA_RISC_MAXID))
67 #endif /* _PA_RISC_ID */
68
69 /* Size (in chars) of the temporary buffers used during fixup and string
70    table writes.   */
71    
72 #define SOM_TMP_BUFSIZE 8192
73
74 /* Size of the hash table in archives.  */
75 #define SOM_LST_HASH_SIZE 31
76
77 /* Max number of SOMs to be found in an archive.  */
78 #define SOM_LST_MODULE_LIMIT 1024
79
80 /* SOM allows any one of the four previous relocations to be reused
81    with a "R_PREV_FIXUP" relocation entry.  Since R_PREV_FIXUP
82    relocations are always a single byte, using a R_PREV_FIXUP instead
83    of some multi-byte relocation makes object files smaller. 
84
85    Note one side effect of using a R_PREV_FIXUP is the relocation that
86    is being repeated moves to the front of the queue.  */
87 struct reloc_queue
88   {
89     unsigned char *reloc;
90     unsigned int size;
91   } reloc_queue[4];
92
93 /* This fully describes the symbol types which may be attached to
94    an EXPORT or IMPORT directive.  Only SOM uses this formation
95    (ELF has no need for it).  */
96 typedef enum
97 {
98   SYMBOL_TYPE_UNKNOWN,
99   SYMBOL_TYPE_ABSOLUTE,
100   SYMBOL_TYPE_CODE,
101   SYMBOL_TYPE_DATA,
102   SYMBOL_TYPE_ENTRY,
103   SYMBOL_TYPE_MILLICODE,
104   SYMBOL_TYPE_PLABEL,
105   SYMBOL_TYPE_PRI_PROG,
106   SYMBOL_TYPE_SEC_PROG,
107 } pa_symbol_type;
108
109 struct section_to_type
110 {
111   char *section;
112   char type;
113 };
114
115 /* Assorted symbol information that needs to be derived from the BFD symbol
116    and/or the BFD backend private symbol data.  */
117 struct som_misc_symbol_info
118 {
119   unsigned int symbol_type;
120   unsigned int symbol_scope;
121   unsigned int arg_reloc;
122   unsigned int symbol_info;
123   unsigned int symbol_value;
124 };
125
126 /* Forward declarations */
127
128 static boolean som_mkobject PARAMS ((bfd *));
129 static bfd_target * som_object_setup PARAMS ((bfd *,
130                                               struct header *,
131                                               struct som_exec_auxhdr *));
132 static asection * make_unique_section PARAMS ((bfd *, CONST char *, int));
133 static boolean setup_sections PARAMS ((bfd *, struct header *));
134 static bfd_target * som_object_p PARAMS ((bfd *));
135 static boolean som_write_object_contents PARAMS ((bfd *));
136 static boolean som_slurp_string_table PARAMS ((bfd *));
137 static unsigned int som_slurp_symbol_table PARAMS ((bfd *));
138 static unsigned int som_get_symtab_upper_bound PARAMS ((bfd *));
139 static unsigned int som_canonicalize_reloc PARAMS ((bfd *, sec_ptr,
140                                                     arelent **, asymbol **));
141 static unsigned int som_get_reloc_upper_bound PARAMS ((bfd *, sec_ptr));
142 static unsigned int som_set_reloc_info PARAMS ((unsigned char *, unsigned int,
143                                                 arelent *, asection *,
144                                                 asymbol **, boolean));
145 static boolean som_slurp_reloc_table PARAMS ((bfd *, asection *,
146                                               asymbol **, boolean));
147 static unsigned int som_get_symtab PARAMS ((bfd *, asymbol **));
148 static asymbol * som_make_empty_symbol PARAMS ((bfd *));
149 static void som_print_symbol PARAMS ((bfd *, PTR,
150                                       asymbol *, bfd_print_symbol_type));
151 static boolean som_new_section_hook PARAMS ((bfd *, asection *));
152 static boolean som_set_section_contents PARAMS ((bfd *, sec_ptr, PTR,
153                                                  file_ptr, bfd_size_type));
154 static boolean som_set_arch_mach PARAMS ((bfd *, enum bfd_architecture,
155                                           unsigned long));
156 static boolean som_find_nearest_line PARAMS ((bfd *, asection *,
157                                               asymbol **, bfd_vma,
158                                               CONST char **,
159                                               CONST char **,
160                                               unsigned int *));
161 static void som_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
162 static asection * som_section_from_subspace_index PARAMS ((bfd *, 
163                                                            unsigned int));
164 static int log2 PARAMS ((unsigned int));
165 static bfd_reloc_status_type hppa_som_reloc PARAMS ((bfd *, arelent *,
166                                                      asymbol *, PTR,
167                                                      asection *, bfd *,
168                                                      char **));
169 static void som_initialize_reloc_queue PARAMS ((struct reloc_queue *));
170 static void som_reloc_queue_insert PARAMS ((unsigned char *, unsigned int,
171                                             struct reloc_queue *));
172 static void som_reloc_queue_fix PARAMS ((struct reloc_queue *, unsigned int));
173 static int som_reloc_queue_find PARAMS ((unsigned char *, unsigned int,
174                                          struct reloc_queue *));
175 static unsigned char * try_prev_fixup PARAMS ((bfd *, int *, unsigned char *,
176                                                unsigned int,
177                                                struct reloc_queue *));
178
179 static unsigned char * som_reloc_skip PARAMS ((bfd *, unsigned int,
180                                                unsigned char *, unsigned int *,
181                                                struct reloc_queue *));
182 static unsigned char * som_reloc_addend PARAMS ((bfd *, int, unsigned char *,
183                                                  unsigned int *,
184                                                  struct reloc_queue *));
185 static unsigned char * som_reloc_call PARAMS ((bfd *, unsigned char *,
186                                                unsigned int *,
187                                                arelent *, int,
188                                                struct reloc_queue *));
189 static unsigned long som_count_spaces PARAMS ((bfd *));
190 static unsigned long som_count_subspaces PARAMS ((bfd *));
191 static int compare_syms PARAMS ((asymbol **, asymbol **));
192 static unsigned long som_compute_checksum PARAMS ((bfd *));
193 static boolean som_prep_headers PARAMS ((bfd *));
194 static int som_sizeof_headers PARAMS ((bfd *, boolean));
195 static boolean som_write_headers PARAMS ((bfd *));
196 static boolean som_build_and_write_symbol_table PARAMS ((bfd *));
197 static void som_prep_for_fixups PARAMS ((bfd *, asymbol **, unsigned long));
198 static boolean som_write_fixups PARAMS ((bfd *, unsigned long, unsigned int *));
199 static boolean som_write_space_strings PARAMS ((bfd *, unsigned long,
200                                                 unsigned int *));
201 static boolean som_write_symbol_strings PARAMS ((bfd *, unsigned long,
202                                                  asymbol **, unsigned int,
203                                                  unsigned *));
204 static boolean som_begin_writing PARAMS ((bfd *));
205 static const reloc_howto_type * som_bfd_reloc_type_lookup
206         PARAMS ((bfd_arch_info_type *, bfd_reloc_code_real_type));
207 static char som_section_type PARAMS ((const char *));
208 static int som_decode_symclass PARAMS ((asymbol *));
209 static boolean som_bfd_count_ar_symbols PARAMS ((bfd *, struct lst_header *,
210                                                  symindex *));
211
212 static boolean som_bfd_fill_in_ar_symbols PARAMS ((bfd *, struct lst_header *,
213                                                    carsym **syms));
214 static boolean som_slurp_armap PARAMS ((bfd *));
215 static boolean som_write_armap PARAMS ((bfd *));
216 static boolean som_slurp_extended_name_table PARAMS ((bfd *));
217 static void som_bfd_derive_misc_symbol_info PARAMS ((bfd *, asymbol *,
218                                              struct som_misc_symbol_info *));
219 static boolean som_bfd_prep_for_ar_write PARAMS ((bfd *, unsigned int *,
220                                                   unsigned int *));
221 static unsigned int som_bfd_ar_symbol_hash PARAMS ((asymbol *));
222 static boolean som_bfd_ar_write_symbol_stuff PARAMS ((bfd *, unsigned int,
223                                                       unsigned int,
224                                                       struct lst_header));
225         
226 /* Map SOM section names to POSIX/BSD single-character symbol types.
227
228    This table includes all the standard subspaces as defined in the 
229    current "PRO ABI for PA-RISC Systems", $UNWIND$ which for 
230    some reason was left out, and sections specific to embedded stabs.  */
231
232 static const struct section_to_type stt[] = {
233   {"$TEXT$", 't'},
234   {"$SHLIB_INFO$", 't'},
235   {"$MILLICODE$", 't'},
236   {"$LIT$", 't'},
237   {"$CODE$", 't'},
238   {"$UNWIND_START$", 't'},
239   {"$UNWIND$", 't'},
240   {"$PRIVATE$", 'd'},
241   {"$PLT$", 'd'},
242   {"$SHLIB_DATA$", 'd'},
243   {"$DATA$", 'd'},
244   {"$SHORTDATA$", 'g'},
245   {"$DLT$", 'd'},
246   {"$GLOBAL$", 'g'},
247   {"$SHORTBSS$", 's'},
248   {"$BSS$", 'b'},
249   {"$GDB_STRINGS$", 'N'},
250   {"$GDB_SYMBOLS$", 'N'},
251   {0, 0}
252 };
253
254 /* About the relocation formatting table...
255
256    There are 256 entries in the table, one for each possible
257    relocation opcode available in SOM.  We index the table by
258    the relocation opcode.  The names and operations are those
259    defined by a.out_800 (4).
260
261    Right now this table is only used to count and perform minimal
262    processing on relocation streams so that they can be internalized
263    into BFD and symbolically printed by utilities.  To make actual use 
264    of them would be much more difficult, BFD's concept of relocations
265    is far too simple to handle SOM relocations.  The basic assumption
266    that a relocation can be completely processed independent of other
267    relocations before an object file is written is invalid for SOM.
268
269    The SOM relocations are meant to be processed as a stream, they
270    specify copying of data from the input section to the output section
271    while possibly modifying the data in some manner.  They also can 
272    specify that a variable number of zeros or uninitialized data be
273    inserted on in the output segment at the current offset.  Some
274    relocations specify that some previous relocation be re-applied at
275    the current location in the input/output sections.  And finally a number
276    of relocations have effects on other sections (R_ENTRY, R_EXIT,
277    R_UNWIND_AUX and a variety of others).  There isn't even enough room
278    in the BFD relocation data structure to store enough information to
279    perform all the relocations.
280
281    Each entry in the table has three fields. 
282
283    The first entry is an index into this "class" of relocations.  This
284    index can then be used as a variable within the relocation itself.
285
286    The second field is a format string which actually controls processing
287    of the relocation.  It uses a simple postfix machine to do calculations
288    based on variables/constants found in the string and the relocation
289    stream.  
290
291    The third field specifys whether or not this relocation may use 
292    a constant (V) from the previous R_DATA_OVERRIDE rather than a constant
293    stored in the instruction.
294
295    Variables:  
296   
297    L = input space byte count
298    D = index into class of relocations
299    M = output space byte count
300    N = statement number (unused?)
301    O = stack operation
302    R = parameter relocation bits
303    S = symbol index
304    U = 64 bits of stack unwind and frame size info (we only keep 32 bits)
305    V = a literal constant (usually used in the next relocation)
306    P = a previous relocation
307   
308    Lower case letters (starting with 'b') refer to following 
309    bytes in the relocation stream.  'b' is the next 1 byte,
310    c is the next 2 bytes, d is the next 3 bytes, etc...  
311    This is the variable part of the relocation entries that
312    makes our life a living hell.
313
314    numerical constants are also used in the format string.  Note
315    the constants are represented in decimal. 
316
317    '+', "*" and "=" represents the obvious postfix operators.
318    '<' represents a left shift. 
319
320    Stack Operations:
321
322    Parameter Relocation Bits:
323
324    Unwind Entries:  
325    
326    Previous Relocations:  The index field represents which in the queue
327    of 4 previous fixups should be re-applied.
328
329    Literal Constants:  These are generally used to represent addend
330    parts of relocations when these constants are not stored in the
331    fields of the instructions themselves.  For example the instruction
332    addil foo-$global$-0x1234 would use an override for "0x1234" rather
333    than storing it into the addil itself.  */
334
335 struct fixup_format
336 {
337   int D;
338   char *format;
339 };
340
341 static const struct fixup_format som_fixup_formats[256] =
342 {
343   /* R_NO_RELOCATION */
344   0,   "LD1+4*=",       /* 0x00 */
345   1,   "LD1+4*=",       /* 0x01 */
346   2,   "LD1+4*=",       /* 0x02 */
347   3,   "LD1+4*=",       /* 0x03 */
348   4,   "LD1+4*=",       /* 0x04 */
349   5,   "LD1+4*=",       /* 0x05 */
350   6,   "LD1+4*=",       /* 0x06 */
351   7,   "LD1+4*=",       /* 0x07 */
352   8,   "LD1+4*=",       /* 0x08 */
353   9,   "LD1+4*=",       /* 0x09 */
354   10,  "LD1+4*=",       /* 0x0a */
355   11,  "LD1+4*=",       /* 0x0b */
356   12,  "LD1+4*=",       /* 0x0c */
357   13,  "LD1+4*=",       /* 0x0d */
358   14,  "LD1+4*=",       /* 0x0e */
359   15,  "LD1+4*=",       /* 0x0f */
360   16,  "LD1+4*=",       /* 0x10 */
361   17,  "LD1+4*=",       /* 0x11 */
362   18,  "LD1+4*=",       /* 0x12 */
363   19,  "LD1+4*=",       /* 0x13 */
364   20,  "LD1+4*=",       /* 0x14 */
365   21,  "LD1+4*=",       /* 0x15 */
366   22,  "LD1+4*=",       /* 0x16 */
367   23,  "LD1+4*=",       /* 0x17 */
368   0,   "LD8<b+1+4*=",   /* 0x18 */
369   1,   "LD8<b+1+4*=",   /* 0x19 */
370   2,   "LD8<b+1+4*=",   /* 0x1a */
371   3,   "LD8<b+1+4*=",   /* 0x1b */
372   0,   "LD16<c+1+4*=",  /* 0x1c */
373   1,   "LD16<c+1+4*=",  /* 0x1d */
374   2,   "LD16<c+1+4*=",  /* 0x1e */
375   0,   "Ld1+=",         /* 0x1f */
376   /* R_ZEROES */
377   0,    "Lb1+4*=",      /* 0x20 */
378   1,    "Ld1+=",        /* 0x21 */
379   /* R_UNINIT */
380   0,    "Lb1+4*=",      /* 0x22 */
381   1,    "Ld1+=",        /* 0x23 */
382   /* R_RELOCATION */
383   0,    "L4=",          /* 0x24 */
384   /* R_DATA_ONE_SYMBOL */
385   0,    "L4=Sb=",       /* 0x25 */
386   1,    "L4=Sd=",       /* 0x26 */
387   /* R_DATA_PLEBEL */
388   0,    "L4=Sb=",       /* 0x27 */
389   1,    "L4=Sd=",       /* 0x28 */
390   /* R_SPACE_REF */
391   0,    "L4=",          /* 0x29 */
392   /* R_REPEATED_INIT */
393   0,    "L4=Mb1+4*=",   /* 0x2a */
394   1,    "Lb4*=Mb1+L*=", /* 0x2b */
395   2,    "Lb4*=Md1+4*=", /* 0x2c */
396   3,    "Ld1+=Me1+=",   /* 0x2d */
397   /* R_RESERVED */
398   0,    "",             /* 0x2e */
399   0,    "",             /* 0x2f */
400   /* R_PCREL_CALL */
401   0,    "L4=RD=Sb=",    /* 0x30 */
402   1,    "L4=RD=Sb=",    /* 0x31 */
403   2,    "L4=RD=Sb=",    /* 0x32 */
404   3,    "L4=RD=Sb=",    /* 0x33 */
405   4,    "L4=RD=Sb=",    /* 0x34 */
406   5,    "L4=RD=Sb=",    /* 0x35 */
407   6,    "L4=RD=Sb=",    /* 0x36 */
408   7,    "L4=RD=Sb=",    /* 0x37 */
409   8,    "L4=RD=Sb=",    /* 0x38 */
410   9,    "L4=RD=Sb=",    /* 0x39 */
411   0,    "L4=RD8<b+=Sb=",/* 0x3a */
412   1,    "L4=RD8<b+=Sb=",/* 0x3b */
413   0,    "L4=RD8<b+=Sd=",/* 0x3c */
414   1,    "L4=RD8<b+=Sd=",/* 0x3d */
415   /* R_RESERVED */
416   0,    "",             /* 0x3e */
417   0,    "",             /* 0x3f */
418   /* R_ABS_CALL */
419   0,    "L4=RD=Sb=",    /* 0x40 */
420   1,    "L4=RD=Sb=",    /* 0x41 */
421   2,    "L4=RD=Sb=",    /* 0x42 */
422   3,    "L4=RD=Sb=",    /* 0x43 */
423   4,    "L4=RD=Sb=",    /* 0x44 */
424   5,    "L4=RD=Sb=",    /* 0x45 */
425   6,    "L4=RD=Sb=",    /* 0x46 */
426   7,    "L4=RD=Sb=",    /* 0x47 */
427   8,    "L4=RD=Sb=",    /* 0x48 */
428   9,    "L4=RD=Sb=",    /* 0x49 */
429   0,    "L4=RD8<b+=Sb=",/* 0x4a */
430   1,    "L4=RD8<b+=Sb=",/* 0x4b */
431   0,    "L4=RD8<b+=Sd=",/* 0x4c */
432   1,    "L4=RD8<b+=Sd=",/* 0x4d */
433   /* R_RESERVED */
434   0,     "",            /* 0x4e */
435   0,     "",            /* 0x4f */
436   /* R_DP_RELATIVE */
437   0,    "L4=SD=",       /* 0x50 */
438   1,    "L4=SD=",       /* 0x51 */
439   2,    "L4=SD=",       /* 0x52 */
440   3,    "L4=SD=",       /* 0x53 */
441   4,    "L4=SD=",       /* 0x54 */
442   5,    "L4=SD=",       /* 0x55 */
443   6,    "L4=SD=",       /* 0x56 */
444   7,    "L4=SD=",       /* 0x57 */
445   8,    "L4=SD=",       /* 0x58 */
446   9,    "L4=SD=",       /* 0x59 */
447   10,   "L4=SD=",       /* 0x5a */
448   11,   "L4=SD=",       /* 0x5b */
449   12,   "L4=SD=",       /* 0x5c */
450   13,   "L4=SD=",       /* 0x5d */
451   14,   "L4=SD=",       /* 0x5e */
452   15,   "L4=SD=",       /* 0x5f */
453   16,   "L4=SD=",       /* 0x60 */
454   17,   "L4=SD=",       /* 0x61 */
455   18,   "L4=SD=",       /* 0x62 */
456   19,   "L4=SD=",       /* 0x63 */
457   20,   "L4=SD=",       /* 0x64 */
458   21,   "L4=SD=",       /* 0x65 */
459   22,   "L4=SD=",       /* 0x66 */
460   23,   "L4=SD=",       /* 0x67 */
461   24,   "L4=SD=",       /* 0x68 */
462   25,   "L4=SD=",       /* 0x69 */
463   26,   "L4=SD=",       /* 0x6a */
464   27,   "L4=SD=",       /* 0x6b */
465   28,   "L4=SD=",       /* 0x6c */
466   29,   "L4=SD=",       /* 0x6d */
467   30,   "L4=SD=",       /* 0x6e */
468   31,   "L4=SD=",       /* 0x6f */
469   32,   "L4=Sb=",       /* 0x70 */
470   33,   "L4=Sd=",       /* 0x71 */
471   /* R_RESERVED */
472   0,    "",             /* 0x72 */
473   0,    "",             /* 0x73 */
474   0,    "",             /* 0x74 */
475   0,    "",             /* 0x75 */
476   0,    "",             /* 0x76 */
477   0,    "",             /* 0x77 */
478   /* R_DLT_REL */
479   0,    "L4=Sb=",       /* 0x78 */
480   1,    "L4=Sd=",       /* 0x79 */
481   /* R_RESERVED */
482   0,    "",             /* 0x7a */
483   0,    "",             /* 0x7b */
484   0,    "",             /* 0x7c */
485   0,    "",             /* 0x7d */
486   0,    "",             /* 0x7e */
487   0,    "",             /* 0x7f */
488   /* R_CODE_ONE_SYMBOL */
489   0,    "L4=SD=",       /* 0x80 */
490   1,    "L4=SD=",       /* 0x81 */
491   2,    "L4=SD=",       /* 0x82 */
492   3,    "L4=SD=",       /* 0x83 */
493   4,    "L4=SD=",       /* 0x84 */
494   5,    "L4=SD=",       /* 0x85 */
495   6,    "L4=SD=",       /* 0x86 */
496   7,    "L4=SD=",       /* 0x87 */
497   8,    "L4=SD=",       /* 0x88 */
498   9,    "L4=SD=",       /* 0x89 */
499   10,   "L4=SD=",       /* 0x8q */
500   11,   "L4=SD=",       /* 0x8b */
501   12,   "L4=SD=",       /* 0x8c */
502   13,   "L4=SD=",       /* 0x8d */
503   14,   "L4=SD=",       /* 0x8e */
504   15,   "L4=SD=",       /* 0x8f */
505   16,   "L4=SD=",       /* 0x90 */
506   17,   "L4=SD=",       /* 0x91 */
507   18,   "L4=SD=",       /* 0x92 */
508   19,   "L4=SD=",       /* 0x93 */
509   20,   "L4=SD=",       /* 0x94 */
510   21,   "L4=SD=",       /* 0x95 */
511   22,   "L4=SD=",       /* 0x96 */
512   23,   "L4=SD=",       /* 0x97 */
513   24,   "L4=SD=",       /* 0x98 */
514   25,   "L4=SD=",       /* 0x99 */
515   26,   "L4=SD=",       /* 0x9a */
516   27,   "L4=SD=",       /* 0x9b */
517   28,   "L4=SD=",       /* 0x9c */
518   29,   "L4=SD=",       /* 0x9d */
519   30,   "L4=SD=",       /* 0x9e */
520   31,   "L4=SD=",       /* 0x9f */
521   32,   "L4=Sb=",       /* 0xa0 */
522   33,   "L4=Sd=",       /* 0xa1 */
523   /* R_RESERVED */
524   0,    "",             /* 0xa2 */
525   0,    "",             /* 0xa3 */
526   0,    "",             /* 0xa4 */
527   0,    "",             /* 0xa5 */
528   0,    "",             /* 0xa6 */
529   0,    "",             /* 0xa7 */
530   0,    "",             /* 0xa8 */
531   0,    "",             /* 0xa9 */
532   0,    "",             /* 0xaa */
533   0,    "",             /* 0xab */
534   0,    "",             /* 0xac */
535   0,    "",             /* 0xad */
536   /* R_MILLI_REL */
537   0,    "L4=Sb=",       /* 0xae */
538   1,    "L4=Sd=",       /* 0xaf */
539   /* R_CODE_PLABEL */
540   0,    "L4=Sb=",       /* 0xb0 */
541   1,    "L4=Sd=",       /* 0xb1 */
542   /* R_BREAKPOINT */
543   0,    "L4=",          /* 0xb2 */
544   /* R_ENTRY */
545   0,    "Ui=",          /* 0xb3 */
546   1,    "Uf=",          /* 0xb4 */
547   /* R_ALT_ENTRY */
548   0,    "",             /* 0xb5 */
549   /* R_EXIT */
550   0,    "",             /* 0xb6 */
551   /* R_BEGIN_TRY */
552   0,    "",             /* 0xb7 */
553   /* R_END_TRY */
554   0,    "R0=",          /* 0xb8 */
555   1,    "Rb4*=",        /* 0xb9 */
556   2,    "Rd4*=",        /* 0xba */
557   /* R_BEGIN_BRTAB */
558   0,    "",             /* 0xbb */
559   /* R_END_BRTAB */
560   0,    "",             /* 0xbc */
561   /* R_STATEMENT */
562   0,    "Nb=",          /* 0xbd */
563   1,    "Nc=",          /* 0xbe */
564   2,    "Nd=",          /* 0xbf */
565   /* R_DATA_EXPR */
566   0,    "L4=",          /* 0xc0 */
567   /* R_CODE_EXPR */
568   0,    "L4=",          /* 0xc1 */
569   /* R_FSEL */
570   0,    "",             /* 0xc2 */
571   /* R_LSEL */
572   0,    "",             /* 0xc3 */
573   /* R_RSEL */
574   0,    "",             /* 0xc4 */
575   /* R_N_MODE */
576   0,    "",             /* 0xc5 */
577   /* R_S_MODE */
578   0,    "",             /* 0xc6 */
579   /* R_D_MODE */
580   0,    "",             /* 0xc7 */
581   /* R_R_MODE */
582   0,    "",             /* 0xc8 */
583   /* R_DATA_OVERRIDE */
584   0,    "V0=",          /* 0xc9 */
585   1,    "Vb=",          /* 0xca */
586   2,    "Vc=",          /* 0xcb */
587   3,    "Vd=",          /* 0xcc */
588   4,    "Ve=",          /* 0xcd */
589   /* R_TRANSLATED */
590   0,    "",             /* 0xce */
591   /* R_RESERVED */
592   0,    "",             /* 0xcf */
593   /* R_COMP1 */
594   0,    "Ob=",          /* 0xd0 */
595   /* R_COMP2 */
596   0,    "Ob=Sd=",       /* 0xd1 */
597   /* R_COMP3 */
598   0,    "Ob=Ve=",       /* 0xd2 */
599   /* R_PREV_FIXUP */
600   0,    "P",            /* 0xd3 */
601   1,    "P",            /* 0xd4 */
602   2,    "P",            /* 0xd5 */
603   3,    "P",            /* 0xd6 */
604   /* R_RESERVED */
605   0,    "",             /* 0xd7 */
606   0,    "",             /* 0xd8 */
607   0,    "",             /* 0xd9 */
608   0,    "",             /* 0xda */
609   0,    "",             /* 0xdb */
610   0,    "",             /* 0xdc */
611   0,    "",             /* 0xdd */
612   0,    "",             /* 0xde */
613   0,    "",             /* 0xdf */
614   0,    "",             /* 0xe0 */
615   0,    "",             /* 0xe1 */
616   0,    "",             /* 0xe2 */
617   0,    "",             /* 0xe3 */
618   0,    "",             /* 0xe4 */
619   0,    "",             /* 0xe5 */
620   0,    "",             /* 0xe6 */
621   0,    "",             /* 0xe7 */
622   0,    "",             /* 0xe8 */
623   0,    "",             /* 0xe9 */
624   0,    "",             /* 0xea */
625   0,    "",             /* 0xeb */
626   0,    "",             /* 0xec */
627   0,    "",             /* 0xed */
628   0,    "",             /* 0xee */
629   0,    "",             /* 0xef */
630   0,    "",             /* 0xf0 */
631   0,    "",             /* 0xf1 */
632   0,    "",             /* 0xf2 */
633   0,    "",             /* 0xf3 */
634   0,    "",             /* 0xf4 */
635   0,    "",             /* 0xf5 */
636   0,    "",             /* 0xf6 */
637   0,    "",             /* 0xf7 */
638   0,    "",             /* 0xf8 */
639   0,    "",             /* 0xf9 */
640   0,    "",             /* 0xfa */
641   0,    "",             /* 0xfb */
642   0,    "",             /* 0xfc */
643   0,    "",             /* 0xfd */
644   0,    "",             /* 0xfe */
645   0,    "",             /* 0xff */
646 };
647
648 static const int comp1_opcodes[] =
649 {
650   0x00,
651   0x40,
652   0x41,
653   0x42,
654   0x43,
655   0x44,
656   0x45,
657   0x46,
658   0x47,
659   0x48,
660   0x49,
661   0x4a,
662   0x4b,
663   0x60,
664   0x80,
665   0xa0,
666   0xc0,
667   -1
668 };
669
670 static const int comp2_opcodes[] =
671 {
672   0x00,
673   0x80,
674   0x82,
675   0xc0,
676   -1
677 };
678
679 static const int comp3_opcodes[] =
680 {
681   0x00,
682   0x02,
683   -1
684 };
685
686 /* These apparently are not in older versions of hpux reloc.h.  */
687 #ifndef R_DLT_REL
688 #define R_DLT_REL 0x78
689 #endif
690
691 #ifndef R_AUX_UNWIND
692 #define R_AUX_UNWIND 0xcf
693 #endif
694
695 #ifndef R_SEC_STMT
696 #define R_SEC_STMT 0xd7
697 #endif
698
699 static reloc_howto_type som_hppa_howto_table[] =
700 {
701   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
702   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
703   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
704   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
705   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
706   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
707   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
708   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
709   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
710   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
711   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
712   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
713   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
714   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
715   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
716   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
717   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
718   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
719   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
720   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
721   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
722   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
723   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
724   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
725   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
726   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
727   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
728   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
729   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
730   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
731   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
732   {R_NO_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_NO_RELOCATION"},
733   {R_ZEROES, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ZEROES"},
734   {R_ZEROES, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ZEROES"},
735   {R_UNINIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_UNINIT"},
736   {R_UNINIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_UNINIT"},
737   {R_RELOCATION, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RELOCATION"},
738   {R_DATA_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_ONE_SYMBOL"},
739   {R_DATA_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_ONE_SYMBOL"},
740   {R_DATA_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_PLABEL"},
741   {R_DATA_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_PLABEL"},
742   {R_SPACE_REF, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_SPACE_REF"},
743   {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
744   {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
745   {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
746   {R_REPEATED_INIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "REPEATED_INIT"},
747   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
748   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
749   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
750   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
751   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
752   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
753   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
754   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
755   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
756   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
757   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
758   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
759   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
760   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
761   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
762   {R_PCREL_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PCREL_CALL"},
763   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
764   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
765   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
766   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
767   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
768   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
769   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
770   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
771   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
772   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
773   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
774   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
775   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
776   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
777   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
778   {R_ABS_CALL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ABS_CALL"},
779   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
780   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
781   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
782   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
783   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
784   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
785   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
786   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
787   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
788   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
789   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
790   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
791   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
792   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
793   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
794   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
795   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
796   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
797   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
798   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
799   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
800   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
801   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
802   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
803   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
804   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
805   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
806   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
807   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
808   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
809   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
810   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
811   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
812   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
813   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
814   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
815   {R_DP_RELATIVE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DP_RELATIVE"},
816   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
817   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
818   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
819   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
820   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
821   {R_DLT_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DLT_REL"},
822   {R_DLT_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DLT_REL"},
823   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
824   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
825   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
826   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
827   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
828   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
829   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
830   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
831   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
832   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
833   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
834   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
835   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
836   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
837   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
838   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
839   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
840   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
841   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
842   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
843   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
844   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
845   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
846   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
847   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
848   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
849   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
850   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
851   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
852   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
853   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
854   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
855   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
856   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
857   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
858   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
859   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
860   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
861   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
862   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
863   {R_CODE_ONE_SYMBOL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_ONE_SYMBOL"},
864   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
865   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
866   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
867   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
868   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
869   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
870   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
871   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
872   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
873   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
874   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
875   {R_MILLI_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_MILLI_REL"},
876   {R_MILLI_REL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_MILLI_REL"},
877   {R_CODE_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_PLABEL"},
878   {R_CODE_PLABEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_PLABEL"},
879   {R_BREAKPOINT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BREAKPOINT"},
880   {R_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ENTRY"},
881   {R_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ENTRY"},
882   {R_ALT_ENTRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_ALT_ENTRY"},
883   {R_EXIT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_EXIT"},
884   {R_BEGIN_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BEGIN_TRY"},
885   {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
886   {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
887   {R_END_TRY, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_TRY"},
888   {R_BEGIN_BRTAB, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_BEGIN_BRTAB"},
889   {R_END_BRTAB, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_END_BRTAB"},
890   {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
891   {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
892   {R_STATEMENT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_STATEMENT"},
893   {R_DATA_EXPR, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_EXPR"},
894   {R_CODE_EXPR, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_CODE_EXPR"},
895   {R_FSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_FSEL"},
896   {R_LSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_LSEL"},
897   {R_RSEL, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RSEL"},
898   {R_N_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_N_MODE"},
899   {R_S_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_S_MODE"},
900   {R_D_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_D_MODE"},
901   {R_R_MODE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_R_MODE"},
902   {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
903   {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
904   {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
905   {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
906   {R_DATA_OVERRIDE, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_DATA_OVERRIDE"},
907   {R_TRANSLATED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_TRANSLATED"},
908   {R_AUX_UNWIND, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_AUX_UNWIND"},
909   {R_COMP1, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP1"},
910   {R_COMP2, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP2"},
911   {R_COMP3, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_COMP3"},
912   {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
913   {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
914   {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
915   {R_PREV_FIXUP, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_PREV_FIXUP"},
916   {R_SEC_STMT, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_SEC_STMT"},
917   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
918   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
919   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
920   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
921   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
922   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
923   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
924   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
925   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
926   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
927   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
928   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
929   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
930   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
931   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
932   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
933   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
934   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
935   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
936   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
937   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
938   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
939   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
940   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
941   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
942   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
943   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
944   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
945   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
946   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
947   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
948   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
949   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
950   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
951   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
952   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
953   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
954   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
955   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"},
956   {R_RESERVED, 0, 0, 32, false, 0, 0, hppa_som_reloc, "R_RESERVED"}};
957   
958
959 /* Initialize the SOM relocation queue.  By definition the queue holds
960    the last four multibyte fixups.  */
961   
962 static void
963 som_initialize_reloc_queue (queue)
964      struct reloc_queue *queue;
965 {
966   queue[0].reloc = NULL;
967   queue[0].size = 0;
968   queue[1].reloc = NULL;
969   queue[1].size = 0;
970   queue[2].reloc = NULL;
971   queue[2].size = 0;
972   queue[3].reloc = NULL;
973   queue[3].size = 0;
974 }
975
976 /* Insert a new relocation into the relocation queue.  */
977
978 static void
979 som_reloc_queue_insert (p, size, queue)
980      unsigned char *p;
981      unsigned int size;
982      struct reloc_queue *queue;
983 {
984   queue[3].reloc = queue[2].reloc;
985   queue[3].size = queue[2].size;
986   queue[2].reloc = queue[1].reloc;
987   queue[2].size = queue[1].size;
988   queue[1].reloc = queue[0].reloc;
989   queue[1].size = queue[0].size;
990   queue[0].reloc = p;
991   queue[0].size = size;
992 }
993
994 /* When an entry in the relocation queue is reused, the entry moves
995    to the front of the queue.  */
996
997 static void
998 som_reloc_queue_fix (queue, index)
999      struct reloc_queue *queue;
1000      unsigned int index;
1001 {
1002   if (index == 0)
1003     return;
1004
1005   if (index == 1)
1006     {
1007       unsigned char *tmp1 = queue[0].reloc;
1008       unsigned int tmp2 = queue[0].size;
1009       queue[0].reloc = queue[1].reloc;
1010       queue[0].size = queue[1].size;
1011       queue[1].reloc = tmp1;
1012       queue[1].size = tmp2;
1013       return;
1014     }
1015
1016   if (index == 2)
1017     {
1018       unsigned char *tmp1 = queue[0].reloc;
1019       unsigned int tmp2 = queue[0].size;
1020       queue[0].reloc = queue[2].reloc;
1021       queue[0].size = queue[2].size;
1022       queue[2].reloc = queue[1].reloc;
1023       queue[2].size = queue[1].size;
1024       queue[1].reloc = tmp1;
1025       queue[1].size = tmp2;
1026       return;
1027     }
1028
1029   if (index == 3)
1030     {
1031       unsigned char *tmp1 = queue[0].reloc;
1032       unsigned int tmp2 = queue[0].size;
1033       queue[0].reloc = queue[3].reloc;
1034       queue[0].size = queue[3].size;
1035       queue[3].reloc = queue[2].reloc;
1036       queue[3].size = queue[2].size;
1037       queue[2].reloc = queue[1].reloc;
1038       queue[2].size = queue[1].size;
1039       queue[1].reloc = tmp1;
1040       queue[1].size = tmp2;
1041       return;
1042     }
1043   abort();
1044 }
1045
1046 /* Search for a particular relocation in the relocation queue.  */
1047
1048 static int
1049 som_reloc_queue_find (p, size, queue)
1050      unsigned char *p;
1051      unsigned int size;
1052      struct reloc_queue *queue;
1053 {
1054   if (queue[0].reloc && !bcmp (p, queue[0].reloc, size)
1055       && size == queue[0].size)
1056     return 0;
1057   if (queue[1].reloc && !bcmp (p, queue[1].reloc, size)
1058       && size == queue[1].size)
1059     return 1;
1060   if (queue[2].reloc && !bcmp (p, queue[2].reloc, size)
1061       && size == queue[2].size)
1062     return 2;
1063   if (queue[3].reloc && !bcmp (p, queue[3].reloc, size)
1064       && size == queue[3].size)
1065     return 3;
1066   return -1;
1067 }
1068
1069 static unsigned char *
1070 try_prev_fixup (abfd, subspace_reloc_sizep, p, size, queue)
1071      bfd *abfd;
1072      int *subspace_reloc_sizep;
1073      unsigned char *p;
1074      unsigned int size;
1075      struct reloc_queue *queue;
1076 {
1077   int queue_index = som_reloc_queue_find (p, size, queue);
1078
1079   if (queue_index != -1)
1080     {
1081       /* Found this in a previous fixup.  Undo the fixup we
1082          just built and use R_PREV_FIXUP instead.  We saved 
1083          a total of size - 1 bytes in the fixup stream.  */
1084       bfd_put_8 (abfd, R_PREV_FIXUP + queue_index, p);
1085       p += 1;
1086       *subspace_reloc_sizep += 1;
1087       som_reloc_queue_fix (queue, queue_index);
1088     }
1089   else
1090     {
1091       som_reloc_queue_insert (p, size, queue);
1092       *subspace_reloc_sizep += size;
1093       p += size;
1094     }
1095   return p;
1096 }
1097
1098 /* Emit the proper R_NO_RELOCATION fixups to map the next SKIP
1099    bytes without any relocation.  Update the size of the subspace
1100    relocation stream via SUBSPACE_RELOC_SIZE_P; also return the 
1101    current pointer into the relocation stream.  */
1102
1103 static unsigned char *
1104 som_reloc_skip (abfd, skip, p, subspace_reloc_sizep, queue)
1105      bfd *abfd;
1106      unsigned int skip;
1107      unsigned char *p;
1108      unsigned int *subspace_reloc_sizep;
1109      struct reloc_queue *queue;
1110 {
1111   /* Use a 4 byte R_NO_RELOCATION entry with a maximal value
1112      then R_PREV_FIXUPs to get the difference down to a
1113      reasonable size.  */
1114   if (skip >= 0x1000000)
1115     {
1116       skip -= 0x1000000;
1117       bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
1118       bfd_put_8 (abfd, 0xff, p + 1);
1119       bfd_put_16 (abfd, 0xffff, p + 2);
1120       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
1121       while (skip >= 0x1000000)
1122         {
1123           skip -= 0x1000000;
1124           bfd_put_8 (abfd, R_PREV_FIXUP, p);
1125           p++;
1126           *subspace_reloc_sizep += 1;
1127           /* No need to adjust queue here since we are repeating the
1128              most recent fixup.  */
1129         }
1130     }
1131   
1132   /* The difference must be less than 0x1000000.  Use one 
1133      more R_NO_RELOCATION entry to get to the right difference.  */
1134   if ((skip & 3) == 0 && skip <= 0xc0000 && skip > 0)
1135     {
1136       /* Difference can be handled in a simple single-byte
1137          R_NO_RELOCATION entry.  */
1138       if (skip <= 0x60)
1139         {
1140           bfd_put_8 (abfd, R_NO_RELOCATION + (skip >> 2) - 1, p);
1141           *subspace_reloc_sizep += 1;
1142           p++;
1143         }
1144       /* Handle it with a two byte R_NO_RELOCATION entry.  */
1145       else if (skip <= 0x1000)
1146         {
1147           bfd_put_8 (abfd, R_NO_RELOCATION + 24 + (((skip >> 2) - 1) >> 8), p);
1148           bfd_put_8 (abfd, (skip >> 2) - 1, p + 1);
1149           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
1150         }
1151       /* Handle it with a three byte R_NO_RELOCATION entry.  */
1152       else
1153         {
1154           bfd_put_8 (abfd, R_NO_RELOCATION + 28 + (((skip >> 2) - 1) >> 16), p);
1155           bfd_put_16 (abfd, (skip >> 2) - 1, p + 1);
1156           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
1157         }
1158     }
1159   /* Ugh.  Punt and use a 4 byte entry.  */
1160   else if (skip > 0)
1161     {
1162       bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
1163       bfd_put_8 (abfd, skip >> 16, p + 1);
1164       bfd_put_16 (abfd, skip, p + 2);
1165       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
1166     }
1167   return p;
1168 }
1169
1170 /* Emit the proper R_DATA_OVERRIDE fixups to handle a nonzero addend
1171    from a BFD relocation.  Update the size of the subspace relocation
1172    stream via SUBSPACE_RELOC_SIZE_P; also return the current pointer
1173    into the relocation stream.  */
1174
1175 static unsigned char *
1176 som_reloc_addend (abfd, addend, p, subspace_reloc_sizep, queue)
1177      bfd *abfd;
1178      int addend;
1179      unsigned char *p;
1180      unsigned int *subspace_reloc_sizep;
1181      struct reloc_queue *queue;
1182 {
1183   if ((unsigned)(addend) + 0x80 < 0x100)
1184     {
1185       bfd_put_8 (abfd, R_DATA_OVERRIDE + 1, p);
1186       bfd_put_8 (abfd, addend, p + 1);
1187       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue); 
1188     }
1189   else if ((unsigned) (addend) + 0x8000 < 0x10000)
1190     {
1191       bfd_put_8 (abfd, R_DATA_OVERRIDE + 2, p);
1192       bfd_put_16 (abfd, addend, p + 1);
1193       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
1194     }
1195   else if ((unsigned) (addend) + 0x800000 < 0x1000000)
1196     {
1197       bfd_put_8 (abfd, R_DATA_OVERRIDE + 3, p);
1198       bfd_put_8 (abfd, addend >> 16, p + 1);
1199       bfd_put_16 (abfd, addend, p + 2);
1200       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
1201     }
1202   else
1203     {
1204       bfd_put_8 (abfd, R_DATA_OVERRIDE + 4, p);
1205       bfd_put_32 (abfd, addend, p + 1);
1206       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
1207     }
1208   return p;
1209 }
1210
1211 /* Handle a single function call relocation.  */
1212
1213 static unsigned char *
1214 som_reloc_call (abfd, p, subspace_reloc_sizep, bfd_reloc, sym_num, queue)
1215      bfd *abfd;
1216      unsigned char *p;
1217      unsigned int *subspace_reloc_sizep;
1218      arelent *bfd_reloc;
1219      int sym_num;
1220      struct reloc_queue *queue;
1221 {
1222   int arg_bits = HPPA_R_ARG_RELOC (bfd_reloc->addend);
1223   int rtn_bits = arg_bits & 0x3;
1224   int type, done = 0;
1225   
1226   /* You'll never believe all this is necessary to handle relocations
1227      for function calls.  Having to compute and pack the argument
1228      relocation bits is the real nightmare.
1229      
1230      If you're interested in how this works, just forget it.  You really
1231      do not want to know about this braindamage.  */
1232
1233   /* First see if this can be done with a "simple" relocation.  Simple
1234      relocations have a symbol number < 0x100 and have simple encodings
1235      of argument relocations.  */
1236
1237   if (sym_num < 0x100)
1238     {
1239       switch (arg_bits)
1240         {
1241         case 0:
1242         case 1:
1243           type = 0;
1244           break;
1245         case 1 << 8:
1246         case 1 << 8 | 1:
1247           type = 1;
1248           break;
1249         case 1 << 8 | 1 << 6:
1250         case 1 << 8 | 1 << 6 | 1:
1251           type = 2;
1252           break;
1253         case 1 << 8 | 1 << 6 | 1 << 4:
1254         case 1 << 8 | 1 << 6 | 1 << 4 | 1:
1255           type = 3;
1256           break;
1257         case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2:
1258         case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2 | 1:
1259           type = 4;
1260           break;
1261         default:
1262           /* Not one of the easy encodings.  This will have to be
1263              handled by the more complex code below.  */
1264           type = -1;
1265           break;
1266         }
1267       if (type != -1)
1268         {
1269           /* Account for the return value too.  */
1270           if (rtn_bits)
1271             type += 5;
1272
1273           /* Emit a 2 byte relocation.  Then see if it can be handled
1274              with a relocation which is already in the relocation queue.  */
1275           bfd_put_8 (abfd, bfd_reloc->howto->type + type, p);
1276           bfd_put_8 (abfd, sym_num, p + 1);
1277           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
1278           done = 1;
1279         }
1280     }
1281   
1282   /* If this could not be handled with a simple relocation, then do a hard
1283      one.  Hard relocations occur if the symbol number was too high or if
1284      the encoding of argument relocation bits is too complex.  */
1285   if (! done)
1286     {
1287       /* Don't ask about these magic sequences.  I took them straight
1288          from gas-1.36 which took them from the a.out man page.  */
1289       type = rtn_bits;
1290       if ((arg_bits >> 6 & 0xf) == 0xe)
1291         type += 9 * 40;
1292       else
1293         type += (3 * (arg_bits >> 8 & 3) + (arg_bits >> 6 & 3)) * 40;
1294       if ((arg_bits >> 2 & 0xf) == 0xe)
1295         type += 9 * 4;
1296       else
1297         type += (3 * (arg_bits >> 4 & 3) + (arg_bits >> 2 & 3)) * 4;
1298       
1299       /* Output the first two bytes of the relocation.  These describe
1300          the length of the relocation and encoding style.  */
1301       bfd_put_8 (abfd, bfd_reloc->howto->type + 10
1302                  + 2 * (sym_num >= 0x100) + (type >= 0x100),
1303                  p);
1304       bfd_put_8 (abfd, type, p + 1);
1305       
1306       /* Now output the symbol index and see if this bizarre relocation
1307          just happened to be in the relocation queue.  */
1308       if (sym_num < 0x100)
1309         {
1310           bfd_put_8 (abfd, sym_num, p + 2);
1311           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
1312         }
1313       else
1314         {
1315           bfd_put_8 (abfd, sym_num >> 16, p + 2);
1316           bfd_put_16 (abfd, sym_num, p + 3);
1317           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
1318         }
1319     }
1320   return p;
1321 }
1322
1323
1324 /* Return the logarithm of X, base 2, considering X unsigned. 
1325    Abort -1 if X is not a power or two or is zero.  */
1326
1327 static int
1328 log2 (x)
1329      unsigned int x;
1330 {
1331   int log = 0;
1332
1333   /* Test for 0 or a power of 2.  */
1334   if (x == 0 || x != (x & -x))
1335     return -1;
1336
1337   while ((x >>= 1) != 0)
1338     log++;
1339   return log;
1340 }
1341
1342 static bfd_reloc_status_type
1343 hppa_som_reloc (abfd, reloc_entry, symbol_in, data,
1344                 input_section, output_bfd, error_message)
1345      bfd *abfd;
1346      arelent *reloc_entry;
1347      asymbol *symbol_in;
1348      PTR data;
1349      asection *input_section;
1350      bfd *output_bfd;
1351      char **error_message;
1352 {
1353   if (output_bfd)
1354     {
1355       reloc_entry->address += input_section->output_offset;
1356       return bfd_reloc_ok;
1357     }
1358   return bfd_reloc_ok;
1359 }
1360
1361 /* Given a generic HPPA relocation type, the instruction format,
1362    and a field selector, return one or more appropriate SOM relocations.  */
1363
1364 int **
1365 hppa_som_gen_reloc_type (abfd, base_type, format, field)
1366      bfd *abfd;
1367      int base_type;
1368      int format;
1369      enum hppa_reloc_field_selector_type_alt field;
1370 {
1371   int *final_type, **final_types;
1372
1373   final_types = (int **) bfd_alloc_by_size_t (abfd, sizeof (int *) * 3);
1374   final_type = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
1375   if (!final_types || !final_type)
1376     {
1377       bfd_set_error (bfd_error_no_memory);
1378       return NULL;
1379     }
1380
1381   /* The field selector may require additional relocations to be 
1382      generated.  It's impossible to know at this moment if additional
1383      relocations will be needed, so we make them.  The code to actually
1384      write the relocation/fixup stream is responsible for removing
1385      any redundant relocations.  */
1386   switch (field)
1387     {
1388       case e_fsel:
1389       case e_psel:
1390       case e_lpsel:
1391       case e_rpsel:
1392         final_types[0] = final_type;
1393         final_types[1] = NULL;
1394         final_types[2] = NULL;
1395         *final_type = base_type;
1396         break;
1397
1398       case e_tsel:
1399       case e_ltsel:
1400       case e_rtsel:
1401         final_types[0] = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
1402         if (!final_types[0])
1403           {
1404             bfd_set_error (bfd_error_no_memory);
1405             return NULL;
1406           }
1407         if (field == e_tsel)
1408           *final_types[0] = R_FSEL;
1409         else if (field == e_ltsel)
1410           *final_types[0] = R_LSEL;
1411         else
1412           *final_types[0] = R_RSEL;
1413         final_types[1] = final_type;
1414         final_types[2] = NULL;
1415         *final_type = base_type;
1416         break;
1417
1418       case e_lssel:
1419       case e_rssel:
1420         final_types[0] = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
1421         if (!final_types[0])
1422           {
1423             bfd_set_error (bfd_error_no_memory);
1424             return NULL;
1425           }
1426         *final_types[0] = R_S_MODE;
1427         final_types[1] = final_type;
1428         final_types[2] = NULL;
1429         *final_type = base_type;
1430         break;
1431
1432       case e_lsel:
1433       case e_rsel:
1434         final_types[0] = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
1435         if (!final_types[0])
1436           {
1437             bfd_set_error (bfd_error_no_memory);
1438             return NULL;
1439           }
1440         *final_types[0] = R_N_MODE;
1441         final_types[1] = final_type;
1442         final_types[2] = NULL;
1443         *final_type = base_type;
1444         break;
1445
1446       case e_ldsel:
1447       case e_rdsel:
1448         final_types[0] = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
1449         if (!final_types[0])
1450           {
1451             bfd_set_error (bfd_error_no_memory);
1452             return NULL;
1453           }
1454         *final_types[0] = R_D_MODE;
1455         final_types[1] = final_type;
1456         final_types[2] = NULL;
1457         *final_type = base_type;
1458         break;
1459
1460       case e_lrsel:
1461       case e_rrsel:
1462         final_types[0] = (int *) bfd_alloc_by_size_t (abfd, sizeof (int));
1463         if (!final_types[0])
1464           {
1465             bfd_set_error (bfd_error_no_memory);
1466             return NULL;
1467           }
1468         *final_types[0] = R_R_MODE;
1469         final_types[1] = final_type;
1470         final_types[2] = NULL;
1471         *final_type = base_type;
1472         break;
1473     }
1474   
1475   switch (base_type)
1476     {
1477     case R_HPPA:
1478       /* PLABELs get their own relocation type.  */
1479       if (field == e_psel
1480           || field == e_lpsel
1481           || field == e_rpsel)
1482         {
1483           /* A PLABEL relocation that has a size of 32 bits must
1484              be a R_DATA_PLABEL.  All others are R_CODE_PLABELs.  */
1485           if (format == 32)
1486             *final_type = R_DATA_PLABEL;
1487           else
1488             *final_type = R_CODE_PLABEL;
1489         }
1490       /* PIC stuff.  */
1491       else if (field == e_tsel
1492           || field == e_ltsel
1493           || field == e_rtsel)
1494         *final_type = R_DLT_REL;
1495       /* A relocation in the data space is always a full 32bits.  */
1496       else if (format == 32)
1497         *final_type = R_DATA_ONE_SYMBOL;
1498
1499       break;
1500
1501     case R_HPPA_GOTOFF:
1502       /* More PLABEL special cases.  */
1503       if (field == e_psel
1504           || field == e_lpsel
1505           || field == e_rpsel)
1506         *final_type = R_DATA_PLABEL;
1507       break;
1508
1509     case R_HPPA_NONE:
1510     case R_HPPA_ABS_CALL:
1511     case R_HPPA_PCREL_CALL:
1512     case R_HPPA_COMPLEX:
1513     case R_HPPA_COMPLEX_PCREL_CALL:
1514     case R_HPPA_COMPLEX_ABS_CALL:
1515       /* Right now we can default all these.  */
1516       break;
1517     }
1518   return final_types;
1519 }
1520
1521 /* Return the address of the correct entry in the PA SOM relocation
1522    howto table.  */
1523
1524 static const reloc_howto_type *
1525 som_bfd_reloc_type_lookup (arch, code)
1526      bfd_arch_info_type *arch;
1527      bfd_reloc_code_real_type code;
1528 {
1529   if ((int) code < (int) R_NO_RELOCATION + 255)
1530     {
1531       BFD_ASSERT ((int) som_hppa_howto_table[(int) code].type == (int) code);
1532       return &som_hppa_howto_table[(int) code];
1533     }
1534
1535   return (reloc_howto_type *) 0;
1536 }
1537
1538 /* Perform some initialization for an object.  Save results of this
1539    initialization in the BFD.  */
1540
1541 static bfd_target *
1542 som_object_setup (abfd, file_hdrp, aux_hdrp)
1543      bfd *abfd;
1544      struct header *file_hdrp;
1545      struct som_exec_auxhdr *aux_hdrp;
1546 {
1547   /* som_mkobject will set bfd_error if som_mkobject fails.  */
1548   if (som_mkobject (abfd) != true)
1549     return 0;
1550
1551   /* Set BFD flags based on what information is available in the SOM.  */
1552   abfd->flags = NO_FLAGS;
1553   if (! file_hdrp->entry_offset)
1554     abfd->flags |= HAS_RELOC;
1555   else
1556     abfd->flags |= EXEC_P;
1557   if (file_hdrp->symbol_total)
1558     abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
1559
1560   bfd_get_start_address (abfd) = aux_hdrp->exec_entry;
1561   bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 0);
1562   bfd_get_symcount (abfd) = file_hdrp->symbol_total;
1563
1564   /* Initialize the saved symbol table and string table to NULL.  
1565      Save important offsets and sizes from the SOM header into
1566      the BFD.  */
1567   obj_som_stringtab (abfd) = (char  *) NULL;
1568   obj_som_symtab (abfd) = (som_symbol_type *) NULL;
1569   obj_som_stringtab_size (abfd) = file_hdrp->symbol_strings_size;
1570   obj_som_sym_filepos (abfd) = file_hdrp->symbol_location;
1571   obj_som_str_filepos (abfd) = file_hdrp->symbol_strings_location;
1572   obj_som_reloc_filepos (abfd) = file_hdrp->fixup_request_location;
1573
1574   return abfd->xvec;
1575 }
1576
1577 /* Create a new BFD section for NAME.  If NAME already exists, then create a
1578    new unique name, with NAME as the prefix.  This exists because SOM .o files
1579    may have more than one $CODE$ subspace.  */
1580
1581 static asection *
1582 make_unique_section (abfd, name, num)
1583      bfd *abfd;
1584      CONST char *name;
1585      int num;
1586 {
1587   asection *sect;
1588   char *newname;
1589   char altname[100];
1590
1591   sect = bfd_make_section (abfd, name);
1592   while (!sect)
1593     {
1594       sprintf (altname, "%s-%d", name, num++);
1595       sect = bfd_make_section (abfd, altname);
1596     }
1597
1598   newname = bfd_alloc (abfd, strlen (sect->name) + 1);
1599   if (!newname)
1600     {
1601       bfd_set_error (bfd_error_no_memory);
1602       return NULL;
1603     }
1604   strcpy (newname, sect->name);
1605
1606   sect->name = newname;
1607   return sect;
1608 }
1609
1610 /* Convert all of the space and subspace info into BFD sections.  Each space
1611    contains a number of subspaces, which in turn describe the mapping between
1612    regions of the exec file, and the address space that the program runs in.
1613    BFD sections which correspond to spaces will overlap the sections for the
1614    associated subspaces.  */
1615
1616 static boolean
1617 setup_sections (abfd, file_hdr)
1618      bfd *abfd;
1619      struct header *file_hdr;
1620 {
1621   char *space_strings;
1622   int space_index;
1623   unsigned int total_subspaces = 0;
1624
1625   /* First, read in space names */
1626
1627   space_strings = alloca (file_hdr->space_strings_size);
1628   if (!space_strings)
1629     return false;
1630
1631   if (bfd_seek (abfd, file_hdr->space_strings_location, SEEK_SET) < 0)
1632     return false;
1633   if (bfd_read (space_strings, 1, file_hdr->space_strings_size, abfd)
1634       != file_hdr->space_strings_size)
1635     return false;
1636
1637   /* Loop over all of the space dictionaries, building up sections */
1638   for (space_index = 0; space_index < file_hdr->space_total; space_index++)
1639     {
1640       struct space_dictionary_record space;
1641       struct subspace_dictionary_record subspace, save_subspace;
1642       int subspace_index;
1643       asection *space_asect;
1644
1645       /* Read the space dictionary element */
1646       if (bfd_seek (abfd, file_hdr->space_location
1647                     + space_index * sizeof space, SEEK_SET) < 0)
1648         return false;
1649       if (bfd_read (&space, 1, sizeof space, abfd) != sizeof space)
1650         return false;
1651
1652       /* Setup the space name string */
1653       space.name.n_name = space.name.n_strx + space_strings;
1654
1655       /* Make a section out of it */
1656       space_asect = make_unique_section (abfd, space.name.n_name, space_index);
1657       if (!space_asect)
1658         return false;
1659
1660       /* Now, read in the first subspace for this space */
1661       if (bfd_seek (abfd, file_hdr->subspace_location
1662                     + space.subspace_index * sizeof subspace,
1663                     SEEK_SET) < 0)
1664         return false;
1665       if (bfd_read (&subspace, 1, sizeof subspace, abfd) != sizeof subspace)
1666         return false;
1667       /* Seek back to the start of the subspaces for loop below */
1668       if (bfd_seek (abfd, file_hdr->subspace_location
1669                     + space.subspace_index * sizeof subspace,
1670                     SEEK_SET) < 0)
1671         return false;
1672
1673       /* Setup the start address and file loc from the first subspace record */
1674       space_asect->vma = subspace.subspace_start;
1675       space_asect->filepos = subspace.file_loc_init_value;
1676       space_asect->alignment_power = log2 (subspace.alignment);
1677       if (space_asect->alignment_power == -1)
1678         return false;
1679
1680       /* Initialize save_subspace so we can reliably determine if this
1681          loop placed any useful values into it.  */
1682       memset (&save_subspace, 0, sizeof (struct subspace_dictionary_record));
1683
1684       /* Loop over the rest of the subspaces, building up more sections */
1685       for (subspace_index = 0; subspace_index < space.subspace_quantity;
1686            subspace_index++)
1687         {
1688           asection *subspace_asect;
1689
1690           /* Read in the next subspace */
1691           if (bfd_read (&subspace, 1, sizeof subspace, abfd)
1692               != sizeof subspace)
1693             return false;
1694
1695           /* Setup the subspace name string */
1696           subspace.name.n_name = subspace.name.n_strx + space_strings;
1697
1698           /* Make a section out of this subspace */
1699           subspace_asect = make_unique_section (abfd, subspace.name.n_name,
1700                                      space.subspace_index + subspace_index);
1701
1702           if (!subspace_asect)
1703             return false;
1704
1705           /* Keep an easy mapping between subspaces and sections.  */
1706           som_section_data (subspace_asect)->subspace_index 
1707             = total_subspaces++;
1708
1709           /* Set SEC_READONLY and SEC_CODE/SEC_DATA as specified
1710              by the access_control_bits in the subspace header.  */
1711           switch (subspace.access_control_bits >> 4)
1712             {
1713             /* Readonly data.  */  
1714             case 0x0:
1715               subspace_asect->flags |= SEC_DATA | SEC_READONLY;
1716               break;
1717
1718             /* Normal data.  */  
1719             case 0x1:
1720               subspace_asect->flags |= SEC_DATA;
1721               break;
1722
1723             /* Readonly code and the gateways.
1724                Gateways have other attributes which do not map
1725                into anything BFD knows about.  */
1726             case 0x2:
1727             case 0x4:
1728             case 0x5:
1729             case 0x6:
1730             case 0x7:
1731               subspace_asect->flags |= SEC_CODE | SEC_READONLY;
1732               break;
1733
1734             /* dynamic (writable) code.  */
1735             case 0x3:
1736               subspace_asect->flags |= SEC_CODE;
1737               break;
1738             }
1739           
1740           if (subspace.dup_common || subspace.is_common) 
1741             subspace_asect->flags |= SEC_IS_COMMON;
1742           else if (subspace.subspace_length > 0)
1743             subspace_asect->flags |= SEC_HAS_CONTENTS;
1744           if (subspace.is_loadable)
1745             subspace_asect->flags |= SEC_ALLOC | SEC_LOAD;
1746           if (subspace.code_only)
1747             subspace_asect->flags |= SEC_CODE;
1748
1749           /* Both file_loc_init_value and initialization_length will
1750              be zero for a BSS like subspace.  */
1751           if (subspace.file_loc_init_value == 0
1752               && subspace.initialization_length == 0)
1753             subspace_asect->flags &= ~(SEC_DATA | SEC_LOAD);
1754
1755           /* This subspace has relocations.
1756              The fixup_request_quantity is a byte count for the number of
1757              entries in the relocation stream; it is not the actual number
1758              of relocations in the subspace.  */
1759           if (subspace.fixup_request_quantity != 0)
1760             {
1761               subspace_asect->flags |= SEC_RELOC;
1762               subspace_asect->rel_filepos = subspace.fixup_request_index;
1763               som_section_data (subspace_asect)->reloc_size
1764                 = subspace.fixup_request_quantity;
1765               /* We can not determine this yet.  When we read in the 
1766                  relocation table the correct value will be filled in.  */
1767               subspace_asect->reloc_count = -1;
1768             }
1769
1770           /* Update save_subspace if appropriate.  */
1771           if (subspace.file_loc_init_value > save_subspace.file_loc_init_value)
1772             save_subspace = subspace;
1773
1774           subspace_asect->vma = subspace.subspace_start;
1775           subspace_asect->_cooked_size = subspace.subspace_length;
1776           subspace_asect->_raw_size = subspace.subspace_length;
1777           subspace_asect->filepos = subspace.file_loc_init_value;
1778           subspace_asect->alignment_power = log2 (subspace.alignment);
1779           if (subspace_asect->alignment_power == -1)
1780             return false;
1781         }
1782
1783       /* Yow! there is no subspace within the space which actually 
1784          has initialized information in it; this should never happen
1785          as far as I know.  */
1786       if (!save_subspace.file_loc_init_value)
1787         return false;
1788
1789       /* Setup the sizes for the space section based upon the info in the
1790          last subspace of the space.  */
1791       space_asect->_cooked_size = save_subspace.subspace_start
1792         - space_asect->vma + save_subspace.subspace_length;
1793       space_asect->_raw_size = save_subspace.file_loc_init_value
1794         - space_asect->filepos + save_subspace.initialization_length;
1795     }
1796   return true;
1797 }
1798
1799 /* Read in a SOM object and make it into a BFD.  */
1800
1801 static bfd_target *
1802 som_object_p (abfd)
1803      bfd *abfd;
1804 {
1805   struct header file_hdr;
1806   struct som_exec_auxhdr aux_hdr;
1807
1808   if (bfd_read ((PTR) & file_hdr, 1, FILE_HDR_SIZE, abfd) != FILE_HDR_SIZE)
1809     {
1810       bfd_set_error (bfd_error_system_call);
1811       return 0;
1812     }
1813
1814   if (!_PA_RISC_ID (file_hdr.system_id))
1815     {
1816       bfd_set_error (bfd_error_wrong_format);
1817       return 0;
1818     }
1819
1820   switch (file_hdr.a_magic)
1821     {
1822     case RELOC_MAGIC:
1823     case EXEC_MAGIC:
1824     case SHARE_MAGIC:
1825     case DEMAND_MAGIC:
1826 #ifdef DL_MAGIC
1827     case DL_MAGIC:
1828 #endif
1829 #ifdef SHL_MAGIC
1830     case SHL_MAGIC:
1831 #endif
1832 #ifdef EXECLIBMAGIC
1833     case EXECLIBMAGIC:
1834 #endif
1835 #ifdef SHARED_MAGIC_CNX
1836     case SHARED_MAGIC_CNX:
1837 #endif
1838       break;
1839     default:
1840       bfd_set_error (bfd_error_wrong_format);
1841       return 0;
1842     }
1843
1844   if (file_hdr.version_id != VERSION_ID
1845       && file_hdr.version_id != NEW_VERSION_ID)
1846     {
1847       bfd_set_error (bfd_error_wrong_format);
1848       return 0;
1849     }
1850
1851   /* If the aux_header_size field in the file header is zero, then this
1852      object is an incomplete executable (a .o file).  Do not try to read
1853      a non-existant auxiliary header.  */
1854   memset (&aux_hdr, 0, sizeof (struct som_exec_auxhdr));
1855   if (file_hdr.aux_header_size != 0)
1856     {
1857       if (bfd_read ((PTR) & aux_hdr, 1, AUX_HDR_SIZE, abfd) != AUX_HDR_SIZE)
1858         {
1859           bfd_set_error (bfd_error_wrong_format);
1860           return 0;
1861         }
1862     }
1863
1864   if (!setup_sections (abfd, &file_hdr))
1865     {
1866       /* setup_sections does not bubble up a bfd error code.  */
1867       bfd_set_error (bfd_error_bad_value);
1868       return 0;
1869     }
1870
1871   /* This appears to be a valid SOM object.  Do some initialization.  */
1872   return som_object_setup (abfd, &file_hdr, &aux_hdr);
1873 }
1874
1875 /* Create a SOM object.  */
1876
1877 static boolean
1878 som_mkobject (abfd)
1879      bfd *abfd;
1880 {
1881   /* Allocate memory to hold backend information.  */
1882   abfd->tdata.som_data = (struct som_data_struct *)
1883     bfd_zalloc (abfd, sizeof (struct som_data_struct));
1884   if (abfd->tdata.som_data == NULL)
1885     {
1886       bfd_set_error (bfd_error_no_memory);
1887       return false;
1888     }
1889   obj_som_file_hdr (abfd)
1890     = (struct header *) bfd_zalloc (abfd, sizeof (struct header));
1891   if (obj_som_file_hdr (abfd) == NULL)
1892
1893     {
1894       bfd_set_error (bfd_error_no_memory);
1895       return false;
1896     }
1897   return true;
1898 }
1899
1900 /* Initialize some information in the file header.  This routine makes
1901    not attempt at doing the right thing for a full executable; it
1902    is only meant to handle relocatable objects.  */
1903
1904 static boolean
1905 som_prep_headers (abfd)
1906      bfd *abfd;
1907 {
1908   struct header *file_hdr = obj_som_file_hdr (abfd);
1909   asection *section;
1910
1911   /* FIXME.  This should really be conditional based on whether or not
1912      PA1.1 instructions/registers have been used.  */
1913   file_hdr->system_id = CPU_PA_RISC1_0;
1914
1915   /* FIXME.  Only correct for building relocatable objects.  */
1916   if (abfd->flags & EXEC_P)
1917     abort ();
1918   else
1919     file_hdr->a_magic = RELOC_MAGIC;
1920
1921   /* Only new format SOM is supported.  */
1922   file_hdr->version_id = NEW_VERSION_ID;
1923
1924   /* These fields are optional, and embedding timestamps is not always
1925      a wise thing to do, it makes comparing objects during a multi-stage
1926      bootstrap difficult.  */
1927   file_hdr->file_time.secs = 0;
1928   file_hdr->file_time.nanosecs = 0; 
1929
1930   if (abfd->flags & EXEC_P)
1931     abort ();
1932   else
1933     {
1934       file_hdr->entry_space = 0;
1935       file_hdr->entry_subspace = 0;
1936       file_hdr->entry_offset = 0;
1937     }
1938   
1939   file_hdr->presumed_dp = 0;
1940
1941   /* Now iterate over the sections translating information from
1942      BFD sections to SOM spaces/subspaces.  */
1943
1944   for (section = abfd->sections; section != NULL; section = section->next)
1945     {
1946       /* Ignore anything which has not been marked as a space or
1947          subspace.  */
1948       if (som_section_data (section)->is_space == 0
1949
1950           && som_section_data (section)->is_subspace == 0)
1951         continue;
1952
1953       if (som_section_data (section)->is_space)
1954         {
1955           /* Set space attributes.  Note most attributes of SOM spaces
1956              are set based on the subspaces it contains.  */
1957           som_section_data (section)->space_dict.loader_fix_index = -1;
1958           som_section_data (section)->space_dict.init_pointer_index = -1;
1959         }
1960       else
1961         {
1962           /* Set subspace attributes.  Basic stuff is done here, additional
1963              attributes are filled in later as more information becomes
1964              available.  */
1965           if (section->flags & SEC_IS_COMMON)
1966             {
1967               som_section_data (section)->subspace_dict.dup_common = 1;
1968               som_section_data (section)->subspace_dict.is_common = 1;
1969             }
1970
1971           if (section->flags & SEC_ALLOC)
1972             som_section_data (section)->subspace_dict.is_loadable = 1;
1973
1974           if (section->flags & SEC_CODE)
1975             som_section_data (section)->subspace_dict.code_only = 1;
1976
1977           som_section_data (section)->subspace_dict.subspace_start = 
1978             section->vma;
1979           som_section_data (section)->subspace_dict.subspace_length =
1980             bfd_section_size (abfd, section);
1981           som_section_data (section)->subspace_dict.initialization_length =
1982             bfd_section_size (abfd, section);
1983           som_section_data (section)->subspace_dict.alignment = 
1984             1 << section->alignment_power;
1985         }
1986     }
1987   return true;
1988 }
1989
1990 /* Count and return the number of spaces attached to the given BFD.  */
1991
1992 static unsigned long
1993 som_count_spaces (abfd)
1994      bfd *abfd;
1995 {
1996   int count = 0;
1997   asection *section;
1998
1999   for (section = abfd->sections; section != NULL; section = section->next)
2000     count += som_section_data (section)->is_space;
2001
2002   return count;
2003 }
2004
2005 /* Count the number of subspaces attached to the given BFD.  */
2006
2007 static unsigned long
2008 som_count_subspaces (abfd)
2009      bfd *abfd;
2010 {
2011   int count = 0;
2012   asection *section;
2013
2014   for (section = abfd->sections; section != NULL; section = section->next)
2015     count += som_section_data (section)->is_subspace;
2016
2017   return count;
2018 }
2019
2020 /* Return -1, 0, 1 indicating the relative ordering of sym1 and sym2.
2021
2022    We desire symbols to be ordered starting with the symbol with the
2023    highest relocation count down to the symbol with the lowest relocation
2024    count.  Doing so compacts the relocation stream.  */
2025
2026 static int
2027 compare_syms (sym1, sym2)
2028      asymbol **sym1;
2029      asymbol **sym2;
2030
2031 {
2032   unsigned int count1, count2;
2033   
2034   /* Get relocation count for each symbol.  Note that the count
2035      is stored in the udata pointer for section symbols!  */
2036   if ((*sym1)->flags & BSF_SECTION_SYM)
2037     count1 = (int)(*sym1)->udata;
2038   else
2039     count1 = som_symbol_data (*sym1)->reloc_count;
2040
2041   if ((*sym2)->flags & BSF_SECTION_SYM)
2042     count2 = (int)(*sym2)->udata;
2043   else
2044     count2 = som_symbol_data (*sym2)->reloc_count;
2045
2046   /* Return the appropriate value.  */
2047   if (count1 < count2)
2048     return 1;
2049   else if (count1 > count2)
2050     return -1;
2051   return 0;
2052 }
2053
2054 /* Perform various work in preparation for emitting the fixup stream.  */
2055
2056 static void
2057 som_prep_for_fixups (abfd, syms, num_syms)
2058      bfd *abfd;
2059      asymbol **syms;
2060      unsigned long num_syms;
2061 {
2062   int i;
2063   asection *section;
2064
2065   /* Most SOM relocations involving a symbol have a length which is
2066      dependent on the index of the symbol.  So symbols which are
2067      used often in relocations should have a small index.  */
2068
2069   /* First initialize the counters for each symbol.  */
2070   for (i = 0; i < num_syms; i++)
2071     {
2072       /* Handle a section symbol; these have no pointers back to the 
2073          SOM symbol info.  So we just use the pointer field (udata)
2074          to hold the relocation count.
2075
2076          FIXME.  While we're here set the name of any section symbol
2077          to something which will not screw GDB.  How do other formats
2078          deal with this?!?  */
2079       if (som_symbol_data (syms[i]) == NULL)
2080         {
2081           syms[i]->flags |= BSF_SECTION_SYM;
2082           syms[i]->name = "L$0\002";
2083           syms[i]->udata = (PTR) 0;
2084         }
2085       else
2086         som_symbol_data (syms[i])->reloc_count = 0;
2087     }
2088
2089   /* Now that the counters are initialized, make a weighted count
2090      of how often a given symbol is used in a relocation.  */
2091   for (section = abfd->sections; section != NULL; section = section->next)
2092     {
2093       int i;
2094
2095       /* Does this section have any relocations?  */
2096       if (section->reloc_count <= 0)
2097         continue;
2098
2099       /* Walk through each relocation for this section.  */
2100       for (i = 1; i < section->reloc_count; i++)
2101         {
2102           arelent *reloc = section->orelocation[i];
2103           int scale;
2104
2105           /* If no symbol, then there is no counter to increase.  */
2106           if (reloc->sym_ptr_ptr == NULL)
2107             continue;
2108
2109           /* Scaling to encourage symbols involved in R_DP_RELATIVE 
2110              and R_CODE_ONE_SYMBOL relocations to come first.  These
2111              two relocations have single byte versions if the symbol
2112              index is very small.  */
2113           if (reloc->howto->type == R_DP_RELATIVE
2114               || reloc->howto->type == R_CODE_ONE_SYMBOL)
2115             scale = 2;
2116           else
2117             scale = 1;
2118
2119           /* Handle section symbols by ramming the count in the udata
2120              field.  It will not be used and the count is very important
2121              for these symbols.  */
2122           if ((*reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
2123             {
2124               (*reloc->sym_ptr_ptr)->udata =
2125                 (PTR) ((int) (*reloc->sym_ptr_ptr)->udata + scale);
2126               continue;
2127             }
2128
2129           /* A normal symbol.  Increment the count.  */
2130           som_symbol_data (*reloc->sym_ptr_ptr)->reloc_count += scale;
2131         }
2132     }
2133
2134   /* Now sort the symbols.  */
2135   qsort (syms, num_syms, sizeof (asymbol *), compare_syms);
2136
2137   /* Compute the symbol indexes, they will be needed by the relocation
2138      code.  */
2139   for (i = 0; i < num_syms; i++)
2140     {
2141       /* A section symbol.  Again, there is no pointer to backend symbol
2142          information, so we reuse (abuse) the udata field again.  */
2143       if (syms[i]->flags & BSF_SECTION_SYM)
2144         syms[i]->udata = (PTR) i;
2145       else
2146         som_symbol_data (syms[i])->index = i;
2147     }
2148 }
2149
2150 static boolean
2151 som_write_fixups (abfd, current_offset, total_reloc_sizep)
2152      bfd *abfd;
2153      unsigned long current_offset;
2154      unsigned int *total_reloc_sizep;
2155 {
2156   unsigned int i, j;
2157   unsigned char *tmp_space, *p;
2158   unsigned int total_reloc_size = 0;
2159   unsigned int subspace_reloc_size = 0;
2160   unsigned int num_spaces = obj_som_file_hdr (abfd)->space_total;
2161   asection *section = abfd->sections;
2162
2163   /* Get a chunk of memory that we can use as buffer space, then throw
2164      away.  */
2165   tmp_space = alloca (SOM_TMP_BUFSIZE);
2166   memset (tmp_space, 0, SOM_TMP_BUFSIZE);
2167   p = tmp_space;
2168
2169   /* All the fixups for a particular subspace are emitted in a single
2170      stream.  All the subspaces for a particular space are emitted
2171      as a single stream.
2172
2173      So, to get all the locations correct one must iterate through all the
2174      spaces, for each space iterate through its subspaces and output a
2175      fixups stream.  */
2176   for (i = 0; i < num_spaces; i++)
2177     {
2178       asection *subsection;
2179
2180       /* Find a space.  */
2181       while (som_section_data (section)->is_space == 0)
2182         section = section->next;
2183
2184       /* Now iterate through each of its subspaces.  */
2185       for (subsection = abfd->sections;
2186            subsection != NULL;
2187            subsection = subsection->next)
2188         {
2189           int reloc_offset, current_rounding_mode;
2190
2191           /* Find a subspace of this space.  */
2192           if (som_section_data (subsection)->is_subspace == 0
2193               || som_section_data (subsection)->containing_space != section)
2194             continue;
2195
2196           /* If this subspace had no relocations, then we're finished 
2197              with it.  */
2198           if (subsection->reloc_count <= 0)
2199             {
2200               som_section_data (subsection)->subspace_dict.fixup_request_index
2201                 = -1;
2202               continue;
2203             }
2204
2205           /* This subspace has some relocations.  Put the relocation stream
2206              index into the subspace record.  */
2207           som_section_data (subsection)->subspace_dict.fixup_request_index
2208             = total_reloc_size;
2209
2210           /* To make life easier start over with a clean slate for 
2211              each subspace.  Seek to the start of the relocation stream
2212              for this subspace in preparation for writing out its fixup
2213              stream.  */
2214           if (bfd_seek (abfd, current_offset + total_reloc_size, SEEK_SET) != 0)
2215             {
2216               bfd_set_error (bfd_error_system_call);
2217               return false;
2218             }
2219
2220           /* Buffer space has already been allocated.  Just perform some
2221              initialization here.  */
2222           p = tmp_space;
2223           subspace_reloc_size = 0;
2224           reloc_offset = 0;
2225           som_initialize_reloc_queue (reloc_queue);
2226           current_rounding_mode = R_N_MODE;
2227
2228           /* Translate each BFD relocation into one or more SOM 
2229              relocations.  */
2230           for (j = 0; j < subsection->reloc_count; j++)
2231             {
2232               arelent *bfd_reloc = subsection->orelocation[j];
2233               unsigned int skip;
2234               int sym_num;
2235
2236               /* Get the symbol number.  Remember it's stored in a 
2237                  special place for section symbols.  */
2238               if ((*bfd_reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
2239                 sym_num = (int) (*bfd_reloc->sym_ptr_ptr)->udata;
2240               else
2241                 sym_num = som_symbol_data (*bfd_reloc->sym_ptr_ptr)->index;
2242               
2243               /* If there is not enough room for the next couple relocations,
2244                  then dump the current buffer contents now.  Also reinitialize
2245                  the relocation queue. 
2246
2247                  No single BFD relocation could ever translate into more
2248                  than 100 bytes of SOM relocations (20bytes is probably the
2249                  upper limit, but leave lots of space for growth).  */
2250               if (p - tmp_space + 100 > SOM_TMP_BUFSIZE)
2251                 {
2252                   if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
2253                       != p - tmp_space)
2254                     {
2255                       bfd_set_error (bfd_error_system_call);
2256                       return false;
2257                     }
2258                   p = tmp_space;
2259                   som_initialize_reloc_queue (reloc_queue);
2260                 }
2261
2262               /* Emit R_NO_RELOCATION fixups to map any bytes which were
2263                  skipped.  */
2264               skip = bfd_reloc->address - reloc_offset;
2265               p = som_reloc_skip (abfd, skip, p,
2266                                   &subspace_reloc_size, reloc_queue);
2267
2268               /* Update reloc_offset for the next iteration.
2269
2270                  Many relocations do not consume input bytes.  They
2271                  are markers, or set state necessary to perform some
2272                  later relocation.  */
2273               switch (bfd_reloc->howto->type)
2274                 {
2275                 /* This only needs to handle relocations that may be
2276                    made by hppa_som_gen_reloc.  */
2277                 case R_ENTRY:
2278                 case R_EXIT:
2279                 case R_N_MODE:
2280                 case R_S_MODE:
2281                 case R_D_MODE:
2282                 case R_R_MODE:
2283                 case R_FSEL:
2284                 case R_LSEL:
2285                 case R_RSEL:
2286                   reloc_offset = bfd_reloc->address;
2287                   break;
2288
2289                 default:
2290                   reloc_offset = bfd_reloc->address + 4;
2291                   break;
2292                 }
2293
2294               /* Now the actual relocation we care about.  */
2295               switch (bfd_reloc->howto->type)
2296                 {
2297                 case R_PCREL_CALL:
2298                 case R_ABS_CALL:
2299                   p = som_reloc_call (abfd, p, &subspace_reloc_size,
2300                                       bfd_reloc, sym_num, reloc_queue);
2301                   break;
2302
2303                 case R_CODE_ONE_SYMBOL:
2304                 case R_DP_RELATIVE:
2305                   /* Account for any addend.  */
2306                   if (bfd_reloc->addend)
2307                     p = som_reloc_addend (abfd, bfd_reloc->addend, p, 
2308                                           &subspace_reloc_size, reloc_queue);
2309
2310                   if (sym_num < 0x20)
2311                     {
2312                       bfd_put_8 (abfd, bfd_reloc->howto->type + sym_num, p);
2313                       subspace_reloc_size += 1;
2314                       p += 1;
2315                     }
2316                   else if (sym_num < 0x100)
2317                     {
2318                       bfd_put_8 (abfd, bfd_reloc->howto->type + 32, p);
2319                       bfd_put_8 (abfd, sym_num, p + 1);
2320                       p = try_prev_fixup (abfd, &subspace_reloc_size, p,
2321                                           2, reloc_queue);
2322                     }
2323                   else if (sym_num < 0x10000000)
2324                     {
2325                       bfd_put_8 (abfd, bfd_reloc->howto->type + 33, p);
2326                       bfd_put_8 (abfd, sym_num >> 16, p + 1);
2327                       bfd_put_16 (abfd, sym_num, p + 2); 
2328                       p = try_prev_fixup (abfd, &subspace_reloc_size,
2329                                           p, 4, reloc_queue);
2330                     }
2331                   else
2332                     abort ();
2333                   break;
2334
2335                 case R_DATA_ONE_SYMBOL:
2336                 case R_DATA_PLABEL:
2337                 case R_CODE_PLABEL:
2338                 case R_DLT_REL:
2339                   /* Account for any addend.  */
2340                   if (bfd_reloc->addend)
2341                     p = som_reloc_addend (abfd, bfd_reloc->addend, p, 
2342                                           &subspace_reloc_size, reloc_queue);
2343
2344                   if (sym_num < 0x100)
2345                     {
2346                       bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2347                       bfd_put_8 (abfd, sym_num, p + 1);
2348                       p = try_prev_fixup (abfd, &subspace_reloc_size, p,
2349                                           2, reloc_queue);
2350                     }
2351                   else if (sym_num < 0x10000000)
2352                     {
2353                       bfd_put_8 (abfd, bfd_reloc->howto->type + 1, p);
2354                       bfd_put_8 (abfd, sym_num >> 16, p + 1);
2355                       bfd_put_16 (abfd, sym_num, p + 2); 
2356                       p = try_prev_fixup (abfd, &subspace_reloc_size,
2357                                           p, 4, reloc_queue);
2358                     }
2359                   else
2360                     abort ();
2361                   break;
2362
2363                 case R_ENTRY:
2364                   {
2365                     int *descp
2366                        = (int *) som_symbol_data (*bfd_reloc->sym_ptr_ptr)->unwind;
2367                     bfd_put_8 (abfd, R_ENTRY, p);
2368                     bfd_put_32 (abfd, descp[0], p + 1);
2369                     bfd_put_32 (abfd, descp[1], p + 5);
2370                     p = try_prev_fixup (abfd, &subspace_reloc_size,
2371                                         p, 9, reloc_queue);
2372                     break;
2373                   }
2374                   
2375                 case R_EXIT:
2376                   bfd_put_8 (abfd, R_EXIT, p);
2377                   subspace_reloc_size += 1;
2378                   p += 1;
2379                   break;
2380
2381                 case R_N_MODE:
2382                 case R_S_MODE:
2383                 case R_D_MODE:
2384                 case R_R_MODE:
2385                   /* If this relocation requests the current rounding
2386                      mode, then it is redundant.  */
2387                   if (bfd_reloc->howto->type != current_rounding_mode)
2388                     {
2389                       bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2390                       subspace_reloc_size += 1;
2391                       p += 1;
2392                       current_rounding_mode = bfd_reloc->howto->type;
2393                     }
2394                   break;
2395
2396                 case R_FSEL:
2397                 case R_LSEL:
2398                 case R_RSEL:
2399                   bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2400                   subspace_reloc_size += 1;
2401                   p += 1;
2402                   break;
2403
2404                 /* Put a "R_RESERVED" relocation in the stream if
2405                    we hit something we do not understand.  The linker
2406                    will complain loudly if this ever happens.  */
2407                 default:
2408                   bfd_put_8 (abfd, 0xff, p);
2409                   subspace_reloc_size += 1;
2410                   p += 1;
2411                   break;
2412                 }
2413             }
2414
2415           /* Last BFD relocation for a subspace has been processed.
2416              Map the rest of the subspace with R_NO_RELOCATION fixups.  */
2417           p = som_reloc_skip (abfd, bfd_section_size (abfd, subsection) 
2418                                       - reloc_offset,
2419                               p, &subspace_reloc_size, reloc_queue);
2420
2421           /* Scribble out the relocations.  */
2422           if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
2423               != p - tmp_space)
2424             {
2425               bfd_set_error (bfd_error_system_call);
2426               return false;
2427             }
2428           p = tmp_space;
2429
2430           total_reloc_size += subspace_reloc_size;
2431           som_section_data (subsection)->subspace_dict.fixup_request_quantity
2432             = subspace_reloc_size;
2433         }
2434       section = section->next;
2435     }
2436   *total_reloc_sizep = total_reloc_size;
2437   return true;
2438 }
2439
2440 /* Write out the space/subspace string table.  */
2441
2442 static boolean
2443 som_write_space_strings (abfd, current_offset, string_sizep)
2444      bfd *abfd;
2445      unsigned long current_offset;
2446      unsigned int *string_sizep;
2447 {
2448   unsigned char *tmp_space, *p;
2449   unsigned int strings_size = 0;
2450   asection *section;
2451
2452   /* Get a chunk of memory that we can use as buffer space, then throw
2453      away.  */
2454   tmp_space = alloca (SOM_TMP_BUFSIZE);
2455   memset (tmp_space, 0, SOM_TMP_BUFSIZE);
2456   p = tmp_space;
2457
2458   /* Seek to the start of the space strings in preparation for writing
2459      them out.  */
2460   if (bfd_seek (abfd, current_offset, SEEK_SET) != 0)
2461     {
2462       bfd_set_error (bfd_error_system_call);
2463       return false;
2464     }
2465
2466   /* Walk through all the spaces and subspaces (order is not important)
2467      building up and writing string table entries for their names.  */
2468   for (section = abfd->sections; section != NULL; section = section->next)
2469     {
2470       int length;
2471
2472       /* Only work with space/subspaces; avoid any other sections
2473          which might have been made (.text for example).  */
2474       if (som_section_data (section)->is_space == 0
2475           && som_section_data (section)->is_subspace == 0)
2476         continue;
2477
2478       /* Get the length of the space/subspace name.  */
2479       length = strlen (section->name);
2480
2481       /* If there is not enough room for the next entry, then dump the
2482          current buffer contents now.  Each entry will take 4 bytes to
2483          hold the string length + the string itself + null terminator.  */
2484       if (p - tmp_space + 5 + length > SOM_TMP_BUFSIZE)
2485         {
2486           if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
2487               != p - tmp_space) 
2488             {
2489               bfd_set_error (bfd_error_system_call);
2490               return false;
2491             }
2492           /* Reset to beginning of the buffer space.  */
2493           p = tmp_space;
2494         }
2495
2496       /* First element in a string table entry is the length of the
2497          string.  Alignment issues are already handled.  */
2498       bfd_put_32 (abfd, length, p);
2499       p += 4;
2500       strings_size += 4;
2501
2502       /* Record the index in the space/subspace records.  */
2503       if (som_section_data (section)->is_space)
2504         som_section_data (section)->space_dict.name.n_strx = strings_size;
2505       else
2506         som_section_data (section)->subspace_dict.name.n_strx = strings_size;
2507
2508       /* Next comes the string itself + a null terminator.  */
2509       strcpy (p, section->name);
2510       p += length + 1;
2511       strings_size += length + 1;
2512
2513       /* Always align up to the next word boundary.  */
2514       while (strings_size % 4)
2515         {
2516           bfd_put_8 (abfd, 0, p);
2517           p++;
2518           strings_size++;
2519         }
2520     }
2521
2522   /* Done with the space/subspace strings.  Write out any information
2523      contained in a partial block.  */
2524   if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) != p - tmp_space)
2525     {
2526       bfd_set_error (bfd_error_system_call);
2527       return false;
2528     }
2529   *string_sizep = strings_size;
2530   return true;
2531 }
2532
2533 /* Write out the symbol string table.  */
2534
2535 static boolean
2536 som_write_symbol_strings (abfd, current_offset, syms, num_syms, string_sizep)
2537      bfd *abfd;
2538      unsigned long current_offset;
2539      asymbol **syms;
2540      unsigned int num_syms;
2541      unsigned int *string_sizep;
2542 {
2543   unsigned int i;
2544   unsigned char *tmp_space, *p;
2545   unsigned int strings_size = 0;
2546
2547   /* Get a chunk of memory that we can use as buffer space, then throw
2548      away.  */
2549   tmp_space = alloca (SOM_TMP_BUFSIZE);
2550   memset (tmp_space, 0, SOM_TMP_BUFSIZE);
2551   p = tmp_space;
2552
2553   /* Seek to the start of the space strings in preparation for writing
2554      them out.  */
2555   if (bfd_seek (abfd, current_offset, SEEK_SET) != 0)
2556     {
2557       bfd_set_error (bfd_error_system_call);
2558       return false;
2559     }
2560
2561   for (i = 0; i < num_syms; i++)
2562     {
2563       int length = strlen (syms[i]->name);
2564
2565       /* If there is not enough room for the next entry, then dump the
2566          current buffer contents now.  */
2567      if (p - tmp_space + 5 + length > SOM_TMP_BUFSIZE)
2568         {
2569           if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd)
2570               != p - tmp_space)
2571             {
2572               bfd_set_error (bfd_error_system_call);
2573               return false;
2574             }
2575           /* Reset to beginning of the buffer space.  */
2576           p = tmp_space;
2577         }
2578
2579       /* First element in a string table entry is the length of the
2580          string.  This must always be 4 byte aligned.  This is also
2581          an appropriate time to fill in the string index field in the
2582          symbol table entry.  */
2583       bfd_put_32 (abfd, length, p);
2584       strings_size += 4;
2585       p += 4;
2586
2587       /* Next comes the string itself + a null terminator.  */
2588       strcpy (p, syms[i]->name);
2589
2590       /* ACK.  FIXME.  */
2591       syms[i]->name = (char *)strings_size;
2592       p += length + 1;
2593       strings_size += length + 1;
2594
2595       /* Always align up to the next word boundary.  */
2596       while (strings_size % 4)
2597         {
2598           bfd_put_8 (abfd, 0, p);
2599           strings_size++;
2600           p++;
2601         }
2602     }
2603
2604   /* Scribble out any partial block.  */
2605   if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) != p - tmp_space)
2606     {
2607       bfd_set_error (bfd_error_system_call);
2608       return false;
2609     }
2610
2611   *string_sizep = strings_size;
2612   return true;
2613 }
2614
2615 /* Compute variable information to be placed in the SOM headers, 
2616    space/subspace dictionaries, relocation streams, etc.  Begin
2617    writing parts of the object file.  */
2618
2619 static boolean 
2620 som_begin_writing (abfd)
2621      bfd *abfd;
2622 {
2623   unsigned long current_offset = 0;
2624   int strings_size = 0;
2625   unsigned int total_reloc_size = 0;
2626   unsigned long num_spaces, num_subspaces, num_syms, i;
2627   asection *section;
2628   asymbol **syms = bfd_get_outsymbols (abfd);
2629   unsigned int total_subspaces = 0;
2630
2631   /* The file header will always be first in an object file, 
2632      everything else can be in random locations.  To keep things
2633      "simple" BFD will lay out the object file in the manner suggested
2634      by the PRO ABI for PA-RISC Systems.  */
2635
2636   /* Before any output can really begin offsets for all the major
2637      portions of the object file must be computed.  So, starting
2638      with the initial file header compute (and sometimes write)
2639      each portion of the object file.  */
2640
2641   /* Make room for the file header, it's contents are not complete
2642      yet, so it can not be written at this time.  */
2643   current_offset += sizeof (struct header);  
2644
2645   /* Any auxiliary headers will follow the file header.  Right now
2646      we support only the copyright and version headers.  */
2647   obj_som_file_hdr (abfd)->aux_header_location = current_offset;
2648   obj_som_file_hdr (abfd)->aux_header_size = 0;
2649   if (obj_som_version_hdr (abfd) != NULL)
2650     {
2651       unsigned int len;
2652
2653       bfd_seek (abfd, current_offset, SEEK_SET);
2654
2655       /* Write the aux_id structure and the string length.  */
2656       len = sizeof (struct aux_id) + sizeof (unsigned int);
2657       obj_som_file_hdr (abfd)->aux_header_size += len;
2658       current_offset += len;
2659       if (bfd_write ((PTR) obj_som_version_hdr (abfd), len, 1, abfd) != len)
2660         {
2661           bfd_set_error (bfd_error_system_call);
2662           return false;
2663         }
2664
2665       /* Write the version string.  */
2666       len = obj_som_version_hdr (abfd)->header_id.length - sizeof (int);
2667       obj_som_file_hdr (abfd)->aux_header_size += len;
2668       current_offset += len;
2669       if (bfd_write ((PTR) obj_som_version_hdr (abfd)->user_string,
2670                      len, 1, abfd) != len)
2671         {
2672           bfd_set_error (bfd_error_system_call);
2673           return false;
2674         }
2675     }
2676
2677   if (obj_som_copyright_hdr (abfd) != NULL)
2678     {
2679       unsigned int len;
2680
2681       bfd_seek (abfd, current_offset, SEEK_SET);
2682
2683       /* Write the aux_id structure and the string length.  */
2684       len = sizeof (struct aux_id) + sizeof (unsigned int);
2685       obj_som_file_hdr (abfd)->aux_header_size += len;
2686       current_offset += len;
2687       if (bfd_write ((PTR) obj_som_copyright_hdr (abfd), len, 1, abfd) != len)
2688         {
2689           bfd_set_error (bfd_error_system_call);
2690           return false;
2691         }
2692
2693       /* Write the copyright string.  */
2694       len = obj_som_copyright_hdr (abfd)->header_id.length - sizeof (int);
2695       obj_som_file_hdr (abfd)->aux_header_size += len;
2696       current_offset += len;
2697       if (bfd_write ((PTR) obj_som_copyright_hdr (abfd)->copyright,
2698                      len, 1, abfd) != len)
2699         {
2700           bfd_set_error (bfd_error_system_call);
2701           return false;
2702         }
2703     }
2704
2705   /* Next comes the initialization pointers; we have no initialization
2706      pointers, so current offset does not change.  */
2707   obj_som_file_hdr (abfd)->init_array_location = current_offset;
2708   obj_som_file_hdr (abfd)->init_array_total = 0;
2709
2710   /* Next are the space records.  These are fixed length records.
2711
2712      Count the number of spaces to determine how much room is needed
2713      in the object file for the space records.
2714
2715      The names of the spaces are stored in a separate string table,
2716      and the index for each space into the string table is computed
2717      below.  Therefore, it is not possible to write the space headers
2718      at this time.  */
2719   num_spaces = som_count_spaces (abfd);
2720   obj_som_file_hdr (abfd)->space_location = current_offset;
2721   obj_som_file_hdr (abfd)->space_total = num_spaces;
2722   current_offset += num_spaces * sizeof (struct space_dictionary_record);
2723
2724   /* Next are the subspace records.  These are fixed length records.
2725
2726      Count the number of subspaes to determine how much room is needed
2727      in the object file for the subspace records.
2728
2729      A variety if fields in the subspace record are still unknown at
2730      this time (index into string table, fixup stream location/size, etc).  */
2731   num_subspaces = som_count_subspaces (abfd);
2732   obj_som_file_hdr (abfd)->subspace_location = current_offset;
2733   obj_som_file_hdr (abfd)->subspace_total = num_subspaces;
2734   current_offset += num_subspaces * sizeof (struct subspace_dictionary_record);
2735
2736   /* Next is the string table for the space/subspace names.  We will
2737      build and write the string table on the fly.  At the same time
2738      we will fill in the space/subspace name index fields.  */
2739
2740   /* The string table needs to be aligned on a word boundary.  */
2741   if (current_offset % 4)
2742     current_offset += (4 - (current_offset % 4));
2743
2744   /* Mark the offset of the space/subspace string table in the 
2745      file header.  */
2746   obj_som_file_hdr (abfd)->space_strings_location = current_offset;
2747
2748   /* Scribble out the space strings.  */
2749   if (som_write_space_strings (abfd, current_offset, &strings_size) == false)
2750     return false;
2751
2752   /* Record total string table size in the header and update the
2753      current offset.  */
2754   obj_som_file_hdr (abfd)->space_strings_size = strings_size;
2755   current_offset += strings_size;
2756
2757   /* Next is the symbol table.  These are fixed length records.
2758
2759      Count the number of symbols to determine how much room is needed
2760      in the object file for the symbol table.
2761
2762      The names of the symbols are stored in a separate string table,
2763      and the index for each symbol name into the string table is computed
2764      below.  Therefore, it is not possible to write the symobl table
2765      at this time.  */
2766   num_syms = bfd_get_symcount (abfd);
2767   obj_som_file_hdr (abfd)->symbol_location = current_offset;
2768   obj_som_file_hdr (abfd)->symbol_total = num_syms;
2769   current_offset += num_syms * sizeof (struct symbol_dictionary_record);
2770
2771   /* Do prep work before handling fixups.  */
2772   som_prep_for_fixups (abfd, syms, num_syms);
2773
2774   /* Next comes the fixup stream which starts on a word boundary.  */
2775   if (current_offset % 4)
2776     current_offset += (4 - (current_offset % 4)); 
2777   obj_som_file_hdr (abfd)->fixup_request_location = current_offset;
2778
2779   /* Write the fixups and update fields in subspace headers which
2780      relate to the fixup stream.  */
2781   if (som_write_fixups (abfd, current_offset, &total_reloc_size) == false)
2782     return false;
2783
2784   /* Record the total size of the fixup stream in the file header.  */
2785   obj_som_file_hdr (abfd)->fixup_request_total = total_reloc_size;
2786   current_offset += total_reloc_size;
2787
2788   /* Next are the symbol strings.
2789      Align them to a word boundary.  */
2790   if (current_offset % 4)
2791     current_offset += (4 - (current_offset % 4));
2792   obj_som_file_hdr (abfd)->symbol_strings_location = current_offset;
2793
2794   /* Scribble out the symbol strings.  */
2795   if (som_write_symbol_strings (abfd, current_offset, syms, 
2796                                 num_syms, &strings_size)
2797       == false)
2798     return false;
2799
2800   /* Record total string table size in header and update the
2801      current offset.  */
2802   obj_som_file_hdr (abfd)->symbol_strings_size = strings_size;
2803   current_offset += strings_size;
2804
2805   /* Next is the compiler records.  We do not use these.  */
2806   obj_som_file_hdr (abfd)->compiler_location = current_offset;
2807   obj_som_file_hdr (abfd)->compiler_total = 0;
2808
2809   /* Now compute the file positions for the loadable subspaces.  */
2810
2811   section = abfd->sections;
2812   for (i = 0; i < num_spaces; i++)
2813     {
2814       asection *subsection;
2815
2816       /* Find a space.  */
2817       while (som_section_data (section)->is_space == 0)
2818         section = section->next;
2819
2820       /* Now look for all its subspaces.  */
2821       for (subsection = abfd->sections;
2822            subsection != NULL;
2823            subsection = subsection->next)
2824         {
2825           
2826           if (som_section_data (subsection)->is_subspace == 0
2827               || som_section_data (subsection)->containing_space != section
2828               || (subsection->flags & SEC_ALLOC) == 0)
2829             continue;
2830
2831           som_section_data (subsection)->subspace_index = total_subspaces++;
2832           /* This is real data to be loaded from the file.  */
2833           if (subsection->flags & SEC_LOAD)
2834             {
2835               som_section_data (subsection)->subspace_dict.file_loc_init_value
2836                 = current_offset;
2837               section->filepos = current_offset;
2838               current_offset += bfd_section_size (abfd, subsection); 
2839             }
2840           /* Looks like uninitialized data.  */
2841           else
2842             {
2843               som_section_data (subsection)->subspace_dict.file_loc_init_value
2844                 = 0;
2845               som_section_data (subsection)->subspace_dict.
2846                 initialization_length = 0;
2847             }
2848         }
2849       /* Goto the next section.  */
2850       section = section->next; 
2851     }
2852
2853   /* Finally compute the file positions for unloadable subspaces.  */
2854
2855   obj_som_file_hdr (abfd)->unloadable_sp_location = current_offset;
2856   section = abfd->sections;
2857   for (i = 0; i < num_spaces; i++)
2858     {
2859       asection *subsection;
2860
2861       /* Find a space.  */
2862       while (som_section_data (section)->is_space == 0)
2863         section = section->next;
2864
2865       /* Now look for all its subspaces.  */
2866       for (subsection = abfd->sections;
2867            subsection != NULL;
2868            subsection = subsection->next)
2869         {
2870           
2871           if (som_section_data (subsection)->is_subspace == 0
2872               || som_section_data (subsection)->containing_space != section
2873               || (subsection->flags & SEC_ALLOC) != 0)
2874             continue;
2875
2876           som_section_data (subsection)->subspace_index = total_subspaces++;
2877           /* This is real data to be loaded from the file.  */
2878           if ((subsection->flags & SEC_LOAD) == 0)
2879             {
2880               som_section_data (subsection)->subspace_dict.file_loc_init_value
2881                 = current_offset;
2882               section->filepos = current_offset;
2883               current_offset += bfd_section_size (abfd, subsection); 
2884             }
2885           /* Looks like uninitialized data.  */
2886           else
2887             {
2888               som_section_data (subsection)->subspace_dict.file_loc_init_value
2889                 = 0;
2890               som_section_data (subsection)->subspace_dict.
2891                 initialization_length = bfd_section_size (abfd, subsection);
2892             }
2893         }
2894       /* Goto the next section.  */
2895       section = section->next; 
2896     }
2897
2898   obj_som_file_hdr (abfd)->unloadable_sp_size
2899     = current_offset - obj_som_file_hdr (abfd)->unloadable_sp_location;
2900
2901   /* Loader fixups are not supported in any way shape or form.  */
2902   obj_som_file_hdr (abfd)->loader_fixup_location = 0;
2903   obj_som_file_hdr (abfd)->loader_fixup_total = 0;
2904
2905   /* Done.  Store the total size of the SOM.  */
2906   obj_som_file_hdr (abfd)->som_length = current_offset;
2907   return true;
2908 }
2909
2910 /* Finally, scribble out the various headers to the disk.  */
2911
2912 static boolean
2913 som_write_headers (abfd)
2914      bfd *abfd;
2915 {
2916   int num_spaces = som_count_spaces (abfd);
2917   int i;
2918   int subspace_index = 0;
2919   file_ptr location;
2920   asection *section;
2921
2922   /* Subspaces are written first so that we can set up information
2923      about them in their containing spaces as the subspace is written.  */
2924
2925   /* Seek to the start of the subspace dictionary records.  */
2926   location = obj_som_file_hdr (abfd)->subspace_location;
2927   bfd_seek (abfd, location, SEEK_SET);
2928   section = abfd->sections;
2929   /* Now for each loadable space write out records for its subspaces.  */
2930   for (i = 0; i < num_spaces; i++)
2931     {
2932       asection *subsection;
2933
2934       /* Find a space.  */
2935       while (som_section_data (section)->is_space == 0)
2936         section = section->next;
2937
2938       /* Now look for all its subspaces.  */
2939       for (subsection = abfd->sections;
2940            subsection != NULL;
2941            subsection = subsection->next)
2942         {
2943           
2944           /* Skip any section which does not correspond to a space
2945              or subspace.  Or does not have SEC_ALLOC set (and therefore
2946              has no real bits on the disk).  */
2947           if (som_section_data (subsection)->is_subspace == 0
2948               || som_section_data (subsection)->containing_space != section
2949               || (subsection->flags & SEC_ALLOC) == 0)
2950             continue;
2951
2952           /* If this is the first subspace for this space, then save
2953              the index of the subspace in its containing space.  Also
2954              set "is_loadable" in the containing space.  */
2955
2956           if (som_section_data (section)->space_dict.subspace_quantity == 0)
2957             {
2958               som_section_data (section)->space_dict.is_loadable = 1;
2959               som_section_data (section)->space_dict.subspace_index
2960                 = subspace_index;
2961             }
2962
2963           /* Increment the number of subspaces seen and the number of
2964              subspaces contained within the current space.  */
2965           subspace_index++;
2966           som_section_data (section)->space_dict.subspace_quantity++;
2967
2968           /* Mark the index of the current space within the subspace's
2969              dictionary record.  */
2970           som_section_data (subsection)->subspace_dict.space_index = i;
2971           
2972           /* Dump the current subspace header.  */
2973           if (bfd_write ((PTR) &som_section_data (subsection)->subspace_dict,
2974                          sizeof (struct subspace_dictionary_record), 1, abfd)
2975               != sizeof (struct subspace_dictionary_record))
2976             {
2977               bfd_set_error (bfd_error_system_call);
2978               return false;
2979             }
2980         }
2981       /* Goto the next section.  */
2982       section = section->next; 
2983     }
2984
2985   /* Now repeat the process for unloadable subspaces.  */
2986   section = abfd->sections;
2987   /* Now for each space write out records for its subspaces.  */
2988   for (i = 0; i < num_spaces; i++)
2989     {
2990       asection *subsection;
2991
2992       /* Find a space.  */
2993       while (som_section_data (section)->is_space == 0)
2994         section = section->next;
2995
2996       /* Now look for all its subspaces.  */
2997       for (subsection = abfd->sections;
2998            subsection != NULL;
2999            subsection = subsection->next)
3000         {
3001           
3002           /* Skip any section which does not correspond to a space or
3003              subspace, or which SEC_ALLOC set (and therefore handled
3004              in the loadable spaces/subspaces code above.  */
3005
3006           if (som_section_data (subsection)->is_subspace == 0
3007               || som_section_data (subsection)->containing_space != section
3008               || (subsection->flags & SEC_ALLOC) != 0)
3009             continue;
3010
3011           /* If this is the first subspace for this space, then save
3012              the index of the subspace in its containing space.  Clear
3013              "is_loadable".  */
3014
3015           if (som_section_data (section)->space_dict.subspace_quantity == 0)
3016             {
3017               som_section_data (section)->space_dict.is_loadable = 0;
3018               som_section_data (section)->space_dict.subspace_index
3019                 = subspace_index;
3020             }
3021
3022           /* Increment the number of subspaces seen and the number of
3023              subspaces contained within the current space.  */
3024           som_section_data (section)->space_dict.subspace_quantity++;
3025           subspace_index++; 
3026
3027           /* Mark the index of the current space within the subspace's
3028              dictionary record.  */
3029           som_section_data (subsection)->subspace_dict.space_index = i;
3030           
3031           /* Dump this subspace header.  */
3032           if (bfd_write ((PTR) &som_section_data (subsection)->subspace_dict,
3033                          sizeof (struct subspace_dictionary_record), 1, abfd)
3034               != sizeof (struct subspace_dictionary_record))
3035             {
3036               bfd_set_error (bfd_error_system_call);
3037               return false;
3038             }
3039         }
3040       /* Goto the next section.  */
3041       section = section->next; 
3042     }
3043
3044   /* All the subspace dictiondary records are written, and all the
3045      fields are set up in the space dictionary records.
3046
3047      Seek to the right location and start writing the space
3048      dictionary records.  */
3049   location = obj_som_file_hdr (abfd)->space_location;
3050   bfd_seek (abfd, location, SEEK_SET);
3051
3052   section = abfd->sections;
3053   for (i = 0; i < num_spaces; i++)
3054     {
3055
3056       /* Find a space.  */
3057       while (som_section_data (section)->is_space == 0)
3058         section = section->next;
3059
3060       /* Dump its header  */
3061       if (bfd_write ((PTR) &som_section_data (section)->space_dict,
3062                      sizeof (struct space_dictionary_record), 1, abfd)
3063           != sizeof (struct space_dictionary_record))
3064         {
3065           bfd_set_error (bfd_error_system_call);
3066           return false;
3067         }
3068
3069       /* Goto the next section.  */
3070       section = section->next;
3071     }
3072
3073   /* Only thing left to do is write out the file header.  It is always
3074      at location zero.  Seek there and write it.  */
3075   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
3076   if (bfd_write ((PTR) obj_som_file_hdr (abfd),
3077                  sizeof (struct header), 1, abfd)
3078       != sizeof (struct header))
3079     {
3080       bfd_set_error (bfd_error_system_call);
3081       return false;
3082     }
3083   return true;
3084 }
3085
3086 /* Compute and return the checksum for a SOM file header.  */
3087
3088 static unsigned long
3089 som_compute_checksum (abfd)
3090      bfd *abfd;
3091 {
3092   unsigned long checksum, count, i;
3093   unsigned long *buffer = (unsigned long *) obj_som_file_hdr (abfd);
3094
3095   checksum = 0;
3096   count = sizeof (struct header) / sizeof (unsigned long);
3097   for (i = 0; i < count; i++)
3098     checksum ^= *(buffer + i);
3099
3100   return checksum;
3101 }
3102
3103 static void
3104 som_bfd_derive_misc_symbol_info (abfd, sym, info)
3105      bfd *abfd;
3106      asymbol *sym;
3107      struct som_misc_symbol_info *info;
3108 {
3109   /* Initialize.  */
3110   memset (info, 0, sizeof (struct som_misc_symbol_info));
3111
3112   /* The HP SOM linker requires detailed type information about
3113      all symbols (including undefined symbols!).  Unfortunately,
3114      the type specified in an import/export statement does not
3115      always match what the linker wants.  Severe braindamage.  */
3116          
3117   /* Section symbols will not have a SOM symbol type assigned to
3118      them yet.  Assign all section symbols type ST_DATA.  */
3119   if (sym->flags & BSF_SECTION_SYM)
3120     info->symbol_type = ST_DATA;
3121   else
3122     {
3123       /* Common symbols must have scope SS_UNSAT and type
3124          ST_STORAGE or the linker will choke.  */
3125       if (sym->section == &bfd_com_section)
3126         {
3127           info->symbol_scope = SS_UNSAT;
3128           info->symbol_type = ST_STORAGE;
3129         }
3130
3131       /* It is possible to have a symbol without an associated
3132          type.  This happens if the user imported the symbol
3133          without a type and the symbol was never defined
3134          locally.  If BSF_FUNCTION is set for this symbol, then
3135          assign it type ST_CODE (the HP linker requires undefined
3136          external functions to have type ST_CODE rather than ST_ENTRY).  */
3137       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN
3138                && sym->section == &bfd_und_section
3139                && sym->flags & BSF_FUNCTION)
3140         info->symbol_type = ST_CODE;
3141
3142       /* Handle function symbols which were defined in this file.
3143          They should have type ST_ENTRY.  Also retrieve the argument
3144          relocation bits from the SOM backend information.  */
3145       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_ENTRY
3146                || (som_symbol_data (sym)->som_type == SYMBOL_TYPE_CODE
3147                    && (sym->flags & BSF_FUNCTION))
3148                || (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN
3149                    && (sym->flags & BSF_FUNCTION)))
3150         {
3151           info->symbol_type = ST_ENTRY;
3152           info->arg_reloc = som_symbol_data (sym)->tc_data.hppa_arg_reloc;
3153         }
3154
3155       /* If the type is unknown at this point, it should be
3156          ST_DATA (functions were handled as special cases above).  */
3157       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN)
3158         info->symbol_type = ST_DATA;
3159
3160       /* From now on it's a very simple mapping.  */
3161       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_ABSOLUTE)
3162         info->symbol_type = ST_ABSOLUTE;
3163       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_CODE)
3164         info->symbol_type = ST_CODE;
3165       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_DATA)
3166         info->symbol_type = ST_DATA;
3167       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_MILLICODE)
3168         info->symbol_type = ST_MILLICODE;
3169       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_PLABEL)
3170         info->symbol_type = ST_PLABEL;
3171       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_PRI_PROG)
3172         info->symbol_type = ST_PRI_PROG;
3173       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_SEC_PROG)
3174         info->symbol_type = ST_SEC_PROG;
3175     }
3176         
3177   /* Now handle the symbol's scope.  Exported data which is not
3178      in the common section has scope SS_UNIVERSAL.  Note scope
3179      of common symbols was handled earlier!  */
3180   if (sym->flags & BSF_EXPORT && sym->section != &bfd_com_section)
3181     info->symbol_scope = SS_UNIVERSAL;
3182   /* Any undefined symbol at this point has a scope SS_UNSAT.  */
3183   else if (sym->section == &bfd_und_section)
3184     info->symbol_scope = SS_UNSAT;
3185   /* Anything else which is not in the common section has scope
3186      SS_LOCAL.  */
3187   else if (sym->section != &bfd_com_section)
3188     info->symbol_scope = SS_LOCAL;
3189
3190   /* Now set the symbol_info field.  It has no real meaning
3191      for undefined or common symbols, but the HP linker will
3192      choke if it's not set to some "reasonable" value.  We
3193      use zero as a reasonable value.  */
3194   if (sym->section == &bfd_com_section || sym->section == &bfd_und_section
3195       || sym->section == &bfd_abs_section)
3196     info->symbol_info = 0;
3197   /* For all other symbols, the symbol_info field contains the 
3198      subspace index of the space this symbol is contained in.  */
3199   else
3200     info->symbol_info = som_section_data (sym->section)->subspace_index;
3201
3202   /* Set the symbol's value.  */
3203   info->symbol_value = sym->value + sym->section->vma;
3204 }
3205
3206 /* Build and write, in one big chunk, the entire symbol table for
3207    this BFD.  */
3208
3209 static boolean
3210 som_build_and_write_symbol_table (abfd)
3211      bfd *abfd;
3212 {
3213   unsigned int num_syms = bfd_get_symcount (abfd);
3214   file_ptr symtab_location = obj_som_file_hdr (abfd)->symbol_location;
3215   asymbol **bfd_syms = bfd_get_outsymbols (abfd);
3216   struct symbol_dictionary_record *som_symtab;
3217   int i, symtab_size;
3218
3219   /* Compute total symbol table size and allocate a chunk of memory
3220      to hold the symbol table as we build it.  */
3221   symtab_size = num_syms * sizeof (struct symbol_dictionary_record);
3222   som_symtab = (struct symbol_dictionary_record *) alloca (symtab_size);
3223   memset (som_symtab, 0, symtab_size);
3224
3225   /* Walk over each symbol.  */
3226   for (i = 0; i < num_syms; i++)
3227     {
3228       struct som_misc_symbol_info info;
3229
3230       /* This is really an index into the symbol strings table.  
3231          By the time we get here, the index has already been 
3232          computed and stored into the name field in the BFD symbol.  */
3233       som_symtab[i].name.n_strx = (int) bfd_syms[i]->name;
3234
3235       /* Derive SOM information from the BFD symbol.  */
3236       som_bfd_derive_misc_symbol_info (abfd, bfd_syms[i], &info);
3237
3238       /* Now use it.  */
3239       som_symtab[i].symbol_type = info.symbol_type;
3240       som_symtab[i].symbol_scope = info.symbol_scope;
3241       som_symtab[i].arg_reloc = info.arg_reloc;
3242       som_symtab[i].symbol_info = info.symbol_info;
3243       som_symtab[i].symbol_value = info.symbol_value;
3244     }
3245
3246   /* Everything is ready, seek to the right location and
3247      scribble out the symbol table.  */
3248   if (bfd_seek (abfd, symtab_location, SEEK_SET) != 0)
3249     {
3250       bfd_set_error (bfd_error_system_call);
3251       return false;
3252     }
3253
3254   if (bfd_write ((PTR) som_symtab, symtab_size, 1, abfd) != symtab_size)
3255     {
3256       bfd_set_error (bfd_error_system_call);
3257       return false;
3258     }
3259   return true; 
3260 }
3261
3262 /* Write an object in SOM format.  */  
3263
3264 static boolean
3265 som_write_object_contents (abfd)
3266      bfd *abfd;
3267 {
3268   if (abfd->output_has_begun == false)
3269     {
3270       /* Set up fixed parts of the file, space, and subspace headers.
3271          Notify the world that output has begun.  */
3272       som_prep_headers (abfd);
3273       abfd->output_has_begun = true;
3274       /* Start writing the object file.  This include all the string
3275          tables, fixup streams, and other portions of the object file.  */
3276       som_begin_writing (abfd);
3277     }
3278
3279   /* Now that the symbol table information is complete, build and
3280      write the symbol table.  */
3281   if (som_build_and_write_symbol_table (abfd) == false)
3282     return false;
3283
3284   /* Compute the checksum for the file header just before writing
3285      the header to disk.  */
3286   obj_som_file_hdr (abfd)->checksum = som_compute_checksum (abfd);
3287   return (som_write_headers (abfd));
3288 }
3289
3290 \f
3291 /* Read and save the string table associated with the given BFD.  */
3292
3293 static boolean
3294 som_slurp_string_table (abfd)
3295      bfd *abfd;
3296 {
3297   char *stringtab;
3298
3299   /* Use the saved version if its available.  */
3300   if (obj_som_stringtab (abfd) != NULL)
3301     return true;
3302
3303   /* Allocate and read in the string table.  */
3304   stringtab = bfd_zalloc (abfd, obj_som_stringtab_size (abfd));
3305   if (stringtab == NULL)
3306     {
3307       bfd_set_error (bfd_error_no_memory);
3308       return false;
3309     }
3310
3311   if (bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET) < 0)
3312     {
3313       bfd_set_error (bfd_error_system_call);
3314       return false;
3315     }
3316   
3317   if (bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd)
3318       != obj_som_stringtab_size (abfd))
3319     {
3320       bfd_set_error (bfd_error_system_call);
3321       return false;
3322     }
3323
3324   /* Save our results and return success. */
3325   obj_som_stringtab (abfd) = stringtab;
3326   return true;
3327 }
3328
3329 /* Return the amount of data (in bytes) required to hold the symbol
3330    table for this object.  */
3331
3332 static unsigned int
3333 som_get_symtab_upper_bound (abfd)
3334      bfd *abfd;
3335 {
3336   if (!som_slurp_symbol_table (abfd))
3337     return 0;
3338
3339   return (bfd_get_symcount (abfd) + 1) * (sizeof (asymbol *));
3340 }
3341
3342 /* Convert from a SOM subspace index to a BFD section.  */
3343
3344 static asection *
3345 som_section_from_subspace_index (abfd, index)
3346      bfd *abfd;
3347      unsigned int index;
3348 {
3349   asection *section;
3350
3351   for (section = abfd->sections; section != NULL; section = section->next)
3352     if (som_section_data (section)->subspace_index == index)
3353       return section;
3354
3355   /* Should never happen.  */
3356   abort();
3357 }
3358
3359 /* Read and save the symbol table associated with the given BFD.  */
3360
3361 static unsigned int
3362 som_slurp_symbol_table (abfd)
3363      bfd *abfd;
3364 {
3365   int symbol_count = bfd_get_symcount (abfd);
3366   int symsize = sizeof (struct symbol_dictionary_record);
3367   char *stringtab;
3368   struct symbol_dictionary_record *buf, *bufp, *endbufp;
3369   som_symbol_type *sym, *symbase;
3370
3371   /* Return saved value if it exists.  */
3372   if (obj_som_symtab (abfd) != NULL)
3373     return true;
3374
3375   /* Special case.  This is *not* an error.  */
3376   if (symbol_count == 0)
3377     return true;
3378
3379   if (!som_slurp_string_table (abfd))
3380     return false;
3381
3382   stringtab = obj_som_stringtab (abfd);
3383
3384   symbase = (som_symbol_type *)
3385     bfd_zalloc (abfd, symbol_count * sizeof (som_symbol_type));
3386   if (symbase == NULL)
3387     {
3388       bfd_set_error (bfd_error_no_memory);
3389       return false;
3390     }
3391
3392   /* Read in the external SOM representation.  */
3393   buf = alloca (symbol_count * symsize);
3394   if (buf == NULL)
3395     {
3396       bfd_set_error (bfd_error_no_memory);
3397       return false;
3398     }
3399   if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) < 0)
3400     {
3401       bfd_set_error (bfd_error_system_call);
3402       return false;
3403     }
3404   if (bfd_read (buf, symbol_count * symsize, 1, abfd) 
3405       != symbol_count * symsize)
3406     {
3407       bfd_set_error (bfd_error_no_symbols);
3408       return (false);
3409     }
3410
3411   /* Iterate over all the symbols and internalize them.  */
3412   endbufp = buf + symbol_count;
3413   for (bufp = buf, sym = symbase; bufp < endbufp; ++bufp)
3414     {
3415
3416       /* I don't think we care about these.  */
3417       if (bufp->symbol_type == ST_SYM_EXT
3418           || bufp->symbol_type == ST_ARG_EXT)
3419         continue;
3420
3421       /* Set some private data we care about.  */
3422       if (bufp->symbol_type == ST_NULL)
3423         som_symbol_data (sym)->som_type = SYMBOL_TYPE_UNKNOWN;
3424       else if (bufp->symbol_type == ST_ABSOLUTE)
3425         som_symbol_data (sym)->som_type = SYMBOL_TYPE_ABSOLUTE;
3426       else if (bufp->symbol_type == ST_DATA)
3427         som_symbol_data (sym)->som_type = SYMBOL_TYPE_DATA;
3428       else if (bufp->symbol_type == ST_CODE)
3429         som_symbol_data (sym)->som_type = SYMBOL_TYPE_CODE;
3430       else if (bufp->symbol_type == ST_PRI_PROG)
3431         som_symbol_data (sym)->som_type = SYMBOL_TYPE_PRI_PROG;
3432       else if (bufp->symbol_type == ST_SEC_PROG)
3433         som_symbol_data (sym)->som_type = SYMBOL_TYPE_SEC_PROG;
3434       else if (bufp->symbol_type == ST_ENTRY)
3435         som_symbol_data (sym)->som_type = SYMBOL_TYPE_ENTRY;
3436       else if (bufp->symbol_type == ST_MILLICODE)
3437         som_symbol_data (sym)->som_type = SYMBOL_TYPE_MILLICODE;
3438       else if (bufp->symbol_type == ST_PLABEL)
3439         som_symbol_data (sym)->som_type = SYMBOL_TYPE_PLABEL;
3440       else
3441         som_symbol_data (sym)->som_type = SYMBOL_TYPE_UNKNOWN;
3442       som_symbol_data (sym)->tc_data.hppa_arg_reloc = bufp->arg_reloc;
3443
3444       /* Some reasonable defaults.  */
3445       sym->symbol.the_bfd = abfd;
3446       sym->symbol.name = bufp->name.n_strx + stringtab;
3447       sym->symbol.value = bufp->symbol_value;
3448       sym->symbol.section = 0;
3449       sym->symbol.flags = 0;
3450
3451       switch (bufp->symbol_type)
3452         {
3453         case ST_ENTRY:
3454         case ST_PRI_PROG:
3455         case ST_SEC_PROG:
3456         case ST_MILLICODE:
3457           sym->symbol.flags |= BSF_FUNCTION;
3458           sym->symbol.value &= ~0x3;
3459           break;
3460
3461         case ST_STUB:
3462         case ST_CODE:
3463           sym->symbol.value &= ~0x3;
3464
3465         default:
3466           break;
3467         }
3468
3469       /* Handle scoping and section information.  */
3470       switch (bufp->symbol_scope)
3471         {
3472         /* symbol_info field is undefined for SS_EXTERNAL and SS_UNSAT symbols,
3473            so the section associated with this symbol can't be known.  */
3474         case SS_EXTERNAL:
3475         case SS_UNSAT:
3476           if (bufp->symbol_type != ST_STORAGE)
3477             sym->symbol.section = &bfd_und_section;
3478           else
3479             sym->symbol.section = &bfd_com_section;
3480           sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
3481           break;
3482
3483         case SS_UNIVERSAL:
3484           sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
3485           sym->symbol.section
3486             = som_section_from_subspace_index (abfd, bufp->symbol_info);
3487           sym->symbol.value -= sym->symbol.section->vma;
3488           break;
3489
3490 #if 0
3491         /* SS_GLOBAL and SS_LOCAL are two names for the same thing.
3492            Sound dumb?  It is.  */
3493         case SS_GLOBAL:
3494 #endif
3495         case SS_LOCAL:
3496           sym->symbol.flags |= BSF_LOCAL;
3497           sym->symbol.section
3498             = som_section_from_subspace_index (abfd, bufp->symbol_info);
3499           sym->symbol.value -= sym->symbol.section->vma;
3500           break;
3501         }
3502
3503       /* Mark symbols left around by the debugger.  */
3504       if (strlen (sym->symbol.name) >= 2
3505           && sym->symbol.name[0] == 'L'
3506           && (sym->symbol.name[1] == '$' || sym->symbol.name[2] == '$'
3507               || sym->symbol.name[3] == '$'))
3508         sym->symbol.flags |= BSF_DEBUGGING;
3509
3510       /* Note increment at bottom of loop, since we skip some symbols
3511          we can not include it as part of the for statement.  */
3512       sym++;
3513     }
3514
3515   /* Save our results and return success.  */
3516   obj_som_symtab (abfd) = symbase;
3517   return (true);
3518 }
3519
3520 /* Canonicalize a SOM symbol table.  Return the number of entries
3521    in the symbol table.  */
3522
3523 static unsigned int
3524 som_get_symtab (abfd, location)
3525      bfd *abfd;
3526      asymbol **location;
3527 {
3528   int i;
3529   som_symbol_type *symbase;
3530
3531   if (!som_slurp_symbol_table (abfd))
3532     return 0;
3533
3534   i = bfd_get_symcount (abfd);
3535   symbase = obj_som_symtab (abfd);
3536
3537   for (; i > 0; i--, location++, symbase++)
3538     *location = &symbase->symbol;
3539
3540   /* Final null pointer.  */
3541   *location = 0;
3542   return (bfd_get_symcount (abfd));
3543 }
3544
3545 /* Make a SOM symbol.  There is nothing special to do here.  */
3546
3547 static asymbol *
3548 som_make_empty_symbol (abfd)
3549      bfd *abfd;
3550 {
3551   som_symbol_type *new =
3552   (som_symbol_type *) bfd_zalloc (abfd, sizeof (som_symbol_type));
3553   if (new == NULL)
3554     {
3555       bfd_set_error (bfd_error_no_memory);
3556       return 0;
3557     }
3558   new->symbol.the_bfd = abfd;
3559
3560   return &new->symbol;
3561 }
3562
3563 /* Print symbol information.  */
3564
3565 static void
3566 som_print_symbol (ignore_abfd, afile, symbol, how)
3567      bfd *ignore_abfd;
3568      PTR afile;
3569      asymbol *symbol;
3570      bfd_print_symbol_type how;
3571 {
3572   FILE *file = (FILE *) afile;
3573   switch (how)
3574     {
3575     case bfd_print_symbol_name:
3576       fprintf (file, "%s", symbol->name);
3577       break;
3578     case bfd_print_symbol_more:
3579       fprintf (file, "som ");
3580       fprintf_vma (file, symbol->value);
3581       fprintf (file, " %lx", (long) symbol->flags);
3582       break;
3583     case bfd_print_symbol_all:
3584       {
3585         CONST char *section_name;
3586         section_name = symbol->section ? symbol->section->name : "(*none*)";
3587         bfd_print_symbol_vandf ((PTR) file, symbol);
3588         fprintf (file, " %s\t%s", section_name, symbol->name);
3589         break;
3590       }
3591     }
3592 }
3593
3594 /* Count or process variable-length SOM fixup records.
3595
3596    To avoid code duplication we use this code both to compute the number
3597    of relocations requested by a stream, and to internalize the stream.
3598
3599    When computing the number of relocations requested by a stream the
3600    variables rptr, section, and symbols have no meaning.
3601
3602    Return the number of relocations requested by the fixup stream.  When
3603    not just counting 
3604
3605    This needs at least two or three more passes to get it cleaned up.  */
3606
3607 static unsigned int
3608 som_set_reloc_info (fixup, end, internal_relocs, section, symbols, just_count)
3609      unsigned char *fixup;
3610      unsigned int end;
3611      arelent *internal_relocs;
3612      asection *section;
3613      asymbol **symbols;
3614      boolean just_count;
3615 {
3616   unsigned int op, varname;
3617   unsigned char *end_fixups = &fixup[end];
3618   const struct fixup_format *fp;
3619   char *cp;
3620   unsigned char *save_fixup;
3621   int variables[26], stack[20], c, v, count, prev_fixup, *sp;
3622   const int *subop;
3623   arelent *rptr= internal_relocs;
3624   unsigned int offset = just_count ? 0 : section->vma;
3625
3626 #define var(c)          variables[(c) - 'A']
3627 #define push(v)         (*sp++ = (v))
3628 #define pop()           (*--sp)
3629 #define emptystack()    (sp == stack)
3630
3631   som_initialize_reloc_queue (reloc_queue);
3632   memset (variables, 0, sizeof (variables));
3633   memset (stack, 0, sizeof (stack));
3634   count = 0;
3635   prev_fixup = 0;
3636   sp = stack;
3637
3638   while (fixup < end_fixups)
3639     {
3640
3641       /* Save pointer to the start of this fixup.  We'll use
3642          it later to determine if it is necessary to put this fixup
3643          on the queue.  */
3644       save_fixup = fixup;
3645
3646       /* Get the fixup code and its associated format.  */
3647       op = *fixup++;
3648       fp = &som_fixup_formats[op];
3649
3650       /* Handle a request for a previous fixup.  */
3651       if (*fp->format == 'P')
3652         {
3653           /* Get pointer to the beginning of the prev fixup, move
3654              the repeated fixup to the head of the queue.  */
3655           fixup = reloc_queue[fp->D].reloc;
3656           som_reloc_queue_fix (reloc_queue, fp->D);
3657           prev_fixup = 1;
3658
3659           /* Get the fixup code and its associated format.  */
3660           op = *fixup++;
3661           fp = &som_fixup_formats[op];
3662         }
3663
3664       /* If we are not just counting, set some reasonable defaults.  */
3665       if (! just_count)
3666         {
3667           rptr->address = offset;
3668           rptr->howto = &som_hppa_howto_table[op];
3669           rptr->addend = 0;
3670           rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
3671         }
3672
3673       /* Set default input length to 0.  Get the opcode class index
3674          into D.  */
3675       var ('L') = 0;
3676       var ('D') = fp->D;
3677
3678       /* Get the opcode format.  */
3679       cp = fp->format;
3680
3681       /* Process the format string.  Parsing happens in two phases,
3682          parse RHS, then assign to LHS.  Repeat until no more 
3683          characters in the format string.  */
3684       while (*cp)
3685         {
3686           /* The variable this pass is going to compute a value for.  */
3687           varname = *cp++;
3688
3689           /* Start processing RHS.  Continue until a NULL or '=' is found.  */
3690           do
3691             {
3692               c = *cp++;
3693
3694               /* If this is a variable, push it on the stack.  */
3695               if (isupper (c))
3696                 push (var (c));
3697
3698               /* If this is a lower case letter, then it represents
3699                  additional data from the fixup stream to be pushed onto
3700                  the stack.  */
3701               else if (islower (c))
3702                 {
3703                   for (v = 0; c > 'a'; --c)
3704                     v = (v << 8) | *fixup++;
3705                   push (v);
3706                 }
3707
3708               /* A decimal constant.  Push it on the stack.  */
3709               else if (isdigit (c))
3710                 {
3711                   v = c - '0';
3712                   while (isdigit (*cp))
3713                     v = (v * 10) + (*cp++ - '0');
3714                   push (v);
3715                 }
3716               else
3717
3718                 /* An operator.  Pop two two values from the stack and
3719                    use them as operands to the given operation.  Push
3720                    the result of the operation back on the stack.  */
3721                 switch (c)
3722                   {
3723                   case '+':
3724                     v = pop ();
3725                     v += pop ();
3726                     push (v);
3727                     break;
3728                   case '*':
3729                     v = pop ();
3730                     v *= pop ();
3731                     push (v);
3732                     break;
3733                   case '<':
3734                     v = pop ();
3735                     v = pop () << v;
3736                     push (v);
3737                     break;
3738                   default:
3739                     abort ();
3740                   }
3741             }
3742           while (*cp && *cp != '=');
3743
3744           /* Move over the equal operator.  */
3745           cp++;
3746
3747           /* Pop the RHS off the stack.  */
3748           c = pop ();
3749
3750           /* Perform the assignment.  */
3751           var (varname) = c;
3752
3753           /* Handle side effects. and special 'O' stack cases.  */
3754           switch (varname)
3755             {
3756             /* Consume some bytes from the input space.  */
3757             case 'L':
3758               offset += c;
3759               break;
3760             /* A symbol to use in the relocation.  Make a note
3761                of this if we are not just counting.  */
3762             case 'S':
3763               if (! just_count)
3764                 rptr->sym_ptr_ptr = &symbols[c];
3765               break;
3766             /* Handle the linker expression stack.  */
3767             case 'O':
3768               switch (op)
3769                 {
3770                 case R_COMP1:
3771                   subop = comp1_opcodes;
3772                   break;
3773                 case R_COMP2:
3774                   subop = comp2_opcodes;
3775                   break;
3776                 case R_COMP3:
3777                   subop = comp3_opcodes;
3778                   break;
3779                 default:
3780                   abort ();
3781                 }
3782               while (*subop <= (unsigned char) c)
3783                 ++subop;
3784               --subop;
3785               break;
3786             default:
3787               break;
3788             }
3789         }
3790
3791       /* If we used a previous fixup, clean up after it.  */
3792       if (prev_fixup)
3793         {
3794           fixup = save_fixup + 1;
3795           prev_fixup = 0;
3796         }
3797       /* Queue it.  */
3798       else if (fixup > save_fixup + 1)
3799         som_reloc_queue_insert (save_fixup, fixup - save_fixup, reloc_queue);
3800
3801       /* We do not pass R_DATA_OVERRIDE or R_NO_RELOCATION 
3802          fixups to BFD.  */
3803       if (som_hppa_howto_table[op].type != R_DATA_OVERRIDE
3804           && som_hppa_howto_table[op].type != R_NO_RELOCATION)
3805         {
3806           /* Done with a single reloction. Loop back to the top.  */
3807           if (! just_count)
3808             {
3809               rptr->addend = var ('V');
3810               rptr++;
3811             }
3812           count++;
3813           /* Now that we've handled a "full" relocation, reset
3814              some state.  */
3815           memset (variables, 0, sizeof (variables));
3816           memset (stack, 0, sizeof (stack));
3817         }
3818     }
3819   return count;
3820
3821 #undef var
3822 #undef push
3823 #undef pop
3824 #undef emptystack
3825 }
3826
3827 /* Read in the relocs (aka fixups in SOM terms) for a section. 
3828
3829    som_get_reloc_upper_bound calls this routine with JUST_COUNT 
3830    set to true to indicate it only needs a count of the number
3831    of actual relocations.  */
3832
3833 static boolean
3834 som_slurp_reloc_table (abfd, section, symbols, just_count)
3835      bfd *abfd;
3836      asection *section;
3837      asymbol **symbols;
3838      boolean just_count;
3839 {
3840   char *external_relocs;
3841   unsigned int fixup_stream_size;
3842   arelent *internal_relocs;
3843   unsigned int num_relocs;
3844
3845   fixup_stream_size = som_section_data (section)->reloc_size;
3846   /* If there were no relocations, then there is nothing to do.  */
3847   if (section->reloc_count == 0)
3848     return true;
3849
3850   /* If reloc_count is -1, then the relocation stream has not been 
3851      parsed.  We must do so now to know how many relocations exist.  */
3852   if (section->reloc_count == -1)
3853     {
3854       external_relocs = (char *) bfd_zalloc (abfd, fixup_stream_size);
3855       if (external_relocs == (char *) NULL)
3856         {
3857           bfd_set_error (bfd_error_no_memory);
3858           return false;
3859         }
3860       /* Read in the external forms. */
3861       if (bfd_seek (abfd,
3862                     obj_som_reloc_filepos (abfd) + section->rel_filepos,
3863                     SEEK_SET)
3864           != 0)
3865         {
3866           bfd_set_error (bfd_error_system_call);
3867           return false;
3868         }
3869       if (bfd_read (external_relocs, 1, fixup_stream_size, abfd)
3870           != fixup_stream_size)
3871         {
3872           bfd_set_error (bfd_error_system_call);
3873           return false;
3874         }
3875       /* Let callers know how many relocations found.
3876          also save the relocation stream as we will
3877          need it again.  */
3878       section->reloc_count = som_set_reloc_info (external_relocs,
3879                                                  fixup_stream_size,
3880                                                  NULL, NULL, NULL, true);
3881
3882       som_section_data (section)->reloc_stream = external_relocs;
3883     }
3884
3885   /* If the caller only wanted a count, then return now.  */
3886   if (just_count)
3887     return true;
3888
3889   num_relocs = section->reloc_count;
3890   external_relocs = som_section_data (section)->reloc_stream;
3891   /* Return saved information about the relocations if it is available.  */
3892   if (section->relocation != (arelent *) NULL)
3893     return true;
3894
3895   internal_relocs = (arelent *) bfd_zalloc (abfd,
3896                                             num_relocs * sizeof (arelent));
3897   if (internal_relocs == (arelent *) NULL)
3898     {
3899       bfd_set_error (bfd_error_no_memory);
3900       return false;
3901     }
3902
3903   /* Process and internalize the relocations.  */
3904   som_set_reloc_info (external_relocs, fixup_stream_size,
3905                       internal_relocs, section, symbols, false);
3906
3907   /* Save our results and return success.  */
3908   section->relocation = internal_relocs;
3909   return (true);
3910 }
3911
3912 /* Return the number of bytes required to store the relocation
3913    information associated with the given section.  */ 
3914
3915 static unsigned int
3916 som_get_reloc_upper_bound (abfd, asect)
3917      bfd *abfd;
3918      sec_ptr asect;
3919 {
3920   /* If section has relocations, then read in the relocation stream
3921      and parse it to determine how many relocations exist.  */
3922   if (asect->flags & SEC_RELOC)
3923     {
3924       if (som_slurp_reloc_table (abfd, asect, NULL, true))
3925         return (asect->reloc_count + 1) * sizeof (arelent);
3926     }
3927   /* Either there are no relocations or an error occurred while 
3928      reading and parsing the relocation stream.  */ 
3929   return 0;
3930 }
3931
3932 /* Convert relocations from SOM (external) form into BFD internal
3933    form.  Return the number of relocations.  */
3934
3935 static unsigned int
3936 som_canonicalize_reloc (abfd, section, relptr, symbols)
3937      bfd *abfd;
3938      sec_ptr section;
3939      arelent **relptr;
3940      asymbol **symbols;
3941 {
3942   arelent *tblptr;
3943   int count;
3944
3945   if (som_slurp_reloc_table (abfd, section, symbols, false) == false)
3946     return 0;
3947
3948   count = section->reloc_count;
3949   tblptr = section->relocation;
3950   if (tblptr == (arelent *) NULL)
3951     return 0;
3952
3953   while (count--)
3954     *relptr++ = tblptr++;
3955
3956   *relptr = (arelent *) NULL;
3957   return section->reloc_count;
3958 }
3959
3960 extern bfd_target som_vec;
3961
3962 /* A hook to set up object file dependent section information.  */
3963
3964 static boolean
3965 som_new_section_hook (abfd, newsect)
3966      bfd *abfd;
3967      asection *newsect;
3968 {
3969   newsect->used_by_bfd =
3970     (PTR) bfd_zalloc (abfd, sizeof (struct som_section_data_struct));
3971   if (!newsect->used_by_bfd)
3972     {
3973       bfd_set_error (bfd_error_no_memory);
3974       return false;
3975     }
3976   newsect->alignment_power = 3;
3977
3978   /* Initialize the subspace_index field to -1 so that it does
3979      not match a subspace with an index of 0.  */
3980   som_section_data (newsect)->subspace_index = -1;
3981
3982   /* We allow more than three sections internally */
3983   return true;
3984 }
3985
3986 /* Set backend info for sections which can not be described
3987    in the BFD data structures.  */
3988
3989 void
3990 bfd_som_set_section_attributes (section, defined, private, sort_key, spnum)
3991      asection *section;
3992      int defined;
3993      int private;
3994      unsigned int sort_key;
3995      int spnum;
3996 {
3997   struct space_dictionary_record *space_dict;
3998
3999   som_section_data (section)->is_space = 1;
4000   space_dict = &som_section_data (section)->space_dict;
4001   space_dict->is_defined = defined;
4002   space_dict->is_private = private;
4003   space_dict->sort_key = sort_key;
4004   space_dict->space_number = spnum;
4005 }
4006
4007 /* Set backend info for subsections which can not be described 
4008    in the BFD data structures.  */
4009
4010 void
4011 bfd_som_set_subsection_attributes (section, container, access,
4012                                    sort_key, quadrant)
4013      asection *section;
4014      asection *container;
4015      int access;
4016      unsigned int sort_key;
4017      int quadrant;
4018 {
4019   struct subspace_dictionary_record *subspace_dict;
4020   som_section_data (section)->is_subspace = 1;
4021   subspace_dict = &som_section_data (section)->subspace_dict;
4022   subspace_dict->access_control_bits = access;
4023   subspace_dict->sort_key = sort_key;
4024   subspace_dict->quadrant = quadrant;
4025   som_section_data (section)->containing_space = container;
4026 }
4027
4028 /* Set the full SOM symbol type.  SOM needs far more symbol information
4029    than any other object file format I'm aware of.  It is mandatory
4030    to be able to know if a symbol is an entry point, millicode, data,
4031    code, absolute, storage request, or procedure label.  If you get
4032    the symbol type wrong your program will not link.  */
4033
4034 void
4035 bfd_som_set_symbol_type (symbol, type)
4036      asymbol *symbol;
4037      unsigned int type;
4038 {
4039   som_symbol_data (symbol)->som_type = type;
4040 }
4041
4042 /* Attach 64bits of unwind information to a symbol (which hopefully
4043    is a function of some kind!).  It would be better to keep this
4044    in the R_ENTRY relocation, but there is not enough space.  */
4045
4046 void
4047 bfd_som_attach_unwind_info (symbol, unwind_desc)
4048      asymbol *symbol;
4049      char *unwind_desc;
4050 {
4051   som_symbol_data (symbol)->unwind = unwind_desc;
4052 }
4053
4054 /* Attach an auxiliary header to the BFD backend so that it may be
4055    written into the object file.  */
4056 boolean
4057 bfd_som_attach_aux_hdr (abfd, type, string)
4058      bfd *abfd;
4059      int type;
4060      char *string;
4061 {
4062   if (type == VERSION_AUX_ID)
4063     {
4064       int len = strlen (string);
4065       int pad = 0;
4066
4067       if (len % 4)
4068         pad = (4 - (len % 4));
4069       obj_som_version_hdr (abfd) = (struct user_string_aux_hdr *)
4070         bfd_zalloc (abfd, sizeof (struct aux_id)
4071                               + sizeof (unsigned int) + len + pad);
4072       if (!obj_som_version_hdr (abfd))
4073         {
4074           bfd_set_error (bfd_error_no_memory);
4075           return false;
4076         }
4077       obj_som_version_hdr (abfd)->header_id.type = VERSION_AUX_ID;
4078       obj_som_version_hdr (abfd)->header_id.length = len + pad;
4079       obj_som_version_hdr (abfd)->header_id.length += sizeof (int);
4080       obj_som_version_hdr (abfd)->string_length = len;
4081       strncpy (obj_som_version_hdr (abfd)->user_string, string, len);
4082     }
4083   else if (type == COPYRIGHT_AUX_ID)
4084     {
4085       int len = strlen (string);
4086       int pad = 0;
4087
4088       if (len % 4)
4089         pad = (4 - (len % 4));
4090       obj_som_copyright_hdr (abfd) = (struct copyright_aux_hdr *)
4091         bfd_zalloc (abfd, sizeof (struct aux_id)
4092                             + sizeof (unsigned int) + len + pad);
4093       if (!obj_som_copyright_hdr (abfd))
4094         {
4095           bfd_set_error (bfd_error_no_error);
4096           return false;
4097         }
4098       obj_som_copyright_hdr (abfd)->header_id.type = COPYRIGHT_AUX_ID;
4099       obj_som_copyright_hdr (abfd)->header_id.length = len + pad;
4100       obj_som_copyright_hdr (abfd)->header_id.length += sizeof (int);
4101       obj_som_copyright_hdr (abfd)->string_length = len;
4102       strcpy (obj_som_copyright_hdr (abfd)->copyright, string);
4103     }
4104   return true;
4105 }
4106
4107 static boolean
4108 som_set_section_contents (abfd, section, location, offset, count)
4109      bfd *abfd;
4110      sec_ptr section;
4111      PTR location;
4112      file_ptr offset;
4113      bfd_size_type count;
4114 {
4115   if (abfd->output_has_begun == false)
4116     {
4117       /* Set up fixed parts of the file, space, and subspace headers.
4118          Notify the world that output has begun.  */
4119       som_prep_headers (abfd);
4120       abfd->output_has_begun = true;
4121       /* Start writing the object file.  This include all the string
4122          tables, fixup streams, and other portions of the object file.  */
4123       som_begin_writing (abfd);
4124     }
4125
4126   /* Only write subspaces which have "real" contents (eg. the contents
4127      are not generated at run time by the OS).  */
4128   if (som_section_data (section)->is_subspace != 1
4129       || ((section->flags & (SEC_LOAD | SEC_DEBUGGING)) == 0))
4130     return true;
4131
4132   /* Seek to the proper offset within the object file and write the
4133      data.  */
4134   offset += som_section_data (section)->subspace_dict.file_loc_init_value; 
4135   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
4136     {
4137       bfd_set_error (bfd_error_system_call);
4138       return false;
4139     }
4140
4141   if (bfd_write ((PTR) location, 1, count, abfd) != count)
4142     {
4143       bfd_set_error (bfd_error_system_call);
4144       return false;
4145     }
4146   return true;
4147 }
4148
4149 static boolean
4150 som_set_arch_mach (abfd, arch, machine)
4151      bfd *abfd;
4152      enum bfd_architecture arch;
4153      unsigned long machine;
4154 {
4155   /* Allow any architecture to be supported by the SOM backend */
4156   return bfd_default_set_arch_mach (abfd, arch, machine);
4157 }
4158
4159 static boolean
4160 som_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
4161                         functionname_ptr, line_ptr)
4162      bfd *abfd;
4163      asection *section;
4164      asymbol **symbols;
4165      bfd_vma offset;
4166      CONST char **filename_ptr;
4167      CONST char **functionname_ptr;
4168      unsigned int *line_ptr;
4169 {
4170   fprintf (stderr, "som_find_nearest_line unimplemented\n");
4171   fflush (stderr);
4172   abort ();
4173   return (false);
4174 }
4175
4176 static int
4177 som_sizeof_headers (abfd, reloc)
4178      bfd *abfd;
4179      boolean reloc;
4180 {
4181   fprintf (stderr, "som_sizeof_headers unimplemented\n");
4182   fflush (stderr);
4183   abort ();
4184   return (0);
4185 }
4186
4187 /* Return the single-character symbol type corresponding to
4188    SOM section S, or '?' for an unknown SOM section.  */
4189
4190 static char
4191 som_section_type (s)
4192      const char *s;
4193 {
4194   const struct section_to_type *t;
4195
4196   for (t = &stt[0]; t->section; t++)
4197     if (!strcmp (s, t->section))
4198       return t->type;
4199   return '?';
4200 }
4201
4202 static int
4203 som_decode_symclass (symbol)
4204      asymbol *symbol;
4205 {
4206   char c;
4207
4208   if (bfd_is_com_section (symbol->section))
4209     return 'C';
4210   if (symbol->section == &bfd_und_section)
4211     return 'U';
4212   if (symbol->section == &bfd_ind_section)
4213     return 'I';
4214   if (!(symbol->flags & (BSF_GLOBAL|BSF_LOCAL)))
4215     return '?';
4216
4217   if (symbol->section == &bfd_abs_section)
4218     c = 'a';
4219   else if (symbol->section)
4220     c = som_section_type (symbol->section->name);
4221   else
4222     return '?';
4223   if (symbol->flags & BSF_GLOBAL)
4224     c = toupper (c);
4225   return c;
4226 }
4227
4228 /* Return information about SOM symbol SYMBOL in RET.  */
4229
4230 static void
4231 som_get_symbol_info (ignore_abfd, symbol, ret)
4232      bfd *ignore_abfd;
4233      asymbol *symbol;
4234      symbol_info *ret;
4235 {
4236   ret->type = som_decode_symclass (symbol);
4237   if (ret->type != 'U')
4238     ret->value = symbol->value+symbol->section->vma;
4239   else
4240     ret->value = 0;
4241   ret->name = symbol->name;
4242 }
4243
4244 /* Count the number of symbols in the archive symbol table.  Necessary
4245    so that we can allocate space for all the carsyms at once.  */
4246
4247 static boolean
4248 som_bfd_count_ar_symbols (abfd, lst_header, count)
4249      bfd *abfd;
4250      struct lst_header *lst_header;
4251      symindex *count;
4252 {
4253   unsigned int i;
4254   unsigned int *hash_table =
4255     (unsigned int *) alloca (lst_header->hash_size * sizeof (unsigned int));
4256   file_ptr lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header);
4257
4258   /* Don't forget to initialize the counter!  */
4259   *count = 0;
4260
4261   /* Read in the hash table.  The has table is an array of 32bit file offsets
4262      which point to the hash chains.  */
4263   if (bfd_read ((PTR) hash_table, lst_header->hash_size, 4, abfd)
4264       != lst_header->hash_size * 4)
4265     {
4266       bfd_set_error (bfd_error_system_call);
4267       return false;
4268     }
4269
4270   /* Walk each chain counting the number of symbols found on that particular
4271      chain.  */
4272   for (i = 0; i < lst_header->hash_size; i++)
4273     {
4274       struct lst_symbol_record lst_symbol;
4275
4276       /* An empty chain has zero as it's file offset.  */
4277       if (hash_table[i] == 0)
4278         continue;
4279
4280       /* Seek to the first symbol in this hash chain.  */
4281       if (bfd_seek (abfd, lst_filepos + hash_table[i], SEEK_SET) < 0)
4282         {
4283           bfd_set_error (bfd_error_system_call);
4284           return false;
4285         }
4286
4287       /* Read in this symbol and update the counter.  */
4288       if (bfd_read ((PTR) & lst_symbol, 1, sizeof (lst_symbol), abfd)
4289           != sizeof (lst_symbol))
4290         {
4291           bfd_set_error (bfd_error_system_call);
4292           return false;
4293         }
4294       (*count)++;
4295
4296       /* Now iterate through the rest of the symbols on this chain.  */
4297       while (lst_symbol.next_entry)
4298         {
4299
4300           /* Seek to the next symbol.  */
4301           if (bfd_seek (abfd, lst_filepos + lst_symbol.next_entry, SEEK_SET)
4302               < 0)
4303             {
4304               bfd_set_error (bfd_error_system_call);
4305               return false;
4306             }
4307
4308           /* Read the symbol in and update the counter.  */
4309           if (bfd_read ((PTR) & lst_symbol, 1, sizeof (lst_symbol), abfd)
4310               != sizeof (lst_symbol))
4311             {
4312               bfd_set_error (bfd_error_system_call);
4313               return false;
4314             }
4315           (*count)++;
4316         }
4317     }
4318   return true;
4319 }
4320
4321 /* Fill in the canonical archive symbols (SYMS) from the archive described
4322    by ABFD and LST_HEADER.  */
4323
4324 static boolean
4325 som_bfd_fill_in_ar_symbols (abfd, lst_header, syms)
4326      bfd *abfd;
4327      struct lst_header *lst_header;
4328      carsym **syms;
4329 {
4330   unsigned int i, len;
4331   carsym *set = syms[0];
4332   unsigned int *hash_table =
4333     (unsigned int *) alloca (lst_header->hash_size * sizeof (unsigned int));
4334   struct som_entry *som_dict =
4335     (struct som_entry *) alloca (lst_header->module_count
4336                                  * sizeof (struct som_entry));
4337   file_ptr lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header);
4338
4339   /* Read in the hash table.  The has table is an array of 32bit file offsets
4340      which point to the hash chains.  */
4341   if (bfd_read ((PTR) hash_table, lst_header->hash_size, 4, abfd)
4342       != lst_header->hash_size * 4)
4343     {
4344       bfd_set_error (bfd_error_system_call);
4345       return false;
4346     }
4347
4348   /* Seek to and read in the SOM dictionary.  We will need this to fill
4349      in the carsym's filepos field.  */
4350   if (bfd_seek (abfd, lst_filepos + lst_header->dir_loc, SEEK_SET) < 0)
4351     {
4352       bfd_set_error (bfd_error_system_call);
4353       return false;
4354     }
4355
4356   if (bfd_read ((PTR) som_dict, lst_header->module_count, 
4357                 sizeof (struct som_entry), abfd)
4358       != lst_header->module_count * sizeof (struct som_entry))
4359     {
4360       bfd_set_error (bfd_error_system_call);
4361       return false;
4362     }
4363
4364   /* Walk each chain filling in the carsyms as we go along.  */
4365   for (i = 0; i < lst_header->hash_size; i++)
4366     {
4367       struct lst_symbol_record lst_symbol;
4368
4369       /* An empty chain has zero as it's file offset.  */
4370       if (hash_table[i] == 0)
4371         continue;
4372
4373       /* Seek to and read the first symbol on the chain.  */
4374       if (bfd_seek (abfd, lst_filepos + hash_table[i], SEEK_SET) < 0)
4375         {
4376           bfd_set_error (bfd_error_system_call);
4377           return false;
4378         }
4379
4380       if (bfd_read ((PTR) & lst_symbol, 1, sizeof (lst_symbol), abfd)
4381           != sizeof (lst_symbol))
4382         {
4383           bfd_set_error (bfd_error_system_call);
4384           return false;
4385         }
4386
4387       /* Get the name of the symbol, first get the length which is stored
4388          as a 32bit integer just before the symbol.
4389
4390          One might ask why we don't just read in the entire string table
4391          and index into it.  Well, according to the SOM ABI the string
4392          index can point *anywhere* in the archive to save space, so just
4393          using the string table would not be safe.  */
4394       if (bfd_seek (abfd, lst_filepos + lst_header->string_loc
4395                             + lst_symbol.name.n_strx - 4, SEEK_SET) < 0)
4396         {
4397           bfd_set_error (bfd_error_system_call);
4398           return false;
4399         }
4400
4401       if (bfd_read (&len, 1, 4, abfd) != 4)
4402         {
4403           bfd_set_error (bfd_error_system_call);
4404           return false;
4405         }
4406
4407       /* Allocate space for the name and null terminate it too.  */
4408       set->name = bfd_zalloc (abfd, len + 1);
4409       if (!set->name)
4410         {
4411           bfd_set_error (bfd_error_no_memory);
4412           return false;
4413         }
4414       if (bfd_read (set->name, 1, len, abfd) != len)
4415         {
4416           bfd_set_error (bfd_error_system_call);
4417           return false;
4418         }
4419       set->name[len] = 0;
4420
4421       /* Fill in the file offset.  Note that the "location" field points
4422          to the SOM itself, not the ar_hdr in front of it.  */
4423       set->file_offset = som_dict[lst_symbol.som_index].location
4424                           - sizeof (struct ar_hdr);
4425
4426       /* Go to the next symbol.  */
4427       set++;
4428
4429       /* Iterate through the rest of the chain.  */
4430       while (lst_symbol.next_entry)
4431         {
4432           /* Seek to the next symbol and read it in.  */
4433           if (bfd_seek (abfd, lst_filepos + lst_symbol.next_entry, SEEK_SET)
4434               < 0)
4435             {
4436               bfd_set_error (bfd_error_system_call);
4437               return false;
4438             }
4439
4440           if (bfd_read ((PTR) & lst_symbol, 1, sizeof (lst_symbol), abfd)
4441               != sizeof (lst_symbol))
4442             {
4443               bfd_set_error (bfd_error_system_call);
4444               return false;
4445             }
4446
4447           /* Seek to the name length & string and read them in.  */
4448           if (bfd_seek (abfd, lst_filepos + lst_header->string_loc 
4449                                 + lst_symbol.name.n_strx - 4, SEEK_SET) < 0)
4450             {
4451               bfd_set_error (bfd_error_system_call);
4452               return false;
4453             }
4454
4455           if (bfd_read (&len, 1, 4, abfd) != 4)
4456             {
4457               bfd_set_error (bfd_error_system_call);
4458               return false;
4459             }
4460
4461           /* Allocate space for the name and null terminate it too.  */
4462           set->name = bfd_zalloc (abfd, len + 1);
4463           if (!set->name)
4464             {
4465               bfd_set_error (bfd_error_no_memory);
4466               return false;
4467             }
4468           if (bfd_read (set->name, 1, len, abfd) != len)
4469             {
4470               bfd_set_error (bfd_error_system_call);
4471               return false;
4472             }
4473           set->name[len] = 0;
4474
4475           /* Fill in the file offset.  Note that the "location" field points
4476              to the SOM itself, not the ar_hdr in front of it.  */
4477           set->file_offset = som_dict[lst_symbol.som_index].location
4478                                - sizeof (struct ar_hdr);
4479
4480           /* Go on to the next symbol.  */
4481           set++;
4482         }
4483     }
4484   /* If we haven't died by now, then we successfully read the entire 
4485      archive symbol table.  */
4486   return true;
4487 }
4488
4489 /* Read in the LST from the archive.  */
4490 static boolean
4491 som_slurp_armap (abfd)
4492      bfd *abfd;
4493 {
4494   struct lst_header lst_header;
4495   struct ar_hdr ar_header;
4496   unsigned int parsed_size;
4497   struct artdata *ardata = bfd_ardata (abfd);
4498   char nextname[17];
4499   int i = bfd_read ((PTR) nextname, 1, 16, abfd);
4500
4501   /* Special cases.  */
4502   if (i == 0)
4503     return true;
4504   if (i != 16)
4505     return false;
4506
4507   if (bfd_seek (abfd, (file_ptr) - 16, SEEK_CUR) < 0)
4508     {
4509       bfd_set_error (bfd_error_system_call);
4510       return false;
4511     }
4512
4513   /* For archives without .o files there is no symbol table.  */
4514   if (strncmp (nextname, "/               ", 16))
4515     {
4516       bfd_has_map (abfd) = false;
4517       return true;
4518     }
4519
4520   /* Read in and sanity check the archive header.  */
4521   if (bfd_read ((PTR) &ar_header, 1, sizeof (struct ar_hdr), abfd)
4522       != sizeof (struct ar_hdr))
4523     {
4524       bfd_set_error (bfd_error_system_call);
4525       return false;
4526     }
4527
4528   if (strncmp (ar_header.ar_fmag, ARFMAG, 2))
4529     {
4530       bfd_set_error (bfd_error_malformed_archive);
4531       return NULL;
4532     }
4533
4534   /* How big is the archive symbol table entry?  */
4535   errno = 0;
4536   parsed_size = strtol (ar_header.ar_size, NULL, 10);
4537   if (errno != 0)
4538     {
4539       bfd_set_error (bfd_error_malformed_archive);
4540       return NULL;
4541     }
4542
4543   /* Save off the file offset of the first real user data.  */
4544   ardata->first_file_filepos = bfd_tell (abfd) + parsed_size;
4545
4546   /* Read in the library symbol table.  We'll make heavy use of this
4547      in just a minute.  */
4548   if (bfd_read ((PTR) & lst_header, 1, sizeof (struct lst_header), abfd)
4549       != sizeof (struct lst_header))
4550     {
4551       bfd_set_error (bfd_error_system_call);
4552       return false;
4553     }
4554
4555   /* Sanity check.  */
4556   if (lst_header.a_magic != LIBMAGIC)
4557     {
4558       bfd_set_error (bfd_error_malformed_archive);
4559       return NULL;
4560     }
4561
4562   /* Count the number of symbols in the library symbol table.  */
4563   if (som_bfd_count_ar_symbols (abfd, &lst_header, &ardata->symdef_count)
4564       == false)
4565     return false;
4566
4567   /* Get back to the start of the library symbol table.  */
4568   if (bfd_seek (abfd, ardata->first_file_filepos - parsed_size 
4569                         + sizeof (struct lst_header), SEEK_SET) < 0)
4570     {
4571       bfd_set_error (bfd_error_system_call);
4572       return false;
4573     }
4574
4575   /* Initializae the cache and allocate space for the library symbols.  */
4576   ardata->cache = 0;
4577   ardata->symdefs = (carsym *) bfd_alloc (abfd,
4578                                           (ardata->symdef_count
4579                                            * sizeof (carsym)));
4580   if (!ardata->symdefs)
4581     {
4582       bfd_set_error (bfd_error_no_memory);
4583       return false;
4584     }
4585
4586   /* Now fill in the canonical archive symbols.  */
4587   if (som_bfd_fill_in_ar_symbols (abfd, &lst_header, &ardata->symdefs)
4588       == false)
4589     return false;
4590
4591   /* Notify the generic archive code that we have a symbol map.  */
4592   bfd_has_map (abfd) = true;
4593   return true;
4594 }
4595
4596 /* Begin preparing to write a SOM library symbol table.
4597
4598    As part of the prep work we need to determine the number of symbols
4599    and the size of the associated string section.  */
4600
4601 static boolean
4602 som_bfd_prep_for_ar_write (abfd, num_syms, stringsize)
4603      bfd *abfd;
4604      unsigned int *num_syms, *stringsize;
4605 {
4606   bfd *curr_bfd = abfd->archive_head;
4607
4608   /* Some initialization.  */
4609   *num_syms = 0;
4610   *stringsize = 0;
4611
4612   /* Iterate over each BFD within this archive.  */
4613   while (curr_bfd != NULL)
4614     {
4615       unsigned int curr_count, i;
4616       som_symbol_type *sym;
4617
4618       /* Make sure the symbol table has been read, then snag a pointer
4619          to it.  It's a little slimey to grab the symbols via obj_som_symtab,
4620          but doing so avoids allocating lots of extra memory.  */
4621       if (som_slurp_symbol_table (curr_bfd) == false)
4622         return false;
4623
4624       sym = obj_som_symtab (curr_bfd);
4625       curr_count = bfd_get_symcount (curr_bfd);
4626
4627       /* Examine each symbol to determine if it belongs in the
4628          library symbol table.  */
4629       for (i = 0; i < curr_count; i++, sym++)
4630         {
4631           struct som_misc_symbol_info info;
4632
4633           /* Derive SOM information from the BFD symbol.  */
4634           som_bfd_derive_misc_symbol_info (curr_bfd, &sym->symbol, &info);
4635
4636           /* Should we include this symbol?  */
4637           if (info.symbol_type == ST_NULL
4638               || info.symbol_type == ST_SYM_EXT
4639               || info.symbol_type == ST_ARG_EXT)
4640             continue;
4641
4642           /* Only global symbols and unsatisfied commons.  */
4643           if (info.symbol_scope != SS_UNIVERSAL
4644               && info.symbol_type != ST_STORAGE)
4645             continue;
4646
4647           /* Do no include undefined symbols.  */
4648           if (sym->symbol.section == &bfd_und_section)
4649             continue;
4650
4651           /* Bump the various counters, being careful to honor
4652              alignment considerations in the string table.  */
4653           (*num_syms)++;
4654           *stringsize = *stringsize + strlen (sym->symbol.name) + 5;
4655           while (*stringsize % 4)
4656             (*stringsize)++;
4657         }
4658
4659       curr_bfd = curr_bfd->next;
4660     }
4661   return true;
4662 }
4663
4664 /* Hash a symbol name based on the hashing algorithm presented in the
4665    SOM ABI.  */
4666 static unsigned int
4667 som_bfd_ar_symbol_hash (symbol)
4668      asymbol *symbol;
4669 {
4670   unsigned int len = strlen (symbol->name);
4671
4672   /* Names with length 1 are special.  */
4673   if (len == 1)
4674     return 0x1000100 | (symbol->name[0] << 16) | symbol->name[0];
4675
4676   return ((len & 0x7f) << 24) | (symbol->name[1] << 16)
4677           | (symbol->name[len-2] << 8) | symbol->name[len-1];
4678 }
4679
4680 /* Do the bulk of the work required to write the SOM library
4681    symbol table.  */
4682    
4683 static boolean
4684 som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst)
4685      bfd *abfd;
4686      unsigned int nsyms, string_size;
4687      struct lst_header lst;
4688 {
4689   file_ptr lst_filepos;
4690   char *strings, *p;
4691   struct lst_symbol_record *lst_syms, *curr_lst_sym;
4692   bfd *curr_bfd = abfd->archive_head;
4693   unsigned int *hash_table =
4694     (unsigned int *) alloca (lst.hash_size * sizeof (unsigned int));
4695   struct som_entry *som_dict =
4696     (struct som_entry *) alloca (lst.module_count
4697                                  * sizeof (struct som_entry));
4698   struct lst_symbol_record **last_hash_entry =
4699     ((struct lst_symbol_record **)
4700      alloca (lst.hash_size * sizeof (struct lst_symbol_record *)));
4701   unsigned int curr_som_offset, som_index;
4702
4703   /* Lots of fields are file positions relative to the start
4704      of the lst record.  So save its location.  */
4705   lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header);
4706
4707   /* Some initialization.  */
4708   memset (hash_table, 0, 4 * lst.hash_size);
4709   memset (som_dict, 0, lst.module_count * sizeof (struct som_entry));
4710   memset (last_hash_entry, 0,   
4711           lst.hash_size * sizeof (struct lst_symbol_record *));
4712
4713   /* Symbols have som_index fields, so we have to keep track of the
4714      index of each SOM in the archive.
4715
4716      The SOM dictionary has (among other things) the absolute file
4717      position for the SOM which a particular dictionary entry
4718      describes.  We have to compute that information as we iterate
4719      through the SOMs/symbols.  */
4720   som_index = 0;
4721   curr_som_offset = 8 + 2 * sizeof (struct ar_hdr) + lst.file_end;
4722
4723   /* FIXME should be done with buffers just like everything else... */
4724   lst_syms = alloca (nsyms * sizeof (struct lst_symbol_record));
4725   strings = alloca (string_size);
4726   p = strings;
4727   curr_lst_sym = lst_syms;
4728
4729
4730   while (curr_bfd != NULL)
4731     {
4732       unsigned int curr_count, i;
4733       som_symbol_type *sym;
4734
4735       /* Make sure the symbol table has been read, then snag a pointer
4736          to it.  It's a little slimey to grab the symbols via obj_som_symtab,
4737          but doing so avoids allocating lots of extra memory.  */
4738       if (som_slurp_symbol_table (curr_bfd) == false)
4739         return false;
4740
4741       sym = obj_som_symtab (curr_bfd);
4742       curr_count = bfd_get_symcount (curr_bfd);
4743
4744       for (i = 0; i < curr_count; i++, sym++)
4745         {
4746           struct som_misc_symbol_info info;
4747
4748           /* Derive SOM information from the BFD symbol.  */
4749           som_bfd_derive_misc_symbol_info (curr_bfd, &sym->symbol, &info);
4750
4751           /* Should we include this symbol?  */
4752           if (info.symbol_type == ST_NULL
4753               || info.symbol_type == ST_SYM_EXT
4754               || info.symbol_type == ST_ARG_EXT)
4755             continue;
4756
4757           /* Only global symbols and unsatisfied commons.  */
4758           if (info.symbol_scope != SS_UNIVERSAL
4759               && info.symbol_type != ST_STORAGE)
4760             continue;
4761
4762           /* Do no include undefined symbols.  */
4763           if (sym->symbol.section == &bfd_und_section)
4764             continue;
4765
4766           /* If this is the first symbol from this SOM, then update
4767              the SOM dictionary too.  */
4768           if (som_dict[som_index].location == 0)
4769             {
4770               som_dict[som_index].location = curr_som_offset;
4771               som_dict[som_index].length = arelt_size (curr_bfd);
4772             }
4773
4774           /* Fill in the lst symbol record.  */
4775           curr_lst_sym->hidden = 0;
4776           curr_lst_sym->secondary_def = 0;
4777           curr_lst_sym->symbol_type = info.symbol_type;
4778           curr_lst_sym->symbol_scope = info.symbol_scope;
4779           curr_lst_sym->check_level = 0;
4780           curr_lst_sym->must_qualify = 0;
4781           curr_lst_sym->initially_frozen = 0;
4782           curr_lst_sym->memory_resident = 0;
4783           curr_lst_sym->is_common = (sym->symbol.section == &bfd_com_section);
4784           curr_lst_sym->dup_common = 0;
4785           curr_lst_sym->xleast = 0;
4786           curr_lst_sym->arg_reloc = info.arg_reloc;
4787           curr_lst_sym->name.n_strx = p - strings + 4;
4788           curr_lst_sym->qualifier_name.n_strx = 0;
4789           curr_lst_sym->symbol_info = info.symbol_info;
4790           curr_lst_sym->symbol_value = info.symbol_value;
4791           curr_lst_sym->symbol_descriptor = 0;
4792           curr_lst_sym->reserved = 0;
4793           curr_lst_sym->som_index = som_index;
4794           curr_lst_sym->symbol_key = som_bfd_ar_symbol_hash (&sym->symbol);
4795           curr_lst_sym->next_entry = 0;
4796
4797           /* Insert into the hash table.  */
4798           if (hash_table[curr_lst_sym->symbol_key % lst.hash_size])
4799             {
4800               struct lst_symbol_record *tmp;
4801
4802               /* There is already something at the head of this hash chain,
4803                  so tack this symbol onto the end of the chain.  */
4804               tmp = last_hash_entry[curr_lst_sym->symbol_key % lst.hash_size];
4805               tmp->next_entry
4806                 = (curr_lst_sym - lst_syms) * sizeof (struct lst_symbol_record)
4807                   + lst.hash_size * 4 
4808                   + lst.module_count * sizeof (struct som_entry)
4809                   + sizeof (struct lst_header);
4810             }
4811           else
4812             {
4813               /* First entry in this hash chain.  */
4814               hash_table[curr_lst_sym->symbol_key % lst.hash_size]
4815                 = (curr_lst_sym - lst_syms) * sizeof (struct lst_symbol_record)
4816                   + lst.hash_size * 4 
4817                   + lst.module_count * sizeof (struct som_entry)
4818                   + sizeof (struct lst_header);
4819             }
4820
4821           /* Keep track of the last symbol we added to this chain so we can
4822              easily update its next_entry pointer.  */
4823           last_hash_entry[curr_lst_sym->symbol_key % lst.hash_size]
4824             = curr_lst_sym;
4825
4826
4827           /* Update the string table.  */
4828           bfd_put_32 (abfd, strlen (sym->symbol.name), p);
4829           p += 4;
4830           strcpy (p, sym->symbol.name);
4831           p += strlen (sym->symbol.name) + 1;
4832           while ((int)p % 4)
4833             {
4834               bfd_put_8 (abfd, 0, p);
4835               p++;
4836             }
4837
4838           /* Head to the next symbol.  */
4839           curr_lst_sym++;
4840         }
4841
4842       /* Keep track of where each SOM will finally reside; then look
4843          at the next BFD.  */
4844       curr_som_offset += arelt_size (curr_bfd) + sizeof (struct ar_hdr);
4845       curr_bfd = curr_bfd->next;
4846       som_index++;
4847     }
4848
4849   /* Now scribble out the hash table.  */
4850   if (bfd_write ((PTR) hash_table, lst.hash_size, 4, abfd)
4851       != lst.hash_size * 4)
4852     {
4853       bfd_set_error (bfd_error_system_call);
4854       return false;
4855     }
4856
4857   /* Then the SOM dictionary.  */
4858   if (bfd_write ((PTR) som_dict, lst.module_count,
4859                  sizeof (struct som_entry), abfd)
4860       != lst.module_count * sizeof (struct som_entry))
4861     {
4862       bfd_set_error (bfd_error_system_call);
4863       return false;
4864     }
4865
4866   /* The library symbols.  */
4867   if (bfd_write ((PTR) lst_syms, nsyms, sizeof (struct lst_symbol_record), abfd)
4868       != nsyms * sizeof (struct lst_symbol_record))
4869     {
4870       bfd_set_error (bfd_error_system_call);
4871       return false;
4872     }
4873
4874   /* And finally the strings.  */
4875   if (bfd_write ((PTR) strings, string_size, 1, abfd) != string_size)
4876     {
4877       bfd_set_error (bfd_error_system_call);
4878       return false;
4879     }
4880
4881   return true;
4882 }
4883
4884 /* Write out the LST for the archive.
4885
4886    You'll never believe this is really how armaps are handled in SOM...  */
4887
4888 static boolean
4889 som_write_armap (abfd)
4890      bfd *abfd;
4891 {
4892   bfd *curr_bfd;
4893   struct stat statbuf;
4894   unsigned int i, lst_size, nsyms, stringsize;
4895   struct ar_hdr hdr;
4896   struct lst_header lst;
4897   int *p;
4898  
4899   /* We'll use this for the archive's date and mode later.  */
4900   if (stat (abfd->filename, &statbuf) != 0)
4901     {
4902       bfd_set_error (bfd_error_system_call);
4903       return false;
4904     }
4905   /* Fudge factor.  */
4906   bfd_ardata (abfd)->armap_timestamp = statbuf.st_mtime + 60;
4907
4908   /* Account for the lst header first.  */
4909   lst_size = sizeof (struct lst_header);
4910
4911   /* Start building the LST header.  */
4912   lst.system_id = HP9000S800_ID;
4913   lst.a_magic = LIBMAGIC;
4914   lst.version_id = VERSION_ID;
4915   lst.file_time.secs = 0;
4916   lst.file_time.nanosecs = 0;
4917
4918   lst.hash_loc = lst_size;
4919   lst.hash_size = SOM_LST_HASH_SIZE;
4920
4921   /* Hash table is a SOM_LST_HASH_SIZE 32bit offsets.  */
4922   lst_size += 4 * SOM_LST_HASH_SIZE;
4923
4924   /* We need to count the number of SOMs in this archive.  */
4925   curr_bfd = abfd->archive_head;
4926   lst.module_count = 0;
4927   while (curr_bfd != NULL)
4928     {
4929       lst.module_count++;
4930       curr_bfd = curr_bfd->next;
4931     }
4932   lst.module_limit = lst.module_count;
4933   lst.dir_loc = lst_size;
4934   lst_size += sizeof (struct som_entry) * lst.module_count;
4935
4936   /* We don't support import/export tables, auxiliary headers,
4937      or free lists yet.  Make the linker work a little harder
4938      to make our life easier.  */
4939
4940   lst.export_loc = 0;
4941   lst.export_count = 0;
4942   lst.import_loc = 0;
4943   lst.aux_loc = 0;
4944   lst.aux_size = 0;
4945
4946   /* Count how many symbols we will have on the hash chains and the
4947      size of the associated string table.  */
4948   if (som_bfd_prep_for_ar_write (abfd, &nsyms, &stringsize) == false)
4949     return false;
4950
4951   lst_size += sizeof (struct lst_symbol_record) * nsyms;
4952
4953   /* For the string table.  One day we might actually use this info
4954      to avoid small seeks/reads when reading archives.  */
4955   lst.string_loc = lst_size;
4956   lst.string_size = stringsize;
4957   lst_size += stringsize;
4958
4959   /* SOM ABI says this must be zero.  */
4960   lst.free_list = 0;
4961
4962   lst.file_end = lst_size;
4963
4964   /* Compute the checksum.  Must happen after the entire lst header
4965      has filled in.  */
4966   p = (int *)&lst;
4967   for (i = 0; i < sizeof (struct lst_header)/sizeof (int) - 1; i++)
4968     lst.checksum ^= *p++;
4969
4970   sprintf (hdr.ar_name, "/               ");
4971   sprintf (hdr.ar_date, "%ld", bfd_ardata (abfd)->armap_timestamp);
4972   sprintf (hdr.ar_uid, "%d", getuid ());
4973   sprintf (hdr.ar_gid, "%d", getgid ());
4974   sprintf (hdr.ar_mode, "%-8o", (unsigned int) statbuf.st_mode);
4975   sprintf (hdr.ar_size, "%-10d", (int) lst_size);
4976   hdr.ar_fmag[0] = '`';
4977   hdr.ar_fmag[1] = '\012';
4978
4979   /* Turn any nulls into spaces.  */
4980   for (i = 0; i < sizeof (struct ar_hdr); i++)
4981     if (((char *) (&hdr))[i] == '\0')
4982       (((char *) (&hdr))[i]) = ' ';
4983
4984   /* Scribble out the ar header.  */
4985   if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
4986       != sizeof (struct ar_hdr))
4987     {
4988       bfd_set_error (bfd_error_system_call);
4989       return false;
4990     }
4991
4992   /* Now scribble out the lst header.  */
4993   if (bfd_write ((PTR) &lst, 1, sizeof (struct lst_header), abfd)
4994       != sizeof (struct lst_header))
4995     {
4996       bfd_set_error (bfd_error_system_call);
4997       return false;
4998     }
4999
5000   /* Build and write the armap.  */
5001   if (som_bfd_ar_write_symbol_stuff (abfd, nsyms, stringsize, lst) == false)
5002     return false;
5003   
5004   /* Done.  */
5005   return true;
5006 }
5007
5008 /* Apparently the extened names are never used, even though they appear
5009    in the SOM ABI.  Hmmm.  */
5010 static boolean
5011 som_slurp_extended_name_table (abfd)
5012      bfd *abfd;
5013 {
5014   bfd_ardata (abfd)->extended_names = NULL;
5015   return true;
5016 }
5017
5018 /* End of miscellaneous support functions. */
5019
5020 #define som_bfd_debug_info_start        bfd_void
5021 #define som_bfd_debug_info_end          bfd_void
5022 #define som_bfd_debug_info_accumulate   (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
5023
5024 #define som_openr_next_archived_file    bfd_generic_openr_next_archived_file
5025 #define som_generic_stat_arch_elt       bfd_generic_stat_arch_elt
5026 #define som_truncate_arname             bfd_bsd_truncate_arname
5027
5028 #define som_get_lineno                   (struct lineno_cache_entry *(*)())bfd_nullvoidptr
5029 #define som_close_and_cleanup              bfd_generic_close_and_cleanup
5030 #define som_get_section_contents          bfd_generic_get_section_contents
5031
5032 #define som_bfd_get_relocated_section_contents \
5033  bfd_generic_get_relocated_section_contents
5034 #define som_bfd_relax_section bfd_generic_relax_section
5035 #define som_bfd_make_debug_symbol \
5036   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
5037 #define som_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
5038 #define som_bfd_link_add_symbols _bfd_generic_link_add_symbols
5039 #define som_bfd_final_link _bfd_generic_final_link
5040
5041 /* Core file support is in the hpux-core backend.  */
5042 #define som_core_file_failing_command   _bfd_dummy_core_file_failing_command
5043 #define som_core_file_failing_signal    _bfd_dummy_core_file_failing_signal
5044 #define som_core_file_matches_executable_p      _bfd_dummy_core_file_matches_executable_p
5045
5046 bfd_target som_vec =
5047 {
5048   "som",                        /* name */
5049   bfd_target_som_flavour,
5050   true,                         /* target byte order */
5051   true,                         /* target headers byte order */
5052   (HAS_RELOC | EXEC_P |         /* object flags */
5053    HAS_LINENO | HAS_DEBUG |
5054    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
5055   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
5056    | SEC_ALLOC | SEC_LOAD | SEC_RELOC),         /* section flags */
5057
5058 /* leading_symbol_char: is the first char of a user symbol
5059    predictable, and if so what is it */
5060   0,
5061   '/',                          /* ar_pad_char */
5062   16,                           /* ar_max_namelen */
5063   3,                            /* minimum alignment */
5064   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
5065   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
5066   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
5067   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
5068   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
5069   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
5070   {_bfd_dummy_target,
5071    som_object_p,                /* bfd_check_format */
5072    bfd_generic_archive_p,
5073    _bfd_dummy_target
5074   },
5075   {
5076     bfd_false,
5077     som_mkobject,
5078     _bfd_generic_mkarchive,
5079     bfd_false
5080   },
5081   {
5082     bfd_false,
5083     som_write_object_contents,
5084     _bfd_write_archive_contents,
5085     bfd_false,
5086   },
5087 #undef som
5088   JUMP_TABLE (som),
5089   (PTR) 0
5090 };
5091
5092 #endif /* HOST_HPPAHPUX || HOST_HPPABSD || HOST_HPPAOSF */