Skip to content

Commit 104a704

Browse files
authored
Merge pull request #3867 from tautschnig/osx-hybrid-binaries
goto-gcc hybrid binaries on OS X
2 parents 3d94a62 + c7e8bcc commit 104a704

15 files changed

+427
-27
lines changed

regression/goto-gcc/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22
# isn't marked as an executable (target), which CMake requires. Thus construct a
33
# path in the same way the symbolic link is created in the goto-cc directory.
44
add_test_pl_tests("$<TARGET_FILE_DIR:goto-cc>/goto-gcc")
5+
6+
add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/archives/libour_archive.a"
7+
COMMAND "$<TARGET_FILE_DIR:goto-cc>/goto-gcc" -c foo.c
8+
COMMAND ${CMAKE_AR} rcs libour_archive.a foo.o
9+
DEPENDS
10+
"$<TARGET_FILE_DIR:goto-cc>/goto-gcc"
11+
"${CMAKE_CURRENT_SOURCE_DIR}/archives/foo.c"
12+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/archives"
13+
)
14+
15+
add_custom_target(libour_archive.a ALL
16+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/archives/libour_archive.a"
17+
)

regression/goto-gcc/Makefile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ test:
99
tests.log: ../test.pl
1010

1111
else
12-
test:
13-
-@ln -s goto-cc ../../src/goto-cc/goto-gcc
12+
test: ../../src/goto-cc/goto-gcc
1413
@../test.pl -e -p -c ../../../src/goto-cc/goto-gcc
1514

16-
tests.log: ../test.pl
17-
-@ln -s goto-cc ../../src/goto-cc/goto-gcc
15+
tests.log: ../test.pl ../../src/goto-cc/goto-gcc
1816
@../test.pl -e -p -c ../../../src/goto-cc/goto-gcc
1917

18+
../../src/goto-cc/goto-gcc: ../../src/goto-cc/goto-cc
19+
@ln -sf goto-cc ../../src/goto-cc/goto-gcc
20+
21+
test tests.log: archives/libour_archive.a
22+
archives/libour_archive.a: archives/foo.c ../../src/goto-cc/goto-gcc
23+
@cd archives && \
24+
../../../src/goto-cc/goto-gcc -c foo.c && \
25+
$(AR) rcs libour_archive.a foo.o
2026
endif
2127

2228
show:
@@ -30,4 +36,4 @@ clean:
3036
find -name '*.out' -execdir $(RM) '{}' \;
3137
find -name '*.gb' -execdir $(RM) '{}' \;
3238
find -name '*.goto-cc-saved' -execdir $(RM) '{}' \;
33-
$(RM) tests.log
39+
$(RM) tests.log archives/lib_ourarchive.a archives/foo.o

regression/goto-gcc/archives/foo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int foo()
2+
{
3+
return 0;
4+
}

regression/goto-gcc/archives/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int foo();
2+
3+
int main()
4+
{
5+
return foo();
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
10
3+
main.c -L. -lour_archive --verbosity
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^Reading: .*/foo.o$
7+
--
8+
^warning: ignoring
9+
^CONVERSION ERROR$
10+
--
11+
We need to make sure main.c is listed before the archive for the Linux linker to
12+
pick up dependencies - thus "main.c" is given as an argument on line 3 in this
13+
file and the "10" in line 2 is a dummy to be appended at the end, setting the
14+
verbosity as needed.

src/goto-cc/as_mode.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ int as_modet::doit()
241241
// We can generate hybrid ELF and Mach-O binaries
242242
// containing both executable machine code and the goto-binary.
243243
if(produce_hybrid_binary)
244-
return as_hybrid_binary();
244+
return as_hybrid_binary(compiler);
245245

246246
return EX_OK;
247247
}
@@ -270,7 +270,7 @@ int as_modet::run_as()
270270
return run(new_argv[0], new_argv, cmdline.stdin_file, "", "");
271271
}
272272

273-
int as_modet::as_hybrid_binary()
273+
int as_modet::as_hybrid_binary(const compilet &compiler)
274274
{
275275
std::string output_file="a.out";
276276

@@ -301,7 +301,11 @@ int as_modet::as_hybrid_binary()
301301
if(result == 0)
302302
{
303303
result = hybrid_binary(
304-
native_tool_name, saved, output_file, get_message_handler());
304+
native_tool_name,
305+
saved,
306+
output_file,
307+
compiler.mode == compilet::COMPILE_LINK_EXECUTABLE,
308+
get_message_handler());
305309
}
306310

307311
return result;

src/goto-cc/as_mode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Date: July 2016
1717
#include "gcc_message_handler.h"
1818
#include "goto_cc_mode.h"
1919

20+
class compilet;
21+
2022
class as_modet:public goto_cc_modet
2123
{
2224
public:
@@ -35,7 +37,7 @@ class as_modet:public goto_cc_modet
3537

3638
int run_as(); // call as with original command line
3739

38-
int as_hybrid_binary();
40+
int as_hybrid_binary(const compilet &compiler);
3941
};
4042

4143
#endif // CPROVER_GOTO_CC_AS_MODE_H

src/goto-cc/gcc_mode.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,11 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
985985

986986
if(result==0)
987987
result = hybrid_binary(
988-
native_tool, goto_binary, *it, get_message_handler());
988+
native_tool,
989+
goto_binary,
990+
*it,
991+
compiler.mode == compilet::COMPILE_LINK_EXECUTABLE,
992+
get_message_handler());
989993
}
990994

991995
return result;

src/goto-cc/hybrid_binary.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ int hybrid_binary(
2020
const std::string &compiler_or_linker,
2121
const std::string &goto_binary_file,
2222
const std::string &output_file,
23+
bool building_executable,
2324
message_handlert &message_handler)
2425
{
2526
messaget message(message_handler);
2627

2728
int result;
2829

2930
#if defined(__linux__) || defined(__FreeBSD_kernel__)
31+
// we can use objcopy for both object files and executables
32+
(void)building_executable;
33+
3034
std::string objcopy_cmd;
3135

3236
if(has_suffix(compiler_or_linker, "-ld"))
@@ -69,9 +73,10 @@ int hybrid_binary(
6973
// Mac
7074

7175
message.debug() << "merging " << output_file << " and " << goto_binary_file
72-
<< " using lipo"
76+
<< " using " << (building_executable ? "lipo" : "ld")
7377
<< messaget::eom;
7478

79+
if(building_executable)
7580
{
7681
// Add goto-binary as hppa7100LC section.
7782
// This overwrites if there's already one.
@@ -81,6 +86,21 @@ int hybrid_binary(
8186

8287
result = run(lipo_argv[0], lipo_argv);
8388
}
89+
else
90+
{
91+
// This fails if there's already one.
92+
std::vector<std::string> ld_argv = {"ld",
93+
"-r",
94+
"-sectcreate",
95+
"__TEXT",
96+
"goto-cc",
97+
goto_binary_file,
98+
output_file,
99+
"-o",
100+
output_file};
101+
102+
result = run(ld_argv[0], ld_argv);
103+
}
84104

85105
// delete the goto binary
86106
int remove_result = remove(goto_binary_file.c_str());
@@ -97,6 +117,7 @@ int hybrid_binary(
97117
(void)compiler_or_linker;
98118
(void)goto_binary_file;
99119
(void)output_file;
120+
(void)building_executable;
100121
message.error() << "binary merging not implemented for this platform"
101122
<< messaget::eom;
102123
result = 1;

src/goto-cc/hybrid_binary.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ Date: May 2018
2323
/// deduce the name of objcopy
2424
/// \param goto_binary_file: The file name of the goto binary
2525
/// \param output_file: The name of the object file; the result is stored here.
26+
/// \param building_executable: The \p output_file is an executable.
27+
/// \param message_handler: Message handler for output.
2628
int hybrid_binary(
2729
const std::string &compiler_or_linker,
2830
const std::string &goto_binary_file,
2931
const std::string &output_file,
30-
message_handlert &);
32+
bool building_executable,
33+
message_handlert &message_handler);
3134

3235
#endif // CPROVER_GOTO_CC_HYBRID_BINARY_H

src/goto-cc/ld_mode.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ int ld_modet::ld_hybrid_binary(compilet &compiler)
212212
std::string native_linker = linker_name(cmdline, base_name);
213213

214214
result = hybrid_binary(
215-
native_linker, goto_binary, output_file, get_message_handler());
215+
native_linker,
216+
goto_binary,
217+
output_file,
218+
compiler.mode == compilet::COMPILE_LINK_EXECUTABLE,
219+
get_message_handler());
216220
}
217221

218222
return result;

src/goto-programs/module_dependencies.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
analyses # dubious - concerns call_graph and does_remove_const
22
ansi-c # for GraphML witnesses
3+
architecture # system
34
goto-programs
45
goto-symex # dubious - spurious inclusion of symex_target_equation in graphml_witness
56
json

0 commit comments

Comments
 (0)