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