[lld] [LLD][COFF] Handle --start-lib/--end-lib group in the same way as other archives (PR #136496)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Fri May 2 14:52:17 PDT 2025


================
@@ -2172,37 +2173,78 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
     return false;
   };
 
+  // Store start_lib/end_lib arguments in order to render dignostics in the
+  // same way the flags are written on the command line.
+  llvm::opt::Arg *startLibArg = nullptr;
+  llvm::opt::Arg *endLibArg = nullptr;
+  auto endLibSpelling = [&]() {
+    return endLibArg ? endLibArg->getSpelling()
+                     : lld::args::getOptionSpellingLikeArg(
+                           ctx.optTable, OPT_end_lib, startLibArg, ctx.saver);
+  };
+
   // Create a list of input files. These can be given as OPT_INPUT options
   // and OPT_wholearchive_file options, and we also need to track OPT_start_lib
   // and OPT_end_lib.
   {
     llvm::TimeTraceScope timeScope2("Parse & queue inputs");
-    bool inLib = false;
+    std::optional<std::shared_ptr<CmdLineArchive *>> inCmdLineArchive;
+    auto close = [&]() {
+      enqueueTask([=]() {
+        assert(inCmdLineArchive);
+        if (CmdLineArchive *a = **inCmdLineArchive)
+          a->maybeParse();
+      });
+    };
+
     for (auto *arg : args) {
       switch (arg->getOption().getID()) {
       case OPT_end_lib:
-        if (!inLib)
+        if (!inCmdLineArchive) {
           Err(ctx) << "stray " << arg->getSpelling();
-        inLib = false;
+        } else {
+          endLibArg = arg;
+          close();
+          inCmdLineArchive = std::nullopt;
+        }
         break;
       case OPT_start_lib:
-        if (inLib)
+        if (inCmdLineArchive) {
           Err(ctx) << "nested " << arg->getSpelling();
-        inLib = true;
+        } else {
+          startLibArg = arg;
+          inCmdLineArchive = std::make_shared<CmdLineArchive *>();
+          enqueueTask([&, inCmdLineArchive, startLibArg, endLibArg]() {
+            // In is important to create a fake archive here so that we
+            // remember its placement on the command-line. This will be
+            // later needed to resolve symbols in the archive order required
+            // by the MSVC specification.
+            **inCmdLineArchive = make<CmdLineArchive>(
----------------
aganea wrote:

The whole point of the PR is this line. We need, in a subsequent PR, to associate `ObjFile`s with `CmdLineArchive` and make it record an Index of where it is on the command line.

https://github.com/llvm/llvm-project/pull/136496


More information about the llvm-commits mailing list