3b42857aa9ce7e89d1e5c66da7c1a2e55eb24f42
[deliverable/binutils-gdb.git] / bfd / vms-lib.c
1 /* BFD back-end for VMS archive files.
2
3 Copyright (C) 2010-2020 Free Software Foundation, Inc.
4 Written by Tristan Gingold <gingold@adacore.com>, AdaCore.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "safe-ctype.h"
27 #include "bfdver.h"
28 #include "libiberty.h"
29 #include "vms.h"
30 #include "vms/lbr.h"
31 #include "vms/dcx.h"
32
33 /* The standard VMS disk block size. */
34 #ifndef VMS_BLOCK_SIZE
35 #define VMS_BLOCK_SIZE 512
36 #endif
37
38 /* Maximum key length (which is also the maximum symbol length in archive). */
39 #define MAX_KEYLEN 128
40 #define MAX_EKEYLEN 1024
41
42 /* DCX Submaps. */
43
44 struct dcxsbm_desc
45 {
46 unsigned char min_char;
47 unsigned char max_char;
48 unsigned char *flags;
49 unsigned char *nodes;
50 unsigned short *next;
51 };
52
53 /* Kind of library. Used to filter in archive_p. */
54
55 enum vms_lib_kind
56 {
57 vms_lib_vax,
58 vms_lib_alpha,
59 vms_lib_ia64,
60 vms_lib_txt
61 };
62
63 /* Back-end private data. */
64
65 struct lib_tdata
66 {
67 /* Standard tdata for an archive. But we don't use many fields. */
68 struct artdata artdata;
69
70 /* Major version. */
71 unsigned char ver;
72
73 /* Type of the archive. */
74 unsigned char type;
75
76 /* Kind of archive. Summary of its type. */
77 enum vms_lib_kind kind;
78
79 /* Total size of the mhd (element header). */
80 unsigned int mhd_size;
81
82 /* Creation date. */
83 unsigned int credat_lo;
84 unsigned int credat_hi;
85
86 /* Vector of modules (archive elements), already sorted. */
87 unsigned int nbr_modules;
88 struct carsym *modules;
89 bfd **cache;
90
91 /* DCX (decompression) data. */
92 unsigned int nbr_dcxsbm;
93 struct dcxsbm_desc *dcxsbm;
94 };
95
96 #define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
97
98 /* End-Of-Text pattern. This is a special record to mark the end of file. */
99
100 static const unsigned char eotdesc[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
101
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
110 freed. */
111
112 struct carsym_mem
113 {
114 /* The table of content. */
115 struct carsym *idx;
116
117 /* Number of entries used in the table. */
118 unsigned int nbr;
119
120 /* Maximum number of entries. */
121 unsigned int max;
122
123 /* Do not allocate more that this number of entries. */
124 unsigned int limit;
125
126 /* If true, the table was reallocated on the heap. If false, it is still
127 in the BFD's objalloc. */
128 bfd_boolean realloced;
129 };
130
131 /* Simply add a name to the index. */
132
133 static bfd_boolean
134 vms_add_index (struct carsym_mem *cs, char *name,
135 unsigned int idx_vbn, unsigned int idx_off)
136 {
137 if (cs->nbr == cs->max)
138 {
139 struct carsym *n;
140 size_t amt;
141
142 if (cs->max > -33u / 2 || cs->max >= cs->limit)
143 {
144 bfd_set_error (bfd_error_file_too_big);
145 return FALSE;
146 }
147 cs->max = 2 * cs->max + 32;
148 if (cs->max > cs->limit)
149 cs->max = cs->limit;
150 if (_bfd_mul_overflow (cs->max, sizeof (struct carsym), &amt))
151 {
152 bfd_set_error (bfd_error_file_too_big);
153 return FALSE;
154 }
155
156 if (!cs->realloced)
157 {
158 n = bfd_malloc (amt);
159 if (n == NULL)
160 return FALSE;
161 memcpy (n, cs->idx, cs->nbr * sizeof (struct carsym));
162 /* And unfortunately we can't free cs->idx. */
163 }
164 else
165 {
166 n = bfd_realloc_or_free (cs->idx, amt);
167 if (n == NULL)
168 return FALSE;
169 }
170 cs->idx = n;
171 cs->realloced = TRUE;
172 }
173 cs->idx[cs->nbr].file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
174 cs->idx[cs->nbr].name = name;
175 cs->nbr++;
176 return TRUE;
177 }
178
179 /* Follow all member of a lns list (pointed by RFA) and add indexes for
180 NAME. Return FALSE in case of error. */
181
182 static bfd_boolean
183 vms_add_indexes_from_list (bfd *abfd, struct carsym_mem *cs, char *name,
184 struct vms_rfa *rfa)
185 {
186 struct vms_lns lns;
187 unsigned int vbn;
188 file_ptr off;
189
190 while (1)
191 {
192 vbn = bfd_getl32 (rfa->vbn);
193 if (vbn == 0)
194 return TRUE;
195
196 /* Read the LHS. */
197 off = (vbn - 1) * VMS_BLOCK_SIZE + bfd_getl16 (rfa->offset);
198 if (bfd_seek (abfd, off, SEEK_SET) != 0
199 || bfd_bread (&lns, sizeof (lns), abfd) != sizeof (lns))
200 return FALSE;
201
202 if (!vms_add_index (cs, name,
203 bfd_getl32 (lns.modrfa.vbn),
204 bfd_getl16 (lns.modrfa.offset)))
205 return FALSE;
206
207 rfa = &lns.nxtrfa;
208 }
209 }
210
211 /* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
212
213 static bfd_boolean
214 vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
215 {
216 file_ptr off;
217
218 off = (vbn - 1) * VMS_BLOCK_SIZE;
219 if (bfd_seek (abfd, off, SEEK_SET) != 0
220 || bfd_bread (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
221 return FALSE;
222
223 return TRUE;
224 }
225
226 /* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
227
228 static bfd_boolean
229 vms_write_block (bfd *abfd, unsigned int vbn, void *blk)
230 {
231 file_ptr off;
232
233 off = (vbn - 1) * VMS_BLOCK_SIZE;
234 if (bfd_seek (abfd, off, SEEK_SET) != 0
235 || bfd_bwrite (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
236 return FALSE;
237
238 return TRUE;
239 }
240
241 /* Read index block VBN and put the entry in **IDX (which is updated).
242 If the entry is indirect, recurse. */
243
244 static bfd_boolean
245 vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs)
246 {
247 struct vms_indexdef indexdef;
248 file_ptr off;
249 unsigned char *p;
250 unsigned char *endp;
251 unsigned int n;
252
253 /* Read the index block. */
254 BFD_ASSERT (sizeof (indexdef) == VMS_BLOCK_SIZE);
255 if (!vms_read_block (abfd, vbn, &indexdef))
256 return FALSE;
257
258 /* Traverse it. */
259 p = &indexdef.keys[0];
260 n = bfd_getl16 (indexdef.used);
261 if (n > sizeof (indexdef.keys))
262 return FALSE;
263 endp = p + n;
264 while (p < endp)
265 {
266 unsigned int idx_vbn;
267 unsigned int idx_off;
268 unsigned int keylen;
269 unsigned char *keyname;
270 unsigned int flags;
271
272 /* Extract key length. */
273 if (bfd_libdata (abfd)->ver == LBR_MAJORID)
274 {
275 struct vms_idx *ridx = (struct vms_idx *)p;
276
277 idx_vbn = bfd_getl32 (ridx->rfa.vbn);
278 idx_off = bfd_getl16 (ridx->rfa.offset);
279
280 keylen = ridx->keylen;
281 flags = 0;
282 keyname = ridx->keyname;
283 }
284 else if (bfd_libdata (abfd)->ver == LBR_ELFMAJORID)
285 {
286 struct vms_elfidx *ridx = (struct vms_elfidx *)p;
287
288 idx_vbn = bfd_getl32 (ridx->rfa.vbn);
289 idx_off = bfd_getl16 (ridx->rfa.offset);
290
291 keylen = bfd_getl16 (ridx->keylen);
292 flags = ridx->flags;
293 keyname = ridx->keyname;
294 }
295 else
296 return FALSE;
297
298 /* Illegal value. */
299 if (idx_vbn == 0)
300 return FALSE;
301
302 /* Point to the next index entry. */
303 p = keyname + keylen;
304 if (p > endp)
305 return FALSE;
306
307 if (idx_off == RFADEF__C_INDEX)
308 {
309 /* Indirect entry. Recurse. */
310 if (!vms_traverse_index (abfd, idx_vbn, cs))
311 return FALSE;
312 }
313 else
314 {
315 /* Add a new entry. */
316 char *name;
317
318 if (flags & ELFIDX__SYMESC)
319 {
320 /* Extended key name. */
321 unsigned int noff = 0;
322 unsigned int koff;
323 unsigned int kvbn;
324 struct vms_kbn *kbn;
325 unsigned char kblk[VMS_BLOCK_SIZE];
326
327 /* Sanity check. */
328 if (keylen != sizeof (struct vms_kbn))
329 return FALSE;
330
331 kbn = (struct vms_kbn *)keyname;
332 keylen = bfd_getl16 (kbn->keylen);
333
334 name = bfd_alloc (abfd, keylen + 1);
335 if (name == NULL)
336 return FALSE;
337 kvbn = bfd_getl32 (kbn->rfa.vbn);
338 koff = bfd_getl16 (kbn->rfa.offset);
339
340 /* Read the key, chunk by chunk. */
341 do
342 {
343 unsigned int klen;
344
345 if (!vms_read_block (abfd, kvbn, kblk))
346 return FALSE;
347 if (koff > sizeof (kblk) - sizeof (struct vms_kbn))
348 return FALSE;
349 kbn = (struct vms_kbn *)(kblk + koff);
350 klen = bfd_getl16 (kbn->keylen);
351 if (klen > sizeof (kblk) - koff)
352 return FALSE;
353 kvbn = bfd_getl32 (kbn->rfa.vbn);
354 koff = bfd_getl16 (kbn->rfa.offset);
355
356 if (noff + klen > keylen)
357 return FALSE;
358 memcpy (name + noff, kbn + 1, klen);
359 noff += klen;
360 }
361 while (kvbn != 0);
362
363 /* Sanity check. */
364 if (noff != keylen)
365 return FALSE;
366 }
367 else
368 {
369 /* Usual key name. */
370 name = bfd_alloc (abfd, keylen + 1);
371 if (name == NULL)
372 return FALSE;
373
374 memcpy (name, keyname, keylen);
375 }
376 name[keylen] = 0;
377
378 if (flags & ELFIDX__LISTRFA)
379 {
380 struct vms_lhs lhs;
381
382 /* Read the LHS. */
383 off = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
384 if (bfd_seek (abfd, off, SEEK_SET) != 0
385 || bfd_bread (&lhs, sizeof (lhs), abfd) != sizeof (lhs))
386 return FALSE;
387
388 /* These extra entries may cause reallocation of CS. */
389 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_g_rfa))
390 return FALSE;
391 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_wk_rfa))
392 return FALSE;
393 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_g_rfa))
394 return FALSE;
395 if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_wk_rfa))
396 return FALSE;
397 }
398 else
399 {
400 if (!vms_add_index (cs, name, idx_vbn, idx_off))
401 return FALSE;
402 }
403 }
404 }
405
406 return TRUE;
407 }
408
409 /* Read index #IDX, which must have NBREL entries. */
410
411 static struct carsym *
412 vms_lib_read_index (bfd *abfd, int idx, unsigned int *nbrel)
413 {
414 struct vms_idd idd;
415 unsigned int flags;
416 unsigned int vbn;
417 ufile_ptr filesize;
418 size_t amt;
419 struct carsym_mem csm;
420
421 /* Read index desription. */
422 if (bfd_seek (abfd, LHD_IDXDESC + idx * IDD_LENGTH, SEEK_SET) != 0
423 || bfd_bread (&idd, sizeof (idd), abfd) != sizeof (idd))
424 return NULL;
425
426 /* Sanity checks. */
427 flags = bfd_getl16 (idd.flags);
428 if (!(flags & IDD__FLAGS_ASCII)
429 || !(flags & IDD__FLAGS_VARLENIDX))
430 return NULL;
431
432 filesize = bfd_get_file_size (abfd);
433 csm.nbr = 0;
434 csm.max = *nbrel;
435 csm.limit = -1u;
436 csm.realloced = FALSE;
437 if (filesize != 0)
438 {
439 /* Put an upper bound based on a file full of single char keys.
440 This is to prevent fuzzed binary silliness. It is easily
441 possible to set up loops over file blocks that add syms
442 without end. */
443 if (filesize / (sizeof (struct vms_rfa) + 2) <= -1u)
444 csm.limit = filesize / (sizeof (struct vms_rfa) + 2);
445 }
446 if (csm.max > csm.limit)
447 csm.max = csm.limit;
448 if (_bfd_mul_overflow (csm.max, sizeof (struct carsym), &amt))
449 return NULL;
450 csm.idx = bfd_alloc (abfd, amt);
451 if (csm.idx == NULL)
452 return NULL;
453
454 /* Note: if the index is empty, there is no block to traverse. */
455 vbn = bfd_getl32 (idd.vbn);
456 if (vbn != 0 && !vms_traverse_index (abfd, vbn, &csm))
457 {
458 if (csm.realloced && csm.idx != NULL)
459 free (csm.idx);
460
461 /* Note: in case of error, we can free what was allocated on the
462 BFD's objalloc. */
463 bfd_release (abfd, csm.idx);
464 return NULL;
465 }
466
467 if (csm.realloced)
468 {
469 /* There are more entries than the first estimate. Allocate on
470 the BFD's objalloc. */
471 struct carsym *csbuf;
472 csbuf = bfd_alloc (abfd, csm.nbr * sizeof (struct carsym));
473 if (csbuf == NULL)
474 return NULL;
475 memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym));
476 free (csm.idx);
477 csm.idx = csbuf;
478 }
479 *nbrel = csm.nbr;
480 return csm.idx;
481 }
482
483 /* Standard function. */
484
485 static const bfd_target *
486 _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
487 {
488 struct vms_lhd lhd;
489 unsigned int sanity;
490 unsigned int majorid;
491 struct lib_tdata *tdata_hold;
492 struct lib_tdata *tdata;
493 unsigned int dcxvbn;
494 unsigned int nbr_ent;
495
496 /* Read header. */
497 if (bfd_bread (&lhd, sizeof (lhd), abfd) != sizeof (lhd))
498 {
499 if (bfd_get_error () != bfd_error_system_call)
500 bfd_set_error (bfd_error_wrong_format);
501 return NULL;
502 }
503
504 /* Check sanity (= magic) number. */
505 sanity = bfd_getl32 (lhd.sanity);
506 if (!(sanity == LHD_SANEID3
507 || sanity == LHD_SANEID6
508 || sanity == LHD_SANEID_DCX))
509 {
510 bfd_set_error (bfd_error_wrong_format);
511 return NULL;
512 }
513 majorid = bfd_getl32 (lhd.majorid);
514
515 /* Check archive kind. */
516 switch (kind)
517 {
518 case vms_lib_alpha:
519 if ((lhd.type != LBR__C_TYP_EOBJ && lhd.type != LBR__C_TYP_ESHSTB)
520 || majorid != LBR_MAJORID
521 || lhd.nindex != 2)
522 {
523 bfd_set_error (bfd_error_wrong_format);
524 return NULL;
525 }
526 break;
527 case vms_lib_ia64:
528 if ((lhd.type != LBR__C_TYP_IOBJ && lhd.type != LBR__C_TYP_ISHSTB)
529 || majorid != LBR_ELFMAJORID
530 || lhd.nindex != 2)
531 {
532 bfd_set_error (bfd_error_wrong_format);
533 return NULL;
534 }
535 break;
536 case vms_lib_txt:
537 if ((lhd.type != LBR__C_TYP_TXT
538 && lhd.type != LBR__C_TYP_MLB
539 && lhd.type != LBR__C_TYP_HLP)
540 || majorid != LBR_MAJORID
541 || lhd.nindex != 1)
542 {
543 bfd_set_error (bfd_error_wrong_format);
544 return NULL;
545 }
546 break;
547 default:
548 abort ();
549 }
550
551 /* Allocate and initialize private data. */
552 tdata_hold = bfd_libdata (abfd);
553 tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
554 if (tdata == NULL)
555 return NULL;
556 abfd->tdata.any = (void *)tdata;
557 tdata->ver = majorid;
558 tdata->mhd_size = MHD__C_USRDAT + lhd.mhdusz;
559 tdata->type = lhd.type;
560 tdata->kind = kind;
561 tdata->credat_lo = bfd_getl32 (lhd.credat + 0);
562 tdata->credat_hi = bfd_getl32 (lhd.credat + 4);
563
564 /* Read indexes. */
565 tdata->nbr_modules = bfd_getl32 (lhd.modcnt);
566 tdata->artdata.symdef_count = bfd_getl32 (lhd.idxcnt) - tdata->nbr_modules;
567 nbr_ent = tdata->nbr_modules;
568 tdata->modules = vms_lib_read_index (abfd, 0, &nbr_ent);
569 if (tdata->modules == NULL || nbr_ent != tdata->nbr_modules)
570 goto err;
571 if (lhd.nindex == 2)
572 {
573 nbr_ent = tdata->artdata.symdef_count;
574 tdata->artdata.symdefs = vms_lib_read_index (abfd, 1, &nbr_ent);
575 if (tdata->artdata.symdefs == NULL)
576 goto err;
577 /* Only IA64 archives may have more entries in the index that what
578 was declared. */
579 if (nbr_ent != tdata->artdata.symdef_count
580 && kind != vms_lib_ia64)
581 goto err;
582 tdata->artdata.symdef_count = nbr_ent;
583 }
584 tdata->cache = bfd_zalloc (abfd, sizeof (bfd *) * tdata->nbr_modules);
585 if (tdata->cache == NULL)
586 goto err;
587
588 /* Read DCX submaps. */
589 dcxvbn = bfd_getl32 (lhd.dcxmapvbn);
590 if (dcxvbn != 0)
591 {
592 unsigned char buf_reclen[4];
593 unsigned int reclen;
594 unsigned char *buf;
595 struct vms_dcxmap *map;
596 unsigned int sbm_off;
597 unsigned int i;
598
599 if (bfd_seek (abfd, (dcxvbn - 1) * VMS_BLOCK_SIZE, SEEK_SET) != 0
600 || bfd_bread (buf_reclen, sizeof (buf_reclen), abfd)
601 != sizeof (buf_reclen))
602 goto err;
603 reclen = bfd_getl32 (buf_reclen);
604 if (reclen < sizeof (struct vms_dcxmap))
605 goto err;
606 buf = _bfd_malloc_and_read (abfd, reclen, reclen);
607 if (buf == NULL)
608 goto err;
609 map = (struct vms_dcxmap *)buf;
610 tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs);
611 sbm_off = bfd_getl16 (map->sub0);
612 tdata->dcxsbm = (struct dcxsbm_desc *)bfd_alloc
613 (abfd, tdata->nbr_dcxsbm * sizeof (struct dcxsbm_desc));
614 for (i = 0; i < tdata->nbr_dcxsbm; i++)
615 {
616 struct vms_dcxsbm *sbm;
617 struct dcxsbm_desc *sbmdesc = &tdata->dcxsbm[i];
618 unsigned int sbm_len;
619 unsigned int sbm_sz;
620 unsigned int off;
621 unsigned char *buf1;
622 unsigned int l, j;
623
624 if (sbm_off > reclen
625 || reclen - sbm_off < sizeof (struct vms_dcxsbm))
626 goto err;
627 sbm = (struct vms_dcxsbm *) (buf + sbm_off);
628 sbm_sz = bfd_getl16 (sbm->size);
629 sbm_off += sbm_sz;
630
631 sbmdesc->min_char = sbm->min_char;
632 BFD_ASSERT (sbmdesc->min_char == 0);
633 sbmdesc->max_char = sbm->max_char;
634 sbm_len = sbmdesc->max_char - sbmdesc->min_char + 1;
635 l = (2 * sbm_len + 7) / 8;
636 if (sbm_sz < sizeof (struct vms_dcxsbm) + l + sbm_len
637 || (tdata->nbr_dcxsbm > 1
638 && sbm_sz < sizeof (struct vms_dcxsbm) + l + 3 * sbm_len))
639 goto err;
640 sbmdesc->flags = (unsigned char *)bfd_alloc (abfd, l);
641 off = bfd_getl16 (sbm->flags);
642 if (off > reclen - sbm_off
643 || reclen - sbm_off - off < l)
644 goto err;
645 memcpy (sbmdesc->flags, (bfd_byte *) sbm + off, l);
646 sbmdesc->nodes = (unsigned char *)bfd_alloc (abfd, 2 * sbm_len);
647 off = bfd_getl16 (sbm->nodes);
648 if (off > reclen - sbm_off
649 || reclen - sbm_off - off < 2 * sbm_len)
650 goto err;
651 memcpy (sbmdesc->nodes, (bfd_byte *) sbm + off, 2 * sbm_len);
652 off = bfd_getl16 (sbm->next);
653 if (off != 0)
654 {
655 if (off > reclen - sbm_off
656 || reclen - sbm_off - off < 2 * sbm_len)
657 goto err;
658 /* Read the 'next' array. */
659 sbmdesc->next = (unsigned short *) bfd_alloc (abfd, 2 * sbm_len);
660 buf1 = (bfd_byte *) sbm + off;
661 for (j = 0; j < sbm_len; j++)
662 sbmdesc->next[j] = bfd_getl16 (buf1 + j * 2);
663 }
664 else
665 {
666 /* There is no next array if there is only one submap. */
667 BFD_ASSERT (tdata->nbr_dcxsbm == 1);
668 sbmdesc->next = NULL;
669 }
670 }
671 free (buf);
672 }
673 else
674 {
675 tdata->nbr_dcxsbm = 0;
676 }
677
678 /* The map is always present. Also mark shared image library. */
679 abfd->has_armap = TRUE;
680 if (tdata->type == LBR__C_TYP_ESHSTB || tdata->type == LBR__C_TYP_ISHSTB)
681 abfd->is_thin_archive = TRUE;
682
683 return abfd->xvec;
684
685 err:
686 bfd_release (abfd, tdata);
687 abfd->tdata.any = (void *)tdata_hold;
688 return NULL;
689 }
690
691 /* Standard function for alpha libraries. */
692
693 const bfd_target *
694 _bfd_vms_lib_alpha_archive_p (bfd *abfd)
695 {
696 return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
697 }
698
699 /* Standard function for ia64 libraries. */
700
701 const bfd_target *
702 _bfd_vms_lib_ia64_archive_p (bfd *abfd)
703 {
704 return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
705 }
706
707 /* Standard function for text libraries. */
708
709 static const bfd_target *
710 _bfd_vms_lib_txt_archive_p (bfd *abfd)
711 {
712 return _bfd_vms_lib_archive_p (abfd, vms_lib_txt);
713 }
714
715 /* Standard bfd function. */
716
717 static bfd_boolean
718 _bfd_vms_lib_mkarchive (bfd *abfd, enum vms_lib_kind kind)
719 {
720 struct lib_tdata *tdata;
721
722 tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
723 if (tdata == NULL)
724 return FALSE;
725
726 abfd->tdata.any = (void *)tdata;
727 vms_get_time (&tdata->credat_hi, &tdata->credat_lo);
728
729 tdata->kind = kind;
730 switch (kind)
731 {
732 case vms_lib_alpha:
733 tdata->ver = LBR_MAJORID;
734 tdata->mhd_size = offsetof (struct vms_mhd, pad1);
735 tdata->type = LBR__C_TYP_EOBJ;
736 break;
737 case vms_lib_ia64:
738 tdata->ver = LBR_ELFMAJORID;
739 tdata->mhd_size = sizeof (struct vms_mhd);
740 tdata->type = LBR__C_TYP_IOBJ;
741 break;
742 default:
743 abort ();
744 }
745
746 tdata->nbr_modules = 0;
747 tdata->artdata.symdef_count = 0;
748 tdata->modules = NULL;
749 tdata->artdata.symdefs = NULL;
750 tdata->cache = NULL;
751
752 return TRUE;
753 }
754
755 bfd_boolean
756 _bfd_vms_lib_alpha_mkarchive (bfd *abfd)
757 {
758 return _bfd_vms_lib_mkarchive (abfd, vms_lib_alpha);
759 }
760
761 bfd_boolean
762 _bfd_vms_lib_ia64_mkarchive (bfd *abfd)
763 {
764 return _bfd_vms_lib_mkarchive (abfd, vms_lib_ia64);
765 }
766
767 /* Find NAME in the symbol index. Return the index. */
768
769 symindex
770 _bfd_vms_lib_find_symbol (bfd *abfd, const char *name)
771 {
772 struct lib_tdata *tdata = bfd_libdata (abfd);
773 carsym *syms = tdata->artdata.symdefs;
774 int lo, hi;
775
776 /* Open-coded binary search for speed. */
777 lo = 0;
778 hi = tdata->artdata.symdef_count - 1;
779
780 while (lo <= hi)
781 {
782 int mid = lo + (hi - lo) / 2;
783 int diff;
784
785 diff = (char)(name[0] - syms[mid].name[0]);
786 if (diff == 0)
787 diff = strcmp (name, syms[mid].name);
788 if (diff == 0)
789 return mid;
790 else if (diff < 0)
791 hi = mid - 1;
792 else
793 lo = mid + 1;
794 }
795 return BFD_NO_MORE_SYMBOLS;
796 }
797
798 /* IO vector for archive member. Need that because members are not linearly
799 stored in archives. */
800
801 struct vms_lib_iovec
802 {
803 /* Current offset. */
804 ufile_ptr where;
805
806 /* Length of the module, when known. */
807 ufile_ptr file_len;
808
809 /* Current position in the record from bfd_bread point of view (ie, after
810 decompression). 0 means that no data byte have been read, -2 and -1
811 are reserved for the length word. */
812 int rec_pos;
813 #define REC_POS_NL -4
814 #define REC_POS_PAD -3
815 #define REC_POS_LEN0 -2
816 #define REC_POS_LEN1 -1
817
818 /* Record length. */
819 unsigned short rec_len;
820 /* Number of bytes to read in the current record. */
821 unsigned short rec_rem;
822 /* Offset of the next block. */
823 file_ptr next_block;
824 /* Current *data* offset in the data block. */
825 unsigned short blk_off;
826
827 /* Offset of the first block. Extracted from the index. */
828 file_ptr first_block;
829
830 /* Initial next_block. Extracted when the MHD is read. */
831 file_ptr init_next_block;
832 /* Initial blk_off, once the MHD is read. */
833 unsigned short init_blk_off;
834
835 /* Used to store any 3 byte record, which could be the EOF pattern. */
836 unsigned char pattern[4];
837
838 /* DCX. */
839 struct dcxsbm_desc *dcxsbms;
840 /* Current submap. */
841 struct dcxsbm_desc *dcx_sbm;
842 /* Current offset in the submap. */
843 unsigned int dcx_offset;
844 int dcx_pos;
845
846 /* Compressed buffer. */
847 unsigned char *dcx_buf;
848 /* Size of the buffer. Used to resize. */
849 unsigned int dcx_max;
850 /* Number of valid bytes in the buffer. */
851 unsigned int dcx_rlen;
852 };
853
854 /* Return the current position. */
855
856 static file_ptr
857 vms_lib_btell (struct bfd *abfd)
858 {
859 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
860 return vec->where;
861 }
862
863 /* Read the header of the next data block if all bytes of the current block
864 have been read. */
865
866 static bfd_boolean
867 vms_lib_read_block (struct bfd *abfd)
868 {
869 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
870
871 if (vec->blk_off == DATA__LENGTH)
872 {
873 unsigned char hdr[DATA__DATA];
874
875 /* Read next block. */
876 if (bfd_seek (abfd->my_archive, vec->next_block, SEEK_SET) != 0)
877 return FALSE;
878 if (bfd_bread (hdr, sizeof (hdr), abfd->my_archive) != sizeof (hdr))
879 return FALSE;
880 vec->next_block = (bfd_getl32 (hdr + 2) - 1) * VMS_BLOCK_SIZE;
881 vec->blk_off = sizeof (hdr);
882 }
883 return TRUE;
884 }
885
886 /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
887 not stored. Read linearly from the library, but handle blocks. This
888 function does not handle records nor EOF. */
889
890 static file_ptr
891 vms_lib_bread_raw (struct bfd *abfd, unsigned char *buf, file_ptr nbytes)
892 {
893 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
894 file_ptr res;
895
896 res = 0;
897 while (nbytes > 0)
898 {
899 unsigned int l;
900
901 /* Be sure the current data block is read. */
902 if (!vms_lib_read_block (abfd))
903 return -1;
904
905 /* Do not read past the data block, do not read more than requested. */
906 l = DATA__LENGTH - vec->blk_off;
907 if (l > nbytes)
908 l = nbytes;
909 if (l == 0)
910 return 0;
911 if (buf != NULL)
912 {
913 /* Really read into BUF. */
914 if (bfd_bread (buf, l, abfd->my_archive) != l)
915 return -1;
916 }
917 else
918 {
919 /* Make as if we are reading. */
920 if (bfd_seek (abfd->my_archive, l, SEEK_CUR) != 0)
921 return -1;
922 }
923
924 if (buf != NULL)
925 buf += l;
926 vec->blk_off += l;
927 nbytes -= l;
928 res += l;
929 }
930 return res;
931 }
932
933 /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
934
935 static file_ptr
936 vms_lib_dcx (struct vms_lib_iovec *vec, unsigned char *buf, file_ptr nbytes)
937 {
938 struct dcxsbm_desc *sbm;
939 unsigned int i;
940 unsigned int offset;
941 unsigned int j;
942 file_ptr res = 0;
943
944 /* The loop below expect to deliver at least one byte. */
945 if (nbytes == 0)
946 return 0;
947
948 /* Get the current state. */
949 sbm = vec->dcx_sbm;
950 offset = vec->dcx_offset;
951 j = vec->dcx_pos & 7;
952
953 for (i = vec->dcx_pos >> 3; i < vec->dcx_rlen; i++)
954 {
955 unsigned char b = vec->dcx_buf[i];
956
957 for (; j < 8; j++)
958 {
959 if (b & (1 << j))
960 offset++;
961 if (!(sbm->flags[offset >> 3] & (1 << (offset & 7))))
962 {
963 unsigned int n_offset = sbm->nodes[offset];
964 if (n_offset == 0)
965 {
966 /* End of buffer. Stay where we are. */
967 vec->dcx_pos = (i << 3) + j;
968 if (b & (1 << j))
969 offset--;
970 vec->dcx_offset = offset;
971 vec->dcx_sbm = sbm;
972 return res;
973 }
974 offset = 2 * n_offset;
975 }
976 else
977 {
978 unsigned char v = sbm->nodes[offset];
979
980 if (sbm->next != NULL)
981 sbm = vec->dcxsbms + sbm->next[v];
982 offset = 0;
983 res++;
984
985 if (buf)
986 {
987 *buf++ = v;
988 nbytes--;
989
990 if (nbytes == 0)
991 {
992 vec->dcx_pos = (i << 3) + j + 1;
993 vec->dcx_offset = offset;
994 vec->dcx_sbm = sbm;
995
996 return res;
997 }
998 }
999 }
1000 }
1001 j = 0;
1002 }
1003 return -1;
1004 }
1005
1006 /* Standard IOVEC function. */
1007
1008 static file_ptr
1009 vms_lib_bread (struct bfd *abfd, void *vbuf, file_ptr nbytes)
1010 {
1011 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1012 file_ptr res;
1013 file_ptr chunk;
1014 unsigned char *buf = (unsigned char *)vbuf;
1015
1016 /* Do not read past the end. */
1017 if (vec->where >= vec->file_len)
1018 return 0;
1019
1020 res = 0;
1021 while (nbytes > 0)
1022 {
1023 if (vec->rec_rem == 0)
1024 {
1025 unsigned char blen[2];
1026
1027 /* Read record length. */
1028 if (vms_lib_bread_raw (abfd, blen, sizeof (blen)) != sizeof (blen))
1029 return -1;
1030 vec->rec_len = bfd_getl16 (blen);
1031 if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
1032 {
1033 /* Discard record size and align byte. */
1034 vec->rec_pos = 0;
1035 vec->rec_rem = vec->rec_len;
1036 }
1037 else
1038 {
1039 /* Prepend record size. */
1040 vec->rec_pos = REC_POS_LEN0;
1041 vec->rec_rem = (vec->rec_len + 1) & ~1; /* With align byte. */
1042 }
1043 if (vec->rec_len == 3)
1044 {
1045 /* Possibly end of file. Check the pattern. */
1046 if (vms_lib_bread_raw (abfd, vec->pattern, 4) != 4)
1047 return -1;
1048 if (!memcmp (vec->pattern, eotdesc + 2, 3))
1049 {
1050 /* This is really an EOF. */
1051 vec->where += res;
1052 vec->file_len = vec->where;
1053 return res;
1054 }
1055 }
1056
1057 if (vec->dcxsbms != NULL)
1058 {
1059 /* This is a compressed member. */
1060 unsigned int len;
1061 file_ptr elen;
1062
1063 /* Be sure there is enough room for the expansion. */
1064 len = (vec->rec_len + 1) & ~1;
1065 if (len > vec->dcx_max)
1066 {
1067 while (len > vec->dcx_max)
1068 vec->dcx_max *= 2;
1069 vec->dcx_buf = bfd_alloc (abfd, vec->dcx_max);
1070 if (vec->dcx_buf == NULL)
1071 return -1;
1072 }
1073
1074 /* Read the compressed record. */
1075 vec->dcx_rlen = len;
1076 if (vec->rec_len == 3)
1077 {
1078 /* Already read. */
1079 memcpy (vec->dcx_buf, vec->pattern, 3);
1080 }
1081 else
1082 {
1083 elen = vms_lib_bread_raw (abfd, vec->dcx_buf, len);
1084 if (elen != len)
1085 return -1;
1086 }
1087
1088 /* Dummy expansion to get the expanded length. */
1089 vec->dcx_offset = 0;
1090 vec->dcx_sbm = vec->dcxsbms;
1091 vec->dcx_pos = 0;
1092 elen = vms_lib_dcx (vec, NULL, 0x10000);
1093 if (elen < 0)
1094 return -1;
1095 vec->rec_len = elen;
1096 vec->rec_rem = elen;
1097
1098 /* Reset the state. */
1099 vec->dcx_offset = 0;
1100 vec->dcx_sbm = vec->dcxsbms;
1101 vec->dcx_pos = 0;
1102 }
1103 }
1104 if (vec->rec_pos < 0)
1105 {
1106 unsigned char c;
1107 switch (vec->rec_pos)
1108 {
1109 case REC_POS_LEN0:
1110 c = vec->rec_len & 0xff;
1111 vec->rec_pos = REC_POS_LEN1;
1112 break;
1113 case REC_POS_LEN1:
1114 c = (vec->rec_len >> 8) & 0xff;
1115 vec->rec_pos = 0;
1116 break;
1117 case REC_POS_PAD:
1118 c = 0;
1119 vec->rec_rem = 0;
1120 break;
1121 case REC_POS_NL:
1122 c = '\n';
1123 vec->rec_rem = 0;
1124 break;
1125 default:
1126 abort ();
1127 }
1128 if (buf != NULL)
1129 {
1130 *buf = c;
1131 buf++;
1132 }
1133 nbytes--;
1134 res++;
1135 continue;
1136 }
1137
1138 if (nbytes > vec->rec_rem)
1139 chunk = vec->rec_rem;
1140 else
1141 chunk = nbytes;
1142
1143 if (vec->dcxsbms != NULL)
1144 {
1145 /* Optimize the stat() case: no need to decompress again as we
1146 know the length. */
1147 if (!(buf == NULL && chunk == vec->rec_rem))
1148 chunk = vms_lib_dcx (vec, buf, chunk);
1149 }
1150 else
1151 {
1152 if (vec->rec_len == 3)
1153 {
1154 if (buf != NULL)
1155 memcpy (buf, vec->pattern + vec->rec_pos, chunk);
1156 }
1157 else
1158 chunk = vms_lib_bread_raw (abfd, buf, chunk);
1159 }
1160 if (chunk < 0)
1161 return -1;
1162 res += chunk;
1163 if (buf != NULL)
1164 buf += chunk;
1165 nbytes -= chunk;
1166 vec->rec_pos += chunk;
1167 vec->rec_rem -= chunk;
1168
1169 if (vec->rec_rem == 0)
1170 {
1171 /* End of record reached. */
1172 if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
1173 {
1174 if ((vec->rec_len & 1) == 1
1175 && vec->rec_len != 3
1176 && vec->dcxsbms == NULL)
1177 {
1178 /* Eat the pad byte. */
1179 unsigned char pad;
1180 if (vms_lib_bread_raw (abfd, &pad, 1) != 1)
1181 return -1;
1182 }
1183 vec->rec_pos = REC_POS_NL;
1184 vec->rec_rem = 1;
1185 }
1186 else
1187 {
1188 if ((vec->rec_len & 1) == 1 && vec->dcxsbms != NULL)
1189 {
1190 vec->rec_pos = REC_POS_PAD;
1191 vec->rec_rem = 1;
1192 }
1193 }
1194 }
1195 }
1196 vec->where += res;
1197 return res;
1198 }
1199
1200 /* Standard function, but we currently only handle the rewind case. */
1201
1202 static int
1203 vms_lib_bseek (struct bfd *abfd, file_ptr offset, int whence)
1204 {
1205 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1206
1207 if (whence == SEEK_SET && offset == 0)
1208 {
1209 vec->where = 0;
1210 vec->rec_rem = 0;
1211 vec->dcx_pos = -1;
1212 vec->blk_off = vec->init_blk_off;
1213 vec->next_block = vec->init_next_block;
1214
1215 if (bfd_seek (abfd->my_archive, vec->first_block, SEEK_SET) != 0)
1216 return -1;
1217 }
1218 else
1219 abort ();
1220 return 0;
1221 }
1222
1223 static file_ptr
1224 vms_lib_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED,
1225 const void *where ATTRIBUTE_UNUSED,
1226 file_ptr nbytes ATTRIBUTE_UNUSED)
1227 {
1228 return -1;
1229 }
1230
1231 static int
1232 vms_lib_bclose (struct bfd *abfd)
1233 {
1234 abfd->iostream = NULL;
1235 return 0;
1236 }
1237
1238 static int
1239 vms_lib_bflush (struct bfd *abfd ATTRIBUTE_UNUSED)
1240 {
1241 return 0;
1242 }
1243
1244 static int
1245 vms_lib_bstat (struct bfd *abfd ATTRIBUTE_UNUSED,
1246 struct stat *sb ATTRIBUTE_UNUSED)
1247 {
1248 /* Not supported. */
1249 return 0;
1250 }
1251
1252 static void *
1253 vms_lib_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
1254 void *addr ATTRIBUTE_UNUSED,
1255 bfd_size_type len ATTRIBUTE_UNUSED,
1256 int prot ATTRIBUTE_UNUSED,
1257 int flags ATTRIBUTE_UNUSED,
1258 file_ptr offset ATTRIBUTE_UNUSED,
1259 void **map_addr ATTRIBUTE_UNUSED,
1260 bfd_size_type *map_len ATTRIBUTE_UNUSED)
1261 {
1262 return (void *) -1;
1263 }
1264
1265 static const struct bfd_iovec vms_lib_iovec = {
1266 &vms_lib_bread, &vms_lib_bwrite, &vms_lib_btell, &vms_lib_bseek,
1267 &vms_lib_bclose, &vms_lib_bflush, &vms_lib_bstat, &vms_lib_bmmap
1268 };
1269
1270 /* Open a library module. FILEPOS is the position of the module header. */
1271
1272 static bfd_boolean
1273 vms_lib_bopen (bfd *el, file_ptr filepos)
1274 {
1275 struct vms_lib_iovec *vec;
1276 unsigned char buf[256];
1277 struct vms_mhd *mhd;
1278 struct lib_tdata *tdata = bfd_libdata (el->my_archive);
1279 unsigned int len;
1280
1281 /* Allocate and initialized the iovec. */
1282 vec = bfd_zalloc (el, sizeof (*vec));
1283 if (vec == NULL)
1284 return FALSE;
1285
1286 el->iostream = vec;
1287 el->iovec = &vms_lib_iovec;
1288
1289 /* File length is not known. */
1290 vec->file_len = -1;
1291
1292 /* Read the first data block. */
1293 vec->next_block = filepos & ~(VMS_BLOCK_SIZE - 1);
1294 vec->blk_off = DATA__LENGTH;
1295 if (!vms_lib_read_block (el))
1296 return FALSE;
1297
1298 /* Prepare to read the first record. */
1299 vec->blk_off = filepos & (VMS_BLOCK_SIZE - 1);
1300 vec->rec_rem = 0;
1301 if (bfd_seek (el->my_archive, filepos, SEEK_SET) != 0)
1302 return FALSE;
1303
1304 /* Read Record length + MHD + align byte. */
1305 len = tdata->mhd_size;
1306 if (vms_lib_bread_raw (el, buf, 2) != 2)
1307 return FALSE;
1308 if (bfd_getl16 (buf) != len)
1309 return FALSE;
1310 len = (len + 1) & ~1;
1311 BFD_ASSERT (len <= sizeof (buf));
1312 if (vms_lib_bread_raw (el, buf, len) != len)
1313 return FALSE;
1314
1315 /* Get info from mhd. */
1316 mhd = (struct vms_mhd *)buf;
1317 /* Check id. */
1318 if (mhd->id != MHD__C_MHDID)
1319 return FALSE;
1320 if (len >= MHD__C_MHDLEN + 1)
1321 el->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
1322 el->mtime = vms_rawtime_to_time_t (mhd->datim);
1323 el->mtime_set = TRUE;
1324
1325 /* Reinit the iovec so that seek() will point to the first record after
1326 the mhd. */
1327 vec->where = 0;
1328 vec->init_blk_off = vec->blk_off;
1329 vec->init_next_block = vec->next_block;
1330 vec->first_block = bfd_tell (el->my_archive);
1331 vec->dcxsbms = bfd_libdata (el->my_archive)->dcxsbm;
1332
1333 if (vec->dcxsbms != NULL)
1334 {
1335 /* Handle DCX. */
1336 vec->dcx_max = 10 * 1024;
1337 vec->dcx_buf = bfd_alloc (el, vec->dcx_max);
1338 vec->dcx_pos = -1;
1339 if (vec->dcx_buf == NULL)
1340 return -1;
1341 }
1342 return TRUE;
1343 }
1344
1345 /* Get member MODIDX. Return NULL in case of error. */
1346
1347 static bfd *
1348 _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
1349 {
1350 struct lib_tdata *tdata = bfd_libdata (abfd);
1351 bfd *res;
1352 file_ptr file_off;
1353 const char *name;
1354 char *newname;
1355 size_t namelen;
1356
1357 /* Sanity check. */
1358 if (modidx >= tdata->nbr_modules)
1359 return NULL;
1360
1361 /* Already loaded. */
1362 if (tdata->cache[modidx])
1363 return tdata->cache[modidx];
1364
1365 /* Build it. */
1366 file_off = tdata->modules[modidx].file_offset;
1367 if (tdata->type != LBR__C_TYP_IOBJ)
1368 {
1369 res = _bfd_create_empty_archive_element_shell (abfd);
1370 if (res == NULL)
1371 return NULL;
1372
1373 /* Special reader to deal with data blocks. */
1374 if (!vms_lib_bopen (res, file_off))
1375 return NULL;
1376 }
1377 else
1378 {
1379 char buf[256];
1380 struct vms_mhd *mhd;
1381 struct areltdata *arelt;
1382
1383 /* Sanity check. The MHD must be big enough to contain module size. */
1384 if (tdata->mhd_size < offsetof (struct vms_mhd, modsize) + 4)
1385 return NULL;
1386
1387 /* Read the MHD now. */
1388 if (bfd_seek (abfd, file_off, SEEK_SET) != 0)
1389 return NULL;
1390 if (bfd_bread (buf, tdata->mhd_size, abfd) != tdata->mhd_size)
1391 return NULL;
1392
1393 mhd = (struct vms_mhd *) buf;
1394 if (mhd->id != MHD__C_MHDID)
1395 return NULL;
1396
1397 res = _bfd_create_empty_archive_element_shell (abfd);
1398 if (res == NULL)
1399 return NULL;
1400 arelt = bfd_zmalloc (sizeof (*arelt));
1401 if (arelt == NULL)
1402 {
1403 bfd_close (res);
1404 return NULL;
1405 }
1406 res->arelt_data = arelt;
1407
1408 /* Get info from mhd. */
1409 if (tdata->mhd_size >= offsetof (struct vms_mhd, objstat) + 1)
1410 res->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
1411 res->mtime = vms_rawtime_to_time_t (mhd->datim);
1412 res->mtime_set = TRUE;
1413
1414 arelt->parsed_size = bfd_getl32 (mhd->modsize);
1415
1416 /* No need for a special reader as members are stored linearly.
1417 Just skip the MHD. */
1418 res->origin = file_off + tdata->mhd_size;
1419 }
1420
1421 /* Set filename. */
1422 name = tdata->modules[modidx].name;
1423 namelen = strlen (name);
1424 newname = bfd_malloc (namelen + 4 + 1);
1425 if (newname == NULL)
1426 {
1427 bfd_close (res);
1428 return NULL;
1429 }
1430 strcpy (newname, name);
1431 switch (tdata->type)
1432 {
1433 case LBR__C_TYP_IOBJ:
1434 case LBR__C_TYP_EOBJ:
1435 /* For object archives, append .obj to mimic standard behaviour. */
1436 strcpy (newname + namelen, ".obj");
1437 break;
1438 default:
1439 break;
1440 }
1441 bfd_set_filename (res, newname);
1442
1443 tdata->cache[modidx] = res;
1444
1445 return res;
1446 }
1447
1448 /* Standard function: get member at IDX. */
1449
1450 bfd *
1451 _bfd_vms_lib_get_elt_at_index (bfd *abfd, symindex symidx)
1452 {
1453 struct lib_tdata *tdata = bfd_libdata (abfd);
1454 file_ptr file_off;
1455 unsigned int modidx;
1456
1457 /* Check symidx. */
1458 if (symidx > tdata->artdata.symdef_count)
1459 return NULL;
1460 file_off = tdata->artdata.symdefs[symidx].file_offset;
1461
1462 /* Linear-scan. */
1463 for (modidx = 0; modidx < tdata->nbr_modules; modidx++)
1464 {
1465 if (tdata->modules[modidx].file_offset == file_off)
1466 break;
1467 }
1468 if (modidx >= tdata->nbr_modules)
1469 return NULL;
1470
1471 return _bfd_vms_lib_get_module (abfd, modidx);
1472 }
1473
1474 /* Elements of an imagelib are stubs. You can get the real image with this
1475 function. */
1476
1477 bfd *
1478 _bfd_vms_lib_get_imagelib_file (bfd *el)
1479 {
1480 bfd *archive = el->my_archive;
1481 const char *modname = el->filename;
1482 int modlen = strlen (modname);
1483 char *filename;
1484 int j;
1485 bfd *res;
1486
1487 /* Convert module name to lower case and append '.exe'. */
1488 filename = bfd_alloc (el, modlen + 5);
1489 if (filename == NULL)
1490 return NULL;
1491 for (j = 0; j < modlen; j++)
1492 if (ISALPHA (modname[j]))
1493 filename[j] = TOLOWER (modname[j]);
1494 else
1495 filename[j] = modname[j];
1496 memcpy (filename + modlen, ".exe", 5);
1497
1498 filename = _bfd_append_relative_path (archive, filename);
1499 if (filename == NULL)
1500 return NULL;
1501 res = bfd_openr (filename, NULL);
1502
1503 if (res == NULL)
1504 {
1505 /* xgettext:c-format */
1506 _bfd_error_handler(_("could not open shared image '%s' from '%s'"),
1507 filename, archive->filename);
1508 bfd_release (archive, filename);
1509 return NULL;
1510 }
1511
1512 /* FIXME: put it in a cache ? */
1513 return res;
1514 }
1515
1516 /* Standard function. */
1517
1518 bfd *
1519 _bfd_vms_lib_openr_next_archived_file (bfd *archive,
1520 bfd *last_file)
1521 {
1522 unsigned int idx;
1523 bfd *res;
1524
1525 if (!last_file)
1526 idx = 0;
1527 else
1528 idx = last_file->proxy_origin + 1;
1529
1530 if (idx >= bfd_libdata (archive)->nbr_modules)
1531 {
1532 bfd_set_error (bfd_error_no_more_archived_files);
1533 return NULL;
1534 }
1535
1536 res = _bfd_vms_lib_get_module (archive, idx);
1537 if (res == NULL)
1538 return res;
1539 res->proxy_origin = idx;
1540 return res;
1541 }
1542
1543 /* Standard function. Just compute the length. */
1544
1545 int
1546 _bfd_vms_lib_generic_stat_arch_elt (bfd *abfd, struct stat *st)
1547 {
1548 struct lib_tdata *tdata;
1549
1550 /* Sanity check. */
1551 if (abfd->my_archive == NULL)
1552 {
1553 bfd_set_error (bfd_error_invalid_operation);
1554 return -1;
1555 }
1556
1557 tdata = bfd_libdata (abfd->my_archive);
1558 if (tdata->type != LBR__C_TYP_IOBJ)
1559 {
1560 struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
1561
1562 if (vec->file_len == (ufile_ptr)-1)
1563 {
1564 if (vms_lib_bseek (abfd, 0, SEEK_SET) != 0)
1565 return -1;
1566
1567 /* Compute length. */
1568 while (vms_lib_bread (abfd, NULL, 1 << 20) > 0)
1569 ;
1570 }
1571 st->st_size = vec->file_len;
1572 }
1573 else
1574 {
1575 st->st_size = ((struct areltdata *)abfd->arelt_data)->parsed_size;
1576 }
1577
1578 if (abfd->mtime_set)
1579 st->st_mtime = abfd->mtime;
1580 else
1581 st->st_mtime = 0;
1582 st->st_uid = 0;
1583 st->st_gid = 0;
1584 st->st_mode = 0644;
1585
1586 return 0;
1587 }
1588
1589 /* Internal representation of an index entry. */
1590
1591 struct lib_index
1592 {
1593 /* Corresponding archive member. */
1594 bfd *abfd;
1595
1596 /* Number of reference to this entry. */
1597 unsigned int ref;
1598
1599 /* Length of the key. */
1600 unsigned short namlen;
1601
1602 /* Key. */
1603 const char *name;
1604 };
1605
1606 /* Used to sort index entries. */
1607
1608 static int
1609 lib_index_cmp (const void *lv, const void *rv)
1610 {
1611 const struct lib_index *l = lv;
1612 const struct lib_index *r = rv;
1613
1614 return strcmp (l->name, r->name);
1615 }
1616
1617 /* Maximum number of index blocks level. */
1618
1619 #define MAX_LEVEL 10
1620
1621 /* Get the size of an index entry. */
1622
1623 static unsigned int
1624 get_idxlen (struct lib_index *idx, bfd_boolean is_elfidx)
1625 {
1626 if (is_elfidx)
1627 {
1628 /* 9 is the size of struct vms_elfidx without keyname. */
1629 if (idx->namlen > MAX_KEYLEN)
1630 return 9 + sizeof (struct vms_kbn);
1631 else
1632 return 9 + idx->namlen;
1633 }
1634 else
1635 {
1636 /* 7 is the size of struct vms_idx without keyname. */
1637 return 7 + idx->namlen;
1638 }
1639 }
1640
1641 /* Write the index composed by NBR symbols contained in IDX.
1642 VBN is the first vbn to be used, and will contain on return the last vbn.
1643 Can be called with ABFD set to NULL just to size the index.
1644 If not null, TOPVBN will be assigned to the vbn of the root index tree.
1645 IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
1646 Return TRUE on success. */
1647
1648 static bfd_boolean
1649 vms_write_index (bfd *abfd,
1650 struct lib_index *idx, unsigned int nbr, unsigned int *vbn,
1651 unsigned int *topvbn, bfd_boolean is_elfidx)
1652 {
1653 /* The index is organized as a tree. This function implements a naive
1654 algorithm to balance the tree: it fills the leaves, and create a new
1655 branch when all upper leaves and branches are full. We only keep in
1656 memory a path to the current leaf. */
1657 unsigned int i;
1658 int j;
1659 int level;
1660 /* Disk blocks for the current path. */
1661 struct vms_indexdef *rblk[MAX_LEVEL];
1662 /* Info on the current blocks. */
1663 struct idxblk
1664 {
1665 unsigned int vbn; /* VBN of the block. */
1666 /* The last entry is identified so that it could be copied to the
1667 parent block. */
1668 unsigned short len; /* Length up to the last entry. */
1669 unsigned short lastlen; /* Length of the last entry. */
1670 } blk[MAX_LEVEL];
1671
1672 /* The kbn blocks are used to store long symbol names. */
1673 unsigned int kbn_sz = 0; /* Number of bytes available in the kbn block. */
1674 unsigned int kbn_vbn = 0; /* VBN of the kbn block. */
1675 unsigned char *kbn_blk = NULL; /* Contents of the kbn block. */
1676
1677 if (nbr == 0)
1678 {
1679 /* No entries. Very easy to handle. */
1680 if (topvbn != NULL)
1681 *topvbn = 0;
1682 return TRUE;
1683 }
1684
1685 if (abfd == NULL)
1686 {
1687 /* Sort the index the first time this function is called. */
1688 qsort (idx, nbr, sizeof (struct lib_index), lib_index_cmp);
1689 }
1690
1691 /* Allocate first index block. */
1692 level = 1;
1693 if (abfd != NULL)
1694 rblk[0] = bfd_zmalloc (sizeof (struct vms_indexdef));
1695 blk[0].vbn = (*vbn)++;
1696 blk[0].len = 0;
1697 blk[0].lastlen = 0;
1698
1699 for (i = 0; i < nbr; i++, idx++)
1700 {
1701 unsigned int idxlen;
1702 int flush = 0;
1703 unsigned int key_vbn = 0;
1704 unsigned int key_off = 0;
1705
1706 idxlen = get_idxlen (idx, is_elfidx);
1707
1708 if (is_elfidx && idx->namlen > MAX_KEYLEN)
1709 {
1710 /* If the key (ie name) is too long, write it in the kbn block. */
1711 unsigned int kl = idx->namlen;
1712 unsigned int kl_chunk;
1713 const char *key = idx->name;
1714
1715 /* Write the key in the kbn, chunk after chunk. */
1716 do
1717 {
1718 if (kbn_sz < sizeof (struct vms_kbn))
1719 {
1720 /* Not enough room in the kbn block. */
1721 if (abfd != NULL)
1722 {
1723 /* Write it to the disk (if there is one). */
1724 if (kbn_vbn != 0)
1725 {
1726 if (!vms_write_block (abfd, kbn_vbn, kbn_blk))
1727 return FALSE;
1728 }
1729 else
1730 {
1731 kbn_blk = bfd_malloc (VMS_BLOCK_SIZE);
1732 if (kbn_blk == NULL)
1733 return FALSE;
1734 }
1735 *(unsigned short *)kbn_blk = 0;
1736 }
1737 /* Allocate a new block for the keys. */
1738 kbn_vbn = (*vbn)++;
1739 kbn_sz = VMS_BLOCK_SIZE - 2;
1740 }
1741 /* Size of the chunk written to the current key block. */
1742 if (kl + sizeof (struct vms_kbn) > kbn_sz)
1743 kl_chunk = kbn_sz - sizeof (struct vms_kbn);
1744 else
1745 kl_chunk = kl;
1746
1747 if (kbn_blk != NULL)
1748 {
1749 struct vms_kbn *kbn;
1750
1751 kbn = (struct vms_kbn *)(kbn_blk + VMS_BLOCK_SIZE - kbn_sz);
1752
1753 if (key_vbn == 0)
1754 {
1755 /* Save the rfa of the first chunk. */
1756 key_vbn = kbn_vbn;
1757 key_off = VMS_BLOCK_SIZE - kbn_sz;
1758 }
1759
1760 bfd_putl16 (kl_chunk, kbn->keylen);
1761 if (kl_chunk == kl)
1762 {
1763 /* No next chunk. */
1764 bfd_putl32 (0, kbn->rfa.vbn);
1765 bfd_putl16 (0, kbn->rfa.offset);
1766 }
1767 else
1768 {
1769 /* Next chunk will be at the start of the next block. */
1770 bfd_putl32 (*vbn, kbn->rfa.vbn);
1771 bfd_putl16 (2, kbn->rfa.offset);
1772 }
1773 memcpy ((char *)(kbn + 1), key, kl_chunk);
1774 key += kl_chunk;
1775 }
1776 kl -= kl_chunk;
1777 kl_chunk = (kl_chunk + 1) & ~1; /* Always align. */
1778 kbn_sz -= kl_chunk + sizeof (struct vms_kbn);
1779 }
1780 while (kl > 0);
1781 }
1782
1783 /* Check if a block might overflow. In this case we will flush this
1784 block and all the blocks below it. */
1785 for (j = 0; j < level; j++)
1786 if (blk[j].len + blk[j].lastlen + idxlen > INDEXDEF__BLKSIZ)
1787 flush = j + 1;
1788
1789 for (j = 0; j < level; j++)
1790 {
1791 if (j < flush)
1792 {
1793 /* There is not enough room to write the new entry in this
1794 block or in a parent block. */
1795
1796 if (j + 1 == level)
1797 {
1798 BFD_ASSERT (level < MAX_LEVEL);
1799
1800 /* Need to create a parent. */
1801 if (abfd != NULL)
1802 {
1803 rblk[level] = bfd_zmalloc (sizeof (struct vms_indexdef));
1804 bfd_putl32 (*vbn, rblk[j]->parent);
1805 }
1806 blk[level].vbn = (*vbn)++;
1807 blk[level].len = 0;
1808 blk[level].lastlen = blk[j].lastlen;
1809
1810 level++;
1811 }
1812
1813 /* Update parent block: write the last entry from the current
1814 block. */
1815 if (abfd != NULL)
1816 {
1817 struct vms_rfa *rfa;
1818
1819 /* Pointer to the last entry in parent block. */
1820 rfa = (struct vms_rfa *)(rblk[j + 1]->keys + blk[j + 1].len);
1821
1822 /* Copy the whole entry. */
1823 BFD_ASSERT (blk[j + 1].lastlen == blk[j].lastlen);
1824 memcpy (rfa, rblk[j]->keys + blk[j].len, blk[j].lastlen);
1825 /* Fix the entry (which in always the first field of an
1826 entry. */
1827 bfd_putl32 (blk[j].vbn, rfa->vbn);
1828 bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
1829 }
1830
1831 if (j + 1 == flush)
1832 {
1833 /* And allocate it. Do it only on the block that won't be
1834 flushed (so that the parent of the parent can be
1835 updated too). */
1836 blk[j + 1].len += blk[j + 1].lastlen;
1837 blk[j + 1].lastlen = 0;
1838 }
1839
1840 /* Write this block on the disk. */
1841 if (abfd != NULL)
1842 {
1843 bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
1844 if (!vms_write_block (abfd, blk[j].vbn, rblk[j]))
1845 return FALSE;
1846 }
1847
1848 /* Reset this block. */
1849 blk[j].len = 0;
1850 blk[j].lastlen = 0;
1851 blk[j].vbn = (*vbn)++;
1852 }
1853
1854 /* Append it to the block. */
1855 if (j == 0)
1856 {
1857 /* Keep the previous last entry. */
1858 blk[j].len += blk[j].lastlen;
1859
1860 if (abfd != NULL)
1861 {
1862 struct vms_rfa *rfa;
1863
1864 rfa = (struct vms_rfa *)(rblk[j]->keys + blk[j].len);
1865 bfd_putl32 ((idx->abfd->proxy_origin / VMS_BLOCK_SIZE) + 1,
1866 rfa->vbn);
1867 bfd_putl16
1868 ((idx->abfd->proxy_origin % VMS_BLOCK_SIZE)
1869 + (is_elfidx ? 0 : DATA__DATA),
1870 rfa->offset);
1871
1872 if (is_elfidx)
1873 {
1874 /* Use elfidx format. */
1875 struct vms_elfidx *en = (struct vms_elfidx *)rfa;
1876
1877 en->flags = 0;
1878 if (key_vbn != 0)
1879 {
1880 /* Long symbol name. */
1881 struct vms_kbn *k = (struct vms_kbn *)(en->keyname);
1882 bfd_putl16 (sizeof (struct vms_kbn), en->keylen);
1883 bfd_putl16 (idx->namlen, k->keylen);
1884 bfd_putl32 (key_vbn, k->rfa.vbn);
1885 bfd_putl16 (key_off, k->rfa.offset);
1886 en->flags |= ELFIDX__SYMESC;
1887 }
1888 else
1889 {
1890 bfd_putl16 (idx->namlen, en->keylen);
1891 memcpy (en->keyname, idx->name, idx->namlen);
1892 }
1893 }
1894 else
1895 {
1896 /* Use idx format. */
1897 struct vms_idx *en = (struct vms_idx *)rfa;
1898 en->keylen = idx->namlen;
1899 memcpy (en->keyname, idx->name, idx->namlen);
1900 }
1901 }
1902 }
1903 /* The last added key can now be the last one all blocks in the
1904 path. */
1905 blk[j].lastlen = idxlen;
1906 }
1907 }
1908
1909 /* Save VBN of the root. */
1910 if (topvbn != NULL)
1911 *topvbn = blk[level - 1].vbn;
1912
1913 if (abfd == NULL)
1914 return TRUE;
1915
1916 /* Flush. */
1917 for (j = 1; j < level; j++)
1918 {
1919 /* Update parent block: write the new entry. */
1920 unsigned char *en;
1921 unsigned char *par;
1922 struct vms_rfa *rfa;
1923
1924 en = rblk[j - 1]->keys + blk[j - 1].len;
1925 par = rblk[j]->keys + blk[j].len;
1926 BFD_ASSERT (blk[j].lastlen == blk[j - 1].lastlen);
1927 memcpy (par, en, blk[j - 1].lastlen);
1928 rfa = (struct vms_rfa *)par;
1929 bfd_putl32 (blk[j - 1].vbn, rfa->vbn);
1930 bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
1931 }
1932
1933 for (j = 0; j < level; j++)
1934 {
1935 /* Write this block on the disk. */
1936 bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
1937 if (!vms_write_block (abfd, blk[j].vbn, rblk[j]))
1938 return FALSE;
1939
1940 free (rblk[j]);
1941 }
1942
1943 /* Write the last kbn (if any). */
1944 if (kbn_vbn != 0)
1945 {
1946 if (!vms_write_block (abfd, kbn_vbn, kbn_blk))
1947 return FALSE;
1948 free (kbn_blk);
1949 }
1950
1951 return TRUE;
1952 }
1953
1954 /* Append data to the data block DATA. Force write if PAD is true. */
1955
1956 static bfd_boolean
1957 vms_write_data_block (bfd *arch, struct vms_datadef *data, file_ptr *off,
1958 const unsigned char *buf, unsigned int len, int pad)
1959 {
1960 while (len > 0 || pad)
1961 {
1962 unsigned int doff = *off & (VMS_BLOCK_SIZE - 1);
1963 unsigned int remlen = (DATA__LENGTH - DATA__DATA) - doff;
1964 unsigned int l;
1965
1966 l = (len > remlen) ? remlen : len;
1967 memcpy (data->data + doff, buf, l);
1968 buf += l;
1969 len -= l;
1970 doff += l;
1971 *off += l;
1972
1973 if (doff == (DATA__LENGTH - DATA__DATA) || (len == 0 && pad))
1974 {
1975 data->recs = 0;
1976 data->fill_1 = 0;
1977 bfd_putl32 ((*off / VMS_BLOCK_SIZE) + 2, data->link);
1978
1979 if (bfd_bwrite (data, sizeof (*data), arch) != sizeof (*data))
1980 return FALSE;
1981
1982 *off += DATA__LENGTH - doff;
1983
1984 if (len == 0)
1985 break;
1986 }
1987 }
1988 return TRUE;
1989 }
1990
1991 /* Build the symbols index. */
1992
1993 static bfd_boolean
1994 _bfd_vms_lib_build_map (unsigned int nbr_modules,
1995 struct lib_index *modules,
1996 unsigned int *res_cnt,
1997 struct lib_index **res)
1998 {
1999 unsigned int i;
2000 asymbol **syms = NULL;
2001 long syms_max = 0;
2002 struct lib_index *map = NULL;
2003 unsigned int map_max = 1024; /* Fine initial default. */
2004 unsigned int map_count = 0;
2005
2006 map = (struct lib_index *) bfd_malloc (map_max * sizeof (struct lib_index));
2007 if (map == NULL)
2008 goto error_return;
2009
2010 /* Gather symbols. */
2011 for (i = 0; i < nbr_modules; i++)
2012 {
2013 long storage;
2014 long symcount;
2015 long src_count;
2016 bfd *current = modules[i].abfd;
2017
2018 if ((bfd_get_file_flags (current) & HAS_SYMS) == 0)
2019 continue;
2020
2021 storage = bfd_get_symtab_upper_bound (current);
2022 if (storage < 0)
2023 goto error_return;
2024
2025 if (storage != 0)
2026 {
2027 if (storage > syms_max)
2028 {
2029 if (syms_max > 0)
2030 free (syms);
2031 syms_max = storage;
2032 syms = (asymbol **) bfd_malloc (syms_max);
2033 if (syms == NULL)
2034 goto error_return;
2035 }
2036 symcount = bfd_canonicalize_symtab (current, syms);
2037 if (symcount < 0)
2038 goto error_return;
2039
2040 /* Now map over all the symbols, picking out the ones we
2041 want. */
2042 for (src_count = 0; src_count < symcount; src_count++)
2043 {
2044 flagword flags = (syms[src_count])->flags;
2045 asection *sec = syms[src_count]->section;
2046
2047 if ((flags & BSF_GLOBAL
2048 || flags & BSF_WEAK
2049 || flags & BSF_INDIRECT
2050 || bfd_is_com_section (sec))
2051 && ! bfd_is_und_section (sec))
2052 {
2053 struct lib_index *new_map;
2054
2055 /* This symbol will go into the archive header. */
2056 if (map_count == map_max)
2057 {
2058 map_max *= 2;
2059 new_map = (struct lib_index *)
2060 bfd_realloc (map, map_max * sizeof (struct lib_index));
2061 if (new_map == NULL)
2062 goto error_return;
2063 map = new_map;
2064 }
2065
2066 map[map_count].abfd = current;
2067 map[map_count].namlen = strlen (syms[src_count]->name);
2068 map[map_count].name = syms[src_count]->name;
2069 map_count++;
2070 modules[i].ref++;
2071 }
2072 }
2073 }
2074 }
2075
2076 *res_cnt = map_count;
2077 *res = map;
2078 return TRUE;
2079
2080 error_return:
2081 if (syms_max > 0)
2082 free (syms);
2083 if (map != NULL)
2084 free (map);
2085 return FALSE;
2086 }
2087
2088 /* Do the hard work: write an archive on the disk. */
2089
2090 bfd_boolean
2091 _bfd_vms_lib_write_archive_contents (bfd *arch)
2092 {
2093 bfd *current;
2094 unsigned int nbr_modules;
2095 struct lib_index *modules;
2096 unsigned int nbr_symbols;
2097 struct lib_index *symbols;
2098 struct lib_tdata *tdata = bfd_libdata (arch);
2099 unsigned int i;
2100 file_ptr off;
2101 unsigned int nbr_mod_iblk;
2102 unsigned int nbr_sym_iblk;
2103 unsigned int vbn;
2104 unsigned int mod_idx_vbn;
2105 unsigned int sym_idx_vbn;
2106 bfd_boolean is_elfidx = tdata->kind == vms_lib_ia64;
2107 unsigned int max_keylen = is_elfidx ? MAX_EKEYLEN : MAX_KEYLEN;
2108
2109 /* Count the number of modules (and do a first sanity check). */
2110 nbr_modules = 0;
2111 for (current = arch->archive_head;
2112 current != NULL;
2113 current = current->archive_next)
2114 {
2115 /* This check is checking the bfds for the objects we're reading
2116 from (which are usually either an object file or archive on
2117 disk), not the archive entries we're writing to. We don't
2118 actually create bfds for the archive members, we just copy
2119 them byte-wise when we write out the archive. */
2120 if (bfd_write_p (current) || !bfd_check_format (current, bfd_object))
2121 {
2122 bfd_set_error (bfd_error_invalid_operation);
2123 goto input_err;
2124 }
2125
2126 nbr_modules++;
2127 }
2128
2129 /* Build the modules list. */
2130 BFD_ASSERT (tdata->modules == NULL);
2131 modules = bfd_alloc (arch, nbr_modules * sizeof (struct lib_index));
2132 if (modules == NULL)
2133 return FALSE;
2134
2135 for (current = arch->archive_head, i = 0;
2136 current != NULL;
2137 current = current->archive_next, i++)
2138 {
2139 unsigned int nl;
2140
2141 modules[i].abfd = current;
2142 modules[i].name = vms_get_module_name (current->filename, FALSE);
2143 modules[i].ref = 1;
2144
2145 /* FIXME: silently truncate long names ? */
2146 nl = strlen (modules[i].name);
2147 modules[i].namlen = (nl > max_keylen ? max_keylen : nl);
2148 }
2149
2150 /* Create the module index. */
2151 vbn = 0;
2152 if (!vms_write_index (NULL, modules, nbr_modules, &vbn, NULL, is_elfidx))
2153 return FALSE;
2154 nbr_mod_iblk = vbn;
2155
2156 /* Create symbol index. */
2157 if (!_bfd_vms_lib_build_map (nbr_modules, modules, &nbr_symbols, &symbols))
2158 return FALSE;
2159
2160 vbn = 0;
2161 if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx))
2162 return FALSE;
2163 nbr_sym_iblk = vbn;
2164
2165 /* Write modules and remember their position. */
2166 off = (1 + nbr_mod_iblk + nbr_sym_iblk) * VMS_BLOCK_SIZE;
2167
2168 if (bfd_seek (arch, off, SEEK_SET) != 0)
2169 return FALSE;
2170
2171 for (i = 0; i < nbr_modules; i++)
2172 {
2173 struct vms_datadef data;
2174 unsigned char blk[VMS_BLOCK_SIZE];
2175 struct vms_mhd *mhd;
2176 unsigned int sz;
2177
2178 current = modules[i].abfd;
2179 current->proxy_origin = off;
2180
2181 if (is_elfidx)
2182 sz = 0;
2183 else
2184 {
2185 /* Write the MHD as a record (ie, size first). */
2186 sz = 2;
2187 bfd_putl16 (tdata->mhd_size, blk);
2188 }
2189 mhd = (struct vms_mhd *)(blk + sz);
2190 memset (mhd, 0, sizeof (struct vms_mhd));
2191 mhd->lbrflag = 0;
2192 mhd->id = MHD__C_MHDID;
2193 mhd->objidlng = 4;
2194 memcpy (mhd->objid, "V1.0", 4);
2195 bfd_putl32 (modules[i].ref, mhd->refcnt);
2196 /* FIXME: datim. */
2197
2198 sz += tdata->mhd_size;
2199 sz = (sz + 1) & ~1;
2200
2201 /* Rewind the member to be put into the archive. */
2202 if (bfd_seek (current, 0, SEEK_SET) != 0)
2203 goto input_err;
2204
2205 /* Copy the member into the archive. */
2206 if (is_elfidx)
2207 {
2208 unsigned int modsize = 0;
2209 bfd_size_type amt;
2210 file_ptr off_hdr = off;
2211
2212 /* Read to complete the first block. */
2213 amt = bfd_bread (blk + sz, VMS_BLOCK_SIZE - sz, current);
2214 if (amt == (bfd_size_type)-1)
2215 goto input_err;
2216 modsize = amt;
2217 if (amt < VMS_BLOCK_SIZE - sz)
2218 {
2219 /* The member size is less than a block. Pad the block. */
2220 memset (blk + sz + amt, 0, VMS_BLOCK_SIZE - sz - amt);
2221 }
2222 bfd_putl32 (modsize, mhd->modsize);
2223
2224 /* Write the first block (which contains an mhd). */
2225 if (bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
2226 goto input_err;
2227 off += VMS_BLOCK_SIZE;
2228
2229 if (amt == VMS_BLOCK_SIZE - sz)
2230 {
2231 /* Copy the remaining. */
2232 char buffer[DEFAULT_BUFFERSIZE];
2233
2234 while (1)
2235 {
2236 amt = bfd_bread (buffer, sizeof (buffer), current);
2237 if (amt == (bfd_size_type)-1)
2238 goto input_err;
2239 if (amt == 0)
2240 break;
2241 modsize += amt;
2242 if (amt != sizeof (buffer))
2243 {
2244 /* Clear the padding. */
2245 memset (buffer + amt, 0, sizeof (buffer) - amt);
2246 amt = (amt + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
2247 }
2248 if (bfd_bwrite (buffer, amt, arch) != amt)
2249 goto input_err;
2250 off += amt;
2251 }
2252
2253 /* Now that the size is known, write the first block (again). */
2254 bfd_putl32 (modsize, mhd->modsize);
2255 if (bfd_seek (arch, off_hdr, SEEK_SET) != 0
2256 || bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
2257 goto input_err;
2258 if (bfd_seek (arch, off, SEEK_SET) != 0)
2259 goto input_err;
2260 }
2261 }
2262 else
2263 {
2264 /* Write the MHD. */
2265 if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
2266 goto input_err;
2267
2268 /* Write the member. */
2269 while (1)
2270 {
2271 sz = bfd_bread (blk, sizeof (blk), current);
2272 if (sz == 0)
2273 break;
2274 if (vms_write_data_block (arch, &data, &off, blk, sz, 0) < 0)
2275 goto input_err;
2276 }
2277
2278 /* Write the end of module marker. */
2279 if (vms_write_data_block (arch, &data, &off,
2280 eotdesc, sizeof (eotdesc), 1) < 0)
2281 goto input_err;
2282 }
2283 }
2284
2285 /* Write the indexes. */
2286 vbn = 2;
2287 if (!vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn,
2288 is_elfidx))
2289 return FALSE;
2290 if (!vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn,
2291 is_elfidx))
2292 return FALSE;
2293
2294 /* Write libary header. */
2295 {
2296 unsigned char blk[VMS_BLOCK_SIZE];
2297 struct vms_lhd *lhd = (struct vms_lhd *)blk;
2298 struct vms_idd *idd = (struct vms_idd *)(blk + sizeof (*lhd));
2299 unsigned int idd_flags;
2300 unsigned int saneid;
2301
2302 memset (blk, 0, sizeof (blk));
2303
2304 lhd->type = tdata->type;
2305 lhd->nindex = 2;
2306 switch (tdata->kind)
2307 {
2308 case vms_lib_alpha:
2309 saneid = LHD_SANEID3;
2310 break;
2311 case vms_lib_ia64:
2312 saneid = LHD_SANEID6;
2313 break;
2314 default:
2315 abort ();
2316 }
2317 bfd_putl32 (saneid, lhd->sanity);
2318 bfd_putl16 (tdata->ver, lhd->majorid);
2319 bfd_putl16 (0, lhd->minorid);
2320 snprintf ((char *)lhd->lbrver + 1, sizeof (lhd->lbrver) - 1,
2321 "GNU ar %u.%u.%u",
2322 (unsigned)(BFD_VERSION / 100000000UL),
2323 (unsigned)(BFD_VERSION / 1000000UL) % 100,
2324 (unsigned)(BFD_VERSION / 10000UL) % 100);
2325 lhd->lbrver[sizeof (lhd->lbrver) - 1] = 0;
2326 lhd->lbrver[0] = strlen ((char *)lhd->lbrver + 1);
2327
2328 bfd_putl32 (tdata->credat_lo, lhd->credat + 0);
2329 bfd_putl32 (tdata->credat_hi, lhd->credat + 4);
2330 vms_raw_get_time (lhd->updtim);
2331
2332 lhd->mhdusz = tdata->mhd_size - MHD__C_USRDAT;
2333
2334 bfd_putl32 (nbr_modules + nbr_symbols, lhd->idxcnt);
2335 bfd_putl32 (nbr_modules, lhd->modcnt);
2336 bfd_putl32 (nbr_modules, lhd->modhdrs);
2337
2338 /* Number of blocks for index. */
2339 bfd_putl32 (nbr_mod_iblk + nbr_sym_iblk, lhd->idxblks);
2340 bfd_putl32 (vbn - 1, lhd->hipreal);
2341 bfd_putl32 (vbn - 1, lhd->hiprusd);
2342
2343 /* VBN of the next free block. */
2344 bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextvbn);
2345 bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextrfa + 0);
2346 bfd_putl16 (0, lhd->nextrfa + 4);
2347
2348 /* First index (modules name). */
2349 idd_flags = IDD__FLAGS_ASCII | IDD__FLAGS_VARLENIDX
2350 | IDD__FLAGS_NOCASECMP | IDD__FLAGS_NOCASENTR;
2351 bfd_putl16 (idd_flags, idd->flags);
2352 bfd_putl16 (max_keylen + 1, idd->keylen);
2353 bfd_putl16 (mod_idx_vbn, idd->vbn);
2354 idd++;
2355
2356 /* Second index (symbols name). */
2357 bfd_putl16 (idd_flags, idd->flags);
2358 bfd_putl16 (max_keylen + 1, idd->keylen);
2359 bfd_putl16 (sym_idx_vbn, idd->vbn);
2360 idd++;
2361
2362 if (!vms_write_block (arch, 1, blk))
2363 return FALSE;
2364 }
2365
2366 return TRUE;
2367
2368 input_err:
2369 bfd_set_input_error (current, bfd_get_error ());
2370 return FALSE;
2371 }
2372
2373 /* Add a target for text library. This costs almost nothing and is useful to
2374 read VMS library on the host. */
2375
2376 const bfd_target alpha_vms_lib_txt_vec =
2377 {
2378 "vms-libtxt", /* Name. */
2379 bfd_target_unknown_flavour,
2380 BFD_ENDIAN_UNKNOWN, /* byteorder */
2381 BFD_ENDIAN_UNKNOWN, /* header_byteorder */
2382 0, /* Object flags. */
2383 0, /* Sect flags. */
2384 0, /* symbol_leading_char. */
2385 ' ', /* ar_pad_char. */
2386 15, /* ar_max_namelen. */
2387 0, /* match priority. */
2388 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2389 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2390 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
2391 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2392 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2393 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
2394 { /* bfd_check_format. */
2395 _bfd_dummy_target,
2396 _bfd_dummy_target,
2397 _bfd_vms_lib_txt_archive_p,
2398 _bfd_dummy_target
2399 },
2400 { /* bfd_set_format. */
2401 _bfd_bool_bfd_false_error,
2402 _bfd_bool_bfd_false_error,
2403 _bfd_bool_bfd_false_error,
2404 _bfd_bool_bfd_false_error
2405 },
2406 { /* bfd_write_contents. */
2407 _bfd_bool_bfd_false_error,
2408 _bfd_bool_bfd_false_error,
2409 _bfd_bool_bfd_false_error,
2410 _bfd_bool_bfd_false_error
2411 },
2412 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
2413 BFD_JUMP_TABLE_COPY (_bfd_generic),
2414 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2415 BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib),
2416 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
2417 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
2418 BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
2419 BFD_JUMP_TABLE_LINK (_bfd_nolink),
2420 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2421
2422 NULL,
2423
2424 NULL
2425 };
This page took 0.11618 seconds and 4 git commands to generate.