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 bfd_set_error (bfd_error_no_memory);
74 coff = coff_data (abfd);
75 coff->symbols = (coff_symbol_type *) NULL;
76 coff->conversion_table = (unsigned int *) NULL;
77 coff->raw_syments = (struct coff_ptr_struct *) NULL;
80 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
82 /* We set cputype to -1 to indicate that it has not been
84 xcoff_data (abfd)->cputype = -1;
86 xcoff_data (abfd)->csects = NULL;
87 xcoff_data (abfd)->debug_indices = NULL;
92 /* Copy XCOFF data from one BFD to another. */
95 xcoff_copy_private_bfd_data (ibfd, obfd)
99 struct xcoff_tdata *ix, *ox;
101 if (ibfd->xvec != obfd->xvec)
103 ix = xcoff_data (ibfd);
104 ox = xcoff_data (obfd);
106 ox->text_align_power = ix->text_align_power;
107 ox->data_align_power = ix->data_align_power;
108 ox->modtype = ix->modtype;
109 ox->cputype = ix->cputype;
110 ox->maxdata = ix->maxdata;
111 ox->maxstack = ix->maxstack;
115 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
116 bitsize and whether they are signed or not, along with a
117 conventional type. This table is for the types, which are used for
118 different algorithms for putting in the reloc. Many of these
119 relocs need special_function entries, which I have not written. */
121 static reloc_howto_type xcoff_howto_table[] =
123 /* Standard 32 bit relocation. */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
128 false, /* pc_relative */
130 complain_overflow_bitfield, /* complain_on_overflow */
131 0, /* special_function */
133 true, /* partial_inplace */
134 0xffffffff, /* src_mask */
135 0xffffffff, /* dst_mask */
136 false), /* pcrel_offset */
138 /* 32 bit relocation, but store negative value. */
141 -2, /* size (0 = byte, 1 = short, 2 = long) */
143 false, /* pc_relative */
145 complain_overflow_bitfield, /* complain_on_overflow */
146 0, /* special_function */
148 true, /* partial_inplace */
149 0xffffffff, /* src_mask */
150 0xffffffff, /* dst_mask */
151 false), /* pcrel_offset */
153 /* 32 bit PC relative relocation. */
156 2, /* size (0 = byte, 1 = short, 2 = long) */
158 true, /* pc_relative */
160 complain_overflow_signed, /* complain_on_overflow */
161 0, /* special_function */
163 true, /* partial_inplace */
164 0xffffffff, /* src_mask */
165 0xffffffff, /* dst_mask */
166 false), /* pcrel_offset */
168 /* 16 bit TOC relative relocation. */
171 1, /* size (0 = byte, 1 = short, 2 = long) */
173 false, /* pc_relative */
175 complain_overflow_bitfield, /* complain_on_overflow */
176 0, /* special_function */
178 true, /* partial_inplace */
179 0xffff, /* src_mask */
180 0xffff, /* dst_mask */
181 false), /* pcrel_offset */
183 /* I don't really know what this is. */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
188 false, /* pc_relative */
190 complain_overflow_bitfield, /* complain_on_overflow */
191 0, /* special_function */
193 true, /* partial_inplace */
194 0xffffffff, /* src_mask */
195 0xffffffff, /* dst_mask */
196 false), /* pcrel_offset */
198 /* External TOC relative symbol. */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
203 false, /* pc_relative */
205 complain_overflow_bitfield, /* complain_on_overflow */
206 0, /* special_function */
208 true, /* partial_inplace */
209 0xffff, /* src_mask */
210 0xffff, /* dst_mask */
211 false), /* pcrel_offset */
213 /* Local TOC relative symbol. */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
218 false, /* pc_relative */
220 complain_overflow_bitfield, /* complain_on_overflow */
221 0, /* special_function */
223 true, /* partial_inplace */
224 0xffff, /* src_mask */
225 0xffff, /* dst_mask */
226 false), /* pcrel_offset */
230 /* Non modifiable absolute branch. */
233 2, /* size (0 = byte, 1 = short, 2 = long) */
235 false, /* pc_relative */
237 complain_overflow_bitfield, /* complain_on_overflow */
238 0, /* special_function */
240 true, /* partial_inplace */
241 0x3fffffc, /* src_mask */
242 0x3fffffc, /* dst_mask */
243 false), /* pcrel_offset */
247 /* Non modifiable relative branch. */
248 HOWTO (0xa, /* type */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
252 true, /* pc_relative */
254 complain_overflow_signed, /* complain_on_overflow */
255 0, /* special_function */
257 true, /* partial_inplace */
258 0x3fffffc, /* src_mask */
259 0x3fffffc, /* dst_mask */
260 false), /* pcrel_offset */
265 HOWTO (0xc, /* type */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
269 false, /* pc_relative */
271 complain_overflow_bitfield, /* complain_on_overflow */
272 0, /* special_function */
274 true, /* partial_inplace */
275 0xffff, /* src_mask */
276 0xffff, /* dst_mask */
277 false), /* pcrel_offset */
280 HOWTO (0xd, /* type */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
284 false, /* pc_relative */
286 complain_overflow_bitfield, /* complain_on_overflow */
287 0, /* special_function */
289 true, /* partial_inplace */
290 0xffff, /* src_mask */
291 0xffff, /* dst_mask */
292 false), /* pcrel_offset */
296 /* Non-relocating reference. */
297 HOWTO (0xf, /* type */
299 2, /* size (0 = byte, 1 = short, 2 = long) */
301 false, /* pc_relative */
303 complain_overflow_bitfield, /* complain_on_overflow */
304 0, /* special_function */
306 false, /* partial_inplace */
309 false), /* pcrel_offset */
314 /* TOC relative indirect load. */
315 HOWTO (0x12, /* type */
317 2, /* size (0 = byte, 1 = short, 2 = long) */
319 false, /* pc_relative */
321 complain_overflow_bitfield, /* complain_on_overflow */
322 0, /* special_function */
324 true, /* partial_inplace */
325 0xffff, /* src_mask */
326 0xffff, /* dst_mask */
327 false), /* pcrel_offset */
329 /* TOC relative load address. */
330 HOWTO (0x13, /* type */
332 2, /* size (0 = byte, 1 = short, 2 = long) */
334 false, /* pc_relative */
336 complain_overflow_bitfield, /* complain_on_overflow */
337 0, /* special_function */
339 true, /* partial_inplace */
340 0xffff, /* src_mask */
341 0xffff, /* dst_mask */
342 false), /* pcrel_offset */
344 /* Modifiable relative branch. */
345 HOWTO (0x14, /* type */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
349 false, /* pc_relative */
351 complain_overflow_bitfield, /* complain_on_overflow */
352 0, /* special_function */
353 "R_RRTBI", /* name */
354 true, /* partial_inplace */
355 0xffffffff, /* src_mask */
356 0xffffffff, /* dst_mask */
357 false), /* pcrel_offset */
359 /* Modifiable absolute branch. */
360 HOWTO (0x15, /* type */
362 2, /* size (0 = byte, 1 = short, 2 = long) */
364 false, /* pc_relative */
366 complain_overflow_bitfield, /* complain_on_overflow */
367 0, /* special_function */
368 "R_RRTBA", /* name */
369 true, /* partial_inplace */
370 0xffffffff, /* src_mask */
371 0xffffffff, /* dst_mask */
372 false), /* pcrel_offset */
374 /* Modifiable call absolute indirect. */
375 HOWTO (0x16, /* type */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
379 false, /* pc_relative */
381 complain_overflow_bitfield, /* complain_on_overflow */
382 0, /* special_function */
384 true, /* partial_inplace */
385 0xffff, /* src_mask */
386 0xffff, /* dst_mask */
387 false), /* pcrel_offset */
389 /* Modifiable call relative. */
390 HOWTO (0x17, /* type */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
394 false, /* pc_relative */
396 complain_overflow_bitfield, /* complain_on_overflow */
397 0, /* special_function */
399 true, /* partial_inplace */
400 0xffff, /* src_mask */
401 0xffff, /* dst_mask */
402 false), /* pcrel_offset */
404 /* Modifiable branch absolute. */
405 HOWTO (0x18, /* type */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
409 false, /* pc_relative */
411 complain_overflow_bitfield, /* complain_on_overflow */
412 0, /* special_function */
414 true, /* partial_inplace */
415 0xffff, /* src_mask */
416 0xffff, /* dst_mask */
417 false), /* pcrel_offset */
419 /* Modifiable branch absolute. */
420 HOWTO (0x19, /* type */
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 0xffff, /* src_mask */
431 0xffff, /* dst_mask */
432 false), /* pcrel_offset */
434 /* Modifiable branch relative. */
435 HOWTO (0x1a, /* type */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
439 false, /* pc_relative */
441 complain_overflow_signed, /* complain_on_overflow */
442 0, /* special_function */
444 true, /* partial_inplace */
445 0xffff, /* src_mask */
446 0xffff, /* dst_mask */
447 false), /* pcrel_offset */
449 /* Modifiable branch absolute. */
450 HOWTO (0x1b, /* type */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
454 false, /* pc_relative */
456 complain_overflow_bitfield, /* complain_on_overflow */
457 0, /* special_function */
459 true, /* partial_inplace */
460 0xffff, /* src_mask */
461 0xffff, /* dst_mask */
462 false) /* pcrel_offset */
466 xcoff_rtype2howto (relent, internal)
468 struct internal_reloc *internal;
470 relent->howto = xcoff_howto_table + internal->r_type;
472 /* The r_size field of an XCOFF reloc encodes the bitsize of the
473 relocation, as well as indicating whether it is signed or not.
474 Doublecheck that the relocation information gathered from the
475 type matches this information. */
476 if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1)
479 if ((internal->r_size & 0x80) != 0
480 ? (relent->howto->complain_on_overflow != complain_overflow_signed)
481 : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
486 static reloc_howto_type *
487 xcoff_reloc_type_lookup (abfd, code)
489 bfd_reloc_code_real_type code;
493 case BFD_RELOC_PPC_B26:
494 return &xcoff_howto_table[0xa];
495 case BFD_RELOC_PPC_BA26:
496 return &xcoff_howto_table[8];
497 case BFD_RELOC_PPC_TOC16:
498 return &xcoff_howto_table[3];
500 return &xcoff_howto_table[0];
506 #define SELECT_RELOC(internal, howto) \
508 internal.r_type = howto->type; \
510 ((howto->complain_on_overflow == complain_overflow_signed \
513 | (howto->bitsize - 1)); \
516 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
518 #define COFF_LONG_FILENAMES
520 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
522 #define coff_mkobject xcoff_mkobject
523 #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data
524 #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup
525 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
527 #include "coffcode.h"
529 /* XCOFF archive support. The original version of this code was by
530 Damon A. Permezel. It was enhanced to permit cross support, and
531 writing archive files, by Ian Lance Taylor, Cygnus Support.
533 XCOFF uses its own archive format. Everything is hooked together
534 with file offset links, so it is possible to rapidly update an
535 archive in place. Of course, we don't do that. An XCOFF archive
536 has a real file header, not just an ARMAG string. The structure of
537 the file header and of each archive header appear below.
539 An XCOFF archive also has a member table, which is a list of
540 elements in the archive (you can get that by looking through the
541 linked list, but you have to read a lot more of the file). The
542 member table has a normal archive header with an empty name. It is
543 normally (and perhaps must be) the second to last entry in the
544 archive. The member table data is almost printable ASCII. It
545 starts with a 12 character decimal string which is the number of
546 entries in the table. For each entry it has a 12 character decimal
547 string which is the offset in the archive of that member. These
548 entries are followed by a series of null terminated strings which
549 are the member names for each entry.
551 Finally, an XCOFF archive has a global symbol table, which is what
552 we call the armap. The global symbol table has a normal archive
553 header with an empty name. It is normally (and perhaps must be)
554 the last entry in the archive. The contents start with a four byte
555 binary number which is the number of entries. This is followed by
556 a that many four byte binary numbers; each is the file offset of an
557 entry in the archive. These numbers are followed by a series of
558 null terminated strings, which are symbol names. */
560 /* XCOFF archives use this as a magic string. */
562 #define XCOFFARMAG "<aiaff>\012"
563 #define SXCOFFARMAG 8
565 /* This terminates an XCOFF archive member name. */
567 #define XCOFFARFMAG "`\012"
568 #define SXCOFFARFMAG 2
570 /* XCOFF archives start with this (printable) structure. */
572 struct xcoff_ar_file_hdr
575 char magic[SXCOFFARMAG];
577 /* Offset of the member table (decimal ASCII string). */
580 /* Offset of the global symbol table (decimal ASCII string). */
583 /* Offset of the first member in the archive (decimal ASCII string). */
584 char firstmemoff[12];
586 /* Offset of the last member in the archive (decimal ASCII string). */
589 /* Offset of the first member on the free list (decimal ASCII
594 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
596 /* Each XCOFF archive member starts with this (printable) structure. */
600 /* File size not including the header (decimal ASCII string). */
603 /* File offset of next archive member (decimal ASCII string). */
606 /* File offset of previous archive member (decimal ASCII string). */
609 /* File mtime (decimal ASCII string). */
612 /* File UID (decimal ASCII string). */
615 /* File GID (decimal ASCII string). */
618 /* File mode (octal ASCII string). */
621 /* Length of file name (decimal ASCII string). */
624 /* This structure is followed by the file name. The length of the
625 name is given in the namlen field. If the length of the name is
626 odd, the name is followed by a null byte. The name and optional
627 null byte are followed by XCOFFARFMAG, which is not included in
628 namlen. The contents of the archive member follow; the number of
629 bytes is given in the size field. */
632 #define SIZEOF_AR_HDR (7 * 12 + 4)
634 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
635 artdata structure. */
636 #define xcoff_ardata(abfd) \
637 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
639 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
641 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
642 #define arch_xhdr(bfd) \
643 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
645 /* XCOFF archives do not have anything which corresponds to an
646 extended name table. */
648 #define xcoff_slurp_extended_name_table bfd_false
649 #define xcoff_construct_extended_name_table \
650 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
652 #define xcoff_truncate_arname bfd_dont_truncate_arname
654 /* XCOFF archives do not have a timestamp. */
656 #define xcoff_update_armap_timestamp bfd_true
658 /* Read in the armap of an XCOFF archive. */
661 xcoff_slurp_armap (abfd)
665 struct xcoff_ar_hdr hdr;
668 bfd_byte *contents, *cend;
673 if (xcoff_ardata (abfd) == NULL)
675 bfd_has_map (abfd) = false;
679 off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
682 bfd_has_map (abfd) = false;
686 if (bfd_seek (abfd, off, SEEK_SET) != 0)
689 /* The symbol table starts with a normal archive header. */
690 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
693 /* Skip the name (normally empty). */
694 namlen = strtol (hdr.namlen, (char **) NULL, 10);
695 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
698 /* Read in the entire symbol table. */
699 sz = strtol (hdr.size, (char **) NULL, 10);
700 contents = (bfd_byte *) bfd_alloc (abfd, sz);
701 if (contents == NULL)
703 bfd_set_error (bfd_error_no_memory);
706 if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
709 /* The symbol table starts with a four byte count. */
710 c = bfd_h_get_32 (abfd, contents);
714 bfd_set_error (bfd_error_bad_value);
718 bfd_ardata (abfd)->symdefs = ((carsym *)
719 bfd_alloc (abfd, c * sizeof (carsym)));
720 if (bfd_ardata (abfd)->symdefs == NULL)
722 bfd_set_error (bfd_error_no_memory);
726 /* After the count comes a list of four byte file offsets. */
727 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
729 ++i, ++arsym, p += 4)
730 arsym->file_offset = bfd_h_get_32 (abfd, p);
732 /* After the file offsets come null terminated symbol names. */
733 cend = contents + sz;
734 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
736 ++i, ++arsym, p += strlen ((char *) p) + 1)
740 bfd_set_error (bfd_error_bad_value);
743 arsym->name = (char *) p;
746 bfd_ardata (abfd)->symdef_count = c;
747 bfd_has_map (abfd) = true;
752 /* See if this is an XCOFF archive. */
754 static const bfd_target *
755 xcoff_archive_p (abfd)
758 struct xcoff_ar_file_hdr hdr;
760 if (bfd_read ((PTR) &hdr, SIZEOF_AR_FILE_HDR, 1, abfd)
761 != SIZEOF_AR_FILE_HDR)
763 if (bfd_get_error () != bfd_error_system_call)
764 bfd_set_error (bfd_error_wrong_format);
768 if (strncmp (hdr.magic, XCOFFARMAG, SXCOFFARMAG) != 0)
770 bfd_set_error (bfd_error_wrong_format);
774 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
775 involves a cast, we can't do it as the left operand of
777 abfd->tdata.aout_ar_data =
778 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
780 if (bfd_ardata (abfd) == (struct artdata *) NULL)
782 bfd_set_error (bfd_error_no_memory);
786 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
788 bfd_ardata (abfd)->cache = NULL;
789 bfd_ardata (abfd)->archive_head = NULL;
790 bfd_ardata (abfd)->symdefs = NULL;
791 bfd_ardata (abfd)->extended_names = NULL;
793 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
794 if (bfd_ardata (abfd)->tdata == NULL)
796 bfd_set_error (bfd_error_no_memory);
800 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
802 if (! xcoff_slurp_armap (abfd))
804 bfd_release (abfd, bfd_ardata (abfd));
805 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
812 /* Read the archive header in an XCOFF archive. */
815 xcoff_read_ar_hdr (abfd)
818 struct xcoff_ar_hdr hdr;
820 struct xcoff_ar_hdr *hdrp;
821 struct areltdata *ret;
823 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
826 namlen = strtol (hdr.namlen, (char **) NULL, 10);
827 hdrp = bfd_alloc (abfd, SIZEOF_AR_HDR + namlen + 1);
830 bfd_set_error (bfd_error_no_memory);
833 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
834 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
836 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
838 ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
841 bfd_set_error (bfd_error_no_memory);
844 ret->arch_header = (char *) hdrp;
845 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
846 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
848 /* Skip over the XCOFFARFMAG at the end of the file name. */
849 if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
855 /* Open the next element in an XCOFF archive. */
858 xcoff_openr_next_archived_file (archive, last_file)
864 if (xcoff_ardata (archive) == NULL)
866 bfd_set_error (bfd_error_invalid_operation);
870 if (last_file == NULL)
871 filestart = bfd_ardata (archive)->first_file_filepos;
873 filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL, 10);
876 || filestart == strtol (xcoff_ardata (archive)->memoff,
878 || filestart == strtol (xcoff_ardata (archive)->symoff,
881 bfd_set_error (bfd_error_no_more_archived_files);
885 return _bfd_get_elt_at_filepos (archive, filestart);
888 /* Stat an element in an XCOFF archive. */
891 xcoff_generic_stat_arch_elt (abfd, s)
895 struct xcoff_ar_hdr *hdrp;
897 if (abfd->arelt_data == NULL)
899 bfd_set_error (bfd_error_invalid_operation);
903 hdrp = arch_xhdr (abfd);
905 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
906 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
907 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
908 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
909 s->st_size = arch_eltdata (abfd)->parsed_size;
914 /* Normalize a file name for inclusion in an archive. */
917 normalize_filename (abfd)
921 const char *filename;
923 file = bfd_get_filename (abfd);
924 filename = strrchr (file, '/');
925 if (filename != NULL)
932 /* Write out an XCOFF armap. */
936 xcoff_write_armap (abfd, elength, map, orl_count, stridx)
938 unsigned int elength;
940 unsigned int orl_count;
943 struct xcoff_ar_hdr hdr;
945 unsigned char buf[4];
950 memset (&hdr, 0, sizeof hdr);
951 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
952 sprintf (hdr.nextoff, "%d", 0);
953 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
954 sprintf (hdr.date, "%d", 0);
955 sprintf (hdr.uid, "%d", 0);
956 sprintf (hdr.gid, "%d", 0);
957 sprintf (hdr.mode, "%d", 0);
958 sprintf (hdr.namlen, "%d", 0);
960 /* We need spaces, not null bytes, in the header. */
961 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
965 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
966 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
969 bfd_h_put_32 (abfd, orl_count, buf);
970 if (bfd_write (buf, 1, 4, abfd) != 4)
973 sub = abfd->archive_head;
974 fileoff = SIZEOF_AR_FILE_HDR;
976 while (sub != NULL && i < orl_count)
980 while (((bfd *) (map[i]).pos) == sub)
982 bfd_h_put_32 (abfd, fileoff, buf);
983 if (bfd_write (buf, 1, 4, abfd) != 4)
987 namlen = strlen (normalize_filename (sub));
988 namlen = (namlen + 1) &~ 1;
989 fileoff += (SIZEOF_AR_HDR
993 fileoff = (fileoff + 1) &~ 1;
997 for (i = 0; i < orl_count; i++)
1002 name = *map[i].name;
1003 namlen = strlen (name);
1004 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1008 if ((stridx & 1) != 0)
1013 if (bfd_write (&b, 1, 1, abfd) != 1)
1020 /* Write out an XCOFF archive. We always write an entire archive,
1021 rather than fussing with the freelist and so forth. */
1024 xcoff_write_archive_contents (abfd)
1027 struct xcoff_ar_file_hdr fhdr;
1029 size_t total_namlen;
1033 file_ptr prevoff, nextoff;
1036 struct xcoff_ar_hdr ahdr;
1041 memset (&fhdr, 0, sizeof fhdr);
1042 strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1043 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1044 sprintf (fhdr.freeoff, "%d", 0);
1048 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1051 total_namlen += strlen (normalize_filename (sub)) + 1;
1053 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1054 if (offsets == NULL)
1056 bfd_set_error (bfd_error_no_memory);
1060 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1063 makemap = bfd_has_map (abfd);
1066 nextoff = SIZEOF_AR_FILE_HDR;
1067 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1071 struct xcoff_ar_hdr *ahdrp;
1072 bfd_size_type remaining;
1074 if (makemap && ! hasobjects)
1076 if (bfd_check_format (sub, bfd_object))
1080 name = normalize_filename (sub);
1081 namlen = strlen (name);
1083 if (sub->arelt_data != NULL)
1084 ahdrp = arch_xhdr (sub);
1092 memset (&ahdr, 0, sizeof ahdr);
1094 if (stat (bfd_get_filename (sub), &s) != 0)
1096 bfd_set_error (bfd_error_system_call);
1100 sprintf (ahdrp->size, "%ld", (long) s.st_size);
1101 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1102 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1103 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1104 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1106 if (sub->arelt_data == NULL)
1108 sub->arelt_data = ((struct areltdata *)
1109 bfd_alloc (sub, sizeof (struct areltdata)));
1110 if (sub->arelt_data == NULL)
1112 bfd_set_error (bfd_error_no_memory);
1117 arch_eltdata (sub)->parsed_size = s.st_size;
1120 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1121 sprintf (ahdrp->namlen, "%ld", (long) namlen);
1123 /* If the length of the name is odd, we write out the null byte
1124 after the name as well. */
1125 namlen = (namlen + 1) &~ 1;
1127 remaining = arelt_size (sub);
1128 size = (SIZEOF_AR_HDR
1133 BFD_ASSERT (nextoff == bfd_tell (abfd));
1135 offsets[i] = nextoff;
1138 nextoff += size + (size & 1);
1140 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1142 /* We need spaces, not null bytes, in the header. */
1143 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1147 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1148 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1149 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1153 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1155 while (remaining != 0)
1158 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1160 amt = sizeof buffer;
1161 if (amt > remaining)
1163 if (bfd_read (buffer, 1, amt, sub) != amt
1164 || bfd_write (buffer, 1, amt, abfd) != amt)
1169 if ((size & 1) != 0)
1174 if (bfd_write (&b, 1, 1, abfd) != 1)
1179 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1181 /* Write out the member table. */
1183 BFD_ASSERT (nextoff == bfd_tell (abfd));
1184 sprintf (fhdr.memoff, "%ld", (long) nextoff);
1186 memset (&ahdr, 0, sizeof ahdr);
1187 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1188 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1189 sprintf (ahdr.date, "%d", 0);
1190 sprintf (ahdr.uid, "%d", 0);
1191 sprintf (ahdr.gid, "%d", 0);
1192 sprintf (ahdr.mode, "%d", 0);
1193 sprintf (ahdr.namlen, "%d", 0);
1195 size = (SIZEOF_AR_HDR
1202 nextoff += size + (size & 1);
1204 if (makemap && hasobjects)
1205 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1207 sprintf (ahdr.nextoff, "%d", 0);
1209 /* We need spaces, not null bytes, in the header. */
1210 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
1214 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1215 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1219 sprintf (decbuf, "%-12ld", (long) count);
1220 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1222 for (i = 0; i < count; i++)
1224 sprintf (decbuf, "%-12ld", (long) offsets[i]);
1225 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1228 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1233 name = normalize_filename (sub);
1234 namlen = strlen (name);
1235 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
1238 if ((size & 1) != 0)
1243 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
1247 /* Write out the armap, if appropriate. */
1249 if (! makemap || ! hasobjects)
1250 sprintf (fhdr.symoff, "%d", 0);
1253 BFD_ASSERT (nextoff == bfd_tell (abfd));
1254 sprintf (fhdr.symoff, "%ld", (long) nextoff);
1255 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
1256 if (! _bfd_compute_and_write_armap (abfd, 0))
1260 /* Write out the archive file header. */
1262 /* We need spaces, not null bytes, in the header. */
1263 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
1267 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1268 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
1269 SIZEOF_AR_FILE_HDR))
1275 #define CORE_FILE_P _bfd_dummy_target
1277 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
1278 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
1279 #define coff_core_file_matches_executable_p \
1280 _bfd_nocore_core_file_matches_executable_p
1284 #define CORE_FILE_P rs6000coff_core_p
1285 extern const bfd_target * rs6000coff_core_p ();
1286 extern boolean rs6000coff_get_section_contents ();
1287 extern boolean rs6000coff_core_file_matches_executable_p ();
1289 #undef coff_core_file_matches_executable_p
1290 #define coff_core_file_matches_executable_p \
1291 rs6000coff_core_file_matches_executable_p
1293 extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd));
1294 #undef coff_core_file_failing_command
1295 #define coff_core_file_failing_command rs6000coff_core_file_failing_command
1297 extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd));
1298 #undef coff_core_file_failing_signal
1299 #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
1301 #undef coff_get_section_contents
1302 #define coff_get_section_contents rs6000coff_get_section_contents
1303 #endif /* AIX_CORE */
1308 #define CORE_FILE_P lynx_core_file_p
1309 extern const bfd_target *lynx_core_file_p PARAMS ((bfd *abfd));
1311 extern boolean lynx_core_file_matches_executable_p PARAMS ((bfd *core_bfd,
1313 #undef coff_core_file_matches_executable_p
1314 #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
1316 extern char *lynx_core_file_failing_command PARAMS ((bfd *abfd));
1317 #undef coff_core_file_failing_command
1318 #define coff_core_file_failing_command lynx_core_file_failing_command
1320 extern int lynx_core_file_failing_signal PARAMS ((bfd *abfd));
1321 #undef coff_core_file_failing_signal
1322 #define coff_core_file_failing_signal lynx_core_file_failing_signal
1324 #endif /* LYNX_CORE */
1326 #define _bfd_xcoff_sizeof_headers coff_sizeof_headers
1327 #define _bfd_xcoff_bfd_get_relocated_section_contents \
1328 coff_bfd_get_relocated_section_contents
1329 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
1330 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
1332 /* The transfer vector that leads the outside world to all of the above. */
1334 const bfd_target rs6000coff_vec =
1336 "aixcoff-rs6000", /* name */
1337 bfd_target_coff_flavour,
1338 true, /* data byte order is big */
1339 true, /* header byte order is big */
1341 (HAS_RELOC | EXEC_P | /* object flags */
1342 HAS_LINENO | HAS_DEBUG | DYNAMIC |
1343 HAS_SYMS | HAS_LOCALS | WP_TEXT),
1345 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1346 0, /* leading char */
1347 '/', /* ar_pad_char */
1348 15, /* ar_max_namelen??? FIXMEmgo */
1350 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1351 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1352 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1353 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1354 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1355 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1357 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1358 xcoff_archive_p, CORE_FILE_P},
1359 {bfd_false, coff_mkobject, /* bfd_set_format */
1360 _bfd_generic_mkarchive, bfd_false},
1361 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1362 xcoff_write_archive_contents, bfd_false},
1364 BFD_JUMP_TABLE_GENERIC (coff),
1365 BFD_JUMP_TABLE_COPY (coff),
1366 BFD_JUMP_TABLE_CORE (coff),
1367 BFD_JUMP_TABLE_ARCHIVE (xcoff),
1368 BFD_JUMP_TABLE_SYMBOLS (coff),
1369 BFD_JUMP_TABLE_RELOCS (coff),
1370 BFD_JUMP_TABLE_WRITE (coff),
1371 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
1372 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),