1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 FIXME: Can someone provide a transliteration of this name into ASCII?
5 Using the following chars caused a compiler warning on HIUX (so I replaced
6 them with octal escapes), and isn't useful without an understanding of what
8 Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365,
10 Archive support from Damon A. Permezel.
11 Contributed by IBM Corporation and Cygnus Support.
13 This file is part of BFD, the Binary File Descriptor library.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include "coff/internal.h"
33 #include "coff/rs6000.h"
35 #define TARGET_NAME "aixcoff-rs6000"
36 #define TARGET_SYM rs6000coff_vec
37 #include "xcoff-target.h"
39 /* The main body of code is in coffcode.h. */
41 static const char *normalize_filename PARAMS ((bfd *));
43 /* We use our own tdata type. Its first field is the COFF tdata type,
44 so the COFF routines are compatible. */
47 _bfd_xcoff_mkobject (abfd)
52 abfd->tdata.xcoff_obj_data =
53 ((struct xcoff_tdata *)
54 bfd_zalloc (abfd, sizeof (struct xcoff_tdata)));
55 if (abfd->tdata.xcoff_obj_data == NULL)
57 coff = coff_data (abfd);
58 coff->symbols = (coff_symbol_type *) NULL;
59 coff->conversion_table = (unsigned int *) NULL;
60 coff->raw_syments = (struct coff_ptr_struct *) NULL;
63 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
65 /* We set cputype to -1 to indicate that it has not been
67 xcoff_data (abfd)->cputype = -1;
69 xcoff_data (abfd)->csects = NULL;
70 xcoff_data (abfd)->debug_indices = NULL;
75 /* Copy XCOFF data from one BFD to another. */
78 _bfd_xcoff_copy_private_bfd_data (ibfd, obfd)
82 struct xcoff_tdata *ix, *ox;
85 if (ibfd->xvec != obfd->xvec)
87 ix = xcoff_data (ibfd);
88 ox = xcoff_data (obfd);
89 ox->full_aouthdr = ix->full_aouthdr;
95 sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
99 ox->sntoc = sec->output_section->target_index;
101 if (ix->snentry == 0)
105 sec = coff_section_from_bfd_index (ibfd, ix->snentry);
109 ox->snentry = sec->output_section->target_index;
111 ox->text_align_power = ix->text_align_power;
112 ox->data_align_power = ix->data_align_power;
113 ox->modtype = ix->modtype;
114 ox->cputype = ix->cputype;
115 ox->maxdata = ix->maxdata;
116 ox->maxstack = ix->maxstack;
120 /* I don't think XCOFF really has a notion of local labels based on
121 name. This will mean that ld -X doesn't actually strip anything.
122 The AIX native linker does not have a -X option, and it ignores the
126 _bfd_xcoff_is_local_label_name (abfd, name)
127 bfd *abfd ATTRIBUTE_UNUSED;
128 const char *name ATTRIBUTE_UNUSED;
135 _bfd_xcoff_swap_sym_in (abfd, ext1, in1)
140 SYMENT *ext = (SYMENT *)ext1;
141 struct internal_syment *in = (struct internal_syment *)in1;
143 if(ext->e.e_name[0] != 0)
145 memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
149 in->_n._n_n._n_zeroes = 0;
150 in->_n._n_n._n_offset =
151 bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
154 in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value);
155 in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
156 in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
157 in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
158 in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
162 _bfd_xcoff_swap_sym_out (abfd, inp, extp)
167 struct internal_syment *in = (struct internal_syment *)inp;
168 SYMENT *ext =(SYMENT *)extp;
170 if(in->_n._n_name[0] != 0)
172 memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
176 bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
177 bfd_h_put_32(abfd, in->_n._n_n._n_offset,
178 (bfd_byte *) ext->e.e.e_offset);
181 bfd_h_put_32(abfd, in->n_value , (bfd_byte *) ext->e_value);
182 bfd_h_put_16(abfd, in->n_scnum , (bfd_byte *) ext->e_scnum);
183 bfd_h_put_16(abfd, in->n_type , (bfd_byte *) ext->e_type);
184 bfd_h_put_8(abfd, in->n_sclass , ext->e_sclass);
185 bfd_h_put_8(abfd, in->n_numaux , ext->e_numaux);
186 return bfd_coff_symesz (abfd);
189 #define PUTWORD bfd_h_put_32
190 #define PUTHALF bfd_h_put_16
191 #define PUTBYTE bfd_h_put_8
192 #define GETWORD bfd_h_get_32
193 #define GETHALF bfd_h_get_16
194 #define GETBYTE bfd_h_get_8
197 _bfd_xcoff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
206 AUXENT *ext = (AUXENT *)ext1;
207 union internal_auxent *in = (union internal_auxent *)in1;
211 if (ext->x_file.x_fname[0] == 0) {
212 in->x_file.x_n.x_zeroes = 0;
213 in->x_file.x_n.x_offset =
214 bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
219 memcpy (in->x_file.x_fname, ext->x_file.x_fname,
220 numaux * sizeof (AUXENT));
224 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
229 /* RS/6000 "csect" auxents */
232 if (indx + 1 == numaux)
234 in->x_csect.x_scnlen.l =
235 bfd_h_get_32 (abfd, ext->x_csect.x_scnlen);
236 in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
237 ext->x_csect.x_parmhash);
238 in->x_csect.x_snhash = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
239 /* We don't have to hack bitfields in x_smtyp because it's
240 defined by shifts-and-ands, which are equivalent on all
242 in->x_csect.x_smtyp = bfd_h_get_8 (abfd, ext->x_csect.x_smtyp);
243 in->x_csect.x_smclas = bfd_h_get_8 (abfd, ext->x_csect.x_smclas);
244 in->x_csect.x_stab = bfd_h_get_32 (abfd, ext->x_csect.x_stab);
245 in->x_csect.x_snstab = bfd_h_get_16 (abfd, ext->x_csect.x_snstab);
253 if (type == T_NULL) {
254 in->x_scn.x_scnlen = bfd_h_get_32(abfd,
255 (bfd_byte *) ext->x_scn.x_scnlen);
256 in->x_scn.x_nreloc = bfd_h_get_16(abfd,
257 (bfd_byte *) ext->x_scn.x_nreloc);
258 in->x_scn.x_nlinno = bfd_h_get_16(abfd,
259 (bfd_byte *) ext->x_scn.x_nlinno);
260 /* PE defines some extra fields; we zero them out for
262 in->x_scn.x_checksum = 0;
263 in->x_scn.x_associated = 0;
264 in->x_scn.x_comdat = 0;
271 in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
272 in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
274 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
276 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = bfd_h_get_32(abfd, (bfd_byte *)
277 ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
278 in->x_sym.x_fcnary.x_fcn.x_endndx.l = bfd_h_get_32(abfd, (bfd_byte *)
279 ext->x_sym.x_fcnary.x_fcn.x_endndx);
283 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
284 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
285 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
286 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
287 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
288 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
289 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
290 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
293 in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
296 in->x_sym.x_misc.x_lnsz.x_lnno = bfd_h_get_16(abfd, (bfd_byte *)
297 ext->x_sym.x_misc.x_lnsz.x_lnno);
298 in->x_sym.x_misc.x_lnsz.x_size = bfd_h_get_16(abfd, (bfd_byte *)
299 ext->x_sym.x_misc.x_lnsz.x_size);
303 /* the semicolon is because MSVC doesn't like labels at
309 _bfd_xcoff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
314 int indx ATTRIBUTE_UNUSED;
315 int numaux ATTRIBUTE_UNUSED;
318 union internal_auxent *in = (union internal_auxent *)inp;
319 AUXENT *ext = (AUXENT *)extp;
321 memset((PTR)ext, 0, bfd_coff_auxesz (abfd));
325 if (in->x_file.x_fname[0] == 0)
327 PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
329 in->x_file.x_n.x_offset,
330 (bfd_byte *) ext->x_file.x_n.x_offset);
334 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
338 /* RS/6000 "csect" auxents */
341 if (indx + 1 == numaux)
343 PUTWORD (abfd, in->x_csect.x_scnlen.l,ext->x_csect.x_scnlen);
344 PUTWORD (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
345 PUTHALF (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
346 /* We don't have to hack bitfields in x_smtyp because it's
347 defined by shifts-and-ands, which are equivalent on all
349 PUTBYTE (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
350 PUTBYTE (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
351 PUTWORD (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
352 PUTHALF (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
360 if (type == T_NULL) {
361 bfd_h_put_32(abfd, in->x_scn.x_scnlen, (bfd_byte *) ext->x_scn.x_scnlen);
362 bfd_h_put_16(abfd, in->x_scn.x_nreloc, (bfd_byte *) ext->x_scn.x_nreloc);
363 bfd_h_put_16(abfd, in->x_scn.x_nlinno, (bfd_byte *) ext->x_scn.x_nlinno);
369 PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
370 bfd_h_put_16 (abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
372 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
374 bfd_h_put_32(abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
375 (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
376 PUTWORD(abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
377 (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx);
381 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
382 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
383 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
384 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
385 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
386 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
387 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
388 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
392 PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
393 (bfd_byte *) ext->x_sym.x_misc.x_fsize);
396 bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
397 (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno);
398 bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_size,
399 (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_size);
403 return bfd_coff_auxesz (abfd);
407 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
408 bitsize and whether they are signed or not, along with a
409 conventional type. This table is for the types, which are used for
410 different algorithms for putting in the reloc. Many of these
411 relocs need special_function entries, which I have not written. */
413 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
414 from smaller values. Start with zero, widen, *then* decrement. */
415 #define MINUS_ONE (((bfd_vma)0) - 1)
417 reloc_howto_type xcoff_howto_table[] =
419 /* Standard 32 bit relocation. */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
424 false, /* pc_relative */
426 complain_overflow_bitfield, /* complain_on_overflow */
427 0, /* special_function */
429 true, /* partial_inplace */
430 0xffffffff, /* src_mask */
431 0xffffffff, /* dst_mask */
432 false), /* pcrel_offset */
434 /* 32 bit relocation, but store negative value. */
437 -2, /* size (0 = byte, 1 = short, 2 = long) */
439 false, /* pc_relative */
441 complain_overflow_bitfield, /* complain_on_overflow */
442 0, /* special_function */
444 true, /* partial_inplace */
445 0xffffffff, /* src_mask */
446 0xffffffff, /* dst_mask */
447 false), /* pcrel_offset */
449 /* 32 bit PC relative relocation. */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
454 true, /* pc_relative */
456 complain_overflow_signed, /* complain_on_overflow */
457 0, /* special_function */
459 true, /* partial_inplace */
460 0xffffffff, /* src_mask */
461 0xffffffff, /* dst_mask */
462 false), /* pcrel_offset */
464 /* 16 bit TOC relative relocation. */
467 1, /* size (0 = byte, 1 = short, 2 = long) */
469 false, /* pc_relative */
471 complain_overflow_bitfield, /* complain_on_overflow */
472 0, /* special_function */
474 true, /* partial_inplace */
475 0xffff, /* src_mask */
476 0xffff, /* dst_mask */
477 false), /* pcrel_offset */
479 /* I don't really know what this is. */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
484 false, /* pc_relative */
486 complain_overflow_bitfield, /* complain_on_overflow */
487 0, /* special_function */
489 true, /* partial_inplace */
490 0xffffffff, /* src_mask */
491 0xffffffff, /* dst_mask */
492 false), /* pcrel_offset */
494 /* External TOC relative symbol. */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
499 false, /* pc_relative */
501 complain_overflow_bitfield, /* complain_on_overflow */
502 0, /* special_function */
504 true, /* partial_inplace */
505 0xffff, /* src_mask */
506 0xffff, /* dst_mask */
507 false), /* pcrel_offset */
509 /* Local TOC relative symbol. */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
514 false, /* pc_relative */
516 complain_overflow_bitfield, /* complain_on_overflow */
517 0, /* special_function */
519 true, /* partial_inplace */
520 0xffff, /* src_mask */
521 0xffff, /* dst_mask */
522 false), /* pcrel_offset */
526 /* Non modifiable absolute branch. */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
531 false, /* pc_relative */
533 complain_overflow_bitfield, /* complain_on_overflow */
534 0, /* special_function */
536 true, /* partial_inplace */
537 0x3fffffc, /* src_mask */
538 0x3fffffc, /* dst_mask */
539 false), /* pcrel_offset */
543 /* Non modifiable relative branch. */
544 HOWTO (0xa, /* type */
546 2, /* size (0 = byte, 1 = short, 2 = long) */
548 true, /* pc_relative */
550 complain_overflow_signed, /* complain_on_overflow */
551 0, /* special_function */
553 true, /* partial_inplace */
554 0x3fffffc, /* src_mask */
555 0x3fffffc, /* dst_mask */
556 false), /* pcrel_offset */
561 HOWTO (0xc, /* type */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
565 false, /* pc_relative */
567 complain_overflow_bitfield, /* complain_on_overflow */
568 0, /* special_function */
570 true, /* partial_inplace */
571 0xffff, /* src_mask */
572 0xffff, /* dst_mask */
573 false), /* pcrel_offset */
576 HOWTO (0xd, /* type */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
580 false, /* pc_relative */
582 complain_overflow_bitfield, /* complain_on_overflow */
583 0, /* special_function */
585 true, /* partial_inplace */
586 0xffff, /* src_mask */
587 0xffff, /* dst_mask */
588 false), /* pcrel_offset */
592 /* Non-relocating reference. */
593 HOWTO (0xf, /* type */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
597 false, /* pc_relative */
599 complain_overflow_bitfield, /* complain_on_overflow */
600 0, /* special_function */
602 false, /* partial_inplace */
605 false), /* pcrel_offset */
610 /* TOC relative indirect load. */
611 HOWTO (0x12, /* type */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
615 false, /* pc_relative */
617 complain_overflow_bitfield, /* complain_on_overflow */
618 0, /* special_function */
620 true, /* partial_inplace */
621 0xffff, /* src_mask */
622 0xffff, /* dst_mask */
623 false), /* pcrel_offset */
625 /* TOC relative load address. */
626 HOWTO (0x13, /* type */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
630 false, /* pc_relative */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 0, /* special_function */
635 true, /* partial_inplace */
636 0xffff, /* src_mask */
637 0xffff, /* dst_mask */
638 false), /* pcrel_offset */
640 /* Modifiable relative branch. */
641 HOWTO (0x14, /* type */
643 2, /* size (0 = byte, 1 = short, 2 = long) */
645 false, /* pc_relative */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 0, /* special_function */
649 "R_RRTBI", /* name */
650 true, /* partial_inplace */
651 0xffffffff, /* src_mask */
652 0xffffffff, /* dst_mask */
653 false), /* pcrel_offset */
655 /* Modifiable absolute branch. */
656 HOWTO (0x15, /* type */
658 2, /* size (0 = byte, 1 = short, 2 = long) */
660 false, /* pc_relative */
662 complain_overflow_bitfield, /* complain_on_overflow */
663 0, /* special_function */
664 "R_RRTBA", /* name */
665 true, /* partial_inplace */
666 0xffffffff, /* src_mask */
667 0xffffffff, /* dst_mask */
668 false), /* pcrel_offset */
670 /* Modifiable call absolute indirect. */
671 HOWTO (0x16, /* type */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
675 false, /* pc_relative */
677 complain_overflow_bitfield, /* complain_on_overflow */
678 0, /* special_function */
680 true, /* partial_inplace */
681 0xffff, /* src_mask */
682 0xffff, /* dst_mask */
683 false), /* pcrel_offset */
685 /* Modifiable call relative. */
686 HOWTO (0x17, /* type */
688 2, /* size (0 = byte, 1 = short, 2 = long) */
690 false, /* pc_relative */
692 complain_overflow_bitfield, /* complain_on_overflow */
693 0, /* special_function */
695 true, /* partial_inplace */
696 0xffff, /* src_mask */
697 0xffff, /* dst_mask */
698 false), /* pcrel_offset */
700 /* Modifiable branch absolute. */
701 HOWTO (0x18, /* type */
703 2, /* size (0 = byte, 1 = short, 2 = long) */
705 false, /* pc_relative */
707 complain_overflow_bitfield, /* complain_on_overflow */
708 0, /* special_function */
710 true, /* partial_inplace */
711 0xffff, /* src_mask */
712 0xffff, /* dst_mask */
713 false), /* pcrel_offset */
715 /* Modifiable branch absolute. */
716 HOWTO (0x19, /* type */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
720 false, /* pc_relative */
722 complain_overflow_bitfield, /* complain_on_overflow */
723 0, /* special_function */
725 true, /* partial_inplace */
726 0xffff, /* src_mask */
727 0xffff, /* dst_mask */
728 false), /* pcrel_offset */
730 /* Modifiable branch relative. */
731 HOWTO (0x1a, /* type */
733 2, /* size (0 = byte, 1 = short, 2 = long) */
735 false, /* pc_relative */
737 complain_overflow_signed, /* complain_on_overflow */
738 0, /* special_function */
740 true, /* partial_inplace */
741 0xffff, /* src_mask */
742 0xffff, /* dst_mask */
743 false), /* pcrel_offset */
745 /* Modifiable branch absolute. */
746 HOWTO (0x1b, /* type */
748 2, /* size (0 = byte, 1 = short, 2 = long) */
750 false, /* pc_relative */
752 complain_overflow_bitfield, /* complain_on_overflow */
753 0, /* special_function */
755 true, /* partial_inplace */
756 0xffff, /* src_mask */
757 0xffff, /* dst_mask */
758 false), /* pcrel_offset */
761 4, /* size (0 = byte, 1 = short, 2 = long) */
763 false, /* pc_relative */
765 complain_overflow_bitfield, /* complain_on_overflow */
766 0, /* special_function */
768 true, /* partial_inplace */
769 MINUS_ONE, /* src_mask */
770 MINUS_ONE, /* dst_mask */
771 false) /* pcrel_offset */
775 /* These are the first two like the above but for 16-bit relocs. */
776 static reloc_howto_type xcoff_howto_table_16[] =
778 /* Standard 16 bit relocation. */
781 2, /* size (0 = byte, 1 = short, 2 = long) */
783 false, /* pc_relative */
785 complain_overflow_bitfield, /* complain_on_overflow */
786 0, /* special_function */
787 "R_POS_16", /* name */
788 true, /* partial_inplace */
789 0xffffffff, /* src_mask */
790 0xffffffff, /* dst_mask */
791 false), /* pcrel_offset */
793 /* 16 bit relocation, but store negative value. */
796 -2, /* size (0 = byte, 1 = short, 2 = long) */
798 false, /* pc_relative */
800 complain_overflow_bitfield, /* complain_on_overflow */
801 0, /* special_function */
802 "R_NEG_16", /* name */
803 true, /* partial_inplace */
804 0xffffffff, /* src_mask */
805 0xffffffff, /* dst_mask */
806 false), /* pcrel_offset */
808 /* 16 bit PC relative relocation. */
811 2, /* size (0 = byte, 1 = short, 2 = long) */
813 true, /* pc_relative */
815 complain_overflow_signed, /* complain_on_overflow */
816 0, /* special_function */
817 "R_REL_16", /* name */
818 true, /* partial_inplace */
819 0xffffffff, /* src_mask */
820 0xffffffff, /* dst_mask */
821 false) /* pcrel_offset */
825 _bfd_xcoff_rtype2howto (relent, internal)
827 struct internal_reloc *internal;
829 relent->howto = xcoff_howto_table + internal->r_type;
831 if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1
833 < sizeof (xcoff_howto_table_16)/sizeof (xcoff_howto_table_16[0])))
834 relent->howto = xcoff_howto_table_16 + internal->r_type;
836 /* The r_size field of an XCOFF reloc encodes the bitsize of the
837 relocation, as well as indicating whether it is signed or not.
838 Doublecheck that the relocation information gathered from the
839 type matches this information. The bitsize is not significant
841 if (relent->howto->dst_mask != 0
842 && (relent->howto->bitsize
843 != ((unsigned int) internal->r_size & 0x3f) + 1))
846 if ((internal->r_size & 0x80) != 0
847 ? (relent->howto->complain_on_overflow != complain_overflow_signed)
848 : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
854 _bfd_xcoff_reloc_type_lookup (abfd, code)
855 bfd *abfd ATTRIBUTE_UNUSED;
856 bfd_reloc_code_real_type code;
860 case BFD_RELOC_PPC_B26:
861 return &xcoff_howto_table[0xa];
862 case BFD_RELOC_PPC_BA26:
863 return &xcoff_howto_table[8];
864 case BFD_RELOC_PPC_TOC16:
865 return &xcoff_howto_table[3];
868 return &xcoff_howto_table[0];
870 return &xcoff_howto_table[0x1c];
877 /* XCOFF archive support. The original version of this code was by
878 Damon A. Permezel. It was enhanced to permit cross support, and
879 writing archive files, by Ian Lance Taylor, Cygnus Support.
881 XCOFF uses its own archive format. Everything is hooked together
882 with file offset links, so it is possible to rapidly update an
883 archive in place. Of course, we don't do that. An XCOFF archive
884 has a real file header, not just an ARMAG string. The structure of
885 the file header and of each archive header appear below.
887 An XCOFF archive also has a member table, which is a list of
888 elements in the archive (you can get that by looking through the
889 linked list, but you have to read a lot more of the file). The
890 member table has a normal archive header with an empty name. It is
891 normally (and perhaps must be) the second to last entry in the
892 archive. The member table data is almost printable ASCII. It
893 starts with a 12 character decimal string which is the number of
894 entries in the table. For each entry it has a 12 character decimal
895 string which is the offset in the archive of that member. These
896 entries are followed by a series of null terminated strings which
897 are the member names for each entry.
899 Finally, an XCOFF archive has a global symbol table, which is what
900 we call the armap. The global symbol table has a normal archive
901 header with an empty name. It is normally (and perhaps must be)
902 the last entry in the archive. The contents start with a four byte
903 binary number which is the number of entries. This is followed by
904 a that many four byte binary numbers; each is the file offset of an
905 entry in the archive. These numbers are followed by a series of
906 null terminated strings, which are symbol names.
908 AIX 4.3 introduced a new archive format which can handle larger
909 files and also 32- and 64-bit objects in the same archive. The
910 things said above remain true except that there is now more than
911 one global symbol table. The one is used to index 32-bit objects,
912 the other for 64-bit objects.
914 The new archives (recognizable by the new ARMAG string) has larger
915 field lengths so that we cannot really share any code. Also we have
916 to take care that we are not generating the new form of archives
917 on AIX 4.2 or earlier systems. */
919 /* XCOFF archives use this as a magic string. Note that both strings
920 have the same length. */
922 #define XCOFFARMAG "<aiaff>\012"
923 #define XCOFFARMAGBIG "<bigaf>\012"
924 #define SXCOFFARMAG 8
926 /* This terminates an XCOFF archive member name. */
928 #define XCOFFARFMAG "`\012"
929 #define SXCOFFARFMAG 2
931 /* XCOFF archives start with this (printable) structure. */
933 struct xcoff_ar_file_hdr
936 char magic[SXCOFFARMAG];
938 /* Offset of the member table (decimal ASCII string). */
941 /* Offset of the global symbol table (decimal ASCII string). */
944 /* Offset of the first member in the archive (decimal ASCII string). */
945 char firstmemoff[12];
947 /* Offset of the last member in the archive (decimal ASCII string). */
950 /* Offset of the first member on the free list (decimal ASCII
955 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
957 /* This is the equivalent data structure for the big archive format. */
959 struct xcoff_ar_file_hdr_big
962 char magic[SXCOFFARMAG];
964 /* Offset of the member table (decimal ASCII string). */
967 /* Offset of the global symbol table for 32-bit objects (decimal ASCII
971 /* Offset of the global symbol table for 64-bit objects (decimal ASCII
975 /* Offset of the first member in the archive (decimal ASCII string). */
976 char firstmemoff[20];
978 /* Offset of the last member in the archive (decimal ASCII string). */
981 /* Offset of the first member on the free list (decimal ASCII
986 #define SIZEOF_AR_FILE_HDR_BIG (6 * 20 + SXCOFFARMAG)
988 /* Each XCOFF archive member starts with this (printable) structure. */
992 /* File size not including the header (decimal ASCII string). */
995 /* File offset of next archive member (decimal ASCII string). */
998 /* File offset of previous archive member (decimal ASCII string). */
1001 /* File mtime (decimal ASCII string). */
1004 /* File UID (decimal ASCII string). */
1007 /* File GID (decimal ASCII string). */
1010 /* File mode (octal ASCII string). */
1013 /* Length of file name (decimal ASCII string). */
1016 /* This structure is followed by the file name. The length of the
1017 name is given in the namlen field. If the length of the name is
1018 odd, the name is followed by a null byte. The name and optional
1019 null byte are followed by XCOFFARFMAG, which is not included in
1020 namlen. The contents of the archive member follow; the number of
1021 bytes is given in the size field. */
1024 #define SIZEOF_AR_HDR (7 * 12 + 4)
1026 /* The equivalent for the big archive format. */
1028 struct xcoff_ar_hdr_big
1030 /* File size not including the header (decimal ASCII string). */
1033 /* File offset of next archive member (decimal ASCII string). */
1036 /* File offset of previous archive member (decimal ASCII string). */
1039 /* File mtime (decimal ASCII string). */
1042 /* File UID (decimal ASCII string). */
1045 /* File GID (decimal ASCII string). */
1048 /* File mode (octal ASCII string). */
1051 /* Length of file name (decimal ASCII string). */
1054 /* This structure is followed by the file name. The length of the
1055 name is given in the namlen field. If the length of the name is
1056 odd, the name is followed by a null byte. The name and optional
1057 null byte are followed by XCOFFARFMAG, which is not included in
1058 namlen. The contents of the archive member follow; the number of
1059 bytes is given in the size field. */
1062 #define SIZEOF_AR_HDR_BIG (3 * 20 + 4 * 12 + 4)
1064 /* We often have to distinguish between the old and big file format.
1065 Make it a bit cleaner. We can use `xcoff_ardata' here because the
1066 `hdr' member has the same size and position in both formats. */
1067 #define xcoff_big_format_p(abfd) \
1068 (xcoff_ardata (abfd)->magic[1] == 'b')
1070 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
1071 artdata structure. Similar for the big archive. */
1072 #define xcoff_ardata(abfd) \
1073 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
1074 #define xcoff_ardata_big(abfd) \
1075 ((struct xcoff_ar_file_hdr_big *) bfd_ardata (abfd)->tdata)
1077 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
1078 archive element. Similar for the big archive. */
1079 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
1080 #define arch_xhdr(bfd) \
1081 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
1082 #define arch_xhdr_big(bfd) \
1083 ((struct xcoff_ar_hdr_big *) arch_eltdata (bfd)->arch_header)
1085 /* Read in the armap of an XCOFF archive. */
1088 _bfd_xcoff_slurp_armap (abfd)
1094 bfd_byte *contents, *cend;
1099 if (xcoff_ardata (abfd) == NULL)
1101 bfd_has_map (abfd) = false;
1105 if (! xcoff_big_format_p (abfd))
1107 /* This is for the old format. */
1108 struct xcoff_ar_hdr hdr;
1110 off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
1113 bfd_has_map (abfd) = false;
1117 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1120 /* The symbol table starts with a normal archive header. */
1121 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1124 /* Skip the name (normally empty). */
1125 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1126 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1129 sz = strtol (hdr.size, (char **) NULL, 10);
1131 /* Read in the entire symbol table. */
1132 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1133 if (contents == NULL)
1135 if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1138 /* The symbol table starts with a four byte count. */
1139 c = bfd_h_get_32 (abfd, contents);
1143 bfd_set_error (bfd_error_bad_value);
1147 bfd_ardata (abfd)->symdefs = ((carsym *)
1148 bfd_alloc (abfd, c * sizeof (carsym)));
1149 if (bfd_ardata (abfd)->symdefs == NULL)
1152 /* After the count comes a list of four byte file offsets. */
1153 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1155 ++i, ++arsym, p += 4)
1156 arsym->file_offset = bfd_h_get_32 (abfd, p);
1160 /* This is for the new format. */
1161 struct xcoff_ar_hdr_big hdr;
1163 off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1166 bfd_has_map (abfd) = false;
1170 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1173 /* The symbol table starts with a normal archive header. */
1174 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1175 != SIZEOF_AR_HDR_BIG)
1178 /* Skip the name (normally empty). */
1179 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1180 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1183 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1184 machines) since the field width is 20 and there numbers with more
1185 than 32 bits can be represented. */
1186 sz = strtol (hdr.size, (char **) NULL, 10);
1188 /* Read in the entire symbol table. */
1189 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1190 if (contents == NULL)
1192 if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1195 /* The symbol table starts with an eight byte count. */
1196 c = bfd_h_get_64 (abfd, contents);
1200 bfd_set_error (bfd_error_bad_value);
1204 bfd_ardata (abfd)->symdefs = ((carsym *)
1205 bfd_alloc (abfd, c * sizeof (carsym)));
1206 if (bfd_ardata (abfd)->symdefs == NULL)
1209 /* After the count comes a list of eight byte file offsets. */
1210 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1212 ++i, ++arsym, p += 8)
1213 arsym->file_offset = bfd_h_get_64 (abfd, p);
1216 /* After the file offsets come null terminated symbol names. */
1217 cend = contents + sz;
1218 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1220 ++i, ++arsym, p += strlen ((char *) p) + 1)
1224 bfd_set_error (bfd_error_bad_value);
1227 arsym->name = (char *) p;
1230 bfd_ardata (abfd)->symdef_count = c;
1231 bfd_has_map (abfd) = true;
1236 /* See if this is an XCOFF archive. */
1239 _bfd_xcoff_archive_p (abfd)
1242 char magic[SXCOFFARMAG];
1244 if (bfd_read ((PTR) magic, SXCOFFARMAG, 1, abfd) != SXCOFFARMAG)
1246 if (bfd_get_error () != bfd_error_system_call)
1247 bfd_set_error (bfd_error_wrong_format);
1251 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1252 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1254 bfd_set_error (bfd_error_wrong_format);
1258 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
1259 involves a cast, we can't do it as the left operand of
1261 abfd->tdata.aout_ar_data =
1262 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
1264 if (bfd_ardata (abfd) == (struct artdata *) NULL)
1267 bfd_ardata (abfd)->cache = NULL;
1268 bfd_ardata (abfd)->archive_head = NULL;
1269 bfd_ardata (abfd)->symdefs = NULL;
1270 bfd_ardata (abfd)->extended_names = NULL;
1272 /* Now handle the two formats. */
1273 if (magic[1] != 'b')
1275 /* This is the old format. */
1276 struct xcoff_ar_file_hdr hdr;
1278 /* Copy over the magic string. */
1279 memcpy (hdr.magic, magic, SXCOFFARMAG);
1281 /* Now read the rest of the file header. */
1282 if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR - SXCOFFARMAG, 1,
1283 abfd) != SIZEOF_AR_FILE_HDR - SXCOFFARMAG)
1285 if (bfd_get_error () != bfd_error_system_call)
1286 bfd_set_error (bfd_error_wrong_format);
1290 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1291 (char **) NULL, 10);
1293 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
1294 if (bfd_ardata (abfd)->tdata == NULL)
1297 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1301 /* This is the new format. */
1302 struct xcoff_ar_file_hdr_big hdr;
1304 /* Copy over the magic string. */
1305 memcpy (hdr.magic, magic, SXCOFFARMAG);
1307 /* Now read the rest of the file header. */
1308 if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG, 1,
1309 abfd) != SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG)
1311 if (bfd_get_error () != bfd_error_system_call)
1312 bfd_set_error (bfd_error_wrong_format);
1316 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1317 machines) since the field width is 20 and there numbers with more
1318 than 32 bits can be represented. */
1319 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1320 (char **) NULL, 10);
1322 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR_BIG);
1323 if (bfd_ardata (abfd)->tdata == NULL)
1326 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1329 if (! _bfd_xcoff_slurp_armap (abfd))
1331 bfd_release (abfd, bfd_ardata (abfd));
1332 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
1339 /* Read the archive header in an XCOFF archive. */
1342 _bfd_xcoff_read_ar_hdr (abfd)
1346 struct areltdata *ret;
1348 ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
1352 if (! xcoff_big_format_p (abfd))
1354 struct xcoff_ar_hdr hdr;
1355 struct xcoff_ar_hdr *hdrp;
1357 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1363 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1364 hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd,
1365 SIZEOF_AR_HDR + namlen + 1);
1371 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1372 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
1377 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1379 ret->arch_header = (char *) hdrp;
1380 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1381 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1385 struct xcoff_ar_hdr_big hdr;
1386 struct xcoff_ar_hdr_big *hdrp;
1388 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1389 != SIZEOF_AR_HDR_BIG)
1395 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1396 hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd,
1404 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1405 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, 1, namlen, abfd) != namlen)
1410 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1412 ret->arch_header = (char *) hdrp;
1413 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1414 machines) since the field width is 20 and there numbers with more
1415 than 32 bits can be represented. */
1416 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1417 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1420 /* Skip over the XCOFFARFMAG at the end of the file name. */
1421 if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1427 /* Open the next element in an XCOFF archive. */
1430 _bfd_xcoff_openr_next_archived_file (archive, last_file)
1436 if (xcoff_ardata (archive) == NULL)
1438 bfd_set_error (bfd_error_invalid_operation);
1442 if (! xcoff_big_format_p (archive))
1444 if (last_file == NULL)
1445 filestart = bfd_ardata (archive)->first_file_filepos;
1447 filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1451 || filestart == strtol (xcoff_ardata (archive)->memoff,
1453 || filestart == strtol (xcoff_ardata (archive)->symoff,
1454 (char **) NULL, 10))
1456 bfd_set_error (bfd_error_no_more_archived_files);
1462 if (last_file == NULL)
1463 filestart = bfd_ardata (archive)->first_file_filepos;
1465 /* XXX These actually have to be a calls to strtoll (at least
1466 on 32-bit machines) since the fields's width is 20 and
1467 there numbers with more than 32 bits can be represented. */
1468 filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1471 /* XXX These actually have to be calls to strtoll (at least on 32-bit
1472 machines) since the fields's width is 20 and there numbers with more
1473 than 32 bits can be represented. */
1475 || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1477 || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1478 (char **) NULL, 10))
1480 bfd_set_error (bfd_error_no_more_archived_files);
1485 return _bfd_get_elt_at_filepos (archive, filestart);
1488 /* Stat an element in an XCOFF archive. */
1491 _bfd_xcoff_generic_stat_arch_elt (abfd, s)
1495 if (abfd->arelt_data == NULL)
1497 bfd_set_error (bfd_error_invalid_operation);
1501 if (! xcoff_big_format_p (abfd))
1503 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1505 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1506 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1507 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1508 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1509 s->st_size = arch_eltdata (abfd)->parsed_size;
1513 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1515 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1516 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1517 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1518 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1519 s->st_size = arch_eltdata (abfd)->parsed_size;
1525 /* Normalize a file name for inclusion in an archive. */
1528 normalize_filename (abfd)
1532 const char *filename;
1534 file = bfd_get_filename (abfd);
1535 filename = strrchr (file, '/');
1536 if (filename != NULL)
1543 /* Write out an XCOFF armap. */
1547 xcoff_write_armap_old (abfd, elength, map, orl_count, stridx)
1549 unsigned int elength ATTRIBUTE_UNUSED;
1551 unsigned int orl_count;
1554 struct xcoff_ar_hdr hdr;
1556 unsigned char buf[4];
1561 memset (&hdr, 0, sizeof hdr);
1562 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1563 sprintf (hdr.nextoff, "%d", 0);
1564 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1565 sprintf (hdr.date, "%d", 0);
1566 sprintf (hdr.uid, "%d", 0);
1567 sprintf (hdr.gid, "%d", 0);
1568 sprintf (hdr.mode, "%d", 0);
1569 sprintf (hdr.namlen, "%d", 0);
1571 /* We need spaces, not null bytes, in the header. */
1572 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1576 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
1577 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1580 bfd_h_put_32 (abfd, orl_count, buf);
1581 if (bfd_write (buf, 1, 4, abfd) != 4)
1584 sub = abfd->archive_head;
1585 fileoff = SIZEOF_AR_FILE_HDR;
1587 while (sub != NULL && i < orl_count)
1591 while (((bfd *) (map[i]).pos) == sub)
1593 bfd_h_put_32 (abfd, fileoff, buf);
1594 if (bfd_write (buf, 1, 4, abfd) != 4)
1598 namlen = strlen (normalize_filename (sub));
1599 namlen = (namlen + 1) &~ 1;
1600 fileoff += (SIZEOF_AR_HDR
1603 + arelt_size (sub));
1604 fileoff = (fileoff + 1) &~ 1;
1608 for (i = 0; i < orl_count; i++)
1613 name = *map[i].name;
1614 namlen = strlen (name);
1615 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1619 if ((stridx & 1) != 0)
1624 if (bfd_write (&b, 1, 1, abfd) != 1)
1631 /* Write a single armap in the big format. */
1633 xcoff_write_one_armap_big (abfd, map, orl_count, orl_ccount, stridx, bits64,
1637 unsigned int orl_count;
1638 unsigned int orl_ccount;
1639 unsigned int stridx;
1641 const char *prevoff;
1644 struct xcoff_ar_hdr_big hdr;
1646 unsigned char buf[4];
1649 const bfd_arch_info_type *arch_info;
1653 memset (&hdr, 0, sizeof hdr);
1654 /* XXX This call actually should use %lld (at least on 32-bit
1655 machines) since the fields's width is 20 and there numbers with
1656 more than 32 bits can be represented. */
1657 sprintf (hdr.size, "%ld", (long) (4 + orl_ccount * 4 + stridx));
1659 sprintf (hdr.nextoff, "%d", 0);
1661 sprintf (hdr.nextoff, "%d", (strtol (prevoff, (char **) NULL, 10)
1662 + 4 + orl_ccount * 4 + stridx));
1663 memcpy (hdr.prevoff, prevoff, sizeof (hdr.prevoff));
1664 sprintf (hdr.date, "%d", 0);
1665 sprintf (hdr.uid, "%d", 0);
1666 sprintf (hdr.gid, "%d", 0);
1667 sprintf (hdr.mode, "%d", 0);
1668 sprintf (hdr.namlen, "%d", 0);
1670 /* We need spaces, not null bytes, in the header. */
1671 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR_BIG; p++)
1675 memcpy (nextoff, hdr.nextoff, sizeof (hdr.nextoff));
1677 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd) != SIZEOF_AR_HDR_BIG
1678 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1681 bfd_h_put_32 (abfd, orl_ccount, buf);
1682 if (bfd_write (buf, 1, 4, abfd) != 4)
1685 sub = abfd->archive_head;
1686 fileoff = SIZEOF_AR_FILE_HDR_BIG;
1688 while (sub != NULL && i < orl_count)
1692 if ((bfd_arch_bits_per_address ((bfd *) map[i].pos) == 64) == bits64)
1693 while (((bfd *) (map[i]).pos) == sub)
1695 bfd_h_put_32 (abfd, fileoff, buf);
1696 if (bfd_write (buf, 1, 4, abfd) != 4)
1701 while (((bfd *) (map[i]).pos) == sub)
1704 namlen = strlen (normalize_filename (sub));
1705 namlen = (namlen + 1) &~ 1;
1706 fileoff += (SIZEOF_AR_HDR_BIG
1709 + arelt_size (sub));
1710 fileoff = (fileoff + 1) &~ 1;
1715 for (i = 0; i < orl_count; i++)
1719 bfd *ob = (bfd *)map[i].pos;
1721 if (ob != object_bfd)
1722 arch_info = bfd_get_arch_info (ob);
1723 if ((arch_info->bits_per_address == 64) != bits64)
1726 name = *map[i].name;
1727 namlen = strlen (name);
1728 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1732 if ((stridx & 1) != 0)
1737 if (bfd_write (&b, 1, 1, abfd) != 1)
1746 xcoff_write_armap_big (abfd, elength, map, orl_count, stridx)
1748 unsigned int elength ATTRIBUTE_UNUSED;
1750 unsigned int orl_count;
1754 unsigned int orl_count_32, orl_count_64;
1755 unsigned int stridx_32, stridx_64;
1756 const bfd_arch_info_type *arch_info;
1759 /* First, we look through the symbols and work out which are
1760 from 32-bit objects and which from 64-bit ones. */
1766 for (i = 0; i < orl_count; i++)
1768 bfd *ob = (bfd *)map[i].pos;
1770 if (ob != object_bfd)
1771 arch_info = bfd_get_arch_info (ob);
1772 len = strlen (*map[i].name) + 1;
1773 if (arch_info->bits_per_address == 64)
1785 /* A quick sanity check... */
1786 BFD_ASSERT (orl_count_64 + orl_count_32 == orl_count);
1787 BFD_ASSERT (stridx_64 + stridx_32 == stridx);
1789 /* Now write out each map. */
1790 if (! xcoff_write_one_armap_big (abfd, map, orl_count, orl_count_32,
1792 xcoff_ardata_big (abfd)->memoff,
1793 xcoff_ardata_big (abfd)->symoff))
1795 if (! xcoff_write_one_armap_big (abfd, map, orl_count, orl_count_64,
1797 xcoff_ardata_big (abfd)->symoff,
1798 xcoff_ardata_big (abfd)->symoff64))
1806 _bfd_xcoff_write_armap (abfd, elength, map, orl_count, stridx)
1808 unsigned int elength ATTRIBUTE_UNUSED;
1810 unsigned int orl_count;
1813 if (! xcoff_big_format_p (abfd))
1814 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
1816 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
1819 /* Write out an XCOFF archive. We always write an entire archive,
1820 rather than fussing with the freelist and so forth. */
1823 xcoff_write_archive_contents_old (abfd)
1826 struct xcoff_ar_file_hdr fhdr;
1828 size_t total_namlen;
1832 file_ptr prevoff, nextoff;
1835 struct xcoff_ar_hdr ahdr;
1840 memset (&fhdr, 0, sizeof fhdr);
1841 strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1842 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1843 sprintf (fhdr.freeoff, "%d", 0);
1847 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1850 total_namlen += strlen (normalize_filename (sub)) + 1;
1852 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1853 if (offsets == NULL)
1856 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1859 makemap = bfd_has_map (abfd);
1862 nextoff = SIZEOF_AR_FILE_HDR;
1863 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1867 struct xcoff_ar_hdr *ahdrp;
1868 bfd_size_type remaining;
1870 if (makemap && ! hasobjects)
1872 if (bfd_check_format (sub, bfd_object))
1876 name = normalize_filename (sub);
1877 namlen = strlen (name);
1879 if (sub->arelt_data != NULL)
1880 ahdrp = arch_xhdr (sub);
1888 memset (&ahdr, 0, sizeof ahdr);
1890 if (stat (bfd_get_filename (sub), &s) != 0)
1892 bfd_set_error (bfd_error_system_call);
1896 sprintf (ahdrp->size, "%ld", (long) s.st_size);
1897 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1898 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1899 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1900 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1902 if (sub->arelt_data == NULL)
1904 sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
1905 if (sub->arelt_data == NULL)
1909 arch_eltdata (sub)->parsed_size = s.st_size;
1912 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1913 sprintf (ahdrp->namlen, "%ld", (long) namlen);
1915 /* If the length of the name is odd, we write out the null byte
1916 after the name as well. */
1917 namlen = (namlen + 1) &~ 1;
1919 remaining = arelt_size (sub);
1920 size = (SIZEOF_AR_HDR
1925 BFD_ASSERT (nextoff == bfd_tell (abfd));
1927 offsets[i] = nextoff;
1930 nextoff += size + (size & 1);
1932 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1934 /* We need spaces, not null bytes, in the header. */
1935 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1939 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1940 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1941 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1945 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1947 while (remaining != 0)
1950 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1952 amt = sizeof buffer;
1953 if (amt > remaining)
1955 if (bfd_read (buffer, 1, amt, sub) != amt
1956 || bfd_write (buffer, 1, amt, abfd) != amt)
1961 if ((size & 1) != 0)
1966 if (bfd_write (&b, 1, 1, abfd) != 1)
1971 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1973 /* Write out the member table. */
1975 BFD_ASSERT (nextoff == bfd_tell (abfd));
1976 sprintf (fhdr.memoff, "%ld", (long) nextoff);
1978 memset (&ahdr, 0, sizeof ahdr);
1979 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1980 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1981 sprintf (ahdr.date, "%d", 0);
1982 sprintf (ahdr.uid, "%d", 0);
1983 sprintf (ahdr.gid, "%d", 0);
1984 sprintf (ahdr.mode, "%d", 0);
1985 sprintf (ahdr.namlen, "%d", 0);
1987 size = (SIZEOF_AR_HDR
1994 nextoff += size + (size & 1);
1996 if (makemap && hasobjects)
1997 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1999 sprintf (ahdr.nextoff, "%d", 0);
2001 /* We need spaces, not null bytes, in the header. */
2002 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2006 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2007 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2011 sprintf (decbuf, "%-12ld", (long) count);
2012 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2014 for (i = 0; i < count; i++)
2016 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2017 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2020 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2025 name = normalize_filename (sub);
2026 namlen = strlen (name);
2027 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2030 if ((size & 1) != 0)
2035 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2039 /* Write out the armap, if appropriate. */
2041 if (! makemap || ! hasobjects)
2042 sprintf (fhdr.symoff, "%d", 0);
2045 BFD_ASSERT (nextoff == bfd_tell (abfd));
2046 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2047 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2048 if (! _bfd_compute_and_write_armap (abfd, 0))
2052 /* Write out the archive file header. */
2054 /* We need spaces, not null bytes, in the header. */
2055 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2059 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2060 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
2061 SIZEOF_AR_FILE_HDR))
2068 xcoff_write_archive_contents_big (abfd)
2071 struct xcoff_ar_file_hdr_big fhdr;
2073 size_t total_namlen;
2077 file_ptr prevoff, nextoff;
2080 struct xcoff_ar_hdr_big ahdr;
2085 memset (&fhdr, 0, sizeof fhdr);
2086 strncpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2087 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR_BIG);
2088 sprintf (fhdr.freeoff, "%d", 0);
2092 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2095 total_namlen += strlen (normalize_filename (sub)) + 1;
2097 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2098 if (offsets == NULL)
2101 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2104 makemap = bfd_has_map (abfd);
2107 nextoff = SIZEOF_AR_FILE_HDR_BIG;
2108 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
2112 struct xcoff_ar_hdr_big *ahdrp;
2113 bfd_size_type remaining;
2115 if (makemap && ! hasobjects)
2117 if (bfd_check_format (sub, bfd_object))
2121 name = normalize_filename (sub);
2122 namlen = strlen (name);
2124 if (sub->arelt_data != NULL)
2125 ahdrp = arch_xhdr_big (sub);
2133 memset (&ahdr, 0, sizeof ahdr);
2135 /* XXX This should actually be a call to stat64 (at least on
2136 32-bit machines). */
2137 if (stat (bfd_get_filename (sub), &s) != 0)
2139 bfd_set_error (bfd_error_system_call);
2143 /* XXX This call actually should use %lld (at least on 32-bit
2144 machines) since the fields's width is 20 and there numbers with
2145 more than 32 bits can be represented. */
2146 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2147 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2148 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2149 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2150 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2152 if (sub->arelt_data == NULL)
2154 sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
2155 if (sub->arelt_data == NULL)
2159 arch_eltdata (sub)->parsed_size = s.st_size;
2162 /* XXX These calls actually should use %lld (at least on 32-bit
2163 machines) since the fields's width is 20 and there numbers with
2164 more than 32 bits can be represented. */
2165 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2166 sprintf (ahdrp->namlen, "%ld", (long) namlen);
2168 /* If the length of the name is odd, we write out the null byte
2169 after the name as well. */
2170 namlen = (namlen + 1) &~ 1;
2172 remaining = arelt_size (sub);
2173 size = (SIZEOF_AR_HDR_BIG
2178 BFD_ASSERT (nextoff == bfd_tell (abfd));
2180 offsets[i] = nextoff;
2183 nextoff += size + (size & 1);
2185 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
2187 /* We need spaces, not null bytes, in the header. */
2188 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR_BIG; p++)
2192 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR_BIG, abfd)
2193 != SIZEOF_AR_HDR_BIG
2194 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
2195 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2199 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
2201 while (remaining != 0)
2204 bfd_byte buffer[DEFAULT_BUFFERSIZE];
2206 amt = sizeof buffer;
2207 if (amt > remaining)
2209 if (bfd_read (buffer, 1, amt, sub) != amt
2210 || bfd_write (buffer, 1, amt, abfd) != amt)
2215 if ((size & 1) != 0)
2220 if (bfd_write (&b, 1, 1, abfd) != 1)
2225 /* XXX This call actually should use %lld (at least on 32-bit
2226 machines) since the fields's width is 20 and there numbers with
2227 more than 32 bits can be represented. */
2228 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2230 /* Write out the member table. */
2232 BFD_ASSERT (nextoff == bfd_tell (abfd));
2233 /* XXX This call actually should use %lld (at least on 32-bit
2234 machines) since the fields's width is 20 and there numbers with
2235 more than 32 bits can be represented. */
2236 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2238 memset (&ahdr, 0, sizeof ahdr);
2239 /* XXX The next two calls actually should use %lld (at least on 32-bit
2240 machines) since the fields's width is 20 and there numbers with
2241 more than 32 bits can be represented. */
2242 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
2243 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2244 sprintf (ahdr.date, "%d", 0);
2245 sprintf (ahdr.uid, "%d", 0);
2246 sprintf (ahdr.gid, "%d", 0);
2247 sprintf (ahdr.mode, "%d", 0);
2248 sprintf (ahdr.namlen, "%d", 0);
2250 size = (SIZEOF_AR_HDR_BIG
2257 nextoff += size + (size & 1);
2259 if (makemap && hasobjects)
2260 /* XXX This call actually should use %lld (at least on 32-bit
2261 machines) since the fields's width is 20 and there numbers with
2262 more than 32 bits can be represented. */
2263 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2265 sprintf (ahdr.nextoff, "%d", 0);
2267 /* We need spaces, not null bytes, in the header. */
2268 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR_BIG; p++)
2272 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2273 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2277 sprintf (decbuf, "%-12ld", (long) count);
2278 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2280 for (i = 0; i < count; i++)
2282 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2283 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2286 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2291 name = normalize_filename (sub);
2292 namlen = strlen (name);
2293 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2296 if ((size & 1) != 0)
2301 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2305 /* Write out the armap, if appropriate. */
2307 if (! makemap || ! hasobjects)
2308 sprintf (fhdr.symoff, "%d", 0);
2311 BFD_ASSERT (nextoff == bfd_tell (abfd));
2312 /* XXX This call actually should use %lld (at least on 32-bit
2313 machines) since the fields's width is 20 and there numbers with
2314 more than 32 bits can be represented. */
2315 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2316 if (! _bfd_compute_and_write_armap (abfd, 0))
2320 /* Write out the archive file header. */
2322 /* We need spaces, not null bytes, in the header. */
2323 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR_BIG; p++)
2327 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2328 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR_BIG, 1, abfd) !=
2329 SIZEOF_AR_FILE_HDR_BIG))
2336 _bfd_xcoff_write_archive_contents (abfd)
2339 if (! xcoff_big_format_p (abfd))
2340 return xcoff_write_archive_contents_old (abfd);
2342 return xcoff_write_archive_contents_big (abfd);
2345 /* We can't use the usual coff_sizeof_headers routine, because AIX
2346 always uses an a.out header. */
2350 _bfd_xcoff_sizeof_headers (abfd, reloc)
2352 boolean reloc ATTRIBUTE_UNUSED;
2357 if (xcoff_data (abfd)->full_aouthdr)
2360 size += SMALL_AOUTSZ;
2361 size += abfd->section_count * SCNHSZ;