about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/compilers/llvm/7/compiler-rt/crtbegin-and-end.patch
blob: e63be181f95cc3890343787e8d07508cf6956585 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
Get crtbegin and crtend without compiler GCC! PR is at https://reviews.llvm.org/D28791

Index: compiler-rt/CMakeLists.txt
===================================================================
--- compiler-rt/CMakeLists.txt
+++ compiler-rt/CMakeLists.txt
@@ -29,6 +29,8 @@
 
 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
+option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON)
+mark_as_advanced(COMPILER_RT_BUILD_CRT)
 option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
 mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
 option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
Index: compiler-rt/cmake/Modules/AddCompilerRT.cmake
===================================================================
--- compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -132,7 +132,7 @@
 # Adds static or shared runtime for a list of architectures and operating
 # systems and puts it in the proper directory in the build and install trees.
 # add_compiler_rt_runtime(<name>
-#                         {STATIC|SHARED}
+#                         {OBJECT|STATIC|SHARED}
 #                         ARCHS <architectures>
 #                         OS <os list>
 #                         SOURCES <source files>
@@ -144,8 +144,8 @@
 #                         PARENT_TARGET <convenience parent target>
 #                         ADDITIONAL_HEADERS <header files>)
 function(add_compiler_rt_runtime name type)
-  if(NOT type MATCHES "^(STATIC|SHARED)$")
-    message(FATAL_ERROR "type argument must be STATIC or SHARED")
+  if(NOT type MATCHES "^(OBJECT|STATIC|SHARED)$")
+    message(FATAL_ERROR "type argument must be OBJECT, STATIC or SHARED")
     return()
   endif()
   cmake_parse_arguments(LIB
@@ -204,7 +204,10 @@
         message(FATAL_ERROR "Architecture ${arch} can't be targeted")
         return()
       endif()
-      if(type STREQUAL "STATIC")
+      if(type STREQUAL "OBJECT")
+        set(libname "${name}-${arch}")
+        set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
+      elseif(type STREQUAL "STATIC")
         set(libname "${name}-${arch}")
         set_output_name(output_name_${libname} ${name} ${arch})
       else()
@@ -270,12 +273,34 @@
       set(COMPONENT_OPTION COMPONENT ${libname})
     endif()
 
-    add_library(${libname} ${type} ${sources_${libname}})
-    set_target_compile_flags(${libname} ${extra_cflags_${libname}})
-    set_target_link_flags(${libname} ${extra_link_flags_${libname}})
-    set_property(TARGET ${libname} APPEND PROPERTY
-                COMPILE_DEFINITIONS ${LIB_DEFS})
-    set_target_output_directories(${libname} ${output_dir_${libname}})
+    if(type STREQUAL "OBJECT")
+      string(TOUPPER ${CMAKE_BUILD_TYPE} config)
+      get_property(cflags SOURCE ${sources_${libname}} PROPERTY COMPILE_FLAGS)
+      separate_arguments(cflags)
+      add_custom_command(
+          OUTPUT ${output_dir_${libname}}/${libname}.o
+          COMMAND ${CMAKE_C_COMPILER} ${sources_${libname}} ${cflags} ${extra_cflags_${libname}} -c -o ${output_dir_${libname}}/${libname}.o
+          DEPENDS ${sources_${libname}}
+          COMMENT "Building C object ${libname}.o")
+      add_custom_target(${libname} DEPENDS ${output_dir_${libname}}/${libname}.o)
+      install(FILES ${output_dir_${libname}}/${libname}.o
+        DESTINATION ${install_dir_${libname}}
+        ${COMPONENT_OPTION})
+    else()
+      add_library(${libname} ${type} ${sources_${libname}})
+      set_target_compile_flags(${libname} ${extra_cflags_${libname}})
+      set_target_link_flags(${libname} ${extra_link_flags_${libname}})
+      set_property(TARGET ${libname} APPEND PROPERTY
+                   COMPILE_DEFINITIONS ${LIB_DEFS})
+      set_target_output_directories(${libname} ${output_dir_${libname}})
+      install(TARGETS ${libname}
+        ARCHIVE DESTINATION ${install_dir_${libname}}
+                ${COMPONENT_OPTION}
+        LIBRARY DESTINATION ${install_dir_${libname}}
+                ${COMPONENT_OPTION}
+        RUNTIME DESTINATION ${install_dir_${libname}}
+                ${COMPONENT_OPTION})
+    endif()
     set_target_properties(${libname} PROPERTIES
         OUTPUT_NAME ${output_name_${libname}})
     set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
@@ -299,13 +324,6 @@
         )
       endif()
     endif()
-    install(TARGETS ${libname}
-      ARCHIVE DESTINATION ${install_dir_${libname}}
-              ${COMPONENT_OPTION}
-      LIBRARY DESTINATION ${install_dir_${libname}}
-              ${COMPONENT_OPTION}
-      RUNTIME DESTINATION ${install_dir_${libname}}
-              ${COMPONENT_OPTION})
 
     # We only want to generate per-library install targets if you aren't using
     # an IDE because the extra targets get cluttered in IDEs.
Index: compiler-rt/cmake/config-ix.cmake
===================================================================
--- compiler-rt/cmake/config-ix.cmake
+++ compiler-rt/cmake/config-ix.cmake
@@ -227,6 +227,7 @@
     ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${S390X})
 set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64}
     ${MIPS32} ${MIPS64} ${PPC64} ${S390X})
+set(ALL_CRT_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64})
 set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64})
 set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64})
 
@@ -474,6 +475,7 @@
     SANITIZER_COMMON_SUPPORTED_ARCH)
 
 else()
+  filter_available_targets(CRT_SUPPORTED_ARCH ${ALL_CRT_SUPPORTED_ARCH})
   # Architectures supported by compiler-rt libraries.
   filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
     ${ALL_SANITIZER_COMMON_SUPPORTED_ARCH})
@@ -563,6 +565,12 @@
 
 # TODO: Add builtins support.
 
+if (CRT_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux")
+  set(COMPILER_RT_HAS_CRT TRUE)
+else()
+  set(COMPILER_RT_HAS_CRT FALSE)
+endif()
+
 if (COMPILER_RT_HAS_SANITIZER_COMMON AND DFSAN_SUPPORTED_ARCH AND
     OS_NAME MATCHES "Linux")
   set(COMPILER_RT_HAS_DFSAN TRUE)
Index: compiler-rt/lib/CMakeLists.txt
===================================================================
--- compiler-rt/lib/CMakeLists.txt
+++ compiler-rt/lib/CMakeLists.txt
@@ -17,6 +17,10 @@
   add_subdirectory(builtins)
 endif()
 
+if(COMPILER_RT_BUILD_CRT)
+  add_subdirectory(crt)
+endif()
+
 function(compiler_rt_build_runtime runtime)
   string(TOUPPER ${runtime} runtime_uppercase)
   if(COMPILER_RT_HAS_${runtime_uppercase})
Index: compiler-rt/lib/crt/CMakeLists.txt
===================================================================
--- /dev/null
+++ compiler-rt/lib/crt/CMakeLists.txt
@@ -0,0 +1,102 @@
+add_compiler_rt_component(crt)
+
+function(check_cxx_section_exists section output)
+  cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN})
+  if(NOT ARG_SOURCE)
+    set(ARG_SOURCE "int main() { return 0; }\n")
+  endif()
+
+  string(RANDOM TARGET_NAME)
+  set(TARGET_NAME "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cmTC_${TARGET_NAME}.dir")
+  file(MAKE_DIRECTORY ${TARGET_NAME})
+
+  file(WRITE "${TARGET_NAME}/CheckSectionExists.c" "${ARG_SOURCE}\n")
+
+  string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
+         ${CMAKE_C_COMPILE_OBJECT})
+
+  set(try_compile_flags "${ARG_FLAGS}")
+  if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
+    list(APPEND try_compile_flags "-target ${CMAKE_C_COMPILER_TARGET}")
+  endif()
+
+  string(REPLACE ";" " " extra_flags "${try_compile_flags}")
+
+  set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}")
+  foreach(substitution ${substitutions})
+    if(substitution STREQUAL "<CMAKE_C_COMPILER>")
+      string(REPLACE "<CMAKE_C_COMPILER>"
+             "${CMAKE_C_COMPILER}" test_compile_command ${test_compile_command})
+    elseif(substitution STREQUAL "<OBJECT>")
+      string(REPLACE "<OBJECT>" "${TARGET_NAME}/CheckSectionExists.o"
+             test_compile_command ${test_compile_command})
+    elseif(substitution STREQUAL "<SOURCE>")
+      string(REPLACE "<SOURCE>" "${TARGET_NAME}/CheckSectionExists.c"
+             test_compile_command ${test_compile_command})
+    elseif(substitution STREQUAL "<FLAGS>")
+      string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}"
+             test_compile_command ${test_compile_command})
+    else()
+      string(REPLACE "${substitution}" "" test_compile_command
+             ${test_compile_command})
+    endif()
+  endforeach()
+
+  string(REPLACE " " ";" test_compile_command "${test_compile_command}")
+
+  execute_process(
+    COMMAND ${test_compile_command}
+    RESULT_VARIABLE TEST_RESULT
+    OUTPUT_VARIABLE TEST_OUTPUT
+    ERROR_VARIABLE TEST_ERROR
+  )
+
+  execute_process(
+    COMMAND ${CMAKE_OBJDUMP} -h "${TARGET_NAME}/CheckSectionExists.o"
+    RESULT_VARIABLE CHECK_RESULT
+    OUTPUT_VARIABLE CHECK_OUTPUT
+    ERROR_VARIABLE CHECK_ERROR
+  )
+  string(FIND "${CHECK_OUTPUT}" "${section}" SECTION_FOUND)
+
+  if(NOT SECTION_FOUND EQUAL -1)
+    set(${output} TRUE PARENT_SCOPE)
+  else()
+    set(${output} FALSE PARENT_SCOPE)
+  endif()
+
+  file(REMOVE_RECURSE ${TARGET_NAME})
+endfunction()
+
+check_cxx_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
+  SOURCE "__attribute__((constructor)) void f() {}\nint main() { return 0; }\n")
+
+append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
+append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
+
+foreach(arch ${CRT_SUPPORTED_ARCH})
+  add_compiler_rt_runtime(clang_rt.crtbegin
+    OBJECT
+    ARCHS ${arch}
+    SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
+    CFLAGS ${CRT_CFLAGS}
+    PARENT_TARGET crt)
+  add_compiler_rt_runtime(clang_rt.crtbegin_shared
+    OBJECT
+    ARCHS ${arch}
+    SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
+    CFLAGS ${CRT_CFLAGS} -DCRT_SHARED
+    PARENT_TARGET crt)
+  add_compiler_rt_runtime(clang_rt.crtend
+    OBJECT
+    ARCHS ${arch}
+    SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
+    CFLAGS ${CRT_CFLAGS}
+    PARENT_TARGET crt)
+  add_compiler_rt_runtime(clang_rt.crtend_shared
+    OBJECT
+    ARCHS ${arch}
+    SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
+    CFLAGS ${CRT_CFLAGS} -DCRT_SHARED
+    PARENT_TARGET crt)
+endforeach()
Index: compiler-rt/lib/crt/crtbegin.c
===================================================================
--- /dev/null
+++ compiler-rt/lib/crt/crtbegin.c
@@ -0,0 +1,108 @@
+/* ===-- crtbegin.c - Start of constructors and destructors ----------------===
+ *
+ *      	       The LLVM Compiler Infrastructure
+ *
+ * This file is dual licensed under the MIT and the University of Illinois Open
+ * Source Licenses. See LICENSE.TXT for details.
+ *
+ * ===----------------------------------------------------------------------===
+ */
+
+#include <stddef.h>
+
+__attribute__((visibility("hidden")))
+#ifdef CRT_SHARED
+void *__dso_handle = &__dso_handle;
+#else
+void *__dso_handle = (void *)0;
+#endif
+
+static long __EH_FRAME_LIST__[]
+    __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};
+
+extern void __register_frame_info(const void *, void *) __attribute__((weak));
+extern void *__deregister_frame_info(const void *) __attribute__((weak));
+
+#ifndef CRT_HAS_INITFINI_ARRAY
+typedef void (*fp)(void);
+
+static fp __CTOR_LIST__[]
+    __attribute__((section(".ctors"), aligned(sizeof(fp)), used)) = {(fp)-1};
+extern fp __CTOR_LIST_END__[];
+#endif
+
+#ifdef CRT_SHARED
+extern void __cxa_finalize(void *) __attribute__((weak));
+#endif
+
+static void __attribute__((used)) __do_init() {
+  static _Bool __initialized;
+  if (__builtin_expect(__initialized, 0))
+    return;
+  __initialized = 1;
+
+  static struct { void *p[8]; } __object;
+  if (__register_frame_info)
+    __register_frame_info(__EH_FRAME_LIST__, &__object);
+
+#ifndef CRT_HAS_INITFINI_ARRAY
+  const size_t n = __CTOR_LIST_END__ - __CTOR_LIST__ - 1;
+  for (size_t i = n; i >= 1; i--) __CTOR_LIST__[i]();
+#endif
+}
+
+#ifdef CRT_HAS_INITFINI_ARRAY
+__attribute__((section(".init_array"),
+               used)) static void (*__init)(void) = __do_init;
+#else  // CRT_HAS_INITFINI_ARRAY
+#if defined(__i386__) || defined(__x86_64__)
+asm(".pushsection .init,\"ax\",@progbits\n\t"
+    "call " __USER_LABEL_PREFIX__ "__do_init\n\t"
+    ".popsection");
+#elif defined(__arm__)
+asm(".pushsection .init,\"ax\",%progbits\n\t"
+    "bl " __USER_LABEL_PREFIX__ "__do_init\n\t"
+    ".popsection");
+#endif  // CRT_HAS_INITFINI_ARRAY
+#endif
+
+#ifndef CRT_HAS_INITFINI_ARRAY
+static fp __DTOR_LIST__[]
+    __attribute__((section(".dtors"), aligned(sizeof(fp)), used)) = {(fp)-1};
+extern fp __DTOR_LIST_END__[];
+#endif
+
+static void __attribute__((used)) __do_fini() {
+  static _Bool __finalized;
+  if (__builtin_expect(__finalized, 0))
+    return;
+  __finalized = 1;
+
+#ifdef CRT_SHARED
+  if (__cxa_finalize)
+    __cxa_finalize(__dso_handle);
+#endif
+
+#ifndef CRT_HAS_INITFINI_ARRAY
+  if (__deregister_frame_info)
+    __deregister_frame_info(__EH_FRAME_LIST__);
+
+  const size_t n = __DTOR_LIST_END__ - __DTOR_LIST__ - 1;
+  for (size_t i = 1; i < n; i++) __DTOR_LIST__[i]();
+#endif
+}
+
+#ifdef CRT_HAS_INITFINI_ARRAY
+__attribute__((section(".fini_array"),
+               used)) static void (*__fini)(void) = __do_fini;
+#else  // CRT_HAS_INITFINI_ARRAY
+#if defined(__i386__) || defined(__x86_64__)
+asm(".pushsection .fini,\"ax\",@progbits\n\t"
+    "call " __USER_LABEL_PREFIX__ "__do_fini\n\t"
+    ".popsection");
+#elif defined(__arm__)
+asm(".pushsection .fini,\"ax\",%progbits\n\t"
+    "bl " __USER_LABEL_PREFIX__ "__do_fini\n\t"
+    ".popsection");
+#endif
+#endif  // CRT_HAS_INIT_FINI_ARRAY
Index: compiler-rt/lib/crt/crtend.c
===================================================================
--- /dev/null
+++ compiler-rt/lib/crt/crtend.c
@@ -0,0 +1,24 @@
+/* ===-- crtend.c - End of constructors and destructors --------------------===
+ *
+ *      	       The LLVM Compiler Infrastructure
+ *
+ * This file is dual licensed under the MIT and the University of Illinois Open
+ * Source Licenses. See LICENSE.TXT for details.
+ *
+ * ===----------------------------------------------------------------------===
+ */
+
+#include <stdint.h>
+
+// Put 4-byte zero which is the length field in FDE at the end as a terminator.
+const int32_t __EH_FRAME_LIST_END__[]
+    __attribute__((section(".eh_frame"), aligned(sizeof(int32_t)),
+                   visibility("hidden"), used)) = {0};
+
+#ifndef CRT_HAS_INITFINI_ARRAY
+typedef void (*fp)(void);
+fp __CTOR_LIST_END__[]
+    __attribute__((section(".ctors"), visibility("hidden"), used)) = {0};
+fp __DTOR_LIST_END__[]
+    __attribute__((section(".dtors"), visibility("hidden"), used)) = {0};
+#endif
Index: compiler-rt/test/CMakeLists.txt
===================================================================
--- compiler-rt/test/CMakeLists.txt
+++ compiler-rt/test/CMakeLists.txt
@@ -73,6 +73,9 @@
   if(COMPILER_RT_BUILD_XRAY)
     compiler_rt_test_runtime(xray)
   endif()
+  if(COMPILER_RT_HAS_CRT)
+    add_subdirectory(crt)
+  endif()
   # ShadowCallStack does not yet provide a runtime with compiler-rt, the tests
   # include their own minimal runtime
   add_subdirectory(shadowcallstack)
Index: compiler-rt/test/crt/CMakeLists.txt
===================================================================
--- /dev/null
+++ compiler-rt/test/crt/CMakeLists.txt
@@ -0,0 +1,31 @@
+set(CRT_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
+
+set(CRT_TESTSUITES)
+
+set(CRT_TEST_DEPS "")
+
+if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_BUILD_CRT AND
+   COMPILER_RT_HAS_CRT)
+  list(APPEND CRT_TEST_DEPS crt)
+endif()
+
+set(CRT_TEST_ARCH ${CRT_SUPPORTED_ARCH})
+if (COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT)
+  foreach(arch ${CRT_TEST_ARCH})
+    set(CRT_TEST_TARGET_ARCH ${arch})
+    string(TOLOWER "-${arch}-${OS_NAME}" CRT_TEST_CONFIG_SUFFIX)
+    get_test_cc_for_arch(${arch} CRT_TEST_TARGET_CC CRT_TEST_TARGET_CFLAGS)
+    string(TOUPPER ${arch} ARCH_UPPER_CASE)
+    set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
+
+    configure_lit_site_cfg(
+      ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
+      ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg)
+    list(APPEND CRT_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
+  endforeach()
+endif()
+
+add_lit_testsuite(check-crt "Running the CRT tests"
+  ${CRT_TESTSUITES}
+  DEPENDS ${CRT_TEST_DEPS})
+set_target_properties(check-crt PROPERTIES FOLDER "Compiler-RT Misc")
Index: compiler-rt/test/crt/dso_handle.cpp
===================================================================
--- /dev/null
+++ compiler-rt/test/crt/dso_handle.cpp
@@ -0,0 +1,33 @@
+// RUN: %clangxx -g -DCRT_SHARED -c %s -fPIC -o %tshared.o
+// RUN: %clangxx -g -c %s -fPIC -o %t.o
+// RUN: %clangxx -g -shared -o %t.so -nostdlib %crti %shared_crtbegin %tshared.o %libstdcxx -lc -lm -lgcc_s %shared_crtend %crtn
+// RUN: %clangxx -g -o %t -nostdlib %crt1 %crti %crtbegin %t.o %libstdcxx -lc -lm %libgcc %t.so %crtend %crtn
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+
+// CHECK: 1
+// CHECK-NEXT: ~A()
+
+#ifdef CRT_SHARED
+bool G;
+void C() {
+  printf("%d\n", G);
+}
+
+struct A {
+  A() { G = true; }
+  ~A() {
+    printf("~A()\n");
+  }
+};
+
+A a;
+#else
+void C();
+
+int main() {
+  C();
+  return 0;
+}
+#endif
Index: compiler-rt/test/crt/lit.cfg
===================================================================
--- /dev/null
+++ compiler-rt/test/crt/lit.cfg
@@ -0,0 +1,80 @@
+# -*- Python -*-
+
+import os
+import subprocess
+
+# Setup config name.
+config.name = 'CRT' + config.name_suffix
+
+# Setup source root.
+config.test_source_root = os.path.dirname(__file__)
+
+
+def get_library_path(file):
+    cmd = subprocess.Popen([config.clang.strip(),
+                            config.target_cflags.strip(),
+                            '-print-file-name=%s' % file],
+                           stdout=subprocess.PIPE,
+                           env=config.environment)
+    if not cmd.stdout:
+      lit_config.fatal("Couldn't find the library path for '%s'" % file)
+    dir = cmd.stdout.read().strip()
+    if sys.platform in ['win32'] and execute_external:
+        # Don't pass dosish path separator to msys bash.exe.
+        dir = dir.replace('\\', '/')
+    # Ensure the result is an ascii string, across Python2.5+ - Python3.
+    return str(dir.decode('ascii'))
+
+
+def get_libgcc_file_name():
+    cmd = subprocess.Popen([config.clang.strip(),
+                            config.target_cflags.strip(),
+                            '-print-libgcc-file-name'],
+                           stdout=subprocess.PIPE,
+                           env=config.environment)
+    if not cmd.stdout:
+      lit_config.fatal("Couldn't find the library path for '%s'" % file)
+    dir = cmd.stdout.read().strip()
+    if sys.platform in ['win32'] and execute_external:
+        # Don't pass dosish path separator to msys bash.exe.
+        dir = dir.replace('\\', '/')
+    # Ensure the result is an ascii string, across Python2.5+ - Python3.
+    return str(dir.decode('ascii'))
+
+
+def build_invocation(compile_flags):
+    return ' ' + ' '.join([config.clang] + compile_flags) + ' '
+
+
+# Setup substitutions.
+config.substitutions.append(
+    ('%clang ', build_invocation([config.target_cflags])))
+config.substitutions.append(
+    ('%clangxx ',
+     build_invocation(config.cxx_mode_flags + [config.target_cflags])))
+
+base_lib = os.path.join(
+    config.compiler_rt_libdir, "clang_rt.%%s-%s.o" % config.target_arch)
+config.substitutions.append(('%crtbegin', base_lib % "crtbegin"))
+config.substitutions.append(('%shared_crtbegin', base_lib % "crtbegin_shared"))
+config.substitutions.append(('%crtend', base_lib % "crtend"))
+config.substitutions.append(('%shared_crtend', base_lib % "crtend_shared"))
+
+config.substitutions.append(
+    ('%crt1', get_library_path('crt1.o')))
+config.substitutions.append(
+    ('%crti', get_library_path('crti.o')))
+config.substitutions.append(
+    ('%crtn', get_library_path('crtn.o')))
+
+config.substitutions.append(
+    ('%libgcc', get_libgcc_file_name()))
+
+config.substitutions.append(
+    ('%libstdcxx', '-l' + config.sanitizer_cxx_lib.lstrip('lib')))
+
+# Default test suffixes.
+config.suffixes = ['.c', '.cc', '.cpp']
+
+if config.host_os not in ['Linux']:
+    config.unsupported = True
Index: compiler-rt/test/crt/lit.site.cfg.in
===================================================================
--- /dev/null
+++ compiler-rt/test/crt/lit.site.cfg.in
@@ -0,0 +1,14 @@
+@LIT_SITE_CFG_IN_HEADER@
+
+# Tool-specific config options.
+config.name_suffix = "@CRT_TEST_CONFIG_SUFFIX@"
+config.crt_lit_source_dir = "@CRT_LIT_SOURCE_DIR@"
+config.target_cflags = "@CRT_TEST_TARGET_CFLAGS@"
+config.target_arch = "@CRT_TEST_TARGET_ARCH@"
+config.sanitizer_cxx_lib = "@SANITIZER_TEST_CXX_LIBNAME@"
+
+# Load common config for all compiler-rt lit tests
+lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")
+
+# Load tool-specific config that would do the real work.
+lit_config.load_config(config, "@CRT_LIT_SOURCE_DIR@/lit.cfg")