From a19fa18cc5cc28a2371f25ea900cb9e3dbb47782 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 04:05:55 +0000 Subject: [PATCH] Linux --emit=exe: read the default library's facts from the archive its ld script names The v0.0-pre-alpha111 release run failed on every Linux job: DefaultLib's tests under -mm=own, compile pass only (release and debug 135/182; the JIT and Windows passed), with errors inside lib.generics.ts - "'this value' borrows a field but is used here after it may be released or overwritten", "'this value' borrows 'this._entries' ...". On Linux libTypeScriptDefaultLib.a is a GNU ld script, `INPUT(-lTypeScriptDefaultLibCore -lcurl)` (the default library's build.sh). readOwnFacts and readBorrowFacts read the facts of the static library an --emit=exe program links from that file: not an archive, so none. Under own every call into the library could then destroy anything. The JIT reads the shared library, which is why it passed. Windows reproduces the errors with the own facts left out. staticDefaultLibFactsFile: for a file that is not a link input, the archive of the first `-l` its INPUT(...) names that sits beside it; anything else is itself. The link is unchanged - the script, its folder searched (exe.cpp) - so the archive read is the archive linked. Co-Authored-By: Claude Opus 5.5 --- tslang/tslang/borrowfacts.cpp | 52 +++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/tslang/tslang/borrowfacts.cpp b/tslang/tslang/borrowfacts.cpp index b762cc220..4239ee7b4 100644 --- a/tslang/tslang/borrowfacts.cpp +++ b/tslang/tslang/borrowfacts.cpp @@ -8,6 +8,7 @@ #include "llvm/BinaryFormat/Magic.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/TargetParser/Host.h" #include "llvm/TargetParser/Triple.h" @@ -54,6 +55,53 @@ static bool isStaticLinkInput(llvm::StringRef file) magic == llvm::file_magic::elf_relocatable; } +// The default library's static library, as its facts are read: on Linux libTypeScriptDefaultLib.a is +// a GNU ld script, `INPUT(-lTypeScriptDefaultLibCore -lcurl)` (the default library's build.sh), and +// the code it links is the archive of the first `-l` it names that sits beside it. Read as the script, +// it published nothing: an own program built with --emit=exe got no facts of the library, and every +// call into it might destroy anything. Any other file is itself. +static std::string staticDefaultLibFactsFile(const std::string &file) +{ + if (!llvm::sys::fs::is_regular_file(file) || isStaticLinkInput(file)) + { + return file; + } + + auto buffer = llvm::MemoryBuffer::getFile(file); + if (!buffer || (*buffer)->getBufferSize() > 4096) + { + return file; + } + + auto text = (*buffer)->getBuffer(); + auto input = text.find("INPUT("); + if (input == llvm::StringRef::npos) + { + return file; + } + + auto list = text.substr(input + 6); + list = list.take_until([](char c) { return c == ')'; }); + llvm::SmallVector items; + list.split(items, ' ', -1, /*KeepEmpty=*/false); + for (auto item : items) + { + if (!item.consume_front("-l")) + { + continue; + } + + llvm::SmallString<256> archive(llvm::sys::path::parent_path(file)); + llvm::sys::path::append(archive, "lib" + item + ".a"); + if (llvm::sys::fs::is_regular_file(archive) && isStaticLinkInput(archive)) + { + return archive.str().str(); + } + } + + return file; +} + // The static library a `--lib` value names, when this compile can tell: the value itself if it is // one, else the target's static library of that name (`.lib`, `lib.a`) in the current // directory. Empty otherwise - the linker searches for it, or it is linked dynamically, and then no @@ -153,7 +201,7 @@ void readBorrowFacts(CompileOptions &compileOptions) std::vector files; if (!compileOptions.noDefaultLib) { - files.push_back(getDefaultLibBinary(/*shared=*/false, compileOptions, getLinkTargetTriple())); + files.push_back(staticDefaultLibFactsFile(getDefaultLibBinary(/*shared=*/false, compileOptions, getLinkTargetTriple()))); } for (auto &lib : libs) @@ -200,7 +248,7 @@ void readOwnFacts(CompileOptions &compileOptions) } else { - files.push_back(getDefaultLibBinary(/*shared=*/false, compileOptions, getLinkTargetTriple())); + files.push_back(staticDefaultLibFactsFile(getDefaultLibBinary(/*shared=*/false, compileOptions, getLinkTargetTriple()))); } }