From 44449730c731a50dcdaaef04dff2755418f2546c Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Wed, 12 Aug 2026 12:17:54 +0100 Subject: [PATCH] Elf2: fix crashes when emitting dynamic libraries --- src/link/Elf2.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/link/Elf2.zig b/src/link/Elf2.zig index 761d185d3fc36656adbbdbaa60858726b9116c3d..07a224b79904ae8390b768e23ec0f2cacb917e6d 100644 --- a/src/link/Elf2.zig +++ b/src/link/Elf2.zig @@ -2270,8 +2270,9 @@ fn setGlobalSymbolValue( } // If this symbol was previously undefined, relocations targeting it may have been lowered to - // runtime relocations which we have now discovered we do not need, so delete those. - if (elf.shndx.dynamic != .UNDEF) { + // runtime relocations which we have now discovered we do not need, so delete those. This does + // not apply if the symbol is preemptible, which we check with `classifySymbolValue`. + if (elf.shndx.dynamic != .UNDEF and elf.classifySymbolValue(.global(global_name)) != .dynamic) { Symbol.Id.global(global_name).deleteDynamicTargetRelocs(elf); } @@ -6925,6 +6926,9 @@ fn nodeWantsDsoRelocation(elf: *Elf, node: MappedFile.Node.Index) enum { yes, ye fn maybeAddCopyRelocation(elf: *Elf, global_name: String(.strtab)) Error!bool { assert(elf.shndx.dynamic != .UNDEF); + // Only dynamic executables may contain `R_*_COPY` relocations. + if (elf.base.comp.config.output_mode != .Exe) return false; + const gpa = elf.base.comp.gpa; const global_ptr = elf.globals.strong_undef.getPtr(global_name) orelse @@ -6932,10 +6936,6 @@ fn maybeAddCopyRelocation(elf: *Elf, global_name: String(.strtab)) Error!bool { assert(global_ptr.dynsym_index != 0); - // Only dynamic executables may contain `R_*_COPY` relocations. - if (elf.shndx.dynamic == .UNDEF) return false; - if (elf.base.comp.config.output_mode != .Exe) return false; - const dso_global = elf.dso_globals.get(global_name) orelse { // We do not have a definition to provide the correct size for the symbol. If a definition // is discovered in a later DSO, we may at that point be able to add a copy relocation. -- 2.54.0