From 9cb52ca6ce7043ba0ce08d5650ac542075f10685 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 5 Feb 2023 19:39:04 -0700 Subject: move the cache system from compiler to std lib --- src/link.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/link.zig') diff --git a/src/link.zig b/src/link.zig index 2b3ce51667..5650e0679a 100644 --- a/src/link.zig +++ b/src/link.zig @@ -10,7 +10,7 @@ const wasi_libc = @import("wasi_libc.zig"); const Air = @import("Air.zig"); const Allocator = std.mem.Allocator; -const Cache = @import("Cache.zig"); +const Cache = std.Build.Cache; const Compilation = @import("Compilation.zig"); const LibCInstallation = @import("libc_installation.zig").LibCInstallation; const Liveness = @import("Liveness.zig"); -- cgit v1.2.3 From b1b944a683a2143584055468ca7958d298a9742b Mon Sep 17 00:00:00 2001 From: Motiejus Jakštys Date: Thu, 23 Jun 2022 08:28:35 +0300 Subject: [elf linker] add --sort-section Tested by: created a "hello world" C file and compiled with: zig cc -v main.c -Wl,--sort-section=name -o main ... and verified the `--sort-section=name` is passed to ld.lld. Refs https://github.com/zigchroot/zig-chroot/issues/1 --- src/Compilation.zig | 3 +++ src/link.zig | 3 +++ src/link/Elf.zig | 6 ++++++ src/main.zig | 12 ++++++++++++ 4 files changed, 24 insertions(+) (limited to 'src/link.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index c6737ed3eb..ce0bfb9908 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -989,6 +989,7 @@ pub const InitOptions = struct { linker_optimization: ?u8 = null, linker_compress_debug_sections: ?link.CompressDebugSections = null, linker_module_definition_file: ?[]const u8 = null, + linker_sort_section: ?link.SortSection = null, major_subsystem_version: ?u32 = null, minor_subsystem_version: ?u32 = null, clang_passthrough_mode: bool = false, @@ -1853,6 +1854,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false, .compress_debug_sections = options.linker_compress_debug_sections orelse .none, .module_definition_file = options.linker_module_definition_file, + .sort_section = options.linker_sort_section, .import_memory = options.linker_import_memory orelse false, .import_symbols = options.linker_import_symbols, .import_table = options.linker_import_table, @@ -2684,6 +2686,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes man.hash.add(comp.bin_file.options.hash_style); man.hash.add(comp.bin_file.options.compress_debug_sections); man.hash.add(comp.bin_file.options.include_compiler_rt); + man.hash.addOptional(comp.bin_file.options.sort_section); if (comp.bin_file.options.link_libc) { man.hash.add(comp.bin_file.options.libc_installation != null); if (comp.bin_file.options.libc_installation) |libc_installation| { diff --git a/src/link.zig b/src/link.zig index 5650e0679a..4c4915441d 100644 --- a/src/link.zig +++ b/src/link.zig @@ -25,6 +25,8 @@ pub const SystemLib = struct { weak: bool = false, }; +pub const SortSection = enum { name, alignment }; + pub const CacheMode = enum { incremental, whole }; pub fn hashAddSystemLibs( @@ -159,6 +161,7 @@ pub const Options = struct { disable_lld_caching: bool, is_test: bool, hash_style: HashStyle, + sort_section: ?SortSection, major_subsystem_version: ?u32, minor_subsystem_version: ?u32, gc_sections: ?bool = null, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 37ebfdc0dc..d936a347cf 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1324,6 +1324,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v man.hash.addOptionalBytes(self.base.options.entry); man.hash.addOptional(self.base.options.image_base_override); man.hash.add(gc_sections); + man.hash.addOptional(self.base.options.sort_section); man.hash.add(self.base.options.eh_frame_hdr); man.hash.add(self.base.options.emit_relocs); man.hash.add(self.base.options.rdynamic); @@ -1488,6 +1489,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v try argv.append(linker_script); } + if (self.base.options.sort_section) |how| { + const arg = try std.fmt.allocPrint(arena, "--sort-section={s}", .{@tagName(how)}); + try argv.append(arg); + } + if (gc_sections) { try argv.append("--gc-sections"); } diff --git a/src/main.zig b/src/main.zig index 2e83e98fff..07a8d8b9e7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -509,6 +509,7 @@ const usage_build_generic = \\ zlib Compression with deflate/inflate \\ --gc-sections Force removal of functions and data that are unreachable by the entry point or exported symbols \\ --no-gc-sections Don't force removal of unreachable functions and data + \\ --sort-section=[value] Sort wildcard section patterns by 'name' or 'alignment' \\ --subsystem [subsystem] (Windows) /SUBSYSTEM: to the linker \\ --stack [size] Override default stack size \\ --image-base [addr] Set base address for executable image @@ -731,6 +732,7 @@ fn buildOutputType( var linker_script: ?[]const u8 = null; var version_script: ?[]const u8 = null; var disable_c_depfile = false; + var linker_sort_section: ?link.SortSection = null; var linker_gc_sections: ?bool = null; var linker_compress_debug_sections: ?link.CompressDebugSections = null; var linker_allow_shlib_undefined: ?bool = null; @@ -1894,6 +1896,15 @@ fn buildOutputType( linker_print_icf_sections = true; } else if (mem.eql(u8, arg, "--print-map")) { linker_print_map = true; + } else if (mem.eql(u8, arg, "--sort-section")) { + i += 1; + if (i >= linker_args.items.len) { + fatal("expected linker arg after '{s}'", .{arg}); + } + const arg1 = linker_args.items[i]; + linker_sort_section = std.meta.stringToEnum(link.SortSection, arg1) orelse { + fatal("expected [name|alignment] after --sort-section, found '{s}'", .{arg1}); + }; } else if (mem.eql(u8, arg, "--allow-shlib-undefined") or mem.eql(u8, arg, "-allow-shlib-undefined")) { @@ -3070,6 +3081,7 @@ fn buildOutputType( .version_script = version_script, .disable_c_depfile = disable_c_depfile, .soname = resolved_soname, + .linker_sort_section = linker_sort_section, .linker_gc_sections = linker_gc_sections, .linker_allow_shlib_undefined = linker_allow_shlib_undefined, .linker_bind_global_refs_locally = linker_bind_global_refs_locally, -- cgit v1.2.3