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  // model validation
427  compiler.validate_goto_model = cmdline.isset("validate-goto-model");
428 
429  // determine actions to be undertaken
430  if(cmdline.isset('S'))
431  compiler.mode=compilet::ASSEMBLE_ONLY;
432  else if(cmdline.isset('c'))
433  compiler.mode=compilet::COMPILE_ONLY;
434  else if(cmdline.isset('E'))
436  else if(cmdline.isset("shared") ||
437  cmdline.isset('r')) // really not well documented
438  compiler.mode=compilet::COMPILE_LINK;
439  else
441 
442  // In gcc mode, we have just pass on to gcc to handle the following:
443  // * if -M or -MM is given, we do dependencies only
444  // * preprocessing (-E)
445  // * no input files given
446 
447  if(cmdline.isset('M') ||
448  cmdline.isset("MM") ||
449  cmdline.isset('E') ||
451  return run_gcc(compiler); // exit!
452 
453  // get configuration
454  config.set(cmdline);
455 
456  // Intel-specific
457  // in GCC, m16 is 32-bit (!), as documented here:
458  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59672
459  if(cmdline.isset("m16") ||
460  cmdline.isset("m32") || cmdline.isset("mx32"))
461  {
462  config.ansi_c.arch="i386";
464  }
465  else if(cmdline.isset("m64"))
466  {
467  config.ansi_c.arch="x86_64";
469  }
470 
471  // ARM-specific
472  if(cmdline.isset("mbig-endian") || cmdline.isset("mbig"))
474  else if(cmdline.isset("little-endian") || cmdline.isset("mlittle"))
476 
477  if(cmdline.isset("mthumb") || cmdline.isset("mthumb-interwork"))
479 
480  // -mcpu sets both the arch and tune, but should only be used if
481  // neither -march nor -mtune are passed on the command line.
482  std::string target_cpu=
483  cmdline.isset("march") ? cmdline.get_value("march") :
484  cmdline.isset("mtune") ? cmdline.get_value("mtune") :
485  cmdline.isset("mcpu") ? cmdline.get_value("mcpu") : "";
486 
487  if(!target_cpu.empty())
488  {
489  // Work out what CPROVER architecture we should target.
490  for(auto &pair : arch_map)
491  for(auto &processor : pair.second)
492  if(processor==target_cpu)
493  {
494  if(pair.first.find("mips")==std::string::npos)
495  config.set_arch(pair.first);
496  else
497  {
498  // Targeting a MIPS processor. MIPS is special; we also need
499  // to know the endianness. -EB (big-endian) is the default.
500  if(cmdline.isset("EL"))
501  {
502  if(pair.first=="mips32o")
503  config.set_arch("mipsel");
504  else if(pair.first=="mips32n")
505  config.set_arch("mipsn32el");
506  else
507  config.set_arch("mips64el");
508  }
509  else
510  {
511  if(pair.first=="mips32o")
512  config.set_arch("mips");
513  else if(pair.first=="mips32n")
514  config.set_arch("mipsn32");
515  else
516  config.set_arch("mips64");
517  }
518  }
519  }
520  }
521 
522  // -fshort-wchar makes wchar_t "short unsigned int"
523  if(cmdline.isset("fshort-wchar"))
524  {
527  }
528 
529  // -fsingle-precision-constant makes floating-point constants "float"
530  // instead of double
531  if(cmdline.isset("-fsingle-precision-constant"))
533 
534  // -fshort-double makes double the same as float
535  if(cmdline.isset("fshort-double"))
537 
538  // configure version-specific gcc settings
540 
541  switch(compiler.mode)
542  {
544  debug() << "Linking a library only" << eom; break;
546  debug() << "Compiling only" << eom; break;
548  debug() << "Assembling only" << eom; break;
550  debug() << "Preprocessing only" << eom; break;
552  debug() << "Compiling and linking a library" << eom; break;
554  debug() << "Compiling and linking an executable" << eom; break;
555  }
556 
557  if(cmdline.isset("i386-win32") ||
558  cmdline.isset("winx64"))
559  {
560  // We may wish to reconsider the below.
562  debug() << "Enabling Visual Studio syntax" << eom;
563  }
564  else
565  {
568  else
570  }
571 
572  if(compiler.mode==compilet::ASSEMBLE_ONLY)
573  compiler.object_file_extension="s";
574  else
575  compiler.object_file_extension="o";
576 
577  if(cmdline.isset("std"))
578  {
579  std::string std_string=cmdline.get_value("std");
580 
581  if(std_string=="gnu89" || std_string=="c89")
583 
584  if(std_string=="gnu99" || std_string=="c99" || std_string=="iso9899:1999" ||
585  std_string=="gnu9x" || std_string=="c9x" || std_string=="iso9899:199x")
587 
588  if(std_string=="gnu11" || std_string=="c11" ||
589  std_string=="gnu1x" || std_string=="c1x")
591 
592  if(std_string=="c++11" || std_string=="c++1x" ||
593  std_string=="gnu++11" || std_string=="gnu++1x" ||
594  std_string=="c++1y" ||
595  std_string=="gnu++1y")
596  config.cpp.set_cpp11();
597 
598  if(std_string=="gnu++14" || std_string=="c++14")
599  config.cpp.set_cpp14();
600  }
601  else
602  {
605  }
606 
607  // gcc's default is 32 bits for wchar_t
608  if(cmdline.isset("short-wchar"))
610 
611  // gcc's default is 64 bits for double
612  if(cmdline.isset("short-double"))
614 
615  // gcc's default is signed chars on most architectures
616  if(cmdline.isset("funsigned-char"))
618 
619  if(cmdline.isset("fsigned-char"))
621 
622  if(cmdline.isset('U'))
624 
625  if(cmdline.isset("undef"))
626  config.ansi_c.preprocessor_options.push_back("-undef");
627 
628  if(cmdline.isset("nostdinc"))
629  config.ansi_c.preprocessor_options.push_back("-nostdinc");
630 
631  if(cmdline.isset('L'))
632  compiler.library_paths=cmdline.get_values('L');
633  // Don't add the system paths!
634 
635  if(cmdline.isset('l'))
636  compiler.libraries=cmdline.get_values('l');
637 
638  if(cmdline.isset("static"))
639  compiler.libraries.push_back("c");
640 
641  if(cmdline.isset("pthread"))
642  compiler.libraries.push_back("pthread");
643 
644  if(cmdline.isset('o'))
645  {
646  // given gcc -o file1 -o file2,
647  // gcc will output to file2, not file1
648  compiler.output_file_object=cmdline.get_values('o').back();
649  compiler.output_file_executable=cmdline.get_values('o').back();
650  }
651  else
652  {
653  compiler.output_file_object.clear();
654  compiler.output_file_executable="a.out";
655  }
656 
657  // We now iterate over any input files
658 
659  temp_dirt temp_dir("goto-cc-XXXXXX");
660 
661  {
662  std::string language;
663 
664  for(goto_cc_cmdlinet::parsed_argvt::iterator
665  arg_it=cmdline.parsed_argv.begin();
666  arg_it!=cmdline.parsed_argv.end();
667  arg_it++)
668  {
669  if(arg_it->is_infile_name)
670  {
671  // do any preprocessing needed
672 
673  if(language=="cpp-output" || language=="c++-cpp-output")
674  {
675  compiler.add_input_file(arg_it->arg);
676  }
677  else if(
678  language == "c" || language == "c++" ||
679  (language.empty() && needs_preprocessing(arg_it->arg)))
680  {
681  std::string new_suffix;
682 
683  if(language=="c")
684  new_suffix=".i";
685  else if(language=="c++")
686  new_suffix=".ii";
687  else
688  new_suffix=has_suffix(arg_it->arg, ".c")?".i":".ii";
689 
690  std::string new_name=
691  get_base_name(arg_it->arg, true)+new_suffix;
692  std::string dest=temp_dir(new_name);
693 
694  int exit_code=
695  preprocess(language, arg_it->arg, dest, act_as_bcc);
696 
697  if(exit_code!=0)
698  {
699  error() << "preprocessing has failed" << eom;
700  return exit_code;
701  }
702 
703  compiler.add_input_file(dest);
704  }
705  else
706  compiler.add_input_file(arg_it->arg);
707  }
708  else if(arg_it->arg=="-x")
709  {
710  arg_it++;
711  if(arg_it!=cmdline.parsed_argv.end())
712  {
713  language=arg_it->arg;
714  if(language=="none")
715  language.clear();
716  }
717  }
718  else if(has_prefix(arg_it->arg, "-x"))
719  {
720  language=std::string(arg_it->arg, 2, std::string::npos);
721  if(language=="none")
722  language.clear();
723  }
724  }
725  }
726 
727  if(
728  cmdline.isset('o') && cmdline.isset('c') &&
729  compiler.source_files.size() >= 2)
730  {
731  error() << "cannot specify -o with -c with multiple files" << eom;
732  return 1; // to match gcc's behaviour
733  }
734 
735  // Revert to gcc in case there is no source to compile
736  // and no binary to link.
737 
738  if(compiler.source_files.empty() &&
739  compiler.object_files.empty())
740  return run_gcc(compiler); // exit!
741 
742  if(compiler.mode==compilet::ASSEMBLE_ONLY)
743  return asm_output(act_as_bcc, compiler.source_files, compiler);
744 
745  // do all the rest
746  if(compiler.doit())
747  return 1; // GCC exit code for all kinds of errors
748 
749  // We can generate hybrid ELF and Mach-O binaries
750  // containing both executable machine code and the goto-binary.
751  if(produce_hybrid_binary && !act_as_bcc)
752  return gcc_hybrid_binary(compiler);
753 
754  return EX_OK;
755 }
756 
759  const std::string &language,
760  const std::string &src,
761  const std::string &dest,
762  bool act_as_bcc)
763 {
764  // build new argv
765  std::vector<std::string> new_argv;
766 
767  new_argv.reserve(cmdline.parsed_argv.size());
768 
769  bool skip_next=false;
770 
771  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
772  it=cmdline.parsed_argv.begin();
773  it!=cmdline.parsed_argv.end();
774  it++)
775  {
776  if(skip_next)
777  {
778  // skip
779  skip_next=false;
780  }
781  else if(it->is_infile_name)
782  {
783  // skip
784  }
785  else if(it->arg=="-c" || it->arg=="-E" || it->arg=="-S")
786  {
787  // skip
788  }
789  else if(it->arg=="-o")
790  {
791  skip_next=true;
792  }
793  else if(has_prefix(it->arg, "-o"))
794  {
795  // ignore
796  }
797  else
798  new_argv.push_back(it->arg);
799  }
800 
801  // We just want to preprocess.
802  new_argv.push_back("-E");
803 
804  // destination file
805  std::string stdout_file;
806  if(act_as_bcc)
807  stdout_file=dest;
808  else
809  {
810  new_argv.push_back("-o");
811  new_argv.push_back(dest);
812  }
813 
814  // language, if given
815  if(!language.empty())
816  {
817  new_argv.push_back("-x");
818  new_argv.push_back(language);
819  }
820 
821  // source file
822  new_argv.push_back(src);
823 
824  // overwrite argv[0]
825  INVARIANT(new_argv.size()>=1, "No program name in argv");
826  new_argv[0]=native_tool_name.c_str();
827 
828  debug() << "RUN:";
829  for(std::size_t i=0; i<new_argv.size(); i++)
830  debug() << " " << new_argv[i];
831  debug() << eom;
832 
833  return run(new_argv[0], new_argv, cmdline.stdin_file, stdout_file, "");
834 }
835 
836 int gcc_modet::run_gcc(const compilet &compiler)
837 {
838  PRECONDITION(!cmdline.parsed_argv.empty());
839 
840  // build new argv
841  std::vector<std::string> new_argv;
842  new_argv.reserve(cmdline.parsed_argv.size());
843  for(const auto &a : cmdline.parsed_argv)
844  new_argv.push_back(a.arg);
845 
846  if(compiler.wrote_object_files())
847  {
848  // Undefine all __CPROVER macros for the system compiler
849  std::map<irep_idt, std::size_t> arities;
850  compiler.cprover_macro_arities(arities);
851  for(const auto &pair : arities)
852  {
853  std::ostringstream addition;
854  addition << "-D" << id2string(pair.first) << "(";
855  std::vector<char> params(pair.second);
856  std::iota(params.begin(), params.end(), 'a');
857  for(std::vector<char>::iterator it=params.begin(); it!=params.end(); ++it)
858  {
859  addition << *it;
860  if(it+1!=params.end())
861  addition << ",";
862  }
863  addition << ")= ";
864  new_argv.push_back(addition.str());
865  }
866  }
867 
868  // overwrite argv[0]
869  new_argv[0]=native_tool_name;
870 
871  debug() << "RUN:";
872  for(std::size_t i=0; i<new_argv.size(); i++)
873  debug() << " " << new_argv[i];
874  debug() << eom;
875 
876  return run(new_argv[0], new_argv, cmdline.stdin_file, "", "");
877 }
878 
880 {
881  {
882  bool have_files=false;
883 
884  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
885  it=cmdline.parsed_argv.begin();
886  it!=cmdline.parsed_argv.end();
887  it++)
888  if(it->is_infile_name)
889  have_files=true;
890 
891  if(!have_files)
892  return EX_OK;
893  }
894 
895  std::list<std::string> output_files;
896 
897  if(cmdline.isset('c'))
898  {
899  if(cmdline.isset('o'))
900  {
901  // there should be only one input file
902  output_files.push_back(cmdline.get_value('o'));
903  }
904  else
905  {
906  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
907  i_it=cmdline.parsed_argv.begin();
908  i_it!=cmdline.parsed_argv.end();
909  i_it++)
910  if(i_it->is_infile_name &&
911  needs_preprocessing(i_it->arg))
912  output_files.push_back(get_base_name(i_it->arg, true)+".o");
913  }
914  }
915  else
916  {
917  // -c is not given
918  if(cmdline.isset('o'))
919  output_files.push_back(cmdline.get_value('o'));
920  else
921  output_files.push_back("a.out");
922  }
923 
924  if(output_files.empty() ||
925  (output_files.size()==1 &&
926  output_files.front()=="/dev/null"))
927  return run_gcc(compiler);
928 
929  debug() << "Running " << native_tool_name
930  << " to generate hybrid binary" << eom;
931 
932  // save the goto-cc output files
933  std::list<std::string> goto_binaries;
934  for(std::list<std::string>::const_iterator
935  it=output_files.begin();
936  it!=output_files.end();
937  it++)
938  {
939  std::string bin_name=*it+goto_binary_tmp_suffix;
940  int result=rename(it->c_str(), bin_name.c_str());
941  if(result!=0)
942  {
943  error() << "Rename failed: " << std::strerror(errno) << eom;
944  return result;
945  }
946  goto_binaries.push_back(bin_name);
947  }
948 
949  int result=run_gcc(compiler);
950 
951  if(result==0 &&
952  cmdline.isset('T') &&
953  goto_binaries.size()==1 &&
954  output_files.size()==1)
955  {
956  linker_script_merget ls_merge(
957  compiler, output_files.front(), goto_binaries.front(),
960  }
961 
962  std::string native_tool;
963 
965  native_tool=linker_name(cmdline, base_name);
966  else if(has_suffix(compiler_name(cmdline, base_name), "-gcc"))
967  native_tool=compiler_name(cmdline, base_name);
968 
969  // merge output from gcc with goto-binaries
970  // using objcopy, or do cleanup if an earlier call failed
971  for(std::list<std::string>::const_iterator
972  it=output_files.begin();
973  it!=output_files.end();
974  it++)
975  {
976  std::string goto_binary=*it+goto_binary_tmp_suffix;
977 
978  if(result==0)
980  native_tool,
981  goto_binary,
982  *it,
985  }
986 
987  return result;
988 }
989 
991  bool act_as_bcc,
992  const std::list<std::string> &preprocessed_source_files,
993  const compilet &compiler)
994 {
995  {
996  bool have_files=false;
997 
998  for(goto_cc_cmdlinet::parsed_argvt::const_iterator
999  it=cmdline.parsed_argv.begin();
1000  it!=cmdline.parsed_argv.end();
1001  it++)
1002  if(it->is_infile_name)
1003  have_files=true;
1004 
1005  if(!have_files)
1006  return EX_OK;
1007  }
1008 
1010  {
1011  debug() << "Running " << native_tool_name
1012  << " to generate native asm output" << eom;
1013 
1014  int result=run_gcc(compiler);
1015  if(result!=0)
1016  // native tool failed
1017  return result;
1018  }
1019 
1020  std::map<std::string, std::string> output_files;
1021 
1022  if(cmdline.isset('o'))
1023  {
1024  // GCC --combine supports more than one source file
1025  for(const auto &s : preprocessed_source_files)
1026  output_files.insert(std::make_pair(s, cmdline.get_value('o')));
1027  }
1028  else
1029  {
1030  for(const std::string &s : preprocessed_source_files)
1031  output_files.insert(
1032  std::make_pair(s, get_base_name(s, true)+".s"));
1033  }
1034 
1035  if(output_files.empty() ||
1036  (output_files.size()==1 &&
1037  output_files.begin()->second=="/dev/null"))
1038  return EX_OK;
1039 
1040  debug()
1041  << "Appending preprocessed sources to generate hybrid asm output"
1042  << eom;
1043 
1044  for(const auto &so : output_files)
1045  {
1046  std::ifstream is(so.first);
1047  if(!is.is_open())
1048  {
1049  error() << "Failed to open input source " << so.first << eom;
1050  return 1;
1051  }
1052 
1053  std::ofstream os(so.second, std::ios::app);
1054  if(!os.is_open())
1055  {
1056  error() << "Failed to open output file " << so.second << eom;
1057  return 1;
1058  }
1059 
1060  const char comment=act_as_bcc ? ':' : '#';
1061 
1062  os << comment << comment << " GOTO-CC" << '\n';
1063 
1064  std::string line;
1065 
1066  while(std::getline(is, line))
1067  {
1068  os << comment << comment << line << '\n';
1069  }
1070  }
1071 
1072  return EX_OK;
1073 }
1074 
1077 {
1078  std::cout << "goto-cc understands the options of "
1079  << "gcc plus the following.\n\n";
1080 }
CBMC_VERSION
const char * CBMC_VERSION
Definition: version.cpp:1
gcc_modet::gcc_hybrid_binary
int gcc_hybrid_binary(compilet &compiler)
Definition: gcc_mode.cpp:879
get_base_name
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
Definition: get_base_name.cpp:16
tempfile.h
configt::ansi_ct::set_c99
void set_c99()
Definition: config.h:53
gcc_versiont::get
void get(const std::string &executable)
Definition: gcc_version.cpp:19
linker_script_merge.h
Merge linker script-defined symbols into a goto-program.
arith_tools.h
goto_cc_cmdlinet::parsed_argv
parsed_argvt parsed_argv
Definition: goto_cc_cmdline.h:63
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:28
compilet::source_files
std::list< std::string > source_files
Definition: compile.h:48
goto_cc_cmdlinet::stdin_file
std::string stdin_file
Definition: goto_cc_cmdline.h:74
configt::ansi_ct::wchar_t_width
std::size_t wchar_t_width
Definition: config.h:41
gcc_modet::doit
int doit() final
does it.
Definition: gcc_mode.cpp:320
pos
literalt pos(literalt a)
Definition: literal.h:194
compiler_name
static std::string compiler_name(const cmdlinet &cmdline, const std::string &base_name)
Definition: gcc_mode.cpp:53
linker_script_merget::add_linker_script_definitions
int add_linker_script_definitions()
Add values of linkerscript-defined symbols to the goto-binary.
Definition: linker_script_merge.cpp:28
gcc_modet::help_mode
void help_mode() final
display command line help
Definition: gcc_mode.cpp:1076
replace_symbol.h
gcc_modet::gcc_version
gcc_versiont gcc_version
Definition: gcc_mode.h:66
configt::ansi_ct::flavourt::VISUAL_STUDIO
@ VISUAL_STUDIO
prefix.h
compilet::doit
bool doit()
reads and source and object files, compiles and links them into goto program objects.
Definition: compile.cpp:57
file
Definition: kdev_t.h:19
invariant.h
configure_gcc
void configure_gcc(const gcc_versiont &gcc_version)
Definition: gcc_version.cpp:146
gcc_versiont::flavor
enum gcc_versiont::flavort flavor
configt::ansi_ct::set_arch_spec_i386
void set_arch_spec_i386()
Definition: config.cpp:149
run
int run(const std::string &what, const std::vector< std::string > &argv)
Definition: run.cpp:49
compilet::libraries
std::list< std::string > libraries
Definition: compile.h:50
linker_script_merget
Synthesise definitions of symbols that are defined in linker scripts.
Definition: linker_script_merge.h:63
goto_cc_modet::base_name
const std::string base_name
Definition: goto_cc_mode.h:38
messaget::eom
static eomt eom
Definition: message.h:283
gcc_modet::native_tool_name
std::string native_tool_name
Definition: gcc_mode.h:41
configt::ansi_ct::undefines
std::list< std::string > undefines
Definition: config.h:123
configt::ansi_c
struct configt::ansi_ct ansi_c
run.h
version.h
tempdir.h
has_suffix
bool has_suffix(const std::string &s, const std::string &suffix)
Definition: suffix.h:17
compilet::validate_goto_model
bool validate_goto_model
Definition: compile.h:37
gcc_modet::needs_preprocessing
static bool needs_preprocessing(const std::string &)
Definition: gcc_mode.cpp:305
gcc_versiont::default_cxx_standard
configt::cppt::cpp_standardt default_cxx_standard
Definition: gcc_version.h:40
configt::ansi_ct::flavourt::CLANG
@ CLANG
compilet::wrote_object_files
bool wrote_object_files() const
Has this compiler written any object files?
Definition: compile.h:79
compilet::COMPILE_LINK
@ COMPILE_LINK
Definition: compile.h:43
expr.h
json_parser.h
configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN
@ IS_LITTLE_ENDIAN
configt::cppt::set_cpp14
void set_cpp14()
Definition: config.h:144
cmdlinet
Definition: cmdline.h:21
compilet::output_file_object
std::string output_file_object
Definition: compile.h:58
compilet::mode
enum compilet::@3 mode
compilet::object_files
std::list< std::string > object_files
Definition: compile.h:49
messaget::result
mstreamt & result() const
Definition: message.h:395
configt::ansi_ct::set_arch_spec_arm
void set_arch_spec_arm(const irep_idt &subarch)
Definition: config.cpp:280
messaget::error
mstreamt & error() const
Definition: message.h:385
configt::ansi_ct::wchar_t_is_unsigned
bool wchar_t_is_unsigned
Definition: config.h:44
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
configt::ansi_ct::double_width
std::size_t double_width
Definition: config.h:39
gcc_modet::run_gcc
int run_gcc(const compilet &compiler)
call gcc with original command line
Definition: gcc_mode.cpp:836
compilet::COMPILE_LINK_EXECUTABLE
@ COMPILE_LINK_EXECUTABLE
Definition: compile.h:44
compilet::output_file_executable
std::string output_file_executable
Definition: compile.h:55
gcc_modet::arch_map
const std::map< std::string, std::set< std::string > > arch_map
Associate CBMC architectures with processor names.
Definition: gcc_mode.h:46
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
temp_dirt
Definition: tempdir.h:20
configt::cppt::set_cpp11
void set_cpp11()
Definition: config.h:143
messaget::M_ERROR
@ M_ERROR
Definition: message.h:167
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:46
gcc_modet::goto_binary_tmp_suffix
const std::string goto_binary_tmp_suffix
Definition: gcc_mode.h:43
compilet::library_paths
std::list< std::string > library_paths
Definition: compile.h:47
configt::this_operating_system
static irep_idt this_operating_system()
Definition: config.cpp:1375
gcc_modet::produce_hybrid_binary
const bool produce_hybrid_binary
Definition: gcc_mode.h:39
hybrid_binary
int hybrid_binary(const std::string &compiler_or_linker, const std::string &goto_binary_file, const std::string &output_file, bool building_executable, message_handlert &message_handler)
Merges a goto binary into an object file (e.g.
Definition: hybrid_binary.cpp:23
configt::ansi_ct::arch
irep_idt arch
Definition: config.h:85
compilet::object_file_extension
std::string object_file_extension
Definition: compile.h:54
get_base_name.h
goto_cc_modet::cmdline
goto_cc_cmdlinet & cmdline
Definition: goto_cc_mode.h:37
gcc_modet::asm_output
int asm_output(bool act_as_bcc, const std::list< std::string > &preprocessed_source_files, const compilet &compiler)
Definition: gcc_mode.cpp:990
read_goto_binary.h
configt::ansi_ct::preprocessor_options
std::list< std::string > preprocessor_options
Definition: config.h:124
compilet::ASSEMBLE_ONLY
@ ASSEMBLE_ONLY
Definition: compile.h:41
goto_cc_cmdlinet::have_infile_arg
bool have_infile_arg() const
Definition: goto_cc_cmdline.h:65
gcc_modet::gcc_message_handler
gcc_message_handlert gcc_message_handler
Definition: gcc_mode.h:37
compilet::add_input_file
bool add_input_file(const std::string &)
puts input file names into a list and does preprocessing for libraries.
Definition: compile.cpp:171
config
configt config
Definition: config.cpp:24
gcc_versiont::flavort::CLANG
@ CLANG
hybrid_binary.h
configt::ansi_ct::set_c11
void set_c11()
Definition: config.h:54
configt::this_architecture
static irep_idt this_architecture()
Definition: config.cpp:1275
gcc_modet::preprocess
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:758
configt::ansi_ct::mode
flavourt mode
Definition: config.h:116
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:181
configt::ansi_ct::set_c89
void set_c89()
Definition: config.h:52
compilet::COMPILE_ONLY
@ COMPILE_ONLY
Definition: compile.h:40
messaget::M_WARNING
@ M_WARNING
Definition: message.h:167
configt::ansi_ct::endiannesst::IS_BIG_ENDIAN
@ IS_BIG_ENDIAN
suffix.h
configt::set
bool set(const cmdlinet &cmdline)
Definition: config.cpp:768
configt::ansi_ct::c_standard
enum configt::ansi_ct::c_standardt c_standard
goto_cc_modet
Definition: goto_cc_mode.h:22
gcc_versiont::default_c_standard
configt::ansi_ct::c_standardt default_c_standard
Definition: gcc_version.h:39
compilet::LINK_LIBRARY
@ LINK_LIBRARY
Definition: compile.h:42
config.h
configt::ansi_ct::short_int_width
std::size_t short_int_width
Definition: config.h:35
configt::ansi_ct::char_is_unsigned
bool char_is_unsigned
Definition: config.h:44
configt::ansi_ct::flavourt::GCC
@ GCC
configt::ansi_ct::set_arch_spec_x86_64
void set_arch_spec_x86_64()
Definition: config.cpp:181
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
messaget::debug
mstreamt & debug() const
Definition: message.h:415
goto_cc_modet::help
virtual void help()
display command line help
Definition: goto_cc_mode.cpp:47
configt::ansi_ct::single_width
std::size_t single_width
Definition: config.h:38
compilet::PREPROCESS_ONLY
@ PREPROCESS_ONLY
Definition: compile.h:39
configt::cpp
struct configt::cppt cpp
compilet
Definition: compile.h:27
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:58
messaget::eval_verbosity
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:99
configt::ansi_ct::single_precision_constant
bool single_precision_constant
Definition: config.h:48
cmdlinet::get_values
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:107
comment
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:109
linker_name
static std::string linker_name(const cmdlinet &cmdline, const std::string &base_name)
Definition: gcc_mode.cpp:86
goto_cc_cmdlinet
Definition: goto_cc_cmdline.h:20
configt::set_arch
void set_arch(const irep_idt &)
Definition: config.cpp:702
c_types.h
compilet::cprover_macro_arities
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
configt::ansi_ct::endianness
endiannesst endianness
Definition: config.h:77
validation_modet::INVARIANT
@ INVARIANT
gcc_mode.h
gcc_modet::gcc_modet
gcc_modet(goto_cc_cmdlinet &_cmdline, const std::string &_base_name, bool _produce_hybrid_binary)
Definition: gcc_mode.cpp:106
configt::cppt::cpp_standard
enum configt::cppt::cpp_standardt cpp_standard