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