[lld] [LLD] dtNeeded name should consistent with soNames (PR #72857)

Longsheng Mou via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 20:40:23 PST 2023


https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/72857

>From 69b001a9f7c32659cfba7074b441af18edd68028 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <moulongsheng at huawei.com>
Date: Mon, 20 Nov 2023 19:43:42 +0800
Subject: [PATCH] [LLD] dtNeeded name should consistent with soNames

If dtNeeded name is inconsistent with soNames, lld can not report error right.
---
 lld/ELF/InputFiles.cpp                   |  6 +++---
 lld/test/ELF/dtNeeded-soName-undefined.s | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 lld/test/ELF/dtNeeded-soName-undefined.s

diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 7ac737dab0519a8..75dfa40eb7068f9 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1439,7 +1439,7 @@ template <class ELFT> void SharedFile::parse() {
       uint64_t val = dyn.getVal();
       if (val >= this->stringTable.size())
         fatal(toString(this) + ": invalid DT_NEEDED entry");
-      dtNeeded.push_back(this->stringTable.data() + val);
+      dtNeeded.push_back(path::filename(this->stringTable.data() + val));
     } else if (dyn.d_tag == DT_SONAME) {
       uint64_t val = dyn.getVal();
       if (val >= this->stringTable.size())
@@ -1451,8 +1451,8 @@ template <class ELFT> void SharedFile::parse() {
   // DSOs are uniquified not by filename but by soname.
   DenseMap<CachedHashStringRef, SharedFile *>::iterator it;
   bool wasInserted;
-  std::tie(it, wasInserted) =
-      symtab.soNames.try_emplace(CachedHashStringRef(soName), this);
+  std::tie(it, wasInserted) = symtab.soNames.try_emplace(
+      CachedHashStringRef(path::filename(soName)), this);
 
   // If a DSO appears more than once on the command line with and without
   // --as-needed, --no-as-needed takes precedence over --as-needed because a
diff --git a/lld/test/ELF/dtNeeded-soName-undefined.s b/lld/test/ELF/dtNeeded-soName-undefined.s
new file mode 100644
index 000000000000000..036108db3d2b663
--- /dev/null
+++ b/lld/test/ELF/dtNeeded-soName-undefined.s
@@ -0,0 +1,24 @@
+# REQUIRES: aarch64
+
+# RUN: rm -rf %t && split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=aarch64 main.s -o main.o
+# RUN: llvm-mc -filetype=obj -triple=aarch64 def.s -o def.o && ld.lld -shared def.o -o def.so
+# RUN: llvm-mc -filetype=obj -triple=aarch64 ref.s -o ref.o && ld.lld -shared ref.o -o ref.so ./def.so
+
+# RUN: not ld.lld main.o ref.so def.so -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK-NOT:  error:
+# CHECK:      error: undefined reference due to --no-allow-shlib-undefined: foo
+# CHECK-NEXT: >>> referenced by ref.so
+
+
+#--- def.s
+
+#--- ref.s
+.globl fun
+fun:
+  bl foo at PLT
+#--- main.s
+.golbl _start
+_start:
+  bl fun at PLT



More information about the llvm-commits mailing list