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
);
105 ox
->full_aouthdr
= ix
->full_aouthdr
;
107 if (ix
->toc_section
== NULL
)
108 ox
->toc_section
= NULL
;
110 ox
->toc_section
= ix
->toc_section
->output_section
;
111 if (ix
->entry_section
== NULL
)
112 ox
->entry_section
= NULL
;
114 ox
->entry_section
= ix
->entry_section
->output_section
;
115 ox
->text_align_power
= ix
->text_align_power
;
116 ox
->data_align_power
= ix
->data_align_power
;
117 ox
->modtype
= ix
->modtype
;
118 ox
->cputype
= ix
->cputype
;
119 ox
->maxdata
= ix
->maxdata
;
120 ox
->maxstack
= ix
->maxstack
;
124 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
125 bitsize and whether they are signed or not, along with a
126 conventional type. This table is for the types, which are used for
127 different algorithms for putting in the reloc. Many of these
128 relocs need special_function entries, which I have not written. */
130 static reloc_howto_type xcoff_howto_table
[] =
132 /* Standard 32 bit relocation. */
135 2, /* size (0 = byte, 1 = short, 2 = long) */
137 false, /* pc_relative */
139 complain_overflow_bitfield
, /* complain_on_overflow */
140 0, /* special_function */
142 true, /* partial_inplace */
143 0xffffffff, /* src_mask */
144 0xffffffff, /* dst_mask */
145 false), /* pcrel_offset */
147 /* 32 bit relocation, but store negative value. */
150 -2, /* size (0 = byte, 1 = short, 2 = long) */
152 false, /* pc_relative */
154 complain_overflow_bitfield
, /* complain_on_overflow */
155 0, /* special_function */
157 true, /* partial_inplace */
158 0xffffffff, /* src_mask */
159 0xffffffff, /* dst_mask */
160 false), /* pcrel_offset */
162 /* 32 bit PC relative relocation. */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
167 true, /* pc_relative */
169 complain_overflow_signed
, /* complain_on_overflow */
170 0, /* special_function */
172 true, /* partial_inplace */
173 0xffffffff, /* src_mask */
174 0xffffffff, /* dst_mask */
175 false), /* pcrel_offset */
177 /* 16 bit TOC relative relocation. */
180 1, /* size (0 = byte, 1 = short, 2 = long) */
182 false, /* pc_relative */
184 complain_overflow_bitfield
, /* complain_on_overflow */
185 0, /* special_function */
187 true, /* partial_inplace */
188 0xffff, /* src_mask */
189 0xffff, /* dst_mask */
190 false), /* pcrel_offset */
192 /* I don't really know what this is. */
195 2, /* size (0 = byte, 1 = short, 2 = long) */
197 false, /* pc_relative */
199 complain_overflow_bitfield
, /* complain_on_overflow */
200 0, /* special_function */
202 true, /* partial_inplace */
203 0xffffffff, /* src_mask */
204 0xffffffff, /* dst_mask */
205 false), /* pcrel_offset */
207 /* External TOC relative symbol. */
210 2, /* size (0 = byte, 1 = short, 2 = long) */
212 false, /* pc_relative */
214 complain_overflow_bitfield
, /* complain_on_overflow */
215 0, /* special_function */
217 true, /* partial_inplace */
218 0xffff, /* src_mask */
219 0xffff, /* dst_mask */
220 false), /* pcrel_offset */
222 /* Local TOC relative symbol. */
225 2, /* size (0 = byte, 1 = short, 2 = long) */
227 false, /* pc_relative */
229 complain_overflow_bitfield
, /* complain_on_overflow */
230 0, /* special_function */
232 true, /* partial_inplace */
233 0xffff, /* src_mask */
234 0xffff, /* dst_mask */
235 false), /* pcrel_offset */
239 /* Non modifiable absolute branch. */
242 2, /* size (0 = byte, 1 = short, 2 = long) */
244 false, /* pc_relative */
246 complain_overflow_bitfield
, /* complain_on_overflow */
247 0, /* special_function */
249 true, /* partial_inplace */
250 0x3fffffc, /* src_mask */
251 0x3fffffc, /* dst_mask */
252 false), /* pcrel_offset */
256 /* Non modifiable relative branch. */
257 HOWTO (0xa, /* type */
259 2, /* size (0 = byte, 1 = short, 2 = long) */
261 true, /* pc_relative */
263 complain_overflow_signed
, /* complain_on_overflow */
264 0, /* special_function */
266 true, /* partial_inplace */
267 0x3fffffc, /* src_mask */
268 0x3fffffc, /* dst_mask */
269 false), /* pcrel_offset */
274 HOWTO (0xc, /* type */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
278 false, /* pc_relative */
280 complain_overflow_bitfield
, /* complain_on_overflow */
281 0, /* special_function */
283 true, /* partial_inplace */
284 0xffff, /* src_mask */
285 0xffff, /* dst_mask */
286 false), /* pcrel_offset */
289 HOWTO (0xd, /* type */
291 2, /* size (0 = byte, 1 = short, 2 = long) */
293 false, /* pc_relative */
295 complain_overflow_bitfield
, /* complain_on_overflow */
296 0, /* special_function */
298 true, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 false), /* pcrel_offset */
305 /* Non-relocating reference. */
306 HOWTO (0xf, /* type */
308 2, /* size (0 = byte, 1 = short, 2 = long) */
310 false, /* pc_relative */
312 complain_overflow_bitfield
, /* complain_on_overflow */
313 0, /* special_function */
315 false, /* partial_inplace */
318 false), /* pcrel_offset */
323 /* TOC relative indirect load. */
324 HOWTO (0x12, /* type */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
328 false, /* pc_relative */
330 complain_overflow_bitfield
, /* complain_on_overflow */
331 0, /* special_function */
333 true, /* partial_inplace */
334 0xffff, /* src_mask */
335 0xffff, /* dst_mask */
336 false), /* pcrel_offset */
338 /* TOC relative load address. */
339 HOWTO (0x13, /* type */
341 2, /* size (0 = byte, 1 = short, 2 = long) */
343 false, /* pc_relative */
345 complain_overflow_bitfield
, /* complain_on_overflow */
346 0, /* special_function */
348 true, /* partial_inplace */
349 0xffff, /* src_mask */
350 0xffff, /* dst_mask */
351 false), /* pcrel_offset */
353 /* Modifiable relative branch. */
354 HOWTO (0x14, /* type */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
358 false, /* pc_relative */
360 complain_overflow_bitfield
, /* complain_on_overflow */
361 0, /* special_function */
362 "R_RRTBI", /* name */
363 true, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 false), /* pcrel_offset */
368 /* Modifiable absolute branch. */
369 HOWTO (0x15, /* type */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
373 false, /* pc_relative */
375 complain_overflow_bitfield
, /* complain_on_overflow */
376 0, /* special_function */
377 "R_RRTBA", /* name */
378 true, /* partial_inplace */
379 0xffffffff, /* src_mask */
380 0xffffffff, /* dst_mask */
381 false), /* pcrel_offset */
383 /* Modifiable call absolute indirect. */
384 HOWTO (0x16, /* type */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
388 false, /* pc_relative */
390 complain_overflow_bitfield
, /* complain_on_overflow */
391 0, /* special_function */
393 true, /* partial_inplace */
394 0xffff, /* src_mask */
395 0xffff, /* dst_mask */
396 false), /* pcrel_offset */
398 /* Modifiable call relative. */
399 HOWTO (0x17, /* type */
401 2, /* size (0 = byte, 1 = short, 2 = long) */
403 false, /* pc_relative */
405 complain_overflow_bitfield
, /* complain_on_overflow */
406 0, /* special_function */
408 true, /* partial_inplace */
409 0xffff, /* src_mask */
410 0xffff, /* dst_mask */
411 false), /* pcrel_offset */
413 /* Modifiable branch absolute. */
414 HOWTO (0x18, /* type */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
418 false, /* pc_relative */
420 complain_overflow_bitfield
, /* complain_on_overflow */
421 0, /* special_function */
423 true, /* partial_inplace */
424 0xffff, /* src_mask */
425 0xffff, /* dst_mask */
426 false), /* pcrel_offset */
428 /* Modifiable branch absolute. */
429 HOWTO (0x19, /* type */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
433 false, /* pc_relative */
435 complain_overflow_bitfield
, /* complain_on_overflow */
436 0, /* special_function */
438 true, /* partial_inplace */
439 0xffff, /* src_mask */
440 0xffff, /* dst_mask */
441 false), /* pcrel_offset */
443 /* Modifiable branch relative. */
444 HOWTO (0x1a, /* type */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
448 false, /* pc_relative */
450 complain_overflow_signed
, /* complain_on_overflow */
451 0, /* special_function */
453 true, /* partial_inplace */
454 0xffff, /* src_mask */
455 0xffff, /* dst_mask */
456 false), /* pcrel_offset */
458 /* Modifiable branch absolute. */
459 HOWTO (0x1b, /* type */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
463 false, /* pc_relative */
465 complain_overflow_bitfield
, /* complain_on_overflow */
466 0, /* special_function */
468 true, /* partial_inplace */
469 0xffff, /* src_mask */
470 0xffff, /* dst_mask */
471 false) /* pcrel_offset */
475 xcoff_rtype2howto (relent
, internal
)
477 struct internal_reloc
*internal
;
479 relent
->howto
= xcoff_howto_table
+ internal
->r_type
;
481 /* The r_size field of an XCOFF reloc encodes the bitsize of the
482 relocation, as well as indicating whether it is signed or not.
483 Doublecheck that the relocation information gathered from the
484 type matches this information. */
485 if (relent
->howto
->bitsize
!= ((unsigned int) internal
->r_size
& 0x1f) + 1)
488 if ((internal
->r_size
& 0x80) != 0
489 ? (relent
->howto
->complain_on_overflow
!= complain_overflow_signed
)
490 : (relent
->howto
->complain_on_overflow
!= complain_overflow_bitfield
))
495 static reloc_howto_type
*
496 xcoff_reloc_type_lookup (abfd
, code
)
498 bfd_reloc_code_real_type code
;
502 case BFD_RELOC_PPC_B26
:
503 return &xcoff_howto_table
[0xa];
504 case BFD_RELOC_PPC_BA26
:
505 return &xcoff_howto_table
[8];
506 case BFD_RELOC_PPC_TOC16
:
507 return &xcoff_howto_table
[3];
509 return &xcoff_howto_table
[0];
515 #define SELECT_RELOC(internal, howto) \
517 internal.r_type = howto->type; \
519 ((howto->complain_on_overflow == complain_overflow_signed \
522 | (howto->bitsize - 1)); \
525 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
527 #define COFF_LONG_FILENAMES
529 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
531 #define coff_mkobject xcoff_mkobject
532 #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data
533 #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup
534 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
536 #include "coffcode.h"
538 /* XCOFF archive support. The original version of this code was by
539 Damon A. Permezel. It was enhanced to permit cross support, and
540 writing archive files, by Ian Lance Taylor, Cygnus Support.
542 XCOFF uses its own archive format. Everything is hooked together
543 with file offset links, so it is possible to rapidly update an
544 archive in place. Of course, we don't do that. An XCOFF archive
545 has a real file header, not just an ARMAG string. The structure of
546 the file header and of each archive header appear below.
548 An XCOFF archive also has a member table, which is a list of
549 elements in the archive (you can get that by looking through the
550 linked list, but you have to read a lot more of the file). The
551 member table has a normal archive header with an empty name. It is
552 normally (and perhaps must be) the second to last entry in the
553 archive. The member table data is almost printable ASCII. It
554 starts with a 12 character decimal string which is the number of
555 entries in the table. For each entry it has a 12 character decimal
556 string which is the offset in the archive of that member. These
557 entries are followed by a series of null terminated strings which
558 are the member names for each entry.
560 Finally, an XCOFF archive has a global symbol table, which is what
561 we call the armap. The global symbol table has a normal archive
562 header with an empty name. It is normally (and perhaps must be)
563 the last entry in the archive. The contents start with a four byte
564 binary number which is the number of entries. This is followed by
565 a that many four byte binary numbers; each is the file offset of an
566 entry in the archive. These numbers are followed by a series of
567 null terminated strings, which are symbol names. */
569 /* XCOFF archives use this as a magic string. */
571 #define XCOFFARMAG "<aiaff>\012"
572 #define SXCOFFARMAG 8
574 /* This terminates an XCOFF archive member name. */
576 #define XCOFFARFMAG "`\012"
577 #define SXCOFFARFMAG 2
579 /* XCOFF archives start with this (printable) structure. */
581 struct xcoff_ar_file_hdr
584 char magic
[SXCOFFARMAG
];
586 /* Offset of the member table (decimal ASCII string). */
589 /* Offset of the global symbol table (decimal ASCII string). */
592 /* Offset of the first member in the archive (decimal ASCII string). */
593 char firstmemoff
[12];
595 /* Offset of the last member in the archive (decimal ASCII string). */
598 /* Offset of the first member on the free list (decimal ASCII
603 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
605 /* Each XCOFF archive member starts with this (printable) structure. */
609 /* File size not including the header (decimal ASCII string). */
612 /* File offset of next archive member (decimal ASCII string). */
615 /* File offset of previous archive member (decimal ASCII string). */
618 /* File mtime (decimal ASCII string). */
621 /* File UID (decimal ASCII string). */
624 /* File GID (decimal ASCII string). */
627 /* File mode (octal ASCII string). */
630 /* Length of file name (decimal ASCII string). */
633 /* This structure is followed by the file name. The length of the
634 name is given in the namlen field. If the length of the name is
635 odd, the name is followed by a null byte. The name and optional
636 null byte are followed by XCOFFARFMAG, which is not included in
637 namlen. The contents of the archive member follow; the number of
638 bytes is given in the size field. */
641 #define SIZEOF_AR_HDR (7 * 12 + 4)
643 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
644 artdata structure. */
645 #define xcoff_ardata(abfd) \
646 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
648 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
650 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
651 #define arch_xhdr(bfd) \
652 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
654 /* XCOFF archives do not have anything which corresponds to an
655 extended name table. */
657 #define xcoff_slurp_extended_name_table bfd_false
658 #define xcoff_construct_extended_name_table \
659 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
661 #define xcoff_truncate_arname bfd_dont_truncate_arname
663 /* XCOFF archives do not have a timestamp. */
665 #define xcoff_update_armap_timestamp bfd_true
667 /* Read in the armap of an XCOFF archive. */
670 xcoff_slurp_armap (abfd
)
674 struct xcoff_ar_hdr hdr
;
677 bfd_byte
*contents
, *cend
;
682 if (xcoff_ardata (abfd
) == NULL
)
684 bfd_has_map (abfd
) = false;
688 off
= strtol (xcoff_ardata (abfd
)->symoff
, (char **) NULL
, 10);
691 bfd_has_map (abfd
) = false;
695 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
698 /* The symbol table starts with a normal archive header. */
699 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
)
702 /* Skip the name (normally empty). */
703 namlen
= strtol (hdr
.namlen
, (char **) NULL
, 10);
704 if (bfd_seek (abfd
, ((namlen
+ 1) & ~1) + SXCOFFARFMAG
, SEEK_CUR
) != 0)
707 /* Read in the entire symbol table. */
708 sz
= strtol (hdr
.size
, (char **) NULL
, 10);
709 contents
= (bfd_byte
*) bfd_alloc (abfd
, sz
);
710 if (contents
== NULL
)
712 bfd_set_error (bfd_error_no_memory
);
715 if (bfd_read ((PTR
) contents
, 1, sz
, abfd
) != sz
)
718 /* The symbol table starts with a four byte count. */
719 c
= bfd_h_get_32 (abfd
, contents
);
723 bfd_set_error (bfd_error_bad_value
);
727 bfd_ardata (abfd
)->symdefs
= ((carsym
*)
728 bfd_alloc (abfd
, c
* sizeof (carsym
)));
729 if (bfd_ardata (abfd
)->symdefs
== NULL
)
731 bfd_set_error (bfd_error_no_memory
);
735 /* After the count comes a list of four byte file offsets. */
736 for (i
= 0, arsym
= bfd_ardata (abfd
)->symdefs
, p
= contents
+ 4;
738 ++i
, ++arsym
, p
+= 4)
739 arsym
->file_offset
= bfd_h_get_32 (abfd
, p
);
741 /* After the file offsets come null terminated symbol names. */
742 cend
= contents
+ sz
;
743 for (i
= 0, arsym
= bfd_ardata (abfd
)->symdefs
;
745 ++i
, ++arsym
, p
+= strlen ((char *) p
) + 1)
749 bfd_set_error (bfd_error_bad_value
);
752 arsym
->name
= (char *) p
;
755 bfd_ardata (abfd
)->symdef_count
= c
;
756 bfd_has_map (abfd
) = true;
761 /* See if this is an XCOFF archive. */
763 static const bfd_target
*
764 xcoff_archive_p (abfd
)
767 struct xcoff_ar_file_hdr hdr
;
769 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_FILE_HDR
, 1, abfd
)
770 != SIZEOF_AR_FILE_HDR
)
772 if (bfd_get_error () != bfd_error_system_call
)
773 bfd_set_error (bfd_error_wrong_format
);
777 if (strncmp (hdr
.magic
, XCOFFARMAG
, SXCOFFARMAG
) != 0)
779 bfd_set_error (bfd_error_wrong_format
);
783 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
784 involves a cast, we can't do it as the left operand of
786 abfd
->tdata
.aout_ar_data
=
787 (struct artdata
*) bfd_zalloc (abfd
, sizeof (struct artdata
));
789 if (bfd_ardata (abfd
) == (struct artdata
*) NULL
)
791 bfd_set_error (bfd_error_no_memory
);
795 bfd_ardata (abfd
)->first_file_filepos
= strtol (hdr
.firstmemoff
,
797 bfd_ardata (abfd
)->cache
= NULL
;
798 bfd_ardata (abfd
)->archive_head
= NULL
;
799 bfd_ardata (abfd
)->symdefs
= NULL
;
800 bfd_ardata (abfd
)->extended_names
= NULL
;
802 bfd_ardata (abfd
)->tdata
= bfd_zalloc (abfd
, SIZEOF_AR_FILE_HDR
);
803 if (bfd_ardata (abfd
)->tdata
== NULL
)
805 bfd_set_error (bfd_error_no_memory
);
809 memcpy (bfd_ardata (abfd
)->tdata
, &hdr
, SIZEOF_AR_FILE_HDR
);
811 if (! xcoff_slurp_armap (abfd
))
813 bfd_release (abfd
, bfd_ardata (abfd
));
814 abfd
->tdata
.aout_ar_data
= (struct artdata
*) NULL
;
821 /* Read the archive header in an XCOFF archive. */
824 xcoff_read_ar_hdr (abfd
)
827 struct xcoff_ar_hdr hdr
;
829 struct xcoff_ar_hdr
*hdrp
;
830 struct areltdata
*ret
;
832 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
)
835 namlen
= strtol (hdr
.namlen
, (char **) NULL
, 10);
836 hdrp
= bfd_alloc (abfd
, SIZEOF_AR_HDR
+ namlen
+ 1);
839 bfd_set_error (bfd_error_no_memory
);
842 memcpy (hdrp
, &hdr
, SIZEOF_AR_HDR
);
843 if (bfd_read ((char *) hdrp
+ SIZEOF_AR_HDR
, 1, namlen
, abfd
) != namlen
)
845 ((char *) hdrp
)[SIZEOF_AR_HDR
+ namlen
] = '\0';
847 ret
= (struct areltdata
*) bfd_alloc (abfd
, sizeof (struct areltdata
));
850 bfd_set_error (bfd_error_no_memory
);
853 ret
->arch_header
= (char *) hdrp
;
854 ret
->parsed_size
= strtol (hdr
.size
, (char **) NULL
, 10);
855 ret
->filename
= (char *) hdrp
+ SIZEOF_AR_HDR
;
857 /* Skip over the XCOFFARFMAG at the end of the file name. */
858 if (bfd_seek (abfd
, (namlen
& 1) + SXCOFFARFMAG
, SEEK_CUR
) != 0)
864 /* Open the next element in an XCOFF archive. */
867 xcoff_openr_next_archived_file (archive
, last_file
)
873 if (xcoff_ardata (archive
) == NULL
)
875 bfd_set_error (bfd_error_invalid_operation
);
879 if (last_file
== NULL
)
880 filestart
= bfd_ardata (archive
)->first_file_filepos
;
882 filestart
= strtol (arch_xhdr (last_file
)->nextoff
, (char **) NULL
, 10);
885 || filestart
== strtol (xcoff_ardata (archive
)->memoff
,
887 || filestart
== strtol (xcoff_ardata (archive
)->symoff
,
890 bfd_set_error (bfd_error_no_more_archived_files
);
894 return _bfd_get_elt_at_filepos (archive
, filestart
);
897 /* Stat an element in an XCOFF archive. */
900 xcoff_generic_stat_arch_elt (abfd
, s
)
904 struct xcoff_ar_hdr
*hdrp
;
906 if (abfd
->arelt_data
== NULL
)
908 bfd_set_error (bfd_error_invalid_operation
);
912 hdrp
= arch_xhdr (abfd
);
914 s
->st_mtime
= strtol (hdrp
->date
, (char **) NULL
, 10);
915 s
->st_uid
= strtol (hdrp
->uid
, (char **) NULL
, 10);
916 s
->st_gid
= strtol (hdrp
->gid
, (char **) NULL
, 10);
917 s
->st_mode
= strtol (hdrp
->mode
, (char **) NULL
, 8);
918 s
->st_size
= arch_eltdata (abfd
)->parsed_size
;
923 /* Normalize a file name for inclusion in an archive. */
926 normalize_filename (abfd
)
930 const char *filename
;
932 file
= bfd_get_filename (abfd
);
933 filename
= strrchr (file
, '/');
934 if (filename
!= NULL
)
941 /* Write out an XCOFF armap. */
945 xcoff_write_armap (abfd
, elength
, map
, orl_count
, stridx
)
947 unsigned int elength
;
949 unsigned int orl_count
;
952 struct xcoff_ar_hdr hdr
;
954 unsigned char buf
[4];
959 memset (&hdr
, 0, sizeof hdr
);
960 sprintf (hdr
.size
, "%ld", (long) (4 + orl_count
* 4 + stridx
));
961 sprintf (hdr
.nextoff
, "%d", 0);
962 memcpy (hdr
.prevoff
, xcoff_ardata (abfd
)->memoff
, 12);
963 sprintf (hdr
.date
, "%d", 0);
964 sprintf (hdr
.uid
, "%d", 0);
965 sprintf (hdr
.gid
, "%d", 0);
966 sprintf (hdr
.mode
, "%d", 0);
967 sprintf (hdr
.namlen
, "%d", 0);
969 /* We need spaces, not null bytes, in the header. */
970 for (p
= (char *) &hdr
; p
< (char *) &hdr
+ SIZEOF_AR_HDR
; p
++)
974 if (bfd_write ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
975 || bfd_write (XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
) != SXCOFFARFMAG
)
978 bfd_h_put_32 (abfd
, orl_count
, buf
);
979 if (bfd_write (buf
, 1, 4, abfd
) != 4)
982 sub
= abfd
->archive_head
;
983 fileoff
= SIZEOF_AR_FILE_HDR
;
985 while (sub
!= NULL
&& i
< orl_count
)
989 while (((bfd
*) (map
[i
]).pos
) == sub
)
991 bfd_h_put_32 (abfd
, fileoff
, buf
);
992 if (bfd_write (buf
, 1, 4, abfd
) != 4)
996 namlen
= strlen (normalize_filename (sub
));
997 namlen
= (namlen
+ 1) &~ 1;
998 fileoff
+= (SIZEOF_AR_HDR
1001 + arelt_size (sub
));
1002 fileoff
= (fileoff
+ 1) &~ 1;
1006 for (i
= 0; i
< orl_count
; i
++)
1011 name
= *map
[i
].name
;
1012 namlen
= strlen (name
);
1013 if (bfd_write (name
, 1, namlen
+ 1, abfd
) != namlen
+ 1)
1017 if ((stridx
& 1) != 0)
1022 if (bfd_write (&b
, 1, 1, abfd
) != 1)
1029 /* Write out an XCOFF archive. We always write an entire archive,
1030 rather than fussing with the freelist and so forth. */
1033 xcoff_write_archive_contents (abfd
)
1036 struct xcoff_ar_file_hdr fhdr
;
1038 size_t total_namlen
;
1042 file_ptr prevoff
, nextoff
;
1045 struct xcoff_ar_hdr ahdr
;
1050 memset (&fhdr
, 0, sizeof fhdr
);
1051 strncpy (fhdr
.magic
, XCOFFARMAG
, SXCOFFARMAG
);
1052 sprintf (fhdr
.firstmemoff
, "%d", SIZEOF_AR_FILE_HDR
);
1053 sprintf (fhdr
.freeoff
, "%d", 0);
1057 for (sub
= abfd
->archive_head
; sub
!= NULL
; sub
= sub
->next
)
1060 total_namlen
+= strlen (normalize_filename (sub
)) + 1;
1062 offsets
= (file_ptr
*) bfd_alloc (abfd
, count
* sizeof (file_ptr
));
1063 if (offsets
== NULL
)
1065 bfd_set_error (bfd_error_no_memory
);
1069 if (bfd_seek (abfd
, SIZEOF_AR_FILE_HDR
, SEEK_SET
) != 0)
1072 makemap
= bfd_has_map (abfd
);
1075 nextoff
= SIZEOF_AR_FILE_HDR
;
1076 for (sub
= abfd
->archive_head
, i
= 0; sub
!= NULL
; sub
= sub
->next
, i
++)
1080 struct xcoff_ar_hdr
*ahdrp
;
1081 bfd_size_type remaining
;
1083 if (makemap
&& ! hasobjects
)
1085 if (bfd_check_format (sub
, bfd_object
))
1089 name
= normalize_filename (sub
);
1090 namlen
= strlen (name
);
1092 if (sub
->arelt_data
!= NULL
)
1093 ahdrp
= arch_xhdr (sub
);
1101 memset (&ahdr
, 0, sizeof ahdr
);
1103 if (stat (bfd_get_filename (sub
), &s
) != 0)
1105 bfd_set_error (bfd_error_system_call
);
1109 sprintf (ahdrp
->size
, "%ld", (long) s
.st_size
);
1110 sprintf (ahdrp
->date
, "%ld", (long) s
.st_mtime
);
1111 sprintf (ahdrp
->uid
, "%ld", (long) s
.st_uid
);
1112 sprintf (ahdrp
->gid
, "%ld", (long) s
.st_gid
);
1113 sprintf (ahdrp
->mode
, "%o", (unsigned int) s
.st_mode
);
1115 if (sub
->arelt_data
== NULL
)
1117 sub
->arelt_data
= ((struct areltdata
*)
1118 bfd_alloc (sub
, sizeof (struct areltdata
)));
1119 if (sub
->arelt_data
== NULL
)
1121 bfd_set_error (bfd_error_no_memory
);
1126 arch_eltdata (sub
)->parsed_size
= s
.st_size
;
1129 sprintf (ahdrp
->prevoff
, "%ld", (long) prevoff
);
1130 sprintf (ahdrp
->namlen
, "%ld", (long) namlen
);
1132 /* If the length of the name is odd, we write out the null byte
1133 after the name as well. */
1134 namlen
= (namlen
+ 1) &~ 1;
1136 remaining
= arelt_size (sub
);
1137 size
= (SIZEOF_AR_HDR
1142 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1144 offsets
[i
] = nextoff
;
1147 nextoff
+= size
+ (size
& 1);
1149 sprintf (ahdrp
->nextoff
, "%ld", (long) nextoff
);
1151 /* We need spaces, not null bytes, in the header. */
1152 for (p
= (char *) ahdrp
; p
< (char *) ahdrp
+ SIZEOF_AR_HDR
; p
++)
1156 if (bfd_write ((PTR
) ahdrp
, 1, SIZEOF_AR_HDR
, abfd
) != SIZEOF_AR_HDR
1157 || bfd_write ((PTR
) name
, 1, namlen
, abfd
) != namlen
1158 || (bfd_write ((PTR
) XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
)
1162 if (bfd_seek (sub
, (file_ptr
) 0, SEEK_SET
) != 0)
1164 while (remaining
!= 0)
1167 bfd_byte buffer
[DEFAULT_BUFFERSIZE
];
1169 amt
= sizeof buffer
;
1170 if (amt
> remaining
)
1172 if (bfd_read (buffer
, 1, amt
, sub
) != amt
1173 || bfd_write (buffer
, 1, amt
, abfd
) != amt
)
1178 if ((size
& 1) != 0)
1183 if (bfd_write (&b
, 1, 1, abfd
) != 1)
1188 sprintf (fhdr
.lastmemoff
, "%ld", (long) prevoff
);
1190 /* Write out the member table. */
1192 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1193 sprintf (fhdr
.memoff
, "%ld", (long) nextoff
);
1195 memset (&ahdr
, 0, sizeof ahdr
);
1196 sprintf (ahdr
.size
, "%ld", (long) (12 + count
* 12 + total_namlen
));
1197 sprintf (ahdr
.prevoff
, "%ld", (long) prevoff
);
1198 sprintf (ahdr
.date
, "%d", 0);
1199 sprintf (ahdr
.uid
, "%d", 0);
1200 sprintf (ahdr
.gid
, "%d", 0);
1201 sprintf (ahdr
.mode
, "%d", 0);
1202 sprintf (ahdr
.namlen
, "%d", 0);
1204 size
= (SIZEOF_AR_HDR
1211 nextoff
+= size
+ (size
& 1);
1213 if (makemap
&& hasobjects
)
1214 sprintf (ahdr
.nextoff
, "%ld", (long) nextoff
);
1216 sprintf (ahdr
.nextoff
, "%d", 0);
1218 /* We need spaces, not null bytes, in the header. */
1219 for (p
= (char *) &ahdr
; p
< (char *) &ahdr
+ SIZEOF_AR_HDR
; p
++)
1223 if (bfd_write ((PTR
) &ahdr
, 1, SIZEOF_AR_HDR
, abfd
) != SIZEOF_AR_HDR
1224 || (bfd_write ((PTR
) XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
)
1228 sprintf (decbuf
, "%-12ld", (long) count
);
1229 if (bfd_write ((PTR
) decbuf
, 1, 12, abfd
) != 12)
1231 for (i
= 0; i
< count
; i
++)
1233 sprintf (decbuf
, "%-12ld", (long) offsets
[i
]);
1234 if (bfd_write ((PTR
) decbuf
, 1, 12, abfd
) != 12)
1237 for (sub
= abfd
->archive_head
; sub
!= NULL
; sub
= sub
->next
)
1242 name
= normalize_filename (sub
);
1243 namlen
= strlen (name
);
1244 if (bfd_write ((PTR
) name
, 1, namlen
+ 1, abfd
) != namlen
+ 1)
1247 if ((size
& 1) != 0)
1252 if (bfd_write ((PTR
) &b
, 1, 1, abfd
) != 1)
1256 /* Write out the armap, if appropriate. */
1258 if (! makemap
|| ! hasobjects
)
1259 sprintf (fhdr
.symoff
, "%d", 0);
1262 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1263 sprintf (fhdr
.symoff
, "%ld", (long) nextoff
);
1264 bfd_ardata (abfd
)->tdata
= (PTR
) &fhdr
;
1265 if (! _bfd_compute_and_write_armap (abfd
, 0))
1269 /* Write out the archive file header. */
1271 /* We need spaces, not null bytes, in the header. */
1272 for (p
= (char *) &fhdr
; p
< (char *) &fhdr
+ SIZEOF_AR_FILE_HDR
; p
++)
1276 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
1277 || (bfd_write ((PTR
) &fhdr
, SIZEOF_AR_FILE_HDR
, 1, abfd
) !=
1278 SIZEOF_AR_FILE_HDR
))
1284 #define CORE_FILE_P _bfd_dummy_target
1286 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
1287 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
1288 #define coff_core_file_matches_executable_p \
1289 _bfd_nocore_core_file_matches_executable_p
1293 #define CORE_FILE_P rs6000coff_core_p
1294 extern const bfd_target
* rs6000coff_core_p ();
1295 extern boolean
rs6000coff_get_section_contents ();
1296 extern boolean
rs6000coff_core_file_matches_executable_p ();
1298 #undef coff_core_file_matches_executable_p
1299 #define coff_core_file_matches_executable_p \
1300 rs6000coff_core_file_matches_executable_p
1302 extern char *rs6000coff_core_file_failing_command
PARAMS ((bfd
*abfd
));
1303 #undef coff_core_file_failing_command
1304 #define coff_core_file_failing_command rs6000coff_core_file_failing_command
1306 extern int rs6000coff_core_file_failing_signal
PARAMS ((bfd
*abfd
));
1307 #undef coff_core_file_failing_signal
1308 #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
1310 #undef coff_get_section_contents
1311 #define coff_get_section_contents rs6000coff_get_section_contents
1312 #endif /* AIX_CORE */
1317 #define CORE_FILE_P lynx_core_file_p
1318 extern const bfd_target
*lynx_core_file_p
PARAMS ((bfd
*abfd
));
1320 extern boolean lynx_core_file_matches_executable_p
PARAMS ((bfd
*core_bfd
,
1322 #undef coff_core_file_matches_executable_p
1323 #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
1325 extern char *lynx_core_file_failing_command
PARAMS ((bfd
*abfd
));
1326 #undef coff_core_file_failing_command
1327 #define coff_core_file_failing_command lynx_core_file_failing_command
1329 extern int lynx_core_file_failing_signal
PARAMS ((bfd
*abfd
));
1330 #undef coff_core_file_failing_signal
1331 #define coff_core_file_failing_signal lynx_core_file_failing_signal
1333 #endif /* LYNX_CORE */
1335 #define _bfd_xcoff_sizeof_headers coff_sizeof_headers
1336 #define _bfd_xcoff_bfd_get_relocated_section_contents \
1337 coff_bfd_get_relocated_section_contents
1338 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
1339 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
1341 /* The transfer vector that leads the outside world to all of the above. */
1343 const bfd_target rs6000coff_vec
=
1345 "aixcoff-rs6000", /* name */
1346 bfd_target_coff_flavour
,
1347 true, /* data byte order is big */
1348 true, /* header byte order is big */
1350 (HAS_RELOC
| EXEC_P
| /* object flags */
1351 HAS_LINENO
| HAS_DEBUG
| DYNAMIC
|
1352 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
1354 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1355 0, /* leading char */
1356 '/', /* ar_pad_char */
1357 15, /* ar_max_namelen??? FIXMEmgo */
1359 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1360 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1361 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1362 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1363 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1364 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1366 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
1367 xcoff_archive_p
, CORE_FILE_P
},
1368 {bfd_false
, coff_mkobject
, /* bfd_set_format */
1369 _bfd_generic_mkarchive
, bfd_false
},
1370 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
1371 xcoff_write_archive_contents
, bfd_false
},
1373 BFD_JUMP_TABLE_GENERIC (coff
),
1374 BFD_JUMP_TABLE_COPY (coff
),
1375 BFD_JUMP_TABLE_CORE (coff
),
1376 BFD_JUMP_TABLE_ARCHIVE (xcoff
),
1377 BFD_JUMP_TABLE_SYMBOLS (coff
),
1378 BFD_JUMP_TABLE_RELOCS (coff
),
1379 BFD_JUMP_TABLE_WRITE (coff
),
1380 BFD_JUMP_TABLE_LINK (_bfd_xcoff
),
1381 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),