* hpux-core.c (hpux_core_core_file_p): Add cast in call to
[external/binutils.git] / bfd / som.c
1 /* bfd back-end for HP PA-RISC SOM objects.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003
4    Free Software Foundation, Inc.
5
6    Contributed by the Center for Software Science at the
7    University of Utah.
8
9    This file is part of BFD, the Binary File Descriptor library.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "alloca-conf.h"
27 #include "bfd.h"
28 #include "sysdep.h"
29
30 #if defined (HOST_HPPAHPUX) || defined (HOST_HPPABSD) || defined (HOST_HPPAOSF) || defined(HOST_HPPAMPEIX)
31
32 #include "libbfd.h"
33 #include "som.h"
34 #include "safe-ctype.h"
35
36 #include <sys/param.h>
37 #include <signal.h>
38 #include <machine/reg.h>
39 #include <sys/file.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 CPU_PA_RISC2_0
52 #define CPU_PA_RISC2_0 0x214
53 #endif /* CPU_PA_RISC2_0 */
54
55 #ifndef _PA_RISC1_0_ID
56 #define _PA_RISC1_0_ID CPU_PA_RISC1_0
57 #endif /* _PA_RISC1_0_ID */
58
59 #ifndef _PA_RISC1_1_ID
60 #define _PA_RISC1_1_ID CPU_PA_RISC1_1
61 #endif /* _PA_RISC1_1_ID */
62
63 #ifndef _PA_RISC2_0_ID
64 #define _PA_RISC2_0_ID CPU_PA_RISC2_0
65 #endif /* _PA_RISC2_0_ID */
66
67 #ifndef _PA_RISC_MAXID
68 #define _PA_RISC_MAXID  0x2FF
69 #endif /* _PA_RISC_MAXID */
70
71 #ifndef _PA_RISC_ID
72 #define _PA_RISC_ID(__m_num)            \
73     (((__m_num) == _PA_RISC1_0_ID) ||   \
74      ((__m_num) >= _PA_RISC1_1_ID && (__m_num) <= _PA_RISC_MAXID))
75 #endif /* _PA_RISC_ID */
76
77 /* HIUX in it's infinite stupidity changed the names for several "well
78    known" constants.  Work around such braindamage.  Try the HPUX version
79    first, then the HIUX version, and finally provide a default.  */
80 #ifdef HPUX_AUX_ID
81 #define EXEC_AUX_ID HPUX_AUX_ID
82 #endif
83
84 #if !defined (EXEC_AUX_ID) && defined (HIUX_AUX_ID)
85 #define EXEC_AUX_ID HIUX_AUX_ID
86 #endif
87
88 #ifndef EXEC_AUX_ID
89 #define EXEC_AUX_ID 0
90 #endif
91
92 /* Size (in chars) of the temporary buffers used during fixup and string
93    table writes.   */
94
95 #define SOM_TMP_BUFSIZE 8192
96
97 /* Size of the hash table in archives.  */
98 #define SOM_LST_HASH_SIZE 31
99
100 /* Max number of SOMs to be found in an archive.  */
101 #define SOM_LST_MODULE_LIMIT 1024
102
103 /* Generic alignment macro.  */
104 #define SOM_ALIGN(val, alignment) \
105   (((val) + (alignment) - 1) &~ ((unsigned long) (alignment) - 1))
106
107 /* SOM allows any one of the four previous relocations to be reused
108    with a "R_PREV_FIXUP" relocation entry.  Since R_PREV_FIXUP
109    relocations are always a single byte, using a R_PREV_FIXUP instead
110    of some multi-byte relocation makes object files smaller.
111
112    Note one side effect of using a R_PREV_FIXUP is the relocation that
113    is being repeated moves to the front of the queue.  */
114 struct reloc_queue {
115   unsigned char *reloc;
116   unsigned int size;
117 } reloc_queue[4];
118
119 /* This fully describes the symbol types which may be attached to
120    an EXPORT or IMPORT directive.  Only SOM uses this formation
121    (ELF has no need for it).  */
122 typedef enum {
123   SYMBOL_TYPE_UNKNOWN,
124   SYMBOL_TYPE_ABSOLUTE,
125   SYMBOL_TYPE_CODE,
126   SYMBOL_TYPE_DATA,
127   SYMBOL_TYPE_ENTRY,
128   SYMBOL_TYPE_MILLICODE,
129   SYMBOL_TYPE_PLABEL,
130   SYMBOL_TYPE_PRI_PROG,
131   SYMBOL_TYPE_SEC_PROG,
132 } pa_symbol_type;
133
134 struct section_to_type {
135   char *section;
136   char type;
137 };
138
139 /* Assorted symbol information that needs to be derived from the BFD symbol
140    and/or the BFD backend private symbol data.  */
141 struct som_misc_symbol_info {
142   unsigned int symbol_type;
143   unsigned int symbol_scope;
144   unsigned int arg_reloc;
145   unsigned int symbol_info;
146   unsigned int symbol_value;
147   unsigned int priv_level;
148   unsigned int secondary_def;
149 };
150
151 /* Forward declarations.  */
152
153 static bfd_boolean som_mkobject
154   PARAMS ((bfd *));
155 static const bfd_target * som_object_setup
156   PARAMS ((bfd *, struct header *, struct som_exec_auxhdr *, unsigned long));
157 static bfd_boolean setup_sections
158   PARAMS ((bfd *, struct header *, unsigned long));
159 static const bfd_target * som_object_p
160   PARAMS ((bfd *));
161 static bfd_boolean som_write_object_contents
162   PARAMS ((bfd *));
163 static bfd_boolean som_slurp_string_table
164   PARAMS ((bfd *));
165 static unsigned int som_slurp_symbol_table
166   PARAMS ((bfd *));
167 static long som_get_symtab_upper_bound
168   PARAMS ((bfd *));
169 static long som_canonicalize_reloc
170   PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
171 static long som_get_reloc_upper_bound
172   PARAMS ((bfd *, sec_ptr));
173 static unsigned int som_set_reloc_info
174   PARAMS ((unsigned char *, unsigned int, arelent *, asection *,
175            asymbol **, bfd_boolean));
176 static bfd_boolean som_slurp_reloc_table
177   PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
178 static long som_canonicalize_symtab
179   PARAMS ((bfd *, asymbol **));
180 static asymbol * som_make_empty_symbol
181   PARAMS ((bfd *));
182 static void som_print_symbol
183   PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
184 static bfd_boolean som_new_section_hook
185   PARAMS ((bfd *, asection *));
186 static bfd_boolean som_bfd_copy_private_symbol_data
187   PARAMS ((bfd *, asymbol *, bfd *, asymbol *));
188 static bfd_boolean som_bfd_copy_private_section_data
189   PARAMS ((bfd *, asection *, bfd *, asection *));
190 static bfd_boolean som_bfd_copy_private_bfd_data
191   PARAMS ((bfd *, bfd *));
192 #define som_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
193 #define som_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
194 static bfd_boolean som_bfd_is_local_label_name
195   PARAMS ((bfd *, const char *));
196 static bfd_boolean som_set_section_contents
197   PARAMS ((bfd *, sec_ptr, const PTR, file_ptr, bfd_size_type));
198 static bfd_boolean som_get_section_contents
199   PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
200 static bfd_boolean som_set_arch_mach
201   PARAMS ((bfd *, enum bfd_architecture, unsigned long));
202 static bfd_boolean som_find_nearest_line
203   PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
204            const char **, unsigned int *));
205 static void som_get_symbol_info
206   PARAMS ((bfd *, asymbol *, symbol_info *));
207 static asection * bfd_section_from_som_symbol
208   PARAMS ((bfd *, struct symbol_dictionary_record *));
209 static int log2
210   PARAMS ((unsigned int));
211 static bfd_reloc_status_type hppa_som_reloc
212   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
213 static void som_initialize_reloc_queue
214   PARAMS ((struct reloc_queue *));
215 static void som_reloc_queue_insert
216   PARAMS ((unsigned char *, unsigned int, struct reloc_queue *));
217 static void som_reloc_queue_fix
218   PARAMS ((struct reloc_queue *, unsigned int));
219 static int som_reloc_queue_find
220   PARAMS ((unsigned char *, unsigned int, struct reloc_queue *));
221 static unsigned char * try_prev_fixup
222   PARAMS ((bfd *, int *, unsigned char *, unsigned int, struct reloc_queue *));
223 static unsigned char * som_reloc_skip
224   PARAMS ((bfd *, unsigned int, unsigned char *, unsigned int *,
225            struct reloc_queue *));
226 static unsigned char * som_reloc_addend
227   PARAMS ((bfd *, bfd_vma, unsigned char *, unsigned int *,
228            struct reloc_queue *));
229 static unsigned char * som_reloc_call
230   PARAMS ((bfd *, unsigned char *, unsigned int *, arelent *, int,
231            struct reloc_queue *));
232 static unsigned long som_count_spaces
233   PARAMS ((bfd *));
234 static unsigned long som_count_subspaces
235   PARAMS ((bfd *));
236 static int compare_syms
237   PARAMS ((const void *, const void *));
238 static int compare_subspaces
239   PARAMS ((const void *, const void *));
240 static unsigned long som_compute_checksum
241   PARAMS ((bfd *));
242 static bfd_boolean som_prep_headers
243   PARAMS ((bfd *));
244 static int som_sizeof_headers
245   PARAMS ((bfd *, bfd_boolean));
246 static bfd_boolean som_finish_writing
247   PARAMS ((bfd *));
248 static bfd_boolean som_build_and_write_symbol_table
249   PARAMS ((bfd *));
250 static void som_prep_for_fixups
251   PARAMS ((bfd *, asymbol **, unsigned long));
252 static bfd_boolean som_write_fixups
253   PARAMS ((bfd *, unsigned long, unsigned int *));
254 static bfd_boolean som_write_space_strings
255   PARAMS ((bfd *, unsigned long, unsigned int *));
256 static bfd_boolean som_write_symbol_strings
257   PARAMS ((bfd *, unsigned long, asymbol **, unsigned int, unsigned *,
258            COMPUNIT *));
259 static bfd_boolean som_begin_writing
260   PARAMS ((bfd *));
261 static reloc_howto_type * som_bfd_reloc_type_lookup
262   PARAMS ((bfd *, bfd_reloc_code_real_type));
263 static char som_section_type
264   PARAMS ((const char *));
265 static int som_decode_symclass
266   PARAMS ((asymbol *));
267 static bfd_boolean som_bfd_count_ar_symbols
268   PARAMS ((bfd *, struct lst_header *, symindex *));
269 static bfd_boolean som_bfd_fill_in_ar_symbols
270   PARAMS ((bfd *, struct lst_header *, carsym **));
271 static bfd_boolean som_slurp_armap
272   PARAMS ((bfd *));
273 static bfd_boolean som_write_armap
274   PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
275 static void som_bfd_derive_misc_symbol_info
276   PARAMS ((bfd *, asymbol *, struct som_misc_symbol_info *));
277 static bfd_boolean som_bfd_prep_for_ar_write
278   PARAMS ((bfd *, unsigned int *, unsigned int *));
279 static unsigned int som_bfd_ar_symbol_hash
280   PARAMS ((asymbol *));
281 static bfd_boolean som_bfd_ar_write_symbol_stuff
282   PARAMS ((bfd *, unsigned int, unsigned int, struct lst_header,
283            unsigned int));
284 static bfd_boolean som_is_space
285   PARAMS ((asection *));
286 static bfd_boolean som_is_subspace
287   PARAMS ((asection *));
288 static bfd_boolean som_is_container
289   PARAMS ((asection *, asection *));
290 static bfd_boolean som_bfd_free_cached_info
291   PARAMS ((bfd *));
292 static bfd_boolean som_bfd_link_split_section
293   PARAMS ((bfd *, asection *));
294
295 /* Map SOM section names to POSIX/BSD single-character symbol types.
296
297    This table includes all the standard subspaces as defined in the
298    current "PRO ABI for PA-RISC Systems", $UNWIND$ which for
299    some reason was left out, and sections specific to embedded stabs.  */
300
301 static const struct section_to_type stt[] = {
302   {"$TEXT$", 't'},
303   {"$SHLIB_INFO$", 't'},
304   {"$MILLICODE$", 't'},
305   {"$LIT$", 't'},
306   {"$CODE$", 't'},
307   {"$UNWIND_START$", 't'},
308   {"$UNWIND$", 't'},
309   {"$PRIVATE$", 'd'},
310   {"$PLT$", 'd'},
311   {"$SHLIB_DATA$", 'd'},
312   {"$DATA$", 'd'},
313   {"$SHORTDATA$", 'g'},
314   {"$DLT$", 'd'},
315   {"$GLOBAL$", 'g'},
316   {"$SHORTBSS$", 's'},
317   {"$BSS$", 'b'},
318   {"$GDB_STRINGS$", 'N'},
319   {"$GDB_SYMBOLS$", 'N'},
320   {0, 0}
321 };
322
323 /* About the relocation formatting table...
324
325    There are 256 entries in the table, one for each possible
326    relocation opcode available in SOM.  We index the table by
327    the relocation opcode.  The names and operations are those
328    defined by a.out_800 (4).
329
330    Right now this table is only used to count and perform minimal
331    processing on relocation streams so that they can be internalized
332    into BFD and symbolically printed by utilities.  To make actual use
333    of them would be much more difficult, BFD's concept of relocations
334    is far too simple to handle SOM relocations.  The basic assumption
335    that a relocation can be completely processed independent of other
336    relocations before an object file is written is invalid for SOM.
337
338    The SOM relocations are meant to be processed as a stream, they
339    specify copying of data from the input section to the output section
340    while possibly modifying the data in some manner.  They also can
341    specify that a variable number of zeros or uninitialized data be
342    inserted on in the output segment at the current offset.  Some
343    relocations specify that some previous relocation be re-applied at
344    the current location in the input/output sections.  And finally a number
345    of relocations have effects on other sections (R_ENTRY, R_EXIT,
346    R_UNWIND_AUX and a variety of others).  There isn't even enough room
347    in the BFD relocation data structure to store enough information to
348    perform all the relocations.
349
350    Each entry in the table has three fields.
351
352    The first entry is an index into this "class" of relocations.  This
353    index can then be used as a variable within the relocation itself.
354
355    The second field is a format string which actually controls processing
356    of the relocation.  It uses a simple postfix machine to do calculations
357    based on variables/constants found in the string and the relocation
358    stream.
359
360    The third field specifys whether or not this relocation may use
361    a constant (V) from the previous R_DATA_OVERRIDE rather than a constant
362    stored in the instruction.
363
364    Variables:
365
366    L = input space byte count
367    D = index into class of relocations
368    M = output space byte count
369    N = statement number (unused?)
370    O = stack operation
371    R = parameter relocation bits
372    S = symbol index
373    T = first 32 bits of stack unwind information
374    U = second 32 bits of stack unwind information
375    V = a literal constant (usually used in the next relocation)
376    P = a previous relocation
377
378    Lower case letters (starting with 'b') refer to following
379    bytes in the relocation stream.  'b' is the next 1 byte,
380    c is the next 2 bytes, d is the next 3 bytes, etc...
381    This is the variable part of the relocation entries that
382    makes our life a living hell.
383
384    numerical constants are also used in the format string.  Note
385    the constants are represented in decimal.
386
387    '+', "*" and "=" represents the obvious postfix operators.
388    '<' represents a left shift.
389
390    Stack Operations:
391
392    Parameter Relocation Bits:
393
394    Unwind Entries:
395
396    Previous Relocations:  The index field represents which in the queue
397    of 4 previous fixups should be re-applied.
398
399    Literal Constants:  These are generally used to represent addend
400    parts of relocations when these constants are not stored in the
401    fields of the instructions themselves.  For example the instruction
402    addil foo-$global$-0x1234 would use an override for "0x1234" rather
403    than storing it into the addil itself.  */
404
405 struct fixup_format {
406   int D;
407   const char *format;
408 };
409
410 static const struct fixup_format som_fixup_formats[256] = {
411   /* R_NO_RELOCATION */
412   {  0, "LD1+4*=" },            /* 0x00 */
413   {  1, "LD1+4*=" },            /* 0x01 */
414   {  2, "LD1+4*=" },            /* 0x02 */
415   {  3, "LD1+4*=" },            /* 0x03 */
416   {  4, "LD1+4*=" },            /* 0x04 */
417   {  5, "LD1+4*=" },            /* 0x05 */
418   {  6, "LD1+4*=" },            /* 0x06 */
419   {  7, "LD1+4*=" },            /* 0x07 */
420   {  8, "LD1+4*=" },            /* 0x08 */
421   {  9, "LD1+4*=" },            /* 0x09 */
422   { 10, "LD1+4*=" },            /* 0x0a */
423   { 11, "LD1+4*=" },            /* 0x0b */
424   { 12, "LD1+4*=" },            /* 0x0c */
425   { 13, "LD1+4*=" },            /* 0x0d */
426   { 14, "LD1+4*=" },            /* 0x0e */
427   { 15, "LD1+4*=" },            /* 0x0f */
428   { 16, "LD1+4*=" },            /* 0x10 */
429   { 17, "LD1+4*=" },            /* 0x11 */
430   { 18, "LD1+4*=" },            /* 0x12 */
431   { 19, "LD1+4*=" },            /* 0x13 */
432   { 20, "LD1+4*=" },            /* 0x14 */
433   { 21, "LD1+4*=" },            /* 0x15 */
434   { 22, "LD1+4*=" },            /* 0x16 */
435   { 23, "LD1+4*=" },            /* 0x17 */
436   {  0, "LD8<b+1+4*=" },        /* 0x18 */
437   {  1, "LD8<b+1+4*=" },        /* 0x19 */
438   {  2, "LD8<b+1+4*=" },        /* 0x1a */
439   {  3, "LD8<b+1+4*=" },        /* 0x1b */
440   {  0, "LD16<c+1+4*=" },       /* 0x1c */
441   {  1, "LD16<c+1+4*=" },       /* 0x1d */
442   {  2, "LD16<c+1+4*=" },       /* 0x1e */
443   {  0, "Ld1+=" },              /* 0x1f */
444   /* R_ZEROES */
445   {  0, "Lb1+4*=" },            /* 0x20 */
446   {  1, "Ld1+=" },              /* 0x21 */
447   /* R_UNINIT */
448   {  0, "Lb1+4*=" },            /* 0x22 */
449   {  1, "Ld1+=" },              /* 0x23 */
450   /* R_RELOCATION */
451   {  0, "L4=" },                /* 0x24 */
452   /* R_DATA_ONE_SYMBOL */
453   {  0, "L4=Sb=" },             /* 0x25 */
454   {  1, "L4=Sd=" },             /* 0x26 */
455   /* R_DATA_PLEBEL */
456   {  0, "L4=Sb=" },             /* 0x27 */
457   {  1, "L4=Sd=" },             /* 0x28 */
458   /* R_SPACE_REF */
459   {  0, "L4=" },                /* 0x29 */
460   /* R_REPEATED_INIT */
461   {  0, "L4=Mb1+4*=" },         /* 0x2a */
462   {  1, "Lb4*=Mb1+L*=" },       /* 0x2b */
463   {  2, "Lb4*=Md1+4*=" },       /* 0x2c */
464   {  3, "Ld1+=Me1+=" },         /* 0x2d */
465   {  0, "" },                   /* 0x2e */
466   {  0, "" },                   /* 0x2f */
467   /* R_PCREL_CALL */
468   {  0, "L4=RD=Sb=" },          /* 0x30 */
469   {  1, "L4=RD=Sb=" },          /* 0x31 */
470   {  2, "L4=RD=Sb=" },          /* 0x32 */
471   {  3, "L4=RD=Sb=" },          /* 0x33 */
472   {  4, "L4=RD=Sb=" },          /* 0x34 */
473   {  5, "L4=RD=Sb=" },          /* 0x35 */
474   {  6, "L4=RD=Sb=" },          /* 0x36 */
475   {  7, "L4=RD=Sb=" },          /* 0x37 */
476   {  8, "L4=RD=Sb=" },          /* 0x38 */
477   {  9, "L4=RD=Sb=" },          /* 0x39 */
478   {  0, "L4=RD8<b+=Sb=" },      /* 0x3a */
479   {  1, "L4=RD8<b+=Sb=" },      /* 0x3b */
480   {  0, "L4=RD8<b+=Sd=" },      /* 0x3c */
481   {  1, "L4=RD8<b+=Sd=" },      /* 0x3d */
482   /* R_SHORT_PCREL_MODE */
483   {  0, "" },                   /* 0x3e */
484   /* R_LONG_PCREL_MODE */
485   {  0, "" },                   /* 0x3f */
486   /* R_ABS_CALL */
487   {  0, "L4=RD=Sb=" },          /* 0x40 */
488   {  1, "L4=RD=Sb=" },          /* 0x41 */
489   {  2, "L4=RD=Sb=" },          /* 0x42 */
490   {  3, "L4=RD=Sb=" },          /* 0x43 */
491   {  4, "L4=RD=Sb=" },          /* 0x44 */
492   {  5, "L4=RD=Sb=" },          /* 0x45 */
493   {  6, "L4=RD=Sb=" },          /* 0x46 */
494   {  7, "L4=RD=Sb=" },          /* 0x47 */
495   {  8, "L4=RD=Sb=" },          /* 0x48 */
496   {  9, "L4=RD=Sb=" },          /* 0x49 */
497   {  0, "L4=RD8<b+=Sb=" },      /* 0x4a */
498   {  1, "L4=RD8<b+=Sb=" },      /* 0x4b */
499   {  0, "L4=RD8<b+=Sd=" },      /* 0x4c */
500   {  1, "L4=RD8<b+=Sd=" },      /* 0x4d */
501   /* R_RESERVED */
502   {  0, "" },                   /* 0x4e */
503   {  0, "" },                   /* 0x4f */
504   /* R_DP_RELATIVE */
505   {  0, "L4=SD=" },             /* 0x50 */
506   {  1, "L4=SD=" },             /* 0x51 */
507   {  2, "L4=SD=" },             /* 0x52 */
508   {  3, "L4=SD=" },             /* 0x53 */
509   {  4, "L4=SD=" },             /* 0x54 */
510   {  5, "L4=SD=" },             /* 0x55 */
511   {  6, "L4=SD=" },             /* 0x56 */
512   {  7, "L4=SD=" },             /* 0x57 */
513   {  8, "L4=SD=" },             /* 0x58 */
514   {  9, "L4=SD=" },             /* 0x59 */
515   { 10, "L4=SD=" },             /* 0x5a */
516   { 11, "L4=SD=" },             /* 0x5b */
517   { 12, "L4=SD=" },             /* 0x5c */
518   { 13, "L4=SD=" },             /* 0x5d */
519   { 14, "L4=SD=" },             /* 0x5e */
520   { 15, "L4=SD=" },             /* 0x5f */
521   { 16, "L4=SD=" },             /* 0x60 */
522   { 17, "L4=SD=" },             /* 0x61 */
523   { 18, "L4=SD=" },             /* 0x62 */
524   { 19, "L4=SD=" },             /* 0x63 */
525   { 20, "L4=SD=" },             /* 0x64 */
526   { 21, "L4=SD=" },             /* 0x65 */
527   { 22, "L4=SD=" },             /* 0x66 */
528   { 23, "L4=SD=" },             /* 0x67 */
529   { 24, "L4=SD=" },             /* 0x68 */
530   { 25, "L4=SD=" },             /* 0x69 */
531   { 26, "L4=SD=" },             /* 0x6a */
532   { 27, "L4=SD=" },             /* 0x6b */
533   { 28, "L4=SD=" },             /* 0x6c */
534   { 29, "L4=SD=" },             /* 0x6d */
535   { 30, "L4=SD=" },             /* 0x6e */
536   { 31, "L4=SD=" },             /* 0x6f */
537   { 32, "L4=Sb=" },             /* 0x70 */
538   { 33, "L4=Sd=" },             /* 0x71 */
539   /* R_RESERVED */
540   {  0, "" },                   /* 0x72 */
541   {  0, "" },                   /* 0x73 */
542   {  0, "" },                   /* 0x74 */
543   {  0, "" },                   /* 0x75 */
544   {  0, "" },                   /* 0x76 */
545   {  0, "" },                   /* 0x77 */
546   /* R_DLT_REL */
547   {  0, "L4=Sb=" },             /* 0x78 */
548   {  1, "L4=Sd=" },             /* 0x79 */
549   /* R_RESERVED */
550   {  0, "" },                   /* 0x7a */
551   {  0, "" },                   /* 0x7b */
552   {  0, "" },                   /* 0x7c */
553   {  0, "" },                   /* 0x7d */
554   {  0, "" },                   /* 0x7e */
555   {  0, "" },                   /* 0x7f */
556   /* R_CODE_ONE_SYMBOL */
557   {  0, "L4=SD=" },             /* 0x80 */
558   {  1, "L4=SD=" },             /* 0x81 */
559   {  2, "L4=SD=" },             /* 0x82 */
560   {  3, "L4=SD=" },             /* 0x83 */
561   {  4, "L4=SD=" },             /* 0x84 */
562   {  5, "L4=SD=" },             /* 0x85 */
563   {  6, "L4=SD=" },             /* 0x86 */
564   {  7, "L4=SD=" },             /* 0x87 */
565   {  8, "L4=SD=" },             /* 0x88 */
566   {  9, "L4=SD=" },             /* 0x89 */
567   { 10, "L4=SD=" },             /* 0x8q */
568   { 11, "L4=SD=" },             /* 0x8b */
569   { 12, "L4=SD=" },             /* 0x8c */
570   { 13, "L4=SD=" },             /* 0x8d */
571   { 14, "L4=SD=" },             /* 0x8e */
572   { 15, "L4=SD=" },             /* 0x8f */
573   { 16, "L4=SD=" },             /* 0x90 */
574   { 17, "L4=SD=" },             /* 0x91 */
575   { 18, "L4=SD=" },             /* 0x92 */
576   { 19, "L4=SD=" },             /* 0x93 */
577   { 20, "L4=SD=" },             /* 0x94 */
578   { 21, "L4=SD=" },             /* 0x95 */
579   { 22, "L4=SD=" },             /* 0x96 */
580   { 23, "L4=SD=" },             /* 0x97 */
581   { 24, "L4=SD=" },             /* 0x98 */
582   { 25, "L4=SD=" },             /* 0x99 */
583   { 26, "L4=SD=" },             /* 0x9a */
584   { 27, "L4=SD=" },             /* 0x9b */
585   { 28, "L4=SD=" },             /* 0x9c */
586   { 29, "L4=SD=" },             /* 0x9d */
587   { 30, "L4=SD=" },             /* 0x9e */
588   { 31, "L4=SD=" },             /* 0x9f */
589   { 32, "L4=Sb=" },             /* 0xa0 */
590   { 33, "L4=Sd=" },             /* 0xa1 */
591   /* R_RESERVED */
592   {  0, "" },                   /* 0xa2 */
593   {  0, "" },                   /* 0xa3 */
594   {  0, "" },                   /* 0xa4 */
595   {  0, "" },                   /* 0xa5 */
596   {  0, "" },                   /* 0xa6 */
597   {  0, "" },                   /* 0xa7 */
598   {  0, "" },                   /* 0xa8 */
599   {  0, "" },                   /* 0xa9 */
600   {  0, "" },                   /* 0xaa */
601   {  0, "" },                   /* 0xab */
602   {  0, "" },                   /* 0xac */
603   {  0, "" },                   /* 0xad */
604   /* R_MILLI_REL */
605   {  0, "L4=Sb=" },             /* 0xae */
606   {  1, "L4=Sd=" },             /* 0xaf */
607   /* R_CODE_PLABEL */
608   {  0, "L4=Sb=" },             /* 0xb0 */
609   {  1, "L4=Sd=" },             /* 0xb1 */
610   /* R_BREAKPOINT */
611   {  0, "L4=" },                /* 0xb2 */
612   /* R_ENTRY */
613   {  0, "Te=Ue=" },             /* 0xb3 */
614   {  1, "Uf=" },                /* 0xb4 */
615   /* R_ALT_ENTRY */
616   {  0, "" },                   /* 0xb5 */
617   /* R_EXIT */
618   {  0, "" },                   /* 0xb6 */
619   /* R_BEGIN_TRY */
620   {  0, "" },                   /* 0xb7 */
621   /* R_END_TRY */
622   {  0, "R0=" },                /* 0xb8 */
623   {  1, "Rb4*=" },              /* 0xb9 */
624   {  2, "Rd4*=" },              /* 0xba */
625   /* R_BEGIN_BRTAB */
626   {  0, "" },                   /* 0xbb */
627   /* R_END_BRTAB */
628   {  0, "" },                   /* 0xbc */
629   /* R_STATEMENT */
630   {  0, "Nb=" },                /* 0xbd */
631   {  1, "Nc=" },                /* 0xbe */
632   {  2, "Nd=" },                /* 0xbf */
633   /* R_DATA_EXPR */
634   {  0, "L4=" },                /* 0xc0 */
635   /* R_CODE_EXPR */
636   {  0, "L4=" },                /* 0xc1 */
637   /* R_FSEL */
638   {  0, "" },                   /* 0xc2 */
639   /* R_LSEL */
640   {  0, "" },                   /* 0xc3 */
641   /* R_RSEL */
642   {  0, "" },                   /* 0xc4 */
643   /* R_N_MODE */
644   {  0, "" },                   /* 0xc5 */
645   /* R_S_MODE */
646   {  0, "" },                   /* 0xc6 */
647   /* R_D_MODE */
648   {  0, "" },                   /* 0xc7 */
649   /* R_R_MODE */
650   {  0, "" },                   /* 0xc8 */
651   /* R_DATA_OVERRIDE */
652   {  0, "V0=" },                /* 0xc9 */
653   {  1, "Vb=" },                /* 0xca */
654   {  2, "Vc=" },                /* 0xcb */
655   {  3, "Vd=" },                /* 0xcc */
656   {  4, "Ve=" },                /* 0xcd */
657   /* R_TRANSLATED */
658   {  0, "" },                   /* 0xce */
659   /* R_AUX_UNWIND */
660   {  0,"Sd=Ve=Ee=" },          /* 0xcf */
661   /* R_COMP1 */
662   {  0, "Ob=" },                /* 0xd0 */
663   /* R_COMP2 */
664   {  0, "Ob=Sd=" },             /* 0xd1 */
665   /* R_COMP3 */
666   {  0, "Ob=Ve=" },             /* 0xd2 */
667   /* R_PREV_FIXUP */
668   {  0, "P" },                  /* 0xd3 */
669   {  1, "P" },                  /* 0xd4 */
670   {  2, "P" },                  /* 0xd5 */
671   {  3, "P" },                  /* 0xd6 */
672   /* R_SEC_STMT */
673   {  0, "" },                   /* 0xd7 */
674   /* R_N0SEL */
675   {  0, "" },                   /* 0xd8 */
676   /* R_N1SEL */
677   {  0, "" },                   /* 0xd9 */
678   /* R_LINETAB */
679   {  0, "Eb=Sd=Ve=" },          /* 0xda */
680   /* R_LINETAB_ESC */
681   {  0, "Eb=Mb=" },             /* 0xdb */
682   /* R_LTP_OVERRIDE */
683   {  0, "" },                   /* 0xdc */
684   /* R_COMMENT */
685   {  0, "Ob=Vf=" },             /* 0xdd */
686   /* R_RESERVED */
687   {  0, "" },                   /* 0xde */
688   {  0, "" },                   /* 0xdf */
689   {  0, "" },                   /* 0xe0 */
690   {  0, "" },                   /* 0xe1 */
691   {  0, "" },                   /* 0xe2 */
692   {  0, "" },                   /* 0xe3 */
693   {  0, "" },                   /* 0xe4 */
694   {  0, "" },                   /* 0xe5 */
695   {  0, "" },                   /* 0xe6 */
696   {  0, "" },                   /* 0xe7 */
697   {  0, "" },                   /* 0xe8 */
698   {  0, "" },                   /* 0xe9 */
699   {  0, "" },                   /* 0xea */
700   {  0, "" },                   /* 0xeb */
701   {  0, "" },                   /* 0xec */
702   {  0, "" },                   /* 0xed */
703   {  0, "" },                   /* 0xee */
704   {  0, "" },                   /* 0xef */
705   {  0, "" },                   /* 0xf0 */
706   {  0, "" },                   /* 0xf1 */
707   {  0, "" },                   /* 0xf2 */
708   {  0, "" },                   /* 0xf3 */
709   {  0, "" },                   /* 0xf4 */
710   {  0, "" },                   /* 0xf5 */
711   {  0, "" },                   /* 0xf6 */
712   {  0, "" },                   /* 0xf7 */
713   {  0, "" },                   /* 0xf8 */
714   {  0, "" },                   /* 0xf9 */
715   {  0, "" },                   /* 0xfa */
716   {  0, "" },                   /* 0xfb */
717   {  0, "" },                   /* 0xfc */
718   {  0, "" },                   /* 0xfd */
719   {  0, "" },                   /* 0xfe */
720   {  0, "" },                   /* 0xff */
721 };
722
723 static const int comp1_opcodes[] = {
724   0x00,
725   0x40,
726   0x41,
727   0x42,
728   0x43,
729   0x44,
730   0x45,
731   0x46,
732   0x47,
733   0x48,
734   0x49,
735   0x4a,
736   0x4b,
737   0x60,
738   0x80,
739   0xa0,
740   0xc0,
741   -1
742 };
743
744 static const int comp2_opcodes[] = {
745   0x00,
746   0x80,
747   0x82,
748   0xc0,
749   -1
750 };
751
752 static const int comp3_opcodes[] = {
753   0x00,
754   0x02,
755   -1
756 };
757
758 /* These apparently are not in older versions of hpux reloc.h (hpux7).  */
759 #ifndef R_DLT_REL
760 #define R_DLT_REL 0x78
761 #endif
762
763 #ifndef R_AUX_UNWIND
764 #define R_AUX_UNWIND 0xcf
765 #endif
766
767 #ifndef R_SEC_STMT
768 #define R_SEC_STMT 0xd7
769 #endif
770
771 /* And these first appeared in hpux10.  */
772 #ifndef R_SHORT_PCREL_MODE
773 #define NO_PCREL_MODES
774 #define R_SHORT_PCREL_MODE 0x3e
775 #endif
776
777 #ifndef R_LONG_PCREL_MODE
778 #define R_LONG_PCREL_MODE 0x3f
779 #endif
780
781 #ifndef R_N0SEL
782 #define R_N0SEL 0xd8
783 #endif
784
785 #ifndef R_N1SEL
786 #define R_N1SEL 0xd9
787 #endif
788
789 #ifndef R_LINETAB
790 #define R_LINETAB 0xda
791 #endif
792
793 #ifndef R_LINETAB_ESC
794 #define R_LINETAB_ESC 0xdb
795 #endif
796
797 #ifndef R_LTP_OVERRIDE
798 #define R_LTP_OVERRIDE 0xdc
799 #endif
800
801 #ifndef R_COMMENT
802 #define R_COMMENT 0xdd
803 #endif
804
805 #define SOM_HOWTO(TYPE, NAME)   \
806   HOWTO(TYPE, 0, 0, 32, FALSE, 0, 0, hppa_som_reloc, NAME, FALSE, 0, 0, FALSE)
807
808 static reloc_howto_type som_hppa_howto_table[] = {
809   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
810   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
811   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
812   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
813   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
814   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
815   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
816   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
817   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
818   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
819   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
820   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
821   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
822   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
823   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
824   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
825   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
826   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
827   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
828   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
829   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
830   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
831   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
832   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
833   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
834   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
835   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
836   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
837   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
838   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
839   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
840   SOM_HOWTO (R_NO_RELOCATION, "R_NO_RELOCATION"),
841   SOM_HOWTO (R_ZEROES, "R_ZEROES"),
842   SOM_HOWTO (R_ZEROES, "R_ZEROES"),
843   SOM_HOWTO (R_UNINIT, "R_UNINIT"),
844   SOM_HOWTO (R_UNINIT, "R_UNINIT"),
845   SOM_HOWTO (R_RELOCATION, "R_RELOCATION"),
846   SOM_HOWTO (R_DATA_ONE_SYMBOL, "R_DATA_ONE_SYMBOL"),
847   SOM_HOWTO (R_DATA_ONE_SYMBOL, "R_DATA_ONE_SYMBOL"),
848   SOM_HOWTO (R_DATA_PLABEL, "R_DATA_PLABEL"),
849   SOM_HOWTO (R_DATA_PLABEL, "R_DATA_PLABEL"),
850   SOM_HOWTO (R_SPACE_REF, "R_SPACE_REF"),
851   SOM_HOWTO (R_REPEATED_INIT, "REPEATED_INIT"),
852   SOM_HOWTO (R_REPEATED_INIT, "REPEATED_INIT"),
853   SOM_HOWTO (R_REPEATED_INIT, "REPEATED_INIT"),
854   SOM_HOWTO (R_REPEATED_INIT, "REPEATED_INIT"),
855   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
856   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
857   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
858   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
859   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
860   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
861   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
862   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
863   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
864   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
865   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
866   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
867   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
868   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
869   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
870   SOM_HOWTO (R_PCREL_CALL, "R_PCREL_CALL"),
871   SOM_HOWTO (R_SHORT_PCREL_MODE, "R_SHORT_PCREL_MODE"),
872   SOM_HOWTO (R_LONG_PCREL_MODE, "R_LONG_PCREL_MODE"),
873   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
874   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
875   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
876   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
877   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
878   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
879   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
880   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
881   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
882   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
883   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
884   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
885   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
886   SOM_HOWTO (R_ABS_CALL, "R_ABS_CALL"),
887   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
888   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
889   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
890   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
891   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
892   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
893   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
894   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
895   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
896   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
897   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
898   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
899   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
900   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
901   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
902   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
903   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
904   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
905   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
906   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
907   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
908   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
909   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
910   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
911   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
912   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
913   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
914   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
915   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
916   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
917   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
918   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
919   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
920   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
921   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
922   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
923   SOM_HOWTO (R_DP_RELATIVE, "R_DP_RELATIVE"),
924   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
925   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
926   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
927   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
928   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
929   SOM_HOWTO (R_DLT_REL, "R_DLT_REL"),
930   SOM_HOWTO (R_DLT_REL, "R_DLT_REL"),
931   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
932   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
933   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
934   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
935   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
936   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
937   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
938   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
939   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
940   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
941   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
942   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
943   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
944   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
945   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
946   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
947   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
948   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
949   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
950   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
951   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
952   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
953   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
954   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
955   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
956   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
957   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
958   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
959   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
960   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
961   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
962   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
963   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
964   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
965   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
966   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
967   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
968   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
969   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
970   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
971   SOM_HOWTO (R_CODE_ONE_SYMBOL, "R_CODE_ONE_SYMBOL"),
972   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
973   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
974   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
975   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
976   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
977   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
978   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
979   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
980   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
981   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
982   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
983   SOM_HOWTO (R_MILLI_REL, "R_MILLI_REL"),
984   SOM_HOWTO (R_MILLI_REL, "R_MILLI_REL"),
985   SOM_HOWTO (R_CODE_PLABEL, "R_CODE_PLABEL"),
986   SOM_HOWTO (R_CODE_PLABEL, "R_CODE_PLABEL"),
987   SOM_HOWTO (R_BREAKPOINT, "R_BREAKPOINT"),
988   SOM_HOWTO (R_ENTRY, "R_ENTRY"),
989   SOM_HOWTO (R_ENTRY, "R_ENTRY"),
990   SOM_HOWTO (R_ALT_ENTRY, "R_ALT_ENTRY"),
991   SOM_HOWTO (R_EXIT, "R_EXIT"),
992   SOM_HOWTO (R_BEGIN_TRY, "R_BEGIN_TRY"),
993   SOM_HOWTO (R_END_TRY, "R_END_TRY"),
994   SOM_HOWTO (R_END_TRY, "R_END_TRY"),
995   SOM_HOWTO (R_END_TRY, "R_END_TRY"),
996   SOM_HOWTO (R_BEGIN_BRTAB, "R_BEGIN_BRTAB"),
997   SOM_HOWTO (R_END_BRTAB, "R_END_BRTAB"),
998   SOM_HOWTO (R_STATEMENT, "R_STATEMENT"),
999   SOM_HOWTO (R_STATEMENT, "R_STATEMENT"),
1000   SOM_HOWTO (R_STATEMENT, "R_STATEMENT"),
1001   SOM_HOWTO (R_DATA_EXPR, "R_DATA_EXPR"),
1002   SOM_HOWTO (R_CODE_EXPR, "R_CODE_EXPR"),
1003   SOM_HOWTO (R_FSEL, "R_FSEL"),
1004   SOM_HOWTO (R_LSEL, "R_LSEL"),
1005   SOM_HOWTO (R_RSEL, "R_RSEL"),
1006   SOM_HOWTO (R_N_MODE, "R_N_MODE"),
1007   SOM_HOWTO (R_S_MODE, "R_S_MODE"),
1008   SOM_HOWTO (R_D_MODE, "R_D_MODE"),
1009   SOM_HOWTO (R_R_MODE, "R_R_MODE"),
1010   SOM_HOWTO (R_DATA_OVERRIDE, "R_DATA_OVERRIDE"),
1011   SOM_HOWTO (R_DATA_OVERRIDE, "R_DATA_OVERRIDE"),
1012   SOM_HOWTO (R_DATA_OVERRIDE, "R_DATA_OVERRIDE"),
1013   SOM_HOWTO (R_DATA_OVERRIDE, "R_DATA_OVERRIDE"),
1014   SOM_HOWTO (R_DATA_OVERRIDE, "R_DATA_OVERRIDE"),
1015   SOM_HOWTO (R_TRANSLATED, "R_TRANSLATED"),
1016   SOM_HOWTO (R_AUX_UNWIND, "R_AUX_UNWIND"),
1017   SOM_HOWTO (R_COMP1, "R_COMP1"),
1018   SOM_HOWTO (R_COMP2, "R_COMP2"),
1019   SOM_HOWTO (R_COMP3, "R_COMP3"),
1020   SOM_HOWTO (R_PREV_FIXUP, "R_PREV_FIXUP"),
1021   SOM_HOWTO (R_PREV_FIXUP, "R_PREV_FIXUP"),
1022   SOM_HOWTO (R_PREV_FIXUP, "R_PREV_FIXUP"),
1023   SOM_HOWTO (R_PREV_FIXUP, "R_PREV_FIXUP"),
1024   SOM_HOWTO (R_SEC_STMT, "R_SEC_STMT"),
1025   SOM_HOWTO (R_N0SEL, "R_N0SEL"),
1026   SOM_HOWTO (R_N1SEL, "R_N1SEL"),
1027   SOM_HOWTO (R_LINETAB, "R_LINETAB"),
1028   SOM_HOWTO (R_LINETAB_ESC, "R_LINETAB_ESC"),
1029   SOM_HOWTO (R_LTP_OVERRIDE, "R_LTP_OVERRIDE"),
1030   SOM_HOWTO (R_COMMENT, "R_COMMENT"),
1031   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1032   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1033   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1034   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1035   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1036   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1037   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1038   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1039   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1040   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1041   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1042   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1043   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1044   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1045   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1046   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1047   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1048   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1049   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1050   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1051   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1052   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1053   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1054   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1055   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1056   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1057   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1058   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1059   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1060   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1061   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1062   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1063   SOM_HOWTO (R_RESERVED, "R_RESERVED"),
1064   SOM_HOWTO (R_RESERVED, "R_RESERVED")
1065 };
1066
1067 /* Initialize the SOM relocation queue.  By definition the queue holds
1068    the last four multibyte fixups.  */
1069
1070 static void
1071 som_initialize_reloc_queue (queue)
1072      struct reloc_queue *queue;
1073 {
1074   queue[0].reloc = NULL;
1075   queue[0].size = 0;
1076   queue[1].reloc = NULL;
1077   queue[1].size = 0;
1078   queue[2].reloc = NULL;
1079   queue[2].size = 0;
1080   queue[3].reloc = NULL;
1081   queue[3].size = 0;
1082 }
1083
1084 /* Insert a new relocation into the relocation queue.  */
1085
1086 static void
1087 som_reloc_queue_insert (p, size, queue)
1088      unsigned char *p;
1089      unsigned int size;
1090      struct reloc_queue *queue;
1091 {
1092   queue[3].reloc = queue[2].reloc;
1093   queue[3].size = queue[2].size;
1094   queue[2].reloc = queue[1].reloc;
1095   queue[2].size = queue[1].size;
1096   queue[1].reloc = queue[0].reloc;
1097   queue[1].size = queue[0].size;
1098   queue[0].reloc = p;
1099   queue[0].size = size;
1100 }
1101
1102 /* When an entry in the relocation queue is reused, the entry moves
1103    to the front of the queue.  */
1104
1105 static void
1106 som_reloc_queue_fix (queue, index)
1107      struct reloc_queue *queue;
1108      unsigned int index;
1109 {
1110   if (index == 0)
1111     return;
1112
1113   if (index == 1)
1114     {
1115       unsigned char *tmp1 = queue[0].reloc;
1116       unsigned int tmp2 = queue[0].size;
1117       queue[0].reloc = queue[1].reloc;
1118       queue[0].size = queue[1].size;
1119       queue[1].reloc = tmp1;
1120       queue[1].size = tmp2;
1121       return;
1122     }
1123
1124   if (index == 2)
1125     {
1126       unsigned char *tmp1 = queue[0].reloc;
1127       unsigned int tmp2 = queue[0].size;
1128       queue[0].reloc = queue[2].reloc;
1129       queue[0].size = queue[2].size;
1130       queue[2].reloc = queue[1].reloc;
1131       queue[2].size = queue[1].size;
1132       queue[1].reloc = tmp1;
1133       queue[1].size = tmp2;
1134       return;
1135     }
1136
1137   if (index == 3)
1138     {
1139       unsigned char *tmp1 = queue[0].reloc;
1140       unsigned int tmp2 = queue[0].size;
1141       queue[0].reloc = queue[3].reloc;
1142       queue[0].size = queue[3].size;
1143       queue[3].reloc = queue[2].reloc;
1144       queue[3].size = queue[2].size;
1145       queue[2].reloc = queue[1].reloc;
1146       queue[2].size = queue[1].size;
1147       queue[1].reloc = tmp1;
1148       queue[1].size = tmp2;
1149       return;
1150     }
1151   abort ();
1152 }
1153
1154 /* Search for a particular relocation in the relocation queue.  */
1155
1156 static int
1157 som_reloc_queue_find (p, size, queue)
1158      unsigned char *p;
1159      unsigned int size;
1160      struct reloc_queue *queue;
1161 {
1162   if (queue[0].reloc && !memcmp (p, queue[0].reloc, size)
1163       && size == queue[0].size)
1164     return 0;
1165   if (queue[1].reloc && !memcmp (p, queue[1].reloc, size)
1166       && size == queue[1].size)
1167     return 1;
1168   if (queue[2].reloc && !memcmp (p, queue[2].reloc, size)
1169       && size == queue[2].size)
1170     return 2;
1171   if (queue[3].reloc && !memcmp (p, queue[3].reloc, size)
1172       && size == queue[3].size)
1173     return 3;
1174   return -1;
1175 }
1176
1177 static unsigned char *
1178 try_prev_fixup (abfd, subspace_reloc_sizep, p, size, queue)
1179      bfd *abfd ATTRIBUTE_UNUSED;
1180      int *subspace_reloc_sizep;
1181      unsigned char *p;
1182      unsigned int size;
1183      struct reloc_queue *queue;
1184 {
1185   int queue_index = som_reloc_queue_find (p, size, queue);
1186
1187   if (queue_index != -1)
1188     {
1189       /* Found this in a previous fixup.  Undo the fixup we
1190          just built and use R_PREV_FIXUP instead.  We saved
1191          a total of size - 1 bytes in the fixup stream.  */
1192       bfd_put_8 (abfd, R_PREV_FIXUP + queue_index, p);
1193       p += 1;
1194       *subspace_reloc_sizep += 1;
1195       som_reloc_queue_fix (queue, queue_index);
1196     }
1197   else
1198     {
1199       som_reloc_queue_insert (p, size, queue);
1200       *subspace_reloc_sizep += size;
1201       p += size;
1202     }
1203   return p;
1204 }
1205
1206 /* Emit the proper R_NO_RELOCATION fixups to map the next SKIP
1207    bytes without any relocation.  Update the size of the subspace
1208    relocation stream via SUBSPACE_RELOC_SIZE_P; also return the
1209    current pointer into the relocation stream.  */
1210
1211 static unsigned char *
1212 som_reloc_skip (abfd, skip, p, subspace_reloc_sizep, queue)
1213      bfd *abfd;
1214      unsigned int skip;
1215      unsigned char *p;
1216      unsigned int *subspace_reloc_sizep;
1217      struct reloc_queue *queue;
1218 {
1219   /* Use a 4 byte R_NO_RELOCATION entry with a maximal value
1220      then R_PREV_FIXUPs to get the difference down to a
1221      reasonable size.  */
1222   if (skip >= 0x1000000)
1223     {
1224       skip -= 0x1000000;
1225       bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
1226       bfd_put_8 (abfd, 0xff, p + 1);
1227       bfd_put_16 (abfd, (bfd_vma) 0xffff, p + 2);
1228       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
1229       while (skip >= 0x1000000)
1230         {
1231           skip -= 0x1000000;
1232           bfd_put_8 (abfd, R_PREV_FIXUP, p);
1233           p++;
1234           *subspace_reloc_sizep += 1;
1235           /* No need to adjust queue here since we are repeating the
1236              most recent fixup.  */
1237         }
1238     }
1239
1240   /* The difference must be less than 0x1000000.  Use one
1241      more R_NO_RELOCATION entry to get to the right difference.  */
1242   if ((skip & 3) == 0 && skip <= 0xc0000 && skip > 0)
1243     {
1244       /* Difference can be handled in a simple single-byte
1245          R_NO_RELOCATION entry.  */
1246       if (skip <= 0x60)
1247         {
1248           bfd_put_8 (abfd, R_NO_RELOCATION + (skip >> 2) - 1, p);
1249           *subspace_reloc_sizep += 1;
1250           p++;
1251         }
1252       /* Handle it with a two byte R_NO_RELOCATION entry.  */
1253       else if (skip <= 0x1000)
1254         {
1255           bfd_put_8 (abfd, R_NO_RELOCATION + 24 + (((skip >> 2) - 1) >> 8), p);
1256           bfd_put_8 (abfd, (skip >> 2) - 1, p + 1);
1257           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
1258         }
1259       /* Handle it with a three byte R_NO_RELOCATION entry.  */
1260       else
1261         {
1262           bfd_put_8 (abfd, R_NO_RELOCATION + 28 + (((skip >> 2) - 1) >> 16), p);
1263           bfd_put_16 (abfd, (bfd_vma) (skip >> 2) - 1, p + 1);
1264           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
1265         }
1266     }
1267   /* Ugh.  Punt and use a 4 byte entry.  */
1268   else if (skip > 0)
1269     {
1270       bfd_put_8 (abfd, R_NO_RELOCATION + 31, p);
1271       bfd_put_8 (abfd, (skip - 1) >> 16, p + 1);
1272       bfd_put_16 (abfd, (bfd_vma) skip - 1, p + 2);
1273       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
1274     }
1275   return p;
1276 }
1277
1278 /* Emit the proper R_DATA_OVERRIDE fixups to handle a nonzero addend
1279    from a BFD relocation.  Update the size of the subspace relocation
1280    stream via SUBSPACE_RELOC_SIZE_P; also return the current pointer
1281    into the relocation stream.  */
1282
1283 static unsigned char *
1284 som_reloc_addend (abfd, addend, p, subspace_reloc_sizep, queue)
1285      bfd *abfd;
1286      bfd_vma addend;
1287      unsigned char *p;
1288      unsigned int *subspace_reloc_sizep;
1289      struct reloc_queue *queue;
1290 {
1291   if (addend + 0x80 < 0x100)
1292     {
1293       bfd_put_8 (abfd, R_DATA_OVERRIDE + 1, p);
1294       bfd_put_8 (abfd, addend, p + 1);
1295       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
1296     }
1297   else if (addend + 0x8000 < 0x10000)
1298     {
1299       bfd_put_8 (abfd, R_DATA_OVERRIDE + 2, p);
1300       bfd_put_16 (abfd, addend, p + 1);
1301       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
1302     }
1303   else if (addend + 0x800000 < 0x1000000)
1304     {
1305       bfd_put_8 (abfd, R_DATA_OVERRIDE + 3, p);
1306       bfd_put_8 (abfd, addend >> 16, p + 1);
1307       bfd_put_16 (abfd, addend, p + 2);
1308       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 4, queue);
1309     }
1310   else
1311     {
1312       bfd_put_8 (abfd, R_DATA_OVERRIDE + 4, p);
1313       bfd_put_32 (abfd, addend, p + 1);
1314       p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
1315     }
1316   return p;
1317 }
1318
1319 /* Handle a single function call relocation.  */
1320
1321 static unsigned char *
1322 som_reloc_call (abfd, p, subspace_reloc_sizep, bfd_reloc, sym_num, queue)
1323      bfd *abfd;
1324      unsigned char *p;
1325      unsigned int *subspace_reloc_sizep;
1326      arelent *bfd_reloc;
1327      int sym_num;
1328      struct reloc_queue *queue;
1329 {
1330   int arg_bits = HPPA_R_ARG_RELOC (bfd_reloc->addend);
1331   int rtn_bits = arg_bits & 0x3;
1332   int type, done = 0;
1333
1334   /* You'll never believe all this is necessary to handle relocations
1335      for function calls.  Having to compute and pack the argument
1336      relocation bits is the real nightmare.
1337
1338      If you're interested in how this works, just forget it.  You really
1339      do not want to know about this braindamage.  */
1340
1341   /* First see if this can be done with a "simple" relocation.  Simple
1342      relocations have a symbol number < 0x100 and have simple encodings
1343      of argument relocations.  */
1344
1345   if (sym_num < 0x100)
1346     {
1347       switch (arg_bits)
1348         {
1349         case 0:
1350         case 1:
1351           type = 0;
1352           break;
1353         case 1 << 8:
1354         case 1 << 8 | 1:
1355           type = 1;
1356           break;
1357         case 1 << 8 | 1 << 6:
1358         case 1 << 8 | 1 << 6 | 1:
1359           type = 2;
1360           break;
1361         case 1 << 8 | 1 << 6 | 1 << 4:
1362         case 1 << 8 | 1 << 6 | 1 << 4 | 1:
1363           type = 3;
1364           break;
1365         case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2:
1366         case 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2 | 1:
1367           type = 4;
1368           break;
1369         default:
1370           /* Not one of the easy encodings.  This will have to be
1371              handled by the more complex code below.  */
1372           type = -1;
1373           break;
1374         }
1375       if (type != -1)
1376         {
1377           /* Account for the return value too.  */
1378           if (rtn_bits)
1379             type += 5;
1380
1381           /* Emit a 2 byte relocation.  Then see if it can be handled
1382              with a relocation which is already in the relocation queue.  */
1383           bfd_put_8 (abfd, bfd_reloc->howto->type + type, p);
1384           bfd_put_8 (abfd, sym_num, p + 1);
1385           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 2, queue);
1386           done = 1;
1387         }
1388     }
1389
1390   /* If this could not be handled with a simple relocation, then do a hard
1391      one.  Hard relocations occur if the symbol number was too high or if
1392      the encoding of argument relocation bits is too complex.  */
1393   if (! done)
1394     {
1395       /* Don't ask about these magic sequences.  I took them straight
1396          from gas-1.36 which took them from the a.out man page.  */
1397       type = rtn_bits;
1398       if ((arg_bits >> 6 & 0xf) == 0xe)
1399         type += 9 * 40;
1400       else
1401         type += (3 * (arg_bits >> 8 & 3) + (arg_bits >> 6 & 3)) * 40;
1402       if ((arg_bits >> 2 & 0xf) == 0xe)
1403         type += 9 * 4;
1404       else
1405         type += (3 * (arg_bits >> 4 & 3) + (arg_bits >> 2 & 3)) * 4;
1406
1407       /* Output the first two bytes of the relocation.  These describe
1408          the length of the relocation and encoding style.  */
1409       bfd_put_8 (abfd, bfd_reloc->howto->type + 10
1410                  + 2 * (sym_num >= 0x100) + (type >= 0x100),
1411                  p);
1412       bfd_put_8 (abfd, type, p + 1);
1413
1414       /* Now output the symbol index and see if this bizarre relocation
1415          just happened to be in the relocation queue.  */
1416       if (sym_num < 0x100)
1417         {
1418           bfd_put_8 (abfd, sym_num, p + 2);
1419           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 3, queue);
1420         }
1421       else
1422         {
1423           bfd_put_8 (abfd, sym_num >> 16, p + 2);
1424           bfd_put_16 (abfd, (bfd_vma) sym_num, p + 3);
1425           p = try_prev_fixup (abfd, subspace_reloc_sizep, p, 5, queue);
1426         }
1427     }
1428   return p;
1429 }
1430
1431 /* Return the logarithm of X, base 2, considering X unsigned.
1432    Abort -1 if X is not a power or two or is zero.  */
1433
1434 static int
1435 log2 (x)
1436      unsigned int x;
1437 {
1438   int log = 0;
1439
1440   /* Test for 0 or a power of 2.  */
1441   if (x == 0 || x != (x & -x))
1442     return -1;
1443
1444   while ((x >>= 1) != 0)
1445     log++;
1446   return log;
1447 }
1448
1449 static bfd_reloc_status_type
1450 hppa_som_reloc (abfd, reloc_entry, symbol_in, data,
1451                 input_section, output_bfd, error_message)
1452      bfd *abfd ATTRIBUTE_UNUSED;
1453      arelent *reloc_entry;
1454      asymbol *symbol_in ATTRIBUTE_UNUSED;
1455      PTR data ATTRIBUTE_UNUSED;
1456      asection *input_section;
1457      bfd *output_bfd;
1458      char **error_message ATTRIBUTE_UNUSED;
1459 {
1460   if (output_bfd)
1461     {
1462       reloc_entry->address += input_section->output_offset;
1463       return bfd_reloc_ok;
1464     }
1465   return bfd_reloc_ok;
1466 }
1467
1468 /* Given a generic HPPA relocation type, the instruction format,
1469    and a field selector, return one or more appropriate SOM relocations.  */
1470
1471 int **
1472 hppa_som_gen_reloc_type (abfd, base_type, format, field, sym_diff, sym)
1473      bfd *abfd;
1474      int base_type;
1475      int format;
1476      enum hppa_reloc_field_selector_type_alt field;
1477      int sym_diff;
1478      asymbol *sym;
1479 {
1480   int *final_type, **final_types;
1481
1482   final_types = (int **) bfd_alloc (abfd, (bfd_size_type) sizeof (int *) * 6);
1483   final_type = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1484   if (!final_types || !final_type)
1485     return NULL;
1486
1487   /* The field selector may require additional relocations to be
1488      generated.  It's impossible to know at this moment if additional
1489      relocations will be needed, so we make them.  The code to actually
1490      write the relocation/fixup stream is responsible for removing
1491      any redundant relocations.  */
1492   switch (field)
1493     {
1494     case e_fsel:
1495     case e_psel:
1496     case e_lpsel:
1497     case e_rpsel:
1498       final_types[0] = final_type;
1499       final_types[1] = NULL;
1500       final_types[2] = NULL;
1501       *final_type = base_type;
1502       break;
1503
1504     case e_tsel:
1505     case e_ltsel:
1506     case e_rtsel:
1507       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1508       if (!final_types[0])
1509         return NULL;
1510       if (field == e_tsel)
1511         *final_types[0] = R_FSEL;
1512       else if (field == e_ltsel)
1513         *final_types[0] = R_LSEL;
1514       else
1515         *final_types[0] = R_RSEL;
1516       final_types[1] = final_type;
1517       final_types[2] = NULL;
1518       *final_type = base_type;
1519       break;
1520
1521     case e_lssel:
1522     case e_rssel:
1523       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1524       if (!final_types[0])
1525         return NULL;
1526       *final_types[0] = R_S_MODE;
1527       final_types[1] = final_type;
1528       final_types[2] = NULL;
1529       *final_type = base_type;
1530       break;
1531
1532     case e_lsel:
1533     case e_rsel:
1534       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1535       if (!final_types[0])
1536         return NULL;
1537       *final_types[0] = R_N_MODE;
1538       final_types[1] = final_type;
1539       final_types[2] = NULL;
1540       *final_type = base_type;
1541       break;
1542
1543     case e_ldsel:
1544     case e_rdsel:
1545       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1546       if (!final_types[0])
1547         return NULL;
1548       *final_types[0] = R_D_MODE;
1549       final_types[1] = final_type;
1550       final_types[2] = NULL;
1551       *final_type = base_type;
1552       break;
1553
1554     case e_lrsel:
1555     case e_rrsel:
1556       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1557       if (!final_types[0])
1558         return NULL;
1559       *final_types[0] = R_R_MODE;
1560       final_types[1] = final_type;
1561       final_types[2] = NULL;
1562       *final_type = base_type;
1563       break;
1564
1565     case e_nsel:
1566       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1567       if (!final_types[0])
1568         return NULL;
1569       *final_types[0] = R_N1SEL;
1570       final_types[1] = final_type;
1571       final_types[2] = NULL;
1572       *final_type = base_type;
1573       break;
1574
1575     case e_nlsel:
1576     case e_nlrsel:
1577       final_types[0] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1578       if (!final_types[0])
1579         return NULL;
1580       *final_types[0] = R_N0SEL;
1581       final_types[1] = (int *) bfd_alloc (abfd, (bfd_size_type) sizeof (int));
1582       if (!final_types[1])
1583         return NULL;
1584       if (field == e_nlsel)
1585         *final_types[1] = R_N_MODE;
1586       else
1587         *final_types[1] = R_R_MODE;
1588       final_types[2] = final_type;
1589       final_types[3] = NULL;
1590       *final_type = base_type;
1591       break;
1592
1593     /* FIXME: These two field selectors are not currently supported.  */
1594     case e_ltpsel:
1595     case e_rtpsel:
1596       abort ();
1597     }
1598
1599   switch (base_type)
1600     {
1601     case R_HPPA:
1602       /* The difference of two symbols needs *very* special handling.  */
1603       if (sym_diff)
1604         {
1605           bfd_size_type amt = sizeof (int);
1606           final_types[0] = (int *) bfd_alloc (abfd, amt);
1607           final_types[1] = (int *) bfd_alloc (abfd, amt);
1608           final_types[2] = (int *) bfd_alloc (abfd, amt);
1609           final_types[3] = (int *) bfd_alloc (abfd, amt);
1610           if (!final_types[0] || !final_types[1] || !final_types[2])
1611             return NULL;
1612           if (field == e_fsel)
1613             *final_types[0] = R_FSEL;
1614           else if (field == e_rsel)
1615             *final_types[0] = R_RSEL;
1616           else if (field == e_lsel)
1617             *final_types[0] = R_LSEL;
1618           *final_types[1] = R_COMP2;
1619           *final_types[2] = R_COMP2;
1620           *final_types[3] = R_COMP1;
1621           final_types[4] = final_type;
1622           if (format == 32)
1623             *final_types[4] = R_DATA_EXPR;
1624           else
1625             *final_types[4] = R_CODE_EXPR;
1626           final_types[5] = NULL;
1627           break;
1628         }
1629       /* PLABELs get their own relocation type.  */
1630       else if (field == e_psel
1631                || field == e_lpsel
1632                || field == e_rpsel)
1633         {
1634           /* A PLABEL relocation that has a size of 32 bits must
1635              be a R_DATA_PLABEL.  All others are R_CODE_PLABELs.  */
1636           if (format == 32)
1637             *final_type = R_DATA_PLABEL;
1638           else
1639             *final_type = R_CODE_PLABEL;
1640         }
1641       /* PIC stuff.  */
1642       else if (field == e_tsel
1643                || field == e_ltsel
1644                || field == e_rtsel)
1645         *final_type = R_DLT_REL;
1646       /* A relocation in the data space is always a full 32bits.  */
1647       else if (format == 32)
1648         {
1649           *final_type = R_DATA_ONE_SYMBOL;
1650
1651           /* If there's no SOM symbol type associated with this BFD
1652              symbol, then set the symbol type to ST_DATA.
1653
1654              Only do this if the type is going to default later when
1655              we write the object file.
1656
1657              This is done so that the linker never encounters an
1658              R_DATA_ONE_SYMBOL reloc involving an ST_CODE symbol.
1659
1660              This allows the compiler to generate exception handling
1661              tables.
1662
1663              Note that one day we may need to also emit BEGIN_BRTAB and
1664              END_BRTAB to prevent the linker from optimizing away insns
1665              in exception handling regions.  */
1666           if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN
1667               && (sym->flags & BSF_SECTION_SYM) == 0
1668               && (sym->flags & BSF_FUNCTION) == 0
1669               && ! bfd_is_com_section (sym->section))
1670             som_symbol_data (sym)->som_type = SYMBOL_TYPE_DATA;
1671         }
1672       break;
1673
1674     case R_HPPA_GOTOFF:
1675       /* More PLABEL special cases.  */
1676       if (field == e_psel
1677           || field == e_lpsel
1678           || field == e_rpsel)
1679         *final_type = R_DATA_PLABEL;
1680       break;
1681
1682     case R_HPPA_COMPLEX:
1683       /* The difference of two symbols needs *very* special handling.  */
1684       if (sym_diff)
1685         {
1686           bfd_size_type amt = sizeof (int);
1687           final_types[0] = (int *) bfd_alloc (abfd, amt);
1688           final_types[1] = (int *) bfd_alloc (abfd, amt);
1689           final_types[2] = (int *) bfd_alloc (abfd, amt);
1690           final_types[3] = (int *) bfd_alloc (abfd, amt);
1691           if (!final_types[0] || !final_types[1] || !final_types[2])
1692             return NULL;
1693           if (field == e_fsel)
1694             *final_types[0] = R_FSEL;
1695           else if (field == e_rsel)
1696             *final_types[0] = R_RSEL;
1697           else if (field == e_lsel)
1698             *final_types[0] = R_LSEL;
1699           *final_types[1] = R_COMP2;
1700           *final_types[2] = R_COMP2;
1701           *final_types[3] = R_COMP1;
1702           final_types[4] = final_type;
1703           if (format == 32)
1704             *final_types[4] = R_DATA_EXPR;
1705           else
1706             *final_types[4] = R_CODE_EXPR;
1707           final_types[5] = NULL;
1708           break;
1709         }
1710       else
1711         break;
1712
1713     case R_HPPA_NONE:
1714     case R_HPPA_ABS_CALL:
1715       /* Right now we can default all these.  */
1716       break;
1717
1718     case R_HPPA_PCREL_CALL:
1719       {
1720 #ifndef NO_PCREL_MODES
1721         /* If we have short and long pcrel modes, then generate the proper
1722            mode selector, then the pcrel relocation.  Redundant selectors
1723            will be eliminated as the relocs are sized and emitted.  */
1724         bfd_size_type amt = sizeof (int);
1725         final_types[0] = (int *) bfd_alloc (abfd, amt);
1726         if (!final_types[0])
1727           return NULL;
1728         if (format == 17)
1729           *final_types[0] = R_SHORT_PCREL_MODE;
1730         else
1731           *final_types[0] = R_LONG_PCREL_MODE;
1732         final_types[1] = final_type;
1733         final_types[2] = NULL;
1734         *final_type = base_type;
1735 #endif
1736         break;
1737       }
1738     }
1739   return final_types;
1740 }
1741
1742 /* Return the address of the correct entry in the PA SOM relocation
1743    howto table.  */
1744
1745 static reloc_howto_type *
1746 som_bfd_reloc_type_lookup (abfd, code)
1747      bfd *abfd ATTRIBUTE_UNUSED;
1748      bfd_reloc_code_real_type code;
1749 {
1750   if ((int) code < (int) R_NO_RELOCATION + 255)
1751     {
1752       BFD_ASSERT ((int) som_hppa_howto_table[(int) code].type == (int) code);
1753       return &som_hppa_howto_table[(int) code];
1754     }
1755
1756   return (reloc_howto_type *) 0;
1757 }
1758
1759 /* Perform some initialization for an object.  Save results of this
1760    initialization in the BFD.  */
1761
1762 static const bfd_target *
1763 som_object_setup (abfd, file_hdrp, aux_hdrp, current_offset)
1764      bfd *abfd;
1765      struct header *file_hdrp;
1766      struct som_exec_auxhdr *aux_hdrp;
1767      unsigned long current_offset;
1768 {
1769   asection *section;
1770   int found;
1771
1772   /* som_mkobject will set bfd_error if som_mkobject fails.  */
1773   if (! som_mkobject (abfd))
1774     return 0;
1775
1776   /* Set BFD flags based on what information is available in the SOM.  */
1777   abfd->flags = BFD_NO_FLAGS;
1778   if (file_hdrp->symbol_total)
1779     abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
1780
1781   switch (file_hdrp->a_magic)
1782     {
1783     case DEMAND_MAGIC:
1784       abfd->flags |= (D_PAGED | WP_TEXT | EXEC_P);
1785       break;
1786     case SHARE_MAGIC:
1787       abfd->flags |= (WP_TEXT | EXEC_P);
1788       break;
1789     case EXEC_MAGIC:
1790       abfd->flags |= (EXEC_P);
1791       break;
1792     case RELOC_MAGIC:
1793       abfd->flags |= HAS_RELOC;
1794       break;
1795 #ifdef SHL_MAGIC
1796     case SHL_MAGIC:
1797 #endif
1798 #ifdef DL_MAGIC
1799     case DL_MAGIC:
1800 #endif
1801       abfd->flags |= DYNAMIC;
1802       break;
1803
1804     default:
1805       break;
1806     }
1807
1808   /* Allocate space to hold the saved exec header information.  */
1809   obj_som_exec_data (abfd) = (struct som_exec_data *)
1810     bfd_zalloc (abfd, (bfd_size_type) sizeof (struct som_exec_data));
1811   if (obj_som_exec_data (abfd) == NULL)
1812     return NULL;
1813
1814   /* The braindamaged OSF1 linker switched exec_flags and exec_entry!
1815
1816      We used to identify OSF1 binaries based on NEW_VERSION_ID, but
1817      apparently the latest HPUX linker is using NEW_VERSION_ID now.
1818
1819      It's about time, OSF has used the new id since at least 1992;
1820      HPUX didn't start till nearly 1995!.
1821
1822      The new approach examines the entry field.  If it's zero or not 4
1823      byte aligned then it's not a proper code address and we guess it's
1824      really the executable flags.  */
1825   found = 0;
1826   for (section = abfd->sections; section; section = section->next)
1827     {
1828       bfd_vma entry;
1829
1830       if ((section->flags & SEC_CODE) == 0)
1831         continue;
1832       entry = aux_hdrp->exec_entry;
1833       if (entry >= section->vma
1834           && entry < section->vma + section->_cooked_size)
1835         found = 1;
1836     }
1837   if (aux_hdrp->exec_entry == 0
1838       || (aux_hdrp->exec_entry & 0x3) != 0
1839       || ! found)
1840     {
1841       bfd_get_start_address (abfd) = aux_hdrp->exec_flags;
1842       obj_som_exec_data (abfd)->exec_flags = aux_hdrp->exec_entry;
1843     }
1844   else
1845     {
1846       bfd_get_start_address (abfd) = aux_hdrp->exec_entry + current_offset;
1847       obj_som_exec_data (abfd)->exec_flags = aux_hdrp->exec_flags;
1848     }
1849
1850   obj_som_exec_data (abfd)->version_id = file_hdrp->version_id;
1851
1852   bfd_default_set_arch_mach (abfd, bfd_arch_hppa, pa10);
1853   bfd_get_symcount (abfd) = file_hdrp->symbol_total;
1854
1855   /* Initialize the saved symbol table and string table to NULL.
1856      Save important offsets and sizes from the SOM header into
1857      the BFD.  */
1858   obj_som_stringtab (abfd) = (char *) NULL;
1859   obj_som_symtab (abfd) = (som_symbol_type *) NULL;
1860   obj_som_sorted_syms (abfd) = NULL;
1861   obj_som_stringtab_size (abfd) = file_hdrp->symbol_strings_size;
1862   obj_som_sym_filepos (abfd) = file_hdrp->symbol_location + current_offset;
1863   obj_som_str_filepos (abfd) = (file_hdrp->symbol_strings_location
1864                                 + current_offset);
1865   obj_som_reloc_filepos (abfd) = (file_hdrp->fixup_request_location
1866                                   + current_offset);
1867   obj_som_exec_data (abfd)->system_id = file_hdrp->system_id;
1868
1869   return abfd->xvec;
1870 }
1871
1872 /* Convert all of the space and subspace info into BFD sections.  Each space
1873    contains a number of subspaces, which in turn describe the mapping between
1874    regions of the exec file, and the address space that the program runs in.
1875    BFD sections which correspond to spaces will overlap the sections for the
1876    associated subspaces.  */
1877
1878 static bfd_boolean
1879 setup_sections (abfd, file_hdr, current_offset)
1880      bfd *abfd;
1881      struct header *file_hdr;
1882      unsigned long current_offset;
1883 {
1884   char *space_strings;
1885   unsigned int space_index, i;
1886   unsigned int total_subspaces = 0;
1887   asection **subspace_sections = NULL;
1888   asection *section;
1889   bfd_size_type amt;
1890
1891   /* First, read in space names.  */
1892
1893   amt = file_hdr->space_strings_size;
1894   space_strings = bfd_malloc (amt);
1895   if (!space_strings && amt != 0)
1896     goto error_return;
1897
1898   if (bfd_seek (abfd, current_offset + file_hdr->space_strings_location,
1899                 SEEK_SET) != 0)
1900     goto error_return;
1901   if (bfd_bread (space_strings, amt, abfd) != amt)
1902     goto error_return;
1903
1904   /* Loop over all of the space dictionaries, building up sections.  */
1905   for (space_index = 0; space_index < file_hdr->space_total; space_index++)
1906     {
1907       struct space_dictionary_record space;
1908       struct subspace_dictionary_record subspace, save_subspace;
1909       unsigned int subspace_index;
1910       asection *space_asect;
1911       char *newname;
1912
1913       /* Read the space dictionary element.  */
1914       if (bfd_seek (abfd,
1915                     (current_offset + file_hdr->space_location
1916                      + space_index * sizeof space),
1917                     SEEK_SET) != 0)
1918         goto error_return;
1919       amt = sizeof space;
1920       if (bfd_bread (&space, amt, abfd) != amt)
1921         goto error_return;
1922
1923       /* Setup the space name string.  */
1924       space.name.n_name = space.name.n_strx + space_strings;
1925
1926       /* Make a section out of it.  */
1927       amt = strlen (space.name.n_name) + 1;
1928       newname = bfd_alloc (abfd, amt);
1929       if (!newname)
1930         goto error_return;
1931       strcpy (newname, space.name.n_name);
1932
1933       space_asect = bfd_make_section_anyway (abfd, newname);
1934       if (!space_asect)
1935         goto error_return;
1936
1937       if (space.is_loadable == 0)
1938         space_asect->flags |= SEC_DEBUGGING;
1939
1940       /* Set up all the attributes for the space.  */
1941       if (! bfd_som_set_section_attributes (space_asect, space.is_defined,
1942                                             space.is_private, space.sort_key,
1943                                             space.space_number))
1944         goto error_return;
1945
1946       /* If the space has no subspaces, then we're done.  */
1947       if (space.subspace_quantity == 0)
1948         continue;
1949
1950       /* Now, read in the first subspace for this space.  */
1951       if (bfd_seek (abfd,
1952                     (current_offset + file_hdr->subspace_location
1953                      + space.subspace_index * sizeof subspace),
1954                     SEEK_SET) != 0)
1955         goto error_return;
1956       amt = sizeof subspace;
1957       if (bfd_bread (&subspace, amt, abfd) != amt)
1958         goto error_return;
1959       /* Seek back to the start of the subspaces for loop below.  */
1960       if (bfd_seek (abfd,
1961                     (current_offset + file_hdr->subspace_location
1962                      + space.subspace_index * sizeof subspace),
1963                     SEEK_SET) != 0)
1964         goto error_return;
1965
1966       /* Setup the start address and file loc from the first subspace
1967          record.  */
1968       space_asect->vma = subspace.subspace_start;
1969       space_asect->filepos = subspace.file_loc_init_value + current_offset;
1970       space_asect->alignment_power = log2 (subspace.alignment);
1971       if (space_asect->alignment_power == (unsigned) -1)
1972         goto error_return;
1973
1974       /* Initialize save_subspace so we can reliably determine if this
1975          loop placed any useful values into it.  */
1976       memset (&save_subspace, 0, sizeof (struct subspace_dictionary_record));
1977
1978       /* Loop over the rest of the subspaces, building up more sections.  */
1979       for (subspace_index = 0; subspace_index < space.subspace_quantity;
1980            subspace_index++)
1981         {
1982           asection *subspace_asect;
1983
1984           /* Read in the next subspace.  */
1985           amt = sizeof subspace;
1986           if (bfd_bread (&subspace, amt, abfd) != amt)
1987             goto error_return;
1988
1989           /* Setup the subspace name string.  */
1990           subspace.name.n_name = subspace.name.n_strx + space_strings;
1991
1992           amt = strlen (subspace.name.n_name) + 1;
1993           newname = bfd_alloc (abfd, amt);
1994           if (!newname)
1995             goto error_return;
1996           strcpy (newname, subspace.name.n_name);
1997
1998           /* Make a section out of this subspace.  */
1999           subspace_asect = bfd_make_section_anyway (abfd, newname);
2000           if (!subspace_asect)
2001             goto error_return;
2002
2003           /* Store private information about the section.  */
2004           if (! bfd_som_set_subsection_attributes (subspace_asect, space_asect,
2005                                                    subspace.access_control_bits,
2006                                                    subspace.sort_key,
2007                                                    subspace.quadrant))
2008             goto error_return;
2009
2010           /* Keep an easy mapping between subspaces and sections.
2011              Note we do not necessarily read the subspaces in the
2012              same order in which they appear in the object file.
2013
2014              So to make the target index come out correctly, we
2015              store the location of the subspace header in target
2016              index, then sort using the location of the subspace
2017              header as the key.  Then we can assign correct
2018              subspace indices.  */
2019           total_subspaces++;
2020           subspace_asect->target_index = bfd_tell (abfd) - sizeof (subspace);
2021
2022           /* Set SEC_READONLY and SEC_CODE/SEC_DATA as specified
2023              by the access_control_bits in the subspace header.  */
2024           switch (subspace.access_control_bits >> 4)
2025             {
2026             /* Readonly data.  */
2027             case 0x0:
2028               subspace_asect->flags |= SEC_DATA | SEC_READONLY;
2029               break;
2030
2031             /* Normal data.  */
2032             case 0x1:
2033               subspace_asect->flags |= SEC_DATA;
2034               break;
2035
2036             /* Readonly code and the gateways.
2037                Gateways have other attributes which do not map
2038                into anything BFD knows about.  */
2039             case 0x2:
2040             case 0x4:
2041             case 0x5:
2042             case 0x6:
2043             case 0x7:
2044               subspace_asect->flags |= SEC_CODE | SEC_READONLY;
2045               break;
2046
2047             /* dynamic (writable) code.  */
2048             case 0x3:
2049               subspace_asect->flags |= SEC_CODE;
2050               break;
2051             }
2052
2053           if (subspace.dup_common || subspace.is_common)
2054             subspace_asect->flags |= SEC_IS_COMMON;
2055           else if (subspace.subspace_length > 0)
2056             subspace_asect->flags |= SEC_HAS_CONTENTS;
2057
2058           if (subspace.is_loadable)
2059             subspace_asect->flags |= SEC_ALLOC | SEC_LOAD;
2060           else
2061             subspace_asect->flags |= SEC_DEBUGGING;
2062
2063           if (subspace.code_only)
2064             subspace_asect->flags |= SEC_CODE;
2065
2066           /* Both file_loc_init_value and initialization_length will
2067              be zero for a BSS like subspace.  */
2068           if (subspace.file_loc_init_value == 0
2069               && subspace.initialization_length == 0)
2070             subspace_asect->flags &= ~(SEC_DATA | SEC_LOAD | SEC_HAS_CONTENTS);
2071
2072           /* This subspace has relocations.
2073              The fixup_request_quantity is a byte count for the number of
2074              entries in the relocation stream; it is not the actual number
2075              of relocations in the subspace.  */
2076           if (subspace.fixup_request_quantity != 0)
2077             {
2078               subspace_asect->flags |= SEC_RELOC;
2079               subspace_asect->rel_filepos = subspace.fixup_request_index;
2080               som_section_data (subspace_asect)->reloc_size
2081                 = subspace.fixup_request_quantity;
2082               /* We can not determine this yet.  When we read in the
2083                  relocation table the correct value will be filled in.  */
2084               subspace_asect->reloc_count = (unsigned) -1;
2085             }
2086
2087           /* Update save_subspace if appropriate.  */
2088           if (subspace.file_loc_init_value > save_subspace.file_loc_init_value)
2089             save_subspace = subspace;
2090
2091           subspace_asect->vma = subspace.subspace_start;
2092           subspace_asect->_cooked_size = subspace.subspace_length;
2093           subspace_asect->_raw_size = subspace.subspace_length;
2094           subspace_asect->filepos = (subspace.file_loc_init_value
2095                                      + current_offset);
2096           subspace_asect->alignment_power = log2 (subspace.alignment);
2097           if (subspace_asect->alignment_power == (unsigned) -1)
2098             goto error_return;
2099         }
2100
2101       /* This can happen for a .o which defines symbols in otherwise
2102          empty subspaces.  */
2103       if (!save_subspace.file_loc_init_value)
2104         {
2105           space_asect->_cooked_size = 0;
2106           space_asect->_raw_size = 0;
2107         }
2108       else
2109         {
2110           /* Setup the sizes for the space section based upon the info in the
2111              last subspace of the space.  */
2112           space_asect->_cooked_size = (save_subspace.subspace_start
2113                                        - space_asect->vma
2114                                        + save_subspace.subspace_length);
2115           space_asect->_raw_size = (save_subspace.file_loc_init_value
2116                                     - space_asect->filepos
2117                                     + save_subspace.initialization_length);
2118         }
2119     }
2120   /* Now that we've read in all the subspace records, we need to assign
2121      a target index to each subspace.  */
2122   amt = total_subspaces;
2123   amt *= sizeof (asection *);
2124   subspace_sections = (asection **) bfd_malloc (amt);
2125   if (subspace_sections == NULL)
2126     goto error_return;
2127
2128   for (i = 0, section = abfd->sections; section; section = section->next)
2129     {
2130       if (!som_is_subspace (section))
2131         continue;
2132
2133       subspace_sections[i] = section;
2134       i++;
2135     }
2136   qsort (subspace_sections, total_subspaces,
2137          sizeof (asection *), compare_subspaces);
2138
2139   /* subspace_sections is now sorted in the order in which the subspaces
2140      appear in the object file.  Assign an index to each one now.  */
2141   for (i = 0; i < total_subspaces; i++)
2142     subspace_sections[i]->target_index = i;
2143
2144   if (space_strings != NULL)
2145     free (space_strings);
2146
2147   if (subspace_sections != NULL)
2148     free (subspace_sections);
2149
2150   return TRUE;
2151
2152  error_return:
2153   if (space_strings != NULL)
2154     free (space_strings);
2155
2156   if (subspace_sections != NULL)
2157     free (subspace_sections);
2158   return FALSE;
2159 }
2160
2161 /* Read in a SOM object and make it into a BFD.  */
2162
2163 static const bfd_target *
2164 som_object_p (abfd)
2165      bfd *abfd;
2166 {
2167   struct header file_hdr;
2168   struct som_exec_auxhdr aux_hdr;
2169   unsigned long current_offset = 0;
2170   struct lst_header lst_header;
2171   struct som_entry som_entry;
2172   bfd_size_type amt;
2173 #define ENTRY_SIZE sizeof (struct som_entry)
2174
2175   amt = FILE_HDR_SIZE;
2176   if (bfd_bread ((PTR) &file_hdr, amt, abfd) != amt)
2177     {
2178       if (bfd_get_error () != bfd_error_system_call)
2179         bfd_set_error (bfd_error_wrong_format);
2180       return 0;
2181     }
2182
2183   if (!_PA_RISC_ID (file_hdr.system_id))
2184     {
2185       bfd_set_error (bfd_error_wrong_format);
2186       return 0;
2187     }
2188
2189   switch (file_hdr.a_magic)
2190     {
2191     case RELOC_MAGIC:
2192     case EXEC_MAGIC:
2193     case SHARE_MAGIC:
2194     case DEMAND_MAGIC:
2195 #ifdef DL_MAGIC
2196     case DL_MAGIC:
2197 #endif
2198 #ifdef SHL_MAGIC
2199     case SHL_MAGIC:
2200 #endif
2201 #ifdef SHARED_MAGIC_CNX
2202     case SHARED_MAGIC_CNX:
2203 #endif
2204       break;
2205
2206 #ifdef EXECLIBMAGIC
2207     case EXECLIBMAGIC:
2208       /* Read the lst header and determine where the SOM directory begins.  */
2209
2210       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2211         {
2212           if (bfd_get_error () != bfd_error_system_call)
2213             bfd_set_error (bfd_error_wrong_format);
2214           return 0;
2215         }
2216
2217       amt = SLSTHDR;
2218       if (bfd_bread ((PTR) &lst_header, amt, abfd) != amt)
2219         {
2220           if (bfd_get_error () != bfd_error_system_call)
2221             bfd_set_error (bfd_error_wrong_format);
2222           return 0;
2223         }
2224
2225       /* Position to and read the first directory entry.  */
2226
2227       if (bfd_seek (abfd, lst_header.dir_loc, SEEK_SET) != 0)
2228         {
2229           if (bfd_get_error () != bfd_error_system_call)
2230             bfd_set_error (bfd_error_wrong_format);
2231           return 0;
2232         }
2233
2234       amt = ENTRY_SIZE;
2235       if (bfd_bread ((PTR) &som_entry, amt, abfd) != amt)
2236         {
2237           if (bfd_get_error () != bfd_error_system_call)
2238             bfd_set_error (bfd_error_wrong_format);
2239           return 0;
2240         }
2241
2242       /* Now position to the first SOM.  */
2243
2244       if (bfd_seek (abfd, som_entry.location, SEEK_SET) != 0)
2245         {
2246           if (bfd_get_error () != bfd_error_system_call)
2247             bfd_set_error (bfd_error_wrong_format);
2248           return 0;
2249         }
2250
2251       current_offset = som_entry.location;
2252
2253       /* And finally, re-read the som header.  */
2254       amt = FILE_HDR_SIZE;
2255       if (bfd_bread ((PTR) &file_hdr, amt, abfd) != amt)
2256         {
2257           if (bfd_get_error () != bfd_error_system_call)
2258             bfd_set_error (bfd_error_wrong_format);
2259           return 0;
2260         }
2261
2262       break;
2263 #endif
2264
2265     default:
2266       bfd_set_error (bfd_error_wrong_format);
2267       return 0;
2268     }
2269
2270   if (file_hdr.version_id != VERSION_ID
2271       && file_hdr.version_id != NEW_VERSION_ID)
2272     {
2273       bfd_set_error (bfd_error_wrong_format);
2274       return 0;
2275     }
2276
2277   /* If the aux_header_size field in the file header is zero, then this
2278      object is an incomplete executable (a .o file).  Do not try to read
2279      a non-existant auxiliary header.  */
2280   memset (&aux_hdr, 0, sizeof (struct som_exec_auxhdr));
2281   if (file_hdr.aux_header_size != 0)
2282     {
2283       amt = AUX_HDR_SIZE;
2284       if (bfd_bread ((PTR) &aux_hdr, amt, abfd) != amt)
2285         {
2286           if (bfd_get_error () != bfd_error_system_call)
2287             bfd_set_error (bfd_error_wrong_format);
2288           return 0;
2289         }
2290     }
2291
2292   if (!setup_sections (abfd, &file_hdr, current_offset))
2293     {
2294       /* setup_sections does not bubble up a bfd error code.  */
2295       bfd_set_error (bfd_error_bad_value);
2296       return 0;
2297     }
2298
2299   /* This appears to be a valid SOM object.  Do some initialization.  */
2300   return som_object_setup (abfd, &file_hdr, &aux_hdr, current_offset);
2301 }
2302
2303 /* Create a SOM object.  */
2304
2305 static bfd_boolean
2306 som_mkobject (abfd)
2307      bfd *abfd;
2308 {
2309   /* Allocate memory to hold backend information.  */
2310   abfd->tdata.som_data = (struct som_data_struct *)
2311     bfd_zalloc (abfd, (bfd_size_type) sizeof (struct som_data_struct));
2312   if (abfd->tdata.som_data == NULL)
2313     return FALSE;
2314   return TRUE;
2315 }
2316
2317 /* Initialize some information in the file header.  This routine makes
2318    not attempt at doing the right thing for a full executable; it
2319    is only meant to handle relocatable objects.  */
2320
2321 static bfd_boolean
2322 som_prep_headers (abfd)
2323      bfd *abfd;
2324 {
2325   struct header *file_hdr;
2326   asection *section;
2327   bfd_size_type amt = sizeof (struct header);
2328
2329   /* Make and attach a file header to the BFD.  */
2330   file_hdr = (struct header *) bfd_zalloc (abfd, amt);
2331   if (file_hdr == NULL)
2332     return FALSE;
2333   obj_som_file_hdr (abfd) = file_hdr;
2334
2335   if (abfd->flags & (EXEC_P | DYNAMIC))
2336     {
2337       /* Make and attach an exec header to the BFD.  */
2338       amt = sizeof (struct som_exec_auxhdr);
2339       obj_som_exec_hdr (abfd) =
2340         (struct som_exec_auxhdr *) bfd_zalloc (abfd, amt);
2341       if (obj_som_exec_hdr (abfd) == NULL)
2342         return FALSE;
2343
2344       if (abfd->flags & D_PAGED)
2345         file_hdr->a_magic = DEMAND_MAGIC;
2346       else if (abfd->flags & WP_TEXT)
2347         file_hdr->a_magic = SHARE_MAGIC;
2348 #ifdef SHL_MAGIC
2349       else if (abfd->flags & DYNAMIC)
2350         file_hdr->a_magic = SHL_MAGIC;
2351 #endif
2352       else
2353         file_hdr->a_magic = EXEC_MAGIC;
2354     }
2355   else
2356     file_hdr->a_magic = RELOC_MAGIC;
2357
2358   /* These fields are optional, and embedding timestamps is not always
2359      a wise thing to do, it makes comparing objects during a multi-stage
2360      bootstrap difficult.  */
2361   file_hdr->file_time.secs = 0;
2362   file_hdr->file_time.nanosecs = 0;
2363
2364   file_hdr->entry_space = 0;
2365   file_hdr->entry_subspace = 0;
2366   file_hdr->entry_offset = 0;
2367   file_hdr->presumed_dp = 0;
2368
2369   /* Now iterate over the sections translating information from
2370      BFD sections to SOM spaces/subspaces.  */
2371
2372   for (section = abfd->sections; section != NULL; section = section->next)
2373     {
2374       /* Ignore anything which has not been marked as a space or
2375          subspace.  */
2376       if (!som_is_space (section) && !som_is_subspace (section))
2377         continue;
2378
2379       if (som_is_space (section))
2380         {
2381           /* Allocate space for the space dictionary.  */
2382           amt = sizeof (struct space_dictionary_record);
2383           som_section_data (section)->space_dict =
2384             (struct space_dictionary_record *) bfd_zalloc (abfd, amt);
2385           if (som_section_data (section)->space_dict == NULL)
2386             return FALSE;
2387           /* Set space attributes.  Note most attributes of SOM spaces
2388              are set based on the subspaces it contains.  */
2389           som_section_data (section)->space_dict->loader_fix_index = -1;
2390           som_section_data (section)->space_dict->init_pointer_index = -1;
2391
2392           /* Set more attributes that were stuffed away in private data.  */
2393           som_section_data (section)->space_dict->sort_key =
2394             som_section_data (section)->copy_data->sort_key;
2395           som_section_data (section)->space_dict->is_defined =
2396             som_section_data (section)->copy_data->is_defined;
2397           som_section_data (section)->space_dict->is_private =
2398             som_section_data (section)->copy_data->is_private;
2399           som_section_data (section)->space_dict->space_number =
2400             som_section_data (section)->copy_data->space_number;
2401         }
2402       else
2403         {
2404           /* Allocate space for the subspace dictionary.  */
2405           amt = sizeof (struct subspace_dictionary_record);
2406           som_section_data (section)->subspace_dict =
2407             (struct subspace_dictionary_record *) bfd_zalloc (abfd, amt);
2408           if (som_section_data (section)->subspace_dict == NULL)
2409             return FALSE;
2410
2411           /* Set subspace attributes.  Basic stuff is done here, additional
2412              attributes are filled in later as more information becomes
2413              available.  */
2414           if (section->flags & SEC_IS_COMMON)
2415             {
2416               som_section_data (section)->subspace_dict->dup_common = 1;
2417               som_section_data (section)->subspace_dict->is_common = 1;
2418             }
2419
2420           if (section->flags & SEC_ALLOC)
2421             som_section_data (section)->subspace_dict->is_loadable = 1;
2422
2423           if (section->flags & SEC_CODE)
2424             som_section_data (section)->subspace_dict->code_only = 1;
2425
2426           som_section_data (section)->subspace_dict->subspace_start =
2427             section->vma;
2428           som_section_data (section)->subspace_dict->subspace_length =
2429             bfd_section_size (abfd, section);
2430           som_section_data (section)->subspace_dict->initialization_length =
2431             bfd_section_size (abfd, section);
2432           som_section_data (section)->subspace_dict->alignment =
2433             1 << section->alignment_power;
2434
2435           /* Set more attributes that were stuffed away in private data.  */
2436           som_section_data (section)->subspace_dict->sort_key =
2437             som_section_data (section)->copy_data->sort_key;
2438           som_section_data (section)->subspace_dict->access_control_bits =
2439             som_section_data (section)->copy_data->access_control_bits;
2440           som_section_data (section)->subspace_dict->quadrant =
2441             som_section_data (section)->copy_data->quadrant;
2442         }
2443     }
2444   return TRUE;
2445 }
2446
2447 /* Return TRUE if the given section is a SOM space, FALSE otherwise.  */
2448
2449 static bfd_boolean
2450 som_is_space (section)
2451      asection *section;
2452 {
2453   /* If no copy data is available, then it's neither a space nor a
2454      subspace.  */
2455   if (som_section_data (section)->copy_data == NULL)
2456     return FALSE;
2457
2458   /* If the containing space isn't the same as the given section,
2459      then this isn't a space.  */
2460   if (som_section_data (section)->copy_data->container != section
2461       && (som_section_data (section)->copy_data->container->output_section
2462           != section))
2463     return FALSE;
2464
2465   /* OK.  Must be a space.  */
2466   return TRUE;
2467 }
2468
2469 /* Return TRUE if the given section is a SOM subspace, FALSE otherwise.  */
2470
2471 static bfd_boolean
2472 som_is_subspace (section)
2473      asection *section;
2474 {
2475   /* If no copy data is available, then it's neither a space nor a
2476      subspace.  */
2477   if (som_section_data (section)->copy_data == NULL)
2478     return FALSE;
2479
2480   /* If the containing space is the same as the given section,
2481      then this isn't a subspace.  */
2482   if (som_section_data (section)->copy_data->container == section
2483       || (som_section_data (section)->copy_data->container->output_section
2484           == section))
2485     return FALSE;
2486
2487   /* OK.  Must be a subspace.  */
2488   return TRUE;
2489 }
2490
2491 /* Return TRUE if the given space contains the given subspace.  It
2492    is safe to assume space really is a space, and subspace really
2493    is a subspace.  */
2494
2495 static bfd_boolean
2496 som_is_container (space, subspace)
2497      asection *space, *subspace;
2498 {
2499   return (som_section_data (subspace)->copy_data->container == space
2500           || (som_section_data (subspace)->copy_data->container->output_section
2501               == space));
2502 }
2503
2504 /* Count and return the number of spaces attached to the given BFD.  */
2505
2506 static unsigned long
2507 som_count_spaces (abfd)
2508      bfd *abfd;
2509 {
2510   int count = 0;
2511   asection *section;
2512
2513   for (section = abfd->sections; section != NULL; section = section->next)
2514     count += som_is_space (section);
2515
2516   return count;
2517 }
2518
2519 /* Count the number of subspaces attached to the given BFD.  */
2520
2521 static unsigned long
2522 som_count_subspaces (abfd)
2523      bfd *abfd;
2524 {
2525   int count = 0;
2526   asection *section;
2527
2528   for (section = abfd->sections; section != NULL; section = section->next)
2529     count += som_is_subspace (section);
2530
2531   return count;
2532 }
2533
2534 /* Return -1, 0, 1 indicating the relative ordering of sym1 and sym2.
2535
2536    We desire symbols to be ordered starting with the symbol with the
2537    highest relocation count down to the symbol with the lowest relocation
2538    count.  Doing so compacts the relocation stream.  */
2539
2540 static int
2541 compare_syms (arg1, arg2)
2542      const PTR arg1;
2543      const PTR arg2;
2544
2545 {
2546   asymbol **sym1 = (asymbol **) arg1;
2547   asymbol **sym2 = (asymbol **) arg2;
2548   unsigned int count1, count2;
2549
2550   /* Get relocation count for each symbol.  Note that the count
2551      is stored in the udata pointer for section symbols!  */
2552   if ((*sym1)->flags & BSF_SECTION_SYM)
2553     count1 = (*sym1)->udata.i;
2554   else
2555     count1 = som_symbol_data (*sym1)->reloc_count;
2556
2557   if ((*sym2)->flags & BSF_SECTION_SYM)
2558     count2 = (*sym2)->udata.i;
2559   else
2560     count2 = som_symbol_data (*sym2)->reloc_count;
2561
2562   /* Return the appropriate value.  */
2563   if (count1 < count2)
2564     return 1;
2565   else if (count1 > count2)
2566     return -1;
2567   return 0;
2568 }
2569
2570 /* Return -1, 0, 1 indicating the relative ordering of subspace1
2571    and subspace.  */
2572
2573 static int
2574 compare_subspaces (arg1, arg2)
2575      const PTR arg1;
2576      const PTR arg2;
2577
2578 {
2579   asection **subspace1 = (asection **) arg1;
2580   asection **subspace2 = (asection **) arg2;
2581
2582   if ((*subspace1)->target_index < (*subspace2)->target_index)
2583     return -1;
2584   else if ((*subspace2)->target_index < (*subspace1)->target_index)
2585     return 1;
2586   else
2587     return 0;
2588 }
2589
2590 /* Perform various work in preparation for emitting the fixup stream.  */
2591
2592 static void
2593 som_prep_for_fixups (abfd, syms, num_syms)
2594      bfd *abfd;
2595      asymbol **syms;
2596      unsigned long num_syms;
2597 {
2598   unsigned long i;
2599   asection *section;
2600   asymbol **sorted_syms;
2601   bfd_size_type amt;
2602
2603   /* Most SOM relocations involving a symbol have a length which is
2604      dependent on the index of the symbol.  So symbols which are
2605      used often in relocations should have a small index.  */
2606
2607   /* First initialize the counters for each symbol.  */
2608   for (i = 0; i < num_syms; i++)
2609     {
2610       /* Handle a section symbol; these have no pointers back to the
2611          SOM symbol info.  So we just use the udata field to hold the
2612          relocation count.  */
2613       if (som_symbol_data (syms[i]) == NULL
2614           || syms[i]->flags & BSF_SECTION_SYM)
2615         {
2616           syms[i]->flags |= BSF_SECTION_SYM;
2617           syms[i]->udata.i = 0;
2618         }
2619       else
2620         som_symbol_data (syms[i])->reloc_count = 0;
2621     }
2622
2623   /* Now that the counters are initialized, make a weighted count
2624      of how often a given symbol is used in a relocation.  */
2625   for (section = abfd->sections; section != NULL; section = section->next)
2626     {
2627       int j;
2628
2629       /* Does this section have any relocations?  */
2630       if ((int) section->reloc_count <= 0)
2631         continue;
2632
2633       /* Walk through each relocation for this section.  */
2634       for (j = 1; j < (int) section->reloc_count; j++)
2635         {
2636           arelent *reloc = section->orelocation[j];
2637           int scale;
2638
2639           /* A relocation against a symbol in the *ABS* section really
2640              does not have a symbol.  Likewise if the symbol isn't associated
2641              with any section.  */
2642           if (reloc->sym_ptr_ptr == NULL
2643               || bfd_is_abs_section ((*reloc->sym_ptr_ptr)->section))
2644             continue;
2645
2646           /* Scaling to encourage symbols involved in R_DP_RELATIVE
2647              and R_CODE_ONE_SYMBOL relocations to come first.  These
2648              two relocations have single byte versions if the symbol
2649              index is very small.  */
2650           if (reloc->howto->type == R_DP_RELATIVE
2651               || reloc->howto->type == R_CODE_ONE_SYMBOL)
2652             scale = 2;
2653           else
2654             scale = 1;
2655
2656           /* Handle section symbols by storing the count in the udata
2657              field.  It will not be used and the count is very important
2658              for these symbols.  */
2659           if ((*reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
2660             {
2661               (*reloc->sym_ptr_ptr)->udata.i =
2662                 (*reloc->sym_ptr_ptr)->udata.i + scale;
2663               continue;
2664             }
2665
2666           /* A normal symbol.  Increment the count.  */
2667           som_symbol_data (*reloc->sym_ptr_ptr)->reloc_count += scale;
2668         }
2669     }
2670
2671   /* Sort a copy of the symbol table, rather than the canonical
2672      output symbol table.  */
2673   amt = num_syms;
2674   amt *= sizeof (asymbol *);
2675   sorted_syms = (asymbol **) bfd_zalloc (abfd, amt);
2676   memcpy (sorted_syms, syms, num_syms * sizeof (asymbol *));
2677   qsort (sorted_syms, num_syms, sizeof (asymbol *), compare_syms);
2678   obj_som_sorted_syms (abfd) = sorted_syms;
2679
2680   /* Compute the symbol indexes, they will be needed by the relocation
2681      code.  */
2682   for (i = 0; i < num_syms; i++)
2683     {
2684       /* A section symbol.  Again, there is no pointer to backend symbol
2685          information, so we reuse the udata field again.  */
2686       if (sorted_syms[i]->flags & BSF_SECTION_SYM)
2687         sorted_syms[i]->udata.i = i;
2688       else
2689         som_symbol_data (sorted_syms[i])->index = i;
2690     }
2691 }
2692
2693 static bfd_boolean
2694 som_write_fixups (abfd, current_offset, total_reloc_sizep)
2695      bfd *abfd;
2696      unsigned long current_offset;
2697      unsigned int *total_reloc_sizep;
2698 {
2699   unsigned int i, j;
2700   /* Chunk of memory that we can use as buffer space, then throw
2701      away.  */
2702   unsigned char tmp_space[SOM_TMP_BUFSIZE];
2703   unsigned char *p;
2704   unsigned int total_reloc_size = 0;
2705   unsigned int subspace_reloc_size = 0;
2706   unsigned int num_spaces = obj_som_file_hdr (abfd)->space_total;
2707   asection *section = abfd->sections;
2708   bfd_size_type amt;
2709
2710   memset (tmp_space, 0, SOM_TMP_BUFSIZE);
2711   p = tmp_space;
2712
2713   /* All the fixups for a particular subspace are emitted in a single
2714      stream.  All the subspaces for a particular space are emitted
2715      as a single stream.
2716
2717      So, to get all the locations correct one must iterate through all the
2718      spaces, for each space iterate through its subspaces and output a
2719      fixups stream.  */
2720   for (i = 0; i < num_spaces; i++)
2721     {
2722       asection *subsection;
2723
2724       /* Find a space.  */
2725       while (!som_is_space (section))
2726         section = section->next;
2727
2728       /* Now iterate through each of its subspaces.  */
2729       for (subsection = abfd->sections;
2730            subsection != NULL;
2731            subsection = subsection->next)
2732         {
2733           int reloc_offset;
2734           unsigned int current_rounding_mode;
2735 #ifndef NO_PCREL_MODES
2736           unsigned int current_call_mode;
2737 #endif
2738
2739           /* Find a subspace of this space.  */
2740           if (!som_is_subspace (subsection)
2741               || !som_is_container (section, subsection))
2742             continue;
2743
2744           /* If this subspace does not have real data, then we are
2745              finished with it.  */
2746           if ((subsection->flags & SEC_HAS_CONTENTS) == 0)
2747             {
2748               som_section_data (subsection)->subspace_dict->fixup_request_index
2749                 = -1;
2750               continue;
2751             }
2752
2753           /* This subspace has some relocations.  Put the relocation stream
2754              index into the subspace record.  */
2755           som_section_data (subsection)->subspace_dict->fixup_request_index
2756             = total_reloc_size;
2757
2758           /* To make life easier start over with a clean slate for
2759              each subspace.  Seek to the start of the relocation stream
2760              for this subspace in preparation for writing out its fixup
2761              stream.  */
2762           if (bfd_seek (abfd, current_offset + total_reloc_size, SEEK_SET) != 0)
2763             return FALSE;
2764
2765           /* Buffer space has already been allocated.  Just perform some
2766              initialization here.  */
2767           p = tmp_space;
2768           subspace_reloc_size = 0;
2769           reloc_offset = 0;
2770           som_initialize_reloc_queue (reloc_queue);
2771           current_rounding_mode = R_N_MODE;
2772 #ifndef NO_PCREL_MODES
2773           current_call_mode = R_SHORT_PCREL_MODE;
2774 #endif
2775
2776           /* Translate each BFD relocation into one or more SOM
2777              relocations.  */
2778           for (j = 0; j < subsection->reloc_count; j++)
2779             {
2780               arelent *bfd_reloc = subsection->orelocation[j];
2781               unsigned int skip;
2782               int sym_num;
2783
2784               /* Get the symbol number.  Remember it's stored in a
2785                  special place for section symbols.  */
2786               if ((*bfd_reloc->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
2787                 sym_num = (*bfd_reloc->sym_ptr_ptr)->udata.i;
2788               else
2789                 sym_num = som_symbol_data (*bfd_reloc->sym_ptr_ptr)->index;
2790
2791               /* If there is not enough room for the next couple relocations,
2792                  then dump the current buffer contents now.  Also reinitialize
2793                  the relocation queue.
2794
2795                  No single BFD relocation could ever translate into more
2796                  than 100 bytes of SOM relocations (20bytes is probably the
2797                  upper limit, but leave lots of space for growth).  */
2798               if (p - tmp_space + 100 > SOM_TMP_BUFSIZE)
2799                 {
2800                   amt = p - tmp_space;
2801                   if (bfd_bwrite ((PTR) tmp_space, amt, abfd) != amt)
2802                     return FALSE;
2803
2804                   p = tmp_space;
2805                   som_initialize_reloc_queue (reloc_queue);
2806                 }
2807
2808               /* Emit R_NO_RELOCATION fixups to map any bytes which were
2809                  skipped.  */
2810               skip = bfd_reloc->address - reloc_offset;
2811               p = som_reloc_skip (abfd, skip, p,
2812                                   &subspace_reloc_size, reloc_queue);
2813
2814               /* Update reloc_offset for the next iteration.
2815
2816                  Many relocations do not consume input bytes.  They
2817                  are markers, or set state necessary to perform some
2818                  later relocation.  */
2819               switch (bfd_reloc->howto->type)
2820                 {
2821                 case R_ENTRY:
2822                 case R_ALT_ENTRY:
2823                 case R_EXIT:
2824                 case R_N_MODE:
2825                 case R_S_MODE:
2826                 case R_D_MODE:
2827                 case R_R_MODE:
2828                 case R_FSEL:
2829                 case R_LSEL:
2830                 case R_RSEL:
2831                 case R_COMP1:
2832                 case R_COMP2:
2833                 case R_BEGIN_BRTAB:
2834                 case R_END_BRTAB:
2835                 case R_BEGIN_TRY:
2836                 case R_END_TRY:
2837                 case R_N0SEL:
2838                 case R_N1SEL:
2839 #ifndef NO_PCREL_MODES
2840                 case R_SHORT_PCREL_MODE:
2841                 case R_LONG_PCREL_MODE:
2842 #endif
2843                   reloc_offset = bfd_reloc->address;
2844                   break;
2845
2846                 default:
2847                   reloc_offset = bfd_reloc->address + 4;
2848                   break;
2849                 }
2850
2851               /* Now the actual relocation we care about.  */
2852               switch (bfd_reloc->howto->type)
2853                 {
2854                 case R_PCREL_CALL:
2855                 case R_ABS_CALL:
2856                   p = som_reloc_call (abfd, p, &subspace_reloc_size,
2857                                       bfd_reloc, sym_num, reloc_queue);
2858                   break;
2859
2860                 case R_CODE_ONE_SYMBOL:
2861                 case R_DP_RELATIVE:
2862                   /* Account for any addend.  */
2863                   if (bfd_reloc->addend)
2864                     p = som_reloc_addend (abfd, bfd_reloc->addend, p,
2865                                           &subspace_reloc_size, reloc_queue);
2866
2867                   if (sym_num < 0x20)
2868                     {
2869                       bfd_put_8 (abfd, bfd_reloc->howto->type + sym_num, p);
2870                       subspace_reloc_size += 1;
2871                       p += 1;
2872                     }
2873                   else if (sym_num < 0x100)
2874                     {
2875                       bfd_put_8 (abfd, bfd_reloc->howto->type + 32, p);
2876                       bfd_put_8 (abfd, sym_num, p + 1);
2877                       p = try_prev_fixup (abfd, &subspace_reloc_size, p,
2878                                           2, reloc_queue);
2879                     }
2880                   else if (sym_num < 0x10000000)
2881                     {
2882                       bfd_put_8 (abfd, bfd_reloc->howto->type + 33, p);
2883                       bfd_put_8 (abfd, sym_num >> 16, p + 1);
2884                       bfd_put_16 (abfd, (bfd_vma) sym_num, p + 2);
2885                       p = try_prev_fixup (abfd, &subspace_reloc_size,
2886                                           p, 4, reloc_queue);
2887                     }
2888                   else
2889                     abort ();
2890                   break;
2891
2892                 case R_DATA_ONE_SYMBOL:
2893                 case R_DATA_PLABEL:
2894                 case R_CODE_PLABEL:
2895                 case R_DLT_REL:
2896                   /* Account for any addend using R_DATA_OVERRIDE.  */
2897                   if (bfd_reloc->howto->type != R_DATA_ONE_SYMBOL
2898                       && bfd_reloc->addend)
2899                     p = som_reloc_addend (abfd, bfd_reloc->addend, p,
2900                                           &subspace_reloc_size, reloc_queue);
2901
2902                   if (sym_num < 0x100)
2903                     {
2904                       bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2905                       bfd_put_8 (abfd, sym_num, p + 1);
2906                       p = try_prev_fixup (abfd, &subspace_reloc_size, p,
2907                                           2, reloc_queue);
2908                     }
2909                   else if (sym_num < 0x10000000)
2910                     {
2911                       bfd_put_8 (abfd, bfd_reloc->howto->type + 1, p);
2912                       bfd_put_8 (abfd, sym_num >> 16, p + 1);
2913                       bfd_put_16 (abfd, (bfd_vma) sym_num, p + 2);
2914                       p = try_prev_fixup (abfd, &subspace_reloc_size,
2915                                           p, 4, reloc_queue);
2916                     }
2917                   else
2918                     abort ();
2919                   break;
2920
2921                 case R_ENTRY:
2922                   {
2923                     unsigned int tmp;
2924                     arelent *tmp_reloc = NULL;
2925                     bfd_put_8 (abfd, R_ENTRY, p);
2926
2927                     /* R_ENTRY relocations have 64 bits of associated
2928                        data.  Unfortunately the addend field of a bfd
2929                        relocation is only 32 bits.  So, we split up
2930                        the 64bit unwind information and store part in
2931                        the R_ENTRY relocation, and the rest in the R_EXIT
2932                        relocation.  */
2933                     bfd_put_32 (abfd, bfd_reloc->addend, p + 1);
2934
2935                     /* Find the next R_EXIT relocation.  */
2936                     for (tmp = j; tmp < subsection->reloc_count; tmp++)
2937                       {
2938                         tmp_reloc = subsection->orelocation[tmp];
2939                         if (tmp_reloc->howto->type == R_EXIT)
2940                           break;
2941                       }
2942
2943                     if (tmp == subsection->reloc_count)
2944                       abort ();
2945
2946                     bfd_put_32 (abfd, tmp_reloc->addend, p + 5);
2947                     p = try_prev_fixup (abfd, &subspace_reloc_size,
2948                                         p, 9, reloc_queue);
2949                     break;
2950                   }
2951
2952                 case R_N_MODE:
2953                 case R_S_MODE:
2954                 case R_D_MODE:
2955                 case R_R_MODE:
2956                   /* If this relocation requests the current rounding
2957                      mode, then it is redundant.  */
2958                   if (bfd_reloc->howto->type != current_rounding_mode)
2959                     {
2960                       bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2961                       subspace_reloc_size += 1;
2962                       p += 1;
2963                       current_rounding_mode = bfd_reloc->howto->type;
2964                     }
2965                   break;
2966
2967 #ifndef NO_PCREL_MODES
2968                 case R_LONG_PCREL_MODE:
2969                 case R_SHORT_PCREL_MODE:
2970                   if (bfd_reloc->howto->type != current_call_mode)
2971                     {
2972                       bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2973                       subspace_reloc_size += 1;
2974                       p += 1;
2975                       current_call_mode = bfd_reloc->howto->type;
2976                     }
2977                   break;
2978 #endif
2979
2980                 case R_EXIT:
2981                 case R_ALT_ENTRY:
2982                 case R_FSEL:
2983                 case R_LSEL:
2984                 case R_RSEL:
2985                 case R_BEGIN_BRTAB:
2986                 case R_END_BRTAB:
2987                 case R_BEGIN_TRY:
2988                 case R_N0SEL:
2989                 case R_N1SEL:
2990                   bfd_put_8 (abfd, bfd_reloc->howto->type, p);
2991                   subspace_reloc_size += 1;
2992                   p += 1;
2993                   break;
2994
2995                 case R_END_TRY:
2996                   /* The end of an exception handling region.  The reloc's
2997                      addend contains the offset of the exception handling
2998                      code.  */
2999                   if (bfd_reloc->addend == 0)
3000                     bfd_put_8 (abfd, bfd_reloc->howto->type, p);
3001                   else if (bfd_reloc->addend < 1024)
3002                     {
3003                       bfd_put_8 (abfd, bfd_reloc->howto->type + 1, p);
3004                       bfd_put_8 (abfd, bfd_reloc->addend / 4, p + 1);
3005                       p = try_prev_fixup (abfd, &subspace_reloc_size,
3006                                           p, 2, reloc_queue);
3007                     }
3008                   else
3009                     {
3010                       bfd_put_8 (abfd, bfd_reloc->howto->type + 2, p);
3011                       bfd_put_8 (abfd, (bfd_reloc->addend / 4) >> 16, p + 1);
3012                       bfd_put_16 (abfd, bfd_reloc->addend / 4, p + 2);
3013                       p = try_prev_fixup (abfd, &subspace_reloc_size,
3014                                           p, 4, reloc_queue);
3015                     }
3016                   break;
3017
3018                 case R_COMP1:
3019                   /* The only time we generate R_COMP1, R_COMP2 and
3020                      R_CODE_EXPR relocs is for the difference of two
3021                      symbols.  Hence we can cheat here.  */
3022                   bfd_put_8 (abfd, bfd_reloc->howto->type, p);
3023                   bfd_put_8 (abfd, 0x44, p + 1);
3024                   p = try_prev_fixup (abfd, &subspace_reloc_size,
3025                                       p, 2, reloc_queue);
3026                   break;
3027
3028                 case R_COMP2:
3029                   /* The only time we generate R_COMP1, R_COMP2 and
3030                      R_CODE_EXPR relocs is for the difference of two
3031                      symbols.  Hence we can cheat here.  */
3032                   bfd_put_8 (abfd, bfd_reloc->howto->type, p);
3033                   bfd_put_8 (abfd, 0x80, p + 1);
3034                   bfd_put_8 (abfd, sym_num >> 16, p + 2);
3035                   bfd_put_16 (abfd, (bfd_vma) sym_num, p + 3);
3036                   p = try_prev_fixup (abfd, &subspace_reloc_size,
3037                                       p, 5, reloc_queue);
3038                   break;
3039
3040                 case R_CODE_EXPR:
3041                 case R_DATA_EXPR:
3042                   /* The only time we generate R_COMP1, R_COMP2 and
3043                      R_CODE_EXPR relocs is for the difference of two
3044                      symbols.  Hence we can cheat here.  */
3045                   bfd_put_8 (abfd, bfd_reloc->howto->type, p);
3046                   subspace_reloc_size += 1;
3047                   p += 1;
3048                   break;
3049
3050                 /* Put a "R_RESERVED" relocation in the stream if
3051                    we hit something we do not understand.  The linker
3052                    will complain loudly if this ever happens.  */
3053                 default:
3054                   bfd_put_8 (abfd, 0xff, p);
3055                   subspace_reloc_size += 1;
3056                   p += 1;
3057                   break;
3058                 }
3059             }
3060
3061           /* Last BFD relocation for a subspace has been processed.
3062              Map the rest of the subspace with R_NO_RELOCATION fixups.  */
3063           p = som_reloc_skip (abfd, (bfd_section_size (abfd, subsection)
3064                                      - reloc_offset),
3065                               p, &subspace_reloc_size, reloc_queue);
3066
3067           /* Scribble out the relocations.  */
3068           amt = p - tmp_space;
3069           if (bfd_bwrite ((PTR) tmp_space, amt, abfd) != amt)
3070             return FALSE;
3071           p = tmp_space;
3072
3073           total_reloc_size += subspace_reloc_size;
3074           som_section_data (subsection)->subspace_dict->fixup_request_quantity
3075             = subspace_reloc_size;
3076         }
3077       section = section->next;
3078     }
3079   *total_reloc_sizep = total_reloc_size;
3080   return TRUE;
3081 }
3082
3083 /* Write out the space/subspace string table.  */
3084
3085 static bfd_boolean
3086 som_write_space_strings (abfd, current_offset, string_sizep)
3087      bfd *abfd;
3088      unsigned long current_offset;
3089      unsigned int *string_sizep;
3090 {
3091   /* Chunk of memory that we can use as buffer space, then throw
3092      away.  */
3093   size_t tmp_space_size = SOM_TMP_BUFSIZE;
3094   unsigned char *tmp_space = alloca (tmp_space_size);
3095   unsigned char *p = tmp_space;
3096   unsigned int strings_size = 0;
3097   asection *section;
3098   bfd_size_type amt;
3099
3100   /* Seek to the start of the space strings in preparation for writing
3101      them out.  */
3102   if (bfd_seek (abfd, (file_ptr) current_offset, SEEK_SET) != 0)
3103     return FALSE;
3104
3105   /* Walk through all the spaces and subspaces (order is not important)
3106      building up and writing string table entries for their names.  */
3107   for (section = abfd->sections; section != NULL; section = section->next)
3108     {
3109       size_t length;
3110
3111       /* Only work with space/subspaces; avoid any other sections
3112          which might have been made (.text for example).  */
3113       if (!som_is_space (section) && !som_is_subspace (section))
3114         continue;
3115
3116       /* Get the length of the space/subspace name.  */
3117       length = strlen (section->name);
3118
3119       /* If there is not enough room for the next entry, then dump the
3120          current buffer contents now and maybe allocate a larger
3121          buffer.  Each entry will take 4 bytes to hold the string
3122          length + the string itself + null terminator.  */
3123       if (p - tmp_space + 5 + length > tmp_space_size)
3124         {
3125           /* Flush buffer before refilling or reallocating.  */
3126           amt = p - tmp_space;
3127           if (bfd_bwrite ((PTR) &tmp_space[0], amt, abfd) != amt)
3128             return FALSE;
3129
3130           /* Reallocate if now empty buffer still too small.  */
3131           if (5 + length > tmp_space_size)
3132             {
3133               /* Ensure a minimum growth factor to avoid O(n**2) space
3134                  consumption for n strings.  The optimal minimum
3135                  factor seems to be 2, as no other value can guarantee
3136                  wasting less than 50% space.  (Note that we cannot
3137                  deallocate space allocated by `alloca' without
3138                  returning from this function.)  The same technique is
3139                  used a few more times below when a buffer is
3140                  reallocated.  */
3141               tmp_space_size = MAX (2 * tmp_space_size, 5 + length);
3142               tmp_space = alloca (tmp_space_size);
3143             }
3144
3145           /* Reset to beginning of the (possibly new) buffer space.  */
3146           p = tmp_space;
3147         }
3148
3149       /* First element in a string table entry is the length of the
3150          string.  Alignment issues are already handled.  */
3151       bfd_put_32 (abfd, (bfd_vma) length, p);
3152       p += 4;
3153       strings_size += 4;
3154
3155       /* Record the index in the space/subspace records.  */
3156       if (som_is_space (section))
3157         som_section_data (section)->space_dict->name.n_strx = strings_size;
3158       else
3159         som_section_data (section)->subspace_dict->name.n_strx = strings_size;
3160
3161       /* Next comes the string itself + a null terminator.  */
3162       strcpy (p, section->name);
3163       p += length + 1;
3164       strings_size += length + 1;
3165
3166       /* Always align up to the next word boundary.  */
3167       while (strings_size % 4)
3168         {
3169           bfd_put_8 (abfd, 0, p);
3170           p++;
3171           strings_size++;
3172         }
3173     }
3174
3175   /* Done with the space/subspace strings.  Write out any information
3176      contained in a partial block.  */
3177   amt = p - tmp_space;
3178   if (bfd_bwrite ((PTR) &tmp_space[0], amt, abfd) != amt)
3179     return FALSE;
3180   *string_sizep = strings_size;
3181   return TRUE;
3182 }
3183
3184 /* Write out the symbol string table.  */
3185
3186 static bfd_boolean
3187 som_write_symbol_strings (abfd, current_offset, syms, num_syms, string_sizep,
3188                           compilation_unit)
3189      bfd *abfd;
3190      unsigned long current_offset;
3191      asymbol **syms;
3192      unsigned int num_syms;
3193      unsigned int *string_sizep;
3194      COMPUNIT *compilation_unit;
3195 {
3196   unsigned int i;
3197
3198   /* Chunk of memory that we can use as buffer space, then throw
3199      away.  */
3200   size_t tmp_space_size = SOM_TMP_BUFSIZE;
3201   unsigned char *tmp_space = alloca (tmp_space_size);
3202   unsigned char *p = tmp_space;
3203
3204   unsigned int strings_size = 0;
3205   unsigned char *comp[4];
3206   bfd_size_type amt;
3207
3208   /* This gets a bit gruesome because of the compilation unit.  The
3209      strings within the compilation unit are part of the symbol
3210      strings, but don't have symbol_dictionary entries.  So, manually
3211      write them and update the compilation unit header.  On input, the
3212      compilation unit header contains local copies of the strings.
3213      Move them aside.  */
3214   if (compilation_unit)
3215     {
3216       comp[0] = compilation_unit->name.n_name;
3217       comp[1] = compilation_unit->language_name.n_name;
3218       comp[2] = compilation_unit->product_id.n_name;
3219       comp[3] = compilation_unit->version_id.n_name;
3220     }
3221
3222   /* Seek to the start of the space strings in preparation for writing
3223      them out.  */
3224   if (bfd_seek (abfd, (file_ptr) current_offset, SEEK_SET) != 0)
3225     return FALSE;
3226
3227   if (compilation_unit)
3228     {
3229       for (i = 0; i < 4; i++)
3230         {
3231           size_t length = strlen (comp[i]);
3232
3233           /* If there is not enough room for the next entry, then dump
3234              the current buffer contents now and maybe allocate a
3235              larger buffer.  */
3236           if (p - tmp_space + 5 + length > tmp_space_size)
3237             {
3238               /* Flush buffer before refilling or reallocating.  */
3239               amt = p - tmp_space;
3240               if (bfd_bwrite ((PTR) &tmp_space[0], amt, abfd) != amt)
3241                 return FALSE;
3242
3243               /* Reallocate if now empty buffer still too small.  */
3244               if (5 + length > tmp_space_size)
3245                 {
3246                   /* See alloca above for discussion of new size.  */
3247                   tmp_space_size = MAX (2 * tmp_space_size, 5 + length);
3248                   tmp_space = alloca (tmp_space_size);
3249                 }
3250
3251               /* Reset to beginning of the (possibly new) buffer
3252                  space.  */
3253               p = tmp_space;
3254             }
3255
3256           /* First element in a string table entry is the length of
3257              the string.  This must always be 4 byte aligned.  This is
3258              also an appropriate time to fill in the string index
3259              field in the symbol table entry.  */
3260           bfd_put_32 (abfd, (bfd_vma) length, p);
3261           strings_size += 4;
3262           p += 4;
3263
3264           /* Next comes the string itself + a null terminator.  */
3265           strcpy (p, comp[i]);
3266
3267           switch (i)
3268             {
3269             case 0:
3270               obj_som_compilation_unit (abfd)->name.n_strx = strings_size;
3271               break;
3272             case 1:
3273               obj_som_compilation_unit (abfd)->language_name.n_strx =
3274                 strings_size;
3275               break;
3276             case 2:
3277               obj_som_compilation_unit (abfd)->product_id.n_strx =
3278                 strings_size;
3279               break;
3280             case 3:
3281               obj_som_compilation_unit (abfd)->version_id.n_strx =
3282                 strings_size;
3283               break;
3284             }
3285
3286           p += length + 1;
3287           strings_size += length + 1;
3288
3289           /* Always align up to the next word boundary.  */
3290           while (strings_size % 4)
3291             {
3292               bfd_put_8 (abfd, 0, p);
3293               strings_size++;
3294               p++;
3295             }
3296         }
3297     }
3298
3299   for (i = 0; i < num_syms; i++)
3300     {
3301       size_t length = strlen (syms[i]->name);
3302
3303       /* If there is not enough room for the next entry, then dump the
3304          current buffer contents now and maybe allocate a larger buffer.  */
3305      if (p - tmp_space + 5 + length > tmp_space_size)
3306         {
3307           /* Flush buffer before refilling or reallocating.  */
3308           amt = p - tmp_space;
3309           if (bfd_bwrite ((PTR) &tmp_space[0], amt, abfd) != amt)
3310             return FALSE;
3311
3312           /* Reallocate if now empty buffer still too small.  */
3313           if (5 + length > tmp_space_size)
3314             {
3315               /* See alloca above for discussion of new size.  */
3316               tmp_space_size = MAX (2 * tmp_space_size, 5 + length);
3317               tmp_space = alloca (tmp_space_size);
3318             }
3319
3320           /* Reset to beginning of the (possibly new) buffer space.  */
3321           p = tmp_space;
3322         }
3323
3324       /* First element in a string table entry is the length of the
3325          string.  This must always be 4 byte aligned.  This is also
3326          an appropriate time to fill in the string index field in the
3327          symbol table entry.  */
3328       bfd_put_32 (abfd, (bfd_vma) length, p);
3329       strings_size += 4;
3330       p += 4;
3331
3332       /* Next comes the string itself + a null terminator.  */
3333       strcpy (p, syms[i]->name);
3334
3335       som_symbol_data (syms[i])->stringtab_offset = strings_size;
3336       p += length + 1;
3337       strings_size += length + 1;
3338
3339       /* Always align up to the next word boundary.  */
3340       while (strings_size % 4)
3341         {
3342           bfd_put_8 (abfd, 0, p);
3343           strings_size++;
3344           p++;
3345         }
3346     }
3347
3348   /* Scribble out any partial block.  */
3349   amt = p - tmp_space;
3350   if (bfd_bwrite ((PTR) &tmp_space[0], amt, abfd) != amt)
3351     return FALSE;
3352
3353   *string_sizep = strings_size;
3354   return TRUE;
3355 }
3356
3357 /* Compute variable information to be placed in the SOM headers,
3358    space/subspace dictionaries, relocation streams, etc.  Begin
3359    writing parts of the object file.  */
3360
3361 static bfd_boolean
3362 som_begin_writing (abfd)
3363      bfd *abfd;
3364 {
3365   unsigned long current_offset = 0;
3366   int strings_size = 0;
3367   unsigned long num_spaces, num_subspaces, i;
3368   asection *section;
3369   unsigned int total_subspaces = 0;
3370   struct som_exec_auxhdr *exec_header = NULL;
3371
3372   /* The file header will always be first in an object file,
3373      everything else can be in random locations.  To keep things
3374      "simple" BFD will lay out the object file in the manner suggested
3375      by the PRO ABI for PA-RISC Systems.  */
3376
3377   /* Before any output can really begin offsets for all the major
3378      portions of the object file must be computed.  So, starting
3379      with the initial file header compute (and sometimes write)
3380      each portion of the object file.  */
3381
3382   /* Make room for the file header, it's contents are not complete
3383      yet, so it can not be written at this time.  */
3384   current_offset += sizeof (struct header);
3385
3386   /* Any auxiliary headers will follow the file header.  Right now
3387      we support only the copyright and version headers.  */
3388   obj_som_file_hdr (abfd)->aux_header_location = current_offset;
3389   obj_som_file_hdr (abfd)->aux_header_size = 0;
3390   if (abfd->flags & (EXEC_P | DYNAMIC))
3391     {
3392       /* Parts of the exec header will be filled in later, so
3393          delay writing the header itself.  Fill in the defaults,
3394          and write it later.  */
3395       current_offset += sizeof (struct som_exec_auxhdr);
3396       obj_som_file_hdr (abfd)->aux_header_size
3397         += sizeof (struct som_exec_auxhdr);
3398       exec_header = obj_som_exec_hdr (abfd);
3399       exec_header->som_auxhdr.type = EXEC_AUX_ID;
3400       exec_header->som_auxhdr.length = 40;
3401     }
3402   if (obj_som_version_hdr (abfd) != NULL)
3403     {
3404       bfd_size_type len;
3405
3406       if (bfd_seek (abfd, (file_ptr) current_offset, SEEK_SET) != 0)
3407         return FALSE;
3408
3409       /* Write the aux_id structure and the string length.  */
3410       len = sizeof (struct aux_id) + sizeof (unsigned int);
3411       obj_som_file_hdr (abfd)->aux_header_size += len;
3412       current_offset += len;
3413       if (bfd_bwrite ((PTR) obj_som_version_hdr (abfd), len, abfd) != len)
3414         return FALSE;
3415
3416       /* Write the version string.  */
3417       len = obj_som_version_hdr (abfd)->header_id.length - sizeof (int);
3418       obj_som_file_hdr (abfd)->aux_header_size += len;
3419       current_offset += len;
3420       if (bfd_bwrite ((PTR) obj_som_version_hdr (abfd)->user_string, len, abfd)
3421           != len)
3422         return FALSE;
3423     }
3424
3425   if (obj_som_copyright_hdr (abfd) != NULL)
3426     {
3427       bfd_size_type len;
3428
3429       if (bfd_seek (abfd, (file_ptr) current_offset, SEEK_SET) != 0)
3430         return FALSE;
3431
3432       /* Write the aux_id structure and the string length.  */
3433       len = sizeof (struct aux_id) + sizeof (unsigned int);
3434       obj_som_file_hdr (abfd)->aux_header_size += len;
3435       current_offset += len;
3436       if (bfd_bwrite ((PTR) obj_som_copyright_hdr (abfd), len, abfd) != len)
3437         return FALSE;
3438
3439       /* Write the copyright string.  */
3440       len = obj_som_copyright_hdr (abfd)->header_id.length - sizeof (int);
3441       obj_som_file_hdr (abfd)->aux_header_size += len;
3442       current_offset += len;
3443       if (bfd_bwrite ((PTR) obj_som_copyright_hdr (abfd)->copyright, len, abfd)
3444           != len)
3445         return FALSE;
3446     }
3447
3448   /* Next comes the initialization pointers; we have no initialization
3449      pointers, so current offset does not change.  */
3450   obj_som_file_hdr (abfd)->init_array_location = current_offset;
3451   obj_som_file_hdr (abfd)->init_array_total = 0;
3452
3453   /* Next are the space records.  These are fixed length records.
3454
3455      Count the number of spaces to determine how much room is needed
3456      in the object file for the space records.
3457
3458      The names of the spaces are stored in a separate string table,
3459      and the index for each space into the string table is computed
3460      below.  Therefore, it is not possible to write the space headers
3461      at this time.  */
3462   num_spaces = som_count_spaces (abfd);
3463   obj_som_file_hdr (abfd)->space_location = current_offset;
3464   obj_som_file_hdr (abfd)->space_total = num_spaces;
3465   current_offset += num_spaces * sizeof (struct space_dictionary_record);
3466
3467   /* Next are the subspace records.  These are fixed length records.
3468
3469      Count the number of subspaes to determine how much room is needed
3470      in the object file for the subspace records.
3471
3472      A variety if fields in the subspace record are still unknown at
3473      this time (index into string table, fixup stream location/size, etc).  */
3474   num_subspaces = som_count_subspaces (abfd);
3475   obj_som_file_hdr (abfd)->subspace_location = current_offset;
3476   obj_som_file_hdr (abfd)->subspace_total = num_subspaces;
3477   current_offset += num_subspaces * sizeof (struct subspace_dictionary_record);
3478
3479   /* Next is the string table for the space/subspace names.  We will
3480      build and write the string table on the fly.  At the same time
3481      we will fill in the space/subspace name index fields.  */
3482
3483   /* The string table needs to be aligned on a word boundary.  */
3484   if (current_offset % 4)
3485     current_offset += (4 - (current_offset % 4));
3486
3487   /* Mark the offset of the space/subspace string table in the
3488      file header.  */
3489   obj_som_file_hdr (abfd)->space_strings_location = current_offset;
3490
3491   /* Scribble out the space strings.  */
3492   if (! som_write_space_strings (abfd, current_offset, &strings_size))
3493     return FALSE;
3494
3495   /* Record total string table size in the header and update the
3496      current offset.  */
3497   obj_som_file_hdr (abfd)->space_strings_size = strings_size;
3498   current_offset += strings_size;
3499
3500   /* Next is the compilation unit.  */
3501   obj_som_file_hdr (abfd)->compiler_location = current_offset;
3502   obj_som_file_hdr (abfd)->compiler_total = 0;
3503   if (obj_som_compilation_unit (abfd))
3504     {
3505       obj_som_file_hdr (abfd)->compiler_total = 1;
3506       current_offset += COMPUNITSZ;
3507     }
3508
3509   /* Now compute the file positions for the loadable subspaces, taking
3510      care to make sure everything stays properly aligned.  */
3511
3512   section = abfd->sections;
3513   for (i = 0; i < num_spaces; i++)
3514     {
3515       asection *subsection;
3516       int first_subspace;
3517       unsigned int subspace_offset = 0;
3518
3519       /* Find a space.  */
3520       while (!som_is_space (section))
3521         section = section->next;
3522
3523       first_subspace = 1;
3524       /* Now look for all its subspaces.  */
3525       for (subsection = abfd->sections;
3526            subsection != NULL;
3527            subsection = subsection->next)
3528         {
3529
3530           if (!som_is_subspace (subsection)
3531               || !som_is_container (section, subsection)
3532               || (subsection->flags & SEC_ALLOC) == 0)
3533             continue;
3534
3535           /* If this is the first subspace in the space, and we are
3536              building an executable, then take care to make sure all
3537              the alignments are correct and update the exec header.  */
3538           if (first_subspace
3539               && (abfd->flags & (EXEC_P | DYNAMIC)))
3540             {
3541               /* Demand paged executables have each space aligned to a
3542                  page boundary.  Sharable executables (write-protected
3543                  text) have just the private (aka data & bss) space aligned
3544                  to a page boundary.  Ugh.  Not true for HPUX.
3545
3546                  The HPUX kernel requires the text to always be page aligned
3547                  within the file regardless of the executable's type.  */
3548               if (abfd->flags & (D_PAGED | DYNAMIC)
3549                   || (subsection->flags & SEC_CODE)
3550                   || ((abfd->flags & WP_TEXT)
3551                       && (subsection->flags & SEC_DATA)))
3552                 current_offset = SOM_ALIGN (current_offset, PA_PAGESIZE);
3553
3554               /* Update the exec header.  */
3555               if (subsection->flags & SEC_CODE && exec_header->exec_tfile == 0)
3556                 {
3557                   exec_header->exec_tmem = section->vma;
3558                   exec_header->exec_tfile = current_offset;
3559                 }
3560               if (subsection->flags & SEC_DATA && exec_header->exec_dfile == 0)
3561                 {
3562                   exec_header->exec_dmem = section->vma;
3563                   exec_header->exec_dfile = current_offset;
3564                 }
3565
3566               /* Keep track of exactly where we are within a particular
3567                  space.  This is necessary as the braindamaged HPUX
3568                  loader will create holes between subspaces *and*
3569                  subspace alignments are *NOT* preserved.  What a crock.  */
3570               subspace_offset = subsection->vma;
3571
3572               /* Only do this for the first subspace within each space.  */
3573               first_subspace = 0;
3574             }
3575           else if (abfd->flags & (EXEC_P | DYNAMIC))
3576             {
3577               /* The braindamaged HPUX loader may have created a hole
3578                  between two subspaces.  It is *not* sufficient to use
3579                  the alignment specifications within the subspaces to
3580                  account for these holes -- I've run into at least one
3581                  case where the loader left one code subspace unaligned
3582                  in a final executable.
3583
3584                  To combat this we keep a current offset within each space,
3585                  and use the subspace vma fields to detect and preserve
3586                  holes.  What a crock!
3587
3588                  ps.  This is not necessary for unloadable space/subspaces.  */
3589               current_offset += subsection->vma - subspace_offset;
3590               if (subsection->flags & SEC_CODE)
3591                 exec_header->exec_tsize += subsection->vma - subspace_offset;
3592               else
3593                 exec_header->exec_dsize += subsection->vma - subspace_offset;
3594               subspace_offset += subsection->vma - subspace_offset;
3595             }
3596
3597           subsection->target_index = total_subspaces++;
3598           /* This is real data to be loaded from the file.  */
3599           if (subsection->flags & SEC_LOAD)
3600             {
3601               /* Update the size of the code & data.  */
3602               if (abfd->flags & (EXEC_P | DYNAMIC)
3603                   && subsection->flags & SEC_CODE)
3604                 exec_header->exec_tsize += subsection->_cooked_size;
3605               else if (abfd->flags & (EXEC_P | DYNAMIC)
3606                        && subsection->flags & SEC_DATA)
3607                 exec_header->exec_dsize += subsection->_cooked_size;
3608               som_section_data (subsection)->subspace_dict->file_loc_init_value
3609                 = current_offset;
3610               subsection->filepos = current_offset;
3611               current_offset += bfd_section_size (abfd, subsection);
3612               subspace_offset += bfd_section_size (abfd, subsection);
3613             }
3614           /* Looks like uninitialized data.  */
3615           else
3616             {
3617               /* Update the size of the bss section.  */
3618               if (abfd->flags & (EXEC_P | DYNAMIC))
3619                 exec_header->exec_bsize += subsection->_cooked_size;
3620
3621               som_section_data (subsection)->subspace_dict->file_loc_init_value
3622                 = 0;
3623               som_section_data (subsection)->subspace_dict->
3624                 initialization_length = 0;
3625             }
3626         }
3627       /* Goto the next section.  */
3628       section = section->next;
3629     }
3630
3631   /* Finally compute the file positions for unloadable subspaces.
3632      If building an executable, start the unloadable stuff on its
3633      own page.  */
3634
3635   if (abfd->flags & (EXEC_P | DYNAMIC))
3636     current_offset = SOM_ALIGN (current_offset, PA_PAGESIZE);
3637
3638   obj_som_file_hdr (abfd)->unloadable_sp_location = current_offset;
3639   section = abfd->sections;
3640   for (i = 0; i < num_spaces; i++)
3641     {
3642       asection *subsection;
3643
3644       /* Find a space.  */
3645       while (!som_is_space (section))
3646         section = section->next;
3647
3648       if (abfd->flags & (EXEC_P | DYNAMIC))
3649         current_offset = SOM_ALIGN (current_offset, PA_PAGESIZE);
3650
3651       /* Now look for all its subspaces.  */
3652       for (subsection = abfd->sections;
3653            subsection != NULL;
3654            subsection = subsection->next)
3655         {
3656
3657           if (!som_is_subspace (subsection)
3658               || !som_is_container (section, subsection)
3659               || (subsection->flags & SEC_ALLOC) != 0)
3660             continue;
3661
3662           subsection->target_index = total_subspaces++;
3663           /* This is real data to be loaded from the file.  */
3664           if ((subsection->flags & SEC_LOAD) == 0)
3665             {
3666               som_section_data (subsection)->subspace_dict->file_loc_init_value
3667                 = current_offset;
3668               subsection->filepos = current_offset;
3669               current_offset += bfd_section_size (abfd, subsection);
3670             }
3671           /* Looks like uninitialized data.  */
3672           else
3673             {
3674               som_section_data (subsection)->subspace_dict->file_loc_init_value
3675                 = 0;
3676               som_section_data (subsection)->subspace_dict->
3677                 initialization_length = bfd_section_size (abfd, subsection);
3678             }
3679         }
3680       /* Goto the next section.  */
3681       section = section->next;
3682     }
3683
3684   /* If building an executable, then make sure to seek to and write
3685      one byte at the end of the file to make sure any necessary
3686      zeros are filled in.  Ugh.  */
3687   if (abfd->flags & (EXEC_P | DYNAMIC))
3688     current_offset = SOM_ALIGN (current_offset, PA_PAGESIZE);
3689   if (bfd_seek (abfd, (file_ptr) current_offset - 1, SEEK_SET) != 0)
3690     return FALSE;
3691   if (bfd_bwrite ((PTR) "", (bfd_size_type) 1, abfd) != 1)
3692     return FALSE;
3693
3694   obj_som_file_hdr (abfd)->unloadable_sp_size
3695     = current_offset - obj_som_file_hdr (abfd)->unloadable_sp_location;
3696
3697   /* Loader fixups are not supported in any way shape or form.  */
3698   obj_som_file_hdr (abfd)->loader_fixup_location = 0;
3699   obj_som_file_hdr (abfd)->loader_fixup_total = 0;
3700
3701   /* Done.  Store the total size of the SOM so far.  */
3702   obj_som_file_hdr (abfd)->som_length = current_offset;
3703
3704   return TRUE;
3705 }
3706
3707 /* Finally, scribble out the various headers to the disk.  */
3708
3709 static bfd_boolean
3710 som_finish_writing (abfd)
3711      bfd *abfd;
3712 {
3713   int num_spaces = som_count_spaces (abfd);
3714   asymbol **syms = bfd_get_outsymbols (abfd);
3715   int i, num_syms, strings_size;
3716   int subspace_index = 0;
3717   file_ptr location;
3718   asection *section;
3719   unsigned long current_offset;
3720   unsigned int total_reloc_size;
3721   bfd_size_type amt;
3722
3723   /* We must set up the version identifier here as objcopy/strip copy
3724      private BFD data too late for us to handle this in som_begin_writing.  */
3725   if (obj_som_exec_data (abfd)
3726       && obj_som_exec_data (abfd)->version_id)
3727     obj_som_file_hdr (abfd)->version_id = obj_som_exec_data (abfd)->version_id;
3728   else
3729     obj_som_file_hdr (abfd)->version_id = NEW_VERSION_ID;
3730
3731   /* Next is the symbol table.  These are fixed length records.
3732
3733      Count the number of symbols to determine how much room is needed
3734      in the object file for the symbol table.
3735
3736      The names of the symbols are stored in a separate string table,
3737      and the index for each symbol name into the string table is computed
3738      below.  Therefore, it is not possible to write the symbol table
3739      at this time.
3740
3741      These used to be output before the subspace contents, but they
3742      were moved here to work around a stupid bug in the hpux linker
3743      (fixed in hpux10).  */
3744   current_offset = obj_som_file_hdr (abfd)->som_length;
3745
3746   /* Make sure we're on a word boundary.  */
3747   if (current_offset % 4)
3748     current_offset += (4 - (current_offset % 4));
3749
3750   num_syms = bfd_get_symcount (abfd);
3751   obj_som_file_hdr (abfd)->symbol_location = current_offset;
3752   obj_som_file_hdr (abfd)->symbol_total = num_syms;
3753   current_offset += num_syms * sizeof (struct symbol_dictionary_record);
3754
3755   /* Next are the symbol strings.
3756      Align them to a word boundary.  */
3757   if (current_offset % 4)
3758     current_offset += (4 - (current_offset % 4));
3759   obj_som_file_hdr (abfd)->symbol_strings_location = current_offset;
3760
3761   /* Scribble out the symbol strings.  */
3762   if (! som_write_symbol_strings (abfd, current_offset, syms,
3763                                   num_syms, &strings_size,
3764                                   obj_som_compilation_unit (abfd)))
3765     return FALSE;
3766
3767   /* Record total string table size in header and update the
3768      current offset.  */
3769   obj_som_file_hdr (abfd)->symbol_strings_size = strings_size;
3770   current_offset += strings_size;
3771
3772   /* Do prep work before handling fixups.  */
3773   som_prep_for_fixups (abfd,
3774                        bfd_get_outsymbols (abfd),
3775                        bfd_get_symcount (abfd));
3776
3777   /* At the end of the file is the fixup stream which starts on a
3778      word boundary.  */
3779   if (current_offset % 4)
3780     current_offset += (4 - (current_offset % 4));
3781   obj_som_file_hdr (abfd)->fixup_request_location = current_offset;
3782
3783   /* Write the fixups and update fields in subspace headers which
3784      relate to the fixup stream.  */
3785   if (! som_write_fixups (abfd, current_offset, &total_reloc_size))
3786     return FALSE;
3787
3788   /* Record the total size of the fixup stream in the file header.  */
3789   obj_som_file_hdr (abfd)->fixup_request_total = total_reloc_size;
3790
3791   /* Done.  Store the total size of the SOM.  */
3792   obj_som_file_hdr (abfd)->som_length = current_offset + total_reloc_size;
3793
3794   /* Now that the symbol table information is complete, build and
3795      write the symbol table.  */
3796   if (! som_build_and_write_symbol_table (abfd))
3797     return FALSE;
3798
3799   /* Subspaces are written first so that we can set up information
3800      about them in their containing spaces as the subspace is written.  */
3801
3802   /* Seek to the start of the subspace dictionary records.  */
3803   location = obj_som_file_hdr (abfd)->subspace_location;
3804   if (bfd_seek (abfd, location, SEEK_SET) != 0)
3805     return FALSE;
3806
3807   section = abfd->sections;
3808   /* Now for each loadable space write out records for its subspaces.  */
3809   for (i = 0; i < num_spaces; i++)
3810     {
3811       asection *subsection;
3812
3813       /* Find a space.  */
3814       while (!som_is_space (section))
3815         section = section->next;
3816
3817       /* Now look for all its subspaces.  */
3818       for (subsection = abfd->sections;
3819            subsection != NULL;
3820            subsection = subsection->next)
3821         {
3822
3823           /* Skip any section which does not correspond to a space
3824              or subspace.  Or does not have SEC_ALLOC set (and therefore
3825              has no real bits on the disk).  */
3826           if (!som_is_subspace (subsection)
3827               || !som_is_container (section, subsection)
3828               || (subsection->flags & SEC_ALLOC) == 0)
3829             continue;
3830
3831           /* If this is the first subspace for this space, then save
3832              the index of the subspace in its containing space.  Also
3833              set "is_loadable" in the containing space.  */
3834
3835           if (som_section_data (section)->space_dict->subspace_quantity == 0)
3836             {
3837               som_section_data (section)->space_dict->is_loadable = 1;
3838               som_section_data (section)->space_dict->subspace_index
3839                 = subspace_index;
3840             }
3841
3842           /* Increment the number of subspaces seen and the number of
3843              subspaces contained within the current space.  */
3844           subspace_index++;
3845           som_section_data (section)->space_dict->subspace_quantity++;
3846
3847           /* Mark the index of the current space within the subspace's
3848              dictionary record.  */
3849           som_section_data (subsection)->subspace_dict->space_index = i;
3850
3851           /* Dump the current subspace header.  */
3852           amt = sizeof (struct subspace_dictionary_record);
3853           if (bfd_bwrite ((PTR) som_section_data (subsection)->subspace_dict,
3854                          amt, abfd) != amt)
3855             return FALSE;
3856         }
3857       /* Goto the next section.  */
3858       section = section->next;
3859     }
3860
3861   /* Now repeat the process for unloadable subspaces.  */
3862   section = abfd->sections;
3863   /* Now for each space write out records for its subspaces.  */
3864   for (i = 0; i < num_spaces; i++)
3865     {
3866       asection *subsection;
3867
3868       /* Find a space.  */
3869       while (!som_is_space (section))
3870         section = section->next;
3871
3872       /* Now look for all its subspaces.  */
3873       for (subsection = abfd->sections;
3874            subsection != NULL;
3875            subsection = subsection->next)
3876         {
3877
3878           /* Skip any section which does not correspond to a space or
3879              subspace, or which SEC_ALLOC set (and therefore handled
3880              in the loadable spaces/subspaces code above).  */
3881
3882           if (!som_is_subspace (subsection)
3883               || !som_is_container (section, subsection)
3884               || (subsection->flags & SEC_ALLOC) != 0)
3885             continue;
3886
3887           /* If this is the first subspace for this space, then save
3888              the index of the subspace in its containing space.  Clear
3889              "is_loadable".  */
3890
3891           if (som_section_data (section)->space_dict->subspace_quantity == 0)
3892             {
3893               som_section_data (section)->space_dict->is_loadable = 0;
3894               som_section_data (section)->space_dict->subspace_index
3895                 = subspace_index;
3896             }
3897
3898           /* Increment the number of subspaces seen and the number of
3899              subspaces contained within the current space.  */
3900           som_section_data (section)->space_dict->subspace_quantity++;
3901           subspace_index++;
3902
3903           /* Mark the index of the current space within the subspace's
3904              dictionary record.  */
3905           som_section_data (subsection)->subspace_dict->space_index = i;
3906
3907           /* Dump this subspace header.  */
3908           amt = sizeof (struct subspace_dictionary_record);
3909           if (bfd_bwrite ((PTR) som_section_data (subsection)->subspace_dict,
3910                          amt, abfd) != amt)
3911             return FALSE;
3912         }
3913       /* Goto the next section.  */
3914       section = section->next;
3915     }
3916
3917   /* All the subspace dictionary records are written, and all the
3918      fields are set up in the space dictionary records.
3919
3920      Seek to the right location and start writing the space
3921      dictionary records.  */
3922   location = obj_som_file_hdr (abfd)->space_location;
3923   if (bfd_seek (abfd, location, SEEK_SET) != 0)
3924     return FALSE;
3925
3926   section = abfd->sections;
3927   for (i = 0; i < num_spaces; i++)
3928     {
3929       /* Find a space.  */
3930       while (!som_is_space (section))
3931         section = section->next;
3932
3933       /* Dump its header.  */
3934       amt = sizeof (struct space_dictionary_record);
3935       if (bfd_bwrite ((PTR) som_section_data (section)->space_dict,
3936                      amt, abfd) != amt)
3937         return FALSE;
3938
3939       /* Goto the next section.  */
3940       section = section->next;
3941     }
3942
3943   /* Write the compilation unit record if there is one.  */
3944   if (obj_som_compilation_unit (abfd))
3945     {
3946       location = obj_som_file_hdr (abfd)->compiler_location;
3947       if (bfd_seek (abfd, location, SEEK_SET) != 0)
3948         return FALSE;
3949
3950       amt = COMPUNITSZ;
3951       if (bfd_bwrite ((PTR) obj_som_compilation_unit (abfd), amt, abfd) != amt)
3952         return FALSE;
3953     }
3954
3955   /* Setting of the system_id has to happen very late now that copying of
3956      BFD private data happens *after* section contents are set.  */
3957   if (abfd->flags & (EXEC_P | DYNAMIC))
3958     obj_som_file_hdr (abfd)->system_id = obj_som_exec_data (abfd)->system_id;
3959   else if (bfd_get_mach (abfd) == pa20)
3960     obj_som_file_hdr (abfd)->system_id = CPU_PA_RISC2_0;
3961   else if (bfd_get_mach (abfd) == pa11)
3962     obj_som_file_hdr (abfd)->system_id = CPU_PA_RISC1_1;
3963   else
3964     obj_som_file_hdr (abfd)->system_id = CPU_PA_RISC1_0;
3965
3966   /* Compute the checksum for the file header just before writing
3967      the header to disk.  */
3968   obj_som_file_hdr (abfd)->checksum = som_compute_checksum (abfd);
3969
3970   /* Only thing left to do is write out the file header.  It is always
3971      at location zero.  Seek there and write it.  */
3972   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3973     return FALSE;
3974   amt = sizeof (struct header);
3975   if (bfd_bwrite ((PTR) obj_som_file_hdr (abfd), amt, abfd) != amt)
3976     return FALSE;
3977
3978   /* Now write the exec header.  */
3979   if (abfd->flags & (EXEC_P | DYNAMIC))
3980     {
3981       long tmp, som_length;
3982       struct som_exec_auxhdr *exec_header;
3983
3984       exec_header = obj_som_exec_hdr (abfd);
3985       exec_header->exec_entry = bfd_get_start_address (abfd);
3986       exec_header->exec_flags = obj_som_exec_data (abfd)->exec_flags;
3987
3988       /* Oh joys.  Ram some of the BSS data into the DATA section
3989          to be compatible with how the hp linker makes objects
3990          (saves memory space).  */
3991       tmp = exec_header->exec_dsize;
3992       tmp = SOM_ALIGN (tmp, PA_PAGESIZE);
3993       exec_header->exec_bsize -= (tmp - exec_header->exec_dsize);
3994       if (exec_header->exec_bsize < 0)
3995         exec_header->exec_bsize = 0;
3996       exec_header->exec_dsize = tmp;
3997
3998       /* Now perform some sanity checks.  The idea is to catch bogons now and
3999          inform the user, instead of silently generating a bogus file.  */
4000       som_length = obj_som_file_hdr (abfd)->som_length;
4001       if (exec_header->exec_tfile + exec_header->exec_tsize > som_length
4002           || exec_header->exec_dfile + exec_header->exec_dsize > som_length)
4003         {
4004           bfd_set_error (bfd_error_bad_value);
4005           return FALSE;
4006         }
4007
4008       if (bfd_seek (abfd, obj_som_file_hdr (abfd)->aux_header_location,
4009                     SEEK_SET) != 0)
4010         return FALSE;
4011
4012       amt = AUX_HDR_SIZE;
4013       if (bfd_bwrite ((PTR) exec_header, amt, abfd) != amt)
4014         return FALSE;
4015     }
4016   return TRUE;
4017 }
4018
4019 /* Compute and return the checksum for a SOM file header.  */
4020
4021 static unsigned long
4022 som_compute_checksum (abfd)
4023      bfd *abfd;
4024 {
4025   unsigned long checksum, count, i;
4026   unsigned long *buffer = (unsigned long *) obj_som_file_hdr (abfd);
4027
4028   checksum = 0;
4029   count = sizeof (struct header) / sizeof (unsigned long);
4030   for (i = 0; i < count; i++)
4031     checksum ^= *(buffer + i);
4032
4033   return checksum;
4034 }
4035
4036 static void
4037 som_bfd_derive_misc_symbol_info (abfd, sym, info)
4038      bfd *abfd ATTRIBUTE_UNUSED;
4039      asymbol *sym;
4040      struct som_misc_symbol_info *info;
4041 {
4042   /* Initialize.  */
4043   memset (info, 0, sizeof (struct som_misc_symbol_info));
4044
4045   /* The HP SOM linker requires detailed type information about
4046      all symbols (including undefined symbols!).  Unfortunately,
4047      the type specified in an import/export statement does not
4048      always match what the linker wants.  Severe braindamage.  */
4049
4050   /* Section symbols will not have a SOM symbol type assigned to
4051      them yet.  Assign all section symbols type ST_DATA.  */
4052   if (sym->flags & BSF_SECTION_SYM)
4053     info->symbol_type = ST_DATA;
4054   else
4055     {
4056       /* Common symbols must have scope SS_UNSAT and type
4057          ST_STORAGE or the linker will choke.  */
4058       if (bfd_is_com_section (sym->section))
4059         {
4060           info->symbol_scope = SS_UNSAT;
4061           info->symbol_type = ST_STORAGE;
4062         }
4063
4064       /* It is possible to have a symbol without an associated
4065          type.  This happens if the user imported the symbol
4066          without a type and the symbol was never defined
4067          locally.  If BSF_FUNCTION is set for this symbol, then
4068          assign it type ST_CODE (the HP linker requires undefined
4069          external functions to have type ST_CODE rather than ST_ENTRY).  */
4070       else if ((som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN
4071                 || som_symbol_data (sym)->som_type == SYMBOL_TYPE_CODE)
4072                && bfd_is_und_section (sym->section)
4073                && sym->flags & BSF_FUNCTION)
4074         info->symbol_type = ST_CODE;
4075
4076       /* Handle function symbols which were defined in this file.
4077          They should have type ST_ENTRY.  Also retrieve the argument
4078          relocation bits from the SOM backend information.  */
4079       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_ENTRY
4080                || (som_symbol_data (sym)->som_type == SYMBOL_TYPE_CODE
4081                    && (sym->flags & BSF_FUNCTION))
4082                || (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN
4083                    && (sym->flags & BSF_FUNCTION)))
4084         {
4085           info->symbol_type = ST_ENTRY;
4086           info->arg_reloc = som_symbol_data (sym)->tc_data.ap.hppa_arg_reloc;
4087           info->priv_level= som_symbol_data (sym)->tc_data.ap.hppa_priv_level;
4088         }
4089
4090       /* For unknown symbols set the symbol's type based on the symbol's
4091          section (ST_DATA for DATA sections, ST_CODE for CODE sections).  */
4092       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN)
4093         {
4094           if (sym->section->flags & SEC_CODE)
4095             info->symbol_type = ST_CODE;
4096           else
4097             info->symbol_type = ST_DATA;
4098         }
4099
4100       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_UNKNOWN)
4101         info->symbol_type = ST_DATA;
4102
4103       /* From now on it's a very simple mapping.  */
4104       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_ABSOLUTE)
4105         info->symbol_type = ST_ABSOLUTE;
4106       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_CODE)
4107         info->symbol_type = ST_CODE;
4108       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_DATA)
4109         info->symbol_type = ST_DATA;
4110       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_MILLICODE)
4111         info->symbol_type = ST_MILLICODE;
4112       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_PLABEL)
4113         info->symbol_type = ST_PLABEL;
4114       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_PRI_PROG)
4115         info->symbol_type = ST_PRI_PROG;
4116       else if (som_symbol_data (sym)->som_type == SYMBOL_TYPE_SEC_PROG)
4117         info->symbol_type = ST_SEC_PROG;
4118     }
4119
4120   /* Now handle the symbol's scope.  Exported data which is not
4121      in the common section has scope SS_UNIVERSAL.  Note scope
4122      of common symbols was handled earlier!  */
4123   if (bfd_is_und_section (sym->section))
4124     info->symbol_scope = SS_UNSAT;
4125   else if (sym->flags & (BSF_EXPORT | BSF_WEAK)
4126            && ! bfd_is_com_section (sym->section))
4127     info->symbol_scope = SS_UNIVERSAL;
4128   /* Anything else which is not in the common section has scope
4129      SS_LOCAL.  */
4130   else if (! bfd_is_com_section (sym->section))
4131     info->symbol_scope = SS_LOCAL;
4132
4133   /* Now set the symbol_info field.  It has no real meaning
4134      for undefined or common symbols, but the HP linker will
4135      choke if it's not set to some "reasonable" value.  We
4136      use zero as a reasonable value.  */
4137   if (bfd_is_com_section (sym->section)
4138       || bfd_is_und_section (sym->section)
4139       || bfd_is_abs_section (sym->section))
4140     info->symbol_info = 0;
4141   /* For all other symbols, the symbol_info field contains the
4142      subspace index of the space this symbol is contained in.  */
4143   else
4144     info->symbol_info = sym->section->target_index;
4145
4146   /* Set the symbol's value.  */
4147   info->symbol_value = sym->value + sym->section->vma;
4148
4149   /* The secondary_def field is for weak symbols.  */
4150   if (sym->flags & BSF_WEAK)
4151     info->secondary_def = TRUE;
4152   else
4153     info->secondary_def = FALSE;
4154
4155 }
4156
4157 /* Build and write, in one big chunk, the entire symbol table for
4158    this BFD.  */
4159
4160 static bfd_boolean
4161 som_build_and_write_symbol_table (abfd)
4162      bfd *abfd;
4163 {
4164   unsigned int num_syms = bfd_get_symcount (abfd);
4165   file_ptr symtab_location = obj_som_file_hdr (abfd)->symbol_location;
4166   asymbol **bfd_syms = obj_som_sorted_syms (abfd);
4167   struct symbol_dictionary_record *som_symtab = NULL;
4168   unsigned int i;
4169   bfd_size_type symtab_size;
4170
4171   /* Compute total symbol table size and allocate a chunk of memory
4172      to hold the symbol table as we build it.  */
4173   symtab_size = num_syms;
4174   symtab_size *= sizeof (struct symbol_dictionary_record);
4175   som_symtab = (struct symbol_dictionary_record *) bfd_zmalloc (symtab_size);
4176   if (som_symtab == NULL && symtab_size != 0)
4177     goto error_return;
4178
4179   /* Walk over each symbol.  */
4180   for (i = 0; i < num_syms; i++)
4181     {
4182       struct som_misc_symbol_info info;
4183
4184       /* This is really an index into the symbol strings table.
4185          By the time we get here, the index has already been
4186          computed and stored into the name field in the BFD symbol.  */
4187       som_symtab[i].name.n_strx = som_symbol_data(bfd_syms[i])->stringtab_offset;
4188
4189       /* Derive SOM information from the BFD symbol.  */
4190       som_bfd_derive_misc_symbol_info (abfd, bfd_syms[i], &info);
4191
4192       /* Now use it.  */
4193       som_symtab[i].symbol_type = info.symbol_type;
4194       som_symtab[i].symbol_scope = info.symbol_scope;
4195       som_symtab[i].arg_reloc = info.arg_reloc;
4196       som_symtab[i].symbol_info = info.symbol_info;
4197       som_symtab[i].xleast = 3;
4198       som_symtab[i].symbol_value = info.symbol_value | info.priv_level;
4199       som_symtab[i].secondary_def = info.secondary_def;
4200     }
4201
4202   /* Everything is ready, seek to the right location and
4203      scribble out the symbol table.  */
4204   if (bfd_seek (abfd, symtab_location, SEEK_SET) != 0)
4205     return FALSE;
4206
4207   if (bfd_bwrite ((PTR) som_symtab, symtab_size, abfd) != symtab_size)
4208     goto error_return;
4209
4210   if (som_symtab != NULL)
4211     free (som_symtab);
4212   return TRUE;
4213  error_return:
4214   if (som_symtab != NULL)
4215     free (som_symtab);
4216   return FALSE;
4217 }
4218
4219 /* Write an object in SOM format.  */
4220
4221 static bfd_boolean
4222 som_write_object_contents (abfd)
4223      bfd *abfd;
4224 {
4225   if (! abfd->output_has_begun)
4226     {
4227       /* Set up fixed parts of the file, space, and subspace headers.
4228          Notify the world that output has begun.  */
4229       som_prep_headers (abfd);
4230       abfd->output_has_begun = TRUE;
4231       /* Start writing the object file.  This include all the string
4232          tables, fixup streams, and other portions of the object file.  */
4233       som_begin_writing (abfd);
4234     }
4235
4236   return (som_finish_writing (abfd));
4237 }
4238 \f
4239 /* Read and save the string table associated with the given BFD.  */
4240
4241 static bfd_boolean
4242 som_slurp_string_table (abfd)
4243      bfd *abfd;
4244 {
4245   char *stringtab;
4246   bfd_size_type amt;
4247
4248   /* Use the saved version if its available.  */
4249   if (obj_som_stringtab (abfd) != NULL)
4250     return TRUE;
4251
4252   /* I don't think this can currently happen, and I'm not sure it should
4253      really be an error, but it's better than getting unpredictable results
4254      from the host's malloc when passed a size of zero.  */
4255   if (obj_som_stringtab_size (abfd) == 0)
4256     {
4257       bfd_set_error (bfd_error_no_symbols);
4258       return FALSE;
4259     }
4260
4261   /* Allocate and read in the string table.  */
4262   amt = obj_som_stringtab_size (abfd);
4263   stringtab = bfd_zmalloc (amt);
4264   if (stringtab == NULL)
4265     return FALSE;
4266
4267   if (bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET) != 0)
4268     return FALSE;
4269
4270   if (bfd_bread (stringtab, amt, abfd) != amt)
4271     return FALSE;
4272
4273   /* Save our results and return success.  */
4274   obj_som_stringtab (abfd) = stringtab;
4275   return TRUE;
4276 }
4277
4278 /* Return the amount of data (in bytes) required to hold the symbol
4279    table for this object.  */
4280
4281 static long
4282 som_get_symtab_upper_bound (abfd)
4283      bfd *abfd;
4284 {
4285   if (!som_slurp_symbol_table (abfd))
4286     return -1;
4287
4288   return (bfd_get_symcount (abfd) + 1) * (sizeof (asymbol *));
4289 }
4290
4291 /* Convert from a SOM subspace index to a BFD section.  */
4292
4293 static asection *
4294 bfd_section_from_som_symbol (abfd, symbol)
4295      bfd *abfd;
4296      struct symbol_dictionary_record *symbol;
4297 {
4298   asection *section;
4299
4300   /* The meaning of the symbol_info field changes for functions
4301      within executables.  So only use the quick symbol_info mapping for
4302      incomplete objects and non-function symbols in executables.  */
4303   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4304       || (symbol->symbol_type != ST_ENTRY
4305           && symbol->symbol_type != ST_PRI_PROG
4306           && symbol->symbol_type != ST_SEC_PROG
4307           && symbol->symbol_type != ST_MILLICODE))
4308     {
4309       int index = symbol->symbol_info;
4310       for (section = abfd->sections; section != NULL; section = section->next)
4311         if (section->target_index == index && som_is_subspace (section))
4312           return section;
4313
4314       /* Could be a symbol from an external library (such as an OMOS
4315          shared library).  Don't abort.  */
4316       return bfd_abs_section_ptr;
4317
4318     }
4319   else
4320     {
4321       unsigned int value = symbol->symbol_value;
4322
4323       /* For executables we will have to use the symbol's address and
4324          find out what section would contain that address.   Yuk.  */
4325       for (section = abfd->sections; section; section = section->next)
4326         {
4327           if (value >= section->vma
4328               && value <= section->vma + section->_cooked_size
4329               && som_is_subspace (section))
4330             return section;
4331         }
4332
4333       /* Could be a symbol from an external library (such as an OMOS
4334          shared library).  Don't abort.  */
4335       return bfd_abs_section_ptr;
4336
4337     }
4338 }
4339
4340 /* Read and save the symbol table associated with the given BFD.  */
4341
4342 static unsigned int
4343 som_slurp_symbol_table (abfd)
4344      bfd *abfd;
4345 {
4346   int symbol_count = bfd_get_symcount (abfd);
4347   int symsize = sizeof (struct symbol_dictionary_record);
4348   char *stringtab;
4349   struct symbol_dictionary_record *buf = NULL, *bufp, *endbufp;
4350   som_symbol_type *sym, *symbase;
4351   bfd_size_type amt;
4352
4353   /* Return saved value if it exists.  */
4354   if (obj_som_symtab (abfd) != NULL)
4355     goto successful_return;
4356
4357   /* Special case.  This is *not* an error.  */
4358   if (symbol_count == 0)
4359     goto successful_return;
4360
4361   if (!som_slurp_string_table (abfd))
4362     goto error_return;
4363
4364   stringtab = obj_som_stringtab (abfd);
4365
4366   amt = symbol_count;
4367   amt *= sizeof (som_symbol_type);
4368   symbase = (som_symbol_type *) bfd_zmalloc (amt);
4369   if (symbase == NULL)
4370     goto error_return;
4371
4372   /* Read in the external SOM representation.  */
4373   amt = symbol_count;
4374   amt *= symsize;
4375   buf = bfd_malloc (amt);
4376   if (buf == NULL && amt != 0)
4377     goto error_return;
4378   if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) != 0)
4379     goto error_return;
4380   if (bfd_bread (buf, amt, abfd) != amt)
4381     goto error_return;
4382
4383   /* Iterate over all the symbols and internalize them.  */
4384   endbufp = buf + symbol_count;
4385   for (bufp = buf, sym = symbase; bufp < endbufp; ++bufp)
4386     {
4387
4388       /* I don't think we care about these.  */
4389       if (bufp->symbol_type == ST_SYM_EXT
4390           || bufp->symbol_type == ST_ARG_EXT)
4391         continue;
4392
4393       /* Set some private data we care about.  */
4394       if (bufp->symbol_type == ST_NULL)
4395         som_symbol_data (sym)->som_type = SYMBOL_TYPE_UNKNOWN;
4396       else if (bufp->symbol_type == ST_ABSOLUTE)
4397         som_symbol_data (sym)->som_type = SYMBOL_TYPE_ABSOLUTE;
4398       else if (bufp->symbol_type == ST_DATA)
4399         som_symbol_data (sym)->som_type = SYMBOL_TYPE_DATA;
4400       else if (bufp->symbol_type == ST_CODE)
4401         som_symbol_data (sym)->som_type = SYMBOL_TYPE_CODE;
4402       else if (bufp->symbol_type == ST_PRI_PROG)
4403         som_symbol_data (sym)->som_type = SYMBOL_TYPE_PRI_PROG;
4404       else if (bufp->symbol_type == ST_SEC_PROG)
4405         som_symbol_data (sym)->som_type = SYMBOL_TYPE_SEC_PROG;
4406       else if (bufp->symbol_type == ST_ENTRY)
4407         som_symbol_data (sym)->som_type = SYMBOL_TYPE_ENTRY;
4408       else if (bufp->symbol_type == ST_MILLICODE)
4409         som_symbol_data (sym)->som_type = SYMBOL_TYPE_MILLICODE;
4410       else if (bufp->symbol_type == ST_PLABEL)
4411         som_symbol_data (sym)->som_type = SYMBOL_TYPE_PLABEL;
4412       else
4413         som_symbol_data (sym)->som_type = SYMBOL_TYPE_UNKNOWN;
4414       som_symbol_data (sym)->tc_data.ap.hppa_arg_reloc = bufp->arg_reloc;
4415
4416       /* Some reasonable defaults.  */
4417       sym->symbol.the_bfd = abfd;
4418       sym->symbol.name = bufp->name.n_strx + stringtab;
4419       sym->symbol.value = bufp->symbol_value;
4420       sym->symbol.section = 0;
4421       sym->symbol.flags = 0;
4422
4423       switch (bufp->symbol_type)
4424         {
4425         case ST_ENTRY:
4426         case ST_MILLICODE:
4427           sym->symbol.flags |= BSF_FUNCTION;
4428           som_symbol_data (sym)->tc_data.ap.hppa_priv_level =
4429             sym->symbol.value & 0x3;
4430           sym->symbol.value &= ~0x3;
4431           break;
4432
4433         case ST_STUB:
4434         case ST_CODE:
4435         case ST_PRI_PROG:
4436         case ST_SEC_PROG:
4437           som_symbol_data (sym)->tc_data.ap.hppa_priv_level =
4438             sym->symbol.value & 0x3;
4439           sym->symbol.value &= ~0x3;
4440           /* If the symbol's scope is SS_UNSAT, then these are
4441              undefined function symbols.  */
4442           if (bufp->symbol_scope == SS_UNSAT)
4443             sym->symbol.flags |= BSF_FUNCTION;
4444
4445         default:
4446           break;
4447         }
4448
4449       /* Handle scoping and section information.  */
4450       switch (bufp->symbol_scope)
4451         {
4452         /* symbol_info field is undefined for SS_EXTERNAL and SS_UNSAT symbols,
4453            so the section associated with this symbol can't be known.  */
4454         case SS_EXTERNAL:
4455           if (bufp->symbol_type != ST_STORAGE)
4456             sym->symbol.section = bfd_und_section_ptr;
4457           else
4458             sym->symbol.section = bfd_com_section_ptr;
4459           sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
4460           break;
4461
4462         case SS_UNSAT:
4463           if (bufp->symbol_type != ST_STORAGE)
4464             sym->symbol.section = bfd_und_section_ptr;
4465           else
4466             sym->symbol.section = bfd_com_section_ptr;
4467           break;
4468
4469         case SS_UNIVERSAL:
4470           sym->symbol.flags |= (BSF_EXPORT | BSF_GLOBAL);
4471           sym->symbol.section = bfd_section_from_som_symbol (abfd, bufp);
4472           sym->symbol.value -= sym->symbol.section->vma;
4473           break;
4474
4475 #if 0
4476         /* SS_GLOBAL and SS_LOCAL are two names for the same thing.
4477            Sound dumb?  It is.  */
4478         case SS_GLOBAL:
4479 #endif
4480         case SS_LOCAL:
4481           sym->symbol.flags |= BSF_LOCAL;
4482           sym->symbol.section = bfd_section_from_som_symbol (abfd, bufp);
4483           sym->symbol.value -= sym->symbol.section->vma;
4484           break;
4485         }
4486
4487       /* Check for a weak symbol.  */
4488       if (bufp->secondary_def)
4489         sym->symbol.flags |= BSF_WEAK;
4490
4491       /* Mark section symbols and symbols used by the debugger.
4492          Note $START$ is a magic code symbol, NOT a section symbol.  */
4493       if (sym->symbol.name[0] == '$'
4494           && sym->symbol.name[strlen (sym->symbol.name) - 1] == '$'
4495           && !strcmp (sym->symbol.name, sym->symbol.section->name))
4496         sym->symbol.flags |= BSF_SECTION_SYM;
4497       else if (!strncmp (sym->symbol.name, "L$0\002", 4))
4498         {
4499           sym->symbol.flags |= BSF_SECTION_SYM;
4500           sym->symbol.name = sym->symbol.section->name;
4501         }
4502       else if (!strncmp (sym->symbol.name, "L$0\001", 4))
4503         sym->symbol.flags |= BSF_DEBUGGING;
4504
4505       /* Note increment at bottom of loop, since we skip some symbols
4506          we can not include it as part of the for statement.  */
4507       sym++;
4508     }
4509
4510   /* We modify the symbol count to record the number of BFD symbols we
4511      created.  */
4512   bfd_get_symcount (abfd) = sym - symbase;
4513
4514   /* Save our results and return success.  */
4515   obj_som_symtab (abfd) = symbase;
4516  successful_return:
4517   if (buf != NULL)
4518     free (buf);
4519   return (TRUE);
4520
4521  error_return:
4522   if (buf != NULL)
4523     free (buf);
4524   return FALSE;
4525 }
4526
4527 /* Canonicalize a SOM symbol table.  Return the number of entries
4528    in the symbol table.  */
4529
4530 static long
4531 som_canonicalize_symtab (abfd, location)
4532      bfd *abfd;
4533      asymbol **location;
4534 {
4535   int i;
4536   som_symbol_type *symbase;
4537
4538   if (!som_slurp_symbol_table (abfd))
4539     return -1;
4540
4541   i = bfd_get_symcount (abfd);
4542   symbase = obj_som_symtab (abfd);
4543
4544   for (; i > 0; i--, location++, symbase++)
4545     *location = &symbase->symbol;
4546
4547   /* Final null pointer.  */
4548   *location = 0;
4549   return (bfd_get_symcount (abfd));
4550 }
4551
4552 /* Make a SOM symbol.  There is nothing special to do here.  */
4553
4554 static asymbol *
4555 som_make_empty_symbol (abfd)
4556      bfd *abfd;
4557 {
4558   bfd_size_type amt = sizeof (som_symbol_type);
4559   som_symbol_type *new = (som_symbol_type *) bfd_zalloc (abfd, amt);
4560   if (new == NULL)
4561     return 0;
4562   new->symbol.the_bfd = abfd;
4563
4564   return &new->symbol;
4565 }
4566
4567 /* Print symbol information.  */
4568
4569 static void
4570 som_print_symbol (abfd, afile, symbol, how)
4571      bfd *abfd;
4572      PTR afile;
4573      asymbol *symbol;
4574      bfd_print_symbol_type how;
4575 {
4576   FILE *file = (FILE *) afile;
4577   switch (how)
4578     {
4579     case bfd_print_symbol_name:
4580       fprintf (file, "%s", symbol->name);
4581       break;
4582     case bfd_print_symbol_more:
4583       fprintf (file, "som ");
4584       fprintf_vma (file, symbol->value);
4585       fprintf (file, " %lx", (long) symbol->flags);
4586       break;
4587     case bfd_print_symbol_all:
4588       {
4589         const char *section_name;
4590         section_name = symbol->section ? symbol->section->name : "(*none*)";
4591         bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
4592         fprintf (file, " %s\t%s", section_name, symbol->name);
4593         break;
4594       }
4595     }
4596 }
4597
4598 static bfd_boolean
4599 som_bfd_is_local_label_name (abfd, name)
4600      bfd *abfd ATTRIBUTE_UNUSED;
4601      const char *name;
4602 {
4603   return (name[0] == 'L' && name[1] == '$');
4604 }
4605
4606 /* Count or process variable-length SOM fixup records.
4607
4608    To avoid code duplication we use this code both to compute the number
4609    of relocations requested by a stream, and to internalize the stream.
4610
4611    When computing the number of relocations requested by a stream the
4612    variables rptr, section, and symbols have no meaning.
4613
4614    Return the number of relocations requested by the fixup stream.  When
4615    not just counting
4616
4617    This needs at least two or three more passes to get it cleaned up.  */
4618
4619 static unsigned int
4620 som_set_reloc_info (fixup, end, internal_relocs, section, symbols, just_count)
4621      unsigned char *fixup;
4622      unsigned int end;
4623      arelent *internal_relocs;
4624      asection *section;
4625      asymbol **symbols;
4626      bfd_boolean just_count;
4627 {
4628   unsigned int op, varname, deallocate_contents = 0;
4629   unsigned char *end_fixups = &fixup[end];
4630   const struct fixup_format *fp;
4631   const char *cp;
4632   unsigned char *save_fixup;
4633   int variables[26], stack[20], c, v, count, prev_fixup, *sp, saved_unwind_bits;
4634   const int *subop;
4635   arelent *rptr = internal_relocs;
4636   unsigned int offset = 0;
4637
4638 #define var(c)          variables[(c) - 'A']
4639 #define push(v)         (*sp++ = (v))
4640 #define pop()           (*--sp)
4641 #define emptystack()    (sp == stack)
4642
4643   som_initialize_reloc_queue (reloc_queue);
4644   memset (variables, 0, sizeof (variables));
4645   memset (stack, 0, sizeof (stack));
4646   count = 0;
4647   prev_fixup = 0;
4648   saved_unwind_bits = 0;
4649   sp = stack;
4650
4651   while (fixup < end_fixups)
4652     {
4653
4654       /* Save pointer to the start of this fixup.  We'll use
4655          it later to determine if it is necessary to put this fixup
4656          on the queue.  */
4657       save_fixup = fixup;
4658
4659       /* Get the fixup code and its associated format.  */
4660       op = *fixup++;
4661       fp = &som_fixup_formats[op];
4662
4663       /* Handle a request for a previous fixup.  */
4664       if (*fp->format == 'P')
4665         {
4666           /* Get pointer to the beginning of the prev fixup, move
4667              the repeated fixup to the head of the queue.  */
4668           fixup = reloc_queue[fp->D].reloc;
4669           som_reloc_queue_fix (reloc_queue, fp->D);
4670           prev_fixup = 1;
4671
4672           /* Get the fixup code and its associated format.  */
4673           op = *fixup++;
4674           fp = &som_fixup_formats[op];
4675         }
4676
4677       /* If this fixup will be passed to BFD, set some reasonable defaults.  */
4678       if (! just_count
4679           && som_hppa_howto_table[op].type != R_NO_RELOCATION
4680           && som_hppa_howto_table[op].type != R_DATA_OVERRIDE)
4681         {
4682           rptr->address = offset;
4683           rptr->howto = &som_hppa_howto_table[op];
4684           rptr->addend = 0;
4685           rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
4686         }
4687
4688       /* Set default input length to 0.  Get the opcode class index
4689          into D.  */
4690       var ('L') = 0;
4691       var ('D') = fp->D;
4692       var ('U') = saved_unwind_bits;
4693
4694       /* Get the opcode format.  */
4695       cp = fp->format;
4696
4697       /* Process the format string.  Parsing happens in two phases,
4698          parse RHS, then assign to LHS.  Repeat until no more
4699          characters in the format string.  */
4700       while (*cp)
4701         {
4702           /* The variable this pass is going to compute a value for.  */
4703           varname = *cp++;
4704
4705           /* Start processing RHS.  Continue until a NULL or '=' is found.  */
4706           do
4707             {
4708               c = *cp++;
4709
4710               /* If this is a variable, push it on the stack.  */
4711               if (ISUPPER (c))
4712                 push (var (c));
4713
4714               /* If this is a lower case letter, then it represents
4715                  additional data from the fixup stream to be pushed onto
4716                  the stack.  */
4717               else if (ISLOWER (c))
4718                 {
4719                   int bits = (c - 'a') * 8;
4720                   for (v = 0; c > 'a'; --c)
4721                     v = (v << 8) | *fixup++;
4722                   if (varname == 'V')
4723                     v = sign_extend (v, bits);
4724                   push (v);
4725                 }
4726
4727               /* A decimal constant.  Push it on the stack.  */
4728               else if (ISDIGIT (c))
4729                 {
4730                   v = c - '0';
4731                   while (ISDIGIT (*cp))
4732                     v = (v * 10) + (*cp++ - '0');
4733                   push (v);
4734                 }
4735               else
4736                 /* An operator.  Pop two two values from the stack and
4737                    use them as operands to the given operation.  Push
4738                    the result of the operation back on the stack.  */
4739                 switch (c)
4740                   {
4741                   case '+':
4742                     v = pop ();
4743                     v += pop ();
4744                     push (v);
4745                     break;
4746                   case '*':
4747                     v = pop ();
4748                     v *= pop ();
4749                     push (v);
4750                     break;
4751                   case '<':
4752                     v = pop ();
4753                     v = pop () << v;
4754                     push (v);
4755                     break;
4756                   default:
4757                     abort ();
4758                   }
4759             }
4760           while (*cp && *cp != '=');
4761
4762           /* Move over the equal operator.  */
4763           cp++;
4764
4765           /* Pop the RHS off the stack.  */
4766           c = pop ();
4767
4768           /* Perform the assignment.  */
4769           var (varname) = c;
4770
4771           /* Handle side effects. and special 'O' stack cases.  */
4772           switch (varname)
4773             {
4774             /* Consume some bytes from the input space.  */
4775             case 'L':
4776               offset += c;
4777               break;
4778             /* A symbol to use in the relocation.  Make a note
4779                of this if we are not just counting.  */
4780             case 'S':
4781               if (! just_count)
4782                 rptr->sym_ptr_ptr = &symbols[c];
4783               break;
4784             /* Argument relocation bits for a function call.  */
4785             case 'R':
4786               if (! just_count)
4787                 {
4788                   unsigned int tmp = var ('R');
4789                   rptr->addend = 0;
4790
4791                   if ((som_hppa_howto_table[op].type == R_PCREL_CALL
4792                        && R_PCREL_CALL + 10 > op)
4793                       || (som_hppa_howto_table[op].type == R_ABS_CALL
4794                           && R_ABS_CALL + 10 > op))
4795                     {
4796                       /* Simple encoding.  */
4797                       if (tmp > 4)
4798                         {
4799                           tmp -= 5;
4800                           rptr->addend |= 1;
4801                         }
4802                       if (tmp == 4)
4803                         rptr->addend |= 1 << 8 | 1 << 6 | 1 << 4 | 1 << 2;
4804                       else if (tmp == 3)
4805                         rptr->addend |= 1 << 8 | 1 << 6 | 1 << 4;
4806                       else if (tmp == 2)
4807                         rptr->addend |= 1 << 8 | 1 << 6;
4808                       else if (tmp == 1)
4809                         rptr->addend |= 1 << 8;
4810                     }
4811                   else
4812                     {
4813                       unsigned int tmp1, tmp2;
4814
4815                       /* First part is easy -- low order two bits are
4816                          directly copied, then shifted away.  */
4817                       rptr->addend = tmp & 0x3;
4818                       tmp >>= 2;
4819
4820                       /* Diving the result by 10 gives us the second
4821                          part.  If it is 9, then the first two words
4822                          are a double precision paramater, else it is
4823                          3 * the first arg bits + the 2nd arg bits.  */
4824                       tmp1 = tmp / 10;
4825                       tmp -= tmp1 * 10;
4826                       if (tmp1 == 9)
4827                         rptr->addend += (0xe << 6);
4828                       else
4829                         {
4830                           /* Get the two pieces.  */
4831                           tmp2 = tmp1 / 3;
4832                           tmp1 -= tmp2 * 3;
4833                           /* Put them in the addend.  */
4834                           rptr->addend += (tmp2 << 8) + (tmp1 << 6);
4835                         }
4836
4837                       /* What's left is the third part.  It's unpacked
4838                          just like the second.  */
4839                       if (tmp == 9)
4840                         rptr->addend += (0xe << 2);
4841                       else
4842                         {
4843                           tmp2 = tmp / 3;
4844                           tmp -= tmp2 * 3;
4845                           rptr->addend += (tmp2 << 4) + (tmp << 2);
4846                         }
4847                     }
4848                   rptr->addend = HPPA_R_ADDEND (rptr->addend, 0);
4849                 }
4850               break;
4851             /* Handle the linker expression stack.  */
4852             case 'O':
4853               switch (op)
4854                 {
4855                 case R_COMP1:
4856                   subop = comp1_opcodes;
4857                   break;
4858                 case R_COMP2:
4859                   subop = comp2_opcodes;
4860                   break;
4861                 case R_COMP3:
4862                   subop = comp3_opcodes;
4863                   break;
4864                 default:
4865                   abort ();
4866                 }
4867               while (*subop <= (unsigned char) c)
4868                 ++subop;
4869               --subop;
4870               break;
4871             /* The lower 32unwind bits must be persistent.  */
4872             case 'U':
4873               saved_unwind_bits = var ('U');
4874               break;
4875
4876             default:
4877               break;
4878             }
4879         }
4880
4881       /* If we used a previous fixup, clean up after it.  */
4882       if (prev_fixup)
4883         {
4884           fixup = save_fixup + 1;
4885           prev_fixup = 0;
4886         }
4887       /* Queue it.  */
4888       else if (fixup > save_fixup + 1)
4889         som_reloc_queue_insert (save_fixup, fixup - save_fixup, reloc_queue);
4890
4891       /* We do not pass R_DATA_OVERRIDE or R_NO_RELOCATION
4892          fixups to BFD.  */
4893       if (som_hppa_howto_table[op].type != R_DATA_OVERRIDE
4894           && som_hppa_howto_table[op].type != R_NO_RELOCATION)
4895         {
4896           /* Done with a single reloction. Loop back to the top.  */
4897           if (! just_count)
4898             {
4899               if (som_hppa_howto_table[op].type == R_ENTRY)
4900                 rptr->addend = var ('T');
4901               else if (som_hppa_howto_table[op].type == R_EXIT)
4902                 rptr->addend = var ('U');
4903               else if (som_hppa_howto_table[op].type == R_PCREL_CALL
4904                        || som_hppa_howto_table[op].type == R_ABS_CALL)
4905                 ;
4906               else if (som_hppa_howto_table[op].type == R_DATA_ONE_SYMBOL)
4907                 {
4908                   /* Try what was specified in R_DATA_OVERRIDE first
4909                      (if anything).  Then the hard way using the
4910                      section contents.  */
4911                   rptr->addend = var ('V');
4912
4913                   if (rptr->addend == 0 && !section->contents)
4914                     {
4915                       /* Got to read the damn contents first.  We don't
4916                          bother saving the contents (yet).  Add it one
4917                          day if the need arises.  */
4918                       section->contents = bfd_malloc (section->_raw_size);
4919                       if (section->contents == NULL)
4920                         return (unsigned) -1;
4921
4922                       deallocate_contents = 1;
4923                       bfd_get_section_contents (section->owner,
4924                                                 section,
4925                                                 section->contents,
4926                                                 (bfd_vma) 0,
4927                                                 section->_raw_size);
4928                     }
4929                   else if (rptr->addend == 0)
4930                     rptr->addend = bfd_get_32 (section->owner,
4931                                                (section->contents
4932                                                 + offset - var ('L')));
4933
4934                 }
4935               else
4936                 rptr->addend = var ('V');
4937               rptr++;
4938             }
4939           count++;
4940           /* Now that we've handled a "full" relocation, reset
4941              some state.  */
4942           memset (variables, 0, sizeof (variables));
4943           memset (stack, 0, sizeof (stack));
4944         }
4945     }
4946   if (deallocate_contents)
4947     free (section->contents);
4948
4949   return count;
4950
4951 #undef var
4952 #undef push
4953 #undef pop
4954 #undef emptystack
4955 }
4956
4957 /* Read in the relocs (aka fixups in SOM terms) for a section.
4958
4959    som_get_reloc_upper_bound calls this routine with JUST_COUNT
4960    set to TRUE to indicate it only needs a count of the number
4961    of actual relocations.  */
4962
4963 static bfd_boolean
4964 som_slurp_reloc_table (abfd, section, symbols, just_count)
4965      bfd *abfd;
4966      asection *section;
4967      asymbol **symbols;
4968      bfd_boolean just_count;
4969 {
4970   char *external_relocs;
4971   unsigned int fixup_stream_size;
4972   arelent *internal_relocs;
4973   unsigned int num_relocs;
4974   bfd_size_type amt;
4975
4976   fixup_stream_size = som_section_data (section)->reloc_size;
4977   /* If there were no relocations, then there is nothing to do.  */
4978   if (section->reloc_count == 0)
4979     return TRUE;
4980
4981   /* If reloc_count is -1, then the relocation stream has not been
4982      parsed.  We must do so now to know how many relocations exist.  */
4983   if (section->reloc_count == (unsigned) -1)
4984     {
4985       amt = fixup_stream_size;
4986       external_relocs = (char *) bfd_malloc (amt);
4987       if (external_relocs == (char *) NULL)
4988         return FALSE;
4989       /* Read in the external forms.  */
4990       if (bfd_seek (abfd,
4991                     obj_som_reloc_filepos (abfd) + section->rel_filepos,
4992                     SEEK_SET)
4993           != 0)
4994         return FALSE;
4995       if (bfd_bread (external_relocs, amt, abfd) != amt)
4996         return FALSE;
4997
4998       /* Let callers know how many relocations found.
4999          also save the relocation stream as we will
5000          need it again.  */
5001       section->reloc_count = som_set_reloc_info (external_relocs,
5002                                                  fixup_stream_size,
5003                                                  NULL, NULL, NULL, TRUE);
5004
5005       som_section_data (section)->reloc_stream = external_relocs;
5006     }
5007
5008   /* If the caller only wanted a count, then return now.  */
5009   if (just_count)
5010     return TRUE;
5011
5012   num_relocs = section->reloc_count;
5013   external_relocs = som_section_data (section)->reloc_stream;
5014   /* Return saved information about the relocations if it is available.  */
5015   if (section->relocation != (arelent *) NULL)
5016     return TRUE;
5017
5018   amt = num_relocs;
5019   amt *= sizeof (arelent);
5020   internal_relocs = (arelent *) bfd_zalloc (abfd, (amt));
5021   if (internal_relocs == (arelent *) NULL)
5022     return FALSE;
5023
5024   /* Process and internalize the relocations.  */
5025   som_set_reloc_info (external_relocs, fixup_stream_size,
5026                       internal_relocs, section, symbols, FALSE);
5027
5028   /* We're done with the external relocations.  Free them.  */
5029   free (external_relocs);
5030   som_section_data (section)->reloc_stream = NULL;
5031
5032   /* Save our results and return success.  */
5033   section->relocation = internal_relocs;
5034   return TRUE;
5035 }
5036
5037 /* Return the number of bytes required to store the relocation
5038    information associated with the given section.  */
5039
5040 static long
5041 som_get_reloc_upper_bound (abfd, asect)
5042      bfd *abfd;
5043      sec_ptr asect;
5044 {
5045   /* If section has relocations, then read in the relocation stream
5046      and parse it to determine how many relocations exist.  */
5047   if (asect->flags & SEC_RELOC)
5048     {
5049       if (! som_slurp_reloc_table (abfd, asect, NULL, TRUE))
5050         return -1;
5051       return (asect->reloc_count + 1) * sizeof (arelent *);
5052     }
5053   /* There are no relocations.  */
5054   return 0;
5055 }
5056
5057 /* Convert relocations from SOM (external) form into BFD internal
5058    form.  Return the number of relocations.  */
5059
5060 static long
5061 som_canonicalize_reloc (abfd, section, relptr, symbols)
5062      bfd *abfd;
5063      sec_ptr section;
5064      arelent **relptr;
5065      asymbol **symbols;
5066 {
5067   arelent *tblptr;
5068   int count;
5069
5070   if (! som_slurp_reloc_table (abfd, section, symbols, FALSE))
5071     return -1;
5072
5073   count = section->reloc_count;
5074   tblptr = section->relocation;
5075
5076   while (count--)
5077     *relptr++ = tblptr++;
5078
5079   *relptr = (arelent *) NULL;
5080   return section->reloc_count;
5081 }
5082
5083 extern const bfd_target som_vec;
5084
5085 /* A hook to set up object file dependent section information.  */
5086
5087 static bfd_boolean
5088 som_new_section_hook (abfd, newsect)
5089      bfd *abfd;
5090      asection *newsect;
5091 {
5092   bfd_size_type amt = sizeof (struct som_section_data_struct);
5093   newsect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
5094   if (!newsect->used_by_bfd)
5095     return FALSE;
5096   newsect->alignment_power = 3;
5097
5098   /* We allow more than three sections internally.  */
5099   return TRUE;
5100 }
5101
5102 /* Copy any private info we understand from the input symbol
5103    to the output symbol.  */
5104
5105 static bfd_boolean
5106 som_bfd_copy_private_symbol_data (ibfd, isymbol, obfd, osymbol)
5107      bfd *ibfd;
5108      asymbol *isymbol;
5109      bfd *obfd;
5110      asymbol *osymbol;
5111 {
5112   struct som_symbol *input_symbol = (struct som_symbol *) isymbol;
5113   struct som_symbol *output_symbol = (struct som_symbol *) osymbol;
5114
5115   /* One day we may try to grok other private data.  */
5116   if (ibfd->xvec->flavour != bfd_target_som_flavour
5117       || obfd->xvec->flavour != bfd_target_som_flavour)
5118     return FALSE;
5119
5120   /* The only private information we need to copy is the argument relocation
5121      bits.  */
5122   output_symbol->tc_data.ap.hppa_arg_reloc =
5123     input_symbol->tc_data.ap.hppa_arg_reloc;
5124
5125   return TRUE;
5126 }
5127
5128 /* Copy any private info we understand from the input section
5129    to the output section.  */
5130
5131 static bfd_boolean
5132 som_bfd_copy_private_section_data (ibfd, isection, obfd, osection)
5133      bfd *ibfd;
5134      asection *isection;
5135      bfd *obfd;
5136      asection *osection;
5137 {
5138   bfd_size_type amt;
5139
5140   /* One day we may try to grok other private data.  */
5141   if (ibfd->xvec->flavour != bfd_target_som_flavour
5142       || obfd->xvec->flavour != bfd_target_som_flavour
5143       || (!som_is_space (isection) && !som_is_subspace (isection)))
5144     return TRUE;
5145
5146   amt = sizeof (struct som_copyable_section_data_struct);
5147   som_section_data (osection)->copy_data =
5148     (struct som_copyable_section_data_struct *) bfd_zalloc (obfd, amt);
5149   if (som_section_data (osection)->copy_data == NULL)
5150     return FALSE;
5151
5152   memcpy (som_section_data (osection)->copy_data,
5153           som_section_data (isection)->copy_data,
5154           sizeof (struct som_copyable_section_data_struct));
5155
5156   /* Reparent if necessary.  */
5157   if (som_section_data (osection)->copy_data->container)
5158     som_section_data (osection)->copy_data->container =
5159       som_section_data (osection)->copy_data->container->output_section;
5160
5161   return TRUE;
5162 }
5163
5164 /* Copy any private info we understand from the input bfd
5165    to the output bfd.  */
5166
5167 static bfd_boolean
5168 som_bfd_copy_private_bfd_data (ibfd, obfd)
5169      bfd *ibfd, *obfd;
5170 {
5171   /* One day we may try to grok other private data.  */
5172   if (ibfd->xvec->flavour != bfd_target_som_flavour
5173       || obfd->xvec->flavour != bfd_target_som_flavour)
5174     return TRUE;
5175
5176   /* Allocate some memory to hold the data we need.  */
5177   obj_som_exec_data (obfd) = (struct som_exec_data *)
5178     bfd_zalloc (obfd, (bfd_size_type) sizeof (struct som_exec_data));
5179   if (obj_som_exec_data (obfd) == NULL)
5180     return FALSE;
5181
5182   /* Now copy the data.  */
5183   memcpy (obj_som_exec_data (obfd), obj_som_exec_data (ibfd),
5184           sizeof (struct som_exec_data));
5185
5186   return TRUE;
5187 }
5188
5189 /* Set backend info for sections which can not be described
5190    in the BFD data structures.  */
5191
5192 bfd_boolean
5193 bfd_som_set_section_attributes (section, defined, private, sort_key, spnum)
5194      asection *section;
5195      int defined;
5196      int private;
5197      unsigned int sort_key;
5198      int spnum;
5199 {
5200   /* Allocate memory to hold the magic information.  */
5201   if (som_section_data (section)->copy_data == NULL)
5202     {
5203       bfd_size_type amt = sizeof (struct som_copyable_section_data_struct);
5204       som_section_data (section)->copy_data =
5205         (struct som_copyable_section_data_struct *) bfd_zalloc (section->owner,
5206                                                                 amt);
5207       if (som_section_data (section)->copy_data == NULL)
5208         return FALSE;
5209     }
5210   som_section_data (section)->copy_data->sort_key = sort_key;
5211   som_section_data (section)->copy_data->is_defined = defined;
5212   som_section_data (section)->copy_data->is_private = private;
5213   som_section_data (section)->copy_data->container = section;
5214   som_section_data (section)->copy_data->space_number = spnum;
5215   return TRUE;
5216 }
5217
5218 /* Set backend info for subsections which can not be described
5219    in the BFD data structures.  */
5220
5221 bfd_boolean
5222 bfd_som_set_subsection_attributes (section, container, access,
5223                                    sort_key, quadrant)
5224      asection *section;
5225      asection *container;
5226      int access;
5227      unsigned int sort_key;
5228      int quadrant;
5229 {
5230   /* Allocate memory to hold the magic information.  */
5231   if (som_section_data (section)->copy_data == NULL)
5232     {
5233       bfd_size_type amt = sizeof (struct som_copyable_section_data_struct);
5234       som_section_data (section)->copy_data =
5235         (struct som_copyable_section_data_struct *) bfd_zalloc (section->owner,
5236                                                                 amt);
5237       if (som_section_data (section)->copy_data == NULL)
5238         return FALSE;
5239     }
5240   som_section_data (section)->copy_data->sort_key = sort_key;
5241   som_section_data (section)->copy_data->access_control_bits = access;
5242   som_section_data (section)->copy_data->quadrant = quadrant;
5243   som_section_data (section)->copy_data->container = container;
5244   return TRUE;
5245 }
5246
5247 /* Set the full SOM symbol type.  SOM needs far more symbol information
5248    than any other object file format I'm aware of.  It is mandatory
5249    to be able to know if a symbol is an entry point, millicode, data,
5250    code, absolute, storage request, or procedure label.  If you get
5251    the symbol type wrong your program will not link.  */
5252
5253 void
5254 bfd_som_set_symbol_type (symbol, type)
5255      asymbol *symbol;
5256      unsigned int type;
5257 {
5258   som_symbol_data (symbol)->som_type = type;
5259 }
5260
5261 /* Attach an auxiliary header to the BFD backend so that it may be
5262    written into the object file.  */
5263
5264 bfd_boolean
5265 bfd_som_attach_aux_hdr (abfd, type, string)
5266      bfd *abfd;
5267      int type;
5268      char *string;
5269 {
5270   bfd_size_type amt;
5271
5272   if (type == VERSION_AUX_ID)
5273     {
5274       size_t len = strlen (string);
5275       int pad = 0;
5276
5277       if (len % 4)
5278         pad = (4 - (len % 4));
5279       amt = sizeof (struct aux_id) + sizeof (unsigned int) + len + pad;
5280       obj_som_version_hdr (abfd) =
5281         (struct user_string_aux_hdr *) bfd_zalloc (abfd, amt);
5282       if (!obj_som_version_hdr (abfd))
5283         return FALSE;
5284       obj_som_version_hdr (abfd)->header_id.type = VERSION_AUX_ID;
5285       obj_som_version_hdr (abfd)->header_id.length = len + pad;
5286       obj_som_version_hdr (abfd)->header_id.length += sizeof (int);
5287       obj_som_version_hdr (abfd)->string_length = len;
5288       strncpy (obj_som_version_hdr (abfd)->user_string, string, len);
5289     }
5290   else if (type == COPYRIGHT_AUX_ID)
5291     {
5292       int len = strlen (string);
5293       int pad = 0;
5294
5295       if (len % 4)
5296         pad = (4 - (len % 4));
5297       amt = sizeof (struct aux_id) + sizeof (unsigned int) + len + pad;
5298       obj_som_copyright_hdr (abfd) =
5299         (struct copyright_aux_hdr *) bfd_zalloc (abfd, amt);
5300       if (!obj_som_copyright_hdr (abfd))
5301         return FALSE;
5302       obj_som_copyright_hdr (abfd)->header_id.type = COPYRIGHT_AUX_ID;
5303       obj_som_copyright_hdr (abfd)->header_id.length = len + pad;
5304       obj_som_copyright_hdr (abfd)->header_id.length += sizeof (int);
5305       obj_som_copyright_hdr (abfd)->string_length = len;
5306       strcpy (obj_som_copyright_hdr (abfd)->copyright, string);
5307     }
5308   return TRUE;
5309 }
5310
5311 /* Attach a compilation unit header to the BFD backend so that it may be
5312    written into the object file.  */
5313
5314 bfd_boolean
5315 bfd_som_attach_compilation_unit (abfd, name, language_name, product_id,
5316                                  version_id)
5317      bfd *abfd;
5318      const char *name;
5319      const char *language_name;
5320      const char *product_id;
5321      const char *version_id;
5322 {
5323   COMPUNIT *n = (COMPUNIT *) bfd_zalloc (abfd, (bfd_size_type) COMPUNITSZ);
5324   if (n == NULL)
5325     return FALSE;
5326
5327 #define STRDUP(f) \
5328   if (f != NULL) \
5329     { \
5330       n->f.n_name = bfd_alloc (abfd, (bfd_size_type) strlen (f) + 1); \
5331       if (n->f.n_name == NULL) \
5332         return FALSE; \
5333       strcpy (n->f.n_name, f); \
5334     }
5335
5336   STRDUP (name);
5337   STRDUP (language_name);
5338   STRDUP (product_id);
5339   STRDUP (version_id);
5340
5341 #undef STRDUP
5342
5343   obj_som_compilation_unit (abfd) = n;
5344
5345   return TRUE;
5346 }
5347
5348 static bfd_boolean
5349 som_get_section_contents (abfd, section, location, offset, count)
5350      bfd *abfd;
5351      sec_ptr section;
5352      PTR location;
5353      file_ptr offset;
5354      bfd_size_type count;
5355 {
5356   if (count == 0 || ((section->flags & SEC_HAS_CONTENTS) == 0))
5357     return TRUE;
5358   if ((bfd_size_type) (offset+count) > section->_raw_size
5359       || bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
5360       || bfd_bread (location, count, abfd) != count)
5361     return FALSE; /* On error.  */
5362   return TRUE;
5363 }
5364
5365 static bfd_boolean
5366 som_set_section_contents (abfd, section, location, offset, count)
5367      bfd *abfd;
5368      sec_ptr section;
5369      const PTR location;
5370      file_ptr offset;
5371      bfd_size_type count;
5372 {
5373   if (! abfd->output_has_begun)
5374     {
5375       /* Set up fixed parts of the file, space, and subspace headers.
5376          Notify the world that output has begun.  */
5377       som_prep_headers (abfd);
5378       abfd->output_has_begun = TRUE;
5379       /* Start writing the object file.  This include all the string
5380          tables, fixup streams, and other portions of the object file.  */
5381       som_begin_writing (abfd);
5382     }
5383
5384   /* Only write subspaces which have "real" contents (eg. the contents
5385      are not generated at run time by the OS).  */
5386   if (!som_is_subspace (section)
5387       || ((section->flags & SEC_HAS_CONTENTS) == 0))
5388     return TRUE;
5389
5390   /* Seek to the proper offset within the object file and write the
5391      data.  */
5392   offset += som_section_data (section)->subspace_dict->file_loc_init_value;
5393   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
5394     return FALSE;
5395
5396   if (bfd_bwrite (location, count, abfd) != count)
5397     return FALSE;
5398   return TRUE;
5399 }
5400
5401 static bfd_boolean
5402 som_set_arch_mach (abfd, arch, machine)
5403      bfd *abfd;
5404      enum bfd_architecture arch;
5405      unsigned long machine;
5406 {
5407   /* Allow any architecture to be supported by the SOM backend.  */
5408   return bfd_default_set_arch_mach (abfd, arch, machine);
5409 }
5410
5411 static bfd_boolean
5412 som_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
5413                        functionname_ptr, line_ptr)
5414      bfd *abfd ATTRIBUTE_UNUSED;
5415      asection *section ATTRIBUTE_UNUSED;
5416      asymbol **symbols ATTRIBUTE_UNUSED;
5417      bfd_vma offset ATTRIBUTE_UNUSED;
5418      const char **filename_ptr ATTRIBUTE_UNUSED;
5419      const char **functionname_ptr ATTRIBUTE_UNUSED;
5420      unsigned int *line_ptr ATTRIBUTE_UNUSED;
5421 {
5422   return FALSE;
5423 }
5424
5425 static int
5426 som_sizeof_headers (abfd, reloc)
5427      bfd *abfd ATTRIBUTE_UNUSED;
5428      bfd_boolean reloc ATTRIBUTE_UNUSED;
5429 {
5430   (*_bfd_error_handler) (_("som_sizeof_headers unimplemented"));
5431   fflush (stderr);
5432   abort ();
5433   return 0;
5434 }
5435
5436 /* Return the single-character symbol type corresponding to
5437    SOM section S, or '?' for an unknown SOM section.  */
5438
5439 static char
5440 som_section_type (s)
5441      const char *s;
5442 {
5443   const struct section_to_type *t;
5444
5445   for (t = &stt[0]; t->section; t++)
5446     if (!strcmp (s, t->section))
5447       return t->type;
5448   return '?';
5449 }
5450
5451 static int
5452 som_decode_symclass (symbol)
5453      asymbol *symbol;
5454 {
5455   char c;
5456
5457   if (bfd_is_com_section (symbol->section))
5458     return 'C';
5459   if (bfd_is_und_section (symbol->section))
5460     return 'U';
5461   if (bfd_is_ind_section (symbol->section))
5462     return 'I';
5463   if (symbol->flags & BSF_WEAK)
5464     return 'W';
5465   if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL)))
5466     return '?';
5467
5468   if (bfd_is_abs_section (symbol->section)
5469       || (som_symbol_data (symbol) != NULL
5470           && som_symbol_data (symbol)->som_type == SYMBOL_TYPE_ABSOLUTE))
5471     c = 'a';
5472   else if (symbol->section)
5473     c = som_section_type (symbol->section->name);
5474   else
5475     return '?';
5476   if (symbol->flags & BSF_GLOBAL)
5477     c = TOUPPER (c);
5478   return c;
5479 }
5480
5481 /* Return information about SOM symbol SYMBOL in RET.  */
5482
5483 static void
5484 som_get_symbol_info (ignore_abfd, symbol, ret)
5485      bfd *ignore_abfd ATTRIBUTE_UNUSED;
5486      asymbol *symbol;
5487      symbol_info *ret;
5488 {
5489   ret->type = som_decode_symclass (symbol);
5490   if (ret->type != 'U')
5491     ret->value = symbol->value + symbol->section->vma;
5492   else
5493     ret->value = 0;
5494   ret->name = symbol->name;
5495 }
5496
5497 /* Count the number of symbols in the archive symbol table.  Necessary
5498    so that we can allocate space for all the carsyms at once.  */
5499
5500 static bfd_boolean
5501 som_bfd_count_ar_symbols (abfd, lst_header, count)
5502      bfd *abfd;
5503      struct lst_header *lst_header;
5504      symindex *count;
5505 {
5506   unsigned int i;
5507   unsigned int *hash_table = NULL;
5508   bfd_size_type amt;
5509   file_ptr lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header);
5510
5511   amt = lst_header->hash_size;
5512   amt *= sizeof (unsigned int);
5513   hash_table = (unsigned int *) bfd_malloc (amt);
5514   if (hash_table == NULL && lst_header->hash_size != 0)
5515     goto error_return;
5516
5517   /* Don't forget to initialize the counter!  */
5518   *count = 0;
5519
5520   /* Read in the hash table.  The has table is an array of 32bit file offsets
5521      which point to the hash chains.  */
5522   if (bfd_bread ((PTR) hash_table, amt, abfd) != amt)
5523     goto error_return;
5524
5525   /* Walk each chain counting the number of symbols found on that particular
5526      chain.  */
5527   for (i = 0; i < lst_header->hash_size; i++)
5528     {
5529       struct lst_symbol_record lst_symbol;
5530
5531       /* An empty chain has zero as it's file offset.  */
5532       if (hash_table[i] == 0)
5533         continue;
5534
5535       /* Seek to the first symbol in this hash chain.  */
5536       if (bfd_seek (abfd, lst_filepos + hash_table[i], SEEK_SET) != 0)
5537         goto error_return;
5538
5539       /* Read in this symbol and update the counter.  */
5540       amt = sizeof (lst_symbol);
5541       if (bfd_bread ((PTR) &lst_symbol, amt, abfd) != amt)
5542         goto error_return;
5543
5544       (*count)++;
5545
5546       /* Now iterate through the rest of the symbols on this chain.  */
5547       while (lst_symbol.next_entry)
5548         {
5549
5550           /* Seek to the next symbol.  */
5551           if (bfd_seek (abfd, lst_filepos + lst_symbol.next_entry, SEEK_SET)
5552               != 0)
5553             goto error_return;
5554
5555           /* Read the symbol in and update the counter.  */
5556           amt = sizeof (lst_symbol);
5557           if (bfd_bread ((PTR) &lst_symbol, amt, abfd) != amt)
5558             goto error_return;
5559
5560           (*count)++;
5561         }
5562     }
5563   if (hash_table != NULL)
5564     free (hash_table);
5565   return TRUE;
5566
5567  error_return:
5568   if (hash_table != NULL)
5569     free (hash_table);
5570   return FALSE;
5571 }
5572
5573 /* Fill in the canonical archive symbols (SYMS) from the archive described
5574    by ABFD and LST_HEADER.  */
5575
5576 static bfd_boolean
5577 som_bfd_fill_in_ar_symbols (abfd, lst_header, syms)
5578      bfd *abfd;
5579      struct lst_header *lst_header;
5580      carsym **syms;
5581 {
5582   unsigned int i, len;
5583   carsym *set = syms[0];
5584   unsigned int *hash_table = NULL;
5585   struct som_entry *som_dict = NULL;
5586   bfd_size_type amt;
5587   file_ptr lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header);
5588
5589   amt = lst_header->hash_size;
5590   amt *= sizeof (unsigned int);
5591   hash_table = (unsigned int *) bfd_malloc (amt);
5592   if (hash_table == NULL && lst_header->hash_size != 0)
5593     goto error_return;
5594
5595   /* Read in the hash table.  The has table is an array of 32bit file offsets
5596      which point to the hash chains.  */
5597   if (bfd_bread ((PTR) hash_table, amt, abfd) != amt)
5598     goto error_return;
5599
5600   /* Seek to and read in the SOM dictionary.  We will need this to fill
5601      in the carsym's filepos field.  */
5602   if (bfd_seek (abfd, lst_filepos + lst_header->dir_loc, SEEK_SET) != 0)
5603     goto error_return;
5604
5605   amt = lst_header->module_count;
5606   amt *= sizeof (struct som_entry);
5607   som_dict = (struct som_entry *) bfd_malloc (amt);
5608   if (som_dict == NULL && lst_header->module_count != 0)
5609     goto error_return;
5610
5611   if (bfd_bread ((PTR) som_dict, amt, abfd) != amt)
5612     goto error_return;
5613
5614   /* Walk each chain filling in the carsyms as we go along.  */
5615   for (i = 0; i < lst_header->hash_size; i++)
5616     {
5617       struct lst_symbol_record lst_symbol;
5618
5619       /* An empty chain has zero as it's file offset.  */
5620       if (hash_table[i] == 0)
5621         continue;
5622
5623       /* Seek to and read the first symbol on the chain.  */
5624       if (bfd_seek (abfd, lst_filepos + hash_table[i], SEEK_SET) != 0)
5625         goto error_return;
5626
5627       amt = sizeof (lst_symbol);
5628       if (bfd_bread ((PTR) &lst_symbol, amt, abfd) != amt)
5629         goto error_return;
5630
5631       /* Get the name of the symbol, first get the length which is stored
5632          as a 32bit integer just before the symbol.
5633
5634          One might ask why we don't just read in the entire string table
5635          and index into it.  Well, according to the SOM ABI the string
5636          index can point *anywhere* in the archive to save space, so just
5637          using the string table would not be safe.  */
5638       if (bfd_seek (abfd, lst_filepos + lst_header->string_loc
5639                             + lst_symbol.name.n_strx - 4, SEEK_SET) != 0)
5640         goto error_return;
5641
5642       if (bfd_bread (&len, (bfd_size_type) 4, abfd) != 4)
5643         goto error_return;
5644
5645       /* Allocate space for the name and null terminate it too.  */
5646       set->name = bfd_zalloc (abfd, (bfd_size_type) len + 1);
5647       if (!set->name)
5648         goto error_return;
5649       if (bfd_bread (set->name, (bfd_size_type) len, abfd) != len)
5650         goto error_return;
5651
5652       set->name[len] = 0;
5653
5654       /* Fill in the file offset.  Note that the "location" field points
5655          to the SOM itself, not the ar_hdr in front of it.  */
5656       set->file_offset = som_dict[lst_symbol.som_index].location
5657                           - sizeof (struct ar_hdr);
5658
5659       /* Go to the next symbol.  */
5660       set++;
5661
5662       /* Iterate through the rest of the chain.  */
5663       while (lst_symbol.next_entry)
5664         {
5665           /* Seek to the next symbol and read it in.  */
5666           if (bfd_seek (abfd, lst_filepos + lst_symbol.next_entry, SEEK_SET)
5667               != 0)
5668             goto error_return;
5669
5670           amt = sizeof (lst_symbol);
5671           if (bfd_bread ((PTR) &lst_symbol, amt, abfd) != amt)
5672             goto error_return;
5673
5674           /* Seek to the name length & string and read them in.  */
5675           if (bfd_seek (abfd, lst_filepos + lst_header->string_loc
5676                                 + lst_symbol.name.n_strx - 4, SEEK_SET) != 0)
5677             goto error_return;
5678
5679           if (bfd_bread (&len, (bfd_size_type) 4, abfd) != 4)
5680             goto error_return;
5681
5682           /* Allocate space for the name and null terminate it too.  */
5683           set->name = bfd_zalloc (abfd, (bfd_size_type) len + 1);
5684           if (!set->name)
5685             goto error_return;
5686
5687           if (bfd_bread (set->name, (bfd_size_type) len, abfd) != len)
5688             goto error_return;
5689           set->name[len] = 0;
5690
5691           /* Fill in the file offset.  Note that the "location" field points
5692              to the SOM itself, not the ar_hdr in front of it.  */
5693           set->file_offset = som_dict[lst_symbol.som_index].location
5694                                - sizeof (struct ar_hdr);
5695
5696           /* Go on to the next symbol.  */
5697           set++;
5698         }
5699     }
5700   /* If we haven't died by now, then we successfully read the entire
5701      archive symbol table.  */
5702   if (hash_table != NULL)
5703     free (hash_table);
5704   if (som_dict != NULL)
5705     free (som_dict);
5706   return TRUE;
5707
5708  error_return:
5709   if (hash_table != NULL)
5710     free (hash_table);
5711   if (som_dict != NULL)
5712     free (som_dict);
5713   return FALSE;
5714 }
5715
5716 /* Read in the LST from the archive.  */
5717
5718 static bfd_boolean
5719 som_slurp_armap (abfd)
5720      bfd *abfd;
5721 {
5722   struct lst_header lst_header;
5723   struct ar_hdr ar_header;
5724   unsigned int parsed_size;
5725   struct artdata *ardata = bfd_ardata (abfd);
5726   char nextname[17];
5727   bfd_size_type amt = 16;
5728   int i = bfd_bread ((PTR) nextname, amt, abfd);
5729
5730   /* Special cases.  */
5731   if (i == 0)
5732     return TRUE;
5733   if (i != 16)
5734     return FALSE;
5735
5736   if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
5737     return FALSE;
5738
5739   /* For archives without .o files there is no symbol table.  */
5740   if (strncmp (nextname, "/               ", 16))
5741     {
5742       bfd_has_map (abfd) = FALSE;
5743       return TRUE;
5744     }
5745
5746   /* Read in and sanity check the archive header.  */
5747   amt = sizeof (struct ar_hdr);
5748   if (bfd_bread ((PTR) &ar_header, amt, abfd) != amt)
5749     return FALSE;
5750
5751   if (strncmp (ar_header.ar_fmag, ARFMAG, 2))
5752     {
5753       bfd_set_error (bfd_error_malformed_archive);
5754       return FALSE;
5755     }
5756
5757   /* How big is the archive symbol table entry?  */
5758   errno = 0;
5759   parsed_size = strtol (ar_header.ar_size, NULL, 10);
5760   if (errno != 0)
5761     {
5762       bfd_set_error (bfd_error_malformed_archive);
5763       return FALSE;
5764     }
5765
5766   /* Save off the file offset of the first real user data.  */
5767   ardata->first_file_filepos = bfd_tell (abfd) + parsed_size;
5768
5769   /* Read in the library symbol table.  We'll make heavy use of this
5770      in just a minute.  */
5771   amt = sizeof (struct lst_header);
5772   if (bfd_bread ((PTR) &lst_header, amt, abfd) != amt)
5773     return FALSE;
5774
5775   /* Sanity check.  */
5776   if (lst_header.a_magic != LIBMAGIC)
5777     {
5778       bfd_set_error (bfd_error_malformed_archive);
5779       return FALSE;
5780     }
5781
5782   /* Count the number of symbols in the library symbol table.  */
5783   if (! som_bfd_count_ar_symbols (abfd, &lst_header, &ardata->symdef_count))
5784     return FALSE;
5785
5786   /* Get back to the start of the library symbol table.  */
5787   if (bfd_seek (abfd, (ardata->first_file_filepos - parsed_size
5788                        + sizeof (struct lst_header)), SEEK_SET) != 0)
5789     return FALSE;
5790
5791   /* Initialize the cache and allocate space for the library symbols.  */
5792   ardata->cache = 0;
5793   amt = ardata->symdef_count;
5794   amt *= sizeof (carsym);
5795   ardata->symdefs = (carsym *) bfd_alloc (abfd, amt);
5796   if (!ardata->symdefs)
5797     return FALSE;
5798
5799   /* Now fill in the canonical archive symbols.  */
5800   if (! som_bfd_fill_in_ar_symbols (abfd, &lst_header, &ardata->symdefs))
5801     return FALSE;
5802
5803   /* Seek back to the "first" file in the archive.  Note the "first"
5804      file may be the extended name table.  */
5805   if (bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET) != 0)
5806     return FALSE;
5807
5808   /* Notify the generic archive code that we have a symbol map.  */
5809   bfd_has_map (abfd) = TRUE;
5810   return TRUE;
5811 }
5812
5813 /* Begin preparing to write a SOM library symbol table.
5814
5815    As part of the prep work we need to determine the number of symbols
5816    and the size of the associated string section.  */
5817
5818 static bfd_boolean
5819 som_bfd_prep_for_ar_write (abfd, num_syms, stringsize)
5820      bfd *abfd;
5821      unsigned int *num_syms, *stringsize;
5822 {
5823   bfd *curr_bfd = abfd->archive_head;
5824
5825   /* Some initialization.  */
5826   *num_syms = 0;
5827   *stringsize = 0;
5828
5829   /* Iterate over each BFD within this archive.  */
5830   while (curr_bfd != NULL)
5831     {
5832       unsigned int curr_count, i;
5833       som_symbol_type *sym;
5834
5835       /* Don't bother for non-SOM objects.  */
5836       if (curr_bfd->format != bfd_object
5837           || curr_bfd->xvec->flavour != bfd_target_som_flavour)
5838         {
5839           curr_bfd = curr_bfd->next;
5840           continue;
5841         }
5842
5843       /* Make sure the symbol table has been read, then snag a pointer
5844          to it.  It's a little slimey to grab the symbols via obj_som_symtab,
5845          but doing so avoids allocating lots of extra memory.  */
5846       if (! som_slurp_symbol_table (curr_bfd))
5847         return FALSE;
5848
5849       sym = obj_som_symtab (curr_bfd);
5850       curr_count = bfd_get_symcount (curr_bfd);
5851
5852       /* Examine each symbol to determine if it belongs in the
5853          library symbol table.  */
5854       for (i = 0; i < curr_count; i++, sym++)
5855         {
5856           struct som_misc_symbol_info info;
5857
5858           /* Derive SOM information from the BFD symbol.  */
5859           som_bfd_derive_misc_symbol_info (curr_bfd, &sym->symbol, &info);
5860
5861           /* Should we include this symbol?  */
5862           if (info.symbol_type == ST_NULL
5863               || info.symbol_type == ST_SYM_EXT
5864               || info.symbol_type == ST_ARG_EXT)
5865             continue;
5866
5867           /* Only global symbols and unsatisfied commons.  */
5868           if (info.symbol_scope != SS_UNIVERSAL
5869               && info.symbol_type != ST_STORAGE)
5870             continue;
5871
5872           /* Do no include undefined symbols.  */
5873           if (bfd_is_und_section (sym->symbol.section))
5874             continue;
5875
5876           /* Bump the various counters, being careful to honor
5877              alignment considerations in the string table.  */
5878           (*num_syms)++;
5879           *stringsize = *stringsize + strlen (sym->symbol.name) + 5;
5880           while (*stringsize % 4)
5881             (*stringsize)++;
5882         }
5883
5884       curr_bfd = curr_bfd->next;
5885     }
5886   return TRUE;
5887 }
5888
5889 /* Hash a symbol name based on the hashing algorithm presented in the
5890    SOM ABI.  */
5891
5892 static unsigned int
5893 som_bfd_ar_symbol_hash (symbol)
5894      asymbol *symbol;
5895 {
5896   unsigned int len = strlen (symbol->name);
5897
5898   /* Names with length 1 are special.  */
5899   if (len == 1)
5900     return 0x1000100 | (symbol->name[0] << 16) | symbol->name[0];
5901
5902   return ((len & 0x7f) << 24) | (symbol->name[1] << 16)
5903           | (symbol->name[len - 2] << 8) | symbol->name[len - 1];
5904 }
5905
5906 /* Do the bulk of the work required to write the SOM library
5907    symbol table.  */
5908
5909 static bfd_boolean
5910 som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst, elength)
5911      bfd *abfd;
5912      unsigned int nsyms, string_size;
5913      struct lst_header lst;
5914      unsigned elength;
5915 {
5916   file_ptr lst_filepos;
5917   char *strings = NULL, *p;
5918   struct lst_symbol_record *lst_syms = NULL, *curr_lst_sym;
5919   bfd *curr_bfd;
5920   unsigned int *hash_table = NULL;
5921   struct som_entry *som_dict = NULL;
5922   struct lst_symbol_record **last_hash_entry = NULL;
5923   unsigned int curr_som_offset, som_index = 0;
5924   bfd_size_type amt;
5925
5926   amt = lst.hash_size;
5927   amt *= sizeof (unsigned int);
5928   hash_table = (unsigned int *) bfd_zmalloc (amt);
5929   if (hash_table == NULL && lst.hash_size != 0)
5930     goto error_return;
5931
5932   amt = lst.module_count;
5933   amt *= sizeof (struct som_entry);
5934   som_dict = (struct som_entry *) bfd_zmalloc (amt);
5935   if (som_dict == NULL && lst.module_count != 0)
5936     goto error_return;
5937
5938   amt = lst.hash_size;
5939   amt *= sizeof (struct lst_symbol_record *);
5940   last_hash_entry = ((struct lst_symbol_record **) bfd_zmalloc (amt));
5941   if (last_hash_entry == NULL && lst.hash_size != 0)
5942     goto error_return;
5943
5944   /* Lots of fields are file positions relative to the start
5945      of the lst record.  So save its location.  */
5946   lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header);
5947
5948   /* Symbols have som_index fields, so we have to keep track of the
5949      index of each SOM in the archive.
5950
5951      The SOM dictionary has (among other things) the absolute file
5952      position for the SOM which a particular dictionary entry
5953      describes.  We have to compute that information as we iterate
5954      through the SOMs/symbols.  */
5955   som_index = 0;
5956
5957   /* We add in the size of the archive header twice as the location
5958      in the SOM dictionary is the actual offset of the SOM, not the
5959      archive header before the SOM.  */
5960   curr_som_offset = 8 + 2 * sizeof (struct ar_hdr) + lst.file_end;
5961
5962   /* Make room for the archive header and the contents of the
5963      extended string table.  Note that elength includes the size
5964      of the archive header for the extended name table!  */
5965   if (elength)
5966     curr_som_offset += elength;
5967
5968   /* Make sure we're properly aligned.  */
5969   curr_som_offset = (curr_som_offset + 0x1) & ~0x1;
5970
5971   /* FIXME should be done with buffers just like everything else...  */
5972   amt = nsyms;
5973   amt *= sizeof (struct lst_symbol_record);
5974   lst_syms = bfd_malloc (amt);
5975   if (lst_syms == NULL && nsyms != 0)
5976     goto error_return;
5977   strings = bfd_malloc ((bfd_size_type) string_size);
5978   if (strings == NULL && string_size != 0)
5979     goto error_return;
5980
5981   p = strings;
5982   curr_lst_sym = lst_syms;
5983
5984   curr_bfd = abfd->archive_head;
5985   while (curr_bfd != NULL)
5986     {
5987       unsigned int curr_count, i;
5988       som_symbol_type *sym;
5989
5990       /* Don't bother for non-SOM objects.  */
5991       if (curr_bfd->format != bfd_object
5992           || curr_bfd->xvec->flavour != bfd_target_som_flavour)
5993         {
5994           curr_bfd = curr_bfd->next;
5995           continue;
5996         }
5997
5998       /* Make sure the symbol table has been read, then snag a pointer
5999          to it.  It's a little slimey to grab the symbols via obj_som_symtab,
6000          but doing so avoids allocating lots of extra memory.  */
6001       if (! som_slurp_symbol_table (curr_bfd))
6002         goto error_return;
6003
6004       sym = obj_som_symtab (curr_bfd);
6005       curr_count = bfd_get_symcount (curr_bfd);
6006
6007       for (i = 0; i < curr_count; i++, sym++)
6008         {
6009           struct som_misc_symbol_info info;
6010
6011           /* Derive SOM information from the BFD symbol.  */
6012           som_bfd_derive_misc_symbol_info (curr_bfd, &sym->symbol, &info);
6013
6014           /* Should we include this symbol?  */
6015           if (info.symbol_type == ST_NULL
6016               || info.symbol_type == ST_SYM_EXT
6017               || info.symbol_type == ST_ARG_EXT)
6018             continue;
6019
6020           /* Only global symbols and unsatisfied commons.  */
6021           if (info.symbol_scope != SS_UNIVERSAL
6022               && info.symbol_type != ST_STORAGE)
6023             continue;
6024
6025           /* Do no include undefined symbols.  */
6026           if (bfd_is_und_section (sym->symbol.section))
6027             continue;
6028
6029           /* If this is the first symbol from this SOM, then update
6030              the SOM dictionary too.  */
6031           if (som_dict[som_index].location == 0)
6032             {
6033               som_dict[som_index].location = curr_som_offset;
6034               som_dict[som_index].length = arelt_size (curr_bfd);
6035             }
6036
6037           /* Fill in the lst symbol record.  */
6038           curr_lst_sym->hidden = 0;
6039           curr_lst_sym->secondary_def = info.secondary_def;
6040           curr_lst_sym->symbol_type = info.symbol_type;
6041           curr_lst_sym->symbol_scope = info.symbol_scope;
6042           curr_lst_sym->check_level = 0;
6043           curr_lst_sym->must_qualify = 0;
6044           curr_lst_sym->initially_frozen = 0;
6045           curr_lst_sym->memory_resident = 0;
6046           curr_lst_sym->is_common = bfd_is_com_section (sym->symbol.section);
6047           curr_lst_sym->dup_common = 0;
6048           curr_lst_sym->xleast = 3;
6049           curr_lst_sym->arg_reloc = info.arg_reloc;
6050           curr_lst_sym->name.n_strx = p - strings + 4;
6051           curr_lst_sym->qualifier_name.n_strx = 0;
6052           curr_lst_sym->symbol_info = info.symbol_info;
6053           curr_lst_sym->symbol_value = info.symbol_value | info.priv_level;
6054           curr_lst_sym->symbol_descriptor = 0;
6055           curr_lst_sym->reserved = 0;
6056           curr_lst_sym->som_index = som_index;
6057           curr_lst_sym->symbol_key = som_bfd_ar_symbol_hash (&sym->symbol);
6058           curr_lst_sym->next_entry = 0;
6059
6060           /* Insert into the hash table.  */
6061           if (hash_table[curr_lst_sym->symbol_key % lst.hash_size])
6062             {
6063               struct lst_symbol_record *tmp;
6064
6065               /* There is already something at the head of this hash chain,
6066                  so tack this symbol onto the end of the chain.  */
6067               tmp = last_hash_entry[curr_lst_sym->symbol_key % lst.hash_size];
6068               tmp->next_entry
6069                 = (curr_lst_sym - lst_syms) * sizeof (struct lst_symbol_record)
6070                   + lst.hash_size * 4
6071                   + lst.module_count * sizeof (struct som_entry)
6072                   + sizeof (struct lst_header);
6073             }
6074           else
6075             {
6076               /* First entry in this hash chain.  */
6077               hash_table[curr_lst_sym->symbol_key % lst.hash_size]
6078                 = (curr_lst_sym - lst_syms) * sizeof (struct lst_symbol_record)
6079                   + lst.hash_size * 4
6080                   + lst.module_count * sizeof (struct som_entry)
6081                   + sizeof (struct lst_header);
6082             }
6083
6084           /* Keep track of the last symbol we added to this chain so we can
6085              easily update its next_entry pointer.  */
6086           last_hash_entry[curr_lst_sym->symbol_key % lst.hash_size]
6087             = curr_lst_sym;
6088
6089           /* Update the string table.  */
6090           bfd_put_32 (abfd, strlen (sym->symbol.name), p);
6091           p += 4;
6092           strcpy (p, sym->symbol.name);
6093           p += strlen (sym->symbol.name) + 1;
6094           while ((int) p % 4)
6095             {
6096               bfd_put_8 (abfd, 0, p);
6097               p++;
6098             }
6099
6100           /* Head to the next symbol.  */
6101           curr_lst_sym++;
6102         }
6103
6104       /* Keep track of where each SOM will finally reside; then look
6105          at the next BFD.  */
6106       curr_som_offset += arelt_size (curr_bfd) + sizeof (struct ar_hdr);
6107
6108       /* A particular object in the archive may have an odd length; the
6109          linker requires objects begin on an even boundary.  So round
6110          up the current offset as necessary.  */
6111       curr_som_offset = (curr_som_offset + 0x1) &~ (unsigned) 1;
6112       curr_bfd = curr_bfd->next;
6113       som_index++;
6114     }
6115
6116   /* Now scribble out the hash table.  */
6117   amt = lst.hash_size * 4;
6118   if (bfd_bwrite ((PTR) hash_table, amt, abfd) != amt)
6119     goto error_return;
6120
6121   /* Then the SOM dictionary.  */
6122   amt = lst.module_count * sizeof (struct som_entry);
6123   if (bfd_bwrite ((PTR) som_dict, amt, abfd) != amt)
6124     goto error_return;
6125
6126   /* The library symbols.  */
6127   amt = nsyms * sizeof (struct lst_symbol_record);
6128   if (bfd_bwrite ((PTR) lst_syms, amt, abfd) != amt)
6129     goto error_return;
6130
6131   /* And finally the strings.  */
6132   amt = string_size;
6133   if (bfd_bwrite ((PTR) strings, amt, abfd) != amt)
6134     goto error_return;
6135
6136   if (hash_table != NULL)
6137     free (hash_table);
6138   if (som_dict != NULL)
6139     free (som_dict);
6140   if (last_hash_entry != NULL)
6141     free (last_hash_entry);
6142   if (lst_syms != NULL)
6143     free (lst_syms);
6144   if (strings != NULL)
6145     free (strings);
6146   return TRUE;
6147
6148  error_return:
6149   if (hash_table != NULL)
6150     free (hash_table);
6151   if (som_dict != NULL)
6152     free (som_dict);
6153   if (last_hash_entry != NULL)
6154     free (last_hash_entry);
6155   if (lst_syms != NULL)
6156     free (lst_syms);
6157   if (strings != NULL)
6158     free (strings);
6159
6160   return FALSE;
6161 }
6162
6163 /* Write out the LST for the archive.
6164
6165    You'll never believe this is really how armaps are handled in SOM...  */
6166
6167 static bfd_boolean
6168 som_write_armap (abfd, elength, map, orl_count, stridx)
6169      bfd *abfd;
6170      unsigned int elength;
6171      struct orl *map ATTRIBUTE_UNUSED;
6172      unsigned int orl_count ATTRIBUTE_UNUSED;
6173      int stridx ATTRIBUTE_UNUSED;
6174 {
6175   bfd *curr_bfd;
6176   struct stat statbuf;
6177   unsigned int i, lst_size, nsyms, stringsize;
6178   struct ar_hdr hdr;
6179   struct lst_header lst;
6180   int *p;
6181   bfd_size_type amt;
6182
6183   /* We'll use this for the archive's date and mode later.  */
6184   if (stat (abfd->filename, &statbuf) != 0)
6185     {
6186       bfd_set_error (bfd_error_system_call);
6187       return FALSE;
6188     }
6189   /* Fudge factor.  */
6190   bfd_ardata (abfd)->armap_timestamp = statbuf.st_mtime + 60;
6191
6192   /* Account for the lst header first.  */
6193   lst_size = sizeof (struct lst_header);
6194
6195   /* Start building the LST header.  */
6196   /* FIXME:  Do we need to examine each element to determine the
6197      largest id number?  */
6198   lst.system_id = CPU_PA_RISC1_0;
6199   lst.a_magic = LIBMAGIC;
6200   lst.version_id = VERSION_ID;
6201   lst.file_time.secs = 0;
6202   lst.file_time.nanosecs = 0;
6203
6204   lst.hash_loc = lst_size;
6205   lst.hash_size = SOM_LST_HASH_SIZE;
6206
6207   /* Hash table is a SOM_LST_HASH_SIZE 32bit offsets.  */
6208   lst_size += 4 * SOM_LST_HASH_SIZE;
6209
6210   /* We need to count the number of SOMs in this archive.  */
6211   curr_bfd = abfd->archive_head;
6212   lst.module_count = 0;
6213   while (curr_bfd != NULL)
6214     {
6215       /* Only true SOM objects count.  */
6216       if (curr_bfd->format == bfd_object
6217           && curr_bfd->xvec->flavour == bfd_target_som_flavour)
6218         lst.module_count++;
6219       curr_bfd = curr_bfd->next;
6220     }
6221   lst.module_limit = lst.module_count;
6222   lst.dir_loc = lst_size;
6223   lst_size += sizeof (struct som_entry) * lst.module_count;
6224
6225   /* We don't support import/export tables, auxiliary headers,
6226      or free lists yet.  Make the linker work a little harder
6227      to make our life easier.  */
6228
6229   lst.export_loc = 0;
6230   lst.export_count = 0;
6231   lst.import_loc = 0;
6232   lst.aux_loc = 0;
6233   lst.aux_size = 0;
6234
6235   /* Count how many symbols we will have on the hash chains and the
6236      size of the associated string table.  */
6237   if (! som_bfd_prep_for_ar_write (abfd, &nsyms, &stringsize))
6238     return FALSE;
6239
6240   lst_size += sizeof (struct lst_symbol_record) * nsyms;
6241
6242   /* For the string table.  One day we might actually use this info
6243      to avoid small seeks/reads when reading archives.  */
6244   lst.string_loc = lst_size;
6245   lst.string_size = stringsize;
6246   lst_size += stringsize;
6247
6248   /* SOM ABI says this must be zero.  */
6249   lst.free_list = 0;
6250   lst.file_end = lst_size;
6251
6252   /* Compute the checksum.  Must happen after the entire lst header
6253      has filled in.  */
6254   p = (int *) &lst;
6255   lst.checksum = 0;
6256   for (i = 0; i < sizeof (struct lst_header) / sizeof (int) - 1; i++)
6257     lst.checksum ^= *p++;
6258
6259   sprintf (hdr.ar_name, "/               ");
6260   sprintf (hdr.ar_date, "%ld", bfd_ardata (abfd)->armap_timestamp);
6261   sprintf (hdr.ar_uid, "%ld", (long) getuid ());
6262   sprintf (hdr.ar_gid, "%ld", (long) getgid ());
6263   sprintf (hdr.ar_mode, "%-8o", (unsigned int) statbuf.st_mode);
6264   sprintf (hdr.ar_size, "%-10d", (int) lst_size);
6265   hdr.ar_fmag[0] = '`';
6266   hdr.ar_fmag[1] = '\012';
6267
6268   /* Turn any nulls into spaces.  */
6269   for (i = 0; i < sizeof (struct ar_hdr); i++)
6270     if (((char *) (&hdr))[i] == '\0')
6271       (((char *) (&hdr))[i]) = ' ';
6272
6273   /* Scribble out the ar header.  */
6274   amt = sizeof (struct ar_hdr);
6275   if (bfd_bwrite ((PTR) &hdr, amt, abfd) != amt)
6276     return FALSE;
6277
6278   /* Now scribble out the lst header.  */
6279   amt = sizeof (struct lst_header);
6280   if (bfd_bwrite ((PTR) &lst, amt, abfd) != amt)
6281     return FALSE;
6282
6283   /* Build and write the armap.  */
6284   if (!som_bfd_ar_write_symbol_stuff (abfd, nsyms, stringsize, lst, elength))
6285     return FALSE;
6286
6287   /* Done.  */
6288   return TRUE;
6289 }
6290
6291 /* Free all information we have cached for this BFD.  We can always
6292    read it again later if we need it.  */
6293
6294 static bfd_boolean
6295 som_bfd_free_cached_info (abfd)
6296      bfd *abfd;
6297 {
6298   asection *o;
6299
6300   if (bfd_get_format (abfd) != bfd_object)
6301     return TRUE;
6302
6303 #define FREE(x) if (x != NULL) { free (x); x = NULL; }
6304   /* Free the native string and symbol tables.  */
6305   FREE (obj_som_symtab (abfd));
6306   FREE (obj_som_stringtab (abfd));
6307   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
6308     {
6309       /* Free the native relocations.  */
6310       o->reloc_count = (unsigned) -1;
6311       FREE (som_section_data (o)->reloc_stream);
6312       /* Free the generic relocations.  */
6313       FREE (o->relocation);
6314     }
6315 #undef FREE
6316
6317   return TRUE;
6318 }
6319
6320 /* End of miscellaneous support functions.  */
6321
6322 /* Linker support functions.  */
6323
6324 static bfd_boolean
6325 som_bfd_link_split_section (abfd, sec)
6326      bfd *abfd ATTRIBUTE_UNUSED;
6327      asection *sec;
6328 {
6329   return (som_is_subspace (sec) && sec->_raw_size > 240000);
6330 }
6331
6332 #define som_close_and_cleanup           som_bfd_free_cached_info
6333
6334 #define som_read_ar_hdr                 _bfd_generic_read_ar_hdr
6335 #define som_openr_next_archived_file    bfd_generic_openr_next_archived_file
6336 #define som_get_elt_at_index            _bfd_generic_get_elt_at_index
6337 #define som_generic_stat_arch_elt       bfd_generic_stat_arch_elt
6338 #define som_truncate_arname             bfd_bsd_truncate_arname
6339 #define som_slurp_extended_name_table   _bfd_slurp_extended_name_table
6340 #define som_construct_extended_name_table \
6341   _bfd_archive_coff_construct_extended_name_table
6342 #define som_update_armap_timestamp      bfd_true
6343 #define som_bfd_print_private_bfd_data  _bfd_generic_bfd_print_private_bfd_data
6344
6345 #define som_get_lineno                  _bfd_nosymbols_get_lineno
6346 #define som_bfd_make_debug_symbol       _bfd_nosymbols_bfd_make_debug_symbol
6347 #define som_read_minisymbols            _bfd_generic_read_minisymbols
6348 #define som_minisymbol_to_symbol        _bfd_generic_minisymbol_to_symbol
6349 #define som_get_section_contents_in_window \
6350   _bfd_generic_get_section_contents_in_window
6351
6352 #define som_bfd_get_relocated_section_contents \
6353  bfd_generic_get_relocated_section_contents
6354 #define som_bfd_relax_section bfd_generic_relax_section
6355 #define som_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
6356 #define som_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
6357 #define som_bfd_link_add_symbols _bfd_generic_link_add_symbols
6358 #define som_bfd_link_just_syms _bfd_generic_link_just_syms
6359 #define som_bfd_final_link _bfd_generic_final_link
6360
6361 #define som_bfd_gc_sections             bfd_generic_gc_sections
6362 #define som_bfd_merge_sections          bfd_generic_merge_sections
6363 #define som_bfd_discard_group           bfd_generic_discard_group
6364
6365 const bfd_target som_vec = {
6366   "som",                        /* name */
6367   bfd_target_som_flavour,
6368   BFD_ENDIAN_BIG,               /* target byte order */
6369   BFD_ENDIAN_BIG,               /* target headers byte order */
6370   (HAS_RELOC | EXEC_P |         /* object flags */
6371    HAS_LINENO | HAS_DEBUG |
6372    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | DYNAMIC),
6373   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
6374    | SEC_ALLOC | SEC_LOAD | SEC_RELOC),         /* section flags */
6375
6376 /* leading_symbol_char: is the first char of a user symbol
6377    predictable, and if so what is it.  */
6378   0,
6379   '/',                          /* ar_pad_char */
6380   14,                           /* ar_max_namelen */
6381   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
6382   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
6383   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
6384   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
6385   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
6386   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
6387   {_bfd_dummy_target,
6388    som_object_p,                /* bfd_check_format */
6389    bfd_generic_archive_p,
6390    _bfd_dummy_target
6391   },
6392   {
6393     bfd_false,
6394     som_mkobject,
6395     _bfd_generic_mkarchive,
6396     bfd_false
6397   },
6398   {
6399     bfd_false,
6400     som_write_object_contents,
6401     _bfd_write_archive_contents,
6402     bfd_false,
6403   },
6404 #undef som
6405
6406   BFD_JUMP_TABLE_GENERIC (som),
6407   BFD_JUMP_TABLE_COPY (som),
6408   BFD_JUMP_TABLE_CORE (_bfd_nocore),
6409   BFD_JUMP_TABLE_ARCHIVE (som),
6410   BFD_JUMP_TABLE_SYMBOLS (som),
6411   BFD_JUMP_TABLE_RELOCS (som),
6412   BFD_JUMP_TABLE_WRITE (som),
6413   BFD_JUMP_TABLE_LINK (som),
6414   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
6415
6416   NULL,
6417
6418   (PTR) 0
6419 };
6420
6421 #endif /* HOST_HPPAHPUX || HOST_HPPABSD || HOST_HPPAOSF */