Coverage Report

Created: 2026-03-10 08:46

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