[PATCH] D142547: [LLD] cleans up context and symbol table to allow multiple invocations to lld::elf::linker()

Balint Joo via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 25 07:56:50 PST 2023


bjoo created this revision.
bjoo added reviewers: jhuber6, jdoerfert.
bjoo added a project: lld.
Herald added subscribers: kosarev, arichardson, emaste.
Herald added a reviewer: MaskRay.
Herald added a project: All.
bjoo requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Fixes: https://github.com/llvm/llvm-project/issues/59874

Initially after the first invocation of the linker() the symbol table is not cleared and this causes issues with the second invocation when linking a new shared library due to a conflict in the presence of a previously existing `_DYNAMIC` symbol tripping a runtime assertion.
The linker already had a cleanup lambda registered in an error handler. This patch just declares that lambda up front, and calls it before registering in the error handler. This way the linker can be called multiple times. This is necessary when JIT-ing e.g. AMDGCN kernels.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142547

Files:
  lld/ELF/Driver.cpp


Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -112,8 +112,7 @@
   // This driver-specific context will be freed later by lldMain().
   auto *ctx = new CommonLinkerContext;
 
-  ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
-  ctx->e.cleanupCallback = []() {
+  auto cleanup = []() {
     elf::ctx.reset();
     symtab = SymbolTable();
 
@@ -128,6 +127,13 @@
 
     SharedFile::vernauxNum = 0;
   };
+
+  // Clears leftovers from any previous invocation
+  cleanup();
+
+  ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
+  ctx->e.cleanupCallback = cleanup;
+
   ctx->e.logName = args::getFilenameWithoutExe(args[0]);
   ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
                                  "--error-limit=0 to see all errors)";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142547.492111.patch
Type: text/x-patch
Size: 902 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230125/ababf2b7/attachment.bin>


More information about the llvm-commits mailing list