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