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