Skip to content

Commit b7f5886

Browse files
authored
Merge pull request diffblue#2241 from diffblue/ld_mode
goto-cc: add separate path for ld
2 parents 5c11eb7 + 4c2cb3a commit b7f5886

13 files changed

+468
-143
lines changed

src/goto-cc/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ SRC = armcc_cmdline.cpp \
1212
goto_cc_languages.cpp \
1313
goto_cc_main.cpp \
1414
goto_cc_mode.cpp \
15+
hybrid_binary.cpp \
1516
ld_cmdline.cpp \
17+
ld_mode.cpp \
1618
linker_script_merge.cpp \
1719
ms_cl_cmdline.cpp \
1820
ms_cl_mode.cpp \

src/goto-cc/compile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ compilet::~compilet()
662662
delete_directory(*it);
663663
}
664664

665-
unsigned compilet::function_body_count(const goto_functionst &functions)
665+
unsigned compilet::function_body_count(const goto_functionst &functions) const
666666
{
667667
int fbs=0;
668668

src/goto-cc/compile.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class compilet:public language_uit
9494
cmdlinet &cmdline;
9595
bool warning_is_fatal;
9696

97-
unsigned function_body_count(const goto_functionst &);
97+
unsigned function_body_count(const goto_functionst &) const;
9898

9999
void add_compiler_specific_defines(class configt &config) const;
100100

src/goto-cc/gcc_mode.cpp

+27-114
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Author: CM Wintersteiger, 2006
4848

4949
#include <cbmc/version.h>
5050

51+
#include "hybrid_binary.h"
5152
#include "linker_script_merge.h"
5253

5354
static std::string compiler_name(
@@ -106,8 +107,6 @@ gcc_modet::gcc_modet(
106107
bool _produce_hybrid_binary):
107108
goto_cc_modet(_cmdline, _base_name, gcc_message_handler),
108109
produce_hybrid_binary(_produce_hybrid_binary),
109-
act_as_ld(base_name=="ld" ||
110-
base_name.find("goto-ld")!=std::string::npos),
111110
goto_binary_tmp_suffix(".goto-cc-saved"),
112111

113112
// Keys are architectures specified in configt::set_arch().
@@ -326,8 +325,6 @@ int gcc_modet::doit()
326325
}
327326

328327
native_tool_name=
329-
act_as_ld ?
330-
linker_name(cmdline, base_name) :
331328
compiler_name(cmdline, base_name);
332329

333330
auto default_verbosity = (cmdline.isset("Wall") || cmdline.isset("Wextra")) ?
@@ -345,10 +342,7 @@ int gcc_modet::doit()
345342
// "-v" a) prints the version and b) increases verbosity.
346343
// Compilation continues, don't exit!
347344

348-
if(act_as_ld)
349-
std::cout << "GNU ld version 2.16.91 20050610 (goto-cc " CBMC_VERSION
350-
<< ")\n";
351-
else if(act_as_bcc)
345+
if(act_as_bcc)
352346
std::cout << "bcc: version 0.16.17 (goto-cc " CBMC_VERSION ")\n";
353347
else
354348
std::cout << "gcc version 3.4.4 (goto-cc " CBMC_VERSION ")\n";
@@ -395,14 +389,7 @@ int gcc_modet::doit()
395389
return EX_OK;
396390
}
397391

398-
if(act_as_ld)
399-
{
400-
if(produce_hybrid_binary)
401-
debug() << "LD mode (hybrid)" << eom;
402-
else
403-
debug() << "LD mode" << eom;
404-
}
405-
else if(act_as_bcc)
392+
if(act_as_bcc)
406393
{
407394
if(produce_hybrid_binary)
408395
debug() << "BCC mode (hybrid)" << eom;
@@ -418,9 +405,7 @@ int gcc_modet::doit()
418405
}
419406

420407
// determine actions to be undertaken
421-
if(act_as_ld)
422-
compiler.mode=compilet::LINK_LIBRARY;
423-
else if(cmdline.isset('S'))
408+
if(cmdline.isset('S'))
424409
compiler.mode=compilet::ASSEMBLE_ONLY;
425410
else if(cmdline.isset('c'))
426411
compiler.mode=compilet::COMPILE_ONLY;
@@ -437,13 +422,10 @@ int gcc_modet::doit()
437422
// * preprocessing (-E)
438423
// * no input files given
439424

440-
if(act_as_ld)
441-
{
442-
}
443-
else if(cmdline.isset('M') ||
444-
cmdline.isset("MM") ||
445-
cmdline.isset('E') ||
446-
!cmdline.have_infile_arg())
425+
if(cmdline.isset('M') ||
426+
cmdline.isset("MM") ||
427+
cmdline.isset('E') ||
428+
!cmdline.have_infile_arg())
447429
return run_gcc(compiler); // exit!
448430

449431
// get configuration
@@ -819,7 +801,7 @@ int gcc_modet::run_gcc(const compilet &compiler)
819801
for(const auto &a : cmdline.parsed_argv)
820802
new_argv.push_back(a.arg);
821803

822-
if(!act_as_ld && compiler.wrote_object_files())
804+
if(compiler.wrote_object_files())
823805
{
824806
// Undefine all __CPROVER macros for the system compiler
825807
std::map<irep_idt, std::size_t> arities;
@@ -922,103 +904,38 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
922904
goto_binaries.push_back(bin_name);
923905
}
924906

925-
std::string objcopy_cmd;
926-
if(has_suffix(linker_name(cmdline, base_name), "-ld"))
927-
{
928-
objcopy_cmd=linker_name(cmdline, base_name);
929-
objcopy_cmd.erase(objcopy_cmd.size()-2);
930-
}
931-
else if(has_suffix(compiler_name(cmdline, base_name), "-gcc"))
932-
{
933-
objcopy_cmd=compiler_name(cmdline, base_name);
934-
objcopy_cmd.erase(objcopy_cmd.size()-3);
935-
}
936-
objcopy_cmd+="objcopy";
937-
938907
int result=run_gcc(compiler);
939908

940-
if(result==0)
909+
if(result==0 &&
910+
cmdline.isset('T') &&
911+
goto_binaries.size()==1 &&
912+
output_files.size()==1)
941913
{
942914
linker_script_merget ls_merge(
943-
compiler, output_files, goto_binaries, cmdline, gcc_message_handler);
915+
compiler, output_files.front(), goto_binaries.front(),
916+
cmdline, gcc_message_handler);
944917
result=ls_merge.add_linker_script_definitions();
945918
}
946919

920+
std::string native_tool;
921+
922+
if(has_suffix(linker_name(cmdline, base_name), "-ld"))
923+
native_tool=linker_name(cmdline, base_name);
924+
else if(has_suffix(compiler_name(cmdline, base_name), "-gcc"))
925+
native_tool=compiler_name(cmdline, base_name);
926+
947927
// merge output from gcc with goto-binaries
948928
// using objcopy, or do cleanup if an earlier call failed
949929
for(std::list<std::string>::const_iterator
950930
it=output_files.begin();
951931
it!=output_files.end();
952932
it++)
953933
{
954-
debug() << "merging " << *it << eom;
955-
std::string saved=*it+goto_binary_tmp_suffix;
956-
957-
#ifdef __linux__
958-
if(result==0 && !cmdline.isset('c'))
959-
{
960-
// remove any existing goto-cc section
961-
std::vector<std::string> objcopy_argv;
962-
963-
objcopy_argv.push_back(objcopy_cmd);
964-
objcopy_argv.push_back("--remove-section=goto-cc");
965-
objcopy_argv.push_back(*it);
934+
std::string goto_binary=*it+goto_binary_tmp_suffix;
966935

967-
result=run(objcopy_argv[0], objcopy_argv, "", "");
968-
}
969-
970-
if(result==0)
971-
{
972-
// now add goto-binary as goto-cc section
973-
std::vector<std::string> objcopy_argv;
974-
975-
objcopy_argv.push_back(objcopy_cmd);
976-
objcopy_argv.push_back("--add-section");
977-
objcopy_argv.push_back("goto-cc="+saved);
978-
objcopy_argv.push_back(*it);
979-
980-
result=run(objcopy_argv[0], objcopy_argv, "", "");
981-
}
982-
983-
int remove_result=remove(saved.c_str());
984-
if(remove_result!=0)
985-
{
986-
error() << "Remove failed: " << std::strerror(errno) << eom;
987-
if(result==0)
988-
result=remove_result;
989-
}
990-
991-
#elif defined(__APPLE__)
992-
// Mac
993936
if(result==0)
994-
{
995-
std::vector<std::string> lipo_argv;
996-
997-
// now add goto-binary as hppa7100LC section
998-
lipo_argv.push_back("lipo");
999-
lipo_argv.push_back(*it);
1000-
lipo_argv.push_back("-create");
1001-
lipo_argv.push_back("-arch");
1002-
lipo_argv.push_back("hppa7100LC");
1003-
lipo_argv.push_back(saved);
1004-
lipo_argv.push_back("-output");
1005-
lipo_argv.push_back(*it);
1006-
1007-
result=run(lipo_argv[0], lipo_argv, "", "");
1008-
}
1009-
1010-
int remove_result=remove(saved.c_str());
1011-
if(remove_result!=0)
1012-
{
1013-
error() << "Remove failed: " << std::strerror(errno) << eom;
1014-
if(result==0)
1015-
result=remove_result;
1016-
}
1017-
1018-
#else
1019-
error() << "binary merging not implemented for this platform" << eom;
1020-
return 1;
1021-
#endif
937+
result = hybrid_binary(
938+
native_tool, goto_binary, *it, get_message_handler());
1022939
}
1023940

1024941
return result;
@@ -1112,10 +1029,6 @@ int gcc_modet::asm_output(
11121029
/// display command line help
11131030
void gcc_modet::help_mode()
11141031
{
1115-
if(act_as_ld)
1116-
std::cout << "goto-ld understands the options of "
1117-
<< "ld plus the following.\n\n";
1118-
else
1119-
std::cout << "goto-cc understands the options of "
1120-
<< "gcc plus the following.\n\n";
1032+
std::cout << "goto-cc understands the options of "
1033+
<< "gcc plus the following.\n\n";
11211034
}

src/goto-cc/gcc_mode.h

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class gcc_modet:public goto_cc_modet
3737

3838
const bool produce_hybrid_binary;
3939

40-
const bool act_as_ld;
4140
std::string native_tool_name;
4241

4342
const std::string goto_binary_tmp_suffix;

src/goto-cc/goto_cc_main.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ Date: May 2006
2525
#include "as_cmdline.h"
2626
#include "as86_cmdline.h"
2727

28-
#include "gcc_mode.h"
29-
#include "cw_mode.h"
30-
#include "ms_cl_mode.h"
3128
#include "armcc_mode.h"
3229
#include "as_mode.h"
30+
#include "cw_mode.h"
31+
#include "gcc_mode.h"
32+
#include "ld_mode.h"
33+
#include "ms_cl_mode.h"
3334

3435
std::string to_lower_string(const std::string &s)
3536
{
@@ -106,8 +107,8 @@ int main(int argc, const char **argv)
106107
{
107108
// this simulates "ld" for linking
108109
ld_cmdlinet cmdline;
109-
gcc_modet gcc_mode(cmdline, base_name, true);
110-
return gcc_mode.main(argc, argv);
110+
ld_modet ld_mode(cmdline, base_name);
111+
return ld_mode.main(argc, argv);
111112
}
112113
else if(base_name.find("goto-bcc")!=std::string::npos)
113114
{

src/goto-cc/goto_cc_mode.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ void goto_cc_modet::help()
4747
std::cout <<
4848
"\n"
4949
// NOLINTNEXTLINE(whitespace/line_length)
50-
"* * goto-cc " CBMC_VERSION " - Copyright (C) 2006-2014 * *\n"
51-
"* * Daniel Kroening, Christoph Wintersteiger * *\n"
52-
"* * [email protected] * *\n"
50+
"* * goto-cc " CBMC_VERSION " - Copyright (C) 2006-2018 * *\n"
51+
"* * Daniel Kroening, Michael Tautschnig, * *\n"
52+
"* * Christoph Wintersteiger * *\n"
5353
"\n";
5454

5555
help_mode();

0 commit comments

Comments
 (0)