PR gas/11507
[binutils.git] / bfd / vms-misc.c
blob67cc25adc065efa5d593fcdd67fb6348cd0dccf1
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 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 #include <rms.h>
37 #include <unixlib.h>
38 #include <starlet.h>
39 #define RME$C_SETRFM 0x00000001
40 #include <unistd.h>
41 #endif
42 #include <time.h>
44 #include "vms.h"
45 #include "vms/emh.h"
47 #if VMS_DEBUG
48 /* Debug functions. */
50 /* Debug function for all vms extensions evaluates environment
51 variable VMS_DEBUG for a numerical value on the first call all
52 error levels below this value are printed:
54 Levels:
55 1 toplevel bfd calls (functions from the bfd vector)
56 2 functions called by bfd calls
57 ...
58 9 almost everything
60 Level is also indentation level. Indentation is performed
61 if level > 0. */
63 void
64 _bfd_vms_debug (int level, char *format, ...)
66 static int min_level = -1;
67 static FILE *output = NULL;
68 char *eptr;
69 va_list args;
70 int abslvl = (level > 0) ? level : - level;
72 if (min_level == -1)
74 if ((eptr = getenv ("VMS_DEBUG")) != NULL)
76 min_level = atoi (eptr);
77 output = stderr;
79 else
80 min_level = 0;
82 if (output == NULL)
83 return;
84 if (abslvl > min_level)
85 return;
87 while (--level > 0)
88 fprintf (output, " ");
89 va_start (args, format);
90 vfprintf (output, format, args);
91 fflush (output);
92 va_end (args);
95 /* A debug function
96 hex dump 'size' bytes starting at 'ptr'. */
98 void
99 _bfd_hexdump (int level, unsigned char *ptr, int size, int offset)
101 unsigned char *lptr = ptr;
102 int count = 0;
103 long start = offset;
105 while (size-- > 0)
107 if ((count % 16) == 0)
108 vms_debug (level, "%08lx:", start);
109 vms_debug (-level, " %02x", *ptr++);
110 count++;
111 start++;
112 if (size == 0)
114 while ((count % 16) != 0)
116 vms_debug (-level, " ");
117 count++;
120 if ((count % 16) == 0)
122 vms_debug (-level, " ");
123 while (lptr < ptr)
125 vms_debug (-level, "%c", (*lptr < 32) ? '.' : *lptr);
126 lptr++;
128 vms_debug (-level, "\n");
131 if ((count % 16) != 0)
132 vms_debug (-level, "\n");
134 #endif
137 /* Copy sized string (string with fixed size) to new allocated area
138 size is string size (size of record) */
140 char *
141 _bfd_vms_save_sized_string (unsigned char *str, int size)
143 char *newstr = bfd_malloc ((bfd_size_type) size + 1);
145 if (newstr == NULL)
146 return NULL;
147 memcpy (newstr, (char *) str, (size_t) size);
148 newstr[size] = 0;
150 return newstr;
153 /* Copy counted string (string with size at first byte) to new allocated area
154 ptr points to size byte on entry */
156 char *
157 _bfd_vms_save_counted_string (unsigned char *ptr)
159 int len = *ptr++;
161 return _bfd_vms_save_sized_string (ptr, len);
164 /* Object output routines. */
166 /* Begin new record.
167 Write 2 bytes rectype and 2 bytes record length. */
169 void
170 _bfd_vms_output_begin (struct vms_rec_wr *recwr, int rectype)
172 vms_debug2 ((6, "_bfd_vms_output_begin (type %d)\n", rectype));
174 /* Record must have been closed. */
175 BFD_ASSERT (recwr->size == 0);
177 _bfd_vms_output_short (recwr, (unsigned int) rectype);
179 /* Placeholder for length. */
180 _bfd_vms_output_short (recwr, 0);
183 /* Begin new sub-record.
184 Write 2 bytes rectype, and 2 bytes record length. */
186 void
187 _bfd_vms_output_begin_subrec (struct vms_rec_wr *recwr, int rectype)
189 vms_debug2 ((6, "_bfd_vms_output_begin_subrec (type %d)\n", rectype));
191 /* Subrecord must have been closed. */
192 BFD_ASSERT (recwr->subrec_offset == 0);
194 /* Save start of subrecord offset. */
195 recwr->subrec_offset = recwr->size;
197 /* Subrecord type. */
198 _bfd_vms_output_short (recwr, (unsigned int) rectype);
200 /* Placeholder for length. */
201 _bfd_vms_output_short (recwr, 0);
204 /* Set record/subrecord alignment. */
206 void
207 _bfd_vms_output_alignment (struct vms_rec_wr *recwr, int alignto)
209 vms_debug2 ((6, "_bfd_vms_output_alignment (%d)\n", alignto));
210 recwr->align = alignto;
213 /* Align the size of the current record (whose length is LENGTH).
214 Warning: this obviously changes the record (and the possible subrecord)
215 length. */
217 static void
218 _bfd_vms_output_align (struct vms_rec_wr *recwr, unsigned int length)
220 unsigned int real_size = recwr->size;
221 unsigned int aligncount;
223 /* Pad with 0 if alignment is required. */
224 aligncount = (recwr->align - (length % recwr->align)) % recwr->align;
225 vms_debug2 ((6, "align: adding %d bytes\n", aligncount));
226 while (aligncount-- > 0)
227 recwr->buf[real_size++] = 0;
229 recwr->size = real_size;
232 /* Ends current sub-record. Set length field. */
234 void
235 _bfd_vms_output_end_subrec (struct vms_rec_wr *recwr)
237 int real_size = recwr->size;
238 int length;
240 /* Subrecord must be open. */
241 BFD_ASSERT (recwr->subrec_offset != 0);
243 length = real_size - recwr->subrec_offset;
245 if (length == 0)
246 return;
248 _bfd_vms_output_align (recwr, length);
250 /* Put length to buffer. */
251 bfd_putl16 ((bfd_vma) (recwr->size - recwr->subrec_offset),
252 recwr->buf + recwr->subrec_offset + 2);
254 /* Close the subrecord. */
255 recwr->subrec_offset = 0;
258 /* Ends current record (and write it). */
260 void
261 _bfd_vms_output_end (bfd *abfd, struct vms_rec_wr *recwr)
263 vms_debug2 ((6, "_bfd_vms_output_end (size %u)\n", recwr->size));
265 /* Subrecord must have been closed. */
266 BFD_ASSERT (recwr->subrec_offset == 0);
268 if (recwr->size == 0)
269 return;
271 _bfd_vms_output_align (recwr, recwr->size);
273 /* Write the length word. */
274 bfd_putl16 ((bfd_vma) recwr->size, recwr->buf + 2);
276 /* File is open in undefined (UDF) format on VMS, but ultimately will be
277 converted to variable length (VAR) format. VAR format has a length
278 word first which must be explicitly output in UDF format. */
279 /* So, first the length word. */
280 bfd_bwrite (recwr->buf + 2, 2, abfd);
282 /* Align. */
283 if (recwr->size & 1)
284 recwr->buf[recwr->size++] = 0;
286 /* Then the record. */
287 bfd_bwrite (recwr->buf, (size_t) recwr->size, abfd);
289 recwr->size = 0;
292 /* Check remaining buffer size. Return what's left. */
295 _bfd_vms_output_check (struct vms_rec_wr *recwr, int size)
297 vms_debug2 ((6, "_bfd_vms_output_check (%d)\n", size));
299 return (MAX_OUTREC_SIZE - (recwr->size + size + MIN_OUTREC_LUFT));
302 /* Output byte (8 bit) value. */
304 void
305 _bfd_vms_output_byte (struct vms_rec_wr *recwr, unsigned int value)
307 vms_debug2 ((6, "_bfd_vms_output_byte (%02x)\n", value));
309 *(recwr->buf + recwr->size) = value;
310 recwr->size += 1;
313 /* Output short (16 bit) value. */
315 void
316 _bfd_vms_output_short (struct vms_rec_wr *recwr, unsigned int value)
318 vms_debug2 ((6, "_bfd_vms_output_short (%04x)\n", value));
320 bfd_putl16 ((bfd_vma) value & 0xffff, recwr->buf + recwr->size);
321 recwr->size += 2;
324 /* Output long (32 bit) value. */
326 void
327 _bfd_vms_output_long (struct vms_rec_wr *recwr, unsigned long value)
329 vms_debug2 ((6, "_bfd_vms_output_long (%08lx)\n", value));
331 bfd_putl32 ((bfd_vma) value, recwr->buf + recwr->size);
332 recwr->size += 4;
335 /* Output quad (64 bit) value. */
337 void
338 _bfd_vms_output_quad (struct vms_rec_wr *recwr, bfd_vma value)
340 vms_debug2 ((6, "_bfd_vms_output_quad (%08lx)\n", (unsigned long)value));
342 bfd_putl64 (value, recwr->buf + recwr->size);
343 recwr->size += 8;
346 /* Output c-string as counted string. */
348 void
349 _bfd_vms_output_counted (struct vms_rec_wr *recwr, char *value)
351 int len;
353 vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value));
355 len = strlen (value);
356 if (len == 0)
358 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with zero bytes"));
359 return;
361 if (len > 255)
363 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with too many bytes"));
364 return;
366 _bfd_vms_output_byte (recwr, (unsigned int) len & 0xff);
367 _bfd_vms_output_dump (recwr, (unsigned char *) value, len);
370 /* Output character area. */
372 void
373 _bfd_vms_output_dump (struct vms_rec_wr *recwr, unsigned char *data, int len)
375 vms_debug2 ((6, "_bfd_vms_output_dump (%d)\n", len));
377 if (len == 0)
378 return;
380 memcpy (recwr->buf + recwr->size, data, (size_t) len);
381 recwr->size += len;
384 /* Output count bytes of value. */
386 void
387 _bfd_vms_output_fill (struct vms_rec_wr *recwr, int value, int count)
389 vms_debug2 ((6, "_bfd_vms_output_fill (val %02x times %d)\n", value, count));
391 if (count == 0)
392 return;
393 memset (recwr->buf + recwr->size, value, (size_t) count);
394 recwr->size += count;
397 #ifdef VMS
398 /* Convert the file to variable record length format. This is done
399 using undocumented system call sys$modify().
400 Pure VMS version. */
402 static void
403 vms_convert_to_var (char *vms_filename)
405 struct FAB fab = cc$rms_fab;
407 fab.fab$l_fna = vms_filename;
408 fab.fab$b_fns = strlen (vms_filename);
409 fab.fab$b_fac = FAB$M_PUT;
410 fab.fab$l_fop = FAB$M_ESC;
411 fab.fab$l_ctx = RME$C_SETRFM;
413 sys$open (&fab);
415 fab.fab$b_rfm = FAB$C_VAR;
417 sys$modify (&fab);
418 sys$close (&fab);
421 static int
422 vms_convert_to_var_1 (char *filename, int type)
424 if (type != DECC$K_FILE)
425 return FALSE;
426 vms_convert_to_var (filename);
427 return TRUE;
430 /* Convert the file to variable record length format. This is done
431 using undocumented system call sys$modify().
432 Unix filename version. */
434 static int
435 vms_convert_to_var_unix_filename (const char *unix_filename)
437 if (decc$to_vms (unix_filename, &vms_convert_to_var_1, 0, 1) != 1)
438 return FALSE;
439 return TRUE;
441 #endif /* VMS */
443 /* Manufacture a VMS like time on a unix based system.
444 stolen from obj-vms.c. */
446 unsigned char *
447 get_vms_time_string (void)
449 static unsigned char tbuf[18];
450 #ifndef VMS
451 char *pnt;
452 time_t timeb;
454 time (& timeb);
455 pnt = ctime (&timeb);
456 pnt[3] = 0;
457 pnt[7] = 0;
458 pnt[10] = 0;
459 pnt[16] = 0;
460 pnt[24] = 0;
461 sprintf ((char *) tbuf, "%2s-%3s-%s %s",
462 pnt + 8, pnt + 4, pnt + 20, pnt + 11);
463 #else
464 struct
466 int Size;
467 unsigned char *Ptr;
468 } Descriptor;
469 Descriptor.Size = 17;
470 Descriptor.Ptr = tbuf;
471 SYS$ASCTIM (0, &Descriptor, 0, 0);
472 #endif /* not VMS */
474 vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf));
476 return tbuf;
479 /* Create module name from filename (ie, extract the basename and convert it
480 in upper cases). Works on both VMS and UNIX pathes.
481 The result has to be free(). */
483 char *
484 vms_get_module_name (const char *filename, bfd_boolean upcase)
486 char *fname, *fptr;
487 const char *fout;
489 /* Strip VMS path. */
490 fout = strrchr (filename, ']');
491 if (fout == NULL)
492 fout = strchr (filename, ':');
493 if (fout != NULL)
494 fout++;
495 else
496 fout = filename;
498 /* Strip UNIX path. */
499 fptr = strrchr (fout, '/');
500 if (fptr != NULL)
501 fout = fptr + 1;
503 fname = strdup (fout);
505 /* Strip suffix. */
506 fptr = strrchr (fname, '.');
507 if (fptr != 0)
508 *fptr = 0;
510 /* Convert to upper case and truncate at 31 characters.
511 (VMS object file format restricts module name length to 31). */
512 fptr = fname;
513 for (fptr = fname; *fptr != 0; fptr++)
515 if (*fptr == ';' || (fptr - fname) >= 31)
517 *fptr = 0;
518 break;
520 if (upcase)
521 *fptr = TOUPPER (*fptr);
523 return fname;
526 /* Convert a raw VMS time to a unix time. */
528 time_t
529 vms_time_to_time_t (unsigned int hi, unsigned int lo)
531 const unsigned int off = 3506716800U;
532 const unsigned int factor = 10000000;
533 unsigned int tmp;
534 unsigned int rlo;
535 int i;
537 /* First convert to seconds. */
538 tmp = hi % factor;
539 hi = hi / factor;
540 rlo = 0;
541 for (i = 0; i < 4; i++)
543 tmp = (tmp << 8) | (lo >> 24);
544 lo <<= 8;
546 rlo = (rlo << 8) | (tmp / factor);
547 tmp %= factor;
549 lo = rlo;
551 /* Return 0 in case of overflow. */
552 if (lo > off && hi > 1)
553 return 0;
555 return lo - off;
558 /* Convert a raw (stored in a buffer) VMS time to a unix time. */
560 time_t
561 vms_rawtime_to_time_t (unsigned char *buf)
563 unsigned int hi = bfd_getl32 (buf + 4);
564 unsigned int lo = bfd_getl32 (buf + 0);
566 return vms_time_to_time_t (hi, lo);