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