1 /* BFD back-end for VMS archive files.
3 Copyright 2010 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
;
80 /* Vector of modules (archive elements), already sorted. */
81 unsigned int nbr_modules
;
82 struct carsym
*modules
;
85 /* DCX (decompression) data. */
86 unsigned int nbr_dcxsbm
;
87 struct dcxsbm_desc
*dcxsbm
;
90 #define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
92 /* End-Of-Text pattern. This is a special record to mark the end of file. */
94 static const unsigned char eotdesc
[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
96 /* Describe the current state of carsym entries while building the archive
97 table of content. Things are simple with Alpha archives as the number
98 of entries is known, but with IA64 archives a entry can make a reference
99 to severals members. Therefore we must be able to extend the table on the
100 fly, but it should be allocated on the bfd - which doesn't support realloc.
101 To reduce the overhead, the table is initially allocated in the BFD's
102 objalloc and extended if necessary on the heap. In the later case, it
103 is finally copied to the BFD's objalloc so that it will automatically be
108 /* The table of content. */
111 /* Number of entries used in the table. */
114 /* Maximum number of entries. */
117 /* If true, the table was reallocated on the heap. If false, it is still
118 in the BFD's objalloc. */
119 bfd_boolean realloced
;
122 /* Simply add a name to the index. */
125 vms_add_index (struct carsym_mem
*cs
, char *name
,
126 unsigned int idx_vbn
, unsigned int idx_off
)
128 if (cs
->nbr
== cs
->max
)
132 cs
->max
= 2 * cs
->max
+ 32;
136 n
= bfd_malloc2 (cs
->max
, sizeof (struct carsym
));
139 memcpy (n
, cs
->idx
, cs
->nbr
* sizeof (struct carsym
));
140 /* And unfortunately we can't free cs->idx. */
144 n
= bfd_realloc_or_free (cs
->idx
, cs
->nbr
* sizeof (struct carsym
));
149 cs
->realloced
= TRUE
;
151 cs
->idx
[cs
->nbr
].file_offset
= (idx_vbn
- 1) * VMS_BLOCK_SIZE
+ idx_off
;
152 cs
->idx
[cs
->nbr
].name
= name
;
157 /* Follow all member of a lns list (pointed by RFA) and add indexes for
158 NAME. Return FALSE in case of error. */
161 vms_add_indexes_from_list (bfd
*abfd
, struct carsym_mem
*cs
, char *name
,
170 vbn
= bfd_getl32 (rfa
->vbn
);
175 off
= (vbn
- 1) * VMS_BLOCK_SIZE
+ bfd_getl16 (rfa
->offset
);
176 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
177 || bfd_bread (&lns
, sizeof (lns
), abfd
) != sizeof (lns
))
180 if (!vms_add_index (cs
, name
,
181 bfd_getl32 (lns
.modrfa
.vbn
),
182 bfd_getl16 (lns
.modrfa
.offset
)))
189 /* Read block VBN from ABFD and store it into BLK. */
192 vms_read_block (bfd
*abfd
, unsigned int vbn
, void *blk
)
196 /* Read the index block. */
197 off
= (vbn
- 1) * VMS_BLOCK_SIZE
;
198 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
199 || bfd_bread (blk
, VMS_BLOCK_SIZE
, abfd
) != VMS_BLOCK_SIZE
)
205 /* Read index block VBN and put the entry in **IDX (which is updated).
206 If the entry is indirect, recurse. */
209 vms_traverse_index (bfd
*abfd
, unsigned int vbn
, struct carsym_mem
*cs
)
211 struct vms_indexdef indexdef
;
216 /* Read the index block. */
217 BFD_ASSERT (sizeof (indexdef
) == VMS_BLOCK_SIZE
);
218 if (!vms_read_block (abfd
, vbn
, &indexdef
))
222 p
= &indexdef
.keys
[0];
223 endp
= p
+ bfd_getl16 (indexdef
.used
);
226 unsigned int idx_vbn
;
227 unsigned int idx_off
;
229 unsigned char *keyname
;
232 /* Extract key length. */
233 if (bfd_libdata (abfd
)->ver
== LBR_MAJORID
)
235 struct vms_idx
*ridx
= (struct vms_idx
*)p
;
237 idx_vbn
= bfd_getl32 (ridx
->rfa
.vbn
);
238 idx_off
= bfd_getl16 (ridx
->rfa
.offset
);
240 keylen
= ridx
->keylen
;
242 keyname
= ridx
->keyname
;
244 else if (bfd_libdata (abfd
)->ver
== LBR_ELFMAJORID
)
246 struct vms_elfidx
*ridx
= (struct vms_elfidx
*)p
;
248 idx_vbn
= bfd_getl32 (ridx
->rfa
.vbn
);
249 idx_off
= bfd_getl16 (ridx
->rfa
.offset
);
251 keylen
= bfd_getl16 (ridx
->keylen
);
253 keyname
= ridx
->keyname
;
262 if (idx_off
== RFADEF__C_INDEX
)
264 /* Indirect entry. Recurse. */
265 if (!vms_traverse_index (abfd
, idx_vbn
, cs
))
270 /* Add a new entry. */
273 if (flags
& ELFIDX__SYMESC
)
275 /* Extended key name. */
276 unsigned int noff
= 0;
280 unsigned char kblk
[VMS_BLOCK_SIZE
];
283 if (keylen
!= sizeof (struct vms_kbn
))
286 kbn
= (struct vms_kbn
*)keyname
;
287 keylen
= bfd_getl16 (kbn
->keylen
);
289 name
= bfd_alloc (abfd
, keylen
+ 1);
292 kvbn
= bfd_getl32 (kbn
->rfa
.vbn
);
293 koff
= bfd_getl16 (kbn
->rfa
.offset
);
295 /* Read the key, chunk by chunk. */
300 if (!vms_read_block (abfd
, kvbn
, kblk
))
302 kbn
= (struct vms_kbn
*)(kblk
+ koff
);
303 klen
= bfd_getl16 (kbn
->keylen
);
304 kvbn
= bfd_getl32 (kbn
->rfa
.vbn
);
305 koff
= bfd_getl16 (kbn
->rfa
.offset
);
307 memcpy (name
+ noff
, kbn
+ 1, klen
);
318 /* Usual key name. */
319 name
= bfd_alloc (abfd
, keylen
+ 1);
323 memcpy (name
, keyname
, keylen
);
327 if (flags
& ELFIDX__LISTRFA
)
332 off
= (idx_vbn
- 1) * VMS_BLOCK_SIZE
+ idx_off
;
333 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
334 || bfd_bread (&lhs
, sizeof (lhs
), abfd
) != sizeof (lhs
))
337 /* FIXME: this adds extra entries that were not accounted. */
338 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.ng_g_rfa
))
340 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.ng_wk_rfa
))
342 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.g_g_rfa
))
344 if (!vms_add_indexes_from_list (abfd
, cs
, name
, &lhs
.g_wk_rfa
))
349 if (!vms_add_index (cs
, name
, idx_vbn
, idx_off
))
354 /* Point to the next index entry. */
355 p
= keyname
+ keylen
;
361 /* Read index #IDX, which must have NBREL entries. */
363 static struct carsym
*
364 vms_lib_read_index (bfd
*abfd
, int idx
, unsigned int *nbrel
)
369 struct carsym
*csbuf
;
370 struct carsym_mem csm
;
372 /* Read index desription. */
373 if (bfd_seek (abfd
, LHD_IDXDESC
+ idx
* IDD_LENGTH
, SEEK_SET
) != 0
374 || bfd_bread (&idd
, sizeof (idd
), abfd
) != sizeof (idd
))
378 flags
= bfd_getl16 (idd
.flags
);
379 if (!(flags
& IDD__FLAGS_ASCII
)
380 || !(flags
& IDD__FLAGS_VARLENIDX
))
383 csbuf
= bfd_alloc (abfd
, *nbrel
* sizeof (struct carsym
));
389 csm
.realloced
= FALSE
;
392 /* Note: if the index is empty, there is no block to traverse. */
393 vbn
= bfd_getl32 (idd
.vbn
);
394 if (vbn
!= 0 && !vms_traverse_index (abfd
, vbn
, &csm
))
396 if (csm
.realloced
&& csm
.idx
!= NULL
)
399 /* Note: in case of error, we can free what was allocated on the
401 bfd_release (abfd
, csbuf
);
407 /* There are more entries than the first estimate. Allocate on
408 the BFD's objalloc. */
409 csbuf
= bfd_alloc (abfd
, csm
.nbr
* sizeof (struct carsym
));
412 memcpy (csbuf
, csm
.idx
, csm
.nbr
* sizeof (struct carsym
));
419 /* Standard function. */
421 static const bfd_target
*
422 _bfd_vms_lib_archive_p (bfd
*abfd
, enum vms_lib_kind kind
)
426 struct lib_tdata
*tdata_hold
;
427 struct lib_tdata
*tdata
;
429 unsigned int nbr_ent
;
432 if (bfd_bread (&lhd
, sizeof (lhd
), abfd
) != sizeof (lhd
))
434 if (bfd_get_error () != bfd_error_system_call
)
435 bfd_set_error (bfd_error_wrong_format
);
439 /* Check sanity (= magic) number. */
440 sanity
= bfd_getl32 (lhd
.sanity
);
441 if (!(sanity
== LHD_SANEID3
442 || sanity
== LHD_SANEID6
443 || sanity
== LHD_SANEID_DCX
))
445 bfd_set_error (bfd_error_wrong_format
);
449 /* Check archive kind. */
453 if ((lhd
.type
!= LBR__C_TYP_EOBJ
&& lhd
.type
!= LBR__C_TYP_ESHSTB
)
454 || bfd_getl32 (lhd
.majorid
) != 3
457 bfd_set_error (bfd_error_wrong_format
);
462 if ((lhd
.type
!= LBR__C_TYP_IOBJ
&& lhd
.type
!= LBR__C_TYP_ISHSTB
)
463 || bfd_getl32 (lhd
.majorid
) != 6
466 bfd_set_error (bfd_error_wrong_format
);
471 if ((lhd
.type
!= LBR__C_TYP_TXT
472 && lhd
.type
!= LBR__C_TYP_MLB
473 && lhd
.type
!= LBR__C_TYP_HLP
)
474 || bfd_getl32 (lhd
.majorid
) != 3
477 bfd_set_error (bfd_error_wrong_format
);
485 /* Allocate and initialize private data. */
486 tdata_hold
= bfd_libdata (abfd
);
487 tdata
= (struct lib_tdata
*) bfd_zalloc (abfd
, sizeof (struct lib_tdata
));
490 abfd
->tdata
.any
= (void *)tdata
;
491 tdata
->ver
= bfd_getl32 (lhd
.majorid
);
492 tdata
->mhd_size
= MHD__C_USRDAT
+ lhd
.mhdusz
;
493 tdata
->type
= lhd
.type
;
497 tdata
->nbr_modules
= bfd_getl32 (lhd
.modcnt
);
498 tdata
->artdata
.symdef_count
= bfd_getl32 (lhd
.idxcnt
) - tdata
->nbr_modules
;
499 nbr_ent
= tdata
->nbr_modules
;
500 tdata
->modules
= vms_lib_read_index (abfd
, 0, &nbr_ent
);
501 if (tdata
->modules
== NULL
|| nbr_ent
!= tdata
->nbr_modules
)
505 nbr_ent
= tdata
->artdata
.symdef_count
;
506 tdata
->artdata
.symdefs
= vms_lib_read_index (abfd
, 1, &nbr_ent
);
507 if (tdata
->artdata
.symdefs
== NULL
)
509 /* Only IA64 archives may have more entries in the index that what
511 if (nbr_ent
!= tdata
->artdata
.symdef_count
512 && kind
!= vms_lib_ia64
)
514 tdata
->artdata
.symdef_count
= nbr_ent
;
516 tdata
->cache
= bfd_zalloc (abfd
, sizeof (bfd
*) * tdata
->nbr_modules
);
517 if (tdata
->cache
== NULL
)
520 /* Read DCX submaps. */
521 dcxvbn
= bfd_getl32 (lhd
.dcxmapvbn
);
524 unsigned char buf_reclen
[4];
527 struct vms_dcxmap
*map
;
528 unsigned int sbm_off
;
531 if (bfd_seek (abfd
, (dcxvbn
- 1) * VMS_BLOCK_SIZE
, SEEK_SET
) != 0
532 || bfd_bread (buf_reclen
, sizeof (buf_reclen
), abfd
)
533 != sizeof (buf_reclen
))
535 reclen
= bfd_getl32 (buf_reclen
);
536 buf
= bfd_malloc (reclen
);
539 if (bfd_bread (buf
, reclen
, abfd
) != reclen
)
544 map
= (struct vms_dcxmap
*)buf
;
545 tdata
->nbr_dcxsbm
= bfd_getl16 (map
->nsubs
);
546 sbm_off
= bfd_getl16 (map
->sub0
);
547 tdata
->dcxsbm
= (struct dcxsbm_desc
*)bfd_alloc
548 (abfd
, tdata
->nbr_dcxsbm
* sizeof (struct dcxsbm_desc
));
549 for (i
= 0; i
< tdata
->nbr_dcxsbm
; i
++)
551 struct vms_dcxsbm
*sbm
= (struct vms_dcxsbm
*) (buf
+ sbm_off
);
552 struct dcxsbm_desc
*sbmdesc
= &tdata
->dcxsbm
[i
];
553 unsigned int sbm_len
;
556 unsigned char *data
= (unsigned char *)sbm
;
560 sbm_sz
= bfd_getl16 (sbm
->size
);
562 BFD_ASSERT (sbm_off
<= reclen
);
564 sbmdesc
->min_char
= sbm
->min_char
;
565 BFD_ASSERT (sbmdesc
->min_char
== 0);
566 sbmdesc
->max_char
= sbm
->max_char
;
567 sbm_len
= sbmdesc
->max_char
- sbmdesc
->min_char
+ 1;
568 l
= (2 * sbm_len
+ 7) / 8;
570 (sbm_sz
>= sizeof (struct vms_dcxsbm
) + l
+ 3 * sbm_len
571 || (tdata
->nbr_dcxsbm
== 1
572 && sbm_sz
>= sizeof (struct vms_dcxsbm
) + l
+ sbm_len
));
573 sbmdesc
->flags
= (unsigned char *)bfd_alloc (abfd
, l
);
574 memcpy (sbmdesc
->flags
, data
+ bfd_getl16 (sbm
->flags
), l
);
575 sbmdesc
->nodes
= (unsigned char *)bfd_alloc (abfd
, 2 * sbm_len
);
576 memcpy (sbmdesc
->nodes
, data
+ bfd_getl16 (sbm
->nodes
), 2 * sbm_len
);
577 off
= bfd_getl16 (sbm
->next
);
580 /* Read the 'next' array. */
581 sbmdesc
->next
= (unsigned short *)bfd_alloc
582 (abfd
, sbm_len
* sizeof (unsigned short));
584 for (j
= 0; j
< sbm_len
; j
++)
585 sbmdesc
->next
[j
] = bfd_getl16 (buf1
+ j
* 2);
589 /* There is no next array if there is only one submap. */
590 BFD_ASSERT (tdata
->nbr_dcxsbm
== 1);
591 sbmdesc
->next
= NULL
;
598 tdata
->nbr_dcxsbm
= 0;
601 /* The map is always present. Also mark shared image library. */
602 abfd
->has_armap
= TRUE
;
603 if (tdata
->type
== LBR__C_TYP_ESHSTB
|| tdata
->type
== LBR__C_TYP_ISHSTB
)
604 abfd
->is_thin_archive
= TRUE
;
609 bfd_release (abfd
, tdata
);
610 abfd
->tdata
.any
= (void *)tdata_hold
;;
614 /* Standard function for alpha libraries. */
617 _bfd_vms_lib_alpha_archive_p (bfd
*abfd
)
619 return _bfd_vms_lib_archive_p (abfd
, vms_lib_alpha
);
622 /* Standard function for ia64 libraries. */
625 _bfd_vms_lib_ia64_archive_p (bfd
*abfd
)
627 return _bfd_vms_lib_archive_p (abfd
, vms_lib_ia64
);
630 /* Standard function for text libraries. */
632 static const bfd_target
*
633 _bfd_vms_lib_txt_archive_p (bfd
*abfd
)
635 return _bfd_vms_lib_archive_p (abfd
, vms_lib_txt
);
638 /* Standard bfd function. */
641 _bfd_vms_lib_mkarchive (bfd
*abfd
)
643 struct lib_tdata
*tdata
;
645 tdata
= (struct lib_tdata
*) bfd_zalloc (abfd
, sizeof (struct lib_tdata
));
649 abfd
->tdata
.any
= (void *)tdata
;
651 tdata
->mhd_size
= sizeof (struct vms_mhd
);
652 tdata
->type
= LBR__C_TYP_EOBJ
;
654 tdata
->nbr_modules
= 0;
655 tdata
->artdata
.symdef_count
= 0;
656 tdata
->modules
= NULL
;
657 tdata
->artdata
.symdefs
= NULL
;
663 /* Find NAME in the symbol index. Return the index. */
666 _bfd_vms_lib_find_symbol (bfd
*abfd
, const char *name
)
668 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
669 carsym
*syms
= tdata
->artdata
.symdefs
;
672 /* Open-coded binary search for speed. */
674 hi
= tdata
->artdata
.symdef_count
- 1;
678 int mid
= lo
+ (hi
- lo
) / 2;
681 diff
= (char)(name
[0] - syms
[mid
].name
[0]);
683 diff
= strcmp (name
, syms
[mid
].name
);
691 return BFD_NO_MORE_SYMBOLS
;
694 /* IO vector for archive member. Need that because members are not linearly
695 stored in archives. */
699 /* Current offset. */
702 /* Length of the module, when known. */
705 /* Current position in the record from bfd_bread point of view (ie, after
706 decompression). 0 means that no data byte have been read, -2 and -1
707 are reserved for the length word. */
709 #define REC_POS_NL -4
710 #define REC_POS_PAD -3
711 #define REC_POS_LEN0 -2
712 #define REC_POS_LEN1 -1
715 unsigned short rec_len
;
716 /* Number of bytes to read in the current record. */
717 unsigned short rec_rem
;
718 /* Offset of the next block. */
720 /* Current *data* offset in the data block. */
721 unsigned short blk_off
;
723 /* Offset of the first block. Extracted from the index. */
724 file_ptr first_block
;
726 /* Initial next_block. Extracted when the MHD is read. */
727 file_ptr init_next_block
;
728 /* Initial blk_off, once the MHD is read. */
729 unsigned short init_blk_off
;
731 /* Used to store any 3 byte record, which could be the EOF pattern. */
732 unsigned char pattern
[4];
735 struct dcxsbm_desc
*dcxsbms
;
736 /* Current submap. */
737 struct dcxsbm_desc
*dcx_sbm
;
738 /* Current offset in the submap. */
739 unsigned int dcx_offset
;
742 /* Compressed buffer. */
743 unsigned char *dcx_buf
;
744 /* Size of the buffer. Used to resize. */
745 unsigned int dcx_max
;
746 /* Number of valid bytes in the buffer. */
747 unsigned int dcx_rlen
;
750 /* Return the current position. */
753 vms_lib_btell (struct bfd
*abfd
)
755 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
759 /* Read the header of the next data block if all bytes of the current block
763 vms_lib_read_block (struct bfd
*abfd
)
765 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
767 if (vec
->blk_off
== DATA__LENGTH
)
769 unsigned char hdr
[DATA__DATA
];
771 /* Read next block. */
772 if (bfd_seek (abfd
->my_archive
, vec
->next_block
, SEEK_SET
) != 0)
774 if (bfd_bread (hdr
, sizeof (hdr
), abfd
->my_archive
) != sizeof (hdr
))
776 vec
->next_block
= (bfd_getl32 (hdr
+ 2) - 1) * VMS_BLOCK_SIZE
;
777 vec
->blk_off
= sizeof (hdr
);
782 /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
783 not stored. Read linearly from the library, but handle blocks. This
784 function does not handle records nor EOF. */
787 vms_lib_bread_raw (struct bfd
*abfd
, void *buf
, file_ptr nbytes
)
789 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
797 /* Be sure the current data block is read. */
798 if (!vms_lib_read_block (abfd
))
801 /* Do not read past the data block, do not read more than requested. */
802 l
= DATA__LENGTH
- vec
->blk_off
;
809 /* Really read into BUF. */
810 if (bfd_bread (buf
, l
, abfd
->my_archive
) != l
)
815 /* Make as if we are reading. */
816 if (bfd_seek (abfd
->my_archive
, l
, SEEK_CUR
) != 0)
829 /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
832 vms_lib_dcx (struct vms_lib_iovec
*vec
, unsigned char *buf
, file_ptr nbytes
)
834 struct dcxsbm_desc
*sbm
;
840 /* The loop below expect to deliver at least one byte. */
844 /* Get the current state. */
846 offset
= vec
->dcx_offset
;
847 j
= vec
->dcx_pos
& 7;
849 for (i
= vec
->dcx_pos
>> 3; i
< vec
->dcx_rlen
; i
++)
851 unsigned char b
= vec
->dcx_buf
[i
];
857 if (!(sbm
->flags
[offset
>> 3] & (1 << (offset
& 7))))
859 unsigned int n_offset
= sbm
->nodes
[offset
];
862 /* End of buffer. Stay where we are. */
863 vec
->dcx_pos
= (i
<< 3) + j
;
866 vec
->dcx_offset
= offset
;
870 offset
= 2 * n_offset
;
874 unsigned char v
= sbm
->nodes
[offset
];
876 if (sbm
->next
!= NULL
)
877 sbm
= vec
->dcxsbms
+ sbm
->next
[v
];
888 vec
->dcx_pos
= (i
<< 3) + j
+ 1;
889 vec
->dcx_offset
= offset
;
902 /* Standard IOVEC function. */
905 vms_lib_bread (struct bfd
*abfd
, void *buf
, file_ptr nbytes
)
907 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
911 /* Do not read past the end. */
912 if (vec
->where
>= vec
->file_len
)
918 if (vec
->rec_rem
== 0)
920 unsigned char blen
[2];
922 /* Read record length. */
923 if (vms_lib_bread_raw (abfd
, &blen
, sizeof (blen
)) != sizeof (blen
))
925 vec
->rec_len
= bfd_getl16 (blen
);
926 if (bfd_libdata (abfd
->my_archive
)->kind
== vms_lib_txt
)
928 /* Discard record size and align byte. */
930 vec
->rec_rem
= vec
->rec_len
;
934 /* Prepend record size. */
935 vec
->rec_pos
= REC_POS_LEN0
;
936 vec
->rec_rem
= (vec
->rec_len
+ 1) & ~1; /* With align byte. */
938 if (vec
->rec_len
== 3)
940 /* Possibly end of file. Check the pattern. */
941 if (vms_lib_bread_raw (abfd
, vec
->pattern
, 4) != 4)
943 if (!memcmp (vec
->pattern
, eotdesc
+ 2, 3))
945 /* This is really an EOF. */
947 vec
->file_len
= vec
->where
;
952 if (vec
->dcxsbms
!= NULL
)
954 /* This is a compressed member. */
958 /* Be sure there is enough room for the expansion. */
959 len
= (vec
->rec_len
+ 1) & ~1;
960 if (len
> vec
->dcx_max
)
962 while (len
> vec
->dcx_max
)
964 vec
->dcx_buf
= bfd_alloc (abfd
, vec
->dcx_max
);
965 if (vec
->dcx_buf
== NULL
)
969 /* Read the compressed record. */
971 if (vec
->rec_len
== 3)
974 memcpy (vec
->dcx_buf
, vec
->pattern
, 3);
978 elen
= vms_lib_bread_raw (abfd
, vec
->dcx_buf
, len
);
983 /* Dummy expansion to get the expanded length. */
985 vec
->dcx_sbm
= vec
->dcxsbms
;
987 elen
= vms_lib_dcx (vec
, NULL
, 0x10000);
993 /* Reset the state. */
995 vec
->dcx_sbm
= vec
->dcxsbms
;
999 if (vec
->rec_pos
< 0)
1002 switch (vec
->rec_pos
)
1005 c
= vec
->rec_len
& 0xff;
1006 vec
->rec_pos
= REC_POS_LEN1
;
1009 c
= (vec
->rec_len
>> 8) & 0xff;
1025 *(unsigned char *)buf
= c
;
1033 if (nbytes
> vec
->rec_rem
)
1034 chunk
= vec
->rec_rem
;
1038 if (vec
->dcxsbms
!= NULL
)
1040 /* Optimize the stat() case: no need to decompress again as we
1042 if (!(buf
== NULL
&& chunk
== vec
->rec_rem
))
1043 chunk
= vms_lib_dcx (vec
, buf
, chunk
);
1047 if (vec
->rec_len
== 3)
1050 memcpy (buf
, vec
->pattern
+ vec
->rec_pos
, chunk
);
1053 chunk
= vms_lib_bread_raw (abfd
, buf
, chunk
);
1061 vec
->rec_pos
+= chunk
;
1062 vec
->rec_rem
-= chunk
;
1064 if (vec
->rec_rem
== 0)
1066 /* End of record reached. */
1067 if (bfd_libdata (abfd
->my_archive
)->kind
== vms_lib_txt
)
1069 if ((vec
->rec_len
& 1) == 1
1070 && vec
->rec_len
!= 3
1071 && vec
->dcxsbms
== NULL
)
1073 /* Eat the pad byte. */
1075 if (vms_lib_bread_raw (abfd
, &pad
, 1) != 1)
1078 vec
->rec_pos
= REC_POS_NL
;
1083 if ((vec
->rec_len
& 1) == 1 && vec
->dcxsbms
!= NULL
)
1085 vec
->rec_pos
= REC_POS_PAD
;
1095 /* Standard function, but we currently only handle the rewind case. */
1098 vms_lib_bseek (struct bfd
*abfd
, file_ptr offset
, int whence
)
1100 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1102 if (whence
== SEEK_SET
&& offset
== 0)
1107 vec
->blk_off
= vec
->init_blk_off
;
1108 vec
->next_block
= vec
->init_next_block
;
1110 if (bfd_seek (abfd
->my_archive
, vec
->first_block
, SEEK_SET
) != 0)
1119 vms_lib_bwrite (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1120 const void *where ATTRIBUTE_UNUSED
,
1121 file_ptr nbytes ATTRIBUTE_UNUSED
)
1127 vms_lib_bclose (struct bfd
*abfd
)
1129 abfd
->iostream
= NULL
;
1134 vms_lib_bflush (struct bfd
*abfd ATTRIBUTE_UNUSED
)
1140 vms_lib_bstat (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1141 struct stat
*sb ATTRIBUTE_UNUSED
)
1143 /* Not supported. */
1148 vms_lib_bmmap (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1149 void *addr ATTRIBUTE_UNUSED
,
1150 bfd_size_type len ATTRIBUTE_UNUSED
,
1151 int prot ATTRIBUTE_UNUSED
,
1152 int flags ATTRIBUTE_UNUSED
,
1153 file_ptr offset ATTRIBUTE_UNUSED
)
1158 static const struct bfd_iovec vms_lib_iovec
= {
1159 &vms_lib_bread
, &vms_lib_bwrite
, &vms_lib_btell
, &vms_lib_bseek
,
1160 &vms_lib_bclose
, &vms_lib_bflush
, &vms_lib_bstat
, &vms_lib_bmmap
1163 /* Open a library module. FILEPOS is the position of the module header. */
1166 vms_lib_bopen (bfd
*el
, file_ptr filepos
)
1168 struct vms_lib_iovec
*vec
;
1170 struct vms_mhd
*mhd
;
1171 struct lib_tdata
*tdata
= bfd_libdata (el
->my_archive
);
1174 /* Allocate and initialized the iovec. */
1175 vec
= bfd_zalloc (el
, sizeof (*vec
));
1180 el
->iovec
= &vms_lib_iovec
;
1182 /* File length is not known. */
1185 /* Read the first data block. */
1186 vec
->next_block
= filepos
& ~(VMS_BLOCK_SIZE
- 1);
1187 vec
->blk_off
= DATA__LENGTH
;
1188 if (!vms_lib_read_block (el
))
1191 /* Prepare to read the first record. */
1192 vec
->blk_off
= filepos
& (VMS_BLOCK_SIZE
- 1);
1194 if (bfd_seek (el
->my_archive
, filepos
, SEEK_SET
) != 0)
1197 /* Read Record length + MHD + align byte. */
1198 len
= tdata
->mhd_size
;
1199 if (vms_lib_bread_raw (el
, buf
, 2) != 2)
1201 if (bfd_getl16 (buf
) != len
)
1203 len
= (len
+ 1) & ~1;
1204 BFD_ASSERT (len
<= sizeof (buf
));
1205 if (vms_lib_bread_raw (el
, buf
, len
) != len
)
1208 /* Get info from mhd. */
1209 mhd
= (struct vms_mhd
*)buf
;
1211 if (mhd
->id
!= MHD__C_MHDID
)
1213 if (len
>= MHD__C_MHDLEN
+ 1)
1214 el
->selective_search
= (mhd
->objstat
& MHD__M_SELSRC
) ? 1 : 0;
1215 el
->mtime
= vms_rawtime_to_time_t (mhd
->datim
);
1216 el
->mtime_set
= TRUE
;
1218 /* Reinit the iovec so that seek() will point to the first record after
1221 vec
->init_blk_off
= vec
->blk_off
;
1222 vec
->init_next_block
= vec
->next_block
;
1223 vec
->first_block
= bfd_tell (el
->my_archive
);
1224 vec
->dcxsbms
= bfd_libdata (el
->my_archive
)->dcxsbm
;
1226 if (vec
->dcxsbms
!= NULL
)
1229 vec
->dcx_max
= 10 * 1024;
1230 vec
->dcx_buf
= bfd_alloc (el
, vec
->dcx_max
);
1232 if (vec
->dcx_buf
== NULL
)
1238 /* Get member MODIDX. Return NULL in case of error. */
1241 _bfd_vms_lib_get_module (bfd
*abfd
, unsigned int modidx
)
1243 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
1248 if (modidx
>= tdata
->nbr_modules
)
1251 /* Already loaded. */
1252 if (tdata
->cache
[modidx
])
1253 return tdata
->cache
[modidx
];
1256 file_off
= tdata
->modules
[modidx
].file_offset
;
1257 if (tdata
->type
!= LBR__C_TYP_IOBJ
)
1259 res
= _bfd_create_empty_archive_element_shell (abfd
);
1263 /* Special reader to deal with data blocks. */
1264 if (!vms_lib_bopen (res
, file_off
))
1270 struct vms_mhd
*mhd
;
1271 struct areltdata
*arelt
;
1273 /* Sanity check. The MHD must be big enough to contain module size. */
1274 if (tdata
->mhd_size
< offsetof (struct vms_mhd
, modsize
) + 4)
1277 /* Read the MHD now. */
1278 if (bfd_seek (abfd
, file_off
, SEEK_SET
) != 0)
1280 if (bfd_bread (buf
, tdata
->mhd_size
, abfd
) != tdata
->mhd_size
)
1283 res
= _bfd_create_empty_archive_element_shell (abfd
);
1286 arelt
= bfd_zalloc (res
, sizeof (*arelt
));
1289 res
->arelt_data
= arelt
;
1291 /* Get info from mhd. */
1292 mhd
= (struct vms_mhd
*)buf
;
1293 if (mhd
->id
!= MHD__C_MHDID
)
1295 if (tdata
->mhd_size
>= offsetof (struct vms_mhd
, objstat
) + 1)
1296 res
->selective_search
= (mhd
->objstat
& MHD__M_SELSRC
) ? 1 : 0;
1297 res
->mtime
= vms_rawtime_to_time_t (mhd
->datim
);
1298 res
->mtime_set
= TRUE
;
1300 arelt
->parsed_size
= bfd_getl32 (mhd
->modsize
);
1302 /* No need for a special reader as members are stored linearly.
1303 Just skip the MHD. */
1304 res
->origin
= file_off
+ tdata
->mhd_size
;
1307 res
->filename
= tdata
->modules
[modidx
].name
;
1309 tdata
->cache
[modidx
] = res
;
1314 /* Standard function: get member at IDX. */
1317 _bfd_vms_lib_get_elt_at_index (bfd
*abfd
, symindex symidx
)
1319 struct lib_tdata
*tdata
= bfd_libdata (abfd
);
1321 unsigned int modidx
;
1324 if (symidx
> tdata
->artdata
.symdef_count
)
1326 file_off
= tdata
->artdata
.symdefs
[symidx
].file_offset
;
1329 for (modidx
= 0; modidx
< tdata
->nbr_modules
; modidx
++)
1331 if (tdata
->modules
[modidx
].file_offset
== file_off
)
1334 if (modidx
>= tdata
->nbr_modules
)
1337 return _bfd_vms_lib_get_module (abfd
, modidx
);
1340 /* Elements of an imagelib are stubs. You can get the real image with this
1344 _bfd_vms_lib_get_imagelib_file (bfd
*el
)
1346 bfd
*archive
= el
->my_archive
;
1347 const char *modname
= el
->filename
;
1348 int modlen
= strlen (modname
);
1353 /* Convert module name to lower case and append '.exe'. */
1354 filename
= bfd_alloc (el
, modlen
+ 5);
1355 if (filename
== NULL
)
1357 for (j
= 0; j
< modlen
; j
++)
1358 if (ISALPHA (modname
[j
]))
1359 filename
[j
] = TOLOWER (modname
[j
]);
1361 filename
[j
] = modname
[j
];
1362 memcpy (filename
+ modlen
, ".exe", 5);
1364 filename
= _bfd_append_relative_path (archive
, filename
);
1365 if (filename
== NULL
)
1367 res
= bfd_openr (filename
, NULL
);
1371 (*_bfd_error_handler
)(_("could not open shared image '%s' from '%s'"),
1372 filename
, archive
->filename
);
1373 bfd_release (archive
, filename
);
1377 /* FIXME: put it in a cache ? */
1381 /* Standard function. */
1384 _bfd_vms_lib_openr_next_archived_file (bfd
*archive
,
1393 idx
= last_file
->proxy_origin
+ 1;
1395 if (idx
>= bfd_libdata (archive
)->nbr_modules
)
1397 bfd_set_error (bfd_error_no_more_archived_files
);
1401 res
= _bfd_vms_lib_get_module (archive
, idx
);
1404 res
->proxy_origin
= idx
;
1408 /* Standard function. Just compute the length. */
1411 _bfd_vms_lib_generic_stat_arch_elt (bfd
*abfd
, struct stat
*st
)
1413 struct lib_tdata
*tdata
;
1416 if (abfd
->my_archive
== NULL
)
1418 bfd_set_error (bfd_error_invalid_operation
);
1422 tdata
= bfd_libdata (abfd
->my_archive
);
1423 if (tdata
->type
!= LBR__C_TYP_IOBJ
)
1425 struct vms_lib_iovec
*vec
= (struct vms_lib_iovec
*) abfd
->iostream
;
1427 if (vec
->file_len
== (ufile_ptr
)-1)
1429 if (vms_lib_bseek (abfd
, 0, SEEK_SET
) != 0)
1432 /* Compute length. */
1433 while (vms_lib_bread (abfd
, NULL
, 1 << 20) > 0)
1436 st
->st_size
= vec
->file_len
;
1440 st
->st_size
= ((struct areltdata
*)abfd
->arelt_data
)->parsed_size
;
1443 if (abfd
->mtime_set
)
1444 st
->st_mtime
= abfd
->mtime
;
1454 /* Internal representation of an index entry. */
1458 /* Corresponding archive member. */
1461 /* Number of reference to this entry. */
1464 /* Length of the key. */
1465 unsigned short namlen
;
1471 /* Used to sort index entries. */
1474 vms_index_cmp (const void *lv
, const void *rv
)
1476 const struct vms_index
*l
= lv
;
1477 const struct vms_index
*r
= rv
;
1479 return strcmp (l
->name
, r
->name
);
1482 /* Maximum number of index blocks level. */
1484 #define MAX_LEVEL 10
1486 /* Get the size of an index entry. */
1489 get_idxlen (struct vms_index
*idx
)
1491 return 7 + idx
->namlen
;
1494 /* Write the index. VBN is the first vbn to be used, and will contain
1495 on return the last vbn.
1496 Return TRUE on success. */
1499 vms_write_index (bfd
*abfd
,
1500 struct vms_index
*idx
, unsigned int nbr
, unsigned int *vbn
,
1501 unsigned int *topvbn
)
1506 struct vms_indexdef
*rblk
[MAX_LEVEL
];
1511 unsigned short lastlen
;
1523 /* Sort the index the first time this function is called. */
1524 qsort (idx
, nbr
, sizeof (struct vms_index
), vms_index_cmp
);
1527 /* Allocate first index block. */
1530 rblk
[0] = bfd_malloc (sizeof (struct vms_indexdef
));
1531 blk
[0].vbn
= (*vbn
)++;
1535 for (i
= 0; i
< nbr
; i
++, idx
++)
1537 unsigned int idxlen
= get_idxlen (idx
);
1541 /* Check if a block might overflow. In this case we will flush this
1542 block and all the blocks below it. */
1543 for (j
= 0; j
< level
; j
++)
1544 if (blk
[j
].len
+ blk
[j
].lastlen
+ idxlen
> INDEXDEF__BLKSIZ
)
1547 for (j
= 0; j
< level
; j
++)
1551 /* There is not enough room to write the new entry in this
1552 block or in a parent block. */
1556 BFD_ASSERT (level
< MAX_LEVEL
);
1558 /* Need to create a parent. */
1561 rblk
[level
] = bfd_malloc (sizeof (struct vms_indexdef
));
1562 bfd_putl32 (*vbn
, rblk
[j
]->parent
);
1564 blk
[level
].vbn
= (*vbn
)++;
1566 blk
[level
].lastlen
= 0;
1571 /* Update parent block: write the new entry. */
1574 en
= (struct vms_idx
*)(rblk
[j
]->keys
+ blk
[j
].len
);
1575 memcpy (rblk
[j
+ 1]->keys
+ blk
[j
+ 1].len
, en
,
1577 en
= (struct vms_idx
*)(rblk
[j
+ 1]->keys
+ blk
[j
+ 1].len
);
1578 bfd_putl32 (blk
[j
].vbn
, en
->rfa
.vbn
);
1579 bfd_putl16 (RFADEF__C_INDEX
, en
->rfa
.offset
);
1584 /* And allocate it. Do it only on the block that won't be
1585 flushed (so that the parent of the parent can be
1587 blk
[j
+ 1].len
+= blk
[j
].lastlen
;
1588 blk
[j
+ 1].lastlen
= 0;
1591 /* Write this block on the disk. */
1594 bfd_putl16 (blk
[j
].len
+ blk
[j
].lastlen
, rblk
[j
]->used
);
1595 if (bfd_seek (abfd
, (blk
[j
].vbn
- 1) * VMS_BLOCK_SIZE
,
1598 if (bfd_bwrite (rblk
[j
], sizeof (struct vms_indexdef
), abfd
)
1599 != sizeof (struct vms_indexdef
))
1603 /* Reset this block. */
1606 blk
[j
].vbn
= (*vbn
)++;
1609 /* Append it to the block. */
1612 blk
[j
].len
+= blk
[j
].lastlen
;
1616 en
= (struct vms_idx
*)(rblk
[j
]->keys
+ blk
[j
].len
);
1617 bfd_putl32 ((idx
->abfd
->proxy_origin
/ VMS_BLOCK_SIZE
) + 1,
1620 ((idx
->abfd
->proxy_origin
% VMS_BLOCK_SIZE
) + DATA__DATA
,
1622 en
->keylen
= idx
->namlen
;
1623 memcpy (en
->keyname
, idx
->name
, idx
->namlen
);
1627 blk
[j
].lastlen
= idxlen
;
1632 *topvbn
= blk
[level
- 1].vbn
;
1638 for (j
= 0; j
< level
; j
++)
1642 /* Update parent block: write the new entry. */
1644 struct vms_idx
*par
;
1646 en
= (struct vms_idx
*)(rblk
[j
- 1]->keys
+ blk
[j
- 1].len
);
1647 par
= (struct vms_idx
*)(rblk
[j
]->keys
+ blk
[j
].len
);
1648 memcpy (par
, en
, blk
[j
- 1].lastlen
);
1649 bfd_putl32 (blk
[j
- 1].vbn
, par
->rfa
.vbn
);
1650 bfd_putl16 (RFADEF__C_INDEX
, par
->rfa
.offset
);
1653 /* Write this block on the disk. */
1654 bfd_putl16 (blk
[j
].len
+ blk
[j
].lastlen
, rblk
[j
]->used
);
1655 if (bfd_seek (abfd
, (blk
[j
].vbn
- 1) * VMS_BLOCK_SIZE
,
1658 if (bfd_bwrite (rblk
[j
], sizeof (struct vms_indexdef
), abfd
)
1659 != sizeof (struct vms_indexdef
))
1668 /* Append data to the data block DATA. Force write if PAD is true. */
1671 vms_write_data_block (bfd
*arch
, struct vms_datadef
*data
, file_ptr
*off
,
1672 const unsigned char *buf
, unsigned int len
, int pad
)
1674 while (len
> 0 || pad
)
1676 unsigned int doff
= *off
& (VMS_BLOCK_SIZE
- 1);
1677 unsigned int remlen
= (DATA__LENGTH
- DATA__DATA
) - doff
;
1680 l
= (len
> remlen
) ? remlen
: len
;
1681 memcpy (data
->data
+ doff
, buf
, l
);
1687 if (doff
== (DATA__LENGTH
- DATA__DATA
) || (len
== 0 && pad
))
1691 bfd_putl32 ((*off
/ VMS_BLOCK_SIZE
) + 2, data
->link
);
1693 if (bfd_bwrite (data
, sizeof (*data
), arch
) != sizeof (*data
))
1696 *off
+= DATA__LENGTH
- doff
;
1705 /* Build the symbols index. */
1708 _bfd_vms_lib_build_map (unsigned int nbr_modules
,
1709 struct vms_index
*modules
,
1710 unsigned int *res_cnt
,
1711 struct vms_index
**res
)
1714 asymbol
**syms
= NULL
;
1716 struct vms_index
*map
= NULL
;
1717 unsigned int map_max
= 1024; /* Fine initial default. */
1718 unsigned int map_count
= 0;
1720 map
= (struct vms_index
*) bfd_malloc (map_max
* sizeof (struct vms_index
));
1724 /* Gather symbols. */
1725 for (i
= 0; i
< nbr_modules
; i
++)
1730 bfd
*current
= modules
[i
].abfd
;
1732 if ((bfd_get_file_flags (current
) & HAS_SYMS
) == 0)
1735 storage
= bfd_get_symtab_upper_bound (current
);
1741 if (storage
> syms_max
)
1746 syms
= (asymbol
**) bfd_malloc (syms_max
);
1750 symcount
= bfd_canonicalize_symtab (current
, syms
);
1754 /* Now map over all the symbols, picking out the ones we
1756 for (src_count
= 0; src_count
< symcount
; src_count
++)
1758 flagword flags
= (syms
[src_count
])->flags
;
1759 asection
*sec
= syms
[src_count
]->section
;
1761 if ((flags
& BSF_GLOBAL
1763 || flags
& BSF_INDIRECT
1764 || bfd_is_com_section (sec
))
1765 && ! bfd_is_und_section (sec
))
1767 struct vms_index
*new_map
;
1769 /* This symbol will go into the archive header. */
1770 if (map_count
== map_max
)
1773 new_map
= (struct vms_index
*)
1774 bfd_realloc (map
, map_max
* sizeof (struct vms_index
));
1775 if (new_map
== NULL
)
1780 map
[map_count
].abfd
= current
;
1781 /* FIXME: check length. */
1782 map
[map_count
].namlen
= strlen (syms
[src_count
]->name
);
1783 map
[map_count
].name
= syms
[src_count
]->name
;
1791 *res_cnt
= map_count
;
1803 /* Do the hard work: write an archive on the disk. */
1806 _bfd_vms_lib_write_archive_contents (bfd
*arch
)
1809 unsigned int nbr_modules
;
1810 struct vms_index
*modules
;
1811 unsigned int nbr_symbols
;
1812 struct vms_index
*symbols
;
1813 struct lib_tdata
*tdata
= bfd_libdata (arch
);
1816 unsigned int nbr_mod_iblk
;
1817 unsigned int nbr_sym_iblk
;
1819 unsigned int mod_idx_vbn
;
1820 unsigned int sym_idx_vbn
;
1822 /* Count the number of modules (and do a first sanity check). */
1824 for (current
= arch
->archive_head
;
1826 current
= current
->archive_next
)
1828 /* This check is checking the bfds for the objects we're reading
1829 from (which are usually either an object file or archive on
1830 disk), not the archive entries we're writing to. We don't
1831 actually create bfds for the archive members, we just copy
1832 them byte-wise when we write out the archive. */
1833 if (bfd_write_p (current
) || !bfd_check_format (current
, bfd_object
))
1835 bfd_set_error (bfd_error_invalid_operation
);
1842 /* Build the modules list. */
1843 BFD_ASSERT (tdata
->modules
== NULL
);
1844 modules
= bfd_alloc (arch
, nbr_modules
* sizeof (struct vms_index
));
1845 if (modules
== NULL
)
1848 for (current
= arch
->archive_head
, i
= 0;
1850 current
= current
->archive_next
, i
++)
1854 modules
[i
].abfd
= current
;
1855 modules
[i
].name
= vms_get_module_name (current
->filename
, FALSE
);
1858 /* FIXME: silently truncate long names ? */
1859 nl
= strlen (modules
[i
].name
);
1860 modules
[i
].namlen
= (nl
> MAX_KEYLEN
? MAX_KEYLEN
: nl
);
1863 /* Create the module index. */
1865 if (!vms_write_index (NULL
, modules
, nbr_modules
, &vbn
, NULL
))
1869 /* Create symbol index. */
1870 if (!_bfd_vms_lib_build_map (nbr_modules
, modules
, &nbr_symbols
, &symbols
))
1874 if (!vms_write_index (NULL
, symbols
, nbr_symbols
, &vbn
, NULL
))
1878 /* Write modules and remember their position. */
1879 off
= (1 + nbr_mod_iblk
+ nbr_sym_iblk
) * VMS_BLOCK_SIZE
;
1881 if (bfd_seek (arch
, off
, SEEK_SET
) != 0)
1884 for (i
= 0; i
< nbr_modules
; i
++)
1886 struct vms_datadef data
;
1887 unsigned char blk
[VMS_BLOCK_SIZE
];
1888 struct vms_mhd
*mhd
;
1891 current
= modules
[i
].abfd
;
1892 current
->proxy_origin
= off
;
1894 bfd_putl16 (sizeof (struct vms_mhd
), blk
);
1895 mhd
= (struct vms_mhd
*)(blk
+ 2);
1896 memset (mhd
, 0, sizeof (struct vms_mhd
));
1898 mhd
->id
= MHD__C_MHDID
;
1900 memcpy (mhd
->objid
, "V1.0", 4);
1901 bfd_putl32 (modules
[i
].ref
, mhd
->refcnt
);
1904 sz
= (2 + sizeof (struct vms_mhd
) + 1) & ~1;
1905 if (vms_write_data_block (arch
, &data
, &off
, blk
, sz
, 0) < 0)
1908 if (bfd_seek (current
, 0, SEEK_SET
) != 0)
1913 sz
= bfd_bread (blk
, sizeof (blk
), current
);
1916 if (vms_write_data_block (arch
, &data
, &off
, blk
, sz
, 0) < 0)
1919 if (vms_write_data_block (arch
, &data
, &off
,
1920 eotdesc
, sizeof (eotdesc
), 1) < 0)
1924 /* Write the indexes. */
1926 if (vms_write_index (arch
, modules
, nbr_modules
, &vbn
, &mod_idx_vbn
) != TRUE
)
1928 if (vms_write_index (arch
, symbols
, nbr_symbols
, &vbn
, &sym_idx_vbn
) != TRUE
)
1931 /* Write libary header. */
1933 unsigned char blk
[VMS_BLOCK_SIZE
];
1934 struct vms_lhd
*lhd
= (struct vms_lhd
*)blk
;
1935 struct vms_idd
*idd
= (struct vms_idd
*)(blk
+ sizeof (*lhd
));
1936 unsigned int idd_flags
;
1938 memset (blk
, 0, sizeof (blk
));
1940 lhd
->type
= LBR__C_TYP_EOBJ
;
1942 bfd_putl32 (LHD_SANEID3
, lhd
->sanity
);
1943 bfd_putl16 (3, lhd
->majorid
);
1944 bfd_putl16 (0, lhd
->minorid
);
1945 snprintf ((char *)lhd
->lbrver
+ 1, sizeof (lhd
->lbrver
) - 1,
1947 (unsigned)(BFD_VERSION
/ 100000000UL),
1948 (unsigned)(BFD_VERSION
/ 1000000UL) % 100,
1949 (unsigned)(BFD_VERSION
/ 10000UL) % 100);
1950 lhd
->lbrver
[sizeof (lhd
->lbrver
) - 1] = 0;
1951 lhd
->lbrver
[0] = strlen ((char *)lhd
->lbrver
+ 1);
1954 bfd_putl64 (0, lhd
->credat
);
1955 bfd_putl64 (0, lhd
->updtim
);
1957 lhd
->mhdusz
= sizeof (struct vms_mhd
) - MHD__C_USRDAT
;
1959 bfd_putl32 (nbr_modules
+ nbr_symbols
, lhd
->idxcnt
);
1960 bfd_putl32 (nbr_modules
, lhd
->modcnt
);
1961 bfd_putl32 (nbr_modules
, lhd
->modhdrs
);
1963 bfd_putl32 (vbn
- 1, lhd
->hipreal
);
1964 bfd_putl32 (vbn
- 1, lhd
->hiprusd
);
1966 /* First index (modules name). */
1967 idd_flags
= IDD__FLAGS_ASCII
| IDD__FLAGS_VARLENIDX
1968 | IDD__FLAGS_NOCASECMP
| IDD__FLAGS_NOCASENTR
;
1969 bfd_putl16 (idd_flags
, idd
->flags
);
1970 bfd_putl16 (MAX_KEYLEN
, idd
->keylen
);
1971 bfd_putl16 (mod_idx_vbn
, idd
->vbn
);
1974 /* Second index (symbols name). */
1975 bfd_putl16 (idd_flags
, idd
->flags
);
1976 bfd_putl16 (MAX_KEYLEN
, idd
->keylen
);
1977 bfd_putl16 (sym_idx_vbn
, idd
->vbn
);
1980 if (bfd_seek (arch
, 0, SEEK_SET
) != 0)
1982 if (bfd_bwrite (blk
, sizeof (blk
), arch
) != sizeof (blk
))
1989 bfd_set_error (bfd_error_on_input
, current
, bfd_get_error ());
1993 /* Add a target for text library. This costs almost nothing and is useful to
1994 read VMS library on the host. */
1996 const bfd_target vms_lib_txt_vec
=
1998 "vms-libtxt", /* Name. */
1999 bfd_target_unknown_flavour
,
2000 BFD_ENDIAN_UNKNOWN
, /* byteorder */
2001 BFD_ENDIAN_UNKNOWN
, /* header_byteorder */
2002 0, /* Object flags. */
2003 0, /* Sect flags. */
2004 0, /* symbol_leading_char. */
2005 ' ', /* ar_pad_char. */
2006 15, /* ar_max_namelen. */
2007 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2008 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2009 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
,
2010 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2011 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2012 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
,
2014 {_bfd_dummy_target
, _bfd_dummy_target
, /* bfd_check_format. */
2015 _bfd_vms_lib_txt_archive_p
, _bfd_dummy_target
},
2016 {bfd_false
, bfd_false
, bfd_false
, bfd_false
}, /* bfd_set_format. */
2017 {bfd_false
, bfd_false
, bfd_false
, bfd_false
}, /* bfd_write_contents. */
2019 BFD_JUMP_TABLE_GENERIC (_bfd_generic
),
2020 BFD_JUMP_TABLE_COPY (_bfd_generic
),
2021 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2022 BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib
),
2023 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols
),
2024 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
2025 BFD_JUMP_TABLE_WRITE (_bfd_nowrite
),
2026 BFD_JUMP_TABLE_LINK (_bfd_nolink
),
2027 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),