authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-06-04 16:20:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-24 20:01:18+00:00
log3b26e508638e96997aff3d5ef2c42a917bf86cc8
tree969d4867b78ae9fcc79db31a9c34bb27ab82c439
parentdf1a2ecd3bfea5506151f62780f442dbe487db96

macOS: macho ld64.lld fixes

* bring `construct_linker_job_macho` to parity with `construct_linker_job_elf` * macho now sets `-error-limit` * macho on macOS now sets `-macosx_version_min` and `-sdk_version` to `10.13` when running `zig0` * macho now detects when `-l` prefix is not needed * macho on macOS detects system libraries in a case-insensitive manner * macho now ads user-specified libraries to linker command-line args when condition `is_native_os != true` * re-ordered some macho args positions to match elf positions closes #5059 closes #5067

6 files changed, 92 insertions(+), 46 deletions(-)

src-self-hosted/stage2.zig+2
......@@ -1271,3 +1271,5 @@ export fn stage2_clang_arg_next(it: *ClangArgIterator) Error {
12711271 };
12721272 return .None;
12731273}
1274
1275export const stage2_is_zig0 = false;
src/link.cpp+59-35
......@@ -2002,6 +2002,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
20022002 lj->args.append(buf_ptr(compiler_rt_o_path));
20032003 }
20042004
2005 // libraries
20052006 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {
20062007 LinkLib *link_lib = g->link_libs_list.at(i);
20072008 if (buf_eql_str(link_lib->name, "c")) {
......@@ -2662,8 +2663,8 @@ static void construct_linker_job_coff(LinkJob *lj) {
26622663static void construct_linker_job_macho(LinkJob *lj) {
26632664 CodeGen *g = lj->codegen;
26642665
2665 // LLD MACH-O has no error limit option.
2666 //lj->args.append("-error-limit=0");
2666 lj->args.append("-error-limit");
2667 lj->args.append("0");
26672668 lj->args.append("-demangle");
26682669
26692670 switch (g->linker_gc_sections) {
......@@ -2736,6 +2737,7 @@ static void construct_linker_job_macho(LinkJob *lj) {
27362737 lj->args.append("-iphoneos_version_min");
27372738 }
27382739 }
2740
27392741 Buf *version_string = buf_sprintf("%d.%d.%d",
27402742 g->zig_target->glibc_or_darwin_version->major,
27412743 g->zig_target->glibc_or_darwin_version->minor,
......@@ -2744,6 +2746,12 @@ static void construct_linker_job_macho(LinkJob *lj) {
27442746
27452747 lj->args.append("-sdk_version");
27462748 lj->args.append(buf_ptr(version_string));
2749 } else if (stage2_is_zig0 && g->zig_target->os == OsMacOSX) {
2750 // running `zig0`; `-pie` requires versions >= 10.5; select 10.13
2751 lj->args.append("-macosx_version_min");
2752 lj->args.append("10.13");
2753 lj->args.append("-sdk_version");
2754 lj->args.append("10.13");
27472755 }
27482756
27492757 if (g->out_type == OutTypeExe) {
......@@ -2773,45 +2781,74 @@ static void construct_linker_job_macho(LinkJob *lj) {
27732781 lj->args.append(lib_dir);
27742782 }
27752783
2784 // .o files
27762785 for (size_t i = 0; i < g->link_objects.length; i += 1) {
27772786 lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
27782787 }
27792788
2780 // libc++ dep
2781 if (g->libcpp_link_lib != nullptr && g->out_type != OutTypeObj) {
2782 lj->args.append(build_libcxxabi(g, lj->build_dep_prog_node));
2783 lj->args.append(build_libcxx(g, lj->build_dep_prog_node));
2784 }
2785
27862789 // compiler_rt on darwin is missing some stuff, so we still build it and rely on LinkOnce
27872790 if (g->out_type == OutTypeExe || is_dyn_lib) {
27882791 Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib, lj->build_dep_prog_node);
27892792 lj->args.append(buf_ptr(compiler_rt_o_path));
27902793 }
27912794
2792 if (g->zig_target->is_native_os) {
2793 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
2794 LinkLib *link_lib = g->link_libs_list.at(lib_i);
2795 if (target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) {
2796 // handled by libSystem
2797 continue;
2798 }
2799 if (strchr(buf_ptr(link_lib->name), '/') == nullptr) {
2800 Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name));
2801 lj->args.append(buf_ptr(arg));
2802 } else {
2803 lj->args.append(buf_ptr(link_lib->name));
2804 }
2795 // libraries
2796 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
2797 LinkLib *link_lib = g->link_libs_list.at(lib_i);
2798 if (buf_eql_str(link_lib->name, "c")) {
2799 // libc is linked specially
2800 continue;
2801 }
2802 if (target_is_libcpp_lib_name(g->zig_target, buf_ptr(link_lib->name))) {
2803 // libc++ is linked specially
2804 continue;
28052805 }
2806 if (g->zig_target->is_native_os && target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) {
2807 // libSystem is linked specially
2808 continue;
2809 }
2810
2811 Buf *arg;
2812 if (buf_starts_with_str(link_lib->name, "/") || buf_ends_with_str(link_lib->name, ".a") ||
2813 buf_ends_with_str(link_lib->name, ".dylib"))
2814 {
2815 arg = link_lib->name;
2816 } else {
2817 arg = buf_sprintf("-l%s", buf_ptr(link_lib->name));
2818 }
2819 lj->args.append(buf_ptr(arg));
2820 }
2821
2822 // libc++ dep
2823 if (g->libcpp_link_lib != nullptr && g->out_type != OutTypeObj) {
2824 lj->args.append(build_libcxxabi(g, lj->build_dep_prog_node));
2825 lj->args.append(build_libcxx(g, lj->build_dep_prog_node));
2826 }
2827
2828 // libc dep
2829 if (g->zig_target->is_native_os || stage2_is_zig0) {
28062830 // on Darwin, libSystem has libc in it, but also you have to use it
28072831 // to make syscalls because the syscall numbers are not documented
28082832 // and change between versions.
28092833 // so we always link against libSystem
28102834 lj->args.append("-lSystem");
28112835 }
2836
2837 for (size_t i = 0; i < g->framework_dirs.length; i += 1) {
2838 const char *framework_dir = g->framework_dirs.at(i);
2839 lj->args.append("-F");
2840 lj->args.append(framework_dir);
2841 }
2842
2843 for (size_t i = 0; i < g->darwin_frameworks.length; i += 1) {
2844 lj->args.append("-framework");
2845 lj->args.append(buf_ptr(g->darwin_frameworks.at(i)));
2846 }
2847
28122848 switch (g->linker_allow_shlib_undefined) {
28132849 case OptionalBoolNull:
2814 if (!g->zig_target->is_native_os) {
2850 if (!g->zig_target->is_native_os && !stage2_is_zig0) {
2851 // TODO https://github.com/ziglang/zig/issues/5059
28152852 lj->args.append("-undefined");
28162853 lj->args.append("dynamic_lookup");
28172854 }
......@@ -2831,18 +2868,6 @@ static void construct_linker_job_macho(LinkJob *lj) {
28312868 lj->args.append("-Bsymbolic");
28322869 break;
28332870 }
2834
2835 for (size_t i = 0; i < g->framework_dirs.length; i += 1) {
2836 const char *framework_dir = g->framework_dirs.at(i);
2837 lj->args.append("-F");
2838 lj->args.append(framework_dir);
2839 }
2840
2841 for (size_t i = 0; i < g->darwin_frameworks.length; i += 1) {
2842 lj->args.append("-framework");
2843 lj->args.append(buf_ptr(g->darwin_frameworks.at(i)));
2844 }
2845
28462871}
28472872
28482873static void construct_linker_job(LinkJob *lj) {
......@@ -2920,7 +2945,6 @@ void codegen_link(CodeGen *g) {
29202945
29212946 construct_linker_job(&lj);
29222947
2923
29242948 if (g->verbose_link) {
29252949 for (size_t i = 0; i < lj.args.length; i += 1) {
29262950 const char *space = (i != 0) ? " " : "";
src/stage2.cpp+2
......@@ -322,3 +322,5 @@ enum Error stage2_clang_arg_next(struct Stage2ClangArgIterator *it) {
322322 const char *msg = "stage0 called stage2_clang_arg_next";
323323 stage2_panic(msg, strlen(msg));
324324}
325
326const bool stage2_is_zig0 = true;
src/stage2.h+3
......@@ -379,4 +379,7 @@ ZIG_EXTERN_C void stage2_clang_arg_iterator(struct Stage2ClangArgIterator *it,
379379// ABI warning
380380ZIG_EXTERN_C enum Error stage2_clang_arg_next(struct Stage2ClangArgIterator *it);
381381
382// ABI warning
383ZIG_EXTERN_C const bool stage2_is_zig0;
384
382385#endif
src/target.cpp+18-11
......@@ -1209,39 +1209,46 @@ const char *target_libc_generic_name(const ZigTarget *target) {
12091209}
12101210
12111211bool target_is_libc_lib_name(const ZigTarget *target, const char *name) {
1212 if (strcmp(name, "c") == 0)
1212 auto equal = str_eql_str;
1213 if (target->os == OsMacOSX)
1214 equal = str_eql_str_ignore_case;
1215
1216 if (equal(name, "c"))
12131217 return true;
12141218
12151219 if (target_abi_is_gnu(target->abi) && target->os == OsWindows) {
12161220 // mingw-w64
12171221
1218 if (strcmp(name, "m") == 0)
1222 if (equal(name, "m"))
12191223 return true;
12201224
12211225 return false;
12221226 }
12231227
12241228 if (target_abi_is_gnu(target->abi) || target_abi_is_musl(target->abi) || target_os_is_darwin(target->os)) {
1225 if (strcmp(name, "m") == 0)
1229 if (equal(name, "m"))
12261230 return true;
1227 if (strcmp(name, "rt") == 0)
1231 if (equal(name, "rt"))
12281232 return true;
1229 if (strcmp(name, "pthread") == 0)
1233 if (equal(name, "pthread"))
12301234 return true;
1231 if (strcmp(name, "crypt") == 0)
1235 if (equal(name, "crypt"))
12321236 return true;
1233 if (strcmp(name, "util") == 0)
1237 if (equal(name, "util"))
12341238 return true;
1235 if (strcmp(name, "xnet") == 0)
1239 if (equal(name, "xnet"))
12361240 return true;
1237 if (strcmp(name, "resolv") == 0)
1241 if (equal(name, "resolv"))
12381242 return true;
1239 if (strcmp(name, "dl") == 0)
1243 if (equal(name, "dl"))
12401244 return true;
1241 if (strcmp(name, "util") == 0)
1245 if (equal(name, "util"))
12421246 return true;
12431247 }
12441248
1249 if (target_os_is_darwin(target->os) && equal(name, "System"))
1250 return true;
1251
12451252 return false;
12461253}
12471254
src/util.hpp+8
......@@ -96,6 +96,14 @@ static inline bool mem_eql_str(const char *mem, size_t mem_len, const char *str)
9696 return mem_eql_mem(mem, mem_len, str, strlen(str));
9797}
9898
99static inline bool str_eql_str(const char *a, const char* b) {
100 return mem_eql_mem(a, strlen(a), b, strlen(b));
101}
102
103static inline bool str_eql_str_ignore_case(const char *a, const char* b) {
104 return mem_eql_mem_ignore_case(a, strlen(a), b, strlen(b));
105}
106
99107static inline bool is_power_of_2(uint64_t x) {
100108 return x != 0 && ((x & (~x + 1)) == x);
101109}