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