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