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