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