1 /* BFD back-end for VMS archive files.
3 Copyright (C) 2010-2015 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 /* If true, the table was reallocated on the heap. If false, it is still
124 in the BFD's objalloc. */
125 bfd_boolean realloced;
128 /* Simply add a name to the index. */
131 vms_add_index (struct carsym_mem *cs, char *name,
132 unsigned int idx_vbn, unsigned int idx_off)
134 if (cs->nbr == cs->max)
138 cs->max = 2 * cs->max + 32;
142 n = bfd_malloc2 (cs->max, sizeof (struct carsym));
145 memcpy (n, cs->idx, cs->nbr * sizeof (struct carsym));
146 /* And unfortunately we can't free cs->idx. */
150 n = bfd_realloc_or_free (cs->idx, cs->nbr * sizeof (struct carsym));
155 cs->realloced = TRUE;
157 cs->idx[cs->nbr].file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
158 cs->idx[cs->nbr].name = name;
163 /* Follow all member of a lns list (pointed by RFA) and add indexes for
164 NAME. Return FALSE in case of error. */
167 vms_add_indexes_from_list (bfd *abfd, struct carsym_mem *cs, char *name,
176 vbn = bfd_getl32 (rfa->vbn);
181 off = (vbn - 1) * VMS_BLOCK_SIZE + bfd_getl16 (rfa->offset);
182 if (bfd_seek (abfd, off, SEEK_SET) != 0
183 || bfd_bread (&lns, sizeof (lns), abfd) != sizeof (lns))
186 if (!vms_add_index (cs, name,
187 bfd_getl32 (lns.modrfa.vbn),
188 bfd_getl16 (lns.modrfa.offset)))
195 /* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
198 vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
202 off = (vbn - 1) * VMS_BLOCK_SIZE;
203 if (bfd_seek (abfd, off, SEEK_SET) != 0
204 || bfd_bread (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
210 /* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
213 vms_write_block (bfd *abfd, unsigned int vbn, void *blk)
217 off = (vbn - 1) * VMS_BLOCK_SIZE;
218 if (bfd_seek (abfd, off, SEEK_SET) != 0
219 || bfd_bwrite (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
225 /* Read index block VBN and put the entry in **IDX (which is updated).
226 If the entry is indirect, recurse. */
229 vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs)
231 struct vms_indexdef indexdef;
236 /* Read the index block. */
237 BFD_ASSERT (sizeof (indexdef) == VMS_BLOCK_SIZE);
238 if (!vms_read_block (abfd, vbn, &indexdef))
242 p = &indexdef.keys[0];
243 endp = p + bfd_getl16 (indexdef.used);
246 unsigned int idx_vbn;
247 unsigned int idx_off;
249 unsigned char *keyname;
252 /* Extract key length. */
253 if (bfd_libdata (abfd)->ver == LBR_MAJORID)
255 struct vms_idx *ridx = (struct vms_idx *)p;
257 idx_vbn = bfd_getl32 (ridx->rfa.vbn);
258 idx_off = bfd_getl16 (ridx->rfa.offset);
260 keylen = ridx->keylen;
262 keyname = ridx->keyname;
264 else if (bfd_libdata (abfd)->ver == LBR_ELFMAJORID)
266 struct vms_elfidx *ridx = (struct vms_elfidx *)p;
268 idx_vbn = bfd_getl32 (ridx->rfa.vbn);
269 idx_off = bfd_getl16 (ridx->rfa.offset);
271 keylen = bfd_getl16 (ridx->keylen);
273 keyname = ridx->keyname;
282 /* Point to the next index entry. */
283 p = keyname + keylen;
285 if (idx_off == RFADEF__C_INDEX)
287 /* Indirect entry. Recurse. */
288 if (!vms_traverse_index (abfd, idx_vbn, cs))
293 /* Add a new entry. */
296 if (flags & ELFIDX__SYMESC)
298 /* Extended key name. */
299 unsigned int noff = 0;
303 unsigned char kblk[VMS_BLOCK_SIZE];
306 if (keylen != sizeof (struct vms_kbn))
309 kbn = (struct vms_kbn *)keyname;
310 keylen = bfd_getl16 (kbn->keylen);
312 name = bfd_alloc (abfd, keylen + 1);
315 kvbn = bfd_getl32 (kbn->rfa.vbn);
316 koff = bfd_getl16 (kbn->rfa.offset);
318 /* Read the key, chunk by chunk. */
323 if (!vms_read_block (abfd, kvbn, kblk))
325 kbn = (struct vms_kbn *)(kblk + koff);
326 klen = bfd_getl16 (kbn->keylen);
327 kvbn = bfd_getl32 (kbn->rfa.vbn);
328 koff = bfd_getl16 (kbn->rfa.offset);
330 memcpy (name + noff, kbn + 1, klen);
341 /* Usual key name. */
342 name = bfd_alloc (abfd, keylen + 1);
346 memcpy (name, keyname, keylen);
350 if (flags & ELFIDX__LISTRFA)
355 off = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
356 if (bfd_seek (abfd, off, SEEK_SET) != 0
357 || bfd_bread (&lhs, sizeof (lhs), abfd) != sizeof (lhs))
360 /* FIXME: this adds extra entries that were not accounted. */
361 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_g_rfa))
363 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_wk_rfa))
365 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_g_rfa))
367 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_wk_rfa))
372 if (!vms_add_index (cs, name, idx_vbn, idx_off))
381 /* Read index #IDX, which must have NBREL entries. */
383 static struct carsym *
384 vms_lib_read_index (bfd *abfd, int idx, unsigned int *nbrel)
389 struct carsym *csbuf;
390 struct carsym_mem csm;
392 /* Read index desription. */
393 if (bfd_seek (abfd, LHD_IDXDESC + idx * IDD_LENGTH, SEEK_SET) != 0
394 || bfd_bread (&idd, sizeof (idd), abfd) != sizeof (idd))
398 flags = bfd_getl16 (idd.flags);
399 if (!(flags & IDD__FLAGS_ASCII)
400 || !(flags & IDD__FLAGS_VARLENIDX))
403 csbuf = bfd_alloc (abfd, *nbrel * sizeof (struct carsym));
409 csm.realloced = FALSE;
412 /* Note: if the index is empty, there is no block to traverse. */
413 vbn = bfd_getl32 (idd.vbn);
414 if (vbn != 0 && !vms_traverse_index (abfd, vbn, &csm))
416 if (csm.realloced && csm.idx != NULL)
419 /* Note: in case of error, we can free what was allocated on the
421 bfd_release (abfd, csbuf);
427 /* There are more entries than the first estimate. Allocate on
428 the BFD's objalloc. */
429 csbuf = bfd_alloc (abfd, csm.nbr * sizeof (struct carsym));
432 memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym));
439 /* Standard function. */
441 static const bfd_target *
442 _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
446 unsigned int majorid;
447 struct lib_tdata *tdata_hold;
448 struct lib_tdata *tdata;
450 unsigned int nbr_ent;
453 if (bfd_bread (&lhd, sizeof (lhd), abfd) != sizeof (lhd))
455 if (bfd_get_error () != bfd_error_system_call)
456 bfd_set_error (bfd_error_wrong_format);
460 /* Check sanity (= magic) number. */
461 sanity = bfd_getl32 (lhd.sanity);
462 if (!(sanity == LHD_SANEID3
463 || sanity == LHD_SANEID6
464 || sanity == LHD_SANEID_DCX))
466 bfd_set_error (bfd_error_wrong_format);
469 majorid = bfd_getl32 (lhd.majorid);
471 /* Check archive kind. */
475 if ((lhd.type != LBR__C_TYP_EOBJ && lhd.type != LBR__C_TYP_ESHSTB)
476 || majorid != LBR_MAJORID
479 bfd_set_error (bfd_error_wrong_format);
484 if ((lhd.type != LBR__C_TYP_IOBJ && lhd.type != LBR__C_TYP_ISHSTB)
485 || majorid != LBR_ELFMAJORID
488 bfd_set_error (bfd_error_wrong_format);
493 if ((lhd.type != LBR__C_TYP_TXT
494 && lhd.type != LBR__C_TYP_MLB
495 && lhd.type != LBR__C_TYP_HLP)
496 || majorid != LBR_MAJORID
499 bfd_set_error (bfd_error_wrong_format);
507 /* Allocate and initialize private data. */
508 tdata_hold = bfd_libdata (abfd);
509 tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
512 abfd->tdata.any = (void *)tdata;
513 tdata->ver = majorid;
514 tdata->mhd_size = MHD__C_USRDAT + lhd.mhdusz;
515 tdata->type = lhd.type;
517 tdata->credat_lo = bfd_getl32 (lhd.credat + 0);
518 tdata->credat_hi = bfd_getl32 (lhd.credat + 4);
521 tdata->nbr_modules = bfd_getl32 (lhd.modcnt);
522 tdata->artdata.symdef_count = bfd_getl32 (lhd.idxcnt) - tdata->nbr_modules;
523 nbr_ent = tdata->nbr_modules;
524 tdata->modules = vms_lib_read_index (abfd, 0, &nbr_ent);
525 if (tdata->modules == NULL || nbr_ent != tdata->nbr_modules)
529 nbr_ent = tdata->artdata.symdef_count;
530 tdata->artdata.symdefs = vms_lib_read_index (abfd, 1, &nbr_ent);
531 if (tdata->artdata.symdefs == NULL)
533 /* Only IA64 archives may have more entries in the index that what
535 if (nbr_ent != tdata->artdata.symdef_count
536 && kind != vms_lib_ia64)
538 tdata->artdata.symdef_count = nbr_ent;
540 tdata->cache = bfd_zalloc (abfd, sizeof (bfd *) * tdata->nbr_modules);
541 if (tdata->cache == NULL)
544 /* Read DCX submaps. */
545 dcxvbn = bfd_getl32 (lhd.dcxmapvbn);
548 unsigned char buf_reclen[4];
551 struct vms_dcxmap *map;
552 unsigned int sbm_off;
555 if (bfd_seek (abfd, (dcxvbn - 1) * VMS_BLOCK_SIZE, SEEK_SET) != 0
556 || bfd_bread (buf_reclen, sizeof (buf_reclen), abfd)
557 != sizeof (buf_reclen))
559 reclen = bfd_getl32 (buf_reclen);
560 buf = bfd_malloc (reclen);
563 if (bfd_bread (buf, reclen, abfd) != reclen)
568 map = (struct vms_dcxmap *)buf;
569 tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs);
570 sbm_off = bfd_getl16 (map->sub0);
571 tdata->dcxsbm = (struct dcxsbm_desc *)bfd_alloc
572 (abfd, tdata->nbr_dcxsbm * sizeof (struct dcxsbm_desc));
573 for (i = 0; i < tdata->nbr_dcxsbm; i++)
575 struct vms_dcxsbm *sbm = (struct vms_dcxsbm *) (buf + sbm_off);
576 struct dcxsbm_desc *sbmdesc = &tdata->dcxsbm[i];
577 unsigned int sbm_len;
580 unsigned char *data = (unsigned char *)sbm;
584 sbm_sz = bfd_getl16 (sbm->size);
586 BFD_ASSERT (sbm_off <= reclen);
588 sbmdesc->min_char = sbm->min_char;
589 BFD_ASSERT (sbmdesc->min_char == 0);
590 sbmdesc->max_char = sbm->max_char;
591 sbm_len = sbmdesc->max_char - sbmdesc->min_char + 1;
592 l = (2 * sbm_len + 7) / 8;
594 (sbm_sz >= sizeof (struct vms_dcxsbm) + l + 3 * sbm_len
595 || (tdata->nbr_dcxsbm == 1
596 && sbm_sz >= sizeof (struct vms_dcxsbm) + l + sbm_len));
597 sbmdesc->flags = (unsigned char *)bfd_alloc (abfd, l);
598 memcpy (sbmdesc->flags, data + bfd_getl16 (sbm->flags), l);
599 sbmdesc->nodes = (unsigned char *)bfd_alloc (abfd, 2 * sbm_len);
600 memcpy (sbmdesc->nodes, data + bfd_getl16 (sbm->nodes), 2 * sbm_len);
601 off = bfd_getl16 (sbm->next);
604 /* Read the 'next' array. */
605 sbmdesc->next = (unsigned short *)bfd_alloc
606 (abfd, sbm_len * sizeof (unsigned short));
608 for (j = 0; j < sbm_len; j++)
609 sbmdesc->next[j] = bfd_getl16 (buf1 + j * 2);
613 /* There is no next array if there is only one submap. */
614 BFD_ASSERT (tdata->nbr_dcxsbm == 1);
615 sbmdesc->next = NULL;
622 tdata->nbr_dcxsbm = 0;
625 /* The map is always present. Also mark shared image library. */
626 abfd->has_armap = TRUE;
627 if (tdata->type == LBR__C_TYP_ESHSTB || tdata->type == LBR__C_TYP_ISHSTB)
628 abfd->is_thin_archive = TRUE;
633 bfd_release (abfd, tdata);
634 abfd->tdata.any = (void *)tdata_hold;
638 /* Standard function for alpha libraries. */
641 _bfd_vms_lib_alpha_archive_p (bfd *abfd)
643 return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
646 /* Standard function for ia64 libraries. */
649 _bfd_vms_lib_ia64_archive_p (bfd *abfd)
651 return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
654 /* Standard function for text libraries. */
656 static const bfd_target *
657 _bfd_vms_lib_txt_archive_p (bfd *abfd)
659 return _bfd_vms_lib_archive_p (abfd, vms_lib_txt);
662 /* Standard bfd function. */
665 _bfd_vms_lib_mkarchive (bfd *abfd, enum vms_lib_kind kind)
667 struct lib_tdata *tdata;
669 tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
673 abfd->tdata.any = (void *)tdata;
674 vms_get_time (&tdata->credat_hi, &tdata->credat_lo);
680 tdata->ver = LBR_MAJORID;
681 tdata->mhd_size = offsetof (struct vms_mhd, pad1);
682 tdata->type = LBR__C_TYP_EOBJ;
685 tdata->ver = LBR_ELFMAJORID;
686 tdata->mhd_size = sizeof (struct vms_mhd);
687 tdata->type = LBR__C_TYP_IOBJ;
693 tdata->nbr_modules = 0;
694 tdata->artdata.symdef_count = 0;
695 tdata->modules = NULL;
696 tdata->artdata.symdefs = NULL;
703 _bfd_vms_lib_alpha_mkarchive (bfd *abfd)
705 return _bfd_vms_lib_mkarchive (abfd, vms_lib_alpha);
709 _bfd_vms_lib_ia64_mkarchive (bfd *abfd)
711 return _bfd_vms_lib_mkarchive (abfd, vms_lib_ia64);
714 /* Find NAME in the symbol index. Return the index. */
717 _bfd_vms_lib_find_symbol (bfd *abfd, const char *name)
719 struct lib_tdata *tdata = bfd_libdata (abfd);
720 carsym *syms = tdata->artdata.symdefs;
723 /* Open-coded binary search for speed. */
725 hi = tdata->artdata.symdef_count - 1;
729 int mid = lo + (hi - lo) / 2;
732 diff = (char)(name[0] - syms[mid].name[0]);
734 diff = strcmp (name, syms[mid].name);
742 return BFD_NO_MORE_SYMBOLS;
745 /* IO vector for archive member. Need that because members are not linearly
746 stored in archives. */
750 /* Current offset. */
753 /* Length of the module, when known. */
756 /* Current position in the record from bfd_bread point of view (ie, after
757 decompression). 0 means that no data byte have been read, -2 and -1
758 are reserved for the length word. */
760 #define REC_POS_NL -4
761 #define REC_POS_PAD -3
762 #define REC_POS_LEN0 -2
763 #define REC_POS_LEN1 -1
766 unsigned short rec_len;
767 /* Number of bytes to read in the current record. */
768 unsigned short rec_rem;
769 /* Offset of the next block. */
771 /* Current *data* offset in the data block. */
772 unsigned short blk_off;
774 /* Offset of the first block. Extracted from the index. */
775 file_ptr first_block;
777 /* Initial next_block. Extracted when the MHD is read. */
778 file_ptr init_next_block;
779 /* Initial blk_off, once the MHD is read. */
780 unsigned short init_blk_off;
782 /* Used to store any 3 byte record, which could be the EOF pattern. */
783 unsigned char pattern[4];
786 struct dcxsbm_desc *dcxsbms;
787 /* Current submap. */
788 struct dcxsbm_desc *dcx_sbm;
789 /* Current offset in the submap. */
790 unsigned int dcx_offset;
793 /* Compressed buffer. */
794 unsigned char *dcx_buf;
795 /* Size of the buffer. Used to resize. */
796 unsigned int dcx_max;
797 /* Number of valid bytes in the buffer. */
798 unsigned int dcx_rlen;
801 /* Return the current position. */
804 vms_lib_btell (struct bfd *abfd)
806 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
810 /* Read the header of the next data block if all bytes of the current block
814 vms_lib_read_block (struct bfd *abfd)
816 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
818 if (vec->blk_off == DATA__LENGTH)
820 unsigned char hdr[DATA__DATA];
822 /* Read next block. */
823 if (bfd_seek (abfd->my_archive, vec->next_block, SEEK_SET) != 0)
825 if (bfd_bread (hdr, sizeof (hdr), abfd->my_archive) != sizeof (hdr))
827 vec->next_block = (bfd_getl32 (hdr + 2) - 1) * VMS_BLOCK_SIZE;
828 vec->blk_off = sizeof (hdr);
833 /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
834 not stored. Read linearly from the library, but handle blocks. This
835 function does not handle records nor EOF. */
838 vms_lib_bread_raw (struct bfd *abfd, unsigned char *buf, file_ptr nbytes)
840 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
848 /* Be sure the current data block is read. */
849 if (!vms_lib_read_block (abfd))
852 /* Do not read past the data block, do not read more than requested. */
853 l = DATA__LENGTH - vec->blk_off;
860 /* Really read into BUF. */
861 if (bfd_bread (buf, l, abfd->my_archive) != l)
866 /* Make as if we are reading. */
867 if (bfd_seek (abfd->my_archive, l, SEEK_CUR) != 0)
880 /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
883 vms_lib_dcx (struct vms_lib_iovec *vec, unsigned char *buf, file_ptr nbytes)
885 struct dcxsbm_desc *sbm;
891 /* The loop below expect to deliver at least one byte. */
895 /* Get the current state. */
897 offset = vec->dcx_offset;
898 j = vec->dcx_pos & 7;
900 for (i = vec->dcx_pos >> 3; i < vec->dcx_rlen; i++)
902 unsigned char b = vec->dcx_buf[i];
908 if (!(sbm->flags[offset >> 3] & (1 << (offset & 7))))
910 unsigned int n_offset = sbm->nodes[offset];
913 /* End of buffer. Stay where we are. */
914 vec->dcx_pos = (i << 3) + j;
917 vec->dcx_offset = offset;
921 offset = 2 * n_offset;
925 unsigned char v = sbm->nodes[offset];
927 if (sbm->next != NULL)
928 sbm = vec->dcxsbms + sbm->next[v];
939 vec->dcx_pos = (i << 3) + j + 1;
940 vec->dcx_offset = offset;
953 /* Standard IOVEC function. */
956 vms_lib_bread (struct bfd *abfd, void *vbuf, file_ptr nbytes)
958 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
961 unsigned char *buf = (unsigned char *)vbuf;
963 /* Do not read past the end. */
964 if (vec->where >= vec->file_len)
970 if (vec->rec_rem == 0)
972 unsigned char blen[2];
974 /* Read record length. */
975 if (vms_lib_bread_raw (abfd, blen, sizeof (blen)) != sizeof (blen))
977 vec->rec_len = bfd_getl16 (blen);
978 if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
980 /* Discard record size and align byte. */
982 vec->rec_rem = vec->rec_len;
986 /* Prepend record size. */
987 vec->rec_pos = REC_POS_LEN0;
988 vec->rec_rem = (vec->rec_len + 1) & ~1; /* With align byte. */
990 if (vec->rec_len == 3)
992 /* Possibly end of file. Check the pattern. */
993 if (vms_lib_bread_raw (abfd, vec->pattern, 4) != 4)
995 if (!memcmp (vec->pattern, eotdesc + 2, 3))
997 /* This is really an EOF. */
999 vec->file_len = vec->where;
1004 if (vec->dcxsbms != NULL)
1006 /* This is a compressed member. */
1010 /* Be sure there is enough room for the expansion. */
1011 len = (vec->rec_len + 1) & ~1;
1012 if (len > vec->dcx_max)
1014 while (len > vec->dcx_max)
1016 vec->dcx_buf = bfd_alloc (abfd, vec->dcx_max);
1017 if (vec->dcx_buf == NULL)
1021 /* Read the compressed record. */
1022 vec->dcx_rlen = len;
1023 if (vec->rec_len == 3)
1026 memcpy (vec->dcx_buf, vec->pattern, 3);
1030 elen = vms_lib_bread_raw (abfd, vec->dcx_buf, len);
1035 /* Dummy expansion to get the expanded length. */
1036 vec->dcx_offset = 0;
1037 vec->dcx_sbm = vec->dcxsbms;
1039 elen = vms_lib_dcx (vec, NULL, 0x10000);
1042 vec->rec_len = elen;
1043 vec->rec_rem = elen;
1045 /* Reset the state. */
1046 vec->dcx_offset = 0;
1047 vec->dcx_sbm = vec->dcxsbms;
1051 if (vec->rec_pos < 0)
1054 switch (vec->rec_pos)
1057 c = vec->rec_len & 0xff;
1058 vec->rec_pos = REC_POS_LEN1;
1061 c = (vec->rec_len >> 8) & 0xff;
1085 if (nbytes > vec->rec_rem)
1086 chunk = vec->rec_rem;
1090 if (vec->dcxsbms != NULL)
1092 /* Optimize the stat() case: no need to decompress again as we
1094 if (!(buf == NULL && chunk == vec->rec_rem))
1095 chunk = vms_lib_dcx (vec, buf, chunk);
1099 if (vec->rec_len == 3)
1102 memcpy (buf, vec->pattern + vec->rec_pos, chunk);
1105 chunk = vms_lib_bread_raw (abfd, buf, chunk);
1113 vec->rec_pos += chunk;
1114 vec->rec_rem -= chunk;
1116 if (vec->rec_rem == 0)
1118 /* End of record reached. */
1119 if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
1121 if ((vec->rec_len & 1) == 1
1122 && vec->rec_len != 3
1123 && vec->dcxsbms == NULL)
1125 /* Eat the pad byte. */
1127 if (vms_lib_bread_raw (abfd, &pad, 1) != 1)
1130 vec->rec_pos = REC_POS_NL;
1135 if ((vec->rec_len & 1) == 1 && vec->dcxsbms != NULL)
1137 vec->rec_pos = REC_POS_PAD;
1147 /* Standard function, but we currently only handle the rewind case. */
1150 vms_lib_bseek (struct bfd *abfd, file_ptr offset, int whence)
1152 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1154 if (whence == SEEK_SET && offset == 0)
1159 vec->blk_off = vec->init_blk_off;
1160 vec->next_block = vec->init_next_block;
1162 if (bfd_seek (abfd->my_archive, vec->first_block, SEEK_SET) != 0)
1171 vms_lib_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED,
1172 const void *where ATTRIBUTE_UNUSED,
1173 file_ptr nbytes ATTRIBUTE_UNUSED)
1179 vms_lib_bclose (struct bfd *abfd)
1181 abfd->iostream = NULL;
1186 vms_lib_bflush (struct bfd *abfd ATTRIBUTE_UNUSED)
1192 vms_lib_bstat (struct bfd *abfd ATTRIBUTE_UNUSED,
1193 struct stat *sb ATTRIBUTE_UNUSED)
1195 /* Not supported. */
1200 vms_lib_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
1201 void *addr ATTRIBUTE_UNUSED,
1202 bfd_size_type len ATTRIBUTE_UNUSED,
1203 int prot ATTRIBUTE_UNUSED,
1204 int flags ATTRIBUTE_UNUSED,
1205 file_ptr offset ATTRIBUTE_UNUSED,
1206 void **map_addr ATTRIBUTE_UNUSED,
1207 bfd_size_type *map_len ATTRIBUTE_UNUSED)
1212 static const struct bfd_iovec vms_lib_iovec = {
1213 &vms_lib_bread, &vms_lib_bwrite, &vms_lib_btell, &vms_lib_bseek,
1214 &vms_lib_bclose, &vms_lib_bflush, &vms_lib_bstat, &vms_lib_bmmap
1217 /* Open a library module. FILEPOS is the position of the module header. */
1220 vms_lib_bopen (bfd *el, file_ptr filepos)
1222 struct vms_lib_iovec *vec;
1223 unsigned char buf[256];
1224 struct vms_mhd *mhd;
1225 struct lib_tdata *tdata = bfd_libdata (el->my_archive);
1228 /* Allocate and initialized the iovec. */
1229 vec = bfd_zalloc (el, sizeof (*vec));
1234 el->iovec = &vms_lib_iovec;
1236 /* File length is not known. */
1239 /* Read the first data block. */
1240 vec->next_block = filepos & ~(VMS_BLOCK_SIZE - 1);
1241 vec->blk_off = DATA__LENGTH;
1242 if (!vms_lib_read_block (el))
1245 /* Prepare to read the first record. */
1246 vec->blk_off = filepos & (VMS_BLOCK_SIZE - 1);
1248 if (bfd_seek (el->my_archive, filepos, SEEK_SET) != 0)
1251 /* Read Record length + MHD + align byte. */
1252 len = tdata->mhd_size;
1253 if (vms_lib_bread_raw (el, buf, 2) != 2)
1255 if (bfd_getl16 (buf) != len)
1257 len = (len + 1) & ~1;
1258 BFD_ASSERT (len <= sizeof (buf));
1259 if (vms_lib_bread_raw (el, buf, len) != len)
1262 /* Get info from mhd. */
1263 mhd = (struct vms_mhd *)buf;
1265 if (mhd->id != MHD__C_MHDID)
1267 if (len >= MHD__C_MHDLEN + 1)
1268 el->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
1269 el->mtime = vms_rawtime_to_time_t (mhd->datim);
1270 el->mtime_set = TRUE;
1272 /* Reinit the iovec so that seek() will point to the first record after
1275 vec->init_blk_off = vec->blk_off;
1276 vec->init_next_block = vec->next_block;
1277 vec->first_block = bfd_tell (el->my_archive);
1278 vec->dcxsbms = bfd_libdata (el->my_archive)->dcxsbm;
1280 if (vec->dcxsbms != NULL)
1283 vec->dcx_max = 10 * 1024;
1284 vec->dcx_buf = bfd_alloc (el, vec->dcx_max);
1286 if (vec->dcx_buf == NULL)
1292 /* Get member MODIDX. Return NULL in case of error. */
1295 _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
1297 struct lib_tdata *tdata = bfd_libdata (abfd);
1303 if (modidx >= tdata->nbr_modules)
1306 /* Already loaded. */
1307 if (tdata->cache[modidx])
1308 return tdata->cache[modidx];
1311 file_off = tdata->modules[modidx].file_offset;
1312 if (tdata->type != LBR__C_TYP_IOBJ)
1314 res = _bfd_create_empty_archive_element_shell (abfd);
1318 /* Special reader to deal with data blocks. */
1319 if (!vms_lib_bopen (res, file_off))
1325 struct vms_mhd *mhd;
1326 struct areltdata *arelt;
1328 /* Sanity check. The MHD must be big enough to contain module size. */
1329 if (tdata->mhd_size < offsetof (struct vms_mhd, modsize) + 4)
1332 /* Read the MHD now. */
1333 if (bfd_seek (abfd, file_off, SEEK_SET) != 0)
1335 if (bfd_bread (buf, tdata->mhd_size, abfd) != tdata->mhd_size)
1338 res = _bfd_create_empty_archive_element_shell (abfd);
1341 arelt = bfd_zmalloc (sizeof (*arelt));
1344 res->arelt_data = arelt;
1346 /* Get info from mhd. */
1347 mhd = (struct vms_mhd *)buf;
1348 if (mhd->id != MHD__C_MHDID)
1350 if (tdata->mhd_size >= offsetof (struct vms_mhd, objstat) + 1)
1351 res->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
1352 res->mtime = vms_rawtime_to_time_t (mhd->datim);
1353 res->mtime_set = TRUE;
1355 arelt->parsed_size = bfd_getl32 (mhd->modsize);
1357 /* No need for a special reader as members are stored linearly.
1358 Just skip the MHD. */
1359 res->origin = file_off + tdata->mhd_size;
1363 name = tdata->modules[modidx].name;
1364 switch (tdata->type)
1366 case LBR__C_TYP_IOBJ:
1367 case LBR__C_TYP_EOBJ:
1368 /* For object archives, append .obj to mimic standard behaviour. */
1370 size_t namelen = strlen (name);
1371 char *name1 = bfd_alloc (res, namelen + 4 + 1);
1372 memcpy (name1, name, namelen);
1373 strcpy (name1 + namelen, ".obj");
1380 res->filename = xstrdup (name);
1382 tdata->cache[modidx] = res;
1387 /* Standard function: get member at IDX. */
1390 _bfd_vms_lib_get_elt_at_index (bfd *abfd, symindex symidx)
1392 struct lib_tdata *tdata = bfd_libdata (abfd);
1394 unsigned int modidx;
1397 if (symidx > tdata->artdata.symdef_count)
1399 file_off = tdata->artdata.symdefs[symidx].file_offset;
1402 for (modidx = 0; modidx < tdata->nbr_modules; modidx++)
1404 if (tdata->modules[modidx].file_offset == file_off)
1407 if (modidx >= tdata->nbr_modules)
1410 return _bfd_vms_lib_get_module (abfd, modidx);
1413 /* Elements of an imagelib are stubs. You can get the real image with this
1417 _bfd_vms_lib_get_imagelib_file (bfd *el)
1419 bfd *archive = el->my_archive;
1420 const char *modname = el->filename;
1421 int modlen = strlen (modname);
1426 /* Convert module name to lower case and append '.exe'. */
1427 filename = bfd_alloc (el, modlen + 5);
1428 if (filename == NULL)
1430 for (j = 0; j < modlen; j++)
1431 if (ISALPHA (modname[j]))
1432 filename[j] = TOLOWER (modname[j]);
1434 filename[j] = modname[j];
1435 memcpy (filename + modlen, ".exe", 5);
1437 filename = _bfd_append_relative_path (archive, filename);
1438 if (filename == NULL)
1440 res = bfd_openr (filename, NULL);
1444 (*_bfd_error_handler)(_("could not open shared image '%s' from '%s'"),
1445 filename, archive->filename);
1446 bfd_release (archive, filename);
1450 /* FIXME: put it in a cache ? */
1454 /* Standard function. */
1457 _bfd_vms_lib_openr_next_archived_file (bfd *archive,
1466 idx = last_file->proxy_origin + 1;
1468 if (idx >= bfd_libdata (archive)->nbr_modules)
1470 bfd_set_error (bfd_error_no_more_archived_files);
1474 res = _bfd_vms_lib_get_module (archive, idx);
1477 res->proxy_origin = idx;
1481 /* Standard function. Just compute the length. */
1484 _bfd_vms_lib_generic_stat_arch_elt (bfd *abfd, struct stat *st)
1486 struct lib_tdata *tdata;
1489 if (abfd->my_archive == NULL)
1491 bfd_set_error (bfd_error_invalid_operation);
1495 tdata = bfd_libdata (abfd->my_archive);
1496 if (tdata->type != LBR__C_TYP_IOBJ)
1498 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1500 if (vec->file_len == (ufile_ptr)-1)
1502 if (vms_lib_bseek (abfd, 0, SEEK_SET) != 0)
1505 /* Compute length. */
1506 while (vms_lib_bread (abfd, NULL, 1 << 20) > 0)
1509 st->st_size = vec->file_len;
1513 st->st_size = ((struct areltdata *)abfd->arelt_data)->parsed_size;
1516 if (abfd->mtime_set)
1517 st->st_mtime = abfd->mtime;
1527 /* Internal representation of an index entry. */
1531 /* Corresponding archive member. */
1534 /* Number of reference to this entry. */
1537 /* Length of the key. */
1538 unsigned short namlen;
1544 /* Used to sort index entries. */
1547 lib_index_cmp (const void *lv, const void *rv)
1549 const struct lib_index *l = lv;
1550 const struct lib_index *r = rv;
1552 return strcmp (l->name, r->name);
1555 /* Maximum number of index blocks level. */
1557 #define MAX_LEVEL 10
1559 /* Get the size of an index entry. */
1562 get_idxlen (struct lib_index *idx, bfd_boolean is_elfidx)
1566 /* 9 is the size of struct vms_elfidx without keyname. */
1567 if (idx->namlen > MAX_KEYLEN)
1568 return 9 + sizeof (struct vms_kbn);
1570 return 9 + idx->namlen;
1574 /* 7 is the size of struct vms_idx without keyname. */
1575 return 7 + idx->namlen;
1579 /* Write the index composed by NBR symbols contained in IDX.
1580 VBN is the first vbn to be used, and will contain on return the last vbn.
1581 Can be called with ABFD set to NULL just to size the index.
1582 If not null, TOPVBN will be assigned to the vbn of the root index tree.
1583 IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
1584 Return TRUE on success. */
1587 vms_write_index (bfd *abfd,
1588 struct lib_index *idx, unsigned int nbr, unsigned int *vbn,
1589 unsigned int *topvbn, bfd_boolean is_elfidx)
1591 /* The index is organized as a tree. This function implements a naive
1592 algorithm to balance the tree: it fills the leaves, and create a new
1593 branch when all upper leaves and branches are full. We only keep in
1594 memory a path to the current leaf. */
1598 /* Disk blocks for the current path. */
1599 struct vms_indexdef *rblk[MAX_LEVEL];
1600 /* Info on the current blocks. */
1603 unsigned int vbn; /* VBN of the block. */
1604 /* The last entry is identified so that it could be copied to the
1606 unsigned short len; /* Length up to the last entry. */
1607 unsigned short lastlen; /* Length of the last entry. */
1610 /* The kbn blocks are used to store long symbol names. */
1611 unsigned int kbn_sz = 0; /* Number of bytes available in the kbn block. */
1612 unsigned int kbn_vbn = 0; /* VBN of the kbn block. */
1613 unsigned char *kbn_blk = NULL; /* Contents of the kbn block. */
1617 /* No entries. Very easy to handle. */
1625 /* Sort the index the first time this function is called. */
1626 qsort (idx, nbr, sizeof (struct lib_index), lib_index_cmp);
1629 /* Allocate first index block. */
1632 rblk[0] = bfd_zmalloc (sizeof (struct vms_indexdef));
1633 blk[0].vbn = (*vbn)++;
1637 for (i = 0; i < nbr; i++, idx++)
1639 unsigned int idxlen;
1641 unsigned int key_vbn = 0;
1642 unsigned int key_off = 0;
1644 idxlen = get_idxlen (idx, is_elfidx);
1646 if (is_elfidx && idx->namlen > MAX_KEYLEN)
1648 /* If the key (ie name) is too long, write it in the kbn block. */
1649 unsigned int kl = idx->namlen;
1650 unsigned int kl_chunk;
1651 const char *key = idx->name;
1653 /* Write the key in the kbn, chunk after chunk. */
1656 if (kbn_sz < sizeof (struct vms_kbn))
1658 /* Not enough room in the kbn block. */
1661 /* Write it to the disk (if there is one). */
1664 if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
1669 kbn_blk = bfd_malloc (VMS_BLOCK_SIZE);
1670 if (kbn_blk == NULL)
1673 *(unsigned short *)kbn_blk = 0;
1675 /* Allocate a new block for the keys. */
1677 kbn_sz = VMS_BLOCK_SIZE - 2;
1679 /* Size of the chunk written to the current key block. */
1680 if (kl + sizeof (struct vms_kbn) > kbn_sz)
1681 kl_chunk = kbn_sz - sizeof (struct vms_kbn);
1685 if (kbn_blk != NULL)
1687 struct vms_kbn *kbn;
1689 kbn = (struct vms_kbn *)(kbn_blk + VMS_BLOCK_SIZE - kbn_sz);
1693 /* Save the rfa of the first chunk. */
1695 key_off = VMS_BLOCK_SIZE - kbn_sz;
1698 bfd_putl16 (kl_chunk, kbn->keylen);
1701 /* No next chunk. */
1702 bfd_putl32 (0, kbn->rfa.vbn);
1703 bfd_putl16 (0, kbn->rfa.offset);
1707 /* Next chunk will be at the start of the next block. */
1708 bfd_putl32 (*vbn, kbn->rfa.vbn);
1709 bfd_putl16 (2, kbn->rfa.offset);
1711 memcpy ((char *)(kbn + 1), key, kl_chunk);
1715 kl_chunk = (kl_chunk + 1) & ~1; /* Always align. */
1716 kbn_sz -= kl_chunk + sizeof (struct vms_kbn);
1721 /* Check if a block might overflow. In this case we will flush this
1722 block and all the blocks below it. */
1723 for (j = 0; j < level; j++)
1724 if (blk[j].len + blk[j].lastlen + idxlen > INDEXDEF__BLKSIZ)
1727 for (j = 0; j < level; j++)
1731 /* There is not enough room to write the new entry in this
1732 block or in a parent block. */
1736 BFD_ASSERT (level < MAX_LEVEL);
1738 /* Need to create a parent. */
1741 rblk[level] = bfd_zmalloc (sizeof (struct vms_indexdef));
1742 bfd_putl32 (*vbn, rblk[j]->parent);
1744 blk[level].vbn = (*vbn)++;
1746 blk[level].lastlen = blk[j].lastlen;
1751 /* Update parent block: write the last entry from the current
1755 struct vms_rfa *rfa;
1757 /* Pointer to the last entry in parent block. */
1758 rfa = (struct vms_rfa *)(rblk[j + 1]->keys + blk[j + 1].len);
1760 /* Copy the whole entry. */
1761 BFD_ASSERT (blk[j + 1].lastlen == blk[j].lastlen);
1762 memcpy (rfa, rblk[j]->keys + blk[j].len, blk[j].lastlen);
1763 /* Fix the entry (which in always the first field of an
1765 bfd_putl32 (blk[j].vbn, rfa->vbn);
1766 bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
1771 /* And allocate it. Do it only on the block that won't be
1772 flushed (so that the parent of the parent can be
1774 blk[j + 1].len += blk[j + 1].lastlen;
1775 blk[j + 1].lastlen = 0;
1778 /* Write this block on the disk. */
1781 bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
1782 if (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
1786 /* Reset this block. */
1789 blk[j].vbn = (*vbn)++;
1792 /* Append it to the block. */
1795 /* Keep the previous last entry. */
1796 blk[j].len += blk[j].lastlen;
1800 struct vms_rfa *rfa;
1802 rfa = (struct vms_rfa *)(rblk[j]->keys + blk[j].len);
1803 bfd_putl32 ((idx->abfd->proxy_origin / VMS_BLOCK_SIZE) + 1,
1806 ((idx->abfd->proxy_origin % VMS_BLOCK_SIZE)
1807 + (is_elfidx ? 0 : DATA__DATA),
1812 /* Use elfidx format. */
1813 struct vms_elfidx *en = (struct vms_elfidx *)rfa;
1818 /* Long symbol name. */
1819 struct vms_kbn *k = (struct vms_kbn *)(en->keyname);
1820 bfd_putl16 (sizeof (struct vms_kbn), en->keylen);
1821 bfd_putl16 (idx->namlen, k->keylen);
1822 bfd_putl32 (key_vbn, k->rfa.vbn);
1823 bfd_putl16 (key_off, k->rfa.offset);
1824 en->flags |= ELFIDX__SYMESC;
1828 bfd_putl16 (idx->namlen, en->keylen);
1829 memcpy (en->keyname, idx->name, idx->namlen);
1834 /* Use idx format. */
1835 struct vms_idx *en = (struct vms_idx *)rfa;
1836 en->keylen = idx->namlen;
1837 memcpy (en->keyname, idx->name, idx->namlen);
1841 /* The last added key can now be the last one all blocks in the
1843 blk[j].lastlen = idxlen;
1847 /* Save VBN of the root. */
1849 *topvbn = blk[level - 1].vbn;
1855 for (j = 1; j < level; j++)
1857 /* Update parent block: write the new entry. */
1860 struct vms_rfa *rfa;
1862 en = rblk[j - 1]->keys + blk[j - 1].len;
1863 par = rblk[j]->keys + blk[j].len;
1864 BFD_ASSERT (blk[j].lastlen == blk[j - 1].lastlen);
1865 memcpy (par, en, blk[j - 1].lastlen);
1866 rfa = (struct vms_rfa *)par;
1867 bfd_putl32 (blk[j - 1].vbn, rfa->vbn);
1868 bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
1871 for (j = 0; j < level; j++)
1873 /* Write this block on the disk. */
1874 bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
1875 if (vms_write_block (abfd, blk[j].vbn, rblk[j]) != TRUE)
1881 /* Write the last kbn (if any). */
1884 if (vms_write_block (abfd, kbn_vbn, kbn_blk) != TRUE)
1892 /* Append data to the data block DATA. Force write if PAD is true. */
1895 vms_write_data_block (bfd *arch, struct vms_datadef *data, file_ptr *off,
1896 const unsigned char *buf, unsigned int len, int pad)
1898 while (len > 0 || pad)
1900 unsigned int doff = *off & (VMS_BLOCK_SIZE - 1);
1901 unsigned int remlen = (DATA__LENGTH - DATA__DATA) - doff;
1904 l = (len > remlen) ? remlen : len;
1905 memcpy (data->data + doff, buf, l);
1911 if (doff == (DATA__LENGTH - DATA__DATA) || (len == 0 && pad))
1915 bfd_putl32 ((*off / VMS_BLOCK_SIZE) + 2, data->link);
1917 if (bfd_bwrite (data, sizeof (*data), arch) != sizeof (*data))
1920 *off += DATA__LENGTH - doff;
1929 /* Build the symbols index. */
1932 _bfd_vms_lib_build_map (unsigned int nbr_modules,
1933 struct lib_index *modules,
1934 unsigned int *res_cnt,
1935 struct lib_index **res)
1938 asymbol **syms = NULL;
1940 struct lib_index *map = NULL;
1941 unsigned int map_max = 1024; /* Fine initial default. */
1942 unsigned int map_count = 0;
1944 map = (struct lib_index *) bfd_malloc (map_max * sizeof (struct lib_index));
1948 /* Gather symbols. */
1949 for (i = 0; i < nbr_modules; i++)
1954 bfd *current = modules[i].abfd;
1956 if ((bfd_get_file_flags (current) & HAS_SYMS) == 0)
1959 storage = bfd_get_symtab_upper_bound (current);
1965 if (storage > syms_max)
1970 syms = (asymbol **) bfd_malloc (syms_max);
1974 symcount = bfd_canonicalize_symtab (current, syms);
1978 /* Now map over all the symbols, picking out the ones we
1980 for (src_count = 0; src_count < symcount; src_count++)
1982 flagword flags = (syms[src_count])->flags;
1983 asection *sec = syms[src_count]->section;
1985 if ((flags & BSF_GLOBAL
1987 || flags & BSF_INDIRECT
1988 || bfd_is_com_section (sec))
1989 && ! bfd_is_und_section (sec))
1991 struct lib_index *new_map;
1993 /* This symbol will go into the archive header. */
1994 if (map_count == map_max)
1997 new_map = (struct lib_index *)
1998 bfd_realloc (map, map_max * sizeof (struct lib_index));
1999 if (new_map == NULL)
2004 map[map_count].abfd = current;
2005 map[map_count].namlen = strlen (syms[src_count]->name);
2006 map[map_count].name = syms[src_count]->name;
2014 *res_cnt = map_count;
2026 /* Do the hard work: write an archive on the disk. */
2029 _bfd_vms_lib_write_archive_contents (bfd *arch)
2032 unsigned int nbr_modules;
2033 struct lib_index *modules;
2034 unsigned int nbr_symbols;
2035 struct lib_index *symbols;
2036 struct lib_tdata *tdata = bfd_libdata (arch);
2039 unsigned int nbr_mod_iblk;
2040 unsigned int nbr_sym_iblk;
2042 unsigned int mod_idx_vbn;
2043 unsigned int sym_idx_vbn;
2044 bfd_boolean is_elfidx = tdata->kind == vms_lib_ia64;
2045 unsigned int max_keylen = is_elfidx ? MAX_EKEYLEN : MAX_KEYLEN;
2047 /* Count the number of modules (and do a first sanity check). */
2049 for (current = arch->archive_head;
2051 current = current->archive_next)
2053 /* This check is checking the bfds for the objects we're reading
2054 from (which are usually either an object file or archive on
2055 disk), not the archive entries we're writing to. We don't
2056 actually create bfds for the archive members, we just copy
2057 them byte-wise when we write out the archive. */
2058 if (bfd_write_p (current) || !bfd_check_format (current, bfd_object))
2060 bfd_set_error (bfd_error_invalid_operation);
2067 /* Build the modules list. */
2068 BFD_ASSERT (tdata->modules == NULL);
2069 modules = bfd_alloc (arch, nbr_modules * sizeof (struct lib_index));
2070 if (modules == NULL)
2073 for (current = arch->archive_head, i = 0;
2075 current = current->archive_next, i++)
2079 modules[i].abfd = current;
2080 modules[i].name = vms_get_module_name (current->filename, FALSE);
2083 /* FIXME: silently truncate long names ? */
2084 nl = strlen (modules[i].name);
2085 modules[i].namlen = (nl > max_keylen ? max_keylen : nl);
2088 /* Create the module index. */
2090 if (!vms_write_index (NULL, modules, nbr_modules, &vbn, NULL, is_elfidx))
2094 /* Create symbol index. */
2095 if (!_bfd_vms_lib_build_map (nbr_modules, modules, &nbr_symbols, &symbols))
2099 if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx))
2103 /* Write modules and remember their position. */
2104 off = (1 + nbr_mod_iblk + nbr_sym_iblk) * VMS_BLOCK_SIZE;
2106 if (bfd_seek (arch, off, SEEK_SET) != 0)
2109 for (i = 0; i < nbr_modules; i++)
2111 struct vms_datadef data;
2112 unsigned char blk[VMS_BLOCK_SIZE];
2113 struct vms_mhd *mhd;
2116 current = modules[i].abfd;
2117 current->proxy_origin = off;
2123 /* Write the MHD as a record (ie, size first). */
2125 bfd_putl16 (tdata->mhd_size, blk);
2127 mhd = (struct vms_mhd *)(blk + sz);
2128 memset (mhd, 0, sizeof (struct vms_mhd));
2130 mhd->id = MHD__C_MHDID;
2132 memcpy (mhd->objid, "V1.0", 4);
2133 bfd_putl32 (modules[i].ref, mhd->refcnt);
2136 sz += tdata->mhd_size;
2139 /* Rewind the member to be put into the archive. */
2140 if (bfd_seek (current, 0, SEEK_SET) != 0)
2143 /* Copy the member into the archive. */
2146 unsigned int modsize = 0;
2148 file_ptr off_hdr = off;
2150 /* Read to complete the first block. */
2151 amt = bfd_bread (blk + sz, VMS_BLOCK_SIZE - sz, current);
2152 if (amt == (bfd_size_type)-1)
2155 if (amt < VMS_BLOCK_SIZE - sz)
2157 /* The member size is less than a block. Pad the block. */
2158 memset (blk + sz + amt, 0, VMS_BLOCK_SIZE - sz - amt);
2160 bfd_putl32 (modsize, mhd->modsize);
2162 /* Write the first block (which contains an mhd). */
2163 if (bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
2165 off += VMS_BLOCK_SIZE;
2167 if (amt == VMS_BLOCK_SIZE - sz)
2169 /* Copy the remaining. */
2170 char buffer[DEFAULT_BUFFERSIZE];
2174 amt = bfd_bread (buffer, sizeof (buffer), current);
2175 if (amt == (bfd_size_type)-1)
2180 if (amt != sizeof (buffer))
2182 /* Clear the padding. */
2183 memset (buffer + amt, 0, sizeof (buffer) - amt);
2184 amt = (amt + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
2186 if (bfd_bwrite (buffer, amt, arch) != amt)
2191 /* Now that the size is known, write the first block (again). */
2192 bfd_putl32 (modsize, mhd->modsize);
2193 if (bfd_seek (arch, off_hdr, SEEK_SET) != 0
2194 || bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
2196 if (bfd_seek (arch, off, SEEK_SET) != 0)
2202 /* Write the MHD. */
2203 if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
2206 /* Write the member. */
2209 sz = bfd_bread (blk, sizeof (blk), current);
2212 if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
2216 /* Write the end of module marker. */
2217 if (vms_write_data_block (arch, &data, &off,
2218 eotdesc, sizeof (eotdesc), 1) < 0)
2223 /* Write the indexes. */
2225 if (vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn,
2228 if (vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn,
2232 /* Write libary header. */
2234 unsigned char blk[VMS_BLOCK_SIZE];
2235 struct vms_lhd *lhd = (struct vms_lhd *)blk;
2236 struct vms_idd *idd = (struct vms_idd *)(blk + sizeof (*lhd));
2237 unsigned int idd_flags;
2238 unsigned int saneid;
2240 memset (blk, 0, sizeof (blk));
2242 lhd->type = tdata->type;
2244 switch (tdata->kind)
2247 saneid = LHD_SANEID3;
2250 saneid = LHD_SANEID6;
2255 bfd_putl32 (saneid, lhd->sanity);
2256 bfd_putl16 (tdata->ver, lhd->majorid);
2257 bfd_putl16 (0, lhd->minorid);
2258 snprintf ((char *)lhd->lbrver + 1, sizeof (lhd->lbrver) - 1,
2260 (unsigned)(BFD_VERSION / 100000000UL),
2261 (unsigned)(BFD_VERSION / 1000000UL) % 100,
2262 (unsigned)(BFD_VERSION / 10000UL) % 100);
2263 lhd->lbrver[sizeof (lhd->lbrver) - 1] = 0;
2264 lhd->lbrver[0] = strlen ((char *)lhd->lbrver + 1);
2266 bfd_putl32 (tdata->credat_lo, lhd->credat + 0);
2267 bfd_putl32 (tdata->credat_hi, lhd->credat + 4);
2268 vms_raw_get_time (lhd->updtim);
2270 lhd->mhdusz = tdata->mhd_size - MHD__C_USRDAT;
2272 bfd_putl32 (nbr_modules + nbr_symbols, lhd->idxcnt);
2273 bfd_putl32 (nbr_modules, lhd->modcnt);
2274 bfd_putl32 (nbr_modules, lhd->modhdrs);
2276 /* Number of blocks for index. */
2277 bfd_putl32 (nbr_mod_iblk + nbr_sym_iblk, lhd->idxblks);
2278 bfd_putl32 (vbn - 1, lhd->hipreal);
2279 bfd_putl32 (vbn - 1, lhd->hiprusd);
2281 /* VBN of the next free block. */
2282 bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextvbn);
2283 bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextrfa + 0);
2284 bfd_putl16 (0, lhd->nextrfa + 4);
2286 /* First index (modules name). */
2287 idd_flags = IDD__FLAGS_ASCII | IDD__FLAGS_VARLENIDX
2288 | IDD__FLAGS_NOCASECMP | IDD__FLAGS_NOCASENTR;
2289 bfd_putl16 (idd_flags, idd->flags);
2290 bfd_putl16 (max_keylen + 1, idd->keylen);
2291 bfd_putl16 (mod_idx_vbn, idd->vbn);
2294 /* Second index (symbols name). */
2295 bfd_putl16 (idd_flags, idd->flags);
2296 bfd_putl16 (max_keylen + 1, idd->keylen);
2297 bfd_putl16 (sym_idx_vbn, idd->vbn);
2300 if (vms_write_block (arch, 1, blk) != TRUE)
2307 bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
2311 /* Add a target for text library. This costs almost nothing and is useful to
2312 read VMS library on the host. */
2314 const bfd_target alpha_vms_lib_txt_vec =
2316 "vms-libtxt", /* Name. */
2317 bfd_target_unknown_flavour,
2318 BFD_ENDIAN_UNKNOWN, /* byteorder */
2319 BFD_ENDIAN_UNKNOWN, /* header_byteorder */
2320 0, /* Object flags. */
2321 0, /* Sect flags. */
2322 0, /* symbol_leading_char. */
2323 ' ', /* ar_pad_char. */
2324 15, /* ar_max_namelen. */
2325 0, /* match priority. */
2326 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2327 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2328 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
2329 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2330 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2331 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
2333 {_bfd_dummy_target, _bfd_dummy_target, /* bfd_check_format. */
2334 _bfd_vms_lib_txt_archive_p, _bfd_dummy_target},
2335 {bfd_false, bfd_false, bfd_false, bfd_false}, /* bfd_set_format. */
2336 {bfd_false, bfd_false, bfd_false, bfd_false}, /* bfd_write_contents. */
2338 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
2339 BFD_JUMP_TABLE_COPY (_bfd_generic),
2340 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2341 BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib),
2342 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
2343 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
2344 BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
2345 BFD_JUMP_TABLE_LINK (_bfd_nolink),
2346 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),