authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-04 15:16:52+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-04 11:21:56-07:00
log8b4e3b6aee61a26e7229ea30728e6c5e31c7cd0b
treec78414c59ad23d9dd24b8989cced5589203370a2
parenta306bfcd8eec166906ce2839bc7ff86ba6335376

comp: add support for -fdata-sections


10 files changed, 85 insertions(+), 12 deletions(-)

lib/std/Build/Step/Compile.zig+7
...@@ -123,6 +123,10 @@ link_emit_relocs: bool = false,...@@ -123,6 +123,10 @@ link_emit_relocs: bool = false,
123/// safely garbage-collected during the linking phase.123/// safely garbage-collected during the linking phase.
124link_function_sections: bool = false,124link_function_sections: bool = false,
125125
126/// Place every data in its own section so that unused ones may be
127/// safely garbage-collected during the linking phase.
128link_data_sections: bool = false,
129
126/// Remove functions and data that are unreachable by the entry point or130/// Remove functions and data that are unreachable by the entry point or
127/// exported symbols.131/// exported symbols.
128link_gc_sections: ?bool = null,132link_gc_sections: ?bool = null,
...@@ -1647,6 +1651,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -1647,6 +1651,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1647 if (self.link_function_sections) {1651 if (self.link_function_sections) {
1648 try zig_args.append("-ffunction-sections");1652 try zig_args.append("-ffunction-sections");
1649 }1653 }
1654 if (self.link_data_sections) {
1655 try zig_args.append("-fdata-sections");
1656 }
1650 if (self.link_gc_sections) |x| {1657 if (self.link_gc_sections) |x| {
1651 try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");1658 try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
1652 }1659 }
src/Compilation.zig+8
...@@ -623,6 +623,7 @@ pub const InitOptions = struct {...@@ -623,6 +623,7 @@ pub const InitOptions = struct {
623 formatted_panics: ?bool = null,623 formatted_panics: ?bool = null,
624 rdynamic: bool = false,624 rdynamic: bool = false,
625 function_sections: bool = false,625 function_sections: bool = false,
626 data_sections: bool = false,
626 no_builtin: bool = false,627 no_builtin: bool = false,
627 is_native_os: bool,628 is_native_os: bool,
628 is_native_abi: bool,629 is_native_abi: bool,
...@@ -1193,6 +1194,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1193,6 +1194,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1193 cache.hash.add(omit_frame_pointer);1194 cache.hash.add(omit_frame_pointer);
1194 cache.hash.add(link_mode);1195 cache.hash.add(link_mode);
1195 cache.hash.add(options.function_sections);1196 cache.hash.add(options.function_sections);
1197 cache.hash.add(options.data_sections);
1196 cache.hash.add(options.no_builtin);1198 cache.hash.add(options.no_builtin);
1197 cache.hash.add(strip);1199 cache.hash.add(strip);
1198 cache.hash.add(link_libc);1200 cache.hash.add(link_libc);
...@@ -1572,6 +1574,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1572,6 +1574,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1572 .is_native_os = options.is_native_os,1574 .is_native_os = options.is_native_os,
1573 .is_native_abi = options.is_native_abi,1575 .is_native_abi = options.is_native_abi,
1574 .function_sections = options.function_sections,1576 .function_sections = options.function_sections,
1577 .data_sections = options.data_sections,
1575 .no_builtin = options.no_builtin,1578 .no_builtin = options.no_builtin,
1576 .allow_shlib_undefined = options.linker_allow_shlib_undefined,1579 .allow_shlib_undefined = options.linker_allow_shlib_undefined,
1577 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,1580 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
...@@ -4903,6 +4906,10 @@ pub fn addCCArgs(...@@ -4903,6 +4906,10 @@ pub fn addCCArgs(
4903 try argv.append("-ffunction-sections");4906 try argv.append("-ffunction-sections");
4904 }4907 }
49054908
4909 if (comp.bin_file.options.data_sections) {
4910 try argv.append("-fdata-sections");
4911 }
4912
4906 if (comp.bin_file.options.no_builtin) {4913 if (comp.bin_file.options.no_builtin) {
4907 try argv.append("-fno-builtin");4914 try argv.append("-fno-builtin");
4908 }4915 }
...@@ -6405,6 +6412,7 @@ fn buildOutputFromZig(...@@ -6405,6 +6412,7 @@ fn buildOutputFromZig(
6405 .optimize_mode = comp.compilerRtOptMode(),6412 .optimize_mode = comp.compilerRtOptMode(),
6406 .link_mode = .Static,6413 .link_mode = .Static,
6407 .function_sections = true,6414 .function_sections = true,
6415 .data_sections = true,
6408 .no_builtin = true,6416 .no_builtin = true,
6409 .want_sanitize_c = false,6417 .want_sanitize_c = false,
6410 .want_stack_check = false,6418 .want_stack_check = false,
src/clang_options_data.zig+42-10
...@@ -2282,6 +2282,22 @@ flagpd1("fopenmp-is-device"),...@@ -2282,6 +2282,22 @@ flagpd1("fopenmp-is-device"),
2282 .pd2 = false,2282 .pd2 = false,
2283 .psl = true,2283 .psl = true,
2284},2284},
2285.{
2286 .name = "Qgather-",
2287 .syntax = .flag,
2288 .zig_equivalent = .other,
2289 .pd1 = true,
2290 .pd2 = false,
2291 .psl = true,
2292},
2293.{
2294 .name = "Qscatter-",
2295 .syntax = .flag,
2296 .zig_equivalent = .other,
2297 .pd1 = true,
2298 .pd2 = false,
2299 .psl = true,
2300},
2285sepd1("Xmicrosoft-visualc-tools-root"),2301sepd1("Xmicrosoft-visualc-tools-root"),
2286sepd1("Xmicrosoft-visualc-tools-version"),2302sepd1("Xmicrosoft-visualc-tools-version"),
2287sepd1("Xmicrosoft-windows-sdk-root"),2303sepd1("Xmicrosoft-windows-sdk-root"),
...@@ -2727,7 +2743,14 @@ flagpd1("fcxx-modules"),...@@ -2727,7 +2743,14 @@ flagpd1("fcxx-modules"),
2727flagpd1("fc++-static-destructors"),2743flagpd1("fc++-static-destructors"),
2728flagpd1("fd-lines-as-code"),2744flagpd1("fd-lines-as-code"),
2729flagpd1("fd-lines-as-comments"),2745flagpd1("fd-lines-as-comments"),
2730flagpd1("fdata-sections"),2746.{
2747 .name = "fdata-sections",
2748 .syntax = .flag,
2749 .zig_equivalent = .data_sections,
2750 .pd1 = true,
2751 .pd2 = false,
2752 .psl = false,
2753},
2731sepd1("fdebug-compilation-dir"),2754sepd1("fdebug-compilation-dir"),
2732flagpd1("fdebug-dump-all"),2755flagpd1("fdebug-dump-all"),
2733flagpd1("fdebug-dump-parse-tree"),2756flagpd1("fdebug-dump-parse-tree"),
...@@ -3130,7 +3153,14 @@ flagpd1("fno-cxx-modules"),...@@ -3130,7 +3153,14 @@ flagpd1("fno-cxx-modules"),
3130flagpd1("fno-c++-static-destructors"),3153flagpd1("fno-c++-static-destructors"),
3131flagpd1("fno-d-lines-as-code"),3154flagpd1("fno-d-lines-as-code"),
3132flagpd1("fno-d-lines-as-comments"),3155flagpd1("fno-d-lines-as-comments"),
3133flagpd1("fno-data-sections"),3156.{
3157 .name = "fno-data-sections",
3158 .syntax = .flag,
3159 .zig_equivalent = .no_data_sections,
3160 .pd1 = true,
3161 .pd2 = false,
3162 .psl = false,
3163},
3134flagpd1("fno-debug-info-for-profiling"),3164flagpd1("fno-debug-info-for-profiling"),
3135flagpd1("fno-debug-macro"),3165flagpd1("fno-debug-macro"),
3136flagpd1("fno-debug-pass-manager"),3166flagpd1("fno-debug-pass-manager"),
...@@ -4080,7 +4110,7 @@ flagpd1("malign-double"),...@@ -4080,7 +4110,7 @@ flagpd1("malign-double"),
4080m("maltivec"),4110m("maltivec"),
4081flagpd1("mamdgpu-ieee"),4111flagpd1("mamdgpu-ieee"),
4082m("mamx-bf16"),4112m("mamx-bf16"),
4083flagpd1("mamx-complex"),4113m("mamx-complex"),
4084m("mamx-fp16"),4114m("mamx-fp16"),
4085m("mamx-int8"),4115m("mamx-int8"),
4086m("mamx-tile"),4116m("mamx-tile"),
...@@ -4109,7 +4139,7 @@ m("mavx512vpopcntdq"),...@@ -4109,7 +4139,7 @@ m("mavx512vpopcntdq"),
4109m("mavxifma"),4139m("mavxifma"),
4110m("mavxneconvert"),4140m("mavxneconvert"),
4111m("mavxvnni"),4141m("mavxvnni"),
4112flagpd1("mavxvnniint16"),4142m("mavxvnniint16"),
4113m("mavxvnniint8"),4143m("mavxvnniint8"),
4114flagpd1("mbackchain"),4144flagpd1("mbackchain"),
4115flagpd1("mbig-endian"),4145flagpd1("mbig-endian"),
...@@ -4264,7 +4294,7 @@ m("mno-aes"),...@@ -4264,7 +4294,7 @@ m("mno-aes"),
4264m("mno-altivec"),4294m("mno-altivec"),
4265flagpd1("mno-amdgpu-ieee"),4295flagpd1("mno-amdgpu-ieee"),
4266m("mno-amx-bf16"),4296m("mno-amx-bf16"),
4267flagpd1("mno-amx-complex"),4297m("mno-amx-complex"),
4268m("mno-amx-fp16"),4298m("mno-amx-fp16"),
4269m("mno-amx-int8"),4299m("mno-amx-int8"),
4270m("mno-amx-tile"),4300m("mno-amx-tile"),
...@@ -4290,7 +4320,7 @@ m("mno-avx512vpopcntdq"),...@@ -4290,7 +4320,7 @@ m("mno-avx512vpopcntdq"),
4290m("mno-avxifma"),4320m("mno-avxifma"),
4291m("mno-avxneconvert"),4321m("mno-avxneconvert"),
4292m("mno-avxvnni"),4322m("mno-avxvnni"),
4293flagpd1("mno-avxvnniint16"),4323m("mno-avxvnniint16"),
4294m("mno-avxvnniint8"),4324m("mno-avxvnniint8"),
4295flagpd1("mno-backchain"),4325flagpd1("mno-backchain"),
4296m("mno-bmi"),4326m("mno-bmi"),
...@@ -4336,6 +4366,7 @@ m("mno-fpu"),...@@ -4336,6 +4366,7 @@ m("mno-fpu"),
4336m("mno-fsgsbase"),4366m("mno-fsgsbase"),
4337m("mno-fsmuld"),4367m("mno-fsmuld"),
4338m("mno-fxsr"),4368m("mno-fxsr"),
4369flagpd1("mno-gather"),
4339m("mno-gfni"),4370m("mno-gfni"),
4340m("mno-ginv"),4371m("mno-ginv"),
4341flagpd1("mno-global-merge"),4372flagpd1("mno-global-merge"),
...@@ -4433,16 +4464,17 @@ flagpd1("mno-rtd"),...@@ -4433,16 +4464,17 @@ flagpd1("mno-rtd"),
4433m("mno-rtm"),4464m("mno-rtm"),
4434m("mno-sahf"),4465m("mno-sahf"),
4435m("mno-save-restore"),4466m("mno-save-restore"),
4467flagpd1("mno-scatter"),
4436m("mno-serialize"),4468m("mno-serialize"),
4437m("mno-seses"),4469m("mno-seses"),
4438m("mno-sgx"),4470m("mno-sgx"),
4439m("mno-sha"),4471m("mno-sha"),
4440flagpd1("mno-sha512"),4472m("mno-sha512"),
4441m("mno-shstk"),4473m("mno-shstk"),
4442m("mno-sign-ext"),4474m("mno-sign-ext"),
4443m("mno-simd128"),4475m("mno-simd128"),
4444flagpd1("mno-skip-rax-setup"),4476flagpd1("mno-skip-rax-setup"),
4445flagpd1("mno-sm3"),4477m("mno-sm3"),
4446m("mno-sm4"),4478m("mno-sm4"),
4447m("mno-soft-float"),4479m("mno-soft-float"),
4448m("mno-spe"),4480m("mno-spe"),
...@@ -4568,14 +4600,14 @@ m("msecure-plt"),...@@ -4568,14 +4600,14 @@ m("msecure-plt"),
4568m("mserialize"),4600m("mserialize"),
4569m("msgx"),4601m("msgx"),
4570m("msha"),4602m("msha"),
4571flagpd1("msha512"),4603m("msha512"),
4572m("mshstk"),4604m("mshstk"),
4573m("msign-ext"),4605m("msign-ext"),
4574flagpd1("msim"),4606flagpd1("msim"),
4575m("msimd128"),4607m("msimd128"),
4576m("msingle-float"),4608m("msingle-float"),
4577flagpd1("mskip-rax-setup"),4609flagpd1("mskip-rax-setup"),
4578flagpd1("msm3"),4610m("msm3"),
4579m("msm4"),4611m("msm4"),
4580sepd1("msmall-data-limit"),4612sepd1("msmall-data-limit"),
4581m("msoft-float"),4613m("msoft-float"),
src/codegen/llvm.zig+1
...@@ -955,6 +955,7 @@ pub const Object = struct {...@@ -955,6 +955,7 @@ pub const Object = struct {
955 reloc_mode,955 reloc_mode,
956 code_model,956 code_model,
957 options.function_sections,957 options.function_sections,
958 options.data_sections,
958 float_abi,959 float_abi,
959 if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,960 if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,
960 );961 );
src/codegen/llvm/bindings.zig+1
...@@ -919,6 +919,7 @@ pub const TargetMachine = opaque {...@@ -919,6 +919,7 @@ pub const TargetMachine = opaque {
919 Reloc: RelocMode,919 Reloc: RelocMode,
920 CodeModel: CodeModel,920 CodeModel: CodeModel,
921 function_sections: bool,921 function_sections: bool,
922 data_sections: bool,
922 float_abi: ABIType,923 float_abi: ABIType,
923 abi_name: ?[*:0]const u8,924 abi_name: ?[*:0]const u8,
924 ) *TargetMachine;925 ) *TargetMachine;
src/link.zig+1
...@@ -139,6 +139,7 @@ pub const Options = struct {...@@ -139,6 +139,7 @@ pub const Options = struct {
139 link_libunwind: bool,139 link_libunwind: bool,
140 darwin_sdk_layout: ?DarwinSdkLayout,140 darwin_sdk_layout: ?DarwinSdkLayout,
141 function_sections: bool,141 function_sections: bool,
142 data_sections: bool,
142 no_builtin: bool,143 no_builtin: bool,
143 eh_frame_hdr: bool,144 eh_frame_hdr: bool,
144 emit_relocs: bool,145 emit_relocs: bool,
src/main.zig+12
...@@ -453,6 +453,8 @@ const usage_build_generic =...@@ -453,6 +453,8 @@ const usage_build_generic =
453 \\ -fno-builtin Disable implicit builtin knowledge of functions453 \\ -fno-builtin Disable implicit builtin knowledge of functions
454 \\ -ffunction-sections Places each function in a separate section454 \\ -ffunction-sections Places each function in a separate section
455 \\ -fno-function-sections All functions go into same section455 \\ -fno-function-sections All functions go into same section
456 \\ -fdata-sections Places each data in a separate section
457 \\ -fno-data-sections All data go into same section
456 \\ -fstrip Omit debug symbols458 \\ -fstrip Omit debug symbols
457 \\ -fno-strip Keep debug symbols459 \\ -fno-strip Keep debug symbols
458 \\ -fformatted-panics Enable formatted safety panics460 \\ -fformatted-panics Enable formatted safety panics
...@@ -793,6 +795,7 @@ fn buildOutputType(...@@ -793,6 +795,7 @@ fn buildOutputType(
793 var strip: ?bool = null;795 var strip: ?bool = null;
794 var formatted_panics: ?bool = null;796 var formatted_panics: ?bool = null;
795 var function_sections = false;797 var function_sections = false;
798 var data_sections = false;
796 var no_builtin = false;799 var no_builtin = false;
797 var listen: Listen = .none;800 var listen: Listen = .none;
798 var debug_compile_errors = false;801 var debug_compile_errors = false;
...@@ -1459,6 +1462,10 @@ fn buildOutputType(...@@ -1459,6 +1462,10 @@ fn buildOutputType(
1459 function_sections = true;1462 function_sections = true;
1460 } else if (mem.eql(u8, arg, "-fno-function-sections")) {1463 } else if (mem.eql(u8, arg, "-fno-function-sections")) {
1461 function_sections = false;1464 function_sections = false;
1465 } else if (mem.eql(u8, arg, "-fdata-sections")) {
1466 data_sections = true;
1467 } else if (mem.eql(u8, arg, "-fno-data-sections")) {
1468 data_sections = false;
1462 } else if (mem.eql(u8, arg, "-fbuiltin")) {1469 } else if (mem.eql(u8, arg, "-fbuiltin")) {
1463 no_builtin = false;1470 no_builtin = false;
1464 } else if (mem.eql(u8, arg, "-fno-builtin")) {1471 } else if (mem.eql(u8, arg, "-fno-builtin")) {
...@@ -1780,6 +1787,8 @@ fn buildOutputType(...@@ -1780,6 +1787,8 @@ fn buildOutputType(
1780 .no_omit_frame_pointer => omit_frame_pointer = false,1787 .no_omit_frame_pointer => omit_frame_pointer = false,
1781 .function_sections => function_sections = true,1788 .function_sections => function_sections = true,
1782 .no_function_sections => function_sections = false,1789 .no_function_sections => function_sections = false,
1790 .data_sections => data_sections = true,
1791 .no_data_sections => data_sections = false,
1783 .builtin => no_builtin = false,1792 .builtin => no_builtin = false,
1784 .no_builtin => no_builtin = true,1793 .no_builtin => no_builtin = true,
1785 .color_diagnostics => color = .on,1794 .color_diagnostics => color = .on,
...@@ -3475,6 +3484,7 @@ fn buildOutputType(...@@ -3475,6 +3484,7 @@ fn buildOutputType(
3475 .formatted_panics = formatted_panics,3484 .formatted_panics = formatted_panics,
3476 .single_threaded = single_threaded,3485 .single_threaded = single_threaded,
3477 .function_sections = function_sections,3486 .function_sections = function_sections,
3487 .data_sections = data_sections,
3478 .no_builtin = no_builtin,3488 .no_builtin = no_builtin,
3479 .self_exe_path = self_exe_path,3489 .self_exe_path = self_exe_path,
3480 .thread_pool = &thread_pool,3490 .thread_pool = &thread_pool,
...@@ -5589,6 +5599,8 @@ pub const ClangArgIterator = struct {...@@ -5589,6 +5599,8 @@ pub const ClangArgIterator = struct {
5589 no_omit_frame_pointer,5599 no_omit_frame_pointer,
5590 function_sections,5600 function_sections,
5591 no_function_sections,5601 no_function_sections,
5602 data_sections,
5603 no_data_sections,
5592 builtin,5604 builtin,
5593 no_builtin,5605 no_builtin,
5594 color_diagnostics,5606 color_diagnostics,
src/zig_llvm.cpp+3-1
...@@ -82,7 +82,8 @@ static const bool assertions_on = false;...@@ -82,7 +82,8 @@ static const bool assertions_on = false;
8282
83LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,83LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
84 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,84 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
85 LLVMCodeModel CodeModel, bool function_sections, ZigLLVMABIType float_abi, const char *abi_name)85 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
86 const char *abi_name)
86{87{
87 std::optional<Reloc::Model> RM;88 std::optional<Reloc::Model> RM;
88 switch (Reloc){89 switch (Reloc){
...@@ -130,6 +131,7 @@ LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Tri...@@ -130,6 +131,7 @@ LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Tri
130 TargetOptions opt;131 TargetOptions opt;
131132
132 opt.FunctionSections = function_sections;133 opt.FunctionSections = function_sections;
134 opt.DataSections = data_sections;
133 switch (float_abi) {135 switch (float_abi) {
134 case ZigLLVMABITypeDefault:136 case ZigLLVMABITypeDefault:
135 opt.FloatABIType = FloatABI::Default;137 opt.FloatABIType = FloatABI::Default;
src/zig_llvm.h+2-1
...@@ -57,7 +57,8 @@ enum ZigLLVMABIType {...@@ -57,7 +57,8 @@ enum ZigLLVMABIType {
5757
58ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,58ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
59 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,59 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
60 LLVMCodeModel CodeModel, bool function_sections, enum ZigLLVMABIType float_abi, const char *abi_name);60 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
61 const char *abi_name);
6162
62ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);63ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
6364
tools/update_clang_options.zig+8
...@@ -332,6 +332,14 @@ const known_options = [_]KnownOpt{...@@ -332,6 +332,14 @@ const known_options = [_]KnownOpt{
332 .name = "fno-function-sections",332 .name = "fno-function-sections",
333 .ident = "no_function_sections",333 .ident = "no_function_sections",
334 },334 },
335 .{
336 .name = "fdata-sections",
337 .ident = "data_sections",
338 },
339 .{
340 .name = "fno-data-sections",
341 .ident = "no_data_sections",
342 },
335 .{343 .{
336 .name = "fbuiltin",344 .name = "fbuiltin",
337 .ident = "builtin",345 .ident = "builtin",