[lld] [lld][ELF] Enable link script to support absolute path matching (PR #156340)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 1 08:24:09 PDT 2025


=?utf-8?b?5a+H5aqa5aqb?= <3174896597 at qq.com>,koumeiyuan
 <koumeiyuan at huawei.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/156340 at github.com>


https://github.com/mykouHW updated https://github.com/llvm/llvm-project/pull/156340

>From bfc421e0fefa96d83e53e82b2c42391407a224e0 Mon Sep 17 00:00:00 2001
From: koumeiyuan <koumeiyuan at huawei.com>
Date: Fri, 29 Aug 2025 09:07:10 +0000
Subject: [PATCH 1/3] [lld][ELF] Improve the vulnerability in Orphan Sections
 initialization

Fix the error generated during the linking process when the relocation section is placed before the relocated section and the relocated section is not defined in the linker script.
---
 lld/ELF/LinkerScript.cpp                      |  8 +++--
 .../ELF/linkerscript/orphan-sections-init.s   | 30 +++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 lld/test/ELF/linkerscript/orphan-sections-init.s

diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 921128dae2bdb..067abbc42a13d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1037,10 +1037,14 @@ void LinkerScript::addOrphanSections() {
     if (ctx.arg.relocatable && (isec->flags & SHF_LINK_ORDER))
       continue;
 
-    if (auto *sec = dyn_cast<InputSection>(isec))
-      if (InputSectionBase *rel = sec->getRelocatedSection())
+    if (auto *sec = dyn_cast<InputSection>(isec)){
+      if (InputSectionBase *rel = sec->getRelocatedSection()){
+        if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel))
+          add(relIS);
         if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
           add(relIS);
+      }
+    }
     add(isec);
     if (ctx.arg.relocatable)
       for (InputSectionBase *depSec : isec->dependentSections)
diff --git a/lld/test/ELF/linkerscript/orphan-sections-init.s b/lld/test/ELF/linkerscript/orphan-sections-init.s
new file mode 100644
index 0000000000000..1701336f098e2
--- /dev/null
+++ b/lld/test/ELF/linkerscript/orphan-sections-init.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
+
+# RUN: ld.lld -r  foo.o -T script.ld -o foo_mc.o
+
+# RUN: llvm-objcopy --rename-section .text=.com.text foo_mc.o foo_mc.o
+# RUN: llvm-objcopy --rename-section .rela.text=.rela.com.text foo_mc.o foo_mc.o
+
+# RUN: ld.lld -r foo_mc.o  -T script.ld -o foo_mc_after.o
+
+#--- foo.s
+  .text
+  .globl	foo
+  .p2align	4
+  .type	foo, at function
+foo:
+  mov $bar, %rax
+
+
+
+#--- script.ld
+SECTIONS
+{
+  .rela.text    0 : { *(.rela.text) }
+  .text         0 : { *(.text) }
+}
+

>From 3e2d4ce52a4816f5b0c48cf34acd98e190e44c04 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AF=87=E5=AA=9A=E5=AA=9B?= <3174896597 at qq.com>
Date: Mon, 1 Sep 2025 22:45:28 +0800
Subject: [PATCH 2/3] Revert "[lld][ELF] Improve the vulnerability in Orphan
 Sections initialization"

This reverts commit bfc421e0fefa96d83e53e82b2c42391407a224e0.
---
 lld/ELF/LinkerScript.cpp                      |  8 ++---
 .../ELF/linkerscript/orphan-sections-init.s   | 30 -------------------
 2 files changed, 2 insertions(+), 36 deletions(-)
 delete mode 100644 lld/test/ELF/linkerscript/orphan-sections-init.s

diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 067abbc42a13d..921128dae2bdb 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1037,14 +1037,10 @@ void LinkerScript::addOrphanSections() {
     if (ctx.arg.relocatable && (isec->flags & SHF_LINK_ORDER))
       continue;
 
-    if (auto *sec = dyn_cast<InputSection>(isec)){
-      if (InputSectionBase *rel = sec->getRelocatedSection()){
-        if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel))
-          add(relIS);
+    if (auto *sec = dyn_cast<InputSection>(isec))
+      if (InputSectionBase *rel = sec->getRelocatedSection())
         if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
           add(relIS);
-      }
-    }
     add(isec);
     if (ctx.arg.relocatable)
       for (InputSectionBase *depSec : isec->dependentSections)
diff --git a/lld/test/ELF/linkerscript/orphan-sections-init.s b/lld/test/ELF/linkerscript/orphan-sections-init.s
deleted file mode 100644
index 1701336f098e2..0000000000000
--- a/lld/test/ELF/linkerscript/orphan-sections-init.s
+++ /dev/null
@@ -1,30 +0,0 @@
-# REQUIRES: x86
-# RUN: rm -rf %t && mkdir -p %t
-# RUN: split-file %s %t && cd %t
-
-# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
-
-# RUN: ld.lld -r  foo.o -T script.ld -o foo_mc.o
-
-# RUN: llvm-objcopy --rename-section .text=.com.text foo_mc.o foo_mc.o
-# RUN: llvm-objcopy --rename-section .rela.text=.rela.com.text foo_mc.o foo_mc.o
-
-# RUN: ld.lld -r foo_mc.o  -T script.ld -o foo_mc_after.o
-
-#--- foo.s
-  .text
-  .globl	foo
-  .p2align	4
-  .type	foo, at function
-foo:
-  mov $bar, %rax
-
-
-
-#--- script.ld
-SECTIONS
-{
-  .rela.text    0 : { *(.rela.text) }
-  .text         0 : { *(.text) }
-}
-

>From dbc084f4869c6ca9cebbe239239086f396d381f9 Mon Sep 17 00:00:00 2001
From: koumeiyuan <koumeiyuan at huawei.com>
Date: Mon, 25 Aug 2025 17:34:40 +0000
Subject: [PATCH 3/3] [lld][ELF] Enable link script to support absolute path
 matching

Fixing the vulnerability in LLVM lld regarding file matching in linker scripts:
There is a compatibility issue with filename matching. When input files use absolute paths, the matching results from mc lld do not meet expectations.
---
 lld/ELF/LinkerScript.cpp                   | 31 +++++++++---
 lld/ELF/LinkerScript.h                     |  2 +-
 lld/test/ELF/linkerscript/abs-path-match.s | 56 ++++++++++++++++++++++
 3 files changed, 81 insertions(+), 8 deletions(-)
 create mode 100644 lld/test/ELF/linkerscript/abs-path-match.s

diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 921128dae2bdb..c325f18616fe3 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -411,7 +411,17 @@ void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
   cmd->sym->type = v.type;
 }
 
-bool InputSectionDescription::matchesFile(const InputFile &file) const {
+// Convert an absolute address to a filename
+static inline StringRef getExtractFilename(StringRef filename) {
+  size_t pos = filename.rfind("/");
+  if (pos != std::string::npos) {
+    return filename.substr(pos + 1);
+  }
+  return filename;
+}
+
+bool InputSectionDescription::matchesFile(const InputFile &file,
+                                          bool ExtractFlag) const {
   if (filePat.isTrivialMatchAll())
     return true;
 
@@ -419,10 +429,17 @@ bool InputSectionDescription::matchesFile(const InputFile &file) const {
     if (matchType == MatchType::WholeArchive) {
       matchesFileCache.emplace(&file, filePat.match(file.archiveName));
     } else {
-      if (matchType == MatchType::ArchivesExcluded && !file.archiveName.empty())
+      if (matchType == MatchType::ArchivesExcluded && !file.archiveName.empty()){
         matchesFileCache.emplace(&file, false);
-      else
-        matchesFileCache.emplace(&file, filePat.match(file.getNameForScript()));
+      } else {
+        bool MatchFilename = filePat.match(file.getNameForScript());
+        StringRef ExtractFilename = getExtractFilename(file.getNameForScript());
+        // only use for computeInputSections
+        if (ExtractFlag) {
+          MatchFilename = MatchFilename || filePat.match(ExtractFilename);
+        }
+        matchesFileCache.emplace(&file, MatchFilename);
+      }
     }
   }
 
@@ -442,7 +459,7 @@ bool SectionPattern::excludesFile(const InputFile &file) const {
 
 bool LinkerScript::shouldKeep(InputSectionBase *s) {
   for (InputSectionDescription *id : keptSections)
-    if (id->matchesFile(*s->file))
+    if (id->matchesFile(*s->file, false))
       for (SectionPattern &p : id->sectionPatterns)
         if (p.sectionPat.match(s->name) &&
             (s->flags & id->withFlags) == id->withFlags &&
@@ -571,8 +588,8 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
         if (!pat.sectionPat.match(sec->name))
           continue;
 
-        if (!cmd->matchesFile(*sec->file) || pat.excludesFile(*sec->file) ||
-            !flagsMatch(sec))
+        if (!cmd->matchesFile(*sec->file, true) ||
+            pat.excludesFile(*sec->file) || !flagsMatch(sec))
           continue;
 
         if (sec->parent) {
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 80c4f564afabc..452cfbcd9b777 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -227,7 +227,7 @@ class InputSectionDescription : public SectionCommand {
     return c->kind == InputSectionKind;
   }
 
-  bool matchesFile(const InputFile &file) const;
+  bool matchesFile(const InputFile &file, bool ExtractFilename) const;
 
   // Input sections that matches at least one of SectionPatterns
   // will be associated with this InputSectionDescription.
diff --git a/lld/test/ELF/linkerscript/abs-path-match.s b/lld/test/ELF/linkerscript/abs-path-match.s
new file mode 100644
index 0000000000000..cc31dcd1e8031
--- /dev/null
+++ b/lld/test/ELF/linkerscript/abs-path-match.s
@@ -0,0 +1,56 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 main.s -o main.o
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
+# RUN: llvm-objcopy --rename-section .text=.text_foo  foo.o foo.o
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 bar.s -o bar.o
+# RUN: llvm-objcopy --rename-section .text=.text_bar  bar.o bar.o
+
+# RUN: ld.lld -r main.o %t/foo.o %t/bar.o -T script.ld -o main_abs.o
+
+# RUN: llvm-objdump -S main_abs.o > main_abs
+# RUN: llvm-objdump -S main_abs.o | FileCheck %s
+# CHECK: Disassembly of section .goo:
+
+
+#--- foo.s
+    .text
+    .globl	foo
+    .p2align	4
+    .type	foo, at function
+foo:
+    nop
+
+
+#--- bar.s
+    .text
+    .globl	bar
+    .p2align	4
+    .type	bar, at function
+bar:      
+    nop
+
+
+#--- main.s
+	.text
+	.globl	main
+	.p2align	4
+	.type	main, at function
+main:
+	callq	foo at PLT
+	callq	bar at PLT
+	retq
+
+
+#--- script.ld
+SECTIONS {
+  .text : { *(.text) }
+  .goo : {
+    bar.o(.text_bar);
+    foo.o(.text_foo);
+  }
+}
\ No newline at end of file



More information about the llvm-commits mailing list