vms-misc.c revision 1.1.1.6 1 /* vms-misc.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright (C) 1996-2019 Free Software Foundation, Inc.
4
5 Miscellaneous functions.
6
7 Written by Klaus K"ampf (kkaempf (at) rmi.de)
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
24 #if __STDC__
25 #include <stdarg.h>
26 #endif
27
28 #include "sysdep.h"
29 #include "bfd.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "safe-ctype.h"
33
34 #ifdef VMS
35 #define __NEW_STARLET
36 #include <rms.h>
37 #include <unixlib.h>
38 #include <gen64def.h>
39 #include <starlet.h>
40 #define RME$C_SETRFM 0x00000001
41 #include <unistd.h>
42 #endif
43 #include <time.h>
44
45 #include "vms.h"
46 #include "vms/emh.h"
47
48 #if VMS_DEBUG
49 /* Debug functions. */
50
51 /* Debug function for all vms extensions evaluates environment
52 variable VMS_DEBUG for a numerical value on the first call all
53 error levels below this value are printed:
54
55 Levels:
56 1 toplevel bfd calls (functions from the bfd vector)
57 2 functions called by bfd calls
58 ...
59 9 almost everything
60
61 Level is also indentation level. Indentation is performed
62 if level > 0. */
63
64 void
65 _bfd_vms_debug (int level, char *format, ...)
66 {
67 static int min_level = -1;
68 static FILE *output = NULL;
69 char *eptr;
70 va_list args;
71 int abslvl = (level > 0) ? level : - level;
72
73 if (min_level == -1)
74 {
75 if ((eptr = getenv ("VMS_DEBUG")) != NULL)
76 {
77 min_level = atoi (eptr);
78 output = stderr;
79 }
80 else
81 min_level = 0;
82 }
83 if (output == NULL)
84 return;
85 if (abslvl > min_level)
86 return;
87
88 while (--level > 0)
89 fprintf (output, " ");
90 va_start (args, format);
91 vfprintf (output, format, args);
92 fflush (output);
93 va_end (args);
94 }
95
96 /* A debug function
97 hex dump 'size' bytes starting at 'ptr'. */
98
99 void
100 _bfd_hexdump (int level, unsigned char *ptr, int size, int offset)
101 {
102 unsigned char *lptr = ptr;
103 int count = 0;
104 long start = offset;
105
106 while (size-- > 0)
107 {
108 if ((count % 16) == 0)
109 vms_debug (level, "%08lx:", start);
110 vms_debug (-level, " %02x", *ptr++);
111 count++;
112 start++;
113 if (size == 0)
114 {
115 while ((count % 16) != 0)
116 {
117 vms_debug (-level, " ");
118 count++;
119 }
120 }
121 if ((count % 16) == 0)
122 {
123 vms_debug (-level, " ");
124 while (lptr < ptr)
125 {
126 vms_debug (-level, "%c", (*lptr < 32) ? '.' : *lptr);
127 lptr++;
128 }
129 vms_debug (-level, "\n");
130 }
131 }
132 if ((count % 16) != 0)
133 vms_debug (-level, "\n");
134 }
135 #endif
136
137
139 /* Copy sized string (string with fixed size) to new allocated area.
140 Size is string size (size of record). */
141
142 char *
143 _bfd_vms_save_sized_string (unsigned char *str, unsigned int size)
144 {
145 char *newstr = bfd_malloc ((bfd_size_type) size + 1);
146
147 if (newstr == NULL)
148 return NULL;
149 memcpy (newstr, (char *) str, (size_t) size);
150 newstr[size] = 0;
151
152 return newstr;
153 }
154
155 /* Copy counted string (string with size at first byte) to new allocated area.
156 PTR points to size byte on entry. */
157
158 char *
159 _bfd_vms_save_counted_string (unsigned char *ptr, unsigned int maxlen)
160 {
161 unsigned int len = *ptr++;
162
163 if (len > maxlen)
164 return NULL;
165 return _bfd_vms_save_sized_string (ptr, len);
166 }
167
168 /* Object output routines. */
170
171 /* Begin new record.
172 Write 2 bytes rectype and 2 bytes record length. */
173
174 void
175 _bfd_vms_output_begin (struct vms_rec_wr *recwr, int rectype)
176 {
177 vms_debug2 ((6, "_bfd_vms_output_begin (type %d)\n", rectype));
178
179 /* Record must have been closed. */
180 BFD_ASSERT (recwr->size == 0);
181
182 _bfd_vms_output_short (recwr, (unsigned int) rectype);
183
184 /* Placeholder for length. */
185 _bfd_vms_output_short (recwr, 0);
186 }
187
188 /* Begin new sub-record.
189 Write 2 bytes rectype, and 2 bytes record length. */
190
191 void
192 _bfd_vms_output_begin_subrec (struct vms_rec_wr *recwr, int rectype)
193 {
194 vms_debug2 ((6, "_bfd_vms_output_begin_subrec (type %d)\n", rectype));
195
196 /* Subrecord must have been closed. */
197 BFD_ASSERT (recwr->subrec_offset == 0);
198
199 /* Save start of subrecord offset. */
200 recwr->subrec_offset = recwr->size;
201
202 /* Subrecord type. */
203 _bfd_vms_output_short (recwr, (unsigned int) rectype);
204
205 /* Placeholder for length. */
206 _bfd_vms_output_short (recwr, 0);
207 }
208
209 /* Set record/subrecord alignment. */
210
211 void
212 _bfd_vms_output_alignment (struct vms_rec_wr *recwr, int alignto)
213 {
214 vms_debug2 ((6, "_bfd_vms_output_alignment (%d)\n", alignto));
215 recwr->align = alignto;
216 }
217
218 /* Align the size of the current record (whose length is LENGTH).
219 Warning: this obviously changes the record (and the possible subrecord)
220 length. */
221
222 static void
223 _bfd_vms_output_align (struct vms_rec_wr *recwr, unsigned int length)
224 {
225 unsigned int real_size = recwr->size;
226 unsigned int aligncount;
227
228 /* Pad with 0 if alignment is required. */
229 aligncount = (recwr->align - (length % recwr->align)) % recwr->align;
230 vms_debug2 ((6, "align: adding %d bytes\n", aligncount));
231 while (aligncount-- > 0)
232 recwr->buf[real_size++] = 0;
233
234 recwr->size = real_size;
235 }
236
237 /* Ends current sub-record. Set length field. */
238
239 void
240 _bfd_vms_output_end_subrec (struct vms_rec_wr *recwr)
241 {
242 int real_size = recwr->size;
243 int length;
244
245 /* Subrecord must be open. */
246 BFD_ASSERT (recwr->subrec_offset != 0);
247
248 length = real_size - recwr->subrec_offset;
249
250 if (length == 0)
251 return;
252
253 _bfd_vms_output_align (recwr, length);
254
255 /* Put length to buffer. */
256 bfd_putl16 ((bfd_vma) (recwr->size - recwr->subrec_offset),
257 recwr->buf + recwr->subrec_offset + 2);
258
259 /* Close the subrecord. */
260 recwr->subrec_offset = 0;
261 }
262
263 /* Ends current record (and write it). */
264
265 void
266 _bfd_vms_output_end (bfd *abfd, struct vms_rec_wr *recwr)
267 {
268 vms_debug2 ((6, "_bfd_vms_output_end (size %u)\n", recwr->size));
269
270 /* Subrecord must have been closed. */
271 BFD_ASSERT (recwr->subrec_offset == 0);
272
273 if (recwr->size == 0)
274 return;
275
276 _bfd_vms_output_align (recwr, recwr->size);
277
278 /* Write the length word. */
279 bfd_putl16 ((bfd_vma) recwr->size, recwr->buf + 2);
280
281 /* File is open in undefined (UDF) format on VMS, but ultimately will be
282 converted to variable length (VAR) format. VAR format has a length
283 word first which must be explicitly output in UDF format. */
284 /* So, first the length word. */
285 bfd_bwrite (recwr->buf + 2, 2, abfd);
286
287 /* Align. */
288 if (recwr->size & 1)
289 recwr->buf[recwr->size++] = 0;
290
291 /* Then the record. */
292 bfd_bwrite (recwr->buf, (size_t) recwr->size, abfd);
293
294 recwr->size = 0;
295 }
296
297 /* Check remaining buffer size. Return what's left. */
298
299 int
300 _bfd_vms_output_check (struct vms_rec_wr *recwr, int size)
301 {
302 vms_debug2 ((6, "_bfd_vms_output_check (%d)\n", size));
303
304 return (MAX_OUTREC_SIZE - (recwr->size + size + MIN_OUTREC_LUFT));
305 }
306
307 /* Output byte (8 bit) value. */
308
309 void
310 _bfd_vms_output_byte (struct vms_rec_wr *recwr, unsigned int value)
311 {
312 vms_debug2 ((6, "_bfd_vms_output_byte (%02x)\n", value));
313
314 *(recwr->buf + recwr->size) = value;
315 recwr->size += 1;
316 }
317
318 /* Output short (16 bit) value. */
319
320 void
321 _bfd_vms_output_short (struct vms_rec_wr *recwr, unsigned int value)
322 {
323 vms_debug2 ((6, "_bfd_vms_output_short (%04x)\n", value));
324
325 bfd_putl16 ((bfd_vma) value & 0xffff, recwr->buf + recwr->size);
326 recwr->size += 2;
327 }
328
329 /* Output long (32 bit) value. */
330
331 void
332 _bfd_vms_output_long (struct vms_rec_wr *recwr, unsigned long value)
333 {
334 vms_debug2 ((6, "_bfd_vms_output_long (%08lx)\n", value));
335
336 bfd_putl32 ((bfd_vma) value, recwr->buf + recwr->size);
337 recwr->size += 4;
338 }
339
340 /* Output quad (64 bit) value. */
341
342 void
343 _bfd_vms_output_quad (struct vms_rec_wr *recwr, bfd_vma value)
344 {
345 vms_debug2 ((6, "_bfd_vms_output_quad (%08lx)\n", (unsigned long)value));
346
347 bfd_putl64 (value, recwr->buf + recwr->size);
348 recwr->size += 8;
349 }
350
351 /* Output c-string as counted string. */
352
353 void
354 _bfd_vms_output_counted (struct vms_rec_wr *recwr, const char *value)
355 {
356 int len;
357
358 vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value));
359
360 len = strlen (value);
361 if (len == 0)
362 {
363 _bfd_error_handler (_("_bfd_vms_output_counted called with zero bytes"));
364 return;
365 }
366 if (len > 255)
367 {
368 _bfd_error_handler (_("_bfd_vms_output_counted called with too many bytes"));
369 return;
370 }
371 _bfd_vms_output_byte (recwr, (unsigned int) len & 0xff);
372 _bfd_vms_output_dump (recwr, (const unsigned char *)value, len);
373 }
374
375 /* Output character area. */
376
377 void
378 _bfd_vms_output_dump (struct vms_rec_wr *recwr, const unsigned char *data, int len)
379 {
380 vms_debug2 ((6, "_bfd_vms_output_dump (%d)\n", len));
381
382 if (len == 0)
383 return;
384
385 memcpy (recwr->buf + recwr->size, data, (size_t) len);
386 recwr->size += len;
387 }
388
389 /* Output count bytes of value. */
390
391 void
392 _bfd_vms_output_fill (struct vms_rec_wr *recwr, int value, int count)
393 {
394 vms_debug2 ((6, "_bfd_vms_output_fill (val %02x times %d)\n", value, count));
395
396 if (count == 0)
397 return;
398 memset (recwr->buf + recwr->size, value, (size_t) count);
399 recwr->size += count;
400 }
401
402 #ifdef VMS
403 /* Convert the file to variable record length format. This is done
404 using undocumented system call sys$modify().
405 Pure VMS version. */
406
407 static void
408 vms_convert_to_var (char * vms_filename)
409 {
410 struct FAB fab = cc$rms_fab;
411
412 fab.fab$l_fna = vms_filename;
413 fab.fab$b_fns = strlen (vms_filename);
414 fab.fab$b_fac = FAB$M_PUT;
415 fab.fab$l_fop = FAB$M_ESC;
416 fab.fab$l_ctx = RME$C_SETRFM;
417
418 sys$open (&fab);
419
420 fab.fab$b_rfm = FAB$C_VAR;
421
422 sys$modify (&fab);
423 sys$close (&fab);
424 }
425
426 static int
427 vms_convert_to_var_1 (char *filename, int type)
428 {
429 if (type != DECC$K_FILE)
430 return FALSE;
431 vms_convert_to_var (filename);
432 return TRUE;
433 }
434
435 /* Convert the file to variable record length format. This is done
436 using undocumented system call sys$modify().
437 Unix filename version. */
438
439 int
440 _bfd_vms_convert_to_var_unix_filename (const char *unix_filename)
441 {
442 if (decc$to_vms (unix_filename, &vms_convert_to_var_1, 0, 1) != 1)
443 return FALSE;
444 return TRUE;
445 }
446 #endif /* VMS */
447
448 /* Manufacture a VMS like time on a unix based system.
449 stolen from obj-vms.c. */
450
451 unsigned char *
452 get_vms_time_string (void)
453 {
454 static unsigned char tbuf[18];
455 #ifndef VMS
456 char *pnt;
457 time_t timeb;
458
459 time (& timeb);
460 pnt = ctime (&timeb);
461 pnt[3] = 0;
462 pnt[7] = 0;
463 pnt[10] = 0;
464 pnt[16] = 0;
465 pnt[24] = 0;
466 sprintf ((char *) tbuf, "%2s-%3s-%s %s",
467 pnt + 8, pnt + 4, pnt + 20, pnt + 11);
468 #else
469 struct
470 {
471 int Size;
472 unsigned char *Ptr;
473 } Descriptor;
474 Descriptor.Size = 17;
475 Descriptor.Ptr = tbuf;
476 SYS$ASCTIM (0, &Descriptor, 0, 0);
477 #endif /* not VMS */
478
479 vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf));
480
481 return tbuf;
482 }
483
484 /* Create module name from filename (ie, extract the basename and convert it
485 in upper cases). Works on both VMS and UNIX pathes.
486 The result has to be free(). */
487
488 char *
489 vms_get_module_name (const char *filename, bfd_boolean upcase)
490 {
491 char *fname, *fptr;
492 const char *fout;
493
494 /* Strip VMS path. */
495 fout = strrchr (filename, ']');
496 if (fout == NULL)
497 fout = strchr (filename, ':');
498 if (fout != NULL)
499 fout++;
500 else
501 fout = filename;
502
503 /* Strip UNIX path. */
504 fptr = strrchr (fout, '/');
505 if (fptr != NULL)
506 fout = fptr + 1;
507
508 fname = strdup (fout);
509
510 /* Strip suffix. */
511 fptr = strrchr (fname, '.');
512 if (fptr != 0)
513 *fptr = 0;
514
515 /* Convert to upper case and truncate at 31 characters.
516 (VMS object file format restricts module name length to 31). */
517 fptr = fname;
518 for (fptr = fname; *fptr != 0; fptr++)
519 {
520 if (*fptr == ';' || (fptr - fname) >= 31)
521 {
522 *fptr = 0;
523 break;
524 }
525 if (upcase)
526 *fptr = TOUPPER (*fptr);
527 }
528 return fname;
529 }
530
531 /* Compared to usual UNIX time_t, VMS time has less limits:
532 - 64 bit (63 bits in fact as the MSB must be 0)
533 - 100ns granularity
534 - epoch is Nov 17, 1858.
535 Here has the constants and the routines used to convert VMS from/to UNIX time.
536 The conversion routines don't assume 64 bits arithmetic.
537
538 Here we assume that the definition of time_t is the UNIX one, ie integer
539 type, expressing seconds since the epoch. */
540
541 /* UNIX time granularity for VMS, ie 1s / 100ns. */
542 #define VMS_TIME_FACTOR 10000000
543
544 /* Number of seconds since VMS epoch of the UNIX epoch. */
545 #define VMS_TIME_OFFSET 3506716800U
546
547 /* Convert a VMS time to a unix time. */
548
549 time_t
550 vms_time_to_time_t (unsigned int hi, unsigned int lo)
551 {
552 unsigned int tmp;
553 unsigned int rlo;
554 int i;
555 time_t res;
556
557 /* First convert to seconds. */
558 tmp = hi % VMS_TIME_FACTOR;
559 hi = hi / VMS_TIME_FACTOR;
560 rlo = 0;
561 for (i = 0; i < 4; i++)
562 {
563 tmp = (tmp << 8) | (lo >> 24);
564 lo <<= 8;
565
566 rlo = (rlo << 8) | (tmp / VMS_TIME_FACTOR);
567 tmp %= VMS_TIME_FACTOR;
568 }
569 lo = rlo;
570
571 /* Return 0 in case of overflow. */
572 if (hi > 1
573 || (hi == 1 && lo >= VMS_TIME_OFFSET))
574 return 0;
575
576 /* Return 0 in case of underflow. */
577 if (hi == 0 && lo < VMS_TIME_OFFSET)
578 return 0;
579
580 res = lo - VMS_TIME_OFFSET;
581 if (res <= 0)
582 return 0;
583 return res;
584 }
585
586 /* Convert a time_t to a VMS time. */
587
588 void
589 vms_time_t_to_vms_time (time_t ut, unsigned int *hi, unsigned int *lo)
590 {
591 unsigned short val[4];
592 unsigned short tmp[4];
593 unsigned int carry;
594 int i;
595
596 /* Put into val. */
597 val[0] = ut & 0xffff;
598 val[1] = (ut >> 16) & 0xffff;
599 val[2] = sizeof (ut) > 4 ? (ut >> 32) & 0xffff : 0;
600 val[3] = sizeof (ut) > 4 ? (ut >> 48) & 0xffff : 0;
601
602 /* Add offset. */
603 tmp[0] = VMS_TIME_OFFSET & 0xffff;
604 tmp[1] = VMS_TIME_OFFSET >> 16;
605 tmp[2] = 0;
606 tmp[3] = 0;
607 carry = 0;
608 for (i = 0; i < 4; i++)
609 {
610 carry += tmp[i] + val[i];
611 val[i] = carry & 0xffff;
612 carry = carry >> 16;
613 }
614
615 /* Multiply by factor, well first by 10000 and then by 1000. */
616 carry = 0;
617 for (i = 0; i < 4; i++)
618 {
619 carry += val[i] * 10000;
620 val[i] = carry & 0xffff;
621 carry = carry >> 16;
622 }
623 carry = 0;
624 for (i = 0; i < 4; i++)
625 {
626 carry += val[i] * 1000;
627 val[i] = carry & 0xffff;
628 carry = carry >> 16;
629 }
630
631 /* Write the result. */
632 *lo = val[0] | (val[1] << 16);
633 *hi = val[2] | (val[3] << 16);
634 }
635
636 /* Convert a raw (stored in a buffer) VMS time to a unix time. */
637
638 time_t
639 vms_rawtime_to_time_t (unsigned char *buf)
640 {
641 unsigned int hi = bfd_getl32 (buf + 4);
642 unsigned int lo = bfd_getl32 (buf + 0);
643
644 return vms_time_to_time_t (hi, lo);
645 }
646
647 void
648 vms_get_time (unsigned int *hi, unsigned int *lo)
649 {
650 #ifdef VMS
651 struct _generic_64 t;
652
653 sys$gettim (&t);
654 *lo = t.gen64$q_quadword;
655 *hi = t.gen64$q_quadword >> 32;
656 #else
657 time_t t;
658
659 time (&t);
660 vms_time_t_to_vms_time (t, hi, lo);
661 #endif
662 }
663
664 /* Get the current time into a raw buffer BUF. */
665
666 void
667 vms_raw_get_time (unsigned char *buf)
668 {
669 unsigned int hi, lo;
670
671 vms_get_time (&hi, &lo);
672 bfd_putl32 (lo, buf + 0);
673 bfd_putl32 (hi, buf + 4);
674 }
675