[lld] r249025 - Copy DT_SONAME to DT_NEEDED.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 08:47:51 PDT 2015


Author: rafael
Date: Thu Oct  1 10:47:50 2015
New Revision: 249025

URL: http://llvm.org/viewvc/llvm-project?rev=249025&view=rev
Log:
Copy DT_SONAME to DT_NEEDED.

If a shared library has a DT_SONAME entry, that is what should be included
in the DT_NEEDED of a program using it.

We don't implement -soname yet, so check in a .so for now.

Added:
    lld/trunk/test/elf2/Inputs/soname.so   (with props)
    lld/trunk/test/elf2/soname.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/InputFiles.h
    lld/trunk/ELF/OutputSections.cpp

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=249025&r1=249024&r2=249025&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Thu Oct  1 10:47:50 2015
@@ -216,14 +216,39 @@ SharedFile<ELFT>::SharedFile(MemoryBuffe
     : SharedFileBase(getStaticELFKind<ELFT>(), M), ELFData<ELFT>(M) {}
 
 template <class ELFT> void SharedFile<ELFT>::parse() {
-  for (const Elf_Shdr &Sec : this->ELFObj.sections()) {
-    if (Sec.sh_type == SHT_DYNSYM) {
+  typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
+  typedef typename ELFFile<ELFT>::uintX_t uintX_t;
+  const Elf_Shdr *DynamicSec = nullptr;
+
+  const ELFFile<ELFT> Obj = this->ELFObj;
+  for (const Elf_Shdr &Sec : Obj.sections()) {
+    uint32_t Type = Sec.sh_type;
+    if (Type == SHT_DYNSYM)
       this->Symtab = &Sec;
-      break;
-    }
+    else if (Type == SHT_DYNAMIC)
+      DynamicSec = &Sec;
   }
 
+  // Also sets StringTable
   Elf_Sym_Range Syms = this->getNonLocalSymbols();
+  SoName = getName();
+
+  if (DynamicSec) {
+    auto *Begin =
+        reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
+    const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
+
+    for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
+      if (Dyn.d_tag == DT_SONAME) {
+        uintX_t Val = Dyn.getVal();
+        if (Val >= this->StringTable.size())
+          error("Invalid DT_SONAME entry");
+        SoName = StringRef(this->StringTable.data() + Val);
+        break;
+      }
+    }
+  }
+
   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
   SymbolBodies.reserve(NumSymbols);
   for (const Elf_Sym &Sym : Syms) {

Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=249025&r1=249024&r2=249025&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Thu Oct  1 10:47:50 2015
@@ -185,10 +185,14 @@ private:
 
 // .so file.
 class SharedFileBase : public ELFFileBase {
+protected:
+  StringRef SoName;
+
 public:
   SharedFileBase(ELFKind EKind, MemoryBufferRef M)
       : ELFFileBase(SharedKind, EKind, M) {}
   static bool classof(const InputFile *F) { return F->kind() == SharedKind; }
+  StringRef getSoName() const { return SoName; }
 };
 
 template <class ELFT>

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=249025&r1=249024&r2=249025&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Oct  1 10:47:50 2015
@@ -207,7 +207,7 @@ template <class ELFT> void DynamicSectio
   const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
       SymTab.getSharedFiles();
   for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
-    DynStrSec.add(File->getName());
+    DynStrSec.add(File->getSoName());
   NumEntries += SharedFiles.size();
 
   ++NumEntries; // DT_NULL
@@ -266,7 +266,7 @@ template <class ELFT> void DynamicSectio
       SymTab.getSharedFiles();
   for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
     P->d_tag = DT_NEEDED;
-    P->d_un.d_val = DynStrSec.getFileOff(File->getName());
+    P->d_un.d_val = DynStrSec.getFileOff(File->getSoName());
     ++P;
   }
 

Added: lld/trunk/test/elf2/Inputs/soname.so
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/soname.so?rev=249025&view=auto
==============================================================================
Binary files lld/trunk/test/elf2/Inputs/soname.so (added) and lld/trunk/test/elf2/Inputs/soname.so Thu Oct  1 10:47:50 2015 differ

Propchange: lld/trunk/test/elf2/Inputs/soname.so
------------------------------------------------------------------------------
    svn:executable = *

Added: lld/trunk/test/elf2/soname.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/soname.s?rev=249025&view=auto
==============================================================================
--- lld/trunk/test/elf2/soname.s (added)
+++ lld/trunk/test/elf2/soname.s Thu Oct  1 10:47:50 2015
@@ -0,0 +1,8 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: lld -flavor gnu2 %t.o %p/Inputs/soname.so -o %t
+// RUN: llvm-readobj --dynamic-table %t | FileCheck %s
+
+// CHECK:  0x0000000000000001 NEEDED               SharedLibrary (bar)
+
+.global _start
+_start:




More information about the llvm-commits mailing list