Debian Codegraph @

gcc/cobol/gcobolspec.cc

raw
1 /* Specific flags and argument handling of the Cobol front-end.
2 Copyright (C) 2021-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This file implements gcobol's language-specific option handling for the COBOL front
21 end. It is based on a similar file for the Fortran front end, which
22 itself was derived from the C front end. Specifically, it defines
23
24 lang_specific_driver(cl_decoded_option**, unsigned int*, int*)
25
26 for gcobol.
27
28 For GNU COBOL, we do the following to the argument list
29 before passing it to `gcc':
30
31 1. Make sure `-lgcobol -lm' is at the end of the list.
32
33 2. Make sure each time `-lgcobol' or `-lm' is seen, it forms
34 part of the series `-lgcobol -lm'.
35
36 #1 and #2 are not done if `-nostdlib' or any option that disables
37 the linking phase is present, or if `-xfoo' is in effect. Note that
38 a lack of source files or -l options disables linking.
39
40 The way this file builds the new argument list was rewritten to be easier to
41 maintain, and improve the way it decides to add or not add extra arguments,
42 etc. Several improvements were made in the handling of arguments, primarily
43 to make it more consistent with `gcc' itself. */
44
45 /*
46 * Number of extra output files that lang_specific_pre_link may generate.
47 * Unused.
48 */
49
50 #include "cobol-system.h"
51 #include "coretypes.h"
52 #include "opt-suggestions.h"
53 #include "gcc.h"
54 #include "opts.h"
55 #include "tm.h"
56 #include "intl.h"
57
58 int lang_specific_extra_outfiles = 0;
59
60 #ifndef DL_LIBRARY
61 #define DL_LIBRARY "dl"
62 #endif
63
64 #ifndef STDCPP_LIBRARY
65 #define STDCPP_LIBRARY "stdc++"
66 #endif
67
68 #ifndef COBOL_LIBRARY
69 #define COBOL_LIBRARY "gcobol"
70 #endif
71
72 #define SPEC_FILE "libgcobol.spec"
73
74 /* The original argument list and related info is copied here. */
75 static const struct cl_decoded_option *original_options;
76
77 /* The new argument list will be built here. */
78 static std::vector<cl_decoded_option>new_opt;
79
80 static bool need_libgcobol = true;
81
82 // #define NOISY 1
83
84 static void
85 append_arg(const cl_decoded_option& arg)
86 {
87 #ifdef NOISY
88 static int counter = 1;
89 fprintf( stderr,
90 ">>>>>> #%2d Appending %4ld %s\n",
91 counter++,
92 arg.opt_index,
93 arg.orig_option_with_args_text);
94 #endif
95
96 new_opt.push_back(arg);
97 }
98
99 static void
100 append_option (size_t opt_index, const char *arg, int value)
101 {
102 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
103 being built. */
104 struct cl_decoded_option decoded;
105 generate_option(opt_index, arg, value, CL_DRIVER, &decoded);
106 append_arg(decoded);
107 }
108
109 static void
110 add_arg_lib(const char *library, bool force_static ATTRIBUTE_UNUSED)
111 {
112 /* Append a libgcobol argument to the list being built. If
113 FORCE_STATIC, ensure the library is linked statically. */
114 #ifdef HAVE_LD_STATIC_DYNAMIC
115 if( force_static )
116 {
117 append_option (OPT_Wl_, LD_STATIC_OPTION, 1);
118 }
119 #endif
120 append_option (OPT_l, library, 1);
121 #ifdef HAVE_LD_STATIC_DYNAMIC
122 if( force_static )
123 {
124 append_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1);
125 }
126 #endif
127 }
128
129 void
130 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
131 unsigned int *in_decoded_options_count,
132 int *in_added_libraries ATTRIBUTE_UNUSED)
133 {
134 int argc = (int)*in_decoded_options_count;
135 struct cl_decoded_option *decoded_options = *in_decoded_options;
136
137 // This is the language in effect; it is changed by the OPT_x option.
138 // Start it out with the default of "none", which is the same as "cobol".
139 const char *language = "none";
140
141 /* The number of input and output files in the incoming arg list. */
142 int n_infiles = 0;
143 int n_outfiles = 0;
144
145 // The number of input files when the language is "none" or "cobol"
146 int n_cobol_files = 0;
147
148 // saw_OPT_no_main means "don't expect -main"
149 bool saw_OPT_no_main = false;
150
151 // The number of incoming OPT_main and OPT_main_ options seen
152 int n_mains = 0;
153
154 bool saw_OPT_c = false;
155 bool saw_OPT_shared = false;
156
157 bool verbose = false;
158
159 // These flags indicate whether we need various libraries
160
161 bool need_libdl = (DL_LIBRARY[0] != '\0');
162 bool need_libstdc = (STDCPP_LIBRARY[0] != '\0');
163
164 // Separate flags for a couple of static libraries
165 bool static_libgcobol = false;
166 bool static_in_general = false;
167
168 /* WEIRDNESS ALERT:
169
170 Sometime around August of 2024, changes were made to the GCC source code
171 that resulted in an "memory released twice" run-time error when a
172 std::unordered_map was destructed twice, which usually can't happen. But
173 it was happening in a gcobol-generated executable. Investigation revealed
174 that
175
176 gocobol ... libgcobol.a -lgcobol
177
178 resulted in __gg__alphabet_states being destructed twice.
179
180 This should not happen! In normal -shared code, including both libxxx.a
181 and -lxxx is perfectly legitimate and causes no problem, because the first
182 one to be encountered provides the globals. But something about the
183 extremely complex makefile for libgcobol was resulting in the double
184 destructor problem.
185
186 A couple of days of looking for a fix were unsuccessful.
187
188 So, I have added logic to this module to prevent the otherwise automatic
189 insertion of "-lgcobol" when there is an explicit "libgcobol.a" in the
190 parameters.
191
192 */
193
194 int index_libgcobol_a = 0;
195
196 bool no_files_error = true;
197
198 #ifdef NOISY
199 int counter=1;
200 for(int i = 0; i < argc; i++)
201 {
202 fprintf( stderr,
203 ">>>>>> #%2d Incoming: %4ld %s\n",
204 counter++,
205 decoded_options[i].opt_index,
206 decoded_options[i].orig_option_with_args_text);
207 }
208 fprintf (stderr, "\n");
209 #endif
210
211 // There is always the possibility that no changes to the options
212 // will be needed:
213
214 /* First pass through arglist.
215
216 If -nostdlib or a "turn-off-linking" option is anywhere in the
217 command line, don't do any library-option processing (except
218 relating to -x). */
219
220 for(int i = 1; i < argc; ++i)
221 {
222 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
223 {
224 continue;
225 }
226
227 if( strcmp( decoded_options[i].orig_option_with_args_text, "-###") == 0 )
228 {
229 no_files_error = false;
230 }
231
232 switch(decoded_options[i].opt_index)
233 {
234 case OPT_SPECIAL_input_file:
235 no_files_error = false;
236 n_infiles += 1;
237 if( strcmp(language, "none") == 0
238 || strcmp(language, "cobol") == 0 )
239 {
240 n_cobol_files += 1;
241 }
242 if( strstr(decoded_options[i].orig_option_with_args_text, "libgcobol.a") )
243 {
244 // We have been given an explicit libgcobol.a. We need to note that.
245 index_libgcobol_a = i;
246 }
247 continue;
248
249 case OPT_shared:
250 saw_OPT_shared = true;
251 break;
252
253 case OPT_c:
254 // Note -c specially.
255 saw_OPT_c = true;
256 // FALLTHROUGH
257 case OPT_nostdlib:
258 case OPT_nodefaultlibs:
259 case OPT_r:
260 case OPT_S:
261 case OPT_fsyntax_only:
262 case OPT_E:
263 // With these options, no libraries need be loaded
264 need_libgcobol = false;
265 need_libdl = false;
266 need_libstdc = false;
267 break;
268
269 case OPT_static_libgcobol:
270 static_libgcobol = true;
271 need_libgcobol = true;
272 break;
273
274 case OPT_l:
275 n_infiles += 1;
276 if(strcmp(decoded_options[i].arg, DL_LIBRARY) == 0)
277 {
278 need_libdl = false;
279 }
280 else if(strcmp(decoded_options[i].arg, COBOL_LIBRARY) == 0)
281 {
282 need_libgcobol = false;
283 }
284 else if(strcmp(decoded_options[i].arg, STDCPP_LIBRARY) == 0)
285 {
286 need_libstdc = false;
287 }
288 break;
289
290 case OPT_o:
291 n_outfiles += 1;
292 break;
293
294 case OPT_nomain:
295 saw_OPT_no_main = true;
296 break;
297
298 case OPT_main:
299 case OPT_main_:
300 n_mains += 1;
301 break;
302
303 case OPT_print_search_dirs:
304 case OPT_print_file_name_:
305 case OPT_print_prog_name_:
306 case OPT_print_multi_lib:
307 case OPT_print_multi_directory:
308 case OPT_print_sysroot:
309 case OPT_print_multi_os_directory:
310 case OPT_print_multiarch:
311 case OPT_print_sysroot_headers_suffix:
312 no_files_error = false;
313 break;
314
315 case OPT_v:
316 no_files_error = false;
317 verbose = true;
318 break;
319
320 case OPT_x:
321 language = decoded_options[i].arg;
322 break;
323
324 case OPT__version:
325 no_files_error = false;
326 break;
327
328 case OPT__help:
329 /*
330 * $ man ./gcobol.1 | ./help.gen
331 */
332 puts( "Options specific to gcobol: " );
333 puts(
334 " -main option uses the first PROGRAM of filename as the entry point for\n"
335 " the main() procedure. \n"
336 " -no_main \n"
337 " means that there is no -main, and the main() entry point is\n"
338 " provided by some other compilation or .o file\n"
339 " -findicator-column\n"
340 " describes the location of the Indicator Area in a COBOL file with\n"
341 " standard 80-column lines. \n"
342 " -ffixed-form\n"
343 " Use strict Reference Format in reading the COBOL input: 72-char‐\n"
344 " acter lines, with a 6-character sequence area, and an indicator\n"
345 " column. \n"
346 " -ffree-form\n"
347 " Force the COBOL input to be interpreted as free format. \n"
348 " -fmax-errors nerror\n"
349 " nerror represents the number of error messages produced. \n"
350 " -fflex-debug, -fyacc-debug\n"
351 " produce messages useful for compiler development. \n" );
352
353
354 /* Let gcc.cc handle this, as it has a really
355 cool facility for handling --help and --verbose --help. */
356 return;
357
358 default:
359 break;
360 }
361 }
362
363 if( saw_OPT_no_main && n_mains )
364 {
365 char ach[] = "\"-no-main\" and \"-main\" are incompatible";
366 fatal_error(input_location,"%s", ach);
367 }
368
369 bool suppress_main = saw_OPT_no_main
370 || (saw_OPT_c && n_mains==0)
371 || saw_OPT_shared;
372
373 if( no_files_error || ((n_outfiles != 0) && (n_infiles == 0)) )
374 {
375 fatal_error(input_location, "no input files");
376 }
377
378 /* If there are no input files, there is no need for any libraries. */
379 if( n_infiles == 0 )
380 {
381 need_libgcobol = false;
382 need_libdl = false;
383 need_libstdc = false;
384 }
385
386 /* Second pass through arglist, transforming arguments as appropriate. */
387
388 append_arg(decoded_options[0]); /* Start with command name, of course. */
389
390 bool first_COBOL_file = true;
391 bool prior_main = false;
392 const char *entry_point = NULL;
393
394 // Reset the current language, in case it was changed during the first pass
395 language = "none";
396
397 for(int i = 1; i < argc; ++i)
398 {
399 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
400 {
401 append_arg(decoded_options[i]);
402 continue;
403 }
404
405 switch (decoded_options[i].opt_index)
406 {
407 case OPT_SPECIAL_input_file:
408 if( strcmp(language, "none") == 0
409 || strcmp(language, "cobol") == 0 )
410 {
411 // This is a COBOL source code file
412 if( !suppress_main && n_mains==0 && first_COBOL_file )
413 {
414 // This is a case where the -c option is not present, and there
415 // were no -main switches. So, we are going to insert a -main switch
416 // in front of this, the first COBOL file
417 first_COBOL_file = false;
418 prior_main = true;
419 }
420
421 if( prior_main )
422 {
423 const char *ach;
424 if (entry_point)
425 ach = entry_point;
426 else
427 ach = decoded_options[i].arg;
428 append_option(OPT_main_, ach, 1);
429 prior_main = false;
430 entry_point = NULL;
431 }
432 }
433 append_arg(decoded_options[i]);
434 break;
435
436 case OPT_main:
437 if( prior_main )
438 {
439 char ach[] = "Multiple \"-main\" without a source file";
440 fatal_error(input_location, "%s", ach);
441 }
442 // This is a simple -main that needs to be followed by a COBOL file
443 prior_main = true;
444 break;
445
446 case OPT_main_: // Note the trailing underscore
447 if( prior_main )
448 {
449 char ach[] = "Multiple \"-main\" without a source file";
450 fatal_error(input_location, "%s", ach);
451 }
452 // This is -main=<arg> that needs to be followed by a COBOL file
453 entry_point = decoded_options[i].arg;
454 prior_main = true;
455 break;
456
457 case OPT_nomain:
458 append_arg(decoded_options[i]);
459 break;
460
461 case OPT_x:
462 language = decoded_options[i].arg;
463 append_arg(decoded_options[i]);
464 break;
465
466 case OPT_static_libgcobol:
467 #if !defined (HAVE_LD_STATIC_DYNAMIC)
468 // Allow the target to use spec substitution.
469 append_arg(decoded_options[i]);
470 #endif
471 // Else don't pass this one on to cobol1
472 break;
473
474 ////#ifdef __x86_64__
475 //// case OPT_m32:
476 //// error ( "unrecognized command-line option %<-%s%>; "
477 //// "(32-bit executables cannot be generated)", "m32");
478 //// break;
479 ////#endif
480 case OPT_static:
481 #if defined (HAVE_LD_STATIC_DYNAMIC)
482 append_arg(decoded_options[i]);
483 static_in_general = true;
484 #endif
485 break;
486
487 default:
488 append_arg(decoded_options[i]);
489 break;
490 }
491 }
492
493 /* As described above, we have empirically noticed that when the command line
494 explicitly specifies libgcobol.a as an input, a following -lgcobol causes
495 the "on exit" functions of the library to be executed twice. This can
496 cause trouble for c++ class destructors that expect to be run only once.
497
498 So, we rather hamhandedly prevent the inclusion of the default -lgcobol
499 parameter when a libgcobol.a was found to be present.
500
501 Note that if the user *explicitly* specifies both libgcobol.a and
502 -lgocobol, then he gets what he asked for, and the problem then belongs to
503 them.
504
505 */
506
507 if( index_libgcobol_a )
508 {
509 need_libgcobol = false;
510 }
511
512 if( static_in_general )
513 {
514 // These two options interfere with each other.
515 static_libgcobol = false;
516 }
517
518 if( need_libgcobol )
519 {
520 add_arg_lib(COBOL_LIBRARY, static_libgcobol);
521 }
522 if( need_libdl )
523 {
524 add_arg_lib(DL_LIBRARY, false);
525 }
526 if( need_libstdc )
527 {
528 add_arg_lib(STDCPP_LIBRARY, false);
529 }
530
531 if( prior_main )
532 {
533 char ach[] = "\"-main\" without a source file";
534 fatal_error(input_location, "%s", ach);
535 }
536
537 // We now take the new_opt vector, and turn it into an array of
538 // cl_decoded_option
539
540 size_t new_option_count = new_opt.size();
541 struct cl_decoded_option *new_options = XNEWVEC (struct cl_decoded_option,
542 new_option_count);
543
544 for(size_t i=0; i<new_option_count; i++)
545 {
546 new_options[i] = new_opt[i];
547 }
548
549 #ifdef NOISY
550 verbose = true;
551 #endif
552 if( verbose && new_options != original_options ) // cppcheck-suppress knownConditionTrueFalse
553 {
554 fprintf(stderr, _("Driving: (" HOST_SIZE_T_PRINT_DEC ")\n"),
555 (fmt_size_t)new_option_count);
556 for(size_t i=0; i<new_option_count; i++)
557 {
558 fprintf(stderr,
559 " [%2" GCC_PRISZ "d] %4" GCC_PRISZ "d %s\n",
560 (fmt_size_t)i,
561 (fmt_size_t)new_options[i].opt_index,
562 new_options[i].orig_option_with_args_text);
563 }
564 fprintf (stderr, "\n");
565 }
566
567 *in_decoded_options_count = new_option_count;
568 *in_decoded_options = new_options;
569 }
570
571 /* Called before linking. Returns 0 on success and -1 on failure. */
572 int
573 lang_specific_pre_link (void)
574 {
575 if (need_libgcobol)
576 do_spec ("%:include(libgcobol.spec)");
577
578 return 0;
579 }