aboutsummaryrefslogtreecommitdiff
path: root/gas/config/te-vms.c
blob: 42f965ae4123d79e58bf4df98a4e0edeb075a82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* te-vms.c -- Utilities for VMS.
   Copyright 2009 Free Software Foundation, Inc.

   Written by Douglas B Rupp <rupp@gnat.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */

#include "as.h"
#include "te-vms.h"

/* The purspose of the two alternate versions below is to have one that
   works for native VMS and one that works on an NFS mounted filesystem
   (Unix Server/VMS client).  The main issue being to generate the special
   VMS file timestamps for the debug info.  */

#ifdef VMS
#define __NEW_STARLET 1
#include <vms/starlet.h>
#include <vms/rms.h>
#include <vms/atrdef.h>
#include <vms/fibdef.h>
#include <vms/stsdef.h>
#include <vms/iodef.h>
#include <vms/fatdef.h>
#include <errno.h>
#include <vms/descrip.h>
#include <string.h>
#include <unixlib.h>

#define MAXPATH 256

/* Descrip.h doesn't have everything...  */
typedef struct fibdef * __fibdef_ptr32 __attribute__ (( mode (SI) ));

struct dsc$descriptor_fib
{
  unsigned int   fib$l_len;
  __fibdef_ptr32 fib$l_addr;
};

/* I/O Status Block.  */
struct IOSB
{
  unsigned short status, count;
  unsigned int devdep;
};

static char *tryfile;

/* Variable length string.  */
struct vstring
{
  short length;
  char string[NAM$C_MAXRSS+1];
};

static char filename_buff [MAXPATH];
static char vms_filespec [MAXPATH];

/* Callback function for filespec style conversion.  */

static int
translate_unix (char *name, int type ATTRIBUTE_UNUSED)
{
  strncpy (filename_buff, name, MAXPATH);
  filename_buff [MAXPATH - 1] = (char) 0;
  return 0;
}

/* Wrapper for DECC function that converts a Unix filespec
   to VMS style filespec.  */

static char *
to_vms_file_spec (char *filespec)
{
  strncpy (vms_filespec, "", MAXPATH);
  decc$to_vms (filespec, translate_unix, 1, 1);
  strncpy (vms_filespec, filename_buff, MAXPATH);

  vms_filespec [MAXPATH - 1] = (char) 0;

  return vms_filespec;
}

#else /* not VMS */

#define _BSD_SOURCE 1
#include <sys/stat.h>
#include <time.h>

#define VMS_EPOCH_OFFSET        35067168000000000LL
#define VMS_GRANULARITY_FACTOR  10000000

#endif /* VMS */

/* Return VMS file date, size, format, version given a name.  */

static int
vms_file_stats_name (const char *dirname,
                     const char *filename,
		     long long *cdt,
		     long *siz,
		     char *rfo,
		     int *ver)
{
  char fullname[strlen (dirname) + strlen (filename) + 1];
#ifdef VMS
  struct FAB fab;
  struct NAM nam;

  unsigned long long create;
  FAT recattr;
  char ascnamebuff [256];

  ATRDEF atrlst[]
    = {
      { ATR$S_CREDATE,  ATR$C_CREDATE,  &create },
      { ATR$S_RECATTR,  ATR$C_RECATTR,  &recattr },
      { ATR$S_ASCNAME,  ATR$C_ASCNAME,  &ascnamebuff },
      { 0, 0, 0}
    };

  FIBDEF fib;
  struct dsc$descriptor_fib fibdsc = {sizeof (fib), (void *) &fib};

  struct IOSB iosb;

  long status;
  unsigned short chan;

  struct vstring file;
  struct dsc$descriptor_s filedsc
    = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) file.string};
  struct vstring device;
  struct dsc$descriptor_s devicedsc
    = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) device.string};
  struct vstring result;
  struct dsc$descriptor_s resultdsc
    = {NAM$C_MAXRSS, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, (void *) result.string};

  if (strcmp (filename, "<internal>") == 0
      || strcmp (filename, "<built-in>") == 0)
    {
      if (cdt)
	*cdt = 0;

      if (siz)
	*siz = 0;

      if (rfo)
	*rfo = 0;

      if (ver)
        *ver = 0;

      return 0;
    }

  strcpy (fullname, dirname);
  strcat (fullname, filename);

  tryfile = to_vms_file_spec (fullname);

  /* Allocate and initialize a FAB and NAM structures.  */
  fab = cc$rms_fab;
  nam = cc$rms_nam;

  nam.nam$l_esa = file.string;
  nam.nam$b_ess = NAM$C_MAXRSS;
  nam.nam$l_rsa = result.string;
  nam.nam$b_rss = NAM$C_MAXRSS;
  fab.fab$l_fna = tryfile;
  fab.fab$b_fns = strlen (tryfile);
  fab.fab$l_nam = &nam;

  /* Validate filespec syntax and device existence.  */
  status = SYS$PARSE (&fab, 0, 0);
  if ((status & 1) != 1)
    return 1;

  file.string[nam.nam$b_esl] = 0;

  /* Find matching filespec.  */
  status = SYS$SEARCH (&fab, 0, 0);
  if ((status & 1) != 1)
    return 1;

  file.string[nam.nam$b_esl] = 0;
  result.string[result.length=nam.nam$b_rsl] = 0;

  /* Get the device name and assign an IO channel.  */
  strncpy (device.string, nam.nam$l_dev, nam.nam$b_dev);
  devicedsc.dsc$w_length  = nam.nam$b_dev;
  chan = 0;
  status = SYS$ASSIGN (&devicedsc, &chan, 0, 0, 0);
  if ((status & 1) != 1)
    return 1;

  /* Initialize the FIB and fill in the directory id field.  */
  memset (&fib, 0, sizeof (fib));
  fib.fib$w_did[0]  = nam.nam$w_did[0];
  fib.fib$w_did[1]  = nam.nam$w_did[1];
  fib.fib$w_did[2]  = nam.nam$w_did[2];
  fib.fib$l_acctl = 0;
  fib.fib$l_wcc = 0;
  strcpy (file.string, (strrchr (result.string, ']') + 1));
  filedsc.dsc$w_length = strlen (file.string);
  result.string[result.length = 0] = 0;

  /* Open and close the file to fill in the attributes.  */
  status
    = SYS$QIOW (0, chan, IO$_ACCESS|IO$M_ACCESS, &iosb, 0, 0,
		&fibdsc, &filedsc, &result.length, &resultdsc, &atrlst, 0);
  if ((status & 1) != 1)
    return 1;
  if ((iosb.status & 1) != 1)
    return 1;

  result.string[result.length] = 0;
  status = SYS$QIOW (0, chan, IO$_DEACCESS, &iosb, 0, 0, &fibdsc, 0, 0, 0,
		     &atrlst, 0);
  if ((status & 1) != 1)
    return 1;
  if ((iosb.status & 1) != 1)
    return 1;

  /* Deassign the channel and exit.  */
  status = SYS$DASSGN (chan);
  if ((status & 1) != 1)
    return 1;

  if (cdt) *cdt = create;
  if (siz) *siz = (512 * 65536 * recattr.fat$w_efblkh) +
                  (512 * (recattr.fat$w_efblkl - 1)) +
                  recattr.fat$w_ffbyte;
  if (rfo) *rfo = recattr.fat$v_rtype;
  if (ver) *ver = strtol (strrchr (ascnamebuff, ';') + 1, 0, 10);
#else /* not VMS */

  struct stat buff;
  struct tm *ts;
  long long gmtoff, secs, nsecs;

  strcpy (fullname, dirname);
  strcat (fullname, filename);

  if ((stat (fullname, &buff)) != 0)
     return 1;

  if (cdt)
    {
      ts = localtime (& buff.st_mtime);

#ifdef HAVE_TM_GMTOFF
	gmtoff = ts->tm_gmtoff;
#else
	{
	  extern long timezone;

	  if (ts->tm_isdst == 1)
	    gmtoff = - (timezone - 3600);
	  else
	    gmtoff = - timezone;
	}
#endif

#ifdef HAVE_ST_MTIM_TV_SEC
      secs = buff.st_mtim.tv_sec;
#else
      secs = buff.st_mtime;
#endif

#ifdef HAVE_ST_MTIM_TV_NSEC
      nsecs = buff.st_mtim.tv_nsec;
#else
      nsecs = 0;
#endif

      /* VMS timestamps are stored in local time to 100 nsec accuracy, but by
	 experiment I found timestamps truncated to (at least) microseconds
	 on an NFS mounted filesystem, hence the adjustment below. DBR. */
      *cdt = ((secs + gmtoff) * VMS_GRANULARITY_FACTOR)
	+ (nsecs / 1000 * 10) + VMS_EPOCH_OFFSET;
    }

  if (siz)
    *siz = buff.st_size;

  if (rfo)
    *rfo = 2; /* Stream LF format.  */

  /* Returning a file version of 0 is never correct for debug info, version 1
     will be correct if file editing is done only on the Unix side.  If editing
     is done on the VMS side, then its TBD.  */
  if (ver)
    *ver = 1;
#endif /* VMS */

  return 0;
}

bfd_uint64_t
vms_dwarf2_file_time_name (const char *filename, const char *dirname)
{
  long long cdt;

  if (vms_file_stats_name (dirname, filename, &cdt, 0, 0, 0) == 0)
    return cdt;
  else
    return 0;
}

long
vms_dwarf2_file_size_name (const char *filename, const char *dirname)
{
  long siz;

  if (vms_file_stats_name (dirname, filename, 0, &siz, 0, 0) == 0)
    return siz;
  else
    return 0;
}

/* VMS debugger needs the filename with version appended.  */
/* Longest filename on VMS is 255 characters. Largest version is 32768.  */
char *
vms_dwarf2_file_name (const char *filename, const char *dirname)
{
  int ver;
  static char buff [255 + 7];

  vms_file_stats_name (dirname, filename, 0, 0, 0, &ver);
  snprintf (buff, 255 + 7, "%s;%d", filename, ver);     
  return buff;
}