[llvm] e77c149 - [llvm-readobj] - Refine --needed-libs implementation and add a test.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 27 02:30:07 PST 2020


Author: Georgii Rymar
Date: 2020-01-27T13:29:28+03:00
New Revision: e77c149f0e4db4e51184f726bd3c5c83b17aa39a

URL: https://github.com/llvm/llvm-project/commit/e77c149f0e4db4e51184f726bd3c5c83b17aa39a
DIFF: https://github.com/llvm/llvm-project/commit/e77c149f0e4db4e51184f726bd3c5c83b17aa39a.diff

LOG: [llvm-readobj] - Refine --needed-libs implementation and add a test.

We have no good test for --needed-libs option.
The one we have as a part of Object/readobj-shared-object.test
is not complete.

In this patch I've did a minor NFC changes to the implementation and
added a test. This allowed to remove this piece from
Object/readobj-shared-object.test

Differential revision: https://reviews.llvm.org/D73174

Added: 
    llvm/test/tools/llvm-readobj/ELF/needed-libs.test

Modified: 
    llvm/test/Object/readobj-shared-object.test
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/Object/readobj-shared-object.test b/llvm/test/Object/readobj-shared-object.test
index 8401affc090c..32b84e9f7780 100644
--- a/llvm/test/Object/readobj-shared-object.test
+++ b/llvm/test/Object/readobj-shared-object.test
@@ -1,7 +1,7 @@
-# RUN: llvm-readobj --sections --symbols --dyn-syms --needed-libs \
+# RUN: llvm-readobj --sections --symbols --dyn-syms \
 # RUN:   %p/Inputs/shared-object-test.elf-i386 | FileCheck %s
 
-# RUN: llvm-readobj --sections --symbols --dyn-syms --needed-libs \
+# RUN: llvm-readobj --sections --symbols --dyn-syms \
 # RUN:   %p/Inputs/shared-object-test.elf-x86-64 | FileCheck %s
 
 # CHECK:   Sections [
@@ -281,8 +281,3 @@
 # CHECK:     Section: Absolute (0xFFF1)
 # CHECK:   }
 # CHECK: ]
-
-# CHECK:      NeededLibraries [
-# CHECK-NEXT:  libc.so.6
-# CHECK-NEXT:  libm.so.6
-# CHECK-NEXT: ]

diff  --git a/llvm/test/tools/llvm-readobj/ELF/needed-libs.test b/llvm/test/tools/llvm-readobj/ELF/needed-libs.test
new file mode 100644
index 000000000000..3be99cade5dc
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/needed-libs.test
@@ -0,0 +1,94 @@
+## In this test we check the --needed-libs option.
+
+# RUN: yaml2obj %s --docnum=1 -o %t1
+# RUN: llvm-readobj --needed-libs %t1 \
+# RUN:   | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=NEEDED-LIBS
+# RUN: llvm-readelf --needed-libs %t1 \
+# RUN:   | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=NEEDED-LIBS
+
+## Check that library names are sorted when printed.
+## Document that we also sort error entries.
+
+#      NEEDED-LIBS:NeededLibraries [
+# NEEDED-LIBS-NEXT:  <Invalid offset 0x1111111>
+# NEEDED-LIBS-NEXT:  <Invalid offset 0x9999999>
+# NEEDED-LIBS-NEXT:  aaa
+# NEEDED-LIBS-NEXT:  bbb
+# NEEDED-LIBS-NEXT:  ccc
+# NEEDED-LIBS-NEXT:]
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:  .dynstr
+    Type:  SHT_STRTAB
+    Flags: [ SHF_ALLOC ]
+    Content: '00616161006262620063636300' ## 0,a,a,a,0,b,b,b,0,c,c,c,0
+  - Name:    .dynamic
+    Type:    SHT_DYNAMIC
+    Entries:
+      - Tag:   DT_STRTAB
+        Value: 0x0000000000000000
+      - Tag:   DT_NEEDED
+        Value: 0x9999999
+      - Tag:   DT_NEEDED
+        Value: 9
+      - Tag:   DT_NEEDED
+        Value: 1
+      - Tag:   DT_NEEDED
+        Value: 5
+      - Tag:   DT_NEEDED
+        Value: 0x1111111
+      - Tag:   DT_STRSZ
+        Value: 0xD
+      - Tag:   DT_NULL
+        Value: 0x0
+ProgramHeaders:
+  - Type: PT_LOAD
+    VAddr: 0x0
+    Sections:
+      - Section: .dynstr
+      - Section: .dynamic
+
+## Check what we print when the dynamic string table is empty.
+# RUN: yaml2obj %s --docnum=2 -o %t2
+# RUN: llvm-readobj --needed-libs %t2 | FileCheck %s --check-prefix=EMPTY-DYNSTR
+# RUN: llvm-readelf --needed-libs %t2 | FileCheck %s --check-prefix=EMPTY-DYNSTR
+
+# EMPTY-DYNSTR:      NeededLibraries [
+# EMPTY-DYNSTR-NEXT:   <String table is empty or was not found>
+# EMPTY-DYNSTR-NEXT: ]
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:  .dynstr
+    Type:  SHT_STRTAB
+    Flags: [ SHF_ALLOC ]
+    Size:  0
+  - Name:    .dynamic
+    Type:    SHT_DYNAMIC
+    Address: 0x1000
+    Entries:
+      - Tag:   DT_STRTAB
+        Value: 0x0000000000000000
+      - Tag:   DT_NEEDED
+        Value: 1
+      - Tag:   DT_STRSZ
+        Value: 0x0
+      - Tag:   DT_NULL
+        Value: 0x0
+ProgramHeaders:
+  - Type: PT_LOAD
+    VAddr: 0x0
+    Sections:
+      - Section: .dynstr
+      - Section: .dynamic

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 766684624fd7..f321bab44c41 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -2471,9 +2471,9 @@ template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
     if (Entry.d_tag == ELF::DT_NEEDED)
       Libs.push_back(getDynamicString(Entry.d_un.d_val));
 
-  llvm::stable_sort(Libs);
+  llvm::sort(Libs);
 
-  for (const auto &L : Libs)
+  for (const std::string &L : Libs)
     W.startLine() << L << "\n";
 }
 


        


More information about the llvm-commits mailing list