diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2025-10-29 18:04:11 -0400 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2025-10-29 18:15:09 -0400 |
| commit | 0834e696f75d8477e5bc7a2dc49e7d10800039bc (patch) | |
| tree | 4bc63d8cf74e7fd9dfe7ab06bde9dd16bba62306 /src/Compilation/Config.zig | |
| parent | 40901440a620caf1849c627cff0d3a96eda273f5 (diff) | |
| download | zig-0834e696f75d8477e5bc7a2dc49e7d10800039bc.tar.gz zig-0834e696f75d8477e5bc7a2dc49e7d10800039bc.zip | |
Elf2: start implementing dynamic linking
Diffstat (limited to 'src/Compilation/Config.zig')
| -rw-r--r-- | src/Compilation/Config.zig | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index c2b9240480..2eca2c8411 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -123,6 +123,7 @@ pub const ResolveError = error{ WasiExecModelRequiresWasi, SharedMemoryIsWasmOnly, ObjectFilesCannotShareMemory, + ObjectFilesCannotSpecifyDynamicLinker, SharedMemoryRequiresAtomicsAndBulkMemory, ThreadsRequireSharedMemory, EmittingLlvmModuleRequiresLlvmBackend, @@ -131,6 +132,7 @@ pub const ResolveError = error{ EmittingBinaryRequiresLlvmLibrary, LldIncompatibleObjectFormat, LldCannotIncrementallyLink, + LldCannotSpecifyDynamicLinkerForSharedLibraries, LtoRequiresLld, SanitizeThreadRequiresLibCpp, LibCRequiresLibUnwind, @@ -142,6 +144,7 @@ pub const ResolveError = error{ TargetCannotStaticLinkExecutables, LibCRequiresDynamicLinking, SharedLibrariesRequireDynamicLinking, + DynamicLinkingWithLldRequiresSharedLibraries, ExportMemoryAndDynamicIncompatible, DynamicLibraryPrecludesPie, TargetRequiresPie, @@ -274,16 +277,11 @@ pub fn resolve(options: Options) ResolveError!Config { if (options.link_mode == .static) return error.LibCRequiresDynamicLinking; break :b .dynamic; } - // When creating a executable that links to system libraries, we - // require dynamic linking, but we must not link static libraries - // or object files dynamically! - if (options.any_dyn_libs and options.output_mode == .Exe) { - if (options.link_mode == .static) return error.SharedLibrariesRequireDynamicLinking; - break :b .dynamic; - } if (options.link_mode) |link_mode| break :b link_mode; + if (options.any_dyn_libs) break :b .dynamic; + if (explicitly_exe_or_dyn_lib and link_libc) { // When using the native glibc/musl ABI, dynamic linking is usually what people want. if (options.resolved_target.is_native_abi and (target.isGnuLibC() or target.isMuslLibC())) { @@ -425,6 +423,25 @@ pub fn resolve(options: Options) ResolveError!Config { break :b use_llvm; }; + switch (options.output_mode) { + .Exe => if (options.any_dyn_libs) { + // When creating a executable that links to system libraries, we + // require dynamic linking, but we must not link static libraries + // or object files dynamically! + if (link_mode == .static) return error.SharedLibrariesRequireDynamicLinking; + } else if (use_lld and !link_libc and !link_libcpp and !link_libunwind) { + // Lld does not support creating dynamic executables when not + // linking to any shared libraries. + if (link_mode == .dynamic) return error.DynamicLinkingWithLldRequiresSharedLibraries; + }, + .Lib => if (use_lld and options.resolved_target.is_explicit_dynamic_linker) { + return error.LldCannotSpecifyDynamicLinkerForSharedLibraries; + }, + .Obj => if (options.resolved_target.is_explicit_dynamic_linker) { + return error.ObjectFilesCannotSpecifyDynamicLinker; + }, + } + const use_new_linker = b: { if (use_lld) { if (options.use_new_linker == true) return error.NewLinkerIncompatibleWithLld; |
