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];
510 return &xcoff_howto_table
[0];
516 #define SELECT_RELOC(internal, howto) \
518 internal.r_type = howto->type; \
520 ((howto->complain_on_overflow == complain_overflow_signed \
523 | (howto->bitsize - 1)); \
526 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
528 #define COFF_LONG_FILENAMES
530 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
532 #define coff_mkobject xcoff_mkobject
533 #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data
534 #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup
535 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
537 #include "coffcode.h"
539 /* XCOFF archive support. The original version of this code was by
540 Damon A. Permezel. It was enhanced to permit cross support, and
541 writing archive files, by Ian Lance Taylor, Cygnus Support.
543 XCOFF uses its own archive format. Everything is hooked together
544 with file offset links, so it is possible to rapidly update an
545 archive in place. Of course, we don't do that. An XCOFF archive
546 has a real file header, not just an ARMAG string. The structure of
547 the file header and of each archive header appear below.
549 An XCOFF archive also has a member table, which is a list of
550 elements in the archive (you can get that by looking through the
551 linked list, but you have to read a lot more of the file). The
552 member table has a normal archive header with an empty name. It is
553 normally (and perhaps must be) the second to last entry in the
554 archive. The member table data is almost printable ASCII. It
555 starts with a 12 character decimal string which is the number of
556 entries in the table. For each entry it has a 12 character decimal
557 string which is the offset in the archive of that member. These
558 entries are followed by a series of null terminated strings which
559 are the member names for each entry.
561 Finally, an XCOFF archive has a global symbol table, which is what
562 we call the armap. The global symbol table has a normal archive
563 header with an empty name. It is normally (and perhaps must be)
564 the last entry in the archive. The contents start with a four byte
565 binary number which is the number of entries. This is followed by
566 a that many four byte binary numbers; each is the file offset of an
567 entry in the archive. These numbers are followed by a series of
568 null terminated strings, which are symbol names. */
570 /* XCOFF archives use this as a magic string. */
572 #define XCOFFARMAG "<aiaff>\012"
573 #define SXCOFFARMAG 8
575 /* This terminates an XCOFF archive member name. */
577 #define XCOFFARFMAG "`\012"
578 #define SXCOFFARFMAG 2
580 /* XCOFF archives start with this (printable) structure. */
582 struct xcoff_ar_file_hdr
585 char magic
[SXCOFFARMAG
];
587 /* Offset of the member table (decimal ASCII string). */
590 /* Offset of the global symbol table (decimal ASCII string). */
593 /* Offset of the first member in the archive (decimal ASCII string). */
594 char firstmemoff
[12];
596 /* Offset of the last member in the archive (decimal ASCII string). */
599 /* Offset of the first member on the free list (decimal ASCII
604 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
606 /* Each XCOFF archive member starts with this (printable) structure. */
610 /* File size not including the header (decimal ASCII string). */
613 /* File offset of next archive member (decimal ASCII string). */
616 /* File offset of previous archive member (decimal ASCII string). */
619 /* File mtime (decimal ASCII string). */
622 /* File UID (decimal ASCII string). */
625 /* File GID (decimal ASCII string). */
628 /* File mode (octal ASCII string). */
631 /* Length of file name (decimal ASCII string). */
634 /* This structure is followed by the file name. The length of the
635 name is given in the namlen field. If the length of the name is
636 odd, the name is followed by a null byte. The name and optional
637 null byte are followed by XCOFFARFMAG, which is not included in
638 namlen. The contents of the archive member follow; the number of
639 bytes is given in the size field. */
642 #define SIZEOF_AR_HDR (7 * 12 + 4)
644 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
645 artdata structure. */
646 #define xcoff_ardata(abfd) \
647 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
649 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
651 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
652 #define arch_xhdr(bfd) \
653 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
655 /* XCOFF archives do not have anything which corresponds to an
656 extended name table. */
658 #define xcoff_slurp_extended_name_table bfd_false
659 #define xcoff_construct_extended_name_table \
660 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
662 #define xcoff_truncate_arname bfd_dont_truncate_arname
664 /* XCOFF archives do not have a timestamp. */
666 #define xcoff_update_armap_timestamp bfd_true
668 /* Read in the armap of an XCOFF archive. */
671 xcoff_slurp_armap (abfd
)
675 struct xcoff_ar_hdr hdr
;
678 bfd_byte
*contents
, *cend
;
683 if (xcoff_ardata (abfd
) == NULL
)
685 bfd_has_map (abfd
) = false;
689 off
= strtol (xcoff_ardata (abfd
)->symoff
, (char **) NULL
, 10);
692 bfd_has_map (abfd
) = false;
696 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
699 /* The symbol table starts with a normal archive header. */
700 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
)
703 /* Skip the name (normally empty). */
704 namlen
= strtol (hdr
.namlen
, (char **) NULL
, 10);
705 if (bfd_seek (abfd
, ((namlen
+ 1) & ~1) + SXCOFFARFMAG
, SEEK_CUR
) != 0)
708 /* Read in the entire symbol table. */
709 sz
= strtol (hdr
.size
, (char **) NULL
, 10);
710 contents
= (bfd_byte
*) bfd_alloc (abfd
, sz
);
711 if (contents
== NULL
)
713 bfd_set_error (bfd_error_no_memory
);
716 if (bfd_read ((PTR
) contents
, 1, sz
, abfd
) != sz
)
719 /* The symbol table starts with a four byte count. */
720 c
= bfd_h_get_32 (abfd
, contents
);
724 bfd_set_error (bfd_error_bad_value
);
728 bfd_ardata (abfd
)->symdefs
= ((carsym
*)
729 bfd_alloc (abfd
, c
* sizeof (carsym
)));
730 if (bfd_ardata (abfd
)->symdefs
== NULL
)
732 bfd_set_error (bfd_error_no_memory
);
736 /* After the count comes a list of four byte file offsets. */
737 for (i
= 0, arsym
= bfd_ardata (abfd
)->symdefs
, p
= contents
+ 4;
739 ++i
, ++arsym
, p
+= 4)
740 arsym
->file_offset
= bfd_h_get_32 (abfd
, p
);
742 /* After the file offsets come null terminated symbol names. */
743 cend
= contents
+ sz
;
744 for (i
= 0, arsym
= bfd_ardata (abfd
)->symdefs
;
746 ++i
, ++arsym
, p
+= strlen ((char *) p
) + 1)
750 bfd_set_error (bfd_error_bad_value
);
753 arsym
->name
= (char *) p
;
756 bfd_ardata (abfd
)->symdef_count
= c
;
757 bfd_has_map (abfd
) = true;
762 /* See if this is an XCOFF archive. */
764 static const bfd_target
*
765 xcoff_archive_p (abfd
)
768 struct xcoff_ar_file_hdr hdr
;
770 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_FILE_HDR
, 1, abfd
)
771 != SIZEOF_AR_FILE_HDR
)
773 if (bfd_get_error () != bfd_error_system_call
)
774 bfd_set_error (bfd_error_wrong_format
);
778 if (strncmp (hdr
.magic
, XCOFFARMAG
, SXCOFFARMAG
) != 0)
780 bfd_set_error (bfd_error_wrong_format
);
784 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
785 involves a cast, we can't do it as the left operand of
787 abfd
->tdata
.aout_ar_data
=
788 (struct artdata
*) bfd_zalloc (abfd
, sizeof (struct artdata
));
790 if (bfd_ardata (abfd
) == (struct artdata
*) NULL
)
792 bfd_set_error (bfd_error_no_memory
);
796 bfd_ardata (abfd
)->first_file_filepos
= strtol (hdr
.firstmemoff
,
798 bfd_ardata (abfd
)->cache
= NULL
;
799 bfd_ardata (abfd
)->archive_head
= NULL
;
800 bfd_ardata (abfd
)->symdefs
= NULL
;
801 bfd_ardata (abfd
)->extended_names
= NULL
;
803 bfd_ardata (abfd
)->tdata
= bfd_zalloc (abfd
, SIZEOF_AR_FILE_HDR
);
804 if (bfd_ardata (abfd
)->tdata
== NULL
)
806 bfd_set_error (bfd_error_no_memory
);
810 memcpy (bfd_ardata (abfd
)->tdata
, &hdr
, SIZEOF_AR_FILE_HDR
);
812 if (! xcoff_slurp_armap (abfd
))
814 bfd_release (abfd
, bfd_ardata (abfd
));
815 abfd
->tdata
.aout_ar_data
= (struct artdata
*) NULL
;
822 /* Read the archive header in an XCOFF archive. */
825 xcoff_read_ar_hdr (abfd
)
828 struct xcoff_ar_hdr hdr
;
830 struct xcoff_ar_hdr
*hdrp
;
831 struct areltdata
*ret
;
833 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
)
836 namlen
= strtol (hdr
.namlen
, (char **) NULL
, 10);
837 hdrp
= bfd_alloc (abfd
, SIZEOF_AR_HDR
+ namlen
+ 1);
840 bfd_set_error (bfd_error_no_memory
);
843 memcpy (hdrp
, &hdr
, SIZEOF_AR_HDR
);
844 if (bfd_read ((char *) hdrp
+ SIZEOF_AR_HDR
, 1, namlen
, abfd
) != namlen
)
846 ((char *) hdrp
)[SIZEOF_AR_HDR
+ namlen
] = '\0';
848 ret
= (struct areltdata
*) bfd_alloc (abfd
, sizeof (struct areltdata
));
851 bfd_set_error (bfd_error_no_memory
);
854 ret
->arch_header
= (char *) hdrp
;
855 ret
->parsed_size
= strtol (hdr
.size
, (char **) NULL
, 10);
856 ret
->filename
= (char *) hdrp
+ SIZEOF_AR_HDR
;
858 /* Skip over the XCOFFARFMAG at the end of the file name. */
859 if (bfd_seek (abfd
, (namlen
& 1) + SXCOFFARFMAG
, SEEK_CUR
) != 0)
865 /* Open the next element in an XCOFF archive. */
868 xcoff_openr_next_archived_file (archive
, last_file
)
874 if (xcoff_ardata (archive
) == NULL
)
876 bfd_set_error (bfd_error_invalid_operation
);
880 if (last_file
== NULL
)
881 filestart
= bfd_ardata (archive
)->first_file_filepos
;
883 filestart
= strtol (arch_xhdr (last_file
)->nextoff
, (char **) NULL
, 10);
886 || filestart
== strtol (xcoff_ardata (archive
)->memoff
,
888 || filestart
== strtol (xcoff_ardata (archive
)->symoff
,
891 bfd_set_error (bfd_error_no_more_archived_files
);
895 return _bfd_get_elt_at_filepos (archive
, filestart
);
898 /* Stat an element in an XCOFF archive. */
901 xcoff_generic_stat_arch_elt (abfd
, s
)
905 struct xcoff_ar_hdr
*hdrp
;
907 if (abfd
->arelt_data
== NULL
)
909 bfd_set_error (bfd_error_invalid_operation
);
913 hdrp
= arch_xhdr (abfd
);
915 s
->st_mtime
= strtol (hdrp
->date
, (char **) NULL
, 10);
916 s
->st_uid
= strtol (hdrp
->uid
, (char **) NULL
, 10);
917 s
->st_gid
= strtol (hdrp
->gid
, (char **) NULL
, 10);
918 s
->st_mode
= strtol (hdrp
->mode
, (char **) NULL
, 8);
919 s
->st_size
= arch_eltdata (abfd
)->parsed_size
;
924 /* Normalize a file name for inclusion in an archive. */
927 normalize_filename (abfd
)
931 const char *filename
;
933 file
= bfd_get_filename (abfd
);
934 filename
= strrchr (file
, '/');
935 if (filename
!= NULL
)
942 /* Write out an XCOFF armap. */
946 xcoff_write_armap (abfd
, elength
, map
, orl_count
, stridx
)
948 unsigned int elength
;
950 unsigned int orl_count
;
953 struct xcoff_ar_hdr hdr
;
955 unsigned char buf
[4];
960 memset (&hdr
, 0, sizeof hdr
);
961 sprintf (hdr
.size
, "%ld", (long) (4 + orl_count
* 4 + stridx
));
962 sprintf (hdr
.nextoff
, "%d", 0);
963 memcpy (hdr
.prevoff
, xcoff_ardata (abfd
)->memoff
, 12);
964 sprintf (hdr
.date
, "%d", 0);
965 sprintf (hdr
.uid
, "%d", 0);
966 sprintf (hdr
.gid
, "%d", 0);
967 sprintf (hdr
.mode
, "%d", 0);
968 sprintf (hdr
.namlen
, "%d", 0);
970 /* We need spaces, not null bytes, in the header. */
971 for (p
= (char *) &hdr
; p
< (char *) &hdr
+ SIZEOF_AR_HDR
; p
++)
975 if (bfd_write ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
976 || bfd_write (XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
) != SXCOFFARFMAG
)
979 bfd_h_put_32 (abfd
, orl_count
, buf
);
980 if (bfd_write (buf
, 1, 4, abfd
) != 4)
983 sub
= abfd
->archive_head
;
984 fileoff
= SIZEOF_AR_FILE_HDR
;
986 while (sub
!= NULL
&& i
< orl_count
)
990 while (((bfd
*) (map
[i
]).pos
) == sub
)
992 bfd_h_put_32 (abfd
, fileoff
, buf
);
993 if (bfd_write (buf
, 1, 4, abfd
) != 4)
997 namlen
= strlen (normalize_filename (sub
));
998 namlen
= (namlen
+ 1) &~ 1;
999 fileoff
+= (SIZEOF_AR_HDR
1002 + arelt_size (sub
));
1003 fileoff
= (fileoff
+ 1) &~ 1;
1007 for (i
= 0; i
< orl_count
; i
++)
1012 name
= *map
[i
].name
;
1013 namlen
= strlen (name
);
1014 if (bfd_write (name
, 1, namlen
+ 1, abfd
) != namlen
+ 1)
1018 if ((stridx
& 1) != 0)
1023 if (bfd_write (&b
, 1, 1, abfd
) != 1)
1030 /* Write out an XCOFF archive. We always write an entire archive,
1031 rather than fussing with the freelist and so forth. */
1034 xcoff_write_archive_contents (abfd
)
1037 struct xcoff_ar_file_hdr fhdr
;
1039 size_t total_namlen
;
1043 file_ptr prevoff
, nextoff
;
1046 struct xcoff_ar_hdr ahdr
;
1051 memset (&fhdr
, 0, sizeof fhdr
);
1052 strncpy (fhdr
.magic
, XCOFFARMAG
, SXCOFFARMAG
);
1053 sprintf (fhdr
.firstmemoff
, "%d", SIZEOF_AR_FILE_HDR
);
1054 sprintf (fhdr
.freeoff
, "%d", 0);
1058 for (sub
= abfd
->archive_head
; sub
!= NULL
; sub
= sub
->next
)
1061 total_namlen
+= strlen (normalize_filename (sub
)) + 1;
1063 offsets
= (file_ptr
*) bfd_alloc (abfd
, count
* sizeof (file_ptr
));
1064 if (offsets
== NULL
)
1066 bfd_set_error (bfd_error_no_memory
);
1070 if (bfd_seek (abfd
, SIZEOF_AR_FILE_HDR
, SEEK_SET
) != 0)
1073 makemap
= bfd_has_map (abfd
);
1076 nextoff
= SIZEOF_AR_FILE_HDR
;
1077 for (sub
= abfd
->archive_head
, i
= 0; sub
!= NULL
; sub
= sub
->next
, i
++)
1081 struct xcoff_ar_hdr
*ahdrp
;
1082 bfd_size_type remaining
;
1084 if (makemap
&& ! hasobjects
)
1086 if (bfd_check_format (sub
, bfd_object
))
1090 name
= normalize_filename (sub
);
1091 namlen
= strlen (name
);
1093 if (sub
->arelt_data
!= NULL
)
1094 ahdrp
= arch_xhdr (sub
);
1102 memset (&ahdr
, 0, sizeof ahdr
);
1104 if (stat (bfd_get_filename (sub
), &s
) != 0)
1106 bfd_set_error (bfd_error_system_call
);
1110 sprintf (ahdrp
->size
, "%ld", (long) s
.st_size
);
1111 sprintf (ahdrp
->date
, "%ld", (long) s
.st_mtime
);
1112 sprintf (ahdrp
->uid
, "%ld", (long) s
.st_uid
);
1113 sprintf (ahdrp
->gid
, "%ld", (long) s
.st_gid
);
1114 sprintf (ahdrp
->mode
, "%o", (unsigned int) s
.st_mode
);
1116 if (sub
->arelt_data
== NULL
)
1118 sub
->arelt_data
= ((struct areltdata
*)
1119 bfd_alloc (sub
, sizeof (struct areltdata
)));
1120 if (sub
->arelt_data
== NULL
)
1122 bfd_set_error (bfd_error_no_memory
);
1127 arch_eltdata (sub
)->parsed_size
= s
.st_size
;
1130 sprintf (ahdrp
->prevoff
, "%ld", (long) prevoff
);
1131 sprintf (ahdrp
->namlen
, "%ld", (long) namlen
);
1133 /* If the length of the name is odd, we write out the null byte
1134 after the name as well. */
1135 namlen
= (namlen
+ 1) &~ 1;
1137 remaining
= arelt_size (sub
);
1138 size
= (SIZEOF_AR_HDR
1143 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1145 offsets
[i
] = nextoff
;
1148 nextoff
+= size
+ (size
& 1);
1150 sprintf (ahdrp
->nextoff
, "%ld", (long) nextoff
);
1152 /* We need spaces, not null bytes, in the header. */
1153 for (p
= (char *) ahdrp
; p
< (char *) ahdrp
+ SIZEOF_AR_HDR
; p
++)
1157 if (bfd_write ((PTR
) ahdrp
, 1, SIZEOF_AR_HDR
, abfd
) != SIZEOF_AR_HDR
1158 || bfd_write ((PTR
) name
, 1, namlen
, abfd
) != namlen
1159 || (bfd_write ((PTR
) XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
)
1163 if (bfd_seek (sub
, (file_ptr
) 0, SEEK_SET
) != 0)
1165 while (remaining
!= 0)
1168 bfd_byte buffer
[DEFAULT_BUFFERSIZE
];
1170 amt
= sizeof buffer
;
1171 if (amt
> remaining
)
1173 if (bfd_read (buffer
, 1, amt
, sub
) != amt
1174 || bfd_write (buffer
, 1, amt
, abfd
) != amt
)
1179 if ((size
& 1) != 0)
1184 if (bfd_write (&b
, 1, 1, abfd
) != 1)
1189 sprintf (fhdr
.lastmemoff
, "%ld", (long) prevoff
);
1191 /* Write out the member table. */
1193 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1194 sprintf (fhdr
.memoff
, "%ld", (long) nextoff
);
1196 memset (&ahdr
, 0, sizeof ahdr
);
1197 sprintf (ahdr
.size
, "%ld", (long) (12 + count
* 12 + total_namlen
));
1198 sprintf (ahdr
.prevoff
, "%ld", (long) prevoff
);
1199 sprintf (ahdr
.date
, "%d", 0);
1200 sprintf (ahdr
.uid
, "%d", 0);
1201 sprintf (ahdr
.gid
, "%d", 0);
1202 sprintf (ahdr
.mode
, "%d", 0);
1203 sprintf (ahdr
.namlen
, "%d", 0);
1205 size
= (SIZEOF_AR_HDR
1212 nextoff
+= size
+ (size
& 1);
1214 if (makemap
&& hasobjects
)
1215 sprintf (ahdr
.nextoff
, "%ld", (long) nextoff
);
1217 sprintf (ahdr
.nextoff
, "%d", 0);
1219 /* We need spaces, not null bytes, in the header. */
1220 for (p
= (char *) &ahdr
; p
< (char *) &ahdr
+ SIZEOF_AR_HDR
; p
++)
1224 if (bfd_write ((PTR
) &ahdr
, 1, SIZEOF_AR_HDR
, abfd
) != SIZEOF_AR_HDR
1225 || (bfd_write ((PTR
) XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
)
1229 sprintf (decbuf
, "%-12ld", (long) count
);
1230 if (bfd_write ((PTR
) decbuf
, 1, 12, abfd
) != 12)
1232 for (i
= 0; i
< count
; i
++)
1234 sprintf (decbuf
, "%-12ld", (long) offsets
[i
]);
1235 if (bfd_write ((PTR
) decbuf
, 1, 12, abfd
) != 12)
1238 for (sub
= abfd
->archive_head
; sub
!= NULL
; sub
= sub
->next
)
1243 name
= normalize_filename (sub
);
1244 namlen
= strlen (name
);
1245 if (bfd_write ((PTR
) name
, 1, namlen
+ 1, abfd
) != namlen
+ 1)
1248 if ((size
& 1) != 0)
1253 if (bfd_write ((PTR
) &b
, 1, 1, abfd
) != 1)
1257 /* Write out the armap, if appropriate. */
1259 if (! makemap
|| ! hasobjects
)
1260 sprintf (fhdr
.symoff
, "%d", 0);
1263 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1264 sprintf (fhdr
.symoff
, "%ld", (long) nextoff
);
1265 bfd_ardata (abfd
)->tdata
= (PTR
) &fhdr
;
1266 if (! _bfd_compute_and_write_armap (abfd
, 0))
1270 /* Write out the archive file header. */
1272 /* We need spaces, not null bytes, in the header. */
1273 for (p
= (char *) &fhdr
; p
< (char *) &fhdr
+ SIZEOF_AR_FILE_HDR
; p
++)
1277 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
1278 || (bfd_write ((PTR
) &fhdr
, SIZEOF_AR_FILE_HDR
, 1, abfd
) !=
1279 SIZEOF_AR_FILE_HDR
))
1285 #define CORE_FILE_P _bfd_dummy_target
1287 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
1288 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
1289 #define coff_core_file_matches_executable_p \
1290 _bfd_nocore_core_file_matches_executable_p
1294 #define CORE_FILE_P rs6000coff_core_p
1295 extern const bfd_target
* rs6000coff_core_p ();
1296 extern boolean
rs6000coff_get_section_contents ();
1297 extern boolean
rs6000coff_core_file_matches_executable_p ();
1299 #undef coff_core_file_matches_executable_p
1300 #define coff_core_file_matches_executable_p \
1301 rs6000coff_core_file_matches_executable_p
1303 extern char *rs6000coff_core_file_failing_command
PARAMS ((bfd
*abfd
));
1304 #undef coff_core_file_failing_command
1305 #define coff_core_file_failing_command rs6000coff_core_file_failing_command
1307 extern int rs6000coff_core_file_failing_signal
PARAMS ((bfd
*abfd
));
1308 #undef coff_core_file_failing_signal
1309 #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
1311 #undef coff_get_section_contents
1312 #define coff_get_section_contents rs6000coff_get_section_contents
1313 #endif /* AIX_CORE */
1318 #define CORE_FILE_P lynx_core_file_p
1319 extern const bfd_target
*lynx_core_file_p
PARAMS ((bfd
*abfd
));
1321 extern boolean lynx_core_file_matches_executable_p
PARAMS ((bfd
*core_bfd
,
1323 #undef coff_core_file_matches_executable_p
1324 #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
1326 extern char *lynx_core_file_failing_command
PARAMS ((bfd
*abfd
));
1327 #undef coff_core_file_failing_command
1328 #define coff_core_file_failing_command lynx_core_file_failing_command
1330 extern int lynx_core_file_failing_signal
PARAMS ((bfd
*abfd
));
1331 #undef coff_core_file_failing_signal
1332 #define coff_core_file_failing_signal lynx_core_file_failing_signal
1334 #endif /* LYNX_CORE */
1336 #define _bfd_xcoff_sizeof_headers coff_sizeof_headers
1337 #define _bfd_xcoff_bfd_get_relocated_section_contents \
1338 coff_bfd_get_relocated_section_contents
1339 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
1340 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
1342 /* The transfer vector that leads the outside world to all of the above. */
1344 const bfd_target rs6000coff_vec
=
1346 "aixcoff-rs6000", /* name */
1347 bfd_target_coff_flavour
,
1348 true, /* data byte order is big */
1349 true, /* header byte order is big */
1351 (HAS_RELOC
| EXEC_P
| /* object flags */
1352 HAS_LINENO
| HAS_DEBUG
| DYNAMIC
|
1353 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
1355 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1356 0, /* leading char */
1357 '/', /* ar_pad_char */
1358 15, /* ar_max_namelen??? FIXMEmgo */
1360 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1361 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1362 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1363 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1364 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1365 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1367 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
1368 xcoff_archive_p
, CORE_FILE_P
},
1369 {bfd_false
, coff_mkobject
, /* bfd_set_format */
1370 _bfd_generic_mkarchive
, bfd_false
},
1371 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
1372 xcoff_write_archive_contents
, bfd_false
},
1374 BFD_JUMP_TABLE_GENERIC (coff
),
1375 BFD_JUMP_TABLE_COPY (coff
),
1376 BFD_JUMP_TABLE_CORE (coff
),
1377 BFD_JUMP_TABLE_ARCHIVE (xcoff
),
1378 BFD_JUMP_TABLE_SYMBOLS (coff
),
1379 BFD_JUMP_TABLE_RELOCS (coff
),
1380 BFD_JUMP_TABLE_WRITE (coff
),
1381 BFD_JUMP_TABLE_LINK (_bfd_xcoff
),
1382 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),