1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 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
34 #include "coff/internal.h"
35 #include "coff/rs6000.h"
38 /* The main body of code is in coffcode.h. */
40 static boolean xcoff_mkobject
PARAMS ((bfd
*));
41 static boolean xcoff_copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
42 static boolean xcoff_is_local_label_name
PARAMS ((bfd
*, const char *));
43 static void xcoff_rtype2howto
44 PARAMS ((arelent
*, struct internal_reloc
*));
45 static reloc_howto_type
*xcoff_reloc_type_lookup
46 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
47 static boolean xcoff_slurp_armap
PARAMS ((bfd
*));
48 static const bfd_target
*xcoff_archive_p
PARAMS ((bfd
*));
49 static PTR xcoff_read_ar_hdr
PARAMS ((bfd
*));
50 static bfd
*xcoff_openr_next_archived_file
PARAMS ((bfd
*, bfd
*));
51 static int xcoff_generic_stat_arch_elt
PARAMS ((bfd
*, struct stat
*));
52 static const char *normalize_filename
PARAMS ((bfd
*));
53 static boolean xcoff_write_armap
54 PARAMS ((bfd
*, unsigned int, struct orl
*, unsigned int, int));
55 static boolean xcoff_write_archive_contents
PARAMS ((bfd
*));
57 /* We use our own tdata type. Its first field is the COFF tdata type,
58 so the COFF routines are compatible. */
66 abfd
->tdata
.xcoff_obj_data
=
67 ((struct xcoff_tdata
*)
68 bfd_zalloc (abfd
, sizeof (struct xcoff_tdata
)));
69 if (abfd
->tdata
.xcoff_obj_data
== NULL
)
71 coff
= coff_data (abfd
);
72 coff
->symbols
= (coff_symbol_type
*) NULL
;
73 coff
->conversion_table
= (unsigned int *) NULL
;
74 coff
->raw_syments
= (struct coff_ptr_struct
*) NULL
;
77 xcoff_data (abfd
)->modtype
= ('1' << 8) | 'L';
79 /* We set cputype to -1 to indicate that it has not been
81 xcoff_data (abfd
)->cputype
= -1;
83 xcoff_data (abfd
)->csects
= NULL
;
84 xcoff_data (abfd
)->debug_indices
= NULL
;
89 /* Copy XCOFF data from one BFD to another. */
92 xcoff_copy_private_bfd_data (ibfd
, obfd
)
96 struct xcoff_tdata
*ix
, *ox
;
99 if (ibfd
->xvec
!= obfd
->xvec
)
101 ix
= xcoff_data (ibfd
);
102 ox
= xcoff_data (obfd
);
103 ox
->full_aouthdr
= ix
->full_aouthdr
;
109 sec
= coff_section_from_bfd_index (ibfd
, ix
->sntoc
);
113 ox
->sntoc
= sec
->output_section
->target_index
;
115 if (ix
->snentry
== 0)
119 sec
= coff_section_from_bfd_index (ibfd
, ix
->snentry
);
123 ox
->snentry
= sec
->output_section
->target_index
;
125 ox
->text_align_power
= ix
->text_align_power
;
126 ox
->data_align_power
= ix
->data_align_power
;
127 ox
->modtype
= ix
->modtype
;
128 ox
->cputype
= ix
->cputype
;
129 ox
->maxdata
= ix
->maxdata
;
130 ox
->maxstack
= ix
->maxstack
;
134 /* I don't think XCOFF really has a notion of local labels based on
135 name. This will mean that ld -X doesn't actually strip anything.
136 The AIX native linker does not have a -X option, and it ignores the
140 xcoff_is_local_label_name (abfd
, name
)
147 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
148 bitsize and whether they are signed or not, along with a
149 conventional type. This table is for the types, which are used for
150 different algorithms for putting in the reloc. Many of these
151 relocs need special_function entries, which I have not written. */
153 static reloc_howto_type xcoff_howto_table
[] =
155 /* Standard 32 bit relocation. */
158 2, /* size (0 = byte, 1 = short, 2 = long) */
160 false, /* pc_relative */
162 complain_overflow_bitfield
, /* complain_on_overflow */
163 0, /* special_function */
165 true, /* partial_inplace */
166 0xffffffff, /* src_mask */
167 0xffffffff, /* dst_mask */
168 false), /* pcrel_offset */
170 /* 32 bit relocation, but store negative value. */
173 -2, /* size (0 = byte, 1 = short, 2 = long) */
175 false, /* pc_relative */
177 complain_overflow_bitfield
, /* complain_on_overflow */
178 0, /* special_function */
180 true, /* partial_inplace */
181 0xffffffff, /* src_mask */
182 0xffffffff, /* dst_mask */
183 false), /* pcrel_offset */
185 /* 32 bit PC relative relocation. */
188 2, /* size (0 = byte, 1 = short, 2 = long) */
190 true, /* pc_relative */
192 complain_overflow_signed
, /* complain_on_overflow */
193 0, /* special_function */
195 true, /* partial_inplace */
196 0xffffffff, /* src_mask */
197 0xffffffff, /* dst_mask */
198 false), /* pcrel_offset */
200 /* 16 bit TOC relative relocation. */
203 1, /* size (0 = byte, 1 = short, 2 = long) */
205 false, /* pc_relative */
207 complain_overflow_bitfield
, /* complain_on_overflow */
208 0, /* special_function */
210 true, /* partial_inplace */
211 0xffff, /* src_mask */
212 0xffff, /* dst_mask */
213 false), /* pcrel_offset */
215 /* I don't really know what this is. */
218 2, /* size (0 = byte, 1 = short, 2 = long) */
220 false, /* pc_relative */
222 complain_overflow_bitfield
, /* complain_on_overflow */
223 0, /* special_function */
225 true, /* partial_inplace */
226 0xffffffff, /* src_mask */
227 0xffffffff, /* dst_mask */
228 false), /* pcrel_offset */
230 /* External TOC relative symbol. */
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 0xffff, /* src_mask */
242 0xffff, /* dst_mask */
243 false), /* pcrel_offset */
245 /* Local TOC relative symbol. */
248 2, /* size (0 = byte, 1 = short, 2 = long) */
250 false, /* pc_relative */
252 complain_overflow_bitfield
, /* complain_on_overflow */
253 0, /* special_function */
255 true, /* partial_inplace */
256 0xffff, /* src_mask */
257 0xffff, /* dst_mask */
258 false), /* pcrel_offset */
262 /* Non modifiable absolute branch. */
265 2, /* size (0 = byte, 1 = short, 2 = long) */
267 false, /* pc_relative */
269 complain_overflow_bitfield
, /* complain_on_overflow */
270 0, /* special_function */
272 true, /* partial_inplace */
273 0x3fffffc, /* src_mask */
274 0x3fffffc, /* dst_mask */
275 false), /* pcrel_offset */
279 /* Non modifiable relative branch. */
280 HOWTO (0xa, /* type */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
284 true, /* pc_relative */
286 complain_overflow_signed
, /* complain_on_overflow */
287 0, /* special_function */
289 true, /* partial_inplace */
290 0x3fffffc, /* src_mask */
291 0x3fffffc, /* dst_mask */
292 false), /* pcrel_offset */
297 HOWTO (0xc, /* 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 true, /* partial_inplace */
307 0xffff, /* src_mask */
308 0xffff, /* dst_mask */
309 false), /* pcrel_offset */
312 HOWTO (0xd, /* type */
314 2, /* size (0 = byte, 1 = short, 2 = long) */
316 false, /* pc_relative */
318 complain_overflow_bitfield
, /* complain_on_overflow */
319 0, /* special_function */
321 true, /* partial_inplace */
322 0xffff, /* src_mask */
323 0xffff, /* dst_mask */
324 false), /* pcrel_offset */
328 /* Non-relocating reference. */
329 HOWTO (0xf, /* type */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
333 false, /* pc_relative */
335 complain_overflow_bitfield
, /* complain_on_overflow */
336 0, /* special_function */
338 false, /* partial_inplace */
341 false), /* pcrel_offset */
346 /* TOC relative indirect load. */
347 HOWTO (0x12, /* type */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
351 false, /* pc_relative */
353 complain_overflow_bitfield
, /* complain_on_overflow */
354 0, /* special_function */
356 true, /* partial_inplace */
357 0xffff, /* src_mask */
358 0xffff, /* dst_mask */
359 false), /* pcrel_offset */
361 /* TOC relative load address. */
362 HOWTO (0x13, /* type */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
366 false, /* pc_relative */
368 complain_overflow_bitfield
, /* complain_on_overflow */
369 0, /* special_function */
371 true, /* partial_inplace */
372 0xffff, /* src_mask */
373 0xffff, /* dst_mask */
374 false), /* pcrel_offset */
376 /* Modifiable relative branch. */
377 HOWTO (0x14, /* type */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
381 false, /* pc_relative */
383 complain_overflow_bitfield
, /* complain_on_overflow */
384 0, /* special_function */
385 "R_RRTBI", /* name */
386 true, /* partial_inplace */
387 0xffffffff, /* src_mask */
388 0xffffffff, /* dst_mask */
389 false), /* pcrel_offset */
391 /* Modifiable absolute branch. */
392 HOWTO (0x15, /* type */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
396 false, /* pc_relative */
398 complain_overflow_bitfield
, /* complain_on_overflow */
399 0, /* special_function */
400 "R_RRTBA", /* name */
401 true, /* partial_inplace */
402 0xffffffff, /* src_mask */
403 0xffffffff, /* dst_mask */
404 false), /* pcrel_offset */
406 /* Modifiable call absolute indirect. */
407 HOWTO (0x16, /* type */
409 2, /* size (0 = byte, 1 = short, 2 = long) */
411 false, /* pc_relative */
413 complain_overflow_bitfield
, /* complain_on_overflow */
414 0, /* special_function */
416 true, /* partial_inplace */
417 0xffff, /* src_mask */
418 0xffff, /* dst_mask */
419 false), /* pcrel_offset */
421 /* Modifiable call relative. */
422 HOWTO (0x17, /* type */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
426 false, /* pc_relative */
428 complain_overflow_bitfield
, /* complain_on_overflow */
429 0, /* special_function */
431 true, /* partial_inplace */
432 0xffff, /* src_mask */
433 0xffff, /* dst_mask */
434 false), /* pcrel_offset */
436 /* Modifiable branch absolute. */
437 HOWTO (0x18, /* type */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
441 false, /* pc_relative */
443 complain_overflow_bitfield
, /* complain_on_overflow */
444 0, /* special_function */
446 true, /* partial_inplace */
447 0xffff, /* src_mask */
448 0xffff, /* dst_mask */
449 false), /* pcrel_offset */
451 /* Modifiable branch absolute. */
452 HOWTO (0x19, /* type */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
456 false, /* pc_relative */
458 complain_overflow_bitfield
, /* complain_on_overflow */
459 0, /* special_function */
461 true, /* partial_inplace */
462 0xffff, /* src_mask */
463 0xffff, /* dst_mask */
464 false), /* pcrel_offset */
466 /* Modifiable branch relative. */
467 HOWTO (0x1a, /* type */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
471 false, /* pc_relative */
473 complain_overflow_signed
, /* complain_on_overflow */
474 0, /* special_function */
476 true, /* partial_inplace */
477 0xffff, /* src_mask */
478 0xffff, /* dst_mask */
479 false), /* pcrel_offset */
481 /* Modifiable branch absolute. */
482 HOWTO (0x1b, /* type */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
486 false, /* pc_relative */
488 complain_overflow_bitfield
, /* complain_on_overflow */
489 0, /* special_function */
491 true, /* partial_inplace */
492 0xffff, /* src_mask */
493 0xffff, /* dst_mask */
494 false) /* pcrel_offset */
498 xcoff_rtype2howto (relent
, internal
)
500 struct internal_reloc
*internal
;
502 relent
->howto
= xcoff_howto_table
+ internal
->r_type
;
504 /* The r_size field of an XCOFF reloc encodes the bitsize of the
505 relocation, as well as indicating whether it is signed or not.
506 Doublecheck that the relocation information gathered from the
507 type matches this information. */
508 if (relent
->howto
->bitsize
!= ((unsigned int) internal
->r_size
& 0x1f) + 1)
511 if ((internal
->r_size
& 0x80) != 0
512 ? (relent
->howto
->complain_on_overflow
!= complain_overflow_signed
)
513 : (relent
->howto
->complain_on_overflow
!= complain_overflow_bitfield
))
518 static reloc_howto_type
*
519 xcoff_reloc_type_lookup (abfd
, code
)
521 bfd_reloc_code_real_type code
;
525 case BFD_RELOC_PPC_B26
:
526 return &xcoff_howto_table
[0xa];
527 case BFD_RELOC_PPC_BA26
:
528 return &xcoff_howto_table
[8];
529 case BFD_RELOC_PPC_TOC16
:
530 return &xcoff_howto_table
[3];
533 return &xcoff_howto_table
[0];
539 #define SELECT_RELOC(internal, howto) \
541 internal.r_type = howto->type; \
543 ((howto->complain_on_overflow == complain_overflow_signed \
546 | (howto->bitsize - 1)); \
549 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
551 #define COFF_LONG_FILENAMES
553 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
555 #define coff_mkobject xcoff_mkobject
556 #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data
557 #define coff_bfd_is_local_label_name xcoff_is_local_label_name
558 #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup
559 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
561 #include "coffcode.h"
563 /* XCOFF archive support. The original version of this code was by
564 Damon A. Permezel. It was enhanced to permit cross support, and
565 writing archive files, by Ian Lance Taylor, Cygnus Support.
567 XCOFF uses its own archive format. Everything is hooked together
568 with file offset links, so it is possible to rapidly update an
569 archive in place. Of course, we don't do that. An XCOFF archive
570 has a real file header, not just an ARMAG string. The structure of
571 the file header and of each archive header appear below.
573 An XCOFF archive also has a member table, which is a list of
574 elements in the archive (you can get that by looking through the
575 linked list, but you have to read a lot more of the file). The
576 member table has a normal archive header with an empty name. It is
577 normally (and perhaps must be) the second to last entry in the
578 archive. The member table data is almost printable ASCII. It
579 starts with a 12 character decimal string which is the number of
580 entries in the table. For each entry it has a 12 character decimal
581 string which is the offset in the archive of that member. These
582 entries are followed by a series of null terminated strings which
583 are the member names for each entry.
585 Finally, an XCOFF archive has a global symbol table, which is what
586 we call the armap. The global symbol table has a normal archive
587 header with an empty name. It is normally (and perhaps must be)
588 the last entry in the archive. The contents start with a four byte
589 binary number which is the number of entries. This is followed by
590 a that many four byte binary numbers; each is the file offset of an
591 entry in the archive. These numbers are followed by a series of
592 null terminated strings, which are symbol names. */
594 /* XCOFF archives use this as a magic string. */
596 #define XCOFFARMAG "<aiaff>\012"
597 #define SXCOFFARMAG 8
599 /* This terminates an XCOFF archive member name. */
601 #define XCOFFARFMAG "`\012"
602 #define SXCOFFARFMAG 2
604 /* XCOFF archives start with this (printable) structure. */
606 struct xcoff_ar_file_hdr
609 char magic
[SXCOFFARMAG
];
611 /* Offset of the member table (decimal ASCII string). */
614 /* Offset of the global symbol table (decimal ASCII string). */
617 /* Offset of the first member in the archive (decimal ASCII string). */
618 char firstmemoff
[12];
620 /* Offset of the last member in the archive (decimal ASCII string). */
623 /* Offset of the first member on the free list (decimal ASCII
628 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
630 /* Each XCOFF archive member starts with this (printable) structure. */
634 /* File size not including the header (decimal ASCII string). */
637 /* File offset of next archive member (decimal ASCII string). */
640 /* File offset of previous archive member (decimal ASCII string). */
643 /* File mtime (decimal ASCII string). */
646 /* File UID (decimal ASCII string). */
649 /* File GID (decimal ASCII string). */
652 /* File mode (octal ASCII string). */
655 /* Length of file name (decimal ASCII string). */
658 /* This structure is followed by the file name. The length of the
659 name is given in the namlen field. If the length of the name is
660 odd, the name is followed by a null byte. The name and optional
661 null byte are followed by XCOFFARFMAG, which is not included in
662 namlen. The contents of the archive member follow; the number of
663 bytes is given in the size field. */
666 #define SIZEOF_AR_HDR (7 * 12 + 4)
668 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
669 artdata structure. */
670 #define xcoff_ardata(abfd) \
671 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
673 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
675 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
676 #define arch_xhdr(bfd) \
677 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
679 /* XCOFF archives do not have anything which corresponds to an
680 extended name table. */
682 #define xcoff_slurp_extended_name_table bfd_false
683 #define xcoff_construct_extended_name_table \
684 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
686 #define xcoff_truncate_arname bfd_dont_truncate_arname
688 /* We can use the standard get_elt_at_index routine. */
690 #define xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
692 /* XCOFF archives do not have a timestamp. */
694 #define xcoff_update_armap_timestamp bfd_true
696 /* Read in the armap of an XCOFF archive. */
699 xcoff_slurp_armap (abfd
)
703 struct xcoff_ar_hdr hdr
;
706 bfd_byte
*contents
, *cend
;
711 if (xcoff_ardata (abfd
) == NULL
)
713 bfd_has_map (abfd
) = false;
717 off
= strtol (xcoff_ardata (abfd
)->symoff
, (char **) NULL
, 10);
720 bfd_has_map (abfd
) = false;
724 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
727 /* The symbol table starts with a normal archive header. */
728 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
)
731 /* Skip the name (normally empty). */
732 namlen
= strtol (hdr
.namlen
, (char **) NULL
, 10);
733 if (bfd_seek (abfd
, ((namlen
+ 1) & ~1) + SXCOFFARFMAG
, SEEK_CUR
) != 0)
736 /* Read in the entire symbol table. */
737 sz
= strtol (hdr
.size
, (char **) NULL
, 10);
738 contents
= (bfd_byte
*) bfd_alloc (abfd
, sz
);
739 if (contents
== NULL
)
741 if (bfd_read ((PTR
) contents
, 1, sz
, abfd
) != sz
)
744 /* The symbol table starts with a four byte count. */
745 c
= bfd_h_get_32 (abfd
, contents
);
749 bfd_set_error (bfd_error_bad_value
);
753 bfd_ardata (abfd
)->symdefs
= ((carsym
*)
754 bfd_alloc (abfd
, c
* sizeof (carsym
)));
755 if (bfd_ardata (abfd
)->symdefs
== NULL
)
758 /* After the count comes a list of four byte file offsets. */
759 for (i
= 0, arsym
= bfd_ardata (abfd
)->symdefs
, p
= contents
+ 4;
761 ++i
, ++arsym
, p
+= 4)
762 arsym
->file_offset
= bfd_h_get_32 (abfd
, p
);
764 /* After the file offsets come null terminated symbol names. */
765 cend
= contents
+ sz
;
766 for (i
= 0, arsym
= bfd_ardata (abfd
)->symdefs
;
768 ++i
, ++arsym
, p
+= strlen ((char *) p
) + 1)
772 bfd_set_error (bfd_error_bad_value
);
775 arsym
->name
= (char *) p
;
778 bfd_ardata (abfd
)->symdef_count
= c
;
779 bfd_has_map (abfd
) = true;
784 /* See if this is an XCOFF archive. */
786 static const bfd_target
*
787 xcoff_archive_p (abfd
)
790 struct xcoff_ar_file_hdr hdr
;
792 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_FILE_HDR
, 1, abfd
)
793 != SIZEOF_AR_FILE_HDR
)
795 if (bfd_get_error () != bfd_error_system_call
)
796 bfd_set_error (bfd_error_wrong_format
);
800 if (strncmp (hdr
.magic
, XCOFFARMAG
, SXCOFFARMAG
) != 0)
802 bfd_set_error (bfd_error_wrong_format
);
806 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
807 involves a cast, we can't do it as the left operand of
809 abfd
->tdata
.aout_ar_data
=
810 (struct artdata
*) bfd_zalloc (abfd
, sizeof (struct artdata
));
812 if (bfd_ardata (abfd
) == (struct artdata
*) NULL
)
815 bfd_ardata (abfd
)->first_file_filepos
= strtol (hdr
.firstmemoff
,
817 bfd_ardata (abfd
)->cache
= NULL
;
818 bfd_ardata (abfd
)->archive_head
= NULL
;
819 bfd_ardata (abfd
)->symdefs
= NULL
;
820 bfd_ardata (abfd
)->extended_names
= NULL
;
822 bfd_ardata (abfd
)->tdata
= bfd_zalloc (abfd
, SIZEOF_AR_FILE_HDR
);
823 if (bfd_ardata (abfd
)->tdata
== NULL
)
826 memcpy (bfd_ardata (abfd
)->tdata
, &hdr
, SIZEOF_AR_FILE_HDR
);
828 if (! xcoff_slurp_armap (abfd
))
830 bfd_release (abfd
, bfd_ardata (abfd
));
831 abfd
->tdata
.aout_ar_data
= (struct artdata
*) NULL
;
838 /* Read the archive header in an XCOFF archive. */
841 xcoff_read_ar_hdr (abfd
)
844 struct xcoff_ar_hdr hdr
;
846 struct xcoff_ar_hdr
*hdrp
;
847 struct areltdata
*ret
;
849 if (bfd_read ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
)
852 namlen
= strtol (hdr
.namlen
, (char **) NULL
, 10);
853 hdrp
= bfd_alloc (abfd
, SIZEOF_AR_HDR
+ namlen
+ 1);
856 memcpy (hdrp
, &hdr
, SIZEOF_AR_HDR
);
857 if (bfd_read ((char *) hdrp
+ SIZEOF_AR_HDR
, 1, namlen
, abfd
) != namlen
)
859 ((char *) hdrp
)[SIZEOF_AR_HDR
+ namlen
] = '\0';
861 ret
= (struct areltdata
*) bfd_alloc (abfd
, sizeof (struct areltdata
));
864 ret
->arch_header
= (char *) hdrp
;
865 ret
->parsed_size
= strtol (hdr
.size
, (char **) NULL
, 10);
866 ret
->filename
= (char *) hdrp
+ SIZEOF_AR_HDR
;
868 /* Skip over the XCOFFARFMAG at the end of the file name. */
869 if (bfd_seek (abfd
, (namlen
& 1) + SXCOFFARFMAG
, SEEK_CUR
) != 0)
875 /* Open the next element in an XCOFF archive. */
878 xcoff_openr_next_archived_file (archive
, last_file
)
884 if (xcoff_ardata (archive
) == NULL
)
886 bfd_set_error (bfd_error_invalid_operation
);
890 if (last_file
== NULL
)
891 filestart
= bfd_ardata (archive
)->first_file_filepos
;
893 filestart
= strtol (arch_xhdr (last_file
)->nextoff
, (char **) NULL
, 10);
896 || filestart
== strtol (xcoff_ardata (archive
)->memoff
,
898 || filestart
== strtol (xcoff_ardata (archive
)->symoff
,
901 bfd_set_error (bfd_error_no_more_archived_files
);
905 return _bfd_get_elt_at_filepos (archive
, filestart
);
908 /* Stat an element in an XCOFF archive. */
911 xcoff_generic_stat_arch_elt (abfd
, s
)
915 struct xcoff_ar_hdr
*hdrp
;
917 if (abfd
->arelt_data
== NULL
)
919 bfd_set_error (bfd_error_invalid_operation
);
923 hdrp
= arch_xhdr (abfd
);
925 s
->st_mtime
= strtol (hdrp
->date
, (char **) NULL
, 10);
926 s
->st_uid
= strtol (hdrp
->uid
, (char **) NULL
, 10);
927 s
->st_gid
= strtol (hdrp
->gid
, (char **) NULL
, 10);
928 s
->st_mode
= strtol (hdrp
->mode
, (char **) NULL
, 8);
929 s
->st_size
= arch_eltdata (abfd
)->parsed_size
;
934 /* Normalize a file name for inclusion in an archive. */
937 normalize_filename (abfd
)
941 const char *filename
;
943 file
= bfd_get_filename (abfd
);
944 filename
= strrchr (file
, '/');
945 if (filename
!= NULL
)
952 /* Write out an XCOFF armap. */
956 xcoff_write_armap (abfd
, elength
, map
, orl_count
, stridx
)
958 unsigned int elength
;
960 unsigned int orl_count
;
963 struct xcoff_ar_hdr hdr
;
965 unsigned char buf
[4];
970 memset (&hdr
, 0, sizeof hdr
);
971 sprintf (hdr
.size
, "%ld", (long) (4 + orl_count
* 4 + stridx
));
972 sprintf (hdr
.nextoff
, "%d", 0);
973 memcpy (hdr
.prevoff
, xcoff_ardata (abfd
)->memoff
, 12);
974 sprintf (hdr
.date
, "%d", 0);
975 sprintf (hdr
.uid
, "%d", 0);
976 sprintf (hdr
.gid
, "%d", 0);
977 sprintf (hdr
.mode
, "%d", 0);
978 sprintf (hdr
.namlen
, "%d", 0);
980 /* We need spaces, not null bytes, in the header. */
981 for (p
= (char *) &hdr
; p
< (char *) &hdr
+ SIZEOF_AR_HDR
; p
++)
985 if (bfd_write ((PTR
) &hdr
, SIZEOF_AR_HDR
, 1, abfd
) != SIZEOF_AR_HDR
986 || bfd_write (XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
) != SXCOFFARFMAG
)
989 bfd_h_put_32 (abfd
, orl_count
, buf
);
990 if (bfd_write (buf
, 1, 4, abfd
) != 4)
993 sub
= abfd
->archive_head
;
994 fileoff
= SIZEOF_AR_FILE_HDR
;
996 while (sub
!= NULL
&& i
< orl_count
)
1000 while (((bfd
*) (map
[i
]).pos
) == sub
)
1002 bfd_h_put_32 (abfd
, fileoff
, buf
);
1003 if (bfd_write (buf
, 1, 4, abfd
) != 4)
1007 namlen
= strlen (normalize_filename (sub
));
1008 namlen
= (namlen
+ 1) &~ 1;
1009 fileoff
+= (SIZEOF_AR_HDR
1012 + arelt_size (sub
));
1013 fileoff
= (fileoff
+ 1) &~ 1;
1017 for (i
= 0; i
< orl_count
; i
++)
1022 name
= *map
[i
].name
;
1023 namlen
= strlen (name
);
1024 if (bfd_write (name
, 1, namlen
+ 1, abfd
) != namlen
+ 1)
1028 if ((stridx
& 1) != 0)
1033 if (bfd_write (&b
, 1, 1, abfd
) != 1)
1040 /* Write out an XCOFF archive. We always write an entire archive,
1041 rather than fussing with the freelist and so forth. */
1044 xcoff_write_archive_contents (abfd
)
1047 struct xcoff_ar_file_hdr fhdr
;
1049 size_t total_namlen
;
1053 file_ptr prevoff
, nextoff
;
1056 struct xcoff_ar_hdr ahdr
;
1061 memset (&fhdr
, 0, sizeof fhdr
);
1062 strncpy (fhdr
.magic
, XCOFFARMAG
, SXCOFFARMAG
);
1063 sprintf (fhdr
.firstmemoff
, "%d", SIZEOF_AR_FILE_HDR
);
1064 sprintf (fhdr
.freeoff
, "%d", 0);
1068 for (sub
= abfd
->archive_head
; sub
!= NULL
; sub
= sub
->next
)
1071 total_namlen
+= strlen (normalize_filename (sub
)) + 1;
1073 offsets
= (file_ptr
*) bfd_alloc (abfd
, count
* sizeof (file_ptr
));
1074 if (offsets
== NULL
)
1077 if (bfd_seek (abfd
, SIZEOF_AR_FILE_HDR
, SEEK_SET
) != 0)
1080 makemap
= bfd_has_map (abfd
);
1083 nextoff
= SIZEOF_AR_FILE_HDR
;
1084 for (sub
= abfd
->archive_head
, i
= 0; sub
!= NULL
; sub
= sub
->next
, i
++)
1088 struct xcoff_ar_hdr
*ahdrp
;
1089 bfd_size_type remaining
;
1091 if (makemap
&& ! hasobjects
)
1093 if (bfd_check_format (sub
, bfd_object
))
1097 name
= normalize_filename (sub
);
1098 namlen
= strlen (name
);
1100 if (sub
->arelt_data
!= NULL
)
1101 ahdrp
= arch_xhdr (sub
);
1109 memset (&ahdr
, 0, sizeof ahdr
);
1111 if (stat (bfd_get_filename (sub
), &s
) != 0)
1113 bfd_set_error (bfd_error_system_call
);
1117 sprintf (ahdrp
->size
, "%ld", (long) s
.st_size
);
1118 sprintf (ahdrp
->date
, "%ld", (long) s
.st_mtime
);
1119 sprintf (ahdrp
->uid
, "%ld", (long) s
.st_uid
);
1120 sprintf (ahdrp
->gid
, "%ld", (long) s
.st_gid
);
1121 sprintf (ahdrp
->mode
, "%o", (unsigned int) s
.st_mode
);
1123 if (sub
->arelt_data
== NULL
)
1125 sub
->arelt_data
= ((struct areltdata
*)
1126 bfd_alloc (sub
, sizeof (struct areltdata
)));
1127 if (sub
->arelt_data
== NULL
)
1131 arch_eltdata (sub
)->parsed_size
= s
.st_size
;
1134 sprintf (ahdrp
->prevoff
, "%ld", (long) prevoff
);
1135 sprintf (ahdrp
->namlen
, "%ld", (long) namlen
);
1137 /* If the length of the name is odd, we write out the null byte
1138 after the name as well. */
1139 namlen
= (namlen
+ 1) &~ 1;
1141 remaining
= arelt_size (sub
);
1142 size
= (SIZEOF_AR_HDR
1147 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1149 offsets
[i
] = nextoff
;
1152 nextoff
+= size
+ (size
& 1);
1154 sprintf (ahdrp
->nextoff
, "%ld", (long) nextoff
);
1156 /* We need spaces, not null bytes, in the header. */
1157 for (p
= (char *) ahdrp
; p
< (char *) ahdrp
+ SIZEOF_AR_HDR
; p
++)
1161 if (bfd_write ((PTR
) ahdrp
, 1, SIZEOF_AR_HDR
, abfd
) != SIZEOF_AR_HDR
1162 || bfd_write ((PTR
) name
, 1, namlen
, abfd
) != namlen
1163 || (bfd_write ((PTR
) XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
)
1167 if (bfd_seek (sub
, (file_ptr
) 0, SEEK_SET
) != 0)
1169 while (remaining
!= 0)
1172 bfd_byte buffer
[DEFAULT_BUFFERSIZE
];
1174 amt
= sizeof buffer
;
1175 if (amt
> remaining
)
1177 if (bfd_read (buffer
, 1, amt
, sub
) != amt
1178 || bfd_write (buffer
, 1, amt
, abfd
) != amt
)
1183 if ((size
& 1) != 0)
1188 if (bfd_write (&b
, 1, 1, abfd
) != 1)
1193 sprintf (fhdr
.lastmemoff
, "%ld", (long) prevoff
);
1195 /* Write out the member table. */
1197 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1198 sprintf (fhdr
.memoff
, "%ld", (long) nextoff
);
1200 memset (&ahdr
, 0, sizeof ahdr
);
1201 sprintf (ahdr
.size
, "%ld", (long) (12 + count
* 12 + total_namlen
));
1202 sprintf (ahdr
.prevoff
, "%ld", (long) prevoff
);
1203 sprintf (ahdr
.date
, "%d", 0);
1204 sprintf (ahdr
.uid
, "%d", 0);
1205 sprintf (ahdr
.gid
, "%d", 0);
1206 sprintf (ahdr
.mode
, "%d", 0);
1207 sprintf (ahdr
.namlen
, "%d", 0);
1209 size
= (SIZEOF_AR_HDR
1216 nextoff
+= size
+ (size
& 1);
1218 if (makemap
&& hasobjects
)
1219 sprintf (ahdr
.nextoff
, "%ld", (long) nextoff
);
1221 sprintf (ahdr
.nextoff
, "%d", 0);
1223 /* We need spaces, not null bytes, in the header. */
1224 for (p
= (char *) &ahdr
; p
< (char *) &ahdr
+ SIZEOF_AR_HDR
; p
++)
1228 if (bfd_write ((PTR
) &ahdr
, 1, SIZEOF_AR_HDR
, abfd
) != SIZEOF_AR_HDR
1229 || (bfd_write ((PTR
) XCOFFARFMAG
, 1, SXCOFFARFMAG
, abfd
)
1233 sprintf (decbuf
, "%-12ld", (long) count
);
1234 if (bfd_write ((PTR
) decbuf
, 1, 12, abfd
) != 12)
1236 for (i
= 0; i
< count
; i
++)
1238 sprintf (decbuf
, "%-12ld", (long) offsets
[i
]);
1239 if (bfd_write ((PTR
) decbuf
, 1, 12, abfd
) != 12)
1242 for (sub
= abfd
->archive_head
; sub
!= NULL
; sub
= sub
->next
)
1247 name
= normalize_filename (sub
);
1248 namlen
= strlen (name
);
1249 if (bfd_write ((PTR
) name
, 1, namlen
+ 1, abfd
) != namlen
+ 1)
1252 if ((size
& 1) != 0)
1257 if (bfd_write ((PTR
) &b
, 1, 1, abfd
) != 1)
1261 /* Write out the armap, if appropriate. */
1263 if (! makemap
|| ! hasobjects
)
1264 sprintf (fhdr
.symoff
, "%d", 0);
1267 BFD_ASSERT (nextoff
== bfd_tell (abfd
));
1268 sprintf (fhdr
.symoff
, "%ld", (long) nextoff
);
1269 bfd_ardata (abfd
)->tdata
= (PTR
) &fhdr
;
1270 if (! _bfd_compute_and_write_armap (abfd
, 0))
1274 /* Write out the archive file header. */
1276 /* We need spaces, not null bytes, in the header. */
1277 for (p
= (char *) &fhdr
; p
< (char *) &fhdr
+ SIZEOF_AR_FILE_HDR
; p
++)
1281 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
1282 || (bfd_write ((PTR
) &fhdr
, SIZEOF_AR_FILE_HDR
, 1, abfd
) !=
1283 SIZEOF_AR_FILE_HDR
))
1289 /* We can't use the usual coff_sizeof_headers routine, because AIX
1290 always uses an a.out header. */
1294 _bfd_xcoff_sizeof_headers (abfd
, reloc
)
1301 if (xcoff_data (abfd
)->full_aouthdr
)
1304 size
+= SMALL_AOUTSZ
;
1305 size
+= abfd
->section_count
* SCNHSZ
;
1309 #define CORE_FILE_P _bfd_dummy_target
1311 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
1312 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
1313 #define coff_core_file_matches_executable_p \
1314 _bfd_nocore_core_file_matches_executable_p
1318 #define CORE_FILE_P rs6000coff_core_p
1319 extern const bfd_target
* rs6000coff_core_p ();
1320 extern boolean
rs6000coff_get_section_contents ();
1321 extern boolean
rs6000coff_core_file_matches_executable_p ();
1323 #undef coff_core_file_matches_executable_p
1324 #define coff_core_file_matches_executable_p \
1325 rs6000coff_core_file_matches_executable_p
1327 extern char *rs6000coff_core_file_failing_command
PARAMS ((bfd
*abfd
));
1328 #undef coff_core_file_failing_command
1329 #define coff_core_file_failing_command rs6000coff_core_file_failing_command
1331 extern int rs6000coff_core_file_failing_signal
PARAMS ((bfd
*abfd
));
1332 #undef coff_core_file_failing_signal
1333 #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
1335 #undef coff_get_section_contents
1336 #define coff_get_section_contents rs6000coff_get_section_contents
1337 #endif /* AIX_CORE */
1342 #define CORE_FILE_P lynx_core_file_p
1343 extern const bfd_target
*lynx_core_file_p
PARAMS ((bfd
*abfd
));
1345 extern boolean lynx_core_file_matches_executable_p
PARAMS ((bfd
*core_bfd
,
1347 #undef coff_core_file_matches_executable_p
1348 #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
1350 extern char *lynx_core_file_failing_command
PARAMS ((bfd
*abfd
));
1351 #undef coff_core_file_failing_command
1352 #define coff_core_file_failing_command lynx_core_file_failing_command
1354 extern int lynx_core_file_failing_signal
PARAMS ((bfd
*abfd
));
1355 #undef coff_core_file_failing_signal
1356 #define coff_core_file_failing_signal lynx_core_file_failing_signal
1358 #endif /* LYNX_CORE */
1360 #define _bfd_xcoff_bfd_get_relocated_section_contents \
1361 coff_bfd_get_relocated_section_contents
1362 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
1363 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
1365 /* The transfer vector that leads the outside world to all of the above. */
1377 "aixcoff-rs6000", /* name */
1379 bfd_target_coff_flavour
,
1380 BFD_ENDIAN_BIG
, /* data byte order is big */
1381 BFD_ENDIAN_BIG
, /* header byte order is big */
1383 (HAS_RELOC
| EXEC_P
| /* object flags */
1384 HAS_LINENO
| HAS_DEBUG
| DYNAMIC
|
1385 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
1387 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1388 0, /* leading char */
1389 '/', /* ar_pad_char */
1390 15, /* ar_max_namelen??? FIXMEmgo */
1392 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1393 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1394 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1395 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1396 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1397 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1399 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
1400 xcoff_archive_p
, CORE_FILE_P
},
1401 {bfd_false
, coff_mkobject
, /* bfd_set_format */
1402 _bfd_generic_mkarchive
, bfd_false
},
1403 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
1404 xcoff_write_archive_contents
, bfd_false
},
1406 BFD_JUMP_TABLE_GENERIC (coff
),
1407 BFD_JUMP_TABLE_COPY (coff
),
1408 BFD_JUMP_TABLE_CORE (coff
),
1409 BFD_JUMP_TABLE_ARCHIVE (xcoff
),
1410 BFD_JUMP_TABLE_SYMBOLS (coff
),
1411 BFD_JUMP_TABLE_RELOCS (coff
),
1412 BFD_JUMP_TABLE_WRITE (coff
),
1413 BFD_JUMP_TABLE_LINK (_bfd_xcoff
),
1414 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff
),