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