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

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


https://github.com/ls-Mou created https://github.com/llvm/llvm-project/pull/72080

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

>From 291745effe8291b45483eeb840a8a467945dee88 Mon Sep 17 00:00:00 2001
From: ls-Mou <moulongsheng at huawei.com>
Date: Mon, 13 Nov 2023 11:10:45 +0800
Subject: [PATCH] Update InputFiles.cpp

The name of soNames and deNeeded should be consistent
---
 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 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



More information about the llvm-commits mailing list