vms-misc.c revision 1.1.1.7
1/* vms-misc.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
2   EVAX (openVMS/Alpha) files.
3   Copyright (C) 1996-2020 Free Software Foundation, Inc.
4
5   Miscellaneous functions.
6
7   Written by Klaus K"ampf (kkaempf@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
64void
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
99void
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
138/* Copy sized string (string with fixed size) to new allocated area.
139   Size is string size (size of record).  */
140
141char *
142_bfd_vms_save_sized_string (bfd *abfd, unsigned char *str, size_t size)
143{
144  char *newstr;
145
146  if (size == (size_t) -1)
147    {
148      bfd_set_error (bfd_error_no_memory);
149      return NULL;
150    }
151  newstr = bfd_alloc (abfd, size + 1);
152  if (newstr == NULL)
153    return NULL;
154  memcpy (newstr, str, size);
155  newstr[size] = 0;
156
157  return newstr;
158}
159
160/* Copy counted string (string with size at first byte) to new allocated area.
161   PTR points to size byte on entry.  */
162
163char *
164_bfd_vms_save_counted_string (bfd *abfd, unsigned char *ptr, size_t maxlen)
165{
166  unsigned int len = *ptr++;
167
168  if (len > maxlen)
169    return NULL;
170  return _bfd_vms_save_sized_string (abfd, ptr, len);
171}
172
173/* Object output routines.   */
174
175/* Begin new record.
176   Write 2 bytes rectype and 2 bytes record length.  */
177
178void
179_bfd_vms_output_begin (struct vms_rec_wr *recwr, int rectype)
180{
181  vms_debug2 ((6, "_bfd_vms_output_begin (type %d)\n", rectype));
182
183  /* Record must have been closed.  */
184  BFD_ASSERT (recwr->size == 0);
185
186  _bfd_vms_output_short (recwr, (unsigned int) rectype);
187
188  /* Placeholder for length.  */
189  _bfd_vms_output_short (recwr, 0);
190}
191
192/* Begin new sub-record.
193   Write 2 bytes rectype, and 2 bytes record length.  */
194
195void
196_bfd_vms_output_begin_subrec (struct vms_rec_wr *recwr, int rectype)
197{
198  vms_debug2 ((6, "_bfd_vms_output_begin_subrec (type %d)\n", rectype));
199
200  /* Subrecord must have been closed.  */
201  BFD_ASSERT (recwr->subrec_offset == 0);
202
203  /* Save start of subrecord offset.  */
204  recwr->subrec_offset = recwr->size;
205
206  /* Subrecord type.  */
207  _bfd_vms_output_short (recwr, (unsigned int) rectype);
208
209  /* Placeholder for length.  */
210  _bfd_vms_output_short (recwr, 0);
211}
212
213/* Set record/subrecord alignment.   */
214
215void
216_bfd_vms_output_alignment (struct vms_rec_wr *recwr, int alignto)
217{
218  vms_debug2 ((6, "_bfd_vms_output_alignment (%d)\n", alignto));
219  recwr->align = alignto;
220}
221
222/* Align the size of the current record (whose length is LENGTH).
223   Warning: this obviously changes the record (and the possible subrecord)
224   length.  */
225
226static void
227_bfd_vms_output_align (struct vms_rec_wr *recwr, unsigned int length)
228{
229  unsigned int real_size = recwr->size;
230  unsigned int aligncount;
231
232  /* Pad with 0 if alignment is required.  */
233  aligncount = (recwr->align - (length % recwr->align)) % recwr->align;
234  vms_debug2 ((6, "align: adding %d bytes\n", aligncount));
235  while (aligncount-- > 0)
236    recwr->buf[real_size++] = 0;
237
238  recwr->size = real_size;
239}
240
241/* Ends current sub-record.  Set length field.  */
242
243void
244_bfd_vms_output_end_subrec (struct vms_rec_wr *recwr)
245{
246  int real_size = recwr->size;
247  int length;
248
249  /* Subrecord must be open.  */
250  BFD_ASSERT (recwr->subrec_offset != 0);
251
252  length = real_size - recwr->subrec_offset;
253
254  if (length == 0)
255    return;
256
257  _bfd_vms_output_align (recwr, length);
258
259  /* Put length to buffer.  */
260  bfd_putl16 ((bfd_vma) (recwr->size - recwr->subrec_offset),
261	      recwr->buf + recwr->subrec_offset + 2);
262
263  /* Close the subrecord.  */
264  recwr->subrec_offset = 0;
265}
266
267/* Ends current record (and write it).  */
268
269void
270_bfd_vms_output_end (bfd *abfd, struct vms_rec_wr *recwr)
271{
272  vms_debug2 ((6, "_bfd_vms_output_end (size %u)\n", recwr->size));
273
274  /* Subrecord must have been closed.  */
275  BFD_ASSERT (recwr->subrec_offset == 0);
276
277  if (recwr->size == 0)
278    return;
279
280  _bfd_vms_output_align (recwr, recwr->size);
281
282  /* Write the length word.  */
283  bfd_putl16 ((bfd_vma) recwr->size, recwr->buf + 2);
284
285  /* File is open in undefined (UDF) format on VMS, but ultimately will be
286     converted to variable length (VAR) format.  VAR format has a length
287     word first which must be explicitly output in UDF format.  */
288  /* So, first the length word.  */
289  bfd_bwrite (recwr->buf + 2, 2, abfd);
290
291  /* Align.  */
292  if (recwr->size & 1)
293    recwr->buf[recwr->size++] = 0;
294
295  /* Then the record.  */
296  bfd_bwrite (recwr->buf, (size_t) recwr->size, abfd);
297
298  recwr->size = 0;
299}
300
301/* Check remaining buffer size.  Return what's left.  */
302
303int
304_bfd_vms_output_check (struct vms_rec_wr *recwr, int size)
305{
306  vms_debug2 ((6, "_bfd_vms_output_check (%d)\n", size));
307
308  return (MAX_OUTREC_SIZE - (recwr->size + size + MIN_OUTREC_LUFT));
309}
310
311/* Output byte (8 bit) value.  */
312
313void
314_bfd_vms_output_byte (struct vms_rec_wr *recwr, unsigned int value)
315{
316  vms_debug2 ((6, "_bfd_vms_output_byte (%02x)\n", value));
317
318  *(recwr->buf + recwr->size) = value;
319  recwr->size += 1;
320}
321
322/* Output short (16 bit) value.  */
323
324void
325_bfd_vms_output_short (struct vms_rec_wr *recwr, unsigned int value)
326{
327  vms_debug2 ((6, "_bfd_vms_output_short (%04x)\n", value));
328
329  bfd_putl16 ((bfd_vma) value & 0xffff, recwr->buf + recwr->size);
330  recwr->size += 2;
331}
332
333/* Output long (32 bit) value.  */
334
335void
336_bfd_vms_output_long (struct vms_rec_wr *recwr, unsigned long value)
337{
338  vms_debug2 ((6, "_bfd_vms_output_long (%08lx)\n", value));
339
340  bfd_putl32 ((bfd_vma) value, recwr->buf + recwr->size);
341  recwr->size += 4;
342}
343
344/* Output quad (64 bit) value.  */
345
346void
347_bfd_vms_output_quad (struct vms_rec_wr *recwr, bfd_vma value)
348{
349  vms_debug2 ((6, "_bfd_vms_output_quad (%08lx)\n", (unsigned long)value));
350
351  bfd_putl64 (value, recwr->buf + recwr->size);
352  recwr->size += 8;
353}
354
355/* Output c-string as counted string.  */
356
357void
358_bfd_vms_output_counted (struct vms_rec_wr *recwr, const char *value)
359{
360  int len;
361
362  vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value));
363
364  len = strlen (value);
365  if (len == 0)
366    {
367      _bfd_error_handler (_("_bfd_vms_output_counted called with zero bytes"));
368      return;
369    }
370  if (len > 255)
371    {
372      _bfd_error_handler (_("_bfd_vms_output_counted called with too many bytes"));
373      return;
374    }
375  _bfd_vms_output_byte (recwr, (unsigned int) len & 0xff);
376  _bfd_vms_output_dump (recwr, (const unsigned char *)value, len);
377}
378
379/* Output character area.  */
380
381void
382_bfd_vms_output_dump (struct vms_rec_wr *recwr, const unsigned char *data, int len)
383{
384  vms_debug2 ((6, "_bfd_vms_output_dump (%d)\n", len));
385
386  if (len == 0)
387    return;
388
389  memcpy (recwr->buf + recwr->size, data, (size_t) len);
390  recwr->size += len;
391}
392
393/* Output count bytes of value.  */
394
395void
396_bfd_vms_output_fill (struct vms_rec_wr *recwr, int value, int count)
397{
398  vms_debug2 ((6, "_bfd_vms_output_fill (val %02x times %d)\n", value, count));
399
400  if (count == 0)
401    return;
402  memset (recwr->buf + recwr->size, value, (size_t) count);
403  recwr->size += count;
404}
405
406#ifdef VMS
407/* Convert the file to variable record length format. This is done
408   using undocumented system call sys$modify().
409   Pure VMS version.  */
410
411static void
412vms_convert_to_var (char * vms_filename)
413{
414  struct FAB fab = cc$rms_fab;
415
416  fab.fab$l_fna = vms_filename;
417  fab.fab$b_fns = strlen (vms_filename);
418  fab.fab$b_fac = FAB$M_PUT;
419  fab.fab$l_fop = FAB$M_ESC;
420  fab.fab$l_ctx = RME$C_SETRFM;
421
422  sys$open (&fab);
423
424  fab.fab$b_rfm = FAB$C_VAR;
425
426  sys$modify (&fab);
427  sys$close (&fab);
428}
429
430static int
431vms_convert_to_var_1 (char *filename, int type)
432{
433  if (type != DECC$K_FILE)
434    return FALSE;
435  vms_convert_to_var (filename);
436  return TRUE;
437}
438
439/* Convert the file to variable record length format. This is done
440   using undocumented system call sys$modify().
441   Unix filename version.  */
442
443int
444_bfd_vms_convert_to_var_unix_filename (const char *unix_filename)
445{
446  if (decc$to_vms (unix_filename, &vms_convert_to_var_1, 0, 1) != 1)
447    return FALSE;
448  return TRUE;
449}
450#endif /* VMS */
451
452/* Manufacture a VMS like time on a unix based system.
453   stolen from obj-vms.c.  */
454
455unsigned char *
456get_vms_time_string (void)
457{
458  static unsigned char tbuf[18];
459#ifndef VMS
460  char *pnt;
461  time_t timeb;
462
463  time (& timeb);
464  pnt = ctime (&timeb);
465  pnt[3] = 0;
466  pnt[7] = 0;
467  pnt[10] = 0;
468  pnt[16] = 0;
469  pnt[24] = 0;
470  sprintf ((char *) tbuf, "%2s-%3s-%s %s",
471	   pnt + 8, pnt + 4, pnt + 20, pnt + 11);
472#else
473  struct
474  {
475    int Size;
476    unsigned char *Ptr;
477  } Descriptor;
478  Descriptor.Size = 17;
479  Descriptor.Ptr = tbuf;
480  SYS$ASCTIM (0, &Descriptor, 0, 0);
481#endif /* not VMS */
482
483  vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf));
484
485  return tbuf;
486}
487
488/* Create module name from filename (ie, extract the basename and convert it
489   in upper cases).  Works on both VMS and UNIX pathes.
490   The result has to be free().  */
491
492char *
493vms_get_module_name (const char *filename, bfd_boolean upcase)
494{
495  char *fname, *fptr;
496  const char *fout;
497
498  /* Strip VMS path.  */
499  fout = strrchr (filename, ']');
500  if (fout == NULL)
501    fout = strchr (filename, ':');
502  if (fout != NULL)
503    fout++;
504  else
505    fout = filename;
506
507  /* Strip UNIX path.  */
508  fptr = strrchr (fout, '/');
509  if (fptr != NULL)
510    fout = fptr + 1;
511
512  fname = strdup (fout);
513
514  /* Strip suffix.  */
515  fptr = strrchr (fname, '.');
516  if (fptr != 0)
517    *fptr = 0;
518
519  /* Convert to upper case and truncate at 31 characters.
520     (VMS object file format restricts module name length to 31).  */
521  fptr = fname;
522  for (fptr = fname; *fptr != 0; fptr++)
523    {
524      if (*fptr == ';' || (fptr - fname) >= 31)
525	{
526	  *fptr = 0;
527	  break;
528	}
529      if (upcase)
530	*fptr = TOUPPER (*fptr);
531    }
532  return fname;
533}
534
535/* Compared to usual UNIX time_t, VMS time has less limits:
536   -  64 bit (63 bits in fact as the MSB must be 0)
537   -  100ns granularity
538   -  epoch is Nov 17, 1858.
539   Here has the constants and the routines used to convert VMS from/to UNIX time.
540   The conversion routines don't assume 64 bits arithmetic.
541
542   Here we assume that the definition of time_t is the UNIX one, ie integer
543   type, expressing seconds since the epoch.  */
544
545/* UNIX time granularity for VMS, ie 1s / 100ns.  */
546#define VMS_TIME_FACTOR 10000000
547
548/* Number of seconds since VMS epoch of the UNIX epoch.  */
549#define VMS_TIME_OFFSET 3506716800U
550
551/* Convert a VMS time to a unix time.  */
552
553time_t
554vms_time_to_time_t (unsigned int hi, unsigned int lo)
555{
556  unsigned int tmp;
557  unsigned int rlo;
558  int i;
559  time_t res;
560
561  /* First convert to seconds.  */
562  tmp = hi % VMS_TIME_FACTOR;
563  hi = hi / VMS_TIME_FACTOR;
564  rlo = 0;
565  for (i = 0; i < 4; i++)
566    {
567      tmp = (tmp << 8) | (lo >> 24);
568      lo <<= 8;
569
570      rlo = (rlo << 8) | (tmp / VMS_TIME_FACTOR);
571      tmp %= VMS_TIME_FACTOR;
572    }
573  lo = rlo;
574
575  /* Return 0 in case of overflow.  */
576  if (hi > 1
577      || (hi == 1 && lo >= VMS_TIME_OFFSET))
578    return 0;
579
580  /* Return 0 in case of underflow.  */
581  if (hi == 0 && lo < VMS_TIME_OFFSET)
582    return 0;
583
584  res = lo - VMS_TIME_OFFSET;
585  if (res <= 0)
586    return 0;
587  return res;
588}
589
590/* Convert a time_t to a VMS time.  */
591
592void
593vms_time_t_to_vms_time (time_t ut, unsigned int *hi, unsigned int *lo)
594{
595  unsigned short val[4];
596  unsigned short tmp[4];
597  unsigned int carry;
598  int i;
599
600  /* Put into val.  */
601  val[0] = ut & 0xffff;
602  val[1] = (ut >> 16) & 0xffff;
603  val[2] = sizeof (ut) > 4 ? (ut >> 32) & 0xffff : 0;
604  val[3] = sizeof (ut) > 4 ? (ut >> 48) & 0xffff : 0;
605
606  /* Add offset.  */
607  tmp[0] = VMS_TIME_OFFSET & 0xffff;
608  tmp[1] = VMS_TIME_OFFSET >> 16;
609  tmp[2] = 0;
610  tmp[3] = 0;
611  carry = 0;
612  for (i = 0; i < 4; i++)
613    {
614      carry += tmp[i] + val[i];
615      val[i] = carry & 0xffff;
616      carry = carry >> 16;
617    }
618
619  /* Multiply by factor, well first by 10000 and then by 1000.  */
620  carry = 0;
621  for (i = 0; i < 4; i++)
622    {
623      carry += val[i] * 10000;
624      val[i] = carry & 0xffff;
625      carry = carry >> 16;
626    }
627  carry = 0;
628  for (i = 0; i < 4; i++)
629    {
630      carry += val[i] * 1000;
631      val[i] = carry & 0xffff;
632      carry = carry >> 16;
633    }
634
635  /* Write the result.  */
636  *lo = val[0] | (val[1] << 16);
637  *hi = val[2] | (val[3] << 16);
638}
639
640/* Convert a raw (stored in a buffer) VMS time to a unix time.  */
641
642time_t
643vms_rawtime_to_time_t (unsigned char *buf)
644{
645  unsigned int hi = bfd_getl32 (buf + 4);
646  unsigned int lo = bfd_getl32 (buf + 0);
647
648  return vms_time_to_time_t (hi, lo);
649}
650
651void
652vms_get_time (unsigned int *hi, unsigned int *lo)
653{
654#ifdef VMS
655  struct _generic_64 t;
656
657  sys$gettim (&t);
658  *lo = t.gen64$q_quadword;
659  *hi = t.gen64$q_quadword >> 32;
660#else
661  time_t t;
662
663  time (&t);
664  vms_time_t_to_vms_time (t, hi, lo);
665#endif
666}
667
668/* Get the current time into a raw buffer BUF.  */
669
670void
671vms_raw_get_time (unsigned char *buf)
672{
673  unsigned int hi, lo;
674
675  vms_get_time (&hi, &lo);
676  bfd_putl32 (lo, buf + 0);
677  bfd_putl32 (hi, buf + 4);
678}
679