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