[llvm-nm][test] Add additional test coverage for llvm-nm options
authorJames Henderson <james.henderson@sony.com>
Fri, 12 Feb 2021 14:23:45 +0000 (14:23 +0000)
committerJames Henderson <james.henderson@sony.com>
Mon, 15 Feb 2021 13:54:08 +0000 (13:54 +0000)
Some of these options have a degree of incidental coverage, or are for
Mach-O only. This patch adds dedicated ELF (where applicable) coverage.

Differential Revision: https://reviews.llvm.org/D96602

Reviewed by: rupprecht, Higuoxing

llvm/test/tools/llvm-nm/X86/bitcode.test [new file with mode: 0644]
llvm/test/tools/llvm-nm/defined-only.test [new file with mode: 0644]
llvm/test/tools/llvm-nm/format-bsd.test [new file with mode: 0644]
llvm/test/tools/llvm-nm/just-symbol-name.test [new file with mode: 0644]
llvm/test/tools/llvm-nm/reverse-sort.test [new file with mode: 0644]

diff --git a/llvm/test/tools/llvm-nm/X86/bitcode.test b/llvm/test/tools/llvm-nm/X86/bitcode.test
new file mode 100644 (file)
index 0000000..42826c9
--- /dev/null
@@ -0,0 +1,16 @@
+;; Show that llvm-nm prints dashes for the symbol value of symbols in values.
+;; Also show that the --no-llvm-bc option prevents loading bitcode files.
+; RUN: llvm-as %s -o %t.bc
+
+; RUN: llvm-nm %t.bc | FileCheck %s --check-prefix=RECOGNISED
+; RUN: not llvm-nm %t.bc --no-llvm-bc 2>&1 | FileCheck %s --check-prefix=UNRECOGNISED -DFILE=%t.bc
+
+; RECOGNISED: ---------------- T foo
+; UNRECOGNISED: error: [[FILE]]: The file was not recognized as a valid object file
+
+target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @foo() {
+  ret void
+}
diff --git a/llvm/test/tools/llvm-nm/defined-only.test b/llvm/test/tools/llvm-nm/defined-only.test
new file mode 100644 (file)
index 0000000..e36297a
--- /dev/null
@@ -0,0 +1,27 @@
+## Show that the --defined-only option omits undefined symbols.
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: llvm-nm %t.o --defined-only > %t.defonly.txt
+# RUN: llvm-nm %t.o -U > %t.U.txt
+
+# RUN: cmp %t.defonly.txt %t.U.txt
+
+# RUN: FileCheck %s --input-file=%t.defonly.txt --implicit-check-not={{.}}
+
+# CHECK: 0000000000000000 T defined
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+Sections:
+  - Name:  .text
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+  - Name:    defined
+    Section: .text
+    Binding: STB_GLOBAL
+  - Name:    undefined
+    Binding: STB_GLOBAL
diff --git a/llvm/test/tools/llvm-nm/format-bsd.test b/llvm/test/tools/llvm-nm/format-bsd.test
new file mode 100644 (file)
index 0000000..d04ecdc
--- /dev/null
@@ -0,0 +1,35 @@
+## --format-bsd is the default output format of llvm-nm. Show that the default
+## and bsd output (including its aliases) match to confirm this.
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: llvm-nm %t.o --format=bsd > %t.formatbsd.txt
+# RUN: llvm-nm %t.o -f=bsd > %t.fbsd.txt
+# RUN: llvm-nm %t.o -B > %t.b.txt
+# RUN: llvm-nm %t.o > %t.default.txt
+
+# RUN: cmp %t.formatbsd.txt %t.default.txt
+# RUN: cmp %t.formatbsd.txt %t.fbsd.txt
+# RUN: cmp %t.formatbsd.txt %t.b.txt
+
+## Sanity check that some output is actually printed.
+# RUN: FileCheck %s --input-file=%t.default.txt
+
+# CHECK:      0000000000000000 T sym1
+# CHECK-NEXT: 0000000000000000 W sym2
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+Sections:
+  - Name:  .text
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+  - Name:    sym1
+    Section: .text
+    Binding: STB_GLOBAL
+  - Name:    sym2
+    Section: .text
+    Binding: STB_WEAK
diff --git a/llvm/test/tools/llvm-nm/just-symbol-name.test b/llvm/test/tools/llvm-nm/just-symbol-name.test
new file mode 100644 (file)
index 0000000..85be79a
--- /dev/null
@@ -0,0 +1,38 @@
+## Show that the -j/--just-symbol-name prints only the the symbol name (except
+## in posix output).
+
+# RUN: yaml2obj %s -o %t.o
+
+# RUN: llvm-nm --just-symbol-name %t.o > %t.bsd.txt
+# RUN: llvm-nm -j %t.o > %t.j.txt
+# RUN: cmp %t.bsd.txt %t.j.txt
+
+# RUN: FileCheck %s --input-file=%t.bsd.txt --implicit-check-not={{.}} --check-prefix=COMMON
+# RUN: llvm-nm -j %t.o --format=sysv | \
+# RUN:   FileCheck %s --implicit-check-not={{.}} --check-prefixes=COMMON,SYSV -DFILE=%t.o
+# RUN: llvm-nm -j %t.o --format=posix | FileCheck %s --implicit-check-not={{.}} --check-prefix=POSIX
+
+# SYSV:        Symbols from [[FILE]]:
+# SYSV-EMPTY:
+# SYSV-NEXT:   Name Value Class Type Size Line Section
+# COMMON:      {{^}}defined{{$}}
+# COMMON-NEXT: {{^}}undefined{{$}}
+
+# POSIX:      defined T 0 0
+# POSIX-NEXT: undefined U 0 0
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+Sections:
+  - Name:  .text
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+  - Name:    defined
+    Section: .text
+    Binding: STB_GLOBAL
+  - Name:    undefined
+    Binding: STB_GLOBAL
diff --git a/llvm/test/tools/llvm-nm/reverse-sort.test b/llvm/test/tools/llvm-nm/reverse-sort.test
new file mode 100644 (file)
index 0000000..6cbf674
--- /dev/null
@@ -0,0 +1,37 @@
+## Show that the --reverse-sort option reverses the selected sort order.
+# RUN: yaml2obj %s -o %t.o
+# RUN: llvm-nm %t.o --reverse-sort | FileCheck %s --check-prefix=SORTED
+# RUN: llvm-nm %t.o --reverse-sort --numeric-sort | FileCheck %s --check-prefix=SORTED
+# RUN: llvm-nm %t.o --reverse-sort --size-sort | FileCheck %s --check-prefix=SORTED
+# RUN: llvm-nm %t.o --reverse-sort --no-sort | FileCheck %s --check-prefix=UNSORTED
+
+# SORTED:      third
+# SORTED-NEXT: second
+# SORTED-NEXT: first
+
+# UNSORTED:      second
+# UNSORTED-NEXT: third
+# UNSORTED-NEXT: first
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+Sections:
+  - Name:  .text
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+  - Name:    second
+    Section: .text
+    Value:   2
+    Size:    2
+  - Name:    third
+    Section: .text
+    Value:   3
+    Size:    3
+  - Name:    first
+    Section: .text
+    Value:   1
+    Size:    1