1 /* BFD back-end for VMS archive files.
3 Copyright 2010, 2011, 2012 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 128
39 #define MAX_EKEYLEN 1024
45 unsigned char min_char;
46 unsigned char max_char;
52 /* Kind of library. Used to filter in archive_p. */
62 /* Back-end private data. */
66 /* Standard tdata for an archive. But we don't use many fields. */
67 struct artdata artdata;
72 /* Type of the archive. */
75 /* Kind of archive. Summary of its type. */
76 enum vms_lib_kind kind;
78 /* Total size of the mhd (element header). */
79 unsigned int mhd_size;
82 unsigned int credat_lo;
83 unsigned int credat_hi;
85 /* Vector of modules (archive elements), already sorted. */
86 unsigned int nbr_modules;
87 struct carsym *modules;
90 /* DCX (decompression) data. */
91 unsigned int nbr_dcxsbm;
92 struct dcxsbm_desc *dcxsbm;
95 #define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
97 /* End-Of-Text pattern. This is a special record to mark the end of file. */
99 static const unsigned char eotdesc[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
101 /* Describe the current state of carsym entries while building the archive
102 table of content. Things are simple with Alpha archives as the number
103 of entries is known, but with IA64 archives a entry can make a reference
104 to severals members. Therefore we must be able to extend the table on the
105 fly, but it should be allocated on the bfd - which doesn't support realloc.
106 To reduce the overhead, the table is initially allocated in the BFD's
107 objalloc and extended if necessary on the heap. In the later case, it
108 is finally copied to the BFD's objalloc so that it will automatically be
113 /* The table of content. */
116 /* Number of entries used in the table. */
119 /* Maximum number of entries. */
122 /* If true, the table was reallocated on the heap. If false, it is still
123 in the BFD's objalloc. */
124 bfd_boolean realloced;
127 /* Simply add a name to the index. */
130 vms_add_index (struct carsym_mem *cs, char *name,
131 unsigned int idx_vbn, unsigned int idx_off)
133 if (cs->nbr == cs->max)
137 cs->max = 2 * cs->max + 32;
141 n = bfd_malloc2 (cs->max, sizeof (struct carsym));
144 memcpy (n, cs->idx, cs->nbr * sizeof (struct carsym));
145 /* And unfortunately we can't free cs->idx. */
149 n = bfd_realloc_or_free (cs->idx, cs->nbr * sizeof (struct carsym));
154 cs->realloced = TRUE;
156 cs->idx[cs->nbr].file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
157 cs->idx[cs->nbr].name = name;
162 /* Follow all member of a lns list (pointed by RFA) and add indexes for
163 NAME. Return FALSE in case of error. */
166 vms_add_indexes_from_list (bfd *abfd, struct carsym_mem *cs, char *name,
175 vbn = bfd_getl32 (rfa->vbn);
180 off = (vbn - 1) * VMS_BLOCK_SIZE + bfd_getl16 (rfa->offset);
181 if (bfd_seek (abfd, off, SEEK_SET) != 0
182 || bfd_bread (&lns, sizeof (lns), abfd) != sizeof (lns))
185 if (!vms_add_index (cs, name,
186 bfd_getl32 (lns.modrfa.vbn),
187 bfd_getl16 (lns.modrfa.offset)))
194 /* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
197 vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
201 off = (vbn - 1) * VMS_BLOCK_SIZE;
202 if (bfd_seek (abfd, off, SEEK_SET) != 0
203 || bfd_bread (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
209 /* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
212 vms_write_block (bfd *abfd, unsigned int vbn, void *blk)
216 off = (vbn - 1) * VMS_BLOCK_SIZE;
217 if (bfd_seek (abfd, off, SEEK_SET) != 0
218 || bfd_bwrite (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
224 /* Read index block VBN and put the entry in **IDX (which is updated).
225 If the entry is indirect, recurse. */
228 vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs)
230 struct vms_indexdef indexdef;
235 /* Read the index block. */
236 BFD_ASSERT (sizeof (indexdef) == VMS_BLOCK_SIZE);
237 if (!vms_read_block (abfd, vbn, &indexdef))
241 p = &indexdef.keys[0];
242 endp = p + bfd_getl16 (indexdef.used);
245 unsigned int idx_vbn;
246 unsigned int idx_off;
248 unsigned char *keyname;
251 /* Extract key length. */
252 if (bfd_libdata (abfd)->ver == LBR_MAJORID)
254 struct vms_idx *ridx = (struct vms_idx *)p;
256 idx_vbn = bfd_getl32 (ridx->rfa.vbn);
257 idx_off = bfd_getl16 (ridx->rfa.offset);
259 keylen = ridx->keylen;
261 keyname = ridx->keyname;
263 else if (bfd_libdata (abfd)->ver == LBR_ELFMAJORID)
265 struct vms_elfidx *ridx = (struct vms_elfidx *)p;
267 idx_vbn = bfd_getl32 (ridx->rfa.vbn);
268 idx_off = bfd_getl16 (ridx->rfa.offset);
270 keylen = bfd_getl16 (ridx->keylen);
272 keyname = ridx->keyname;
281 /* Point to the next index entry. */
282 p = keyname + keylen;
284 if (idx_off == RFADEF__C_INDEX)
286 /* Indirect entry. Recurse. */
287 if (!vms_traverse_index (abfd, idx_vbn, cs))
292 /* Add a new entry. */
295 if (flags & ELFIDX__SYMESC)
297 /* Extended key name. */
298 unsigned int noff = 0;
302 unsigned char kblk[VMS_BLOCK_SIZE];
305 if (keylen != sizeof (struct vms_kbn))
308 kbn = (struct vms_kbn *)keyname;
309 keylen = bfd_getl16 (kbn->keylen);
311 name = bfd_alloc (abfd, keylen + 1);
314 kvbn = bfd_getl32 (kbn->rfa.vbn);
315 koff = bfd_getl16 (kbn->rfa.offset);
317 /* Read the key, chunk by chunk. */
322 if (!vms_read_block (abfd, kvbn, kblk))
324 kbn = (struct vms_kbn *)(kblk + koff);
325 klen = bfd_getl16 (kbn->keylen);
326 kvbn = bfd_getl32 (kbn->rfa.vbn);
327 koff = bfd_getl16 (kbn->rfa.offset);
329 memcpy (name + noff, kbn + 1, klen);
340 /* Usual key name. */
341 name = bfd_alloc (abfd, keylen + 1);
345 memcpy (name, keyname, keylen);
349 if (flags & ELFIDX__LISTRFA)
354 off = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
355 if (bfd_seek (abfd, off, SEEK_SET) != 0
356 || bfd_bread (&lhs, sizeof (lhs), abfd) != sizeof (lhs))
359 /* FIXME: this adds extra entries that were not accounted. */
360 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_g_rfa))
362 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_wk_rfa))
364 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_g_rfa))
366 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_wk_rfa))
371 if (!vms_add_index (cs, name, idx_vbn, idx_off))
380 /* Read index #IDX, which must have NBREL entries. */
382 static struct carsym *
383 vms_lib_read_index (bfd *abfd, int idx, unsigned int *nbrel)
388 struct carsym *csbuf;
389 struct carsym_mem csm;
391 /* Read index desription. */
392 if (bfd_seek (abfd, LHD_IDXDESC + idx * IDD_LENGTH, SEEK_SET) != 0
393 || bfd_bread (&idd, sizeof (idd), abfd) != sizeof (idd))
397 flags = bfd_getl16 (idd.flags);
398 if (!(flags & IDD__FLAGS_ASCII)
399 || !(flags & IDD__FLAGS_VARLENIDX))
402 csbuf = bfd_alloc (abfd, *nbrel * sizeof (struct carsym));
408 csm.realloced = FALSE;
411 /* Note: if the index is empty, there is no block to traverse. */
412 vbn = bfd_getl32 (idd.vbn);
413 if (vbn != 0 && !vms_traverse_index (abfd, vbn, &csm))
415 if (csm.realloced && csm.idx != NULL)
418 /* Note: in case of error, we can free what was allocated on the
420 bfd_release (abfd, csbuf);
426 /* There are more entries than the first estimate. Allocate on
427 the BFD's objalloc. */
428 csbuf = bfd_alloc (abfd, csm.nbr * sizeof (struct carsym));
431 memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym));
438 /* Standard function. */
440 static const bfd_target *
441 _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
445 unsigned int majorid;
446 struct lib_tdata *tdata_hold;
447 struct lib_tdata *tdata;
449 unsigned int nbr_ent;
452 if (bfd_bread (&lhd, sizeof (lhd), abfd) != sizeof (lhd))
454 if (bfd_get_error () != bfd_error_system_call)
455 bfd_set_error (bfd_error_wrong_format);
459 /* Check sanity (= magic) number. */
460 sanity = bfd_getl32 (lhd.sanity);
461 if (!(sanity == LHD_SANEID3
462 || sanity == LHD_SANEID6
463 || sanity == LHD_SANEID_DCX))
465 bfd_set_error (bfd_error_wrong_format);
468 majorid = bfd_getl32 (lhd.majorid);
470 /* Check archive kind. */
474 if ((lhd.type != LBR__C_TYP_EOBJ && lhd.type != LBR__C_TYP_ESHSTB)
475 || majorid != LBR_MAJORID
478 bfd_set_error (bfd_error_wrong_format);
483 if ((lhd.type != LBR__C_TYP_IOBJ && lhd.type != LBR__C_TYP_ISHSTB)
484 || majorid != LBR_ELFMAJORID
487 bfd_set_error (bfd_error_wrong_format);
492 if ((lhd.type != LBR__C_TYP_TXT
493 && lhd.type != LBR__C_TYP_MLB
494 && lhd.type != LBR__C_TYP_HLP)
495 || majorid != LBR_MAJORID
498 bfd_set_error (bfd_error_wrong_format);
506 /* Allocate and initialize private data. */
507 tdata_hold = bfd_libdata (abfd);
508 tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
511 abfd->tdata.any = (void *)tdata;
512 tdata->ver = majorid;
513 tdata->mhd_size = MHD__C_USRDAT + lhd.mhdusz;
514 tdata->type = lhd.type;
516 tdata->credat_lo = bfd_getl32 (lhd.credat + 0);
517 tdata->credat_hi = bfd_getl32 (lhd.credat + 4);
520 tdata->nbr_modules = bfd_getl32 (lhd.modcnt);
521 tdata->artdata.symdef_count = bfd_getl32 (lhd.idxcnt) - tdata->nbr_modules;
522 nbr_ent = tdata->nbr_modules;
523 tdata->modules = vms_lib_read_index (abfd, 0, &nbr_ent);
524 if (tdata->modules == NULL || nbr_ent != tdata->nbr_modules)
528 nbr_ent = tdata->artdata.symdef_count;
529 tdata->artdata.symdefs = vms_lib_read_index (abfd, 1, &nbr_ent);
530 if (tdata->artdata.symdefs == NULL)
532 /* Only IA64 archives may have more entries in the index that what
534 if (nbr_ent != tdata->artdata.symdef_count
535 && kind != vms_lib_ia64)
537 tdata->artdata.symdef_count = nbr_ent;
539 tdata->cache = bfd_zalloc (abfd, sizeof (bfd *) * tdata->nbr_modules);
540 if (tdata->cache == NULL)
543 /* Read DCX submaps. */
544 dcxvbn = bfd_getl32 (lhd.dcxmapvbn);
547 unsigned char buf_reclen[4];
550 struct vms_dcxmap *map;
551 unsigned int sbm_off;
554 if (bfd_seek (abfd, (dcxvbn - 1) * VMS_BLOCK_SIZE, SEEK_SET) != 0
555 || bfd_bread (buf_reclen, sizeof (buf_reclen), abfd)
556 != sizeof (buf_reclen))
558 reclen = bfd_getl32 (buf_reclen);
559 buf = bfd_malloc (reclen);
562 if (bfd_bread (buf, reclen, abfd) != reclen)
567 map = (struct vms_dcxmap *)buf;
568 tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs);
569 sbm_off = bfd_getl16 (map->sub0);
570 tdata->dcxsbm = (struct dcxsbm_desc *)bfd_alloc
571 (abfd, tdata->nbr_dcxsbm * sizeof (struct dcxsbm_desc));
572 for (i = 0; i < tdata->nbr_dcxsbm; i++)
574 struct vms_dcxsbm *sbm = (struct vms_dcxsbm *) (buf + sbm_off);
575 struct dcxsbm_desc *sbmdesc = &tdata->dcxsbm[i];
576 unsigned int sbm_len;
579 unsigned char *data = (unsigned char *)sbm;
583 sbm_sz = bfd_getl16 (sbm->size);
585 BFD_ASSERT (sbm_off <= reclen);
587 sbmdesc->min_char = sbm->min_char;
588 BFD_ASSERT (sbmdesc->min_char == 0);
589 sbmdesc->max_char = sbm->max_char;
590 sbm_len = sbmdesc->max_char - sbmdesc->min_char + 1;
591 l = (2 * sbm_len + 7) / 8;
593 (sbm_sz >= sizeof (struct vms_dcxsbm) + l + 3 * sbm_len
594 || (tdata->nbr_dcxsbm == 1
595 && sbm_sz >= sizeof (struct vms_dcxsbm) + l + sbm_len));
596 sbmdesc->flags = (unsigned char *)bfd_alloc (abfd, l);
597 memcpy (sbmdesc->flags, data + bfd_getl16 (sbm->flags), l);
598 sbmdesc->nodes = (unsigned char *)bfd_alloc (abfd, 2 * sbm_len);
599 memcpy (sbmdesc->nodes, data + bfd_getl16 (sbm->nodes), 2 * sbm_len);
600 off = bfd_getl16 (sbm->next);
603 /* Read the 'next' array. */
604 sbmdesc->next = (unsigned short *)bfd_alloc
605 (abfd, sbm_len * sizeof (unsigned short));
607 for (j = 0; j < sbm_len; j++)
608 sbmdesc->next[j] = bfd_getl16 (buf1 + j * 2);
612 /* There is no next array if there is only one submap. */
613 BFD_ASSERT (tdata->nbr_dcxsbm == 1);
614 sbmdesc->next = NULL;
621 tdata->nbr_dcxsbm = 0;
624 /* The map is always present. Also mark shared image library. */
625 abfd->has_armap = TRUE;
626 if (tdata->type == LBR__C_TYP_ESHSTB || tdata->type == LBR__C_TYP_ISHSTB)
627 abfd->is_thin_archive = TRUE;
632 bfd_release (abfd, tdata);
633 abfd->tdata.any = (void *)tdata_hold;
637 /* Standard function for alpha libraries. */
640 _bfd_vms_lib_alpha_archive_p (bfd *abfd)
642 return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
645 /* Standard function for ia64 libraries. */
648 _bfd_vms_lib_ia64_archive_p (bfd *abfd)
650 return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
653 /* Standard function for text libraries. */
655 static const bfd_target *
656 _bfd_vms_lib_txt_archive_p (bfd *abfd)
658 return _bfd_vms_lib_archive_p (abfd, vms_lib_txt);
661 /* Standard bfd function. */
664 _bfd_vms_lib_mkarchive (bfd *abfd, enum vms_lib_kind kind)
666 struct lib_tdata *tdata;
668 tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
672 abfd->tdata.any = (void *)tdata;
673 vms_get_time (&tdata->credat_hi, &tdata->credat_lo);
679 tdata->ver = LBR_MAJORID;
680 tdata->mhd_size = offsetof (struct vms_mhd, pad1);
681 tdata->type = LBR__C_TYP_EOBJ;
684 tdata->ver = LBR_ELFMAJORID;
685 tdata->mhd_size = sizeof (struct vms_mhd);
686 tdata->type = LBR__C_TYP_IOBJ;
692 tdata->nbr_modules = 0;
693 tdata->artdata.symdef_count = 0;
694 tdata->modules = NULL;
695 tdata->artdata.symdefs = NULL;
702 _bfd_vms_lib_alpha_mkarchive (bfd *abfd)
704 return _bfd_vms_lib_mkarchive (abfd, vms_lib_alpha);
708 _bfd_vms_lib_ia64_mkarchive (bfd *abfd)
710 return _bfd_vms_lib_mkarchive (abfd, vms_lib_ia64);
713 /* Find NAME in the symbol index. Return the index. */
716 _bfd_vms_lib_find_symbol (bfd *abfd, const char *name)
718 struct lib_tdata *tdata = bfd_libdata (abfd);
719 carsym *syms = tdata->artdata.symdefs;
722 /* Open-coded binary search for speed. */
724 hi = tdata->artdata.symdef_count - 1;
728 int mid = lo + (hi - lo) / 2;
731 diff = (char)(name[0] - syms[mid].name[0]);
733 diff = strcmp (name, syms[mid].name);
741 return BFD_NO_MORE_SYMBOLS;
744 /* IO vector for archive member. Need that because members are not linearly
745 stored in archives. */
749 /* Current offset. */
752 /* Length of the module, when known. */
755 /* Current position in the record from bfd_bread point of view (ie, after
756 decompression). 0 means that no data byte have been read, -2 and -1
757 are reserved for the length word. */
759 #define REC_POS_NL -4
760 #define REC_POS_PAD -3
761 #define REC_POS_LEN0 -2
762 #define REC_POS_LEN1 -1
765 unsigned short rec_len;
766 /* Number of bytes to read in the current record. */
767 unsigned short rec_rem;
768 /* Offset of the next block. */
770 /* Current *data* offset in the data block. */
771 unsigned short blk_off;
773 /* Offset of the first block. Extracted from the index. */
774 file_ptr first_block;
776 /* Initial next_block. Extracted when the MHD is read. */
777 file_ptr init_next_block;
778 /* Initial blk_off, once the MHD is read. */
779 unsigned short init_blk_off;
781 /* Used to store any 3 byte record, which could be the EOF pattern. */
782 unsigned char pattern[4];
785 struct dcxsbm_desc *dcxsbms;
786 /* Current submap. */
787 struct dcxsbm_desc *dcx_sbm;
788 /* Current offset in the submap. */
789 unsigned int dcx_offset;
792 /* Compressed buffer. */
793 unsigned char *dcx_buf;
794 /* Size of the buffer. Used to resize. */
795 unsigned int dcx_max;
796 /* Number of valid bytes in the buffer. */
797 unsigned int dcx_rlen;
800 /* Return the current position. */
803 vms_lib_btell (struct bfd *abfd)
805 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
809 /* Read the header of the next data block if all bytes of the current block
813 vms_lib_read_block (struct bfd *abfd)
815 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
817 if (vec->blk_off == DATA__LENGTH)
819 unsigned char hdr[DATA__DATA];
821 /* Read next block. */
822 if (bfd_seek (abfd->my_archive, vec->next_block, SEEK_SET) != 0)
824 if (bfd_bread (hdr, sizeof (hdr), abfd->my_archive) != sizeof (hdr))
826 vec->next_block = (bfd_getl32 (hdr + 2) - 1) * VMS_BLOCK_SIZE;
827 vec->blk_off = sizeof (hdr);
832 /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
833 not stored. Read linearly from the library, but handle blocks. This
834 function does not handle records nor EOF. */
837 vms_lib_bread_raw (struct bfd *abfd, unsigned char *buf, file_ptr nbytes)
839 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
847 /* Be sure the current data block is read. */
848 if (!vms_lib_read_block (abfd))
851 /* Do not read past the data block, do not read more than requested. */
852 l = DATA__LENGTH - vec->blk_off;
859 /* Really read into BUF. */
860 if (bfd_bread (buf, l, abfd->my_archive) != l)
865 /* Make as if we are reading. */
866 if (bfd_seek (abfd->my_archive, l, SEEK_CUR) != 0)
879 /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
882 vms_lib_dcx (struct vms_lib_iovec *vec, unsigned char *buf, file_ptr nbytes)
884 struct dcxsbm_desc *sbm;
890 /* The loop below expect to deliver at least one byte. */
894 /* Get the current state. */
896 offset = vec->dcx_offset;
897 j = vec->dcx_pos & 7;
899 for (i = vec->dcx_pos >> 3; i < vec->dcx_rlen; i++)
901 unsigned char b = vec->dcx_buf[i];
907 if (!(sbm->flags[offset >> 3] & (1 << (offset & 7))))
909 unsigned int n_offset = sbm->nodes[offset];
912 /* End of buffer. Stay where we are. */
913 vec->dcx_pos = (i << 3) + j;
916 vec->dcx_offset = offset;
920 offset = 2 * n_offset;
924 unsigned char v = sbm->nodes[offset];
926 if (sbm->next != NULL)
927 sbm = vec->dcxsbms + sbm->next[v];
938 vec->dcx_pos = (i << 3) + j + 1;
939 vec->dcx_offset = offset;
952 /* Standard IOVEC function. */
955 vms_lib_bread (struct bfd *abfd, void *vbuf, file_ptr nbytes)
957 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
960 unsigned char *buf = (unsigned char *)vbuf;
962 /* Do not read past the end. */
963 if (vec->where >= vec->file_len)
969 if (vec->rec_rem == 0)
971 unsigned char blen[2];
973 /* Read record length. */
974 if (vms_lib_bread_raw (abfd, blen, sizeof (blen)) != sizeof (blen))
976 vec->rec_len = bfd_getl16 (blen);
977 if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
979 /* Discard record size and align byte. */
981 vec->rec_rem = vec->rec_len;
985 /* Prepend record size. */
986 vec->rec_pos = REC_POS_LEN0;
987 vec->rec_rem = (vec->rec_len + 1) & ~1; /* With align byte. */
989 if (vec->rec_len == 3)
991 /* Possibly end of file. Check the pattern. */
992 if (vms_lib_bread_raw (abfd, vec->pattern, 4) != 4)
994 if (!memcmp (vec->pattern, eotdesc + 2, 3))
996 /* This is really an EOF. */
998 vec->file_len = vec->where;
1003 if (vec->dcxsbms != NULL)
1005 /* This is a compressed member. */
1009 /* Be sure there is enough room for the expansion. */
1010 len = (vec->rec_len + 1) & ~1;
1011 if (len > vec->dcx_max)
1013 while (len > vec->dcx_max)
1015 vec->dcx_buf = bfd_alloc (abfd, vec->dcx_max);
1016 if (vec->dcx_buf == NULL)
1020 /* Read the compressed record. */
1021 vec->dcx_rlen = len;
1022 if (vec->rec_len == 3)
1025 memcpy (vec->dcx_buf, vec->pattern, 3);
1029 elen = vms_lib_bread_raw (abfd, vec->dcx_buf, len);
1034 /* Dummy expansion to get the expanded length. */
1035 vec->dcx_offset = 0;
1036 vec->dcx_sbm = vec->dcxsbms;
1038 elen = vms_lib_dcx (vec, NULL, 0x10000);
1041 vec->rec_len = elen;
1042 vec->rec_rem = elen;
1044 /* Reset the state. */
1045 vec->dcx_offset = 0;
1046 vec->dcx_sbm = vec->dcxsbms;
1050 if (vec->rec_pos < 0)
1053 switch (vec->rec_pos)
1056 c = vec->rec_len & 0xff;
1057 vec->rec_pos = REC_POS_LEN1;
1060 c = (vec->rec_len >> 8) & 0xff;
1084 if (nbytes > vec->rec_rem)
1085 chunk = vec->rec_rem;
1089 if (vec->dcxsbms != NULL)
1091 /* Optimize the stat() case: no need to decompress again as we
1093 if (!(buf == NULL && chunk == vec->rec_rem))
1094 chunk = vms_lib_dcx (vec, buf, chunk);
1098 if (vec->rec_len == 3)
1101 memcpy (buf, vec->pattern + vec->rec_pos, chunk);
1104 chunk = vms_lib_bread_raw (abfd, buf, chunk);
1112 vec->rec_pos += chunk;
1113 vec->rec_rem -= chunk;
1115 if (vec->rec_rem == 0)
1117 /* End of record reached. */
1118 if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
1120 if ((vec->rec_len & 1) == 1
1121 && vec->rec_len != 3
1122 && vec->dcxsbms == NULL)
1124 /* Eat the pad byte. */
1126 if (vms_lib_bread_raw (abfd, &pad, 1) != 1)
1129 vec->rec_pos = REC_POS_NL;
1134 if ((vec->rec_len & 1) == 1 && vec->dcxsbms != NULL)
1136 vec->rec_pos = REC_POS_PAD;
1146 /* Standard function, but we currently only handle the rewind case. */
1149 vms_lib_bseek (struct bfd *abfd, file_ptr offset, int whence)
1151 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1153 if (whence == SEEK_SET && offset == 0)
1158 vec->blk_off = vec->init_blk_off;
1159 vec->next_block = vec->init_next_block;
1161 if (bfd_seek (abfd->my_archive, vec->first_block, SEEK_SET) != 0)
1170 vms_lib_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED,
1171 const void *where ATTRIBUTE_UNUSED,
1172 file_ptr nbytes ATTRIBUTE_UNUSED)
1178 vms_lib_bclose (struct bfd *abfd)
1180 abfd->iostream = NULL;
1185 vms_lib_bflush (struct bfd *abfd ATTRIBUTE_UNUSED)
1191 vms_lib_bstat (struct bfd *abfd ATTRIBUTE_UNUSED,
1192 struct stat *sb ATTRIBUTE_UNUSED)
1194 /* Not supported. */
1199 vms_lib_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
1200 void *addr ATTRIBUTE_UNUSED,
1201 bfd_size_type len ATTRIBUTE_UNUSED,
1202 int prot ATTRIBUTE_UNUSED,
1203 int flags ATTRIBUTE_UNUSED,
1204 file_ptr offset ATTRIBUTE_UNUSED,
1205 void **map_addr ATTRIBUTE_UNUSED,
1206 bfd_size_type *map_len ATTRIBUTE_UNUSED)
1211 static const struct bfd_iovec vms_lib_iovec = {
1212 &vms_lib_bread, &vms_lib_bwrite, &vms_lib_btell, &vms_lib_bseek,
1213 &vms_lib_bclose, &vms_lib_bflush, &vms_lib_bstat, &vms_lib_bmmap
1216 /* Open a library module. FILEPOS is the position of the module header. */
1219 vms_lib_bopen (bfd *el, file_ptr filepos)
1221 struct vms_lib_iovec *vec;
1222 unsigned char buf[256];
1223 struct vms_mhd *mhd;
1224 struct lib_tdata *tdata = bfd_libdata (el->my_archive);
1227 /* Allocate and initialized the iovec. */
1228 vec = bfd_zalloc (el, sizeof (*vec));
1233 el->iovec = &vms_lib_iovec;
1235 /* File length is not known. */
1238 /* Read the first data block. */
1239 vec->next_block = filepos & ~(VMS_BLOCK_SIZE - 1);
1240 vec->blk_off = DATA__LENGTH;
1241 if (!vms_lib_read_block (el))
1244 /* Prepare to read the first record. */
1245 vec->blk_off = filepos & (VMS_BLOCK_SIZE - 1);
1247 if (bfd_seek (el->my_archive, filepos, SEEK_SET) != 0)
1250 /* Read Record length + MHD + align byte. */
1251 len = tdata->mhd_size;
1252 if (vms_lib_bread_raw (el, buf, 2) != 2)
1254 if (bfd_getl16 (buf) != len)
1256 len = (len + 1) & ~1;
1257 BFD_ASSERT (len <= sizeof (buf));
1258 if (vms_lib_bread_raw (el, buf, len) != len)
1261 /* Get info from mhd. */
1262 mhd = (struct vms_mhd *)buf;
1264 if (mhd->id != MHD__C_MHDID)
1266 if (len >= MHD__C_MHDLEN + 1)
1267 el->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
1268 el->mtime = vms_rawtime_to_time_t (mhd->datim);
1269 el->mtime_set = TRUE;
1271 /* Reinit the iovec so that seek() will point to the first record after
1274 vec->init_blk_off = vec->blk_off;
1275 vec->init_next_block = vec->next_block;
1276 vec->first_block = bfd_tell (el->my_archive);
1277 vec->dcxsbms = bfd_libdata (el->my_archive)->dcxsbm;
1279 if (vec->dcxsbms != NULL)
1282 vec->dcx_max = 10 * 1024;
1283 vec->dcx_buf = bfd_alloc (el, vec->dcx_max);
1285 if (vec->dcx_buf == NULL)
1291 /* Get member MODIDX. Return NULL in case of error. */
1294 _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
1296 struct lib_tdata *tdata = bfd_libdata (abfd);
1302 if (modidx >= tdata->nbr_modules)
1305 /* Already loaded. */
1306 if (tdata->cache[modidx])
1307 return tdata->cache[modidx];
1310 file_off = tdata->modules[modidx].file_offset;
1311 if (tdata->type != LBR__C_TYP_IOBJ)
1313 res = _bfd_create_empty_archive_element_shell (abfd);
1317 /* Special reader to deal with data blocks. */
1318 if (!vms_lib_bopen (res, file_off))
1324 struct vms_mhd *mhd;
1325 struct areltdata *arelt;
1327 /* Sanity check. The MHD must be big enough to contain module size. */
1328 if (tdata->mhd_size < offsetof (struct vms_mhd, modsize) + 4)
1331 /* Read the MHD now. */
1332 if (bfd_seek (abfd, file_off, SEEK_SET) != 0)
1334 if (bfd_bread (buf, tdata->mhd_size, abfd) != tdata->mhd_size)
1337 res = _bfd_create_empty_archive_element_shell (abfd);
1340 arelt = bfd_zmalloc (sizeof (*arelt));
1343 res->arelt_data = arelt;
1345 /* Get info from mhd. */
1346 mhd = (struct vms_mhd *)buf;
1347 if (mhd->id != MHD__C_MHDID)
1349 if (tdata->mhd_size >= offsetof (struct vms_mhd, objstat) + 1)
1350 res->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
1351 res->mtime = vms_rawtime_to_time_t (mhd->datim);
1352 res->mtime_set = TRUE;
1354 arelt->parsed_size = bfd_getl32 (mhd->modsize);
1356 /* No need for a special reader as members are stored linearly.
1357 Just skip the MHD. */
1358 res->origin = file_off + tdata->mhd_size;
1362 name = tdata->modules[modidx].name;
1363 switch (tdata->type)
1365 case LBR__C_TYP_IOBJ:
1366 case LBR__C_TYP_EOBJ:
1367 /* For object archives, append .obj to mimic standard behaviour. */
1369 size_t namelen = strlen (name);
1370 char *name1 = bfd_alloc (res, namelen + 4 + 1);
1371 memcpy (name1, name, namelen);
1372 strcpy (name1 + namelen, ".obj");
1379 res->filename = name;
1381 tdata->cache[modidx] = res;
1386 /* Standard function: get member at IDX. */
1389 _bfd_vms_lib_get_elt_at_index (bfd *abfd, symindex symidx)
1391 struct lib_tdata *tdata = bfd_libdata (abfd);
1393 unsigned int modidx;
1396 if (symidx > tdata->artdata.symdef_count)
1398 file_off = tdata->artdata.symdefs[symidx].file_offset;
1401 for (modidx = 0; modidx < tdata->nbr_modules; modidx++)
1403 if (tdata->modules[modidx].file_offset == file_off)
1406 if (modidx >= tdata->nbr_modules)
1409 return _bfd_vms_lib_get_module (abfd, modidx);
1412 /* Elements of an imagelib are stubs. You can get the real image with this
1416 _bfd_vms_lib_get_imagelib_file (bfd *el)
1418 bfd *archive = el->my_archive;
1419 const char *modname = el->filename;
1420 int modlen = strlen (modname);
1425 /* Convert module name to lower case and append '.exe'. */
1426 filename = bfd_alloc (el, modlen + 5);
1427 if (filename == NULL)
1429 for (j = 0; j < modlen; j++)
1430 if (ISALPHA (modname[j]))
1431 filename[j] = TOLOWER (modname[j]);
1433 filename[j] = modname[j];
1434 memcpy (filename + modlen, ".exe", 5);
1436 filename = _bfd_append_relative_path (archive, filename);
1437 if (filename == NULL)
1439 res = bfd_openr (filename, NULL);
1443 (*_bfd_error_handler)(_("could not open shared image '%s' from '%s'"),
1444 filename, archive->filename);
1445 bfd_release (archive, filename);
1449 /* FIXME: put it in a cache ? */
1453 /* Standard function. */
1456 _bfd_vms_lib_openr_next_archived_file (bfd *archive,
1465 idx = last_file->proxy_origin + 1;
1467 if (idx >= bfd_libdata (archive)->nbr_modules)
1469 bfd_set_error (bfd_error_no_more_archived_files);
1473 res = _bfd_vms_lib_get_module (archive, idx);
1476 res->proxy_origin = idx;
1480 /* Standard function. Just compute the length. */
1483 _bfd_vms_lib_generic_stat_arch_elt (bfd *abfd, struct stat *st)
1485 struct lib_tdata *tdata;
1488 if (abfd->my_archive == NULL)
1490 bfd_set_error (bfd_error_invalid_operation);
1494 tdata = bfd_libdata (abfd->my_archive);
1495 if (tdata->type != LBR__C_TYP_IOBJ)
1497 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1499 if (vec->file_len == (ufile_ptr)-1)
1501 if (vms_lib_bseek (abfd, 0, SEEK_SET) != 0)
1504 /* Compute length. */
1505 while (vms_lib_bread (abfd, NULL, 1 << 20) > 0)
1508 st->st_size = vec->file_len;
1512 st->st_size = ((struct areltdata *)abfd->arelt_data)->parsed_size;
1515 if (abfd->mtime_set)
1516 st->st_mtime = abfd->mtime;
1526 /* Internal representation of an index entry. */
1530 /* Corresponding archive member. */
1533 /* Number of reference to this entry. */
1536 /* Length of the key. */
1537 unsigned short namlen;
1543 /* Used to sort index entries. */
1546 lib_index_cmp (const void *lv, const void *rv)
1548 const struct lib_index *l = lv;
1549 const struct lib_index *r = rv;
1551 return strcmp (l->name, r->name);
1554 /* Maximum number of index blocks level. */
1556 #define MAX_LEVEL 10
1558 /* Get the size of an index entry. */
1561 get_idxlen (struct lib_index *idx, bfd_boolean is_elfidx)
1565 /* 9 is the size of struct vms_elfidx without keyname. */
1566 if (idx->namlen > MAX_KEYLEN)
1567 return 9 + sizeof (struct vms_kbn);
1569 return 9 + idx->namlen;
1573 /* 7 is the size of struct vms_idx without keyname. */
1574 return 7 + idx->namlen;
1578 /* Write the index composed by NBR symbols contained in IDX.
1579 VBN is the first vbn to be used, and will contain on return the last vbn.
1580 Can be called with ABFD set to NULL just to size the index.
1581 If not null, TOPVBN will be assigned to the vbn of the root index tree.
1582 IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
1583 Return TRUE on success. */
1586 vms_write_index (bfd *abfd,
1587 struct lib_index *idx, unsigned int nbr, unsigned int *vbn,
1588 unsigned int *topvbn, bfd_boolean is_elfidx)
1590 /* The index is organized as a tree. This function implements a naive
1591 algorithm to balance the tree: it fills the leaves, and create a new
1592 branch when all upper leaves and branches are full. We only keep in
1593 memory a path to the current leaf. */
1597 /* Disk blocks for the current path. */
1598 struct vms_indexdef *rblk[MAX_LEVEL];
1599 /* Info on the current blocks. */
1602 unsigned int vbn; /* VBN of the block. */
1603 /* The last entry is identified so that it could be copied to the
1605 unsigned short len; /* Length up to the last entry. */
1606 unsigned short lastlen; /* Length of the last entry. */
1609 /* The kbn blocks are used to store long symbol names. */
1610 unsigned int kbn_sz = 0; /* Number of bytes available in the kbn block. */
1611 unsigned int kbn_vbn = 0; /* VBN of the kbn block. */
1612 unsigned char *kbn_blk = NULL; /* Contents of the kbn block. */
1616 /* No entries. Very easy to handle. */
1624 /* Sort the index the first time this function is called. */
1625 qsort (idx, nbr, sizeof (struct lib_index), lib_index_cmp);
1628 /* Allocate first index block. */
1631 rblk[0] = bfd_zmalloc (sizeof (struct vms_indexdef));
1632 blk[0].vbn = (*vbn)++;
1636 for (i = 0; i < nbr; i++, idx++)
1638 unsigned int idxlen;
1640 unsigned int key_vbn = 0;
1641 unsigned int key_off = 0;
1643 idxlen = get_idxlen (idx, is_elfidx);
1645 if (is_elfidx && idx->namlen > MAX_KEYLEN)
1647 /* If the key (ie name) is too long, write it in the kbn block. */
1648 unsigned int kl = idx->namlen;
1649 unsigned int kl_chunk;
1650 const char *key = idx->name;
1652 /* Write the key in the kbn, chunk after chunk. */
1655 if (kbn_sz < sizeof (struct vms_kbn))
1657 /* Not enough room in the kbn block. */
1660 /* Write it to the disk (if there is one). */
1663 if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
1668 kbn_blk = bfd_malloc (VMS_BLOCK_SIZE);
1669 if (kbn_blk == NULL)
1672 *(unsigned short *)kbn_blk = 0;
1674 /* Allocate a new block for the keys. */
1676 kbn_sz = VMS_BLOCK_SIZE - 2;
1678 /* Size of the chunk written to the current key block. */
1679 if (kl + sizeof (struct vms_kbn) > kbn_sz)
1680 kl_chunk = kbn_sz - sizeof (struct vms_kbn);
1684 if (kbn_blk != NULL)
1686 struct vms_kbn *kbn;
1688 kbn = (struct vms_kbn *)(kbn_blk + VMS_BLOCK_SIZE - kbn_sz);
1692 /* Save the rfa of the first chunk. */
1694 key_off = VMS_BLOCK_SIZE - kbn_sz;
1697 bfd_putl16 (kl_chunk, kbn->keylen);
1700 /* No next chunk. */
1701 bfd_putl32 (0, kbn->rfa.vbn);
1702 bfd_putl16 (0, kbn->rfa.offset);
1706 /* Next chunk will be at the start of the next block. */
1707 bfd_putl32 (*vbn, kbn->rfa.vbn);
1708 bfd_putl16 (2, kbn->rfa.offset);
1710 memcpy ((char *)(kbn + 1), key, kl_chunk);
1714 kl_chunk = (kl_chunk + 1) & ~1; /* Always align. */
1715 kbn_sz -= kl_chunk + sizeof (struct vms_kbn);
1720 /* Check if a block might overflow. In this case we will flush this
1721 block and all the blocks below it. */
1722 for (j = 0; j < level; j++)
1723 if (blk[j].len + blk[j].lastlen + idxlen > INDEXDEF__BLKSIZ)
1726 for (j = 0; j < level; j++)
1730 /* There is not enough room to write the new entry in this
1731 block or in a parent block. */
1735 BFD_ASSERT (level < MAX_LEVEL);
1737 /* Need to create a parent. */
1740 rblk[level] = bfd_zmalloc (sizeof (struct vms_indexdef));
1741 bfd_putl32 (*vbn, rblk[j]->parent);
1743 blk[level].vbn = (*vbn)++;
1745 blk[level].lastlen = blk[j].lastlen;
1750 /* Update parent block: write the last entry from the current
1754 struct vms_rfa *rfa;
1756 /* Pointer to the last entry in parent block. */
1757 rfa = (struct vms_rfa *)(rblk[j + 1]->keys + blk[j + 1].len);
1759 /* Copy the whole entry. */
1760 BFD_ASSERT (blk[j + 1].lastlen == blk[j].lastlen);
1761 memcpy (rfa, rblk[j]->keys + blk[j].len, blk[j].lastlen);
1762 /* Fix the entry (which in always the first field of an
1764 bfd_putl32 (blk[j].vbn, rfa->vbn);
1765 bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
1770 /* And allocate it. Do it only on the block that won't be
1771 flushed (so that the parent of the parent can be
1773 blk[j + 1].len += blk[j + 1].lastlen;
1774 blk[j + 1].lastlen = 0;
1777 /* Write this block on the disk. */
1780 bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
1781 if (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
1785 /* Reset this block. */
1788 blk[j].vbn = (*vbn)++;
1791 /* Append it to the block. */
1794 /* Keep the previous last entry. */
1795 blk[j].len += blk[j].lastlen;
1799 struct vms_rfa *rfa;
1801 rfa = (struct vms_rfa *)(rblk[j]->keys + blk[j].len);
1802 bfd_putl32 ((idx->abfd->proxy_origin / VMS_BLOCK_SIZE) + 1,
1805 ((idx->abfd->proxy_origin % VMS_BLOCK_SIZE)
1806 + (is_elfidx ? 0 : DATA__DATA),
1811 /* Use elfidx format. */
1812 struct vms_elfidx *en = (struct vms_elfidx *)rfa;
1817 /* Long symbol name. */
1818 struct vms_kbn *k = (struct vms_kbn *)(en->keyname);
1819 bfd_putl16 (sizeof (struct vms_kbn), en->keylen);
1820 bfd_putl16 (idx->namlen, k->keylen);
1821 bfd_putl32 (key_vbn, k->rfa.vbn);
1822 bfd_putl16 (key_off, k->rfa.offset);
1823 en->flags |= ELFIDX__SYMESC;
1827 bfd_putl16 (idx->namlen, en->keylen);
1828 memcpy (en->keyname, idx->name, idx->namlen);
1833 /* Use idx format. */
1834 struct vms_idx *en = (struct vms_idx *)rfa;
1835 en->keylen = idx->namlen;
1836 memcpy (en->keyname, idx->name, idx->namlen);
1840 /* The last added key can now be the last one all blocks in the
1842 blk[j].lastlen = idxlen;
1846 /* Save VBN of the root. */
1848 *topvbn = blk[level - 1].vbn;
1854 for (j = 1; j < level; j++)
1856 /* Update parent block: write the new entry. */
1859 struct vms_rfa *rfa;
1861 en = rblk[j - 1]->keys + blk[j - 1].len;
1862 par = rblk[j]->keys + blk[j].len;
1863 BFD_ASSERT (blk[j].lastlen == blk[j - 1].lastlen);
1864 memcpy (par, en, blk[j - 1].lastlen);
1865 rfa = (struct vms_rfa *)par;
1866 bfd_putl32 (blk[j - 1].vbn, rfa->vbn);
1867 bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
1870 for (j = 0; j < level; j++)
1872 /* Write this block on the disk. */
1873 bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
1874 if (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
1880 /* Write the last kbn (if any). */
1883 if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
1891 /* Append data to the data block DATA. Force write if PAD is true. */
1894 vms_write_data_block (bfd *arch, struct vms_datadef *data, file_ptr *off,
1895 const unsigned char *buf, unsigned int len, int pad)
1897 while (len > 0 || pad)
1899 unsigned int doff = *off & (VMS_BLOCK_SIZE - 1);
1900 unsigned int remlen = (DATA__LENGTH - DATA__DATA) - doff;
1903 l = (len > remlen) ? remlen : len;
1904 memcpy (data->data + doff, buf, l);
1910 if (doff == (DATA__LENGTH - DATA__DATA) || (len == 0 && pad))
1914 bfd_putl32 ((*off / VMS_BLOCK_SIZE) + 2, data->link);
1916 if (bfd_bwrite (data, sizeof (*data), arch) != sizeof (*data))
1919 *off += DATA__LENGTH - doff;
1928 /* Build the symbols index. */
1931 _bfd_vms_lib_build_map (unsigned int nbr_modules,
1932 struct lib_index *modules,
1933 unsigned int *res_cnt,
1934 struct lib_index **res)
1937 asymbol **syms = NULL;
1939 struct lib_index *map = NULL;
1940 unsigned int map_max = 1024; /* Fine initial default. */
1941 unsigned int map_count = 0;
1943 map = (struct lib_index *) bfd_malloc (map_max * sizeof (struct lib_index));
1947 /* Gather symbols. */
1948 for (i = 0; i < nbr_modules; i++)
1953 bfd *current = modules[i].abfd;
1955 if ((bfd_get_file_flags (current) & HAS_SYMS) == 0)
1958 storage = bfd_get_symtab_upper_bound (current);
1964 if (storage > syms_max)
1969 syms = (asymbol **) bfd_malloc (syms_max);
1973 symcount = bfd_canonicalize_symtab (current, syms);
1977 /* Now map over all the symbols, picking out the ones we
1979 for (src_count = 0; src_count < symcount; src_count++)
1981 flagword flags = (syms[src_count])->flags;
1982 asection *sec = syms[src_count]->section;
1984 if ((flags & BSF_GLOBAL
1986 || flags & BSF_INDIRECT
1987 || bfd_is_com_section (sec))
1988 && ! bfd_is_und_section (sec))
1990 struct lib_index *new_map;
1992 /* This symbol will go into the archive header. */
1993 if (map_count == map_max)
1996 new_map = (struct lib_index *)
1997 bfd_realloc (map, map_max * sizeof (struct lib_index));
1998 if (new_map == NULL)
2003 map[map_count].abfd = current;
2004 map[map_count].namlen = strlen (syms[src_count]->name);
2005 map[map_count].name = syms[src_count]->name;
2013 *res_cnt = map_count;
2025 /* Do the hard work: write an archive on the disk. */
2028 _bfd_vms_lib_write_archive_contents (bfd *arch)
2031 unsigned int nbr_modules;
2032 struct lib_index *modules;
2033 unsigned int nbr_symbols;
2034 struct lib_index *symbols;
2035 struct lib_tdata *tdata = bfd_libdata (arch);
2038 unsigned int nbr_mod_iblk;
2039 unsigned int nbr_sym_iblk;
2041 unsigned int mod_idx_vbn;
2042 unsigned int sym_idx_vbn;
2043 bfd_boolean is_elfidx = tdata->kind == vms_lib_ia64;
2044 unsigned int max_keylen = is_elfidx ? MAX_EKEYLEN : MAX_KEYLEN;
2046 /* Count the number of modules (and do a first sanity check). */
2048 for (current = arch->archive_head;
2050 current = current->archive_next)
2052 /* This check is checking the bfds for the objects we're reading
2053 from (which are usually either an object file or archive on
2054 disk), not the archive entries we're writing to. We don't
2055 actually create bfds for the archive members, we just copy
2056 them byte-wise when we write out the archive. */
2057 if (bfd_write_p (current) || !bfd_check_format (current, bfd_object))
2059 bfd_set_error (bfd_error_invalid_operation);
2066 /* Build the modules list. */
2067 BFD_ASSERT (tdata->modules == NULL);
2068 modules = bfd_alloc (arch, nbr_modules * sizeof (struct lib_index));
2069 if (modules == NULL)
2072 for (current = arch->archive_head, i = 0;
2074 current = current->archive_next, i++)
2078 modules[i].abfd = current;
2079 modules[i].name = vms_get_module_name (current->filename, FALSE);
2082 /* FIXME: silently truncate long names ? */
2083 nl = strlen (modules[i].name);
2084 modules[i].namlen = (nl > max_keylen ? max_keylen : nl);
2087 /* Create the module index. */
2089 if (!vms_write_index (NULL, modules, nbr_modules, &vbn, NULL, is_elfidx))
2093 /* Create symbol index. */
2094 if (!_bfd_vms_lib_build_map (nbr_modules, modules, &nbr_symbols, &symbols))
2098 if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx))
2102 /* Write modules and remember their position. */
2103 off = (1 + nbr_mod_iblk + nbr_sym_iblk) * VMS_BLOCK_SIZE;
2105 if (bfd_seek (arch, off, SEEK_SET) != 0)
2108 for (i = 0; i < nbr_modules; i++)
2110 struct vms_datadef data;
2111 unsigned char blk[VMS_BLOCK_SIZE];
2112 struct vms_mhd *mhd;
2115 current = modules[i].abfd;
2116 current->proxy_origin = off;
2122 /* Write the MHD as a record (ie, size first). */
2124 bfd_putl16 (tdata->mhd_size, blk);
2126 mhd = (struct vms_mhd *)(blk + sz);
2127 memset (mhd, 0, sizeof (struct vms_mhd));
2129 mhd->id = MHD__C_MHDID;
2131 memcpy (mhd->objid, "V1.0", 4);
2132 bfd_putl32 (modules[i].ref, mhd->refcnt);
2135 sz += tdata->mhd_size;
2138 /* Rewind the member to be put into the archive. */
2139 if (bfd_seek (current, 0, SEEK_SET) != 0)
2142 /* Copy the member into the archive. */
2145 unsigned int modsize = 0;
2147 file_ptr off_hdr = off;
2149 /* Read to complete the first block. */
2150 amt = bfd_bread (blk + sz, VMS_BLOCK_SIZE - sz, current);
2151 if (amt == (bfd_size_type)-1)
2154 if (amt < VMS_BLOCK_SIZE - sz)
2156 /* The member size is less than a block. Pad the block. */
2157 memset (blk + sz + amt, 0, VMS_BLOCK_SIZE - sz - amt);
2159 bfd_putl32 (modsize, mhd->modsize);
2161 /* Write the first block (which contains an mhd). */
2162 if (bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
2164 off += VMS_BLOCK_SIZE;
2166 if (amt == VMS_BLOCK_SIZE - sz)
2168 /* Copy the remaining. */
2169 char buffer[DEFAULT_BUFFERSIZE];
2173 amt = bfd_bread (buffer, sizeof (buffer), current);
2174 if (amt == (bfd_size_type)-1)
2179 if (amt != sizeof (buffer))
2181 /* Clear the padding. */
2182 memset (buffer + amt, 0, sizeof (buffer) - amt);
2183 amt = (amt + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
2185 if (bfd_bwrite (buffer, amt, arch) != amt)
2190 /* Now that the size is known, write the first block (again). */
2191 bfd_putl32 (modsize, mhd->modsize);
2192 if (bfd_seek (arch, off_hdr, SEEK_SET) != 0
2193 || bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
2195 if (bfd_seek (arch, off, SEEK_SET) != 0)
2201 /* Write the MHD. */
2202 if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
2205 /* Write the member. */
2208 sz = bfd_bread (blk, sizeof (blk), current);
2211 if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
2215 /* Write the end of module marker. */
2216 if (vms_write_data_block (arch, &data, &off,
2217 eotdesc, sizeof (eotdesc), 1) < 0)
2222 /* Write the indexes. */
2224 if (vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn,
2227 if (vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn,
2231 /* Write libary header. */
2233 unsigned char blk[VMS_BLOCK_SIZE];
2234 struct vms_lhd *lhd = (struct vms_lhd *)blk;
2235 struct vms_idd *idd = (struct vms_idd *)(blk + sizeof (*lhd));
2236 unsigned int idd_flags;
2237 unsigned int saneid;
2239 memset (blk, 0, sizeof (blk));
2241 lhd->type = tdata->type;
2243 switch (tdata->kind)
2246 saneid = LHD_SANEID3;
2249 saneid = LHD_SANEID6;
2254 bfd_putl32 (saneid, lhd->sanity);
2255 bfd_putl16 (tdata->ver, lhd->majorid);
2256 bfd_putl16 (0, lhd->minorid);
2257 snprintf ((char *)lhd->lbrver + 1, sizeof (lhd->lbrver) - 1,
2259 (unsigned)(BFD_VERSION / 100000000UL),
2260 (unsigned)(BFD_VERSION / 1000000UL) % 100,
2261 (unsigned)(BFD_VERSION / 10000UL) % 100);
2262 lhd->lbrver[sizeof (lhd->lbrver) - 1] = 0;
2263 lhd->lbrver[0] = strlen ((char *)lhd->lbrver + 1);
2265 bfd_putl32 (tdata->credat_lo, lhd->credat + 0);
2266 bfd_putl32 (tdata->credat_hi, lhd->credat + 4);
2267 vms_raw_get_time (lhd->updtim);
2269 lhd->mhdusz = tdata->mhd_size - MHD__C_USRDAT;
2271 bfd_putl32 (nbr_modules + nbr_symbols, lhd->idxcnt);
2272 bfd_putl32 (nbr_modules, lhd->modcnt);
2273 bfd_putl32 (nbr_modules, lhd->modhdrs);
2275 /* Number of blocks for index. */
2276 bfd_putl32 (nbr_mod_iblk + nbr_sym_iblk, lhd->idxblks);
2277 bfd_putl32 (vbn - 1, lhd->hipreal);
2278 bfd_putl32 (vbn - 1, lhd->hiprusd);
2280 /* VBN of the next free block. */
2281 bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextvbn);
2282 bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextrfa + 0);
2283 bfd_putl16 (0, lhd->nextrfa + 4);
2285 /* First index (modules name). */
2286 idd_flags = IDD__FLAGS_ASCII | IDD__FLAGS_VARLENIDX
2287 | IDD__FLAGS_NOCASECMP | IDD__FLAGS_NOCASENTR;
2288 bfd_putl16 (idd_flags, idd->flags);
2289 bfd_putl16 (max_keylen + 1, idd->keylen);
2290 bfd_putl16 (mod_idx_vbn, idd->vbn);
2293 /* Second index (symbols name). */
2294 bfd_putl16 (idd_flags, idd->flags);
2295 bfd_putl16 (max_keylen + 1, idd->keylen);
2296 bfd_putl16 (sym_idx_vbn, idd->vbn);
2299 if (vms_write_block (arch, 1, blk) != TRUE)
2306 bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
2310 /* Add a target for text library. This costs almost nothing and is useful to
2311 read VMS library on the host. */
2313 const bfd_target vms_lib_txt_vec =
2315 "vms-libtxt", /* Name. */
2316 bfd_target_unknown_flavour,
2317 BFD_ENDIAN_UNKNOWN, /* byteorder */
2318 BFD_ENDIAN_UNKNOWN, /* header_byteorder */
2319 0, /* Object flags. */
2320 0, /* Sect flags. */
2321 0, /* symbol_leading_char. */
2322 ' ', /* ar_pad_char. */
2323 15, /* ar_max_namelen. */
2324 0, /* match priority. */
2325 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2326 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2327 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
2328 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2329 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2330 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
2332 {_bfd_dummy_target, _bfd_dummy_target, /* bfd_check_format. */
2333 _bfd_vms_lib_txt_archive_p, _bfd_dummy_target},
2334 {bfd_false, bfd_false, bfd_false, bfd_false}, /* bfd_set_format. */
2335 {bfd_false, bfd_false, bfd_false, bfd_false}, /* bfd_write_contents. */
2337 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
2338 BFD_JUMP_TABLE_COPY (_bfd_generic),
2339 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2340 BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib),
2341 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
2342 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
2343 BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
2344 BFD_JUMP_TABLE_LINK (_bfd_nolink),
2345 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),