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