[lld] [lld][ELF] Enable link script to support absolute path matching (PR #156353)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 22 03:27:45 PDT 2025
https://github.com/mykouHW updated https://github.com/llvm/llvm-project/pull/156353
>From a40f367827db7f0b0a891f8558299b1aa91f0eb1 Mon Sep 17 00:00:00 2001
From: koumeiyuan <koumeiyuan at huawei.com>
Date: Mon, 25 Aug 2025 17:34:40 +0000
Subject: [PATCH 1/2] [lld][ELF] Enable link script to support absolute path
matching
Fixing the compatibility issue with filename matching in linker scripts. When input files use absolute paths, the matching results from lld do not meet expectations.
---
lld/ELF/InputFiles.cpp | 11 ++++-
lld/ELF/LinkerScript.cpp | 20 +++++---
lld/ELF/LinkerScript.h | 2 +-
lld/test/ELF/linkerscript/abs-path-match.s | 57 ++++++++++++++++++++++
4 files changed, 79 insertions(+), 11 deletions(-)
create mode 100644 lld/test/ELF/linkerscript/abs-path-match.s
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index a5921feb18299..db73b5f494a94 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -31,6 +31,7 @@
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
+#include <filesystem>
using namespace llvm;
using namespace llvm::ELF;
@@ -368,8 +369,14 @@ void elf::parseFiles(Ctx &ctx,
// Concatenates arguments to construct a string representing an error location.
StringRef InputFile::getNameForScript() const {
- if (archiveName.empty())
- return getName();
+ if (archiveName.empty()) {
+ if (std::filesystem::current_path() ==
+ std::filesystem::path(getName().str()).parent_path()) {
+ return llvm::sys::path::filename(getName());
+ } else {
+ return getName();
+ }
+ }
if (nameForScriptCache.empty())
nameForScriptCache = (archiveName + Twine(':') + getName()).str();
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 921128dae2bdb..218c9d3a86184 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1021,10 +1021,6 @@ void LinkerScript::addOrphanSections() {
}
};
- // For further --emit-reloc handling code we need target output section
- // to be created before we create relocation output section, so we want
- // to create target sections first. We do not want priority handling
- // for synthetic sections because them are special.
size_t n = 0;
for (InputSectionBase *isec : ctx.inputSections) {
// Process InputSection and MergeInputSection.
@@ -1037,10 +1033,18 @@ 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->parent))
- add(relIS);
+ if (auto *sec = dyn_cast<InputSection>(isec)) {
+ if (InputSectionBase *relocated = sec->getRelocatedSection()) {
+ // For --emit-relocs and -r, ensure the output section for .text.foo
+ // is created before the output section for .rela.text.foo.
+ add(relocated);
+ // EhInputSection sections are not added to ctx.inputSections. If we see
+ // .rela.eh_frame, ensure the output section for the synthetic
+ // EhFrameSection is created first.
+ if (auto *p = dyn_cast_or_null<InputSectionBase>(relocated->parent))
+ add(p);
+ }
+ }
add(isec);
if (ctx.arg.relocatable)
for (InputSectionBase *depSec : isec->dependentSections)
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..1d87b4ee50c73
--- /dev/null
+++ b/lld/test/ELF/linkerscript/abs-path-match.s
@@ -0,0 +1,57 @@
+# 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-mc -filetype=obj -triple=x86_64 bar.s -o bar.o
+
+# RUN: ld.lld main.o %t/foo.o %t/bar.o -T script.ld -o main_abs.o -Map=main_abs.map
+
+# RUN: FileCheck %s < main_abs.map
+# CHECK: .goo
+# CHECK: bar.o:(.text_bar)
+# CHECK: bar
+# CHECK: foo.o:(.text_foo)
+# CHECK: foo
+# CHECK-NOT: .text_bar
+# CHECK-NOT: .text_foo
+
+#--- foo.s
+ .section .text_foo, "ax", %progbits
+ .globl foo
+ .p2align 4
+ .type foo, at function
+foo:
+ nop
+
+
+#--- bar.s
+ .section .text_bar, "ax", %progbits
+ .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
>From d89f465673f5808b25d96dc476a5ec67850bc173 Mon Sep 17 00:00:00 2001
From: koumeiyuan <koumeiyuan at huawei.com>
Date: Mon, 25 Aug 2025 17:34:40 +0000
Subject: [PATCH 2/2] [lld][ELF] Enable link script to support absolute path
matching
Fixing the compatibility issue with filename matching in linker scripts. When input files use absolute paths, the matching results from lld do not meet expectations.
---
lld/ELF/InputFiles.cpp | 2 +-
lld/ELF/LinkerScript.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index db73b5f494a94..341c502f75f64 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -30,8 +30,8 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
-#include <optional>
#include <filesystem>
+#include <optional>
using namespace llvm;
using namespace llvm::ELF;
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 452cfbcd9b777..80c4f564afabc 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, bool ExtractFilename) const;
+ bool matchesFile(const InputFile &file) const;
// Input sections that matches at least one of SectionPatterns
// will be associated with this InputSectionDescription.
More information about the llvm-commits
mailing list