From: Armando Montanez Date: Mon, 7 Jan 2019 17:33:10 +0000 (+0000) Subject: [elfabi] Add option to manually specify file read format X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6101a1a3201a098a049ecf02278a2a3896aedd9f;p=llvm [elfabi] Add option to manually specify file read format Although llvm-elfabi will attempt to read input files without needing the format to be manually specified, doing so has the potential to introduce extraneous errors that can hinder debugging (since multiple readers may fail in attempts to read the file). This change allows the input file format to be manually specified to force elfabi to use a single reader. This makes it easier to test and debug errors specific to a given reader. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350545 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/tools/llvm-elfabi/read-tbe-as-elf.test b/test/tools/llvm-elfabi/read-tbe-as-elf.test new file mode 100644 index 00000000000..eaddd38867a --- /dev/null +++ b/test/tools/llvm-elfabi/read-tbe-as-elf.test @@ -0,0 +1,16 @@ +# RUN: not llvm-elfabi --elf %s --emit-tbe=%t 2>&1 | 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: The file was not recognized as a valid object file +# CHECK: No file readers succeeded reading `{{.*}}read-tbe-as-elf.test` (unsupported/malformed file?) diff --git a/test/tools/llvm-elfabi/read-tbe-as-tbe.test b/test/tools/llvm-elfabi/read-tbe-as-tbe.test new file mode 100644 index 00000000000..a5e2d44134e --- /dev/null +++ b/test/tools/llvm-elfabi/read-tbe-as-tbe.test @@ -0,0 +1,13 @@ +# RUN: llvm-elfabi --tbe %s --emit-tbe=- | FileCheck %s + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: AArch64 +Symbols: {} +... + +# CHECK: --- !tapi-tbe +# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} +# CHECK-NEXT: Arch: AArch64 +# CHECK-NEXT: Symbols: {} +# CHECK-NEXT: ... diff --git a/tools/llvm-elfabi/llvm-elfabi.cpp b/tools/llvm-elfabi/llvm-elfabi.cpp index e7c81604ee1..4c15bc2eaf3 100644 --- a/tools/llvm-elfabi/llvm-elfabi.cpp +++ b/tools/llvm-elfabi/llvm-elfabi.cpp @@ -19,10 +19,27 @@ #include "llvm/TextAPI/ELF/TBEHandler.h" #include +namespace llvm { +namespace elfabi { + +enum class FileFormat { + TBE, + ELF +}; + +} // end namespace elfabi +} // end namespace llvm + using namespace llvm; using namespace llvm::elfabi; // Command line flags: +cl::opt InputFileFormat( + cl::desc("Force input file format:"), + cl::values(clEnumValN(FileFormat::TBE, + "tbe", "Read `input` as text-based ELF stub"), + clEnumValN(FileFormat::ELF, + "elf", "Read `input` as ELF binary"))); cl::opt InputFilePath(cl::Positional, cl::desc("input"), cl::Required); cl::opt @@ -67,20 +84,26 @@ static Expected> readInputFile(StringRef FilePath) { ErrorCollector EC(/*UseFatalErrors=*/false); // First try to read as a binary (fails fast if not binary). - Expected> StubFromELF = - readELFFile(FileReadBuffer->getMemBufferRef()); - if (StubFromELF) { - return std::move(*StubFromELF); + if (InputFileFormat.getNumOccurrences() == 0 || + InputFileFormat == FileFormat::ELF) { + Expected> StubFromELF = + readELFFile(FileReadBuffer->getMemBufferRef()); + if (StubFromELF) { + return std::move(*StubFromELF); + } + EC.addError(StubFromELF.takeError(), "BinaryRead"); } - EC.addError(StubFromELF.takeError(), "BinaryRead"); // Fall back to reading as a tbe. - Expected> StubFromTBE = - readTBEFromBuffer(FileReadBuffer->getBuffer()); - if (StubFromTBE) { - return std::move(*StubFromTBE); + if (InputFileFormat.getNumOccurrences() == 0 || + InputFileFormat == FileFormat::TBE) { + Expected> StubFromTBE = + readTBEFromBuffer(FileReadBuffer->getBuffer()); + if (StubFromTBE) { + return std::move(*StubFromTBE); + } + EC.addError(StubFromTBE.takeError(), "YamlParse"); } - EC.addError(StubFromTBE.takeError(), "YamlParse"); // If both readers fail, build a new error that includes all information. EC.addError(createStringError(errc::not_supported,