1 /* Output VMS debug format symbol table information from GCC.
2    Copyright (C) 1987-2018 Free Software Foundation, Inc.
3    Contributed by Douglas B. Rupp (rupp@gnat.com).
4    Updated by Bernard W. Giroud (bgiroud@users.sourceforge.net).
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12 
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21 
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 
27 #ifdef VMS_DEBUGGING_INFO
28 #include "alias.h"
29 #include "tree.h"
30 #include "varasm.h"
31 #include "version.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "output.h"
35 #include "vmsdbg.h"
36 #include "debug.h"
37 #include "langhooks.h"
38 #include "function.h"
39 #include "target.h"
40 #include "file-prefix-map.h" /* remap_debug_filename()  */
41 
42 /* Difference in seconds between the VMS Epoch and the Unix Epoch */
43 static const long long vms_epoch_offset = 3506716800ll;
44 
45 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
46 
47 /* NOTE: In the comments in this file, many references are made to "Debug
48    Symbol Table".  This term is abbreviated as `DST' throughout the remainder
49    of this file.  */
50 
51 typedef struct dst_line_info_struct *dst_line_info_ref;
52 
53 /* Each entry in the line_info_table maintains the file and
54    line number associated with the label generated for that
55    entry.  The label gives the PC value associated with
56    the line number entry.  */
57 typedef struct dst_line_info_struct
58 {
59   unsigned long dst_file_num;
60   unsigned long dst_line_num;
61 }
62 dst_line_info_entry;
63 
64 typedef struct dst_file_info_struct *dst_file_info_ref;
65 
66 typedef struct dst_file_info_struct
67 {
68   char *file_name;
69   unsigned int max_line;
70   unsigned int listing_line_start;
71   long long cdt;
72   long ebk;
73   short ffb;
74   char rfo;
75 }
76 dst_file_info_entry;
77 
78 /* Maximum size (in bytes) of an artificially generated label.  */
79 #define MAX_ARTIFICIAL_LABEL_BYTES	30
80 
81 /* Make sure we know the sizes of the various types debug can describe. These
82    are only defaults.  If the sizes are different for your target, you should
83    override these values by defining the appropriate symbols in your tm.h
84    file.  */
85 #ifndef PTR_SIZE
86 #define PTR_SIZE 4 /* Must be 32 bits for VMS debug info */
87 #endif
88 
89 /* Pointer to a structure of filenames referenced by this compilation unit.  */
90 static dst_file_info_ref file_info_table;
91 
92 /* Total number of entries in the table (i.e. array) pointed to by
93    `file_info_table'.  This is the *total* and includes both used and unused
94    slots.  */
95 static unsigned int file_info_table_allocated;
96 
97 /* Number of entries in the file_info_table which are actually in use.  */
98 static unsigned int file_info_table_in_use;
99 
100 /* Size (in elements) of increments by which we may expand the filename
101    table.  */
102 #define FILE_TABLE_INCREMENT 64
103 
104 typedef char *char_p;
105 
106 static vec<char_p> funcnam_table;
107 static vec<unsigned> funcnum_table;
108 #define FUNC_TABLE_INITIAL 256
109 
110 /* Local pointer to the name of the main input file.  Initialized in
111    vmsdbgout_init.  */
112 static const char *primary_filename;
113 
114 static char *module_producer;
115 static unsigned int module_language;
116 
117 /* A pointer to the base of a table that contains line information
118    for each source code line in .text in the compilation unit.  */
119 static dst_line_info_ref line_info_table;
120 
121 /* Number of elements currently allocated for line_info_table.  */
122 static unsigned int line_info_table_allocated;
123 
124 /* Number of elements in line_info_table currently in use.  */
125 static unsigned int line_info_table_in_use;
126 
127 /* Size (in elements) of increments by which we may expand line_info_table.  */
128 #define LINE_INFO_TABLE_INCREMENT 1024
129 
130 /* Forward declarations for functions defined in this file.  */
131 static char *full_name (const char *);
132 static unsigned int lookup_filename (const char *);
133 static int write_debug_header (DST_HEADER *, const char *, int);
134 static int write_debug_addr (const char *, const char *, int);
135 static int write_debug_data1 (unsigned int, const char *, int);
136 static int write_debug_data2 (unsigned int, const char *, int);
137 static int write_debug_data4 (unsigned long, const char *, int);
138 static int write_debug_data8 (unsigned long long, const char *, int);
139 static int write_debug_delta4 (const char *, const char *, const char *, int);
140 static int write_debug_string (const char *, const char *, int);
141 static int write_modbeg (int);
142 static int write_modend (int);
143 static int write_rtnbeg (int, int);
144 static int write_rtnend (int, int);
145 static int write_pclines (int);
146 static int write_srccorr (int, dst_file_info_entry, int);
147 static int write_srccorrs (int);
148 
149 static void vmsdbgout_init (const char *);
150 static void vmsdbgout_finish (const char *);
151 static void vmsdbgout_early_finish (const char *);
152 static void vmsdbgout_assembly_start (void);
153 static void vmsdbgout_define (unsigned int, const char *);
154 static void vmsdbgout_undef (unsigned int, const char *);
155 static void vmsdbgout_start_source_file (unsigned int, const char *);
156 static void vmsdbgout_end_source_file (unsigned int);
157 static void vmsdbgout_begin_block (unsigned int, unsigned int);
158 static void vmsdbgout_end_block (unsigned int, unsigned int);
159 static bool vmsdbgout_ignore_block (const_tree);
160 static void vmsdbgout_source_line (unsigned int, unsigned int, const char *,
161 				   int, bool);
162 static void vmsdbgout_write_source_line (unsigned, const char *, int , bool);
163 static void vmsdbgout_begin_prologue (unsigned int, unsigned int,
164 				      const char *);
165 static void vmsdbgout_end_prologue (unsigned int, const char *);
166 static void vmsdbgout_end_function (unsigned int);
167 static void vmsdbgout_begin_epilogue (unsigned int, const char *);
168 static void vmsdbgout_end_epilogue (unsigned int, const char *);
169 static void vmsdbgout_begin_function (tree);
170 static void vmsdbgout_function_decl (tree);
171 static void vmsdbgout_early_global_decl (tree);
172 static void vmsdbgout_late_global_decl (tree);
173 static void vmsdbgout_type_decl (tree, int);
174 static void vmsdbgout_abstract_function (tree);
175 
176 /* The debug hooks structure.  */
177 
178 const struct gcc_debug_hooks vmsdbg_debug_hooks
179 = {vmsdbgout_init,
180    vmsdbgout_finish,
181    vmsdbgout_early_finish,
182    vmsdbgout_assembly_start,
183    vmsdbgout_define,
184    vmsdbgout_undef,
185    vmsdbgout_start_source_file,
186    vmsdbgout_end_source_file,
187    vmsdbgout_begin_block,
188    vmsdbgout_end_block,
189    vmsdbgout_ignore_block,
190    vmsdbgout_source_line,
191    vmsdbgout_begin_prologue,
192    vmsdbgout_end_prologue,
193    vmsdbgout_begin_epilogue,
194    vmsdbgout_end_epilogue,
195    vmsdbgout_begin_function,
196    vmsdbgout_end_function,
197    debug_nothing_tree,		  /* register_main_translation_unit */
198    vmsdbgout_function_decl,
199    vmsdbgout_early_global_decl,
200    vmsdbgout_late_global_decl,
201    vmsdbgout_type_decl,		  /* type_decl */
202    debug_nothing_tree_tree_tree_bool_bool, /* imported_module_or_decl */
203    debug_false_tree_charstarstar_uhwistar, /* die_ref_for_decl */
204    debug_nothing_tree_charstar_uhwi, /* register_external_die */
205    debug_nothing_tree,		  /* deferred_inline_function */
206    vmsdbgout_abstract_function,
207    debug_nothing_rtx_code_label,  /* label */
208    debug_nothing_int,		  /* handle_pch */
209    debug_nothing_rtx_insn,	  /* var_location */
210    debug_nothing_tree,	          /* inline_entry */
211    debug_nothing_tree,		  /* size_function */
212    debug_nothing_void,            /* switch_text_section */
213    debug_nothing_tree_tree,	  /* set_name */
214    0,                             /* start_end_main_source_file */
215    TYPE_SYMTAB_IS_ADDRESS         /* tree_type_symtab_field */
216 };
217 
218 /* Definitions of defaults for assembler-dependent names of various
219    pseudo-ops and section names.  */
220 #define VMS_UNALIGNED_SHORT_ASM_OP	".word"
221 #define VMS_UNALIGNED_INT_ASM_OP	".long"
222 #define VMS_UNALIGNED_LONG_ASM_OP	".long"
223 #define VMS_UNALIGNED_DOUBLE_INT_ASM_OP	".quad"
224 
225 #define VMS_ASM_BYTE_OP	".byte"
226 
227 #define NUMBYTES(I) ((I) < 256 ? 1 : (I) < 65536 ? 2 : 4)
228 
229 #define NUMBYTES0(I) ((I) < 128 ? 0 : (I) < 65536 ? 2 : 4)
230 
231 #ifndef UNALIGNED_PTR_ASM_OP
232 #define UNALIGNED_PTR_ASM_OP \
233   (PTR_SIZE == 8 ? VMS_UNALIGNED_DOUBLE_INT_ASM_OP : VMS_UNALIGNED_INT_ASM_OP)
234 #endif
235 
236 #ifndef UNALIGNED_OFFSET_ASM_OP
237 #define UNALIGNED_OFFSET_ASM_OP(OFFSET) \
238   (NUMBYTES (OFFSET) == 4 \
239    ? VMS_UNALIGNED_LONG_ASM_OP \
240    : (NUMBYTES (OFFSET) == 2 ? VMS_UNALIGNED_SHORT_ASM_OP : VMS_ASM_BYTE_OP))
241 #endif
242 
243 /* Definitions of defaults for formats and names of various special
244    (artificial) labels which may be generated within this file (when the -g
245    options is used and VMS_DEBUGGING_INFO is in effect.  If necessary, these
246    may be overridden from within the tm.h file, but typically, overriding these
247    defaults is unnecessary.  */
248 
249 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
250 
251 #ifndef TEXT_END_LABEL
252 #define TEXT_END_LABEL		"Lvetext"
253 #endif
254 #ifndef FUNC_BEGIN_LABEL
255 #define FUNC_BEGIN_LABEL	"LVFB"
256 #endif
257 #ifndef FUNC_PROLOG_LABEL
258 #define FUNC_PROLOG_LABEL	"LVFP"
259 #endif
260 #ifndef FUNC_EPILOG_LABEL
261 #define FUNC_EPILOG_LABEL	"LVEB"
262 #endif
263 #ifndef FUNC_END_LABEL
264 #define FUNC_END_LABEL		"LVFE"
265 #endif
266 #ifndef BLOCK_BEGIN_LABEL
267 #define BLOCK_BEGIN_LABEL	"LVBB"
268 #endif
269 #ifndef BLOCK_END_LABEL
270 #define BLOCK_END_LABEL		"LVBE"
271 #endif
272 #ifndef LINE_CODE_LABEL
273 #define LINE_CODE_LABEL		"LVM"
274 #endif
275 
276 #ifndef ASM_OUTPUT_DEBUG_DELTA2
277 #define ASM_OUTPUT_DEBUG_DELTA2(FILE,LABEL1,LABEL2)			 \
278   do									 \
279     {									 \
280       fprintf ((FILE), "\t%s\t", VMS_UNALIGNED_SHORT_ASM_OP);		 \
281       assemble_name (FILE, LABEL1);					 \
282       fprintf (FILE, "-");						 \
283       assemble_name (FILE, LABEL2);					 \
284     }									 \
285   while (0)
286 #endif
287 
288 #ifndef ASM_OUTPUT_DEBUG_DELTA4
289 #define ASM_OUTPUT_DEBUG_DELTA4(FILE,LABEL1,LABEL2)			 \
290   do									 \
291     {									 \
292       fprintf ((FILE), "\t%s\t", VMS_UNALIGNED_INT_ASM_OP);		 \
293       assemble_name (FILE, LABEL1);					 \
294       fprintf (FILE, "-");						 \
295       assemble_name (FILE, LABEL2);					 \
296     }									 \
297   while (0)
298 #endif
299 
300 #ifndef ASM_OUTPUT_DEBUG_ADDR_DELTA
301 #define ASM_OUTPUT_DEBUG_ADDR_DELTA(FILE,LABEL1,LABEL2)			 \
302   do									 \
303     {									 \
304       fprintf ((FILE), "\t%s\t", UNALIGNED_PTR_ASM_OP);			 \
305       assemble_name (FILE, LABEL1);					 \
306       fprintf (FILE, "-");						 \
307       assemble_name (FILE, LABEL2);					 \
308     }									 \
309   while (0)
310 #endif
311 
312 #ifndef ASM_OUTPUT_DEBUG_ADDR
313 #define ASM_OUTPUT_DEBUG_ADDR(FILE,LABEL)				 \
314   do									 \
315     {									 \
316       fprintf ((FILE), "\t%s\t", UNALIGNED_PTR_ASM_OP);			 \
317       assemble_name (FILE, LABEL);					 \
318     }									 \
319   while (0)
320 #endif
321 
322 #ifndef ASM_OUTPUT_DEBUG_ADDR_CONST
323 #define ASM_OUTPUT_DEBUG_ADDR_CONST(FILE,ADDR)				\
324   fprintf ((FILE), "\t%s\t%s", UNALIGNED_PTR_ASM_OP, (ADDR))
325 #endif
326 
327 #ifndef ASM_OUTPUT_DEBUG_DATA1
328 #define ASM_OUTPUT_DEBUG_DATA1(FILE,VALUE) \
329   fprintf ((FILE), "\t%s\t%#x", VMS_ASM_BYTE_OP, (unsigned char) VALUE)
330 #endif
331 
332 #ifndef ASM_OUTPUT_DEBUG_DATA2
333 #define ASM_OUTPUT_DEBUG_DATA2(FILE,VALUE) \
334   fprintf ((FILE), "\t%s\t%#x", VMS_UNALIGNED_SHORT_ASM_OP, \
335 	   (unsigned short) VALUE)
336 #endif
337 
338 #ifndef ASM_OUTPUT_DEBUG_DATA4
339 #define ASM_OUTPUT_DEBUG_DATA4(FILE,VALUE) \
340   fprintf ((FILE), "\t%s\t%#lx", VMS_UNALIGNED_INT_ASM_OP, \
341 	   (unsigned long) VALUE)
342 #endif
343 
344 #ifndef ASM_OUTPUT_DEBUG_DATA
345 #define ASM_OUTPUT_DEBUG_DATA(FILE,VALUE) \
346   fprintf ((FILE), "\t%s\t%#lx", UNALIGNED_OFFSET_ASM_OP (VALUE), VALUE)
347 #endif
348 
349 #ifndef ASM_OUTPUT_DEBUG_ADDR_DATA
350 #define ASM_OUTPUT_DEBUG_ADDR_DATA(FILE,VALUE) \
351   fprintf ((FILE), "\t%s\t%#lx", UNALIGNED_PTR_ASM_OP, \
352 	   (unsigned long) VALUE)
353 #endif
354 
355 #ifndef ASM_OUTPUT_DEBUG_DATA8
356 #define ASM_OUTPUT_DEBUG_DATA8(FILE,VALUE) \
357   fprintf ((FILE), "\t%s\t%#llx", VMS_UNALIGNED_DOUBLE_INT_ASM_OP, \
358                                  (unsigned long long) VALUE)
359 #endif
360 
361 /* This is similar to the default ASM_OUTPUT_ASCII, except that no trailing
362    newline is produced.  When flag_verbose_asm is asserted, we add commentary
363    at the end of the line, so we must avoid output of a newline here.  */
364 #ifndef ASM_OUTPUT_DEBUG_STRING
365 #define ASM_OUTPUT_DEBUG_STRING(FILE,P)		\
366   do						\
367     {						\
368       register int slen = strlen (P);		\
369       register const char *p = (P);		\
370       register int i;				\
371       fprintf (FILE, "\t.ascii \"");		\
372       for (i = 0; i < slen; i++)		\
373 	{					\
374 	  register int c = p[i];		\
375 	  if (c == '\"' || c == '\\')		\
376 	    putc ('\\', FILE);			\
377 	  if (c >= ' ' && c < 0177)		\
378 	    putc (c, FILE);			\
379 	  else					\
380 	    fprintf (FILE, "\\%o", c);		\
381 	}					\
382       fprintf (FILE, "\"");			\
383     }						\
384   while (0)
385 #endif
386 
387 /* Convert a reference to the assembler name of a C-level name.  This
388    macro has the same effect as ASM_OUTPUT_LABELREF, but copies to
389    a string rather than writing to a file.  */
390 #ifndef ASM_NAME_TO_STRING
391 #define ASM_NAME_TO_STRING(STR, NAME)		\
392   do						\
393     {						\
394       if ((NAME)[0] == '*')			\
395 	strcpy (STR, NAME+1);			\
396       else					\
397 	strcpy (STR, NAME);			\
398     }						\
399   while (0)
400 #endif
401 
402 
403 /* Output the debug header HEADER.  Also output COMMENT if flag_verbose_asm is
404    set.  Return the header size.  Just return the size if DOSIZEONLY is
405    nonzero.  */
406 
407 static int
write_debug_header(DST_HEADER * header,const char * comment,int dosizeonly)408 write_debug_header (DST_HEADER *header, const char *comment, int dosizeonly)
409 {
410   if (!dosizeonly)
411     {
412       ASM_OUTPUT_DEBUG_DATA2 (asm_out_file,
413 			      header->dst__header_length.dst_w_length);
414 
415       if (flag_verbose_asm)
416 	fprintf (asm_out_file, "\t%s record length", ASM_COMMENT_START);
417       fputc ('\n', asm_out_file);
418 
419       ASM_OUTPUT_DEBUG_DATA2 (asm_out_file,
420 			      header->dst__header_type.dst_w_type);
421 
422       if (flag_verbose_asm)
423 	fprintf (asm_out_file, "\t%s record type (%s)", ASM_COMMENT_START,
424 		 comment);
425 
426       fputc ('\n', asm_out_file);
427     }
428 
429   return 4;
430 }
431 
432 /* Output the address of SYMBOL.  Also output COMMENT if flag_verbose_asm is
433    set.  Return the address size.  Just return the size if DOSIZEONLY is
434    nonzero.  */
435 
436 static int
write_debug_addr(const char * symbol,const char * comment,int dosizeonly)437 write_debug_addr (const char *symbol, const char *comment, int dosizeonly)
438 {
439   if (!dosizeonly)
440     {
441       ASM_OUTPUT_DEBUG_ADDR (asm_out_file, symbol);
442       if (flag_verbose_asm)
443 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
444       fputc ('\n', asm_out_file);
445     }
446 
447   return PTR_SIZE;
448 }
449 
450 /* Output the single byte DATA1.  Also output COMMENT if flag_verbose_asm is
451    set.  Return the data size.  Just return the size if DOSIZEONLY is
452    nonzero.  */
453 
454 static int
write_debug_data1(unsigned int data1,const char * comment,int dosizeonly)455 write_debug_data1 (unsigned int data1, const char *comment, int dosizeonly)
456 {
457   if (!dosizeonly)
458     {
459       ASM_OUTPUT_DEBUG_DATA1 (asm_out_file, data1);
460       if (flag_verbose_asm)
461 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
462       fputc ('\n', asm_out_file);
463     }
464 
465   return 1;
466 }
467 
468 /* Output the single word DATA2.  Also output COMMENT if flag_verbose_asm is
469    set.  Return the data size.  Just return the size if DOSIZEONLY is
470    nonzero.  */
471 
472 static int
write_debug_data2(unsigned int data2,const char * comment,int dosizeonly)473 write_debug_data2 (unsigned int data2, const char *comment, int dosizeonly)
474 {
475   if (!dosizeonly)
476     {
477       ASM_OUTPUT_DEBUG_DATA2 (asm_out_file, data2);
478       if (flag_verbose_asm)
479 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
480       fputc ('\n', asm_out_file);
481     }
482 
483   return 2;
484 }
485 
486 /* Output double word DATA4.  Also output COMMENT if flag_verbose_asm is set.
487    Return the data size.  Just return the size if DOSIZEONLY is nonzero.  */
488 
489 static int
write_debug_data4(unsigned long data4,const char * comment,int dosizeonly)490 write_debug_data4 (unsigned long data4, const char *comment, int dosizeonly)
491 {
492   if (!dosizeonly)
493     {
494       ASM_OUTPUT_DEBUG_DATA4 (asm_out_file, data4);
495       if (flag_verbose_asm)
496 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
497       fputc ('\n', asm_out_file);
498     }
499 
500   return 4;
501 }
502 
503 /* Output quad word DATA8.  Also output COMMENT if flag_verbose_asm is set.
504    Return the data size.  Just return the size if DOSIZEONLY is nonzero.  */
505 
506 static int
write_debug_data8(unsigned long long data8,const char * comment,int dosizeonly)507 write_debug_data8 (unsigned long long data8, const char *comment,
508 		   int dosizeonly)
509 {
510   if (!dosizeonly)
511     {
512       ASM_OUTPUT_DEBUG_DATA8 (asm_out_file, data8);
513       if (flag_verbose_asm)
514 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
515       fputc ('\n', asm_out_file);
516     }
517 
518   return 8;
519 }
520 
521 /* Output the difference between LABEL1 and LABEL2.  Also output COMMENT if
522    flag_verbose_asm is set.  Return the data size.  Just return the size if
523    DOSIZEONLY is nonzero.  */
524 
525 static int
write_debug_delta4(const char * label1,const char * label2,const char * comment,int dosizeonly)526 write_debug_delta4 (const char *label1, const char *label2,
527 		    const char *comment, int dosizeonly)
528 {
529   if (!dosizeonly)
530     {
531       ASM_OUTPUT_DEBUG_DELTA4 (asm_out_file, label1, label2);
532       if (flag_verbose_asm)
533 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
534       fputc ('\n', asm_out_file);
535     }
536 
537   return 4;
538 }
539 
540 /* Output a character string STRING.  Also write COMMENT if flag_verbose_asm is
541    set.  Return the string length.  Just return the length if DOSIZEONLY is
542    nonzero.  */
543 
544 static int
write_debug_string(const char * string,const char * comment,int dosizeonly)545 write_debug_string (const char *string, const char *comment, int dosizeonly)
546 {
547   if (!dosizeonly)
548     {
549       ASM_OUTPUT_DEBUG_STRING (asm_out_file, string);
550       if (flag_verbose_asm)
551 	fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
552       fputc ('\n', asm_out_file);
553     }
554 
555   return strlen (string);
556 }
557 
558 /* Output a module begin header and return the header size.  Just return the
559    size if DOSIZEONLY is nonzero.  */
560 
561 static int
write_modbeg(int dosizeonly)562 write_modbeg (int dosizeonly)
563 {
564   DST_MODULE_BEGIN modbeg;
565   DST_MB_TRLR mb_trlr;
566   int i;
567   char *module_name, *m;
568   int modnamelen;
569   int prodnamelen;
570   int totsize = 0;
571 
572   /* Assumes primary filename has Unix syntax file spec.  */
573   module_name = xstrdup (lbasename (primary_filename));
574 
575   m = strrchr (module_name, '.');
576   if (m)
577     *m = 0;
578 
579   modnamelen = strlen (module_name);
580   for (i = 0; i < modnamelen; i++)
581     module_name[i] = TOUPPER (module_name[i]);
582 
583   prodnamelen = strlen (module_producer);
584 
585   modbeg.dst_a_modbeg_header.dst__header_length.dst_w_length
586     = DST_K_MODBEG_SIZE + modnamelen + DST_K_MB_TRLR_SIZE + prodnamelen - 1;
587   modbeg.dst_a_modbeg_header.dst__header_type.dst_w_type = DST_K_MODBEG;
588   modbeg.dst_b_modbeg_flags.dst_v_modbeg_hide = 0;
589   modbeg.dst_b_modbeg_flags.dst_v_modbeg_version = 1;
590   modbeg.dst_b_modbeg_flags.dst_v_modbeg_unused = 0;
591   modbeg.dst_b_modbeg_unused = 0;
592   modbeg.dst_l_modbeg_language = (DST_LANGUAGE) module_language;
593   modbeg.dst_w_version_major = DST_K_VERSION_MAJOR;
594   modbeg.dst_w_version_minor = DST_K_VERSION_MINOR;
595   modbeg.dst_b_modbeg_name = strlen (module_name);
596 
597   mb_trlr.dst_b_compiler = strlen (module_producer);
598 
599   totsize += write_debug_header (&modbeg.dst_a_modbeg_header,
600 				 "modbeg", dosizeonly);
601   totsize += write_debug_data1 (*((char *) &modbeg.dst_b_modbeg_flags),
602 				"flags", dosizeonly);
603   totsize += write_debug_data1 (modbeg.dst_b_modbeg_unused,
604 				"unused", dosizeonly);
605   totsize += write_debug_data4 (modbeg.dst_l_modbeg_language,
606 				"language", dosizeonly);
607   totsize += write_debug_data2 (modbeg.dst_w_version_major,
608 				"DST major version", dosizeonly);
609   totsize += write_debug_data2 (modbeg.dst_w_version_minor,
610 				"DST minor version", dosizeonly);
611   totsize += write_debug_data1 (modbeg.dst_b_modbeg_name,
612 				"length of module name", dosizeonly);
613   totsize += write_debug_string (module_name, "module name", dosizeonly);
614   totsize += write_debug_data1 (mb_trlr.dst_b_compiler,
615 				"length of compiler name", dosizeonly);
616   totsize += write_debug_string (module_producer, "compiler name", dosizeonly);
617 
618   return totsize;
619 }
620 
621 /* Output a module end trailer and return the trailer size.   Just return
622    the size if DOSIZEONLY is nonzero.  */
623 
624 static int
write_modend(int dosizeonly)625 write_modend (int dosizeonly)
626 {
627   DST_MODULE_END modend;
628   int totsize = 0;
629 
630   modend.dst_a_modend_header.dst__header_length.dst_w_length
631    = DST_K_MODEND_SIZE - 1;
632   modend.dst_a_modend_header.dst__header_type.dst_w_type = DST_K_MODEND;
633 
634   totsize += write_debug_header (&modend.dst_a_modend_header, "modend",
635 				 dosizeonly);
636 
637   return totsize;
638 }
639 
640 /* Output a routine begin header routine RTNNUM and return the header size.
641    Just return the size if DOSIZEONLY is nonzero.  */
642 
643 static int
write_rtnbeg(int rtnnum,int dosizeonly)644 write_rtnbeg (int rtnnum, int dosizeonly)
645 {
646   const char *rtnname;
647   int rtnnamelen;
648   char *rtnentryname;
649   int totsize = 0;
650   char label[MAX_ARTIFICIAL_LABEL_BYTES];
651   DST_ROUTINE_BEGIN rtnbeg;
652   DST_PROLOG prolog;
653 
654   rtnname = funcnam_table[rtnnum];
655   rtnnamelen = strlen (rtnname);
656   rtnentryname = concat (rtnname, "..en", NULL);
657 
658   if (!strcmp (rtnname, "main"))
659     {
660       DST_HEADER header;
661       const char *go = "TRANSFER$BREAK$GO";
662 
663       /* This command isn't documented in DSTRECORDS, so it's made to
664 	 look like what DEC C does */
665 
666       /* header size - 1st byte + flag byte + STO_LW size
667 	 + string count byte + string length */
668       header.dst__header_length.dst_w_length
669 	= DST_K_DST_HEADER_SIZE - 1 + 1 + 4 + 1 + strlen (go);
670       header.dst__header_type.dst_w_type = DST_K_TBG;
671 
672       totsize += write_debug_header (&header, "transfer", dosizeonly);
673 
674       /* I think this is a flag byte, but I don't know what this flag means */
675       totsize += write_debug_data1 (0x1, "flags ???", dosizeonly);
676 
677       /* Routine Begin PD Address */
678       totsize += write_debug_addr (rtnname, "main procedure descriptor",
679 				   dosizeonly);
680       totsize += write_debug_data1 (strlen (go), "length of main_name",
681 				    dosizeonly);
682       totsize += write_debug_string (go, "main name", dosizeonly);
683     }
684 
685   /* The header length never includes the length byte.  */
686   rtnbeg.dst_a_rtnbeg_header.dst__header_length.dst_w_length
687    = DST_K_RTNBEG_SIZE + rtnnamelen - 1;
688   rtnbeg.dst_a_rtnbeg_header.dst__header_type.dst_w_type = DST_K_RTNBEG;
689   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_unused = 0;
690   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_unalloc = 0;
691   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_prototype = 0;
692   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_inlined = 0;
693   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_no_call = 1;
694   rtnbeg.dst_b_rtnbeg_name = rtnnamelen;
695 
696   totsize += write_debug_header (&rtnbeg.dst_a_rtnbeg_header, "rtnbeg",
697 				 dosizeonly);
698   totsize += write_debug_data1 (*((char *) &rtnbeg.dst_b_rtnbeg_flags),
699 				"flags", dosizeonly);
700 
701   /* Routine Begin Address */
702   totsize += write_debug_addr (rtnentryname, "routine entry name", dosizeonly);
703 
704   /* Routine Begin PD Address */
705   totsize += write_debug_addr (rtnname, "routine procedure descriptor",
706 			       dosizeonly);
707 
708   /* Routine Begin Name */
709   totsize += write_debug_data1 (rtnbeg.dst_b_rtnbeg_name,
710 				"length of routine name", dosizeonly);
711 
712   totsize += write_debug_string (rtnname, "routine name", dosizeonly);
713 
714   free (rtnentryname);
715 
716   if (debug_info_level > DINFO_LEVEL_TERSE)
717     {
718       prolog.dst_a_prolog_header.dst__header_length.dst_w_length
719 	= DST_K_PROLOG_SIZE - 1;
720       prolog.dst_a_prolog_header.dst__header_type.dst_w_type = DST_K_PROLOG;
721 
722       totsize += write_debug_header (&prolog.dst_a_prolog_header, "prolog",
723 				     dosizeonly);
724 
725       ASM_GENERATE_INTERNAL_LABEL
726         (label, FUNC_PROLOG_LABEL,
727 	 funcnum_table[rtnnum]);
728       totsize += write_debug_addr (label, "prolog breakpoint addr",
729 				   dosizeonly);
730     }
731 
732   return totsize;
733 }
734 
735 /* Output a routine end trailer for routine RTNNUM and return the header size.
736    Just return the size if DOSIZEONLY is nonzero.  */
737 
738 static int
write_rtnend(int rtnnum,int dosizeonly)739 write_rtnend (int rtnnum, int dosizeonly)
740 {
741   DST_ROUTINE_END rtnend;
742   char label1[MAX_ARTIFICIAL_LABEL_BYTES];
743   char label2[MAX_ARTIFICIAL_LABEL_BYTES];
744   int totsize;
745 
746   totsize = 0;
747 
748   rtnend.dst_a_rtnend_header.dst__header_length.dst_w_length
749    = DST_K_RTNEND_SIZE - 1;
750   rtnend.dst_a_rtnend_header.dst__header_type.dst_w_type = DST_K_RTNEND;
751   rtnend.dst_b_rtnend_unused = 0;
752   rtnend.dst_l_rtnend_size = 0; /* Calculated below.  */
753 
754   totsize += write_debug_header (&rtnend.dst_a_rtnend_header, "rtnend",
755 				 dosizeonly);
756   totsize += write_debug_data1 (rtnend.dst_b_rtnend_unused, "unused",
757 				dosizeonly);
758 
759   ASM_GENERATE_INTERNAL_LABEL
760    (label1, FUNC_BEGIN_LABEL,
761     funcnum_table[rtnnum]);
762   ASM_GENERATE_INTERNAL_LABEL
763    (label2, FUNC_END_LABEL,
764     funcnum_table[rtnnum]);
765   totsize += write_debug_delta4 (label2, label1, "routine size", dosizeonly);
766 
767   return totsize;
768 }
769 
770 #define K_DELTA_PC(I) \
771  ((I) < 128 ? -(I) : (I) < 65536 ? DST_K_DELTA_PC_W : DST_K_DELTA_PC_L)
772 
773 #define K_SET_LINUM(I) \
774  ((I) < 256 ? DST_K_SET_LINUM_B \
775   : (I) < 65536 ? DST_K_SET_LINUM : DST_K_SET_LINUM_L)
776 
777 #define K_INCR_LINUM(I) \
778  ((I) < 256 ? DST_K_INCR_LINUM \
779   : (I) < 65536 ? DST_K_INCR_LINUM_W : DST_K_INCR_LINUM_L)
780 
781 /* Output the PC to line number correlations and return the size.  Just return
782    the size if DOSIZEONLY is nonzero */
783 
784 static int
write_pclines(int dosizeonly)785 write_pclines (int dosizeonly)
786 {
787   unsigned i;
788   int fn;
789   int ln, lastln;
790   int linestart = 0;
791   int max_line;
792   DST_LINE_NUM_HEADER line_num;
793   DST_PCLINE_COMMANDS pcline;
794   char label[MAX_ARTIFICIAL_LABEL_BYTES];
795   char lastlabel[MAX_ARTIFICIAL_LABEL_BYTES];
796   int totsize = 0;
797   char buff[256];
798 
799   max_line = file_info_table[1].max_line;
800   file_info_table[1].listing_line_start = linestart;
801   linestart = linestart + ((max_line / 100000) + 1) * 100000;
802 
803   for (i = 2; i < file_info_table_in_use; i++)
804     {
805       max_line = file_info_table[i].max_line;
806       file_info_table[i].listing_line_start = linestart;
807       linestart = linestart + ((max_line / 10000) + 1) * 10000;
808     }
809 
810   /* Set starting address to beginning of text section.  */
811   line_num.dst_a_line_num_header.dst__header_length.dst_w_length = 8;
812   line_num.dst_a_line_num_header.dst__header_type.dst_w_type = DST_K_LINE_NUM;
813   pcline.dst_b_pcline_command = DST_K_SET_ABS_PC;
814 
815   totsize += write_debug_header (&line_num.dst_a_line_num_header,
816 				 "line_num", dosizeonly);
817   totsize += write_debug_data1 (pcline.dst_b_pcline_command,
818 				"line_num (SET ABS PC)", dosizeonly);
819 
820   if (dosizeonly)
821     totsize += 4;
822   else
823     {
824       ASM_OUTPUT_DEBUG_ADDR (asm_out_file, TEXT_SECTION_ASM_OP);
825       if (flag_verbose_asm)
826 	fprintf (asm_out_file, "\t%s line_num", ASM_COMMENT_START);
827       fputc ('\n', asm_out_file);
828     }
829 
830   fn = line_info_table[1].dst_file_num;
831   ln = (file_info_table[fn].listing_line_start
832 	+ line_info_table[1].dst_line_num);
833   line_num.dst_a_line_num_header.dst__header_length.dst_w_length = 4 + 4;
834   pcline.dst_b_pcline_command = DST_K_SET_LINUM_L;
835 
836   totsize += write_debug_header (&line_num.dst_a_line_num_header,
837 				 "line_num", dosizeonly);
838   totsize += write_debug_data1 (pcline.dst_b_pcline_command,
839 				"line_num (SET LINUM LONG)", dosizeonly);
840 
841   sprintf (buff, "line_num (%d)", ln ? ln - 1 : 0);
842   totsize += write_debug_data4 (ln ? ln - 1 : 0, buff, dosizeonly);
843 
844   lastln = ln;
845   strcpy (lastlabel, TEXT_SECTION_ASM_OP);
846   for (i = 1; i < line_info_table_in_use; i++)
847     {
848       int extrabytes;
849 
850       fn = line_info_table[i].dst_file_num;
851       ln = (file_info_table[fn].listing_line_start
852 	    + line_info_table[i].dst_line_num);
853 
854       if (ln - lastln > 1)
855 	extrabytes = 5; /* NUMBYTES (ln - lastln - 1) + 1; */
856       else if (ln <= lastln)
857 	extrabytes = 5; /* NUMBYTES (ln - 1) + 1; */
858       else
859 	extrabytes = 0;
860 
861       line_num.dst_a_line_num_header.dst__header_length.dst_w_length
862 	= 8 + extrabytes;
863 
864       totsize += write_debug_header
865 	(&line_num.dst_a_line_num_header, "line_num", dosizeonly);
866 
867       if (ln - lastln > 1)
868 	{
869 	  int lndif = ln - lastln - 1;
870 
871 	  /* K_INCR_LINUM (lndif); */
872 	  pcline.dst_b_pcline_command = DST_K_INCR_LINUM_L;
873 
874 	  totsize += write_debug_data1 (pcline.dst_b_pcline_command,
875 					"line_num (INCR LINUM LONG)",
876 					dosizeonly);
877 
878 	  sprintf (buff, "line_num (%d)", lndif);
879 	  totsize += write_debug_data4 (lndif, buff, dosizeonly);
880 	}
881       else if (ln <= lastln)
882 	{
883 	  /* K_SET_LINUM (ln-1); */
884 	  pcline.dst_b_pcline_command = DST_K_SET_LINUM_L;
885 
886 	  totsize += write_debug_data1 (pcline.dst_b_pcline_command,
887 					"line_num (SET LINUM LONG)",
888 					dosizeonly);
889 
890 	  sprintf (buff, "line_num (%d)", ln - 1);
891 	  totsize += write_debug_data4 (ln - 1, buff, dosizeonly);
892 	}
893 
894       pcline.dst_b_pcline_command = DST_K_DELTA_PC_L;
895 
896       totsize += write_debug_data1 (pcline.dst_b_pcline_command,
897 				    "line_num (DELTA PC LONG)", dosizeonly);
898 
899       ASM_GENERATE_INTERNAL_LABEL (label, LINE_CODE_LABEL, i);
900       totsize += write_debug_delta4 (label, lastlabel, "increment line_num",
901 				     dosizeonly);
902 
903       lastln = ln;
904       strcpy (lastlabel, label);
905     }
906 
907   return totsize;
908 }
909 
910 /* Output a source correlation for file FILEID using information saved in
911    FILE_INFO_ENTRY and return the size.  Just return the size if DOSIZEONLY is
912    nonzero.  */
913 
914 static int
write_srccorr(int fileid,dst_file_info_entry file_info_entry,int dosizeonly)915 write_srccorr (int fileid, dst_file_info_entry file_info_entry,
916 	       int dosizeonly)
917 {
918   int src_command_size;
919   int linesleft = file_info_entry.max_line;
920   int linestart = file_info_entry.listing_line_start;
921   int flen = strlen (file_info_entry.file_name);
922   int linestodo = 0;
923   DST_SOURCE_CORR src_header;
924   DST_SRC_COMMAND src_command;
925   DST_SRC_COMMAND src_command_sf;
926   DST_SRC_COMMAND src_command_sl;
927   DST_SRC_COMMAND src_command_sr;
928   DST_SRC_COMMAND src_command_dl;
929   DST_SRC_CMDTRLR src_cmdtrlr;
930   char buff[256];
931   int totsize = 0;
932 
933   if (fileid == 1)
934     {
935       src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
936 	= DST_K_SOURCE_CORR_HEADER_SIZE + 1 - 1;
937       src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
938 	= DST_K_SOURCE;
939       src_command.dst_b_src_command = DST_K_SRC_FORMFEED;
940 
941       totsize += write_debug_header (&src_header.dst_a_source_corr_header,
942 				     "source corr", dosizeonly);
943 
944       totsize += write_debug_data1 (src_command.dst_b_src_command,
945 				    "source_corr (SRC FORMFEED)",
946 				    dosizeonly);
947     }
948 
949   src_command_size
950     = DST_K_SRC_COMMAND_SIZE + flen + DST_K_SRC_CMDTRLR_SIZE;
951   src_command.dst_b_src_command = DST_K_SRC_DECLFILE;
952   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_length
953     = src_command_size - 2;
954   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_flags = 0;
955   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_fileid
956     = fileid;
957   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_q_src_df_rms_cdt
958     = file_info_entry.cdt;
959   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_l_src_df_rms_ebk
960     = file_info_entry.ebk;
961   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_rms_ffb
962     = file_info_entry.ffb;
963   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_rms_rfo
964     = file_info_entry.rfo;
965   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_filename
966     = flen;
967 
968   src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
969     = DST_K_SOURCE_CORR_HEADER_SIZE + src_command_size - 1;
970   src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
971     = DST_K_SOURCE;
972 
973   src_cmdtrlr.dst_b_src_df_libmodname = 0;
974 
975   totsize += write_debug_header (&src_header.dst_a_source_corr_header,
976 				 "source corr", dosizeonly);
977   totsize += write_debug_data1 (src_command.dst_b_src_command,
978 				"source_corr (DECL SRC FILE)", dosizeonly);
979   totsize += write_debug_data1
980     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_length,
981      "source_corr (length)", dosizeonly);
982 
983   totsize += write_debug_data1
984     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_flags,
985      "source_corr (flags)", dosizeonly);
986 
987   totsize += write_debug_data2
988     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_fileid,
989      "source_corr (fileid)", dosizeonly);
990 
991   totsize += write_debug_data8
992     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_q_src_df_rms_cdt,
993      "source_corr (creation date)", dosizeonly);
994 
995   totsize += write_debug_data4
996     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_l_src_df_rms_ebk,
997      "source_corr (EOF block number)", dosizeonly);
998 
999   totsize += write_debug_data2
1000     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_rms_ffb,
1001      "source_corr (first free byte)", dosizeonly);
1002 
1003   totsize += write_debug_data1
1004     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_rms_rfo,
1005      "source_corr (record and file organization)", dosizeonly);
1006 
1007   totsize += write_debug_data1
1008     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_filename,
1009      "source_corr (filename length)", dosizeonly);
1010 
1011   totsize += write_debug_string (remap_debug_filename (
1012 				    file_info_entry.file_name),
1013 				 "source file name", dosizeonly);
1014   totsize += write_debug_data1 (src_cmdtrlr.dst_b_src_df_libmodname,
1015 				"source_corr (libmodname)", dosizeonly);
1016 
1017   src_command_sf.dst_b_src_command = DST_K_SRC_SETFILE;
1018   src_command_sf.dst_a_src_cmd_fields.dst_w_src_unsword = fileid;
1019 
1020   src_command_sr.dst_b_src_command = DST_K_SRC_SETREC_W;
1021   src_command_sr.dst_a_src_cmd_fields.dst_w_src_unsword = 1;
1022 
1023   src_command_sl.dst_b_src_command = DST_K_SRC_SETLNUM_L;
1024   src_command_sl.dst_a_src_cmd_fields.dst_l_src_unslong = linestart + 1;
1025 
1026   src_command_dl.dst_b_src_command = DST_K_SRC_DEFLINES_W;
1027 
1028   if (linesleft > 65534)
1029     linesleft = linesleft - 65534, linestodo = 65534;
1030   else
1031     linestodo = linesleft, linesleft = 0;
1032 
1033   src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword = linestodo;
1034 
1035   src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
1036     = DST_K_SOURCE_CORR_HEADER_SIZE + 3 + 3 + 5 + 3 - 1;
1037   src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
1038     = DST_K_SOURCE;
1039 
1040   if (src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword)
1041     {
1042       totsize += write_debug_header (&src_header.dst_a_source_corr_header,
1043 				     "source corr", dosizeonly);
1044 
1045       totsize += write_debug_data1 (src_command_sf.dst_b_src_command,
1046 				    "source_corr (src setfile)", dosizeonly);
1047 
1048       totsize += write_debug_data2
1049 	(src_command_sf.dst_a_src_cmd_fields.dst_w_src_unsword,
1050 	 "source_corr (fileid)", dosizeonly);
1051 
1052       totsize += write_debug_data1 (src_command_sr.dst_b_src_command,
1053 				    "source_corr (setrec)", dosizeonly);
1054 
1055       totsize += write_debug_data2
1056 	(src_command_sr.dst_a_src_cmd_fields.dst_w_src_unsword,
1057 	 "source_corr (recnum)", dosizeonly);
1058 
1059       totsize += write_debug_data1 (src_command_sl.dst_b_src_command,
1060 				    "source_corr (setlnum)", dosizeonly);
1061 
1062       totsize += write_debug_data4
1063 	(src_command_sl.dst_a_src_cmd_fields.dst_l_src_unslong,
1064 	 "source_corr (linenum)", dosizeonly);
1065 
1066       totsize += write_debug_data1 (src_command_dl.dst_b_src_command,
1067 				    "source_corr (deflines)", dosizeonly);
1068 
1069       sprintf (buff, "source_corr (%d)",
1070 	       src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword);
1071       totsize += write_debug_data2
1072 	(src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword,
1073 	 buff, dosizeonly);
1074 
1075       while (linesleft > 0)
1076 	{
1077 	  src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
1078 	    = DST_K_SOURCE_CORR_HEADER_SIZE + 3 - 1;
1079 	  src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
1080 	    = DST_K_SOURCE;
1081 	  src_command_dl.dst_b_src_command = DST_K_SRC_DEFLINES_W;
1082 
1083 	  if (linesleft > 65534)
1084 	    linesleft = linesleft - 65534, linestodo = 65534;
1085 	  else
1086 	    linestodo = linesleft, linesleft = 0;
1087 
1088 	  src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword = linestodo;
1089 
1090 	  totsize += write_debug_header (&src_header.dst_a_source_corr_header,
1091 					 "source corr", dosizeonly);
1092 	  totsize += write_debug_data1 (src_command_dl.dst_b_src_command,
1093 					"source_corr (deflines)", dosizeonly);
1094 	  sprintf (buff, "source_corr (%d)",
1095 		   src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword);
1096 	  totsize += write_debug_data2
1097 	    (src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword,
1098 	     buff, dosizeonly);
1099 	}
1100     }
1101 
1102   return totsize;
1103 }
1104 
1105 /* Output all the source correlation entries and return the size.  Just return
1106    the size if DOSIZEONLY is nonzero.  */
1107 
1108 static int
write_srccorrs(int dosizeonly)1109 write_srccorrs (int dosizeonly)
1110 {
1111   unsigned int i;
1112   int totsize = 0;
1113 
1114   for (i = 1; i < file_info_table_in_use; i++)
1115     totsize += write_srccorr (i, file_info_table[i], dosizeonly);
1116 
1117   return totsize;
1118 }
1119 
1120 /* Output a marker (i.e. a label) for the beginning of a function, before
1121    the prologue.  */
1122 
1123 static void
vmsdbgout_begin_prologue(unsigned int line,unsigned int column,const char * file)1124 vmsdbgout_begin_prologue (unsigned int line, unsigned int column,
1125 			  const char *file)
1126 {
1127   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 
1129   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1130     (*dwarf2_debug_hooks.begin_prologue) (line, column, file);
1131 
1132   if (debug_info_level > DINFO_LEVEL_NONE)
1133     {
1134       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1135 				   current_function_funcdef_no);
1136       ASM_OUTPUT_LABEL (asm_out_file, label);
1137     }
1138 }
1139 
1140 /* Output a marker (i.e. a label) for the beginning of a function, after
1141    the prologue.  */
1142 
1143 static void
vmsdbgout_end_prologue(unsigned int line,const char * file)1144 vmsdbgout_end_prologue (unsigned int line, const char *file)
1145 {
1146   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1147 
1148   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1149     (*dwarf2_debug_hooks.end_prologue) (line, file);
1150 
1151   if (debug_info_level > DINFO_LEVEL_TERSE)
1152     {
1153       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_PROLOG_LABEL,
1154 				   current_function_funcdef_no);
1155       ASM_OUTPUT_LABEL (asm_out_file, label);
1156 
1157       /* VMS PCA expects every PC range to correlate to some line and file.  */
1158       vmsdbgout_write_source_line (line, file, 0, true);
1159     }
1160 }
1161 
1162 /* No output for VMS debug, but make obligatory call to Dwarf2 debug */
1163 
1164 static void
vmsdbgout_end_function(unsigned int line)1165 vmsdbgout_end_function (unsigned int line)
1166 {
1167   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1168     (*dwarf2_debug_hooks.end_function) (line);
1169 }
1170 
1171 /* Output a marker (i.e. a label) for the beginning of the epilogue.
1172    This gets called *before* the epilogue code has been generated.  */
1173 
1174 static void
vmsdbgout_begin_epilogue(unsigned int line,const char * file)1175 vmsdbgout_begin_epilogue (unsigned int line, const char *file)
1176 {
1177   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1178   static int save_current_function_funcdef_no = -1;
1179 
1180   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1181     (*dwarf2_debug_hooks.begin_epilogue) (line, file);
1182 
1183   if (debug_info_level > DINFO_LEVEL_NONE)
1184     {
1185       if (save_current_function_funcdef_no != current_function_funcdef_no)
1186 	{
1187 	  /* Output a label to mark the endpoint of the code generated for this
1188 	     function.  */
1189 	  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_EPILOG_LABEL,
1190 				       current_function_funcdef_no);
1191 
1192 	  ASM_OUTPUT_LABEL (asm_out_file, label);
1193 
1194 	  save_current_function_funcdef_no = current_function_funcdef_no;
1195 
1196 	  /* VMS PCA expects every PC range to correlate to some line and
1197 	     file.  */
1198 	  vmsdbgout_write_source_line (line, file, 0, true);
1199 	}
1200     }
1201 }
1202 
1203 /* Output a marker (i.e. a label) for the absolute end of the generated code
1204    for a function definition.  This gets called *after* the epilogue code has
1205    been generated.  */
1206 
1207 static void
vmsdbgout_end_epilogue(unsigned int line,const char * file)1208 vmsdbgout_end_epilogue (unsigned int line, const char *file)
1209 {
1210   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1211 
1212   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1213     (*dwarf2_debug_hooks.end_epilogue) (line, file);
1214 
1215   if (debug_info_level > DINFO_LEVEL_NONE)
1216     {
1217       /* Output a label to mark the endpoint of the code generated for this
1218          function.  */
1219       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1220 				   current_function_funcdef_no);
1221       ASM_OUTPUT_LABEL (asm_out_file, label);
1222 
1223       /* VMS PCA expects every PC range to correlate to some line and file.  */
1224       vmsdbgout_write_source_line (line, file, 0, true);
1225     }
1226 }
1227 
1228 /* Output a marker (i.e. a label) for the beginning of the generated code for
1229    a lexical block.  */
1230 
1231 static void
vmsdbgout_begin_block(register unsigned line,register unsigned blocknum)1232 vmsdbgout_begin_block (register unsigned line, register unsigned blocknum)
1233 {
1234   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1235     (*dwarf2_debug_hooks.begin_block) (line, blocknum);
1236 
1237   if (debug_info_level > DINFO_LEVEL_TERSE)
1238     targetm.asm_out.internal_label (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
1239 }
1240 
1241 /* Output a marker (i.e. a label) for the end of the generated code for a
1242    lexical block.  */
1243 
1244 static void
vmsdbgout_end_block(register unsigned line,register unsigned blocknum)1245 vmsdbgout_end_block (register unsigned line, register unsigned blocknum)
1246 {
1247   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1248     (*dwarf2_debug_hooks.end_block) (line, blocknum);
1249 
1250   if (debug_info_level > DINFO_LEVEL_TERSE)
1251     targetm.asm_out.internal_label (asm_out_file, BLOCK_END_LABEL, blocknum);
1252 }
1253 
1254 /* Not implemented in VMS Debug.  */
1255 
1256 static bool
vmsdbgout_ignore_block(const_tree block)1257 vmsdbgout_ignore_block (const_tree block)
1258 {
1259   bool retval = 0;
1260 
1261   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1262     retval = (*dwarf2_debug_hooks.ignore_block) (block);
1263 
1264   return retval;
1265 }
1266 
1267 /* Add an entry for function DECL into the funcnam_table.  */
1268 
1269 static void
vmsdbgout_begin_function(tree decl)1270 vmsdbgout_begin_function (tree decl)
1271 {
1272   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1273 
1274   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1275     (*dwarf2_debug_hooks.begin_function) (decl);
1276 
1277   /* Add the new entry to the end of the function name table.  */
1278   funcnam_table.safe_push (xstrdup (name));
1279   funcnum_table.safe_push (current_function_funcdef_no);
1280 }
1281 
1282 static char fullname_buff [4096];
1283 
1284 /* Return the full file specification for FILENAME.  The specification must be
1285    in VMS syntax in order to be processed by VMS Debug.  */
1286 
1287 static char *
full_name(const char * filename)1288 full_name (const char *filename)
1289 {
1290 #ifdef VMS
1291   FILE *fp = fopen (filename, "r");
1292 
1293   fgetname (fp, fullname_buff, 1);
1294   fclose (fp);
1295 #else
1296   /* Unix paths really mess up VMS debug. Better to just output the
1297      base filename.  */
1298   strcpy (fullname_buff, filename);
1299 #endif
1300 
1301   return fullname_buff;
1302 }
1303 
1304 /* Lookup a filename (in the list of filenames that we know about here in
1305    vmsdbgout.c) and return its "index".  The index of each (known) filename is
1306    just a unique number which is associated with only that one filename.  We
1307    need such numbers for the sake of generating labels  and references
1308    to those files numbers.  If the filename given as an argument is not
1309    found in our current list, add it to the list and assign it the next
1310    available unique index number.  In order to speed up searches, we remember
1311    the index of the filename was looked up last.  This handles the majority of
1312    all searches.  */
1313 
1314 static unsigned int
lookup_filename(const char * file_name)1315 lookup_filename (const char *file_name)
1316 {
1317   static unsigned int last_file_lookup_index = 0;
1318   register char *fn;
1319   register unsigned i;
1320   const char *fnam;
1321   long long cdt = 0;
1322   long ebk = 0;
1323   short ffb = 0;
1324   char rfo = 0;
1325   long siz = 0;
1326   int ver = 0;
1327 
1328   fnam = full_name (file_name);
1329 
1330   /* Check to see if the file name that was searched on the previous call
1331      matches this file name. If so, return the index.  */
1332   if (last_file_lookup_index != 0)
1333     {
1334       fn = file_info_table[last_file_lookup_index].file_name;
1335       if (strcmp (fnam, fn) == 0)
1336 	return last_file_lookup_index;
1337     }
1338 
1339   /* Didn't match the previous lookup, search the table */
1340   for (i = 1; i < file_info_table_in_use; ++i)
1341     {
1342       fn = file_info_table[i].file_name;
1343       if (strcmp (fnam, fn) == 0)
1344 	{
1345 	  last_file_lookup_index = i;
1346 	  return i;
1347 	}
1348     }
1349 
1350   /* Prepare to add a new table entry by making sure there is enough space in
1351      the table to do so.  If not, expand the current table.  */
1352   if (file_info_table_in_use == file_info_table_allocated)
1353     {
1354 
1355       file_info_table_allocated += FILE_TABLE_INCREMENT;
1356       file_info_table = XRESIZEVEC (dst_file_info_entry, file_info_table,
1357 				    file_info_table_allocated);
1358     }
1359 
1360   if (vms_file_stats_name (file_name, &cdt, &siz, &rfo, &ver) == 0)
1361     {
1362       ebk = siz / 512 + 1;
1363       ffb = siz - ((siz / 512) * 512);
1364     }
1365 
1366   /* Add the new entry to the end of the filename table.  */
1367   file_info_table[file_info_table_in_use].file_name = xstrdup (fnam);
1368   file_info_table[file_info_table_in_use].max_line = 0;
1369   file_info_table[file_info_table_in_use].cdt = cdt;
1370   file_info_table[file_info_table_in_use].ebk = ebk;
1371   file_info_table[file_info_table_in_use].ffb = ffb;
1372   file_info_table[file_info_table_in_use].rfo = rfo;
1373 
1374   last_file_lookup_index = file_info_table_in_use++;
1375   return last_file_lookup_index;
1376 }
1377 
1378 /* Output a label to mark the beginning of a source code line entry
1379    and record information relating to this source line, in
1380    'line_info_table' for later output of the .debug_line section.  */
1381 
1382 static void
vmsdbgout_write_source_line(unsigned line,const char * filename,int,bool)1383 vmsdbgout_write_source_line (unsigned line, const char *filename,
1384                              int /* discriminator */, bool /* is_stmt */)
1385 {
1386   dst_line_info_ref line_info;
1387 
1388   targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
1389                                   line_info_table_in_use);
1390 
1391   /* Expand the line info table if necessary.  */
1392   if (line_info_table_in_use == line_info_table_allocated)
1393     {
1394       line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1395       line_info_table = XRESIZEVEC (dst_line_info_entry, line_info_table,
1396                                     line_info_table_allocated);
1397     }
1398 
1399   /* Add the new entry at the end of the line_info_table.  */
1400   line_info = &line_info_table[line_info_table_in_use++];
1401   line_info->dst_file_num = lookup_filename (filename);
1402   line_info->dst_line_num = line;
1403   if (line > file_info_table[line_info->dst_file_num].max_line)
1404     file_info_table[line_info->dst_file_num].max_line = line;
1405 }
1406 
1407 static void
vmsdbgout_source_line(register unsigned line,unsigned int column,register const char * filename,int discriminator,bool is_stmt)1408 vmsdbgout_source_line (register unsigned line, unsigned int column,
1409 		       register const char *filename,
1410                        int discriminator, bool is_stmt)
1411 {
1412   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1413     (*dwarf2_debug_hooks.source_line) (line, column, filename, discriminator,
1414 				       is_stmt);
1415 
1416   if (debug_info_level >= DINFO_LEVEL_TERSE)
1417     vmsdbgout_write_source_line (line, filename, discriminator, is_stmt);
1418 }
1419 
1420 /* Record the beginning of a new source file, for later output.
1421    At present, unimplemented.  */
1422 
1423 static void
vmsdbgout_start_source_file(unsigned int lineno,const char * filename)1424 vmsdbgout_start_source_file (unsigned int lineno, const char *filename)
1425 {
1426   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1427     (*dwarf2_debug_hooks.start_source_file) (lineno, filename);
1428 }
1429 
1430 /* Record the end of a source file, for later output.
1431    At present, unimplemented.  */
1432 
1433 static void
vmsdbgout_end_source_file(unsigned int lineno ATTRIBUTE_UNUSED)1434 vmsdbgout_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
1435 {
1436   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1437     (*dwarf2_debug_hooks.end_source_file) (lineno);
1438 }
1439 
1440 /* Set up for Debug output at the start of compilation.  */
1441 
1442 static void
vmsdbgout_init(const char * filename)1443 vmsdbgout_init (const char *filename)
1444 {
1445   const char *language_string = lang_hooks.name;
1446 
1447   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1448     (*dwarf2_debug_hooks.init) (filename);
1449 
1450   if (debug_info_level == DINFO_LEVEL_NONE)
1451     return;
1452 
1453   /* Remember the name of the primary input file.  */
1454   primary_filename = filename;
1455 
1456   /* Allocate the initial hunk of the file_info_table.  */
1457   file_info_table = XCNEWVEC (dst_file_info_entry, FILE_TABLE_INCREMENT);
1458   file_info_table_allocated = FILE_TABLE_INCREMENT;
1459   /* Skip the first entry - file numbers begin at 1.  */
1460   file_info_table_in_use = 1;
1461 
1462   funcnam_table.create (FUNC_TABLE_INITIAL);
1463   funcnum_table.create (FUNC_TABLE_INITIAL);
1464 
1465   /* Allocate the initial hunk of the line_info_table.  */
1466   line_info_table = XCNEWVEC (dst_line_info_entry, LINE_INFO_TABLE_INCREMENT);
1467   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
1468   /* zero-th entry is allocated, but unused */
1469   line_info_table_in_use = 1;
1470 
1471   lookup_filename (primary_filename);
1472 
1473   if (lang_GNU_C ())
1474     module_language = DST_K_C;
1475   else if (lang_GNU_CXX ())
1476     module_language = DST_K_CXX;
1477   else if (!strcmp (language_string, "GNU Ada"))
1478     module_language = DST_K_ADA;
1479   else if (!strcmp (language_string, "GNU F77"))
1480     module_language = DST_K_FORTRAN;
1481   else
1482     module_language = DST_K_UNKNOWN;
1483 
1484   module_producer = concat (language_string, " ", version_string, NULL);
1485 
1486   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
1487 
1488 }
1489 
1490 /* Not implemented in VMS Debug.  */
1491 
1492 static void
vmsdbgout_assembly_start(void)1493 vmsdbgout_assembly_start (void)
1494 {
1495   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1496     (*dwarf2_debug_hooks.assembly_start) ();
1497 }
1498 
1499 /* Not implemented in VMS Debug.  */
1500 
1501 static void
vmsdbgout_define(unsigned int lineno,const char * buffer)1502 vmsdbgout_define (unsigned int lineno, const char *buffer)
1503 {
1504   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1505     (*dwarf2_debug_hooks.define) (lineno, buffer);
1506 }
1507 
1508 /* Not implemented in VMS Debug.  */
1509 
1510 static void
vmsdbgout_undef(unsigned int lineno,const char * buffer)1511 vmsdbgout_undef (unsigned int lineno, const char *buffer)
1512 {
1513   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1514     (*dwarf2_debug_hooks.undef) (lineno, buffer);
1515 }
1516 
1517 /* Not implemented in VMS Debug.  */
1518 
1519 static void
vmsdbgout_function_decl(tree decl)1520 vmsdbgout_function_decl (tree decl)
1521 {
1522   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1523     (*dwarf2_debug_hooks.function_decl) (decl);
1524 }
1525 
1526 /* Not implemented in VMS Debug.  */
1527 
1528 static void
vmsdbgout_early_global_decl(tree decl)1529 vmsdbgout_early_global_decl (tree decl)
1530 {
1531   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1532     (*dwarf2_debug_hooks.early_global_decl) (decl);
1533 }
1534 
1535 /* Not implemented in VMS Debug.  */
1536 
1537 static void
vmsdbgout_late_global_decl(tree decl)1538 vmsdbgout_late_global_decl (tree decl)
1539 {
1540   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1541     (*dwarf2_debug_hooks.late_global_decl) (decl);
1542 }
1543 
1544 /* Not implemented in VMS Debug.  */
1545 
1546 static void
vmsdbgout_type_decl(tree decl,int local)1547 vmsdbgout_type_decl (tree decl, int local)
1548 {
1549   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1550     (*dwarf2_debug_hooks.type_decl) (decl, local);
1551 }
1552 
1553 /* Not implemented in VMS Debug.  */
1554 
1555 static void
vmsdbgout_abstract_function(tree decl)1556 vmsdbgout_abstract_function (tree decl)
1557 {
1558   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1559     (*dwarf2_debug_hooks.outlining_inline_function) (decl);
1560 }
1561 
1562 static void
vmsdbgout_early_finish(const char * filename)1563 vmsdbgout_early_finish (const char *filename)
1564 {
1565   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1566     (*dwarf2_debug_hooks.early_finish) (filename);
1567 }
1568 
1569 /* Output stuff that Debug requires at the end of every file and generate the
1570    VMS Debug debugging info.  */
1571 
1572 static void
vmsdbgout_finish(const char * filename ATTRIBUTE_UNUSED)1573 vmsdbgout_finish (const char *filename ATTRIBUTE_UNUSED)
1574 {
1575   unsigned int i, ifunc;
1576   int totsize;
1577 
1578   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1579     (*dwarf2_debug_hooks.finish) (filename);
1580 
1581   if (debug_info_level == DINFO_LEVEL_NONE)
1582     return;
1583 
1584   /* Output a terminator label for the .text section.  */
1585   switch_to_section (text_section);
1586   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
1587 
1588   /* Output debugging information.
1589      Warning! Do not change the name of the .vmsdebug section without
1590      changing it in the assembler also.  */
1591   switch_to_section (get_named_section (NULL, ".vmsdebug", 0));
1592   ASM_OUTPUT_ALIGN (asm_out_file, 0);
1593 
1594   totsize = write_modbeg (1);
1595   FOR_EACH_VEC_ELT (funcnum_table, i, ifunc)
1596     {
1597       totsize += write_rtnbeg (i, 1);
1598       totsize += write_rtnend (i, 1);
1599     }
1600   totsize += write_pclines (1);
1601 
1602   write_modbeg (0);
1603   FOR_EACH_VEC_ELT (funcnum_table, i, ifunc)
1604     {
1605       write_rtnbeg (i, 0);
1606       write_rtnend (i, 0);
1607     }
1608   write_pclines (0);
1609 
1610   if (debug_info_level > DINFO_LEVEL_TERSE)
1611     {
1612       totsize = write_srccorrs (1);
1613       write_srccorrs (0);
1614     }
1615 
1616   totsize = write_modend (1);
1617   write_modend (0);
1618 }
1619 
1620 /* Need for both Dwarf2 on IVMS and VMS Debug on AVMS */
1621 
1622 #ifdef VMS
1623 #define __NEW_STARLET 1
1624 #include <vms/rms.h>
1625 #include <vms/atrdef.h>
1626 #include <vms/fibdef.h>
1627 #include <vms/stsdef.h>
1628 #include <vms/iodef.h>
1629 #include <vms/fatdef.h>
1630 #include <vms/descrip.h>
1631 #include <unixlib.h>
1632 
1633 #define MAXPATH 256
1634 
1635 /* descrip.h doesn't have everything ...  */
1636 typedef struct fibdef* __fibdef_ptr32 __attribute__ (( mode (SI) ));
1637 struct dsc$descriptor_fib
1638 {
1639   unsigned int fib$l_len;
1640   __fibdef_ptr32 fib$l_addr;
1641 };
1642 
1643 /* I/O Status Block.  */
1644 struct IOSB
1645 {
1646   unsigned short status, count;
1647   unsigned int devdep;
1648 };
1649 
1650 static char *tryfile;
1651 
1652 /* Variable length string.  */
1653 struct vstring
1654 {
1655   short length;
1656   char string[NAM$C_MAXRSS+1];
1657 };
1658 
1659 static char filename_buff [MAXPATH];
1660 static char vms_filespec [MAXPATH];
1661 
1662 /* Callback function for filespec style conversion.  */
1663 
1664 static int
translate_unix(char * name,int type ATTRIBUTE_UNUSED)1665 translate_unix (char *name, int type ATTRIBUTE_UNUSED)
1666 {
1667   strncpy (filename_buff, name, MAXPATH);
1668   filename_buff [MAXPATH - 1] = (char) 0;
1669   return 0;
1670 }
1671 
1672 /* Wrapper for DECC function that converts a Unix filespec
1673    to VMS style filespec.  */
1674 
1675 static char *
to_vms_file_spec(char * filespec)1676 to_vms_file_spec (char *filespec)
1677 {
1678   strncpy (vms_filespec, "", MAXPATH);
1679   decc$to_vms (filespec, translate_unix, 1, 1);
1680   strncpy (vms_filespec, filename_buff, MAXPATH);
1681 
1682   vms_filespec [MAXPATH - 1] = (char) 0;
1683 
1684   return vms_filespec;
1685 }
1686 
1687 #else
1688 #define VMS_EPOCH_OFFSET 35067168000000000LL
1689 #define VMS_GRANULARITY_FACTOR 10000000
1690 #endif
1691 
1692 /* Return VMS file date, size, format, version given a name.  */
1693 
1694 int
vms_file_stats_name(const char * filename,long long * cdt,long * siz,char * rfo,int * ver)1695 vms_file_stats_name (const char *filename, long long *cdt, long *siz, char *rfo,
1696 		     int *ver)
1697 {
1698 #ifdef VMS
1699   struct FAB fab;
1700   struct NAM nam;
1701 
1702   unsigned long long create;
1703   FAT recattr;
1704   char ascnamebuff [256];
1705 
1706   ATRDEF atrlst[]
1707     = {
1708       { ATR$S_CREDATE,  ATR$C_CREDATE,  &create },
1709       { ATR$S_RECATTR,  ATR$C_RECATTR,  &recattr },
1710       { ATR$S_ASCNAME,  ATR$C_ASCNAME,  &ascnamebuff },
1711       { 0, 0, 0}
1712     };
1713 
1714   FIBDEF fib;
1715   struct dsc$descriptor_fib fibdsc = {sizeof (fib), (void *) &fib};
1716 
1717   struct IOSB iosb;
1718 
1719   long status;
1720   unsigned short chan;
1721 
1722   struct vstring file;
1723   struct dsc$descriptor_s filedsc
1724     = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) file.string};
1725   struct vstring device;
1726   struct dsc$descriptor_s devicedsc
1727     = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) device.string};
1728   struct vstring result;
1729   struct dsc$descriptor_s resultdsc
1730     = {NAM$C_MAXRSS, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, (void *) result.string};
1731 
1732   if (strcmp (filename, "<internal>") == 0
1733       || strcmp (filename, "<built-in>") == 0)
1734     {
1735       if (cdt)
1736 	*cdt = 0;
1737 
1738       if (siz)
1739 	*siz = 0;
1740 
1741       if (rfo)
1742 	*rfo = 0;
1743 
1744       if (ver)
1745         *ver = 0;
1746 
1747       return 0;
1748     }
1749 
1750   tryfile = to_vms_file_spec (filename);
1751 
1752   /* Allocate and initialize a FAB and NAM structures.  */
1753   fab = cc$rms_fab;
1754   nam = cc$rms_nam;
1755 
1756   nam.nam$l_esa = file.string;
1757   nam.nam$b_ess = NAM$C_MAXRSS;
1758   nam.nam$l_rsa = result.string;
1759   nam.nam$b_rss = NAM$C_MAXRSS;
1760   fab.fab$l_fna = tryfile;
1761   fab.fab$b_fns = strlen (tryfile);
1762   fab.fab$l_nam = &nam;
1763 
1764   /* Validate filespec syntax and device existence.  */
1765   status = SYS$PARSE (&fab, 0, 0);
1766   if ((status & 1) != 1)
1767     return 1;
1768 
1769   file.string[nam.nam$b_esl] = 0;
1770 
1771   /* Find matching filespec.  */
1772   status = SYS$SEARCH (&fab, 0, 0);
1773   if ((status & 1) != 1)
1774     return 1;
1775 
1776   file.string[nam.nam$b_esl] = 0;
1777   result.string[result.length=nam.nam$b_rsl] = 0;
1778 
1779   /* Get the device name and assign an IO channel.  */
1780   strncpy (device.string, nam.nam$l_dev, nam.nam$b_dev);
1781   devicedsc.dsc$w_length  = nam.nam$b_dev;
1782   chan = 0;
1783   status = SYS$ASSIGN (&devicedsc, &chan, 0, 0, 0);
1784   if ((status & 1) != 1)
1785     return 1;
1786 
1787   /* Initialize the FIB and fill in the directory id field.  */
1788   memset (&fib, 0, sizeof (fib));
1789   fib.fib$w_did[0]  = nam.nam$w_did[0];
1790   fib.fib$w_did[1]  = nam.nam$w_did[1];
1791   fib.fib$w_did[2]  = nam.nam$w_did[2];
1792   fib.fib$l_acctl = 0;
1793   fib.fib$l_wcc = 0;
1794   strcpy (file.string, (strrchr (result.string, ']') + 1));
1795   filedsc.dsc$w_length = strlen (file.string);
1796   result.string[result.length = 0] = 0;
1797 
1798   /* Open and close the file to fill in the attributes.  */
1799   status
1800     = SYS$QIOW (0, chan, IO$_ACCESS|IO$M_ACCESS, &iosb, 0, 0,
1801 		&fibdsc, &filedsc, &result.length, &resultdsc, &atrlst, 0);
1802   if ((status & 1) != 1)
1803     return 1;
1804   if ((iosb.status & 1) != 1)
1805     return 1;
1806 
1807   result.string[result.length] = 0;
1808   status = SYS$QIOW (0, chan, IO$_DEACCESS, &iosb, 0, 0, &fibdsc, 0, 0, 0,
1809 		     &atrlst, 0);
1810   if ((status & 1) != 1)
1811     return 1;
1812   if ((iosb.status & 1) != 1)
1813     return 1;
1814 
1815   /* Deassign the channel and exit.  */
1816   status = SYS$DASSGN (chan);
1817   if ((status & 1) != 1)
1818     return 1;
1819 
1820   if (cdt) *cdt = create;
1821   if (siz) *siz = (512 * 65536 * recattr.fat$w_efblkh) +
1822                   (512 * (recattr.fat$w_efblkl - 1)) +
1823                   recattr.fat$w_ffbyte;
1824   if (rfo) *rfo = recattr.fat$v_rtype;
1825   if (ver) *ver = strtol (strrchr (ascnamebuff, ';')+1, 0, 10);
1826 
1827   return 0;
1828 #else
1829   struct stat buff;
1830 
1831   if ((stat (filename, &buff)) != 0)
1832      return 1;
1833 
1834   if (cdt)
1835     *cdt = (long long) (buff.st_mtime * VMS_GRANULARITY_FACTOR)
1836                         + VMS_EPOCH_OFFSET;
1837 
1838   if (siz)
1839     *siz = buff.st_size;
1840 
1841   if (rfo)
1842     *rfo = 2; /* Stream LF format */
1843 
1844   if (ver)
1845     *ver = 1;
1846 
1847   return 0;
1848 #endif
1849 }
1850 #endif
1851