Fix typo
[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;
2573 return size;
2574}
beb1bf64
TR
2575\f
2576/* Routines to swap information in the XCOFF .loader section. If we
2577 ever need to write an XCOFF loader, this stuff will need to be
2578 moved to another file shared by the linker (which XCOFF calls the
2579 ``binder'') and the loader. */
2580
2581/* Swap in the ldhdr structure. */
2582
2583static void
2c3fc389 2584xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
beb1bf64 2585{
814fa6ab
AM
2586 const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2587
beb1bf64
TR
2588 dst->l_version = bfd_get_32 (abfd, src->l_version);
2589 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2590 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2591 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2592 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2593 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2594 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2595 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2596}
2597
2598/* Swap out the ldhdr structure. */
2599
2600static void
2c3fc389 2601xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
beb1bf64 2602{
814fa6ab
AM
2603 struct external_ldhdr *dst = (struct external_ldhdr *) d;
2604
dc810e39 2605 bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
beb1bf64
TR
2606 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2607 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2608 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2609 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2610 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2611 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2612 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2613}
2614
2615/* Swap in the ldsym structure. */
2616
2617static void
2c3fc389 2618xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
beb1bf64 2619{
814fa6ab
AM
2620 const struct external_ldsym *src = (const struct external_ldsym *) s;
2621
beb1bf64
TR
2622 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2623 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2624 } else {
2625 dst->_l._l_l._l_zeroes = 0;
2626 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2627 }
2628 dst->l_value = bfd_get_32 (abfd, src->l_value);
2629 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2630 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2631 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2632 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2633 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2634}
2635
2636/* Swap out the ldsym structure. */
2637
2638static void
2c3fc389 2639xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
beb1bf64 2640{
814fa6ab 2641 struct external_ldsym *dst = (struct external_ldsym *) d;
beb1bf64
TR
2642
2643 if (src->_l._l_l._l_zeroes != 0)
2644 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2645 else
2646 {
dc810e39
AM
2647 bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2648 bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2649 dst->_l._l_l._l_offset);
beb1bf64
TR
2650 }
2651 bfd_put_32 (abfd, src->l_value, dst->l_value);
dc810e39 2652 bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
beb1bf64
TR
2653 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2654 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2655 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2656 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2657}
2658
59862849 2659static void
2c3fc389 2660xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
59862849
TR
2661{
2662 struct external_reloc *src = (struct external_reloc *) s;
2663 struct internal_reloc *dst = (struct internal_reloc *) d;
2664
2665 memset (dst, 0, sizeof (struct internal_reloc));
2666
2667 dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2668 dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2669 dst->r_size = bfd_get_8 (abfd, src->r_size);
2670 dst->r_type = bfd_get_8 (abfd, src->r_type);
2671}
2672
2673static unsigned int
2c3fc389 2674xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
59862849
TR
2675{
2676 struct internal_reloc *src = (struct internal_reloc *) s;
2677 struct external_reloc *dst = (struct external_reloc *) d;
2678
2679 bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2680 bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2681 bfd_put_8 (abfd, src->r_type, dst->r_type);
2682 bfd_put_8 (abfd, src->r_size, dst->r_size);
2683
2684 return bfd_coff_relsz (abfd);
2685}
2686
beb1bf64
TR
2687/* Swap in the ldrel structure. */
2688
2689static void
2c3fc389 2690xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
beb1bf64 2691{
814fa6ab
AM
2692 const struct external_ldrel *src = (const struct external_ldrel *) s;
2693
beb1bf64
TR
2694 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2695 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2696 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2697 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2698}
2699
2700/* Swap out the ldrel structure. */
2701
2702static void
2c3fc389 2703xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
beb1bf64 2704{
814fa6ab
AM
2705 struct external_ldrel *dst = (struct external_ldrel *) d;
2706
beb1bf64
TR
2707 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2708 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
dc810e39
AM
2709 bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2710 bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
beb1bf64
TR
2711}
2712\f
2713
b34976b6 2714bfd_boolean
417236c0
TG
2715xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
2716 asection *input_section ATTRIBUTE_UNUSED,
2717 bfd *output_bfd ATTRIBUTE_UNUSED,
2718 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2719 struct internal_syment *sym ATTRIBUTE_UNUSED,
2720 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2721 bfd_vma val ATTRIBUTE_UNUSED,
2722 bfd_vma addend ATTRIBUTE_UNUSED,
2723 bfd_vma *relocation ATTRIBUTE_UNUSED,
2724 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6 2725{
b34976b6 2726 return TRUE;
dbe341c6
TR
2727}
2728
b34976b6 2729bfd_boolean
417236c0
TG
2730xcoff_reloc_type_fail (bfd *input_bfd,
2731 asection *input_section ATTRIBUTE_UNUSED,
2732 bfd *output_bfd ATTRIBUTE_UNUSED,
2733 struct internal_reloc *rel,
2734 struct internal_syment *sym ATTRIBUTE_UNUSED,
2735 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2736 bfd_vma val ATTRIBUTE_UNUSED,
2737 bfd_vma addend ATTRIBUTE_UNUSED,
2738 bfd_vma *relocation ATTRIBUTE_UNUSED,
2739 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6
TR
2740{
2741 (*_bfd_error_handler)
2742 (_("%s: unsupported relocation type 0x%02x"),
2743 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
2744 bfd_set_error (bfd_error_bad_value);
b34976b6 2745 return FALSE;
dbe341c6
TR
2746}
2747
b34976b6 2748bfd_boolean
417236c0
TG
2749xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
2750 asection *input_section ATTRIBUTE_UNUSED,
2751 bfd *output_bfd ATTRIBUTE_UNUSED,
2752 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2753 struct internal_syment *sym ATTRIBUTE_UNUSED,
2754 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2755 bfd_vma val,
2756 bfd_vma addend,
2757 bfd_vma *relocation,
2758 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6
TR
2759{
2760 *relocation = val + addend;
b34976b6 2761 return TRUE;
dbe341c6
TR
2762}
2763
b34976b6 2764bfd_boolean
417236c0
TG
2765xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
2766 asection *input_section ATTRIBUTE_UNUSED,
2767 bfd *output_bfd ATTRIBUTE_UNUSED,
2768 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2769 struct internal_syment *sym ATTRIBUTE_UNUSED,
2770 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2771 bfd_vma val,
2772 bfd_vma addend,
2773 bfd_vma *relocation,
2774 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6
TR
2775{
2776 *relocation = addend - val;
b34976b6 2777 return TRUE;
dbe341c6
TR
2778}
2779
b34976b6 2780bfd_boolean
417236c0
TG
2781xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
2782 asection *input_section,
2783 bfd *output_bfd ATTRIBUTE_UNUSED,
2784 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2785 struct internal_syment *sym ATTRIBUTE_UNUSED,
2786 struct reloc_howto_struct *howto,
2787 bfd_vma val,
2788 bfd_vma addend,
2789 bfd_vma *relocation,
2790 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6 2791{
b34976b6 2792 howto->pc_relative = TRUE;
dbe341c6
TR
2793
2794 /* A PC relative reloc includes the section address. */
2795 addend += input_section->vma;
2796
2797 *relocation = val + addend;
cf9ab45b
AM
2798 *relocation -= (input_section->output_section->vma
2799 + input_section->output_offset);
b34976b6 2800 return TRUE;
dbe341c6 2801}
f1f0d9ab 2802
b34976b6 2803bfd_boolean
417236c0
TG
2804xcoff_reloc_type_toc (bfd *input_bfd,
2805 asection *input_section ATTRIBUTE_UNUSED,
2806 bfd *output_bfd,
2807 struct internal_reloc *rel,
2808 struct internal_syment *sym,
2809 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2810 bfd_vma val,
2811 bfd_vma addend ATTRIBUTE_UNUSED,
2812 bfd_vma *relocation,
2813 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6
TR
2814{
2815 struct xcoff_link_hash_entry *h;
2816
cf9ab45b 2817 if (0 > rel->r_symndx)
b34976b6 2818 return FALSE;
dbe341c6
TR
2819
2820 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2821
2822 if (h != NULL && h->smclas != XMC_TD)
2823 {
2824 if (h->toc_section == NULL)
2825 {
2826 (*_bfd_error_handler)
2827 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
2828 bfd_get_filename (input_bfd), rel->r_vaddr,
2829 h->root.root.string);
2830 bfd_set_error (bfd_error_bad_value);
b34976b6 2831 return FALSE;
dbe341c6 2832 }
cf9ab45b 2833
dbe341c6
TR
2834 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
2835 val = (h->toc_section->output_section->vma
2836 + h->toc_section->output_offset);
2837 }
cf9ab45b
AM
2838
2839 *relocation = ((val - xcoff_data (output_bfd)->toc)
2840 - (sym->n_value - xcoff_data (input_bfd)->toc));
b34976b6 2841 return TRUE;
dbe341c6 2842}
f1f0d9ab 2843
b34976b6 2844bfd_boolean
417236c0
TG
2845xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
2846 asection *input_section ATTRIBUTE_UNUSED,
2847 bfd *output_bfd ATTRIBUTE_UNUSED,
2848 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2849 struct internal_syment *sym ATTRIBUTE_UNUSED,
2850 struct reloc_howto_struct *howto,
2851 bfd_vma val,
2852 bfd_vma addend,
2853 bfd_vma *relocation,
2854 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6 2855{
a78eab4e
AM
2856 howto->src_mask &= ~3;
2857 howto->dst_mask = howto->src_mask;
dbe341c6
TR
2858
2859 *relocation = val + addend;
2860
b34976b6 2861 return TRUE;
dbe341c6
TR
2862}
2863
b34976b6 2864static bfd_boolean
417236c0
TG
2865xcoff_reloc_type_br (bfd *input_bfd,
2866 asection *input_section,
2867 bfd *output_bfd ATTRIBUTE_UNUSED,
2868 struct internal_reloc *rel,
2869 struct internal_syment *sym ATTRIBUTE_UNUSED,
2870 struct reloc_howto_struct *howto,
2871 bfd_vma val,
2872 bfd_vma addend,
2873 bfd_vma *relocation,
2874 bfd_byte *contents)
dbe341c6
TR
2875{
2876 struct xcoff_link_hash_entry *h;
12b2cce9 2877 bfd_vma section_offset;
dbe341c6 2878
cf9ab45b 2879 if (0 > rel->r_symndx)
b34976b6 2880 return FALSE;
dbe341c6
TR
2881
2882 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
12b2cce9 2883 section_offset = rel->r_vaddr - input_section->vma;
dbe341c6
TR
2884
2885 /* If we see an R_BR or R_RBR reloc which is jumping to global
2886 linkage code, and it is followed by an appropriate cror nop
2887 instruction, we replace the cror with lwz r2,20(r1). This
2888 restores the TOC after the glink code. Contrariwise, if the
2889 call is followed by a lwz r2,20(r1), but the call is not
2890 going to global linkage code, we can replace the load with a
2891 cror. */
cf9ab45b 2892 if (NULL != h
8602d4fe
RS
2893 && (bfd_link_hash_defined == h->root.type
2894 || bfd_link_hash_defweak == h->root.type)
12b2cce9 2895 && section_offset + 8 <= input_section->size)
dbe341c6
TR
2896 {
2897 bfd_byte *pnext;
2898 unsigned long next;
cf9ab45b 2899
12b2cce9 2900 pnext = contents + section_offset + 4;
dbe341c6 2901 next = bfd_get_32 (input_bfd, pnext);
cf9ab45b 2902
dbe341c6
TR
2903 /* The _ptrgl function is magic. It is used by the AIX
2904 compiler to call a function through a pointer. */
2905 if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
2906 {
cf9ab45b
AM
2907 if (next == 0x4def7b82 /* cror 15,15,15 */
2908 || next == 0x4ffffb82 /* cror 31,31,31 */
2909 || next == 0x60000000) /* ori r0,r0,0 */
12b2cce9 2910 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r2,20(r1) */
cf9ab45b
AM
2911
2912 }
2913 else
2914 {
12b2cce9 2915 if (next == 0x80410014) /* lwz r2,20(r1) */
cf9ab45b
AM
2916 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
2917 }
2918 }
2919 else if (NULL != h && bfd_link_hash_undefined == h->root.type)
dbe341c6
TR
2920 {
2921 /* Normally, this relocation is against a defined symbol. In the
2922 case where this is a partial link and the output section offset
cf9ab45b 2923 is greater than 2^25, the linker will return an invalid error
dbe341c6 2924 message that the relocation has been truncated. Yes it has been
cf9ab45b 2925 truncated but no it not important. For this case, disable the
dbe341c6 2926 overflow checking. */
cf9ab45b 2927
dbe341c6
TR
2928 howto->complain_on_overflow = complain_overflow_dont;
2929 }
cf9ab45b 2930
12b2cce9
RS
2931 /* The original PC-relative relocation is biased by -r_vaddr, so adding
2932 the value below will give the absolute target address. */
2933 *relocation = val + addend + rel->r_vaddr;
2934
a78eab4e
AM
2935 howto->src_mask &= ~3;
2936 howto->dst_mask = howto->src_mask;
dbe341c6 2937
12b2cce9 2938 if (h != NULL
8602d4fe
RS
2939 && (h->root.type == bfd_link_hash_defined
2940 || h->root.type == bfd_link_hash_defweak)
12b2cce9
RS
2941 && bfd_is_abs_section (h->root.u.def.section)
2942 && section_offset + 4 <= input_section->size)
2943 {
2944 bfd_byte *ptr;
2945 bfd_vma insn;
cf9ab45b 2946
12b2cce9
RS
2947 /* Turn the relative branch into an absolute one by setting the
2948 AA bit. */
2949 ptr = contents + section_offset;
2950 insn = bfd_get_32 (input_bfd, ptr);
2951 insn |= 2;
2952 bfd_put_32 (input_bfd, insn, ptr);
2953
2954 /* Make the howto absolute too. */
2955 howto->pc_relative = FALSE;
2956 howto->complain_on_overflow = complain_overflow_bitfield;
2957 }
2958 else
2959 {
2960 /* Use a PC-relative howto and subtract the instruction's address
2961 from the target address we calculated above. */
2962 howto->pc_relative = TRUE;
2963 *relocation -= (input_section->output_section->vma
2964 + input_section->output_offset
2965 + section_offset);
2966 }
b34976b6 2967 return TRUE;
dbe341c6
TR
2968}
2969
b34976b6 2970bfd_boolean
417236c0
TG
2971xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
2972 asection *input_section,
2973 bfd *output_bfd ATTRIBUTE_UNUSED,
2974 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2975 struct internal_syment *sym ATTRIBUTE_UNUSED,
2976 struct reloc_howto_struct *howto,
2977 bfd_vma val ATTRIBUTE_UNUSED,
2978 bfd_vma addend,
2979 bfd_vma *relocation,
2980 bfd_byte *contents ATTRIBUTE_UNUSED)
dbe341c6 2981{
b34976b6 2982 howto->pc_relative = TRUE;
a78eab4e
AM
2983 howto->src_mask &= ~3;
2984 howto->dst_mask = howto->src_mask;
dbe341c6
TR
2985
2986 /* A PC relative reloc includes the section address. */
2987 addend += input_section->vma;
2988
2989 *relocation = val + addend;
cf9ab45b
AM
2990 *relocation -= (input_section->output_section->vma
2991 + input_section->output_offset);
b34976b6 2992 return TRUE;
dbe341c6
TR
2993}
2994
b34976b6 2995static bfd_boolean
417236c0
TG
2996xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
2997 bfd_vma val ATTRIBUTE_UNUSED,
2998 bfd_vma relocation ATTRIBUTE_UNUSED,
2999 struct reloc_howto_struct *
3000 howto ATTRIBUTE_UNUSED)
dbe341c6 3001{
b34976b6 3002 return FALSE;
dbe341c6
TR
3003}
3004
b34976b6 3005static bfd_boolean
417236c0
TG
3006xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3007 bfd_vma val,
3008 bfd_vma relocation,
3009 struct reloc_howto_struct *howto)
dbe341c6 3010{
c7e2358a 3011 bfd_vma fieldmask, signmask, ss;
dbe341c6 3012 bfd_vma a, b, sum;
cf9ab45b 3013
dbe341c6
TR
3014 /* Get the values to be added together. For signed and unsigned
3015 relocations, we assume that all values should be truncated to
3016 the size of an address. For bitfields, all the bits matter.
3017 See also bfd_check_overflow. */
3018 fieldmask = N_ONES (howto->bitsize);
dbe341c6
TR
3019 a = relocation;
3020 b = val & howto->src_mask;
3021
3022 /* Much like unsigned, except no trimming with addrmask. In
3023 addition, the sum overflows if there is a carry out of
3024 the bfd_vma, i.e., the sum is less than either input
3025 operand. */
3026 a >>= howto->rightshift;
3027 b >>= howto->bitpos;
cf9ab45b 3028
dbe341c6
TR
3029 /* Bitfields are sometimes used for signed numbers; for
3030 example, a 13-bit field sometimes represents values in
3031 0..8191 and sometimes represents values in -4096..4095.
3032 If the field is signed and a is -4095 (0x1001) and b is
3033 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3034 0x1fff is 0x3000). It's not clear how to handle this
3035 everywhere, since there is not way to know how many bits
3036 are significant in the relocation, but the original code
3037 assumed that it was fully sign extended, and we will keep
3038 that assumption. */
3039 signmask = (fieldmask >> 1) + 1;
cf9ab45b 3040
dbe341c6
TR
3041 if ((a & ~ fieldmask) != 0)
3042 {
3043 /* Some bits out of the field are set. This might not
3044 be a problem: if this is a signed bitfield, it is OK
3045 iff all the high bits are set, including the sign
3046 bit. We'll try setting all but the most significant
3047 bit in the original relocation value: if this is all
3048 ones, we are OK, assuming a signed bitfield. */
3049 ss = (signmask << howto->rightshift) - 1;
3050 if ((ss | relocation) != ~ (bfd_vma) 0)
b34976b6 3051 return TRUE;
dbe341c6
TR
3052 a &= fieldmask;
3053 }
cf9ab45b 3054
dbe341c6 3055 /* We just assume (b & ~ fieldmask) == 0. */
cf9ab45b 3056
dbe341c6
TR
3057 /* We explicitly permit wrap around if this relocation
3058 covers the high bit of an address. The Linux kernel
3059 relies on it, and it is the only way to write assembler
3060 code which can run when loaded at a location 0x80000000
3061 away from the location at which it is linked. */
3062 if (howto->bitsize + howto->rightshift
3063 == bfd_arch_bits_per_address (input_bfd))
b34976b6 3064 return FALSE;
cf9ab45b 3065
dbe341c6
TR
3066 sum = a + b;
3067 if (sum < a || (sum & ~ fieldmask) != 0)
3068 {
3069 /* There was a carry out, or the field overflow. Test
3070 for signed operands again. Here is the overflow test
3071 is as for complain_overflow_signed. */
3072 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
b34976b6 3073 return TRUE;
dbe341c6 3074 }
cf9ab45b 3075
b34976b6 3076 return FALSE;
dbe341c6
TR
3077}
3078
b34976b6 3079static bfd_boolean
417236c0
TG
3080xcoff_complain_overflow_signed_func (bfd *input_bfd,
3081 bfd_vma val,
3082 bfd_vma relocation,
3083 struct reloc_howto_struct *howto)
dbe341c6
TR
3084{
3085 bfd_vma addrmask, fieldmask, signmask, ss;
3086 bfd_vma a, b, sum;
cf9ab45b 3087
dbe341c6
TR
3088 /* Get the values to be added together. For signed and unsigned
3089 relocations, we assume that all values should be truncated to
3090 the size of an address. For bitfields, all the bits matter.
3091 See also bfd_check_overflow. */
3092 fieldmask = N_ONES (howto->bitsize);
3093 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3094 a = relocation;
3095 b = val & howto->src_mask;
3096
3097 a = (a & addrmask) >> howto->rightshift;
cf9ab45b 3098
dbe341c6
TR
3099 /* If any sign bits are set, all sign bits must be set.
3100 That is, A must be a valid negative address after
3101 shifting. */
3102 signmask = ~ (fieldmask >> 1);
3103 ss = a & signmask;
3104 if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
b34976b6 3105 return TRUE;
cf9ab45b 3106
dbe341c6
TR
3107 /* We only need this next bit of code if the sign bit of B
3108 is below the sign bit of A. This would only happen if
3109 SRC_MASK had fewer bits than BITSIZE. Note that if
3110 SRC_MASK has more bits than BITSIZE, we can get into
3111 trouble; we would need to verify that B is in range, as
3112 we do for A above. */
3113 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3114 if ((b & signmask) != 0)
3115 {
3116 /* Set all the bits above the sign bit. */
3117 b -= signmask <<= 1;
3118 }
cf9ab45b 3119
dbe341c6 3120 b = (b & addrmask) >> howto->bitpos;
cf9ab45b 3121
dbe341c6
TR
3122 /* Now we can do the addition. */
3123 sum = a + b;
cf9ab45b 3124
dbe341c6
TR
3125 /* See if the result has the correct sign. Bits above the
3126 sign bit are junk now; ignore them. If the sum is
3127 positive, make sure we did not have all negative inputs;
3128 if the sum is negative, make sure we did not have all
3129 positive inputs. The test below looks only at the sign
3130 bits, and it really just
3131 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3132 */
3133 signmask = (fieldmask >> 1) + 1;
3134 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
b34976b6 3135 return TRUE;
cf9ab45b 3136
b34976b6 3137 return FALSE;
dbe341c6
TR
3138}
3139
b34976b6 3140static bfd_boolean
417236c0
TG
3141xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3142 bfd_vma val,
3143 bfd_vma relocation,
3144 struct reloc_howto_struct *howto)
dbe341c6
TR
3145{
3146 bfd_vma addrmask, fieldmask;
3147 bfd_vma a, b, sum;
cf9ab45b 3148
dbe341c6
TR
3149 /* Get the values to be added together. For signed and unsigned
3150 relocations, we assume that all values should be truncated to
3151 the size of an address. For bitfields, all the bits matter.
3152 See also bfd_check_overflow. */
3153 fieldmask = N_ONES (howto->bitsize);
3154 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3155 a = relocation;
3156 b = val & howto->src_mask;
3157
3158 /* Checking for an unsigned overflow is relatively easy:
3159 trim the addresses and add, and trim the result as well.
3160 Overflow is normally indicated when the result does not
3161 fit in the field. However, we also need to consider the
3162 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3163 input is 0x80000000, and bfd_vma is only 32 bits; then we
3164 will get sum == 0, but there is an overflow, since the
3165 inputs did not fit in the field. Instead of doing a
3166 separate test, we can check for this by or-ing in the
3167 operands when testing for the sum overflowing its final
3168 field. */
3169 a = (a & addrmask) >> howto->rightshift;
3170 b = (b & addrmask) >> howto->bitpos;
3171 sum = (a + b) & addrmask;
3172 if ((a | b | sum) & ~ fieldmask)
b34976b6 3173 return TRUE;
cf9ab45b 3174
b34976b6 3175 return FALSE;
dbe341c6 3176}
beb1bf64
TR
3177
3178/* This is the relocation function for the RS/6000/POWER/PowerPC.
3179 This is currently the only processor which uses XCOFF; I hope that
cf9ab45b 3180 will never change.
beb1bf64 3181
dbe341c6
TR
3182 I took the relocation type definitions from two documents:
3183 the PowerPC AIX Version 4 Application Binary Interface, First
3184 Edition (April 1992), and the PowerOpen ABI, Big-Endian
3185 32-Bit Hardware Implementation (June 30, 1994). Differences
cf9ab45b 3186 between the documents are noted below.
dbe341c6 3187
cf9ab45b 3188 Unsupported r_type's
dbe341c6
TR
3189
3190 R_RTB:
3191 R_RRTBI:
3192 R_RRTBA:
cf9ab45b 3193
dbe341c6
TR
3194 These relocs are defined by the PowerPC ABI to be
3195 relative branches which use half of the difference
3196 between the symbol and the program counter. I can't
3197 quite figure out when this is useful. These relocs are
cf9ab45b 3198 not defined by the PowerOpen ABI.
dbe341c6
TR
3199
3200 Supported r_type's
3201
3202 R_POS:
3203 Simple positive relocation.
3204
3205 R_NEG:
cf9ab45b 3206 Simple negative relocation.
dbe341c6
TR
3207
3208 R_REL:
3209 Simple PC relative relocation.
3210
3211 R_TOC:
3212 TOC relative relocation. The value in the instruction in
3213 the input file is the offset from the input file TOC to
3214 the desired location. We want the offset from the final
3215 TOC to the desired location. We have:
3216 isym = iTOC + in
3217 iinsn = in + o
3218 osym = oTOC + on
3219 oinsn = on + o
3220 so we must change insn by on - in.
3221
3222 R_GL:
3223 GL linkage relocation. The value of this relocation
cf9ab45b 3224 is the address of the entry in the TOC section.
dbe341c6
TR
3225
3226 R_TCL:
3227 Local object TOC address. I can't figure out the
cf9ab45b 3228 difference between this and case R_GL.
dbe341c6
TR
3229
3230 R_TRL:
3231 TOC relative relocation. A TOC relative load instruction
3232 which may be changed to a load address instruction.
cf9ab45b 3233 FIXME: We don't currently implement this optimization.
dbe341c6
TR
3234
3235 R_TRLA:
3236 TOC relative relocation. This is a TOC relative load
3237 address instruction which may be changed to a load
3238 instruction. FIXME: I don't know if this is the correct
3239 implementation.
3240
3241 R_BA:
3242 Absolute branch. We don't want to mess with the lower
cf9ab45b 3243 two bits of the instruction.
dbe341c6
TR
3244
3245 R_CAI:
3246 The PowerPC ABI defines this as an absolute call which
3247 may be modified to become a relative call. The PowerOpen
cf9ab45b
AM
3248 ABI does not define this relocation type.
3249
dbe341c6
TR
3250 R_RBA:
3251 Absolute branch which may be modified to become a
cf9ab45b 3252 relative branch.
dbe341c6
TR
3253
3254 R_RBAC:
3255 The PowerPC ABI defines this as an absolute branch to a
3256 fixed address which may be modified to an absolute branch
3257 to a symbol. The PowerOpen ABI does not define this
cf9ab45b 3258 relocation type.
dbe341c6
TR
3259
3260 R_RBRC:
3261 The PowerPC ABI defines this as an absolute branch to a
3262 fixed address which may be modified to a relative branch.
cf9ab45b 3263 The PowerOpen ABI does not define this relocation type.
dbe341c6
TR
3264
3265 R_BR:
3266 Relative branch. We don't want to mess with the lower
cf9ab45b 3267 two bits of the instruction.
dbe341c6
TR
3268
3269 R_CREL:
3270 The PowerPC ABI defines this as a relative call which may
3271 be modified to become an absolute call. The PowerOpen
cf9ab45b 3272 ABI does not define this relocation type.
dbe341c6
TR
3273
3274 R_RBR:
3275 A relative branch which may be modified to become an
12b2cce9 3276 absolute branch.
dbe341c6
TR
3277
3278 R_RL:
3279 The PowerPC AIX ABI describes this as a load which may be
3280 changed to a load address. The PowerOpen ABI says this
cf9ab45b 3281 is the same as case R_POS.
dbe341c6
TR
3282
3283 R_RLA:
3284 The PowerPC AIX ABI describes this as a load address
3285 which may be changed to a load. The PowerOpen ABI says
cf9ab45b 3286 this is the same as R_POS.
dbe341c6
TR
3287*/
3288
b34976b6 3289bfd_boolean
417236c0
TG
3290xcoff_ppc_relocate_section (bfd *output_bfd,
3291 struct bfd_link_info *info,
3292 bfd *input_bfd,
3293 asection *input_section,
3294 bfd_byte *contents,
3295 struct internal_reloc *relocs,
3296 struct internal_syment *syms,
3297 asection **sections)
beb1bf64
TR
3298{
3299 struct internal_reloc *rel;
3300 struct internal_reloc *relend;
3301
3302 rel = relocs;
3303 relend = rel + input_section->reloc_count;
beb1bf64
TR
3304 for (; rel < relend; rel++)
3305 {
3306 long symndx;
3307 struct xcoff_link_hash_entry *h;
3308 struct internal_syment *sym;
3309 bfd_vma addend;
3310 bfd_vma val;
3311 struct reloc_howto_struct howto;
dbe341c6
TR
3312 bfd_vma relocation;
3313 bfd_vma value_to_relocate;
3314 bfd_vma address;
3315 bfd_byte *location;
beb1bf64
TR
3316
3317 /* Relocation type R_REF is a special relocation type which is
cf9ab45b
AM
3318 merely used to prevent garbage collection from occurring for
3319 the csect including the symbol which it references. */
beb1bf64
TR
3320 if (rel->r_type == R_REF)
3321 continue;
3322
dbe341c6 3323 /* howto */
beb1bf64
TR
3324 howto.type = rel->r_type;
3325 howto.rightshift = 0;
beb1bf64 3326 howto.bitsize = (rel->r_size & 0x1f) + 1;
dbe341c6 3327 howto.size = howto.bitsize > 16 ? 2 : 1;
b34976b6 3328 howto.pc_relative = FALSE;
beb1bf64 3329 howto.bitpos = 0;
cf9ab45b
AM
3330 howto.complain_on_overflow = (rel->r_size & 0x80
3331 ? complain_overflow_signed
3332 : complain_overflow_bitfield);
beb1bf64
TR
3333 howto.special_function = NULL;
3334 howto.name = "internal";
b34976b6 3335 howto.partial_inplace = TRUE;
cf9ab45b 3336 howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
b34976b6 3337 howto.pcrel_offset = FALSE;
beb1bf64 3338
dbe341c6 3339 /* symbol */
beb1bf64 3340 val = 0;
dbe341c6
TR
3341 addend = 0;
3342 h = NULL;
3343 sym = NULL;
cf9ab45b 3344 symndx = rel->r_symndx;
beb1bf64 3345
cf9ab45b 3346 if (-1 != symndx)
beb1bf64
TR
3347 {
3348 asection *sec;
cf9ab45b 3349
dbe341c6
TR
3350 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3351 sym = syms + symndx;
3352 addend = - sym->n_value;
cf9ab45b
AM
3353
3354 if (NULL == h)
beb1bf64
TR
3355 {
3356 sec = sections[symndx];
3357 /* Hack to make sure we use the right TOC anchor value
dbe341c6 3358 if this reloc is against the TOC anchor. */
beb1bf64 3359 if (sec->name[3] == '0'
dbe341c6
TR
3360 && strcmp (sec->name, ".tc0") == 0)
3361 val = xcoff_data (output_bfd)->toc;
f4ffd778 3362 else
dbe341c6
TR
3363 val = (sec->output_section->vma
3364 + sec->output_offset
3365 + sym->n_value
3366 - sec->vma);
cf9ab45b
AM
3367 }
3368 else
dbe341c6 3369 {
858ef0ce
RS
3370 if (info->unresolved_syms_in_objects != RM_IGNORE
3371 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3372 {
3373 if (! ((*info->callbacks->undefined_symbol)
3374 (info, h->root.root.string,
3375 input_bfd, input_section,
3376 rel->r_vaddr - input_section->vma,
3377 (info->unresolved_syms_in_objects
3378 == RM_GENERATE_ERROR))))
3379 return FALSE;
3380 }
cf9ab45b
AM
3381 if (h->root.type == bfd_link_hash_defined
3382 || h->root.type == bfd_link_hash_defweak)
dbe341c6
TR
3383 {
3384 sec = h->root.u.def.section;
3385 val = (h->root.u.def.value
3386 + sec->output_section->vma
3387 + sec->output_offset);
cf9ab45b
AM
3388 }
3389 else if (h->root.type == bfd_link_hash_common)
f4ffd778 3390 {
dbe341c6 3391 sec = h->root.u.c.p->section;
f4ffd778 3392 val = (sec->output_section->vma
dbe341c6 3393 + sec->output_offset);
cf9ab45b
AM
3394
3395 }
858ef0ce 3396 else
dbe341c6 3397 {
858ef0ce 3398 BFD_ASSERT (info->relocatable
94313f36
RS
3399 || (info->static_link
3400 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
858ef0ce
RS
3401 || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3402 || (h->flags & XCOFF_IMPORT) != 0);
f4ffd778 3403 }
beb1bf64
TR
3404 }
3405 }
beb1bf64 3406
cf9ab45b
AM
3407 if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3408 || !((*xcoff_calculate_relocation[rel->r_type])
3409 (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3410 addend, &relocation, contents)))
b34976b6 3411 return FALSE;
cf9ab45b 3412
dbe341c6
TR
3413 /* address */
3414 address = rel->r_vaddr - input_section->vma;
3415 location = contents + address;
cf9ab45b 3416
eea6121a 3417 if (address > input_section->size)
cf9ab45b 3418 abort ();
dbe341c6
TR
3419
3420 /* Get the value we are going to relocate. */
3421 if (1 == howto.size)
3422 value_to_relocate = bfd_get_16 (input_bfd, location);
cf9ab45b 3423 else
dbe341c6 3424 value_to_relocate = bfd_get_32 (input_bfd, location);
cf9ab45b
AM
3425
3426 /* overflow.
3427
dbe341c6
TR
3428 FIXME: We may drop bits during the addition
3429 which we don't check for. We must either check at every single
3430 operation, which would be tedious, or we must do the computations
3431 in a type larger than bfd_vma, which would be inefficient. */
beb1bf64 3432
cf9ab45b
AM
3433 if ((unsigned int) howto.complain_on_overflow
3434 >= XCOFF_MAX_COMPLAIN_OVERFLOW)
3435 abort ();
3436
3437 if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3438 (input_bfd, value_to_relocate, relocation, &howto)))
beb1bf64 3439 {
dbe341c6
TR
3440 const char *name;
3441 char buf[SYMNMLEN + 1];
3442 char reloc_type_name[10];
cf9ab45b
AM
3443
3444 if (symndx == -1)
beb1bf64 3445 {
dbe341c6 3446 name = "*ABS*";
cf9ab45b
AM
3447 }
3448 else if (h != NULL)
beb1bf64 3449 {
dfeffb9f 3450 name = NULL;
cf9ab45b
AM
3451 }
3452 else
beb1bf64 3453 {
dbe341c6
TR
3454 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3455 if (name == NULL)
3456 name = "UNKNOWN";
beb1bf64 3457 }
dbe341c6 3458 sprintf (reloc_type_name, "0x%02x", rel->r_type);
cf9ab45b 3459
dbe341c6 3460 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3461 (info, (h ? &h->root : NULL), name, reloc_type_name,
3462 (bfd_vma) 0, input_bfd, input_section,
3463 rel->r_vaddr - input_section->vma)))
b34976b6 3464 return FALSE;
beb1bf64 3465 }
cf9ab45b 3466
dbe341c6 3467 /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
cf9ab45b
AM
3468 value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3469 | (((value_to_relocate & howto.src_mask)
3470 + relocation) & howto.dst_mask));
3471
dbe341c6
TR
3472 /* Put the value back in the object file. */
3473 if (1 == howto.size)
3474 bfd_put_16 (input_bfd, value_to_relocate, location);
cf9ab45b 3475 else
dbe341c6 3476 bfd_put_32 (input_bfd, value_to_relocate, location);
beb1bf64
TR
3477 }
3478
b34976b6 3479 return TRUE;
beb1bf64
TR
3480}
3481
b34976b6 3482static bfd_boolean
417236c0
TG
3483_bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
3484 struct xcoff_loader_info *ldinfo,
3485 struct internal_ldsym *ldsym,
3486 const char *name)
beb1bf64
TR
3487{
3488 size_t len;
3489 len = strlen (name);
3490
3491 if (len <= SYMNMLEN)
3492 strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3493 else
3494 {
3495 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3496 {
dc810e39 3497 bfd_size_type newalc;
f075ee0c 3498 char *newstrings;
beb1bf64
TR
3499
3500 newalc = ldinfo->string_alc * 2;
3501 if (newalc == 0)
3502 newalc = 32;
3503 while (ldinfo->string_size + len + 3 > newalc)
3504 newalc *= 2;
3505
f075ee0c 3506 newstrings = bfd_realloc (ldinfo->strings, newalc);
beb1bf64
TR
3507 if (newstrings == NULL)
3508 {
b34976b6
AM
3509 ldinfo->failed = TRUE;
3510 return FALSE;
beb1bf64
TR
3511 }
3512 ldinfo->string_alc = newalc;
3513 ldinfo->strings = newstrings;
3514 }
3515
dc810e39
AM
3516 bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
3517 ldinfo->strings + ldinfo->string_size);
beb1bf64
TR
3518 strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3519 ldsym->_l._l_l._l_zeroes = 0;
3520 ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3521 ldinfo->string_size += len + 3;
3522 }
3523
b34976b6 3524 return TRUE;
beb1bf64
TR
3525}
3526
b34976b6 3527static bfd_boolean
dc810e39 3528_bfd_xcoff_put_symbol_name (bfd *abfd, struct bfd_strtab_hash *strtab,
beb1bf64 3529 struct internal_syment *sym,
f4ffd778
NC
3530 const char *name)
3531{
3532 if (strlen (name) <= SYMNMLEN)
3533 {
3534 strncpy (sym->_n._n_name, name, SYMNMLEN);
3535 }
3536 else
3537 {
b34976b6 3538 bfd_boolean hash;
f4ffd778
NC
3539 bfd_size_type indx;
3540
b34976b6 3541 hash = TRUE;
f4ffd778 3542 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
b34976b6
AM
3543 hash = FALSE;
3544 indx = _bfd_stringtab_add (strtab, name, hash, FALSE);
f4ffd778 3545 if (indx == (bfd_size_type) -1)
b34976b6 3546 return FALSE;
f4ffd778
NC
3547 sym->_n._n_n._n_zeroes = 0;
3548 sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3549 }
b34976b6 3550 return TRUE;
beb1bf64
TR
3551}
3552
3553static asection *
417236c0
TG
3554xcoff_create_csect_from_smclas (bfd *abfd,
3555 union internal_auxent *aux,
3556 const char *symbol_name)
beb1bf64 3557{
beb1bf64
TR
3558 asection *return_value = NULL;
3559
f4ffd778
NC
3560 /* .sv64 = x_smclas == 17
3561 This is an invalid csect for 32 bit apps. */
9a1ada6c
TG
3562 static const char * const names[] =
3563 {
3564 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
3565 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
3566 ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
3567 };
3568
3569 if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
cf9ab45b 3570 && (NULL != names[aux->x_csect.x_smclas]))
f4ffd778 3571 {
dc810e39 3572 return_value = bfd_make_section_anyway
f4ffd778
NC
3573 (abfd, names[aux->x_csect.x_smclas]);
3574 }
3575 else
3576 {
3577 (*_bfd_error_handler)
d003868e
AM
3578 (_("%B: symbol `%s' has unrecognized smclas %d"),
3579 abfd, symbol_name, aux->x_csect.x_smclas);
f4ffd778
NC
3580 bfd_set_error (bfd_error_bad_value);
3581 }
beb1bf64
TR
3582
3583 return return_value;
3584}
3585
b34976b6 3586static bfd_boolean
417236c0 3587xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
beb1bf64 3588{
f4ffd778 3589 if (0xffff <= value)
b34976b6 3590 return TRUE;
f4ffd778 3591
b34976b6 3592 return FALSE;
beb1bf64
TR
3593}
3594
b34976b6 3595static bfd_boolean
417236c0 3596xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
beb1bf64 3597{
f4ffd778 3598 if (0xffff <= value)
b34976b6 3599 return TRUE;
f4ffd778 3600
b34976b6 3601 return FALSE;
beb1bf64
TR
3602}
3603
a7b97311 3604static bfd_vma
417236c0
TG
3605xcoff_loader_symbol_offset (bfd *abfd,
3606 struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
beb1bf64 3607{
cf9ab45b 3608 return bfd_xcoff_ldhdrsz (abfd);
beb1bf64
TR
3609}
3610
a7b97311 3611static bfd_vma
417236c0 3612xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
beb1bf64 3613{
cf9ab45b 3614 return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
beb1bf64
TR
3615}
3616
b34976b6 3617static bfd_boolean
417236c0
TG
3618xcoff_generate_rtinit (bfd *abfd, const char *init, const char *fini,
3619 bfd_boolean rtld)
9a4c7f16
TR
3620{
3621 bfd_byte filehdr_ext[FILHSZ];
3622 bfd_byte scnhdr_ext[SCNHSZ];
69f284c7
TR
3623 bfd_byte syment_ext[SYMESZ * 10];
3624 bfd_byte reloc_ext[RELSZ * 3];
9a4c7f16
TR
3625 bfd_byte *data_buffer;
3626 bfd_size_type data_buffer_size;
d426c6b0 3627 bfd_byte *string_table = NULL, *st_tmp = NULL;
9a4c7f16
TR
3628 bfd_size_type string_table_size;
3629 bfd_vma val;
3630 size_t initsz, finisz;
3631 struct internal_filehdr filehdr;
3632 struct internal_scnhdr scnhdr;
3633 struct internal_syment syment;
3634 union internal_auxent auxent;
3635 struct internal_reloc reloc;
cf9ab45b 3636
9a4c7f16
TR
3637 char *data_name = ".data";
3638 char *rtinit_name = "__rtinit";
69f284c7 3639 char *rtld_name = "__rtld";
cf9ab45b 3640
69f284c7 3641 if (! bfd_xcoff_rtinit_size (abfd))
b34976b6 3642 return FALSE;
9a4c7f16
TR
3643
3644 initsz = (init == NULL ? 0 : 1 + strlen (init));
3645 finisz = (fini == NULL ? 0 : 1 + strlen (fini));
3646
3647 /* file header */
3648 memset (filehdr_ext, 0, FILHSZ);
3649 memset (&filehdr, 0, sizeof (struct internal_filehdr));
3650 filehdr.f_magic = bfd_xcoff_magic_number (abfd);
cf9ab45b 3651 filehdr.f_nscns = 1;
9a4c7f16 3652 filehdr.f_timdat = 0;
69f284c7 3653 filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
9a4c7f16
TR
3654 filehdr.f_symptr = 0; /* set below */
3655 filehdr.f_opthdr = 0;
3656 filehdr.f_flags = 0;
3657
3658 /* section header */
3659 memset (scnhdr_ext, 0, SCNHSZ);
3660 memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
3661 memcpy (scnhdr.s_name, data_name, strlen (data_name));
3662 scnhdr.s_paddr = 0;
3663 scnhdr.s_vaddr = 0;
3664 scnhdr.s_size = 0; /* set below */
3665 scnhdr.s_scnptr = FILHSZ + SCNHSZ;
3666 scnhdr.s_relptr = 0; /* set below */
3667 scnhdr.s_lnnoptr = 0;
3668 scnhdr.s_nreloc = 0; /* either 1 or 2 */
3669 scnhdr.s_nlnno = 0;
3670 scnhdr.s_flags = STYP_DATA;
3671
cf9ab45b
AM
3672 /* .data
3673 0x0000 0x00000000 : rtl
3674 0x0004 0x00000010 : offset to init, or 0
3675 0x0008 0x00000028 : offset to fini, or 0
3676 0x000C 0x0000000C : size of descriptor
3677 0x0010 0x00000000 : init, needs a reloc
3678 0x0014 0x00000040 : offset to init name
3679 0x0018 0x00000000 : flags, padded to a word
3680 0x001C 0x00000000 : empty init
3681 0x0020 0x00000000 :
3682 0x0024 0x00000000 :
3683 0x0028 0x00000000 : fini, needs a reloc
3684 0x002C 0x00000??? : offset to fini name
3685 0x0030 0x00000000 : flags, padded to a word
3686 0x0034 0x00000000 : empty fini
3687 0x0038 0x00000000 :
3688 0x003C 0x00000000 :
3689 0x0040 init name
9a4c7f16
TR
3690 0x0040 + initsz fini name */
3691
3692 data_buffer_size = 0x0040 + initsz + finisz;
2a52da53 3693 data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
330693f5 3694 data_buffer = NULL;
9bab7074 3695 data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
330693f5 3696 if (data_buffer == NULL)
b34976b6 3697 return FALSE;
9a4c7f16 3698
cf9ab45b 3699 if (initsz)
9a4c7f16
TR
3700 {
3701 val = 0x10;
3702 bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
3703 val = 0x40;
3704 bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
3705 memcpy (&data_buffer[val], init, initsz);
3706 }
3707
cf9ab45b 3708 if (finisz)
9a4c7f16
TR
3709 {
3710 val = 0x28;
3711 bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
3712 val = 0x40 + initsz;
3713 bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
3714 memcpy (&data_buffer[val], fini, finisz);
3715 }
3716
3717 val = 0x0C;
3718 bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
3719
3720 scnhdr.s_size = data_buffer_size;
3721
3722 /* string table */
3723 string_table_size = 0;
cf9ab45b 3724 if (initsz > 9)
9a4c7f16
TR
3725 string_table_size += initsz;
3726 if (finisz > 9)
3727 string_table_size += finisz;
3728 if (string_table_size)
3729 {
3730 string_table_size += 4;
9bab7074 3731 string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
021d6096 3732 if (string_table == NULL)
b34976b6 3733 return FALSE;
9bab7074 3734
9a4c7f16
TR
3735 val = string_table_size;
3736 bfd_h_put_32 (abfd, val, &string_table[0]);
3737 st_tmp = string_table + 4;
3738 }
cf9ab45b
AM
3739
3740 /* symbols
9a4c7f16
TR
3741 0. .data csect
3742 2. __rtinit
cf9ab45b
AM
3743 4. init function
3744 6. fini function
69f284c7
TR
3745 8. __rtld */
3746 memset (syment_ext, 0, 10 * SYMESZ);
3747 memset (reloc_ext, 0, 3 * RELSZ);
9a4c7f16
TR
3748
3749 /* .data csect */
3750 memset (&syment, 0, sizeof (struct internal_syment));
3751 memset (&auxent, 0, sizeof (union internal_auxent));
3752 memcpy (syment._n._n_name, data_name, strlen (data_name));
3753 syment.n_scnum = 1;
3754 syment.n_sclass = C_HIDEXT;
3755 syment.n_numaux = 1;
3756 auxent.x_csect.x_scnlen.l = data_buffer_size;
3757 auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
3758 auxent.x_csect.x_smclas = XMC_RW;
cf9ab45b 3759 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3760 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3761 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3762 syment.n_numaux,
9a4c7f16
TR
3763 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3764 filehdr.f_nsyms += 2;
3765
3766 /* __rtinit */
3767 memset (&syment, 0, sizeof (struct internal_syment));
3768 memset (&auxent, 0, sizeof (union internal_auxent));
3769 memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
3770 syment.n_scnum = 1;
3771 syment.n_sclass = C_EXT;
3772 syment.n_numaux = 1;
3773 auxent.x_csect.x_smtyp = XTY_LD;
3774 auxent.x_csect.x_smclas = XMC_RW;
cf9ab45b 3775 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3776 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3777 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3778 syment.n_numaux,
9a4c7f16
TR
3779 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3780 filehdr.f_nsyms += 2;
3781
3782 /* init */
cf9ab45b 3783 if (initsz)
9a4c7f16
TR
3784 {
3785 memset (&syment, 0, sizeof (struct internal_syment));
3786 memset (&auxent, 0, sizeof (union internal_auxent));
3787
cf9ab45b 3788 if (initsz > 9)
9a4c7f16
TR
3789 {
3790 syment._n._n_n._n_offset = st_tmp - string_table;
3791 memcpy (st_tmp, init, initsz);
3792 st_tmp += initsz;
3793 }
3794 else
3795 memcpy (syment._n._n_name, init, initsz - 1);
3796
3797 syment.n_sclass = C_EXT;
3798 syment.n_numaux = 1;
cf9ab45b 3799 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3800 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3801 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3802 syment.n_numaux,
9a4c7f16
TR
3803 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3804
3805 /* reloc */
3806 memset (&reloc, 0, sizeof (struct internal_reloc));
3807 reloc.r_vaddr = 0x0010;
3808 reloc.r_symndx = filehdr.f_nsyms;
3809 reloc.r_type = R_POS;
3810 reloc.r_size = 31;
3811 bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
3812
3813 filehdr.f_nsyms += 2;
3814 scnhdr.s_nreloc += 1;
3815 }
cf9ab45b 3816
9a4c7f16 3817 /* fini */
cf9ab45b 3818 if (finisz)
9a4c7f16
TR
3819 {
3820 memset (&syment, 0, sizeof (struct internal_syment));
3821 memset (&auxent, 0, sizeof (union internal_auxent));
3822
cf9ab45b 3823 if (finisz > 9)
9a4c7f16
TR
3824 {
3825 syment._n._n_n._n_offset = st_tmp - string_table;
3826 memcpy (st_tmp, fini, finisz);
3827 st_tmp += finisz;
3828 }
3829 else
3830 memcpy (syment._n._n_name, fini, finisz - 1);
3831
3832 syment.n_sclass = C_EXT;
3833 syment.n_numaux = 1;
cf9ab45b 3834 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3835 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3836 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3837 syment.n_numaux,
9a4c7f16
TR
3838 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3839
3840 /* reloc */
3841 memset (&reloc, 0, sizeof (struct internal_reloc));
3842 reloc.r_vaddr = 0x0028;
3843 reloc.r_symndx = filehdr.f_nsyms;
3844 reloc.r_type = R_POS;
3845 reloc.r_size = 31;
cf9ab45b 3846 bfd_coff_swap_reloc_out (abfd, &reloc,
9a4c7f16
TR
3847 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3848
3849 filehdr.f_nsyms += 2;
3850 scnhdr.s_nreloc += 1;
3851 }
3852
69f284c7
TR
3853 if (rtld)
3854 {
3855 memset (&syment, 0, sizeof (struct internal_syment));
3856 memset (&auxent, 0, sizeof (union internal_auxent));
3857 memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
3858 syment.n_sclass = C_EXT;
3859 syment.n_numaux = 1;
cf9ab45b 3860 bfd_coff_swap_sym_out (abfd, &syment,
69f284c7 3861 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3862 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3863 syment.n_numaux,
69f284c7
TR
3864 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3865
3866 /* reloc */
3867 memset (&reloc, 0, sizeof (struct internal_reloc));
3868 reloc.r_vaddr = 0x0000;
3869 reloc.r_symndx = filehdr.f_nsyms;
3870 reloc.r_type = R_POS;
3871 reloc.r_size = 31;
cf9ab45b 3872 bfd_coff_swap_reloc_out (abfd, &reloc,
69f284c7
TR
3873 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3874
3875 filehdr.f_nsyms += 2;
3876 scnhdr.s_nreloc += 1;
3877 }
3878
9a4c7f16
TR
3879 scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
3880 filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
3881
3882 bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
3883 bfd_bwrite (filehdr_ext, FILHSZ, abfd);
3884 bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
3885 bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
3886 bfd_bwrite (data_buffer, data_buffer_size, abfd);
3887 bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
3888 bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
3889 bfd_bwrite (string_table, string_table_size, abfd);
3890
330693f5
TR
3891 free (data_buffer);
3892 data_buffer = NULL;
3893
b34976b6 3894 return TRUE;
9a4c7f16
TR
3895}
3896
beb1bf64
TR
3897
3898static reloc_howto_type xcoff_dynamic_reloc =
cf9ab45b
AM
3899HOWTO (0, /* type */
3900 0, /* rightshift */
3901 2, /* size (0 = byte, 1 = short, 2 = long) */
3902 32, /* bitsize */
b34976b6 3903 FALSE, /* pc_relative */
cf9ab45b 3904 0, /* bitpos */
beb1bf64 3905 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
3906 0, /* special_function */
3907 "R_POS", /* name */
b34976b6 3908 TRUE, /* partial_inplace */
cf9ab45b
AM
3909 0xffffffff, /* src_mask */
3910 0xffffffff, /* dst_mask */
b34976b6 3911 FALSE); /* pcrel_offset */
beb1bf64 3912
dc810e39
AM
3913/* glink
3914
3915 The first word of global linkage code must be modified by filling in
f4ffd778
NC
3916 the correct TOC offset. */
3917
beb1bf64 3918static unsigned long xcoff_glink_code[9] =
f4ffd778
NC
3919 {
3920 0x81820000, /* lwz r12,0(r2) */
3921 0x90410014, /* stw r2,20(r1) */
3922 0x800c0000, /* lwz r0,0(r12) */
3923 0x804c0004, /* lwz r2,4(r12) */
3924 0x7c0903a6, /* mtctr r0 */
3925 0x4e800420, /* bctr */
3926 0x00000000, /* start of traceback table */
3927 0x000c8000, /* traceback table */
3928 0x00000000, /* traceback table */
3929 };
beb1bf64 3930
85645aed
TG
3931/* Table to convert DWARF flags to section names. */
3932
3933const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
3934 { SSUBTYP_DWINFO, ".dwinfo", TRUE },
3935 { SSUBTYP_DWLINE, ".dwline", TRUE },
3936 { SSUBTYP_DWPBNMS, ".dwpbnms", TRUE },
3937 { SSUBTYP_DWPBTYP, ".dwpbtyp", TRUE },
3938 { SSUBTYP_DWARNGE, ".dwarnge", TRUE },
3939 { SSUBTYP_DWABREV, ".dwabrev", FALSE },
3940 { SSUBTYP_DWSTR, ".dwstr", TRUE },
3941 { SSUBTYP_DWRNGES, ".dwrnges", TRUE }
3942};
beb1bf64 3943
dc810e39 3944static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
f4ffd778
NC
3945 {
3946 { /* COFF backend, defined in libcoff.h. */
cf9ab45b
AM
3947 _bfd_xcoff_swap_aux_in,
3948 _bfd_xcoff_swap_sym_in,
3949 coff_swap_lineno_in,
3950 _bfd_xcoff_swap_aux_out,
3951 _bfd_xcoff_swap_sym_out,
3952 coff_swap_lineno_out,
3953 xcoff_swap_reloc_out,
3954 coff_swap_filehdr_out,
3955 coff_swap_aouthdr_out,
3956 coff_swap_scnhdr_out,
3957 FILHSZ,
3958 AOUTSZ,
3959 SCNHSZ,
3960 SYMESZ,
3961 AUXESZ,
3962 RELSZ,
3963 LINESZ,
3964 FILNMLEN,
b34976b6 3965 TRUE, /* _bfd_coff_long_filenames */
88183869 3966 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
cf9ab45b 3967 3, /* _bfd_coff_default_section_alignment_power */
b34976b6 3968 FALSE, /* _bfd_coff_force_symnames_in_strings */
cf9ab45b
AM
3969 2, /* _bfd_coff_debug_string_prefix_length */
3970 coff_swap_filehdr_in,
3971 coff_swap_aouthdr_in,
3972 coff_swap_scnhdr_in,
3973 xcoff_swap_reloc_in,
3974 coff_bad_format_hook,
3975 coff_set_arch_mach_hook,
3976 coff_mkobject_hook,
3977 styp_to_sec_flags,
3978 coff_set_alignment_hook,
3979 coff_slurp_symbol_table,
3980 symname_in_debug_hook,
3981 coff_pointerize_aux_hook,
3982 coff_print_aux,
3983 dummy_reloc16_extra_cases,
3984 dummy_reloc16_estimate,
3985 NULL, /* bfd_coff_sym_is_global */
3986 coff_compute_section_file_positions,
3987 NULL, /* _bfd_coff_start_final_link */
3988 xcoff_ppc_relocate_section,
3989 coff_rtype_to_howto,
3990 NULL, /* _bfd_coff_adjust_symndx */
3991 _bfd_generic_link_add_one_symbol,
3992 coff_link_output_has_begun,
2b5c217d
NC
3993 coff_final_link_postscript,
3994 NULL /* print_pdata. */
f4ffd778
NC
3995 },
3996
cf9ab45b
AM
3997 0x01DF, /* magic number */
3998 bfd_arch_rs6000,
3999 bfd_mach_rs6k,
dc810e39 4000
f4ffd778 4001 /* Function pointers to xcoff specific swap routines. */
cf9ab45b
AM
4002 xcoff_swap_ldhdr_in,
4003 xcoff_swap_ldhdr_out,
4004 xcoff_swap_ldsym_in,
4005 xcoff_swap_ldsym_out,
4006 xcoff_swap_ldrel_in,
4007 xcoff_swap_ldrel_out,
f4ffd778
NC
4008
4009 /* Sizes. */
cf9ab45b
AM
4010 LDHDRSZ,
4011 LDSYMSZ,
4012 LDRELSZ,
4013 12, /* _xcoff_function_descriptor_size */
4014 SMALL_AOUTSZ,
f4ffd778 4015
cf9ab45b
AM
4016 /* Versions. */
4017 1, /* _xcoff_ldhdr_version */
f4ffd778 4018
cf9ab45b
AM
4019 _bfd_xcoff_put_symbol_name,
4020 _bfd_xcoff_put_ldsymbol_name,
4021 &xcoff_dynamic_reloc,
4022 xcoff_create_csect_from_smclas,
f4ffd778
NC
4023
4024 /* Lineno and reloc count overflow. */
4025 xcoff_is_lineno_count_overflow,
4026 xcoff_is_reloc_count_overflow,
4027
4028 xcoff_loader_symbol_offset,
4029 xcoff_loader_reloc_offset,
4030
4031 /* glink. */
cf9ab45b
AM
4032 &xcoff_glink_code[0],
4033 36, /* _xcoff_glink_size */
9a4c7f16
TR
4034
4035 /* rtinit */
cf9ab45b
AM
4036 64, /* _xcoff_rtinit_size */
4037 xcoff_generate_rtinit,
4038 };
beb1bf64 4039
eb1e0e80 4040/* The transfer vector that leads the outside world to all of the above. */
beb1bf64 4041const bfd_target rs6000coff_vec =
cf9ab45b
AM
4042 {
4043 "aixcoff-rs6000",
4044 bfd_target_xcoff_flavour,
4045 BFD_ENDIAN_BIG, /* data byte order is big */
4046 BFD_ENDIAN_BIG, /* header byte order is big */
4047
4048 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4049 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4050
a7c71b0c 4051 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
cf9ab45b
AM
4052 0, /* leading char */
4053 '/', /* ar_pad_char */
4054 15, /* ar_max_namelen */
0aabe54e 4055 0, /* match priority. */
cf9ab45b
AM
4056
4057 /* data */
4058 bfd_getb64,
4059 bfd_getb_signed_64,
4060 bfd_putb64,
4061 bfd_getb32,
4062 bfd_getb_signed_32,
4063 bfd_putb32,
4064 bfd_getb16,
4065 bfd_getb_signed_16,
4066 bfd_putb16,
4067
4068 /* hdrs */
4069 bfd_getb64,
4070 bfd_getb_signed_64,
4071 bfd_putb64,
4072 bfd_getb32,
4073 bfd_getb_signed_32,
4074 bfd_putb32,
4075 bfd_getb16,
4076 bfd_getb_signed_16,
4077 bfd_putb16,
4078
4079 { /* bfd_check_format */
4080 _bfd_dummy_target,
4081 coff_object_p,
4082 _bfd_xcoff_archive_p,
4083 CORE_FILE_P
4084 },
dc810e39 4085
cf9ab45b
AM
4086 { /* bfd_set_format */
4087 bfd_false,
4088 coff_mkobject,
4089 _bfd_generic_mkarchive,
4090 bfd_false
4091 },
4092
4093 {/* bfd_write_contents */
4094 bfd_false,
4095 coff_write_object_contents,
4096 _bfd_xcoff_write_archive_contents,
4097 bfd_false
4098 },
4099
4100 /* Generic */
e3f9f6d8 4101 _bfd_archive_close_and_cleanup,
cf9ab45b
AM
4102 bfd_true,
4103 coff_new_section_hook,
4104 _bfd_generic_get_section_contents,
4105 _bfd_generic_get_section_contents_in_window,
4106
4107 /* Copy */
4108 _bfd_xcoff_copy_private_bfd_data,
ac96f0c7 4109 _bfd_generic_bfd_merge_private_bfd_data,
60b48850 4110 _bfd_generic_init_private_section_data,
ac96f0c7
TG
4111 _bfd_generic_bfd_copy_private_section_data,
4112 _bfd_generic_bfd_copy_private_symbol_data,
4113 _bfd_generic_bfd_copy_private_header_data,
4114 _bfd_generic_bfd_set_private_flags,
4115 _bfd_generic_bfd_print_private_bfd_data,
cf9ab45b
AM
4116
4117 /* Core */
261b8d08 4118 BFD_JUMP_TABLE_CORE (coff),
cf9ab45b
AM
4119
4120 /* Archive */
4121 _bfd_xcoff_slurp_armap,
ac96f0c7
TG
4122 _bfd_noarchive_slurp_extended_name_table,
4123 _bfd_noarchive_construct_extended_name_table,
cf9ab45b
AM
4124 bfd_dont_truncate_arname,
4125 _bfd_xcoff_write_armap,
4126 _bfd_xcoff_read_ar_hdr,
8f95b6e4 4127 _bfd_generic_write_ar_hdr,
cf9ab45b
AM
4128 _bfd_xcoff_openr_next_archived_file,
4129 _bfd_generic_get_elt_at_index,
4130 _bfd_xcoff_stat_arch_elt,
4131 bfd_true,
4132
4133 /* Symbols */
4134 coff_get_symtab_upper_bound,
6cee3f79 4135 coff_canonicalize_symtab,
cf9ab45b
AM
4136 coff_make_empty_symbol,
4137 coff_print_symbol,
4138 coff_get_symbol_info,
4139 _bfd_xcoff_is_local_label_name,
7db6994f 4140 coff_bfd_is_target_special_symbol,
cf9ab45b 4141 coff_get_lineno,
fc28f9aa 4142 xcoff_find_nearest_line,
a685c4e6 4143 xcoff_find_nearest_line_discriminator,
4575b1b5 4144 _bfd_generic_find_line,
4ab527b0 4145 coff_find_inliner_info,
cf9ab45b
AM
4146 coff_bfd_make_debug_symbol,
4147 _bfd_generic_read_minisymbols,
4148 _bfd_generic_minisymbol_to_symbol,
4149
4150 /* Reloc */
4151 coff_get_reloc_upper_bound,
4152 coff_canonicalize_reloc,
4153 _bfd_xcoff_reloc_type_lookup,
157090f7 4154 _bfd_xcoff_reloc_name_lookup,
cf9ab45b
AM
4155
4156 /* Write */
4157 coff_set_arch_mach,
4158 coff_set_section_contents,
4159
4160 /* Link */
4161 _bfd_xcoff_sizeof_headers,
4162 bfd_generic_get_relocated_section_contents,
4163 bfd_generic_relax_section,
4164 _bfd_xcoff_bfd_link_hash_table_create,
4165 _bfd_generic_link_hash_table_free,
4166 _bfd_xcoff_bfd_link_add_symbols,
4167 _bfd_generic_link_just_syms,
1338dd10 4168 _bfd_generic_copy_link_hash_symbol_type,
cf9ab45b
AM
4169 _bfd_xcoff_bfd_final_link,
4170 _bfd_generic_link_split_section,
4171 bfd_generic_gc_sections,
ae17ab41 4172 bfd_generic_lookup_section_flags,
cf9ab45b 4173 bfd_generic_merge_sections,
72adc230 4174 bfd_generic_is_group_section,
cf9ab45b 4175 bfd_generic_discard_group,
082b7297 4176 _bfd_generic_section_already_linked,
3023e3f6 4177 _bfd_xcoff_define_common_symbol,
cf9ab45b
AM
4178
4179 /* Dynamic */
4180 _bfd_xcoff_get_dynamic_symtab_upper_bound,
4181 _bfd_xcoff_canonicalize_dynamic_symtab,
4c45e5c9 4182 _bfd_nodynamic_get_synthetic_symtab,
cf9ab45b
AM
4183 _bfd_xcoff_get_dynamic_reloc_upper_bound,
4184 _bfd_xcoff_canonicalize_dynamic_reloc,
4185
4186 /* Opposite endian version, none exists */
4187 NULL,
4188
2c3fc389 4189 & bfd_xcoff_backend_data,
cf9ab45b 4190 };
beb1bf64 4191
cf9ab45b
AM
4192/* xcoff-powermac target
4193 Old target.
4194 Only difference between this target and the rs6000 target is the
4195 the default architecture and machine type used in coffcode.h
4196
4197 PowerPC Macs use the same magic numbers as RS/6000
4198 (because that's how they were bootstrapped originally),
4199 but they are always PowerPC architecture. */
dc810e39 4200static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
cf9ab45b
AM
4201 {
4202 { /* COFF backend, defined in libcoff.h. */
4203 _bfd_xcoff_swap_aux_in,
4204 _bfd_xcoff_swap_sym_in,
4205 coff_swap_lineno_in,
4206 _bfd_xcoff_swap_aux_out,
4207 _bfd_xcoff_swap_sym_out,
4208 coff_swap_lineno_out,
4209 xcoff_swap_reloc_out,
4210 coff_swap_filehdr_out,
4211 coff_swap_aouthdr_out,
4212 coff_swap_scnhdr_out,
4213 FILHSZ,
4214 AOUTSZ,
4215 SCNHSZ,
4216 SYMESZ,
4217 AUXESZ,
4218 RELSZ,
4219 LINESZ,
4220 FILNMLEN,
b34976b6 4221 TRUE, /* _bfd_coff_long_filenames */
88183869 4222 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
cf9ab45b 4223 3, /* _bfd_coff_default_section_alignment_power */
b34976b6 4224 FALSE, /* _bfd_coff_force_symnames_in_strings */
cf9ab45b
AM
4225 2, /* _bfd_coff_debug_string_prefix_length */
4226 coff_swap_filehdr_in,
4227 coff_swap_aouthdr_in,
4228 coff_swap_scnhdr_in,
4229 xcoff_swap_reloc_in,
4230 coff_bad_format_hook,
4231 coff_set_arch_mach_hook,
4232 coff_mkobject_hook,
4233 styp_to_sec_flags,
4234 coff_set_alignment_hook,
4235 coff_slurp_symbol_table,
4236 symname_in_debug_hook,
4237 coff_pointerize_aux_hook,
4238 coff_print_aux,
4239 dummy_reloc16_extra_cases,
4240 dummy_reloc16_estimate,
4241 NULL, /* bfd_coff_sym_is_global */
4242 coff_compute_section_file_positions,
4243 NULL, /* _bfd_coff_start_final_link */
4244 xcoff_ppc_relocate_section,
4245 coff_rtype_to_howto,
4246 NULL, /* _bfd_coff_adjust_symndx */
4247 _bfd_generic_link_add_one_symbol,
4248 coff_link_output_has_begun,
2b5c217d
NC
4249 coff_final_link_postscript,
4250 NULL /* print_pdata. */
cf9ab45b
AM
4251 },
4252
4253 0x01DF, /* magic number */
4254 bfd_arch_powerpc,
4255 bfd_mach_ppc,
4256
4257 /* Function pointers to xcoff specific swap routines. */
4258 xcoff_swap_ldhdr_in,
4259 xcoff_swap_ldhdr_out,
4260 xcoff_swap_ldsym_in,
4261 xcoff_swap_ldsym_out,
4262 xcoff_swap_ldrel_in,
4263 xcoff_swap_ldrel_out,
4264
4265 /* Sizes. */
4266 LDHDRSZ,
4267 LDSYMSZ,
4268 LDRELSZ,
4269 12, /* _xcoff_function_descriptor_size */
4270 SMALL_AOUTSZ,
4271
4272 /* Versions. */
4273 1, /* _xcoff_ldhdr_version */
4274
4275 _bfd_xcoff_put_symbol_name,
4276 _bfd_xcoff_put_ldsymbol_name,
4277 &xcoff_dynamic_reloc,
4278 xcoff_create_csect_from_smclas,
4279
4280 /* Lineno and reloc count overflow. */
4281 xcoff_is_lineno_count_overflow,
4282 xcoff_is_reloc_count_overflow,
4283
4284 xcoff_loader_symbol_offset,
4285 xcoff_loader_reloc_offset,
beb1bf64 4286
cf9ab45b
AM
4287 /* glink. */
4288 &xcoff_glink_code[0],
4289 36, /* _xcoff_glink_size */
4290
4291 /* rtinit */
4292 0, /* _xcoff_rtinit_size */
4293 xcoff_generate_rtinit,
4294 };
4295
4296/* The transfer vector that leads the outside world to all of the above. */
beb1bf64 4297const bfd_target pmac_xcoff_vec =
cf9ab45b
AM
4298 {
4299 "xcoff-powermac",
4300 bfd_target_xcoff_flavour,
4301 BFD_ENDIAN_BIG, /* data byte order is big */
4302 BFD_ENDIAN_BIG, /* header byte order is big */
4303
4304 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4305 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4306
a7c71b0c 4307 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
cf9ab45b
AM
4308 0, /* leading char */
4309 '/', /* ar_pad_char */
4310 15, /* ar_max_namelen */
0aabe54e 4311 0, /* match priority. */
cf9ab45b
AM
4312
4313 /* data */
4314 bfd_getb64,
4315 bfd_getb_signed_64,
4316 bfd_putb64,
4317 bfd_getb32,
4318 bfd_getb_signed_32,
4319 bfd_putb32,
4320 bfd_getb16,
4321 bfd_getb_signed_16,
4322 bfd_putb16,
4323
4324 /* hdrs */
4325 bfd_getb64,
4326 bfd_getb_signed_64,
4327 bfd_putb64,
4328 bfd_getb32,
4329 bfd_getb_signed_32,
4330 bfd_putb32,
4331 bfd_getb16,
4332 bfd_getb_signed_16,
4333 bfd_putb16,
4334
4335 { /* bfd_check_format */
4336 _bfd_dummy_target,
4337 coff_object_p,
4338 _bfd_xcoff_archive_p,
4339 CORE_FILE_P
4340 },
4341
4342 { /* bfd_set_format */
4343 bfd_false,
4344 coff_mkobject,
4345 _bfd_generic_mkarchive,
4346 bfd_false
4347 },
4348
4349 {/* bfd_write_contents */
4350 bfd_false,
4351 coff_write_object_contents,
4352 _bfd_xcoff_write_archive_contents,
4353 bfd_false
4354 },
dc810e39 4355
cf9ab45b 4356 /* Generic */
e3f9f6d8 4357 _bfd_archive_close_and_cleanup,
cf9ab45b
AM
4358 bfd_true,
4359 coff_new_section_hook,
4360 _bfd_generic_get_section_contents,
4361 _bfd_generic_get_section_contents_in_window,
4362
4363 /* Copy */
4364 _bfd_xcoff_copy_private_bfd_data,
ac96f0c7 4365 _bfd_generic_bfd_merge_private_bfd_data,
60b48850 4366 _bfd_generic_init_private_section_data,
ac96f0c7
TG
4367 _bfd_generic_bfd_copy_private_section_data,
4368 _bfd_generic_bfd_copy_private_symbol_data,
4369 _bfd_generic_bfd_copy_private_header_data,
4370 _bfd_generic_bfd_set_private_flags,
4371 _bfd_generic_bfd_print_private_bfd_data,
cf9ab45b
AM
4372
4373 /* Core */
261b8d08 4374 BFD_JUMP_TABLE_CORE (coff),
cf9ab45b
AM
4375
4376 /* Archive */
4377 _bfd_xcoff_slurp_armap,
ac96f0c7
TG
4378 _bfd_noarchive_slurp_extended_name_table,
4379 _bfd_noarchive_construct_extended_name_table,
cf9ab45b
AM
4380 bfd_dont_truncate_arname,
4381 _bfd_xcoff_write_armap,
4382 _bfd_xcoff_read_ar_hdr,
8f95b6e4 4383 _bfd_generic_write_ar_hdr,
cf9ab45b
AM
4384 _bfd_xcoff_openr_next_archived_file,
4385 _bfd_generic_get_elt_at_index,
4386 _bfd_xcoff_stat_arch_elt,
4387 bfd_true,
4388
4389 /* Symbols */
4390 coff_get_symtab_upper_bound,
6cee3f79 4391 coff_canonicalize_symtab,
cf9ab45b
AM
4392 coff_make_empty_symbol,
4393 coff_print_symbol,
4394 coff_get_symbol_info,
4395 _bfd_xcoff_is_local_label_name,
7db6994f 4396 coff_bfd_is_target_special_symbol,
cf9ab45b 4397 coff_get_lineno,
fc28f9aa 4398 xcoff_find_nearest_line,
9b8d1a36 4399 _bfd_generic_find_nearest_line_discriminator,
4575b1b5 4400 _bfd_generic_find_line,
4ab527b0 4401 coff_find_inliner_info,
cf9ab45b
AM
4402 coff_bfd_make_debug_symbol,
4403 _bfd_generic_read_minisymbols,
4404 _bfd_generic_minisymbol_to_symbol,
4405
4406 /* Reloc */
4407 coff_get_reloc_upper_bound,
4408 coff_canonicalize_reloc,
4409 _bfd_xcoff_reloc_type_lookup,
157090f7 4410 _bfd_xcoff_reloc_name_lookup,
cf9ab45b
AM
4411
4412 /* Write */
4413 coff_set_arch_mach,
4414 coff_set_section_contents,
4415
4416 /* Link */
4417 _bfd_xcoff_sizeof_headers,
4418 bfd_generic_get_relocated_section_contents,
4419 bfd_generic_relax_section,
4420 _bfd_xcoff_bfd_link_hash_table_create,
4421 _bfd_generic_link_hash_table_free,
4422 _bfd_xcoff_bfd_link_add_symbols,
4423 _bfd_generic_link_just_syms,
1338dd10 4424 _bfd_generic_copy_link_hash_symbol_type,
cf9ab45b
AM
4425 _bfd_xcoff_bfd_final_link,
4426 _bfd_generic_link_split_section,
4427 bfd_generic_gc_sections,
ae17ab41 4428 bfd_generic_lookup_section_flags,
cf9ab45b 4429 bfd_generic_merge_sections,
72adc230 4430 bfd_generic_is_group_section,
cf9ab45b 4431 bfd_generic_discard_group,
082b7297 4432 _bfd_generic_section_already_linked,
3023e3f6 4433 _bfd_xcoff_define_common_symbol,
cf9ab45b
AM
4434
4435 /* Dynamic */
4436 _bfd_xcoff_get_dynamic_symtab_upper_bound,
4437 _bfd_xcoff_canonicalize_dynamic_symtab,
4c45e5c9 4438 _bfd_nodynamic_get_synthetic_symtab,
cf9ab45b
AM
4439 _bfd_xcoff_get_dynamic_reloc_upper_bound,
4440 _bfd_xcoff_canonicalize_dynamic_reloc,
4441
4442 /* Opposite endian version, none exists */
4443 NULL,
4444
2c3fc389 4445 & bfd_pmac_xcoff_backend_data,
cf9ab45b 4446 };
This page took 1.144988 seconds and 4 git commands to generate.