authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-02 17:29:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-02 17:29:22-04:00
log5314641e117f3b3d9cef2db527af532ddf18ccfc
tree736b147b2016cf79979a5bf21923d3fa8c3ee4ed
parentc1778bd41f1fd340662920909fdd9992ac55133e
signaturelock-open Commit is signed but in an unrecognized format.

zig cc: support more linker args


4 files changed, 124 insertions(+), 7 deletions(-)

src/all_types.hpp+11
......@@ -2015,6 +2015,12 @@ enum WantCSanitize {
20152015 WantCSanitizeEnabled,
20162016};
20172017
2018enum OptionalBool {
2019 OptionalBoolNull,
2020 OptionalBoolFalse,
2021 OptionalBoolTrue,
2022};
2023
20182024struct CFile {
20192025 ZigList<const char *> args;
20202026 const char *source_path;
......@@ -2260,6 +2266,8 @@ struct CodeGen {
22602266 TargetSubsystem subsystem; // careful using this directly; see detect_subsystem
22612267 ValgrindSupport valgrind_support;
22622268 CodeModel code_model;
2269 OptionalBool linker_gc_sections;
2270 OptionalBool linker_allow_shlib_undefined;
22632271 bool strip_debug_symbols;
22642272 bool is_test_build;
22652273 bool is_single_threaded;
......@@ -2280,6 +2288,8 @@ struct CodeGen {
22802288 bool emit_asm;
22812289 bool emit_llvm_ir;
22822290 bool test_is_evented;
2291 bool linker_z_nodelete;
2292 bool linker_z_defs;
22832293
22842294 Buf *root_out_name;
22852295 Buf *test_filter;
......@@ -2288,6 +2298,7 @@ struct CodeGen {
22882298 Buf *zig_std_dir;
22892299 Buf *version_script_path;
22902300 Buf *override_soname;
2301 Buf *linker_optimization;
22912302
22922303 const char **llvm_argv;
22932304 size_t llvm_argv_len;
src/codegen.cpp+5
......@@ -10558,6 +10558,11 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
1055810558 }
1055910559 cache_buf_opt(ch, g->version_script_path);
1056010560 cache_buf_opt(ch, g->override_soname);
10561 cache_buf_opt(ch, g->linker_optimization);
10562 cache_int(ch, g->linker_gc_sections);
10563 cache_int(ch, g->linker_allow_shlib_undefined);
10564 cache_bool(ch, g->linker_z_nodelete);
10565 cache_bool(ch, g->linker_z_defs);
1056110566
1056210567 // gen_c_objects appends objects to g->link_objects which we want to include in the hash
1056310568 gen_c_objects(g);
src/link.cpp+73-7
......@@ -1769,8 +1769,17 @@ static void construct_linker_job_elf(LinkJob *lj) {
17691769 lj->args.append(g->linker_script);
17701770 }
17711771
1772 if (g->out_type != OutTypeObj) {
1773 lj->args.append("--gc-sections");
1772 switch (g->linker_gc_sections) {
1773 case OptionalBoolNull:
1774 if (g->out_type != OutTypeObj) {
1775 lj->args.append("--gc-sections");
1776 }
1777 break;
1778 case OptionalBoolTrue:
1779 lj->args.append("--gc-sections");
1780 break;
1781 case OptionalBoolFalse:
1782 break;
17741783 }
17751784
17761785 if (g->link_eh_frame_hdr) {
......@@ -1781,6 +1790,19 @@ static void construct_linker_job_elf(LinkJob *lj) {
17811790 lj->args.append("--export-dynamic");
17821791 }
17831792
1793 if (g->linker_optimization != nullptr) {
1794 lj->args.append(buf_ptr(g->linker_optimization));
1795 }
1796
1797 if (g->linker_z_nodelete) {
1798 lj->args.append("-z");
1799 lj->args.append("nodelete");
1800 }
1801 if (g->linker_z_defs) {
1802 lj->args.append("-z");
1803 lj->args.append("defs");
1804 }
1805
17841806 lj->args.append("-m");
17851807 lj->args.append(getLDMOption(g->zig_target));
17861808
......@@ -1971,8 +1993,17 @@ static void construct_linker_job_elf(LinkJob *lj) {
19711993 }
19721994 }
19731995
1974 if (!g->zig_target->is_native_os) {
1975 lj->args.append("--allow-shlib-undefined");
1996 switch (g->linker_allow_shlib_undefined) {
1997 case OptionalBoolNull:
1998 if (!g->zig_target->is_native_os) {
1999 lj->args.append("--allow-shlib-undefined");
2000 }
2001 break;
2002 case OptionalBoolFalse:
2003 break;
2004 case OptionalBoolTrue:
2005 lj->args.append("--allow-shlib-undefined");
2006 break;
19762007 }
19772008}
19782009
......@@ -2536,10 +2567,34 @@ static void construct_linker_job_macho(LinkJob *lj) {
25362567 //lj->args.append("-error-limit=0");
25372568 lj->args.append("-demangle");
25382569
2570 switch (g->linker_gc_sections) {
2571 case OptionalBoolNull:
2572 // TODO why do we not follow the same logic of elf here?
2573 break;
2574 case OptionalBoolTrue:
2575 lj->args.append("--gc-sections");
2576 break;
2577 case OptionalBoolFalse:
2578 break;
2579 }
2580
25392581 if (g->linker_rdynamic) {
25402582 lj->args.append("-export_dynamic");
25412583 }
25422584
2585 if (g->linker_optimization != nullptr) {
2586 lj->args.append(buf_ptr(g->linker_optimization));
2587 }
2588
2589 if (g->linker_z_nodelete) {
2590 lj->args.append("-z");
2591 lj->args.append("nodelete");
2592 }
2593 if (g->linker_z_defs) {
2594 lj->args.append("-z");
2595 lj->args.append("defs");
2596 }
2597
25432598 bool is_lib = g->out_type == OutTypeLib;
25442599 bool is_dyn_lib = g->is_dynamic && is_lib;
25452600 if (is_lib && !g->is_dynamic) {
......@@ -2654,9 +2709,20 @@ static void construct_linker_job_macho(LinkJob *lj) {
26542709 // and change between versions.
26552710 // so we always link against libSystem
26562711 lj->args.append("-lSystem");
2657 } else {
2658 lj->args.append("-undefined");
2659 lj->args.append("dynamic_lookup");
2712 }
2713 switch (g->linker_allow_shlib_undefined) {
2714 case OptionalBoolNull:
2715 if (!g->zig_target->is_native_os) {
2716 lj->args.append("-undefined");
2717 lj->args.append("dynamic_lookup");
2718 }
2719 break;
2720 case OptionalBoolFalse:
2721 break;
2722 case OptionalBoolTrue:
2723 lj->args.append("-undefined");
2724 lj->args.append("dynamic_lookup");
2725 break;
26602726 }
26612727
26622728 for (size_t i = 0; i < g->framework_dirs.length; i += 1) {
src/main.cpp+35
......@@ -459,6 +459,11 @@ static int main0(int argc, char **argv) {
459459 bool ensure_libc_on_non_freestanding = false;
460460 bool ensure_libcpp_on_non_freestanding = false;
461461 bool disable_c_depfile = false;
462 Buf *linker_optimization = nullptr;
463 OptionalBool linker_gc_sections = OptionalBoolNull;
464 OptionalBool linker_allow_shlib_undefined = OptionalBoolNull;
465 bool linker_z_nodelete = false;
466 bool linker_z_defs = false;
462467
463468 ZigList<const char *> llvm_argv = {0};
464469 llvm_argv.append("zig (LLVM option parsing)");
......@@ -822,6 +827,30 @@ static int main0(int argc, char **argv) {
822827 return EXIT_FAILURE;
823828 }
824829 version_script = linker_args.at(i);
830 } else if (buf_starts_with_str(arg, "-O")) {
831 linker_optimization = arg;
832 } else if (buf_eql_str(arg, "--gc-sections")) {
833 linker_gc_sections = OptionalBoolTrue;
834 } else if (buf_eql_str(arg, "--no-gc-sections")) {
835 linker_gc_sections = OptionalBoolFalse;
836 } else if (buf_eql_str(arg, "--allow-shlib-undefined")) {
837 linker_allow_shlib_undefined = OptionalBoolTrue;
838 } else if (buf_eql_str(arg, "--no-allow-shlib-undefined")) {
839 linker_allow_shlib_undefined = OptionalBoolFalse;
840 } else if (buf_eql_str(arg, "-z")) {
841 i += 1;
842 if (i >= linker_args.length) {
843 fprintf(stderr, "expected linker arg after '%s'\n", buf_ptr(arg));
844 return EXIT_FAILURE;
845 }
846 Buf *z_arg = linker_args.at(i);
847 if (buf_eql_str(z_arg, "nodelete")) {
848 linker_z_nodelete = true;
849 } else if (buf_eql_str(z_arg, "defs")) {
850 linker_z_defs = true;
851 } else {
852 fprintf(stderr, "warning: unsupported linker arg: -z %s\n", buf_ptr(z_arg));
853 }
825854 } else {
826855 fprintf(stderr, "warning: unsupported linker arg: %s\n", buf_ptr(arg));
827856 }
......@@ -1542,6 +1571,12 @@ static int main0(int argc, char **argv) {
15421571 g->code_model = code_model;
15431572 g->disable_c_depfile = disable_c_depfile;
15441573
1574 g->linker_optimization = linker_optimization;
1575 g->linker_gc_sections = linker_gc_sections;
1576 g->linker_allow_shlib_undefined = linker_allow_shlib_undefined;
1577 g->linker_z_nodelete = linker_z_nodelete;
1578 g->linker_z_defs = linker_z_defs;
1579
15451580 if (override_soname) {
15461581 g->override_soname = buf_create_from_str(override_soname);
15471582 }