[lld] [lld][ELF]Emit warning when both scripts are specified (PR #163497)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 14 22:05:25 PDT 2025


https://github.com/mingmingl-llvm updated https://github.com/llvm/llvm-project/pull/163497

>From b0f593ab438100e57457ea6615b6d9fa0f56f1d7 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Tue, 14 Oct 2025 22:00:54 -0700
Subject: [PATCH 1/2] [lld][ELF]Emit warning when both scripts are specified

---
 lld/ELF/Driver.cpp                         |  9 +++++++++
 lld/test/ELF/linkerscript/default-script.s | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 62f7fffce7dbe..c8cce0810ef9a 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2091,6 +2091,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
   nextGroupId = 0;
   isInGroup = false;
   bool hasInput = false, hasScript = false;
+  StringRef scriptFilePath, defaultScriptFilePath;
   for (auto *arg : args) {
     switch (arg->getOption().getID()) {
     case OPT_library:
@@ -2106,7 +2107,11 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
       break;
     }
     case OPT_script:
+      scriptFilePath = arg->getValue();
+      [[fallthrough]];
     case OPT_default_script:
+      if (arg->getOption().matches(OPT_default_script))
+        defaultScriptFilePath = arg->getValue();
       if (std::optional<std::string> path =
               searchScript(ctx, arg->getValue())) {
         if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
@@ -2201,6 +2206,10 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
 
   if (defaultScript && !hasScript)
     readLinkerScript(ctx, *defaultScript);
+  if (defaultScript && hasScript)
+    Warn(ctx) << "--script at path " << scriptFilePath
+              << " will override --default-script at path "
+              << defaultScriptFilePath;
   if (files.empty() && !hasInput && errCount(ctx) == 0)
     ErrAlways(ctx) << "no input files";
 }
diff --git a/lld/test/ELF/linkerscript/default-script.s b/lld/test/ELF/linkerscript/default-script.s
index bb716a5fe0cdd..a06f96b7165d4 100644
--- a/lld/test/ELF/linkerscript/default-script.s
+++ b/lld/test/ELF/linkerscript/default-script.s
@@ -32,6 +32,17 @@
 # CHECK1-NEXT: 3: 000000000000002a     0 NOTYPE  GLOBAL DEFAULT   ABS def
 # CHECK1-EMPTY:
 
+## When both --script and --default-script are specified, --script overrides
+## --default-script.
+# RUN: ld.lld --default-script def.t --script b.t a.o -o out2 2>warn.txt
+# RUN: llvm-readelf -Ss out2 | FileCheck %s --check-prefix=OVERRIDE
+# OVERRIDE:      .foo0
+# OVERRIDE-NEXT: foo1
+# OVERRIDE-NEXT: foo2
+# RUN: cat warn.txt | FileCheck %s --check-prefix=WARNING
+# WARNING: --script at path b.t will override --default-script at path def.t
+
+
 # RUN: not ld.lld --default-script not-exist.t b.t -T a.t a.o 2>&1 | FileCheck %s --check-prefix=ERR
 # ERR: error: cannot find linker script not-exist.t
 
@@ -61,3 +72,4 @@ SECTIONS {
   .foo1 : {}
   .foo0 : {}
 }
+

>From a96588d6ae75e660c770e54a8ac155cdd22a66e7 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Tue, 14 Oct 2025 22:05:05 -0700
Subject: [PATCH 2/2] trim blank lines

---
 lld/test/ELF/linkerscript/default-script.s | 2 --
 1 file changed, 2 deletions(-)

diff --git a/lld/test/ELF/linkerscript/default-script.s b/lld/test/ELF/linkerscript/default-script.s
index a06f96b7165d4..d1aae029de404 100644
--- a/lld/test/ELF/linkerscript/default-script.s
+++ b/lld/test/ELF/linkerscript/default-script.s
@@ -42,7 +42,6 @@
 # RUN: cat warn.txt | FileCheck %s --check-prefix=WARNING
 # WARNING: --script at path b.t will override --default-script at path def.t
 
-
 # RUN: not ld.lld --default-script not-exist.t b.t -T a.t a.o 2>&1 | FileCheck %s --check-prefix=ERR
 # ERR: error: cannot find linker script not-exist.t
 
@@ -72,4 +71,3 @@ SECTIONS {
   .foo1 : {}
   .foo0 : {}
 }
-



More information about the llvm-commits mailing list