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