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