authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-15 19:25:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
log638db680f4c6380bb193da520f29a7c587bfb719
treec1b813f52c7970b061e928e8c7b9e0d859a82ded
parentb54ad9317591873159594e673953088a21d66e7b

move dll_export_fns and rdynamic to Compilation.Config


8 files changed, 44 insertions(+), 46 deletions(-)

src/Compilation.zig+3-10
......@@ -956,7 +956,6 @@ pub const InitOptions = struct {
956956 emit_docs: ?EmitLoc = null,
957957 /// `null` means to not emit an import lib.
958958 emit_implib: ?EmitLoc = null,
959 dll_export_fns: ?bool = false,
960959 /// Normally when using LLD to link, Zig uses a file named "lld.id" in the
961960 /// same directory as the output binary which contains the hash of the link
962961 /// operation, allowing Zig to skip linking when the hash would be unchanged.
......@@ -989,7 +988,6 @@ pub const InitOptions = struct {
989988 want_compiler_rt: ?bool = null,
990989 want_lto: ?bool = null,
991990 formatted_panics: ?bool = null,
992 rdynamic: bool = false,
993991 function_sections: bool = false,
994992 data_sections: bool = false,
995993 no_builtin: bool = false,
......@@ -1200,8 +1198,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
12001198
12011199 const link_libc = options.config.link_libc;
12021200
1203 const dll_export_fns = options.dll_export_fns orelse (is_dyn_lib or options.rdynamic);
1204
12051201 const libc_dirs = try detectLibCIncludeDirs(
12061202 arena,
12071203 options.zig_lib_directory.path.?,
......@@ -1524,10 +1520,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15241520 .gc_sections = options.linker_gc_sections,
15251521 .eh_frame_hdr = link_eh_frame_hdr,
15261522 .emit_relocs = options.link_emit_relocs,
1527 .rdynamic = options.rdynamic,
15281523 .soname = options.soname,
15291524 .compatibility_version = options.compatibility_version,
1530 .dll_export_fns = dll_export_fns,
15311525 .each_lib_rpath = each_lib_rpath,
15321526 .build_id = build_id,
15331527 .disable_lld_caching = options.disable_lld_caching or cache_mode == .whole,
......@@ -1555,7 +1549,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15551549 // Synchronize with other matching comments: ZigOnlyHashStuff
15561550 hash.add(use_llvm);
15571551 hash.add(options.config.use_lib_llvm);
1558 hash.add(dll_export_fns);
1552 hash.add(options.config.dll_export_fns);
15591553 hash.add(options.config.is_test);
15601554 hash.add(options.config.test_evented_io);
15611555 hash.addOptionalBytes(options.test_filter);
......@@ -2430,6 +2424,8 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24302424 man.hash.add(comp.config.import_memory);
24312425 man.hash.add(comp.config.export_memory);
24322426 man.hash.add(comp.config.shared_memory);
2427 man.hash.add(comp.config.dll_export_fns);
2428 man.hash.add(comp.config.rdynamic);
24332429
24342430 if (comp.bin_file) |lf| {
24352431 man.hash.add(lf.stack_size);
......@@ -2442,7 +2438,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24422438 switch (lf.tag) {
24432439 .elf => {
24442440 const elf = lf.cast(link.File.Elf).?;
2445 man.hash.add(elf.rdynamic);
24462441 man.hash.add(elf.eh_frame_hdr);
24472442 man.hash.add(elf.image_base);
24482443 man.hash.add(elf.emit_relocs);
......@@ -2468,7 +2463,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24682463 },
24692464 .wasm => {
24702465 const wasm = lf.cast(link.File.Wasm).?;
2471 man.hash.add(wasm.rdynamic);
24722466 man.hash.addOptional(wasm.initial_memory);
24732467 man.hash.addOptional(wasm.max_memory);
24742468 man.hash.addOptional(wasm.global_base);
......@@ -2485,7 +2479,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24852479 },
24862480 .coff => {
24872481 const coff = lf.cast(link.File.Coff).?;
2488 man.hash.add(coff.dll_export_fns);
24892482 man.hash.add(coff.image_base);
24902483 man.hash.addOptional(coff.subsystem);
24912484 man.hash.add(coff.tsaware);
src/Compilation/Config.zig+17
......@@ -41,6 +41,8 @@ entry: ?[]const u8,
4141debug_format: DebugFormat,
4242root_strip: bool,
4343root_error_tracing: bool,
44dll_export_fns: bool,
45rdynamic: bool,
4446
4547pub const CFrontend = enum { clang, aro };
4648
......@@ -93,6 +95,8 @@ pub const Options = struct {
9395 shared_memory: ?bool = null,
9496 test_evented_io: bool = false,
9597 debug_format: ?Config.DebugFormat = null,
98 dll_export_fns: ?bool = null,
99 rdynamic: ?bool = null,
96100};
97101
98102pub fn resolve(options: Options) !Config {
......@@ -415,6 +419,17 @@ pub fn resolve(options: Options) !Config {
415419
416420 const any_error_tracing = root_error_tracing or options.any_error_tracing;
417421
422 const rdynamic = options.rdynamic orelse false;
423
424 const dll_export_fns = b: {
425 if (options.dll_export_fns) |x| break :b x;
426 if (rdynamic) break :b true;
427 break :b switch (options.output_mode) {
428 .Obj, .Exe => false,
429 .Lib => link_mode == .Dynamic,
430 };
431 };
432
418433 return .{
419434 .output_mode = options.output_mode,
420435 .have_zcu = options.have_zcu,
......@@ -443,6 +458,8 @@ pub fn resolve(options: Options) !Config {
443458 .wasi_exec_model = wasi_exec_model,
444459 .debug_format = debug_format,
445460 .root_strip = root_strip,
461 .dll_export_fns = dll_export_fns,
462 .rdynamic = rdynamic,
446463 };
447464}
448465
src/codegen/llvm.zig+8-9
......@@ -1672,6 +1672,7 @@ pub const Object = struct {
16721672 // because we call `updateExports` at the end of `updateFunc` and `updateDecl`.
16731673 const global_index = self.decl_map.get(decl_index) orelse return;
16741674 const decl = mod.declPtr(decl_index);
1675 const comp = mod.comp;
16751676 if (decl.isExtern(mod)) {
16761677 const decl_name = decl_name: {
16771678 const decl_name = mod.intern_pool.stringToSlice(decl.name);
......@@ -1696,7 +1697,8 @@ pub const Object = struct {
16961697 try global_index.rename(decl_name, &self.builder);
16971698 global_index.setLinkage(.external, &self.builder);
16981699 global_index.setUnnamedAddr(.default, &self.builder);
1699 if (wantDllExports(mod)) global_index.setDllStorageClass(.default, &self.builder);
1700 if (comp.config.dll_export_fns)
1701 global_index.setDllStorageClass(.default, &self.builder);
17001702 if (self.di_map.get(decl)) |di_node| {
17011703 const decl_name_slice = decl_name.slice(&self.builder).?;
17021704 if (try decl.isFunction(mod)) {
......@@ -1762,7 +1764,8 @@ pub const Object = struct {
17621764 );
17631765 try global_index.rename(fqn, &self.builder);
17641766 global_index.setLinkage(.internal, &self.builder);
1765 if (wantDllExports(mod)) global_index.setDllStorageClass(.default, &self.builder);
1767 if (comp.config.dll_export_fns)
1768 global_index.setDllStorageClass(.default, &self.builder);
17661769 global_index.setUnnamedAddr(.unnamed_addr, &self.builder);
17671770 if (decl.val.getVariable(mod)) |decl_var| {
17681771 const decl_namespace = mod.namespacePtr(decl.src_namespace);
......@@ -1821,7 +1824,9 @@ pub const Object = struct {
18211824 exports: []const *Module.Export,
18221825 ) link.File.UpdateExportsError!void {
18231826 global_index.setUnnamedAddr(.default, &o.builder);
1824 if (wantDllExports(mod)) global_index.setDllStorageClass(.dllexport, &o.builder);
1827 const comp = mod.comp;
1828 if (comp.config.dll_export_fns)
1829 global_index.setDllStorageClass(.dllexport, &o.builder);
18251830 global_index.setLinkage(switch (exports[0].opts.linkage) {
18261831 .Internal => unreachable,
18271832 .Strong => .external,
......@@ -11758,9 +11763,3 @@ fn constraintAllowsRegister(constraint: []const u8) bool {
1175811763 }
1175911764 } else return false;
1176011765}
11761
11762fn wantDllExports(zcu: *const Zcu) bool {
11763 const lf = zcu.bin_file.?;
11764 const coff = lf.cast(link.File.Coff) orelse return false;
11765 return coff.dll_export_fns;
11766}
src/link.zig-2
......@@ -88,7 +88,6 @@ pub const File = struct {
8888 image_base: ?u64,
8989 eh_frame_hdr: bool,
9090 emit_relocs: bool,
91 rdynamic: bool,
9291 z_nodelete: bool,
9392 z_notext: bool,
9493 z_defs: bool,
......@@ -110,7 +109,6 @@ pub const File = struct {
110109 max_memory: ?u64,
111110 export_symbol_names: []const []const u8,
112111 global_base: ?u64,
113 dll_export_fns: bool,
114112 each_lib_rpath: bool,
115113 build_id: std.zig.BuildId,
116114 disable_lld_caching: bool,
src/link/Coff.zig-2
......@@ -8,7 +8,6 @@ llvm_object: ?*LlvmObject = null,
88
99base: link.File,
1010image_base: u64,
11dll_export_fns: bool,
1211subsystem: ?std.Target.SubSystem,
1312tsaware: bool,
1413nxcompat: bool,
......@@ -416,7 +415,6 @@ pub fn createEmpty(
416415 .Obj => 0,
417416 },
418417
419 .dll_export_fns = options.dll_export_fns,
420418 .subsystem = options.subsystem,
421419 .tsaware = options.tsaware,
422420 .nxcompat = options.nxcompat,
src/link/Elf.zig+4-6
......@@ -1,6 +1,5 @@
11base: link.File,
22image_base: u64,
3rdynamic: bool,
43eh_frame_hdr: bool,
54emit_relocs: bool,
65z_nodelete: bool,
......@@ -370,7 +369,7 @@ pub fn createEmpty(
370369 else => 0x1000,
371370 };
372371 const is_dyn_lib = output_mode == .Lib and link_mode == .Dynamic;
373 const default_sym_version: elf.Elf64_Versym = if (is_dyn_lib or options.rdynamic)
372 const default_sym_version: elf.Elf64_Versym = if (is_dyn_lib or comp.config.rdynamic)
374373 elf.VER_NDX_GLOBAL
375374 else
376375 elf.VER_NDX_LOCAL;
......@@ -402,7 +401,6 @@ pub fn createEmpty(
402401 };
403402 },
404403
405 .rdynamic = options.rdynamic,
406404 .eh_frame_hdr = options.eh_frame_hdr,
407405 .emit_relocs = options.emit_relocs,
408406 .z_nodelete = options.z_nodelete,
......@@ -1725,7 +1723,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
17251723 try argv.append("--eh-frame-hdr");
17261724 }
17271725
1728 if (self.rdynamic) {
1726 if (comp.config.rdynamic) {
17291727 try argv.append("--export-dynamic");
17301728 }
17311729
......@@ -2434,7 +2432,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
24342432 man.hash.addOptional(self.sort_section);
24352433 man.hash.add(self.eh_frame_hdr);
24362434 man.hash.add(self.emit_relocs);
2437 man.hash.add(self.rdynamic);
2435 man.hash.add(comp.config.rdynamic);
24382436 man.hash.addListOfBytes(self.lib_dirs);
24392437 man.hash.addListOfBytes(self.base.rpath_list);
24402438 man.hash.add(self.each_lib_rpath);
......@@ -2637,7 +2635,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
26372635 try argv.append("--emit-relocs");
26382636 }
26392637
2640 if (self.rdynamic) {
2638 if (comp.config.rdynamic) {
26412639 try argv.append("--export-dynamic");
26422640 }
26432641
src/link/Wasm.zig+6-7
......@@ -39,7 +39,6 @@ pub const base_tag: link.File.Tag = .wasm;
3939base: link.File,
4040import_symbols: bool,
4141export_symbol_names: []const []const u8,
42rdynamic: bool,
4342global_base: ?u64,
4443initial_memory: ?u64,
4544max_memory: ?u64,
......@@ -563,7 +562,6 @@ pub fn createEmpty(
563562 .export_table = options.export_table,
564563 .import_symbols = options.import_symbols,
565564 .export_symbol_names = options.export_symbol_names,
566 .rdynamic = options.rdynamic,
567565 .global_base = options.global_base,
568566 .initial_memory = options.initial_memory,
569567 .max_memory = options.max_memory,
......@@ -2973,8 +2971,9 @@ fn mergeTypes(wasm: *Wasm) !void {
29732971}
29742972
29752973fn setupExports(wasm: *Wasm) !void {
2976 const gpa = wasm.base.comp.gpa;
2977 if (wasm.base.comp.config.output_mode == .Obj) return;
2974 const comp = wasm.base.comp;
2975 const gpa = comp.gpa;
2976 if (comp.config.output_mode == .Obj) return;
29782977 log.debug("Building exports from symbols", .{});
29792978
29802979 const force_exp_names = wasm.export_symbol_names;
......@@ -2999,7 +2998,7 @@ fn setupExports(wasm: *Wasm) !void {
29992998
30002999 for (wasm.resolved_symbols.keys()) |sym_loc| {
30013000 const symbol = sym_loc.getSymbol(wasm);
3002 if (!symbol.isExported(wasm.rdynamic)) continue;
3001 if (!symbol.isExported(comp.config.rdynamic)) continue;
30033002
30043003 const sym_name = sym_loc.getName(wasm);
30053004 const export_name = if (wasm.export_names.get(sym_loc)) |name| name else blk: {
......@@ -4789,7 +4788,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
47894788 try argv.append(arg);
47904789 }
47914790
4792 if (wasm.rdynamic) {
4791 if (comp.config.rdynamic) {
47934792 try argv.append("--export-dynamic");
47944793 }
47954794
......@@ -5288,7 +5287,7 @@ fn markReferences(wasm: *Wasm) !void {
52885287
52895288 for (wasm.resolved_symbols.keys()) |sym_loc| {
52905289 const sym = sym_loc.getSymbol(wasm);
5291 if (sym.isExported(wasm.rdynamic) or sym.isNoStrip() or !do_garbage_collect) {
5290 if (sym.isExported(comp.config.rdynamic) or sym.isNoStrip() or !do_garbage_collect) {
52925291 try wasm.mark(sym_loc);
52935292 continue;
52945293 }
src/main.zig+6-10
......@@ -786,7 +786,6 @@ fn buildOutputType(
786786 arg_mode: ArgMode,
787787) !void {
788788 var provided_name: ?[]const u8 = null;
789 var dll_export_fns: ?bool = null;
790789 var root_src_file: ?[]const u8 = null;
791790 var version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 };
792791 var have_version = false;
......@@ -824,7 +823,6 @@ fn buildOutputType(
824823 var soname: SOName = undefined;
825824 var want_native_include_dirs = false;
826825 var want_compiler_rt: ?bool = null;
827 var rdynamic: bool = false;
828826 var linker_script: ?[]const u8 = null;
829827 var version_script: ?[]const u8 = null;
830828 var disable_c_depfile = false;
......@@ -1411,7 +1409,7 @@ fn buildOutputType(
14111409 } else if (mem.eql(u8, arg, "-fno-error-tracing")) {
14121410 mod_opts.error_tracing = false;
14131411 } else if (mem.eql(u8, arg, "-rdynamic")) {
1414 rdynamic = true;
1412 create_module.opts.rdynamic = true;
14151413 } else if (mem.eql(u8, arg, "-fsoname")) {
14161414 soname = .yes_default_value;
14171415 } else if (mem.startsWith(u8, arg, "-fsoname=")) {
......@@ -1472,9 +1470,9 @@ fn buildOutputType(
14721470 lib_preferred_mode = .Static;
14731471 lib_search_strategy = .no_fallback;
14741472 } else if (mem.eql(u8, arg, "-fdll-export-fns")) {
1475 dll_export_fns = true;
1473 create_module.opts.dll_export_fns = true;
14761474 } else if (mem.eql(u8, arg, "-fno-dll-export-fns")) {
1477 dll_export_fns = false;
1475 create_module.opts.dll_export_fns = false;
14781476 } else if (mem.eql(u8, arg, "--show-builtin")) {
14791477 show_builtin = true;
14801478 emit_bin = .no;
......@@ -1882,7 +1880,7 @@ fn buildOutputType(
18821880 create_module.opts.link_mode = .Dynamic;
18831881 is_shared_lib = true;
18841882 },
1885 .rdynamic => rdynamic = true,
1883 .rdynamic => create_module.opts.rdynamic = true,
18861884 .wl => {
18871885 var split_it = mem.splitScalar(u8, it.only_arg, ',');
18881886 while (split_it.next()) |linker_arg| {
......@@ -2136,7 +2134,7 @@ fn buildOutputType(
21362134 mem.eql(u8, arg, "--export-dynamic") or
21372135 mem.eql(u8, arg, "-export-dynamic"))
21382136 {
2139 rdynamic = true;
2137 create_module.opts.rdynamic = true;
21402138 } else if (mem.eql(u8, arg, "-version-script") or mem.eql(u8, arg, "--version-script")) {
21412139 version_script = linker_args_it.nextOrFatal();
21422140 } else if (mem.eql(u8, arg, "-O")) {
......@@ -2295,7 +2293,7 @@ fn buildOutputType(
22952293 } else if (mem.eql(u8, arg, "--high-entropy-va")) {
22962294 // This option does not do anything.
22972295 } else if (mem.eql(u8, arg, "--export-all-symbols")) {
2298 rdynamic = true;
2296 create_module.opts.rdynamic = true;
22992297 } else if (mem.eql(u8, arg, "--color-diagnostics") or
23002298 mem.eql(u8, arg, "--color-diagnostics=always"))
23012299 {
......@@ -3365,7 +3363,6 @@ fn buildOutputType(
33653363 .emit_llvm_bc = emit_llvm_bc_resolved.data,
33663364 .emit_docs = emit_docs_resolved.data,
33673365 .emit_implib = emit_implib_resolved.data,
3368 .dll_export_fns = dll_export_fns,
33693366 .lib_dirs = lib_dirs.items,
33703367 .rpath_list = rpath_list.items,
33713368 .symbol_wrap_set = symbol_wrap_set,
......@@ -3381,7 +3378,6 @@ fn buildOutputType(
33813378 .wasi_emulated_libs = create_module.wasi_emulated_libs.items,
33823379 .want_compiler_rt = want_compiler_rt,
33833380 .hash_style = hash_style,
3384 .rdynamic = rdynamic,
33853381 .linker_script = linker_script,
33863382 .version_script = version_script,
33873383 .disable_c_depfile = disable_c_depfile,