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

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 03:57:00 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Longsheng Mou (CoTinker)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/72857.diff


1 Files Affected:

- (modified) lld/ELF/InputFiles.cpp (+2-2) 


``````````diff
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

``````````

</details>


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


More information about the llvm-commits mailing list