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,
123123/// safely garbage-collected during the linking phase.
124124link_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
126130/// Remove functions and data that are unreachable by the entry point or
127131/// exported symbols.
128132link_gc_sections: ?bool = null,
......@@ -1647,6 +1651,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
16471651 if (self.link_function_sections) {
16481652 try zig_args.append("-ffunction-sections");
16491653 }
1654 if (self.link_data_sections) {
1655 try zig_args.append("-fdata-sections");
1656 }
16501657 if (self.link_gc_sections) |x| {
16511658 try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
16521659 }
src/Compilation.zig+8
......@@ -623,6 +623,7 @@ pub const InitOptions = struct {
623623 formatted_panics: ?bool = null,
624624 rdynamic: bool = false,
625625 function_sections: bool = false,
626 data_sections: bool = false,
626627 no_builtin: bool = false,
627628 is_native_os: bool,
628629 is_native_abi: bool,
......@@ -1193,6 +1194,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11931194 cache.hash.add(omit_frame_pointer);
11941195 cache.hash.add(link_mode);
11951196 cache.hash.add(options.function_sections);
1197 cache.hash.add(options.data_sections);
11961198 cache.hash.add(options.no_builtin);
11971199 cache.hash.add(strip);
11981200 cache.hash.add(link_libc);
......@@ -1572,6 +1574,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15721574 .is_native_os = options.is_native_os,
15731575 .is_native_abi = options.is_native_abi,
15741576 .function_sections = options.function_sections,
1577 .data_sections = options.data_sections,
15751578 .no_builtin = options.no_builtin,
15761579 .allow_shlib_undefined = options.linker_allow_shlib_undefined,
15771580 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
......@@ -4903,6 +4906,10 @@ pub fn addCCArgs(
49034906 try argv.append("-ffunction-sections");
49044907 }
49054908
4909 if (comp.bin_file.options.data_sections) {
4910 try argv.append("-fdata-sections");
4911 }
4912
49064913 if (comp.bin_file.options.no_builtin) {
49074914 try argv.append("-fno-builtin");
49084915 }
......@@ -6405,6 +6412,7 @@ fn buildOutputFromZig(
64056412 .optimize_mode = comp.compilerRtOptMode(),
64066413 .link_mode = .Static,
64076414 .function_sections = true,
6415 .data_sections = true,
64086416 .no_builtin = true,
64096417 .want_sanitize_c = false,
64106418 .want_stack_check = false,
src/clang_options_data.zig+42-10
......@@ -2282,6 +2282,22 @@ flagpd1("fopenmp-is-device"),
22822282 .pd2 = false,
22832283 .psl = true,
22842284},
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},
22852301sepd1("Xmicrosoft-visualc-tools-root"),
22862302sepd1("Xmicrosoft-visualc-tools-version"),
22872303sepd1("Xmicrosoft-windows-sdk-root"),
......@@ -2727,7 +2743,14 @@ flagpd1("fcxx-modules"),
27272743flagpd1("fc++-static-destructors"),
27282744flagpd1("fd-lines-as-code"),
27292745flagpd1("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},
27312754sepd1("fdebug-compilation-dir"),
27322755flagpd1("fdebug-dump-all"),
27332756flagpd1("fdebug-dump-parse-tree"),
......@@ -3130,7 +3153,14 @@ flagpd1("fno-cxx-modules"),
31303153flagpd1("fno-c++-static-destructors"),
31313154flagpd1("fno-d-lines-as-code"),
31323155flagpd1("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},
31343164flagpd1("fno-debug-info-for-profiling"),
31353165flagpd1("fno-debug-macro"),
31363166flagpd1("fno-debug-pass-manager"),
......@@ -4080,7 +4110,7 @@ flagpd1("malign-double"),
40804110m("maltivec"),
40814111flagpd1("mamdgpu-ieee"),
40824112m("mamx-bf16"),
4083flagpd1("mamx-complex"),
4113m("mamx-complex"),
40844114m("mamx-fp16"),
40854115m("mamx-int8"),
40864116m("mamx-tile"),
......@@ -4109,7 +4139,7 @@ m("mavx512vpopcntdq"),
41094139m("mavxifma"),
41104140m("mavxneconvert"),
41114141m("mavxvnni"),
4112flagpd1("mavxvnniint16"),
4142m("mavxvnniint16"),
41134143m("mavxvnniint8"),
41144144flagpd1("mbackchain"),
41154145flagpd1("mbig-endian"),
......@@ -4264,7 +4294,7 @@ m("mno-aes"),
42644294m("mno-altivec"),
42654295flagpd1("mno-amdgpu-ieee"),
42664296m("mno-amx-bf16"),
4267flagpd1("mno-amx-complex"),
4297m("mno-amx-complex"),
42684298m("mno-amx-fp16"),
42694299m("mno-amx-int8"),
42704300m("mno-amx-tile"),
......@@ -4290,7 +4320,7 @@ m("mno-avx512vpopcntdq"),
42904320m("mno-avxifma"),
42914321m("mno-avxneconvert"),
42924322m("mno-avxvnni"),
4293flagpd1("mno-avxvnniint16"),
4323m("mno-avxvnniint16"),
42944324m("mno-avxvnniint8"),
42954325flagpd1("mno-backchain"),
42964326m("mno-bmi"),
......@@ -4336,6 +4366,7 @@ m("mno-fpu"),
43364366m("mno-fsgsbase"),
43374367m("mno-fsmuld"),
43384368m("mno-fxsr"),
4369flagpd1("mno-gather"),
43394370m("mno-gfni"),
43404371m("mno-ginv"),
43414372flagpd1("mno-global-merge"),
......@@ -4433,16 +4464,17 @@ flagpd1("mno-rtd"),
44334464m("mno-rtm"),
44344465m("mno-sahf"),
44354466m("mno-save-restore"),
4467flagpd1("mno-scatter"),
44364468m("mno-serialize"),
44374469m("mno-seses"),
44384470m("mno-sgx"),
44394471m("mno-sha"),
4440flagpd1("mno-sha512"),
4472m("mno-sha512"),
44414473m("mno-shstk"),
44424474m("mno-sign-ext"),
44434475m("mno-simd128"),
44444476flagpd1("mno-skip-rax-setup"),
4445flagpd1("mno-sm3"),
4477m("mno-sm3"),
44464478m("mno-sm4"),
44474479m("mno-soft-float"),
44484480m("mno-spe"),
......@@ -4568,14 +4600,14 @@ m("msecure-plt"),
45684600m("mserialize"),
45694601m("msgx"),
45704602m("msha"),
4571flagpd1("msha512"),
4603m("msha512"),
45724604m("mshstk"),
45734605m("msign-ext"),
45744606flagpd1("msim"),
45754607m("msimd128"),
45764608m("msingle-float"),
45774609flagpd1("mskip-rax-setup"),
4578flagpd1("msm3"),
4610m("msm3"),
45794611m("msm4"),
45804612sepd1("msmall-data-limit"),
45814613m("msoft-float"),
src/codegen/llvm.zig+1
......@@ -955,6 +955,7 @@ pub const Object = struct {
955955 reloc_mode,
956956 code_model,
957957 options.function_sections,
958 options.data_sections,
958959 float_abi,
959960 if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,
960961 );
src/codegen/llvm/bindings.zig+1
......@@ -919,6 +919,7 @@ pub const TargetMachine = opaque {
919919 Reloc: RelocMode,
920920 CodeModel: CodeModel,
921921 function_sections: bool,
922 data_sections: bool,
922923 float_abi: ABIType,
923924 abi_name: ?[*:0]const u8,
924925 ) *TargetMachine;
src/link.zig+1
......@@ -139,6 +139,7 @@ pub const Options = struct {
139139 link_libunwind: bool,
140140 darwin_sdk_layout: ?DarwinSdkLayout,
141141 function_sections: bool,
142 data_sections: bool,
142143 no_builtin: bool,
143144 eh_frame_hdr: bool,
144145 emit_relocs: bool,
src/main.zig+12
......@@ -453,6 +453,8 @@ const usage_build_generic =
453453 \\ -fno-builtin Disable implicit builtin knowledge of functions
454454 \\ -ffunction-sections Places each function in a separate section
455455 \\ -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
456458 \\ -fstrip Omit debug symbols
457459 \\ -fno-strip Keep debug symbols
458460 \\ -fformatted-panics Enable formatted safety panics
......@@ -793,6 +795,7 @@ fn buildOutputType(
793795 var strip: ?bool = null;
794796 var formatted_panics: ?bool = null;
795797 var function_sections = false;
798 var data_sections = false;
796799 var no_builtin = false;
797800 var listen: Listen = .none;
798801 var debug_compile_errors = false;
......@@ -1459,6 +1462,10 @@ fn buildOutputType(
14591462 function_sections = true;
14601463 } else if (mem.eql(u8, arg, "-fno-function-sections")) {
14611464 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;
14621469 } else if (mem.eql(u8, arg, "-fbuiltin")) {
14631470 no_builtin = false;
14641471 } else if (mem.eql(u8, arg, "-fno-builtin")) {
......@@ -1780,6 +1787,8 @@ fn buildOutputType(
17801787 .no_omit_frame_pointer => omit_frame_pointer = false,
17811788 .function_sections => function_sections = true,
17821789 .no_function_sections => function_sections = false,
1790 .data_sections => data_sections = true,
1791 .no_data_sections => data_sections = false,
17831792 .builtin => no_builtin = false,
17841793 .no_builtin => no_builtin = true,
17851794 .color_diagnostics => color = .on,
......@@ -3475,6 +3484,7 @@ fn buildOutputType(
34753484 .formatted_panics = formatted_panics,
34763485 .single_threaded = single_threaded,
34773486 .function_sections = function_sections,
3487 .data_sections = data_sections,
34783488 .no_builtin = no_builtin,
34793489 .self_exe_path = self_exe_path,
34803490 .thread_pool = &thread_pool,
......@@ -5589,6 +5599,8 @@ pub const ClangArgIterator = struct {
55895599 no_omit_frame_pointer,
55905600 function_sections,
55915601 no_function_sections,
5602 data_sections,
5603 no_data_sections,
55925604 builtin,
55935605 no_builtin,
55945606 color_diagnostics,
src/zig_llvm.cpp+3-1
......@@ -82,7 +82,8 @@ static const bool assertions_on = false;
8282
8383LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
8484 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)
8687{
8788 std::optional<Reloc::Model> RM;
8889 switch (Reloc){
......@@ -130,6 +131,7 @@ LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Tri
130131 TargetOptions opt;
131132
132133 opt.FunctionSections = function_sections;
134 opt.DataSections = data_sections;
133135 switch (float_abi) {
134136 case ZigLLVMABITypeDefault:
135137 opt.FloatABIType = FloatABI::Default;
src/zig_llvm.h+2-1
......@@ -57,7 +57,8 @@ enum ZigLLVMABIType {
5757
5858ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
5959 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
6263ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
6364
tools/update_clang_options.zig+8
......@@ -332,6 +332,14 @@ const known_options = [_]KnownOpt{
332332 .name = "fno-function-sections",
333333 .ident = "no_function_sections",
334334 },
335 .{
336 .name = "fdata-sections",
337 .ident = "data_sections",
338 },
339 .{
340 .name = "fno-data-sections",
341 .ident = "no_data_sections",
342 },
335343 .{
336344 .name = "fbuiltin",
337345 .ident = "builtin",