1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 FIXME: Can someone provide a transliteration of this name into ASCII?
4 Using the following chars caused a compiler warning on HIUX (so I replaced
5 them with octal escapes), and isn't useful without an understanding of what
7 Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365,
9 Archive support from Damon A. Permezel.
10 Contributed by IBM Corporation and Cygnus Support.
12 This file is part of BFD, the Binary File Descriptor library.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 /* Internalcoff.h and coffcode.h modify themselves based on this flag. */
29 #define RS6000COFF_C 1
35 #include "coff/internal.h"
36 #include "coff/rs6000.h"
39 /* The main body of code is in coffcode.h. */
41 static boolean xcoff_mkobject PARAMS ((bfd *));
42 static boolean xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *));
43 static void xcoff_rtype2howto
44 PARAMS ((arelent *, struct internal_reloc *));
45 static reloc_howto_type *xcoff_reloc_type_lookup
46 PARAMS ((bfd *, bfd_reloc_code_real_type));
47 static boolean xcoff_slurp_armap PARAMS ((bfd *));
48 static const bfd_target *xcoff_archive_p PARAMS ((bfd *));
49 static PTR xcoff_read_ar_hdr PARAMS ((bfd *));
50 static bfd *xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
51 static int xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
52 static const char *normalize_filename PARAMS ((bfd *));
53 static boolean xcoff_write_armap
54 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
55 static boolean xcoff_write_archive_contents PARAMS ((bfd *));
57 /* We use our own tdata type. Its first field is the COFF tdata type,
58 so the COFF routines are compatible. */
66 abfd->tdata.xcoff_obj_data =
67 ((struct xcoff_tdata *)
68 bfd_zalloc (abfd, sizeof (struct xcoff_tdata)));
69 if (abfd->tdata.xcoff_obj_data == NULL)
71 coff = coff_data (abfd);
72 coff->symbols = (coff_symbol_type *) NULL;
73 coff->conversion_table = (unsigned int *) NULL;
74 coff->raw_syments = (struct coff_ptr_struct *) NULL;
77 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
79 /* We set cputype to -1 to indicate that it has not been
81 xcoff_data (abfd)->cputype = -1;
83 xcoff_data (abfd)->csects = NULL;
84 xcoff_data (abfd)->debug_indices = NULL;
89 /* Copy XCOFF data from one BFD to another. */
92 xcoff_copy_private_bfd_data (ibfd, obfd)
96 struct xcoff_tdata *ix, *ox;
98 if (ibfd->xvec != obfd->xvec)
100 ix = xcoff_data (ibfd);
101 ox = xcoff_data (obfd);
102 ox->full_aouthdr = ix->full_aouthdr;
104 if (ix->toc_section == NULL)
105 ox->toc_section = NULL;
107 ox->toc_section = ix->toc_section->output_section;
108 if (ix->entry_section == NULL)
109 ox->entry_section = NULL;
111 ox->entry_section = ix->entry_section->output_section;
112 ox->text_align_power = ix->text_align_power;
113 ox->data_align_power = ix->data_align_power;
114 ox->modtype = ix->modtype;
115 ox->cputype = ix->cputype;
116 ox->maxdata = ix->maxdata;
117 ox->maxstack = ix->maxstack;
121 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
122 bitsize and whether they are signed or not, along with a
123 conventional type. This table is for the types, which are used for
124 different algorithms for putting in the reloc. Many of these
125 relocs need special_function entries, which I have not written. */
127 static reloc_howto_type xcoff_howto_table[] =
129 /* Standard 32 bit relocation. */
132 2, /* size (0 = byte, 1 = short, 2 = long) */
134 false, /* pc_relative */
136 complain_overflow_bitfield, /* complain_on_overflow */
137 0, /* special_function */
139 true, /* partial_inplace */
140 0xffffffff, /* src_mask */
141 0xffffffff, /* dst_mask */
142 false), /* pcrel_offset */
144 /* 32 bit relocation, but store negative value. */
147 -2, /* size (0 = byte, 1 = short, 2 = long) */
149 false, /* pc_relative */
151 complain_overflow_bitfield, /* complain_on_overflow */
152 0, /* special_function */
154 true, /* partial_inplace */
155 0xffffffff, /* src_mask */
156 0xffffffff, /* dst_mask */
157 false), /* pcrel_offset */
159 /* 32 bit PC relative relocation. */
162 2, /* size (0 = byte, 1 = short, 2 = long) */
164 true, /* pc_relative */
166 complain_overflow_signed, /* complain_on_overflow */
167 0, /* special_function */
169 true, /* partial_inplace */
170 0xffffffff, /* src_mask */
171 0xffffffff, /* dst_mask */
172 false), /* pcrel_offset */
174 /* 16 bit TOC relative relocation. */
177 1, /* size (0 = byte, 1 = short, 2 = long) */
179 false, /* pc_relative */
181 complain_overflow_bitfield, /* complain_on_overflow */
182 0, /* special_function */
184 true, /* partial_inplace */
185 0xffff, /* src_mask */
186 0xffff, /* dst_mask */
187 false), /* pcrel_offset */
189 /* I don't really know what this is. */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
194 false, /* pc_relative */
196 complain_overflow_bitfield, /* complain_on_overflow */
197 0, /* special_function */
199 true, /* partial_inplace */
200 0xffffffff, /* src_mask */
201 0xffffffff, /* dst_mask */
202 false), /* pcrel_offset */
204 /* External TOC relative symbol. */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
209 false, /* pc_relative */
211 complain_overflow_bitfield, /* complain_on_overflow */
212 0, /* special_function */
214 true, /* partial_inplace */
215 0xffff, /* src_mask */
216 0xffff, /* dst_mask */
217 false), /* pcrel_offset */
219 /* Local TOC relative symbol. */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
224 false, /* pc_relative */
226 complain_overflow_bitfield, /* complain_on_overflow */
227 0, /* special_function */
229 true, /* partial_inplace */
230 0xffff, /* src_mask */
231 0xffff, /* dst_mask */
232 false), /* pcrel_offset */
236 /* Non modifiable absolute branch. */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
241 false, /* pc_relative */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 0, /* special_function */
246 true, /* partial_inplace */
247 0x3fffffc, /* src_mask */
248 0x3fffffc, /* dst_mask */
249 false), /* pcrel_offset */
253 /* Non modifiable relative branch. */
254 HOWTO (0xa, /* type */
256 2, /* size (0 = byte, 1 = short, 2 = long) */
258 true, /* pc_relative */
260 complain_overflow_signed, /* complain_on_overflow */
261 0, /* special_function */
263 true, /* partial_inplace */
264 0x3fffffc, /* src_mask */
265 0x3fffffc, /* dst_mask */
266 false), /* pcrel_offset */
271 HOWTO (0xc, /* type */
273 2, /* size (0 = byte, 1 = short, 2 = long) */
275 false, /* pc_relative */
277 complain_overflow_bitfield, /* complain_on_overflow */
278 0, /* special_function */
280 true, /* partial_inplace */
281 0xffff, /* src_mask */
282 0xffff, /* dst_mask */
283 false), /* pcrel_offset */
286 HOWTO (0xd, /* type */
288 2, /* size (0 = byte, 1 = short, 2 = long) */
290 false, /* pc_relative */
292 complain_overflow_bitfield, /* complain_on_overflow */
293 0, /* special_function */
295 true, /* partial_inplace */
296 0xffff, /* src_mask */
297 0xffff, /* dst_mask */
298 false), /* pcrel_offset */
302 /* Non-relocating reference. */
303 HOWTO (0xf, /* type */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
307 false, /* pc_relative */
309 complain_overflow_bitfield, /* complain_on_overflow */
310 0, /* special_function */
312 false, /* partial_inplace */
315 false), /* pcrel_offset */
320 /* TOC relative indirect load. */
321 HOWTO (0x12, /* type */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
325 false, /* pc_relative */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 0, /* special_function */
330 true, /* partial_inplace */
331 0xffff, /* src_mask */
332 0xffff, /* dst_mask */
333 false), /* pcrel_offset */
335 /* TOC relative load address. */
336 HOWTO (0x13, /* type */
338 2, /* size (0 = byte, 1 = short, 2 = long) */
340 false, /* pc_relative */
342 complain_overflow_bitfield, /* complain_on_overflow */
343 0, /* special_function */
345 true, /* partial_inplace */
346 0xffff, /* src_mask */
347 0xffff, /* dst_mask */
348 false), /* pcrel_offset */
350 /* Modifiable relative branch. */
351 HOWTO (0x14, /* type */
353 2, /* size (0 = byte, 1 = short, 2 = long) */
355 false, /* pc_relative */
357 complain_overflow_bitfield, /* complain_on_overflow */
358 0, /* special_function */
359 "R_RRTBI", /* name */
360 true, /* partial_inplace */
361 0xffffffff, /* src_mask */
362 0xffffffff, /* dst_mask */
363 false), /* pcrel_offset */
365 /* Modifiable absolute branch. */
366 HOWTO (0x15, /* type */
368 2, /* size (0 = byte, 1 = short, 2 = long) */
370 false, /* pc_relative */
372 complain_overflow_bitfield, /* complain_on_overflow */
373 0, /* special_function */
374 "R_RRTBA", /* name */
375 true, /* partial_inplace */
376 0xffffffff, /* src_mask */
377 0xffffffff, /* dst_mask */
378 false), /* pcrel_offset */
380 /* Modifiable call absolute indirect. */
381 HOWTO (0x16, /* type */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
385 false, /* pc_relative */
387 complain_overflow_bitfield, /* complain_on_overflow */
388 0, /* special_function */
390 true, /* partial_inplace */
391 0xffff, /* src_mask */
392 0xffff, /* dst_mask */
393 false), /* pcrel_offset */
395 /* Modifiable call relative. */
396 HOWTO (0x17, /* type */
398 2, /* size (0 = byte, 1 = short, 2 = long) */
400 false, /* pc_relative */
402 complain_overflow_bitfield, /* complain_on_overflow */
403 0, /* special_function */
405 true, /* partial_inplace */
406 0xffff, /* src_mask */
407 0xffff, /* dst_mask */
408 false), /* pcrel_offset */
410 /* Modifiable branch absolute. */
411 HOWTO (0x18, /* type */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
415 false, /* pc_relative */
417 complain_overflow_bitfield, /* complain_on_overflow */
418 0, /* special_function */
420 true, /* partial_inplace */
421 0xffff, /* src_mask */
422 0xffff, /* dst_mask */
423 false), /* pcrel_offset */
425 /* Modifiable branch absolute. */
426 HOWTO (0x19, /* type */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
430 false, /* pc_relative */
432 complain_overflow_bitfield, /* complain_on_overflow */
433 0, /* special_function */
435 true, /* partial_inplace */
436 0xffff, /* src_mask */
437 0xffff, /* dst_mask */
438 false), /* pcrel_offset */
440 /* Modifiable branch relative. */
441 HOWTO (0x1a, /* type */
443 2, /* size (0 = byte, 1 = short, 2 = long) */
445 false, /* pc_relative */
447 complain_overflow_signed, /* complain_on_overflow */
448 0, /* special_function */
450 true, /* partial_inplace */
451 0xffff, /* src_mask */
452 0xffff, /* dst_mask */
453 false), /* pcrel_offset */
455 /* Modifiable branch absolute. */
456 HOWTO (0x1b, /* type */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
460 false, /* pc_relative */
462 complain_overflow_bitfield, /* complain_on_overflow */
463 0, /* special_function */
465 true, /* partial_inplace */
466 0xffff, /* src_mask */
467 0xffff, /* dst_mask */
468 false) /* pcrel_offset */
472 xcoff_rtype2howto (relent, internal)
474 struct internal_reloc *internal;
476 relent->howto = xcoff_howto_table + internal->r_type;
478 /* The r_size field of an XCOFF reloc encodes the bitsize of the
479 relocation, as well as indicating whether it is signed or not.
480 Doublecheck that the relocation information gathered from the
481 type matches this information. */
482 if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1)
485 if ((internal->r_size & 0x80) != 0
486 ? (relent->howto->complain_on_overflow != complain_overflow_signed)
487 : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
492 static reloc_howto_type *
493 xcoff_reloc_type_lookup (abfd, code)
495 bfd_reloc_code_real_type code;
499 case BFD_RELOC_PPC_B26:
500 return &xcoff_howto_table[0xa];
501 case BFD_RELOC_PPC_BA26:
502 return &xcoff_howto_table[8];
503 case BFD_RELOC_PPC_TOC16:
504 return &xcoff_howto_table[3];
507 return &xcoff_howto_table[0];
513 #define SELECT_RELOC(internal, howto) \
515 internal.r_type = howto->type; \
517 ((howto->complain_on_overflow == complain_overflow_signed \
520 | (howto->bitsize - 1)); \
523 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
525 #define COFF_LONG_FILENAMES
527 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
529 #define coff_mkobject xcoff_mkobject
530 #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data
531 #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup
532 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
534 #include "coffcode.h"
536 /* XCOFF archive support. The original version of this code was by
537 Damon A. Permezel. It was enhanced to permit cross support, and
538 writing archive files, by Ian Lance Taylor, Cygnus Support.
540 XCOFF uses its own archive format. Everything is hooked together
541 with file offset links, so it is possible to rapidly update an
542 archive in place. Of course, we don't do that. An XCOFF archive
543 has a real file header, not just an ARMAG string. The structure of
544 the file header and of each archive header appear below.
546 An XCOFF archive also has a member table, which is a list of
547 elements in the archive (you can get that by looking through the
548 linked list, but you have to read a lot more of the file). The
549 member table has a normal archive header with an empty name. It is
550 normally (and perhaps must be) the second to last entry in the
551 archive. The member table data is almost printable ASCII. It
552 starts with a 12 character decimal string which is the number of
553 entries in the table. For each entry it has a 12 character decimal
554 string which is the offset in the archive of that member. These
555 entries are followed by a series of null terminated strings which
556 are the member names for each entry.
558 Finally, an XCOFF archive has a global symbol table, which is what
559 we call the armap. The global symbol table has a normal archive
560 header with an empty name. It is normally (and perhaps must be)
561 the last entry in the archive. The contents start with a four byte
562 binary number which is the number of entries. This is followed by
563 a that many four byte binary numbers; each is the file offset of an
564 entry in the archive. These numbers are followed by a series of
565 null terminated strings, which are symbol names. */
567 /* XCOFF archives use this as a magic string. */
569 #define XCOFFARMAG "<aiaff>\012"
570 #define SXCOFFARMAG 8
572 /* This terminates an XCOFF archive member name. */
574 #define XCOFFARFMAG "`\012"
575 #define SXCOFFARFMAG 2
577 /* XCOFF archives start with this (printable) structure. */
579 struct xcoff_ar_file_hdr
582 char magic[SXCOFFARMAG];
584 /* Offset of the member table (decimal ASCII string). */
587 /* Offset of the global symbol table (decimal ASCII string). */
590 /* Offset of the first member in the archive (decimal ASCII string). */
591 char firstmemoff[12];
593 /* Offset of the last member in the archive (decimal ASCII string). */
596 /* Offset of the first member on the free list (decimal ASCII
601 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
603 /* Each XCOFF archive member starts with this (printable) structure. */
607 /* File size not including the header (decimal ASCII string). */
610 /* File offset of next archive member (decimal ASCII string). */
613 /* File offset of previous archive member (decimal ASCII string). */
616 /* File mtime (decimal ASCII string). */
619 /* File UID (decimal ASCII string). */
622 /* File GID (decimal ASCII string). */
625 /* File mode (octal ASCII string). */
628 /* Length of file name (decimal ASCII string). */
631 /* This structure is followed by the file name. The length of the
632 name is given in the namlen field. If the length of the name is
633 odd, the name is followed by a null byte. The name and optional
634 null byte are followed by XCOFFARFMAG, which is not included in
635 namlen. The contents of the archive member follow; the number of
636 bytes is given in the size field. */
639 #define SIZEOF_AR_HDR (7 * 12 + 4)
641 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
642 artdata structure. */
643 #define xcoff_ardata(abfd) \
644 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
646 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
648 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
649 #define arch_xhdr(bfd) \
650 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
652 /* XCOFF archives do not have anything which corresponds to an
653 extended name table. */
655 #define xcoff_slurp_extended_name_table bfd_false
656 #define xcoff_construct_extended_name_table \
657 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
659 #define xcoff_truncate_arname bfd_dont_truncate_arname
661 /* We can use the standard get_elt_at_index routine. */
663 #define xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
665 /* XCOFF archives do not have a timestamp. */
667 #define xcoff_update_armap_timestamp bfd_true
669 /* Read in the armap of an XCOFF archive. */
672 xcoff_slurp_armap (abfd)
676 struct xcoff_ar_hdr hdr;
679 bfd_byte *contents, *cend;
684 if (xcoff_ardata (abfd) == NULL)
686 bfd_has_map (abfd) = false;
690 off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
693 bfd_has_map (abfd) = false;
697 if (bfd_seek (abfd, off, SEEK_SET) != 0)
700 /* The symbol table starts with a normal archive header. */
701 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
704 /* Skip the name (normally empty). */
705 namlen = strtol (hdr.namlen, (char **) NULL, 10);
706 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
709 /* Read in the entire symbol table. */
710 sz = strtol (hdr.size, (char **) NULL, 10);
711 contents = (bfd_byte *) bfd_alloc (abfd, sz);
712 if (contents == NULL)
714 if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
717 /* The symbol table starts with a four byte count. */
718 c = bfd_h_get_32 (abfd, contents);
722 bfd_set_error (bfd_error_bad_value);
726 bfd_ardata (abfd)->symdefs = ((carsym *)
727 bfd_alloc (abfd, c * sizeof (carsym)));
728 if (bfd_ardata (abfd)->symdefs == NULL)
731 /* After the count comes a list of four byte file offsets. */
732 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
734 ++i, ++arsym, p += 4)
735 arsym->file_offset = bfd_h_get_32 (abfd, p);
737 /* After the file offsets come null terminated symbol names. */
738 cend = contents + sz;
739 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
741 ++i, ++arsym, p += strlen ((char *) p) + 1)
745 bfd_set_error (bfd_error_bad_value);
748 arsym->name = (char *) p;
751 bfd_ardata (abfd)->symdef_count = c;
752 bfd_has_map (abfd) = true;
757 /* See if this is an XCOFF archive. */
759 static const bfd_target *
760 xcoff_archive_p (abfd)
763 struct xcoff_ar_file_hdr hdr;
765 if (bfd_read ((PTR) &hdr, SIZEOF_AR_FILE_HDR, 1, abfd)
766 != SIZEOF_AR_FILE_HDR)
768 if (bfd_get_error () != bfd_error_system_call)
769 bfd_set_error (bfd_error_wrong_format);
773 if (strncmp (hdr.magic, XCOFFARMAG, SXCOFFARMAG) != 0)
775 bfd_set_error (bfd_error_wrong_format);
779 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
780 involves a cast, we can't do it as the left operand of
782 abfd->tdata.aout_ar_data =
783 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
785 if (bfd_ardata (abfd) == (struct artdata *) NULL)
788 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
790 bfd_ardata (abfd)->cache = NULL;
791 bfd_ardata (abfd)->archive_head = NULL;
792 bfd_ardata (abfd)->symdefs = NULL;
793 bfd_ardata (abfd)->extended_names = NULL;
795 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
796 if (bfd_ardata (abfd)->tdata == NULL)
799 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
801 if (! xcoff_slurp_armap (abfd))
803 bfd_release (abfd, bfd_ardata (abfd));
804 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
811 /* Read the archive header in an XCOFF archive. */
814 xcoff_read_ar_hdr (abfd)
817 struct xcoff_ar_hdr hdr;
819 struct xcoff_ar_hdr *hdrp;
820 struct areltdata *ret;
822 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
825 namlen = strtol (hdr.namlen, (char **) NULL, 10);
826 hdrp = bfd_alloc (abfd, SIZEOF_AR_HDR + namlen + 1);
829 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
830 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
832 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
834 ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
837 ret->arch_header = (char *) hdrp;
838 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
839 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
841 /* Skip over the XCOFFARFMAG at the end of the file name. */
842 if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
848 /* Open the next element in an XCOFF archive. */
851 xcoff_openr_next_archived_file (archive, last_file)
857 if (xcoff_ardata (archive) == NULL)
859 bfd_set_error (bfd_error_invalid_operation);
863 if (last_file == NULL)
864 filestart = bfd_ardata (archive)->first_file_filepos;
866 filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL, 10);
869 || filestart == strtol (xcoff_ardata (archive)->memoff,
871 || filestart == strtol (xcoff_ardata (archive)->symoff,
874 bfd_set_error (bfd_error_no_more_archived_files);
878 return _bfd_get_elt_at_filepos (archive, filestart);
881 /* Stat an element in an XCOFF archive. */
884 xcoff_generic_stat_arch_elt (abfd, s)
888 struct xcoff_ar_hdr *hdrp;
890 if (abfd->arelt_data == NULL)
892 bfd_set_error (bfd_error_invalid_operation);
896 hdrp = arch_xhdr (abfd);
898 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
899 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
900 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
901 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
902 s->st_size = arch_eltdata (abfd)->parsed_size;
907 /* Normalize a file name for inclusion in an archive. */
910 normalize_filename (abfd)
914 const char *filename;
916 file = bfd_get_filename (abfd);
917 filename = strrchr (file, '/');
918 if (filename != NULL)
925 /* Write out an XCOFF armap. */
929 xcoff_write_armap (abfd, elength, map, orl_count, stridx)
931 unsigned int elength;
933 unsigned int orl_count;
936 struct xcoff_ar_hdr hdr;
938 unsigned char buf[4];
943 memset (&hdr, 0, sizeof hdr);
944 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
945 sprintf (hdr.nextoff, "%d", 0);
946 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
947 sprintf (hdr.date, "%d", 0);
948 sprintf (hdr.uid, "%d", 0);
949 sprintf (hdr.gid, "%d", 0);
950 sprintf (hdr.mode, "%d", 0);
951 sprintf (hdr.namlen, "%d", 0);
953 /* We need spaces, not null bytes, in the header. */
954 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
958 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
959 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
962 bfd_h_put_32 (abfd, orl_count, buf);
963 if (bfd_write (buf, 1, 4, abfd) != 4)
966 sub = abfd->archive_head;
967 fileoff = SIZEOF_AR_FILE_HDR;
969 while (sub != NULL && i < orl_count)
973 while (((bfd *) (map[i]).pos) == sub)
975 bfd_h_put_32 (abfd, fileoff, buf);
976 if (bfd_write (buf, 1, 4, abfd) != 4)
980 namlen = strlen (normalize_filename (sub));
981 namlen = (namlen + 1) &~ 1;
982 fileoff += (SIZEOF_AR_HDR
986 fileoff = (fileoff + 1) &~ 1;
990 for (i = 0; i < orl_count; i++)
996 namlen = strlen (name);
997 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1001 if ((stridx & 1) != 0)
1006 if (bfd_write (&b, 1, 1, abfd) != 1)
1013 /* Write out an XCOFF archive. We always write an entire archive,
1014 rather than fussing with the freelist and so forth. */
1017 xcoff_write_archive_contents (abfd)
1020 struct xcoff_ar_file_hdr fhdr;
1022 size_t total_namlen;
1026 file_ptr prevoff, nextoff;
1029 struct xcoff_ar_hdr ahdr;
1034 memset (&fhdr, 0, sizeof fhdr);
1035 strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1036 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1037 sprintf (fhdr.freeoff, "%d", 0);
1041 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1044 total_namlen += strlen (normalize_filename (sub)) + 1;
1046 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1047 if (offsets == NULL)
1050 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1053 makemap = bfd_has_map (abfd);
1056 nextoff = SIZEOF_AR_FILE_HDR;
1057 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1061 struct xcoff_ar_hdr *ahdrp;
1062 bfd_size_type remaining;
1064 if (makemap && ! hasobjects)
1066 if (bfd_check_format (sub, bfd_object))
1070 name = normalize_filename (sub);
1071 namlen = strlen (name);
1073 if (sub->arelt_data != NULL)
1074 ahdrp = arch_xhdr (sub);
1082 memset (&ahdr, 0, sizeof ahdr);
1084 if (stat (bfd_get_filename (sub), &s) != 0)
1086 bfd_set_error (bfd_error_system_call);
1090 sprintf (ahdrp->size, "%ld", (long) s.st_size);
1091 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1092 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1093 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1094 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1096 if (sub->arelt_data == NULL)
1098 sub->arelt_data = ((struct areltdata *)
1099 bfd_alloc (sub, sizeof (struct areltdata)));
1100 if (sub->arelt_data == NULL)
1104 arch_eltdata (sub)->parsed_size = s.st_size;
1107 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1108 sprintf (ahdrp->namlen, "%ld", (long) namlen);
1110 /* If the length of the name is odd, we write out the null byte
1111 after the name as well. */
1112 namlen = (namlen + 1) &~ 1;
1114 remaining = arelt_size (sub);
1115 size = (SIZEOF_AR_HDR
1120 BFD_ASSERT (nextoff == bfd_tell (abfd));
1122 offsets[i] = nextoff;
1125 nextoff += size + (size & 1);
1127 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1129 /* We need spaces, not null bytes, in the header. */
1130 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1134 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1135 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1136 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1140 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1142 while (remaining != 0)
1145 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1147 amt = sizeof buffer;
1148 if (amt > remaining)
1150 if (bfd_read (buffer, 1, amt, sub) != amt
1151 || bfd_write (buffer, 1, amt, abfd) != amt)
1156 if ((size & 1) != 0)
1161 if (bfd_write (&b, 1, 1, abfd) != 1)
1166 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1168 /* Write out the member table. */
1170 BFD_ASSERT (nextoff == bfd_tell (abfd));
1171 sprintf (fhdr.memoff, "%ld", (long) nextoff);
1173 memset (&ahdr, 0, sizeof ahdr);
1174 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1175 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1176 sprintf (ahdr.date, "%d", 0);
1177 sprintf (ahdr.uid, "%d", 0);
1178 sprintf (ahdr.gid, "%d", 0);
1179 sprintf (ahdr.mode, "%d", 0);
1180 sprintf (ahdr.namlen, "%d", 0);
1182 size = (SIZEOF_AR_HDR
1189 nextoff += size + (size & 1);
1191 if (makemap && hasobjects)
1192 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1194 sprintf (ahdr.nextoff, "%d", 0);
1196 /* We need spaces, not null bytes, in the header. */
1197 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
1201 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1202 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1206 sprintf (decbuf, "%-12ld", (long) count);
1207 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1209 for (i = 0; i < count; i++)
1211 sprintf (decbuf, "%-12ld", (long) offsets[i]);
1212 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1215 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1220 name = normalize_filename (sub);
1221 namlen = strlen (name);
1222 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
1225 if ((size & 1) != 0)
1230 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
1234 /* Write out the armap, if appropriate. */
1236 if (! makemap || ! hasobjects)
1237 sprintf (fhdr.symoff, "%d", 0);
1240 BFD_ASSERT (nextoff == bfd_tell (abfd));
1241 sprintf (fhdr.symoff, "%ld", (long) nextoff);
1242 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
1243 if (! _bfd_compute_and_write_armap (abfd, 0))
1247 /* Write out the archive file header. */
1249 /* We need spaces, not null bytes, in the header. */
1250 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
1254 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1255 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
1256 SIZEOF_AR_FILE_HDR))
1262 /* We can't use the usual coff_sizeof_headers routine, because AIX
1263 always uses an a.out header. */
1267 _bfd_xcoff_sizeof_headers (abfd, reloc)
1274 if (xcoff_data (abfd)->full_aouthdr)
1277 size += SMALL_AOUTSZ;
1278 size += abfd->section_count * SCNHSZ;
1282 #define CORE_FILE_P _bfd_dummy_target
1284 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
1285 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
1286 #define coff_core_file_matches_executable_p \
1287 _bfd_nocore_core_file_matches_executable_p
1291 #define CORE_FILE_P rs6000coff_core_p
1292 extern const bfd_target * rs6000coff_core_p ();
1293 extern boolean rs6000coff_get_section_contents ();
1294 extern boolean rs6000coff_core_file_matches_executable_p ();
1296 #undef coff_core_file_matches_executable_p
1297 #define coff_core_file_matches_executable_p \
1298 rs6000coff_core_file_matches_executable_p
1300 extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd));
1301 #undef coff_core_file_failing_command
1302 #define coff_core_file_failing_command rs6000coff_core_file_failing_command
1304 extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd));
1305 #undef coff_core_file_failing_signal
1306 #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
1308 #undef coff_get_section_contents
1309 #define coff_get_section_contents rs6000coff_get_section_contents
1310 #endif /* AIX_CORE */
1315 #define CORE_FILE_P lynx_core_file_p
1316 extern const bfd_target *lynx_core_file_p PARAMS ((bfd *abfd));
1318 extern boolean lynx_core_file_matches_executable_p PARAMS ((bfd *core_bfd,
1320 #undef coff_core_file_matches_executable_p
1321 #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
1323 extern char *lynx_core_file_failing_command PARAMS ((bfd *abfd));
1324 #undef coff_core_file_failing_command
1325 #define coff_core_file_failing_command lynx_core_file_failing_command
1327 extern int lynx_core_file_failing_signal PARAMS ((bfd *abfd));
1328 #undef coff_core_file_failing_signal
1329 #define coff_core_file_failing_signal lynx_core_file_failing_signal
1331 #endif /* LYNX_CORE */
1333 #define _bfd_xcoff_bfd_get_relocated_section_contents \
1334 coff_bfd_get_relocated_section_contents
1335 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
1336 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
1338 /* The transfer vector that leads the outside world to all of the above. */
1350 "aixcoff-rs6000", /* name */
1352 bfd_target_coff_flavour,
1353 BFD_ENDIAN_BIG, /* data byte order is big */
1354 BFD_ENDIAN_BIG, /* header byte order is big */
1356 (HAS_RELOC | EXEC_P | /* object flags */
1357 HAS_LINENO | HAS_DEBUG | DYNAMIC |
1358 HAS_SYMS | HAS_LOCALS | WP_TEXT),
1360 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1361 0, /* leading char */
1362 '/', /* ar_pad_char */
1363 15, /* ar_max_namelen??? FIXMEmgo */
1365 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1366 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1367 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1368 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1369 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1370 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1372 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1373 xcoff_archive_p, CORE_FILE_P},
1374 {bfd_false, coff_mkobject, /* bfd_set_format */
1375 _bfd_generic_mkarchive, bfd_false},
1376 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1377 xcoff_write_archive_contents, bfd_false},
1379 BFD_JUMP_TABLE_GENERIC (coff),
1380 BFD_JUMP_TABLE_COPY (coff),
1381 BFD_JUMP_TABLE_CORE (coff),
1382 BFD_JUMP_TABLE_ARCHIVE (xcoff),
1383 BFD_JUMP_TABLE_SYMBOLS (coff),
1384 BFD_JUMP_TABLE_RELOCS (coff),
1385 BFD_JUMP_TABLE_WRITE (coff),
1386 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
1387 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),