comp: add support for -fdata-sections

This commit is contained in:
Jakub Konka 2023-10-04 15:16:52 +02:00 committed by Andrew Kelley
parent a306bfcd8e
commit 8b4e3b6aee
10 changed files with 85 additions and 12 deletions

View file

@ -123,6 +123,10 @@ link_emit_relocs: bool = false,
/// safely garbage-collected during the linking phase.
link_function_sections: bool = false,
/// Place every data in its own section so that unused ones may be
/// safely garbage-collected during the linking phase.
link_data_sections: bool = false,
/// Remove functions and data that are unreachable by the entry point or
/// exported symbols.
link_gc_sections: ?bool = null,
@ -1647,6 +1651,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
if (self.link_function_sections) {
try zig_args.append("-ffunction-sections");
}
if (self.link_data_sections) {
try zig_args.append("-fdata-sections");
}
if (self.link_gc_sections) |x| {
try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
}

View file

@ -623,6 +623,7 @@ pub const InitOptions = struct {
formatted_panics: ?bool = null,
rdynamic: bool = false,
function_sections: bool = false,
data_sections: bool = false,
no_builtin: bool = false,
is_native_os: bool,
is_native_abi: bool,
@ -1193,6 +1194,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
cache.hash.add(omit_frame_pointer);
cache.hash.add(link_mode);
cache.hash.add(options.function_sections);
cache.hash.add(options.data_sections);
cache.hash.add(options.no_builtin);
cache.hash.add(strip);
cache.hash.add(link_libc);
@ -1572,6 +1574,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
.is_native_os = options.is_native_os,
.is_native_abi = options.is_native_abi,
.function_sections = options.function_sections,
.data_sections = options.data_sections,
.no_builtin = options.no_builtin,
.allow_shlib_undefined = options.linker_allow_shlib_undefined,
.bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
@ -4903,6 +4906,10 @@ pub fn addCCArgs(
try argv.append("-ffunction-sections");
}
if (comp.bin_file.options.data_sections) {
try argv.append("-fdata-sections");
}
if (comp.bin_file.options.no_builtin) {
try argv.append("-fno-builtin");
}
@ -6405,6 +6412,7 @@ fn buildOutputFromZig(
.optimize_mode = comp.compilerRtOptMode(),
.link_mode = .Static,
.function_sections = true,
.data_sections = true,
.no_builtin = true,
.want_sanitize_c = false,
.want_stack_check = false,

View file

@ -2282,6 +2282,22 @@ flagpd1("fopenmp-is-device"),
.pd2 = false,
.psl = true,
},
.{
.name = "Qgather-",
.syntax = .flag,
.zig_equivalent = .other,
.pd1 = true,
.pd2 = false,
.psl = true,
},
.{
.name = "Qscatter-",
.syntax = .flag,
.zig_equivalent = .other,
.pd1 = true,
.pd2 = false,
.psl = true,
},
sepd1("Xmicrosoft-visualc-tools-root"),
sepd1("Xmicrosoft-visualc-tools-version"),
sepd1("Xmicrosoft-windows-sdk-root"),
@ -2727,7 +2743,14 @@ flagpd1("fcxx-modules"),
flagpd1("fc++-static-destructors"),
flagpd1("fd-lines-as-code"),
flagpd1("fd-lines-as-comments"),
flagpd1("fdata-sections"),
.{
.name = "fdata-sections",
.syntax = .flag,
.zig_equivalent = .data_sections,
.pd1 = true,
.pd2 = false,
.psl = false,
},
sepd1("fdebug-compilation-dir"),
flagpd1("fdebug-dump-all"),
flagpd1("fdebug-dump-parse-tree"),
@ -3130,7 +3153,14 @@ flagpd1("fno-cxx-modules"),
flagpd1("fno-c++-static-destructors"),
flagpd1("fno-d-lines-as-code"),
flagpd1("fno-d-lines-as-comments"),
flagpd1("fno-data-sections"),
.{
.name = "fno-data-sections",
.syntax = .flag,
.zig_equivalent = .no_data_sections,
.pd1 = true,
.pd2 = false,
.psl = false,
},
flagpd1("fno-debug-info-for-profiling"),
flagpd1("fno-debug-macro"),
flagpd1("fno-debug-pass-manager"),
@ -4080,7 +4110,7 @@ flagpd1("malign-double"),
m("maltivec"),
flagpd1("mamdgpu-ieee"),
m("mamx-bf16"),
flagpd1("mamx-complex"),
m("mamx-complex"),
m("mamx-fp16"),
m("mamx-int8"),
m("mamx-tile"),
@ -4109,7 +4139,7 @@ m("mavx512vpopcntdq"),
m("mavxifma"),
m("mavxneconvert"),
m("mavxvnni"),
flagpd1("mavxvnniint16"),
m("mavxvnniint16"),
m("mavxvnniint8"),
flagpd1("mbackchain"),
flagpd1("mbig-endian"),
@ -4264,7 +4294,7 @@ m("mno-aes"),
m("mno-altivec"),
flagpd1("mno-amdgpu-ieee"),
m("mno-amx-bf16"),
flagpd1("mno-amx-complex"),
m("mno-amx-complex"),
m("mno-amx-fp16"),
m("mno-amx-int8"),
m("mno-amx-tile"),
@ -4290,7 +4320,7 @@ m("mno-avx512vpopcntdq"),
m("mno-avxifma"),
m("mno-avxneconvert"),
m("mno-avxvnni"),
flagpd1("mno-avxvnniint16"),
m("mno-avxvnniint16"),
m("mno-avxvnniint8"),
flagpd1("mno-backchain"),
m("mno-bmi"),
@ -4336,6 +4366,7 @@ m("mno-fpu"),
m("mno-fsgsbase"),
m("mno-fsmuld"),
m("mno-fxsr"),
flagpd1("mno-gather"),
m("mno-gfni"),
m("mno-ginv"),
flagpd1("mno-global-merge"),
@ -4433,16 +4464,17 @@ flagpd1("mno-rtd"),
m("mno-rtm"),
m("mno-sahf"),
m("mno-save-restore"),
flagpd1("mno-scatter"),
m("mno-serialize"),
m("mno-seses"),
m("mno-sgx"),
m("mno-sha"),
flagpd1("mno-sha512"),
m("mno-sha512"),
m("mno-shstk"),
m("mno-sign-ext"),
m("mno-simd128"),
flagpd1("mno-skip-rax-setup"),
flagpd1("mno-sm3"),
m("mno-sm3"),
m("mno-sm4"),
m("mno-soft-float"),
m("mno-spe"),
@ -4568,14 +4600,14 @@ m("msecure-plt"),
m("mserialize"),
m("msgx"),
m("msha"),
flagpd1("msha512"),
m("msha512"),
m("mshstk"),
m("msign-ext"),
flagpd1("msim"),
m("msimd128"),
m("msingle-float"),
flagpd1("mskip-rax-setup"),
flagpd1("msm3"),
m("msm3"),
m("msm4"),
sepd1("msmall-data-limit"),
m("msoft-float"),

View file

@ -955,6 +955,7 @@ pub const Object = struct {
reloc_mode,
code_model,
options.function_sections,
options.data_sections,
float_abi,
if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,
);

View file

@ -919,6 +919,7 @@ pub const TargetMachine = opaque {
Reloc: RelocMode,
CodeModel: CodeModel,
function_sections: bool,
data_sections: bool,
float_abi: ABIType,
abi_name: ?[*:0]const u8,
) *TargetMachine;

View file

@ -139,6 +139,7 @@ pub const Options = struct {
link_libunwind: bool,
darwin_sdk_layout: ?DarwinSdkLayout,
function_sections: bool,
data_sections: bool,
no_builtin: bool,
eh_frame_hdr: bool,
emit_relocs: bool,

View file

@ -453,6 +453,8 @@ const usage_build_generic =
\\ -fno-builtin Disable implicit builtin knowledge of functions
\\ -ffunction-sections Places each function in a separate section
\\ -fno-function-sections All functions go into same section
\\ -fdata-sections Places each data in a separate section
\\ -fno-data-sections All data go into same section
\\ -fstrip Omit debug symbols
\\ -fno-strip Keep debug symbols
\\ -fformatted-panics Enable formatted safety panics
@ -793,6 +795,7 @@ fn buildOutputType(
var strip: ?bool = null;
var formatted_panics: ?bool = null;
var function_sections = false;
var data_sections = false;
var no_builtin = false;
var listen: Listen = .none;
var debug_compile_errors = false;
@ -1459,6 +1462,10 @@ fn buildOutputType(
function_sections = true;
} else if (mem.eql(u8, arg, "-fno-function-sections")) {
function_sections = false;
} else if (mem.eql(u8, arg, "-fdata-sections")) {
data_sections = true;
} else if (mem.eql(u8, arg, "-fno-data-sections")) {
data_sections = false;
} else if (mem.eql(u8, arg, "-fbuiltin")) {
no_builtin = false;
} else if (mem.eql(u8, arg, "-fno-builtin")) {
@ -1780,6 +1787,8 @@ fn buildOutputType(
.no_omit_frame_pointer => omit_frame_pointer = false,
.function_sections => function_sections = true,
.no_function_sections => function_sections = false,
.data_sections => data_sections = true,
.no_data_sections => data_sections = false,
.builtin => no_builtin = false,
.no_builtin => no_builtin = true,
.color_diagnostics => color = .on,
@ -3475,6 +3484,7 @@ fn buildOutputType(
.formatted_panics = formatted_panics,
.single_threaded = single_threaded,
.function_sections = function_sections,
.data_sections = data_sections,
.no_builtin = no_builtin,
.self_exe_path = self_exe_path,
.thread_pool = &thread_pool,
@ -5589,6 +5599,8 @@ pub const ClangArgIterator = struct {
no_omit_frame_pointer,
function_sections,
no_function_sections,
data_sections,
no_data_sections,
builtin,
no_builtin,
color_diagnostics,

View file

@ -82,7 +82,8 @@ static const bool assertions_on = false;
LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
LLVMCodeModel CodeModel, bool function_sections, ZigLLVMABIType float_abi, const char *abi_name)
LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
const char *abi_name)
{
std::optional<Reloc::Model> RM;
switch (Reloc){
@ -130,6 +131,7 @@ LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Tri
TargetOptions opt;
opt.FunctionSections = function_sections;
opt.DataSections = data_sections;
switch (float_abi) {
case ZigLLVMABITypeDefault:
opt.FloatABIType = FloatABI::Default;

View file

@ -57,7 +57,8 @@ enum ZigLLVMABIType {
ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
LLVMCodeModel CodeModel, bool function_sections, enum ZigLLVMABIType float_abi, const char *abi_name);
LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
const char *abi_name);
ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);

View file

@ -332,6 +332,14 @@ const known_options = [_]KnownOpt{
.name = "fno-function-sections",
.ident = "no_function_sections",
},
.{
.name = "fdata-sections",
.ident = "data_sections",
},
.{
.name = "fno-data-sections",
.ident = "no_data_sections",
},
.{
.name = "fbuiltin",
.ident = "builtin",