From ecd2332bb5c74739ade40f1b9eaf412b4642a36f Mon Sep 17 00:00:00 2001 From: emekoi Date: Fri, 29 Mar 2019 19:25:47 -0500 Subject: made lld flags on windows consistent --- src/link.cpp | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index c2d46f0ac6..65f4060105 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1122,14 +1122,14 @@ static bool zig_lld_link(ZigLLVM_ObjectFormatType oformat, const char **args, si } static void add_uefi_link_args(LinkJob *lj) { - lj->args.append("/BASE:0"); - lj->args.append("/ENTRY:EfiMain"); - lj->args.append("/OPT:REF"); - lj->args.append("/SAFESEH:NO"); - lj->args.append("/MERGE:.rdata=.data"); - lj->args.append("/ALIGN:32"); - lj->args.append("/NODEFAULTLIB"); - lj->args.append("/SECTION:.xdata,D"); + lj->args.append("-BASE:0"); + lj->args.append("-ENTRY:EfiMain"); + lj->args.append("-OPT:REF"); + lj->args.append("-SAFESEH:NO"); + lj->args.append("-MERGE:.rdata=.data"); + lj->args.append("-ALIGN:32"); + lj->args.append("-NODEFAULTLIB"); + lj->args.append("-SECTION:.xdata,D"); } static void add_nt_link_args(LinkJob *lj, bool is_library) { @@ -1163,12 +1163,12 @@ static void add_nt_link_args(LinkJob *lj, bool is_library) { lj->args.append("kernel32.lib"); lj->args.append("ntdll.lib"); } else { - lj->args.append("/NODEFAULTLIB"); + lj->args.append("-NODEFAULTLIB"); if (!is_library) { if (g->have_winmain) { - lj->args.append("/ENTRY:WinMain"); + lj->args.append("-ENTRY:WinMain"); } else { - lj->args.append("/ENTRY:WinMainCRTStartup"); + lj->args.append("-ENTRY:WinMainCRTStartup"); } } } @@ -1178,17 +1178,17 @@ static void construct_linker_job_coff(LinkJob *lj) { Error err; CodeGen *g = lj->codegen; - lj->args.append("/ERRORLIMIT:0"); + lj->args.append("-ERRORLIMIT:0"); - lj->args.append("/NOLOGO"); + lj->args.append("-NOLOGO"); if (!g->strip_debug_symbols) { - lj->args.append("/DEBUG"); + lj->args.append("-DEBUG"); } if (g->out_type == OutTypeExe) { // TODO compile time stack upper bound detection - lj->args.append("/STACK:16777216"); + lj->args.append("-STACK:16777216"); } coff_append_machine_arg(g, &lj->args); @@ -1203,35 +1203,35 @@ static void construct_linker_job_coff(LinkJob *lj) { } break; case TargetSubsystemConsole: - lj->args.append("/SUBSYSTEM:console"); + lj->args.append("-SUBSYSTEM:console"); add_nt_link_args(lj, is_library); break; case TargetSubsystemEfiApplication: - lj->args.append("/SUBSYSTEM:efi_application"); + lj->args.append("-SUBSYSTEM:efi_application"); add_uefi_link_args(lj); break; case TargetSubsystemEfiBootServiceDriver: - lj->args.append("/SUBSYSTEM:efi_boot_service_driver"); + lj->args.append("-SUBSYSTEM:efi_boot_service_driver"); add_uefi_link_args(lj); break; case TargetSubsystemEfiRom: - lj->args.append("/SUBSYSTEM:efi_rom"); + lj->args.append("-SUBSYSTEM:efi_rom"); add_uefi_link_args(lj); break; case TargetSubsystemEfiRuntimeDriver: - lj->args.append("/SUBSYSTEM:efi_runtime_driver"); + lj->args.append("-SUBSYSTEM:efi_runtime_driver"); add_uefi_link_args(lj); break; case TargetSubsystemNative: - lj->args.append("/SUBSYSTEM:native"); + lj->args.append("-SUBSYSTEM:native"); add_nt_link_args(lj, is_library); break; case TargetSubsystemPosix: - lj->args.append("/SUBSYSTEM:posix"); + lj->args.append("-SUBSYSTEM:posix"); add_nt_link_args(lj, is_library); break; case TargetSubsystemWindows: - lj->args.append("/SUBSYSTEM:windows"); + lj->args.append("-SUBSYSTEM:windows"); add_nt_link_args(lj, is_library); break; } -- cgit v1.2.3 From 6cc443ee457d06ea23713bb78ab832d886029d57 Mon Sep 17 00:00:00 2001 From: emekoi Date: Fri, 29 Mar 2019 20:37:15 -0500 Subject: added code for linking libc on mingw --- src/link.cpp | 200 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 144 insertions(+), 56 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 65f4060105..d7532d96e3 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1132,40 +1132,114 @@ static void add_uefi_link_args(LinkJob *lj) { lj->args.append("-SECTION:.xdata,D"); } -static void add_nt_link_args(LinkJob *lj, bool is_library) { +static void add_msvc_link_args(LinkJob *lj, bool is_library) { CodeGen *g = lj->codegen; - if (lj->link_in_crt) { - // TODO: https://github.com/ziglang/zig/issues/2064 - bool is_dynamic = true; // g->is_dynamic; - const char *lib_str = is_dynamic ? "" : "lib"; - const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : ""; - - if (!is_dynamic) { - Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str); - lj->args.append(buf_ptr(cmt_lib_name)); + // TODO: https://github.com/ziglang/zig/issues/2064 + bool is_dynamic = true; // g->is_dynamic; + const char *lib_str = is_dynamic ? "" : "lib"; + const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : ""; + + if (!is_dynamic) { + Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str); + lj->args.append(buf_ptr(cmt_lib_name)); + } else { + Buf *msvcrt_lib_name = buf_sprintf("msvcrt%s.lib", d_str); + lj->args.append(buf_ptr(msvcrt_lib_name)); + } + + Buf *vcruntime_lib_name = buf_sprintf("%svcruntime%s.lib", lib_str, d_str); + lj->args.append(buf_ptr(vcruntime_lib_name)); + + Buf *crt_lib_name = buf_sprintf("%sucrt%s.lib", lib_str, d_str); + lj->args.append(buf_ptr(crt_lib_name)); + + //Visual C++ 2015 Conformance Changes + //https://msdn.microsoft.com/en-us/library/bb531344.aspx + lj->args.append("legacy_stdio_definitions.lib"); + + // msvcrt depends on kernel32 and ntdll + lj->args.append("kernel32.lib"); + lj->args.append("ntdll.lib"); +} + +static const char *get_libc_file(ZigLibCInstallation *lib, const char *file) { + Buf *out_buf = buf_alloc(); + os_path_join(&lib->crt_dir, buf_create_from_str(file), out_buf); + return buf_ptr(out_buf); +} + +static const char *get_libc_static_file(ZigLibCInstallation *lib, const char *file) { + Buf *static_crt_dir = buf_alloc(); + Buf *out_buf = buf_alloc(); + if (zig_libc_cc_print_file_name(file, static_crt_dir, true, true) != ErrorNone) { + abort(); + } + os_path_join(static_crt_dir, buf_create_from_str(file), out_buf); + return buf_ptr(out_buf); +} + +static void add_gnu_link_args(LinkJob *lj, bool is_library) { + CodeGen *g = lj->codegen; + + bool is_dll = g->out_type == OutTypeLib && g->is_dynamic; + + if (g->zig_target->arch == ZigLLVM_x86) { + lj->args.append("-ALTERNATENAME:__image_base__=___ImageBase"); + } else { + lj->args.append("-ALTERNATENAME:__image_base__=__ImageBase"); + } + + if (is_dll) { + lj->args.append(get_libc_file(g->libc, "dllcrt2.o")); + } else { + lj->args.append(get_libc_file(g->libc, "crt2.o")); + } + + lj->args.append(get_libc_static_file(g->libc, "crtbegin.o")); + + if (g->libc_link_lib != nullptr) { + lj->args.append("libmingw32.a"); + + if (is_dll) { + lj->args.append(get_libc_static_file(g->libc, "libgcc_s.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); } else { - Buf *msvcrt_lib_name = buf_sprintf("msvcrt%s.lib", d_str); - lj->args.append(buf_ptr(msvcrt_lib_name)); + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc_eh.a")); } - Buf *vcruntime_lib_name = buf_sprintf("%svcruntime%s.lib", lib_str, d_str); - lj->args.append(buf_ptr(vcruntime_lib_name)); + lj->args.append(get_libc_static_file(g->libc, "libssp.a")); + lj->args.append("libmoldname.a"); + lj->args.append("libmingwex.a"); + lj->args.append("libmsvcrt.a"); - Buf *crt_lib_name = buf_sprintf("%sucrt%s.lib", lib_str, d_str); - lj->args.append(buf_ptr(crt_lib_name)); + if (g->subsystem == TargetSubsystemWindows) { + lj->args.append("libgdi32.a"); + lj->args.append("libcomdlg32.a"); + } - //Visual C++ 2015 Conformance Changes - //https://msdn.microsoft.com/en-us/library/bb531344.aspx - lj->args.append("legacy_stdio_definitions.lib"); + lj->args.append("libadvapi32.a"); + lj->args.append("libadvapi32.a"); + lj->args.append("libshell32.a"); + lj->args.append("libuser32.a"); + lj->args.append("libkernel32.a"); - // msvcrt depends on kernel32 and ntdll - lj->args.append("kernel32.lib"); - lj->args.append("ntdll.lib"); + lj->args.append(get_libc_static_file(g->libc, "crtend.o")); + } +} + +static void add_win_link_args(LinkJob *lj, bool is_library) { + if (lj->link_in_crt) { + if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { + add_gnu_link_args(lj, is_library); + } else { + add_msvc_link_args(lj, is_library); + } } else { lj->args.append("-NODEFAULTLIB"); if (!is_library) { - if (g->have_winmain) { + if (lj->codegen->have_winmain) { lj->args.append("-ENTRY:WinMain"); } else { lj->args.append("-ENTRY:WinMainCRTStartup"); @@ -1194,17 +1268,46 @@ static void construct_linker_job_coff(LinkJob *lj) { coff_append_machine_arg(g, &lj->args); bool is_library = g->out_type == OutTypeLib; + if (is_library && g->is_dynamic) { + lj->args.append("-DLL"); + } + + lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&g->output_file_path)))); + + if (g->libc_link_lib != nullptr) { + assert(g->libc != nullptr); + + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir)))); + + if (target_abi_is_gnu(g->zig_target->abi)) { + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->sys_include_dir)))); + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->include_dir)))); + } else { + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir)))); + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->kernel32_lib_dir)))); + } + } + + for (size_t i = 0; i < g->lib_dirs.length; i += 1) { + const char *lib_dir = g->lib_dirs.at(i); + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", lib_dir))); + } + + for (size_t i = 0; i < g->link_objects.length; i += 1) { + lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); + } + switch (g->subsystem) { case TargetSubsystemAuto: if (g->zig_target->os == OsUefi) { add_uefi_link_args(lj); } else { - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); } break; case TargetSubsystemConsole: lj->args.append("-SUBSYSTEM:console"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; case TargetSubsystemEfiApplication: lj->args.append("-SUBSYSTEM:efi_application"); @@ -1224,41 +1327,18 @@ static void construct_linker_job_coff(LinkJob *lj) { break; case TargetSubsystemNative: lj->args.append("-SUBSYSTEM:native"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; case TargetSubsystemPosix: lj->args.append("-SUBSYSTEM:posix"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; case TargetSubsystemWindows: lj->args.append("-SUBSYSTEM:windows"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; } - lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&g->output_file_path)))); - - if (g->libc_link_lib != nullptr) { - assert(g->libc != nullptr); - - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir)))); - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->kernel32_lib_dir)))); - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir)))); - } - - if (is_library && g->is_dynamic) { - lj->args.append("-DLL"); - } - - for (size_t i = 0; i < g->lib_dirs.length; i += 1) { - const char *lib_dir = g->lib_dirs.at(i); - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", lib_dir))); - } - - for (size_t i = 0; i < g->link_objects.length; i += 1) { - lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); - } - if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) { if (g->libc_link_lib == nullptr && !g->is_dummy_so) { Buf *builtin_a_path = build_a(g, "builtin"); @@ -1278,11 +1358,19 @@ static void construct_linker_job_coff(LinkJob *lj) { continue; } if (link_lib->provided_explicitly) { - if (lj->codegen->zig_target->abi == ZigLLVM_GNU) { - Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name)); - lj->args.append(buf_ptr(arg)); - } - else { + if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { + static const char *test_names[3] = { "lib%s.dll.a", "lib%s.a", nullptr }; + + for (size_t i = 0; test_names[i] != nullptr; i++) { + Buf *test_path = buf_sprintf(test_names[i], buf_ptr(link_lib->name)); + bool exists = false; + if (os_file_exists(test_path, &exists) != ErrorNone) { + zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); + } else if (exists) { + lj->args.append(buf_ptr(test_path)); + } + } + } else { lj->args.append(buf_ptr(link_lib->name)); } } else { -- cgit v1.2.3 From 0bd4901a174786a61fadf484f3b8932169533cd4 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 30 Mar 2019 00:06:04 -0500 Subject: fixed linking of system libraries on mingw --- src/libc_installation.hpp | 2 -- src/link.cpp | 26 +++++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src/link.cpp') diff --git a/src/libc_installation.hpp b/src/libc_installation.hpp index 765ae4ec56..171cc963bf 100644 --- a/src/libc_installation.hpp +++ b/src/libc_installation.hpp @@ -29,8 +29,6 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file); Error ATTRIBUTE_MUST_USE zig_libc_find_native(ZigLibCInstallation *self, bool verbose); -#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_WINDOWS) Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose); -#endif #endif diff --git a/src/link.cpp b/src/link.cpp index d7532d96e3..00d70d5e63 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1359,17 +1359,25 @@ static void construct_linker_job_coff(LinkJob *lj) { } if (link_lib->provided_explicitly) { if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { - static const char *test_names[3] = { "lib%s.dll.a", "lib%s.a", nullptr }; - - for (size_t i = 0; test_names[i] != nullptr; i++) { - Buf *test_path = buf_sprintf(test_names[i], buf_ptr(link_lib->name)); - bool exists = false; - if (os_file_exists(test_path, &exists) != ErrorNone) { - zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); - } else if (exists) { - lj->args.append(buf_ptr(test_path)); + static const char *test_names[3] = { "%s\\lib%s.dll.a", "%s\\lib%s.a", nullptr }; + bool exists = false; + + for (size_t i = 0; test_names[i] != nullptr && !exists; i++) { + for (size_t j = 0; j < g->lib_dirs.length; j += 1) { + const char *lib_dir = g->lib_dirs.at(j); + Buf *test_path = buf_sprintf(test_names[i], lib_dir, buf_ptr(link_lib->name)); + if (os_file_exists(test_path, &exists) != ErrorNone) { + zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); + } else if (exists) { + lj->args.append(buf_ptr(test_path)); + break; + } } } + + if (!exists) { + zig_panic("link: unable to find library: %s", buf_ptr(link_lib->name)); + } } else { lj->args.append(buf_ptr(link_lib->name)); } -- cgit v1.2.3 From 666e8799251452f74ca97b4a6a930c7b3d0ce553 Mon Sep 17 00:00:00 2001 From: Shritesh Bhattarai Date: Wed, 10 Apr 2019 17:14:44 -0500 Subject: Build compiler_rt for WASM exe --- src/link.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index d6093581f7..70f91f2b8e 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1101,6 +1101,16 @@ static void construct_linker_job_wasm(LinkJob *lj) { for (size_t i = 0; i < g->link_objects.length; i += 1) { lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); } + + if (g->out_type == OutTypeExe) { + if (g->libc_link_lib == nullptr) { + Buf *builtin_a_path = build_a(g, "builtin"); + lj->args.append(buf_ptr(builtin_a_path)); + } + + Buf *compiler_rt_o_path = build_compiler_rt(g); + lj->args.append(buf_ptr(compiler_rt_o_path)); + } } static void coff_append_machine_arg(CodeGen *g, ZigList *list) { -- cgit v1.2.3 From 27cd830ec8ca1ddba692c65e8a129a2b5c9a1673 Mon Sep 17 00:00:00 2001 From: Matthew Iannucci Date: Thu, 11 Apr 2019 13:15:17 -0400 Subject: Add initial support for iOS targets (#2237) * Add iOS C int sizes... try to hack in iOS DebugInfo * Get rid of ios link check for now * Remove macos linkversion check --- src/link.cpp | 10 ---------- src/target.cpp | 17 ++++++++++++++++- std/debug.zig | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 70f91f2b8e..2a152e1d1e 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1562,16 +1562,6 @@ static void construct_linker_job_macho(LinkJob *lj) { lj->args.append("dynamic_lookup"); } - if (platform.kind == MacOS) { - if (darwin_version_lt(&platform, 10, 5)) { - lj->args.append("-lgcc_s.10.4"); - } else if (darwin_version_lt(&platform, 10, 6)) { - lj->args.append("-lgcc_s.10.5"); - } - } else { - zig_panic("TODO"); - } - for (size_t i = 0; i < g->darwin_frameworks.length; i += 1) { lj->args.append("-framework"); lj->args.append(buf_ptr(g->darwin_frameworks.at(i))); diff --git a/src/target.cpp b/src/target.cpp index dda8188765..3b4265359c 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -894,10 +894,25 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) { case CIntTypeCount: zig_unreachable(); } + case OsIOS: + switch (id) { + case CIntTypeShort: + case CIntTypeUShort: + return 16; + case CIntTypeInt: + case CIntTypeUInt: + return 32; + case CIntTypeLong: + case CIntTypeULong: + case CIntTypeLongLong: + case CIntTypeULongLong: + return 64; + case CIntTypeCount: + zig_unreachable(); + } case OsAnanas: case OsCloudABI: case OsDragonFly: - case OsIOS: case OsKFreeBSD: case OsLv2: case OsSolaris: diff --git a/std/debug.zig b/std/debug.zig index c85a982059..bfcfbc6e77 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1178,7 +1178,7 @@ pub const DwarfInfo = struct { }; pub const DebugInfo = switch (builtin.os) { - builtin.Os.macosx => struct { + builtin.Os.macosx, builtin.Os.ios => struct { symbols: []const MachoSymbol, strings: []const u8, ofiles: OFileTable, -- cgit v1.2.3 From a895c59971d16481a85b6bff7618a3f9e2a4e0ae Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 11 Apr 2019 14:33:43 -0400 Subject: delete unused function missed by 27cd830ec8ca1dd --- src/link.cpp | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 2a152e1d1e..78e549c80b 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1426,18 +1426,6 @@ static void get_darwin_platform(LinkJob *lj, DarwinPlatform *platform) { } } -static bool darwin_version_lt(DarwinPlatform *platform, int major, int minor) { - if (platform->major < major) { - return true; - } else if (platform->major > major) { - return false; - } - if (platform->minor < minor) { - return true; - } - return false; -} - static void construct_linker_job_macho(LinkJob *lj) { CodeGen *g = lj->codegen; -- cgit v1.2.3 From 94e0871603add796e87ac53a3271cf5b9b3d6b0e Mon Sep 17 00:00:00 2001 From: Shritesh Bhattarai Date: Sun, 14 Apr 2019 00:03:32 -0500 Subject: wasi: don't pass --no-entry to linker --- src/link.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 78e549c80b..6c6ff07ba9 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1091,7 +1091,9 @@ static void construct_linker_job_wasm(LinkJob *lj) { CodeGen *g = lj->codegen; lj->args.append("-error-limit=0"); - lj->args.append("--no-entry"); // So lld doesn't look for _start. + if (g->zig_target->os != OsWASI) { + lj->args.append("--no-entry"); // So lld doesn't look for _start. + } lj->args.append("--allow-undefined"); lj->args.append("--export-all"); lj->args.append("-o"); -- cgit v1.2.3 From 6692cbbe18ad108affccfb9cb85d53e2583408ec Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 15 Apr 2019 20:41:50 -0400 Subject: disable threads when linking WebAssembly to work around an LLD bug See #2283 --- src/link.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 6c6ff07ba9..ad4bd7765c 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1091,6 +1091,11 @@ static void construct_linker_job_wasm(LinkJob *lj) { CodeGen *g = lj->codegen; lj->args.append("-error-limit=0"); + + // This works around a deadlock in LLD's wasm code. + // See https://github.com/ziglang/zig/issues/2283. + lj->args.append("--no-threads"); + if (g->zig_target->os != OsWASI) { lj->args.append("--no-entry"); // So lld doesn't look for _start. } -- cgit v1.2.3 From f488f3fd03d12ea02240855d4784cc2e283af7ed Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 16 Apr 2019 03:58:15 -0400 Subject: remove workaround for LLD bug Zig's embedded LLD now has a patch to resolve the deadlock race condition, and the patch is getting upstreamed too, so this closes #2283. --- src/link.cpp | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index ad4bd7765c..b85ac29e75 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1092,10 +1092,6 @@ static void construct_linker_job_wasm(LinkJob *lj) { lj->args.append("-error-limit=0"); - // This works around a deadlock in LLD's wasm code. - // See https://github.com/ziglang/zig/issues/2283. - lj->args.append("--no-threads"); - if (g->zig_target->os != OsWASI) { lj->args.append("--no-entry"); // So lld doesn't look for _start. } -- cgit v1.2.3 From f4c5fa7ff46ad088e437dba237518df76736f087 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 20 Apr 2019 00:12:33 -0500 Subject: renamed add_gnu_link_args --- src/link.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 00d70d5e63..db5494ee54 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1179,7 +1179,7 @@ static const char *get_libc_static_file(ZigLibCInstallation *lib, const char *fi return buf_ptr(out_buf); } -static void add_gnu_link_args(LinkJob *lj, bool is_library) { +static void add_mingw_link_args(LinkJob *lj, bool is_library) { CodeGen *g = lj->codegen; bool is_dll = g->out_type == OutTypeLib && g->is_dynamic; @@ -1232,7 +1232,7 @@ static void add_gnu_link_args(LinkJob *lj, bool is_library) { static void add_win_link_args(LinkJob *lj, bool is_library) { if (lj->link_in_crt) { if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { - add_gnu_link_args(lj, is_library); + add_mingw_link_args(lj, is_library); } else { add_msvc_link_args(lj, is_library); } -- cgit v1.2.3 From dfada0cc77cb77a337baa784e4e96c7a94d217bb Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 27 Apr 2019 00:24:26 -0500 Subject: added static_crt_dir to libc file --- src/libc_installation.cpp | 36 +++++++++++++++++++---- src/libc_installation.hpp | 3 +- src/link.cpp | 73 ++++++++++++++++------------------------------- 3 files changed, 56 insertions(+), 56 deletions(-) (limited to 'src/link.cpp') diff --git a/src/libc_installation.cpp b/src/libc_installation.cpp index 0862ffec9b..243b8d6400 100644 --- a/src/libc_installation.cpp +++ b/src/libc_installation.cpp @@ -14,6 +14,7 @@ static const char *zig_libc_keys[] = { "include_dir", "sys_include_dir", "crt_dir", + "static_crt_dir", "msvc_lib_dir", "kernel32_lib_dir", }; @@ -34,6 +35,7 @@ static void zig_libc_init_empty(ZigLibCInstallation *libc) { buf_init_from_str(&libc->include_dir, ""); buf_init_from_str(&libc->sys_include_dir, ""); buf_init_from_str(&libc->crt_dir, ""); + buf_init_from_str(&libc->static_crt_dir, ""); buf_init_from_str(&libc->msvc_lib_dir, ""); buf_init_from_str(&libc->kernel32_lib_dir, ""); } @@ -74,8 +76,9 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir); match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->sys_include_dir); match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->crt_dir); - match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->msvc_lib_dir); - match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->kernel32_lib_dir); + match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_crt_dir); + match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir); + match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->kernel32_lib_dir); } for (size_t i = 0; i < zig_libc_keys_len; i += 1) { @@ -110,6 +113,15 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget } } + if (buf_len(&libc->static_crt_dir) == 0) { + if (target->os == OsWindows && target_abi_is_gnu(target->abi)) { + if (verbose) { + fprintf(stderr, "static_crt_dir may not be empty for %s\n", target_os_name(target->os)); + } + return ErrorSemanticAnalyzeFail; + } + } + if (buf_len(&libc->msvc_lib_dir) == 0) { if (target->os == OsWindows && !target_abi_is_gnu(target->abi)) { if (verbose) { @@ -261,7 +273,7 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b return ErrorFileNotFound; } -Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { +static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { const char *cc_exe = getenv("CC"); cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe; ZigList args = {}; @@ -308,6 +320,10 @@ static Error zig_libc_find_native_crt_dir_posix(ZigLibCInstallation *self, bool return zig_libc_cc_print_file_name("crt1.o", &self->crt_dir, true, verbose); } +static Error zig_libc_find_native_static_crt_dir_posix(ZigLibCInstallation *self, bool verbose) { + return zig_libc_cc_print_file_name("crtbegin.o", &self->static_crt_dir, true, verbose); +} + #if defined(ZIG_OS_WINDOWS) static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { Error err; @@ -320,7 +336,7 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, return ErrorNone; } -static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, +static Error zig_libc_find_native_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, bool verbose) { Error err; @@ -396,11 +412,16 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { "# On POSIX it's the directory that includes `sys/errno.h`.\n" "sys_include_dir=%s\n" "\n" - "# The directory that contains `crt1.o`.\n" + "# The directory that contains `crt1.o` or `crt2.o`.\n" "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n" "# Not needed when targeting MacOS.\n" "crt_dir=%s\n" "\n" + "# The directory that contains `crtbegin.o`.\n" + "# On POSIX, can be found with `cc -print-file-name=crtbegin.o`.\n" + "# Not needed when targeting MacOS.\n" + "static_crt_dir=%s\n" + "\n" "# The directory that contains `vcruntime.lib`.\n" "# Only needed when targeting MSVC on Windows.\n" "msvc_lib_dir=%s\n" @@ -413,6 +434,7 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { buf_ptr(&self->include_dir), buf_ptr(&self->sys_include_dir), buf_ptr(&self->crt_dir), + buf_ptr(&self->static_crt_dir), buf_ptr(&self->msvc_lib_dir), buf_ptr(&self->kernel32_lib_dir) ); @@ -429,6 +451,8 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { return err; if ((err = zig_libc_find_native_crt_dir_posix(self, verbose))) return err; + if ((err = zig_libc_find_native_static_crt_dir_posix(self, verbose))) + return err; return ErrorNone; } else { ZigWindowsSDK *sdk; @@ -442,7 +466,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { return err; if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose))) return err; - if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose))) + if ((err = zig_libc_find_native_crt_dir_windows(self, sdk, &native_target, verbose))) return err; return ErrorNone; case ZigFindWindowsSdkErrorOutOfMemory: diff --git a/src/libc_installation.hpp b/src/libc_installation.hpp index 171cc963bf..da06ac39ee 100644 --- a/src/libc_installation.hpp +++ b/src/libc_installation.hpp @@ -19,6 +19,7 @@ struct ZigLibCInstallation { Buf include_dir; Buf sys_include_dir; Buf crt_dir; + Buf static_crt_dir; Buf msvc_lib_dir; Buf kernel32_lib_dir; }; @@ -29,6 +30,4 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file); Error ATTRIBUTE_MUST_USE zig_libc_find_native(ZigLibCInstallation *self, bool verbose); -Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose); - #endif diff --git a/src/link.cpp b/src/link.cpp index db5494ee54..ad1a60065d 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1170,12 +1170,8 @@ static const char *get_libc_file(ZigLibCInstallation *lib, const char *file) { } static const char *get_libc_static_file(ZigLibCInstallation *lib, const char *file) { - Buf *static_crt_dir = buf_alloc(); Buf *out_buf = buf_alloc(); - if (zig_libc_cc_print_file_name(file, static_crt_dir, true, true) != ErrorNone) { - abort(); - } - os_path_join(static_crt_dir, buf_create_from_str(file), out_buf); + os_path_join(&lib->static_crt_dir, buf_create_from_str(file), out_buf); return buf_ptr(out_buf); } @@ -1198,35 +1194,33 @@ static void add_mingw_link_args(LinkJob *lj, bool is_library) { lj->args.append(get_libc_static_file(g->libc, "crtbegin.o")); - if (g->libc_link_lib != nullptr) { - lj->args.append("libmingw32.a"); + lj->args.append(get_libc_file(g->libc, "libmingw32.a")); - if (is_dll) { - lj->args.append(get_libc_static_file(g->libc, "libgcc_s.a")); - lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); - } else { - lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); - lj->args.append(get_libc_static_file(g->libc, "libgcc_eh.a")); - } + if (is_dll) { + lj->args.append(get_libc_static_file(g->libc, "libgcc_s.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); + } else { + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc_eh.a")); + } - lj->args.append(get_libc_static_file(g->libc, "libssp.a")); - lj->args.append("libmoldname.a"); - lj->args.append("libmingwex.a"); - lj->args.append("libmsvcrt.a"); + lj->args.append(get_libc_static_file(g->libc, "libssp.a")); + lj->args.append(get_libc_file(g->libc, "libmoldname.a")); + lj->args.append(get_libc_file(g->libc, "libmingwex.a")); + lj->args.append(get_libc_file(g->libc, "libmsvcrt.a")); - if (g->subsystem == TargetSubsystemWindows) { - lj->args.append("libgdi32.a"); - lj->args.append("libcomdlg32.a"); - } + if (g->subsystem == TargetSubsystemWindows) { + lj->args.append(get_libc_file(g->libc, "libgdi32.a")); + lj->args.append(get_libc_file(g->libc, "libcomdlg32.a")); + } - lj->args.append("libadvapi32.a"); - lj->args.append("libadvapi32.a"); - lj->args.append("libshell32.a"); - lj->args.append("libuser32.a"); - lj->args.append("libkernel32.a"); + lj->args.append(get_libc_file(g->libc, "libadvapi32.a")); + lj->args.append(get_libc_file(g->libc, "libadvapi32.a")); + lj->args.append(get_libc_file(g->libc, "libshell32.a")); + lj->args.append(get_libc_file(g->libc, "libuser32.a")); + lj->args.append(get_libc_file(g->libc, "libkernel32.a")); - lj->args.append(get_libc_static_file(g->libc, "crtend.o")); - } + lj->args.append(get_libc_static_file(g->libc, "crtend.o")); } static void add_win_link_args(LinkJob *lj, bool is_library) { @@ -1359,25 +1353,8 @@ static void construct_linker_job_coff(LinkJob *lj) { } if (link_lib->provided_explicitly) { if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { - static const char *test_names[3] = { "%s\\lib%s.dll.a", "%s\\lib%s.a", nullptr }; - bool exists = false; - - for (size_t i = 0; test_names[i] != nullptr && !exists; i++) { - for (size_t j = 0; j < g->lib_dirs.length; j += 1) { - const char *lib_dir = g->lib_dirs.at(j); - Buf *test_path = buf_sprintf(test_names[i], lib_dir, buf_ptr(link_lib->name)); - if (os_file_exists(test_path, &exists) != ErrorNone) { - zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); - } else if (exists) { - lj->args.append(buf_ptr(test_path)); - break; - } - } - } - - if (!exists) { - zig_panic("link: unable to find library: %s", buf_ptr(link_lib->name)); - } + Buf *lib_name = buf_sprintf("lib%s.a", buf_ptr(link_lib->name)); + lj->args.append(buf_ptr(lib_name)); } else { lj->args.append(buf_ptr(link_lib->name)); } -- cgit v1.2.3 From 8d58dc715b334c1e842e8c97fd345b561ef77fb8 Mon Sep 17 00:00:00 2001 From: Benjamin Feng Date: Sat, 27 Apr 2019 09:52:58 -0500 Subject: Switch wasm export-all to only values marked exports --- src/link.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index bd1587b459..385b28b71e 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1096,10 +1096,16 @@ static void construct_linker_job_wasm(LinkJob *lj) { lj->args.append("--no-entry"); // So lld doesn't look for _start. } lj->args.append("--allow-undefined"); - lj->args.append("--export-all"); lj->args.append("-o"); lj->args.append(buf_ptr(&g->output_file_path)); + auto export_it = g->exported_symbol_names.entry_iterator(); + decltype(g->exported_symbol_names)::Entry *curr_entry = nullptr; + while ((curr_entry = export_it.next()) != nullptr) { + Buf *arg = buf_sprintf("--export=%s", buf_ptr(curr_entry->key)); + lj->args.append(buf_ptr(arg)); + } + // .o files for (size_t i = 0; i < g->link_objects.length; i += 1) { lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); -- cgit v1.2.3 From 9bbd71c9ab3915b8bd8619a319a0eb6de2e2d152 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 8 May 2019 20:49:07 -0400 Subject: add --bundle-compiler-rt function to link options and use it when building libuserland.a The self-hosted part of stage1 relies on zig's compiler-rt, and so we include it in libuserland.a. This should potentially be the default, but for now it's behind a linker option. self-hosted translate-c: small progress on translating functions. --- build.zig | 1 + src-self-hosted/translate_c.zig | 38 +++- src/all_types.hpp | 1 + src/codegen.cpp | 1 + src/link.cpp | 28 +-- src/main.cpp | 5 + std/build.zig | 5 + std/special/compiler_rt.zig | 387 ++++++++++++++++++++-------------------- 8 files changed, 250 insertions(+), 216 deletions(-) (limited to 'src/link.cpp') diff --git a/build.zig b/build.zig index d9456e0d28..bdaf175945 100644 --- a/build.zig +++ b/build.zig @@ -389,6 +389,7 @@ fn addLibUserlandStep(b: *Builder) void { else b.addStaticLibrary("userland", "src-self-hosted/stage1.zig"); artifact.disable_gen_h = true; + artifact.bundle_compiler_rt = true; artifact.setTarget(builtin.arch, builtin.os, builtin.abi); artifact.linkSystemLibrary("c"); const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1"); diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index afe4ab0945..ddf7c6f21d 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -13,14 +13,21 @@ pub const Mode = enum { pub const ClangErrMsg = Stage2ErrorMsg; -pub const Error = error { - OutOfMemory, -}; +pub const Error = error{OutOfMemory}; const Context = struct { tree: *ast.Tree, source_buffer: *std.Buffer, err: Error, + + fn a(c: *Context) *std.mem.Allocator { + return &c.tree.arena_allocator.allocator; + } + + /// Convert a null-terminated C string to a slice allocated in the arena + fn str(c: *Context, s: [*]const u8) ![]u8 { + return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s)); + } }; pub fn translate( @@ -60,12 +67,13 @@ pub fn translate( tree.* = ast.Tree{ .source = undefined, // need to use Buffer.toOwnedSlice later .root_node = root_node, - .arena_allocator = tree_arena, + .arena_allocator = undefined, .tokens = ast.Tree.TokenList.init(arena), .errors = ast.Tree.ErrorList.init(arena), }; + tree.arena_allocator = tree_arena; - var source_buffer = try std.Buffer.initSize(arena, 0); + var source_buffer = try std.Buffer.initSize(&tree.arena_allocator.allocator, 0); var context = Context{ .tree = tree, @@ -94,7 +102,7 @@ extern fn declVisitorC(context: ?*c_void, decl: *const ZigClangDecl) bool { fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { switch (ZigClangDecl_getKind(decl)) { .Function => { - try appendToken(c, .LineComment, "// TODO translate function decl"); + return visitFnDecl(c, @ptrCast(*const ZigClangFunctionDecl, decl)); }, .Typedef => { try appendToken(c, .LineComment, "// TODO translate typedef"); @@ -115,11 +123,25 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { } } -fn appendToken(c: *Context, token_id: Token.Id, src_text: []const u8) !void { +fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { + const fn_name = c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl))); + try appendToken(c, .LineComment, "// TODO translate function '{}'", fn_name); +} + +fn appendToken(c: *Context, token_id: Token.Id, comptime format: []const u8, args: ...) !void { + const S = struct { + fn callback(context: *Context, bytes: []const u8) Error!void { + return context.source_buffer.append(bytes); + } + }; const start_index = c.source_buffer.len(); - try c.source_buffer.append(src_text); + errdefer c.source_buffer.shrink(start_index); + + try std.fmt.format(c, Error, S.callback, format, args); const end_index = c.source_buffer.len(); const new_token = try c.tree.tokens.addOne(); + errdefer c.tree.tokens.shrink(c.tree.tokens.len - 1); + new_token.* = Token{ .id = token_id, .start = start_index, diff --git a/src/all_types.hpp b/src/all_types.hpp index 5cba3f2230..3cfb47db64 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1861,6 +1861,7 @@ struct CodeGen { bool each_lib_rpath; bool is_dummy_so; bool disable_gen_h; + bool bundle_compiler_rt; Buf *mmacosx_version_min; Buf *mios_version_min; diff --git a/src/codegen.cpp b/src/codegen.cpp index db7d96f4df..23d3c5dc5c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -9346,6 +9346,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_bool(ch, g->linker_rdynamic); cache_bool(ch, g->each_lib_rpath); cache_bool(ch, g->disable_gen_h); + cache_bool(ch, g->bundle_compiler_rt); cache_bool(ch, want_valgrind_support(g)); cache_bool(ch, g->have_pic); cache_bool(ch, g->have_dynamic_link); diff --git a/src/link.cpp b/src/link.cpp index 385b28b71e..810f6072ad 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -772,17 +772,15 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) { } } -static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) { +static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path, OutType child_out_type) { // The Mach-O LLD code is not well maintained, and trips an assertion // when we link compiler_rt and builtin as libraries rather than objects. // Here we workaround this by having compiler_rt and builtin be objects. // TODO write our own linker. https://github.com/ziglang/zig/issues/1535 - OutType child_out_type = OutTypeLib; if (parent_gen->zig_target->os == OsMacOSX) { child_out_type = OutTypeObj; } - CodeGen *child_gen = create_child_codegen(parent_gen, full_path, child_out_type, parent_gen->libc); codegen_set_out_name(child_gen, buf_create_from_str(aname)); @@ -804,14 +802,14 @@ static Buf *build_a(CodeGen *parent_gen, const char *aname) { Buf *full_path = buf_alloc(); os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path); - return build_a_raw(parent_gen, aname, full_path); + return build_a_raw(parent_gen, aname, full_path, OutTypeLib); } -static Buf *build_compiler_rt(CodeGen *parent_gen) { +static Buf *build_compiler_rt(CodeGen *parent_gen, OutType child_out_type) { Buf *full_path = buf_alloc(); os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("compiler_rt.zig"), full_path); - return build_a_raw(parent_gen, "compiler_rt", full_path); + return build_a_raw(parent_gen, "compiler_rt", full_path, child_out_type); } static const char *get_darwin_arch_string(const ZigTarget *t) { @@ -1006,7 +1004,7 @@ static void construct_linker_job_elf(LinkJob *lj) { lj->args.append(buf_ptr(builtin_a_path)); } - Buf *compiler_rt_o_path = build_compiler_rt(g); + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib); lj->args.append(buf_ptr(compiler_rt_o_path)); } @@ -1117,7 +1115,7 @@ static void construct_linker_job_wasm(LinkJob *lj) { lj->args.append(buf_ptr(builtin_a_path)); } - Buf *compiler_rt_o_path = build_compiler_rt(g); + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib); lj->args.append(buf_ptr(compiler_rt_o_path)); } } @@ -1361,7 +1359,7 @@ static void construct_linker_job_coff(LinkJob *lj) { } // msvc compiler_rt is missing some stuff, so we still build it and rely on weak linkage - Buf *compiler_rt_o_path = build_compiler_rt(g); + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib); lj->args.append(buf_ptr(compiler_rt_o_path)); } @@ -1604,7 +1602,7 @@ static void construct_linker_job_macho(LinkJob *lj) { // compiler_rt on darwin is missing some stuff, so we still build it and rely on LinkOnce if (g->out_type == OutTypeExe || is_dyn_lib) { - Buf *compiler_rt_o_path = build_compiler_rt(g); + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib); lj->args.append(buf_ptr(compiler_rt_o_path)); } @@ -1681,14 +1679,18 @@ void codegen_link(CodeGen *g) { if (g->out_type == OutTypeLib && !g->is_dynamic) { ZigList file_names = {}; for (size_t i = 0; i < g->link_objects.length; i += 1) { - file_names.append((const char *)buf_ptr(g->link_objects.at(i))); + file_names.append(buf_ptr(g->link_objects.at(i))); + } + if (g->bundle_compiler_rt) { + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj); + file_names.append(buf_ptr(compiler_rt_o_path)); } ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os); codegen_add_time_event(g, "LLVM Link"); if (g->verbose_link) { fprintf(stderr, "ar rcs %s", buf_ptr(&g->output_file_path)); - for (size_t i = 0; i < g->link_objects.length; i += 1) { - fprintf(stderr, " %s", (const char *)buf_ptr(g->link_objects.at(i))); + for (size_t i = 0; i < file_names.length; i += 1) { + fprintf(stderr, " %s", file_names.at(i)); } fprintf(stderr, "\n"); } diff --git a/src/main.cpp b/src/main.cpp index 2915fd4b9d..e9a859eb70 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,6 +86,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " --override-std-dir [arg] use an alternate Zig standard library\n" "\n" "Link Options:\n" + " --bundle-compiler-rt [path] for static libraries, include compiler-rt symbols\n" " --dynamic-linker [path] set the path to ld.so\n" " --each-lib-rpath add rpath for each used dynamic library\n" " --library [lib] link against lib\n" @@ -442,6 +443,7 @@ int main(int argc, char **argv) { TargetSubsystem subsystem = TargetSubsystemAuto; bool want_single_threaded = false; bool disable_gen_h = false; + bool bundle_compiler_rt = false; Buf *override_std_dir = nullptr; Buf *override_lib_dir = nullptr; Buf *main_pkg_path = nullptr; @@ -652,6 +654,8 @@ int main(int argc, char **argv) { want_single_threaded = true; } else if (strcmp(arg, "--disable-gen-h") == 0) { disable_gen_h = true; + } else if (strcmp(arg, "--bundle-compiler-rt") == 0) { + bundle_compiler_rt = true; } else if (strcmp(arg, "--test-cmd-bin") == 0) { test_exec_args.append(nullptr); } else if (arg[1] == 'L' && arg[2] != 0) { @@ -1070,6 +1074,7 @@ int main(int argc, char **argv) { g->verbose_cc = verbose_cc; g->output_dir = output_dir; g->disable_gen_h = disable_gen_h; + g->bundle_compiler_rt = bundle_compiler_rt; codegen_set_errmsg_color(g, color); g->system_linker_hack = system_linker_hack; diff --git a/std/build.zig b/std/build.zig index 4d6c915438..fa3bac83d0 100644 --- a/std/build.zig +++ b/std/build.zig @@ -941,6 +941,7 @@ pub const LibExeObjStep = struct { verbose_link: bool, verbose_cc: bool, disable_gen_h: bool, + bundle_compiler_rt: bool, c_std: Builder.CStd, override_std_dir: ?[]const u8, override_lib_dir: ?[]const u8, @@ -1050,6 +1051,7 @@ pub const LibExeObjStep = struct { .name_prefix = "", .filter = null, .disable_gen_h = false, + .bundle_compiler_rt = false, .output_dir = null, .need_system_paths = false, .single_threaded = false, @@ -1452,6 +1454,9 @@ pub const LibExeObjStep = struct { if (self.disable_gen_h) { try zig_args.append("--disable-gen-h"); } + if (self.bundle_compiler_rt) { + try zig_args.append("--bundle-compiler-rt"); + } switch (self.target) { Target.Native => {}, diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 2faec8eab7..8f6f30ccc1 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -1,15 +1,13 @@ const builtin = @import("builtin"); const is_test = builtin.is_test; -const stack_probe = @import("compiler_rt/stack_probe.zig"); - comptime { const linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Weak; const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong; switch (builtin.arch) { .i386, .x86_64 => @export("__zig_probe_stack", @import("compiler_rt/stack_probe.zig").zig_probe_stack, linkage), - else => { } + else => {}, } @export("__lesf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage); @@ -499,7 +497,6 @@ nakedcc fn __aeabi_memcmp() noreturn { unreachable; } - extern fn __divmodsi4(a: i32, b: i32, rem: *i32) i32 { @setRuntimeSafety(is_test); @@ -1272,17 +1269,17 @@ fn test_one_udivsi3(a: u32, b: u32, expected_q: u32) void { test "test_divsi3" { const cases = [][3]i32{ - []i32{ 0, 1, 0}, - []i32{ 0, -1, 0}, - []i32{ 2, 1, 2}, - []i32{ 2, -1, -2}, - []i32{-2, 1, -2}, - []i32{-2, -1, 2}, - - []i32{@bitCast(i32, u32(0x80000000)), 1, @bitCast(i32, u32(0x80000000))}, - []i32{@bitCast(i32, u32(0x80000000)), -1, @bitCast(i32, u32(0x80000000))}, - []i32{@bitCast(i32, u32(0x80000000)), -2, 0x40000000}, - []i32{@bitCast(i32, u32(0x80000000)), 2, @bitCast(i32, u32(0xC0000000))}, + []i32{ 0, 1, 0 }, + []i32{ 0, -1, 0 }, + []i32{ 2, 1, 2 }, + []i32{ 2, -1, -2 }, + []i32{ -2, 1, -2 }, + []i32{ -2, -1, 2 }, + + []i32{ @bitCast(i32, u32(0x80000000)), 1, @bitCast(i32, u32(0x80000000)) }, + []i32{ @bitCast(i32, u32(0x80000000)), -1, @bitCast(i32, u32(0x80000000)) }, + []i32{ @bitCast(i32, u32(0x80000000)), -2, 0x40000000 }, + []i32{ @bitCast(i32, u32(0x80000000)), 2, @bitCast(i32, u32(0xC0000000)) }, }; for (cases) |case| { @@ -1297,19 +1294,19 @@ fn test_one_divsi3(a: i32, b: i32, expected_q: i32) void { test "test_divmodsi4" { const cases = [][4]i32{ - []i32{ 0, 1, 0, 0}, - []i32{ 0, -1, 0, 0}, - []i32{ 2, 1, 2, 0}, - []i32{ 2, -1, -2, 0}, - []i32{-2, 1, -2, 0}, - []i32{-2, -1, 2, 0}, - []i32{ 7, 5, 1, 2}, - []i32{-7, 5, -1, -2}, - []i32{19, 5, 3, 4}, - []i32{19, -5, -3, 4}, - - []i32{@bitCast(i32, u32(0x80000000)), 8, @bitCast(i32, u32(0xf0000000)), 0}, - []i32{@bitCast(i32, u32(0x80000007)), 8, @bitCast(i32, u32(0xf0000001)), -1}, + []i32{ 0, 1, 0, 0 }, + []i32{ 0, -1, 0, 0 }, + []i32{ 2, 1, 2, 0 }, + []i32{ 2, -1, -2, 0 }, + []i32{ -2, 1, -2, 0 }, + []i32{ -2, -1, 2, 0 }, + []i32{ 7, 5, 1, 2 }, + []i32{ -7, 5, -1, -2 }, + []i32{ 19, 5, 3, 4 }, + []i32{ 19, -5, -3, 4 }, + + []i32{ @bitCast(i32, u32(0x80000000)), 8, @bitCast(i32, u32(0xf0000000)), 0 }, + []i32{ @bitCast(i32, u32(0x80000007)), 8, @bitCast(i32, u32(0xf0000001)), -1 }, }; for (cases) |case| { @@ -1325,17 +1322,17 @@ fn test_one_divmodsi4(a: i32, b: i32, expected_q: i32, expected_r: i32) void { test "test_divdi3" { const cases = [][3]i64{ - []i64{ 0, 1, 0}, - []i64{ 0, -1, 0}, - []i64{ 2, 1, 2}, - []i64{ 2, -1, -2}, - []i64{-2, 1, -2}, - []i64{-2, -1, 2}, - - []i64{@bitCast(i64, u64(0x8000000000000000)), 1, @bitCast(i64, u64(0x8000000000000000))}, - []i64{@bitCast(i64, u64(0x8000000000000000)), -1, @bitCast(i64, u64(0x8000000000000000))}, - []i64{@bitCast(i64, u64(0x8000000000000000)), -2, 0x4000000000000000}, - []i64{@bitCast(i64, u64(0x8000000000000000)), 2, @bitCast(i64, u64(0xC000000000000000))}, + []i64{ 0, 1, 0 }, + []i64{ 0, -1, 0 }, + []i64{ 2, 1, 2 }, + []i64{ 2, -1, -2 }, + []i64{ -2, 1, -2 }, + []i64{ -2, -1, 2 }, + + []i64{ @bitCast(i64, u64(0x8000000000000000)), 1, @bitCast(i64, u64(0x8000000000000000)) }, + []i64{ @bitCast(i64, u64(0x8000000000000000)), -1, @bitCast(i64, u64(0x8000000000000000)) }, + []i64{ @bitCast(i64, u64(0x8000000000000000)), -2, 0x4000000000000000 }, + []i64{ @bitCast(i64, u64(0x8000000000000000)), 2, @bitCast(i64, u64(0xC000000000000000)) }, }; for (cases) |case| { @@ -1350,19 +1347,19 @@ fn test_one_divdi3(a: i64, b: i64, expected_q: i64) void { test "test_moddi3" { const cases = [][3]i64{ - []i64{0, 1, 0}, - []i64{0, -1, 0}, - []i64{5, 3, 2}, - []i64{5, -3, 2}, - []i64{-5, 3, -2}, - []i64{-5, -3, -2}, - - []i64{@bitCast(i64, @intCast(u64, 0x8000000000000000)), 1, 0}, - []i64{@bitCast(i64, @intCast(u64, 0x8000000000000000)), -1, 0}, - []i64{@bitCast(i64, @intCast(u64, 0x8000000000000000)), 2, 0}, - []i64{@bitCast(i64, @intCast(u64, 0x8000000000000000)), -2, 0}, - []i64{@bitCast(i64, @intCast(u64, 0x8000000000000000)), 3, -2}, - []i64{@bitCast(i64, @intCast(u64, 0x8000000000000000)), -3, -2}, + []i64{ 0, 1, 0 }, + []i64{ 0, -1, 0 }, + []i64{ 5, 3, 2 }, + []i64{ 5, -3, 2 }, + []i64{ -5, 3, -2 }, + []i64{ -5, -3, -2 }, + + []i64{ @bitCast(i64, @intCast(u64, 0x8000000000000000)), 1, 0 }, + []i64{ @bitCast(i64, @intCast(u64, 0x8000000000000000)), -1, 0 }, + []i64{ @bitCast(i64, @intCast(u64, 0x8000000000000000)), 2, 0 }, + []i64{ @bitCast(i64, @intCast(u64, 0x8000000000000000)), -2, 0 }, + []i64{ @bitCast(i64, @intCast(u64, 0x8000000000000000)), 3, -2 }, + []i64{ @bitCast(i64, @intCast(u64, 0x8000000000000000)), -3, -2 }, }; for (cases) |case| { @@ -1377,17 +1374,17 @@ fn test_one_moddi3(a: i64, b: i64, expected_r: i64) void { test "test_modsi3" { const cases = [][3]i32{ - []i32{0, 1, 0}, - []i32{0, -1, 0}, - []i32{5, 3, 2}, - []i32{5, -3, 2}, - []i32{-5, 3, -2}, - []i32{-5, -3, -2}, - []i32{@bitCast(i32, @intCast(u32, 0x80000000)), 1, 0x0}, - []i32{@bitCast(i32, @intCast(u32, 0x80000000)), 2, 0x0}, - []i32{@bitCast(i32, @intCast(u32, 0x80000000)), -2, 0x0}, - []i32{@bitCast(i32, @intCast(u32, 0x80000000)), 3, -2}, - []i32{@bitCast(i32, @intCast(u32, 0x80000000)), -3, -2}, + []i32{ 0, 1, 0 }, + []i32{ 0, -1, 0 }, + []i32{ 5, 3, 2 }, + []i32{ 5, -3, 2 }, + []i32{ -5, 3, -2 }, + []i32{ -5, -3, -2 }, + []i32{ @bitCast(i32, @intCast(u32, 0x80000000)), 1, 0x0 }, + []i32{ @bitCast(i32, @intCast(u32, 0x80000000)), 2, 0x0 }, + []i32{ @bitCast(i32, @intCast(u32, 0x80000000)), -2, 0x0 }, + []i32{ @bitCast(i32, @intCast(u32, 0x80000000)), 3, -2 }, + []i32{ @bitCast(i32, @intCast(u32, 0x80000000)), -3, -2 }, }; for (cases) |case| { @@ -1402,138 +1399,138 @@ fn test_one_modsi3(a: i32, b: i32, expected_r: i32) void { test "test_umodsi3" { const cases = [][3]u32{ - []u32{0x00000000, 0x00000001, 0x00000000}, - []u32{0x00000000, 0x00000002, 0x00000000}, - []u32{0x00000000, 0x00000003, 0x00000000}, - []u32{0x00000000, 0x00000010, 0x00000000}, - []u32{0x00000000, 0x078644FA, 0x00000000}, - []u32{0x00000000, 0x0747AE14, 0x00000000}, - []u32{0x00000000, 0x7FFFFFFF, 0x00000000}, - []u32{0x00000000, 0x80000000, 0x00000000}, - []u32{0x00000000, 0xFFFFFFFD, 0x00000000}, - []u32{0x00000000, 0xFFFFFFFE, 0x00000000}, - []u32{0x00000000, 0xFFFFFFFF, 0x00000000}, - []u32{0x00000001, 0x00000001, 0x00000000}, - []u32{0x00000001, 0x00000002, 0x00000001}, - []u32{0x00000001, 0x00000003, 0x00000001}, - []u32{0x00000001, 0x00000010, 0x00000001}, - []u32{0x00000001, 0x078644FA, 0x00000001}, - []u32{0x00000001, 0x0747AE14, 0x00000001}, - []u32{0x00000001, 0x7FFFFFFF, 0x00000001}, - []u32{0x00000001, 0x80000000, 0x00000001}, - []u32{0x00000001, 0xFFFFFFFD, 0x00000001}, - []u32{0x00000001, 0xFFFFFFFE, 0x00000001}, - []u32{0x00000001, 0xFFFFFFFF, 0x00000001}, - []u32{0x00000002, 0x00000001, 0x00000000}, - []u32{0x00000002, 0x00000002, 0x00000000}, - []u32{0x00000002, 0x00000003, 0x00000002}, - []u32{0x00000002, 0x00000010, 0x00000002}, - []u32{0x00000002, 0x078644FA, 0x00000002}, - []u32{0x00000002, 0x0747AE14, 0x00000002}, - []u32{0x00000002, 0x7FFFFFFF, 0x00000002}, - []u32{0x00000002, 0x80000000, 0x00000002}, - []u32{0x00000002, 0xFFFFFFFD, 0x00000002}, - []u32{0x00000002, 0xFFFFFFFE, 0x00000002}, - []u32{0x00000002, 0xFFFFFFFF, 0x00000002}, - []u32{0x00000003, 0x00000001, 0x00000000}, - []u32{0x00000003, 0x00000002, 0x00000001}, - []u32{0x00000003, 0x00000003, 0x00000000}, - []u32{0x00000003, 0x00000010, 0x00000003}, - []u32{0x00000003, 0x078644FA, 0x00000003}, - []u32{0x00000003, 0x0747AE14, 0x00000003}, - []u32{0x00000003, 0x7FFFFFFF, 0x00000003}, - []u32{0x00000003, 0x80000000, 0x00000003}, - []u32{0x00000003, 0xFFFFFFFD, 0x00000003}, - []u32{0x00000003, 0xFFFFFFFE, 0x00000003}, - []u32{0x00000003, 0xFFFFFFFF, 0x00000003}, - []u32{0x00000010, 0x00000001, 0x00000000}, - []u32{0x00000010, 0x00000002, 0x00000000}, - []u32{0x00000010, 0x00000003, 0x00000001}, - []u32{0x00000010, 0x00000010, 0x00000000}, - []u32{0x00000010, 0x078644FA, 0x00000010}, - []u32{0x00000010, 0x0747AE14, 0x00000010}, - []u32{0x00000010, 0x7FFFFFFF, 0x00000010}, - []u32{0x00000010, 0x80000000, 0x00000010}, - []u32{0x00000010, 0xFFFFFFFD, 0x00000010}, - []u32{0x00000010, 0xFFFFFFFE, 0x00000010}, - []u32{0x00000010, 0xFFFFFFFF, 0x00000010}, - []u32{0x078644FA, 0x00000001, 0x00000000}, - []u32{0x078644FA, 0x00000002, 0x00000000}, - []u32{0x078644FA, 0x00000003, 0x00000000}, - []u32{0x078644FA, 0x00000010, 0x0000000A}, - []u32{0x078644FA, 0x078644FA, 0x00000000}, - []u32{0x078644FA, 0x0747AE14, 0x003E96E6}, - []u32{0x078644FA, 0x7FFFFFFF, 0x078644FA}, - []u32{0x078644FA, 0x80000000, 0x078644FA}, - []u32{0x078644FA, 0xFFFFFFFD, 0x078644FA}, - []u32{0x078644FA, 0xFFFFFFFE, 0x078644FA}, - []u32{0x078644FA, 0xFFFFFFFF, 0x078644FA}, - []u32{0x0747AE14, 0x00000001, 0x00000000}, - []u32{0x0747AE14, 0x00000002, 0x00000000}, - []u32{0x0747AE14, 0x00000003, 0x00000002}, - []u32{0x0747AE14, 0x00000010, 0x00000004}, - []u32{0x0747AE14, 0x078644FA, 0x0747AE14}, - []u32{0x0747AE14, 0x0747AE14, 0x00000000}, - []u32{0x0747AE14, 0x7FFFFFFF, 0x0747AE14}, - []u32{0x0747AE14, 0x80000000, 0x0747AE14}, - []u32{0x0747AE14, 0xFFFFFFFD, 0x0747AE14}, - []u32{0x0747AE14, 0xFFFFFFFE, 0x0747AE14}, - []u32{0x0747AE14, 0xFFFFFFFF, 0x0747AE14}, - []u32{0x7FFFFFFF, 0x00000001, 0x00000000}, - []u32{0x7FFFFFFF, 0x00000002, 0x00000001}, - []u32{0x7FFFFFFF, 0x00000003, 0x00000001}, - []u32{0x7FFFFFFF, 0x00000010, 0x0000000F}, - []u32{0x7FFFFFFF, 0x078644FA, 0x00156B65}, - []u32{0x7FFFFFFF, 0x0747AE14, 0x043D70AB}, - []u32{0x7FFFFFFF, 0x7FFFFFFF, 0x00000000}, - []u32{0x7FFFFFFF, 0x80000000, 0x7FFFFFFF}, - []u32{0x7FFFFFFF, 0xFFFFFFFD, 0x7FFFFFFF}, - []u32{0x7FFFFFFF, 0xFFFFFFFE, 0x7FFFFFFF}, - []u32{0x7FFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF}, - []u32{0x80000000, 0x00000001, 0x00000000}, - []u32{0x80000000, 0x00000002, 0x00000000}, - []u32{0x80000000, 0x00000003, 0x00000002}, - []u32{0x80000000, 0x00000010, 0x00000000}, - []u32{0x80000000, 0x078644FA, 0x00156B66}, - []u32{0x80000000, 0x0747AE14, 0x043D70AC}, - []u32{0x80000000, 0x7FFFFFFF, 0x00000001}, - []u32{0x80000000, 0x80000000, 0x00000000}, - []u32{0x80000000, 0xFFFFFFFD, 0x80000000}, - []u32{0x80000000, 0xFFFFFFFE, 0x80000000}, - []u32{0x80000000, 0xFFFFFFFF, 0x80000000}, - []u32{0xFFFFFFFD, 0x00000001, 0x00000000}, - []u32{0xFFFFFFFD, 0x00000002, 0x00000001}, - []u32{0xFFFFFFFD, 0x00000003, 0x00000001}, - []u32{0xFFFFFFFD, 0x00000010, 0x0000000D}, - []u32{0xFFFFFFFD, 0x078644FA, 0x002AD6C9}, - []u32{0xFFFFFFFD, 0x0747AE14, 0x01333341}, - []u32{0xFFFFFFFD, 0x7FFFFFFF, 0x7FFFFFFE}, - []u32{0xFFFFFFFD, 0x80000000, 0x7FFFFFFD}, - []u32{0xFFFFFFFD, 0xFFFFFFFD, 0x00000000}, - []u32{0xFFFFFFFD, 0xFFFFFFFE, 0xFFFFFFFD}, - []u32{0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFD}, - []u32{0xFFFFFFFE, 0x00000001, 0x00000000}, - []u32{0xFFFFFFFE, 0x00000002, 0x00000000}, - []u32{0xFFFFFFFE, 0x00000003, 0x00000002}, - []u32{0xFFFFFFFE, 0x00000010, 0x0000000E}, - []u32{0xFFFFFFFE, 0x078644FA, 0x002AD6CA}, - []u32{0xFFFFFFFE, 0x0747AE14, 0x01333342}, - []u32{0xFFFFFFFE, 0x7FFFFFFF, 0x00000000}, - []u32{0xFFFFFFFE, 0x80000000, 0x7FFFFFFE}, - []u32{0xFFFFFFFE, 0xFFFFFFFD, 0x00000001}, - []u32{0xFFFFFFFE, 0xFFFFFFFE, 0x00000000}, - []u32{0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFE}, - []u32{0xFFFFFFFF, 0x00000001, 0x00000000}, - []u32{0xFFFFFFFF, 0x00000002, 0x00000001}, - []u32{0xFFFFFFFF, 0x00000003, 0x00000000}, - []u32{0xFFFFFFFF, 0x00000010, 0x0000000F}, - []u32{0xFFFFFFFF, 0x078644FA, 0x002AD6CB}, - []u32{0xFFFFFFFF, 0x0747AE14, 0x01333343}, - []u32{0xFFFFFFFF, 0x7FFFFFFF, 0x00000001}, - []u32{0xFFFFFFFF, 0x80000000, 0x7FFFFFFF}, - []u32{0xFFFFFFFF, 0xFFFFFFFD, 0x00000002}, - []u32{0xFFFFFFFF, 0xFFFFFFFE, 0x00000001}, - []u32{0xFFFFFFFF, 0xFFFFFFFF, 0x00000000} + []u32{ 0x00000000, 0x00000001, 0x00000000 }, + []u32{ 0x00000000, 0x00000002, 0x00000000 }, + []u32{ 0x00000000, 0x00000003, 0x00000000 }, + []u32{ 0x00000000, 0x00000010, 0x00000000 }, + []u32{ 0x00000000, 0x078644FA, 0x00000000 }, + []u32{ 0x00000000, 0x0747AE14, 0x00000000 }, + []u32{ 0x00000000, 0x7FFFFFFF, 0x00000000 }, + []u32{ 0x00000000, 0x80000000, 0x00000000 }, + []u32{ 0x00000000, 0xFFFFFFFD, 0x00000000 }, + []u32{ 0x00000000, 0xFFFFFFFE, 0x00000000 }, + []u32{ 0x00000000, 0xFFFFFFFF, 0x00000000 }, + []u32{ 0x00000001, 0x00000001, 0x00000000 }, + []u32{ 0x00000001, 0x00000002, 0x00000001 }, + []u32{ 0x00000001, 0x00000003, 0x00000001 }, + []u32{ 0x00000001, 0x00000010, 0x00000001 }, + []u32{ 0x00000001, 0x078644FA, 0x00000001 }, + []u32{ 0x00000001, 0x0747AE14, 0x00000001 }, + []u32{ 0x00000001, 0x7FFFFFFF, 0x00000001 }, + []u32{ 0x00000001, 0x80000000, 0x00000001 }, + []u32{ 0x00000001, 0xFFFFFFFD, 0x00000001 }, + []u32{ 0x00000001, 0xFFFFFFFE, 0x00000001 }, + []u32{ 0x00000001, 0xFFFFFFFF, 0x00000001 }, + []u32{ 0x00000002, 0x00000001, 0x00000000 }, + []u32{ 0x00000002, 0x00000002, 0x00000000 }, + []u32{ 0x00000002, 0x00000003, 0x00000002 }, + []u32{ 0x00000002, 0x00000010, 0x00000002 }, + []u32{ 0x00000002, 0x078644FA, 0x00000002 }, + []u32{ 0x00000002, 0x0747AE14, 0x00000002 }, + []u32{ 0x00000002, 0x7FFFFFFF, 0x00000002 }, + []u32{ 0x00000002, 0x80000000, 0x00000002 }, + []u32{ 0x00000002, 0xFFFFFFFD, 0x00000002 }, + []u32{ 0x00000002, 0xFFFFFFFE, 0x00000002 }, + []u32{ 0x00000002, 0xFFFFFFFF, 0x00000002 }, + []u32{ 0x00000003, 0x00000001, 0x00000000 }, + []u32{ 0x00000003, 0x00000002, 0x00000001 }, + []u32{ 0x00000003, 0x00000003, 0x00000000 }, + []u32{ 0x00000003, 0x00000010, 0x00000003 }, + []u32{ 0x00000003, 0x078644FA, 0x00000003 }, + []u32{ 0x00000003, 0x0747AE14, 0x00000003 }, + []u32{ 0x00000003, 0x7FFFFFFF, 0x00000003 }, + []u32{ 0x00000003, 0x80000000, 0x00000003 }, + []u32{ 0x00000003, 0xFFFFFFFD, 0x00000003 }, + []u32{ 0x00000003, 0xFFFFFFFE, 0x00000003 }, + []u32{ 0x00000003, 0xFFFFFFFF, 0x00000003 }, + []u32{ 0x00000010, 0x00000001, 0x00000000 }, + []u32{ 0x00000010, 0x00000002, 0x00000000 }, + []u32{ 0x00000010, 0x00000003, 0x00000001 }, + []u32{ 0x00000010, 0x00000010, 0x00000000 }, + []u32{ 0x00000010, 0x078644FA, 0x00000010 }, + []u32{ 0x00000010, 0x0747AE14, 0x00000010 }, + []u32{ 0x00000010, 0x7FFFFFFF, 0x00000010 }, + []u32{ 0x00000010, 0x80000000, 0x00000010 }, + []u32{ 0x00000010, 0xFFFFFFFD, 0x00000010 }, + []u32{ 0x00000010, 0xFFFFFFFE, 0x00000010 }, + []u32{ 0x00000010, 0xFFFFFFFF, 0x00000010 }, + []u32{ 0x078644FA, 0x00000001, 0x00000000 }, + []u32{ 0x078644FA, 0x00000002, 0x00000000 }, + []u32{ 0x078644FA, 0x00000003, 0x00000000 }, + []u32{ 0x078644FA, 0x00000010, 0x0000000A }, + []u32{ 0x078644FA, 0x078644FA, 0x00000000 }, + []u32{ 0x078644FA, 0x0747AE14, 0x003E96E6 }, + []u32{ 0x078644FA, 0x7FFFFFFF, 0x078644FA }, + []u32{ 0x078644FA, 0x80000000, 0x078644FA }, + []u32{ 0x078644FA, 0xFFFFFFFD, 0x078644FA }, + []u32{ 0x078644FA, 0xFFFFFFFE, 0x078644FA }, + []u32{ 0x078644FA, 0xFFFFFFFF, 0x078644FA }, + []u32{ 0x0747AE14, 0x00000001, 0x00000000 }, + []u32{ 0x0747AE14, 0x00000002, 0x00000000 }, + []u32{ 0x0747AE14, 0x00000003, 0x00000002 }, + []u32{ 0x0747AE14, 0x00000010, 0x00000004 }, + []u32{ 0x0747AE14, 0x078644FA, 0x0747AE14 }, + []u32{ 0x0747AE14, 0x0747AE14, 0x00000000 }, + []u32{ 0x0747AE14, 0x7FFFFFFF, 0x0747AE14 }, + []u32{ 0x0747AE14, 0x80000000, 0x0747AE14 }, + []u32{ 0x0747AE14, 0xFFFFFFFD, 0x0747AE14 }, + []u32{ 0x0747AE14, 0xFFFFFFFE, 0x0747AE14 }, + []u32{ 0x0747AE14, 0xFFFFFFFF, 0x0747AE14 }, + []u32{ 0x7FFFFFFF, 0x00000001, 0x00000000 }, + []u32{ 0x7FFFFFFF, 0x00000002, 0x00000001 }, + []u32{ 0x7FFFFFFF, 0x00000003, 0x00000001 }, + []u32{ 0x7FFFFFFF, 0x00000010, 0x0000000F }, + []u32{ 0x7FFFFFFF, 0x078644FA, 0x00156B65 }, + []u32{ 0x7FFFFFFF, 0x0747AE14, 0x043D70AB }, + []u32{ 0x7FFFFFFF, 0x7FFFFFFF, 0x00000000 }, + []u32{ 0x7FFFFFFF, 0x80000000, 0x7FFFFFFF }, + []u32{ 0x7FFFFFFF, 0xFFFFFFFD, 0x7FFFFFFF }, + []u32{ 0x7FFFFFFF, 0xFFFFFFFE, 0x7FFFFFFF }, + []u32{ 0x7FFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF }, + []u32{ 0x80000000, 0x00000001, 0x00000000 }, + []u32{ 0x80000000, 0x00000002, 0x00000000 }, + []u32{ 0x80000000, 0x00000003, 0x00000002 }, + []u32{ 0x80000000, 0x00000010, 0x00000000 }, + []u32{ 0x80000000, 0x078644FA, 0x00156B66 }, + []u32{ 0x80000000, 0x0747AE14, 0x043D70AC }, + []u32{ 0x80000000, 0x7FFFFFFF, 0x00000001 }, + []u32{ 0x80000000, 0x80000000, 0x00000000 }, + []u32{ 0x80000000, 0xFFFFFFFD, 0x80000000 }, + []u32{ 0x80000000, 0xFFFFFFFE, 0x80000000 }, + []u32{ 0x80000000, 0xFFFFFFFF, 0x80000000 }, + []u32{ 0xFFFFFFFD, 0x00000001, 0x00000000 }, + []u32{ 0xFFFFFFFD, 0x00000002, 0x00000001 }, + []u32{ 0xFFFFFFFD, 0x00000003, 0x00000001 }, + []u32{ 0xFFFFFFFD, 0x00000010, 0x0000000D }, + []u32{ 0xFFFFFFFD, 0x078644FA, 0x002AD6C9 }, + []u32{ 0xFFFFFFFD, 0x0747AE14, 0x01333341 }, + []u32{ 0xFFFFFFFD, 0x7FFFFFFF, 0x7FFFFFFE }, + []u32{ 0xFFFFFFFD, 0x80000000, 0x7FFFFFFD }, + []u32{ 0xFFFFFFFD, 0xFFFFFFFD, 0x00000000 }, + []u32{ 0xFFFFFFFD, 0xFFFFFFFE, 0xFFFFFFFD }, + []u32{ 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFD }, + []u32{ 0xFFFFFFFE, 0x00000001, 0x00000000 }, + []u32{ 0xFFFFFFFE, 0x00000002, 0x00000000 }, + []u32{ 0xFFFFFFFE, 0x00000003, 0x00000002 }, + []u32{ 0xFFFFFFFE, 0x00000010, 0x0000000E }, + []u32{ 0xFFFFFFFE, 0x078644FA, 0x002AD6CA }, + []u32{ 0xFFFFFFFE, 0x0747AE14, 0x01333342 }, + []u32{ 0xFFFFFFFE, 0x7FFFFFFF, 0x00000000 }, + []u32{ 0xFFFFFFFE, 0x80000000, 0x7FFFFFFE }, + []u32{ 0xFFFFFFFE, 0xFFFFFFFD, 0x00000001 }, + []u32{ 0xFFFFFFFE, 0xFFFFFFFE, 0x00000000 }, + []u32{ 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFE }, + []u32{ 0xFFFFFFFF, 0x00000001, 0x00000000 }, + []u32{ 0xFFFFFFFF, 0x00000002, 0x00000001 }, + []u32{ 0xFFFFFFFF, 0x00000003, 0x00000000 }, + []u32{ 0xFFFFFFFF, 0x00000010, 0x0000000F }, + []u32{ 0xFFFFFFFF, 0x078644FA, 0x002AD6CB }, + []u32{ 0xFFFFFFFF, 0x0747AE14, 0x01333343 }, + []u32{ 0xFFFFFFFF, 0x7FFFFFFF, 0x00000001 }, + []u32{ 0xFFFFFFFF, 0x80000000, 0x7FFFFFFF }, + []u32{ 0xFFFFFFFF, 0xFFFFFFFD, 0x00000002 }, + []u32{ 0xFFFFFFFF, 0xFFFFFFFE, 0x00000001 }, + []u32{ 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000 }, }; for (cases) |case| { -- cgit v1.2.3 From a7346ea49f4d9b1af2d0babf67354b234a87fe42 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 8 May 2019 22:43:11 -0400 Subject: fix build on macOS Sadly due to a workaround for LLD linker limitations on macOS we cannot put libuserland into an .a file; instead we have to use object files. Again due to linker limitations, bundling compiler_rt.o into another relocatable object also doesn't work. So we're left with disabling stack probing on macOS for the stage1 self-hosted code. These workarounds could all be removed if the macos support in the LLD linker improved, or if Zig project had its own linker that did not have these issues. --- build.zig | 7 ++++++- src/all_types.hpp | 1 + src/codegen.cpp | 8 +++++++- src/codegen.hpp | 1 + src/link.cpp | 10 ++++++---- src/main.cpp | 5 +++++ std/build.zig | 5 +++++ 7 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src/link.cpp') diff --git a/build.zig b/build.zig index bdaf175945..274ea51436 100644 --- a/build.zig +++ b/build.zig @@ -384,12 +384,17 @@ const Context = struct { }; fn addLibUserlandStep(b: *Builder) void { + // Sadly macOS requires hacks to work around the buggy MACH-O linker code. const artifact = if (builtin.os == .macosx) b.addObject("userland", "src-self-hosted/stage1.zig") else b.addStaticLibrary("userland", "src-self-hosted/stage1.zig"); artifact.disable_gen_h = true; - artifact.bundle_compiler_rt = true; + if (builtin.os == .macosx) { + artifact.disable_stack_probing = true; + } else { + artifact.bundle_compiler_rt = true; + } artifact.setTarget(builtin.arch, builtin.os, builtin.abi); artifact.linkSystemLibrary("c"); const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1"); diff --git a/src/all_types.hpp b/src/all_types.hpp index 3cfb47db64..ecc0d1a9bb 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1862,6 +1862,7 @@ struct CodeGen { bool is_dummy_so; bool disable_gen_h; bool bundle_compiler_rt; + bool disable_stack_probing; Buf *mmacosx_version_min; Buf *mios_version_min; diff --git a/src/codegen.cpp b/src/codegen.cpp index 23d3c5dc5c..d21ada1b23 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -401,7 +401,7 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) { static void add_probe_stack_attr(CodeGen *g, LLVMValueRef fn_val) { // Windows already emits its own stack probes - if (g->zig_target->os != OsWindows && + if (!g->disable_stack_probing && g->zig_target->os != OsWindows && (g->zig_target->arch == ZigLLVM_x86 || g->zig_target->arch == ZigLLVM_x86_64)) { addLLVMFnAttrStr(fn_val, "probe-stack", "__zig_probe_stack"); @@ -7043,6 +7043,11 @@ static void zig_llvm_emit_output(CodeGen *g) { } validate_inline_fns(g); g->link_objects.append(output_path); + if (g->bundle_compiler_rt && (g->out_type == OutTypeObj || + (g->out_type == OutTypeLib && !g->is_dynamic))) + { + zig_link_add_compiler_rt(g); + } break; case EmitFileTypeAssembly: @@ -9347,6 +9352,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_bool(ch, g->each_lib_rpath); cache_bool(ch, g->disable_gen_h); cache_bool(ch, g->bundle_compiler_rt); + cache_bool(ch, g->disable_stack_probing); cache_bool(ch, want_valgrind_support(g)); cache_bool(ch, g->have_pic); cache_bool(ch, g->have_dynamic_link); diff --git a/src/codegen.hpp b/src/codegen.hpp index 88c7353cad..47c0097e4b 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -44,6 +44,7 @@ void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patc void codegen_add_time_event(CodeGen *g, const char *name); void codegen_print_timing_report(CodeGen *g, FILE *f); void codegen_link(CodeGen *g); +void zig_link_add_compiler_rt(CodeGen *g); void codegen_build_and_link(CodeGen *g); ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path, diff --git a/src/link.cpp b/src/link.cpp index 810f6072ad..8900d0351b 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -25,6 +25,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type, parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); child_gen->disable_gen_h = true; + child_gen->disable_stack_probing = true; child_gen->verbose_tokenize = parent_gen->verbose_tokenize; child_gen->verbose_ast = parent_gen->verbose_ast; child_gen->verbose_link = parent_gen->verbose_link; @@ -1653,6 +1654,11 @@ static void construct_linker_job(LinkJob *lj) { } } +void zig_link_add_compiler_rt(CodeGen *g) { + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj); + g->link_objects.append(compiler_rt_o_path); +} + void codegen_link(CodeGen *g) { codegen_add_time_event(g, "Build Dependencies"); @@ -1681,10 +1687,6 @@ void codegen_link(CodeGen *g) { for (size_t i = 0; i < g->link_objects.length; i += 1) { file_names.append(buf_ptr(g->link_objects.at(i))); } - if (g->bundle_compiler_rt) { - Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj); - file_names.append(buf_ptr(compiler_rt_o_path)); - } ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os); codegen_add_time_event(g, "LLVM Link"); if (g->verbose_link) { diff --git a/src/main.cpp b/src/main.cpp index e9a859eb70..5659624cb7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,6 +56,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " --disable-gen-h do not generate a C header file (.h)\n" " --disable-valgrind omit valgrind client requests in debug builds\n" " --enable-valgrind include valgrind client requests release builds\n" + " --disable-stack-probing workaround for macosx\n" " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n" " -fPIC enable Position Independent Code\n" " -fno-PIC disable Position Independent Code\n" @@ -444,6 +445,7 @@ int main(int argc, char **argv) { bool want_single_threaded = false; bool disable_gen_h = false; bool bundle_compiler_rt = false; + bool disable_stack_probing = false; Buf *override_std_dir = nullptr; Buf *override_lib_dir = nullptr; Buf *main_pkg_path = nullptr; @@ -656,6 +658,8 @@ int main(int argc, char **argv) { disable_gen_h = true; } else if (strcmp(arg, "--bundle-compiler-rt") == 0) { bundle_compiler_rt = true; + } else if (strcmp(arg, "--disable-stack-probing") == 0) { + disable_stack_probing = true; } else if (strcmp(arg, "--test-cmd-bin") == 0) { test_exec_args.append(nullptr); } else if (arg[1] == 'L' && arg[2] != 0) { @@ -1075,6 +1079,7 @@ int main(int argc, char **argv) { g->output_dir = output_dir; g->disable_gen_h = disable_gen_h; g->bundle_compiler_rt = bundle_compiler_rt; + g->disable_stack_probing = disable_stack_probing; codegen_set_errmsg_color(g, color); g->system_linker_hack = system_linker_hack; diff --git a/std/build.zig b/std/build.zig index fa3bac83d0..b5ec97ab5f 100644 --- a/std/build.zig +++ b/std/build.zig @@ -942,6 +942,7 @@ pub const LibExeObjStep = struct { verbose_cc: bool, disable_gen_h: bool, bundle_compiler_rt: bool, + disable_stack_probing: bool, c_std: Builder.CStd, override_std_dir: ?[]const u8, override_lib_dir: ?[]const u8, @@ -1052,6 +1053,7 @@ pub const LibExeObjStep = struct { .filter = null, .disable_gen_h = false, .bundle_compiler_rt = false, + .disable_stack_probing = false, .output_dir = null, .need_system_paths = false, .single_threaded = false, @@ -1457,6 +1459,9 @@ pub const LibExeObjStep = struct { if (self.bundle_compiler_rt) { try zig_args.append("--bundle-compiler-rt"); } + if (self.disable_stack_probing) { + try zig_args.append("--disable-stack-probing"); + } switch (self.target) { Target.Native => {}, -- cgit v1.2.3