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