1 /* BFD back-end for VMS archive files.
3 Copyright 2010, 2011 Free Software Foundation, Inc.
4 Written by Tristan Gingold <gingold@adacore.com>, AdaCore.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
26 #include "safe-ctype.h"
32 /* The standard VMS disk block size. */
33 #ifndef VMS_BLOCK_SIZE
34 #define VMS_BLOCK_SIZE 512
37 /* Maximum key length (which is also the maximum symbol length in archive). */
38 #define MAX_KEYLEN 129
44 unsigned char min_char
;
45 unsigned char max_char
;
51 /* Kind of library. Used to filter in archive_p. */
61 /* Back-end private data. */
65 /* Standard tdata for an archive. But we don't use many fields. */
66 struct artdata artdata
;
71 /* Type of the archive. */
74 /* Kind of archive. Summary of its type. */
75 enum vms_lib_kind kind
;
77 /* Total size of the mhd (element header). */
78 unsigned int mhd_size
;
81 unsigned int credat_lo
;
82 unsigned int credat_hi
;
84 /* Vector of modules (archive elements), already sorted. */
85 unsigned int nbr_modules
;
86 struct carsym
*modules
;
89 /* DCX (decompression) data. */
90 unsigned int nbr_dcxsbm
;
91 struct dcxsbm_desc
*dcxsbm
;
94 #define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
96 /* End-Of-Text pattern. This is a special record to mark the end of file. */
98 static const unsigned char eotdesc
[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
100 /* Describe the current state of carsym entries while building the archive
101 table of content. Things are simple with Alpha archives as the number
102 of entries is known, but with IA64 archives a entry can make a reference
103 to severals members. Therefore we must be able to extend the table on the
104 fly, but it should be allocated on the bfd - which doesn't support realloc.
105 To reduce the overhead, the table is initially allocated in the BFD's
106 objalloc and extended if necessary on the heap. In the later case, it
107 is finally copied to the BFD's objalloc so that it will automatically be
112 /* The table of content. */
115 /* Number of entries used in the table. */
118 /* Maximum number of entries. */
121 /* If true, the table was reallocated on the heap. If false, it is still
122 in the BFD's objalloc. */
123 bfd_boolean realloced
;
126 /* Simply add a name to the index. */
129 vms_add_index (struct carsym_mem
*cs
, char *name
,
130 unsigned int idx_vbn
, unsigned int idx_off
)
132 if (cs
->nbr
== cs
->max
)
136 cs
->max
= 2 * cs
->max
+ 32;
140 n
= bfd_malloc2 (cs
->max
, sizeof (struct carsym
));
143 memcpy (n
, cs
->idx
, cs
->nbr
* sizeof (struct carsym
));
144 /* And unfortunately we can't free cs->idx. */
148 n
= bfd_realloc_or_free (cs
->idx
, cs
->nbr
* sizeof (struct carsym
));
153 cs
->realloced
= TRUE
;
155 cs
->idx
[cs
->nbr
].file_offset
= (idx_vbn
- 1) * VMS_BLOCK_SIZE
+ idx_off
;
156 cs
->idx
[cs
->nbr
].name
= name
;
161 /* Follow all member of a lns list (pointed by RFA) and add indexes for
162 NAME. Return FALSE in case of error. */
165 vms_add_indexes_from_list (bfd
*abfd
, struct carsym_mem
*cs
, char *name
,
174 vbn
= bfd_getl32 (rfa
->vbn
);
179 off
= (vbn
- 1) * VMS_BLOCK_SIZE
+ bfd_getl16 (rfa
->offset
);
180 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
181 || bfd_bread (&lns
, sizeof (lns
), abfd
) != sizeof (lns
))
184 if (!vms_add_index (cs
, name
,
185 bfd_getl32 (lns
.modrfa
.vbn
),
186 bfd_getl16 (lns
.modrfa
.offset
)))
193 /* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
196 vms_read_block (bfd
*abfd
, unsigned int vbn
, void *blk
)
200 off
= (vbn
- 1) * VMS_BLOCK_SIZE
;
201 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
202 || bfd_bread (blk
, VMS_BLOCK_SIZE
, abfd
) != VMS_BLOCK_SIZE
)
208 /* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
211 vms_write_block (bfd
*abfd
, unsigned int vbn
, void *blk
)
215 off
= (vbn
- 1) * VMS_BLOCK_SIZE
;
216 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
217 || bfd_bwrite (blk
, VMS_BLOCK_SIZE
, abfd
) != VMS_BLOCK_SIZE
)
223 /* Read index block VBN and put the entry in **IDX (which is updated).
224 If the entry is indirect, recurse. */
227 vms_traverse_index (bfd
*abfd
, unsigned int vbn
, struct carsym_mem
*cs
)
229 struct vms_indexdef indexdef
;
234 /* Read the index block. */
235 BFD_ASSERT (sizeof (indexdef
) == VMS_BLOCK_SIZE
);
236 if (!vms_read_block (abfd
, vbn
, &indexdef
))
240 p
= &indexdef
.keys
[0];
241 endp
= p
+ bfd_getl16 (indexdef
.used
);
244 unsigned int idx_vbn
;
245 unsigned int idx_off
;
247 unsigned char *keyname
;
250 /* Extract key length. */
251 if (bfd_libdata (abfd
)->ver
== LBR_MAJORID
)
253 struct vms_idx
*ridx
= (struct vms_idx
*)p
;
255 idx_vbn
= bfd_getl32 (ridx
->rfa
.vbn
);
256 idx_off
= bfd_getl16 (ridx
->rfa
.offset
);
258 keylen
= ridx
->keylen
;
260 keyname
= ridx
->keyname
;
262 else if (bfd_libdata (abfd
)->ver
== LBR_ELFMAJORID
)
264 struct vms_elfidx
*ridx
= (struct vms_elfidx
*)p
;
266 idx_vbn
= bfd_getl32 (ridx
->rfa
.vbn
);
267 idx_off
= bfd_getl16 (ridx
->rfa
.offset
);
269 keylen
= bfd_getl16 (ridx
->keylen
);
271 keyname
= ridx
->keyname
;
280 /* Point to the next index entry. */
281 p
= keyname
+ keylen
;
283 if (idx_off
== RFADEF__C_INDEX
)
285 /* Indirect entry. Recurse. */
286 if (!vms_traverse_index (abfd
, idx_vbn
, cs
))
291 /* Add a new entry. */
294 if (flags
& ELFIDX__SYMESC
)
296 /* Extended key name. */
297 unsigned int noff
= 0;
301 unsigned char kblk
[VMS_BLOCK_SIZE
];
304 if (keylen
!= sizeof (struct vms_kbn
))
307 kbn
= (struct vms_kbn
*)keyname
;
308 keylen
= bfd_getl16 (kbn
->keylen
);
310 name
= bfd_alloc (abfd
, keylen
+ 1);
313 kvbn
= bfd_getl32 (kbn
->rfa
.vbn
);
314 koff
= bfd_getl16 (kbn
->rfa
.offset
);
316 /* Read the key, chunk by chunk. */
321 if (!vms_read_block (abfd
, kvbn
, kblk
))
323 kbn
= (struct vms_kbn
*)(kblk
+ koff
);
324 klen
= bfd_getl16 (kbn
->keylen
);
325 kvbn
= bfd_getl32 (kbn
->rfa
.vbn
);
326 koff
= bfd_getl16 (kbn
->rfa
.offset
);
328 memcpy (name
+ noff
, kbn
+ 1, klen
);
339 /* Usual key name. */
340 name
= bfd_alloc (abfd
, keylen
+ 1);
344 memcpy (name
, keyname
, keylen
);
348 if (flags
& ELFIDX__LISTRFA
)
353 off
= (idx_vbn
- 1) * VMS_BLOCK_SIZE
+ idx_off
;
354 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
355 || bfd_bread (&lhs
, sizeof (lhs
), abfd
) != sizeof (lhs
))
358 /* FIXME: this adds extra entries that were not accounted. */
359 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.ng_g_rfa
))
361 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.ng_wk_rfa
))
363 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.g_g_rfa
))
365 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.g_wk_rfa
))
370 if (!vms_add_index (cs
, name
, idx_vbn
, idx_off
))
379 /* Read index #IDX, which must have NBREL entries. */
381 static struct carsym
*
382 vms_lib_read_index (bfd
*abfd
, int idx
, unsigned int *nbrel
)
387 struct carsym
*csbuf
;
388 struct carsym_mem csm
;
390 /* Read index desription. */
391 if (bfd_seek (abfd
, LHD_IDXDESC
+ idx
* IDD_LENGTH
, SEEK_SET
) != 0
392 || bfd_bread (&idd
, sizeof (idd
), abfd
) != sizeof (idd
))
396 flags
= bfd_getl16 (idd
.flags
);
397 if (!(flags
& IDD__FLAGS_ASCII
)
398 || !(flags
& IDD__FLAGS_VARLENIDX
))
401 csbuf
= bfd_alloc (abfd
, *nbrel
* sizeof (struct carsym
));
407 csm
.realloced
= FALSE
;
410 /* Note: if the index is empty, there is no block to traverse. */
411 vbn
= bfd_getl32 (idd
.vbn
);
412 if (vbn
!= 0 && !vms_traverse_index (abfd
, vbn
, &csm
))
414 if (csm
.realloced
&& csm
.idx
!= NULL
)
417 /* Note: in case of error, we can free what was allocated on the
419 bfd_release (abfd
, csbuf
);
425 /* There are more entries than the first estimate. Allocate on
426 the BFD's objalloc. */
427 csbuf
= bfd_alloc (abfd
, csm
.nbr
* sizeof (struct carsym
));
430 memcpy (csbuf
, csm
.idx
, csm
.nbr
* sizeof (struct carsym
));
437 /* Standard function. */
439 static const bfd_target
*
440 _bfd_vms_lib_archive_p (bfd
*abfd
, enum vms_lib_kind kind
)
444 unsigned int majorid
;
445 struct lib_tdata
*tdata_hold
;
446 struct lib_tdata
*tdata
;
448 unsigned int nbr_ent
;
451 if (bfd_bread (&lhd
, sizeof (lhd
), abfd
) != sizeof (lhd
))
453 if (bfd_get_error () != bfd_error_system_call
)
454 bfd_set_error (bfd_error_wrong_format
);
458 /* Check sanity (= magic) number. */
459 sanity
= bfd_getl32 (lhd
.sanity
);
460 if (!(sanity
== LHD_SANEID3
461 || sanity
== LHD_SANEID6
462 || sanity
== LHD_SANEID_DCX
))
464 bfd_set_error (bfd_error_wrong_format
);
467 majorid
= bfd_getl32 (lhd
.majorid
);
469 /* Check archive kind. */
473 if ((lhd
.type
!= LBR__C_TYP_EOBJ
&& lhd
.type
!= LBR__C_TYP_ESHSTB
)
474 || majorid
!= LBR_MAJORID
477 bfd_set_error (bfd_error_wrong_format
);
482 if ((lhd
.type
!= LBR__C_TYP_IOBJ
&& lhd
.type
!= LBR__C_TYP_ISHSTB
)
483 || majorid
!= LBR_ELFMAJORID
486 bfd_set_error (bfd_error_wrong_format
);
491 if ((lhd
.type
!= LBR__C_TYP_TXT
492 && lhd
.type
!= LBR__C_TYP_MLB
493 && lhd
.type
!= LBR__C_TYP_HLP
)
494 || majorid
!= LBR_MAJORID
497 bfd_set_error (bfd_error_wrong_format
);
505 /* Allocate and initialize private data. */
506 tdata_hold
= bfd_libdata (abfd
);
507 tdata
= (struct lib_tdata
*) bfd_zalloc (abfd
, sizeof (struct lib_tdata
));
510 abfd
->tdata
.any
= (void *)tdata
;
511 tdata
->ver
= majorid
;
512 tdata
->mhd_size
= MHD__C_USRDAT
+ lhd
.mhdusz
;
513 tdata
->type
= lhd
.type
;
515 tdata
->credat_lo
= bfd_getl32 (lhd
.credat
+ 0);
516 tdata
->credat_hi
= bfd_getl32 (lhd
.credat
+ 4);
519 tdata
->nbr_modules
= bfd_getl32 (lhd
.modcnt
);
520 tdata
->artdata
.symdef_count
= bfd_getl32 (lhd
.idxcnt
) - tdata
->nbr_modules
;
521 nbr_ent
= tdata
->nbr_modules
;
522 tdata
->modules
= vms_lib_read_index (abfd
, 0, &nbr_ent
);
523 if (tdata
->modules
== NULL
|| nbr_ent
!= tdata
->nbr_modules
)
527 nbr_ent
= tdata
->artdata
.symdef_count
;
528 tdata
->artdata
.symdefs
= vms_lib_read_index (abfd
, 1, &nbr_ent
);
529 if (tdata
->artdata
.symdefs
== NULL
)
531 /* Only IA64 archives may have more entries in the index that what
533 if (nbr_ent
!= tdata
->artdata
.symdef_count
534 && kind
!= vms_lib_ia64
)
536 tdata
->artdata
.symdef_count
= nbr_ent
;
538 tdata
->cache
= bfd_zalloc (abfd
, sizeof (bfd
*) * tdata
->nbr_modules
);
539 if (tdata
->cache
== NULL
)
542 /* Read DCX submaps. */
543 dcxvbn
= bfd_getl32 (lhd
.dcxmapvbn
);
546 unsigned char buf_reclen
[4];
549 struct vms_dcxmap
*map
;
550 unsigned int sbm_off
;
553 if (bfd_seek (abfd
, (dcxvbn
- 1) * VMS_BLOCK_SIZE
, SEEK_SET
) != 0
554 || bfd_bread (buf_reclen
, sizeof (buf_reclen
), abfd
)
555 != sizeof (buf_reclen
))
557 reclen
= bfd_getl32 (buf_reclen
);
558 buf
= bfd_malloc (reclen
);
561 if (bfd_bread (buf
, reclen
, abfd
) != reclen
)
566 map
= (struct vms_dcxmap
*)buf
;
567 tdata
->nbr_dcxsbm
= bfd_getl16 (map
->nsubs
);
568 sbm_off
= bfd_getl16 (map
->sub0
);
569 tdata
->dcxsbm
= (struct dcxsbm_desc
*)bfd_alloc
570 (abfd
, tdata
->nbr_dcxsbm
* sizeof (struct dcxsbm_desc
));
571 for (i
= 0; i
< tdata
->nbr_dcxsbm
; i
++)
573 struct vms_dcxsbm
*sbm
= (struct vms_dcxsbm
*) (buf
+ sbm_off
);
574 struct dcxsbm_desc
*sbmdesc
= &tdata
->dcxsbm
[i
];
575 unsigned int sbm_len
;
578 unsigned char *data
= (unsigned char *)sbm
;
582 sbm_sz
= bfd_getl16 (sbm
->size
);
584 BFD_ASSERT (sbm_off
<= reclen
);
586 sbmdesc
->min_char
= sbm
->min_char
;
587 BFD_ASSERT (sbmdesc
->min_char
== 0);
588 sbmdesc
->max_char
= sbm
->max_char
;
589 sbm_len
= sbmdesc
->max_char
- sbmdesc
->min_char
+ 1;
590 l
= (2 * sbm_len
+ 7) / 8;
592 (sbm_sz
>= sizeof (struct vms_dcxsbm
) + l
+ 3 * sbm_len
593 || (tdata
->nbr_dcxsbm
== 1
594 && sbm_sz
>= sizeof (struct vms_dcxsbm
) + l
+ sbm_len
));
595 sbmdesc
->flags
= (unsigned char *)bfd_alloc (abfd
, l
);
596 memcpy (sbmdesc
->flags
, data
+ bfd_getl16 (sbm
->flags
), l
);
597 sbmdesc
->nodes
= (unsigned char *)bfd_alloc (abfd
, 2 * sbm_len
);
598 memcpy (sbmdesc
->nodes
, data
+ bfd_getl16 (sbm
->nodes
), 2 * sbm_len
);
599 off
= bfd_getl16 (sbm
->next
);
602 /* Read the 'next' array. */
603 sbmdesc
->next
= (unsigned short *)bfd_alloc
604 (abfd
, sbm_len
* sizeof (unsigned short));
606 for (j
= 0; j
< sbm_len
; j
++)
607 sbmdesc
->next
[j
] = bfd_getl16 (buf1
+ j
* 2);
611 /* There is no next array if there is only one submap. */
612 BFD_ASSERT (tdata
->nbr_dcxsbm
== 1);
613 sbmdesc
->next
= NULL
;
620 tdata
->nbr_dcxsbm
= 0;
623 /* The map is always present. Also mark shared image library. */
624 abfd
->has_armap
= TRUE
;
625 if (tdata
->type
== LBR__C_TYP_ESHSTB
|| tdata
->type
== LBR__C_TYP_ISHSTB
)
626 abfd
->is_thin_archive
= TRUE
;
631 bfd_release (abfd
, tdata
);
632 abfd
->tdata
.any
= (void *)tdata_hold
;;
636 /* Standard function for alpha libraries. */
639 _bfd_vms_lib_alpha_archive_p (bfd
*abfd
)
641 return _bfd_vms_lib_archive_p (abfd
, vms_lib_alpha
);
644 /* Standard function for ia64 libraries. */
647 _bfd_vms_lib_ia64_archive_p (bfd
*abfd
)
649 return _bfd_vms_lib_archive_p (abfd
, vms_lib_ia64
);
652 /* Standard function for text libraries. */
654 static const bfd_target
*
655 _bfd_vms_lib_txt_archive_p (bfd
*abfd
)
657 return _bfd_vms_lib_archive_p (abfd
, vms_lib_txt
);
660 /* Standard bfd function. */
663 _bfd_vms_lib_mkarchive (bfd
*abfd
, enum vms_lib_kind kind
)
665 struct lib_tdata
*tdata
;
667 tdata
= (struct lib_tdata
*) bfd_zalloc (abfd
, sizeof (struct lib_tdata
));
671 abfd
->tdata
.any
= (void *)tdata
;
672 vms_get_time (&tdata
->credat_hi
, &tdata
->credat_lo
);
678 tdata
->ver
= LBR_MAJORID
;
679 tdata
->mhd_size
= offsetof (struct vms_mhd
, pad1
);
680 tdata
->type
= LBR__C_TYP_EOBJ
;
683 tdata
->ver
= LBR_ELFMAJORID
;
684 tdata
->mhd_size
= sizeof (struct vms_mhd
);
685 tdata
->type
= LBR__C_TYP_IOBJ
;
691 tdata
->nbr_modules
= 0;
692 tdata
->artdata
.symdef_count
= 0;
693 tdata
->modules
= NULL
;
694 tdata
->artdata
.symdefs
= NULL
;
701 _bfd_vms_lib_alpha_mkarchive (bfd
*abfd
)
703 return _bfd_vms_lib_mkarchive (abfd
, vms_lib_alpha
);
707 _bfd_vms_lib_ia64_mkarchive (bfd
*abfd
)
709 return _bfd_vms_lib_mkarchive (abfd
, vms_lib_ia64
);
712 /* Find NAME in the symbol index. Return the index. */
715 _bfd_vms_lib_find_symbol (bfd
*abfd
, const char *name
)
717 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
718 carsym
*syms
= tdata
->artdata
.symdefs
;
721 /* Open-coded binary search for speed. */
723 hi
= tdata
->artdata
.symdef_count
- 1;
727 int mid
= lo
+ (hi
- lo
) / 2;
730 diff
= (char)(name
[0] - syms
[mid
].name
[0]);
732 diff
= strcmp (name
, syms
[mid
].name
);
740 return BFD_NO_MORE_SYMBOLS
;
743 /* IO vector for archive member. Need that because members are not linearly
744 stored in archives. */
748 /* Current offset. */
751 /* Length of the module, when known. */
754 /* Current position in the record from bfd_bread point of view (ie, after
755 decompression). 0 means that no data byte have been read, -2 and -1
756 are reserved for the length word. */
758 #define REC_POS_NL -4
759 #define REC_POS_PAD -3
760 #define REC_POS_LEN0 -2
761 #define REC_POS_LEN1 -1
764 unsigned short rec_len
;
765 /* Number of bytes to read in the current record. */
766 unsigned short rec_rem
;
767 /* Offset of the next block. */
769 /* Current *data* offset in the data block. */
770 unsigned short blk_off
;
772 /* Offset of the first block. Extracted from the index. */
773 file_ptr first_block
;
775 /* Initial next_block. Extracted when the MHD is read. */
776 file_ptr init_next_block
;
777 /* Initial blk_off, once the MHD is read. */
778 unsigned short init_blk_off
;
780 /* Used to store any 3 byte record, which could be the EOF pattern. */
781 unsigned char pattern
[4];
784 struct dcxsbm_desc
*dcxsbms
;
785 /* Current submap. */
786 struct dcxsbm_desc
*dcx_sbm
;
787 /* Current offset in the submap. */
788 unsigned int dcx_offset
;
791 /* Compressed buffer. */
792 unsigned char *dcx_buf
;
793 /* Size of the buffer. Used to resize. */
794 unsigned int dcx_max
;
795 /* Number of valid bytes in the buffer. */
796 unsigned int dcx_rlen
;
799 /* Return the current position. */
802 vms_lib_btell (struct bfd
*abfd
)
804 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
808 /* Read the header of the next data block if all bytes of the current block
812 vms_lib_read_block (struct bfd
*abfd
)
814 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
816 if (vec
->blk_off
== DATA__LENGTH
)
818 unsigned char hdr
[DATA__DATA
];
820 /* Read next block. */
821 if (bfd_seek (abfd
->my_archive
, vec
->next_block
, SEEK_SET
) != 0)
823 if (bfd_bread (hdr
, sizeof (hdr
), abfd
->my_archive
) != sizeof (hdr
))
825 vec
->next_block
= (bfd_getl32 (hdr
+ 2) - 1) * VMS_BLOCK_SIZE
;
826 vec
->blk_off
= sizeof (hdr
);
831 /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
832 not stored. Read linearly from the library, but handle blocks. This
833 function does not handle records nor EOF. */
836 vms_lib_bread_raw (struct bfd
*abfd
, unsigned char *buf
, file_ptr nbytes
)
838 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
846 /* Be sure the current data block is read. */
847 if (!vms_lib_read_block (abfd
))
850 /* Do not read past the data block, do not read more than requested. */
851 l
= DATA__LENGTH
- vec
->blk_off
;
858 /* Really read into BUF. */
859 if (bfd_bread (buf
, l
, abfd
->my_archive
) != l
)
864 /* Make as if we are reading. */
865 if (bfd_seek (abfd
->my_archive
, l
, SEEK_CUR
) != 0)
878 /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
881 vms_lib_dcx (struct vms_lib_iovec
*vec
, unsigned char *buf
, file_ptr nbytes
)
883 struct dcxsbm_desc
*sbm
;
889 /* The loop below expect to deliver at least one byte. */
893 /* Get the current state. */
895 offset
= vec
->dcx_offset
;
896 j
= vec
->dcx_pos
& 7;
898 for (i
= vec
->dcx_pos
>> 3; i
< vec
->dcx_rlen
; i
++)
900 unsigned char b
= vec
->dcx_buf
[i
];
906 if (!(sbm
->flags
[offset
>> 3] & (1 << (offset
& 7))))
908 unsigned int n_offset
= sbm
->nodes
[offset
];
911 /* End of buffer. Stay where we are. */
912 vec
->dcx_pos
= (i
<< 3) + j
;
915 vec
->dcx_offset
= offset
;
919 offset
= 2 * n_offset
;
923 unsigned char v
= sbm
->nodes
[offset
];
925 if (sbm
->next
!= NULL
)
926 sbm
= vec
->dcxsbms
+ sbm
->next
[v
];
937 vec
->dcx_pos
= (i
<< 3) + j
+ 1;
938 vec
->dcx_offset
= offset
;
951 /* Standard IOVEC function. */
954 vms_lib_bread (struct bfd
*abfd
, void *vbuf
, file_ptr nbytes
)
956 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
959 unsigned char *buf
= (unsigned char *)vbuf
;
961 /* Do not read past the end. */
962 if (vec
->where
>= vec
->file_len
)
968 if (vec
->rec_rem
== 0)
970 unsigned char blen
[2];
972 /* Read record length. */
973 if (vms_lib_bread_raw (abfd
, blen
, sizeof (blen
)) != sizeof (blen
))
975 vec
->rec_len
= bfd_getl16 (blen
);
976 if (bfd_libdata (abfd
->my_archive
)->kind
== vms_lib_txt
)
978 /* Discard record size and align byte. */
980 vec
->rec_rem
= vec
->rec_len
;
984 /* Prepend record size. */
985 vec
->rec_pos
= REC_POS_LEN0
;
986 vec
->rec_rem
= (vec
->rec_len
+ 1) & ~1; /* With align byte. */
988 if (vec
->rec_len
== 3)
990 /* Possibly end of file. Check the pattern. */
991 if (vms_lib_bread_raw (abfd
, vec
->pattern
, 4) != 4)
993 if (!memcmp (vec
->pattern
, eotdesc
+ 2, 3))
995 /* This is really an EOF. */
997 vec
->file_len
= vec
->where
;
1002 if (vec
->dcxsbms
!= NULL
)
1004 /* This is a compressed member. */
1008 /* Be sure there is enough room for the expansion. */
1009 len
= (vec
->rec_len
+ 1) & ~1;
1010 if (len
> vec
->dcx_max
)
1012 while (len
> vec
->dcx_max
)
1014 vec
->dcx_buf
= bfd_alloc (abfd
, vec
->dcx_max
);
1015 if (vec
->dcx_buf
== NULL
)
1019 /* Read the compressed record. */
1020 vec
->dcx_rlen
= len
;
1021 if (vec
->rec_len
== 3)
1024 memcpy (vec
->dcx_buf
, vec
->pattern
, 3);
1028 elen
= vms_lib_bread_raw (abfd
, vec
->dcx_buf
, len
);
1033 /* Dummy expansion to get the expanded length. */
1034 vec
->dcx_offset
= 0;
1035 vec
->dcx_sbm
= vec
->dcxsbms
;
1037 elen
= vms_lib_dcx (vec
, NULL
, 0x10000);
1040 vec
->rec_len
= elen
;
1041 vec
->rec_rem
= elen
;
1043 /* Reset the state. */
1044 vec
->dcx_offset
= 0;
1045 vec
->dcx_sbm
= vec
->dcxsbms
;
1049 if (vec
->rec_pos
< 0)
1052 switch (vec
->rec_pos
)
1055 c
= vec
->rec_len
& 0xff;
1056 vec
->rec_pos
= REC_POS_LEN1
;
1059 c
= (vec
->rec_len
>> 8) & 0xff;
1083 if (nbytes
> vec
->rec_rem
)
1084 chunk
= vec
->rec_rem
;
1088 if (vec
->dcxsbms
!= NULL
)
1090 /* Optimize the stat() case: no need to decompress again as we
1092 if (!(buf
== NULL
&& chunk
== vec
->rec_rem
))
1093 chunk
= vms_lib_dcx (vec
, buf
, chunk
);
1097 if (vec
->rec_len
== 3)
1100 memcpy (buf
, vec
->pattern
+ vec
->rec_pos
, chunk
);
1103 chunk
= vms_lib_bread_raw (abfd
, buf
, chunk
);
1111 vec
->rec_pos
+= chunk
;
1112 vec
->rec_rem
-= chunk
;
1114 if (vec
->rec_rem
== 0)
1116 /* End of record reached. */
1117 if (bfd_libdata (abfd
->my_archive
)->kind
== vms_lib_txt
)
1119 if ((vec
->rec_len
& 1) == 1
1120 && vec
->rec_len
!= 3
1121 && vec
->dcxsbms
== NULL
)
1123 /* Eat the pad byte. */
1125 if (vms_lib_bread_raw (abfd
, &pad
, 1) != 1)
1128 vec
->rec_pos
= REC_POS_NL
;
1133 if ((vec
->rec_len
& 1) == 1 && vec
->dcxsbms
!= NULL
)
1135 vec
->rec_pos
= REC_POS_PAD
;
1145 /* Standard function, but we currently only handle the rewind case. */
1148 vms_lib_bseek (struct bfd
*abfd
, file_ptr offset
, int whence
)
1150 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1152 if (whence
== SEEK_SET
&& offset
== 0)
1157 vec
->blk_off
= vec
->init_blk_off
;
1158 vec
->next_block
= vec
->init_next_block
;
1160 if (bfd_seek (abfd
->my_archive
, vec
->first_block
, SEEK_SET
) != 0)
1169 vms_lib_bwrite (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1170 const void *where ATTRIBUTE_UNUSED
,
1171 file_ptr nbytes ATTRIBUTE_UNUSED
)
1177 vms_lib_bclose (struct bfd
*abfd
)
1179 abfd
->iostream
= NULL
;
1184 vms_lib_bflush (struct bfd
*abfd ATTRIBUTE_UNUSED
)
1190 vms_lib_bstat (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1191 struct stat
*sb ATTRIBUTE_UNUSED
)
1193 /* Not supported. */
1198 vms_lib_bmmap (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1199 void *addr ATTRIBUTE_UNUSED
,
1200 bfd_size_type len ATTRIBUTE_UNUSED
,
1201 int prot ATTRIBUTE_UNUSED
,
1202 int flags ATTRIBUTE_UNUSED
,
1203 file_ptr offset ATTRIBUTE_UNUSED
,
1204 void **map_addr ATTRIBUTE_UNUSED
,
1205 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
1210 static const struct bfd_iovec vms_lib_iovec
= {
1211 &vms_lib_bread
, &vms_lib_bwrite
, &vms_lib_btell
, &vms_lib_bseek
,
1212 &vms_lib_bclose
, &vms_lib_bflush
, &vms_lib_bstat
, &vms_lib_bmmap
1215 /* Open a library module. FILEPOS is the position of the module header. */
1218 vms_lib_bopen (bfd
*el
, file_ptr filepos
)
1220 struct vms_lib_iovec
*vec
;
1221 unsigned char buf
[256];
1222 struct vms_mhd
*mhd
;
1223 struct lib_tdata
*tdata
= bfd_libdata (el
->my_archive
);
1226 /* Allocate and initialized the iovec. */
1227 vec
= bfd_zalloc (el
, sizeof (*vec
));
1232 el
->iovec
= &vms_lib_iovec
;
1234 /* File length is not known. */
1237 /* Read the first data block. */
1238 vec
->next_block
= filepos
& ~(VMS_BLOCK_SIZE
- 1);
1239 vec
->blk_off
= DATA__LENGTH
;
1240 if (!vms_lib_read_block (el
))
1243 /* Prepare to read the first record. */
1244 vec
->blk_off
= filepos
& (VMS_BLOCK_SIZE
- 1);
1246 if (bfd_seek (el
->my_archive
, filepos
, SEEK_SET
) != 0)
1249 /* Read Record length + MHD + align byte. */
1250 len
= tdata
->mhd_size
;
1251 if (vms_lib_bread_raw (el
, buf
, 2) != 2)
1253 if (bfd_getl16 (buf
) != len
)
1255 len
= (len
+ 1) & ~1;
1256 BFD_ASSERT (len
<= sizeof (buf
));
1257 if (vms_lib_bread_raw (el
, buf
, len
) != len
)
1260 /* Get info from mhd. */
1261 mhd
= (struct vms_mhd
*)buf
;
1263 if (mhd
->id
!= MHD__C_MHDID
)
1265 if (len
>= MHD__C_MHDLEN
+ 1)
1266 el
->selective_search
= (mhd
->objstat
& MHD__M_SELSRC
) ? 1 : 0;
1267 el
->mtime
= vms_rawtime_to_time_t (mhd
->datim
);
1268 el
->mtime_set
= TRUE
;
1270 /* Reinit the iovec so that seek() will point to the first record after
1273 vec
->init_blk_off
= vec
->blk_off
;
1274 vec
->init_next_block
= vec
->next_block
;
1275 vec
->first_block
= bfd_tell (el
->my_archive
);
1276 vec
->dcxsbms
= bfd_libdata (el
->my_archive
)->dcxsbm
;
1278 if (vec
->dcxsbms
!= NULL
)
1281 vec
->dcx_max
= 10 * 1024;
1282 vec
->dcx_buf
= bfd_alloc (el
, vec
->dcx_max
);
1284 if (vec
->dcx_buf
== NULL
)
1290 /* Get member MODIDX. Return NULL in case of error. */
1293 _bfd_vms_lib_get_module (bfd
*abfd
, unsigned int modidx
)
1295 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
1300 if (modidx
>= tdata
->nbr_modules
)
1303 /* Already loaded. */
1304 if (tdata
->cache
[modidx
])
1305 return tdata
->cache
[modidx
];
1308 file_off
= tdata
->modules
[modidx
].file_offset
;
1309 if (tdata
->type
!= LBR__C_TYP_IOBJ
)
1311 res
= _bfd_create_empty_archive_element_shell (abfd
);
1315 /* Special reader to deal with data blocks. */
1316 if (!vms_lib_bopen (res
, file_off
))
1322 struct vms_mhd
*mhd
;
1323 struct areltdata
*arelt
;
1325 /* Sanity check. The MHD must be big enough to contain module size. */
1326 if (tdata
->mhd_size
< offsetof (struct vms_mhd
, modsize
) + 4)
1329 /* Read the MHD now. */
1330 if (bfd_seek (abfd
, file_off
, SEEK_SET
) != 0)
1332 if (bfd_bread (buf
, tdata
->mhd_size
, abfd
) != tdata
->mhd_size
)
1335 res
= _bfd_create_empty_archive_element_shell (abfd
);
1338 arelt
= bfd_zalloc (res
, sizeof (*arelt
));
1341 res
->arelt_data
= arelt
;
1343 /* Get info from mhd. */
1344 mhd
= (struct vms_mhd
*)buf
;
1345 if (mhd
->id
!= MHD__C_MHDID
)
1347 if (tdata
->mhd_size
>= offsetof (struct vms_mhd
, objstat
) + 1)
1348 res
->selective_search
= (mhd
->objstat
& MHD__M_SELSRC
) ? 1 : 0;
1349 res
->mtime
= vms_rawtime_to_time_t (mhd
->datim
);
1350 res
->mtime_set
= TRUE
;
1352 arelt
->parsed_size
= bfd_getl32 (mhd
->modsize
);
1354 /* No need for a special reader as members are stored linearly.
1355 Just skip the MHD. */
1356 res
->origin
= file_off
+ tdata
->mhd_size
;
1359 res
->filename
= tdata
->modules
[modidx
].name
;
1361 tdata
->cache
[modidx
] = res
;
1366 /* Standard function: get member at IDX. */
1369 _bfd_vms_lib_get_elt_at_index (bfd
*abfd
, symindex symidx
)
1371 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
1373 unsigned int modidx
;
1376 if (symidx
> tdata
->artdata
.symdef_count
)
1378 file_off
= tdata
->artdata
.symdefs
[symidx
].file_offset
;
1381 for (modidx
= 0; modidx
< tdata
->nbr_modules
; modidx
++)
1383 if (tdata
->modules
[modidx
].file_offset
== file_off
)
1386 if (modidx
>= tdata
->nbr_modules
)
1389 return _bfd_vms_lib_get_module (abfd
, modidx
);
1392 /* Elements of an imagelib are stubs. You can get the real image with this
1396 _bfd_vms_lib_get_imagelib_file (bfd
*el
)
1398 bfd
*archive
= el
->my_archive
;
1399 const char *modname
= el
->filename
;
1400 int modlen
= strlen (modname
);
1405 /* Convert module name to lower case and append '.exe'. */
1406 filename
= bfd_alloc (el
, modlen
+ 5);
1407 if (filename
== NULL
)
1409 for (j
= 0; j
< modlen
; j
++)
1410 if (ISALPHA (modname
[j
]))
1411 filename
[j
] = TOLOWER (modname
[j
]);
1413 filename
[j
] = modname
[j
];
1414 memcpy (filename
+ modlen
, ".exe", 5);
1416 filename
= _bfd_append_relative_path (archive
, filename
);
1417 if (filename
== NULL
)
1419 res
= bfd_openr (filename
, NULL
);
1423 (*_bfd_error_handler
)(_("could not open shared image '%s' from '%s'"),
1424 filename
, archive
->filename
);
1425 bfd_release (archive
, filename
);
1429 /* FIXME: put it in a cache ? */
1433 /* Standard function. */
1436 _bfd_vms_lib_openr_next_archived_file (bfd
*archive
,
1445 idx
= last_file
->proxy_origin
+ 1;
1447 if (idx
>= bfd_libdata (archive
)->nbr_modules
)
1449 bfd_set_error (bfd_error_no_more_archived_files
);
1453 res
= _bfd_vms_lib_get_module (archive
, idx
);
1456 res
->proxy_origin
= idx
;
1460 /* Standard function. Just compute the length. */
1463 _bfd_vms_lib_generic_stat_arch_elt (bfd
*abfd
, struct stat
*st
)
1465 struct lib_tdata
*tdata
;
1468 if (abfd
->my_archive
== NULL
)
1470 bfd_set_error (bfd_error_invalid_operation
);
1474 tdata
= bfd_libdata (abfd
->my_archive
);
1475 if (tdata
->type
!= LBR__C_TYP_IOBJ
)
1477 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1479 if (vec
->file_len
== (ufile_ptr
)-1)
1481 if (vms_lib_bseek (abfd
, 0, SEEK_SET
) != 0)
1484 /* Compute length. */
1485 while (vms_lib_bread (abfd
, NULL
, 1 << 20) > 0)
1488 st
->st_size
= vec
->file_len
;
1492 st
->st_size
= ((struct areltdata
*)abfd
->arelt_data
)->parsed_size
;
1495 if (abfd
->mtime_set
)
1496 st
->st_mtime
= abfd
->mtime
;
1506 /* Internal representation of an index entry. */
1510 /* Corresponding archive member. */
1513 /* Number of reference to this entry. */
1516 /* Length of the key. */
1517 unsigned short namlen
;
1523 /* Used to sort index entries. */
1526 lib_index_cmp (const void *lv
, const void *rv
)
1528 const struct lib_index
*l
= lv
;
1529 const struct lib_index
*r
= rv
;
1531 return strcmp (l
->name
, r
->name
);
1534 /* Maximum number of index blocks level. */
1536 #define MAX_LEVEL 10
1538 /* Get the size of an index entry. */
1541 get_idxlen (struct lib_index
*idx
, bfd_boolean is_elfidx
)
1545 /* 9 is the size of struct vms_elfidx without keyname. */
1546 if (idx
->namlen
> MAX_KEYLEN
)
1547 return 9 + sizeof (struct vms_kbn
);
1549 return 9 + idx
->namlen
;
1553 /* 7 is the size of struct vms_idx without keyname. */
1554 return 7 + idx
->namlen
;
1558 /* Write the index composed by NBR symbols contained in IDX.
1559 VBN is the first vbn to be used, and will contain on return the last vbn.
1560 Can be called with ABFD set to NULL just to size the index.
1561 If not null, TOPVBN will be assigned to the vbn of the root index tree.
1562 IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
1563 Return TRUE on success. */
1566 vms_write_index (bfd
*abfd
,
1567 struct lib_index
*idx
, unsigned int nbr
, unsigned int *vbn
,
1568 unsigned int *topvbn
, bfd_boolean is_elfidx
)
1573 struct vms_indexdef
*rblk
[MAX_LEVEL
];
1578 unsigned short lastlen
;
1581 /* The kbn blocks are used to store long symbol names. */
1582 unsigned int kbn_sz
= 0; /* Number of bytes available in the kbn block. */
1583 unsigned int kbn_vbn
= 0; /* VBN of the kbn block. */
1584 unsigned char *kbn_blk
= NULL
; /* Contents of the kbn block. */
1588 /* No entries. Very easy to handle. */
1596 /* Sort the index the first time this function is called. */
1597 qsort (idx
, nbr
, sizeof (struct lib_index
), lib_index_cmp
);
1600 /* Allocate first index block. */
1603 rblk
[0] = bfd_zmalloc (sizeof (struct vms_indexdef
));
1604 blk
[0].vbn
= (*vbn
)++;
1608 for (i
= 0; i
< nbr
; i
++, idx
++)
1610 unsigned int idxlen
;
1612 unsigned int key_vbn
= 0;
1613 unsigned int key_off
= 0;
1615 idxlen
= get_idxlen (idx
, is_elfidx
);
1617 if (is_elfidx
&& idx
->namlen
>= MAX_KEYLEN
)
1619 /* If the key (ie name) is too long, write it in the kbn block. */
1620 unsigned int kl
= idx
->namlen
;
1621 unsigned int kl_chunk
;
1622 const char *key
= idx
->name
;
1624 /* Write the key in the kbn, chunk after chunk. */
1627 if (kbn_sz
< sizeof (struct vms_kbn
))
1629 /* Not enough room in the kbn block. */
1632 /* Write it to the disk (if there is one). */
1635 if (vms_write_block (abfd
, kbn_vbn
, kbn_blk
) != TRUE
)
1640 kbn_blk
= bfd_malloc (VMS_BLOCK_SIZE
);
1641 if (kbn_blk
== NULL
)
1644 *(unsigned short *)kbn_blk
= 0;
1646 /* Allocate a new block for the keys. */
1648 kbn_sz
= VMS_BLOCK_SIZE
- 2;
1650 /* Size of the chunk written to the current key block. */
1651 if (kl
+ sizeof (struct vms_kbn
) > kbn_sz
)
1652 kl_chunk
= kbn_sz
- sizeof (struct vms_kbn
);
1656 if (kbn_blk
!= NULL
)
1658 struct vms_kbn
*kbn
;
1660 kbn
= (struct vms_kbn
*)(kbn_blk
+ VMS_BLOCK_SIZE
- kbn_sz
);
1664 /* Save the rfa of the first chunk. */
1666 key_off
= VMS_BLOCK_SIZE
- kbn_sz
;
1669 bfd_putl16 (kl_chunk
, kbn
->keylen
);
1672 /* No next chunk. */
1673 bfd_putl32 (0, kbn
->rfa
.vbn
);
1674 bfd_putl16 (0, kbn
->rfa
.offset
);
1678 /* Next chunk will be at the start of the next block. */
1679 bfd_putl32 (*vbn
, kbn
->rfa
.vbn
);
1680 bfd_putl16 (2, kbn
->rfa
.offset
);
1682 memcpy ((char *)(kbn
+ 1), key
, kl_chunk
);
1686 kl_chunk
= (kl_chunk
+ 1) & ~1; /* Always align. */
1687 kbn_sz
-= kl_chunk
+ sizeof (struct vms_kbn
);
1692 /* Check if a block might overflow. In this case we will flush this
1693 block and all the blocks below it. */
1694 for (j
= 0; j
< level
; j
++)
1695 if (blk
[j
].len
+ blk
[j
].lastlen
+ idxlen
> INDEXDEF__BLKSIZ
)
1698 for (j
= 0; j
< level
; j
++)
1702 /* There is not enough room to write the new entry in this
1703 block or in a parent block. */
1707 BFD_ASSERT (level
< MAX_LEVEL
);
1709 /* Need to create a parent. */
1712 rblk
[level
] = bfd_zmalloc (sizeof (struct vms_indexdef
));
1713 bfd_putl32 (*vbn
, rblk
[j
]->parent
);
1715 blk
[level
].vbn
= (*vbn
)++;
1717 blk
[level
].lastlen
= 0;
1722 /* Update parent block: write the new entry. */
1725 struct vms_rfa
*rfa
;
1727 /* Copy the whole entry. */
1728 memcpy (rblk
[j
+ 1]->keys
+ blk
[j
+ 1].len
,
1729 rblk
[j
]->keys
+ blk
[j
].len
,
1731 /* Fix the entry (which in always the first field of an
1733 rfa
= (struct vms_rfa
*)(rblk
[j
+ 1]->keys
+ blk
[j
+ 1].len
);
1734 bfd_putl32 (blk
[j
].vbn
, rfa
->vbn
);
1735 bfd_putl16 (RFADEF__C_INDEX
, rfa
->offset
);
1740 /* And allocate it. Do it only on the block that won't be
1741 flushed (so that the parent of the parent can be
1743 blk
[j
+ 1].len
+= blk
[j
].lastlen
;
1744 blk
[j
+ 1].lastlen
= 0;
1747 /* Write this block on the disk. */
1750 bfd_putl16 (blk
[j
].len
+ blk
[j
].lastlen
, rblk
[j
]->used
);
1751 if (vms_write_block (abfd
, blk
[j
].vbn
, rblk
[j
]) != TRUE
)
1755 /* Reset this block. */
1758 blk
[j
].vbn
= (*vbn
)++;
1761 /* Append it to the block. */
1764 blk
[j
].len
+= blk
[j
].lastlen
;
1768 struct vms_rfa
*rfa
;
1770 rfa
= (struct vms_rfa
*)(rblk
[j
]->keys
+ blk
[j
].len
);
1771 bfd_putl32 ((idx
->abfd
->proxy_origin
/ VMS_BLOCK_SIZE
) + 1,
1774 ((idx
->abfd
->proxy_origin
% VMS_BLOCK_SIZE
)
1775 + (is_elfidx
? 0 : DATA__DATA
),
1780 /* Use elfidx format. */
1781 struct vms_elfidx
*en
= (struct vms_elfidx
*)rfa
;
1786 /* Long symbol name. */
1787 struct vms_kbn
*k
= (struct vms_kbn
*)(en
->keyname
);
1788 bfd_putl16 (sizeof (struct vms_kbn
), en
->keylen
);
1789 bfd_putl16 (idx
->namlen
, k
->keylen
);
1790 bfd_putl32 (key_vbn
, k
->rfa
.vbn
);
1791 bfd_putl16 (key_off
, k
->rfa
.offset
);
1792 en
->flags
|= ELFIDX__SYMESC
;
1796 bfd_putl16 (idx
->namlen
, en
->keylen
);
1797 memcpy (en
->keyname
, idx
->name
, idx
->namlen
);
1802 /* Use idx format. */
1803 struct vms_idx
*en
= (struct vms_idx
*)rfa
;
1804 en
->keylen
= idx
->namlen
;
1805 memcpy (en
->keyname
, idx
->name
, idx
->namlen
);
1810 blk
[j
].lastlen
= idxlen
;
1815 *topvbn
= blk
[level
- 1].vbn
;
1821 for (j
= 1; j
< level
; j
++)
1823 /* Update parent block: write the new entry. */
1826 struct vms_rfa
*rfa
;
1828 en
= rblk
[j
- 1]->keys
+ blk
[j
- 1].len
;
1829 par
= rblk
[j
]->keys
+ blk
[j
].len
;
1830 memcpy (par
, en
, blk
[j
- 1].lastlen
);
1831 rfa
= (struct vms_rfa
*)par
;
1832 bfd_putl32 (blk
[j
- 1].vbn
, rfa
->vbn
);
1833 bfd_putl16 (RFADEF__C_INDEX
, rfa
->offset
);
1836 for (j
= 0; j
< level
; j
++)
1838 /* Write this block on the disk. */
1839 bfd_putl16 (blk
[j
].len
+ blk
[j
].lastlen
, rblk
[j
]->used
);
1840 if (vms_write_block (abfd
, blk
[j
].vbn
, rblk
[j
]) != TRUE
)
1846 /* Write the last kbn (if any). */
1849 if (vms_write_block (abfd
, kbn_vbn
, kbn_blk
) != TRUE
)
1856 /* Append data to the data block DATA. Force write if PAD is true. */
1859 vms_write_data_block (bfd
*arch
, struct vms_datadef
*data
, file_ptr
*off
,
1860 const unsigned char *buf
, unsigned int len
, int pad
)
1862 while (len
> 0 || pad
)
1864 unsigned int doff
= *off
& (VMS_BLOCK_SIZE
- 1);
1865 unsigned int remlen
= (DATA__LENGTH
- DATA__DATA
) - doff
;
1868 l
= (len
> remlen
) ? remlen
: len
;
1869 memcpy (data
->data
+ doff
, buf
, l
);
1875 if (doff
== (DATA__LENGTH
- DATA__DATA
) || (len
== 0 && pad
))
1879 bfd_putl32 ((*off
/ VMS_BLOCK_SIZE
) + 2, data
->link
);
1881 if (bfd_bwrite (data
, sizeof (*data
), arch
) != sizeof (*data
))
1884 *off
+= DATA__LENGTH
- doff
;
1893 /* Build the symbols index. */
1896 _bfd_vms_lib_build_map (unsigned int nbr_modules
,
1897 struct lib_index
*modules
,
1898 unsigned int *res_cnt
,
1899 struct lib_index
**res
)
1902 asymbol
**syms
= NULL
;
1904 struct lib_index
*map
= NULL
;
1905 unsigned int map_max
= 1024; /* Fine initial default. */
1906 unsigned int map_count
= 0;
1908 map
= (struct lib_index
*) bfd_malloc (map_max
* sizeof (struct lib_index
));
1912 /* Gather symbols. */
1913 for (i
= 0; i
< nbr_modules
; i
++)
1918 bfd
*current
= modules
[i
].abfd
;
1920 if ((bfd_get_file_flags (current
) & HAS_SYMS
) == 0)
1923 storage
= bfd_get_symtab_upper_bound (current
);
1929 if (storage
> syms_max
)
1934 syms
= (asymbol
**) bfd_malloc (syms_max
);
1938 symcount
= bfd_canonicalize_symtab (current
, syms
);
1942 /* Now map over all the symbols, picking out the ones we
1944 for (src_count
= 0; src_count
< symcount
; src_count
++)
1946 flagword flags
= (syms
[src_count
])->flags
;
1947 asection
*sec
= syms
[src_count
]->section
;
1949 if ((flags
& BSF_GLOBAL
1951 || flags
& BSF_INDIRECT
1952 || bfd_is_com_section (sec
))
1953 && ! bfd_is_und_section (sec
))
1955 struct lib_index
*new_map
;
1957 /* This symbol will go into the archive header. */
1958 if (map_count
== map_max
)
1961 new_map
= (struct lib_index
*)
1962 bfd_realloc (map
, map_max
* sizeof (struct lib_index
));
1963 if (new_map
== NULL
)
1968 map
[map_count
].abfd
= current
;
1969 map
[map_count
].namlen
= strlen (syms
[src_count
]->name
);
1970 map
[map_count
].name
= syms
[src_count
]->name
;
1978 *res_cnt
= map_count
;
1990 /* Do the hard work: write an archive on the disk. */
1993 _bfd_vms_lib_write_archive_contents (bfd
*arch
)
1996 unsigned int nbr_modules
;
1997 struct lib_index
*modules
;
1998 unsigned int nbr_symbols
;
1999 struct lib_index
*symbols
;
2000 struct lib_tdata
*tdata
= bfd_libdata (arch
);
2003 unsigned int nbr_mod_iblk
;
2004 unsigned int nbr_sym_iblk
;
2006 unsigned int mod_idx_vbn
;
2007 unsigned int sym_idx_vbn
;
2008 bfd_boolean is_elfidx
= tdata
->kind
== vms_lib_ia64
;
2010 /* Count the number of modules (and do a first sanity check). */
2012 for (current
= arch
->archive_head
;
2014 current
= current
->archive_next
)
2016 /* This check is checking the bfds for the objects we're reading
2017 from (which are usually either an object file or archive on
2018 disk), not the archive entries we're writing to. We don't
2019 actually create bfds for the archive members, we just copy
2020 them byte-wise when we write out the archive. */
2021 if (bfd_write_p (current
) || !bfd_check_format (current
, bfd_object
))
2023 bfd_set_error (bfd_error_invalid_operation
);
2030 /* Build the modules list. */
2031 BFD_ASSERT (tdata
->modules
== NULL
);
2032 modules
= bfd_alloc (arch
, nbr_modules
* sizeof (struct lib_index
));
2033 if (modules
== NULL
)
2036 for (current
= arch
->archive_head
, i
= 0;
2038 current
= current
->archive_next
, i
++)
2042 modules
[i
].abfd
= current
;
2043 modules
[i
].name
= vms_get_module_name (current
->filename
, FALSE
);
2046 /* FIXME: silently truncate long names ? */
2047 nl
= strlen (modules
[i
].name
);
2048 modules
[i
].namlen
= (nl
> MAX_KEYLEN
? MAX_KEYLEN
: nl
);
2051 /* Create the module index. */
2053 if (!vms_write_index (NULL
, modules
, nbr_modules
, &vbn
, NULL
, is_elfidx
))
2057 /* Create symbol index. */
2058 if (!_bfd_vms_lib_build_map (nbr_modules
, modules
, &nbr_symbols
, &symbols
))
2062 if (!vms_write_index (NULL
, symbols
, nbr_symbols
, &vbn
, NULL
, is_elfidx
))
2066 /* Write modules and remember their position. */
2067 off
= (1 + nbr_mod_iblk
+ nbr_sym_iblk
) * VMS_BLOCK_SIZE
;
2069 if (bfd_seek (arch
, off
, SEEK_SET
) != 0)
2072 for (i
= 0; i
< nbr_modules
; i
++)
2074 struct vms_datadef data
;
2075 unsigned char blk
[VMS_BLOCK_SIZE
];
2076 struct vms_mhd
*mhd
;
2079 current
= modules
[i
].abfd
;
2080 current
->proxy_origin
= off
;
2086 /* Write the MHD as a record (ie, size first). */
2088 bfd_putl16 (tdata
->mhd_size
, blk
);
2090 mhd
= (struct vms_mhd
*)(blk
+ sz
);
2091 memset (mhd
, 0, sizeof (struct vms_mhd
));
2093 mhd
->id
= MHD__C_MHDID
;
2095 memcpy (mhd
->objid
, "V1.0", 4);
2096 bfd_putl32 (modules
[i
].ref
, mhd
->refcnt
);
2099 sz
+= tdata
->mhd_size
;
2102 /* Rewind the member to be put into the archive. */
2103 if (bfd_seek (current
, 0, SEEK_SET
) != 0)
2106 /* Copy the member into the archive. */
2109 unsigned int modsize
= 0;
2111 file_ptr off_hdr
= off
;
2113 /* Read to complete the first block. */
2114 amt
= bfd_bread (blk
+ sz
, VMS_BLOCK_SIZE
- sz
, current
);
2115 if (amt
== (bfd_size_type
)-1)
2118 if (amt
< VMS_BLOCK_SIZE
- sz
)
2120 /* The member size is less than a block. Pad the block. */
2121 memset (blk
+ sz
+ amt
, 0, VMS_BLOCK_SIZE
- sz
- amt
);
2123 bfd_putl32 (modsize
, mhd
->modsize
);
2125 /* Write the first block (which contains an mhd). */
2126 if (bfd_bwrite (blk
, VMS_BLOCK_SIZE
, arch
) != VMS_BLOCK_SIZE
)
2128 off
+= VMS_BLOCK_SIZE
;
2130 if (amt
== VMS_BLOCK_SIZE
- sz
)
2132 /* Copy the remaining. */
2133 char buffer
[DEFAULT_BUFFERSIZE
];
2137 amt
= bfd_bread (buffer
, sizeof (buffer
), current
);
2138 if (amt
== (bfd_size_type
)-1)
2143 if (amt
!= sizeof (buffer
))
2145 /* Clear the padding. */
2146 memset (buffer
+ amt
, 0, sizeof (buffer
) - amt
);
2147 amt
= (amt
+ VMS_BLOCK_SIZE
) & ~(VMS_BLOCK_SIZE
- 1);
2149 if (bfd_bwrite (buffer
, amt
, arch
) != amt
)
2154 /* Now that the size is known, write the first block (again). */
2155 bfd_putl32 (modsize
, mhd
->modsize
);
2156 if (bfd_seek (arch
, off_hdr
, SEEK_SET
) != 0
2157 || bfd_bwrite (blk
, VMS_BLOCK_SIZE
, arch
) != VMS_BLOCK_SIZE
)
2159 if (bfd_seek (arch
, off
, SEEK_SET
) != 0)
2165 /* Write the MHD. */
2166 if (vms_write_data_block (arch
, &data
, &off
, blk
, sz
, 0) < 0)
2169 /* Write the member. */
2172 sz
= bfd_bread (blk
, sizeof (blk
), current
);
2175 if (vms_write_data_block (arch
, &data
, &off
, blk
, sz
, 0) < 0)
2179 /* Write the end of module marker. */
2180 if (vms_write_data_block (arch
, &data
, &off
,
2181 eotdesc
, sizeof (eotdesc
), 1) < 0)
2186 /* Write the indexes. */
2188 if (vms_write_index (arch
, modules
, nbr_modules
, &vbn
, &mod_idx_vbn
,
2191 if (vms_write_index (arch
, symbols
, nbr_symbols
, &vbn
, &sym_idx_vbn
,
2195 /* Write libary header. */
2197 unsigned char blk
[VMS_BLOCK_SIZE
];
2198 struct vms_lhd
*lhd
= (struct vms_lhd
*)blk
;
2199 struct vms_idd
*idd
= (struct vms_idd
*)(blk
+ sizeof (*lhd
));
2200 unsigned int idd_flags
;
2201 unsigned int saneid
;
2203 memset (blk
, 0, sizeof (blk
));
2205 lhd
->type
= tdata
->type
;
2207 switch (tdata
->kind
)
2210 saneid
= LHD_SANEID3
;
2213 saneid
= LHD_SANEID6
;
2218 bfd_putl32 (saneid
, lhd
->sanity
);
2219 bfd_putl16 (tdata
->ver
, lhd
->majorid
);
2220 bfd_putl16 (0, lhd
->minorid
);
2221 snprintf ((char *)lhd
->lbrver
+ 1, sizeof (lhd
->lbrver
) - 1,
2223 (unsigned)(BFD_VERSION
/ 100000000UL),
2224 (unsigned)(BFD_VERSION
/ 1000000UL) % 100,
2225 (unsigned)(BFD_VERSION
/ 10000UL) % 100);
2226 lhd
->lbrver
[sizeof (lhd
->lbrver
) - 1] = 0;
2227 lhd
->lbrver
[0] = strlen ((char *)lhd
->lbrver
+ 1);
2229 bfd_putl32 (tdata
->credat_lo
, lhd
->credat
+ 0);
2230 bfd_putl32 (tdata
->credat_hi
, lhd
->credat
+ 4);
2231 vms_raw_get_time (lhd
->updtim
);
2233 lhd
->mhdusz
= tdata
->mhd_size
- MHD__C_USRDAT
;
2235 bfd_putl32 (nbr_modules
+ nbr_symbols
, lhd
->idxcnt
);
2236 bfd_putl32 (nbr_modules
, lhd
->modcnt
);
2237 bfd_putl32 (nbr_modules
, lhd
->modhdrs
);
2239 bfd_putl32 (vbn
- 1, lhd
->hipreal
);
2240 bfd_putl32 (vbn
- 1, lhd
->hiprusd
);
2242 /* First index (modules name). */
2243 idd_flags
= IDD__FLAGS_ASCII
| IDD__FLAGS_VARLENIDX
2244 | IDD__FLAGS_NOCASECMP
| IDD__FLAGS_NOCASENTR
;
2245 bfd_putl16 (idd_flags
, idd
->flags
);
2246 bfd_putl16 (MAX_KEYLEN
, idd
->keylen
);
2247 bfd_putl16 (mod_idx_vbn
, idd
->vbn
);
2250 /* Second index (symbols name). */
2251 bfd_putl16 (idd_flags
, idd
->flags
);
2252 bfd_putl16 (MAX_KEYLEN
, idd
->keylen
);
2253 bfd_putl16 (sym_idx_vbn
, idd
->vbn
);
2256 if (vms_write_block (arch
, 1, blk
) != TRUE
)
2263 bfd_set_error (bfd_error_on_input
, current
, bfd_get_error ());
2267 /* Add a target for text library. This costs almost nothing and is useful to
2268 read VMS library on the host. */
2270 const bfd_target vms_lib_txt_vec
=
2272 "vms-libtxt", /* Name. */
2273 bfd_target_unknown_flavour
,
2274 BFD_ENDIAN_UNKNOWN
, /* byteorder */
2275 BFD_ENDIAN_UNKNOWN
, /* header_byteorder */
2276 0, /* Object flags. */
2277 0, /* Sect flags. */
2278 0, /* symbol_leading_char. */
2279 ' ', /* ar_pad_char. */
2280 15, /* ar_max_namelen. */
2281 0, /* match priority. */
2282 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2283 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2284 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
,
2285 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2286 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2287 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
,
2289 {_bfd_dummy_target
, _bfd_dummy_target
, /* bfd_check_format. */
2290 _bfd_vms_lib_txt_archive_p
, _bfd_dummy_target
},
2291 {bfd_false
, bfd_false
, bfd_false
, bfd_false
}, /* bfd_set_format. */
2292 {bfd_false
, bfd_false
, bfd_false
, bfd_false
}, /* bfd_write_contents. */
2294 BFD_JUMP_TABLE_GENERIC (_bfd_generic
),
2295 BFD_JUMP_TABLE_COPY (_bfd_generic
),
2296 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2297 BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib
),
2298 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols
),
2299 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
2300 BFD_JUMP_TABLE_WRITE (_bfd_nowrite
),
2301 BFD_JUMP_TABLE_LINK (_bfd_nolink
),
2302 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),