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. */
33 #include "safe-ctype.h"
39 #define RME$C_SETRFM 0x00000001
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:
55 1 toplevel bfd calls (functions from the bfd vector)
56 2 functions called by bfd calls
60 Level is also indentation level. Indentation is performed
64 _bfd_vms_debug (int level
, char *format
, ...)
66 static int min_level
= -1;
67 static FILE *output
= NULL
;
70 int abslvl
= (level
> 0) ? level
: - level
;
74 if ((eptr
= getenv ("VMS_DEBUG")) != NULL
)
76 min_level
= atoi (eptr
);
84 if (abslvl
> min_level
)
88 fprintf (output
, " ");
89 va_start (args
, format
);
90 vfprintf (output
, format
, args
);
96 hex dump 'size' bytes starting at 'ptr'. */
99 _bfd_hexdump (int level
, unsigned char *ptr
, int size
, int offset
)
101 unsigned char *lptr
= ptr
;
107 if ((count
% 16) == 0)
108 vms_debug (level
, "%08lx:", start
);
109 vms_debug (-level
, " %02x", *ptr
++);
114 while ((count
% 16) != 0)
116 vms_debug (-level
, " ");
120 if ((count
% 16) == 0)
122 vms_debug (-level
, " ");
125 vms_debug (-level
, "%c", (*lptr
< 32) ? '.' : *lptr
);
128 vms_debug (-level
, "\n");
131 if ((count
% 16) != 0)
132 vms_debug (-level
, "\n");
137 /* Copy sized string (string with fixed size) to new allocated area
138 size is string size (size of record) */
141 _bfd_vms_save_sized_string (unsigned char *str
, int size
)
143 char *newstr
= bfd_malloc ((bfd_size_type
) size
+ 1);
147 memcpy (newstr
, (char *) str
, (size_t) size
);
153 /* Copy counted string (string with size at first byte) to new allocated area
154 ptr points to size byte on entry */
157 _bfd_vms_save_counted_string (unsigned char *ptr
)
161 return _bfd_vms_save_sized_string (ptr
, len
);
164 /* Object output routines. */
167 Write 2 bytes rectype and 2 bytes record length. */
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. */
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. */
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)
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. */
235 _bfd_vms_output_end_subrec (struct vms_rec_wr
*recwr
)
237 int real_size
= recwr
->size
;
240 /* Subrecord must be open. */
241 BFD_ASSERT (recwr
->subrec_offset
!= 0);
243 length
= real_size
- recwr
->subrec_offset
;
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). */
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)
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
);
284 recwr
->buf
[recwr
->size
++] = 0;
286 /* Then the record. */
287 bfd_bwrite (recwr
->buf
, (size_t) recwr
->size
, abfd
);
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. */
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
;
313 /* Output short (16 bit) value. */
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
);
324 /* Output long (32 bit) value. */
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
);
335 /* Output quad (64 bit) value. */
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
);
346 /* Output c-string as counted string. */
349 _bfd_vms_output_counted (struct vms_rec_wr
*recwr
, char *value
)
353 vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value
));
355 len
= strlen (value
);
358 (*_bfd_error_handler
) (_("_bfd_vms_output_counted called with zero bytes"));
363 (*_bfd_error_handler
) (_("_bfd_vms_output_counted called with too many bytes"));
366 _bfd_vms_output_byte (recwr
, (unsigned int) len
& 0xff);
367 _bfd_vms_output_dump (recwr
, (unsigned char *) value
, len
);
370 /* Output character area. */
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
));
380 memcpy (recwr
->buf
+ recwr
->size
, data
, (size_t) len
);
384 /* Output count bytes of value. */
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
));
393 memset (recwr
->buf
+ recwr
->size
, value
, (size_t) count
);
394 recwr
->size
+= count
;
398 /* Convert the file to variable record length format. This is done
399 using undocumented system call sys$modify().
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
;
415 fab
.fab$b_rfm
= FAB$C_VAR
;
422 vms_convert_to_var_1 (char *filename
, int type
)
424 if (type
!= DECC$K_FILE
)
426 vms_convert_to_var (filename
);
430 /* Convert the file to variable record length format. This is done
431 using undocumented system call sys$modify().
432 Unix filename version. */
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)
443 /* Manufacture a VMS like time on a unix based system.
444 stolen from obj-vms.c. */
447 get_vms_time_string (void)
449 static unsigned char tbuf
[18];
455 pnt
= ctime (&timeb
);
461 sprintf ((char *) tbuf
, "%2s-%3s-%s %s",
462 pnt
+ 8, pnt
+ 4, pnt
+ 20, pnt
+ 11);
469 Descriptor
.Size
= 17;
470 Descriptor
.Ptr
= tbuf
;
471 SYS$
ASCTIM (0, &Descriptor
, 0, 0);
474 vms_debug2 ((6, "vmstimestring:'%s'\n", 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(). */
484 vms_get_module_name (const char *filename
, bfd_boolean upcase
)
489 /* Strip VMS path. */
490 fout
= strrchr (filename
, ']');
492 fout
= strchr (filename
, ':');
498 /* Strip UNIX path. */
499 fptr
= strrchr (fout
, '/');
503 fname
= strdup (fout
);
506 fptr
= strrchr (fname
, '.');
510 /* Convert to upper case and truncate at 31 characters.
511 (VMS object file format restricts module name length to 31). */
513 for (fptr
= fname
; *fptr
!= 0; fptr
++)
515 if (*fptr
== ';' || (fptr
- fname
) >= 31)
521 *fptr
= TOUPPER (*fptr
);
526 /* Convert a raw VMS time to a unix time. */
529 vms_time_to_time_t (unsigned int hi
, unsigned int lo
)
531 const unsigned int off
= 3506716800U;
532 const unsigned int factor
= 10000000;
537 /* First convert to seconds. */
541 for (i
= 0; i
< 4; i
++)
543 tmp
= (tmp
<< 8) | (lo
>> 24);
546 rlo
= (rlo
<< 8) | (tmp
/ factor
);
551 /* Return 0 in case of overflow. */
552 if (lo
> off
&& hi
> 1)
558 /* Convert a raw (stored in a buffer) VMS time to a unix time. */
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
);