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