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