[llvm] a5154ab - [llvm-readobj/elf] - Report "bitcode files are not supported" warning for bitcode files.
Georgii Rymar via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 29 01:05:07 PST 2021
Author: Georgii Rymar
Date: 2021-01-29T12:04:41+03:00
New Revision: a5154ab9b0c162cadea70d870884a70032c747c1
URL: https://github.com/llvm/llvm-project/commit/a5154ab9b0c162cadea70d870884a70032c747c1
DIFF: https://github.com/llvm/llvm-project/commit/a5154ab9b0c162cadea70d870884a70032c747c1.diff
LOG: [llvm-readobj/elf] - Report "bitcode files are not supported" warning for bitcode files.
Fixes https://bugs.llvm.org/show_bug.cgi?id=43543
Currently we report "The file was not recognized as a valid object file" for BC files.
Also, we terminate dumping.
Instead we could report a better warning and try to continue dumping other files.
This is what this patch implements.
Differential revision: https://reviews.llvm.org/D95605
Added:
Modified:
llvm/test/tools/llvm-readobj/basic.test
llvm/tools/llvm-readobj/llvm-readobj.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-readobj/basic.test b/llvm/test/tools/llvm-readobj/basic.test
index ddf6acb5c44d..5c5abee108c2 100644
--- a/llvm/test/tools/llvm-readobj/basic.test
+++ b/llvm/test/tools/llvm-readobj/basic.test
@@ -20,6 +20,22 @@ RUN: llvm-ar rc %t.a %t.empty
RUN: llvm-readobj --all %t.a 2>&1 | FileCheck --check-prefix=NO-OUTPUT --allow-empty %s
NO-OUTPUT-NOT: {{.}}
+## Test we report a meaningful warning for bitcode files.
+## Check we try to continue dumping other files.
+
+## Note: 'echo -e -n "\x42\x43\xc0\xde" > %t.bc.1' simply doesn't work properly on windows.
+## It has an issue with writing of the 2 last bytes and emits
diff erent data instead.
+## echo.exe from GnuWin32 works properly though, but using of python is a more stable way.
+RUN: %python -c "import os; open(r'%t.bc.1', 'wb').write(b'\x42\x43\xC0\xDE')"
+RUN: %python -c "import os; open(r'%t.bc.2', 'wb').write(b'\xDE\xC0\x17\x0B')"
+RUN: llvm-readelf %t.bc.1 %t.bc.2 2>&1 | \
+RUN: FileCheck --check-prefix=BITCODE -DFILE1=%t.bc.1 -DFILE2=%t.bc.2 %s
+RUN: llvm-readobj %t.bc.1 %t.bc.2 2>&1 | \
+RUN: FileCheck --check-prefix=BITCODE -DFILE1=%t.bc.1 -DFILE2=%t.bc.2 %s
+
+# BITCODE: warning: '[[FILE1]]': bitcode files are not supported{{$}}
+# BITCODE: warning: '[[FILE2]]': bitcode files are not supported{{$}}
+
# Test case where switch it not recognised.
RUN: not llvm-readobj --unknown-switch 2>&1 | FileCheck --check-prefix=UNKNOWN %s
UNKNOWN: Unknown command line argument '--unknown-switch'
diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp
index 41cd4414d051..841313ebe2b1 100644
--- a/llvm/tools/llvm-readobj/llvm-readobj.cpp
+++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp
@@ -646,28 +646,43 @@ static void dumpWindowsResourceFile(WindowsResource *WinRes,
/// Opens \a File and dumps it.
static void dumpInput(StringRef File, ScopedPrinter &Writer) {
- // Attempt to open the binary.
- Expected<OwningBinary<Binary>> BinaryOrErr =
- createBinary(File, /*Context=*/nullptr, /*InitContent=*/false);
+ ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
+ MemoryBuffer::getFileOrSTDIN(File, /*FileSize=*/-1,
+ /*RequiresNullTerminator=*/false);
+ if (std::error_code EC = FileOrErr.getError())
+ return reportError(errorCodeToError(EC), File);
+
+ std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
+ file_magic Type = identify_magic(Buffer->getBuffer());
+ if (Type == file_magic::bitcode) {
+ reportWarning(createStringError(errc::invalid_argument,
+ "bitcode files are not supported"),
+ File);
+ return;
+ }
+
+ Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
+ Buffer->getMemBufferRef(), /*Context=*/nullptr, /*InitContent=*/false);
if (!BinaryOrErr)
reportError(BinaryOrErr.takeError(), File);
- Binary &Binary = *BinaryOrErr.get().getBinary();
- if (Archive *Arc = dyn_cast<Archive>(&Binary))
+ std::unique_ptr<Binary> Bin = std::move(*BinaryOrErr);
+ if (Archive *Arc = dyn_cast<Archive>(Bin.get()))
dumpArchive(Arc, Writer);
else if (MachOUniversalBinary *UBinary =
- dyn_cast<MachOUniversalBinary>(&Binary))
+ dyn_cast<MachOUniversalBinary>(Bin.get()))
dumpMachOUniversalBinary(UBinary, Writer);
- else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
+ else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Bin.get()))
dumpObject(*Obj, Writer);
- else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
+ else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(Bin.get()))
dumpCOFFImportFile(Import, Writer);
- else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(&Binary))
+ else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(Bin.get()))
dumpWindowsResourceFile(WinRes, Writer);
else
llvm_unreachable("unrecognized file type");
- CVTypes.Binaries.push_back(std::move(*BinaryOrErr));
+ CVTypes.Binaries.push_back(
+ OwningBinary<Binary>(std::move(Bin), std::move(Buffer)));
}
/// Registers aliases that should only be allowed by readobj.
More information about the llvm-commits
mailing list