authorgravatar for maci.stgn@gmail.comMarcel W. Wysocki <maci.stgn@gmail.com> 2026-02-15 23:47:07+08:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-08-16 08:41:27+02:00
loge25015bdce1e6ff4ab594a34fc7d1f0cd296dc25
treeac067a7b4b5f9ad2a08db914246a6db3fa39f152
parent767d2526918192da548d77970f97028dd62cf9d1
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

cc: add --wrap=, --nmagic, --fatal-warnings, and -m linker args

Needed for linking the Linux kernel.

4 files changed, 34 insertions(+), 1 deletions(-)

src/Compilation.zig+4
......@@ -1485,6 +1485,8 @@ pub const CreateOptions = struct {
14851485 linker_print_gc_sections: bool = false,
14861486 linker_print_icf_sections: bool = false,
14871487 linker_print_map: bool = false,
1488 linker_nmagic: bool = false,
1489 linker_fatal_warnings: bool = false,
14881490 llvm_opt_bisect_limit: i32 = -1,
14891491 build_id: ?std.zig.BuildId = null,
14901492 disable_c_depfile: bool = false,
......@@ -2188,6 +2190,8 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
21882190 .print_gc_sections = options.linker_print_gc_sections,
21892191 .print_icf_sections = options.linker_print_icf_sections,
21902192 .print_map = options.linker_print_map,
2193 .nmagic = options.linker_nmagic,
2194 .fatal_warnings = options.linker_fatal_warnings,
21912195 .tsaware = options.linker_tsaware,
21922196 .nxcompat = options.linker_nxcompat,
21932197 .dynamicbase = options.linker_dynamicbase,
src/link.zig+2
......@@ -466,6 +466,8 @@ pub const File = struct {
466466 print_gc_sections: bool,
467467 print_icf_sections: bool,
468468 print_map: bool,
469 nmagic: bool,
470 fatal_warnings: bool,
469471
470472 /// Use a wrapper function for symbol. Any undefined reference to symbol
471473 /// will be resolved to __wrap_symbol. Any undefined reference to
src/link/Lld.zig+12
......@@ -80,6 +80,8 @@ pub const Elf = struct {
8080 sort_section: ?SortSection,
8181 print_icf_sections: bool,
8282 print_map: bool,
83 nmagic: bool,
84 fatal_warnings: bool,
8385 emit_relocs: bool,
8486 z_nodelete: bool,
8587 z_notext: bool,
......@@ -135,6 +137,8 @@ pub const Elf = struct {
135137 .sort_section = options.sort_section,
136138 .print_icf_sections = options.print_icf_sections,
137139 .print_map = options.print_map,
140 .nmagic = options.nmagic,
141 .fatal_warnings = options.fatal_warnings,
138142 .emit_relocs = options.emit_relocs,
139143 .z_nodelete = options.z_nodelete,
140144 .z_notext = options.z_notext,
......@@ -947,6 +951,14 @@ fn elfLink(lld: *Lld, arena: Allocator) !void {
947951 try argv.append("--print-map");
948952 }
949953
954 if (elf.nmagic) {
955 try argv.append("--nmagic");
956 }
957
958 if (elf.fatal_warnings) {
959 try argv.append("--fatal-warnings");
960 }
961
950962 if (comp.link_eh_frame_hdr) {
951963 try argv.append("--eh-frame-hdr");
952964 }
src/main.zig+16-1
......@@ -991,6 +991,8 @@ fn buildOutputType(
991991 var linker_print_gc_sections: bool = false;
992992 var linker_print_icf_sections: bool = false;
993993 var linker_print_map: bool = false;
994 var linker_nmagic: bool = false;
995 var linker_fatal_warnings: bool = false;
994996 var llvm_opt_bisect_limit: c_int = -1;
995997 var linker_z_nocopyreloc = false;
996998 var linker_z_nodelete = false;
......@@ -2676,6 +2678,15 @@ fn buildOutputType(
26762678 linker_print_icf_sections = true;
26772679 } else if (mem.eql(u8, arg, "--print-map")) {
26782680 linker_print_map = true;
2681 } else if (mem.eql(u8, arg, "-n") or mem.eql(u8, arg, "--nmagic")) {
2682 linker_nmagic = true;
2683 } else if (mem.eql(u8, arg, "--fatal-warnings")) {
2684 linker_fatal_warnings = true;
2685 } else if (mem.eql(u8, arg, "--no-fatal-warnings")) {
2686 linker_fatal_warnings = false;
2687 } else if (mem.eql(u8, arg, "-m")) {
2688 _ = linker_args_it.nextOrFatal();
2689 warn("-m option is ignored; emulation is derived from target", .{});
26792690 } else if (mem.eql(u8, arg, "--sort-section")) {
26802691 const arg1 = linker_args_it.nextOrFatal();
26812692 linker_sort_section = stringToEnum(link.File.Lld.Elf.SortSection, arg1) orelse {
......@@ -2948,9 +2959,11 @@ fn buildOutputType(
29482959 hash_style = stringToEnum(link.File.Lld.Elf.HashStyle, next_arg) orelse {
29492960 fatal("expected [sysv|gnu|both] after --hash-style, found {q}", .{next_arg});
29502961 };
2951 } else if (mem.eql(u8, arg, "-wrap")) {
2962 } else if (mem.eql(u8, arg, "-wrap") or mem.eql(u8, arg, "--wrap")) {
29522963 const next_arg = linker_args_it.nextOrFatal();
29532964 try symbol_wrap_set.put(arena, next_arg, {});
2965 } else if (mem.cutPrefix(u8, arg, "--wrap=")) |symbol| {
2966 try symbol_wrap_set.put(arena, symbol, {});
29542967 } else if (mem.startsWith(u8, arg, "/subsystem:")) {
29552968 var split_it = mem.splitBackwardsScalar(u8, arg, ':');
29562969 subsystem = try parseSubsystem(split_it.first());
......@@ -3666,6 +3679,8 @@ fn buildOutputType(
36663679 .linker_print_gc_sections = linker_print_gc_sections,
36673680 .linker_print_icf_sections = linker_print_icf_sections,
36683681 .linker_print_map = linker_print_map,
3682 .linker_nmagic = linker_nmagic,
3683 .linker_fatal_warnings = linker_fatal_warnings,
36693684 .llvm_opt_bisect_limit = llvm_opt_bisect_limit,
36703685 .linker_global_base = linker_global_base,
36713686 .linker_export_symbol_names = linker_export_symbol_names.items,