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