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