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

Longsheng Mou via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 03:56:31 PST 2023


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

If dtNeeded name is inconsistent with soNames, lld can not report error right.
### demo
#### libhave.so
`clang have.c --shared -fPIC -o libhave.so`
```
// have.h
void foo();

// have.c
#include "have.h"
```

#### libuse.so
`clang use.c --shared -fPIC -o libuse.so libhave.so`
```
// use.h
#include "have.h"
void fun();

// use.c
#include "use.h"
void fun() {
    foo();
}
```
#### main

main.c

```
#include "use.h"

int main() {
    fun();
}
```
### can report error
1.
clang case/use.c -o exe/libuse.so **-lhave** -Lexe --shared -fPIC
clang case/main.c -o exe/main.exe -luse -Lexe **-lhave**
soName=**libhave.so**  needed=**libhave.so**
![image](https://github.com/llvm/llvm-project/assets/84192071/464d61f4-7822-4da1-8f2b-40bb4ae8a290)

2.
clang case/use.c -o exe/libuse.so  **exe/libhave.so** --shared -fPIC
clang case/main.c -o exe/main.exe -luse -Lexe  **exe/libhave.so**
soName=**exe/libhave.so**  needed=**exe/libhave.so**
![image](https://github.com/llvm/llvm-project/assets/84192071/ff1a6512-9016-4bd0-b432-d53df6f1232e)

### can not report error

1.
clang case/ususe.c -o exe/libuseo **exe/libhave.so** --shared -fPIC
clang case/main.c -o exe/main.exe -luse -Lexe **-lhave**
soName=**libhave.so**  needed=**exe/libhave.so**

2.
clang case/ususe.c -o exe/libuseo **-lhave** -Lexe --shared -fPIC
clang case/main.c -o exe/main.exe -luse -Lexe **exe/libhave.so**
soName=**exe/libhave.so**  needed=**libhave.so**

We can see that if dtNeeded name is inconsistent with soNames, lld can not report error right.
https://github.com/llvm/llvm-project/blob/4594d5bb3ac6772bb20e429bbb04842ef6eaea35/lld/ELF/Writer.cpp#L2019-L2036

>From 2fcaf6bb208f57e25f796e062fd1167174f7007a 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 not consistent with soNames, lld can not report error right.
---
 lld/ELF/InputFiles.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 7ac737dab0519a8..05b9dc2d5474170 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())
@@ -1452,7 +1452,7 @@ template <class ELFT> void SharedFile::parse() {
   DenseMap<CachedHashStringRef, SharedFile *>::iterator it;
   bool wasInserted;
   std::tie(it, wasInserted) =
-      symtab.soNames.try_emplace(CachedHashStringRef(soName), this);
+      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



More information about the llvm-commits mailing list