Skip to content

Commit 63ad1d5

Browse files
committed
Switch to standalone sysroot for semantics compilation
1 parent a14763d commit 63ad1d5

File tree

26 files changed

+543
-62
lines changed

26 files changed

+543
-62
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ compile_commands.json
2828
.cache
2929
deps/*
3030
third_party/*
31-
build/*
31+
build*/
3232
remill-build/*
3333
generated/*
3434
gtest_build/*

cmake/BCCompiler.cmake

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
# compiler detection
33
#
44

5-
if(DEFINED CMAKE_OSX_SYSROOT)
6-
set(EXTRA_BC_SYSROOT -isysroot ${CMAKE_OSX_SYSROOT})
7-
endif()
8-
5+
# NOTE: This is a fake sysroot, with just enough to build the semantics
6+
set(BC_SYSROOT "${PROJECT_SOURCE_DIR}/include/remill/Arch/Runtime/sysroot")
97
set(DEFAULT_BC_COMPILER_FLAGS
8+
"--sysroot=${BC_SYSROOT}"
9+
-nostdinc++
10+
-isystem "${BC_SYSROOT}"
11+
1012
-emit-llvm -Wno-unknown-warning-option -Wall -Wshadow
1113
-Wconversion -Wpadded -pedantic -Wshorten-64-to-32 -Wgnu-alignof-expression
1214
-Wno-gnu-anonymous-struct -Wno-return-type-c-linkage
@@ -19,7 +21,6 @@ set(DEFAULT_BC_COMPILER_FLAGS
1921
-Wno-unused-function -Wgnu-inline-cpp-without-extern
2022
-Wno-pass-failed=transform-warning
2123
-std=c++17
22-
${EXTRA_BC_SYSROOT}
2324
)
2425

2526
find_package(Clang CONFIG REQUIRED)
@@ -52,6 +53,8 @@ endif()
5253
set(add_runtime_usage "add_runtime(target_name SOURCES <src1 src2> ADDRESS_SIZE <size> DEFINITIONS <def1 def2> BCFLAGS <bcflag1 bcflag2> LINKERFLAGS <lnkflag1 lnkflag2> INCLUDEDIRECTORIES <path1 path2> INSTALLDESTINATION <path> DEPENDENCIES <dependency1 dependency2>")
5354

5455
function(add_runtime target_name)
56+
set(BUILD_COMMANDS "")
57+
5558
if(NOT DEFINED CMAKE_BC_COMPILER)
5659
message(FATAL_ERROR "The bitcode compiler was not found!")
5760
endif()
@@ -176,28 +179,22 @@ function(add_runtime target_name)
176179
set(additional_windows_settings "-D_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH")
177180
endif()
178181

179-
# The hyper call implementation contains inline assembly for each architecture so we'll need to
180-
# cross-compile for the runtime architecture.
181-
if(${source_file} STREQUAL ${hyper_call_source})
182-
# Some architectures add an explicit target for the host to successfully
183-
# compile with 32 bits (like AArch64 to arm), however, we don't want that
184-
# to interfere with the hyper call crosscompile
185-
list(FILTER bc_flag_list EXCLUDE REGEX "--target=.*")
186-
set(target_decl "-target" "${arch}-none-eabi")
187-
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
188-
set(target_decl "-target" "x86_64-apple-macosx11.0.0")
182+
# Only arm32 has eabihf (hard float)
183+
if("${arch}" STREQUAL "arm")
184+
set(target_decl "-target" "${arch}-none-eabihf")
189185
else()
190-
unset(target_decl)
186+
set(target_decl "-target" "${arch}-none-elf")
191187
endif()
192188

193-
194189
add_custom_command(OUTPUT "${absolute_output_file_path}"
195-
COMMAND "${CMAKE_BC_COMPILER}" ${include_directory_list} ${additional_windows_settings} ${target_decl} "-DADDRESS_SIZE_BITS=${address_size}" ${definition_list} ${DEFAULT_BC_COMPILER_FLAGS} ${bc_flag_list} ${source_file_option_list} -c "${absolute_source_file_path}" -o "${absolute_output_file_path}"
190+
COMMAND "${CMAKE_BC_COMPILER}" ${include_directory_list} ${additional_windows_settings} ${target_decl} "-DADDRESS_SIZE_BITS=${address_size}" ${definition_list} ${DEFAULT_BC_COMPILER_FLAGS} ${bc_flag_list} ${source_file_option_list} -c "${absolute_source_file_path}" -o "${absolute_output_file_path}"
196191
MAIN_DEPENDENCY "${absolute_source_file_path}"
197192
${dependency_list_directive}
198-
COMMENT "Building BC object ${absolute_output_file_path}"
193+
COMMENT "Building BC object: \"${CMAKE_BC_COMPILER}\" ${include_directory_list} ${additional_windows_settings} ${target_decl} \"-DADDRESS_SIZE_BITS=${address_size}\" ${definition_list} ${DEFAULT_BC_COMPILER_FLAGS} ${bc_flag_list} ${source_file_option_list} -c \"${absolute_source_file_path}\" -o \"${absolute_output_file_path}\""
199194
)
200195

196+
set(BUILD_COMMANDS "${BUILD_COMMANDS}\"${CMAKE_BC_COMPILER}\" ${include_directory_list} ${additional_windows_settings} ${target_decl} \"-DADDRESS_SIZE_BITS=${address_size}\" ${definition_list} ${DEFAULT_BC_COMPILER_FLAGS} ${bc_flag_list} ${source_file_option_list} -c \"${absolute_source_file_path}\" -o \"${absolute_output_file_path}\"\n")
197+
201198
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${absolute_output_file_path}")
202199
list(APPEND bitcode_file_list "${absolute_output_file_path}")
203200
endforeach()
@@ -213,6 +210,9 @@ function(add_runtime target_name)
213210
DEPENDS ${bitcode_file_list}
214211
COMMENT "Linking BC runtime ${absolute_target_path}"
215212
)
213+
set(BUILD_COMMANDS "${BUILD_COMMANDS}\"${CMAKE_BC_LINKER}\" ${linker_flag_list} ${bitcode_file_list} -o \"${absolute_target_path}\"\n")
214+
string(REPLACE ";" " " BUILD_COMMANDS "${BUILD_COMMANDS}")
215+
file(WRITE "${CMAKE_BINARY_DIR}/runtimes/${target_name}.txt" "${BUILD_COMMANDS}")
216216

217217
set(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${absolute_target_path}")
218218

include/remill/Arch/Instruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Arch;
3535
struct Register;
3636
class OperandExpression;
3737

38-
enum ArchName : unsigned;
38+
enum ArchName : uint32_t;
3939

4040
struct LLVMOpExpr {
4141
unsigned llvm_opcode;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
namespace std {
4+
5+
// http://www.en.cppreference.com/w/cpp/algorithm/min.html
6+
template <class T>
7+
const T &min(const T &a, const T &b) {
8+
return (b < a) ? b : a;
9+
}
10+
11+
// http://www.en.cppreference.com/w/cpp/algorithm/max.html
12+
template <class T>
13+
const T &max(const T &a, const T &b) {
14+
return (a < b) ? b : a;
15+
}
16+
17+
} // namespace std
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#pragma once
2+
3+
#ifndef __clang__
4+
# error Unsupported compiler!
5+
#endif
6+
7+
// NOTE: These values have to match the hardware for correct semantics
8+
9+
#if defined(__arm__)
10+
11+
# define FE_INVALID 1
12+
# define FE_DIVBYZERO 2
13+
# define FE_OVERFLOW 4
14+
# define FE_UNDERFLOW 8
15+
# define FE_INEXACT 16
16+
# define FE_ALL_EXCEPT 31
17+
18+
# define FE_TONEAREST 0
19+
# define FE_DOWNWARD 0x800000
20+
# define FE_UPWARD 0x400000
21+
# define FE_TOWARDZERO 0xc00000
22+
23+
#elif defined(__aarch64__)
24+
25+
# define FE_INVALID 1
26+
# define FE_DIVBYZERO 2
27+
# define FE_OVERFLOW 4
28+
# define FE_UNDERFLOW 8
29+
# define FE_INEXACT 16
30+
# define FE_ALL_EXCEPT 31
31+
32+
# define FE_TONEAREST 0
33+
# define FE_DOWNWARD 0x800000
34+
# define FE_UPWARD 0x400000
35+
# define FE_TOWARDZERO 0xc00000
36+
37+
#elif defined(__powerpc__)
38+
39+
# define FE_INEXACT 0x02000000
40+
# define FE_DIVBYZERO 0x04000000
41+
# define FE_UNDERFLOW 0x08000000
42+
# define FE_OVERFLOW 0x10000000
43+
# define FE_INVALID 0x20000000
44+
# define FE_ALL_EXCEPT 0x3e000000
45+
46+
# define FE_TONEAREST 0
47+
# define FE_TOWARDZERO 1
48+
# define FE_UPWARD 2
49+
# define FE_DOWNWARD 3
50+
51+
#elif defined(__sparc__)
52+
53+
# define FE_INVALID (1 << 9)
54+
# define FE_OVERFLOW (1 << 8)
55+
# define FE_UNDERFLOW (1 << 7)
56+
# define FE_DIVBYZERO (1 << 6)
57+
# define FE_INEXACT (1 << 5)
58+
# define FE_ALL_EXCEPT \
59+
(FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)
60+
61+
# define FE_TONEAREST (0 << 30)
62+
# define FE_TOWARDZERO (1 << 30)
63+
# define FE_UPWARD (-0x7fffffff - 1) /* (2 << 30) */
64+
# define FE_DOWNWARD (-0x40000000) /* (3 << 30) */
65+
66+
#elif defined(__sparc64__)
67+
68+
# define FE_INVALID (1 << 9)
69+
# define FE_OVERFLOW (1 << 8)
70+
# define FE_UNDERFLOW (1 << 7)
71+
# define FE_DIVBYZERO (1 << 6)
72+
# define FE_INEXACT (1 << 5)
73+
# define FE_ALL_EXCEPT \
74+
(FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)
75+
76+
# define FE_TONEAREST (0 << 30)
77+
# define FE_TOWARDZERO (1 << 30)
78+
# define FE_UPWARD (-0x7fffffff - 1) /* (2 << 30) */
79+
# define FE_DOWNWARD (-0x40000000) /* (3 << 30) */
80+
81+
#elif defined(__i386__)
82+
83+
# define FE_INVALID 1
84+
# define __FE_DENORM 2
85+
# define FE_DIVBYZERO 4
86+
# define FE_OVERFLOW 8
87+
# define FE_UNDERFLOW 16
88+
# define FE_INEXACT 32
89+
90+
# define FE_ALL_EXCEPT 63
91+
92+
# define FE_TONEAREST 0
93+
# define FE_DOWNWARD 0x400
94+
# define FE_UPWARD 0x800
95+
# define FE_TOWARDZERO 0xc00
96+
97+
#elif defined(__x86_64__)
98+
99+
# define FE_INVALID 1
100+
# define __FE_DENORM 2
101+
# define FE_DIVBYZERO 4
102+
# define FE_OVERFLOW 8
103+
# define FE_UNDERFLOW 16
104+
# define FE_INEXACT 32
105+
106+
# define FE_ALL_EXCEPT 63
107+
108+
# define FE_TONEAREST 0
109+
# define FE_DOWNWARD 0x400
110+
# define FE_UPWARD 0x800
111+
# define FE_TOWARDZERO 0xc00
112+
113+
#else
114+
# error Unsupported architecture!
115+
#endif
116+
117+
// TODO: actually implement these functions?
118+
119+
namespace std {
120+
121+
// https://en.cppreference.com/w/cpp/numeric/fenv/feround
122+
int fesetround(int round);
123+
int fegetround();
124+
125+
// https://en.cppreference.com/w/cpp/numeric/fenv/fetestexcept
126+
int fetestexcept(int excepts);
127+
128+
// https://en.cppreference.com/w/c/numeric/fenv/feraiseexcept
129+
int feraiseexcept(int excepts);
130+
131+
// https://en.cppreference.com/w/c/numeric/fenv/feclearexcept.html
132+
int feclearexcept(int excepts);
133+
134+
} // namespace std
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#pragma once
2+
3+
// https://en.cppreference.com/w/cpp/numeric/math/FP_categories
4+
#define FP_NAN 0
5+
#define FP_INFINITE 1
6+
#define FP_ZERO 2
7+
#define FP_SUBNORMAL 3
8+
#define FP_NORMAL 4
9+
10+
// https://en.cppreference.com/w/cpp/numeric/math/NAN
11+
#define NAN (__builtin_nanf(""))
12+
13+
namespace std {
14+
15+
// https://en.cppreference.com/w/cpp/numeric/math/signbit
16+
constexpr bool signbit(float __x) {
17+
return __builtin_signbit(__x);
18+
}
19+
20+
constexpr bool signbit(double __x) {
21+
return __builtin_signbit(__x);
22+
}
23+
24+
constexpr bool signbit(long double __x) {
25+
return __builtin_signbit(__x);
26+
}
27+
28+
// https://en.cppreference.com/w/cpp/numeric/math/fpclassify
29+
constexpr int fpclassify(float __x) {
30+
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
31+
FP_ZERO, __x);
32+
}
33+
34+
constexpr int fpclassify(double __x) {
35+
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
36+
FP_ZERO, __x);
37+
}
38+
39+
constexpr int fpclassify(long double __x) {
40+
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
41+
FP_ZERO, __x);
42+
}
43+
44+
// https://en.cppreference.com/w/cpp/numeric/math/isnan
45+
constexpr bool isnan(float num) {
46+
return __builtin_isnan(num);
47+
}
48+
49+
constexpr bool isnan(double num) {
50+
return __builtin_isnan(num);
51+
}
52+
53+
constexpr bool isnan(long double num) {
54+
return __builtin_isnan(num);
55+
}
56+
57+
// https://en.cppreference.com/w/cpp/numeric/math/fabs
58+
constexpr float fabs(float num) {
59+
return __builtin_fabsf(num);
60+
}
61+
62+
constexpr double fabs(double num) {
63+
return __builtin_fabs(num);
64+
}
65+
66+
constexpr long double fabs(long double num) {
67+
return __builtin_fabsl(num);
68+
}
69+
70+
// https://en.cppreference.com/w/cpp/numeric/math/sqrt
71+
constexpr float sqrt(float num) {
72+
return __builtin_sqrtf(num);
73+
}
74+
75+
constexpr double sqrt(double num) {
76+
return __builtin_sqrt(num);
77+
}
78+
79+
constexpr long double sqrt(long double num) {
80+
return __builtin_sqrtl(num);
81+
}
82+
83+
// https://en.cppreference.com/w/cpp/numeric/math/isunordered
84+
constexpr bool isunordered(float x, float y) {
85+
return __builtin_isunordered(x, y);
86+
}
87+
88+
constexpr bool isunordered(double x, double y) {
89+
return __builtin_isunordered(x, y);
90+
}
91+
92+
constexpr bool isunordered(long double x, long double y) {
93+
return __builtin_isunordered(x, y);
94+
}
95+
96+
} // namespace std
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
5+
namespace std {
6+
7+
using size_t = ::size_t;
8+
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include <stdint.h>

0 commit comments

Comments
 (0)