1# Build for the undefined behavior sanitizer runtime support library.
2
3set(UBSAN_SOURCES
4  ubsan_diag.cpp
5  ubsan_init.cpp
6  ubsan_flags.cpp
7  ubsan_handlers.cpp
8  ubsan_monitor.cpp
9  ubsan_value.cpp
10  )
11
12set(UBSAN_STANDALONE_SOURCES
13  ubsan_diag_standalone.cpp
14  ubsan_init_standalone.cpp
15  ubsan_signals_standalone.cpp
16  )
17
18set(UBSAN_CXXABI_SOURCES
19  ubsan_handlers_cxx.cpp
20  ubsan_type_hash.cpp
21  ubsan_type_hash_itanium.cpp
22  ubsan_type_hash_win.cpp
23 )
24
25set(UBSAN_HEADERS
26  ubsan_checks.inc
27  ubsan_diag.h
28  ubsan_flags.h
29  ubsan_flags.inc
30  ubsan_handlers.h
31  ubsan_handlers_cxx.h
32  ubsan_init.h
33  ubsan_interface.inc
34  ubsan_monitor.h
35  ubsan_platform.h
36  ubsan_signals_standalone.h
37  ubsan_type_hash.h
38  ubsan_value.h
39  )
40
41include_directories(..)
42
43set(UBSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
44append_rtti_flag(OFF UBSAN_CFLAGS)
45append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_CFLAGS)
46
47set(UBSAN_STANDALONE_CFLAGS ${SANITIZER_COMMON_CFLAGS})
48append_rtti_flag(OFF UBSAN_STANDALONE_CFLAGS)
49append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_STANDALONE_CFLAGS)
50
51set(UBSAN_CXXFLAGS ${SANITIZER_COMMON_CFLAGS})
52append_rtti_flag(ON UBSAN_CXXFLAGS)
53append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_CXXFLAGS)
54
55set(UBSAN_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
56
57set(UBSAN_DYNAMIC_LIBS ${SANITIZER_CXX_ABI_LIBRARIES} ${SANITIZER_COMMON_LINK_LIBS})
58
59append_list_if(COMPILER_RT_HAS_LIBDL dl UBSAN_DYNAMIC_LIBS)
60append_list_if(COMPILER_RT_HAS_LIBLOG log UBSAN_DYNAMIC_LIBS)
61append_list_if(COMPILER_RT_HAS_LIBRT rt UBSAN_DYNAMIC_LIBS)
62append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread UBSAN_DYNAMIC_LIBS)
63
64add_compiler_rt_component(ubsan)
65
66if(APPLE)
67  set(UBSAN_COMMON_SOURCES ${UBSAN_SOURCES})
68  if(SANITIZER_CAN_USE_CXXABI)
69    list(APPEND UBSAN_COMMON_SOURCES ${UBSAN_CXXABI_SOURCES})
70  endif()
71
72  # Common parts of UBSan runtime.
73  add_compiler_rt_object_libraries(RTUbsan
74    OS ${SANITIZER_COMMON_SUPPORTED_OS}
75    ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH}
76    SOURCES ${UBSAN_COMMON_SOURCES}
77    ADDITIONAL_HEADERS ${UBSAN_HEADERS}
78    CFLAGS ${UBSAN_CXXFLAGS})
79
80  if(COMPILER_RT_HAS_UBSAN)
81    # Initializer of standalone UBSan runtime.
82    add_compiler_rt_object_libraries(RTUbsan_standalone
83      OS ${SANITIZER_COMMON_SUPPORTED_OS}
84      ARCHS ${UBSAN_SUPPORTED_ARCH}
85      SOURCES ${UBSAN_STANDALONE_SOURCES}
86      ADDITIONAL_HEADERS ${UBSAN_HEADERS}
87      CFLAGS ${UBSAN_STANDALONE_CFLAGS})
88
89    add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
90    add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
91
92    add_compiler_rt_runtime(clang_rt.ubsan
93      SHARED
94      OS ${SANITIZER_COMMON_SUPPORTED_OS}
95      ARCHS ${UBSAN_SUPPORTED_ARCH}
96      OBJECT_LIBS RTUbsan
97                  RTUbsan_standalone
98                  RTSanitizerCommon
99                  RTSanitizerCommonLibc
100                  RTSanitizerCommonCoverage
101                  RTSanitizerCommonSymbolizer
102                  RTInterception
103      LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
104      PARENT_TARGET ubsan)
105
106    add_compiler_rt_runtime(clang_rt.ubsan
107      STATIC
108      OS ${SANITIZER_COMMON_SUPPORTED_OS}
109      ARCHS ${UBSAN_SUPPORTED_ARCH}
110      OBJECT_LIBS RTUbsan
111                  RTUbsan_standalone
112                  RTSanitizerCommonNoHooks
113                  RTSanitizerCommonLibcNoHooks
114                  RTSanitizerCommonCoverage
115                  RTSanitizerCommonSymbolizerNoHooks
116                  RTInterception
117      LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
118      PARENT_TARGET ubsan)
119  endif()
120
121else()
122  # Common parts of UBSan runtime.
123  add_compiler_rt_object_libraries(RTUbsan
124    ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH}
125    SOURCES ${UBSAN_SOURCES}
126    ADDITIONAL_HEADERS ${UBSAN_HEADERS}
127    CFLAGS ${UBSAN_CFLAGS})
128
129  if(SANITIZER_CAN_USE_CXXABI)
130    # C++-specific parts of UBSan runtime. Requires a C++ ABI library.
131    set(UBSAN_CXX_SOURCES ${UBSAN_CXXABI_SOURCES})
132  else()
133    # Dummy target if we don't have C++ ABI library.
134    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cxx_dummy.cpp "")
135    set(UBSAN_CXX_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/cxx_dummy.cpp)
136  endif()
137
138  add_compiler_rt_object_libraries(RTUbsan_cxx
139    ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH}
140    SOURCES ${UBSAN_CXX_SOURCES}
141    ADDITIONAL_HEADERS ${UBSAN_HEADERS}
142    CFLAGS ${UBSAN_CXXFLAGS})
143
144  if (WIN32)
145    add_compiler_rt_object_libraries(UbsanWeakInterception
146      ${SANITIZER_COMMON_SUPPORTED_OS}
147      ARCHS ${UBSAN_SUPPORTED_ARCH}
148      SOURCES ubsan_win_weak_interception.cpp
149      CFLAGS ${UBSAN_CFLAGS} -DSANITIZER_DYNAMIC
150      DEFS ${UBSAN_COMMON_DEFINITIONS})
151
152    add_compiler_rt_object_libraries(UbsanDllThunk
153      ${SANITIZER_COMMON_SUPPORTED_OS}
154      ARCHS ${UBSAN_SUPPORTED_ARCH}
155      SOURCES ubsan_win_dll_thunk.cpp
156      CFLAGS ${UBSAN_CFLAGS} -DSANITIZER_DLL_THUNK
157      DEFS ${UBSAN_COMMON_DEFINITIONS})
158
159    set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
160    if(MSVC)
161      list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
162    elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
163      list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
164    endif()
165    add_compiler_rt_object_libraries(UbsanDynamicRuntimeThunk
166      ${SANITIZER_COMMON_SUPPORTED_OS}
167      ARCHS ${UBSAN_SUPPORTED_ARCH}
168      SOURCES ubsan_win_dynamic_runtime_thunk.cpp
169      CFLAGS ${UBSAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
170      DEFS ${UBSAN_COMMON_DEFINITIONS})
171  endif()
172
173  if(COMPILER_RT_HAS_UBSAN)
174    add_compiler_rt_object_libraries(RTUbsan_standalone
175      ARCHS ${UBSAN_SUPPORTED_ARCH}
176      SOURCES ${UBSAN_STANDALONE_SOURCES}
177      ADDITIONAL_HEADERS ${UBSAN_HEADERS}
178      CFLAGS ${UBSAN_STANDALONE_CFLAGS})
179
180    # Standalone UBSan runtimes.
181    add_compiler_rt_runtime(clang_rt.ubsan_standalone
182      STATIC
183      ARCHS ${UBSAN_SUPPORTED_ARCH}
184      SOURCES ubsan_init_standalone_preinit.cpp
185      ADDITIONAL_HEADERS ${UBSAN_HEADERS}
186      OBJECT_LIBS RTSanitizerCommon
187              RTSanitizerCommonLibc
188              RTSanitizerCommonCoverage
189              RTSanitizerCommonSymbolizer
190              RTUbsan
191              RTUbsan_standalone
192              RTInterception
193      CFLAGS ${UBSAN_CFLAGS}
194      PARENT_TARGET ubsan)
195
196    add_compiler_rt_runtime(clang_rt.ubsan_standalone_cxx
197      STATIC
198      ARCHS ${UBSAN_SUPPORTED_ARCH}
199      OBJECT_LIBS RTUbsan_cxx
200      CFLAGS ${UBSAN_CXXFLAGS}
201      PARENT_TARGET ubsan)
202
203    if (COMPILER_RT_HAS_VERSION_SCRIPT)
204      file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
205      add_compiler_rt_object_libraries(RTUbsan_dynamic_version_script_dummy
206        ARCHS ${UBSAN_SUPPORTED_ARCH}
207        SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
208        CFLAGS ${UBSAN_CFLAGS})
209
210      foreach(arch ${UBSAN_SUPPORTED_ARCH})
211        add_sanitizer_rt_version_list(clang_rt.ubsan_standalone-dynamic-${arch}
212                                      LIBS clang_rt.ubsan_standalone-${arch}
213                                           clang_rt.ubsan_standalone_cxx-${arch}
214                                      EXTRA ubsan.syms.extra)
215        set(VERSION_SCRIPT_FLAG
216            -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.ubsan_standalone-dynamic-${arch}.vers)
217        # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
218        # but requires a special option to enable it.
219        if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
220            list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
221        endif()
222        set_property(SOURCE
223          ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
224          APPEND PROPERTY
225          OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.ubsan_standalone-dynamic-${arch}.vers)
226
227        add_compiler_rt_runtime(clang_rt.ubsan_standalone
228          SHARED
229          ARCHS ${arch}
230          OBJECT_LIBS RTSanitizerCommon
231                RTSanitizerCommonLibc
232                RTSanitizerCommonCoverage
233                RTSanitizerCommonSymbolizer
234                RTUbsan
235                RTUbsan_cxx
236                RTUbsan_standalone
237                RTInterception
238                RTUbsan_dynamic_version_script_dummy
239          CFLAGS ${UBSAN_CFLAGS}
240          LINK_FLAGS ${UBSAN_LINK_FLAGS} ${VERSION_SCRIPT_FLAG}
241          LINK_LIBS ${UBSAN_DYNAMIC_LIBS}
242          PARENT_TARGET ubsan)
243      endforeach()
244
245      set(ARCHS_FOR_SYMBOLS ${UBSAN_SUPPORTED_ARCH})
246      list(REMOVE_ITEM ARCHS_FOR_SYMBOLS i386)
247      add_sanitizer_rt_symbols(clang_rt.ubsan_standalone
248        ARCHS ${ARCHS_FOR_SYMBOLS}
249        PARENT_TARGET ubsan
250        EXTRA ubsan.syms.extra)
251      add_sanitizer_rt_symbols(clang_rt.ubsan_standalone_cxx
252        ARCHS ${ARCHS_FOR_SYMBOLS}
253        PARENT_TARGET ubsan
254        EXTRA ubsan.syms.extra)
255    endif()
256  endif()
257endif()
258