[lld] Update InputFiles.cpp (PR #72080)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 12 19:34:44 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld

Author: None (ls-Mou)

<details>
<summary>Changes</summary>

The name of soNames and deNeeded should be consistent

### Library
#### 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();
}
```
### Correct Scenario
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/149390011/7cd9d2b3-a919-4160-85ad-53fa9bc0321b)

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/149390011/022fd486-111c-4771-83d0-27931b2792a4)

the soName and the needed name are consistent, so can report the right error.

### Incorrect Scenario
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**

the soName and the needed name are inconsistent, so the lld can not report error.
https://github.com/llvm/llvm-project/blob/edc38a6cbd12a7cfa5d3c4de9767333c442ef2eb/lld/ELF/Writer.cpp#L2019-L2036

---
Full diff: https://github.com/llvm/llvm-project/pull/72080.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 8c7f2c8773f2cbc..0602f435e7e8264 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/72080


More information about the llvm-commits mailing list