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