1 /* BFD back-end for VMS archive files.
3 Copyright (C) 2010-2020 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"
28 #include "libiberty.h"
33 /* The standard VMS disk block size. */
34 #ifndef VMS_BLOCK_SIZE
35 #define VMS_BLOCK_SIZE 512
38 /* Maximum key length (which is also the maximum symbol length in archive). */
39 #define MAX_KEYLEN 128
40 #define MAX_EKEYLEN 1024
46 unsigned char min_char
;
47 unsigned char max_char
;
53 /* Kind of library. Used to filter in archive_p. */
63 /* Back-end private data. */
67 /* Standard tdata for an archive. But we don't use many fields. */
68 struct artdata artdata
;
73 /* Type of the archive. */
76 /* Kind of archive. Summary of its type. */
77 enum vms_lib_kind kind
;
79 /* Total size of the mhd (element header). */
80 unsigned int mhd_size
;
83 unsigned int credat_lo
;
84 unsigned int credat_hi
;
86 /* Vector of modules (archive elements), already sorted. */
87 unsigned int nbr_modules
;
88 struct carsym
*modules
;
91 /* DCX (decompression) data. */
92 unsigned int nbr_dcxsbm
;
93 struct dcxsbm_desc
*dcxsbm
;
96 #define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
98 /* End-Of-Text pattern. This is a special record to mark the end of file. */
100 static const unsigned char eotdesc
[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
102 /* Describe the current state of carsym entries while building the archive
103 table of content. Things are simple with Alpha archives as the number
104 of entries is known, but with IA64 archives a entry can make a reference
105 to severals members. Therefore we must be able to extend the table on the
106 fly, but it should be allocated on the bfd - which doesn't support realloc.
107 To reduce the overhead, the table is initially allocated in the BFD's
108 objalloc and extended if necessary on the heap. In the later case, it
109 is finally copied to the BFD's objalloc so that it will automatically be
114 /* The table of content. */
117 /* Number of entries used in the table. */
120 /* Maximum number of entries. */
123 /* Do not allocate more that this number of entries. */
126 /* If true, the table was reallocated on the heap. If false, it is still
127 in the BFD's objalloc. */
128 bfd_boolean realloced
;
131 /* Simply add a name to the index. */
134 vms_add_index (struct carsym_mem
*cs
, char *name
,
135 unsigned int idx_vbn
, unsigned int idx_off
)
137 if (cs
->nbr
== cs
->max
)
142 if (cs
->max
> -33u / 2 || cs
->max
>= cs
->limit
)
144 bfd_set_error (bfd_error_file_too_big
);
147 cs
->max
= 2 * cs
->max
+ 32;
148 if (cs
->max
> cs
->limit
)
150 if (_bfd_mul_overflow (cs
->max
, sizeof (struct carsym
), &amt
))
152 bfd_set_error (bfd_error_file_too_big
);
158 n
= bfd_malloc (amt
);
161 memcpy (n
, cs
->idx
, cs
->nbr
* sizeof (struct carsym
));
162 /* And unfortunately we can't free cs->idx. */
166 n
= bfd_realloc_or_free (cs
->idx
, amt
);
171 cs
->realloced
= TRUE
;
173 cs
->idx
[cs
->nbr
].file_offset
= (idx_vbn
- 1) * VMS_BLOCK_SIZE
+ idx_off
;
174 cs
->idx
[cs
->nbr
].name
= name
;
179 /* Follow all member of a lns list (pointed by RFA) and add indexes for
180 NAME. Return FALSE in case of error. */
183 vms_add_indexes_from_list (bfd
*abfd
, struct carsym_mem
*cs
, char *name
,
192 vbn
= bfd_getl32 (rfa
->vbn
);
197 off
= (vbn
- 1) * VMS_BLOCK_SIZE
+ bfd_getl16 (rfa
->offset
);
198 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
199 || bfd_bread (&lns
, sizeof (lns
), abfd
) != sizeof (lns
))
202 if (!vms_add_index (cs
, name
,
203 bfd_getl32 (lns
.modrfa
.vbn
),
204 bfd_getl16 (lns
.modrfa
.offset
)))
211 /* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
214 vms_read_block (bfd
*abfd
, unsigned int vbn
, void *blk
)
218 off
= (vbn
- 1) * VMS_BLOCK_SIZE
;
219 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
220 || bfd_bread (blk
, VMS_BLOCK_SIZE
, abfd
) != VMS_BLOCK_SIZE
)
226 /* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
229 vms_write_block (bfd
*abfd
, unsigned int vbn
, void *blk
)
233 off
= (vbn
- 1) * VMS_BLOCK_SIZE
;
234 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
235 || bfd_bwrite (blk
, VMS_BLOCK_SIZE
, abfd
) != VMS_BLOCK_SIZE
)
241 /* Read index block VBN and put the entry in **IDX (which is updated).
242 If the entry is indirect, recurse. */
245 vms_traverse_index (bfd
*abfd
, unsigned int vbn
, struct carsym_mem
*cs
,
246 unsigned int recur_count
)
248 struct vms_indexdef indexdef
;
254 if (recur_count
== 100)
256 bfd_set_error (bfd_error_bad_value
);
260 /* Read the index block. */
261 BFD_ASSERT (sizeof (indexdef
) == VMS_BLOCK_SIZE
);
262 if (!vms_read_block (abfd
, vbn
, &indexdef
))
266 p
= &indexdef
.keys
[0];
267 n
= bfd_getl16 (indexdef
.used
);
268 if (n
> sizeof (indexdef
.keys
))
273 unsigned int idx_vbn
;
274 unsigned int idx_off
;
276 unsigned char *keyname
;
279 /* Extract key length. */
280 if (bfd_libdata (abfd
)->ver
== LBR_MAJORID
)
282 struct vms_idx
*ridx
= (struct vms_idx
*)p
;
284 idx_vbn
= bfd_getl32 (ridx
->rfa
.vbn
);
285 idx_off
= bfd_getl16 (ridx
->rfa
.offset
);
287 keylen
= ridx
->keylen
;
289 keyname
= ridx
->keyname
;
291 else if (bfd_libdata (abfd
)->ver
== LBR_ELFMAJORID
)
293 struct vms_elfidx
*ridx
= (struct vms_elfidx
*)p
;
295 idx_vbn
= bfd_getl32 (ridx
->rfa
.vbn
);
296 idx_off
= bfd_getl16 (ridx
->rfa
.offset
);
298 keylen
= bfd_getl16 (ridx
->keylen
);
300 keyname
= ridx
->keyname
;
309 /* Point to the next index entry. */
310 p
= keyname
+ keylen
;
314 if (idx_off
== RFADEF__C_INDEX
)
316 /* Indirect entry. Recurse. */
317 if (!vms_traverse_index (abfd
, idx_vbn
, cs
, recur_count
+ 1))
322 /* Add a new entry. */
325 if (flags
& ELFIDX__SYMESC
)
327 /* Extended key name. */
328 unsigned int noff
= 0;
332 unsigned char kblk
[VMS_BLOCK_SIZE
];
335 if (keylen
!= sizeof (struct vms_kbn
))
338 kbn
= (struct vms_kbn
*)keyname
;
339 keylen
= bfd_getl16 (kbn
->keylen
);
341 name
= bfd_alloc (abfd
, keylen
+ 1);
344 kvbn
= bfd_getl32 (kbn
->rfa
.vbn
);
345 koff
= bfd_getl16 (kbn
->rfa
.offset
);
347 /* Read the key, chunk by chunk. */
352 if (!vms_read_block (abfd
, kvbn
, kblk
))
354 if (koff
> sizeof (kblk
) - sizeof (struct vms_kbn
))
356 kbn
= (struct vms_kbn
*)(kblk
+ koff
);
357 klen
= bfd_getl16 (kbn
->keylen
);
358 if (klen
> sizeof (kblk
) - koff
)
360 kvbn
= bfd_getl32 (kbn
->rfa
.vbn
);
361 koff
= bfd_getl16 (kbn
->rfa
.offset
);
363 if (noff
+ klen
> keylen
)
365 memcpy (name
+ noff
, kbn
+ 1, klen
);
376 /* Usual key name. */
377 name
= bfd_alloc (abfd
, keylen
+ 1);
381 memcpy (name
, keyname
, keylen
);
385 if (flags
& ELFIDX__LISTRFA
)
390 off
= (idx_vbn
- 1) * VMS_BLOCK_SIZE
+ idx_off
;
391 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
392 || bfd_bread (&lhs
, sizeof (lhs
), abfd
) != sizeof (lhs
))
395 /* These extra entries may cause reallocation of CS. */
396 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.ng_g_rfa
))
398 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.ng_wk_rfa
))
400 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.g_g_rfa
))
402 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.g_wk_rfa
))
407 if (!vms_add_index (cs
, name
, idx_vbn
, idx_off
))
416 /* Read index #IDX, which must have NBREL entries. */
418 static struct carsym
*
419 vms_lib_read_index (bfd
*abfd
, int idx
, unsigned int *nbrel
)
426 struct carsym
*csbuf
;
427 struct carsym_mem csm
;
429 /* Read index desription. */
430 if (bfd_seek (abfd
, LHD_IDXDESC
+ idx
* IDD_LENGTH
, SEEK_SET
) != 0
431 || bfd_bread (&idd
, sizeof (idd
), abfd
) != sizeof (idd
))
435 flags
= bfd_getl16 (idd
.flags
);
436 if (!(flags
& IDD__FLAGS_ASCII
)
437 || !(flags
& IDD__FLAGS_VARLENIDX
))
440 filesize
= bfd_get_file_size (abfd
);
444 csm
.realloced
= FALSE
;
447 /* Put an upper bound based on a file full of single char keys.
448 This is to prevent fuzzed binary silliness. It is easily
449 possible to set up loops over file blocks that add syms
451 if (filesize
/ (sizeof (struct vms_rfa
) + 2) <= -1u)
452 csm
.limit
= filesize
/ (sizeof (struct vms_rfa
) + 2);
454 if (csm
.max
> csm
.limit
)
456 if (_bfd_mul_overflow (csm
.max
, sizeof (struct carsym
), &amt
))
458 csm
.idx
= csbuf
= bfd_alloc (abfd
, amt
);
462 /* Note: if the index is empty, there is no block to traverse. */
463 vbn
= bfd_getl32 (idd
.vbn
);
464 if (vbn
!= 0 && !vms_traverse_index (abfd
, vbn
, &csm
, 0))
469 /* Note: in case of error, we can free what was allocated on the
471 bfd_release (abfd
, csbuf
);
477 /* There are more entries than the first estimate. Allocate on
478 the BFD's objalloc. */
479 csbuf
= bfd_alloc (abfd
, csm
.nbr
* sizeof (struct carsym
));
482 memcpy (csbuf
, csm
.idx
, csm
.nbr
* sizeof (struct carsym
));
490 /* Standard function. */
493 _bfd_vms_lib_archive_p (bfd
*abfd
, enum vms_lib_kind kind
)
497 unsigned int majorid
;
498 struct lib_tdata
*tdata_hold
;
499 struct lib_tdata
*tdata
;
501 unsigned int nbr_ent
;
504 if (bfd_bread (&lhd
, sizeof (lhd
), abfd
) != sizeof (lhd
))
506 if (bfd_get_error () != bfd_error_system_call
)
507 bfd_set_error (bfd_error_wrong_format
);
511 /* Check sanity (= magic) number. */
512 sanity
= bfd_getl32 (lhd
.sanity
);
513 if (!(sanity
== LHD_SANEID3
514 || sanity
== LHD_SANEID6
515 || sanity
== LHD_SANEID_DCX
))
517 bfd_set_error (bfd_error_wrong_format
);
520 majorid
= bfd_getl32 (lhd
.majorid
);
522 /* Check archive kind. */
526 if ((lhd
.type
!= LBR__C_TYP_EOBJ
&& lhd
.type
!= LBR__C_TYP_ESHSTB
)
527 || majorid
!= LBR_MAJORID
530 bfd_set_error (bfd_error_wrong_format
);
535 if ((lhd
.type
!= LBR__C_TYP_IOBJ
&& lhd
.type
!= LBR__C_TYP_ISHSTB
)
536 || majorid
!= LBR_ELFMAJORID
539 bfd_set_error (bfd_error_wrong_format
);
544 if ((lhd
.type
!= LBR__C_TYP_TXT
545 && lhd
.type
!= LBR__C_TYP_MLB
546 && lhd
.type
!= LBR__C_TYP_HLP
)
547 || majorid
!= LBR_MAJORID
550 bfd_set_error (bfd_error_wrong_format
);
558 /* Allocate and initialize private data. */
559 tdata_hold
= bfd_libdata (abfd
);
560 tdata
= (struct lib_tdata
*) bfd_zalloc (abfd
, sizeof (struct lib_tdata
));
563 abfd
->tdata
.any
= (void *)tdata
;
564 tdata
->ver
= majorid
;
565 tdata
->mhd_size
= MHD__C_USRDAT
+ lhd
.mhdusz
;
566 tdata
->type
= lhd
.type
;
568 tdata
->credat_lo
= bfd_getl32 (lhd
.credat
+ 0);
569 tdata
->credat_hi
= bfd_getl32 (lhd
.credat
+ 4);
572 tdata
->nbr_modules
= bfd_getl32 (lhd
.modcnt
);
573 tdata
->artdata
.symdef_count
= bfd_getl32 (lhd
.idxcnt
) - tdata
->nbr_modules
;
574 nbr_ent
= tdata
->nbr_modules
;
575 tdata
->modules
= vms_lib_read_index (abfd
, 0, &nbr_ent
);
576 if (tdata
->modules
== NULL
|| nbr_ent
!= tdata
->nbr_modules
)
580 nbr_ent
= tdata
->artdata
.symdef_count
;
581 tdata
->artdata
.symdefs
= vms_lib_read_index (abfd
, 1, &nbr_ent
);
582 if (tdata
->artdata
.symdefs
== NULL
)
584 /* Only IA64 archives may have more entries in the index that what
586 if (nbr_ent
!= tdata
->artdata
.symdef_count
587 && kind
!= vms_lib_ia64
)
589 tdata
->artdata
.symdef_count
= nbr_ent
;
591 tdata
->cache
= bfd_zalloc (abfd
, sizeof (bfd
*) * tdata
->nbr_modules
);
592 if (tdata
->cache
== NULL
)
595 /* Read DCX submaps. */
596 dcxvbn
= bfd_getl32 (lhd
.dcxmapvbn
);
599 unsigned char buf_reclen
[4];
602 struct vms_dcxmap
*map
;
603 unsigned int sbm_off
;
606 if (bfd_seek (abfd
, (dcxvbn
- 1) * VMS_BLOCK_SIZE
, SEEK_SET
) != 0
607 || bfd_bread (buf_reclen
, sizeof (buf_reclen
), abfd
)
608 != sizeof (buf_reclen
))
610 reclen
= bfd_getl32 (buf_reclen
);
611 if (reclen
< sizeof (struct vms_dcxmap
))
613 buf
= _bfd_malloc_and_read (abfd
, reclen
, reclen
);
616 map
= (struct vms_dcxmap
*)buf
;
617 tdata
->nbr_dcxsbm
= bfd_getl16 (map
->nsubs
);
618 sbm_off
= bfd_getl16 (map
->sub0
);
619 tdata
->dcxsbm
= (struct dcxsbm_desc
*)bfd_alloc
620 (abfd
, tdata
->nbr_dcxsbm
* sizeof (struct dcxsbm_desc
));
621 for (i
= 0; i
< tdata
->nbr_dcxsbm
; i
++)
623 struct vms_dcxsbm
*sbm
;
624 struct dcxsbm_desc
*sbmdesc
= &tdata
->dcxsbm
[i
];
625 unsigned int sbm_len
;
632 || reclen
- sbm_off
< sizeof (struct vms_dcxsbm
))
638 sbm
= (struct vms_dcxsbm
*) (buf
+ sbm_off
);
639 sbm_sz
= bfd_getl16 (sbm
->size
);
641 if (sbm_off
> reclen
)
644 sbmdesc
->min_char
= sbm
->min_char
;
645 BFD_ASSERT (sbmdesc
->min_char
== 0);
646 sbmdesc
->max_char
= sbm
->max_char
;
647 sbm_len
= sbmdesc
->max_char
- sbmdesc
->min_char
+ 1;
648 l
= (2 * sbm_len
+ 7) / 8;
649 if (sbm_sz
< sizeof (struct vms_dcxsbm
) + l
+ sbm_len
650 || (tdata
->nbr_dcxsbm
> 1
651 && sbm_sz
< sizeof (struct vms_dcxsbm
) + l
+ 3 * sbm_len
))
653 sbmdesc
->flags
= (unsigned char *)bfd_alloc (abfd
, l
);
654 off
= bfd_getl16 (sbm
->flags
);
658 memcpy (sbmdesc
->flags
, (bfd_byte
*) sbm
+ off
, l
);
659 sbmdesc
->nodes
= (unsigned char *)bfd_alloc (abfd
, 2 * sbm_len
);
660 off
= bfd_getl16 (sbm
->nodes
);
662 || sbm_sz
- off
< 2 * sbm_len
)
664 memcpy (sbmdesc
->nodes
, (bfd_byte
*) sbm
+ off
, 2 * sbm_len
);
665 off
= bfd_getl16 (sbm
->next
);
669 || sbm_sz
- off
< 2 * sbm_len
)
671 /* Read the 'next' array. */
672 sbmdesc
->next
= (unsigned short *) bfd_alloc (abfd
, 2 * sbm_len
);
673 buf1
= (bfd_byte
*) sbm
+ off
;
674 for (j
= 0; j
< sbm_len
; j
++)
675 sbmdesc
->next
[j
] = bfd_getl16 (buf1
+ j
* 2);
679 /* There is no next array if there is only one submap. */
680 BFD_ASSERT (tdata
->nbr_dcxsbm
== 1);
681 sbmdesc
->next
= NULL
;
688 tdata
->nbr_dcxsbm
= 0;
691 /* The map is always present. Also mark shared image library. */
692 abfd
->has_armap
= TRUE
;
693 if (tdata
->type
== LBR__C_TYP_ESHSTB
|| tdata
->type
== LBR__C_TYP_ISHSTB
)
694 abfd
->is_thin_archive
= TRUE
;
696 return _bfd_no_cleanup
;
699 bfd_release (abfd
, tdata
);
700 abfd
->tdata
.any
= (void *)tdata_hold
;
704 /* Standard function for alpha libraries. */
707 _bfd_vms_lib_alpha_archive_p (bfd
*abfd
)
709 return _bfd_vms_lib_archive_p (abfd
, vms_lib_alpha
);
712 /* Standard function for ia64 libraries. */
715 _bfd_vms_lib_ia64_archive_p (bfd
*abfd
)
717 return _bfd_vms_lib_archive_p (abfd
, vms_lib_ia64
);
720 /* Standard function for text libraries. */
723 _bfd_vms_lib_txt_archive_p (bfd
*abfd
)
725 return _bfd_vms_lib_archive_p (abfd
, vms_lib_txt
);
728 /* Standard bfd function. */
731 _bfd_vms_lib_mkarchive (bfd
*abfd
, enum vms_lib_kind kind
)
733 struct lib_tdata
*tdata
;
735 tdata
= (struct lib_tdata
*) bfd_zalloc (abfd
, sizeof (struct lib_tdata
));
739 abfd
->tdata
.any
= (void *)tdata
;
740 vms_get_time (&tdata
->credat_hi
, &tdata
->credat_lo
);
746 tdata
->ver
= LBR_MAJORID
;
747 tdata
->mhd_size
= offsetof (struct vms_mhd
, pad1
);
748 tdata
->type
= LBR__C_TYP_EOBJ
;
751 tdata
->ver
= LBR_ELFMAJORID
;
752 tdata
->mhd_size
= sizeof (struct vms_mhd
);
753 tdata
->type
= LBR__C_TYP_IOBJ
;
759 tdata
->nbr_modules
= 0;
760 tdata
->artdata
.symdef_count
= 0;
761 tdata
->modules
= NULL
;
762 tdata
->artdata
.symdefs
= NULL
;
769 _bfd_vms_lib_alpha_mkarchive (bfd
*abfd
)
771 return _bfd_vms_lib_mkarchive (abfd
, vms_lib_alpha
);
775 _bfd_vms_lib_ia64_mkarchive (bfd
*abfd
)
777 return _bfd_vms_lib_mkarchive (abfd
, vms_lib_ia64
);
780 /* Find NAME in the symbol index. Return the index. */
783 _bfd_vms_lib_find_symbol (bfd
*abfd
, const char *name
)
785 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
786 carsym
*syms
= tdata
->artdata
.symdefs
;
789 /* Open-coded binary search for speed. */
791 hi
= tdata
->artdata
.symdef_count
- 1;
795 int mid
= lo
+ (hi
- lo
) / 2;
798 diff
= (char)(name
[0] - syms
[mid
].name
[0]);
800 diff
= strcmp (name
, syms
[mid
].name
);
808 return BFD_NO_MORE_SYMBOLS
;
811 /* IO vector for archive member. Need that because members are not linearly
812 stored in archives. */
816 /* Current offset. */
819 /* Length of the module, when known. */
822 /* Current position in the record from bfd_bread point of view (ie, after
823 decompression). 0 means that no data byte have been read, -2 and -1
824 are reserved for the length word. */
826 #define REC_POS_NL -4
827 #define REC_POS_PAD -3
828 #define REC_POS_LEN0 -2
829 #define REC_POS_LEN1 -1
832 unsigned short rec_len
;
833 /* Number of bytes to read in the current record. */
834 unsigned short rec_rem
;
835 /* Offset of the next block. */
837 /* Current *data* offset in the data block. */
838 unsigned short blk_off
;
840 /* Offset of the first block. Extracted from the index. */
841 file_ptr first_block
;
843 /* Initial next_block. Extracted when the MHD is read. */
844 file_ptr init_next_block
;
845 /* Initial blk_off, once the MHD is read. */
846 unsigned short init_blk_off
;
848 /* Used to store any 3 byte record, which could be the EOF pattern. */
849 unsigned char pattern
[4];
852 struct dcxsbm_desc
*dcxsbms
;
853 /* Current submap. */
854 struct dcxsbm_desc
*dcx_sbm
;
855 /* Current offset in the submap. */
856 unsigned int dcx_offset
;
859 /* Compressed buffer. */
860 unsigned char *dcx_buf
;
861 /* Size of the buffer. Used to resize. */
862 unsigned int dcx_max
;
863 /* Number of valid bytes in the buffer. */
864 unsigned int dcx_rlen
;
867 /* Return the current position. */
870 vms_lib_btell (struct bfd
*abfd
)
872 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
876 /* Read the header of the next data block if all bytes of the current block
880 vms_lib_read_block (struct bfd
*abfd
)
882 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
884 if (vec
->blk_off
== DATA__LENGTH
)
886 unsigned char hdr
[DATA__DATA
];
888 /* Read next block. */
889 if (bfd_seek (abfd
->my_archive
, vec
->next_block
, SEEK_SET
) != 0)
891 if (bfd_bread (hdr
, sizeof (hdr
), abfd
->my_archive
) != sizeof (hdr
))
893 vec
->next_block
= (bfd_getl32 (hdr
+ 2) - 1) * VMS_BLOCK_SIZE
;
894 vec
->blk_off
= sizeof (hdr
);
899 /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
900 not stored. Read linearly from the library, but handle blocks. This
901 function does not handle records nor EOF. */
904 vms_lib_bread_raw (struct bfd
*abfd
, unsigned char *buf
, file_ptr nbytes
)
906 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
914 /* Be sure the current data block is read. */
915 if (!vms_lib_read_block (abfd
))
918 /* Do not read past the data block, do not read more than requested. */
919 l
= DATA__LENGTH
- vec
->blk_off
;
926 /* Really read into BUF. */
927 if (bfd_bread (buf
, l
, abfd
->my_archive
) != l
)
932 /* Make as if we are reading. */
933 if (bfd_seek (abfd
->my_archive
, l
, SEEK_CUR
) != 0)
946 /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
949 vms_lib_dcx (struct vms_lib_iovec
*vec
, unsigned char *buf
, file_ptr nbytes
)
951 struct dcxsbm_desc
*sbm
;
957 /* The loop below expect to deliver at least one byte. */
961 /* Get the current state. */
963 offset
= vec
->dcx_offset
;
964 j
= vec
->dcx_pos
& 7;
966 for (i
= vec
->dcx_pos
>> 3; i
< vec
->dcx_rlen
; i
++)
968 unsigned char b
= vec
->dcx_buf
[i
];
974 if (!(sbm
->flags
[offset
>> 3] & (1 << (offset
& 7))))
976 unsigned int n_offset
= sbm
->nodes
[offset
];
979 /* End of buffer. Stay where we are. */
980 vec
->dcx_pos
= (i
<< 3) + j
;
983 vec
->dcx_offset
= offset
;
987 offset
= 2 * n_offset
;
991 unsigned char v
= sbm
->nodes
[offset
];
993 if (sbm
->next
!= NULL
)
994 sbm
= vec
->dcxsbms
+ sbm
->next
[v
];
1005 vec
->dcx_pos
= (i
<< 3) + j
+ 1;
1006 vec
->dcx_offset
= offset
;
1019 /* Standard IOVEC function. */
1022 vms_lib_bread (struct bfd
*abfd
, void *vbuf
, file_ptr nbytes
)
1024 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1027 unsigned char *buf
= (unsigned char *)vbuf
;
1029 /* Do not read past the end. */
1030 if (vec
->where
>= vec
->file_len
)
1036 if (vec
->rec_rem
== 0)
1038 unsigned char blen
[2];
1040 /* Read record length. */
1041 if (vms_lib_bread_raw (abfd
, blen
, sizeof (blen
)) != sizeof (blen
))
1043 vec
->rec_len
= bfd_getl16 (blen
);
1044 if (bfd_libdata (abfd
->my_archive
)->kind
== vms_lib_txt
)
1046 /* Discard record size and align byte. */
1048 vec
->rec_rem
= vec
->rec_len
;
1052 /* Prepend record size. */
1053 vec
->rec_pos
= REC_POS_LEN0
;
1054 vec
->rec_rem
= (vec
->rec_len
+ 1) & ~1; /* With align byte. */
1056 if (vec
->rec_len
== 3)
1058 /* Possibly end of file. Check the pattern. */
1059 if (vms_lib_bread_raw (abfd
, vec
->pattern
, 4) != 4)
1061 if (!memcmp (vec
->pattern
, eotdesc
+ 2, 3))
1063 /* This is really an EOF. */
1065 vec
->file_len
= vec
->where
;
1070 if (vec
->dcxsbms
!= NULL
)
1072 /* This is a compressed member. */
1076 /* Be sure there is enough room for the expansion. */
1077 len
= (vec
->rec_len
+ 1) & ~1;
1078 if (len
> vec
->dcx_max
)
1080 while (len
> vec
->dcx_max
)
1082 vec
->dcx_buf
= bfd_alloc (abfd
, vec
->dcx_max
);
1083 if (vec
->dcx_buf
== NULL
)
1087 /* Read the compressed record. */
1088 vec
->dcx_rlen
= len
;
1089 if (vec
->rec_len
== 3)
1092 memcpy (vec
->dcx_buf
, vec
->pattern
, 3);
1096 elen
= vms_lib_bread_raw (abfd
, vec
->dcx_buf
, len
);
1101 /* Dummy expansion to get the expanded length. */
1102 vec
->dcx_offset
= 0;
1103 vec
->dcx_sbm
= vec
->dcxsbms
;
1105 elen
= vms_lib_dcx (vec
, NULL
, 0x10000);
1108 vec
->rec_len
= elen
;
1109 vec
->rec_rem
= elen
;
1111 /* Reset the state. */
1112 vec
->dcx_offset
= 0;
1113 vec
->dcx_sbm
= vec
->dcxsbms
;
1117 if (vec
->rec_pos
< 0)
1120 switch (vec
->rec_pos
)
1123 c
= vec
->rec_len
& 0xff;
1124 vec
->rec_pos
= REC_POS_LEN1
;
1127 c
= (vec
->rec_len
>> 8) & 0xff;
1151 if (nbytes
> vec
->rec_rem
)
1152 chunk
= vec
->rec_rem
;
1156 if (vec
->dcxsbms
!= NULL
)
1158 /* Optimize the stat() case: no need to decompress again as we
1160 if (!(buf
== NULL
&& chunk
== vec
->rec_rem
))
1161 chunk
= vms_lib_dcx (vec
, buf
, chunk
);
1165 if (vec
->rec_len
== 3)
1168 memcpy (buf
, vec
->pattern
+ vec
->rec_pos
, chunk
);
1171 chunk
= vms_lib_bread_raw (abfd
, buf
, chunk
);
1179 vec
->rec_pos
+= chunk
;
1180 vec
->rec_rem
-= chunk
;
1182 if (vec
->rec_rem
== 0)
1184 /* End of record reached. */
1185 if (bfd_libdata (abfd
->my_archive
)->kind
== vms_lib_txt
)
1187 if ((vec
->rec_len
& 1) == 1
1188 && vec
->rec_len
!= 3
1189 && vec
->dcxsbms
== NULL
)
1191 /* Eat the pad byte. */
1193 if (vms_lib_bread_raw (abfd
, &pad
, 1) != 1)
1196 vec
->rec_pos
= REC_POS_NL
;
1201 if ((vec
->rec_len
& 1) == 1 && vec
->dcxsbms
!= NULL
)
1203 vec
->rec_pos
= REC_POS_PAD
;
1213 /* Standard function, but we currently only handle the rewind case. */
1216 vms_lib_bseek (struct bfd
*abfd
, file_ptr offset
, int whence
)
1218 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1220 if (whence
== SEEK_SET
&& offset
== 0)
1225 vec
->blk_off
= vec
->init_blk_off
;
1226 vec
->next_block
= vec
->init_next_block
;
1228 if (bfd_seek (abfd
->my_archive
, vec
->first_block
, SEEK_SET
) != 0)
1237 vms_lib_bwrite (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1238 const void *where ATTRIBUTE_UNUSED
,
1239 file_ptr nbytes ATTRIBUTE_UNUSED
)
1245 vms_lib_bclose (struct bfd
*abfd
)
1247 abfd
->iostream
= NULL
;
1252 vms_lib_bflush (struct bfd
*abfd ATTRIBUTE_UNUSED
)
1258 vms_lib_bstat (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1259 struct stat
*sb ATTRIBUTE_UNUSED
)
1261 /* Not supported. */
1266 vms_lib_bmmap (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1267 void *addr ATTRIBUTE_UNUSED
,
1268 bfd_size_type len ATTRIBUTE_UNUSED
,
1269 int prot ATTRIBUTE_UNUSED
,
1270 int flags ATTRIBUTE_UNUSED
,
1271 file_ptr offset ATTRIBUTE_UNUSED
,
1272 void **map_addr ATTRIBUTE_UNUSED
,
1273 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
1278 static const struct bfd_iovec vms_lib_iovec
= {
1279 &vms_lib_bread
, &vms_lib_bwrite
, &vms_lib_btell
, &vms_lib_bseek
,
1280 &vms_lib_bclose
, &vms_lib_bflush
, &vms_lib_bstat
, &vms_lib_bmmap
1283 /* Open a library module. FILEPOS is the position of the module header. */
1286 vms_lib_bopen (bfd
*el
, file_ptr filepos
)
1288 struct vms_lib_iovec
*vec
;
1289 unsigned char buf
[256];
1290 struct vms_mhd
*mhd
;
1291 struct lib_tdata
*tdata
= bfd_libdata (el
->my_archive
);
1294 /* Allocate and initialized the iovec. */
1295 vec
= bfd_zalloc (el
, sizeof (*vec
));
1300 el
->iovec
= &vms_lib_iovec
;
1302 /* File length is not known. */
1305 /* Read the first data block. */
1306 vec
->next_block
= filepos
& ~(VMS_BLOCK_SIZE
- 1);
1307 vec
->blk_off
= DATA__LENGTH
;
1308 if (!vms_lib_read_block (el
))
1311 /* Prepare to read the first record. */
1312 vec
->blk_off
= filepos
& (VMS_BLOCK_SIZE
- 1);
1314 if (bfd_seek (el
->my_archive
, filepos
, SEEK_SET
) != 0)
1317 /* Read Record length + MHD + align byte. */
1318 len
= tdata
->mhd_size
;
1319 if (vms_lib_bread_raw (el
, buf
, 2) != 2)
1321 if (bfd_getl16 (buf
) != len
)
1323 len
= (len
+ 1) & ~1;
1324 BFD_ASSERT (len
<= sizeof (buf
));
1325 if (vms_lib_bread_raw (el
, buf
, len
) != len
)
1328 /* Get info from mhd. */
1329 mhd
= (struct vms_mhd
*)buf
;
1331 if (mhd
->id
!= MHD__C_MHDID
)
1333 if (len
>= MHD__C_MHDLEN
+ 1)
1334 el
->selective_search
= (mhd
->objstat
& MHD__M_SELSRC
) ? 1 : 0;
1335 el
->mtime
= vms_rawtime_to_time_t (mhd
->datim
);
1336 el
->mtime_set
= TRUE
;
1338 /* Reinit the iovec so that seek() will point to the first record after
1341 vec
->init_blk_off
= vec
->blk_off
;
1342 vec
->init_next_block
= vec
->next_block
;
1343 vec
->first_block
= bfd_tell (el
->my_archive
);
1344 vec
->dcxsbms
= bfd_libdata (el
->my_archive
)->dcxsbm
;
1346 if (vec
->dcxsbms
!= NULL
)
1349 vec
->dcx_max
= 10 * 1024;
1350 vec
->dcx_buf
= bfd_alloc (el
, vec
->dcx_max
);
1352 if (vec
->dcx_buf
== NULL
)
1358 /* Get member MODIDX. Return NULL in case of error. */
1361 _bfd_vms_lib_get_module (bfd
*abfd
, unsigned int modidx
)
1363 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
1371 if (modidx
>= tdata
->nbr_modules
)
1374 /* Already loaded. */
1375 if (tdata
->cache
[modidx
])
1376 return tdata
->cache
[modidx
];
1379 file_off
= tdata
->modules
[modidx
].file_offset
;
1380 if (tdata
->type
!= LBR__C_TYP_IOBJ
)
1382 res
= _bfd_create_empty_archive_element_shell (abfd
);
1386 /* Special reader to deal with data blocks. */
1387 if (!vms_lib_bopen (res
, file_off
))
1393 struct vms_mhd
*mhd
;
1394 struct areltdata
*arelt
;
1396 /* Sanity check. The MHD must be big enough to contain module size. */
1397 if (tdata
->mhd_size
< offsetof (struct vms_mhd
, modsize
) + 4)
1400 /* Read the MHD now. */
1401 if (bfd_seek (abfd
, file_off
, SEEK_SET
) != 0)
1403 if (bfd_bread (buf
, tdata
->mhd_size
, abfd
) != tdata
->mhd_size
)
1406 mhd
= (struct vms_mhd
*) buf
;
1407 if (mhd
->id
!= MHD__C_MHDID
)
1410 res
= _bfd_create_empty_archive_element_shell (abfd
);
1413 arelt
= bfd_zmalloc (sizeof (*arelt
));
1419 res
->arelt_data
= arelt
;
1421 /* Get info from mhd. */
1422 if (tdata
->mhd_size
>= offsetof (struct vms_mhd
, objstat
) + 1)
1423 res
->selective_search
= (mhd
->objstat
& MHD__M_SELSRC
) ? 1 : 0;
1424 res
->mtime
= vms_rawtime_to_time_t (mhd
->datim
);
1425 res
->mtime_set
= TRUE
;
1427 arelt
->parsed_size
= bfd_getl32 (mhd
->modsize
);
1429 /* No need for a special reader as members are stored linearly.
1430 Just skip the MHD. */
1431 res
->origin
= file_off
+ tdata
->mhd_size
;
1435 name
= tdata
->modules
[modidx
].name
;
1436 namelen
= strlen (name
);
1437 newname
= bfd_malloc (namelen
+ 4 + 1);
1438 if (newname
== NULL
)
1443 strcpy (newname
, name
);
1444 switch (tdata
->type
)
1446 case LBR__C_TYP_IOBJ
:
1447 case LBR__C_TYP_EOBJ
:
1448 /* For object archives, append .obj to mimic standard behaviour. */
1449 strcpy (newname
+ namelen
, ".obj");
1454 bfd_set_filename (res
, newname
);
1456 tdata
->cache
[modidx
] = res
;
1461 /* Standard function: get member at IDX. */
1464 _bfd_vms_lib_get_elt_at_index (bfd
*abfd
, symindex symidx
)
1466 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
1468 unsigned int modidx
;
1471 if (symidx
> tdata
->artdata
.symdef_count
)
1473 file_off
= tdata
->artdata
.symdefs
[symidx
].file_offset
;
1476 for (modidx
= 0; modidx
< tdata
->nbr_modules
; modidx
++)
1478 if (tdata
->modules
[modidx
].file_offset
== file_off
)
1481 if (modidx
>= tdata
->nbr_modules
)
1484 return _bfd_vms_lib_get_module (abfd
, modidx
);
1487 /* Elements of an imagelib are stubs. You can get the real image with this
1491 _bfd_vms_lib_get_imagelib_file (bfd
*el
)
1493 bfd
*archive
= el
->my_archive
;
1494 const char *modname
= el
->filename
;
1495 int modlen
= strlen (modname
);
1500 /* Convert module name to lower case and append '.exe'. */
1501 filename
= bfd_alloc (el
, modlen
+ 5);
1502 if (filename
== NULL
)
1504 for (j
= 0; j
< modlen
; j
++)
1505 if (ISALPHA (modname
[j
]))
1506 filename
[j
] = TOLOWER (modname
[j
]);
1508 filename
[j
] = modname
[j
];
1509 memcpy (filename
+ modlen
, ".exe", 5);
1511 filename
= _bfd_append_relative_path (archive
, filename
);
1512 if (filename
== NULL
)
1514 res
= bfd_openr (filename
, NULL
);
1518 /* xgettext:c-format */
1519 _bfd_error_handler(_("could not open shared image '%s' from '%s'"),
1520 filename
, archive
->filename
);
1521 bfd_release (archive
, filename
);
1525 /* FIXME: put it in a cache ? */
1529 /* Standard function. */
1532 _bfd_vms_lib_openr_next_archived_file (bfd
*archive
,
1541 idx
= last_file
->proxy_origin
+ 1;
1543 if (idx
>= bfd_libdata (archive
)->nbr_modules
)
1545 bfd_set_error (bfd_error_no_more_archived_files
);
1549 res
= _bfd_vms_lib_get_module (archive
, idx
);
1552 res
->proxy_origin
= idx
;
1556 /* Standard function. Just compute the length. */
1559 _bfd_vms_lib_generic_stat_arch_elt (bfd
*abfd
, struct stat
*st
)
1561 struct lib_tdata
*tdata
;
1564 if (abfd
->my_archive
== NULL
)
1566 bfd_set_error (bfd_error_invalid_operation
);
1570 tdata
= bfd_libdata (abfd
->my_archive
);
1571 if (tdata
->type
!= LBR__C_TYP_IOBJ
)
1573 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1575 if (vec
->file_len
== (ufile_ptr
)-1)
1577 if (vms_lib_bseek (abfd
, 0, SEEK_SET
) != 0)
1580 /* Compute length. */
1581 while (vms_lib_bread (abfd
, NULL
, 1 << 20) > 0)
1584 st
->st_size
= vec
->file_len
;
1588 st
->st_size
= ((struct areltdata
*)abfd
->arelt_data
)->parsed_size
;
1591 if (abfd
->mtime_set
)
1592 st
->st_mtime
= abfd
->mtime
;
1602 /* Internal representation of an index entry. */
1606 /* Corresponding archive member. */
1609 /* Number of reference to this entry. */
1612 /* Length of the key. */
1613 unsigned short namlen
;
1619 /* Used to sort index entries. */
1622 lib_index_cmp (const void *lv
, const void *rv
)
1624 const struct lib_index
*l
= lv
;
1625 const struct lib_index
*r
= rv
;
1627 return strcmp (l
->name
, r
->name
);
1630 /* Maximum number of index blocks level. */
1632 #define MAX_LEVEL 10
1634 /* Get the size of an index entry. */
1637 get_idxlen (struct lib_index
*idx
, bfd_boolean is_elfidx
)
1641 /* 9 is the size of struct vms_elfidx without keyname. */
1642 if (idx
->namlen
> MAX_KEYLEN
)
1643 return 9 + sizeof (struct vms_kbn
);
1645 return 9 + idx
->namlen
;
1649 /* 7 is the size of struct vms_idx without keyname. */
1650 return 7 + idx
->namlen
;
1654 /* Write the index composed by NBR symbols contained in IDX.
1655 VBN is the first vbn to be used, and will contain on return the last vbn.
1656 Can be called with ABFD set to NULL just to size the index.
1657 If not null, TOPVBN will be assigned to the vbn of the root index tree.
1658 IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
1659 Return TRUE on success. */
1662 vms_write_index (bfd
*abfd
,
1663 struct lib_index
*idx
, unsigned int nbr
, unsigned int *vbn
,
1664 unsigned int *topvbn
, bfd_boolean is_elfidx
)
1666 /* The index is organized as a tree. This function implements a naive
1667 algorithm to balance the tree: it fills the leaves, and create a new
1668 branch when all upper leaves and branches are full. We only keep in
1669 memory a path to the current leaf. */
1673 /* Disk blocks for the current path. */
1674 struct vms_indexdef
*rblk
[MAX_LEVEL
];
1675 /* Info on the current blocks. */
1678 unsigned int vbn
; /* VBN of the block. */
1679 /* The last entry is identified so that it could be copied to the
1681 unsigned short len
; /* Length up to the last entry. */
1682 unsigned short lastlen
; /* Length of the last entry. */
1685 /* The kbn blocks are used to store long symbol names. */
1686 unsigned int kbn_sz
= 0; /* Number of bytes available in the kbn block. */
1687 unsigned int kbn_vbn
= 0; /* VBN of the kbn block. */
1688 unsigned char *kbn_blk
= NULL
; /* Contents of the kbn block. */
1692 /* No entries. Very easy to handle. */
1700 /* Sort the index the first time this function is called. */
1701 qsort (idx
, nbr
, sizeof (struct lib_index
), lib_index_cmp
);
1704 /* Allocate first index block. */
1707 rblk
[0] = bfd_zmalloc (sizeof (struct vms_indexdef
));
1708 blk
[0].vbn
= (*vbn
)++;
1712 for (i
= 0; i
< nbr
; i
++, idx
++)
1714 unsigned int idxlen
;
1716 unsigned int key_vbn
= 0;
1717 unsigned int key_off
= 0;
1719 idxlen
= get_idxlen (idx
, is_elfidx
);
1721 if (is_elfidx
&& idx
->namlen
> MAX_KEYLEN
)
1723 /* If the key (ie name) is too long, write it in the kbn block. */
1724 unsigned int kl
= idx
->namlen
;
1725 unsigned int kl_chunk
;
1726 const char *key
= idx
->name
;
1728 /* Write the key in the kbn, chunk after chunk. */
1731 if (kbn_sz
< sizeof (struct vms_kbn
))
1733 /* Not enough room in the kbn block. */
1736 /* Write it to the disk (if there is one). */
1739 if (!vms_write_block (abfd
, kbn_vbn
, kbn_blk
))
1744 kbn_blk
= bfd_malloc (VMS_BLOCK_SIZE
);
1745 if (kbn_blk
== NULL
)
1748 *(unsigned short *)kbn_blk
= 0;
1750 /* Allocate a new block for the keys. */
1752 kbn_sz
= VMS_BLOCK_SIZE
- 2;
1754 /* Size of the chunk written to the current key block. */
1755 if (kl
+ sizeof (struct vms_kbn
) > kbn_sz
)
1756 kl_chunk
= kbn_sz
- sizeof (struct vms_kbn
);
1760 if (kbn_blk
!= NULL
)
1762 struct vms_kbn
*kbn
;
1764 kbn
= (struct vms_kbn
*)(kbn_blk
+ VMS_BLOCK_SIZE
- kbn_sz
);
1768 /* Save the rfa of the first chunk. */
1770 key_off
= VMS_BLOCK_SIZE
- kbn_sz
;
1773 bfd_putl16 (kl_chunk
, kbn
->keylen
);
1776 /* No next chunk. */
1777 bfd_putl32 (0, kbn
->rfa
.vbn
);
1778 bfd_putl16 (0, kbn
->rfa
.offset
);
1782 /* Next chunk will be at the start of the next block. */
1783 bfd_putl32 (*vbn
, kbn
->rfa
.vbn
);
1784 bfd_putl16 (2, kbn
->rfa
.offset
);
1786 memcpy ((char *)(kbn
+ 1), key
, kl_chunk
);
1790 kl_chunk
= (kl_chunk
+ 1) & ~1; /* Always align. */
1791 kbn_sz
-= kl_chunk
+ sizeof (struct vms_kbn
);
1796 /* Check if a block might overflow. In this case we will flush this
1797 block and all the blocks below it. */
1798 for (j
= 0; j
< level
; j
++)
1799 if (blk
[j
].len
+ blk
[j
].lastlen
+ idxlen
> INDEXDEF__BLKSIZ
)
1802 for (j
= 0; j
< level
; j
++)
1806 /* There is not enough room to write the new entry in this
1807 block or in a parent block. */
1811 BFD_ASSERT (level
< MAX_LEVEL
);
1813 /* Need to create a parent. */
1816 rblk
[level
] = bfd_zmalloc (sizeof (struct vms_indexdef
));
1817 bfd_putl32 (*vbn
, rblk
[j
]->parent
);
1819 blk
[level
].vbn
= (*vbn
)++;
1821 blk
[level
].lastlen
= blk
[j
].lastlen
;
1826 /* Update parent block: write the last entry from the current
1830 struct vms_rfa
*rfa
;
1832 /* Pointer to the last entry in parent block. */
1833 rfa
= (struct vms_rfa
*)(rblk
[j
+ 1]->keys
+ blk
[j
+ 1].len
);
1835 /* Copy the whole entry. */
1836 BFD_ASSERT (blk
[j
+ 1].lastlen
== blk
[j
].lastlen
);
1837 memcpy (rfa
, rblk
[j
]->keys
+ blk
[j
].len
, blk
[j
].lastlen
);
1838 /* Fix the entry (which in always the first field of an
1840 bfd_putl32 (blk
[j
].vbn
, rfa
->vbn
);
1841 bfd_putl16 (RFADEF__C_INDEX
, rfa
->offset
);
1846 /* And allocate it. Do it only on the block that won't be
1847 flushed (so that the parent of the parent can be
1849 blk
[j
+ 1].len
+= blk
[j
+ 1].lastlen
;
1850 blk
[j
+ 1].lastlen
= 0;
1853 /* Write this block on the disk. */
1856 bfd_putl16 (blk
[j
].len
+ blk
[j
].lastlen
, rblk
[j
]->used
);
1857 if (!vms_write_block (abfd
, blk
[j
].vbn
, rblk
[j
]))
1861 /* Reset this block. */
1864 blk
[j
].vbn
= (*vbn
)++;
1867 /* Append it to the block. */
1870 /* Keep the previous last entry. */
1871 blk
[j
].len
+= blk
[j
].lastlen
;
1875 struct vms_rfa
*rfa
;
1877 rfa
= (struct vms_rfa
*)(rblk
[j
]->keys
+ blk
[j
].len
);
1878 bfd_putl32 ((idx
->abfd
->proxy_origin
/ VMS_BLOCK_SIZE
) + 1,
1881 ((idx
->abfd
->proxy_origin
% VMS_BLOCK_SIZE
)
1882 + (is_elfidx
? 0 : DATA__DATA
),
1887 /* Use elfidx format. */
1888 struct vms_elfidx
*en
= (struct vms_elfidx
*)rfa
;
1893 /* Long symbol name. */
1894 struct vms_kbn
*k
= (struct vms_kbn
*)(en
->keyname
);
1895 bfd_putl16 (sizeof (struct vms_kbn
), en
->keylen
);
1896 bfd_putl16 (idx
->namlen
, k
->keylen
);
1897 bfd_putl32 (key_vbn
, k
->rfa
.vbn
);
1898 bfd_putl16 (key_off
, k
->rfa
.offset
);
1899 en
->flags
|= ELFIDX__SYMESC
;
1903 bfd_putl16 (idx
->namlen
, en
->keylen
);
1904 memcpy (en
->keyname
, idx
->name
, idx
->namlen
);
1909 /* Use idx format. */
1910 struct vms_idx
*en
= (struct vms_idx
*)rfa
;
1911 en
->keylen
= idx
->namlen
;
1912 memcpy (en
->keyname
, idx
->name
, idx
->namlen
);
1916 /* The last added key can now be the last one all blocks in the
1918 blk
[j
].lastlen
= idxlen
;
1922 /* Save VBN of the root. */
1924 *topvbn
= blk
[level
- 1].vbn
;
1930 for (j
= 1; j
< level
; j
++)
1932 /* Update parent block: write the new entry. */
1935 struct vms_rfa
*rfa
;
1937 en
= rblk
[j
- 1]->keys
+ blk
[j
- 1].len
;
1938 par
= rblk
[j
]->keys
+ blk
[j
].len
;
1939 BFD_ASSERT (blk
[j
].lastlen
== blk
[j
- 1].lastlen
);
1940 memcpy (par
, en
, blk
[j
- 1].lastlen
);
1941 rfa
= (struct vms_rfa
*)par
;
1942 bfd_putl32 (blk
[j
- 1].vbn
, rfa
->vbn
);
1943 bfd_putl16 (RFADEF__C_INDEX
, rfa
->offset
);
1946 for (j
= 0; j
< level
; j
++)
1948 /* Write this block on the disk. */
1949 bfd_putl16 (blk
[j
].len
+ blk
[j
].lastlen
, rblk
[j
]->used
);
1950 if (!vms_write_block (abfd
, blk
[j
].vbn
, rblk
[j
]))
1956 /* Write the last kbn (if any). */
1959 if (!vms_write_block (abfd
, kbn_vbn
, kbn_blk
))
1967 /* Append data to the data block DATA. Force write if PAD is true. */
1970 vms_write_data_block (bfd
*arch
, struct vms_datadef
*data
, file_ptr
*off
,
1971 const unsigned char *buf
, unsigned int len
, int pad
)
1973 while (len
> 0 || pad
)
1975 unsigned int doff
= *off
& (VMS_BLOCK_SIZE
- 1);
1976 unsigned int remlen
= (DATA__LENGTH
- DATA__DATA
) - doff
;
1979 l
= (len
> remlen
) ? remlen
: len
;
1980 memcpy (data
->data
+ doff
, buf
, l
);
1986 if (doff
== (DATA__LENGTH
- DATA__DATA
) || (len
== 0 && pad
))
1990 bfd_putl32 ((*off
/ VMS_BLOCK_SIZE
) + 2, data
->link
);
1992 if (bfd_bwrite (data
, sizeof (*data
), arch
) != sizeof (*data
))
1995 *off
+= DATA__LENGTH
- doff
;
2004 /* Build the symbols index. */
2007 _bfd_vms_lib_build_map (unsigned int nbr_modules
,
2008 struct lib_index
*modules
,
2009 unsigned int *res_cnt
,
2010 struct lib_index
**res
)
2013 asymbol
**syms
= NULL
;
2015 struct lib_index
*map
= NULL
;
2016 unsigned int map_max
= 1024; /* Fine initial default. */
2017 unsigned int map_count
= 0;
2019 map
= (struct lib_index
*) bfd_malloc (map_max
* sizeof (struct lib_index
));
2023 /* Gather symbols. */
2024 for (i
= 0; i
< nbr_modules
; i
++)
2029 bfd
*current
= modules
[i
].abfd
;
2031 if ((bfd_get_file_flags (current
) & HAS_SYMS
) == 0)
2034 storage
= bfd_get_symtab_upper_bound (current
);
2040 if (storage
> syms_max
)
2045 syms
= (asymbol
**) bfd_malloc (syms_max
);
2049 symcount
= bfd_canonicalize_symtab (current
, syms
);
2053 /* Now map over all the symbols, picking out the ones we
2055 for (src_count
= 0; src_count
< symcount
; src_count
++)
2057 flagword flags
= (syms
[src_count
])->flags
;
2058 asection
*sec
= syms
[src_count
]->section
;
2060 if ((flags
& BSF_GLOBAL
2062 || flags
& BSF_INDIRECT
2063 || bfd_is_com_section (sec
))
2064 && ! bfd_is_und_section (sec
))
2066 struct lib_index
*new_map
;
2068 /* This symbol will go into the archive header. */
2069 if (map_count
== map_max
)
2072 new_map
= (struct lib_index
*)
2073 bfd_realloc (map
, map_max
* sizeof (struct lib_index
));
2074 if (new_map
== NULL
)
2079 map
[map_count
].abfd
= current
;
2080 map
[map_count
].namlen
= strlen (syms
[src_count
]->name
);
2081 map
[map_count
].name
= syms
[src_count
]->name
;
2089 *res_cnt
= map_count
;
2101 /* Do the hard work: write an archive on the disk. */
2104 _bfd_vms_lib_write_archive_contents (bfd
*arch
)
2107 unsigned int nbr_modules
;
2108 struct lib_index
*modules
;
2109 unsigned int nbr_symbols
;
2110 struct lib_index
*symbols
;
2111 struct lib_tdata
*tdata
= bfd_libdata (arch
);
2114 unsigned int nbr_mod_iblk
;
2115 unsigned int nbr_sym_iblk
;
2117 unsigned int mod_idx_vbn
;
2118 unsigned int sym_idx_vbn
;
2119 bfd_boolean is_elfidx
= tdata
->kind
== vms_lib_ia64
;
2120 unsigned int max_keylen
= is_elfidx
? MAX_EKEYLEN
: MAX_KEYLEN
;
2122 /* Count the number of modules (and do a first sanity check). */
2124 for (current
= arch
->archive_head
;
2126 current
= current
->archive_next
)
2128 /* This check is checking the bfds for the objects we're reading
2129 from (which are usually either an object file or archive on
2130 disk), not the archive entries we're writing to. We don't
2131 actually create bfds for the archive members, we just copy
2132 them byte-wise when we write out the archive. */
2133 if (bfd_write_p (current
) || !bfd_check_format (current
, bfd_object
))
2135 bfd_set_error (bfd_error_invalid_operation
);
2142 /* Build the modules list. */
2143 BFD_ASSERT (tdata
->modules
== NULL
);
2144 modules
= bfd_alloc (arch
, nbr_modules
* sizeof (struct lib_index
));
2145 if (modules
== NULL
)
2148 for (current
= arch
->archive_head
, i
= 0;
2150 current
= current
->archive_next
, i
++)
2154 modules
[i
].abfd
= current
;
2155 modules
[i
].name
= vms_get_module_name (current
->filename
, FALSE
);
2158 /* FIXME: silently truncate long names ? */
2159 nl
= strlen (modules
[i
].name
);
2160 modules
[i
].namlen
= (nl
> max_keylen
? max_keylen
: nl
);
2163 /* Create the module index. */
2165 if (!vms_write_index (NULL
, modules
, nbr_modules
, &vbn
, NULL
, is_elfidx
))
2169 /* Create symbol index. */
2170 if (!_bfd_vms_lib_build_map (nbr_modules
, modules
, &nbr_symbols
, &symbols
))
2174 if (!vms_write_index (NULL
, symbols
, nbr_symbols
, &vbn
, NULL
, is_elfidx
))
2178 /* Write modules and remember their position. */
2179 off
= (1 + nbr_mod_iblk
+ nbr_sym_iblk
) * VMS_BLOCK_SIZE
;
2181 if (bfd_seek (arch
, off
, SEEK_SET
) != 0)
2184 for (i
= 0; i
< nbr_modules
; i
++)
2186 struct vms_datadef data
;
2187 unsigned char blk
[VMS_BLOCK_SIZE
];
2188 struct vms_mhd
*mhd
;
2191 current
= modules
[i
].abfd
;
2192 current
->proxy_origin
= off
;
2198 /* Write the MHD as a record (ie, size first). */
2200 bfd_putl16 (tdata
->mhd_size
, blk
);
2202 mhd
= (struct vms_mhd
*)(blk
+ sz
);
2203 memset (mhd
, 0, sizeof (struct vms_mhd
));
2205 mhd
->id
= MHD__C_MHDID
;
2207 memcpy (mhd
->objid
, "V1.0", 4);
2208 bfd_putl32 (modules
[i
].ref
, mhd
->refcnt
);
2211 sz
+= tdata
->mhd_size
;
2214 /* Rewind the member to be put into the archive. */
2215 if (bfd_seek (current
, 0, SEEK_SET
) != 0)
2218 /* Copy the member into the archive. */
2221 unsigned int modsize
= 0;
2223 file_ptr off_hdr
= off
;
2225 /* Read to complete the first block. */
2226 amt
= bfd_bread (blk
+ sz
, VMS_BLOCK_SIZE
- sz
, current
);
2227 if (amt
== (bfd_size_type
)-1)
2230 if (amt
< VMS_BLOCK_SIZE
- sz
)
2232 /* The member size is less than a block. Pad the block. */
2233 memset (blk
+ sz
+ amt
, 0, VMS_BLOCK_SIZE
- sz
- amt
);
2235 bfd_putl32 (modsize
, mhd
->modsize
);
2237 /* Write the first block (which contains an mhd). */
2238 if (bfd_bwrite (blk
, VMS_BLOCK_SIZE
, arch
) != VMS_BLOCK_SIZE
)
2240 off
+= VMS_BLOCK_SIZE
;
2242 if (amt
== VMS_BLOCK_SIZE
- sz
)
2244 /* Copy the remaining. */
2245 char buffer
[DEFAULT_BUFFERSIZE
];
2249 amt
= bfd_bread (buffer
, sizeof (buffer
), current
);
2250 if (amt
== (bfd_size_type
)-1)
2255 if (amt
!= sizeof (buffer
))
2257 /* Clear the padding. */
2258 memset (buffer
+ amt
, 0, sizeof (buffer
) - amt
);
2259 amt
= (amt
+ VMS_BLOCK_SIZE
) & ~(VMS_BLOCK_SIZE
- 1);
2261 if (bfd_bwrite (buffer
, amt
, arch
) != amt
)
2266 /* Now that the size is known, write the first block (again). */
2267 bfd_putl32 (modsize
, mhd
->modsize
);
2268 if (bfd_seek (arch
, off_hdr
, SEEK_SET
) != 0
2269 || bfd_bwrite (blk
, VMS_BLOCK_SIZE
, arch
) != VMS_BLOCK_SIZE
)
2271 if (bfd_seek (arch
, off
, SEEK_SET
) != 0)
2277 /* Write the MHD. */
2278 if (vms_write_data_block (arch
, &data
, &off
, blk
, sz
, 0) < 0)
2281 /* Write the member. */
2284 sz
= bfd_bread (blk
, sizeof (blk
), current
);
2287 if (vms_write_data_block (arch
, &data
, &off
, blk
, sz
, 0) < 0)
2291 /* Write the end of module marker. */
2292 if (vms_write_data_block (arch
, &data
, &off
,
2293 eotdesc
, sizeof (eotdesc
), 1) < 0)
2298 /* Write the indexes. */
2300 if (!vms_write_index (arch
, modules
, nbr_modules
, &vbn
, &mod_idx_vbn
,
2303 if (!vms_write_index (arch
, symbols
, nbr_symbols
, &vbn
, &sym_idx_vbn
,
2307 /* Write libary header. */
2309 unsigned char blk
[VMS_BLOCK_SIZE
];
2310 struct vms_lhd
*lhd
= (struct vms_lhd
*)blk
;
2311 struct vms_idd
*idd
= (struct vms_idd
*)(blk
+ sizeof (*lhd
));
2312 unsigned int idd_flags
;
2313 unsigned int saneid
;
2315 memset (blk
, 0, sizeof (blk
));
2317 lhd
->type
= tdata
->type
;
2319 switch (tdata
->kind
)
2322 saneid
= LHD_SANEID3
;
2325 saneid
= LHD_SANEID6
;
2330 bfd_putl32 (saneid
, lhd
->sanity
);
2331 bfd_putl16 (tdata
->ver
, lhd
->majorid
);
2332 bfd_putl16 (0, lhd
->minorid
);
2333 snprintf ((char *)lhd
->lbrver
+ 1, sizeof (lhd
->lbrver
) - 1,
2335 (unsigned)(BFD_VERSION
/ 100000000UL),
2336 (unsigned)(BFD_VERSION
/ 1000000UL) % 100,
2337 (unsigned)(BFD_VERSION
/ 10000UL) % 100);
2338 lhd
->lbrver
[sizeof (lhd
->lbrver
) - 1] = 0;
2339 lhd
->lbrver
[0] = strlen ((char *)lhd
->lbrver
+ 1);
2341 bfd_putl32 (tdata
->credat_lo
, lhd
->credat
+ 0);
2342 bfd_putl32 (tdata
->credat_hi
, lhd
->credat
+ 4);
2343 vms_raw_get_time (lhd
->updtim
);
2345 lhd
->mhdusz
= tdata
->mhd_size
- MHD__C_USRDAT
;
2347 bfd_putl32 (nbr_modules
+ nbr_symbols
, lhd
->idxcnt
);
2348 bfd_putl32 (nbr_modules
, lhd
->modcnt
);
2349 bfd_putl32 (nbr_modules
, lhd
->modhdrs
);
2351 /* Number of blocks for index. */
2352 bfd_putl32 (nbr_mod_iblk
+ nbr_sym_iblk
, lhd
->idxblks
);
2353 bfd_putl32 (vbn
- 1, lhd
->hipreal
);
2354 bfd_putl32 (vbn
- 1, lhd
->hiprusd
);
2356 /* VBN of the next free block. */
2357 bfd_putl32 ((off
/ VMS_BLOCK_SIZE
) + 1, lhd
->nextvbn
);
2358 bfd_putl32 ((off
/ VMS_BLOCK_SIZE
) + 1, lhd
->nextrfa
+ 0);
2359 bfd_putl16 (0, lhd
->nextrfa
+ 4);
2361 /* First index (modules name). */
2362 idd_flags
= IDD__FLAGS_ASCII
| IDD__FLAGS_VARLENIDX
2363 | IDD__FLAGS_NOCASECMP
| IDD__FLAGS_NOCASENTR
;
2364 bfd_putl16 (idd_flags
, idd
->flags
);
2365 bfd_putl16 (max_keylen
+ 1, idd
->keylen
);
2366 bfd_putl16 (mod_idx_vbn
, idd
->vbn
);
2369 /* Second index (symbols name). */
2370 bfd_putl16 (idd_flags
, idd
->flags
);
2371 bfd_putl16 (max_keylen
+ 1, idd
->keylen
);
2372 bfd_putl16 (sym_idx_vbn
, idd
->vbn
);
2375 if (!vms_write_block (arch
, 1, blk
))
2382 bfd_set_input_error (current
, bfd_get_error ());
2386 /* Add a target for text library. This costs almost nothing and is useful to
2387 read VMS library on the host. */
2389 const bfd_target alpha_vms_lib_txt_vec
=
2391 "vms-libtxt", /* Name. */
2392 bfd_target_unknown_flavour
,
2393 BFD_ENDIAN_UNKNOWN
, /* byteorder */
2394 BFD_ENDIAN_UNKNOWN
, /* header_byteorder */
2395 0, /* Object flags. */
2396 0, /* Sect flags. */
2397 0, /* symbol_leading_char. */
2398 ' ', /* ar_pad_char. */
2399 15, /* ar_max_namelen. */
2400 0, /* match priority. */
2401 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2402 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2403 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
,
2404 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2405 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2406 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
,
2407 { /* bfd_check_format. */
2410 _bfd_vms_lib_txt_archive_p
,
2413 { /* bfd_set_format. */
2414 _bfd_bool_bfd_false_error
,
2415 _bfd_bool_bfd_false_error
,
2416 _bfd_bool_bfd_false_error
,
2417 _bfd_bool_bfd_false_error
2419 { /* bfd_write_contents. */
2420 _bfd_bool_bfd_false_error
,
2421 _bfd_bool_bfd_false_error
,
2422 _bfd_bool_bfd_false_error
,
2423 _bfd_bool_bfd_false_error
2425 BFD_JUMP_TABLE_GENERIC (_bfd_generic
),
2426 BFD_JUMP_TABLE_COPY (_bfd_generic
),
2427 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2428 BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib
),
2429 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols
),
2430 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
2431 BFD_JUMP_TABLE_WRITE (_bfd_nowrite
),
2432 BFD_JUMP_TABLE_LINK (_bfd_nolink
),
2433 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),