authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-05 22:45:41-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-05 22:45:41-05:00
logba144b366c40790f6d77a68b25d4567502601dc1
tree6a56ae71620ac0b5994d9564eb0a88af11eb9a64
parent5cecb5e479cc543a929ae6856d6efdea2a59844f
signature Commit is signed but in an unrecognized format.

build libunwind.a from source and link it


4 files changed, 374 insertions(+), 37 deletions(-)

CMakeLists.txt+224
...@@ -1408,6 +1408,219 @@ set(ZIG_LIBC_FILES...@@ -1408,6 +1408,219 @@ set(ZIG_LIBC_FILES
1408 "glibc/time/time.h"1408 "glibc/time/time.h"
1409)1409)
14101410
1411set(ZIG_LIBUNWIND_FILES
1412 "include/__libunwind_config.h"
1413 "include/libunwind.h"
1414 "include/mach-o/compact_unwind_encoding.h"
1415 "include/unwind.h"
1416 "src/AddressSpace.hpp"
1417 "src/CompactUnwinder.hpp"
1418 "src/DwarfInstructions.hpp"
1419 "src/DwarfParser.hpp"
1420 "src/EHHeaderParser.hpp"
1421 "src/RWMutex.hpp"
1422 "src/Registers.hpp"
1423 "src/Unwind-EHABI.cpp"
1424 "src/Unwind-EHABI.h"
1425 "src/Unwind-seh.cpp"
1426 "src/Unwind-sjlj.c"
1427 "src/UnwindCursor.hpp"
1428 "src/UnwindLevel1-gcc-ext.c"
1429 "src/UnwindLevel1.c"
1430 "src/UnwindRegistersRestore.S"
1431 "src/UnwindRegistersSave.S"
1432 "src/Unwind_AppleExtras.cpp"
1433 "src/assembly.h"
1434 "src/config.h"
1435 "src/dwarf2.h"
1436 "src/libunwind.cpp"
1437 "src/libunwind_ext.h"
1438)
1439
1440set(ZIG_LIBCXX_FILES
1441 "include/__bit_reference"
1442 "include/__bsd_locale_defaults.h"
1443 "include/__bsd_locale_fallbacks.h"
1444 "include/__config"
1445 "include/__config_site.in"
1446 "include/__debug"
1447 "include/__errc"
1448 "include/__functional_03"
1449 "include/__functional_base"
1450 "include/__functional_base_03"
1451 "include/__hash_table"
1452 "include/__libcpp_version"
1453 "include/__locale"
1454 "include/__mutex_base"
1455 "include/__node_handle"
1456 "include/__nullptr"
1457 "include/__split_buffer"
1458 "include/__sso_allocator"
1459 "include/__std_stream"
1460 "include/__string"
1461 "include/__threading_support"
1462 "include/__tree"
1463 "include/__tuple"
1464 "include/__undef_macros"
1465 "include/algorithm"
1466 "include/any"
1467 "include/array"
1468 "include/atomic"
1469 "include/bit"
1470 "include/bitset"
1471 "include/cassert"
1472 "include/ccomplex"
1473 "include/cctype"
1474 "include/cerrno"
1475 "include/cfenv"
1476 "include/cfloat"
1477 "include/charconv"
1478 "include/chrono"
1479 "include/cinttypes"
1480 "include/ciso646"
1481 "include/climits"
1482 "include/clocale"
1483 "include/cmath"
1484 "include/codecvt"
1485 "include/compare"
1486 "include/complex"
1487 "include/complex.h"
1488 "include/condition_variable"
1489 "include/csetjmp"
1490 "include/csignal"
1491 "include/cstdarg"
1492 "include/cstdbool"
1493 "include/cstddef"
1494 "include/cstdint"
1495 "include/cstdio"
1496 "include/cstdlib"
1497 "include/cstring"
1498 "include/ctgmath"
1499 "include/ctime"
1500 "include/ctype.h"
1501 "include/cwchar"
1502 "include/cwctype"
1503 "include/deque"
1504 "include/errno.h"
1505 "include/exception"
1506 "include/experimental/__config"
1507 "include/experimental/__memory"
1508 "include/experimental/algorithm"
1509 "include/experimental/any"
1510 "include/experimental/chrono"
1511 "include/experimental/coroutine"
1512 "include/experimental/deque"
1513 "include/experimental/filesystem"
1514 "include/experimental/forward_list"
1515 "include/experimental/functional"
1516 "include/experimental/iterator"
1517 "include/experimental/list"
1518 "include/experimental/map"
1519 "include/experimental/memory_resource"
1520 "include/experimental/numeric"
1521 "include/experimental/optional"
1522 "include/experimental/propagate_const"
1523 "include/experimental/ratio"
1524 "include/experimental/regex"
1525 "include/experimental/set"
1526 "include/experimental/simd"
1527 "include/experimental/string"
1528 "include/experimental/string_view"
1529 "include/experimental/system_error"
1530 "include/experimental/tuple"
1531 "include/experimental/type_traits"
1532 "include/experimental/unordered_map"
1533 "include/experimental/unordered_set"
1534 "include/experimental/utility"
1535 "include/experimental/vector"
1536 "include/ext/__hash"
1537 "include/ext/hash_map"
1538 "include/ext/hash_set"
1539 "include/filesystem"
1540 "include/float.h"
1541 "include/forward_list"
1542 "include/fstream"
1543 "include/functional"
1544 "include/future"
1545 "include/initializer_list"
1546 "include/inttypes.h"
1547 "include/iomanip"
1548 "include/ios"
1549 "include/iosfwd"
1550 "include/iostream"
1551 "include/istream"
1552 "include/iterator"
1553 "include/limits"
1554 "include/limits.h"
1555 "include/list"
1556 "include/locale"
1557 "include/locale.h"
1558 "include/map"
1559 "include/math.h"
1560 "include/memory"
1561 "include/module.modulemap"
1562 "include/mutex"
1563 "include/new"
1564 "include/numeric"
1565 "include/optional"
1566 "include/ostream"
1567 "include/queue"
1568 "include/random"
1569 "include/ratio"
1570 "include/regex"
1571 "include/scoped_allocator"
1572 "include/set"
1573 "include/setjmp.h"
1574 "include/shared_mutex"
1575 "include/span"
1576 "include/sstream"
1577 "include/stack"
1578 "include/stdbool.h"
1579 "include/stddef.h"
1580 "include/stdexcept"
1581 "include/stdint.h"
1582 "include/stdio.h"
1583 "include/stdlib.h"
1584 "include/streambuf"
1585 "include/string"
1586 "include/string.h"
1587 "include/string_view"
1588 "include/strstream"
1589 "include/support/android/locale_bionic.h"
1590 "include/support/fuchsia/xlocale.h"
1591 "include/support/ibm/limits.h"
1592 "include/support/ibm/locale_mgmt_aix.h"
1593 "include/support/ibm/support.h"
1594 "include/support/ibm/xlocale.h"
1595 "include/support/musl/xlocale.h"
1596 "include/support/newlib/xlocale.h"
1597 "include/support/solaris/floatingpoint.h"
1598 "include/support/solaris/wchar.h"
1599 "include/support/solaris/xlocale.h"
1600 "include/support/win32/limits_msvc_win32.h"
1601 "include/support/win32/locale_win32.h"
1602 "include/support/xlocale/__nop_locale_mgmt.h"
1603 "include/support/xlocale/__posix_l_fallback.h"
1604 "include/support/xlocale/__strtonum_fallback.h"
1605 "include/support/xlocale/xlocale.h"
1606 "include/system_error"
1607 "include/tgmath.h"
1608 "include/thread"
1609 "include/tuple"
1610 "include/type_traits"
1611 "include/typeindex"
1612 "include/typeinfo"
1613 "include/unordered_map"
1614 "include/unordered_set"
1615 "include/utility"
1616 "include/valarray"
1617 "include/variant"
1618 "include/vector"
1619 "include/version"
1620 "include/wchar.h"
1621 "include/wctype.h"
1622)
1623
1411if(MSVC)1624if(MSVC)
1412 set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK")1625 set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK")
1413 if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})1626 if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})
...@@ -1419,6 +1632,8 @@ endif()...@@ -1419,6 +1632,8 @@ endif()
1419set(ZIG_LIB_DIR "lib/zig")1632set(ZIG_LIB_DIR "lib/zig")
1420set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include")1633set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include")
1421set(LIBC_FILES_DEST "${ZIG_LIB_DIR}/libc")1634set(LIBC_FILES_DEST "${ZIG_LIB_DIR}/libc")
1635set(LIBUNWIND_FILES_DEST "${ZIG_LIB_DIR}/libunwind")
1636set(LIBCXX_FILES_DEST "${ZIG_LIB_DIR}/libcxx")
1422set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")1637set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
1423set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")1638set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
1424configure_file (1639configure_file (
...@@ -1527,6 +1742,15 @@ foreach(file ${ZIG_LIBC_FILES})...@@ -1527,6 +1742,15 @@ foreach(file ${ZIG_LIBC_FILES})
1527 install(FILES "${CMAKE_SOURCE_DIR}/libc/${file}" DESTINATION "${file_dir}")1742 install(FILES "${CMAKE_SOURCE_DIR}/libc/${file}" DESTINATION "${file_dir}")
1528endforeach()1743endforeach()
15291744
1745foreach(file ${ZIG_LIBUNWIND_FILES})
1746 get_filename_component(file_dir "${LIBUNWIND_FILES_DEST}/${file}" DIRECTORY)
1747 install(FILES "${CMAKE_SOURCE_DIR}/libunwind/${file}" DESTINATION "${file_dir}")
1748endforeach()
1749
1750foreach(file ${ZIG_LIBCXX_FILES})
1751 get_filename_component(file_dir "${LIBCXX_FILES_DEST}/${file}" DIRECTORY)
1752 install(FILES "${CMAKE_SOURCE_DIR}/libcxx/${file}" DESTINATION "${file_dir}")
1753endforeach()
15301754
1531install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/arg.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/")1755install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/arg.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/")
1532install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/main.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/")1756install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/main.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/")
src/codegen.cpp+35-20
...@@ -8275,7 +8275,8 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {...@@ -8275,7 +8275,8 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {
8275 return ErrorNone;8275 return ErrorNone;
8276}8276}
82778277
8278static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {8278// returns true if it was a cache miss
8279static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
8279 Error err;8280 Error err;
82808281
8281 Buf *artifact_dir;8282 Buf *artifact_dir;
...@@ -8346,8 +8347,8 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -8346,8 +8347,8 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
8346 }8347 }
8347 exit(1);8348 exit(1);
8348 }8349 }
8349 if (buf_len(&digest) == 0) {8350 bool is_cache_miss = (buf_len(&digest) == 0);
8350 // cache miss8351 if (is_cache_miss) {
8351 // we can't know the digest until we do the C compiler invocation, so we8352 // we can't know the digest until we do the C compiler invocation, so we
8352 // need a tmp filename.8353 // need a tmp filename.
8353 Buf *out_obj_path = buf_alloc();8354 Buf *out_obj_path = buf_alloc();
...@@ -8428,10 +8429,12 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -8428,10 +8429,12 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
8428 }8429 }
8429 break;8430 break;
8430 case BuildModeFastRelease:8431 case BuildModeFastRelease:
8432 args.append("-DNDEBUG");
8431 args.append("-O2");8433 args.append("-O2");
8432 args.append("-fno-stack-protector");8434 args.append("-fno-stack-protector");
8433 break;8435 break;
8434 case BuildModeSmallRelease:8436 case BuildModeSmallRelease:
8437 args.append("-DNDEBUG");
8435 args.append("-Os");8438 args.append("-Os");
8436 args.append("-fno-stack-protector");8439 args.append("-fno-stack-protector");
8437 break;8440 break;
...@@ -8504,13 +8507,17 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -8504,13 +8507,17 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
85048507
8505 g->link_objects.append(o_final_path);8508 g->link_objects.append(o_final_path);
8506 g->caches_to_release.append(cache_hash);8509 g->caches_to_release.append(cache_hash);
8510
8511 return is_cache_miss;
8507}8512}
85088513
8509static void gen_c_objects(CodeGen *g) {8514// returns true if we had any cache misses
8515static bool gen_c_objects(CodeGen *g) {
8510 Error err;8516 Error err;
8517 bool any_cache_misses = false;
85118518
8512 if (g->c_source_files.length == 0)8519 if (g->c_source_files.length == 0)
8513 return;8520 return any_cache_misses;
85148521
8515 Buf *self_exe_path = buf_alloc();8522 Buf *self_exe_path = buf_alloc();
8516 if ((err = os_self_exe_path(self_exe_path))) {8523 if ((err = os_self_exe_path(self_exe_path))) {
...@@ -8522,8 +8529,10 @@ static void gen_c_objects(CodeGen *g) {...@@ -8522,8 +8529,10 @@ static void gen_c_objects(CodeGen *g) {
85228529
8523 for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {8530 for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {
8524 CFile *c_file = g->c_source_files.at(c_file_i);8531 CFile *c_file = g->c_source_files.at(c_file_i);
8525 gen_c_object(g, self_exe_path, c_file);8532 bool is_cache_miss = gen_c_object(g, self_exe_path, c_file);
8533 any_cache_misses = any_cache_misses || is_cache_miss;
8526 }8534 }
8535 return any_cache_misses;
8527}8536}
85288537
8529void codegen_add_object(CodeGen *g, Buf *object_path) {8538void codegen_add_object(CodeGen *g, Buf *object_path) {
...@@ -9020,7 +9029,8 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {...@@ -9020,7 +9029,8 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
9020}9029}
90219030
9022// Called before init()9031// Called before init()
9023static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {9032// is_cache_hit takes into account gen_c_objects
9033static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_objects_generated) {
9024 Error err;9034 Error err;
90259035
9026 Buf *compiler_id;9036 Buf *compiler_id;
...@@ -9081,7 +9091,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -9081,7 +9091,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
9081 }9091 }
9082 cache_buf(ch, g->dynamic_linker_path);9092 cache_buf(ch, g->dynamic_linker_path);
90839093
9084 gen_c_objects(g);9094 *c_objects_generated = gen_c_objects(g);
90859095
9086 buf_resize(digest, 0);9096 buf_resize(digest, 0);
9087 if ((err = cache_hit(ch, digest)))9097 if ((err = cache_hit(ch, digest)))
...@@ -9188,11 +9198,12 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9188,11 +9198,12 @@ void codegen_build_and_link(CodeGen *g) {
91889198
9189 Buf *artifact_dir = buf_alloc();9199 Buf *artifact_dir = buf_alloc();
9190 Buf digest = BUF_INIT;9200 Buf digest = BUF_INIT;
9201 bool any_c_objects_generated;
9191 if (g->enable_cache) {9202 if (g->enable_cache) {
9192 Buf *manifest_dir = buf_alloc();9203 Buf *manifest_dir = buf_alloc();
9193 os_path_join(g->cache_dir, buf_create_from_str("build"), manifest_dir);9204 os_path_join(g->cache_dir, buf_create_from_str("build"), manifest_dir);
91949205
9195 if ((err = check_cache(g, manifest_dir, &digest))) {9206 if ((err = check_cache(g, manifest_dir, &digest, &any_c_objects_generated))) {
9196 if (err == ErrorCacheUnavailable) {9207 if (err == ErrorCacheUnavailable) {
9197 // message already printed9208 // message already printed
9198 } else {9209 } else {
...@@ -9204,24 +9215,28 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9204,24 +9215,28 @@ void codegen_build_and_link(CodeGen *g) {
9204 os_path_join(g->cache_dir, buf_create_from_str("artifact"), artifact_dir);9215 os_path_join(g->cache_dir, buf_create_from_str("artifact"), artifact_dir);
9205 } else {9216 } else {
9206 // There is a call to this in check_cache9217 // There is a call to this in check_cache
9207 gen_c_objects(g);9218 any_c_objects_generated = gen_c_objects(g);
9208 }9219 }
92099220
9210 if (g->enable_cache && buf_len(&digest) != 0) {9221 if (g->enable_cache && buf_len(&digest) != 0 && !any_c_objects_generated) {
9211 os_path_join(artifact_dir, &digest, &g->artifact_dir);9222 os_path_join(artifact_dir, &digest, &g->artifact_dir);
9212 resolve_out_paths(g);9223 resolve_out_paths(g);
9213 } else {9224 } else {
9214 init(g);9225 if (need_llvm_module(g)) {
9226 init(g);
92159227
9216 codegen_add_time_event(g, "Semantic Analysis");9228 codegen_add_time_event(g, "Semantic Analysis");
92179229
9218 gen_global_asm(g);9230 gen_global_asm(g);
9219 gen_root_source(g);9231 gen_root_source(g);
92209232
9233 }
9221 if (g->enable_cache) {9234 if (g->enable_cache) {
9222 if ((err = cache_final(&g->cache_hash, &digest))) {9235 if (buf_len(&digest) == 0) {
9223 fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));9236 if ((err = cache_final(&g->cache_hash, &digest))) {
9224 exit(1);9237 fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));
9238 exit(1);
9239 }
9225 }9240 }
9226 os_path_join(artifact_dir, &digest, &g->artifact_dir);9241 os_path_join(artifact_dir, &digest, &g->artifact_dir);
9227 } else {9242 } else {
...@@ -9233,9 +9248,9 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9233,9 +9248,9 @@ void codegen_build_and_link(CodeGen *g) {
9233 }9248 }
9234 resolve_out_paths(g);9249 resolve_out_paths(g);
92359250
9236 codegen_add_time_event(g, "Code Generation");
9237
9238 if (need_llvm_module(g)) {9251 if (need_llvm_module(g)) {
9252 codegen_add_time_event(g, "Code Generation");
9253
9239 do_code_gen(g);9254 do_code_gen(g);
9240 codegen_add_time_event(g, "LLVM Emit Output");9255 codegen_add_time_event(g, "LLVM Emit Output");
9241 zig_llvm_emit_output(g);9256 zig_llvm_emit_output(g);
src/compiler.cpp+31-13
...@@ -10,6 +10,7 @@ static Buf saved_lib_dir = BUF_INIT;...@@ -10,6 +10,7 @@ static Buf saved_lib_dir = BUF_INIT;
10static Buf saved_special_dir = BUF_INIT;10static Buf saved_special_dir = BUF_INIT;
11static Buf saved_std_dir = BUF_INIT;11static Buf saved_std_dir = BUF_INIT;
12static Buf saved_dynamic_linker_path = BUF_INIT;12static Buf saved_dynamic_linker_path = BUF_INIT;
13static bool searched_for_dyn_linker = false;
1314
14Buf *get_stage1_cache_path(void) {15Buf *get_stage1_cache_path(void) {
15 if (saved_stage1_path.list.length != 0) {16 if (saved_stage1_path.list.length != 0) {
...@@ -24,6 +25,35 @@ Buf *get_stage1_cache_path(void) {...@@ -24,6 +25,35 @@ Buf *get_stage1_cache_path(void) {
24 return &saved_stage1_path;25 return &saved_stage1_path;
25}26}
2627
28static void detect_dynamic_linker(Buf *lib_path) {
29#if defined(ZIG_OS_LINUX) && defined(ZIG_ARCH_X86_64)
30 if (buf_ends_with_str(lib_path, "ld-linux-x86-64.so.2")) {
31 buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
32 } else if (buf_ends_with_str(lib_path, "ld-musl-x86-64.so.1")) {
33 buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
34 }
35#endif
36}
37
38Buf *get_self_dynamic_linker_path(void) {
39 for (;;) {
40 if (saved_dynamic_linker_path.list.length != 0) {
41 return &saved_dynamic_linker_path;
42 }
43 if (searched_for_dyn_linker)
44 return nullptr;
45 ZigList<Buf *> lib_paths = {};
46 Error err;
47 if ((err = os_self_exe_shared_libs(lib_paths)))
48 return nullptr;
49 for (size_t i = 0; i < lib_paths.length; i += 1) {
50 Buf *lib_path = lib_paths.at(i);
51 detect_dynamic_linker(lib_path);
52 }
53 searched_for_dyn_linker = true;
54 }
55}
56
27Error get_compiler_id(Buf **result) {57Error get_compiler_id(Buf **result) {
28 if (saved_compiler_id.list.length != 0) {58 if (saved_compiler_id.list.length != 0) {
29 *result = &saved_compiler_id;59 *result = &saved_compiler_id;
...@@ -57,15 +87,9 @@ Error get_compiler_id(Buf **result) {...@@ -57,15 +87,9 @@ Error get_compiler_id(Buf **result) {
57 return err;87 return err;
58 for (size_t i = 0; i < lib_paths.length; i += 1) {88 for (size_t i = 0; i < lib_paths.length; i += 1) {
59 Buf *lib_path = lib_paths.at(i);89 Buf *lib_path = lib_paths.at(i);
90 detect_dynamic_linker(lib_path);
60 if ((err = cache_add_file(ch, lib_path)))91 if ((err = cache_add_file(ch, lib_path)))
61 return err;92 return err;
62#if defined(ZIG_OS_LINUX) && defined(ZIG_ARCH_X86_64)
63 if (buf_ends_with_str(lib_path, "ld-linux-x86-64.so.2")) {
64 buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
65 } else if (buf_ends_with_str(lib_path, "ld-musl-x86-64.so.1")) {
66 buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
67 }
68#endif
69 }93 }
70 if ((err = cache_final(ch, &saved_compiler_id)))94 if ((err = cache_final(ch, &saved_compiler_id)))
71 return err;95 return err;
...@@ -76,12 +100,6 @@ Error get_compiler_id(Buf **result) {...@@ -76,12 +100,6 @@ Error get_compiler_id(Buf **result) {
76 return ErrorNone;100 return ErrorNone;
77}101}
78102
79Buf *get_self_dynamic_linker_path(void) {
80 Buf *dontcare;
81 (void)get_compiler_id(&dontcare); // for the side-effects of caching the dynamic linker path
82 return (saved_dynamic_linker_path.list.length == 0) ? nullptr : &saved_dynamic_linker_path;
83}
84
85static bool test_zig_install_prefix(Buf *test_path, Buf *out_zig_lib_dir) {103static bool test_zig_install_prefix(Buf *test_path, Buf *out_zig_lib_dir) {
86 Buf lib_buf = BUF_INIT;104 Buf lib_buf = BUF_INIT;
87 buf_init_from_str(&lib_buf, "lib");105 buf_init_from_str(&lib_buf, "lib");
src/link.cpp+84-4
...@@ -62,14 +62,22 @@ static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFil...@@ -62,14 +62,22 @@ static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFil
62 return buf_ptr(&child_gen->output_file_path);62 return buf_ptr(&child_gen->output_file_path);
63}63}
6464
65static const char *path_from_libc(CodeGen *g, const char *subpath) {65static const char *path_from_zig_lib(CodeGen *g, const char *dir, const char *subpath) {
66 Buf *libc_dir = buf_alloc();66 Buf *dir1 = buf_alloc();
67 os_path_join(g->zig_lib_dir, buf_create_from_str("libc"), libc_dir);67 os_path_join(g->zig_lib_dir, buf_create_from_str(dir), dir1);
68 Buf *result = buf_alloc();68 Buf *result = buf_alloc();
69 os_path_join(libc_dir, buf_create_from_str(subpath), result);69 os_path_join(dir1, buf_create_from_str(subpath), result);
70 return buf_ptr(result);70 return buf_ptr(result);
71}71}
7272
73static const char *path_from_libc(CodeGen *g, const char *subpath) {
74 return path_from_zig_lib(g, "libc", subpath);
75}
76
77static const char *path_from_libunwind(CodeGen *g, const char *subpath) {
78 return path_from_zig_lib(g, "libunwind", subpath);
79}
80
73static const char *build_dummy_so(CodeGen *parent, const char *name, size_t major_version) {81static const char *build_dummy_so(CodeGen *parent, const char *name, size_t major_version) {
74 Buf *glibc_dummy_root_src = buf_sprintf("%s" OS_SEP "libc" OS_SEP "dummy" OS_SEP "%s.zig",82 Buf *glibc_dummy_root_src = buf_sprintf("%s" OS_SEP "libc" OS_SEP "dummy" OS_SEP "%s.zig",
75 buf_ptr(parent->zig_lib_dir), name);83 buf_ptr(parent->zig_lib_dir), name);
...@@ -81,6 +89,70 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo...@@ -81,6 +89,70 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo
81 return buf_ptr(&child_gen->output_file_path);89 return buf_ptr(&child_gen->output_file_path);
82}90}
8391
92static const char *build_libunwind(CodeGen *parent) {
93 CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr);
94 codegen_set_out_name(child_gen, buf_create_from_str("unwind"));
95 child_gen->is_static = true;
96 LinkLib *new_link_lib = codegen_add_link_lib(child_gen, buf_create_from_str("c"));
97 new_link_lib->provided_explicitly = false;
98 enum SrcKind {
99 SrcCpp,
100 SrcC,
101 SrcAsm,
102 };
103 static const struct {
104 const char *path;
105 SrcKind kind;
106 } unwind_src[] = {
107 {"src" OS_SEP "libunwind.cpp", SrcCpp},
108 {"src" OS_SEP "Unwind-EHABI.cpp", SrcCpp},
109 {"src" OS_SEP "Unwind-seh.cpp", SrcCpp},
110
111 {"src" OS_SEP "UnwindLevel1.c", SrcC},
112 {"src" OS_SEP "UnwindLevel1-gcc-ext.c", SrcC},
113 {"src" OS_SEP "Unwind-sjlj.c", SrcC},
114
115 {"src" OS_SEP "UnwindRegistersRestore.S", SrcAsm},
116 {"src" OS_SEP "UnwindRegistersSave.S", SrcAsm},
117 };
118 ZigList<CFile *> c_source_files = {0};
119 for (size_t i = 0; i < array_length(unwind_src); i += 1) {
120 CFile *c_file = allocate<CFile>(1);
121 c_file->source_path = path_from_libunwind(parent, unwind_src[i].path);
122 switch (unwind_src[i].kind) {
123 case SrcC:
124 c_file->args.append("-std=c99");
125 break;
126 case SrcCpp:
127 c_file->args.append("-fno-rtti");
128 c_file->args.append("-I");
129 c_file->args.append(path_from_zig_lib(parent, "libcxx", "include"));
130 break;
131 case SrcAsm:
132 break;
133 }
134 c_file->args.append("-I");
135 c_file->args.append(path_from_libunwind(parent, "include"));
136 c_file->args.append("-fPIC");
137 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
138 if (parent->zig_target->is_native) {
139 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
140 }
141 if (parent->build_mode == BuildModeDebug) {
142 c_file->args.append("-D_DEBUG");
143 } else {
144 c_file->args.append("-NDEBUG");
145 }
146 if (parent->is_single_threaded) {
147 c_file->args.append("-D_LIBUNWIND_HAS_NO_THREADS");
148 }
149 c_source_files.append(c_file);
150 }
151 child_gen->c_source_files = c_source_files;
152 codegen_build_and_link(child_gen);
153 return buf_ptr(&child_gen->output_file_path);
154}
155
84static const char *get_libc_crt_file(CodeGen *parent, const char *file) {156static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
85 if (parent->libc == nullptr && target_is_glibc(parent)) {157 if (parent->libc == nullptr && target_is_glibc(parent)) {
86 if (strcmp(file, "crti.o") == 0) {158 if (strcmp(file, "crti.o") == 0) {
...@@ -611,6 +683,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -611,6 +683,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
611 lj->args.append("-lgcc_s");683 lj->args.append("-lgcc_s");
612 lj->args.append("--no-as-needed");684 lj->args.append("--no-as-needed");
613 } else if (target_is_glibc(g)) {685 } else if (target_is_glibc(g)) {
686 lj->args.append(build_libunwind(g));
614 lj->args.append(get_libc_crt_file(g, "libc.so.6")); // this is our dummy so file687 lj->args.append(get_libc_crt_file(g, "libc.so.6")); // this is our dummy so file
615 lj->args.append(get_libc_crt_file(g, "libc_nonshared.a"));688 lj->args.append(get_libc_crt_file(g, "libc_nonshared.a"));
616 } else {689 } else {
...@@ -1192,6 +1265,13 @@ void codegen_link(CodeGen *g) {...@@ -1192,6 +1265,13 @@ void codegen_link(CodeGen *g) {
1192 }1265 }
1193 ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os);1266 ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os);
1194 codegen_add_time_event(g, "LLVM Link");1267 codegen_add_time_event(g, "LLVM Link");
1268 if (g->verbose_link) {
1269 fprintf(stderr, "ar rcs %s", buf_ptr(&g->output_file_path));
1270 for (size_t i = 0; i < g->link_objects.length; i += 1) {
1271 fprintf(stderr, " %s", (const char *)buf_ptr(g->link_objects.at(i)));
1272 }
1273 fprintf(stderr, "\n");
1274 }
1195 if (ZigLLVMWriteArchive(buf_ptr(&g->output_file_path), file_names.items, file_names.length, os_type)) {1275 if (ZigLLVMWriteArchive(buf_ptr(&g->output_file_path), file_names.items, file_names.length, os_type)) {
1196 fprintf(stderr, "Unable to write archive '%s'\n", buf_ptr(&g->output_file_path));1276 fprintf(stderr, "Unable to write archive '%s'\n", buf_ptr(&g->output_file_path));
1197 exit(1);1277 exit(1);