Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/coff-rs6000.c
Line
Count
Source
1
/* BFD back-end for IBM RS/6000 "XCOFF" files.
2
   Copyright (C) 1990-2026 Free Software Foundation, Inc.
3
   Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
4
   Archive support from Damon A. Permezel.
5
   Contributed by IBM Corporation and Cygnus Support.
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,
22
   MA 02110-1301, USA.  */
23
24
#include "sysdep.h"
25
#include "libiberty.h"
26
#include "bfd.h"
27
#include "bfdlink.h"
28
#include "libbfd.h"
29
#include "coff/internal.h"
30
#include "coff/xcoff.h"
31
#include "coff/rs6000.h"
32
#include "libcoff.h"
33
#include "libxcoff.h"
34
35
extern bool _bfd_xcoff_mkobject (bfd *);
36
extern bool _bfd_xcoff_copy_private_bfd_data (bfd *, bfd *);
37
extern bool _bfd_xcoff_is_local_label_name (bfd *, const char *);
38
extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
39
  (bfd *, bfd_reloc_code_real_type);
40
extern bool _bfd_xcoff_slurp_armap (bfd *);
41
extern bfd_cleanup _bfd_xcoff_archive_p (bfd *);
42
extern void * _bfd_xcoff_read_ar_hdr (bfd *);
43
extern bfd *_bfd_xcoff_openr_next_archived_file (bfd *, bfd *);
44
extern int _bfd_xcoff_stat_arch_elt (bfd *, struct stat *);
45
extern bool _bfd_xcoff_write_armap
46
  (bfd *, unsigned int, struct orl *, unsigned int, int);
47
extern bool _bfd_xcoff_write_archive_contents (bfd *);
48
extern int _bfd_xcoff_sizeof_headers (bfd *, struct bfd_link_info *);
49
extern void _bfd_xcoff_swap_sym_in (bfd *, void *, void *);
50
extern unsigned int _bfd_xcoff_swap_sym_out (bfd *, void *, void *);
51
extern void _bfd_xcoff_swap_aux_in (bfd *, void *, int, int, int, int, void *);
52
extern unsigned int _bfd_xcoff_swap_aux_out
53
  (bfd *, void *, int, int, int, int, void *);
54
static void xcoff_swap_reloc_in (bfd *, void *, void *);
55
static unsigned int xcoff_swap_reloc_out (bfd *, void *, void *);
56
57
/* Forward declare xcoff_rtype2howto for coffcode.h macro.  */
58
void xcoff_rtype2howto (arelent *, struct internal_reloc *);
59
60
/* coffcode.h needs these to be defined.  */
61
#define RS6000COFF_C 1
62
63
#define SELECT_RELOC(internal, howto)         \
64
0
  {                 \
65
0
    internal.r_type = howto->type;          \
66
0
    internal.r_size =             \
67
0
      ((howto->complain_on_overflow == complain_overflow_signed    \
68
0
  ? 0x80                \
69
0
  : 0)               \
70
0
       | (howto->bitsize - 1));           \
71
0
  }
72
73
195k
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
74
#define COFF_LONG_FILENAMES
75
#define NO_COFF_SYMBOLS
76
168
#define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
77
1.87k
#define coff_mkobject _bfd_xcoff_mkobject
78
#define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
79
#ifdef AIX_CORE
80
extern bfd_cleanup rs6000coff_core_p (bfd *abfd);
81
extern bool rs6000coff_core_file_matches_executable_p
82
  (bfd *cbfd, bfd *ebfd);
83
extern char *rs6000coff_core_file_failing_command (bfd *abfd);
84
extern int rs6000coff_core_file_failing_signal (bfd *abfd);
85
#define CORE_FILE_P rs6000coff_core_p
86
#define coff_core_file_failing_command \
87
  rs6000coff_core_file_failing_command
88
#define coff_core_file_failing_signal \
89
  rs6000coff_core_file_failing_signal
90
#define coff_core_file_matches_executable_p \
91
  rs6000coff_core_file_matches_executable_p
92
#define coff_core_file_pid \
93
  _bfd_nocore_core_file_pid
94
#else
95
#define CORE_FILE_P _bfd_dummy_target
96
#define coff_core_file_failing_command \
97
  _bfd_nocore_core_file_failing_command
98
#define coff_core_file_failing_signal \
99
  _bfd_nocore_core_file_failing_signal
100
#define coff_core_file_matches_executable_p \
101
  _bfd_nocore_core_file_matches_executable_p
102
#define coff_core_file_pid \
103
  _bfd_nocore_core_file_pid
104
#endif
105
#define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
106
#define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
107
#define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
108
#define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
109
#define coff_swap_reloc_in xcoff_swap_reloc_in
110
0
#define coff_swap_reloc_out xcoff_swap_reloc_out
111
#define NO_COFF_RELOCS
112
113
#ifndef bfd_pe_print_pdata
114
#define bfd_pe_print_pdata  NULL
115
#endif
116
117
#include "coffcode.h"
118
119
/* The main body of code is in coffcode.h.  */
120
121
static const char *normalize_filename (bfd *);
122
static bool xcoff_write_armap_old
123
  (bfd *, unsigned int, struct orl *, unsigned int, int);
124
static bool xcoff_write_armap_big
125
  (bfd *, unsigned int, struct orl *, unsigned int, int);
126
static bool xcoff_write_archive_contents_old (bfd *);
127
static bool xcoff_write_archive_contents_big (bfd *);
128
static void xcoff_swap_ldhdr_in (bfd *, const void *, struct internal_ldhdr *);
129
static void xcoff_swap_ldhdr_out (bfd *, const struct internal_ldhdr *, void *);
130
static void xcoff_swap_ldsym_in (bfd *, const void *, struct internal_ldsym *);
131
static void xcoff_swap_ldsym_out (bfd *, const struct internal_ldsym *, void *);
132
static void xcoff_swap_ldrel_in (bfd *, const void *, struct internal_ldrel *);
133
static void xcoff_swap_ldrel_out (bfd *, const struct internal_ldrel *, void *);
134
static bool xcoff_ppc_relocate_section
135
  (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
136
   struct internal_reloc *, struct internal_syment *, asection **);
137
static bool _bfd_xcoff_put_ldsymbol_name
138
  (bfd *, struct xcoff_loader_info *, struct internal_ldsym *, const char *);
139
static asection *xcoff_create_csect_from_smclas
140
  (bfd *, union internal_auxent *, const char *);
141
static bool xcoff_is_lineno_count_overflow (bfd *, bfd_vma);
142
static bool xcoff_is_reloc_count_overflow (bfd *, bfd_vma);
143
static bfd_vma xcoff_loader_symbol_offset (bfd *, struct internal_ldhdr *);
144
static bfd_vma xcoff_loader_reloc_offset (bfd *, struct internal_ldhdr *);
145
static bool xcoff_generate_rtinit
146
  (bfd *, const char *, const char *, bool);
147
static bool do_pad (bfd *, unsigned int);
148
static bool do_copy (bfd *, bfd *);
149
150
/* Relocation functions */
151
static xcoff_reloc_function xcoff_reloc_type_br;
152
153
static xcoff_complain_function xcoff_complain_overflow_dont_func;
154
static xcoff_complain_function xcoff_complain_overflow_bitfield_func;
155
static xcoff_complain_function xcoff_complain_overflow_signed_func;
156
static xcoff_complain_function xcoff_complain_overflow_unsigned_func;
157
158
xcoff_reloc_function *const xcoff_calculate_relocation[] =
159
{
160
  xcoff_reloc_type_pos,  /* R_POS   (0x00) */
161
  xcoff_reloc_type_neg,  /* R_NEG   (0x01) */
162
  xcoff_reloc_type_rel,  /* R_REL   (0x02) */
163
  xcoff_reloc_type_toc,  /* R_TOC   (0x03) */
164
  xcoff_reloc_type_toc,  /* R_TRL   (0x04) */
165
  xcoff_reloc_type_toc,  /* R_GL    (0x05) */
166
  xcoff_reloc_type_toc,  /* R_TCL   (0x06) */
167
  xcoff_reloc_type_fail, /*         (0x07) */
168
  xcoff_reloc_type_ba,   /* R_BA    (0x08) */
169
  xcoff_reloc_type_fail, /*         (0x09) */
170
  xcoff_reloc_type_br,   /* R_BR    (0x0a) */
171
  xcoff_reloc_type_fail, /*         (0x0b) */
172
  xcoff_reloc_type_pos,  /* R_RL    (0x0c) */
173
  xcoff_reloc_type_pos,  /* R_RLA   (0x0d) */
174
  xcoff_reloc_type_fail, /*         (0x0e) */
175
  xcoff_reloc_type_noop, /* R_REF   (0x0f) */
176
  xcoff_reloc_type_fail, /*         (0x10) */
177
  xcoff_reloc_type_fail, /*         (0x11) */
178
  xcoff_reloc_type_fail, /*         (0x12) */
179
  xcoff_reloc_type_toc,  /* R_TRLA  (0x13) */
180
  xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
181
  xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
182
  xcoff_reloc_type_ba,   /* R_CAI   (0x16) */
183
  xcoff_reloc_type_crel, /* R_CREL  (0x17) */
184
  xcoff_reloc_type_ba,   /* R_RBA   (0x18) */
185
  xcoff_reloc_type_ba,   /* R_RBAC  (0x19) */
186
  xcoff_reloc_type_br,   /* R_RBR   (0x1a) */
187
  xcoff_reloc_type_ba,   /* R_RBRC  (0x1b) */
188
  xcoff_reloc_type_fail, /*           (0x1c) */
189
  xcoff_reloc_type_fail, /*           (0x1d) */
190
  xcoff_reloc_type_fail, /*           (0x1e) */
191
  xcoff_reloc_type_fail, /*           (0x1f) */
192
  xcoff_reloc_type_tls,  /* R_TLS     (0x20) */
193
  xcoff_reloc_type_tls,  /* R_TLS_IE  (0x21) */
194
  xcoff_reloc_type_tls,  /* R_TLS_LD  (0x22) */
195
  xcoff_reloc_type_tls,  /* R_TLS_LE  (0x23) */
196
  xcoff_reloc_type_tls,  /* R_TLSM    (0x24) */
197
  xcoff_reloc_type_tls,  /* R_TLSML   (0x25) */
198
  xcoff_reloc_type_fail, /*           (0x26) */
199
  xcoff_reloc_type_fail, /*           (0x27) */
200
  xcoff_reloc_type_fail, /*           (0x28) */
201
  xcoff_reloc_type_fail, /*           (0x29) */
202
  xcoff_reloc_type_fail, /*           (0x2a) */
203
  xcoff_reloc_type_fail, /*           (0x2b) */
204
  xcoff_reloc_type_fail, /*           (0x2c) */
205
  xcoff_reloc_type_fail, /*           (0x2d) */
206
  xcoff_reloc_type_fail, /*           (0x2e) */
207
  xcoff_reloc_type_fail, /*           (0x2f) */
208
  xcoff_reloc_type_toc,  /* R_TOCU    (0x30) */
209
  xcoff_reloc_type_toc,  /* R_TOCL    (0x31) */
210
};
211
212
xcoff_complain_function *const xcoff_complain_overflow[] =
213
{
214
  xcoff_complain_overflow_dont_func,
215
  xcoff_complain_overflow_bitfield_func,
216
  xcoff_complain_overflow_signed_func,
217
  xcoff_complain_overflow_unsigned_func,
218
};
219
220
/* Information about one member of an archive.  */
221
struct member_layout
222
{
223
  /* The archive member that this structure describes.  */
224
  bfd *member;
225
226
  /* The number of bytes of padding that must be inserted before the
227
     start of the member in order to ensure that the section contents
228
     are correctly aligned.  */
229
  unsigned int leading_padding;
230
231
  /* The offset of MEMBER from the start of the archive (i.e. the end
232
     of the leading padding).  */
233
  file_ptr offset;
234
235
  /* The normalized name of MEMBER.  */
236
  const char *name;
237
238
  /* The length of NAME, without padding.  */
239
  bfd_size_type namlen;
240
241
  /* The length of NAME, with padding.  */
242
  bfd_size_type padded_namlen;
243
244
  /* The size of MEMBER's header, including the name and magic sequence.  */
245
  bfd_size_type header_size;
246
247
  /* The size of the MEMBER's contents.  */
248
  bfd_size_type contents_size;
249
250
  /* The number of bytes of padding that must be inserted after MEMBER
251
     in order to preserve even alignment.  */
252
  bfd_size_type trailing_padding;
253
};
254
255
/* A structure used for iterating over the members of an archive.  */
256
struct archive_iterator
257
{
258
  /* The archive itself.  */
259
  bfd *archive;
260
261
  /* Information about the current archive member.  */
262
  struct member_layout current;
263
264
  /* Information about the next archive member.  MEMBER is null if there
265
     are no more archive members, in which case OFFSET is the offset of
266
     the first unused byte.  */
267
  struct member_layout next;
268
};
269
270
/* Initialize INFO so that it describes member MEMBER of archive ARCHIVE.
271
   OFFSET is the even-padded offset of MEMBER, not including any leading
272
   padding needed for section alignment.  */
273
274
static void
275
member_layout_init (struct member_layout *info, bfd *archive,
276
        bfd *member, file_ptr offset)
277
94
{
278
94
  info->member = member;
279
94
  info->leading_padding = 0;
280
94
  if (member)
281
20
    {
282
20
      info->name = normalize_filename (member);
283
20
      info->namlen = strlen (info->name);
284
20
      info->padded_namlen = info->namlen + (info->namlen & 1);
285
20
      if (xcoff_big_format_p (archive))
286
20
  info->header_size = SIZEOF_AR_HDR_BIG;
287
0
      else
288
0
  info->header_size = SIZEOF_AR_HDR;
289
20
      info->header_size += info->padded_namlen + SXCOFFARFMAG;
290
20
      info->contents_size = arelt_size (member);
291
20
      info->trailing_padding = info->contents_size & 1;
292
293
20
      if (bfd_check_format (member, bfd_object)
294
1
    && bfd_get_flavour (member) == bfd_target_xcoff_flavour
295
0
    && (member->flags & DYNAMIC) != 0)
296
0
  info->leading_padding
297
0
    = (-(offset + info->header_size)
298
0
       & ((1 << bfd_xcoff_text_align_power (member)) - 1));
299
20
    }
300
94
  info->offset = offset + info->leading_padding;
301
94
}
302
303
/* Set up ITERATOR to iterate through archive ARCHIVE.  */
304
305
static void
306
archive_iterator_begin (struct archive_iterator *iterator,
307
      bfd *archive)
308
74
{
309
74
  iterator->archive = archive;
310
74
  member_layout_init (&iterator->next, archive, archive->archive_head,
311
74
          xcoff_big_format_p (archive)
312
74
          ? SIZEOF_AR_FILE_HDR_BIG
313
74
          : SIZEOF_AR_FILE_HDR);
314
74
}
315
316
/* Make ITERATOR visit the first unvisited archive member.  Return true
317
   on success; return false if all members have been visited.  */
318
319
static bool
320
archive_iterator_next (struct archive_iterator *iterator)
321
94
{
322
94
  if (!iterator->next.member)
323
74
    return false;
324
325
20
  iterator->current = iterator->next;
326
20
  member_layout_init (&iterator->next, iterator->archive,
327
20
          iterator->current.member->archive_next,
328
20
          iterator->current.offset
329
20
          + iterator->current.header_size
330
20
          + iterator->current.contents_size
331
20
          + iterator->current.trailing_padding);
332
20
  return true;
333
94
}
334
335
/* We use our own tdata type.  Its first field is the COFF tdata type,
336
   so the COFF routines are compatible.  */
337
338
bool
339
_bfd_xcoff_mkobject (bfd *abfd)
340
3.85k
{
341
3.85k
  coff_data_type *coff;
342
3.85k
  size_t amt = sizeof (struct xcoff_tdata);
343
344
3.85k
  abfd->tdata.xcoff_obj_data = (struct xcoff_tdata *) bfd_zalloc (abfd, amt);
345
3.85k
  if (abfd->tdata.xcoff_obj_data == NULL)
346
0
    return false;
347
3.85k
  coff = coff_data (abfd);
348
3.85k
  coff->symbols = (coff_symbol_type *) NULL;
349
3.85k
  coff->conversion_table = (unsigned int *) NULL;
350
3.85k
  coff->raw_syments = (struct coff_ptr_struct *) NULL;
351
3.85k
  coff->relocbase = 0;
352
353
3.85k
  xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
354
355
  /* We set cputype to -1 to indicate that it has not been
356
     initialized.  */
357
3.85k
  xcoff_data (abfd)->cputype = -1;
358
359
3.85k
  xcoff_data (abfd)->csects = NULL;
360
3.85k
  xcoff_data (abfd)->debug_indices = NULL;
361
362
  /* text section alignment is different than the default */
363
3.85k
  bfd_xcoff_text_align_power (abfd) = 2;
364
365
3.85k
  return true;
366
3.85k
}
367
368
/* Copy XCOFF data from one BFD to another.  */
369
370
bool
371
_bfd_xcoff_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
372
13
{
373
13
  struct xcoff_tdata *ix, *ox;
374
13
  asection *sec;
375
376
13
  if (ibfd->xvec != obfd->xvec)
377
0
    return true;
378
13
  ix = xcoff_data (ibfd);
379
13
  ox = xcoff_data (obfd);
380
13
  ox->full_aouthdr = ix->full_aouthdr;
381
13
  ox->toc = ix->toc;
382
13
  if (ix->sntoc == 0)
383
13
    ox->sntoc = 0;
384
0
  else
385
0
    {
386
0
      sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
387
0
      if (sec == NULL || sec->output_section == NULL)
388
0
  ox->sntoc = 0;
389
0
      else
390
0
  ox->sntoc = sec->output_section->target_index;
391
0
    }
392
13
  if (ix->snentry == 0)
393
13
    ox->snentry = 0;
394
0
  else
395
0
    {
396
0
      sec = coff_section_from_bfd_index (ibfd, ix->snentry);
397
0
      if (sec == NULL || sec->output_section == NULL)
398
0
  ox->snentry = 0;
399
0
      else
400
0
  ox->snentry = sec->output_section->target_index;
401
0
    }
402
13
  bfd_xcoff_text_align_power (obfd) = bfd_xcoff_text_align_power (ibfd);
403
13
  bfd_xcoff_data_align_power (obfd) = bfd_xcoff_data_align_power (ibfd);
404
13
  ox->modtype = ix->modtype;
405
13
  ox->cputype = ix->cputype;
406
13
  ox->maxdata = ix->maxdata;
407
13
  ox->maxstack = ix->maxstack;
408
13
  return true;
409
13
}
410
411
/* I don't think XCOFF really has a notion of local labels based on
412
   name.  This will mean that ld -X doesn't actually strip anything.
413
   The AIX native linker does not have a -X option, and it ignores the
414
   -x option.  */
415
416
bool
417
_bfd_xcoff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
418
        const char *name ATTRIBUTE_UNUSED)
419
0
{
420
0
  return false;
421
0
}
422

423
void
424
_bfd_xcoff_swap_sym_in (bfd *abfd, void * ext1, void * in1)
425
22.0k
{
426
22.0k
  SYMENT *ext = (SYMENT *)ext1;
427
22.0k
  struct internal_syment * in = (struct internal_syment *)in1;
428
429
22.0k
  if (ext->e.e_name[0] != 0)
430
9.28k
    {
431
9.28k
      memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
432
9.28k
    }
433
12.8k
  else
434
12.8k
    {
435
12.8k
      in->_n._n_n._n_zeroes = 0;
436
12.8k
      in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
437
12.8k
    }
438
439
22.0k
  in->n_value = H_GET_32 (abfd, ext->e_value);
440
22.0k
  in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
441
22.0k
  in->n_type = H_GET_16 (abfd, ext->e_type);
442
22.0k
  in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
443
22.0k
  in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
444
22.0k
}
445
446
unsigned int
447
_bfd_xcoff_swap_sym_out (bfd *abfd, void * inp, void * extp)
448
0
{
449
0
  struct internal_syment *in = (struct internal_syment *)inp;
450
0
  SYMENT *ext =(SYMENT *)extp;
451
452
0
  if (in->_n._n_name[0] != 0)
453
0
    {
454
0
      memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
455
0
    }
456
0
  else
457
0
    {
458
0
      H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
459
0
      H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
460
0
    }
461
462
0
  H_PUT_32 (abfd, in->n_value, ext->e_value);
463
0
  H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
464
0
  H_PUT_16 (abfd, in->n_type, ext->e_type);
465
0
  H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
466
0
  H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
467
0
  return bfd_coff_symesz (abfd);
468
0
}
469
470
void
471
_bfd_xcoff_swap_aux_in (bfd *abfd, void * ext1, int type ATTRIBUTE_UNUSED,
472
      int in_class, int indx, int numaux, void * in1)
473
71.4k
{
474
71.4k
  AUXENT * ext = (AUXENT *)ext1;
475
71.4k
  union internal_auxent *in = (union internal_auxent *)in1;
476
477
71.4k
  switch (in_class)
478
71.4k
    {
479
46.9k
    default:
480
46.9k
      _bfd_error_handler
481
  /* xgettext: c-format */
482
46.9k
  (_("%pB: unsupported swap_aux_in for storage class %#x"),
483
46.9k
   abfd, (unsigned int) in_class);
484
46.9k
      bfd_set_error (bfd_error_bad_value);
485
46.9k
      break;
486
487
4.17k
    case C_FILE:
488
4.17k
      if (ext->x_file.x_n.x_fname[0] == 0)
489
1.43k
  {
490
1.43k
    in->x_file.x_n.x_n.x_zeroes = 0;
491
1.43k
    in->x_file.x_n.x_n.x_offset =
492
1.43k
      H_GET_32 (abfd, ext->x_file.x_n.x_n.x_offset);
493
1.43k
  }
494
2.74k
      else
495
2.74k
  memcpy (in->x_file.x_n.x_fname, ext->x_file.x_n.x_fname, FILNMLEN);
496
4.17k
      in->x_file.x_ftype = H_GET_8 (abfd, ext->x_file.x_ftype);
497
4.17k
      break;
498
499
      /* RS/6000 "csect" auxents.
500
         There is always a CSECT auxiliary entry. But functions can
501
         have FCN ones too. In this case, CSECT is always the last
502
         one. */
503
6.08k
    case C_EXT:
504
9.02k
    case C_AIX_WEAKEXT:
505
11.0k
    case C_HIDEXT:
506
11.0k
      if (indx + 1 == numaux)
507
2.52k
  {
508
2.52k
    in->x_csect.x_scnlen.u64 = H_GET_32 (abfd, ext->x_csect.x_scnlen);
509
2.52k
    in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
510
2.52k
    in->x_csect.x_snhash   = H_GET_16 (abfd, ext->x_csect.x_snhash);
511
    /* We don't have to hack bitfields in x_smtyp because it's
512
       defined by shifts-and-ands, which are equivalent on all
513
       byte orders.  */
514
2.52k
    in->x_csect.x_smtyp    = H_GET_8 (abfd, ext->x_csect.x_smtyp);
515
2.52k
    in->x_csect.x_smclas   = H_GET_8 (abfd, ext->x_csect.x_smclas);
516
2.52k
    in->x_csect.x_stab     = H_GET_32 (abfd, ext->x_csect.x_stab);
517
2.52k
    in->x_csect.x_snstab   = H_GET_16 (abfd, ext->x_csect.x_snstab);
518
2.52k
  }
519
8.52k
      else
520
8.52k
  {
521
    /* x_exptr isn't supported.  */
522
8.52k
    in->x_sym.x_misc.x_fsize
523
8.52k
      = H_GET_32 (abfd, ext->x_fcn.x_fsize);
524
8.52k
    in->x_sym.x_fcnary.x_fcn.x_lnnoptr
525
8.52k
      = H_GET_32 (abfd, ext->x_fcn.x_lnnoptr);
526
8.52k
    in->x_sym.x_fcnary.x_fcn.x_endndx.u32
527
8.52k
      = H_GET_32 (abfd, ext->x_fcn.x_endndx);
528
8.52k
  }
529
11.0k
      break;
530
531
2.43k
    case C_STAT:
532
2.43k
      in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
533
2.43k
      in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
534
2.43k
      in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
535
      /* PE defines some extra fields; we zero them out for
536
   safety.  */
537
2.43k
      in->x_scn.x_checksum = 0;
538
2.43k
      in->x_scn.x_associated = 0;
539
2.43k
      in->x_scn.x_comdat = 0;
540
2.43k
      break;
541
542
1.07k
    case C_BLOCK:
543
2.48k
    case C_FCN:
544
2.48k
      in->x_sym.x_misc.x_lnsz.x_lnno
545
2.48k
  = H_GET_32 (abfd, ext->x_sym.x_lnno);
546
2.48k
      break;
547
548
4.37k
    case C_DWARF:
549
4.37k
      in->x_sect.x_scnlen = H_GET_32 (abfd, ext->x_sect.x_scnlen);
550
4.37k
      in->x_sect.x_nreloc = H_GET_32 (abfd, ext->x_sect.x_nreloc);
551
4.37k
      break;
552
553
71.4k
    }
554
71.4k
}
555
556
unsigned int
557
_bfd_xcoff_swap_aux_out (bfd *abfd, void * inp, int type ATTRIBUTE_UNUSED,
558
       int in_class, int indx, int numaux, void * extp)
559
0
{
560
0
  union internal_auxent *in = (union internal_auxent *)inp;
561
0
  AUXENT *ext = (AUXENT *)extp;
562
563
0
  memset (ext, 0, bfd_coff_auxesz (abfd));
564
0
  switch (in_class)
565
0
    {
566
0
    default:
567
0
      _bfd_error_handler
568
  /* xgettext: c-format */
569
0
  (_("%pB: unsupported swap_aux_out for storage class %#x"),
570
0
   abfd, (unsigned int) in_class);
571
0
      bfd_set_error (bfd_error_bad_value);
572
0
      break;
573
574
0
    case C_FILE:
575
0
      if (in->x_file.x_n.x_fname[0] == 0)
576
0
  {
577
0
    H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
578
0
    H_PUT_32 (abfd, in->x_file.x_n.x_n.x_offset,
579
0
        ext->x_file.x_n.x_n.x_offset);
580
0
  }
581
0
      else
582
0
  memcpy (ext->x_file.x_n.x_fname, in->x_file.x_n.x_fname, FILNMLEN);
583
0
      H_PUT_8 (abfd, in->x_file.x_ftype, ext->x_file.x_ftype);
584
0
      break;
585
586
      /* RS/6000 "csect" auxents */
587
0
    case C_EXT:
588
0
    case C_AIX_WEAKEXT:
589
0
    case C_HIDEXT:
590
0
      if (indx + 1 == numaux)
591
0
  {
592
0
    H_PUT_32 (abfd, in->x_csect.x_scnlen.u64, ext->x_csect.x_scnlen);
593
0
    H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
594
0
    H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
595
    /* We don't have to hack bitfields in x_smtyp because it's
596
       defined by shifts-and-ands, which are equivalent on all
597
       byte orders.  */
598
0
    H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
599
0
    H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
600
0
    H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
601
0
    H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
602
0
  }
603
0
      else
604
0
  {
605
0
    H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_fcn.x_fsize);
606
0
    H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
607
0
        ext->x_fcn.x_lnnoptr);
608
0
    H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.u32,
609
0
        ext->x_fcn.x_endndx);
610
0
  }
611
0
      break;
612
613
0
    case C_STAT:
614
0
      H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
615
0
      H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
616
0
      H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
617
0
      break;
618
619
0
    case C_BLOCK:
620
0
    case C_FCN:
621
0
      H_PUT_32 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext->x_sym.x_lnno);
622
0
      break;
623
624
0
    case C_DWARF:
625
0
      H_PUT_32 (abfd, in->x_sect.x_scnlen, ext->x_sect.x_scnlen);
626
0
      H_PUT_32 (abfd, in->x_sect.x_nreloc, ext->x_sect.x_nreloc);
627
0
      break;
628
0
    }
629
630
0
  return bfd_coff_auxesz (abfd);
631
0
}
632

633
/* The XCOFF reloc table.
634
   XCOFF relocations aren't defined only by the type field r_type.
635
   The bitsize and whether they are signed or not, are defined by
636
   r_size field.  Thus, it's complicated to create a constant
637
   table reference every possible relocation.
638
   This table contains the "default" relocation and few modified
639
   relocations what were already there.  It's enough when
640
   xcoff_rtype2howto is called.
641
   For relocations from an input bfd to an output bfd, the default
642
   relocation is retrieved and when manually adapted.
643
644
   For now, it seems to be enought.  */
645
646
reloc_howto_type xcoff_howto_table[] =
647
{
648
  /* 0x00: Standard 32 bit relocation.  */
649
  HOWTO (R_POS,     /* type */
650
   0,     /* rightshift */
651
   4,     /* size */
652
   32,      /* bitsize */
653
   false,     /* pc_relative */
654
   0,     /* bitpos */
655
   complain_overflow_bitfield, /* complain_on_overflow */
656
   0,     /* special_function */
657
   "R_POS",   /* name */
658
   true,      /* partial_inplace */
659
   0xffffffff,    /* src_mask */
660
   0xffffffff,    /* dst_mask */
661
   false),    /* pcrel_offset */
662
663
  /* 0x01: 32 bit relocation, but store negative value.  */
664
  HOWTO (R_NEG,     /* type */
665
   0,     /* rightshift */
666
   -4,      /* size */
667
   32,      /* bitsize */
668
   false,     /* pc_relative */
669
   0,     /* bitpos */
670
   complain_overflow_bitfield, /* complain_on_overflow */
671
   0,     /* special_function */
672
   "R_NEG",   /* name */
673
   true,      /* partial_inplace */
674
   0xffffffff,    /* src_mask */
675
   0xffffffff,    /* dst_mask */
676
   false),    /* pcrel_offset */
677
678
  /* 0x02: 32 bit PC relative relocation.  */
679
  HOWTO (R_REL,     /* type */
680
   0,     /* rightshift */
681
   4,     /* size */
682
   32,      /* bitsize */
683
   true,      /* pc_relative */
684
   0,     /* bitpos */
685
   complain_overflow_signed, /* complain_on_overflow */
686
   0,     /* special_function */
687
   "R_REL",   /* name */
688
   true,      /* partial_inplace */
689
   0xffffffff,    /* src_mask */
690
   0xffffffff,    /* dst_mask */
691
   false),    /* pcrel_offset */
692
693
  /* 0x03: 16 bit TOC relative relocation.  */
694
  HOWTO (R_TOC,     /* type */
695
   0,     /* rightshift */
696
   2,     /* size */
697
   16,      /* bitsize */
698
   false,     /* pc_relative */
699
   0,     /* bitpos */
700
   complain_overflow_bitfield, /* complain_on_overflow */
701
   0,     /* special_function */
702
   "R_TOC",   /* name */
703
   true,      /* partial_inplace */
704
   0,     /* src_mask */
705
   0xffff,    /* dst_mask */
706
   false),    /* pcrel_offset */
707
708
  /* 0x04: Same as R_TOC  */
709
  HOWTO (R_TRL,     /* type */
710
   0,     /* rightshift */
711
   2,     /* size */
712
   16,      /* bitsize */
713
   false,     /* pc_relative */
714
   0,     /* bitpos */
715
   complain_overflow_bitfield, /* complain_on_overflow */
716
   0,     /* special_function */
717
   "R_TRL",   /* name */
718
   true,      /* partial_inplace */
719
   0,     /* src_mask */
720
   0xffff,    /* dst_mask */
721
   false),    /* pcrel_offset */
722
723
  /* 0x05: External TOC relative symbol.  */
724
  HOWTO (R_GL,      /* type */
725
   0,     /* rightshift */
726
   2,     /* size */
727
   16,      /* bitsize */
728
   false,     /* pc_relative */
729
   0,     /* bitpos */
730
   complain_overflow_bitfield, /* complain_on_overflow */
731
   0,     /* special_function */
732
   "R_GL",    /* name */
733
   true,      /* partial_inplace */
734
   0,     /* src_mask */
735
   0xffff,    /* dst_mask */
736
   false),    /* pcrel_offset */
737
738
  /* 0x06: Local TOC relative symbol.  */
739
  HOWTO (R_TCL,     /* type */
740
   0,     /* rightshift */
741
   2,     /* size */
742
   16,      /* bitsize */
743
   false,     /* pc_relative */
744
   0,     /* bitpos */
745
   complain_overflow_bitfield, /* complain_on_overflow */
746
   0,     /* special_function */
747
   "R_TCL",   /* name */
748
   true,      /* partial_inplace */
749
   0,     /* src_mask */
750
   0xffff,    /* dst_mask */
751
   false),    /* pcrel_offset */
752
753
  EMPTY_HOWTO (7),
754
755
  /* 0x08: Same as R_RBA.  */
756
  HOWTO (R_BA,      /* type */
757
   0,     /* rightshift */
758
   4,     /* size */
759
   26,      /* bitsize */
760
   false,     /* pc_relative */
761
   0,     /* bitpos */
762
   complain_overflow_bitfield, /* complain_on_overflow */
763
   0,     /* special_function */
764
   "R_BA_26",   /* name */
765
   true,      /* partial_inplace */
766
   0x03fffffc,    /* src_mask */
767
   0x03fffffc,    /* dst_mask */
768
   false),    /* pcrel_offset */
769
770
  EMPTY_HOWTO (9),
771
772
  /* 0x0a: Same as R_RBR.  */
773
  HOWTO (R_BR,      /* type */
774
   0,     /* rightshift */
775
   4,     /* size */
776
   26,      /* bitsize */
777
   true,      /* pc_relative */
778
   0,     /* bitpos */
779
   complain_overflow_signed, /* complain_on_overflow */
780
   0,     /* special_function */
781
   "R_BR",    /* name */
782
   true,      /* partial_inplace */
783
   0x03fffffc,    /* src_mask */
784
   0x03fffffc,    /* dst_mask */
785
   false),    /* pcrel_offset */
786
787
  EMPTY_HOWTO (0xb),
788
789
  /* 0x0c: Same as R_POS.  */
790
  HOWTO (R_RL,      /* type */
791
   0,     /* rightshift */
792
   4,     /* size */
793
   32,      /* bitsize */
794
   false,     /* pc_relative */
795
   0,     /* bitpos */
796
   complain_overflow_bitfield, /* complain_on_overflow */
797
   0,     /* special_function */
798
   "R_RL",    /* name */
799
   true,      /* partial_inplace */
800
   0xffffffff,    /* src_mask */
801
   0xffffffff,    /* dst_mask */
802
   false),    /* pcrel_offset */
803
804
  /* 0x0d: Same as R_POS.  */
805
  HOWTO (R_RLA,     /* type */
806
   0,     /* rightshift */
807
   4,     /* size */
808
   32,      /* bitsize */
809
   false,     /* pc_relative */
810
   0,     /* bitpos */
811
   complain_overflow_bitfield, /* complain_on_overflow */
812
   0,     /* special_function */
813
   "R_RLA",   /* name */
814
   true,      /* partial_inplace */
815
   0xffffffff,    /* src_mask */
816
   0xffffffff,    /* dst_mask */
817
   false),    /* pcrel_offset */
818
819
  EMPTY_HOWTO (0xe),
820
821
  /* 0x0f: Non-relocating reference.  Bitsize is 1 so that r_rsize is 0.  */
822
  HOWTO (R_REF,     /* type */
823
   0,     /* rightshift */
824
   1,     /* size */
825
   1,     /* bitsize */
826
   false,     /* pc_relative */
827
   0,     /* bitpos */
828
   complain_overflow_dont, /* complain_on_overflow */
829
   0,     /* special_function */
830
   "R_REF",   /* name */
831
   false,     /* partial_inplace */
832
   0,     /* src_mask */
833
   0,     /* dst_mask */
834
   false),    /* pcrel_offset */
835
836
  EMPTY_HOWTO (0x10),
837
  EMPTY_HOWTO (0x11),
838
  EMPTY_HOWTO (0x12),
839
840
  /* 0x13: Same as R_TOC.  */
841
  HOWTO (R_TRLA,    /* type */
842
   0,     /* rightshift */
843
   2,     /* size */
844
   16,      /* bitsize */
845
   false,     /* pc_relative */
846
   0,     /* bitpos */
847
   complain_overflow_bitfield, /* complain_on_overflow */
848
   0,     /* special_function */
849
   "R_TRLA",    /* name */
850
   true,      /* partial_inplace */
851
   0,     /* src_mask */
852
   0xffff,    /* dst_mask */
853
   false),    /* pcrel_offset */
854
855
  /* 0x14: Modifiable relative branch.  */
856
  HOWTO (R_RRTBI,   /* type */
857
   1,     /* rightshift */
858
   4,     /* size */
859
   32,      /* bitsize */
860
   false,     /* pc_relative */
861
   0,     /* bitpos */
862
   complain_overflow_bitfield, /* complain_on_overflow */
863
   0,     /* special_function */
864
   "R_RRTBI",   /* name */
865
   true,      /* partial_inplace */
866
   0xffffffff,    /* src_mask */
867
   0xffffffff,    /* dst_mask */
868
   false),    /* pcrel_offset */
869
870
  /* 0x15: Modifiable absolute branch.  */
871
  HOWTO (R_RRTBA,   /* type */
872
   1,     /* rightshift */
873
   4,     /* size */
874
   32,      /* bitsize */
875
   false,     /* pc_relative */
876
   0,     /* bitpos */
877
   complain_overflow_bitfield, /* complain_on_overflow */
878
   0,     /* special_function */
879
   "R_RRTBA",   /* name */
880
   true,      /* partial_inplace */
881
   0xffffffff,    /* src_mask */
882
   0xffffffff,    /* dst_mask */
883
   false),    /* pcrel_offset */
884
885
  /* 0x16: Modifiable call absolute indirect.  */
886
  HOWTO (R_CAI,     /* type */
887
   0,     /* rightshift */
888
   2,     /* size */
889
   16,      /* bitsize */
890
   false,     /* pc_relative */
891
   0,     /* bitpos */
892
   complain_overflow_bitfield, /* complain_on_overflow */
893
   0,     /* special_function */
894
   "R_CAI",   /* name */
895
   true,      /* partial_inplace */
896
   0xffff,    /* src_mask */
897
   0xffff,    /* dst_mask */
898
   false),    /* pcrel_offset */
899
900
  /* 0x17: Modifiable call relative.  */
901
  HOWTO (R_CREL,    /* type */
902
   0,     /* rightshift */
903
   2,     /* size */
904
   16,      /* bitsize */
905
   false,     /* pc_relative */
906
   0,     /* bitpos */
907
   complain_overflow_bitfield, /* complain_on_overflow */
908
   0,     /* special_function */
909
   "R_CREL",    /* name */
910
   true,      /* partial_inplace */
911
   0xffff,    /* src_mask */
912
   0xffff,    /* dst_mask */
913
   false),    /* pcrel_offset */
914
915
  /* 0x18: Modifiable branch absolute.  */
916
  HOWTO (R_RBA,     /* type */
917
   0,     /* rightshift */
918
   4,     /* size */
919
   26,      /* bitsize */
920
   false,     /* pc_relative */
921
   0,     /* bitpos */
922
   complain_overflow_bitfield, /* complain_on_overflow */
923
   0,     /* special_function */
924
   "R_RBA",   /* name */
925
   true,      /* partial_inplace */
926
   0x03fffffc,    /* src_mask */
927
   0x03fffffc,    /* dst_mask */
928
   false),    /* pcrel_offset */
929
930
  /* 0x19: Modifiable branch absolute.  */
931
  HOWTO (R_RBAC,    /* type */
932
   0,     /* rightshift */
933
   4,     /* size */
934
   32,      /* bitsize */
935
   false,     /* pc_relative */
936
   0,     /* bitpos */
937
   complain_overflow_bitfield, /* complain_on_overflow */
938
   0,     /* special_function */
939
   "R_RBAC",    /* name */
940
   true,      /* partial_inplace */
941
   0xffffffff,    /* src_mask */
942
   0xffffffff,    /* dst_mask */
943
   false),    /* pcrel_offset */
944
945
  /* 0x1a: Modifiable branch relative.  */
946
  HOWTO (R_RBR,     /* type */
947
   0,     /* rightshift */
948
   4,     /* size */
949
   26,      /* bitsize */
950
   false,     /* pc_relative */
951
   0,     /* bitpos */
952
   complain_overflow_signed, /* complain_on_overflow */
953
   0,     /* special_function */
954
   "R_RBR_26",    /* name */
955
   true,      /* partial_inplace */
956
   0x03fffffc,    /* src_mask */
957
   0x03fffffc,    /* dst_mask */
958
   false),    /* pcrel_offset */
959
960
  /* 0x1b: Modifiable branch absolute.  */
961
  HOWTO (R_RBRC,    /* type */
962
   0,     /* rightshift */
963
   2,     /* size */
964
   16,      /* bitsize */
965
   false,     /* pc_relative */
966
   0,     /* bitpos */
967
   complain_overflow_bitfield, /* complain_on_overflow */
968
   0,     /* special_function */
969
   "R_RBRC",    /* name */
970
   true,      /* partial_inplace */
971
   0xffff,    /* src_mask */
972
   0xffff,    /* dst_mask */
973
   false),    /* pcrel_offset */
974
975
  /* 0x1c: 16 bit Non modifiable absolute branch.  */
976
  HOWTO (R_BA,      /* type */
977
   0,     /* rightshift */
978
   2,     /* size */
979
   16,      /* bitsize */
980
   false,     /* pc_relative */
981
   0,     /* bitpos */
982
   complain_overflow_bitfield, /* complain_on_overflow */
983
   0,     /* special_function */
984
   "R_BA_16",   /* name */
985
   true,      /* partial_inplace */
986
   0xfffc,    /* src_mask */
987
   0xfffc,    /* dst_mask */
988
   false),    /* pcrel_offset */
989
990
  /* 0x1d: Modifiable branch relative.  */
991
  HOWTO (R_RBR,     /* type */
992
   0,     /* rightshift */
993
   2,     /* size */
994
   16,      /* bitsize */
995
   true,      /* pc_relative */
996
   0,     /* bitpos */
997
   complain_overflow_signed, /* complain_on_overflow */
998
   0,     /* special_function */
999
   "R_RBR_16",    /* name */
1000
   true,      /* partial_inplace */
1001
   0xfffc,    /* src_mask */
1002
   0xfffc,    /* dst_mask */
1003
   false),    /* pcrel_offset */
1004
1005
  /* 0x1e: Modifiable branch relative.  */
1006
  HOWTO (R_RBA,     /* type */
1007
   0,     /* rightshift */
1008
   2,     /* size */
1009
   16,      /* bitsize */
1010
   false,     /* pc_relative */
1011
   0,     /* bitpos */
1012
   complain_overflow_signed, /* complain_on_overflow */
1013
   0,     /* special_function */
1014
   "R_RBA_16",    /* name */
1015
   true,      /* partial_inplace */
1016
   0xffff,    /* src_mask */
1017
   0xffff,    /* dst_mask */
1018
   false),    /* pcrel_offset */
1019
1020
  EMPTY_HOWTO (0x1f),
1021
1022
  /* 0x20: General-dynamic TLS relocation.  */
1023
  HOWTO (R_TLS,     /* type */
1024
   0,     /* rightshift */
1025
   4,     /* size */
1026
   32,      /* bitsize */
1027
   false,     /* pc_relative */
1028
   0,     /* bitpos */
1029
   complain_overflow_bitfield, /* complain_on_overflow */
1030
   0,     /* special_function */
1031
   "R_TLS",   /* name */
1032
   true,      /* partial_inplace */
1033
   0xffffffff,    /* src_mask */
1034
   0xffffffff,    /* dst_mask */
1035
   false),    /* pcrel_offset */
1036
1037
  /* 0x21: Initial-exec TLS relocation.  */
1038
  HOWTO (R_TLS_IE,    /* type */
1039
   0,     /* rightshift */
1040
   4,     /* size */
1041
   32,      /* bitsize */
1042
   false,     /* pc_relative */
1043
   0,     /* bitpos */
1044
   complain_overflow_bitfield, /* complain_on_overflow */
1045
   0,     /* special_function */
1046
   "R_TLS_IE",    /* name */
1047
   true,      /* partial_inplace */
1048
   0xffffffff,    /* src_mask */
1049
   0xffffffff,    /* dst_mask */
1050
   false),    /* pcrel_offset */
1051
1052
  /* 0x22: Local-dynamic TLS relocation.  */
1053
  HOWTO (R_TLS_LD,    /* type */
1054
   0,     /* rightshift */
1055
   4,     /* size */
1056
   32,      /* bitsize */
1057
   false,     /* pc_relative */
1058
   0,     /* bitpos */
1059
   complain_overflow_bitfield, /* complain_on_overflow */
1060
   0,     /* special_function */
1061
   "R_TLS_LD",    /* name */
1062
   true,      /* partial_inplace */
1063
   0xffffffff,    /* src_mask */
1064
   0xffffffff,    /* dst_mask */
1065
   false),    /* pcrel_offset */
1066
1067
  /* 0x23: Local-exec TLS relocation.  */
1068
  HOWTO (R_TLS_LE,    /* type */
1069
   0,     /* rightshift */
1070
   4,     /* size */
1071
   32,      /* bitsize */
1072
   false,     /* pc_relative */
1073
   0,     /* bitpos */
1074
   complain_overflow_bitfield, /* complain_on_overflow */
1075
   0,     /* special_function */
1076
   "R_TLS_LE",    /* name */
1077
   true,      /* partial_inplace */
1078
   0xffffffff,    /* src_mask */
1079
   0xffffffff,    /* dst_mask */
1080
   false),    /* pcrel_offset */
1081
1082
  /* 0x24: TLS relocation.  */
1083
  HOWTO (R_TLSM,    /* type */
1084
   0,     /* rightshift */
1085
   4,     /* size */
1086
   32,      /* bitsize */
1087
   false,     /* pc_relative */
1088
   0,     /* bitpos */
1089
   complain_overflow_bitfield, /* complain_on_overflow */
1090
   0,     /* special_function */
1091
   "R_TLSM",    /* name */
1092
   true,      /* partial_inplace */
1093
   0xffffffff,    /* src_mask */
1094
   0xffffffff,    /* dst_mask */
1095
   false),    /* pcrel_offset */
1096
1097
1098
  /* 0x25: TLS module relocation.  */
1099
  HOWTO (R_TLSML,   /* type */
1100
   0,     /* rightshift */
1101
   4,     /* size */
1102
   32,      /* bitsize */
1103
   false,     /* pc_relative */
1104
   0,     /* bitpos */
1105
   complain_overflow_bitfield, /* complain_on_overflow */
1106
   0,     /* special_function */
1107
   "R_TLSML",   /* name */
1108
   true,      /* partial_inplace */
1109
   0xffffffff,    /* src_mask */
1110
   0xffffffff,    /* dst_mask */
1111
   false),    /* pcrel_offset */
1112
1113
  EMPTY_HOWTO(0x26),
1114
  EMPTY_HOWTO(0x27),
1115
  EMPTY_HOWTO(0x28),
1116
  EMPTY_HOWTO(0x29),
1117
  EMPTY_HOWTO(0x2a),
1118
  EMPTY_HOWTO(0x2b),
1119
  EMPTY_HOWTO(0x2c),
1120
  EMPTY_HOWTO(0x2d),
1121
  EMPTY_HOWTO(0x2e),
1122
  EMPTY_HOWTO(0x2f),
1123
1124
  /* 0x30: High-order 16 bit TOC relative relocation.  */
1125
  HOWTO (R_TOCU,    /* type */
1126
   16,      /* rightshift */
1127
   2,     /* size */
1128
   16,      /* bitsize */
1129
   false,     /* pc_relative */
1130
   0,     /* bitpos */
1131
   complain_overflow_bitfield, /* complain_on_overflow */
1132
   0,     /* special_function */
1133
   "R_TOCU",    /* name */
1134
   true,      /* partial_inplace */
1135
   0,     /* src_mask */
1136
   0xffff,    /* dst_mask */
1137
   false),    /* pcrel_offset */
1138
1139
  /* 0x31: Low-order 16 bit TOC relative relocation.  */
1140
  HOWTO (R_TOCL,    /* type */
1141
   0,     /* rightshift */
1142
   2,     /* size */
1143
   16,      /* bitsize */
1144
   false,     /* pc_relative */
1145
   0,     /* bitpos */
1146
   complain_overflow_dont, /* complain_on_overflow */
1147
   0,     /* special_function */
1148
   "R_TOCL",    /* name */
1149
   true,      /* partial_inplace */
1150
   0,     /* src_mask */
1151
   0xffff,    /* dst_mask */
1152
   false),    /* pcrel_offset */
1153
1154
};
1155
1156
void
1157
xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1158
168
{
1159
168
  if (internal->r_type >= ARRAY_SIZE (xcoff_howto_table))
1160
21
    {
1161
21
      relent->howto = NULL;
1162
21
      return;
1163
21
    }
1164
1165
  /* Default howto layout works most of the time */
1166
147
  relent->howto = &xcoff_howto_table[internal->r_type];
1167
1168
  /* Special case some 16 bit reloc */
1169
147
  if (15 == (internal->r_size & 0x1f))
1170
36
    {
1171
36
      if (R_BA == internal->r_type)
1172
0
  relent->howto = &xcoff_howto_table[0x1c];
1173
36
      else if (R_RBR == internal->r_type)
1174
0
  relent->howto = &xcoff_howto_table[0x1d];
1175
36
      else if (R_RBA == internal->r_type)
1176
0
  relent->howto = &xcoff_howto_table[0x1e];
1177
36
    }
1178
1179
  /* The r_size field of an XCOFF reloc encodes the bitsize of the
1180
     relocation, as well as indicating whether it is signed or not.
1181
     Doublecheck that the relocation information gathered from the
1182
     type matches this information.  The bitsize is not significant
1183
     for R_REF relocs.  */
1184
147
  if (relent->howto->dst_mask != 0
1185
100
      && (relent->howto->bitsize
1186
100
    != ((unsigned int) internal->r_size & 0x1f) + 1))
1187
97
    relent->howto = NULL;
1188
147
}
1189
1190
reloc_howto_type *
1191
_bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1192
            bfd_reloc_code_real_type code)
1193
0
{
1194
0
  switch (code)
1195
0
    {
1196
0
    case BFD_RELOC_PPC_B26:
1197
0
      return &xcoff_howto_table[0xa];
1198
0
    case BFD_RELOC_PPC_BA16:
1199
0
      return &xcoff_howto_table[0x1c];
1200
0
    case BFD_RELOC_PPC_BA26:
1201
0
      return &xcoff_howto_table[8];
1202
0
    case BFD_RELOC_PPC_TOC16:
1203
0
      return &xcoff_howto_table[3];
1204
0
    case BFD_RELOC_PPC_TOC16_HI:
1205
0
      return &xcoff_howto_table[0x30];
1206
0
    case BFD_RELOC_PPC_TOC16_LO:
1207
0
      return &xcoff_howto_table[0x31];
1208
0
    case BFD_RELOC_PPC_B16:
1209
0
      return &xcoff_howto_table[0x1d];
1210
0
    case BFD_RELOC_32:
1211
0
    case BFD_RELOC_CTOR:
1212
0
      return &xcoff_howto_table[0];
1213
0
    case BFD_RELOC_NONE:
1214
0
      return &xcoff_howto_table[0xf];
1215
0
    case BFD_RELOC_PPC_NEG:
1216
0
      return &xcoff_howto_table[0x1];
1217
0
    case BFD_RELOC_PPC_TLSGD:
1218
0
      return &xcoff_howto_table[0x20];
1219
0
    case BFD_RELOC_PPC_TLSIE:
1220
0
      return &xcoff_howto_table[0x21];
1221
0
    case BFD_RELOC_PPC_TLSLD:
1222
0
      return &xcoff_howto_table[0x22];
1223
0
    case BFD_RELOC_PPC_TLSLE:
1224
0
      return &xcoff_howto_table[0x23];
1225
0
    case BFD_RELOC_PPC_TLSM:
1226
0
      return &xcoff_howto_table[0x24];
1227
0
    case BFD_RELOC_PPC_TLSML:
1228
0
      return &xcoff_howto_table[0x25];
1229
0
    default:
1230
0
      return NULL;
1231
0
    }
1232
0
}
1233
1234
static reloc_howto_type *
1235
_bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1236
            const char *r_name)
1237
0
{
1238
0
  unsigned int i;
1239
1240
0
  for (i = 0; i < ARRAY_SIZE (xcoff_howto_table); i++)
1241
0
    if (xcoff_howto_table[i].name != NULL
1242
0
  && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
1243
0
      return &xcoff_howto_table[i];
1244
1245
0
  return NULL;
1246
0
}
1247

1248
/* XCOFF archive support.  The original version of this code was by
1249
   Damon A. Permezel.  It was enhanced to permit cross support, and
1250
   writing archive files, by Ian Lance Taylor, Cygnus Support.
1251
1252
   XCOFF uses its own archive format.  Everything is hooked together
1253
   with file offset links, so it is possible to rapidly update an
1254
   archive in place.  Of course, we don't do that.  An XCOFF archive
1255
   has a real file header, not just an ARMAG string.  The structure of
1256
   the file header and of each archive header appear below.
1257
1258
   An XCOFF archive also has a member table, which is a list of
1259
   elements in the archive (you can get that by looking through the
1260
   linked list, but you have to read a lot more of the file).  The
1261
   member table has a normal archive header with an empty name.  It is
1262
   normally (and perhaps must be) the second to last entry in the
1263
   archive.  The member table data is almost printable ASCII.  It
1264
   starts with a 12 character decimal string which is the number of
1265
   entries in the table.  For each entry it has a 12 character decimal
1266
   string which is the offset in the archive of that member.  These
1267
   entries are followed by a series of null terminated strings which
1268
   are the member names for each entry.
1269
1270
   Finally, an XCOFF archive has a global symbol table, which is what
1271
   we call the armap.  The global symbol table has a normal archive
1272
   header with an empty name.  It is normally (and perhaps must be)
1273
   the last entry in the archive.  The contents start with a four byte
1274
   binary number which is the number of entries.  This is followed by
1275
   a that many four byte binary numbers; each is the file offset of an
1276
   entry in the archive.  These numbers are followed by a series of
1277
   null terminated strings, which are symbol names.
1278
1279
   AIX 4.3 introduced a new archive format which can handle larger
1280
   files and also 32- and 64-bit objects in the same archive.  The
1281
   things said above remain true except that there is now more than
1282
   one global symbol table.  The one is used to index 32-bit objects,
1283
   the other for 64-bit objects.
1284
1285
   The new archives (recognizable by the new ARMAG string) has larger
1286
   field lengths so that we cannot really share any code.  Also we have
1287
   to take care that we are not generating the new form of archives
1288
   on AIX 4.2 or earlier systems.  */
1289
1290
/* PR 21786:  The PE/COFF standard does not require NUL termination for any of
1291
   the ASCII fields in the archive headers.  So in order to be able to extract
1292
   numerical values we provide our own versions of strtol and strtoll which
1293
   take a maximum length as an additional parameter.  Also - just to save space,
1294
   we omit the endptr return parameter, since we know that it is never used.  */
1295
1296
static unsigned long
1297
_bfd_strntol (const char * nptr, int base, unsigned int maxlen)
1298
5.33k
{
1299
5.33k
  char buf[24]; /* Should be enough.  */
1300
1301
5.33k
  BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1302
1303
5.33k
  memcpy (buf, nptr, maxlen);
1304
5.33k
  buf[maxlen] = 0;
1305
5.33k
  return strtol (buf, NULL, base);
1306
5.33k
}
1307
1308
static unsigned long long
1309
_bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
1310
0
{
1311
0
  char buf[32]; /* Should be enough.  */
1312
1313
0
  BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1314
1315
0
  memcpy (buf, nptr, maxlen);
1316
0
  buf[maxlen] = 0;
1317
0
  return strtoll (buf, NULL, base);
1318
0
}
1319
1320
/* Macro to read an ASCII value stored in an archive header field.  */
1321
#define GET_VALUE_IN_FIELD(VAR, FIELD, BASE)      \
1322
4.57k
  do                \
1323
4.57k
    {               \
1324
4.57k
      (VAR) = (sizeof (VAR) > sizeof (long)      \
1325
4.57k
         ? _bfd_strntoll (FIELD, BASE, sizeof FIELD)  \
1326
4.57k
         : _bfd_strntol (FIELD, BASE, sizeof FIELD));  \
1327
4.57k
    }               \
1328
4.57k
  while (0)
1329
1330
#define EQ_VALUE_IN_FIELD(VAR, FIELD, BASE)     \
1331
1.19k
  (sizeof (VAR) > sizeof (long)          \
1332
1.19k
   ? (VAR) == _bfd_strntoll (FIELD, BASE, sizeof FIELD)    \
1333
1.19k
   : (VAR) == _bfd_strntol (FIELD, BASE, sizeof FIELD))
1334
1335
/* Read in the armap of an XCOFF archive.  */
1336
1337
bool
1338
_bfd_xcoff_slurp_armap (bfd *abfd)
1339
1.64k
{
1340
1.64k
  ufile_ptr off;
1341
1.64k
  size_t namlen;
1342
1.64k
  bfd_size_type sz;
1343
1.64k
  bfd_byte *contents, *cend;
1344
1.64k
  bfd_vma c, i;
1345
1.64k
  carsym *arsym;
1346
1.64k
  bfd_byte *p;
1347
1348
1.64k
  if (x_artdata (abfd) == NULL)
1349
0
    {
1350
0
      abfd->has_armap = false;
1351
0
      return true;
1352
0
    }
1353
1354
1.64k
  if (! xcoff_big_format_p (abfd))
1355
530
    {
1356
      /* This is for the old format.  */
1357
530
      struct xcoff_ar_hdr hdr;
1358
1359
530
      GET_VALUE_IN_FIELD (off, x_artdata (abfd)->u.hdr.symoff, 10);
1360
530
      if (off == 0)
1361
68
  {
1362
68
    abfd->has_armap = false;
1363
68
    return true;
1364
68
  }
1365
1366
462
      if (bfd_seek (abfd, off, SEEK_SET) != 0)
1367
93
  return false;
1368
1369
      /* The symbol table starts with a normal archive header.  */
1370
369
      if (bfd_read (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
1371
94
  return false;
1372
1373
      /* Skip the name (normally empty).  */
1374
275
      GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1375
275
      off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1376
275
      if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1377
3
  return false;
1378
1379
272
      GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1380
272
      if (sz + 1 < 5)
1381
8
  {
1382
8
    bfd_set_error (bfd_error_bad_value);
1383
8
    return false;
1384
8
  }
1385
1386
      /* Read in the entire symbol table.  */
1387
264
      contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
1388
264
      if (contents == NULL)
1389
161
  return false;
1390
1391
      /* Ensure strings are NULL terminated so we don't wander off the
1392
   end of the buffer.  */
1393
103
      contents[sz] = 0;
1394
1395
      /* The symbol table starts with a four byte count.  */
1396
103
      c = H_GET_32 (abfd, contents);
1397
1398
103
      if (c >= sz / 4)
1399
73
  {
1400
73
    bfd_set_error (bfd_error_bad_value);
1401
73
    return false;
1402
73
  }
1403
1404
30
      bfd_ardata (abfd)->symdefs =
1405
30
  ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1406
30
      if (bfd_ardata (abfd)->symdefs == NULL)
1407
0
  return false;
1408
1409
      /* After the count comes a list of four byte file offsets.  */
1410
30
      for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1411
640
     i < c;
1412
610
     ++i, ++arsym, p += 4)
1413
610
  arsym->u.file_offset = H_GET_32 (abfd, p);
1414
30
    }
1415
1.11k
  else
1416
1.11k
    {
1417
      /* This is for the new format.  */
1418
1.11k
      struct xcoff_ar_hdr_big hdr;
1419
1420
1.11k
      GET_VALUE_IN_FIELD (off, x_artdata (abfd)->u.bhdr.symoff, 10);
1421
1.11k
      if (off == 0)
1422
318
  {
1423
318
    abfd->has_armap = false;
1424
318
    return true;
1425
318
  }
1426
1427
801
      if (bfd_seek (abfd, off, SEEK_SET) != 0)
1428
158
  return false;
1429
1430
      /* The symbol table starts with a normal archive header.  */
1431
643
      if (bfd_read (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
1432
61
  return false;
1433
1434
      /* Skip the name (normally empty).  */
1435
582
      GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1436
582
      off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1437
582
      if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1438
6
  return false;
1439
1440
576
      GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1441
576
      if (sz + 1 < 9)
1442
27
  {
1443
27
    bfd_set_error (bfd_error_bad_value);
1444
27
    return false;
1445
27
  }
1446
1447
      /* Read in the entire symbol table.  */
1448
549
      contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
1449
549
      if (contents == NULL)
1450
257
  return false;
1451
1452
      /* Ensure strings are NULL terminated so we don't wander off the
1453
   end of the buffer.  */
1454
292
      contents[sz] = 0;
1455
1456
      /* The symbol table starts with an eight byte count.  */
1457
292
      c = H_GET_64 (abfd, contents);
1458
1459
292
      if (c >= sz / 8)
1460
242
  {
1461
242
    bfd_set_error (bfd_error_bad_value);
1462
242
    return false;
1463
242
  }
1464
1465
50
      bfd_ardata (abfd)->symdefs =
1466
50
  ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1467
50
      if (bfd_ardata (abfd)->symdefs == NULL)
1468
0
  return false;
1469
1470
      /* After the count comes a list of eight byte file offsets.  */
1471
50
      for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1472
1.18k
     i < c;
1473
1.13k
     ++i, ++arsym, p += 8)
1474
1.13k
  arsym->u.file_offset = H_GET_64 (abfd, p);
1475
50
    }
1476
1477
  /* After the file offsets come null terminated symbol names.  */
1478
80
  cend = contents + sz;
1479
80
  for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1480
1.42k
       i < c;
1481
1.34k
       ++i, ++arsym, p += strlen ((char *) p) + 1)
1482
1.37k
    {
1483
1.37k
      if (p >= cend)
1484
27
  {
1485
27
    bfd_set_error (bfd_error_bad_value);
1486
27
    return false;
1487
27
  }
1488
1.34k
      arsym->name = (char *) p;
1489
1.34k
    }
1490
1491
53
  bfd_ardata (abfd)->symdef_count = c;
1492
53
  abfd->has_armap = true;
1493
1494
53
  return true;
1495
80
}
1496
1497
/* See if this is an XCOFF archive.  */
1498
1499
bfd_cleanup
1500
_bfd_xcoff_archive_p (bfd *abfd)
1501
110k
{
1502
110k
  char magic[SXCOFFARMAG];
1503
110k
  size_t amt = SXCOFFARMAG;
1504
1505
110k
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1506
1507
110k
  if (bfd_read (magic, amt, abfd) != amt)
1508
563
    {
1509
563
      if (bfd_get_error () != bfd_error_system_call)
1510
261
  bfd_set_error (bfd_error_wrong_format);
1511
563
      return NULL;
1512
563
    }
1513
1514
109k
  if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1515
109k
      && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1516
107k
    {
1517
107k
      bfd_set_error (bfd_error_wrong_format);
1518
107k
      return NULL;
1519
107k
    }
1520
1521
1.69k
  amt = sizeof (struct artdata) + sizeof (struct xcoff_artdata);
1522
1.69k
  bfd_ardata (abfd) = bfd_zalloc (abfd, amt);
1523
1.69k
  if (bfd_ardata (abfd) == NULL)
1524
0
    return NULL;
1525
1526
1.69k
  bfd_ardata (abfd)->tdata = (void *) ((struct artdata *) bfd_ardata (abfd) + 1);
1527
1528
  /* Now handle the two formats.  */
1529
1.69k
  if (magic[1] != 'b')
1530
551
    {
1531
      /* This is the old format.  */
1532
551
      struct xcoff_ar_file_hdr hdr;
1533
1534
      /* Copy over the magic string.  */
1535
551
      memcpy (hdr.magic, magic, SXCOFFARMAG);
1536
1537
      /* Now read the rest of the file header.  */
1538
551
      amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1539
551
      if (bfd_read (&hdr.memoff, amt, abfd) != amt)
1540
21
  {
1541
21
    if (bfd_get_error () != bfd_error_system_call)
1542
21
      bfd_set_error (bfd_error_wrong_format);
1543
21
    goto error_ret;
1544
21
  }
1545
1546
530
      GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file.file_offset,
1547
530
        hdr.firstmemoff, 10);
1548
1549
530
      memcpy (&x_artdata (abfd)->u.hdr, &hdr, SIZEOF_AR_FILE_HDR);
1550
530
    }
1551
1.14k
  else
1552
1.14k
    {
1553
      /* This is the new format.  */
1554
1.14k
      struct xcoff_ar_file_hdr_big hdr;
1555
1556
      /* Copy over the magic string.  */
1557
1.14k
      memcpy (hdr.magic, magic, SXCOFFARMAG);
1558
1559
      /* Now read the rest of the file header.  */
1560
1.14k
      amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1561
1.14k
      if (bfd_read (&hdr.memoff, amt, abfd) != amt)
1562
28
  {
1563
28
    if (bfd_get_error () != bfd_error_system_call)
1564
28
      bfd_set_error (bfd_error_wrong_format);
1565
28
    goto error_ret;
1566
28
  }
1567
1568
1.11k
      bfd_ardata (abfd)->first_file.file_offset
1569
1.11k
  = bfd_scan_vma (hdr.firstmemoff, (const char **) 0, 10);
1570
1571
1.11k
      memcpy (&x_artdata (abfd)->u.bhdr, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1572
1.11k
    }
1573
1574
1.64k
  if (! _bfd_xcoff_slurp_armap (abfd))
1575
1.21k
    {
1576
1.25k
    error_ret:
1577
1.25k
      bfd_release (abfd, bfd_ardata (abfd));
1578
1.25k
      return NULL;
1579
1.21k
    }
1580
1581
439
  return _bfd_no_cleanup;
1582
1.64k
}
1583
1584
/* Track file ranges occupied by elements.  Add [START,END) to the
1585
   list of ranges and return TRUE if there is no overlap between the
1586
   new and any other element or the archive file header.  This is
1587
   aimed at preventing infinite looping on malformed archives, for
1588
   "ar" and similar which typically use code like:
1589
   .  for (last = bfd_openr_next_archived_file (archive, NULL);
1590
   .       last;
1591
   .       last = next)
1592
   .    {
1593
   .      do_something_with (last);
1594
   .      next = bfd_openr_next_archived_file (archive, last);
1595
   .      bfd_close (last);
1596
   .    }
1597
   The check implemented here is only possible due to the fact that
1598
   for XCOFF archives bfd_openr_next_archived_file is the only code
1599
   path leading to _bfd_read_ar_hdr.  _bfd_read_ar_hdr is not called
1600
   when reading the armap, nor do XCOFF archives use the extended name
1601
   scheme implemented in archive.c.
1602
1603
   Note that the check relies on the previous element being closed,
1604
   and there is one case where add_range might fail but I think it is
1605
   sufficently unusual that it doesn't warrant fixing:
1606
   If the loop body above called bfd_openr_next_archived_file twice
1607
   with the same arguments and the element returned is bfd_close'd
1608
   between those calls then we'll return false here for the second
1609
   call.  (For why this is so see _bfd_look_for_bfd_in_cache in
1610
   _bfd_get_elt_at_filepos, and know that bfd_close removes elements
1611
   from the cache.)  */
1612
1613
static bool
1614
add_range (bfd *abfd, ufile_ptr start, ufile_ptr end)
1615
218
{
1616
218
  if (end <= start)
1617
0
    {
1618
61
    err:
1619
61
      bfd_set_error (bfd_error_malformed_archive);
1620
61
      return false;
1621
0
    }
1622
1623
  /* This list is kept sorted by address.  Find the highest address
1624
     range on the list that ends before the new range starts.  Exit
1625
     the loop with that range in LO, and the mext higher range in HI.  */
1626
218
  struct ar_ranges *hi = &x_artdata (abfd)->ranges;
1627
218
  struct ar_ranges *lo = NULL;
1628
393
  while (hi && hi->end <= start)
1629
175
    {
1630
175
      lo = hi;
1631
175
      hi = hi->next;
1632
175
    }
1633
1634
218
  if (lo == NULL)
1635
    /* Start overlaps the file header or elements adjacent to it.  */
1636
54
    goto err;
1637
1638
164
  if (hi && hi->start < end)
1639
    /* Overlap with another element.  */
1640
7
    goto err;
1641
1642
  /* A zero size element with a one char name is this big.  */
1643
157
  unsigned min_elt = x_artdata (abfd)->ar_hdr_size + 2 + SXCOFFARFMAG;
1644
157
  if (start - lo->end < min_elt)
1645
86
    {
1646
      /* Merge into an existing range.  */
1647
86
      lo->end = end;
1648
86
      if (hi && hi->start - end < min_elt)
1649
1
  {
1650
    /* In fact, we can merge two ranges.  */
1651
1
    lo->end = hi->end;
1652
1
    lo->next = hi->next;
1653
    /* The list uses bfd_alloc so don't free HI.  */
1654
1
  }
1655
86
      return true;
1656
86
    }
1657
1658
71
  if (hi && hi->start - end < min_elt)
1659
1
    {
1660
      /* Merge into an existing range.  */
1661
1
      hi->start = start;
1662
1
      return true;
1663
1
    }
1664
1665
70
  struct ar_ranges *newr = bfd_alloc (abfd, sizeof (*newr));
1666
70
  if (newr == NULL)
1667
0
    return false;
1668
70
  newr->start = start;
1669
70
  newr->end = end;
1670
70
  newr->next = hi;
1671
70
  lo->next = newr;
1672
70
  return true;
1673
70
}
1674
1675
/* Read the archive header in an XCOFF archive.  */
1676
1677
void *
1678
_bfd_xcoff_read_ar_hdr (bfd *abfd)
1679
328
{
1680
328
  bfd_size_type namlen;
1681
328
  struct areltdata *ret;
1682
328
  bfd_size_type amt;
1683
328
  ufile_ptr start = abfd->where;
1684
1685
328
  if (! xcoff_big_format_p (abfd))
1686
102
    {
1687
102
      struct xcoff_ar_hdr hdr;
1688
102
      struct xcoff_ar_hdr *hdrp;
1689
1690
102
      if (bfd_read (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
1691
35
  return NULL;
1692
1693
67
      GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1694
67
      if (namlen > bfd_get_file_size (abfd))
1695
2
  return NULL;
1696
65
      amt = sizeof (struct areltdata) + SIZEOF_AR_HDR + namlen + 1;
1697
65
      ret = (struct areltdata *) bfd_malloc (amt);
1698
65
      if (ret == NULL)
1699
0
  return ret;
1700
1701
65
      hdrp = (struct xcoff_ar_hdr *) (ret + 1);
1702
65
      memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1703
65
      if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
1704
1
  {
1705
1
    free (ret);
1706
1
    return NULL;
1707
1
  }
1708
64
      ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1709
1710
64
      ret->arch_header = (char *) hdrp;
1711
64
      GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1712
64
      ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1713
64
    }
1714
226
  else
1715
226
    {
1716
226
      struct xcoff_ar_hdr_big hdr;
1717
226
      struct xcoff_ar_hdr_big *hdrp;
1718
1719
226
      if (bfd_read (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
1720
65
  return NULL;
1721
1722
161
      GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1723
161
      if (namlen > bfd_get_file_size (abfd))
1724
3
  return NULL;
1725
158
      amt = sizeof (struct areltdata) + SIZEOF_AR_HDR_BIG + namlen + 1;
1726
158
      ret = (struct areltdata *) bfd_malloc (amt);
1727
158
      if (ret == NULL)
1728
0
  return ret;
1729
1730
158
      hdrp = (struct xcoff_ar_hdr_big *) (ret + 1);
1731
158
      memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1732
158
      if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
1733
4
  {
1734
4
    free (ret);
1735
4
    return NULL;
1736
4
  }
1737
154
      ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1738
1739
154
      ret->arch_header = (char *) hdrp;
1740
154
      GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1741
154
      ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1742
154
    }
1743
1744
  /* Size occupied by the header above that covered in the fixed
1745
     SIZEOF_AR_HDR or SIZEOF_AR_HDR_BIG.  */
1746
218
  ret->extra_size = namlen + (namlen & 1) + SXCOFFARFMAG;
1747
1748
  /* Skip over the XCOFFARFMAG at the end of the file name.  */
1749
218
  if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0
1750
218
      || !add_range (abfd, start, abfd->where + ret->parsed_size))
1751
61
    {
1752
61
      free (ret);
1753
61
      return NULL;
1754
61
    }
1755
1756
157
  return ret;
1757
218
}
1758
1759
/* Open the next element in an XCOFF archive.  */
1760
1761
bfd *
1762
_bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
1763
439
{
1764
439
  ufile_ptr filestart;
1765
1766
439
  BFD_ASSERT (!bfd_is_fake_archive (archive));
1767
1768
439
  if (x_artdata (archive) == NULL)
1769
0
    {
1770
0
      bfd_set_error (bfd_error_invalid_operation);
1771
0
      return NULL;
1772
0
    }
1773
1774
439
  if (! xcoff_big_format_p (archive))
1775
120
    {
1776
120
      if (last_file == NULL)
1777
71
  {
1778
    /* If we are scanning over elements twice in an open archive,
1779
       which can happen in gdb after a fork, ensure we start the
1780
       second scan with clean ranges.  */
1781
71
    x_artdata (archive)->ranges.start = 0;
1782
71
    x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR;
1783
71
    x_artdata (archive)->ranges.next = NULL;
1784
71
    x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR;
1785
71
    filestart = bfd_ardata (archive)->first_file.file_offset;
1786
71
  }
1787
49
      else
1788
49
  GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
1789
1790
120
      if (filestart == 0
1791
106
    || EQ_VALUE_IN_FIELD (filestart,
1792
120
        x_artdata (archive)->u.hdr.memoff, 10)
1793
105
    || EQ_VALUE_IN_FIELD (filestart,
1794
120
        x_artdata (archive)->u.hdr.symoff, 10))
1795
15
  {
1796
15
    bfd_set_error (bfd_error_no_more_archived_files);
1797
15
    return NULL;
1798
15
  }
1799
120
    }
1800
319
  else
1801
319
    {
1802
319
      if (last_file == NULL)
1803
216
  {
1804
216
    x_artdata (archive)->ranges.start = 0;
1805
216
    x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR_BIG;
1806
216
    x_artdata (archive)->ranges.next = NULL;
1807
216
    x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR_BIG;
1808
216
    filestart = bfd_ardata (archive)->first_file.file_offset;
1809
216
  }
1810
103
      else
1811
103
  GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);
1812
1813
319
      if (filestart == 0
1814
275
    || EQ_VALUE_IN_FIELD (filestart,
1815
319
        x_artdata (archive)->u.bhdr.memoff, 10)
1816
274
    || EQ_VALUE_IN_FIELD (filestart,
1817
319
        x_artdata (archive)->u.bhdr.symoff, 10))
1818
45
  {
1819
45
    bfd_set_error (bfd_error_no_more_archived_files);
1820
45
    return NULL;
1821
45
  }
1822
319
    }
1823
1824
  /* Check that we aren't pointing back at the last element.  This is
1825
     necessary depite the add_range checking in _bfd_xcoff_read_ar_hdr
1826
     because archive.c leaves the last element open and thus in the
1827
     archive element cache until the next element is opened.  */
1828
379
  if (last_file != NULL)
1829
107
    {
1830
107
      ufile_ptr laststart = last_file->proxy_handle.file_offset;
1831
107
      laststart -= x_artdata (archive)->ar_hdr_size;
1832
107
      laststart -= arch_eltdata (last_file)->extra_size;
1833
107
      if (filestart == laststart)
1834
6
  {
1835
6
    bfd_set_error (bfd_error_malformed_archive);
1836
6
    return NULL;
1837
6
  }
1838
107
    }
1839
1840
373
  return _bfd_get_elt_at_filepos (archive, filestart, NULL);
1841
379
}
1842
1843
/* Stat an element in an XCOFF archive.  */
1844
1845
int
1846
_bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
1847
216
{
1848
216
  if (abfd->arelt_data == NULL)
1849
192
    {
1850
192
      bfd_set_error (bfd_error_invalid_operation);
1851
192
      return -1;
1852
192
    }
1853
1854
24
  if (! xcoff_big_format_p (abfd->my_archive))
1855
5
    {
1856
5
      struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1857
1858
5
      GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1859
5
      GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1860
5
      GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1861
5
      GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1862
5
      s->st_size = arch_eltdata (abfd)->parsed_size;
1863
5
    }
1864
19
  else
1865
19
    {
1866
19
      struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1867
1868
19
      GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1869
19
      GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1870
19
      GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1871
19
      GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1872
19
      s->st_size = arch_eltdata (abfd)->parsed_size;
1873
19
    }
1874
1875
24
  return 0;
1876
216
}
1877
1878
/* Normalize a file name for inclusion in an archive.  */
1879
1880
static const char *
1881
normalize_filename (bfd *abfd)
1882
60
{
1883
60
  const char *file;
1884
60
  const char *filename;
1885
1886
60
  file = bfd_get_filename (abfd);
1887
60
  filename = strrchr (file, '/');
1888
60
  if (filename != NULL)
1889
60
    filename++;
1890
0
  else
1891
0
    filename = file;
1892
60
  return filename;
1893
60
}
1894
1895
/* Write out an XCOFF armap.  */
1896
1897
static bool
1898
xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1899
           struct orl *map, unsigned int orl_count, int stridx)
1900
0
{
1901
0
  struct archive_iterator iterator;
1902
0
  struct xcoff_ar_hdr hdr;
1903
0
  char *p;
1904
0
  unsigned char buf[4];
1905
0
  unsigned int i;
1906
1907
0
  memset (&hdr, 0, sizeof hdr);
1908
0
  sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1909
0
  sprintf (hdr.nextoff, "%d", 0);
1910
0
  memcpy (hdr.prevoff, x_artdata (abfd)->u.hdr.memoff,
1911
0
    XCOFFARMAG_ELEMENT_SIZE);
1912
0
  sprintf (hdr.date, "%d", 0);
1913
0
  sprintf (hdr.uid, "%d", 0);
1914
0
  sprintf (hdr.gid, "%d", 0);
1915
0
  sprintf (hdr.mode, "%d", 0);
1916
0
  sprintf (hdr.namlen, "%d", 0);
1917
1918
  /* We need spaces, not null bytes, in the header.  */
1919
0
  for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1920
0
    if (*p == '\0')
1921
0
      *p = ' ';
1922
1923
0
  if (bfd_write (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1924
0
      || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1925
0
    return false;
1926
1927
0
  H_PUT_32 (abfd, orl_count, buf);
1928
0
  if (bfd_write (buf, 4, abfd) != 4)
1929
0
    return false;
1930
1931
0
  i = 0;
1932
0
  archive_iterator_begin (&iterator, abfd);
1933
0
  while (i < orl_count && archive_iterator_next (&iterator))
1934
0
    while (map[i].abfd == iterator.current.member)
1935
0
      {
1936
0
  H_PUT_32 (abfd, iterator.current.offset, buf);
1937
0
  if (bfd_write (buf, 4, abfd) != 4)
1938
0
    return false;
1939
0
  ++i;
1940
0
      }
1941
1942
0
  for (i = 0; i < orl_count; i++)
1943
0
    {
1944
0
      const char *name;
1945
0
      size_t namlen;
1946
1947
0
      name = *map[i].name;
1948
0
      namlen = strlen (name);
1949
0
      if (bfd_write (name, namlen + 1, abfd) != namlen + 1)
1950
0
  return false;
1951
0
    }
1952
1953
0
  if ((stridx & 1) != 0)
1954
0
    {
1955
0
      char b;
1956
1957
0
      b = '\0';
1958
0
      if (bfd_write (&b, 1, abfd) != 1)
1959
0
  return false;
1960
0
    }
1961
1962
0
  return true;
1963
0
}
1964
1965
static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1966
518
#define FMT20  "%-20" PRId64
1967
332
#define FMT12  "%-12d"
1968
20
#define FMT12_OCTAL  "%-12o"
1969
88
#define FMT4  "%-4d"
1970
#define PRINT20(d, v) \
1971
518
  sprintf (buff20, FMT20, (uint64_t) (v)), \
1972
518
  memcpy ((void *) (d), buff20, 20)
1973
1974
#define PRINT12(d, v) \
1975
332
  sprintf (buff20, FMT12, (int)(v)), \
1976
332
  memcpy ((void *) (d), buff20, 12)
1977
1978
#define PRINT12_OCTAL(d, v) \
1979
20
  sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1980
20
  memcpy ((void *) (d), buff20, 12)
1981
1982
#define PRINT4(d, v) \
1983
88
  sprintf (buff20, FMT4, (int)(v)), \
1984
88
  memcpy ((void *) (d), buff20, 4)
1985
1986
#define READ20(d, v) \
1987
0
  buff20[20] = 0, \
1988
0
  memcpy (buff20, (d), 20), \
1989
0
  (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
1990
1991
static bool
1992
do_pad (bfd *abfd, unsigned int number)
1993
46
{
1994
46
  bfd_byte b = 0;
1995
1996
  /* Limit pad to <= 4096.  */
1997
46
  if (number > 4096)
1998
0
    return false;
1999
2000
57
  while (number--)
2001
11
    if (bfd_write (&b, 1, abfd) != 1)
2002
0
      return false;
2003
2004
46
  return true;
2005
46
}
2006
2007
static bool
2008
do_copy (bfd *out_bfd, bfd *in_bfd)
2009
20
{
2010
20
  bfd_size_type remaining;
2011
20
  bfd_byte buffer[8 * 1024];
2012
2013
20
  if (bfd_seek (in_bfd, 0, SEEK_SET) != 0)
2014
0
    return false;
2015
2016
20
  remaining = arelt_size (in_bfd);
2017
2018
20
  while (remaining >= sizeof (buffer))
2019
0
    {
2020
0
      if (bfd_read (buffer, sizeof (buffer), in_bfd) != sizeof (buffer)
2021
0
    || bfd_write (buffer, sizeof (buffer), out_bfd) != sizeof (buffer))
2022
0
  return false;
2023
2024
0
      remaining -= sizeof (buffer);
2025
0
    }
2026
2027
20
  if (remaining)
2028
13
    {
2029
13
      if (bfd_read (buffer, remaining, in_bfd) != remaining
2030
13
    || bfd_write (buffer, remaining, out_bfd) != remaining)
2031
0
  return false;
2032
13
    }
2033
2034
20
  return true;
2035
20
}
2036
2037
static bool
2038
xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2039
           struct orl *map, unsigned int orl_count, int stridx)
2040
0
{
2041
0
  struct archive_iterator iterator;
2042
0
  struct xcoff_ar_file_hdr_big *fhdr;
2043
0
  bfd_vma i, sym_32, sym_64, str_32, str_64;
2044
0
  const bfd_arch_info_type *arch_info;
2045
0
  bfd *current_bfd;
2046
0
  size_t string_length;
2047
0
  file_ptr nextoff, prevoff;
2048
2049
  /* First, we look through the symbols and work out which are
2050
     from 32-bit objects and which from 64-bit ones.  */
2051
0
  sym_32 = sym_64 = str_32 = str_64 = 0;
2052
2053
0
  i = 0;
2054
0
  for (current_bfd = abfd->archive_head;
2055
0
       current_bfd != NULL && i < orl_count;
2056
0
       current_bfd = current_bfd->archive_next)
2057
0
    {
2058
0
      arch_info = bfd_get_arch_info (current_bfd);
2059
0
      while (map[i].abfd == current_bfd)
2060
0
  {
2061
0
    string_length = strlen (*map[i].name) + 1;
2062
0
    if (arch_info->bits_per_address == 64)
2063
0
      {
2064
0
        sym_64++;
2065
0
        str_64 += string_length;
2066
0
      }
2067
0
    else
2068
0
      {
2069
0
        sym_32++;
2070
0
        str_32 += string_length;
2071
0
      }
2072
0
    i++;
2073
0
  }
2074
0
    }
2075
2076
  /* A quick sanity check... */
2077
0
  BFD_ASSERT (sym_64 + sym_32 == orl_count);
2078
  /* Explicit cast to int for compiler.  */
2079
0
  BFD_ASSERT ((int)(str_64 + str_32) == stridx);
2080
2081
0
  fhdr = &x_artdata (abfd)->u.bhdr;
2082
2083
  /* xcoff_write_archive_contents_big passes nextoff in symoff. */
2084
0
  READ20 (fhdr->memoff, prevoff);
2085
0
  READ20 (fhdr->symoff, nextoff);
2086
2087
0
  BFD_ASSERT (nextoff == bfd_tell (abfd));
2088
2089
  /* Write out the symbol table.
2090
     Layout :
2091
2092
     standard big archive header
2093
     0x0000         ar_size [0x14]
2094
     0x0014         ar_nxtmem [0x14]
2095
     0x0028         ar_prvmem [0x14]
2096
     0x003C         ar_date [0x0C]
2097
     0x0048         ar_uid  [0x0C]
2098
     0x0054         ar_gid  [0x0C]
2099
     0x0060         ar_mod  [0x0C]
2100
     0x006C         ar_namelen[0x04]
2101
     0x0070         ar_fmag [SXCOFFARFMAG]
2102
2103
     Symbol table
2104
     0x0072         num_syms  [0x08], binary
2105
     0x0078         offsets [0x08 * num_syms], binary
2106
     0x0086 + 0x08 * num_syms names [??]
2107
     ??           pad to even bytes.
2108
  */
2109
2110
0
  if (sym_32)
2111
0
    {
2112
0
      struct xcoff_ar_hdr_big *hdr;
2113
0
      char *symbol_table;
2114
0
      char *st;
2115
2116
0
      bfd_vma symbol_table_size =
2117
0
  SIZEOF_AR_HDR_BIG
2118
0
  + SXCOFFARFMAG
2119
0
  + 8
2120
0
  + 8 * sym_32
2121
0
  + str_32 + (str_32 & 1);
2122
2123
0
      symbol_table = bfd_zmalloc (symbol_table_size);
2124
0
      if (symbol_table == NULL)
2125
0
  return false;
2126
2127
0
      hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2128
2129
0
      PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
2130
2131
0
      if (sym_64)
2132
0
  PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
2133
0
      else
2134
0
  PRINT20 (hdr->nextoff, 0);
2135
2136
0
      PRINT20 (hdr->prevoff, prevoff);
2137
0
      PRINT12 (hdr->date, 0);
2138
0
      PRINT12 (hdr->uid, 0);
2139
0
      PRINT12 (hdr->gid, 0);
2140
0
      PRINT12 (hdr->mode, 0);
2141
0
      PRINT4 (hdr->namlen, 0) ;
2142
2143
0
      st = symbol_table + SIZEOF_AR_HDR_BIG;
2144
0
      memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2145
0
      st += SXCOFFARFMAG;
2146
2147
0
      bfd_h_put_64 (abfd, sym_32, st);
2148
0
      st += 8;
2149
2150
      /* loop over the 32 bit offsets */
2151
0
      i = 0;
2152
0
      archive_iterator_begin (&iterator, abfd);
2153
0
      while (i < orl_count && archive_iterator_next (&iterator))
2154
0
  {
2155
0
    arch_info = bfd_get_arch_info (iterator.current.member);
2156
0
    while (map[i].abfd == iterator.current.member)
2157
0
      {
2158
0
        if (arch_info->bits_per_address == 32)
2159
0
    {
2160
0
      bfd_h_put_64 (abfd, iterator.current.offset, st);
2161
0
      st += 8;
2162
0
    }
2163
0
        i++;
2164
0
      }
2165
0
  }
2166
2167
      /* loop over the 32 bit symbol names */
2168
0
      i = 0;
2169
0
      for (current_bfd = abfd->archive_head;
2170
0
     current_bfd != NULL && i < orl_count;
2171
0
     current_bfd = current_bfd->archive_next)
2172
0
  {
2173
0
    arch_info = bfd_get_arch_info (current_bfd);
2174
0
    while (map[i].abfd == current_bfd)
2175
0
      {
2176
0
        if (arch_info->bits_per_address == 32)
2177
0
    {
2178
0
      string_length = sprintf (st, "%s", *map[i].name);
2179
0
      st += string_length + 1;
2180
0
    }
2181
0
        i++;
2182
0
      }
2183
0
  }
2184
2185
0
      if (bfd_write (symbol_table, symbol_table_size, abfd)
2186
0
    != symbol_table_size)
2187
0
  {
2188
0
    free (symbol_table);
2189
0
    return false;
2190
0
  }
2191
0
      free (symbol_table);
2192
2193
0
      prevoff = nextoff;
2194
0
      nextoff = nextoff + symbol_table_size;
2195
0
    }
2196
0
  else
2197
0
    PRINT20 (fhdr->symoff, 0);
2198
2199
0
  if (sym_64)
2200
0
    {
2201
0
      struct xcoff_ar_hdr_big *hdr;
2202
0
      char *symbol_table;
2203
0
      char *st;
2204
2205
0
      bfd_vma symbol_table_size =
2206
0
  SIZEOF_AR_HDR_BIG
2207
0
  + SXCOFFARFMAG
2208
0
  + 8
2209
0
  + 8 * sym_64
2210
0
  + str_64 + (str_64 & 1);
2211
2212
0
      symbol_table = bfd_zmalloc (symbol_table_size);
2213
0
      if (symbol_table == NULL)
2214
0
  return false;
2215
2216
0
      hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2217
2218
0
      PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
2219
0
      PRINT20 (hdr->nextoff, 0);
2220
0
      PRINT20 (hdr->prevoff, prevoff);
2221
0
      PRINT12 (hdr->date, 0);
2222
0
      PRINT12 (hdr->uid, 0);
2223
0
      PRINT12 (hdr->gid, 0);
2224
0
      PRINT12 (hdr->mode, 0);
2225
0
      PRINT4 (hdr->namlen, 0);
2226
2227
0
      st = symbol_table + SIZEOF_AR_HDR_BIG;
2228
0
      memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2229
0
      st += SXCOFFARFMAG;
2230
2231
0
      bfd_h_put_64 (abfd, sym_64, st);
2232
0
      st += 8;
2233
2234
      /* loop over the 64 bit offsets */
2235
0
      i = 0;
2236
0
      archive_iterator_begin (&iterator, abfd);
2237
0
      while (i < orl_count && archive_iterator_next (&iterator))
2238
0
  {
2239
0
    arch_info = bfd_get_arch_info (iterator.current.member);
2240
0
    while (map[i].abfd == iterator.current.member)
2241
0
      {
2242
0
        if (arch_info->bits_per_address == 64)
2243
0
    {
2244
0
      bfd_h_put_64 (abfd, iterator.current.offset, st);
2245
0
      st += 8;
2246
0
    }
2247
0
        i++;
2248
0
      }
2249
0
  }
2250
2251
      /* loop over the 64 bit symbol names */
2252
0
      i = 0;
2253
0
      for (current_bfd = abfd->archive_head;
2254
0
     current_bfd != NULL && i < orl_count;
2255
0
     current_bfd = current_bfd->archive_next)
2256
0
  {
2257
0
    arch_info = bfd_get_arch_info (current_bfd);
2258
0
    while (map[i].abfd == current_bfd)
2259
0
      {
2260
0
        if (arch_info->bits_per_address == 64)
2261
0
    {
2262
0
      string_length = sprintf (st, "%s", *map[i].name);
2263
0
      st += string_length + 1;
2264
0
    }
2265
0
        i++;
2266
0
      }
2267
0
  }
2268
2269
0
      if (bfd_write (symbol_table, symbol_table_size, abfd)
2270
0
    != symbol_table_size)
2271
0
  {
2272
0
    free (symbol_table);
2273
0
    return false;
2274
0
  }
2275
0
      free (symbol_table);
2276
2277
0
      PRINT20 (fhdr->symoff64, nextoff);
2278
0
    }
2279
0
  else
2280
0
    PRINT20 (fhdr->symoff64, 0);
2281
2282
0
  return true;
2283
0
}
2284
2285
bool
2286
_bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2287
      struct orl *map, unsigned int orl_count, int stridx)
2288
0
{
2289
0
  if (! xcoff_big_format_p (abfd))
2290
0
    return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2291
0
  else
2292
0
    return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2293
0
}
2294
2295
/* Write out an XCOFF archive.  We always write an entire archive,
2296
   rather than fussing with the freelist and so forth.  */
2297
2298
static bool
2299
xcoff_write_archive_contents_old (bfd *abfd)
2300
6
{
2301
6
  struct archive_iterator iterator;
2302
6
  struct xcoff_artdata xtdata;
2303
6
  struct xcoff_ar_file_hdr *fhdr = &xtdata.u.hdr;
2304
6
  bfd_size_type count;
2305
6
  bfd_size_type total_namlen;
2306
6
  file_ptr *offsets;
2307
6
  bool makemap;
2308
6
  bool hasobjects;
2309
6
  file_ptr prevoff, nextoff;
2310
6
  bfd *sub;
2311
6
  size_t i;
2312
6
  struct xcoff_ar_hdr ahdr;
2313
6
  bfd_size_type size;
2314
6
  char *p;
2315
6
  char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
2316
2317
6
  memset (&xtdata, 0, sizeof (xtdata));
2318
6
  memcpy (fhdr->magic, XCOFFARMAG, SXCOFFARMAG);
2319
6
  sprintf (fhdr->firstmemoff, "%zu", SIZEOF_AR_FILE_HDR);
2320
6
  sprintf (fhdr->freeoff, "%d", 0);
2321
2322
6
  count = 0;
2323
6
  total_namlen = 0;
2324
6
  for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2325
0
    {
2326
0
      ++count;
2327
0
      total_namlen += strlen (normalize_filename (sub)) + 1;
2328
0
      if (sub->arelt_data == NULL)
2329
0
  {
2330
0
    sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
2331
0
    if (sub->arelt_data == NULL)
2332
0
      return false;
2333
0
  }
2334
0
      if (arch_xhdr (sub) == NULL)
2335
0
  {
2336
0
    struct xcoff_ar_hdr *ahdrp;
2337
0
    struct stat s;
2338
2339
0
    if ((sub->flags & BFD_IN_MEMORY) != 0)
2340
0
      {
2341
        /* Assume we just "made" the member, and fake it.  */
2342
0
        struct bfd_in_memory *bim
2343
0
    = (struct bfd_in_memory *) sub->iostream;
2344
0
        time (&s.st_mtime);
2345
0
        s.st_uid = getuid ();
2346
0
        s.st_gid = getgid ();
2347
0
        s.st_mode = 0644;
2348
0
        s.st_size = bim->size;
2349
0
      }
2350
0
    else if (stat (bfd_get_filename (sub), &s) != 0)
2351
0
      {
2352
0
        bfd_set_input_error (sub, bfd_error_system_call);
2353
0
        return false;
2354
0
      }
2355
2356
0
    if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2357
0
      {
2358
0
        s.st_mtime = 0;
2359
0
        s.st_uid = 0;
2360
0
        s.st_gid = 0;
2361
0
        s.st_mode = 0644;
2362
0
      }
2363
2364
0
    ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
2365
0
    if (ahdrp == NULL)
2366
0
      return false;
2367
2368
0
    sprintf (ahdrp->size, "%ld", (long) s.st_size);
2369
0
    sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2370
0
    sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2371
0
    sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2372
0
    sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2373
2374
0
    arch_eltdata (sub)->arch_header = (char *) ahdrp;
2375
0
    arch_eltdata (sub)->parsed_size = s.st_size;
2376
0
  }
2377
0
    }
2378
6
  offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2379
6
  if (offsets == NULL)
2380
0
    return false;
2381
2382
6
  if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
2383
0
    return false;
2384
2385
6
  makemap = bfd_has_map (abfd);
2386
6
  hasobjects = false;
2387
6
  prevoff = 0;
2388
6
  for (archive_iterator_begin (&iterator, abfd), i = 0;
2389
6
       archive_iterator_next (&iterator);
2390
6
       i++)
2391
0
    {
2392
0
      bfd_size_type namlen;
2393
0
      struct xcoff_ar_hdr *ahdrp;
2394
2395
0
      if (makemap && ! hasobjects)
2396
0
  {
2397
0
    if (bfd_check_format (iterator.current.member, bfd_object))
2398
0
      hasobjects = true;
2399
0
  }
2400
2401
0
      ahdrp = arch_xhdr (iterator.current.member);
2402
0
      sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2403
0
      sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
2404
0
      sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
2405
2406
      /* We need spaces, not null bytes, in the header.  */
2407
0
      for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2408
0
  if (*p == '\0')
2409
0
    *p = ' ';
2410
2411
0
      if (!do_pad (abfd, iterator.current.leading_padding))
2412
0
  return false;
2413
2414
0
      BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2415
0
      namlen = iterator.current.padded_namlen;
2416
0
      if (bfd_write (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2417
0
    || bfd_write (iterator.current.name, namlen, abfd) != namlen
2418
0
    || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2419
0
    || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2420
0
    || !do_copy (abfd, iterator.current.member)
2421
0
    || !do_pad (abfd, iterator.current.trailing_padding))
2422
0
  return false;
2423
2424
0
      offsets[i] = iterator.current.offset;
2425
0
      prevoff = iterator.current.offset;
2426
0
    }
2427
2428
6
  sprintf (fhdr->lastmemoff, "%ld", (long) prevoff);
2429
2430
  /* Write out the member table.  */
2431
2432
6
  nextoff = iterator.next.offset;
2433
6
  BFD_ASSERT (nextoff == bfd_tell (abfd));
2434
6
  sprintf (fhdr->memoff, "%ld", (long) nextoff);
2435
2436
6
  memset (&ahdr, 0, sizeof ahdr);
2437
6
  sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2438
6
             + count * XCOFFARMAG_ELEMENT_SIZE
2439
6
             + total_namlen));
2440
6
  sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2441
6
  sprintf (ahdr.date, "%d", 0);
2442
6
  sprintf (ahdr.uid, "%d", 0);
2443
6
  sprintf (ahdr.gid, "%d", 0);
2444
6
  sprintf (ahdr.mode, "%d", 0);
2445
6
  sprintf (ahdr.namlen, "%d", 0);
2446
2447
6
  size = (SIZEOF_AR_HDR
2448
6
    + XCOFFARMAG_ELEMENT_SIZE
2449
6
    + count * XCOFFARMAG_ELEMENT_SIZE
2450
6
    + total_namlen
2451
6
    + SXCOFFARFMAG);
2452
2453
6
  prevoff = nextoff;
2454
6
  nextoff += size + (size & 1);
2455
2456
6
  if (makemap && hasobjects)
2457
0
    sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2458
6
  else
2459
6
    sprintf (ahdr.nextoff, "%d", 0);
2460
2461
  /* We need spaces, not null bytes, in the header.  */
2462
534
  for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2463
528
    if (*p == '\0')
2464
474
      *p = ' ';
2465
2466
6
  if ((bfd_write (&ahdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
2467
6
      || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
2468
0
    return false;
2469
2470
6
  sprintf (decbuf, "%-12ld", (long) count);
2471
6
  if (bfd_write (decbuf, XCOFFARMAG_ELEMENT_SIZE, abfd)
2472
6
      != XCOFFARMAG_ELEMENT_SIZE)
2473
0
    return false;
2474
6
  for (i = 0; i < (size_t) count; i++)
2475
0
    {
2476
0
      sprintf (decbuf, "%-12ld", (long) offsets[i]);
2477
0
      if (bfd_write (decbuf, XCOFFARMAG_ELEMENT_SIZE, abfd)
2478
0
    != XCOFFARMAG_ELEMENT_SIZE)
2479
0
  return false;
2480
0
    }
2481
6
  for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2482
0
    {
2483
0
      const char *name;
2484
0
      bfd_size_type namlen;
2485
2486
0
      name = normalize_filename (sub);
2487
0
      namlen = strlen (name);
2488
0
      if (bfd_write (name, namlen + 1, abfd) != namlen + 1)
2489
0
  return false;
2490
0
    }
2491
2492
6
  if (! do_pad (abfd, size & 1))
2493
0
    return false;
2494
2495
  /* Write out the armap, if appropriate.  */
2496
6
  if (! makemap || ! hasobjects)
2497
6
    sprintf (fhdr->symoff, "%d", 0);
2498
0
  else
2499
0
    {
2500
0
      BFD_ASSERT (nextoff == bfd_tell (abfd));
2501
0
      sprintf (fhdr->symoff, "%ld", (long) nextoff);
2502
0
      bfd_ardata (abfd)->tdata = &xtdata;
2503
0
      bool ret = _bfd_compute_and_push_armap (abfd, 0, false,
2504
0
                _bfd_write_armap);
2505
0
      bfd_ardata (abfd)->tdata = NULL;
2506
0
      if (!ret)
2507
0
  return false;
2508
0
    }
2509
2510
  /* Write out the archive file header.  */
2511
2512
  /* We need spaces, not null bytes, in the header.  */
2513
414
  for (p = (char *) fhdr; p < (char *) fhdr + SIZEOF_AR_FILE_HDR; p++)
2514
408
    if (*p == '\0')
2515
318
      *p = ' ';
2516
2517
6
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
2518
6
      || (bfd_write (fhdr, SIZEOF_AR_FILE_HDR, abfd) != SIZEOF_AR_FILE_HDR))
2519
0
    return false;
2520
2521
6
  return true;
2522
6
}
2523
2524
static bool
2525
xcoff_write_archive_contents_big (bfd *abfd)
2526
68
{
2527
68
  struct xcoff_artdata xtdata;
2528
68
  struct xcoff_ar_file_hdr_big *fhdr = &xtdata.u.bhdr;
2529
68
  bfd_size_type count;
2530
68
  bfd_size_type total_namlen;
2531
68
  file_ptr *offsets;
2532
68
  bool makemap;
2533
68
  bool hasobjects;
2534
68
  file_ptr prevoff, nextoff;
2535
68
  bfd *current_bfd;
2536
68
  size_t i;
2537
68
  struct xcoff_ar_hdr_big *hdr;
2538
68
  bfd_size_type size;
2539
68
  char *member_table, *mt;
2540
68
  bfd_vma member_table_size;
2541
68
  struct archive_iterator iterator;
2542
2543
68
  memset (&xtdata, 0, sizeof (xtdata));
2544
68
  memcpy (fhdr->magic, XCOFFARMAGBIG, SXCOFFARMAG);
2545
2546
68
  if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2547
0
    return false;
2548
2549
  /* Calculate count and total_namlen.  */
2550
68
  makemap = bfd_has_map (abfd);
2551
68
  hasobjects = false;
2552
68
  for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2553
88
       current_bfd != NULL;
2554
68
       current_bfd = current_bfd->archive_next, count++)
2555
20
    {
2556
20
      total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2557
2558
20
      if (makemap
2559
12
    && ! hasobjects
2560
12
    && bfd_check_format (current_bfd, bfd_object))
2561
0
  hasobjects = true;
2562
2563
20
      if (current_bfd->arelt_data == NULL)
2564
20
  {
2565
20
    size = sizeof (struct areltdata);
2566
20
    current_bfd->arelt_data = bfd_zmalloc (size);
2567
20
    if (current_bfd->arelt_data == NULL)
2568
0
      return false;
2569
20
  }
2570
2571
20
      if (arch_xhdr_big (current_bfd) == NULL)
2572
20
  {
2573
20
    struct xcoff_ar_hdr_big *ahdrp;
2574
20
    struct stat s;
2575
2576
20
    if ((current_bfd->flags & BFD_IN_MEMORY) != 0)
2577
0
      {
2578
        /* Assume we just "made" the member, and fake it.  */
2579
0
        struct bfd_in_memory *bim
2580
0
    = (struct bfd_in_memory *) current_bfd->iostream;
2581
0
        time (&s.st_mtime);
2582
0
        s.st_uid = getuid ();
2583
0
        s.st_gid = getgid ();
2584
0
        s.st_mode = 0644;
2585
0
        s.st_size = bim->size;
2586
0
      }
2587
20
    else if (stat (bfd_get_filename (current_bfd), &s) != 0)
2588
0
      {
2589
0
        bfd_set_input_error (current_bfd, bfd_error_system_call);
2590
0
        return false;
2591
0
      }
2592
2593
20
    if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2594
0
      {
2595
0
        s.st_mtime = 0;
2596
0
        s.st_uid = 0;
2597
0
        s.st_gid = 0;
2598
0
        s.st_mode = 0644;
2599
0
      }
2600
2601
20
    ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
2602
20
    if (ahdrp == NULL)
2603
0
      return false;
2604
2605
20
    PRINT20 (ahdrp->size, s.st_size);
2606
20
    PRINT12 (ahdrp->date, s.st_mtime);
2607
20
    PRINT12 (ahdrp->uid,  s.st_uid);
2608
20
    PRINT12 (ahdrp->gid,  s.st_gid);
2609
20
    PRINT12_OCTAL (ahdrp->mode, s.st_mode);
2610
2611
20
    arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
2612
20
    arch_eltdata (current_bfd)->parsed_size = s.st_size;
2613
20
  }
2614
20
    }
2615
2616
68
  offsets = NULL;
2617
68
  if (count)
2618
15
    {
2619
15
      offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2620
15
      if (offsets == NULL)
2621
0
  return false;
2622
15
    }
2623
2624
68
  prevoff = 0;
2625
68
  for (archive_iterator_begin (&iterator, abfd), i = 0;
2626
88
       archive_iterator_next (&iterator);
2627
68
       i++)
2628
20
    {
2629
20
      bfd_size_type namlen;
2630
20
      struct xcoff_ar_hdr_big *ahdrp;
2631
2632
20
      ahdrp = arch_xhdr_big (iterator.current.member);
2633
20
      PRINT20 (ahdrp->prevoff, prevoff);
2634
20
      PRINT4 (ahdrp->namlen, iterator.current.namlen);
2635
20
      PRINT20 (ahdrp->nextoff, iterator.next.offset);
2636
2637
20
      if (!do_pad (abfd, iterator.current.leading_padding))
2638
0
  {
2639
0
    free (offsets);
2640
0
    return false;
2641
0
  }
2642
2643
20
      BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2644
20
      namlen = iterator.current.padded_namlen;
2645
20
      if (bfd_write (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2646
20
    || bfd_write (iterator.current.name, namlen, abfd) != namlen
2647
20
    || bfd_write (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2648
20
    || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2649
20
    || !do_copy (abfd, iterator.current.member)
2650
20
    || !do_pad (abfd, iterator.current.trailing_padding))
2651
0
  {
2652
0
    free (offsets);
2653
0
    return false;
2654
0
  }
2655
2656
20
      offsets[i] = iterator.current.offset;
2657
20
      prevoff = iterator.current.offset;
2658
20
    }
2659
2660
68
  if (count)
2661
15
    {
2662
15
      PRINT20 (fhdr->firstmemoff, offsets[0]);
2663
15
      PRINT20 (fhdr->lastmemoff, prevoff);
2664
15
    }
2665
2666
  /* Write out the member table.
2667
     Layout :
2668
2669
     standard big archive header
2670
     0x0000         ar_size [0x14]
2671
     0x0014         ar_nxtmem [0x14]
2672
     0x0028         ar_prvmem [0x14]
2673
     0x003C         ar_date [0x0C]
2674
     0x0048         ar_uid  [0x0C]
2675
     0x0054         ar_gid  [0x0C]
2676
     0x0060         ar_mod  [0x0C]
2677
     0x006C         ar_namelen[0x04]
2678
     0x0070         ar_fmag [0x02]
2679
2680
     Member table
2681
     0x0072         count [0x14]
2682
     0x0086         offsets [0x14 * counts]
2683
     0x0086 + 0x14 * counts   names [??]
2684
     ??           pad to even bytes.
2685
   */
2686
2687
68
  nextoff = iterator.next.offset;
2688
68
  BFD_ASSERT (nextoff == bfd_tell (abfd));
2689
2690
68
  member_table_size = (SIZEOF_AR_HDR_BIG
2691
68
           + SXCOFFARFMAG
2692
68
           + XCOFFARMAGBIG_ELEMENT_SIZE
2693
68
           + count * XCOFFARMAGBIG_ELEMENT_SIZE
2694
68
           + total_namlen);
2695
2696
68
  member_table_size += member_table_size & 1;
2697
68
  member_table = bfd_zmalloc (member_table_size);
2698
68
  if (member_table == NULL)
2699
0
    {
2700
0
      free (offsets);
2701
0
      return false;
2702
0
    }
2703
2704
68
  hdr = (struct xcoff_ar_hdr_big *) member_table;
2705
2706
68
  PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2707
68
           + count * XCOFFARMAGBIG_ELEMENT_SIZE
2708
68
           + total_namlen + (total_namlen & 1)));
2709
68
  if (makemap && hasobjects)
2710
0
    PRINT20 (hdr->nextoff, nextoff + member_table_size);
2711
68
  else
2712
68
    PRINT20 (hdr->nextoff, 0);
2713
68
  PRINT20 (hdr->prevoff, prevoff);
2714
68
  PRINT12 (hdr->date, 0);
2715
68
  PRINT12 (hdr->uid, 0);
2716
68
  PRINT12 (hdr->gid, 0);
2717
68
  PRINT12 (hdr->mode, 0);
2718
68
  PRINT4 (hdr->namlen, 0);
2719
2720
68
  mt = member_table + SIZEOF_AR_HDR_BIG;
2721
68
  memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2722
68
  mt += SXCOFFARFMAG;
2723
2724
68
  PRINT20 (mt, count);
2725
68
  mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2726
88
  for (i = 0; i < (size_t) count; i++)
2727
20
    {
2728
20
      PRINT20 (mt, offsets[i]);
2729
20
      mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2730
20
    }
2731
2732
68
  if (count)
2733
15
    {
2734
15
      free (offsets);
2735
15
      offsets = NULL;
2736
15
    }
2737
2738
68
  for (current_bfd = abfd->archive_head;
2739
88
       current_bfd != NULL;
2740
68
       current_bfd = current_bfd->archive_next)
2741
20
    {
2742
20
      const char *name;
2743
20
      size_t namlen;
2744
2745
20
      name = normalize_filename (current_bfd);
2746
20
      namlen = sprintf (mt, "%s", name);
2747
20
      mt += namlen + 1;
2748
20
    }
2749
2750
68
  if (bfd_write (member_table, member_table_size, abfd) != member_table_size)
2751
0
    return false;
2752
2753
68
  free (member_table);
2754
2755
68
  PRINT20 (fhdr->memoff, nextoff);
2756
2757
68
  prevoff = nextoff;
2758
68
  nextoff += member_table_size;
2759
2760
  /* Write out the armap, if appropriate.  */
2761
2762
68
  if (! makemap || ! hasobjects)
2763
68
    PRINT20 (fhdr->symoff, 0);
2764
0
  else
2765
0
    {
2766
0
      BFD_ASSERT (nextoff == bfd_tell (abfd));
2767
2768
      /* Save nextoff in fhdr->symoff so the armap routine can use it.  */
2769
0
      PRINT20 (fhdr->symoff, nextoff);
2770
2771
0
      bfd_ardata (abfd)->tdata = &xtdata;
2772
0
      bool ret = _bfd_compute_and_push_armap (abfd, 0, false,
2773
0
                _bfd_write_armap);
2774
0
      bfd_ardata (abfd)->tdata = NULL;
2775
0
      if (!ret)
2776
0
  return false;
2777
0
    }
2778
2779
  /* Write out the archive file header.  */
2780
2781
68
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
2782
68
      || (bfd_write (fhdr, SIZEOF_AR_FILE_HDR_BIG, abfd)
2783
68
    != SIZEOF_AR_FILE_HDR_BIG))
2784
0
    return false;
2785
2786
68
  return true;
2787
68
}
2788
2789
bool
2790
_bfd_xcoff_write_archive_contents (bfd *abfd)
2791
74
{
2792
74
  if (! xcoff_big_format_p (abfd))
2793
6
    return xcoff_write_archive_contents_old (abfd);
2794
68
  else
2795
68
    return xcoff_write_archive_contents_big (abfd);
2796
74
}
2797

2798
/* We can't use the usual coff_sizeof_headers routine, because AIX
2799
   always uses an a.out header.  */
2800
2801
int
2802
_bfd_xcoff_sizeof_headers (bfd *abfd,
2803
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
2804
0
{
2805
0
  int size;
2806
2807
0
  size = FILHSZ;
2808
0
  if (xcoff_data (abfd)->full_aouthdr)
2809
0
    size += AOUTSZ;
2810
0
  else
2811
0
    size += SMALL_AOUTSZ;
2812
0
  size += abfd->section_count * SCNHSZ;
2813
2814
0
  if (info->strip != strip_all)
2815
0
    {
2816
      /* There can be additional sections just for dealing with overflow in
2817
   reloc and lineno counts. But the numbers of relocs and lineno aren't
2818
   known when bfd_sizeof_headers is called, so we compute them by
2819
   summing the numbers from input sections.  */
2820
0
      struct nbr_reloc_lineno
2821
0
      {
2822
0
  unsigned int reloc_count;
2823
0
  unsigned int lineno_count;
2824
0
      };
2825
0
      struct nbr_reloc_lineno *n_rl;
2826
0
      bfd *sub;
2827
0
      unsigned int max_index;
2828
0
      asection *s;
2829
2830
      /* Although the number of sections is known, the maximum value of
2831
   section->index isn't (because some sections may have been removed).
2832
   Don't try to renumber sections, just compute the upper bound.  */
2833
0
      max_index = 0;
2834
0
      for (s = abfd->sections; s != NULL; s = s->next)
2835
0
  if (s->index > max_index)
2836
0
    max_index = s->index;
2837
2838
      /* Allocate the per section counters. It could be possible to use a
2839
   preallocated array as the number of sections is limited on XCOFF,
2840
   but this creates a maintainance issue.  */
2841
0
      n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
2842
0
      if (n_rl == NULL)
2843
0
  return -1;
2844
2845
      /* Sum.  */
2846
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2847
0
  for (s = sub->sections; s != NULL; s = s->next)
2848
0
    if (s->output_section->owner == abfd
2849
0
        && !bfd_section_removed_from_list (abfd, s->output_section))
2850
0
      {
2851
0
        struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
2852
0
        e->reloc_count += s->reloc_count;
2853
0
        e->lineno_count += s->lineno_count;
2854
0
      }
2855
2856
      /* Add the size of a section for each section with an overflow.  */
2857
0
      for (s = abfd->sections; s != NULL; s = s->next)
2858
0
  {
2859
0
    struct nbr_reloc_lineno *e = &n_rl[s->index];
2860
2861
0
    if (e->reloc_count >= 0xffff
2862
0
        || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
2863
0
      size += SCNHSZ;
2864
0
  }
2865
2866
0
      free (n_rl);
2867
0
    }
2868
2869
0
  return size;
2870
0
}
2871
2872
bool
2873
_bfd_xcoff_bfd_free_cached_info (bfd *abfd)
2874
4.41k
{
2875
  /* xcoff bfd_core does not have a coff_tdata.  */
2876
4.41k
  if (bfd_get_format (abfd) == bfd_object)
2877
3.77k
    return _bfd_coff_free_cached_info (abfd);
2878
2879
631
  return _bfd_generic_bfd_free_cached_info (abfd);
2880
4.41k
}
2881
2882
/* Routines to swap information in the XCOFF .loader section.  If we
2883
   ever need to write an XCOFF loader, this stuff will need to be
2884
   moved to another file shared by the linker (which XCOFF calls the
2885
   ``binder'') and the loader.  */
2886
2887
/* Swap in the ldhdr structure.  */
2888
2889
static void
2890
xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
2891
0
{
2892
0
  const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2893
2894
0
  dst->l_version = bfd_get_32 (abfd, src->l_version);
2895
0
  dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2896
0
  dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2897
0
  dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2898
0
  dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2899
0
  dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2900
0
  dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2901
0
  dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2902
0
}
2903
2904
/* Swap out the ldhdr structure.  */
2905
2906
static void
2907
xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
2908
0
{
2909
0
  struct external_ldhdr *dst = (struct external_ldhdr *) d;
2910
2911
0
  bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
2912
0
  bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2913
0
  bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2914
0
  bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2915
0
  bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2916
0
  bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2917
0
  bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2918
0
  bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2919
0
}
2920
2921
/* Swap in the ldsym structure.  */
2922
2923
static void
2924
xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
2925
0
{
2926
0
  const struct external_ldsym *src = (const struct external_ldsym *) s;
2927
2928
0
  if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2929
0
    memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2930
0
  } else {
2931
0
    dst->_l._l_l._l_zeroes = 0;
2932
0
    dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2933
0
  }
2934
0
  dst->l_value = bfd_get_32 (abfd, src->l_value);
2935
0
  dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2936
0
  dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2937
0
  dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2938
0
  dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2939
0
  dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2940
0
}
2941
2942
/* Swap out the ldsym structure.  */
2943
2944
static void
2945
xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
2946
0
{
2947
0
  struct external_ldsym *dst = (struct external_ldsym *) d;
2948
2949
0
  if (src->_l._l_l._l_zeroes != 0)
2950
0
    memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2951
0
  else
2952
0
    {
2953
0
      bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2954
0
      bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2955
0
      dst->_l._l_l._l_offset);
2956
0
    }
2957
0
  bfd_put_32 (abfd, src->l_value, dst->l_value);
2958
0
  bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
2959
0
  bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2960
0
  bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2961
0
  bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2962
0
  bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2963
0
}
2964
2965
static void
2966
xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
2967
168
{
2968
168
  struct external_reloc *src = (struct external_reloc *) s;
2969
168
  struct internal_reloc *dst = (struct internal_reloc *) d;
2970
2971
168
  memset (dst, 0, sizeof (struct internal_reloc));
2972
2973
168
  dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2974
168
  dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2975
168
  dst->r_size = bfd_get_8 (abfd, src->r_size);
2976
168
  dst->r_type = bfd_get_8 (abfd, src->r_type);
2977
168
}
2978
2979
static unsigned int
2980
xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
2981
0
{
2982
0
  struct internal_reloc *src = (struct internal_reloc *) s;
2983
0
  struct external_reloc *dst = (struct external_reloc *) d;
2984
2985
0
  bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2986
0
  bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2987
0
  bfd_put_8 (abfd, src->r_type, dst->r_type);
2988
0
  bfd_put_8 (abfd, src->r_size, dst->r_size);
2989
2990
0
  return bfd_coff_relsz (abfd);
2991
0
}
2992
2993
/* Swap in the ldrel structure.  */
2994
2995
static void
2996
xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
2997
0
{
2998
0
  const struct external_ldrel *src = (const struct external_ldrel *) s;
2999
3000
0
  dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
3001
0
  dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
3002
0
  dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
3003
0
  dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
3004
0
}
3005
3006
/* Swap out the ldrel structure.  */
3007
3008
static void
3009
xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
3010
0
{
3011
0
  struct external_ldrel *dst = (struct external_ldrel *) d;
3012
3013
0
  bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
3014
0
  bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
3015
0
  bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
3016
0
  bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
3017
0
}
3018

3019
bfd_reloc_status_type
3020
xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
3021
           asection *input_section ATTRIBUTE_UNUSED,
3022
           bfd *output_bfd ATTRIBUTE_UNUSED,
3023
           struct internal_reloc *rel ATTRIBUTE_UNUSED,
3024
           struct internal_syment *sym ATTRIBUTE_UNUSED,
3025
           struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3026
           bfd_vma val ATTRIBUTE_UNUSED,
3027
           bfd_vma addend ATTRIBUTE_UNUSED,
3028
           bfd_vma *relocation ATTRIBUTE_UNUSED,
3029
           bfd_byte *contents ATTRIBUTE_UNUSED,
3030
           struct bfd_link_info *info ATTRIBUTE_UNUSED)
3031
0
{
3032
0
  return bfd_reloc_ok;
3033
0
}
3034
3035
bfd_reloc_status_type
3036
xcoff_reloc_type_fail (bfd *input_bfd ATTRIBUTE_UNUSED,
3037
           asection *input_section ATTRIBUTE_UNUSED,
3038
           bfd *output_bfd ATTRIBUTE_UNUSED,
3039
           struct internal_reloc *rel ATTRIBUTE_UNUSED,
3040
           struct internal_syment *sym ATTRIBUTE_UNUSED,
3041
           struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3042
           bfd_vma val ATTRIBUTE_UNUSED,
3043
           bfd_vma addend ATTRIBUTE_UNUSED,
3044
           bfd_vma *relocation ATTRIBUTE_UNUSED,
3045
           bfd_byte *contents ATTRIBUTE_UNUSED,
3046
           struct bfd_link_info *info ATTRIBUTE_UNUSED)
3047
0
{
3048
0
  return bfd_reloc_notsupported;
3049
0
}
3050
3051
bfd_reloc_status_type
3052
xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
3053
          asection *input_section ATTRIBUTE_UNUSED,
3054
          bfd *output_bfd ATTRIBUTE_UNUSED,
3055
          struct internal_reloc *rel ATTRIBUTE_UNUSED,
3056
          struct internal_syment *sym ATTRIBUTE_UNUSED,
3057
          struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3058
          bfd_vma val,
3059
          bfd_vma addend,
3060
          bfd_vma *relocation,
3061
          bfd_byte *contents ATTRIBUTE_UNUSED,
3062
          struct bfd_link_info *info ATTRIBUTE_UNUSED)
3063
0
{
3064
0
  *relocation = val + addend;
3065
0
  return bfd_reloc_ok;
3066
0
}
3067
3068
bfd_reloc_status_type
3069
xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
3070
          asection *input_section ATTRIBUTE_UNUSED,
3071
          bfd *output_bfd ATTRIBUTE_UNUSED,
3072
          struct internal_reloc *rel ATTRIBUTE_UNUSED,
3073
          struct internal_syment *sym ATTRIBUTE_UNUSED,
3074
          struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3075
          bfd_vma val,
3076
          bfd_vma addend,
3077
          bfd_vma *relocation,
3078
          bfd_byte *contents ATTRIBUTE_UNUSED,
3079
          struct bfd_link_info *info ATTRIBUTE_UNUSED)
3080
0
{
3081
0
  *relocation = - val - addend;
3082
0
  return bfd_reloc_ok;
3083
0
}
3084
3085
bfd_reloc_status_type
3086
xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
3087
          asection *input_section,
3088
          bfd *output_bfd ATTRIBUTE_UNUSED,
3089
          struct internal_reloc *rel ATTRIBUTE_UNUSED,
3090
          struct internal_syment *sym ATTRIBUTE_UNUSED,
3091
          struct reloc_howto_struct *howto,
3092
          bfd_vma val,
3093
          bfd_vma addend,
3094
          bfd_vma *relocation,
3095
          bfd_byte *contents ATTRIBUTE_UNUSED,
3096
          struct bfd_link_info *info ATTRIBUTE_UNUSED)
3097
0
{
3098
0
  howto->pc_relative = true;
3099
3100
  /* A PC relative reloc includes the section address.  */
3101
0
  addend += input_section->vma;
3102
3103
0
  *relocation = val + addend;
3104
0
  *relocation -= (input_section->output_section->vma
3105
0
      + input_section->output_offset);
3106
0
  return bfd_reloc_ok;
3107
0
}
3108
3109
bfd_reloc_status_type
3110
xcoff_reloc_type_toc (bfd *input_bfd,
3111
          asection *input_section ATTRIBUTE_UNUSED,
3112
          bfd *output_bfd,
3113
          struct internal_reloc *rel,
3114
          struct internal_syment *sym ATTRIBUTE_UNUSED,
3115
          struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3116
          bfd_vma val,
3117
          bfd_vma addend ATTRIBUTE_UNUSED,
3118
          bfd_vma *relocation,
3119
          bfd_byte *contents ATTRIBUTE_UNUSED,
3120
          struct bfd_link_info *info ATTRIBUTE_UNUSED)
3121
0
{
3122
0
  struct xcoff_link_hash_entry *h;
3123
3124
0
  if ((unsigned long) rel->r_symndx >= obj_raw_syment_count (input_bfd))
3125
0
    return bfd_reloc_undefined;
3126
3127
0
  h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3128
3129
0
  if (h != NULL && h->smclas != XMC_TD)
3130
0
    {
3131
0
      if (h->toc_section == NULL)
3132
0
  {
3133
0
    _bfd_error_handler
3134
      /* xgettext: c-format */
3135
0
      (_("%pB: TOC reloc at %#" PRIx64 " to symbol `%s' with no TOC entry"),
3136
0
       input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
3137
0
    return bfd_reloc_undefined;
3138
0
  }
3139
3140
0
      BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
3141
0
      val = (h->toc_section->output_section->vma
3142
0
        + h->toc_section->output_offset);
3143
0
    }
3144
3145
  /* We can't use the preexisting value written down by the
3146
     assembly, as R_TOCU needs to be adjusted when the final
3147
     R_TOCL value is signed.  */
3148
0
  val -= xcoff_data (output_bfd)->toc;
3149
3150
0
  if (rel->r_type == R_TOCU)
3151
0
    val = ((val + 0x8000) >> 16) & 0xffff;
3152
0
  else if (rel->r_type == R_TOCL)
3153
0
    val = val & 0xffff;
3154
3155
0
  *relocation = val;
3156
0
  return bfd_reloc_ok;
3157
0
}
3158
3159
bfd_reloc_status_type
3160
xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
3161
         asection *input_section ATTRIBUTE_UNUSED,
3162
         bfd *output_bfd ATTRIBUTE_UNUSED,
3163
         struct internal_reloc *rel ATTRIBUTE_UNUSED,
3164
         struct internal_syment *sym ATTRIBUTE_UNUSED,
3165
         struct reloc_howto_struct *howto,
3166
         bfd_vma val,
3167
         bfd_vma addend,
3168
         bfd_vma *relocation,
3169
         bfd_byte *contents ATTRIBUTE_UNUSED,
3170
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
3171
0
{
3172
0
  howto->src_mask &= ~3;
3173
0
  howto->dst_mask = howto->src_mask;
3174
3175
0
  *relocation = val + addend;
3176
3177
0
  return bfd_reloc_ok;
3178
0
}
3179
3180
static bfd_reloc_status_type
3181
xcoff_reloc_type_br (bfd *input_bfd,
3182
         asection *input_section,
3183
         bfd *output_bfd ATTRIBUTE_UNUSED,
3184
         struct internal_reloc *rel,
3185
         struct internal_syment *sym ATTRIBUTE_UNUSED,
3186
         struct reloc_howto_struct *howto,
3187
         bfd_vma val,
3188
         bfd_vma addend,
3189
         bfd_vma *relocation,
3190
         bfd_byte *contents,
3191
         struct bfd_link_info *info)
3192
0
{
3193
0
  struct xcoff_link_hash_entry *h;
3194
0
  bfd_vma section_offset;
3195
0
  struct xcoff_stub_hash_entry *stub_entry = NULL;
3196
0
  enum xcoff_stub_type stub_type;
3197
3198
0
  if ((unsigned long) rel->r_symndx >= obj_raw_syment_count (input_bfd))
3199
0
    return bfd_reloc_undefined;
3200
3201
0
  h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3202
0
  section_offset = rel->r_vaddr - input_section->vma;
3203
3204
  /* If we see an R_BR or R_RBR reloc which is jumping to global
3205
     linkage code, and it is followed by an appropriate cror nop
3206
     instruction, we replace the cror with lwz r2,20(r1).  This
3207
     restores the TOC after the glink code.  Contrariwise, if the
3208
     call is followed by a lwz r2,20(r1), but the call is not
3209
     going to global linkage code, we can replace the load with a
3210
     cror.  */
3211
0
  if (NULL != h
3212
0
      && (bfd_link_hash_defined == h->root.type
3213
0
    || bfd_link_hash_defweak == h->root.type)
3214
0
      && section_offset + 8 <= input_section->size)
3215
0
    {
3216
0
      bfd_byte *pnext;
3217
0
      unsigned long next;
3218
3219
0
      pnext = contents + section_offset + 4;
3220
0
      next = bfd_get_32 (input_bfd, pnext);
3221
3222
      /* The _ptrgl function is magic.  It is used by the AIX
3223
   compiler to call a function through a pointer.  */
3224
0
      if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
3225
0
  {
3226
0
    if (next == 0x4def7b82      /* cror 15,15,15 */
3227
0
        || next == 0x4ffffb82      /* cror 31,31,31 */
3228
0
        || next == 0x60000000)     /* ori r0,r0,0 */
3229
0
      bfd_put_32 (input_bfd, 0x80410014, pnext);  /* lwz r2,20(r1) */
3230
3231
0
  }
3232
0
      else
3233
0
  {
3234
0
    if (next == 0x80410014)     /* lwz r2,20(r1) */
3235
0
      bfd_put_32 (input_bfd, 0x60000000, pnext);  /* ori r0,r0,0 */
3236
0
  }
3237
0
    }
3238
0
  else if (NULL != h && bfd_link_hash_undefined == h->root.type)
3239
0
    {
3240
      /* Normally, this relocation is against a defined symbol.  In the
3241
   case where this is a partial link and the output section offset
3242
   is greater than 2^25, the linker will return an invalid error
3243
   message that the relocation has been truncated.  Yes it has been
3244
   truncated but no it not important.  For this case, disable the
3245
   overflow checking. */
3246
3247
0
      howto->complain_on_overflow = complain_overflow_dont;
3248
0
    }
3249
3250
  /* Check if a stub is needed.  */
3251
0
  stub_type = bfd_xcoff_type_of_stub (input_section, rel, val, h);
3252
0
  if (stub_type != xcoff_stub_none)
3253
0
    {
3254
0
      asection *stub_csect;
3255
3256
0
      stub_entry = bfd_xcoff_get_stub_entry (input_section, h, info);
3257
0
      if (stub_entry == NULL)
3258
0
  {
3259
0
    _bfd_error_handler (_("Unable to find the stub entry targeting %s"),
3260
0
            h->root.root.string);
3261
0
    return bfd_reloc_undefined;
3262
0
  }
3263
3264
0
      stub_csect = stub_entry->hcsect->root.u.def.section;
3265
0
      val = (stub_entry->stub_offset
3266
0
       + stub_csect->output_section->vma
3267
0
       + stub_csect->output_offset);
3268
0
    }
3269
3270
  /* The original PC-relative relocation is biased by -r_vaddr, so adding
3271
     the value below will give the absolute target address.  */
3272
0
  *relocation = val + addend + rel->r_vaddr;
3273
3274
0
  howto->src_mask &= ~3;
3275
0
  howto->dst_mask = howto->src_mask;
3276
3277
0
  if (h != NULL
3278
0
      && (h->root.type == bfd_link_hash_defined
3279
0
    || h->root.type == bfd_link_hash_defweak)
3280
0
      && bfd_is_abs_section (h->root.u.def.section)
3281
0
      && section_offset + 4 <= input_section->size)
3282
0
    {
3283
0
      bfd_byte *ptr;
3284
0
      bfd_vma insn;
3285
3286
      /* Turn the relative branch into an absolute one by setting the
3287
   AA bit.  */
3288
0
      ptr = contents + section_offset;
3289
0
      insn = bfd_get_32 (input_bfd, ptr);
3290
0
      insn |= 2;
3291
0
      bfd_put_32 (input_bfd, insn, ptr);
3292
3293
      /* Make the howto absolute too.  */
3294
0
      howto->pc_relative = false;
3295
0
      howto->complain_on_overflow = complain_overflow_bitfield;
3296
0
    }
3297
0
  else
3298
0
    {
3299
      /* Use a PC-relative howto and subtract the instruction's address
3300
   from the target address we calculated above.  */
3301
0
      howto->pc_relative = true;
3302
0
      *relocation -= (input_section->output_section->vma
3303
0
          + input_section->output_offset
3304
0
          + section_offset);
3305
0
    }
3306
0
  return bfd_reloc_ok;
3307
0
}
3308
3309
bfd_reloc_status_type
3310
xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
3311
           asection *input_section,
3312
           bfd *output_bfd ATTRIBUTE_UNUSED,
3313
           struct internal_reloc *rel ATTRIBUTE_UNUSED,
3314
           struct internal_syment *sym ATTRIBUTE_UNUSED,
3315
           struct reloc_howto_struct *howto,
3316
           bfd_vma val ATTRIBUTE_UNUSED,
3317
           bfd_vma addend,
3318
           bfd_vma *relocation,
3319
           bfd_byte *contents ATTRIBUTE_UNUSED,
3320
           struct bfd_link_info *info ATTRIBUTE_UNUSED)
3321
0
{
3322
0
  howto->pc_relative = true;
3323
0
  howto->src_mask &= ~3;
3324
0
  howto->dst_mask = howto->src_mask;
3325
3326
  /* A PC relative reloc includes the section address.  */
3327
0
  addend += input_section->vma;
3328
3329
0
  *relocation = val + addend;
3330
0
  *relocation -= (input_section->output_section->vma
3331
0
      + input_section->output_offset);
3332
0
  return bfd_reloc_ok;
3333
0
}
3334
3335
bfd_reloc_status_type
3336
xcoff_reloc_type_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
3337
          asection *input_section ATTRIBUTE_UNUSED,
3338
          bfd *output_bfd ATTRIBUTE_UNUSED,
3339
          struct internal_reloc *rel ATTRIBUTE_UNUSED,
3340
          struct internal_syment *sym ATTRIBUTE_UNUSED,
3341
          struct reloc_howto_struct *howto,
3342
          bfd_vma val,
3343
          bfd_vma addend,
3344
          bfd_vma *relocation,
3345
          bfd_byte *contents ATTRIBUTE_UNUSED,
3346
          struct bfd_link_info *info ATTRIBUTE_UNUSED)
3347
0
{
3348
0
  struct xcoff_link_hash_entry *h;
3349
3350
0
  if ((unsigned long) rel->r_symndx >= obj_raw_syment_count (input_bfd))
3351
0
    return bfd_reloc_undefined;
3352
3353
0
  h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3354
3355
  /* R_TLSML is handled by the loader but must be from a
3356
     TOC entry targeting itslef.  This is already verified in
3357
     xcoff_link_add_symbols.
3358
     The value must be 0.  */
3359
0
  if (howto->type == R_TLSML)
3360
0
    {
3361
0
      *relocation = 0;
3362
0
      return bfd_reloc_ok;
3363
0
    }
3364
3365
  /* The target symbol should always be available even if it's not
3366
     exported.  */
3367
0
  if (h == NULL)
3368
0
    return bfd_reloc_undefined;
3369
3370
  /* TLS relocations must target a TLS symbol.  */
3371
0
  if (h->smclas != XMC_TL && h->smclas != XMC_UL)
3372
0
    {
3373
0
      _bfd_error_handler
3374
0
  (_("%pB: TLS relocation at 0x%" PRIx64 " over non-TLS symbol %s (0x%x)\n"),
3375
0
   input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string, h->smclas);
3376
0
      return bfd_reloc_undefined;
3377
0
    }
3378
3379
  /* Local TLS relocations must target a local symbol, ie
3380
     non-imported. */
3381
0
  if ((rel->r_type == R_TLS_LD || rel->r_type == R_TLS_LE)
3382
0
      && (((h->flags & XCOFF_DEF_REGULAR) == 0
3383
0
       && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3384
0
    || (h->flags & XCOFF_IMPORT) != 0))
3385
0
    {
3386
0
      _bfd_error_handler
3387
0
  (_("%pB: TLS local relocation at 0x%" PRIx64 " over imported symbol %s\n"),
3388
0
   input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
3389
0
      return bfd_reloc_undefined;
3390
0
    }
3391
3392
  /* R_TLSM are relocations used by the loader.
3393
     The value must be 0.  */
3394
0
  if (howto->type == R_TLSM)
3395
0
    {
3396
0
      *relocation = 0;
3397
0
      return bfd_reloc_ok;
3398
0
    }
3399
3400
  /* Other TLS relocations aims to put offsets from TLS pointers
3401
     starting at -0x7c00 (or -0x7800 in XCOFF64).  It becomes a
3402
     simple R_POS relocation as long as .tdata and .tbss addresses
3403
     start at the same value. This is done in aix ld scripts.
3404
     TODO: implement optimization when tls size is < 62K.  */
3405
0
  *relocation = val + addend;
3406
3407
0
  return bfd_reloc_ok;
3408
0
}
3409
3410
static bool
3411
xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
3412
           bfd_vma val ATTRIBUTE_UNUSED,
3413
           bfd_vma relocation ATTRIBUTE_UNUSED,
3414
           struct reloc_howto_struct *
3415
              howto ATTRIBUTE_UNUSED)
3416
0
{
3417
0
  return false;
3418
0
}
3419
3420
static bool
3421
xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3422
               bfd_vma val,
3423
               bfd_vma relocation,
3424
               struct reloc_howto_struct *howto)
3425
0
{
3426
0
  bfd_vma fieldmask, signmask, ss;
3427
0
  bfd_vma a, b, sum;
3428
3429
  /* Get the values to be added together.  For signed and unsigned
3430
     relocations, we assume that all values should be truncated to
3431
     the size of an address.  For bitfields, all the bits matter.
3432
     See also bfd_check_overflow.  */
3433
0
  fieldmask = N_ONES (howto->bitsize);
3434
0
  a = relocation;
3435
0
  b = val & howto->src_mask;
3436
3437
  /* Much like unsigned, except no trimming with addrmask.  In
3438
     addition, the sum overflows if there is a carry out of
3439
     the bfd_vma, i.e., the sum is less than either input
3440
     operand.  */
3441
0
  a >>= howto->rightshift;
3442
0
  b >>= howto->bitpos;
3443
3444
  /* Bitfields are sometimes used for signed numbers; for
3445
     example, a 13-bit field sometimes represents values in
3446
     0..8191 and sometimes represents values in -4096..4095.
3447
     If the field is signed and a is -4095 (0x1001) and b is
3448
     -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3449
     0x1fff is 0x3000).  It's not clear how to handle this
3450
     everywhere, since there is not way to know how many bits
3451
     are significant in the relocation, but the original code
3452
     assumed that it was fully sign extended, and we will keep
3453
     that assumption.  */
3454
0
  signmask = (fieldmask >> 1) + 1;
3455
3456
0
  if ((a & ~ fieldmask) != 0)
3457
0
    {
3458
      /* Some bits out of the field are set.  This might not
3459
   be a problem: if this is a signed bitfield, it is OK
3460
   iff all the high bits are set, including the sign
3461
   bit.  We'll try setting all but the most significant
3462
   bit in the original relocation value: if this is all
3463
   ones, we are OK, assuming a signed bitfield.  */
3464
0
      ss = (signmask << howto->rightshift) - 1;
3465
0
      if ((ss | relocation) != ~ (bfd_vma) 0)
3466
0
  return true;
3467
0
      a &= fieldmask;
3468
0
    }
3469
3470
  /* We just assume (b & ~ fieldmask) == 0.  */
3471
3472
  /* We explicitly permit wrap around if this relocation
3473
     covers the high bit of an address.  The Linux kernel
3474
     relies on it, and it is the only way to write assembler
3475
     code which can run when loaded at a location 0x80000000
3476
     away from the location at which it is linked.  */
3477
0
  if ((unsigned) howto->bitsize + howto->rightshift
3478
0
      == bfd_arch_bits_per_address (input_bfd))
3479
0
    return false;
3480
3481
0
  sum = a + b;
3482
0
  if (sum < a || (sum & ~ fieldmask) != 0)
3483
0
    {
3484
      /* There was a carry out, or the field overflow.  Test
3485
   for signed operands again.  Here is the overflow test
3486
   is as for complain_overflow_signed.  */
3487
0
      if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3488
0
  return true;
3489
0
    }
3490
3491
0
  return false;
3492
0
}
3493
3494
static bool
3495
xcoff_complain_overflow_signed_func (bfd *input_bfd,
3496
             bfd_vma val,
3497
             bfd_vma relocation,
3498
             struct reloc_howto_struct *howto)
3499
0
{
3500
0
  bfd_vma addrmask, fieldmask, signmask, ss;
3501
0
  bfd_vma a, b, sum;
3502
3503
  /* Get the values to be added together.  For signed and unsigned
3504
     relocations, we assume that all values should be truncated to
3505
     the size of an address.  For bitfields, all the bits matter.
3506
     See also bfd_check_overflow.  */
3507
0
  fieldmask = N_ONES (howto->bitsize);
3508
0
  addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3509
0
  a = relocation;
3510
0
  b = val & howto->src_mask;
3511
3512
0
  a = (a & addrmask) >> howto->rightshift;
3513
3514
  /* If any sign bits are set, all sign bits must be set.
3515
     That is, A must be a valid negative address after
3516
     shifting.  */
3517
0
  signmask = ~ (fieldmask >> 1);
3518
0
  ss = a & signmask;
3519
0
  if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
3520
0
    return true;
3521
3522
  /* We only need this next bit of code if the sign bit of B
3523
     is below the sign bit of A.  This would only happen if
3524
     SRC_MASK had fewer bits than BITSIZE.  Note that if
3525
     SRC_MASK has more bits than BITSIZE, we can get into
3526
     trouble; we would need to verify that B is in range, as
3527
     we do for A above.  */
3528
0
  signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3529
0
  if ((b & signmask) != 0)
3530
0
    {
3531
      /* Set all the bits above the sign bit.  */
3532
0
      b -= signmask <<= 1;
3533
0
    }
3534
3535
0
  b = (b & addrmask) >> howto->bitpos;
3536
3537
  /* Now we can do the addition.  */
3538
0
  sum = a + b;
3539
3540
  /* See if the result has the correct sign.  Bits above the
3541
     sign bit are junk now; ignore them.  If the sum is
3542
     positive, make sure we did not have all negative inputs;
3543
     if the sum is negative, make sure we did not have all
3544
     positive inputs.  The test below looks only at the sign
3545
     bits, and it really just
3546
     SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3547
  */
3548
0
  signmask = (fieldmask >> 1) + 1;
3549
0
  if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3550
0
    return true;
3551
3552
0
  return false;
3553
0
}
3554
3555
static bool
3556
xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3557
               bfd_vma val,
3558
               bfd_vma relocation,
3559
               struct reloc_howto_struct *howto)
3560
0
{
3561
0
  bfd_vma addrmask, fieldmask;
3562
0
  bfd_vma a, b, sum;
3563
3564
  /* Get the values to be added together.  For signed and unsigned
3565
     relocations, we assume that all values should be truncated to
3566
     the size of an address.  For bitfields, all the bits matter.
3567
     See also bfd_check_overflow.  */
3568
0
  fieldmask = N_ONES (howto->bitsize);
3569
0
  addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3570
0
  a = relocation;
3571
0
  b = val & howto->src_mask;
3572
3573
  /* Checking for an unsigned overflow is relatively easy:
3574
     trim the addresses and add, and trim the result as well.
3575
     Overflow is normally indicated when the result does not
3576
     fit in the field.  However, we also need to consider the
3577
     case when, e.g., fieldmask is 0x7fffffff or smaller, an
3578
     input is 0x80000000, and bfd_vma is only 32 bits; then we
3579
     will get sum == 0, but there is an overflow, since the
3580
     inputs did not fit in the field.  Instead of doing a
3581
     separate test, we can check for this by or-ing in the
3582
     operands when testing for the sum overflowing its final
3583
     field.  */
3584
0
  a = (a & addrmask) >> howto->rightshift;
3585
0
  b = (b & addrmask) >> howto->bitpos;
3586
0
  sum = (a + b) & addrmask;
3587
0
  if ((a | b | sum) & ~ fieldmask)
3588
0
    return true;
3589
3590
0
  return false;
3591
0
}
3592
3593
/* This is the relocation function for the RS/6000/POWER/PowerPC.
3594
   This is currently the only processor which uses XCOFF; I hope that
3595
   will never change.
3596
3597
   The original version was based on two documents:
3598
   the PowerPC AIX Version 4 Application Binary Interface, First
3599
   Edition (April 1992), and the PowerOpen ABI, Big-Endian
3600
   32-Bit Hardware Implementation (June 30, 1994).  Differences
3601
   between the documents are noted below.
3602
   Now, IBM has released an official documentation about XCOFF
3603
   format:
3604
   https://www.ibm.com/support/knowledgecenter/ssw_aix_72/filesreference/XCOFF.html
3605
3606
   Unsupported r_type's
3607
3608
   R_RTB:
3609
   R_RRTBI:
3610
   R_RRTBA:
3611
3612
   These relocs are defined by the PowerPC ABI to be
3613
   relative branches which use half of the difference
3614
   between the symbol and the program counter.  I can't
3615
   quite figure out when this is useful.  These relocs are
3616
   not defined by the PowerOpen ABI.
3617
3618
   Supported r_type's
3619
3620
   R_POS:
3621
   Simple positive relocation.
3622
3623
   R_NEG:
3624
   Simple negative relocation.
3625
3626
   R_REL:
3627
   Simple PC relative relocation.
3628
3629
   R_TOC:
3630
   TOC relative relocation.  The value in the instruction in
3631
   the input file is the offset from the input file TOC to
3632
   the desired location.  We want the offset from the final
3633
   TOC to the desired location.  We have:
3634
   isym = iTOC + in
3635
   iinsn = in + o
3636
   osym = oTOC + on
3637
   oinsn = on + o
3638
   so we must change insn by on - in.
3639
   This relocation allows the linker to perform optimizations
3640
   by transforming a load instruction into a add-immediate
3641
   when possible. The relocation is, then, changed to R_TRLA
3642
   in the output file.
3643
   TODO: Currently, the optimisation isn't implemented.
3644
3645
   R_TRL:
3646
   TOC relative relocation.  Same as R_TOC, except that
3647
   the optimization isn't allowed
3648
3649
   R_TRLA:
3650
   TOC relative relocation.  This is a TOC relative load
3651
   address instruction which have been changed to an add-
3652
   immediate instruction.
3653
3654
   R_GL:
3655
   GL linkage relocation.  The value of this relocation
3656
   is the address of the external symbol in the TOC
3657
   section.
3658
3659
   R_TCL:
3660
   Local object TOC address.  I can't figure out the
3661
   difference between this and case R_GL.
3662
3663
   R_RL:
3664
   The PowerPC AIX ABI describes this as a load which may be
3665
   changed to a load address.  The PowerOpen ABI says this
3666
   is the same as case R_POS.
3667
3668
   R_RLA:
3669
   The PowerPC AIX ABI describes this as a load address
3670
   which may be changed to a load.  The PowerOpen ABI says
3671
   this is the same as R_POS.
3672
3673
   R_REF:
3674
   Not a relocation but a way to prevent the garbage
3675
   collector of AIX linker to remove symbols.
3676
   This is not needed in our case.
3677
3678
   R_BA:
3679
   The PowerOpen ABI says this is the same as R_RBA.
3680
3681
   R_RBA:
3682
   Absolute branch which may be modified to become a
3683
   relative branch.
3684
3685
   R_BR:
3686
   The PowerOpen ABI says this is the same as R_RBR.
3687
3688
   R_RBR:
3689
   A relative branch which may be modified to become an
3690
   absolute branch.
3691
3692
   R_CAI:
3693
   The PowerPC ABI defines this as an absolute call which
3694
   may be modified to become a relative call.  The PowerOpen
3695
   ABI does not define this relocation type.
3696
3697
   R_CREL:
3698
   The PowerPC ABI defines this as a relative call which may
3699
   be modified to become an absolute call.  The PowerOpen
3700
   ABI does not define this relocation type.
3701
3702
   R_RBAC:
3703
   The PowerPC ABI defines this as an absolute branch to a
3704
   fixed address which may be modified to an absolute branch
3705
   to a symbol.  The PowerOpen ABI does not define this
3706
   relocation type.
3707
3708
   R_RBRC:
3709
   The PowerPC ABI defines this as an absolute branch to a
3710
   fixed address which may be modified to a relative branch.
3711
   The PowerOpen ABI does not define this relocation type.
3712
3713
   R_TLS:
3714
   Thread-local storage relocation using general-dynamic
3715
   model.
3716
3717
   R_TLS_IE:
3718
   Thread-local storage relocation using initial-exec model.
3719
3720
   R_TLS_LD:
3721
   Thread-local storage relocation using local-dynamic model.
3722
3723
   R_TLS_LE:
3724
   Thread-local storage relocation using local-exec model.
3725
3726
   R_TLSM:
3727
   Tread-local storage relocation used by the loader.
3728
3729
   R_TLSML:
3730
   Tread-local storage relocation used by the loader.
3731
3732
   R_TOCU:
3733
   Upper TOC relative relocation. The value is the
3734
   high-order 16 bit of a TOC relative relocation.
3735
3736
   R_TOCL:
3737
   Lower TOC relative relocation. The value is the
3738
   low-order 16 bit of a TOC relative relocation.
3739
*/
3740
3741
bool
3742
_bfd_xcoff_relocate_section (bfd *output_bfd,
3743
           struct bfd_link_info *info,
3744
           bfd *input_bfd,
3745
           asection *input_section,
3746
           bfd_byte *contents,
3747
           struct internal_reloc *relocs,
3748
           struct internal_syment *syms,
3749
           asection **sections,
3750
           bool xcoff64,
3751
           size_t num_howtos,
3752
           const struct reloc_howto_struct *howto_table,
3753
           xcoff_reloc_function *const *calc_reloc)
3754
0
{
3755
0
  struct internal_reloc *rel = relocs;
3756
0
  struct internal_reloc *relend = rel + input_section->reloc_count;
3757
3758
0
  for (; rel < relend; rel++)
3759
0
    {
3760
0
      long symndx;
3761
0
      struct xcoff_link_hash_entry *h = NULL;
3762
0
      struct internal_syment *sym = NULL;
3763
0
      bfd_vma addend;
3764
0
      bfd_vma val;
3765
0
      struct reloc_howto_struct howto;
3766
0
      bfd_reloc_status_type r;
3767
0
      bfd_vma address = rel->r_vaddr - input_section->vma;
3768
0
      bool fail = false;
3769
3770
      /* Relocation type R_REF is a special relocation type which is
3771
   merely used to prevent garbage collection from occurring for
3772
   the csect including the symbol which it references.  */
3773
0
      if (rel->r_type == R_REF)
3774
0
  continue;
3775
0
      if (rel->r_type >= num_howtos)
3776
0
  {
3777
0
    r = bfd_reloc_notsupported;
3778
0
    goto report_error;
3779
0
  }
3780
3781
      /* Retrieve default value in HOWTO table and fix up according
3782
   to r_size field, if it can be different.
3783
   This should be made during relocation reading but the algorithms
3784
   are expecting constant howtos.  */
3785
0
      memcpy (&howto, howto_table + rel->r_type, sizeof (howto));
3786
0
      if (howto.bitsize != (rel->r_size & (xcoff64 ? 0x3f : 0x1f)) + 1)
3787
0
  {
3788
0
    switch (rel->r_type)
3789
0
      {
3790
0
      case R_POS:
3791
0
      case R_NEG:
3792
0
        howto.bitsize = (rel->r_size & (xcoff64 ? 0x3f : 0x1f)) + 1;
3793
0
        howto.size = HOWTO_RSIZE (howto.bitsize <= 16 ? 2
3794
0
          : !xcoff64 || howto.bitsize <= 32 ? 4
3795
0
          : 8);
3796
0
        howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
3797
0
        break;
3798
3799
0
      default:
3800
0
        _bfd_error_handler
3801
0
    (_("%pB: relocation (%#x) at 0x%" PRIx64
3802
0
       " has wrong r_rsize (0x%x)\n"),
3803
0
     input_bfd, rel->r_type, (uint64_t) rel->r_vaddr, rel->r_size);
3804
0
        r = bfd_reloc_notsupported;
3805
0
        goto report_error;
3806
0
      }
3807
0
  }
3808
3809
0
      howto.complain_on_overflow = (rel->r_size & 0x80
3810
0
            ? complain_overflow_signed
3811
0
            : complain_overflow_bitfield);
3812
3813
      /* symbol */
3814
0
      val = 0;
3815
0
      addend = 0;
3816
0
      symndx = rel->r_symndx;
3817
3818
0
      if ((unsigned long) symndx < obj_raw_syment_count (input_bfd))
3819
0
  {
3820
0
    asection *sec;
3821
3822
0
    h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3823
0
    sym = syms + symndx;
3824
0
    addend = - sym->n_value;
3825
3826
0
    if (NULL == h)
3827
0
      {
3828
0
        sec = sections[symndx];
3829
        /* Hack to make sure we use the right TOC anchor value
3830
     if this reloc is against the TOC anchor.  */
3831
0
        if (sec->name[3] == '0'
3832
0
      && strcmp (sec->name, ".tc0") == 0)
3833
0
    val = xcoff_data (output_bfd)->toc;
3834
0
        else
3835
0
    val = (sec->output_section->vma
3836
0
           + sec->output_offset
3837
0
           + sym->n_value
3838
0
           - sec->vma);
3839
0
      }
3840
0
    else
3841
0
      {
3842
0
        if (info->unresolved_syms_in_objects != RM_IGNORE
3843
0
      && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3844
0
    {
3845
0
      r = bfd_reloc_undefined;
3846
0
      goto report_error;
3847
0
    }
3848
3849
0
        if (h->root.type == bfd_link_hash_defined
3850
0
      || h->root.type == bfd_link_hash_defweak)
3851
0
    {
3852
0
      sec = h->root.u.def.section;
3853
0
      val = (h->root.u.def.value
3854
0
       + sec->output_section->vma
3855
0
       + sec->output_offset);
3856
0
    }
3857
0
        else if (h->root.type == bfd_link_hash_common)
3858
0
    {
3859
0
      sec = h->root.u.c.p->section;
3860
0
      val = (sec->output_section->vma
3861
0
       + sec->output_offset);
3862
0
    }
3863
0
        else
3864
0
    {
3865
0
      BFD_ASSERT (bfd_link_relocatable (info)
3866
0
            || (info->static_link
3867
0
          && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3868
0
            || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3869
0
            || (h->flags & XCOFF_IMPORT) != 0);
3870
0
    }
3871
0
      }
3872
0
  }
3873
0
      else if (symndx != -1)
3874
0
  {
3875
0
    r = bfd_reloc_undefined;
3876
0
    fail = true;
3877
0
    goto report_error;
3878
0
  }
3879
3880
0
      bfd_vma relocation;
3881
0
      r = calc_reloc[rel->r_type] (input_bfd, input_section, output_bfd,
3882
0
           rel, sym, &howto, val, addend,
3883
0
           &relocation, contents, info);
3884
3885
0
      if (r != bfd_reloc_ok)
3886
0
  {
3887
0
    fail = true;
3888
0
    goto report_error;
3889
0
  }
3890
3891
0
      if (!bfd_reloc_offset_in_range (&howto, input_bfd, input_section, address))
3892
0
  {
3893
0
    r = bfd_reloc_outofrange;
3894
0
    goto report_error;
3895
0
  }
3896
0
      bfd_byte *location = contents + address;
3897
3898
      /* Get the value we are going to relocate.  */
3899
0
      bfd_vma value_to_relocate;
3900
0
      unsigned int field_size = bfd_get_reloc_size (&howto);
3901
0
      if (field_size == 2)
3902
0
  value_to_relocate = bfd_get_16 (input_bfd, location);
3903
0
      else if (!xcoff64 || field_size == 4)
3904
0
  value_to_relocate = bfd_get_32 (input_bfd, location);
3905
0
      else
3906
0
  value_to_relocate = bfd_get_64 (input_bfd, location);
3907
3908
      /* overflow.
3909
3910
   FIXME: We may drop bits during the addition
3911
   which we don't check for.  We must either check at every single
3912
   operation, which would be tedious, or we must do the computations
3913
   in a type larger than bfd_vma, which would be inefficient.  */
3914
3915
0
      if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3916
0
     (input_bfd, value_to_relocate, relocation, &howto)))
3917
0
  r = bfd_reloc_overflow;
3918
3919
      /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE.  */
3920
0
      value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3921
0
         | (((value_to_relocate & howto.src_mask)
3922
0
             + relocation) & howto.dst_mask));
3923
3924
      /* Put the value back in the object file.  */
3925
0
      if (field_size == 2)
3926
0
  bfd_put_16 (input_bfd, value_to_relocate, location);
3927
0
      else if (!xcoff64 || field_size == 4)
3928
0
  bfd_put_32 (input_bfd, value_to_relocate, location);
3929
0
      else
3930
0
  bfd_put_64 (input_bfd, value_to_relocate, location);
3931
3932
0
    report_error:
3933
0
      if (r != bfd_reloc_ok)
3934
0
  {
3935
0
    const char *name = NULL;
3936
0
    char buf[SYMNMLEN + 1];
3937
3938
0
    if (h != NULL)
3939
0
      name = h->root.root.string;
3940
0
    else if (sym != NULL)
3941
0
      name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3942
0
    else if (rel->r_symndx == -1)
3943
0
      name = "*ABS*";
3944
0
    if (name == NULL)
3945
0
      name = "UNKNOWN";
3946
3947
0
    if (r == bfd_reloc_undefined)
3948
0
      {
3949
0
        bool err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
3950
0
        && !info->warn_unresolved_syms);
3951
0
        info->callbacks->undefined_symbol
3952
0
    (info, name, input_bfd, input_section, address, err);
3953
0
      }
3954
0
    else
3955
0
      {
3956
0
        char reloc_type_name[10];
3957
0
        sprintf (reloc_type_name, "0x%02x", rel->r_type);
3958
3959
0
        if (r == bfd_reloc_overflow)
3960
0
    info->callbacks->reloc_overflow
3961
0
      (info, h ? &h->root : NULL, name, reloc_type_name, 0,
3962
0
       input_bfd, input_section, address);
3963
0
        else if (r == bfd_reloc_outofrange)
3964
0
    {
3965
0
      info->callbacks->einfo
3966
        /* xgettext:c-format */
3967
0
        (_("%X%P: %pB(%pA): relocation \"%s\" goes out of range\n"),
3968
0
         input_bfd, input_section, reloc_type_name);
3969
0
      fail = true;
3970
0
    }
3971
0
        else if (r == bfd_reloc_notsupported)
3972
0
    {
3973
0
      info->callbacks->einfo
3974
        /* xgettext:c-format */
3975
0
        (_("%X%P: %pB(%pA): relocation \"%s\" is not supported\n"),
3976
0
         input_bfd, input_section, reloc_type_name);
3977
0
      fail = true;
3978
0
    }
3979
0
        else
3980
0
    {
3981
0
      BFD_ASSERT (0);
3982
0
      fail = true;
3983
0
    }
3984
0
      }
3985
0
    if (fail)
3986
0
      return false;
3987
0
  }
3988
0
    }
3989
3990
0
  return true;
3991
0
}
3992
3993
static bool
3994
xcoff_ppc_relocate_section (bfd *output_bfd,
3995
          struct bfd_link_info *info,
3996
          bfd *input_bfd,
3997
          asection *input_section,
3998
          bfd_byte *contents,
3999
          struct internal_reloc *relocs,
4000
          struct internal_syment *syms,
4001
          asection **sections)
4002
0
{
4003
0
  return _bfd_xcoff_relocate_section (output_bfd, info, input_bfd, input_section,
4004
0
              contents, relocs, syms, sections,
4005
0
              false, ARRAY_SIZE (xcoff_howto_table),
4006
0
              xcoff_howto_table,
4007
0
              xcoff_calculate_relocation);
4008
0
}
4009
4010
/* gcc-8 warns (*) on all the strncpy calls in this function about
4011
   possible string truncation.  The "truncation" is not a bug.  We
4012
   have an external representation of structs with fields that are not
4013
   necessarily NULL terminated and corresponding internal
4014
   representation fields that are one larger so that they can always
4015
   be NULL terminated.
4016
   gcc versions between 4.2 and 4.6 do not allow pragma control of
4017
   diagnostics inside functions, giving a hard error if you try to use
4018
   the finer control available with later versions.
4019
   gcc prior to 4.2 warns about diagnostic push and pop.
4020
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
4021
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
4022
   (*) Depending on your system header files!  */
4023
#if GCC_VERSION >= 8000
4024
# pragma GCC diagnostic push
4025
# pragma GCC diagnostic ignored "-Wstringop-truncation"
4026
#endif
4027
static bool
4028
_bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
4029
            struct xcoff_loader_info *ldinfo,
4030
            struct internal_ldsym *ldsym,
4031
            const char *name)
4032
0
{
4033
0
  size_t len;
4034
0
  len = strlen (name);
4035
4036
0
  if (len <= SYMNMLEN)
4037
0
    strncpy (ldsym->_l._l_name, name, SYMNMLEN);
4038
0
  else
4039
0
    {
4040
0
      if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
4041
0
  {
4042
0
    bfd_size_type newalc;
4043
0
    char *newstrings;
4044
4045
0
    newalc = ldinfo->string_alc * 2;
4046
0
    if (newalc == 0)
4047
0
      newalc = 32;
4048
0
    while (ldinfo->string_size + len + 3 > newalc)
4049
0
      newalc *= 2;
4050
4051
0
    newstrings = bfd_realloc (ldinfo->strings, newalc);
4052
0
    if (newstrings == NULL)
4053
0
      {
4054
0
        ldinfo->failed = true;
4055
0
        return false;
4056
0
      }
4057
0
    ldinfo->string_alc = newalc;
4058
0
    ldinfo->strings = newstrings;
4059
0
  }
4060
4061
0
      ldinfo->strings[ldinfo->string_size] = ((len + 1) >> 8) & 0xff;
4062
0
      ldinfo->strings[ldinfo->string_size + 1] = ((len + 1)) & 0xff;
4063
0
      strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
4064
0
      ldsym->_l._l_l._l_zeroes = 0;
4065
0
      ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
4066
0
      ldinfo->string_size += len + 3;
4067
0
    }
4068
4069
0
  return true;
4070
0
}
4071
4072
static bool
4073
_bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
4074
          struct bfd_strtab_hash *strtab,
4075
          struct internal_syment *sym,
4076
          const char *name)
4077
0
{
4078
0
  if (strlen (name) <= SYMNMLEN)
4079
0
    {
4080
0
      strncpy (sym->_n._n_name, name, SYMNMLEN);
4081
0
    }
4082
0
  else
4083
0
    {
4084
0
      bool hash;
4085
0
      bfd_size_type indx;
4086
4087
0
      hash = !info->traditional_format;
4088
0
      indx = _bfd_stringtab_add (strtab, name, hash, false);
4089
0
      if (indx == (bfd_size_type) -1)
4090
0
  return false;
4091
0
      sym->_n._n_n._n_zeroes = 0;
4092
0
      sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4093
0
    }
4094
0
  return true;
4095
0
}
4096
#if GCC_VERSION >= 8000
4097
# pragma GCC diagnostic pop
4098
#endif
4099
4100
static asection *
4101
xcoff_create_csect_from_smclas (bfd *abfd,
4102
        union internal_auxent *aux,
4103
        const char *symbol_name)
4104
0
{
4105
0
  asection *return_value = NULL;
4106
4107
  /* .sv64 = x_smclas == 17
4108
     This is an invalid csect for 32 bit apps.  */
4109
0
  static const char * const names[] =
4110
0
    {
4111
0
      ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
4112
0
      ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
4113
0
      ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
4114
0
    };
4115
4116
0
  if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
4117
0
      && (NULL != names[aux->x_csect.x_smclas]))
4118
0
    {
4119
0
      return_value = bfd_make_section_anyway
4120
0
  (abfd, names[aux->x_csect.x_smclas]);
4121
0
    }
4122
0
  else
4123
0
    {
4124
0
      _bfd_error_handler
4125
  /* xgettext: c-format */
4126
0
  (_("%pB: symbol `%s' has unrecognized smclas %d"),
4127
0
   abfd, symbol_name, aux->x_csect.x_smclas);
4128
0
      bfd_set_error (bfd_error_bad_value);
4129
0
    }
4130
4131
0
  return return_value;
4132
0
}
4133
4134
static bool
4135
xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
4136
0
{
4137
0
  if (0xffff <= value)
4138
0
    return true;
4139
4140
0
  return false;
4141
0
}
4142
4143
static bool
4144
xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
4145
0
{
4146
0
  if (0xffff <= value)
4147
0
    return true;
4148
4149
0
  return false;
4150
0
}
4151
4152
static bfd_vma
4153
xcoff_loader_symbol_offset (bfd *abfd,
4154
          struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
4155
0
{
4156
0
  return bfd_xcoff_ldhdrsz (abfd);
4157
0
}
4158
4159
static bfd_vma
4160
xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
4161
0
{
4162
0
  return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
4163
0
}
4164
4165
static bool
4166
xcoff_generate_rtinit  (bfd *abfd, const char *init, const char *fini,
4167
      bool rtld)
4168
0
{
4169
0
  bfd_byte filehdr_ext[FILHSZ];
4170
0
  bfd_byte scnhdr_ext[SCNHSZ];
4171
0
  bfd_byte syment_ext[SYMESZ * 10];
4172
0
  bfd_byte reloc_ext[RELSZ * 3];
4173
0
  bfd_byte *data_buffer;
4174
0
  bfd_size_type data_buffer_size;
4175
0
  bfd_byte *string_table = NULL, *st_tmp = NULL;
4176
0
  bfd_size_type string_table_size;
4177
0
  bfd_vma val;
4178
0
  size_t initsz, finisz;
4179
0
  struct internal_filehdr filehdr;
4180
0
  struct internal_scnhdr scnhdr;
4181
0
  struct internal_syment syment;
4182
0
  union internal_auxent auxent;
4183
0
  struct internal_reloc reloc;
4184
4185
0
  char *data_name = ".data";
4186
0
  char *rtinit_name = "__rtinit";
4187
0
  char *rtld_name = "__rtld";
4188
4189
0
  if (! bfd_xcoff_rtinit_size (abfd))
4190
0
    return false;
4191
4192
0
  initsz = (init == NULL ? 0 : 1 + strlen (init));
4193
0
  finisz = (fini == NULL ? 0 : 1 + strlen (fini));
4194
4195
  /* file header */
4196
0
  memset (filehdr_ext, 0, FILHSZ);
4197
0
  memset (&filehdr, 0, sizeof (struct internal_filehdr));
4198
0
  filehdr.f_magic = bfd_xcoff_magic_number (abfd);
4199
0
  filehdr.f_nscns = 1;
4200
0
  filehdr.f_timdat = 0;
4201
0
  filehdr.f_nsyms = 0;  /* at least 6, no more than 10 */
4202
0
  filehdr.f_symptr = 0; /* set below */
4203
0
  filehdr.f_opthdr = 0;
4204
0
  filehdr.f_flags = 0;
4205
4206
  /* section header */
4207
0
  memset (scnhdr_ext, 0, SCNHSZ);
4208
0
  memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
4209
0
  memcpy (scnhdr.s_name, data_name, strlen (data_name));
4210
0
  scnhdr.s_paddr = 0;
4211
0
  scnhdr.s_vaddr = 0;
4212
0
  scnhdr.s_size = 0;    /* set below */
4213
0
  scnhdr.s_scnptr = FILHSZ + SCNHSZ;
4214
0
  scnhdr.s_relptr = 0;  /* set below */
4215
0
  scnhdr.s_lnnoptr = 0;
4216
0
  scnhdr.s_nreloc = 0;  /* either 1 or 2 */
4217
0
  scnhdr.s_nlnno = 0;
4218
0
  scnhdr.s_flags = STYP_DATA;
4219
4220
  /* .data
4221
     0x0000       0x00000000 : rtl
4222
     0x0004       0x00000010 : offset to init, or 0
4223
     0x0008       0x00000028 : offset to fini, or 0
4224
     0x000C       0x0000000C : size of descriptor
4225
     0x0010       0x00000000 : init, needs a reloc
4226
     0x0014       0x00000040 : offset to init name
4227
     0x0018       0x00000000 : flags, padded to a word
4228
     0x001C       0x00000000 : empty init
4229
     0x0020       0x00000000 :
4230
     0x0024       0x00000000 :
4231
     0x0028       0x00000000 : fini, needs a reloc
4232
     0x002C       0x00000??? : offset to fini name
4233
     0x0030       0x00000000 : flags, padded to a word
4234
     0x0034       0x00000000 : empty fini
4235
     0x0038       0x00000000 :
4236
     0x003C       0x00000000 :
4237
     0x0040       init name
4238
     0x0040 + initsz  fini name */
4239
4240
0
  data_buffer_size = 0x0040 + initsz + finisz;
4241
0
  data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
4242
0
  data_buffer = NULL;
4243
0
  data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
4244
0
  if (data_buffer == NULL)
4245
0
    return false;
4246
4247
0
  if (initsz)
4248
0
    {
4249
0
      val = 0x10;
4250
0
      bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
4251
0
      val = 0x40;
4252
0
      bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
4253
0
      memcpy (&data_buffer[val], init, initsz);
4254
0
    }
4255
4256
0
  if (finisz)
4257
0
    {
4258
0
      val = 0x28;
4259
0
      bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
4260
0
      val = 0x40 + initsz;
4261
0
      bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
4262
0
      memcpy (&data_buffer[val], fini, finisz);
4263
0
    }
4264
4265
0
  val = 0x0C;
4266
0
  bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
4267
4268
0
  scnhdr.s_size = data_buffer_size;
4269
4270
  /* string table */
4271
0
  string_table_size = 0;
4272
0
  if (initsz > 9)
4273
0
    string_table_size += initsz;
4274
0
  if (finisz > 9)
4275
0
    string_table_size += finisz;
4276
0
  if (string_table_size)
4277
0
    {
4278
0
      string_table_size += 4;
4279
0
      string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
4280
0
      if (string_table == NULL)
4281
0
  {
4282
0
    free (data_buffer);
4283
0
    return false;
4284
0
  }
4285
4286
0
      val = string_table_size;
4287
0
      bfd_h_put_32 (abfd, val, &string_table[0]);
4288
0
      st_tmp = string_table + 4;
4289
0
    }
4290
4291
  /* symbols
4292
     0. .data csect
4293
     2. __rtinit
4294
     4. init function
4295
     6. fini function
4296
     8. __rtld  */
4297
0
  memset (syment_ext, 0, 10 * SYMESZ);
4298
0
  memset (reloc_ext, 0, 3 * RELSZ);
4299
4300
  /* .data csect */
4301
0
  memset (&syment, 0, sizeof (struct internal_syment));
4302
0
  memset (&auxent, 0, sizeof (union internal_auxent));
4303
0
  memcpy (syment._n._n_name, data_name, strlen (data_name));
4304
0
  syment.n_scnum = 1;
4305
0
  syment.n_sclass = C_HIDEXT;
4306
0
  syment.n_numaux = 1;
4307
0
  auxent.x_csect.x_scnlen.u64 = data_buffer_size;
4308
0
  auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
4309
0
  auxent.x_csect.x_smclas = XMC_RW;
4310
0
  bfd_coff_swap_sym_out (abfd, &syment,
4311
0
       &syment_ext[filehdr.f_nsyms * SYMESZ]);
4312
0
  bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4313
0
       syment.n_numaux,
4314
0
       &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4315
0
  filehdr.f_nsyms += 2;
4316
4317
  /* __rtinit */
4318
0
  memset (&syment, 0, sizeof (struct internal_syment));
4319
0
  memset (&auxent, 0, sizeof (union internal_auxent));
4320
0
  memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
4321
0
  syment.n_scnum = 1;
4322
0
  syment.n_sclass = C_EXT;
4323
0
  syment.n_numaux = 1;
4324
0
  auxent.x_csect.x_smtyp = XTY_LD;
4325
0
  auxent.x_csect.x_smclas = XMC_RW;
4326
0
  bfd_coff_swap_sym_out (abfd, &syment,
4327
0
       &syment_ext[filehdr.f_nsyms * SYMESZ]);
4328
0
  bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4329
0
       syment.n_numaux,
4330
0
       &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4331
0
  filehdr.f_nsyms += 2;
4332
4333
  /* init */
4334
0
  if (initsz)
4335
0
    {
4336
0
      memset (&syment, 0, sizeof (struct internal_syment));
4337
0
      memset (&auxent, 0, sizeof (union internal_auxent));
4338
4339
0
      if (initsz > 9)
4340
0
  {
4341
0
    syment._n._n_n._n_offset = st_tmp - string_table;
4342
0
    memcpy (st_tmp, init, initsz);
4343
0
    st_tmp += initsz;
4344
0
  }
4345
0
      else
4346
0
  memcpy (syment._n._n_name, init, initsz - 1);
4347
4348
0
      syment.n_sclass = C_EXT;
4349
0
      syment.n_numaux = 1;
4350
0
      bfd_coff_swap_sym_out (abfd, &syment,
4351
0
           &syment_ext[filehdr.f_nsyms * SYMESZ]);
4352
0
      bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4353
0
           syment.n_numaux,
4354
0
           &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4355
4356
      /* reloc */
4357
0
      memset (&reloc, 0, sizeof (struct internal_reloc));
4358
0
      reloc.r_vaddr = 0x0010;
4359
0
      reloc.r_symndx = filehdr.f_nsyms;
4360
0
      reloc.r_type = R_POS;
4361
0
      reloc.r_size = 31;
4362
0
      bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
4363
4364
0
      filehdr.f_nsyms += 2;
4365
0
      scnhdr.s_nreloc += 1;
4366
0
    }
4367
4368
  /* fini */
4369
0
  if (finisz)
4370
0
    {
4371
0
      memset (&syment, 0, sizeof (struct internal_syment));
4372
0
      memset (&auxent, 0, sizeof (union internal_auxent));
4373
4374
0
      if (finisz > 9)
4375
0
  {
4376
0
    syment._n._n_n._n_offset = st_tmp - string_table;
4377
0
    memcpy (st_tmp, fini, finisz);
4378
0
    st_tmp += finisz;
4379
0
  }
4380
0
      else
4381
0
  memcpy (syment._n._n_name, fini, finisz - 1);
4382
4383
0
      syment.n_sclass = C_EXT;
4384
0
      syment.n_numaux = 1;
4385
0
      bfd_coff_swap_sym_out (abfd, &syment,
4386
0
           &syment_ext[filehdr.f_nsyms * SYMESZ]);
4387
0
      bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4388
0
           syment.n_numaux,
4389
0
           &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4390
4391
      /* reloc */
4392
0
      memset (&reloc, 0, sizeof (struct internal_reloc));
4393
0
      reloc.r_vaddr = 0x0028;
4394
0
      reloc.r_symndx = filehdr.f_nsyms;
4395
0
      reloc.r_type = R_POS;
4396
0
      reloc.r_size = 31;
4397
0
      bfd_coff_swap_reloc_out (abfd, &reloc,
4398
0
             &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4399
4400
0
      filehdr.f_nsyms += 2;
4401
0
      scnhdr.s_nreloc += 1;
4402
0
    }
4403
4404
0
  if (rtld)
4405
0
    {
4406
0
      memset (&syment, 0, sizeof (struct internal_syment));
4407
0
      memset (&auxent, 0, sizeof (union internal_auxent));
4408
0
      memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
4409
0
      syment.n_sclass = C_EXT;
4410
0
      syment.n_numaux = 1;
4411
0
      bfd_coff_swap_sym_out (abfd, &syment,
4412
0
           &syment_ext[filehdr.f_nsyms * SYMESZ]);
4413
0
      bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4414
0
           syment.n_numaux,
4415
0
           &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4416
4417
      /* reloc */
4418
0
      memset (&reloc, 0, sizeof (struct internal_reloc));
4419
0
      reloc.r_vaddr = 0x0000;
4420
0
      reloc.r_symndx = filehdr.f_nsyms;
4421
0
      reloc.r_type = R_POS;
4422
0
      reloc.r_size = 31;
4423
0
      bfd_coff_swap_reloc_out (abfd, &reloc,
4424
0
             &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4425
4426
0
      filehdr.f_nsyms += 2;
4427
0
      scnhdr.s_nreloc += 1;
4428
0
    }
4429
4430
0
  scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
4431
0
  filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
4432
4433
0
  bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
4434
0
  bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext, NULL);
4435
0
  bool ret = true;
4436
0
  if (bfd_write (filehdr_ext, FILHSZ, abfd) != FILHSZ
4437
0
      || bfd_write (scnhdr_ext, SCNHSZ, abfd) != SCNHSZ
4438
0
      || bfd_write (data_buffer, data_buffer_size, abfd) != data_buffer_size
4439
0
      || (bfd_write (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd)
4440
0
    != scnhdr.s_nreloc * RELSZ)
4441
0
      || (bfd_write (syment_ext, filehdr.f_nsyms * SYMESZ, abfd)
4442
0
    != (bfd_size_type) filehdr.f_nsyms * SYMESZ)
4443
0
      || bfd_write (string_table, string_table_size, abfd) != string_table_size)
4444
0
    ret = false;
4445
4446
0
  free (string_table);
4447
0
  free (data_buffer);
4448
0
  return ret;
4449
0
}
4450
4451
4452
static reloc_howto_type xcoff_dynamic_reloc =
4453
HOWTO (0,     /* type */
4454
       0,     /* rightshift */
4455
       4,     /* size */
4456
       32,      /* bitsize */
4457
       false,     /* pc_relative */
4458
       0,     /* bitpos */
4459
       complain_overflow_bitfield, /* complain_on_overflow */
4460
       0,     /* special_function */
4461
       "R_POS",     /* name */
4462
       true,      /* partial_inplace */
4463
       0xffffffff,    /* src_mask */
4464
       0xffffffff,    /* dst_mask */
4465
       false);      /* pcrel_offset */
4466
4467
/* Indirect call stub
4468
   The first word of the code must be modified by filling in
4469
   the correct TOC offset.  */
4470
4471
static const unsigned long xcoff_stub_indirect_call_code[4] =
4472
  {
4473
    0x81820000, /* lwz r12,0(r2) */
4474
    0x800c0000, /* lwz r0,0(r12) */
4475
    0x7c0903a6, /* mtctr r0 */
4476
    0x4e800420, /* bctr */
4477
  };
4478
4479
/*  Shared call stub
4480
    The first word of the code must be modified by filling in
4481
    the correct TOC offset.
4482
    This is exactly as the glink code but without the traceback,
4483
    as it won't be an independent function.  */
4484
4485
static const unsigned long xcoff_stub_shared_call_code[6] =
4486
  {
4487
    0x81820000, /* lwz r12,0(r2) */
4488
    0x90410014, /* stw r2,20(r1) */
4489
    0x800c0000, /* lwz r0,0(r12) */
4490
    0x804c0004, /* lwz r2,4(r12) */
4491
    0x7c0903a6, /* mtctr r0 */
4492
    0x4e800420, /* bctr */
4493
  };
4494
4495
/*  glink
4496
4497
   The first word of global linkage code must be modified by filling in
4498
   the correct TOC offset.  */
4499
4500
static const unsigned long xcoff_glink_code[9] =
4501
  {
4502
    0x81820000, /* lwz r12,0(r2) */
4503
    0x90410014, /* stw r2,20(r1) */
4504
    0x800c0000, /* lwz r0,0(r12) */
4505
    0x804c0004, /* lwz r2,4(r12) */
4506
    0x7c0903a6, /* mtctr r0 */
4507
    0x4e800420, /* bctr */
4508
    0x00000000, /* start of traceback table */
4509
    0x000c8000, /* traceback table */
4510
    0x00000000, /* traceback table */
4511
  };
4512
4513
/* Table to convert DWARF flags to section names.
4514
   Remember to update binutils/dwarf.c:debug_displays
4515
   if new DWARF sections are supported by XCOFF.  */
4516
4517
const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
4518
  { SSUBTYP_DWINFO,  ".dwinfo",  ".debug_info",     true },
4519
  { SSUBTYP_DWLINE,  ".dwline",  ".debug_line",     true },
4520
  { SSUBTYP_DWPBNMS, ".dwpbnms", ".debug_pubnames", true },
4521
  { SSUBTYP_DWPBTYP, ".dwpbtyp", ".debug_pubtypes", true },
4522
  { SSUBTYP_DWARNGE, ".dwarnge", ".debug_aranges",  true },
4523
  { SSUBTYP_DWABREV, ".dwabrev", ".debug_abbrev",   false },
4524
  { SSUBTYP_DWSTR,   ".dwstr",   ".debug_str",      true },
4525
  { SSUBTYP_DWRNGES, ".dwrnges", ".debug_ranges",   true },
4526
  { SSUBTYP_DWLOC,   ".dwloc",   ".debug_loc",      true },
4527
  { SSUBTYP_DWFRAME, ".dwframe", ".debug_frame",    true },
4528
  { SSUBTYP_DWMAC,   ".dwmac",   ".debug_macro",    true }
4529
};
4530
4531
/* For generic entry points.  */
4532
#define _bfd_xcoff_close_and_cleanup coff_close_and_cleanup
4533
#define _bfd_xcoff_new_section_hook coff_new_section_hook
4534
#define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
4535
4536
/* For copy private data entry points.  */
4537
#define _bfd_xcoff_bfd_copy_private_bfd_data \
4538
  _bfd_xcoff_copy_private_bfd_data
4539
#define _bfd_xcoff_bfd_merge_private_bfd_data \
4540
  _bfd_generic_bfd_merge_private_bfd_data
4541
#define _bfd_xcoff_bfd_copy_private_section_data \
4542
  _bfd_generic_bfd_copy_private_section_data
4543
#define _bfd_xcoff_bfd_copy_private_symbol_data \
4544
   _bfd_generic_bfd_copy_private_symbol_data
4545
#define _bfd_xcoff_bfd_copy_private_header_data \
4546
   _bfd_generic_bfd_copy_private_header_data
4547
#define _bfd_xcoff_bfd_set_private_flags \
4548
   _bfd_generic_bfd_set_private_flags
4549
#define _bfd_xcoff_bfd_print_private_bfd_data \
4550
   _bfd_generic_bfd_print_private_bfd_data
4551
4552
/* For archive entry points.  */
4553
#define _bfd_xcoff_slurp_extended_name_table \
4554
   _bfd_noarchive_slurp_extended_name_table
4555
#define _bfd_xcoff_construct_extended_name_table \
4556
   _bfd_noarchive_construct_extended_name_table
4557
#define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
4558
#define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
4559
#define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
4560
#define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
4561
#define _bfd_xcoff_update_armap_timestamp _bfd_bool_bfd_true
4562
4563
/* For symbols entry points.  */
4564
#define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
4565
#define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
4566
#define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
4567
#define _bfd_xcoff_print_symbol coff_print_symbol
4568
#define _bfd_xcoff_get_symbol_info coff_get_symbol_info
4569
#define _bfd_xcoff_get_symbol_version_string \
4570
  _bfd_nosymbols_get_symbol_version_string
4571
#define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
4572
#define _bfd_xcoff_bfd_is_target_special_symbol \
4573
  coff_bfd_is_target_special_symbol
4574
#define _bfd_xcoff_get_lineno coff_get_lineno
4575
#define _bfd_xcoff_find_nearest_line coff_find_nearest_line
4576
#define _bfd_xcoff_find_nearest_line_with_alt \
4577
coff_find_nearest_line_with_alt
4578
#define _bfd_xcoff_find_line coff_find_line
4579
#define _bfd_xcoff_find_inliner_info coff_find_inliner_info
4580
#define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
4581
#define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
4582
#define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
4583
4584
/* For reloc entry points.  */
4585
#define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
4586
#define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
4587
#define _bfd_xcoff_finalize_section_relocs _bfd_generic_finalize_section_relocs
4588
#define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
4589
#define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
4590
4591
/* For link entry points.  */
4592
#define _bfd_xcoff_bfd_get_relocated_section_contents \
4593
  bfd_generic_get_relocated_section_contents
4594
#define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
4595
#define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
4596
#define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
4597
#define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
4598
  _bfd_generic_copy_link_hash_symbol_type
4599
#define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
4600
#define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
4601
#define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
4602
#define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
4603
#define _bfd_xcoff_bfd_group_name bfd_generic_group_name
4604
#define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
4605
#define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
4606
#define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
4607
#define _bfd_xcoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
4608
#define _bfd_xcoff_bfd_define_start_stop    bfd_generic_define_start_stop
4609
#define _bfd_xcoff_bfd_link_check_relocs    _bfd_generic_link_check_relocs
4610
4611
/* For dynamic symbols and relocs entry points.  */
4612
#define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
4613
4614
static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
4615
  {
4616
    { /* COFF backend, defined in libcoff.h.  */
4617
      _bfd_xcoff_swap_aux_in,
4618
      _bfd_xcoff_swap_sym_in,
4619
      coff_swap_lineno_in,
4620
      _bfd_xcoff_swap_aux_out,
4621
      _bfd_xcoff_swap_sym_out,
4622
      coff_swap_lineno_out,
4623
      xcoff_swap_reloc_out,
4624
      coff_swap_filehdr_out,
4625
      coff_swap_aouthdr_out,
4626
      coff_swap_scnhdr_out,
4627
      FILHSZ,
4628
      AOUTSZ,
4629
      SCNHSZ,
4630
      SYMESZ,
4631
      AUXESZ,
4632
      RELSZ,
4633
      LINESZ,
4634
      FILNMLEN,
4635
      true,     /* _bfd_coff_long_filenames */
4636
      XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
4637
      3,      /* _bfd_coff_default_section_alignment_power */
4638
      false,      /* _bfd_coff_force_symnames_in_strings */
4639
      2,      /* _bfd_coff_debug_string_prefix_length */
4640
      32768,      /* _bfd_coff_max_nscns */
4641
      coff_swap_filehdr_in,
4642
      coff_swap_aouthdr_in,
4643
      coff_swap_scnhdr_in,
4644
      xcoff_swap_reloc_in,
4645
      coff_bad_format_hook,
4646
      coff_set_arch_mach_hook,
4647
      coff_mkobject_hook,
4648
      styp_to_sec_flags,
4649
      coff_set_alignment_hook,
4650
      coff_slurp_symbol_table,
4651
      symname_in_debug_hook,
4652
      coff_pointerize_aux_hook,
4653
      coff_print_aux,
4654
      dummy_reloc16_extra_cases,
4655
      dummy_reloc16_estimate,
4656
      NULL,     /* bfd_coff_sym_is_global */
4657
      coff_compute_section_file_positions,
4658
      NULL,     /* _bfd_coff_start_final_link */
4659
      xcoff_ppc_relocate_section,
4660
      coff_rtype_to_howto,
4661
      NULL,     /* _bfd_coff_adjust_symndx */
4662
      coff_link_output_has_begun,
4663
      coff_final_link_postscript,
4664
      NULL      /* print_pdata.  */
4665
    },
4666
4667
    0x01DF,     /* magic number */
4668
    bfd_arch_rs6000,
4669
    bfd_mach_rs6k,
4670
4671
    /* Function pointers to xcoff specific swap routines.  */
4672
    xcoff_swap_ldhdr_in,
4673
    xcoff_swap_ldhdr_out,
4674
    xcoff_swap_ldsym_in,
4675
    xcoff_swap_ldsym_out,
4676
    xcoff_swap_ldrel_in,
4677
    xcoff_swap_ldrel_out,
4678
4679
    /* Sizes.  */
4680
    LDHDRSZ,
4681
    LDSYMSZ,
4682
    LDRELSZ,
4683
    12,       /* _xcoff_function_descriptor_size */
4684
    SMALL_AOUTSZ,
4685
4686
    /* Versions.  */
4687
    1,        /* _xcoff_ldhdr_version */
4688
4689
    _bfd_xcoff_put_symbol_name,
4690
    _bfd_xcoff_put_ldsymbol_name,
4691
    &xcoff_dynamic_reloc,
4692
    xcoff_create_csect_from_smclas,
4693
4694
    /* Lineno and reloc count overflow.  */
4695
    xcoff_is_lineno_count_overflow,
4696
    xcoff_is_reloc_count_overflow,
4697
4698
    xcoff_loader_symbol_offset,
4699
    xcoff_loader_reloc_offset,
4700
4701
    /* glink.  */
4702
    &xcoff_glink_code[0],
4703
    36,       /* _xcoff_glink_size */
4704
4705
    /* rtinit */
4706
    64,       /* _xcoff_rtinit_size */
4707
    xcoff_generate_rtinit,
4708
4709
    /* Stub indirect call.  */
4710
    &xcoff_stub_indirect_call_code[0],
4711
    16,       /* _xcoff_stub_indirect_call_size */
4712
4713
    /* Stub shared call.  */
4714
    &xcoff_stub_shared_call_code[0],
4715
    24,       /* _xcoff_stub_shared_call_size */
4716
  };
4717
4718
/* The transfer vector that leads the outside world to all of the above.  */
4719
const bfd_target rs6000_xcoff_vec =
4720
  {
4721
    "aixcoff-rs6000",
4722
    bfd_target_xcoff_flavour,
4723
    BFD_ENDIAN_BIG,   /* data byte order is big */
4724
    BFD_ENDIAN_BIG,   /* header byte order is big */
4725
4726
    (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4727
     | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4728
4729
    SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4730
    0,        /* leading char */
4731
    '/',      /* ar_pad_char */
4732
    15,       /* ar_max_namelen */
4733
    0,        /* match priority.  */
4734
    TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
4735
    TARGET_MERGE_SECTIONS,
4736
4737
    /* data */
4738
    bfd_getb64,
4739
    bfd_getb_signed_64,
4740
    bfd_putb64,
4741
    bfd_getb32,
4742
    bfd_getb_signed_32,
4743
    bfd_putb32,
4744
    bfd_getb16,
4745
    bfd_getb_signed_16,
4746
    bfd_putb16,
4747
4748
    /* hdrs */
4749
    bfd_getb64,
4750
    bfd_getb_signed_64,
4751
    bfd_putb64,
4752
    bfd_getb32,
4753
    bfd_getb_signed_32,
4754
    bfd_putb32,
4755
    bfd_getb16,
4756
    bfd_getb_signed_16,
4757
    bfd_putb16,
4758
4759
    { /* bfd_check_format */
4760
      _bfd_dummy_target,
4761
      coff_object_p,
4762
      _bfd_xcoff_archive_p,
4763
      CORE_FILE_P
4764
    },
4765
4766
    { /* bfd_set_format */
4767
      _bfd_bool_bfd_false_error,
4768
      coff_mkobject,
4769
      _bfd_generic_mkarchive,
4770
      _bfd_bool_bfd_false_error
4771
    },
4772
4773
    {/* bfd_write_contents */
4774
      _bfd_bool_bfd_false_error,
4775
      coff_write_object_contents,
4776
      _bfd_xcoff_write_archive_contents,
4777
      _bfd_bool_bfd_false_error
4778
    },
4779
4780
    BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4781
    BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4782
    BFD_JUMP_TABLE_CORE (coff),
4783
    BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4784
    BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4785
    BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4786
    BFD_JUMP_TABLE_WRITE (coff),
4787
    BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4788
    BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4789
4790
    /* Opposite endian version, none exists */
4791
    NULL,
4792
4793
    & bfd_xcoff_backend_data,
4794
  };
4795
4796
/* xcoff-powermac target
4797
   Old target.
4798
   Only difference between this target and the rs6000 target is the
4799
   the default architecture and machine type used in coffcode.h
4800
4801
   PowerPC Macs use the same magic numbers as RS/6000
4802
   (because that's how they were bootstrapped originally),
4803
   but they are always PowerPC architecture.  */
4804
static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
4805
  {
4806
    { /* COFF backend, defined in libcoff.h.  */
4807
      _bfd_xcoff_swap_aux_in,
4808
      _bfd_xcoff_swap_sym_in,
4809
      coff_swap_lineno_in,
4810
      _bfd_xcoff_swap_aux_out,
4811
      _bfd_xcoff_swap_sym_out,
4812
      coff_swap_lineno_out,
4813
      xcoff_swap_reloc_out,
4814
      coff_swap_filehdr_out,
4815
      coff_swap_aouthdr_out,
4816
      coff_swap_scnhdr_out,
4817
      FILHSZ,
4818
      AOUTSZ,
4819
      SCNHSZ,
4820
      SYMESZ,
4821
      AUXESZ,
4822
      RELSZ,
4823
      LINESZ,
4824
      FILNMLEN,
4825
      true,     /* _bfd_coff_long_filenames */
4826
      XCOFF_NO_LONG_SECTION_NAMES,  /* _bfd_coff_long_section_names */
4827
      3,      /* _bfd_coff_default_section_alignment_power */
4828
      false,      /* _bfd_coff_force_symnames_in_strings */
4829
      2,      /* _bfd_coff_debug_string_prefix_length */
4830
      32768,      /* _bfd_coff_max_nscns */
4831
      coff_swap_filehdr_in,
4832
      coff_swap_aouthdr_in,
4833
      coff_swap_scnhdr_in,
4834
      xcoff_swap_reloc_in,
4835
      coff_bad_format_hook,
4836
      coff_set_arch_mach_hook,
4837
      coff_mkobject_hook,
4838
      styp_to_sec_flags,
4839
      coff_set_alignment_hook,
4840
      coff_slurp_symbol_table,
4841
      symname_in_debug_hook,
4842
      coff_pointerize_aux_hook,
4843
      coff_print_aux,
4844
      dummy_reloc16_extra_cases,
4845
      dummy_reloc16_estimate,
4846
      NULL,     /* bfd_coff_sym_is_global */
4847
      coff_compute_section_file_positions,
4848
      NULL,     /* _bfd_coff_start_final_link */
4849
      xcoff_ppc_relocate_section,
4850
      coff_rtype_to_howto,
4851
      NULL,     /* _bfd_coff_adjust_symndx */
4852
      coff_link_output_has_begun,
4853
      coff_final_link_postscript,
4854
      NULL      /* print_pdata.  */
4855
    },
4856
4857
    0x01DF,     /* magic number */
4858
    bfd_arch_powerpc,
4859
    bfd_mach_ppc,
4860
4861
    /* Function pointers to xcoff specific swap routines.  */
4862
    xcoff_swap_ldhdr_in,
4863
    xcoff_swap_ldhdr_out,
4864
    xcoff_swap_ldsym_in,
4865
    xcoff_swap_ldsym_out,
4866
    xcoff_swap_ldrel_in,
4867
    xcoff_swap_ldrel_out,
4868
4869
    /* Sizes.  */
4870
    LDHDRSZ,
4871
    LDSYMSZ,
4872
    LDRELSZ,
4873
    12,       /* _xcoff_function_descriptor_size */
4874
    SMALL_AOUTSZ,
4875
4876
    /* Versions.  */
4877
    1,        /* _xcoff_ldhdr_version */
4878
4879
    _bfd_xcoff_put_symbol_name,
4880
    _bfd_xcoff_put_ldsymbol_name,
4881
    &xcoff_dynamic_reloc,
4882
    xcoff_create_csect_from_smclas,
4883
4884
    /* Lineno and reloc count overflow.  */
4885
    xcoff_is_lineno_count_overflow,
4886
    xcoff_is_reloc_count_overflow,
4887
4888
    xcoff_loader_symbol_offset,
4889
    xcoff_loader_reloc_offset,
4890
4891
    /* glink.  */
4892
    &xcoff_glink_code[0],
4893
    36,       /* _xcoff_glink_size */
4894
4895
    /* rtinit */
4896
    0,        /* _xcoff_rtinit_size */
4897
    xcoff_generate_rtinit,
4898
4899
    /* Stub indirect call.  */
4900
    &xcoff_stub_indirect_call_code[0],
4901
    16,       /* _xcoff_stub_indirect_call_size */
4902
4903
    /* Stub shared call.  */
4904
    &xcoff_stub_shared_call_code[0],
4905
    24,       /* _xcoff_stub_shared_call_size */
4906
  };
4907
4908
/* The transfer vector that leads the outside world to all of the above.  */
4909
const bfd_target powerpc_xcoff_vec =
4910
  {
4911
    "xcoff-powermac",
4912
    bfd_target_xcoff_flavour,
4913
    BFD_ENDIAN_BIG,   /* data byte order is big */
4914
    BFD_ENDIAN_BIG,   /* header byte order is big */
4915
4916
    (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4917
     | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4918
4919
    SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4920
    0,        /* leading char */
4921
    '/',      /* ar_pad_char */
4922
    15,       /* ar_max_namelen */
4923
    0,        /* match priority.  */
4924
    TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
4925
    TARGET_MERGE_SECTIONS,
4926
4927
    /* data */
4928
    bfd_getb64,
4929
    bfd_getb_signed_64,
4930
    bfd_putb64,
4931
    bfd_getb32,
4932
    bfd_getb_signed_32,
4933
    bfd_putb32,
4934
    bfd_getb16,
4935
    bfd_getb_signed_16,
4936
    bfd_putb16,
4937
4938
    /* hdrs */
4939
    bfd_getb64,
4940
    bfd_getb_signed_64,
4941
    bfd_putb64,
4942
    bfd_getb32,
4943
    bfd_getb_signed_32,
4944
    bfd_putb32,
4945
    bfd_getb16,
4946
    bfd_getb_signed_16,
4947
    bfd_putb16,
4948
4949
    { /* bfd_check_format */
4950
      _bfd_dummy_target,
4951
      coff_object_p,
4952
      _bfd_xcoff_archive_p,
4953
      CORE_FILE_P
4954
    },
4955
4956
    { /* bfd_set_format */
4957
      _bfd_bool_bfd_false_error,
4958
      coff_mkobject,
4959
      _bfd_generic_mkarchive,
4960
      _bfd_bool_bfd_false_error
4961
    },
4962
4963
    {/* bfd_write_contents */
4964
      _bfd_bool_bfd_false_error,
4965
      coff_write_object_contents,
4966
      _bfd_xcoff_write_archive_contents,
4967
      _bfd_bool_bfd_false_error
4968
    },
4969
4970
    BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4971
    BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4972
    BFD_JUMP_TABLE_CORE (coff),
4973
    BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4974
    BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4975
    BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4976
    BFD_JUMP_TABLE_WRITE (coff),
4977
    BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4978
    BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4979
4980
    /* Opposite endian version, none exists */
4981
    NULL,
4982
4983
    & bfd_pmac_xcoff_backend_data,
4984
  };