[elfabi] Introduce tool for ELF TextAPI

Follow up for D53051

This patch introduces the tool associated with the ELF implementation of
TextAPI (previously llvm-tapi, renamed for better distinction). This
tool will house a number of features related to enalysis and
manipulation of shared object's exposed interfaces. The first major
feature for this tool is support for producing binary stubs that are
useful for compile-time linking of shared objects. This patch introduces
beginnings of support for reading binary ELF objects to work towards
that goal.

Added:

 - elfabi tool.
 - support for reading architecture from a binary ELF file into an
 ELFStub.
 - Support for writing .tbe files.

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

llvm-svn: 350341
diff --git a/llvm/test/CMakeLists.txt b/llvm/test/CMakeLists.txt
index b39086a..d2b2b8d 100644
--- a/llvm/test/CMakeLists.txt
+++ b/llvm/test/CMakeLists.txt
@@ -61,6 +61,7 @@
           dsymutil
           llvm-dwarfdump
           llvm-dwp
+          llvm-elfabi
           llvm-exegesis
           llvm-extract
           llvm-isel-fuzzer
diff --git a/llvm/test/tools/llvm-elfabi/binary-read-arch.test b/llvm/test/tools/llvm-elfabi/binary-read-arch.test
new file mode 100644
index 0000000..ecb2fb8
--- /dev/null
+++ b/llvm/test/tools/llvm-elfabi/binary-read-arch.test
@@ -0,0 +1,15 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-elfabi %t --emit-tbe=- | FileCheck %s
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_DYN
+  Machine:         EM_X86_64
+
+# CHECK:      --- !tapi-tbe
+# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}}
+# CHECK-NEXT: Arch: x86_64
+# CHECK-NEXT: Symbols: {}
+# CHECK-NEXT: ...
diff --git a/llvm/test/tools/llvm-elfabi/fail-file-open.test b/llvm/test/tools/llvm-elfabi/fail-file-open.test
new file mode 100644
index 0000000..b4019af
--- /dev/null
+++ b/llvm/test/tools/llvm-elfabi/fail-file-open.test
@@ -0,0 +1,5 @@
+# RUN: not llvm-elfabi %s.NotAFileInTestingDir --emit-tbe=%t 2>&1 | FileCheck %s
+
+This file will not be read. An invalid file path is fed to llvm-elfabi.
+
+# CHECK: error: Could not open `{{.*}}.NotAFileInTestingDir`
diff --git a/llvm/test/tools/llvm-elfabi/read-unsupported-file.test b/llvm/test/tools/llvm-elfabi/read-unsupported-file.test
new file mode 100644
index 0000000..4ebe1bc
--- /dev/null
+++ b/llvm/test/tools/llvm-elfabi/read-unsupported-file.test
@@ -0,0 +1,7 @@
+# RUN: not llvm-elfabi %s --emit-tbe=%t 2>&1 | FileCheck %s
+
+This is just some text that cannot be read by llvm-elfabi.
+
+# CHECK: The file was not recognized as a valid object file
+# CHECK: YAML failed reading as TBE
+# CHECK: No file readers succeeded reading `{{.*}}` (unsupported/malformed file?)
diff --git a/llvm/test/tools/llvm-elfabi/replace-soname-tbe.test b/llvm/test/tools/llvm-elfabi/replace-soname-tbe.test
new file mode 100644
index 0000000..71d50a8
--- /dev/null
+++ b/llvm/test/tools/llvm-elfabi/replace-soname-tbe.test
@@ -0,0 +1,16 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-elfabi %t --emit-tbe=- --soname=best.so | FileCheck %s
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_DYN
+  Machine:         EM_AARCH64
+
+# CHECK:      --- !tapi-tbe
+# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}}
+# CHECK-NEXT: SoName: best.so
+# CHECK-NEXT: Arch: AArch64
+# CHECK-NEXT: Symbols: {}
+# CHECK-NEXT: ...
diff --git a/llvm/test/tools/llvm-elfabi/tbe-emits-current-version.test b/llvm/test/tools/llvm-elfabi/tbe-emits-current-version.test
new file mode 100644
index 0000000..12a5476
--- /dev/null
+++ b/llvm/test/tools/llvm-elfabi/tbe-emits-current-version.test
@@ -0,0 +1,13 @@
+# RUN: llvm-elfabi %s --emit-tbe=- | FileCheck %s
+
+--- !tapi-tbe
+TbeVersion: 1.0
+Arch: AArch64
+Symbols: {}
+...
+
+# As the tbe reader/writer is updated, update this check to ensure --emit-tbe
+# uses the latest tbe writer by default.
+
+# CHECK:      --- !tapi-tbe
+# CHECK-NEXT: TbeVersion: 1.0
diff --git a/llvm/test/tools/llvm-elfabi/tbe-read-basic.test b/llvm/test/tools/llvm-elfabi/tbe-read-basic.test
new file mode 100644
index 0000000..1599f5a
--- /dev/null
+++ b/llvm/test/tools/llvm-elfabi/tbe-read-basic.test
@@ -0,0 +1,25 @@
+# RUN: llvm-elfabi %s --emit-tbe=- | FileCheck %s
+
+--- !tapi-tbe
+SoName: somelib.so
+TbeVersion: 1.0
+Arch: x86_64
+Symbols:
+  foo: { Type: Func }
+  bar: { Type: Object, Size: 42 }
+  baz: { Type: Object, Size: 8 }
+  not: { Type: Object, Undefined: true, Size: 128 }
+  nor: { Type: Func, Undefined: true }
+...
+
+# CHECK:      --- !tapi-tbe
+# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}}
+# CHECK-NEXT: SoName: somelib.so
+# CHECK-NEXT: Arch: x86_64
+# CHECK-NEXT: Symbols:
+# CHECK-NEXT:   bar: { Type: Object, Size: 42 }
+# CHECK-NEXT:   baz: { Type: Object, Size: 8 }
+# CHECK-NEXT:   foo: { Type: Func }
+# CHECK-NEXT:   nor: { Type: Func, Undefined: true }
+# CHECK-NEXT:   not: { Type: Object, Size: 128, Undefined: true }
+# CHECK-NEXT: ...