1 /* vms-misc.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 Miscellaneous functions.
8 Written by Klaus K"ampf (kkaempf@rmi.de)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
33 #include "safe-ctype.h"
41 #define RME$C_SETRFM 0x00000001
50 /* Debug functions. */
52 /* Debug function for all vms extensions evaluates environment
53 variable VMS_DEBUG for a numerical value on the first call all
54 error levels below this value are printed:
57 1 toplevel bfd calls (functions from the bfd vector)
58 2 functions called by bfd calls
62 Level is also indentation level. Indentation is performed
66 _bfd_vms_debug (int level, char *format, ...)
68 static int min_level = -1;
69 static FILE *output = NULL;
72 int abslvl = (level > 0) ? level : - level;
76 if ((eptr = getenv ("VMS_DEBUG")) != NULL)
78 min_level = atoi (eptr);
86 if (abslvl > min_level)
90 fprintf (output, " ");
91 va_start (args, format);
92 vfprintf (output, format, args);
98 hex dump 'size' bytes starting at 'ptr'. */
101 _bfd_hexdump (int level, unsigned char *ptr, int size, int offset)
103 unsigned char *lptr = ptr;
109 if ((count % 16) == 0)
110 vms_debug (level, "%08lx:", start);
111 vms_debug (-level, " %02x", *ptr++);
116 while ((count % 16) != 0)
118 vms_debug (-level, " ");
122 if ((count % 16) == 0)
124 vms_debug (-level, " ");
127 vms_debug (-level, "%c", (*lptr < 32) ? '.' : *lptr);
130 vms_debug (-level, "\n");
133 if ((count % 16) != 0)
134 vms_debug (-level, "\n");
139 /* Copy sized string (string with fixed size) to new allocated area
140 size is string size (size of record) */
143 _bfd_vms_save_sized_string (unsigned char *str, int size)
145 char *newstr = bfd_malloc ((bfd_size_type) size + 1);
149 memcpy (newstr, (char *) str, (size_t) size);
155 /* Copy counted string (string with size at first byte) to new allocated area
156 ptr points to size byte on entry */
159 _bfd_vms_save_counted_string (unsigned char *ptr)
163 return _bfd_vms_save_sized_string (ptr, len);
166 /* Object output routines. */
169 Write 2 bytes rectype and 2 bytes record length. */
172 _bfd_vms_output_begin (struct vms_rec_wr *recwr, int rectype)
174 vms_debug2 ((6, "_bfd_vms_output_begin (type %d)\n", rectype));
176 /* Record must have been closed. */
177 BFD_ASSERT (recwr->size == 0);
179 _bfd_vms_output_short (recwr, (unsigned int) rectype);
181 /* Placeholder for length. */
182 _bfd_vms_output_short (recwr, 0);
185 /* Begin new sub-record.
186 Write 2 bytes rectype, and 2 bytes record length. */
189 _bfd_vms_output_begin_subrec (struct vms_rec_wr *recwr, int rectype)
191 vms_debug2 ((6, "_bfd_vms_output_begin_subrec (type %d)\n", rectype));
193 /* Subrecord must have been closed. */
194 BFD_ASSERT (recwr->subrec_offset == 0);
196 /* Save start of subrecord offset. */
197 recwr->subrec_offset = recwr->size;
199 /* Subrecord type. */
200 _bfd_vms_output_short (recwr, (unsigned int) rectype);
202 /* Placeholder for length. */
203 _bfd_vms_output_short (recwr, 0);
206 /* Set record/subrecord alignment. */
209 _bfd_vms_output_alignment (struct vms_rec_wr *recwr, int alignto)
211 vms_debug2 ((6, "_bfd_vms_output_alignment (%d)\n", alignto));
212 recwr->align = alignto;
215 /* Align the size of the current record (whose length is LENGTH).
216 Warning: this obviously changes the record (and the possible subrecord)
220 _bfd_vms_output_align (struct vms_rec_wr *recwr, unsigned int length)
222 unsigned int real_size = recwr->size;
223 unsigned int aligncount;
225 /* Pad with 0 if alignment is required. */
226 aligncount = (recwr->align - (length % recwr->align)) % recwr->align;
227 vms_debug2 ((6, "align: adding %d bytes\n", aligncount));
228 while (aligncount-- > 0)
229 recwr->buf[real_size++] = 0;
231 recwr->size = real_size;
234 /* Ends current sub-record. Set length field. */
237 _bfd_vms_output_end_subrec (struct vms_rec_wr *recwr)
239 int real_size = recwr->size;
242 /* Subrecord must be open. */
243 BFD_ASSERT (recwr->subrec_offset != 0);
245 length = real_size - recwr->subrec_offset;
250 _bfd_vms_output_align (recwr, length);
252 /* Put length to buffer. */
253 bfd_putl16 ((bfd_vma) (recwr->size - recwr->subrec_offset),
254 recwr->buf + recwr->subrec_offset + 2);
256 /* Close the subrecord. */
257 recwr->subrec_offset = 0;
260 /* Ends current record (and write it). */
263 _bfd_vms_output_end (bfd *abfd, struct vms_rec_wr *recwr)
265 vms_debug2 ((6, "_bfd_vms_output_end (size %u)\n", recwr->size));
267 /* Subrecord must have been closed. */
268 BFD_ASSERT (recwr->subrec_offset == 0);
270 if (recwr->size == 0)
273 _bfd_vms_output_align (recwr, recwr->size);
275 /* Write the length word. */
276 bfd_putl16 ((bfd_vma) recwr->size, recwr->buf + 2);
278 /* File is open in undefined (UDF) format on VMS, but ultimately will be
279 converted to variable length (VAR) format. VAR format has a length
280 word first which must be explicitly output in UDF format. */
281 /* So, first the length word. */
282 bfd_bwrite (recwr->buf + 2, 2, abfd);
286 recwr->buf[recwr->size++] = 0;
288 /* Then the record. */
289 bfd_bwrite (recwr->buf, (size_t) recwr->size, abfd);
294 /* Check remaining buffer size. Return what's left. */
297 _bfd_vms_output_check (struct vms_rec_wr *recwr, int size)
299 vms_debug2 ((6, "_bfd_vms_output_check (%d)\n", size));
301 return (MAX_OUTREC_SIZE - (recwr->size + size + MIN_OUTREC_LUFT));
304 /* Output byte (8 bit) value. */
307 _bfd_vms_output_byte (struct vms_rec_wr *recwr, unsigned int value)
309 vms_debug2 ((6, "_bfd_vms_output_byte (%02x)\n", value));
311 *(recwr->buf + recwr->size) = value;
315 /* Output short (16 bit) value. */
318 _bfd_vms_output_short (struct vms_rec_wr *recwr, unsigned int value)
320 vms_debug2 ((6, "_bfd_vms_output_short (%04x)\n", value));
322 bfd_putl16 ((bfd_vma) value & 0xffff, recwr->buf + recwr->size);
326 /* Output long (32 bit) value. */
329 _bfd_vms_output_long (struct vms_rec_wr *recwr, unsigned long value)
331 vms_debug2 ((6, "_bfd_vms_output_long (%08lx)\n", value));
333 bfd_putl32 ((bfd_vma) value, recwr->buf + recwr->size);
337 /* Output quad (64 bit) value. */
340 _bfd_vms_output_quad (struct vms_rec_wr *recwr, bfd_vma value)
342 vms_debug2 ((6, "_bfd_vms_output_quad (%08lx)\n", (unsigned long)value));
344 bfd_putl64 (value, recwr->buf + recwr->size);
348 /* Output c-string as counted string. */
351 _bfd_vms_output_counted (struct vms_rec_wr *recwr, const char *value)
355 vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value));
357 len = strlen (value);
360 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with zero bytes"));
365 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with too many bytes"));
368 _bfd_vms_output_byte (recwr, (unsigned int) len & 0xff);
369 _bfd_vms_output_dump (recwr, (const unsigned char *)value, len);
372 /* Output character area. */
375 _bfd_vms_output_dump (struct vms_rec_wr *recwr, const unsigned char *data, int len)
377 vms_debug2 ((6, "_bfd_vms_output_dump (%d)\n", len));
382 memcpy (recwr->buf + recwr->size, data, (size_t) len);
386 /* Output count bytes of value. */
389 _bfd_vms_output_fill (struct vms_rec_wr *recwr, int value, int count)
391 vms_debug2 ((6, "_bfd_vms_output_fill (val %02x times %d)\n", value, count));
395 memset (recwr->buf + recwr->size, value, (size_t) count);
396 recwr->size += count;
400 /* Convert the file to variable record length format. This is done
401 using undocumented system call sys$modify().
405 _bfd_vms_convert_to_var (char * vms_filename)
407 struct FAB fab = cc$rms_fab;
409 fab.fab$l_fna = vms_filename;
410 fab.fab$b_fns = strlen (vms_filename);
411 fab.fab$b_fac = FAB$M_PUT;
412 fab.fab$l_fop = FAB$M_ESC;
413 fab.fab$l_ctx = RME$C_SETRFM;
417 fab.fab$b_rfm = FAB$C_VAR;
424 vms_convert_to_var_1 (char *filename, int type)
426 if (type != DECC$K_FILE)
428 vms_convert_to_var (filename);
432 /* Convert the file to variable record length format. This is done
433 using undocumented system call sys$modify().
434 Unix filename version. */
437 vms_convert_to_var_unix_filename (const char *unix_filename)
439 if (decc$to_vms (unix_filename, &vms_convert_to_var_1, 0, 1) != 1)
445 /* Manufacture a VMS like time on a unix based system.
446 stolen from obj-vms.c. */
449 get_vms_time_string (void)
451 static unsigned char tbuf[18];
457 pnt = ctime (&timeb);
463 sprintf ((char *) tbuf, "%2s-%3s-%s %s",
464 pnt + 8, pnt + 4, pnt + 20, pnt + 11);
471 Descriptor.Size = 17;
472 Descriptor.Ptr = tbuf;
473 SYS$ASCTIM (0, &Descriptor, 0, 0);
476 vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf));
481 /* Create module name from filename (ie, extract the basename and convert it
482 in upper cases). Works on both VMS and UNIX pathes.
483 The result has to be free(). */
486 vms_get_module_name (const char *filename, bfd_boolean upcase)
491 /* Strip VMS path. */
492 fout = strrchr (filename, ']');
494 fout = strchr (filename, ':');
500 /* Strip UNIX path. */
501 fptr = strrchr (fout, '/');
505 fname = strdup (fout);
508 fptr = strrchr (fname, '.');
512 /* Convert to upper case and truncate at 31 characters.
513 (VMS object file format restricts module name length to 31). */
515 for (fptr = fname; *fptr != 0; fptr++)
517 if (*fptr == ';' || (fptr - fname) >= 31)
523 *fptr = TOUPPER (*fptr);
528 /* Compared to usual UNIX time_t, VMS time has less limits:
529 - 64 bit (63 bits in fact as the MSB must be 0)
531 - epoch is Nov 17, 1858.
532 Here has the constants and the routines used to convert VMS from/to UNIX time.
533 The conversion routines don't assume 64 bits arithmetic. */
535 /* UNIX time granularity for VMS, ie 1s / 100ns. */
536 #define VMS_TIME_FACTOR 10000000
538 /* Number of seconds since VMS epoch of the UNIX epoch. */
539 #define VMS_TIME_OFFSET 3506716800U
541 /* Convert a VMS time to a unix time. */
544 vms_time_to_time_t (unsigned int hi, unsigned int lo)
550 /* First convert to seconds. */
551 tmp = hi % VMS_TIME_FACTOR;
552 hi = hi / VMS_TIME_FACTOR;
554 for (i = 0; i < 4; i++)
556 tmp = (tmp << 8) | (lo >> 24);
559 rlo = (rlo << 8) | (tmp / VMS_TIME_FACTOR);
560 tmp %= VMS_TIME_FACTOR;
564 /* Return 0 in case of overflow. */
565 if (lo > VMS_TIME_OFFSET && hi > 1)
568 /* Return 0 in case of underflow. */
569 if (lo < VMS_TIME_OFFSET)
572 return lo - VMS_TIME_OFFSET;
575 /* Convert a time_t to a VMS time. */
578 vms_time_t_to_vms_time (time_t ut, unsigned int *hi, unsigned int *lo)
580 unsigned short val[4];
581 unsigned short tmp[4];
586 val[0] = ut & 0xffff;
587 val[1] = (ut >> 16) & 0xffff;
588 val[2] = sizeof (ut) > 4 ? (ut >> 32) & 0xffff : 0;
589 val[3] = sizeof (ut) > 4 ? (ut >> 48) & 0xffff : 0;
592 tmp[0] = VMS_TIME_OFFSET & 0xffff;
593 tmp[1] = VMS_TIME_OFFSET >> 16;
597 for (i = 0; i < 4; i++)
599 carry += tmp[i] + val[i];
600 val[i] = carry & 0xffff;
604 /* Multiply by factor, well first by 10000 and then by 1000. */
606 for (i = 0; i < 4; i++)
608 carry += val[i] * 10000;
609 val[i] = carry & 0xffff;
613 for (i = 0; i < 4; i++)
615 carry += val[i] * 1000;
616 val[i] = carry & 0xffff;
620 /* Write the result. */
621 *lo = val[0] | (val[1] << 16);
622 *hi = val[2] | (val[3] << 16);
625 /* Convert a raw (stored in a buffer) VMS time to a unix time. */
628 vms_rawtime_to_time_t (unsigned char *buf)
630 unsigned int hi = bfd_getl32 (buf + 4);
631 unsigned int lo = bfd_getl32 (buf + 0);
633 return vms_time_to_time_t (hi, lo);
637 vms_get_time (unsigned int *hi, unsigned int *lo)
640 struct _generic_64 t;
643 *lo = t.gen64$q_quadword;
644 *hi = t.gen64$q_quadword >> 32;
649 vms_time_t_to_vms_time (t, hi, lo);
653 /* Get the current time into a raw buffer BUF. */
656 vms_raw_get_time (unsigned char *buf)
660 vms_get_time (&hi, &lo);
661 bfd_putl32 (lo, buf + 0);
662 bfd_putl32 (hi, buf + 4);