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