Mention PR ld/12327 in ChangeLog entry.
[binutils.git] / bfd / vms-misc.c
bloba2665b7e4f938986a45aeb3e5394278ff8b5d23a
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. */
25 #if __STDC__
26 #include <stdarg.h>
27 #endif
29 #include "sysdep.h"
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "safe-ctype.h"
35 #ifdef VMS
36 #define __NEW_STARLET
37 #include <rms.h>
38 #include <unixlib.h>
39 #include <gen64def.h>
40 #include <starlet.h>
41 #define RME$C_SETRFM 0x00000001
42 #include <unistd.h>
43 #endif
44 #include <time.h>
46 #include "vms.h"
47 #include "vms/emh.h"
49 #if VMS_DEBUG
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:
56 Levels:
57 1 toplevel bfd calls (functions from the bfd vector)
58 2 functions called by bfd calls
59 ...
60 9 almost everything
62 Level is also indentation level. Indentation is performed
63 if level > 0. */
65 void
66 _bfd_vms_debug (int level, char *format, ...)
68 static int min_level = -1;
69 static FILE *output = NULL;
70 char *eptr;
71 va_list args;
72 int abslvl = (level > 0) ? level : - level;
74 if (min_level == -1)
76 if ((eptr = getenv ("VMS_DEBUG")) != NULL)
78 min_level = atoi (eptr);
79 output = stderr;
81 else
82 min_level = 0;
84 if (output == NULL)
85 return;
86 if (abslvl > min_level)
87 return;
89 while (--level > 0)
90 fprintf (output, " ");
91 va_start (args, format);
92 vfprintf (output, format, args);
93 fflush (output);
94 va_end (args);
97 /* A debug function
98 hex dump 'size' bytes starting at 'ptr'. */
100 void
101 _bfd_hexdump (int level, unsigned char *ptr, int size, int offset)
103 unsigned char *lptr = ptr;
104 int count = 0;
105 long start = offset;
107 while (size-- > 0)
109 if ((count % 16) == 0)
110 vms_debug (level, "%08lx:", start);
111 vms_debug (-level, " %02x", *ptr++);
112 count++;
113 start++;
114 if (size == 0)
116 while ((count % 16) != 0)
118 vms_debug (-level, " ");
119 count++;
122 if ((count % 16) == 0)
124 vms_debug (-level, " ");
125 while (lptr < ptr)
127 vms_debug (-level, "%c", (*lptr < 32) ? '.' : *lptr);
128 lptr++;
130 vms_debug (-level, "\n");
133 if ((count % 16) != 0)
134 vms_debug (-level, "\n");
136 #endif
139 /* Copy sized string (string with fixed size) to new allocated area
140 size is string size (size of record) */
142 char *
143 _bfd_vms_save_sized_string (unsigned char *str, int size)
145 char *newstr = bfd_malloc ((bfd_size_type) size + 1);
147 if (newstr == NULL)
148 return NULL;
149 memcpy (newstr, (char *) str, (size_t) size);
150 newstr[size] = 0;
152 return newstr;
155 /* Copy counted string (string with size at first byte) to new allocated area
156 ptr points to size byte on entry */
158 char *
159 _bfd_vms_save_counted_string (unsigned char *ptr)
161 int len = *ptr++;
163 return _bfd_vms_save_sized_string (ptr, len);
166 /* Object output routines. */
168 /* Begin new record.
169 Write 2 bytes rectype and 2 bytes record length. */
171 void
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. */
188 void
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. */
208 void
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)
217 length. */
219 static void
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. */
236 void
237 _bfd_vms_output_end_subrec (struct vms_rec_wr *recwr)
239 int real_size = recwr->size;
240 int length;
242 /* Subrecord must be open. */
243 BFD_ASSERT (recwr->subrec_offset != 0);
245 length = real_size - recwr->subrec_offset;
247 if (length == 0)
248 return;
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). */
262 void
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)
271 return;
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);
284 /* Align. */
285 if (recwr->size & 1)
286 recwr->buf[recwr->size++] = 0;
288 /* Then the record. */
289 bfd_bwrite (recwr->buf, (size_t) recwr->size, abfd);
291 recwr->size = 0;
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. */
306 void
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;
312 recwr->size += 1;
315 /* Output short (16 bit) value. */
317 void
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);
323 recwr->size += 2;
326 /* Output long (32 bit) value. */
328 void
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);
334 recwr->size += 4;
337 /* Output quad (64 bit) value. */
339 void
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);
345 recwr->size += 8;
348 /* Output c-string as counted string. */
350 void
351 _bfd_vms_output_counted (struct vms_rec_wr *recwr, const char *value)
353 int len;
355 vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value));
357 len = strlen (value);
358 if (len == 0)
360 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with zero bytes"));
361 return;
363 if (len > 255)
365 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with too many bytes"));
366 return;
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. */
374 void
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));
379 if (len == 0)
380 return;
382 memcpy (recwr->buf + recwr->size, data, (size_t) len);
383 recwr->size += len;
386 /* Output count bytes of value. */
388 void
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));
393 if (count == 0)
394 return;
395 memset (recwr->buf + recwr->size, value, (size_t) count);
396 recwr->size += count;
399 #ifdef VMS
400 /* Convert the file to variable record length format. This is done
401 using undocumented system call sys$modify().
402 Pure VMS version. */
404 static void
405 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;
415 sys$open (&fab);
417 fab.fab$b_rfm = FAB$C_VAR;
419 sys$modify (&fab);
420 sys$close (&fab);
423 static int
424 vms_convert_to_var_1 (char *filename, int type)
426 if (type != DECC$K_FILE)
427 return FALSE;
428 vms_convert_to_var (filename);
429 return TRUE;
432 /* Convert the file to variable record length format. This is done
433 using undocumented system call sys$modify().
434 Unix filename version. */
437 _bfd_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)
440 return FALSE;
441 return TRUE;
443 #endif /* VMS */
445 /* Manufacture a VMS like time on a unix based system.
446 stolen from obj-vms.c. */
448 unsigned char *
449 get_vms_time_string (void)
451 static unsigned char tbuf[18];
452 #ifndef VMS
453 char *pnt;
454 time_t timeb;
456 time (& timeb);
457 pnt = ctime (&timeb);
458 pnt[3] = 0;
459 pnt[7] = 0;
460 pnt[10] = 0;
461 pnt[16] = 0;
462 pnt[24] = 0;
463 sprintf ((char *) tbuf, "%2s-%3s-%s %s",
464 pnt + 8, pnt + 4, pnt + 20, pnt + 11);
465 #else
466 struct
468 int Size;
469 unsigned char *Ptr;
470 } Descriptor;
471 Descriptor.Size = 17;
472 Descriptor.Ptr = tbuf;
473 SYS$ASCTIM (0, &Descriptor, 0, 0);
474 #endif /* not VMS */
476 vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf));
478 return 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(). */
485 char *
486 vms_get_module_name (const char *filename, bfd_boolean upcase)
488 char *fname, *fptr;
489 const char *fout;
491 /* Strip VMS path. */
492 fout = strrchr (filename, ']');
493 if (fout == NULL)
494 fout = strchr (filename, ':');
495 if (fout != NULL)
496 fout++;
497 else
498 fout = filename;
500 /* Strip UNIX path. */
501 fptr = strrchr (fout, '/');
502 if (fptr != NULL)
503 fout = fptr + 1;
505 fname = strdup (fout);
507 /* Strip suffix. */
508 fptr = strrchr (fname, '.');
509 if (fptr != 0)
510 *fptr = 0;
512 /* Convert to upper case and truncate at 31 characters.
513 (VMS object file format restricts module name length to 31). */
514 fptr = fname;
515 for (fptr = fname; *fptr != 0; fptr++)
517 if (*fptr == ';' || (fptr - fname) >= 31)
519 *fptr = 0;
520 break;
522 if (upcase)
523 *fptr = TOUPPER (*fptr);
525 return fname;
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)
530 - 100ns granularity
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. */
543 time_t
544 vms_time_to_time_t (unsigned int hi, unsigned int lo)
546 unsigned int tmp;
547 unsigned int rlo;
548 int i;
550 /* First convert to seconds. */
551 tmp = hi % VMS_TIME_FACTOR;
552 hi = hi / VMS_TIME_FACTOR;
553 rlo = 0;
554 for (i = 0; i < 4; i++)
556 tmp = (tmp << 8) | (lo >> 24);
557 lo <<= 8;
559 rlo = (rlo << 8) | (tmp / VMS_TIME_FACTOR);
560 tmp %= VMS_TIME_FACTOR;
562 lo = rlo;
564 /* Return 0 in case of overflow. */
565 if (lo > VMS_TIME_OFFSET && hi > 1)
566 return 0;
568 /* Return 0 in case of underflow. */
569 if (lo < VMS_TIME_OFFSET)
570 return 0;
572 return lo - VMS_TIME_OFFSET;
575 /* Convert a time_t to a VMS time. */
577 void
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];
582 unsigned int carry;
583 int i;
585 /* Put into val. */
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;
591 /* Add offset. */
592 tmp[0] = VMS_TIME_OFFSET & 0xffff;
593 tmp[1] = VMS_TIME_OFFSET >> 16;
594 tmp[2] = 0;
595 tmp[3] = 0;
596 carry = 0;
597 for (i = 0; i < 4; i++)
599 carry += tmp[i] + val[i];
600 val[i] = carry & 0xffff;
601 carry = carry >> 16;
604 /* Multiply by factor, well first by 10000 and then by 1000. */
605 carry = 0;
606 for (i = 0; i < 4; i++)
608 carry += val[i] * 10000;
609 val[i] = carry & 0xffff;
610 carry = carry >> 16;
612 carry = 0;
613 for (i = 0; i < 4; i++)
615 carry += val[i] * 1000;
616 val[i] = carry & 0xffff;
617 carry = carry >> 16;
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. */
627 time_t
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);
636 void
637 vms_get_time (unsigned int *hi, unsigned int *lo)
639 #ifdef VMS
640 struct _generic_64 t;
642 sys$gettim (&t);
643 *lo = t.gen64$q_quadword;
644 *hi = t.gen64$q_quadword >> 32;
645 #else
646 time_t t;
648 time (&t);
649 vms_time_t_to_vms_time (t, hi, lo);
650 #endif
653 /* Get the current time into a raw buffer BUF. */
655 void
656 vms_raw_get_time (unsigned char *buf)
658 unsigned int hi, lo;
660 vms_get_time (&hi, &lo);
661 bfd_putl32 (lo, buf + 0);
662 bfd_putl32 (hi, buf + 4);