cprover
gcc_mode.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: GCC Mode
4 
5 Author: CM Wintersteiger, 2006
6 
7 \*******************************************************************/
8 
11 
12 #include "gcc_mode.h"
13 
14 #ifdef _WIN32
15 #define EX_OK 0
16 #define EX_USAGE 64
17 #define EX_SOFTWARE 70
18 #else
19 #include <sysexits.h>
20 #endif
21 
22 #include <algorithm>
23 #include <cstddef>
24 #include <cstdio>
25 #include <iostream>
26 #include <iterator>
27 #include <fstream>
28 #include <cstring>
29 #include <numeric>
30 #include <sstream>
31 
32 #include <json/json_parser.h>
33 
34 #include <util/arith_tools.h>
35 #include <util/c_types.h>
36 #include <util/config.h>
37 #include <util/expr.h>
38 #include <util/get_base_name.h>
39 #include <util/invariant.h>
40 #include <util/prefix.h>
41 #include <util/replace_symbol.h>
42 #include <util/run.h>
43 #include <util/suffix.h>
44 #include <util/tempdir.h>
45 #include <util/tempfile.h>
46 #include <util/version.h>
47 
49 
50 #include "hybrid_binary.h"
51 #include "linker_script_merge.h"
52 
53 static std::string compiler_name(
54  const cmdlinet &cmdline,
55  const std::string &base_name)
56 {
57  if(cmdline.isset("native-compiler"))
58  return cmdline.get_value("native-compiler");
59 
60  if(base_name=="bcc" ||
61  base_name.find("goto-bcc")!=std::string::npos)
62  return "bcc";
63 
64  if(base_name=="goto-clang")
65  return "clang";
66 
67  std::string::size_type pos=base_name.find("goto-gcc");
68 
69  if(pos==std::string::npos ||
70  base_name=="goto-gcc" ||
71  base_name=="goto-ld")
72  {
73  #ifdef __FreeBSD__
74  return "clang";
75  #else
76  return "gcc";
77  #endif
78  }
79 
80  std::string result=base_name;
81  result.replace(pos, 8, "gcc");
82 
83  return result;
84 }
85 
86 static std::string linker_name(
87  const cmdlinet &cmdline,
88  const std::string &base_name)
89 {
90  if(cmdline.isset("native-linker"))
91  return cmdline.get_value("native-linker");
92 
93  std::string::size_type pos=base_name.find("goto-ld");
94 
95  if(pos==std::string::npos ||
96  base_name=="goto-gcc" ||
97  base_name=="goto-ld")
98  return "ld";
99 
100  std::string result=base_name;
101  result.replace(pos, 7, "ld");
102 
103  return result;
104 }
105 
107  goto_cc_cmdlinet &_cmdline,
108  const std::string &_base_name,
109  bool _produce_hybrid_binary):
110  goto_cc_modet(_cmdline, _base_name, gcc_message_handler),
111  produce_hybrid_binary(_produce_hybrid_binary),
112  goto_binary_tmp_suffix(".goto-cc-saved"),
113 
114  // Keys are architectures specified in configt::set_arch().
115  // Values are lists of GCC architectures that can be supplied as
116  // arguments to the -march, -mcpu, and -mtune flags (see the GCC
117  // manual https://gcc.gnu.org/onlinedocs/).
118  arch_map(
119  {
120  // ARM information taken from the following:
121  //
122  // the "ARM core timeline" table on this page:
123  // https://en.wikipedia.org/wiki/List_of_ARM_microarchitectures
124  //
125  // articles on particular core groups, e.g.
126  // https://en.wikipedia.org/wiki/ARM9
127  //
128  // The "Cores" table on this page:
129  // https://en.wikipedia.org/wiki/ARM_architecture
130  // NOLINTNEXTLINE(whitespace/braces)
131  {"arm", {
132  "strongarm", "strongarm110", "strongarm1100", "strongarm1110",
133  "arm2", "arm250", "arm3", "arm6", "arm60", "arm600", "arm610",
134  "arm620", "fa526", "fa626", "fa606te", "fa626te", "fmp626",
135  "xscale", "iwmmxt", "iwmmxt2", "xgene1"
136  }}, // NOLINTNEXTLINE(whitespace/braces)
137  {"armhf", {
138  "armv7", "arm7m", "arm7d", "arm7dm", "arm7di", "arm7dmi",
139  "arm70", "arm700", "arm700i", "arm710", "arm710c", "arm7100",
140  "arm720", "arm7500", "arm7500fe", "arm7tdmi", "arm7tdmi-s",
141  "arm710t", "arm720t", "arm740t", "mpcore", "mpcorenovfp",
142  "arm8", "arm810", "arm9", "arm9e", "arm920", "arm920t",
143  "arm922t", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s",
144  "arm940t", "arm9tdmi", "arm10tdmi", "arm1020t", "arm1026ej-s",
145  "arm10e", "arm1020e", "arm1022e", "arm1136j-s", "arm1136jf-s",
146  "arm1156t2-s", "arm1156t2f-s", "arm1176jz-s", "arm1176jzf-s",
147  "cortex-a5", "cortex-a7", "cortex-a8", "cortex-a9",
148  "cortex-a12", "cortex-a15", "cortex-a53", "cortex-r4",
149  "cortex-r4f", "cortex-r5", "cortex-r7", "cortex-m7",
150  "cortex-m4", "cortex-m3", "cortex-m1", "cortex-m0",
151  "cortex-m0plus", "cortex-m1.small-multiply",
152  "cortex-m0.small-multiply", "cortex-m0plus.small-multiply",
153  "marvell-pj4", "ep9312", "fa726te",
154  }}, // NOLINTNEXTLINE(whitespace/braces)
155  {"arm64", {
156  "cortex-a57", "cortex-a72", "exynos-m1"
157  }}, // NOLINTNEXTLINE(whitespace/braces)
158  {"hppa", {"1.0", "1.1", "2.0"}}, // NOLINTNEXTLINE(whitespace/braces)
159  // PowerPC
160  // https://en.wikipedia.org/wiki/List_of_PowerPC_processors
161  // NOLINTNEXTLINE(whitespace/braces)
162  {"powerpc", {
163  "powerpc", "601", "602", "603", "603e", "604", "604e", "630",
164  // PowerPC G3 == 7xx series
165  "G3", "740", "750",
166  // PowerPC G4 == 74xx series
167  "G4", "7400", "7450",
168  // SoC and low power: https://en.wikipedia.org/wiki/PowerPC_400
169  "401", "403", "405", "405fp", "440", "440fp", "464", "464fp",
170  "476", "476fp",
171  // e series. x00 are 32-bit, x50 are 64-bit. See e.g.
172  // https://en.wikipedia.org/wiki/PowerPC_e6500
173  "e300c2", "e300c3", "e500mc", "ec603e",
174  // https://en.wikipedia.org/wiki/Titan_(microprocessor)
175  "titan",
176  }},
177  // NOLINTNEXTLINE(whitespace/braces)
178  {"powerpc64", {
179  "powerpc64",
180  // First IBM 64-bit processor
181  "620",
182  "970", "G5"
183  // All IBM POWER processors are 64 bit, but POWER 8 is
184  // little-endian so not in this list.
185  // https://en.wikipedia.org/wiki/Ppc64
186  "power3", "power4", "power5", "power5+", "power6", "power6x",
187  "power7", "rs64",
188  // e series SoC chips. x00 are 32-bit, x50 are 64-bit. See e.g.
189  // https://en.wikipedia.org/wiki/PowerPC_e6500
190  "e500mc64", "e5500", "e6500",
191  // https://en.wikipedia.org/wiki/IBM_A2
192  "a2",
193  }},
194  // The latest Power processors are little endian.
195  {"powerpc64le", {"powerpc64le", "power8"}},
196  // There are two MIPS architecture series. The 'old' one comprises
197  // MIPS I - MIPS V (where MIPS I and MIPS II are 32-bit
198  // architectures, and the III, IV and V are 64-bit). The 'new'
199  // architecture series comprises MIPS32 and MIPS64. Source: [0].
200  //
201  // CPROVER's names for these are in configt::this_architecture(),
202  // in particular note the preprocessor variable names. MIPS
203  // processors can run in little-endian or big-endian mode; [1]
204  // gives more information on particular processors. Particular
205  // processors and their architectures are at [2]. This means that
206  // we cannot use the processor flags alone to decide which CPROVER
207  // name to use; we also need to check for the -EB or -EL flags to
208  // decide whether little- or big-endian code should be generated.
209  // Therefore, the keys in this part of the map don't directly map
210  // to CPROVER architectures.
211  //
212  // [0] https://en.wikipedia.org/wiki/MIPS_architecture
213  // [1] https://www.debian.org/ports/mips/
214  // [2] https://en.wikipedia.org/wiki/List_of_MIPS_architecture_processors
215  // NOLINTNEXTLINE(whitespace/braces)
216  {"mips64n", {
217  "loongson2e", "loongson2f", "loongson3a", "mips64", "mips64r2",
218  "mips64r3", "mips64r5", "mips64r6 4kc", "5kc", "5kf", "20kc",
219  "octeon", "octeon+", "octeon2", "octeon3", "sb1", "vr4100",
220  "vr4111", "vr4120", "vr4130", "vr4300", "vr5000", "vr5400",
221  "vr5500", "sr71000", "xlp",
222  }}, // NOLINTNEXTLINE(whitespace/braces)
223  {"mips32n", {
224  "mips32", "mips32r2", "mips32r3", "mips32r5", "mips32r6",
225  // https://www.imgtec.com/mips/classic/
226  "4km", "4kp", "4ksc", "4kec", "4kem", "4kep", "4ksd", "24kc",
227  "24kf2_1", "24kf1_1", "24kec", "24kef2_1", "24kef1_1", "34kc",
228  "34kf2_1", "34kf1_1", "34kn", "74kc", "74kf2_1", "74kf1_1",
229  "74kf3_2", "1004kc", "1004kf2_1", "1004kf1_1", "m4k", "m14k",
230  "m14kc", "m14ke", "m14kec",
231  // https://en.wikipedia.org/wiki/List_of_MIPS_architecture_processors
232  "p5600", "xlr",
233  }}, // NOLINTNEXTLINE(whitespace/braces)
234  {"mips32o", {
235  "mips1", "mips2", "r2000", "r3000",
236  "r6000", // Not a mistake, r4000 came out _after_ this
237  }}, // NOLINTNEXTLINE(whitespace/braces)
238  {"mips64o", {
239  "mips3", "mips4", "r4000", "r4400", "r4600", "r4650", "r4700",
240  "r8000", "rm7000", "rm9000", "r10000", "r12000", "r14000",
241  "r16000",
242  }},
243  // These are IBM mainframes. s390 is 32-bit; s390x is 64-bit [0].
244  // Search that page for s390x and note that 32-bit refers to
245  // everything "prior to 2000's z900 model". The list of model
246  // numbers is at [1].
247  // [0] https://en.wikipedia.org/wiki/Linux_on_z_Systems
248  // [1] https://en.wikipedia.org/wiki/IBM_System_z
249  // NOLINTNEXTLINE(whitespace/braces)
250  {"s390", {
251  "z900", "z990", "g5", "g6",
252  }}, // NOLINTNEXTLINE(whitespace/braces)
253  {"s390x", {
254  "z9-109", "z9-ec", "z10", "z196", "zEC12", "z13"
255  }},
256  // SPARC
257  // In the "Implementations" table of [0], everything with an arch
258  // version up to V8 is 32-bit. V9 and onward are 64-bit.
259  // [0] https://en.wikipedia.org/wiki/SPARC
260  // NOLINTNEXTLINE(whitespace/braces)
261  {"sparc", {
262  "v7", "v8", "leon", "leon3", "leon3v7", "cypress", "supersparc",
263  "hypersparc", "sparclite", "f930", "f934", "sparclite86x",
264  "tsc701",
265  }}, // NOLINTNEXTLINE(whitespace/braces)
266  {"sparc64", {
267  "v9", "ultrasparc", "ultrasparc3", "niagara", "niagara2",
268  "niagara3", "niagara4",
269  }}, // NOLINTNEXTLINE(whitespace/braces)
270  {"ia64", {
271  "itanium", "itanium1", "merced", "itanium2", "mckinley"
272  }}, // NOLINTNEXTLINE(whitespace/braces)
273  // x86 and x86_64. See
274  // https://en.wikipedia.org/wiki/List_of_AMD_microprocessors
275  // https://en.wikipedia.org/wiki/List_of_Intel_microprocessors
276  {"i386", {
277  // Intel generic
278  "i386", "i486", "i586", "i686",
279  // AMD
280  "k6", "k6-2", "k6-3", "athlon" "athlon-tbird", "athlon-4",
281  "athlon-xp", "athlon-mp",
282  // Everything called "pentium" by GCC is 32 bits; the only 64-bit
283  // Pentium flag recognized by GCC is "nocona".
284  "pentium", "pentium-mmx", "pentiumpro" "pentium2", "pentium3",
285  "pentium3m", "pentium-m" "pentium4", "pentium4m", "prescott",
286  // Misc
287  "winchip-c6", "winchip2", "c3", "c3-2", "geode",
288  }}, // NOLINTNEXTLINE(whitespace/braces)
289  {"x86_64", {
290  // Intel
291  "nocona", "core2", "nehalem" "westmere", "sandybridge", "knl",
292  "ivybridge", "haswell", "broadwell" "bonnell", "silvermont",
293  // AMD generic
294  "k8", "k8-sse3", "opteron", "athlon64", "athlon-fx",
295  "opteron-sse3" "athlon64-sse3", "amdfam10", "barcelona",
296  // AMD "bulldozer" (high power, family 15h)
297  "bdver1", "bdver2" "bdver3", "bdver4",
298  // AMD "bobcat" (low power, family 14h)
299  "btver1", "btver2",
300  }},
301  })
302 {
303 }
304 
305 bool gcc_modet::needs_preprocessing(const std::string &file)
306 {
307  if(has_suffix(file, ".c") ||
308  has_suffix(file, ".cc") ||
309  has_suffix(file, ".cp") ||
310  has_suffix(file, ".cpp") ||
311  has_suffix(file, ".CPP") ||
312  has_suffix(file, ".c++") ||
313  has_suffix(file, ".C"))
314  return true;
315  else
316  return false;
317 }
318 
321 {
322  if(cmdline.isset('?') ||
323  cmdline.isset("help"))
324  {
325  help();
326  return EX_OK;
327  }
328 
331 
332  auto default_verbosity = (cmdline.isset("Wall") || cmdline.isset("Wextra")) ?
335  cmdline.get_value("verbosity"), default_verbosity, gcc_message_handler);
336 
337  bool act_as_bcc=
338  base_name=="bcc" ||
339  base_name.find("goto-bcc")!=std::string::npos;
340 
341  // if we are gcc or bcc, then get the version number
343 
344  if((cmdline.isset('v') && cmdline.have_infile_arg()) ||
345  (cmdline.isset("version") && !produce_hybrid_binary))
346  {
347  // "-v" a) prints the version and b) increases verbosity.
348  // Compilation continues, don't exit!
349 
350  if(act_as_bcc)
351  std::cout << "bcc: version " << gcc_version << " (goto-cc "
352  << CBMC_VERSION << ")\n";
353  else
354  {
356  std::cout << "clang version " << gcc_version << " (goto-cc "
357  << CBMC_VERSION << ")\n";
358  else
359  std::cout << "gcc (goto-cc " << CBMC_VERSION << ") " << gcc_version
360  << '\n';
361  }
362  }
363 
364  compilet compiler(cmdline,
366  cmdline.isset("Werror") &&
367  cmdline.isset("Wextra") &&
368  !cmdline.isset("Wno-error"));
369 
370  if(cmdline.isset("version"))
371  {
373  return run_gcc(compiler);
374 
375  std::cout
376  << '\n'
377  << "Copyright (C) 2006-2018 Daniel Kroening, Christoph Wintersteiger\n"
378  << "CBMC version: " << CBMC_VERSION << '\n'
379  << "Architecture: " << config.this_architecture() << '\n'
380  << "OS: " << config.this_operating_system() << '\n';
381 
383  std::cout << "clang: " << gcc_version << '\n';
384  else
385  std::cout << "gcc: " << gcc_version << '\n';
386 
387  return EX_OK; // Exit!
388  }
389 
390  if(
391  cmdline.isset("dumpmachine") || cmdline.isset("dumpspecs") ||
392  cmdline.isset("dumpversion") || cmdline.isset("print-sysroot") ||
393  cmdline.isset("print-sysroot-headers-suffix"))
394  {
396  return run_gcc(compiler);
397 
398  // GCC will only print one of these, even when multiple arguments are
399  // passed, so we do the same
400  if(cmdline.isset("dumpmachine"))
401  std::cout << config.this_architecture() << '\n';
402  else if(cmdline.isset("dumpversion"))
403  std::cout << gcc_version << '\n';
404 
405  // we don't have any meaningful output for the other options, and GCC
406  // doesn't necessarily produce non-empty output either
407 
408  return EX_OK;
409  }
410 
411  if(act_as_bcc)
412  {
414  debug() << "BCC mode (hybrid)" << eom;
415  else
416  debug() << "BCC mode" << eom;
417  }
418  else
419  {
421  debug() << "GCC mode (hybrid)" << eom;
422  else
423  debug() << "GCC mode" << eom;
424  }
425 
426  // determine actions to be undertaken
427  if(cmdline.isset('S'))
428  compiler.mode=compilet::ASSEMBLE_ONLY;
429  else if(cmdline.isset('c'))
430  compiler.mode=compilet::COMPILE_ONLY;
431  else if(cmdline.isset('E'))
432  compiler.mode=compilet::PREPROCESS_ONLY;
433  else if(cmdline.isset("shared") ||
434  cmdline.isset('r')) // really not well documented
435  compiler.mode=compilet::COMPILE_LINK;
436  else
437  compiler.mode=compilet::COMPILE_LINK_EXECUTABLE;
438 
439  // In gcc mode, we have just pass on to gcc to handle the following:
440  // * if -M or -MM is given, we do dependencies only
441  // * preprocessing (-E)
442  // * no input files given
443 
444  if(cmdline.isset('M') ||
445  cmdline.isset("MM") ||
446  cmdline.isset('E') ||
448  return run_gcc(compiler); // exit!
449 
450  // get configuration
451  config.set(cmdline);
452 
453  // Intel-specific
454  // in GCC, m16 is 32-bit (!), as documented here:
455  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59672
456  if(cmdline.isset("m16") ||
457  cmdline.isset("m32") || cmdline.isset("mx32"))
458  {
459  config.ansi_c.arch="i386";
461  }
462  else if(cmdline.isset("m64"))
463  {
464  config.ansi_c.arch="x86_64";
466  }
467 
468  // ARM-specific
469  if(cmdline.isset("mbig-endian") || cmdline.isset("mbig"))
471  else if(cmdline.isset("little-endian") || cmdline.isset("mlittle"))
473 
474  if(cmdline.isset("mthumb") || cmdline.isset("mthumb-interwork"))
476 
477  // -mcpu sets both the arch and tune, but should only be used if
478  // neither -march nor -mtune are passed on the command line.
479  std::string target_cpu=
480  cmdline.isset("march") ? cmdline.get_value("march") :
481  cmdline.isset("mtune") ? cmdline.get_value("mtune") :
482  cmdline.isset("mcpu") ? cmdline.get_value("mcpu") : "";
483 
484  if(target_cpu!="")
485  {
486  // Work out what CPROVER architecture we should target.
487  for(auto &pair : arch_map)
488  for(auto &processor : pair.second)
489  if(processor==target_cpu)
490  {
491  if(pair.first.find("mips")==std::string::npos)
492  config.set_arch(pair.first);
493  else
494  {
495  // Targeting a MIPS processor. MIPS is special; we also need
496  // to know the endianness. -EB (big-endian) is the default.
497  if(cmdline.isset("EL"))
498  {
499  if(pair.first=="mips32o")
500  config.set_arch("mipsel");
501  else if(pair.first=="mips32n")
502  config.set_arch("mipsn32el");
503  else
504  config.set_arch("mips64el");
505  }
506  else
507  {
508  if(pair.first=="mips32o")
509  config.set_arch("mips");
510  else if(pair.first=="mips32n")
511  config.set_arch("mipsn32");
512  else
513  config.set_arch("mips64");
514  }
515  }
516  }
517  }
518 
519  // -fshort-wchar makes wchar_t "short unsigned int"
520  if(cmdline.isset("fshort-wchar"))
521  {
524  }
525 
526  // -fsingle-precision-constant makes floating-point constants "float"
527  // instead of double
528  if(cmdline.isset("-fsingle-precision-constant"))
530 
531  // ISO/IEC TS 18661-3:2015 support was introduced with gcc 7.0
535 
536  const auto gcc_float128_minor_version =
537  config.ansi_c.arch == "x86_64" ? 3u : 5u;
538 
541  gcc_version.is_at_least(4u, gcc_float128_minor_version);
542 
543  // -fshort-double makes double the same as float
544  if(cmdline.isset("fshort-double"))
546 
547  switch(compiler.mode)
548  {
550  debug() << "Linking a library only" << eom; break;
552  debug() << "Compiling only" << eom; break;
554  debug() << "Assembling only" << eom; break;
556  debug() << "Preprocessing only" << eom; break;
558  debug() << "Compiling and linking a library" << eom; break;
560  debug() << "Compiling and linking an executable" << eom; break;
561  }
562 
563  if(cmdline.isset("i386-win32") ||
564  cmdline.isset("winx64"))
565  {
566  // We may wish to reconsider the below.
568  debug() << "Enabling Visual Studio syntax" << eom;
569  }
570  else
571  {
574  else
576  }
577 
578  if(compiler.mode==compilet::ASSEMBLE_ONLY)
579  compiler.object_file_extension="s";
580  else
581  compiler.object_file_extension="o";
582 
583  if(cmdline.isset("std"))
584  {
585  std::string std_string=cmdline.get_value("std");
586 
587  if(std_string=="gnu89" || std_string=="c89")
589 
590  if(std_string=="gnu99" || std_string=="c99" || std_string=="iso9899:1999" ||
591  std_string=="gnu9x" || std_string=="c9x" || std_string=="iso9899:199x")
593 
594  if(std_string=="gnu11" || std_string=="c11" ||
595  std_string=="gnu1x" || std_string=="c1x")
597 
598  if(std_string=="c++11" || std_string=="c++1x" ||
599  std_string=="gnu++11" || std_string=="gnu++1x" ||
600  std_string=="c++1y" ||
601  std_string=="gnu++1y")
602  config.cpp.set_cpp11();
603 
604  if(std_string=="gnu++14" || std_string=="c++14")
605  config.cpp.set_cpp14();
606  }
607  else
608  {
611  }
612 
613  // gcc's default is 32 bits for wchar_t
614  if(cmdline.isset("short-wchar"))
616 
617  // gcc's default is 64 bits for double
618  if(cmdline.isset("short-double"))
620 
621  // gcc's default is signed chars on most architectures
622  if(cmdline.isset("funsigned-char"))
624 
625  if(cmdline.isset("fsigned-char"))
627 
628  if(cmdline.isset('U'))
630 
631  if(cmdline.isset("undef"))
632  config.ansi_c.preprocessor_options.push_back("-undef");
633 
634  if(cmdline.isset("nostdinc"))
635  config.ansi_c.preprocessor_options.push_back("-nostdinc");
636 
637  if(cmdline.isset('L'))
638  compiler.library_paths=cmdline.get_values('L');
639  // Don't add the system paths!
640 
641  if(cmdline.isset('l'))
642  compiler.libraries=cmdline.get_values('l');
643 
644  if(cmdline.isset("static"))
645  compiler.libraries.push_back("c");
646 
647  if(cmdline.isset("pthread"))
648  compiler.libraries.push_back("pthread");
649 
650  if(cmdline.isset('o'))
651  {
652  // given gcc -o file1 -o file2,
653  // gcc will output to file2, not file1
654  compiler.output_file_object=cmdline.get_values('o').back();
655  compiler.output_file_executable=cmdline.get_values('o').back();
656  }
657  else
658  {
659  compiler.output_file_object="";
660  compiler.output_file_executable="a.out";
661  }
662 
663  // We now iterate over any input files
664 
665  temp_dirt temp_dir("goto-cc-XXXXXX");
666 
667  {
668  std::string language;
669 
670  for(goto_cc_cmdlinet::parsed_argvt::iterator
671  arg_it=cmdline.parsed_argv.begin();
672  arg_it!=cmdline.parsed_argv.end();
673  arg_it++)
674  {
675  if(arg_it->is_infile_name)
676  {
677  // do any preprocessing needed
678 
679  if(language=="cpp-output" || language=="c++-cpp-output")
680  {
681  compiler.add_input_file(arg_it->arg);
682  }
683  else if(language=="c" || language=="c++" ||
684  (language=="" && needs_preprocessing(arg_it->arg)))
685  {
686  std::string new_suffix;
687 
688  if(language=="c")
689  new_suffix=".i";
690  else if(language=="c++")
691  new_suffix=".ii";
692  else
693  new_suffix=has_suffix(arg_it->arg, ".c")?".i":".ii";
694 
695  std::string new_name=
696  get_base_name(arg_it->arg, true)+new_suffix;
697  std::string dest=temp_dir(new_name);
698 
699  int exit_code=
700  preprocess(language, arg_it->arg, dest, act_as_bcc);
701 
702  if(exit_code!=0)
703  {
704  error() << "preprocessing has failed" << eom;
705  return exit_code;
706  }
707 
708  compiler.add_input_file(dest);
709  }
710  else
711  compiler.add_input_file(arg_it->arg);
712  }
713  else if(arg_it->arg=="-x")
714  {
715  arg_it++;
716  if(arg_it!=cmdline.parsed_argv.end())
717  {
718  language=arg_it->arg;
719  if(language=="none")
720  language="";
721  }
722  }
723  else if(has_prefix(arg_it->arg, "-x"))
724  {
725  language=std::string(arg_it->arg, 2, std::string::npos);
726  if(language=="none")
727  language="";
728  }
729  }
730  }
731 
732  if(
733  cmdline.isset('o') && cmdline.isset('c') &&
734  compiler.source_files.size() >= 2)
735  {
736  error() << "cannot specify -o with -c with multiple files" << eom;
737  return 1; // to match gcc's behaviour
738  }
739 
740  // Revert to gcc in case there is no source to compile
741  // and no binary to link.
742 
743  if(compiler.source_files.empty() &&
744  compiler.object_files.empty())
745  return run_gcc(compiler); // exit!
746 
747  if(compiler.mode==compilet::ASSEMBLE_ONLY)
748  return asm_output(act_as_bcc, compiler.source_files, compiler);
749 
750  // do all the rest
751  if(compiler.doit())
752  return 1; // GCC exit code for all kinds of errors
753 
754  // We can generate hybrid ELF and Mach-O binaries
755  // containing both executable machine code and the goto-binary.
756  if(produce_hybrid_binary && !act_as_bcc)
757  return gcc_hybrid_binary(compiler);
758 
759  return EX_OK;
760 }
761 
764  const std::string &language,
765  const std::string &src,
766  const std::string &dest,
767  bool act_as_bcc)
768 {
769  // build new argv
770  std::vector<std::string> new_argv;
771 
772  new_argv.reserve(cmdline.parsed_argv.size());
773 
774  bool skip_next=false;
775 
776  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
777  it=cmdline.parsed_argv.begin();
778  it!=cmdline.parsed_argv.end();
779  it++)
780  {
781  if(skip_next)
782  {
783  // skip
784  skip_next=false;
785  }
786  else if(it->is_infile_name)
787  {
788  // skip
789  }
790  else if(it->arg=="-c" || it->arg=="-E" || it->arg=="-S")
791  {
792  // skip
793  }
794  else if(it->arg=="-o")
795  {
796  skip_next=true;
797  }
798  else if(has_prefix(it->arg, "-o"))
799  {
800  // ignore
801  }
802  else
803  new_argv.push_back(it->arg);
804  }
805 
806  // We just want to preprocess.
807  new_argv.push_back("-E");
808 
809  // destination file
810  std::string stdout_file;
811  if(act_as_bcc)
812  stdout_file=dest;
813  else
814  {
815  new_argv.push_back("-o");
816  new_argv.push_back(dest);
817  }
818 
819  // language, if given
820  if(language!="")
821  {
822  new_argv.push_back("-x");
823  new_argv.push_back(language);
824  }
825 
826  // source file
827  new_argv.push_back(src);
828 
829  // overwrite argv[0]
830  INVARIANT(new_argv.size()>=1, "No program name in argv");
831  new_argv[0]=native_tool_name.c_str();
832 
833  debug() << "RUN:";
834  for(std::size_t i=0; i<new_argv.size(); i++)
835  debug() << " " << new_argv[i];
836  debug() << eom;
837 
838  return run(new_argv[0], new_argv, cmdline.stdin_file, stdout_file, "");
839 }
840 
841 int gcc_modet::run_gcc(const compilet &compiler)
842 {
843  PRECONDITION(!cmdline.parsed_argv.empty());
844 
845  // build new argv
846  std::vector<std::string> new_argv;
847  new_argv.reserve(cmdline.parsed_argv.size());
848  for(const auto &a : cmdline.parsed_argv)
849  new_argv.push_back(a.arg);
850 
851  if(compiler.wrote_object_files())
852  {
853  // Undefine all __CPROVER macros for the system compiler
854  std::map<irep_idt, std::size_t> arities;
855  compiler.cprover_macro_arities(arities);
856  for(const auto &pair : arities)
857  {
858  std::ostringstream addition;
859  addition << "-D" << id2string(pair.first) << "(";
860  std::vector<char> params(pair.second);
861  std::iota(params.begin(), params.end(), 'a');
862  for(std::vector<char>::iterator it=params.begin(); it!=params.end(); ++it)
863  {
864  addition << *it;
865  if(it+1!=params.end())
866  addition << ",";
867  }
868  addition << ")= ";
869  new_argv.push_back(addition.str());
870  }
871  }
872 
873  // overwrite argv[0]
874  new_argv[0]=native_tool_name;
875 
876  debug() << "RUN:";
877  for(std::size_t i=0; i<new_argv.size(); i++)
878  debug() << " " << new_argv[i];
879  debug() << eom;
880 
881  return run(new_argv[0], new_argv, cmdline.stdin_file, "", "");
882 }
883 
885 {
886  {
887  bool have_files=false;
888 
889  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
890  it=cmdline.parsed_argv.begin();
891  it!=cmdline.parsed_argv.end();
892  it++)
893  if(it->is_infile_name)
894  have_files=true;
895 
896  if(!have_files)
897  return EX_OK;
898  }
899 
900  std::list<std::string> output_files;
901 
902  if(cmdline.isset('c'))
903  {
904  if(cmdline.isset('o'))
905  {
906  // there should be only one input file
907  output_files.push_back(cmdline.get_value('o'));
908  }
909  else
910  {
911  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
912  i_it=cmdline.parsed_argv.begin();
913  i_it!=cmdline.parsed_argv.end();
914  i_it++)
915  if(i_it->is_infile_name &&
916  needs_preprocessing(i_it->arg))
917  output_files.push_back(get_base_name(i_it->arg, true)+".o");
918  }
919  }
920  else
921  {
922  // -c is not given
923  if(cmdline.isset('o'))
924  output_files.push_back(cmdline.get_value('o'));
925  else
926  output_files.push_back("a.out");
927  }
928 
929  if(output_files.empty() ||
930  (output_files.size()==1 &&
931  output_files.front()=="/dev/null"))
932  return run_gcc(compiler);
933 
934  debug() << "Running " << native_tool_name
935  << " to generate hybrid binary" << eom;
936 
937  // save the goto-cc output files
938  std::list<std::string> goto_binaries;
939  for(std::list<std::string>::const_iterator
940  it=output_files.begin();
941  it!=output_files.end();
942  it++)
943  {
944  std::string bin_name=*it+goto_binary_tmp_suffix;
945  int result=rename(it->c_str(), bin_name.c_str());
946  if(result!=0)
947  {
948  error() << "Rename failed: " << std::strerror(errno) << eom;
949  return result;
950  }
951  goto_binaries.push_back(bin_name);
952  }
953 
954  int result=run_gcc(compiler);
955 
956  if(result==0 &&
957  cmdline.isset('T') &&
958  goto_binaries.size()==1 &&
959  output_files.size()==1)
960  {
961  linker_script_merget ls_merge(
962  compiler, output_files.front(), goto_binaries.front(),
964  result=ls_merge.add_linker_script_definitions();
965  }
966 
967  std::string native_tool;
968 
970  native_tool=linker_name(cmdline, base_name);
971  else if(has_suffix(compiler_name(cmdline, base_name), "-gcc"))
972  native_tool=compiler_name(cmdline, base_name);
973 
974  // merge output from gcc with goto-binaries
975  // using objcopy, or do cleanup if an earlier call failed
976  for(std::list<std::string>::const_iterator
977  it=output_files.begin();
978  it!=output_files.end();
979  it++)
980  {
981  std::string goto_binary=*it+goto_binary_tmp_suffix;
982 
983  if(result==0)
985  native_tool, goto_binary, *it, get_message_handler());
986  }
987 
988  return result;
989 }
990 
992  bool act_as_bcc,
993  const std::list<std::string> &preprocessed_source_files,
994  const compilet &compiler)
995 {
996  {
997  bool have_files=false;
998 
999  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
1000  it=cmdline.parsed_argv.begin();
1001  it!=cmdline.parsed_argv.end();
1002  it++)
1003  if(it->is_infile_name)
1004  have_files=true;
1005 
1006  if(!have_files)
1007  return EX_OK;
1008  }
1009 
1011  {
1012  debug() << "Running " << native_tool_name
1013  << " to generate native asm output" << eom;
1014 
1015  int result=run_gcc(compiler);
1016  if(result!=0)
1017  // native tool failed
1018  return result;
1019  }
1020 
1021  std::map<std::string, std::string> output_files;
1022 
1023  if(cmdline.isset('o'))
1024  {
1025  // GCC --combine supports more than one source file
1026  for(const auto &s : preprocessed_source_files)
1027  output_files.insert(std::make_pair(s, cmdline.get_value('o')));
1028  }
1029  else
1030  {
1031  for(const std::string &s : preprocessed_source_files)
1032  output_files.insert(
1033  std::make_pair(s, get_base_name(s, true)+".s"));
1034  }
1035 
1036  if(output_files.empty() ||
1037  (output_files.size()==1 &&
1038  output_files.begin()->second=="/dev/null"))
1039  return EX_OK;
1040 
1041  debug()
1042  << "Appending preprocessed sources to generate hybrid asm output"
1043  << eom;
1044 
1045  for(const auto &so : output_files)
1046  {
1047  std::ifstream is(so.first);
1048  if(!is.is_open())
1049  {
1050  error() << "Failed to open input source " << so.first << eom;
1051  return 1;
1052  }
1053 
1054  std::ofstream os(so.second, std::ios::app);
1055  if(!os.is_open())
1056  {
1057  error() << "Failed to open output file " << so.second << eom;
1058  return 1;
1059  }
1060 
1061  const char comment=act_as_bcc ? ':' : '#';
1062 
1063  os << comment << comment << " GOTO-CC" << '\n';
1064 
1065  std::string line;
1066 
1067  while(std::getline(is, line))
1068  {
1069  os << comment << comment << line << '\n';
1070  }
1071  }
1072 
1073  return EX_OK;
1074 }
1075 
1078 {
1079  std::cout << "goto-cc understands the options of "
1080  << "gcc plus the following.\n\n";
1081 }
void set_arch_spec_arm(const irep_idt &subarch)
Definition: config.cpp:280
const bool produce_hybrid_binary
Definition: gcc_mode.h:39
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:110
struct configt::ansi_ct ansi_c
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
enum gcc_versiont::flavort flavor
const std::string goto_binary_tmp_suffix
Definition: gcc_mode.h:43
std::string stdin_file
int run_gcc(const compilet &compiler)
call gcc with original command line
Definition: gcc_mode.cpp:841
std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:107
int asm_output(bool act_as_bcc, const std::list< std::string > &preprocessed_source_files, const compilet &compiler)
Definition: gcc_mode.cpp:991
std::size_t single_width
Definition: config.h:37
bool single_precision_constant
Definition: config.h:47
static unsigned eval_verbosity(const std::string &user_input, const message_levelt default_verbosity, message_handlert &dest)
Parse a (user-)provided string as a verbosity level and set it as the verbosity of dest...
Definition: message.cpp:98
endiannesst endianness
Definition: config.h:76
Read Goto Programs.
literalt pos(literalt a)
Definition: literal.h:193
std::string get_value(char option) const
Definition: cmdline.cpp:45
Base class for command line interpretation.
unsignedbv_typet size_type()
Definition: c_types.cpp:58
std::size_t double_width
Definition: config.h:38
std::list< std::string > undefines
Definition: config.h:122
bool wrote_object_files() const
Has this compiler written any object files?
Definition: compile.h:79
configt config
Definition: config.cpp:24
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
gcc_message_handlert gcc_message_handler
Definition: gcc_mode.h:37
void get(const std::string &executable)
Definition: gcc_version.cpp:19
void cprover_macro_arities(std::map< irep_idt, std::size_t > &cprover_macros) const
__CPROVER_... macros written to object files and their arities
Definition: compile.h:86
static std::string linker_name(const cmdlinet &cmdline, const std::string &base_name)
Definition: gcc_mode.cpp:86
int preprocess(const std::string &language, const std::string &src, const std::string &dest, bool act_as_bcc)
call gcc for preprocessing
Definition: gcc_mode.cpp:763
void set_cpp14()
Definition: config.h:143
bool set(const cmdlinet &cmdline)
Definition: config.cpp:767
void set_c89()
Definition: config.h:51
virtual bool isset(char option) const
Definition: cmdline.cpp:27
flavourt mode
Definition: config.h:115
Create hybrid binary with goto-binary section.
void set_arch_spec_x86_64()
Definition: config.cpp:181
gcc_modet(goto_cc_cmdlinet &_cmdline, const std::string &_base_name, bool _produce_hybrid_binary)
Definition: gcc_mode.cpp:106
void set_arch(const irep_idt &)
Definition: config.cpp:701
goto_cc_cmdlinet & cmdline
Definition: goto_cc_mode.h:37
bool is_at_least(unsigned v_major, unsigned v_minor=0, unsigned v_patchlevel=0) const
mstreamt & error() const
Definition: message.h:386
irep_idt arch
Definition: config.h:84
enum configt::cppt::cpp_standardt cpp_standard
#define PRECONDITION(CONDITION)
Definition: invariant.h:438
int run(const std::string &what, const std::vector< std::string > &argv)
Definition: run.cpp:47
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
configt::ansi_ct::c_standardt default_c_standard
Definition: gcc_version.h:39
bool char_is_unsigned
Definition: config.h:43
gcc_versiont gcc_version
Definition: gcc_mode.h:66
void set_c11()
Definition: config.h:53
void help_mode() final
display command line help
Definition: gcc_mode.cpp:1077
enum configt::ansi_ct::c_standardt c_standard
std::size_t wchar_t_width
Definition: config.h:40
int gcc_hybrid_binary(compilet &compiler)
Definition: gcc_mode.cpp:884
bool Float128_type
Definition: config.h:46
static eomt eom
Definition: message.h:284
int doit() final
does it.
Definition: gcc_mode.cpp:320
static bool needs_preprocessing(const std::string &)
Definition: gcc_mode.cpp:305
static irep_idt this_operating_system()
Definition: config.cpp:1368
struct configt::cppt cpp
void set_c99()
Definition: config.h:52
message_handlert & get_message_handler()
Definition: message.h:174
void set_cpp11()
Definition: config.h:142
mstreamt & result() const
Definition: message.h:396
std::string native_tool_name
Definition: gcc_mode.h:41
int hybrid_binary(const std::string &compiler_or_linker, const std::string &goto_binary_file, const std::string &output_file, message_handlert &message_handler)
Merges a goto binary into an object file (e.g.
const char * CBMC_VERSION
Definition: version.cpp:1
virtual void help()
display command line help
void set_arch_spec_i386()
Definition: config.cpp:149
bool have_infile_arg() const
const std::map< std::string, std::set< std::string > > arch_map
Associate CBMC architectures with processor names.
Definition: gcc_mode.h:46
static std::string compiler_name(const cmdlinet &cmdline, const std::string &base_name)
Definition: gcc_mode.cpp:53
bool has_suffix(const std::string &s, const std::string &suffix)
Definition: suffix.h:15
parsed_argvt parsed_argv
mstreamt & debug() const
Definition: message.h:416
Merge linker script-defined symbols into a goto-program.
bool wchar_t_is_unsigned
Definition: config.h:43
std::list< std::string > preprocessor_options
Definition: config.h:123
configt::cppt::cpp_standardt default_cxx_standard
Definition: gcc_version.h:40
bool ts_18661_3_Floatn_types
Definition: config.h:45
const std::string base_name
Definition: goto_cc_mode.h:38
static irep_idt this_architecture()
Definition: config.cpp:1268
std::size_t short_int_width
Definition: config.h:34
Definition: kdev_t.h:19
Synthesise definitions of symbols that are defined in linker scripts.