1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[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
This commit is contained in:
Georgii Rymar 2021-01-28 13:28:55 +03:00
parent d1398bbcf7
commit a153345c4f
2 changed files with 41 additions and 10 deletions

View File

@ -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 different 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'

View File

@ -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.