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