forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvms-lib.c
2461 lines (2124 loc) · 60.4 KB
/
vms-lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BFD back-end for VMS archive files.
Copyright (C) 2010-2025 Free Software Foundation, Inc.
Written by Tristan Gingold <[email protected]>, AdaCore.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "safe-ctype.h"
#include "bfdver.h"
#include "libiberty.h"
#include "vms.h"
#include "vms/lbr.h"
#include "vms/dcx.h"
/* The standard VMS disk block size. */
#ifndef VMS_BLOCK_SIZE
#define VMS_BLOCK_SIZE 512
#endif
/* Maximum key length (which is also the maximum symbol length in archive). */
#define MAX_KEYLEN 128
#define MAX_EKEYLEN 1024
/* DCX Submaps. */
struct dcxsbm_desc
{
unsigned char min_char;
unsigned char max_char;
unsigned char *flags;
unsigned char *nodes;
unsigned short *next;
};
/* Kind of library. Used to filter in archive_p. */
enum vms_lib_kind
{
vms_lib_vax,
vms_lib_alpha,
vms_lib_ia64,
vms_lib_txt
};
/* Back-end private data. */
struct lib_tdata
{
/* Standard tdata for an archive. But we don't use many fields. */
struct artdata artdata;
/* Major version. */
unsigned char ver;
/* Type of the archive. */
unsigned char type;
/* Kind of archive. Summary of its type. */
enum vms_lib_kind kind;
/* Total size of the mhd (element header). */
unsigned int mhd_size;
/* Creation date. */
unsigned int credat_lo;
unsigned int credat_hi;
/* Vector of modules (archive elements), already sorted. */
unsigned int nbr_modules;
struct carsym *modules;
bfd **cache;
/* DCX (decompression) data. */
unsigned int nbr_dcxsbm;
struct dcxsbm_desc *dcxsbm;
};
#define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
/* End-Of-Text pattern. This is a special record to mark the end of file. */
static const unsigned char eotdesc[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
/* Describe the current state of carsym entries while building the archive
table of content. Things are simple with Alpha archives as the number
of entries is known, but with IA64 archives a entry can make a reference
to severals members. Therefore we must be able to extend the table on the
fly, but it should be allocated on the bfd - which doesn't support realloc.
To reduce the overhead, the table is initially allocated in the BFD's
objalloc and extended if necessary on the heap. In the later case, it
is finally copied to the BFD's objalloc so that it will automatically be
freed. */
struct carsym_mem
{
/* The table of content. */
struct carsym *idx;
/* Number of entries used in the table. */
unsigned int nbr;
/* Maximum number of entries. */
unsigned int max;
/* Do not allocate more that this number of entries. */
unsigned int limit;
/* If true, the table was reallocated on the heap. If false, it is still
in the BFD's objalloc. */
bool realloced;
};
/* Simply add a name to the index. */
static bool
vms_add_index (struct carsym_mem *cs, char *name,
unsigned int idx_vbn, unsigned int idx_off)
{
if (cs->nbr == cs->max)
{
struct carsym *n;
size_t amt;
if (cs->max > -33u / 2 || cs->max >= cs->limit)
{
bfd_set_error (bfd_error_file_too_big);
return false;
}
cs->max = 2 * cs->max + 32;
if (cs->max > cs->limit)
cs->max = cs->limit;
if (_bfd_mul_overflow (cs->max, sizeof (struct carsym), &amt))
{
bfd_set_error (bfd_error_file_too_big);
return false;
}
if (!cs->realloced)
{
n = bfd_malloc (amt);
if (n == NULL)
return false;
memcpy (n, cs->idx, cs->nbr * sizeof (struct carsym));
/* And unfortunately we can't free cs->idx. */
}
else
{
n = bfd_realloc_or_free (cs->idx, amt);
if (n == NULL)
return false;
}
cs->idx = n;
cs->realloced = true;
}
cs->idx[cs->nbr].file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
cs->idx[cs->nbr].name = name;
cs->nbr++;
return true;
}
/* Follow all member of a lns list (pointed by RFA) and add indexes for
NAME. Return FALSE in case of error. */
static bool
vms_add_indexes_from_list (bfd *abfd, struct carsym_mem *cs, char *name,
struct vms_rfa *rfa)
{
struct vms_lns lns;
unsigned int vbn;
file_ptr off;
while (1)
{
vbn = bfd_getl32 (rfa->vbn);
if (vbn == 0)
return true;
/* Read the LHS. */
off = (vbn - 1) * VMS_BLOCK_SIZE + bfd_getl16 (rfa->offset);
if (bfd_seek (abfd, off, SEEK_SET) != 0
|| bfd_read (&lns, sizeof (lns), abfd) != sizeof (lns))
return false;
if (!vms_add_index (cs, name,
bfd_getl32 (lns.modrfa.vbn),
bfd_getl16 (lns.modrfa.offset)))
return false;
rfa = &lns.nxtrfa;
}
}
/* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
static bool
vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
{
file_ptr off;
off = (vbn - 1) * VMS_BLOCK_SIZE;
if (bfd_seek (abfd, off, SEEK_SET) != 0
|| bfd_read (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
return false;
return true;
}
/* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
static bool
vms_write_block (bfd *abfd, unsigned int vbn, void *blk)
{
file_ptr off;
off = (vbn - 1) * VMS_BLOCK_SIZE;
if (bfd_seek (abfd, off, SEEK_SET) != 0
|| bfd_write (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
return false;
return true;
}
/* Read index block VBN and put the entry in **IDX (which is updated).
If the entry is indirect, recurse. */
static bool
vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs,
unsigned int recur_count)
{
struct vms_indexdef indexdef;
file_ptr off;
unsigned char *p;
unsigned char *endp;
unsigned int n;
if (recur_count == 100)
{
bfd_set_error (bfd_error_bad_value);
return false;
}
/* Read the index block. */
BFD_ASSERT (sizeof (indexdef) == VMS_BLOCK_SIZE);
if (!vms_read_block (abfd, vbn, &indexdef))
return false;
/* Traverse it. */
p = &indexdef.keys[0];
n = bfd_getl16 (indexdef.used);
if (n > sizeof (indexdef.keys))
return false;
endp = p + n;
while (p < endp)
{
unsigned int idx_vbn;
unsigned int idx_off;
unsigned int keylen;
unsigned char *keyname;
unsigned int flags;
/* Extract key length. */
if (bfd_libdata (abfd)->ver == LBR_MAJORID
&& offsetof (struct vms_idx, keyname) <= (size_t) (endp - p))
{
struct vms_idx *ridx = (struct vms_idx *)p;
idx_vbn = bfd_getl32 (ridx->rfa.vbn);
idx_off = bfd_getl16 (ridx->rfa.offset);
keylen = ridx->keylen;
flags = 0;
keyname = ridx->keyname;
}
else if (bfd_libdata (abfd)->ver == LBR_ELFMAJORID
&& offsetof (struct vms_elfidx, keyname) <= (size_t) (endp - p))
{
struct vms_elfidx *ridx = (struct vms_elfidx *)p;
idx_vbn = bfd_getl32 (ridx->rfa.vbn);
idx_off = bfd_getl16 (ridx->rfa.offset);
keylen = bfd_getl16 (ridx->keylen);
flags = ridx->flags;
keyname = ridx->keyname;
}
else
return false;
/* Illegal value. */
if (idx_vbn == 0)
return false;
/* Point to the next index entry. */
p = keyname + keylen;
if (p > endp)
return false;
if (idx_off == RFADEF__C_INDEX)
{
/* Indirect entry. Recurse. */
if (!vms_traverse_index (abfd, idx_vbn, cs, recur_count + 1))
return false;
}
else
{
/* Add a new entry. */
char *name;
if (flags & ELFIDX__SYMESC)
{
/* Extended key name. */
unsigned int noff = 0;
unsigned int koff;
unsigned int kvbn;
struct vms_kbn *kbn;
unsigned char kblk[VMS_BLOCK_SIZE];
/* Sanity check. */
if (keylen != sizeof (struct vms_kbn))
return false;
kbn = (struct vms_kbn *)keyname;
keylen = bfd_getl16 (kbn->keylen);
name = bfd_alloc (abfd, keylen + 1);
if (name == NULL)
return false;
kvbn = bfd_getl32 (kbn->rfa.vbn);
koff = bfd_getl16 (kbn->rfa.offset);
/* Read the key, chunk by chunk. */
do
{
unsigned int klen;
if (!vms_read_block (abfd, kvbn, kblk))
return false;
if (koff > sizeof (kblk) - sizeof (struct vms_kbn))
return false;
kbn = (struct vms_kbn *)(kblk + koff);
klen = bfd_getl16 (kbn->keylen);
if (klen > sizeof (kblk) - sizeof (struct vms_kbn) - koff)
return false;
kvbn = bfd_getl32 (kbn->rfa.vbn);
koff = bfd_getl16 (kbn->rfa.offset);
if (noff + klen > keylen)
return false;
memcpy (name + noff, kbn + 1, klen);
noff += klen;
}
while (kvbn != 0);
/* Sanity check. */
if (noff != keylen)
return false;
}
else
{
/* Usual key name. */
name = bfd_alloc (abfd, keylen + 1);
if (name == NULL)
return false;
memcpy (name, keyname, keylen);
}
name[keylen] = 0;
if (flags & ELFIDX__LISTRFA)
{
struct vms_lhs lhs;
/* Read the LHS. */
off = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
if (bfd_seek (abfd, off, SEEK_SET) != 0
|| bfd_read (&lhs, sizeof (lhs), abfd) != sizeof (lhs))
return false;
/* These extra entries may cause reallocation of CS. */
if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_g_rfa))
return false;
if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_wk_rfa))
return false;
if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_g_rfa))
return false;
if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_wk_rfa))
return false;
}
else
{
if (!vms_add_index (cs, name, idx_vbn, idx_off))
return false;
}
}
}
return true;
}
/* Read index #IDX, which must have NBREL entries. */
static struct carsym *
vms_lib_read_index (bfd *abfd, int idx, unsigned int *nbrel)
{
struct vms_idd idd;
unsigned int flags;
unsigned int vbn;
ufile_ptr filesize;
size_t amt;
struct carsym *csbuf;
struct carsym_mem csm;
/* Read index desription. */
if (bfd_seek (abfd, LHD_IDXDESC + idx * IDD_LENGTH, SEEK_SET) != 0
|| bfd_read (&idd, sizeof (idd), abfd) != sizeof (idd))
return NULL;
/* Sanity checks. */
flags = bfd_getl16 (idd.flags);
if (!(flags & IDD__FLAGS_ASCII)
|| !(flags & IDD__FLAGS_VARLENIDX))
return NULL;
filesize = bfd_get_file_size (abfd);
csm.nbr = 0;
csm.max = *nbrel;
csm.limit = -1u;
csm.realloced = false;
if (filesize != 0)
{
/* Put an upper bound based on a file full of single char keys.
This is to prevent fuzzed binary silliness. It is easily
possible to set up loops over file blocks that add syms
without end. */
if (filesize / (sizeof (struct vms_rfa) + 2) <= -1u)
csm.limit = filesize / (sizeof (struct vms_rfa) + 2);
}
if (csm.max > csm.limit)
csm.max = csm.limit;
if (_bfd_mul_overflow (csm.max, sizeof (struct carsym), &amt))
return NULL;
csm.idx = csbuf = bfd_alloc (abfd, amt);
if (csm.idx == NULL)
return NULL;
/* Note: if the index is empty, there is no block to traverse. */
vbn = bfd_getl32 (idd.vbn);
if (vbn != 0 && !vms_traverse_index (abfd, vbn, &csm, 0))
{
if (csm.realloced)
free (csm.idx);
/* Note: in case of error, we can free what was allocated on the
BFD's objalloc. */
bfd_release (abfd, csbuf);
return NULL;
}
if (csm.realloced)
{
/* There are more entries than the first estimate. Allocate on
the BFD's objalloc. */
csbuf = bfd_alloc (abfd, csm.nbr * sizeof (struct carsym));
if (csbuf == NULL)
{
free (csm.idx);
return NULL;
}
memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym));
free (csm.idx);
csm.idx = csbuf;
}
*nbrel = csm.nbr;
return csm.idx;
}
/* Standard function. */
static bfd_cleanup
_bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
{
struct vms_lhd lhd;
unsigned int sanity;
unsigned int majorid;
struct lib_tdata *tdata_hold;
struct lib_tdata *tdata;
unsigned int dcxvbn;
unsigned int nbr_ent;
/* Read header. */
if (bfd_read (&lhd, sizeof (lhd), abfd) != sizeof (lhd))
{
if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_wrong_format);
return NULL;
}
/* Check sanity (= magic) number. */
sanity = bfd_getl32 (lhd.sanity);
if (!(sanity == LHD_SANEID3
|| sanity == LHD_SANEID6
|| sanity == LHD_SANEID_DCX))
{
bfd_set_error (bfd_error_wrong_format);
return NULL;
}
majorid = bfd_getl32 (lhd.majorid);
/* Check archive kind. */
switch (kind)
{
case vms_lib_alpha:
if ((lhd.type != LBR__C_TYP_EOBJ && lhd.type != LBR__C_TYP_ESHSTB)
|| majorid != LBR_MAJORID
|| lhd.nindex != 2)
{
bfd_set_error (bfd_error_wrong_format);
return NULL;
}
break;
case vms_lib_ia64:
if ((lhd.type != LBR__C_TYP_IOBJ && lhd.type != LBR__C_TYP_ISHSTB)
|| majorid != LBR_ELFMAJORID
|| lhd.nindex != 2)
{
bfd_set_error (bfd_error_wrong_format);
return NULL;
}
break;
case vms_lib_txt:
if ((lhd.type != LBR__C_TYP_TXT
&& lhd.type != LBR__C_TYP_MLB
&& lhd.type != LBR__C_TYP_HLP)
|| majorid != LBR_MAJORID
|| lhd.nindex != 1)
{
bfd_set_error (bfd_error_wrong_format);
return NULL;
}
break;
default:
abort ();
}
/* Allocate and initialize private data. */
tdata_hold = bfd_libdata (abfd);
tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
if (tdata == NULL)
return NULL;
abfd->tdata.any = (void *)tdata;
tdata->ver = majorid;
tdata->mhd_size = MHD__C_USRDAT + lhd.mhdusz;
tdata->type = lhd.type;
tdata->kind = kind;
tdata->credat_lo = bfd_getl32 (lhd.credat + 0);
tdata->credat_hi = bfd_getl32 (lhd.credat + 4);
/* Read indexes. */
tdata->nbr_modules = bfd_getl32 (lhd.modcnt);
tdata->artdata.symdef_count = bfd_getl32 (lhd.idxcnt) - tdata->nbr_modules;
nbr_ent = tdata->nbr_modules;
tdata->modules = vms_lib_read_index (abfd, 0, &nbr_ent);
if (tdata->modules == NULL || nbr_ent != tdata->nbr_modules)
goto err;
if (lhd.nindex == 2)
{
nbr_ent = tdata->artdata.symdef_count;
tdata->artdata.symdefs = vms_lib_read_index (abfd, 1, &nbr_ent);
if (tdata->artdata.symdefs == NULL)
goto err;
/* Only IA64 archives may have more entries in the index that what
was declared. */
if (nbr_ent != tdata->artdata.symdef_count
&& kind != vms_lib_ia64)
goto err;
tdata->artdata.symdef_count = nbr_ent;
}
tdata->cache = bfd_zalloc (abfd, sizeof (bfd *) * tdata->nbr_modules);
if (tdata->cache == NULL)
goto err;
/* Read DCX submaps. */
dcxvbn = bfd_getl32 (lhd.dcxmapvbn);
if (dcxvbn != 0)
{
unsigned char buf_reclen[4];
unsigned int reclen;
unsigned char *buf;
struct vms_dcxmap *map;
unsigned int sbm_off;
unsigned int i;
if (bfd_seek (abfd, (dcxvbn - 1) * VMS_BLOCK_SIZE, SEEK_SET) != 0
|| bfd_read (buf_reclen, sizeof (buf_reclen), abfd)
!= sizeof (buf_reclen))
goto err;
reclen = bfd_getl32 (buf_reclen);
if (reclen < sizeof (struct vms_dcxmap))
goto err;
buf = _bfd_malloc_and_read (abfd, reclen, reclen);
if (buf == NULL)
goto err;
map = (struct vms_dcxmap *)buf;
tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs);
sbm_off = bfd_getl16 (map->sub0);
tdata->dcxsbm = (struct dcxsbm_desc *)bfd_alloc
(abfd, tdata->nbr_dcxsbm * sizeof (struct dcxsbm_desc));
for (i = 0; i < tdata->nbr_dcxsbm; i++)
{
struct vms_dcxsbm *sbm;
struct dcxsbm_desc *sbmdesc = &tdata->dcxsbm[i];
unsigned int sbm_len;
unsigned int sbm_sz;
unsigned int off;
unsigned char *buf1;
unsigned int l, j;
if (sbm_off > reclen
|| reclen - sbm_off < sizeof (struct vms_dcxsbm))
{
err_free_buf:
free (buf);
goto err;
}
sbm = (struct vms_dcxsbm *) (buf + sbm_off);
sbm_sz = bfd_getl16 (sbm->size);
sbm_off += sbm_sz;
if (sbm_off > reclen)
goto err_free_buf;
sbmdesc->min_char = sbm->min_char;
BFD_ASSERT (sbmdesc->min_char == 0);
sbmdesc->max_char = sbm->max_char;
sbm_len = sbmdesc->max_char - sbmdesc->min_char + 1;
l = (2 * sbm_len + 7) / 8;
if (sbm_sz < sizeof (struct vms_dcxsbm) + l + sbm_len
|| (tdata->nbr_dcxsbm > 1
&& sbm_sz < sizeof (struct vms_dcxsbm) + l + 3 * sbm_len))
goto err_free_buf;
sbmdesc->flags = (unsigned char *)bfd_alloc (abfd, l);
off = bfd_getl16 (sbm->flags);
if (off > sbm_sz
|| sbm_sz - off < l)
goto err_free_buf;
memcpy (sbmdesc->flags, (bfd_byte *) sbm + off, l);
sbmdesc->nodes = (unsigned char *)bfd_alloc (abfd, 2 * sbm_len);
off = bfd_getl16 (sbm->nodes);
if (off > sbm_sz
|| sbm_sz - off < 2 * sbm_len)
goto err_free_buf;
memcpy (sbmdesc->nodes, (bfd_byte *) sbm + off, 2 * sbm_len);
off = bfd_getl16 (sbm->next);
if (off != 0)
{
if (off > sbm_sz
|| sbm_sz - off < 2 * sbm_len)
goto err_free_buf;
/* Read the 'next' array. */
sbmdesc->next = (unsigned short *) bfd_alloc (abfd, 2 * sbm_len);
buf1 = (bfd_byte *) sbm + off;
for (j = 0; j < sbm_len; j++)
sbmdesc->next[j] = bfd_getl16 (buf1 + j * 2);
}
else
{
/* There is no next array if there is only one submap. */
BFD_ASSERT (tdata->nbr_dcxsbm == 1);
sbmdesc->next = NULL;
}
}
free (buf);
}
else
{
tdata->nbr_dcxsbm = 0;
}
/* The map is always present. Also mark shared image library. */
abfd->has_armap = true;
if (tdata->type == LBR__C_TYP_ESHSTB || tdata->type == LBR__C_TYP_ISHSTB)
abfd->is_thin_archive = true;
return _bfd_no_cleanup;
err:
bfd_release (abfd, tdata);
abfd->tdata.any = (void *)tdata_hold;
return NULL;
}
/* Standard function for alpha libraries. */
bfd_cleanup
_bfd_vms_lib_alpha_archive_p (bfd *abfd)
{
return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
}
/* Standard function for ia64 libraries. */
bfd_cleanup
_bfd_vms_lib_ia64_archive_p (bfd *abfd)
{
return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
}
/* Standard function for text libraries. */
static bfd_cleanup
_bfd_vms_lib_txt_archive_p (bfd *abfd)
{
return _bfd_vms_lib_archive_p (abfd, vms_lib_txt);
}
/* Standard bfd function. */
static bool
_bfd_vms_lib_mkarchive (bfd *abfd, enum vms_lib_kind kind)
{
struct lib_tdata *tdata;
tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
if (tdata == NULL)
return false;
abfd->tdata.any = (void *)tdata;
vms_get_time (&tdata->credat_hi, &tdata->credat_lo);
tdata->kind = kind;
switch (kind)
{
case vms_lib_alpha:
tdata->ver = LBR_MAJORID;
tdata->mhd_size = offsetof (struct vms_mhd, pad1);
tdata->type = LBR__C_TYP_EOBJ;
break;
case vms_lib_ia64:
tdata->ver = LBR_ELFMAJORID;
tdata->mhd_size = sizeof (struct vms_mhd);
tdata->type = LBR__C_TYP_IOBJ;
break;
default:
abort ();
}
tdata->nbr_modules = 0;
tdata->artdata.symdef_count = 0;
tdata->modules = NULL;
tdata->artdata.symdefs = NULL;
tdata->cache = NULL;
return true;
}
bool
_bfd_vms_lib_alpha_mkarchive (bfd *abfd)
{
return _bfd_vms_lib_mkarchive (abfd, vms_lib_alpha);
}
bool
_bfd_vms_lib_ia64_mkarchive (bfd *abfd)
{
return _bfd_vms_lib_mkarchive (abfd, vms_lib_ia64);
}
/* Find NAME in the symbol index. Return the index. */
symindex
_bfd_vms_lib_find_symbol (bfd *abfd, const char *name)
{
struct lib_tdata *tdata = bfd_libdata (abfd);
carsym *syms = tdata->artdata.symdefs;
int lo, hi;
/* Open-coded binary search for speed. */
lo = 0;
hi = tdata->artdata.symdef_count - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
int diff;
diff = (char)(name[0] - syms[mid].name[0]);
if (diff == 0)
diff = strcmp (name, syms[mid].name);
if (diff == 0)
return mid;
else if (diff < 0)
hi = mid - 1;
else
lo = mid + 1;
}
return BFD_NO_MORE_SYMBOLS;
}
/* IO vector for archive member. Need that because members are not linearly
stored in archives. */
struct vms_lib_iovec
{
/* Current offset. */
ufile_ptr where;
/* Length of the module, when known. */
ufile_ptr file_len;
/* Current position in the record from bfd_read point of view (ie, after
decompression). 0 means that no data byte have been read, -2 and -1
are reserved for the length word. */
int rec_pos;
#define REC_POS_NL -4
#define REC_POS_PAD -3
#define REC_POS_LEN0 -2
#define REC_POS_LEN1 -1
/* Record length. */
unsigned short rec_len;
/* Number of bytes to read in the current record. */
unsigned short rec_rem;
/* Offset of the next block. */
file_ptr next_block;
/* Current *data* offset in the data block. */
unsigned short blk_off;
/* Offset of the first block. Extracted from the index. */
file_ptr first_block;
/* Initial next_block. Extracted when the MHD is read. */
file_ptr init_next_block;
/* Initial blk_off, once the MHD is read. */
unsigned short init_blk_off;
/* Used to store any 3 byte record, which could be the EOF pattern. */
unsigned char pattern[4];
/* DCX. */
struct dcxsbm_desc *dcxsbms;
/* Current submap. */
struct dcxsbm_desc *dcx_sbm;
/* Current offset in the submap. */
unsigned int dcx_offset;
int dcx_pos;
/* Compressed buffer. */
unsigned char *dcx_buf;
/* Size of the buffer. Used to resize. */
unsigned int dcx_max;
/* Number of valid bytes in the buffer. */
unsigned int dcx_rlen;
};
/* Return the current position. */
static file_ptr
vms_lib_btell (struct bfd *abfd)
{
struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
return vec->where;
}
/* Read the header of the next data block if all bytes of the current block
have been read. */
static bool
vms_lib_read_block (struct bfd *abfd)
{
struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
if (vec->blk_off == DATA__LENGTH)
{
unsigned char hdr[DATA__DATA];
/* Read next block. */
if (bfd_seek (abfd->my_archive, vec->next_block, SEEK_SET) != 0)
return false;
if (bfd_read (hdr, sizeof (hdr), abfd->my_archive) != sizeof (hdr))
return false;
vec->next_block = (bfd_getl32 (hdr + 2) - 1) * VMS_BLOCK_SIZE;
vec->blk_off = sizeof (hdr);
}
return true;
}
/* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
not stored. Read linearly from the library, but handle blocks. This
function does not handle records nor EOF. */
static file_ptr
vms_lib_bread_raw (struct bfd *abfd, unsigned char *buf, file_ptr nbytes)
{
struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
file_ptr res;
res = 0;
while (nbytes > 0)
{
unsigned int l;
/* Be sure the current data block is read. */
if (!vms_lib_read_block (abfd))
return -1;
/* Do not read past the data block, do not read more than requested. */
l = DATA__LENGTH - vec->blk_off;
if (l > nbytes)
l = nbytes;
if (l == 0)
return 0;
if (buf != NULL)
{
/* Really read into BUF. */
if (bfd_read (buf, l, abfd->my_archive) != l)
return -1;
}
else
{
/* Make as if we are reading. */
if (bfd_seek (abfd->my_archive, l, SEEK_CUR) != 0)
return -1;
}
if (buf != NULL)
buf += l;
vec->blk_off += l;
nbytes -= l;
res += l;
}
return res;
}
/* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
static file_ptr
vms_lib_dcx (struct vms_lib_iovec *vec, unsigned char *buf, file_ptr nbytes)
{
struct dcxsbm_desc *sbm;
unsigned int i;
unsigned int offset;
unsigned int j;
file_ptr res = 0;
/* The loop below expect to deliver at least one byte. */
if (nbytes == 0)
return 0;
/* Get the current state. */
sbm = vec->dcx_sbm;
offset = vec->dcx_offset;
j = vec->dcx_pos & 7;
for (i = vec->dcx_pos >> 3; i < vec->dcx_rlen; i++)
{
unsigned char b = vec->dcx_buf[i];
for (; j < 8; j++)
{
if (b & (1 << j))
offset++;
if (!(sbm->flags[offset >> 3] & (1 << (offset & 7))))
{
unsigned int n_offset = sbm->nodes[offset];
if (n_offset == 0)
{
/* End of buffer. Stay where we are. */
vec->dcx_pos = (i << 3) + j;
if (b & (1 << j))
offset--;
vec->dcx_offset = offset;
vec->dcx_sbm = sbm;
return res;
}
offset = 2 * n_offset;
}
else
{
unsigned char v = sbm->nodes[offset];
if (sbm->next != NULL)
sbm = vec->dcxsbms + sbm->next[v];
offset = 0;