Commit | Line | Data |
---|---|---|
0c376465 | 1 | /* vms-misc.c -- BFD back-end for VMS/VAX (openVMS/VAX) and |
252b5132 | 2 | EVAX (openVMS/Alpha) files. |
b90efa5b | 3 | Copyright (C) 1996-2015 Free Software Foundation, Inc. |
0c376465 TG |
4 | |
5 | Miscellaneous functions. | |
252b5132 RH |
6 | |
7 | Written by Klaus K"ampf (kkaempf@rmi.de) | |
8 | ||
7920ce38 NC |
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 | |
cd123cb7 | 11 | the Free Software Foundation; either version 3 of the License, or |
7920ce38 | 12 | (at your option) any later version. |
252b5132 | 13 | |
7920ce38 NC |
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. | |
252b5132 | 18 | |
7920ce38 NC |
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 | |
cd123cb7 NC |
21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
22 | MA 02110-1301, USA. */ | |
252b5132 | 23 | |
252b5132 RH |
24 | #if __STDC__ |
25 | #include <stdarg.h> | |
26 | #endif | |
27 | ||
252b5132 | 28 | #include "sysdep.h" |
3db64b00 | 29 | #include "bfd.h" |
252b5132 RH |
30 | #include "bfdlink.h" |
31 | #include "libbfd.h" | |
4b544b64 | 32 | #include "safe-ctype.h" |
252b5132 | 33 | |
95e34ef7 | 34 | #ifdef VMS |
953b49ed | 35 | #define __NEW_STARLET |
95e34ef7 TG |
36 | #include <rms.h> |
37 | #include <unixlib.h> | |
953b49ed | 38 | #include <gen64def.h> |
95e34ef7 TG |
39 | #include <starlet.h> |
40 | #define RME$C_SETRFM 0x00000001 | |
41 | #include <unistd.h> | |
42 | #endif | |
43 | #include <time.h> | |
0c376465 | 44 | |
95e34ef7 TG |
45 | #include "vms.h" |
46 | #include "vms/emh.h" | |
0c376465 | 47 | |
252b5132 | 48 | #if VMS_DEBUG |
7920ce38 | 49 | /* Debug functions. */ |
252b5132 | 50 | |
0c376465 TG |
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: | |
558e161f | 54 | |
0c376465 | 55 | Levels: |
252b5132 RH |
56 | 1 toplevel bfd calls (functions from the bfd vector) |
57 | 2 functions called by bfd calls | |
58 | ... | |
59 | 9 almost everything | |
60 | ||
0c376465 | 61 | Level is also indentation level. Indentation is performed |
7920ce38 | 62 | if level > 0. */ |
252b5132 | 63 | |
252b5132 RH |
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; | |
7920ce38 | 71 | int abslvl = (level > 0) ? level : - level; |
252b5132 RH |
72 | |
73 | if (min_level == -1) | |
74 | { | |
7920ce38 | 75 | if ((eptr = getenv ("VMS_DEBUG")) != NULL) |
252b5132 | 76 | { |
7920ce38 | 77 | min_level = atoi (eptr); |
252b5132 RH |
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 | ||
95e34ef7 | 88 | while (--level > 0) |
558e161f | 89 | fprintf (output, " "); |
7920ce38 | 90 | va_start (args, format); |
558e161f | 91 | vfprintf (output, format, args); |
7920ce38 NC |
92 | fflush (output); |
93 | va_end (args); | |
252b5132 RH |
94 | } |
95 | ||
7920ce38 NC |
96 | /* A debug function |
97 | hex dump 'size' bytes starting at 'ptr'. */ | |
252b5132 RH |
98 | |
99 | void | |
95e34ef7 | 100 | _bfd_hexdump (int level, unsigned char *ptr, int size, int offset) |
252b5132 RH |
101 | { |
102 | unsigned char *lptr = ptr; | |
103 | int count = 0; | |
104 | long start = offset; | |
105 | ||
106 | while (size-- > 0) | |
107 | { | |
95e34ef7 | 108 | if ((count % 16) == 0) |
252b5132 RH |
109 | vms_debug (level, "%08lx:", start); |
110 | vms_debug (-level, " %02x", *ptr++); | |
111 | count++; | |
112 | start++; | |
113 | if (size == 0) | |
114 | { | |
95e34ef7 | 115 | while ((count % 16) != 0) |
252b5132 RH |
116 | { |
117 | vms_debug (-level, " "); | |
118 | count++; | |
119 | } | |
120 | } | |
95e34ef7 | 121 | if ((count % 16) == 0) |
252b5132 RH |
122 | { |
123 | vms_debug (-level, " "); | |
124 | while (lptr < ptr) | |
125 | { | |
95e34ef7 | 126 | vms_debug (-level, "%c", (*lptr < 32) ? '.' : *lptr); |
252b5132 RH |
127 | lptr++; |
128 | } | |
129 | vms_debug (-level, "\n"); | |
130 | } | |
131 | } | |
95e34ef7 | 132 | if ((count % 16) != 0) |
252b5132 | 133 | vms_debug (-level, "\n"); |
252b5132 RH |
134 | } |
135 | #endif | |
252b5132 | 136 | \f |
0c376465 TG |
137 | |
138 | /* Copy sized string (string with fixed size) to new allocated area | |
139 | size is string size (size of record) */ | |
252b5132 RH |
140 | |
141 | char * | |
7920ce38 | 142 | _bfd_vms_save_sized_string (unsigned char *str, int size) |
252b5132 | 143 | { |
dc810e39 | 144 | char *newstr = bfd_malloc ((bfd_size_type) size + 1); |
252b5132 RH |
145 | |
146 | if (newstr == NULL) | |
7920ce38 | 147 | return NULL; |
95e34ef7 | 148 | memcpy (newstr, (char *) str, (size_t) size); |
252b5132 RH |
149 | newstr[size] = 0; |
150 | ||
151 | return newstr; | |
152 | } | |
153 | ||
0c376465 TG |
154 | /* Copy counted string (string with size at first byte) to new allocated area |
155 | ptr points to size byte on entry */ | |
252b5132 RH |
156 | |
157 | char * | |
7920ce38 | 158 | _bfd_vms_save_counted_string (unsigned char *ptr) |
252b5132 RH |
159 | { |
160 | int len = *ptr++; | |
161 | ||
162 | return _bfd_vms_save_sized_string (ptr, len); | |
163 | } | |
252b5132 | 164 | \f |
95e34ef7 | 165 | /* Object output routines. */ |
252b5132 | 166 | |
95e34ef7 TG |
167 | /* Begin new record. |
168 | Write 2 bytes rectype and 2 bytes record length. */ | |
252b5132 RH |
169 | |
170 | void | |
95e34ef7 | 171 | _bfd_vms_output_begin (struct vms_rec_wr *recwr, int rectype) |
252b5132 | 172 | { |
95e34ef7 | 173 | vms_debug2 ((6, "_bfd_vms_output_begin (type %d)\n", rectype)); |
252b5132 | 174 | |
95e34ef7 TG |
175 | /* Record must have been closed. */ |
176 | BFD_ASSERT (recwr->size == 0); | |
252b5132 | 177 | |
95e34ef7 | 178 | _bfd_vms_output_short (recwr, (unsigned int) rectype); |
252b5132 | 179 | |
95e34ef7 TG |
180 | /* Placeholder for length. */ |
181 | _bfd_vms_output_short (recwr, 0); | |
252b5132 | 182 | } |
252b5132 | 183 | |
95e34ef7 TG |
184 | /* Begin new sub-record. |
185 | Write 2 bytes rectype, and 2 bytes record length. */ | |
252b5132 RH |
186 | |
187 | void | |
95e34ef7 | 188 | _bfd_vms_output_begin_subrec (struct vms_rec_wr *recwr, int rectype) |
252b5132 | 189 | { |
95e34ef7 | 190 | vms_debug2 ((6, "_bfd_vms_output_begin_subrec (type %d)\n", rectype)); |
252b5132 | 191 | |
95e34ef7 TG |
192 | /* Subrecord must have been closed. */ |
193 | BFD_ASSERT (recwr->subrec_offset == 0); | |
252b5132 | 194 | |
95e34ef7 TG |
195 | /* Save start of subrecord offset. */ |
196 | recwr->subrec_offset = recwr->size; | |
252b5132 | 197 | |
95e34ef7 TG |
198 | /* Subrecord type. */ |
199 | _bfd_vms_output_short (recwr, (unsigned int) rectype); | |
252b5132 | 200 | |
7920ce38 | 201 | /* Placeholder for length. */ |
95e34ef7 | 202 | _bfd_vms_output_short (recwr, 0); |
252b5132 RH |
203 | } |
204 | ||
7920ce38 | 205 | /* Set record/subrecord alignment. */ |
252b5132 RH |
206 | |
207 | void | |
95e34ef7 | 208 | _bfd_vms_output_alignment (struct vms_rec_wr *recwr, int alignto) |
252b5132 | 209 | { |
95e34ef7 TG |
210 | vms_debug2 ((6, "_bfd_vms_output_alignment (%d)\n", alignto)); |
211 | recwr->align = alignto; | |
252b5132 RH |
212 | } |
213 | ||
95e34ef7 TG |
214 | /* Align the size of the current record (whose length is LENGTH). |
215 | Warning: this obviously changes the record (and the possible subrecord) | |
216 | length. */ | |
252b5132 | 217 | |
95e34ef7 TG |
218 | static void |
219 | _bfd_vms_output_align (struct vms_rec_wr *recwr, unsigned int length) | |
252b5132 | 220 | { |
95e34ef7 TG |
221 | unsigned int real_size = recwr->size; |
222 | unsigned int aligncount; | |
252b5132 | 223 | |
95e34ef7 TG |
224 | /* Pad with 0 if alignment is required. */ |
225 | aligncount = (recwr->align - (length % recwr->align)) % recwr->align; | |
226 | vms_debug2 ((6, "align: adding %d bytes\n", aligncount)); | |
227 | while (aligncount-- > 0) | |
228 | recwr->buf[real_size++] = 0; | |
252b5132 | 229 | |
95e34ef7 | 230 | recwr->size = real_size; |
252b5132 RH |
231 | } |
232 | ||
95e34ef7 | 233 | /* Ends current sub-record. Set length field. */ |
252b5132 RH |
234 | |
235 | void | |
95e34ef7 | 236 | _bfd_vms_output_end_subrec (struct vms_rec_wr *recwr) |
252b5132 | 237 | { |
95e34ef7 | 238 | int real_size = recwr->size; |
252b5132 RH |
239 | int length; |
240 | ||
95e34ef7 TG |
241 | /* Subrecord must be open. */ |
242 | BFD_ASSERT (recwr->subrec_offset != 0); | |
252b5132 | 243 | |
95e34ef7 | 244 | length = real_size - recwr->subrec_offset; |
252b5132 RH |
245 | |
246 | if (length == 0) | |
247 | return; | |
252b5132 | 248 | |
95e34ef7 | 249 | _bfd_vms_output_align (recwr, length); |
252b5132 | 250 | |
7920ce38 | 251 | /* Put length to buffer. */ |
95e34ef7 TG |
252 | bfd_putl16 ((bfd_vma) (recwr->size - recwr->subrec_offset), |
253 | recwr->buf + recwr->subrec_offset + 2); | |
252b5132 | 254 | |
95e34ef7 TG |
255 | /* Close the subrecord. */ |
256 | recwr->subrec_offset = 0; | |
252b5132 RH |
257 | } |
258 | ||
95e34ef7 | 259 | /* Ends current record (and write it). */ |
252b5132 RH |
260 | |
261 | void | |
95e34ef7 | 262 | _bfd_vms_output_end (bfd *abfd, struct vms_rec_wr *recwr) |
252b5132 | 263 | { |
95e34ef7 | 264 | vms_debug2 ((6, "_bfd_vms_output_end (size %u)\n", recwr->size)); |
252b5132 | 265 | |
95e34ef7 TG |
266 | /* Subrecord must have been closed. */ |
267 | BFD_ASSERT (recwr->subrec_offset == 0); | |
252b5132 | 268 | |
95e34ef7 TG |
269 | if (recwr->size == 0) |
270 | return; | |
252b5132 | 271 | |
95e34ef7 TG |
272 | _bfd_vms_output_align (recwr, recwr->size); |
273 | ||
274 | /* Write the length word. */ | |
275 | bfd_putl16 ((bfd_vma) recwr->size, recwr->buf + 2); | |
276 | ||
277 | /* File is open in undefined (UDF) format on VMS, but ultimately will be | |
278 | converted to variable length (VAR) format. VAR format has a length | |
279 | word first which must be explicitly output in UDF format. */ | |
280 | /* So, first the length word. */ | |
281 | bfd_bwrite (recwr->buf + 2, 2, abfd); | |
282 | ||
283 | /* Align. */ | |
284 | if (recwr->size & 1) | |
285 | recwr->buf[recwr->size++] = 0; | |
286 | ||
287 | /* Then the record. */ | |
288 | bfd_bwrite (recwr->buf, (size_t) recwr->size, abfd); | |
289 | ||
290 | recwr->size = 0; | |
291 | } | |
292 | ||
293 | /* Check remaining buffer size. Return what's left. */ | |
252b5132 RH |
294 | |
295 | int | |
95e34ef7 | 296 | _bfd_vms_output_check (struct vms_rec_wr *recwr, int size) |
252b5132 | 297 | { |
95e34ef7 | 298 | vms_debug2 ((6, "_bfd_vms_output_check (%d)\n", size)); |
252b5132 | 299 | |
95e34ef7 | 300 | return (MAX_OUTREC_SIZE - (recwr->size + size + MIN_OUTREC_LUFT)); |
252b5132 RH |
301 | } |
302 | ||
7920ce38 | 303 | /* Output byte (8 bit) value. */ |
252b5132 RH |
304 | |
305 | void | |
95e34ef7 | 306 | _bfd_vms_output_byte (struct vms_rec_wr *recwr, unsigned int value) |
252b5132 | 307 | { |
95e34ef7 | 308 | vms_debug2 ((6, "_bfd_vms_output_byte (%02x)\n", value)); |
252b5132 | 309 | |
95e34ef7 TG |
310 | *(recwr->buf + recwr->size) = value; |
311 | recwr->size += 1; | |
252b5132 RH |
312 | } |
313 | ||
7920ce38 | 314 | /* Output short (16 bit) value. */ |
252b5132 RH |
315 | |
316 | void | |
95e34ef7 | 317 | _bfd_vms_output_short (struct vms_rec_wr *recwr, unsigned int value) |
252b5132 | 318 | { |
95e34ef7 | 319 | vms_debug2 ((6, "_bfd_vms_output_short (%04x)\n", value)); |
252b5132 | 320 | |
95e34ef7 TG |
321 | bfd_putl16 ((bfd_vma) value & 0xffff, recwr->buf + recwr->size); |
322 | recwr->size += 2; | |
252b5132 RH |
323 | } |
324 | ||
7920ce38 | 325 | /* Output long (32 bit) value. */ |
252b5132 RH |
326 | |
327 | void | |
95e34ef7 | 328 | _bfd_vms_output_long (struct vms_rec_wr *recwr, unsigned long value) |
252b5132 | 329 | { |
95e34ef7 | 330 | vms_debug2 ((6, "_bfd_vms_output_long (%08lx)\n", value)); |
252b5132 | 331 | |
95e34ef7 TG |
332 | bfd_putl32 ((bfd_vma) value, recwr->buf + recwr->size); |
333 | recwr->size += 4; | |
252b5132 RH |
334 | } |
335 | ||
7920ce38 | 336 | /* Output quad (64 bit) value. */ |
252b5132 RH |
337 | |
338 | void | |
95e34ef7 | 339 | _bfd_vms_output_quad (struct vms_rec_wr *recwr, bfd_vma value) |
252b5132 | 340 | { |
95e34ef7 | 341 | vms_debug2 ((6, "_bfd_vms_output_quad (%08lx)\n", (unsigned long)value)); |
252b5132 | 342 | |
95e34ef7 TG |
343 | bfd_putl64 (value, recwr->buf + recwr->size); |
344 | recwr->size += 8; | |
252b5132 RH |
345 | } |
346 | ||
7920ce38 | 347 | /* Output c-string as counted string. */ |
252b5132 RH |
348 | |
349 | void | |
81bb31c0 | 350 | _bfd_vms_output_counted (struct vms_rec_wr *recwr, const char *value) |
252b5132 | 351 | { |
7920ce38 | 352 | int len; |
252b5132 | 353 | |
95e34ef7 | 354 | vms_debug2 ((6, "_bfd_vms_output_counted (%s)\n", value)); |
252b5132 RH |
355 | |
356 | len = strlen (value); | |
357 | if (len == 0) | |
358 | { | |
359 | (*_bfd_error_handler) (_("_bfd_vms_output_counted called with zero bytes")); | |
360 | return; | |
361 | } | |
362 | if (len > 255) | |
363 | { | |
364 | (*_bfd_error_handler) (_("_bfd_vms_output_counted called with too many bytes")); | |
365 | return; | |
366 | } | |
95e34ef7 | 367 | _bfd_vms_output_byte (recwr, (unsigned int) len & 0xff); |
81bb31c0 | 368 | _bfd_vms_output_dump (recwr, (const unsigned char *)value, len); |
252b5132 RH |
369 | } |
370 | ||
7920ce38 | 371 | /* Output character area. */ |
252b5132 RH |
372 | |
373 | void | |
81bb31c0 | 374 | _bfd_vms_output_dump (struct vms_rec_wr *recwr, const unsigned char *data, int len) |
252b5132 | 375 | { |
95e34ef7 | 376 | vms_debug2 ((6, "_bfd_vms_output_dump (%d)\n", len)); |
252b5132 | 377 | |
95e34ef7 | 378 | if (len == 0) |
252b5132 RH |
379 | return; |
380 | ||
95e34ef7 TG |
381 | memcpy (recwr->buf + recwr->size, data, (size_t) len); |
382 | recwr->size += len; | |
252b5132 RH |
383 | } |
384 | ||
7920ce38 | 385 | /* Output count bytes of value. */ |
252b5132 RH |
386 | |
387 | void | |
95e34ef7 | 388 | _bfd_vms_output_fill (struct vms_rec_wr *recwr, int value, int count) |
252b5132 | 389 | { |
95e34ef7 | 390 | vms_debug2 ((6, "_bfd_vms_output_fill (val %02x times %d)\n", value, count)); |
252b5132 RH |
391 | |
392 | if (count == 0) | |
393 | return; | |
95e34ef7 TG |
394 | memset (recwr->buf + recwr->size, value, (size_t) count); |
395 | recwr->size += count; | |
252b5132 RH |
396 | } |
397 | ||
95e34ef7 TG |
398 | #ifdef VMS |
399 | /* Convert the file to variable record length format. This is done | |
400 | using undocumented system call sys$modify(). | |
401 | Pure VMS version. */ | |
252b5132 | 402 | |
bfea910e TG |
403 | static void |
404 | vms_convert_to_var (char * vms_filename) | |
252b5132 | 405 | { |
95e34ef7 | 406 | struct FAB fab = cc$rms_fab; |
252b5132 | 407 | |
95e34ef7 TG |
408 | fab.fab$l_fna = vms_filename; |
409 | fab.fab$b_fns = strlen (vms_filename); | |
410 | fab.fab$b_fac = FAB$M_PUT; | |
411 | fab.fab$l_fop = FAB$M_ESC; | |
412 | fab.fab$l_ctx = RME$C_SETRFM; | |
252b5132 | 413 | |
95e34ef7 | 414 | sys$open (&fab); |
252b5132 | 415 | |
95e34ef7 | 416 | fab.fab$b_rfm = FAB$C_VAR; |
252b5132 | 417 | |
95e34ef7 TG |
418 | sys$modify (&fab); |
419 | sys$close (&fab); | |
252b5132 RH |
420 | } |
421 | ||
95e34ef7 TG |
422 | static int |
423 | vms_convert_to_var_1 (char *filename, int type) | |
252b5132 | 424 | { |
95e34ef7 TG |
425 | if (type != DECC$K_FILE) |
426 | return FALSE; | |
427 | vms_convert_to_var (filename); | |
428 | return TRUE; | |
252b5132 RH |
429 | } |
430 | ||
95e34ef7 TG |
431 | /* Convert the file to variable record length format. This is done |
432 | using undocumented system call sys$modify(). | |
433 | Unix filename version. */ | |
252b5132 | 434 | |
bfea910e TG |
435 | int |
436 | _bfd_vms_convert_to_var_unix_filename (const char *unix_filename) | |
252b5132 | 437 | { |
95e34ef7 TG |
438 | if (decc$to_vms (unix_filename, &vms_convert_to_var_1, 0, 1) != 1) |
439 | return FALSE; | |
440 | return TRUE; | |
441 | } | |
442 | #endif /* VMS */ | |
443 | ||
444 | /* Manufacture a VMS like time on a unix based system. | |
445 | stolen from obj-vms.c. */ | |
446 | ||
447 | unsigned char * | |
448 | get_vms_time_string (void) | |
449 | { | |
450 | static unsigned char tbuf[18]; | |
451 | #ifndef VMS | |
452 | char *pnt; | |
453 | time_t timeb; | |
454 | ||
455 | time (& timeb); | |
456 | pnt = ctime (&timeb); | |
457 | pnt[3] = 0; | |
458 | pnt[7] = 0; | |
459 | pnt[10] = 0; | |
460 | pnt[16] = 0; | |
461 | pnt[24] = 0; | |
462 | sprintf ((char *) tbuf, "%2s-%3s-%s %s", | |
463 | pnt + 8, pnt + 4, pnt + 20, pnt + 11); | |
464 | #else | |
465 | struct | |
466 | { | |
467 | int Size; | |
468 | unsigned char *Ptr; | |
469 | } Descriptor; | |
470 | Descriptor.Size = 17; | |
471 | Descriptor.Ptr = tbuf; | |
472 | SYS$ASCTIM (0, &Descriptor, 0, 0); | |
473 | #endif /* not VMS */ | |
474 | ||
475 | vms_debug2 ((6, "vmstimestring:'%s'\n", tbuf)); | |
476 | ||
477 | return tbuf; | |
252b5132 | 478 | } |
4b544b64 TG |
479 | |
480 | /* Create module name from filename (ie, extract the basename and convert it | |
481 | in upper cases). Works on both VMS and UNIX pathes. | |
482 | The result has to be free(). */ | |
483 | ||
484 | char * | |
485 | vms_get_module_name (const char *filename, bfd_boolean upcase) | |
486 | { | |
487 | char *fname, *fptr; | |
488 | const char *fout; | |
489 | ||
490 | /* Strip VMS path. */ | |
491 | fout = strrchr (filename, ']'); | |
492 | if (fout == NULL) | |
493 | fout = strchr (filename, ':'); | |
494 | if (fout != NULL) | |
495 | fout++; | |
496 | else | |
497 | fout = filename; | |
953b49ed | 498 | |
4b544b64 TG |
499 | /* Strip UNIX path. */ |
500 | fptr = strrchr (fout, '/'); | |
501 | if (fptr != NULL) | |
502 | fout = fptr + 1; | |
953b49ed | 503 | |
4b544b64 TG |
504 | fname = strdup (fout); |
505 | ||
506 | /* Strip suffix. */ | |
507 | fptr = strrchr (fname, '.'); | |
508 | if (fptr != 0) | |
509 | *fptr = 0; | |
953b49ed | 510 | |
4b544b64 TG |
511 | /* Convert to upper case and truncate at 31 characters. |
512 | (VMS object file format restricts module name length to 31). */ | |
513 | fptr = fname; | |
514 | for (fptr = fname; *fptr != 0; fptr++) | |
515 | { | |
516 | if (*fptr == ';' || (fptr - fname) >= 31) | |
517 | { | |
518 | *fptr = 0; | |
519 | break; | |
520 | } | |
521 | if (upcase) | |
522 | *fptr = TOUPPER (*fptr); | |
523 | } | |
524 | return fname; | |
525 | } | |
526 | ||
953b49ed TG |
527 | /* Compared to usual UNIX time_t, VMS time has less limits: |
528 | - 64 bit (63 bits in fact as the MSB must be 0) | |
529 | - 100ns granularity | |
530 | - epoch is Nov 17, 1858. | |
531 | Here has the constants and the routines used to convert VMS from/to UNIX time. | |
62a35308 TG |
532 | The conversion routines don't assume 64 bits arithmetic. |
533 | ||
534 | Here we assume that the definition of time_t is the UNIX one, ie integer | |
535 | type, expressing seconds since the epoch. */ | |
953b49ed TG |
536 | |
537 | /* UNIX time granularity for VMS, ie 1s / 100ns. */ | |
538 | #define VMS_TIME_FACTOR 10000000 | |
539 | ||
540 | /* Number of seconds since VMS epoch of the UNIX epoch. */ | |
541 | #define VMS_TIME_OFFSET 3506716800U | |
542 | ||
543 | /* Convert a VMS time to a unix time. */ | |
4b544b64 TG |
544 | |
545 | time_t | |
546 | vms_time_to_time_t (unsigned int hi, unsigned int lo) | |
547 | { | |
4b544b64 TG |
548 | unsigned int tmp; |
549 | unsigned int rlo; | |
550 | int i; | |
62a35308 | 551 | time_t res; |
4b544b64 TG |
552 | |
553 | /* First convert to seconds. */ | |
953b49ed TG |
554 | tmp = hi % VMS_TIME_FACTOR; |
555 | hi = hi / VMS_TIME_FACTOR; | |
4b544b64 TG |
556 | rlo = 0; |
557 | for (i = 0; i < 4; i++) | |
558 | { | |
559 | tmp = (tmp << 8) | (lo >> 24); | |
560 | lo <<= 8; | |
561 | ||
953b49ed TG |
562 | rlo = (rlo << 8) | (tmp / VMS_TIME_FACTOR); |
563 | tmp %= VMS_TIME_FACTOR; | |
4b544b64 TG |
564 | } |
565 | lo = rlo; | |
566 | ||
567 | /* Return 0 in case of overflow. */ | |
62a35308 TG |
568 | if (hi > 1 |
569 | || (hi == 1 && lo >= VMS_TIME_OFFSET)) | |
953b49ed TG |
570 | return 0; |
571 | ||
572 | /* Return 0 in case of underflow. */ | |
62a35308 | 573 | if (hi == 0 && lo < VMS_TIME_OFFSET) |
4b544b64 TG |
574 | return 0; |
575 | ||
62a35308 TG |
576 | res = lo - VMS_TIME_OFFSET; |
577 | if (res <= 0) | |
578 | return 0; | |
579 | return res; | |
953b49ed TG |
580 | } |
581 | ||
582 | /* Convert a time_t to a VMS time. */ | |
583 | ||
584 | void | |
585 | vms_time_t_to_vms_time (time_t ut, unsigned int *hi, unsigned int *lo) | |
586 | { | |
587 | unsigned short val[4]; | |
588 | unsigned short tmp[4]; | |
589 | unsigned int carry; | |
590 | int i; | |
591 | ||
592 | /* Put into val. */ | |
593 | val[0] = ut & 0xffff; | |
594 | val[1] = (ut >> 16) & 0xffff; | |
81c5c866 JK |
595 | val[2] = sizeof (ut) > 4 ? (ut >> 32) & 0xffff : 0; |
596 | val[3] = sizeof (ut) > 4 ? (ut >> 48) & 0xffff : 0; | |
953b49ed TG |
597 | |
598 | /* Add offset. */ | |
599 | tmp[0] = VMS_TIME_OFFSET & 0xffff; | |
600 | tmp[1] = VMS_TIME_OFFSET >> 16; | |
601 | tmp[2] = 0; | |
602 | tmp[3] = 0; | |
603 | carry = 0; | |
604 | for (i = 0; i < 4; i++) | |
605 | { | |
606 | carry += tmp[i] + val[i]; | |
607 | val[i] = carry & 0xffff; | |
608 | carry = carry >> 16; | |
609 | } | |
610 | ||
611 | /* Multiply by factor, well first by 10000 and then by 1000. */ | |
612 | carry = 0; | |
613 | for (i = 0; i < 4; i++) | |
614 | { | |
615 | carry += val[i] * 10000; | |
616 | val[i] = carry & 0xffff; | |
617 | carry = carry >> 16; | |
618 | } | |
619 | carry = 0; | |
620 | for (i = 0; i < 4; i++) | |
621 | { | |
622 | carry += val[i] * 1000; | |
623 | val[i] = carry & 0xffff; | |
624 | carry = carry >> 16; | |
625 | } | |
626 | ||
627 | /* Write the result. */ | |
628 | *lo = val[0] | (val[1] << 16); | |
629 | *hi = val[2] | (val[3] << 16); | |
4b544b64 TG |
630 | } |
631 | ||
632 | /* Convert a raw (stored in a buffer) VMS time to a unix time. */ | |
633 | ||
634 | time_t | |
635 | vms_rawtime_to_time_t (unsigned char *buf) | |
636 | { | |
637 | unsigned int hi = bfd_getl32 (buf + 4); | |
638 | unsigned int lo = bfd_getl32 (buf + 0); | |
639 | ||
640 | return vms_time_to_time_t (hi, lo); | |
641 | } | |
953b49ed TG |
642 | |
643 | void | |
644 | vms_get_time (unsigned int *hi, unsigned int *lo) | |
645 | { | |
646 | #ifdef VMS | |
647 | struct _generic_64 t; | |
648 | ||
649 | sys$gettim (&t); | |
650 | *lo = t.gen64$q_quadword; | |
651 | *hi = t.gen64$q_quadword >> 32; | |
652 | #else | |
653 | time_t t; | |
654 | ||
655 | time (&t); | |
656 | vms_time_t_to_vms_time (t, hi, lo); | |
657 | #endif | |
658 | } | |
659 | ||
660 | /* Get the current time into a raw buffer BUF. */ | |
661 | ||
662 | void | |
663 | vms_raw_get_time (unsigned char *buf) | |
664 | { | |
665 | unsigned int hi, lo; | |
666 | ||
667 | vms_get_time (&hi, &lo); | |
668 | bfd_putl32 (lo, buf + 0); | |
669 | bfd_putl32 (hi, buf + 4); | |
670 | } |