1 /* vms-misc.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright (C) 1996-2023 Free Software Foundation, Inc.
5 Miscellaneous functions.
7 Written by Klaus K"ampf (kkaempf@rmi.de)
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.
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.
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. */
32 #include "safe-ctype.h"
40 #define RME$C_SETRFM 0x00000001
49 /* Debug functions. */
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:
56 1 toplevel bfd calls (functions from the bfd vector)
57 2 functions called by bfd calls
61 Level is also indentation level. Indentation is performed
65 _bfd_vms_debug (int level
, char *format
, ...)
67 static int min_level
= -1;
68 static FILE *output
= NULL
;
71 int abslvl
= (level
> 0) ? level
: - level
;
75 if ((eptr
= getenv ("VMS_DEBUG")) != NULL
)
77 min_level
= atoi (eptr
);
85 if (abslvl
> min_level
)
89 fprintf (output
, " ");
90 va_start (args
, format
);
91 vfprintf (output
, format
, args
);
97 hex dump 'size' bytes starting at 'ptr'. */
100 _bfd_hexdump (int level
, unsigned char *ptr
, int size
, int offset
)
102 unsigned char *lptr
= ptr
;
108 if ((count
% 16) == 0)
109 vms_debug (level
, "%08lx:", start
);
110 vms_debug (-level
, " %02x", *ptr
++);
115 while ((count
% 16) != 0)
117 vms_debug (-level
, " ");
121 if ((count
% 16) == 0)
123 vms_debug (-level
, " ");
126 vms_debug (-level
, "%c", (*lptr
< 32) ? '.' : *lptr
);
129 vms_debug (-level
, "\n");
132 if ((count
% 16) != 0)
133 vms_debug (-level
, "\n");
138 /* Copy sized string (string with fixed size) to new allocated area.
139 Size is string size (size of record). */
142 _bfd_vms_save_sized_string (bfd
*abfd
, unsigned char *str
, size_t size
)
146 if (size
== (size_t) -1)
148 bfd_set_error (bfd_error_no_memory
);
151 newstr
= bfd_alloc (abfd
, size
+ 1);
154 memcpy (newstr
, str
, size
);
160 /* Copy counted string (string with size at first byte) to new allocated area.
161 PTR points to size byte on entry. */
164 _bfd_vms_save_counted_string (bfd
*abfd
, unsigned char *ptr
, size_t maxlen
)
171 if (len
> maxlen
- 1)
173 return _bfd_vms_save_sized_string (abfd
, ptr
, len
);
176 /* Object output routines. */
179 Write 2 bytes rectype and 2 bytes record length. */
182 _bfd_vms_output_begin (struct vms_rec_wr
*recwr
, int rectype
)
184 vms_debug2 ((6, "_bfd_vms_output_begin (type %d)\n", rectype
));
186 /* Record must have been closed. */
187 BFD_ASSERT (recwr
->size
== 0);
189 _bfd_vms_output_short (recwr
, (unsigned int) rectype
);
191 /* Placeholder for length. */
192 _bfd_vms_output_short (recwr
, 0);
195 /* Begin new sub-record.
196 Write 2 bytes rectype, and 2 bytes record length. */
199 _bfd_vms_output_begin_subrec (struct vms_rec_wr
*recwr
, int rectype
)
201 vms_debug2 ((6, "_bfd_vms_output_begin_subrec (type %d)\n", rectype
));
203 /* Subrecord must have been closed. */
204 BFD_ASSERT (recwr
->subrec_offset
== 0);
206 /* Save start of subrecord offset. */
207 recwr
->subrec_offset
= recwr
->size
;
209 /* Subrecord type. */
210 _bfd_vms_output_short (recwr
, (unsigned int) rectype
);
212 /* Placeholder for length. */
213 _bfd_vms_output_short (recwr
, 0);
216 /* Set record/subrecord alignment. */
219 _bfd_vms_output_alignment (struct vms_rec_wr
*recwr
, int alignto
)
221 vms_debug2 ((6, "_bfd_vms_output_alignment (%d)\n", alignto
));
222 recwr
->align
= alignto
;
225 /* Align the size of the current record (whose length is LENGTH).
226 Warning: this obviously changes the record (and the possible subrecord)
230 _bfd_vms_output_align (struct vms_rec_wr
*recwr
, unsigned int length
)
232 unsigned int real_size
= recwr
->size
;
233 unsigned int aligncount
;
235 /* Pad with 0 if alignment is required. */
236 aligncount
= (recwr
->align
- (length
% recwr
->align
)) % recwr
->align
;
237 vms_debug2 ((6, "align: adding %d bytes\n", aligncount
));
238 while (aligncount
-- > 0)
239 recwr
->buf
[real_size
++] = 0;
241 recwr
->size
= real_size
;
244 /* Ends current sub-record. Set length field. */
247 _bfd_vms_output_end_subrec (struct vms_rec_wr
*recwr
)
249 int real_size
= recwr
->size
;
252 /* Subrecord must be open. */
253 BFD_ASSERT (recwr
->subrec_offset
!= 0);
255 length
= real_size
- recwr
->subrec_offset
;
260 _bfd_vms_output_align (recwr
, length
);
262 /* Put length to buffer. */
263 bfd_putl16 ((bfd_vma
) (recwr
->size
- recwr
->subrec_offset
),
264 recwr
->buf
+ recwr
->subrec_offset
+ 2);
266 /* Close the subrecord. */
267 recwr
->subrec_offset
= 0;
270 /* Ends current record (and write it). */
273 _bfd_vms_output_end (bfd
*abfd
, struct vms_rec_wr
*recwr
)
275 vms_debug2 ((6, "_bfd_vms_output_end (size %u)\n", recwr
->size
));
277 /* Subrecord must have been closed. */
278 BFD_ASSERT (recwr
->subrec_offset
== 0);
280 if (recwr
->size
== 0)
283 _bfd_vms_output_align (recwr
, recwr
->size
);
285 /* Write the length word. */
286 bfd_putl16 ((bfd_vma
) recwr
->size
, recwr
->buf
+ 2);
288 /* File is open in undefined (UDF) format on VMS, but ultimately will be
289 converted to variable length (VAR) format. VAR format has a length
290 word first which must be explicitly output in UDF format. */
291 /* So, first the length word. */
292 if (bfd_write (recwr
->buf
+ 2, 2, abfd
) != 2)
297 recwr
->buf
[recwr
->size
++] = 0;
299 /* Then the record. */
300 if (bfd_write (recwr
->buf
, (size_t) recwr
->size
, abfd
)
301 != (size_t) recwr
->size
)
308 /* Check remaining buffer size. Return what's left. */
311 _bfd_vms_output_check (struct vms_rec_wr
*recwr
, int size
)
313 vms_debug2 ((6, "_bfd_vms_output_check (%d)\n", size
));
315 return (MAX_OUTREC_SIZE
- (recwr
->size
+ size
+ MIN_OUTREC_LUFT
));
318 /* Output byte (8 bit) value. */
321 _bfd_vms_output_byte (struct vms_rec_wr
*recwr
, unsigned int value
)
323 vms_debug2 ((6, "_bfd_vms_output_byte (%02x)\n", value
));
325 *(recwr
->buf
+ recwr
->size
) = value
;
329 /* Output short (16 bit) value. */
332 _bfd_vms_output_short (struct vms_rec_wr
*recwr
, unsigned int value
)
334 vms_debug2 ((6, "_bfd_vms_output_short (%04x)\n", value
));
336 bfd_putl16 ((bfd_vma
) value
& 0xffff, recwr
->buf
+ recwr
->size
);
340 /* Output long (32 bit) value. */
343 _bfd_vms_output_long (struct vms_rec_wr
*recwr
, unsigned long value
)
345 vms_debug2 ((6, "_bfd_vms_output_long (%08lx)\n", value
));
347 bfd_putl32 ((bfd_vma
) value
, recwr
->buf
+ recwr
->size
);
351 /* Output quad (64 bit) value. */
354 _bfd_vms_output_quad (struct vms_rec_wr
*recwr
, bfd_vma value
)
356 vms_debug2 ((6, "_bfd_vms_output_quad (%08lx)\n", (unsigned long)value
));
358 bfd_putl64 (value
, recwr
->buf
+ recwr
->size
);
362 /* Output c-string as counted string. */
365 _bfd_vms_output_counted (struct vms_rec_wr
*recwr
, const char *value
)
369 vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value
));
371 len
= strlen (value
);
374 _bfd_error_handler (_("_bfd_vms_output_counted called with zero bytes"));
379 _bfd_error_handler (_("_bfd_vms_output_counted called with too many bytes"));
382 _bfd_vms_output_byte (recwr
, (unsigned int) len
& 0xff);
383 _bfd_vms_output_dump (recwr
, (const unsigned char *)value
, len
);
386 /* Output character area. */
389 _bfd_vms_output_dump (struct vms_rec_wr
*recwr
, const unsigned char *data
, int len
)
391 vms_debug2 ((6, "_bfd_vms_output_dump (%d)\n", len
));
396 memcpy (recwr
->buf
+ recwr
->size
, data
, (size_t) len
);
400 /* Output count bytes of value. */
403 _bfd_vms_output_fill (struct vms_rec_wr
*recwr
, int value
, int count
)
405 vms_debug2 ((6, "_bfd_vms_output_fill (val %02x times %d)\n", value
, count
));
409 memset (recwr
->buf
+ recwr
->size
, value
, (size_t) count
);
410 recwr
->size
+= count
;
414 /* Convert the file to variable record length format. This is done
415 using undocumented system call sys$modify().
419 vms_convert_to_var (char * vms_filename
)
421 struct FAB fab
= cc$rms_fab
;
423 fab
.fab$l_fna
= vms_filename
;
424 fab
.fab$b_fns
= strlen (vms_filename
);
425 fab
.fab$b_fac
= FAB$M_PUT
;
426 fab
.fab$l_fop
= FAB$M_ESC
;
427 fab
.fab$l_ctx
= RME$C_SETRFM
;
431 fab
.fab$b_rfm
= FAB$C_VAR
;
438 vms_convert_to_var_1 (char *filename
, int type
)
440 if (type
!= DECC$K_FILE
)
442 vms_convert_to_var (filename
);
446 /* Convert the file to variable record length format. This is done
447 using undocumented system call sys$modify().
448 Unix filename version. */
451 _bfd_vms_convert_to_var_unix_filename (const char *unix_filename
)
453 if (decc$
to_vms (unix_filename
, &vms_convert_to_var_1
, 0, 1) != 1)
459 /* Manufacture a VMS like time on a unix based system.
460 stolen from obj-vms.c. */
463 get_vms_time_string (unsigned char *tbuf
)
470 pnt
= ctime (&timeb
);
476 sprintf ((char *) tbuf
, "%2s-%3s-%s %s",
477 pnt
+ 8, pnt
+ 4, pnt
+ 20, pnt
+ 11);
484 Descriptor
.Size
= 17;
485 Descriptor
.Ptr
= tbuf
;
486 SYS$
ASCTIM (0, &Descriptor
, 0, 0);
489 vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf
));
494 /* Create module name from filename (ie, extract the basename and convert it
495 in upper cases). Works on both VMS and UNIX pathes.
496 The result has to be free(). */
499 vms_get_module_name (const char *filename
, bool upcase
)
504 /* Strip VMS path. */
505 fout
= strrchr (filename
, ']');
507 fout
= strchr (filename
, ':');
513 /* Strip UNIX path. */
514 fptr
= strrchr (fout
, '/');
518 fname
= strdup (fout
);
521 fptr
= strrchr (fname
, '.');
525 /* Convert to upper case and truncate at 31 characters.
526 (VMS object file format restricts module name length to 31). */
528 for (fptr
= fname
; *fptr
!= 0; fptr
++)
530 if (*fptr
== ';' || (fptr
- fname
) >= 31)
536 *fptr
= TOUPPER (*fptr
);
541 /* Compared to usual UNIX time_t, VMS time has less limits:
542 - 64 bit (63 bits in fact as the MSB must be 0)
544 - epoch is Nov 17, 1858.
545 Here has the constants and the routines used to convert VMS from/to UNIX time.
546 The conversion routines don't assume 64 bits arithmetic.
548 Here we assume that the definition of time_t is the UNIX one, ie integer
549 type, expressing seconds since the epoch. */
551 /* UNIX time granularity for VMS, ie 1s / 100ns. */
552 #define VMS_TIME_FACTOR 10000000
554 /* Number of seconds since VMS epoch of the UNIX epoch. */
555 #define VMS_TIME_OFFSET 3506716800U
557 /* Convert a VMS time to a unix time. */
560 vms_time_to_time_t (unsigned int hi
, unsigned int lo
)
567 /* First convert to seconds. */
568 tmp
= hi
% VMS_TIME_FACTOR
;
569 hi
= hi
/ VMS_TIME_FACTOR
;
571 for (i
= 0; i
< 4; i
++)
573 tmp
= (tmp
<< 8) | (lo
>> 24);
576 rlo
= (rlo
<< 8) | (tmp
/ VMS_TIME_FACTOR
);
577 tmp
%= VMS_TIME_FACTOR
;
581 /* Return 0 in case of overflow. */
583 || (hi
== 1 && lo
>= VMS_TIME_OFFSET
))
586 /* Return 0 in case of underflow. */
587 if (hi
== 0 && lo
< VMS_TIME_OFFSET
)
590 res
= lo
- VMS_TIME_OFFSET
;
596 /* Convert a time_t to a VMS time. */
599 vms_time_t_to_vms_time (time_t ut
, unsigned int *hi
, unsigned int *lo
)
607 val
[0] = ut
& 0xffff;
608 val
[1] = (ut
>> 16) & 0xffff;
609 val
[2] = sizeof (ut
) > 4 ? (ut
>> 32) & 0xffff : 0;
610 val
[3] = sizeof (ut
) > 4 ? (ut
>> 48) & 0xffff : 0;
613 tmp
[0] = VMS_TIME_OFFSET
& 0xffff;
614 tmp
[1] = VMS_TIME_OFFSET
>> 16;
618 for (i
= 0; i
< 4; i
++)
620 carry
+= tmp
[i
] + val
[i
];
621 val
[i
] = carry
& 0xffff;
625 /* Multiply by factor, well first by 10000 and then by 1000. */
627 for (i
= 0; i
< 4; i
++)
629 carry
+= val
[i
] * 10000;
630 val
[i
] = carry
& 0xffff;
634 for (i
= 0; i
< 4; i
++)
636 carry
+= val
[i
] * 1000;
637 val
[i
] = carry
& 0xffff;
641 /* Write the result. */
642 *lo
= val
[0] | (val
[1] << 16);
643 *hi
= val
[2] | (val
[3] << 16);
646 /* Convert a raw (stored in a buffer) VMS time to a unix time. */
649 vms_rawtime_to_time_t (unsigned char *buf
)
651 unsigned int hi
= bfd_getl32 (buf
+ 4);
652 unsigned int lo
= bfd_getl32 (buf
+ 0);
654 return vms_time_to_time_t (hi
, lo
);
658 vms_get_time (unsigned int *hi
, unsigned int *lo
)
661 struct _generic_64 t
;
664 *lo
= t
.gen64$q_quadword
;
665 *hi
= t
.gen64$q_quadword
>> 32;
670 vms_time_t_to_vms_time (t
, hi
, lo
);
674 /* Get the current time into a raw buffer BUF. */
677 vms_raw_get_time (unsigned char *buf
)
681 vms_get_time (&hi
, &lo
);
682 bfd_putl32 (lo
, buf
+ 0);
683 bfd_putl32 (hi
, buf
+ 4);