| author | |
| committer | |
| log | b1b944a683a2143584055468ca7958d298a9742b |
| tree | b7e969d756016f064f684070aaa6a2a7570e6323 |
| parent | f911c933b287bd9f27b6b5f977a671aef05ca6c9 |
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/14 files changed, 24 insertions(+), 0 deletions(-)
src/Compilation.zig+3| ... | ... | @@ -989,6 +989,7 @@ pub const InitOptions = struct { |
| 989 | 989 | linker_optimization: ?u8 = null, |
| 990 | 990 | linker_compress_debug_sections: ?link.CompressDebugSections = null, |
| 991 | 991 | linker_module_definition_file: ?[]const u8 = null, |
| 992 | linker_sort_section: ?link.SortSection = null, | |
| 992 | 993 | major_subsystem_version: ?u32 = null, |
| 993 | 994 | minor_subsystem_version: ?u32 = null, |
| 994 | 995 | clang_passthrough_mode: bool = false, |
| ... | ... | @@ -1853,6 +1854,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1853 | 1854 | .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false, |
| 1854 | 1855 | .compress_debug_sections = options.linker_compress_debug_sections orelse .none, |
| 1855 | 1856 | .module_definition_file = options.linker_module_definition_file, |
| 1857 | .sort_section = options.linker_sort_section, | |
| 1856 | 1858 | .import_memory = options.linker_import_memory orelse false, |
| 1857 | 1859 | .import_symbols = options.linker_import_symbols, |
| 1858 | 1860 | .import_table = options.linker_import_table, |
| ... | ... | @@ -2684,6 +2686,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes |
| 2684 | 2686 | man.hash.add(comp.bin_file.options.hash_style); |
| 2685 | 2687 | man.hash.add(comp.bin_file.options.compress_debug_sections); |
| 2686 | 2688 | man.hash.add(comp.bin_file.options.include_compiler_rt); |
| 2689 | man.hash.addOptional(comp.bin_file.options.sort_section); | |
| 2687 | 2690 | if (comp.bin_file.options.link_libc) { |
| 2688 | 2691 | man.hash.add(comp.bin_file.options.libc_installation != null); |
| 2689 | 2692 | if (comp.bin_file.options.libc_installation) |libc_installation| { |
src/link.zig+3| ... | ... | @@ -25,6 +25,8 @@ pub const SystemLib = struct { |
| 25 | 25 | weak: bool = false, |
| 26 | 26 | }; |
| 27 | 27 | |
| 28 | pub const SortSection = enum { name, alignment }; | |
| 29 | ||
| 28 | 30 | pub const CacheMode = enum { incremental, whole }; |
| 29 | 31 | |
| 30 | 32 | pub fn hashAddSystemLibs( |
| ... | ... | @@ -159,6 +161,7 @@ pub const Options = struct { |
| 159 | 161 | disable_lld_caching: bool, |
| 160 | 162 | is_test: bool, |
| 161 | 163 | hash_style: HashStyle, |
| 164 | sort_section: ?SortSection, | |
| 162 | 165 | major_subsystem_version: ?u32, |
| 163 | 166 | minor_subsystem_version: ?u32, |
| 164 | 167 | gc_sections: ?bool = null, |
src/link/Elf.zig+6| ... | ... | @@ -1324,6 +1324,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v |
| 1324 | 1324 | man.hash.addOptionalBytes(self.base.options.entry); |
| 1325 | 1325 | man.hash.addOptional(self.base.options.image_base_override); |
| 1326 | 1326 | man.hash.add(gc_sections); |
| 1327 | man.hash.addOptional(self.base.options.sort_section); | |
| 1327 | 1328 | man.hash.add(self.base.options.eh_frame_hdr); |
| 1328 | 1329 | man.hash.add(self.base.options.emit_relocs); |
| 1329 | 1330 | man.hash.add(self.base.options.rdynamic); |
| ... | ... | @@ -1488,6 +1489,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v |
| 1488 | 1489 | try argv.append(linker_script); |
| 1489 | 1490 | } |
| 1490 | 1491 | |
| 1492 | if (self.base.options.sort_section) |how| { | |
| 1493 | const arg = try std.fmt.allocPrint(arena, "--sort-section={s}", .{@tagName(how)}); | |
| 1494 | try argv.append(arg); | |
| 1495 | } | |
| 1496 | ||
| 1491 | 1497 | if (gc_sections) { |
| 1492 | 1498 | try argv.append("--gc-sections"); |
| 1493 | 1499 | } |
src/main.zig+12| ... | ... | @@ -509,6 +509,7 @@ const usage_build_generic = |
| 509 | 509 | \\ zlib Compression with deflate/inflate |
| 510 | 510 | \\ --gc-sections Force removal of functions and data that are unreachable by the entry point or exported symbols |
| 511 | 511 | \\ --no-gc-sections Don't force removal of unreachable functions and data |
| 512 | \\ --sort-section=[value] Sort wildcard section patterns by 'name' or 'alignment' | |
| 512 | 513 | \\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker |
| 513 | 514 | \\ --stack [size] Override default stack size |
| 514 | 515 | \\ --image-base [addr] Set base address for executable image |
| ... | ... | @@ -731,6 +732,7 @@ fn buildOutputType( |
| 731 | 732 | var linker_script: ?[]const u8 = null; |
| 732 | 733 | var version_script: ?[]const u8 = null; |
| 733 | 734 | var disable_c_depfile = false; |
| 735 | var linker_sort_section: ?link.SortSection = null; | |
| 734 | 736 | var linker_gc_sections: ?bool = null; |
| 735 | 737 | var linker_compress_debug_sections: ?link.CompressDebugSections = null; |
| 736 | 738 | var linker_allow_shlib_undefined: ?bool = null; |
| ... | ... | @@ -1894,6 +1896,15 @@ fn buildOutputType( |
| 1894 | 1896 | linker_print_icf_sections = true; |
| 1895 | 1897 | } else if (mem.eql(u8, arg, "--print-map")) { |
| 1896 | 1898 | linker_print_map = true; |
| 1899 | } else if (mem.eql(u8, arg, "--sort-section")) { | |
| 1900 | i += 1; | |
| 1901 | if (i >= linker_args.items.len) { | |
| 1902 | fatal("expected linker arg after '{s}'", .{arg}); | |
| 1903 | } | |
| 1904 | const arg1 = linker_args.items[i]; | |
| 1905 | linker_sort_section = std.meta.stringToEnum(link.SortSection, arg1) orelse { | |
| 1906 | fatal("expected [name|alignment] after --sort-section, found '{s}'", .{arg1}); | |
| 1907 | }; | |
| 1897 | 1908 | } else if (mem.eql(u8, arg, "--allow-shlib-undefined") or |
| 1898 | 1909 | mem.eql(u8, arg, "-allow-shlib-undefined")) |
| 1899 | 1910 | { |
| ... | ... | @@ -3070,6 +3081,7 @@ fn buildOutputType( |
| 3070 | 3081 | .version_script = version_script, |
| 3071 | 3082 | .disable_c_depfile = disable_c_depfile, |
| 3072 | 3083 | .soname = resolved_soname, |
| 3084 | .linker_sort_section = linker_sort_section, | |
| 3073 | 3085 | .linker_gc_sections = linker_gc_sections, |
| 3074 | 3086 | .linker_allow_shlib_undefined = linker_allow_shlib_undefined, |
| 3075 | 3087 | .linker_bind_global_refs_locally = linker_bind_global_refs_locally, |