1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00

llvm-objdump: return non-zero exit code for certain cases of invalid input

* If the input file is missing;
* If the type of input object file can't be recognized;
* If the object file can't be parsed correctly.

llvm-svn: 239065
This commit is contained in:
Alexey Samsonov 2015-06-04 18:34:11 +00:00
parent cc65db81cb
commit 66ffc34011
2 changed files with 17 additions and 7 deletions

View File

@ -0,0 +1,5 @@
RUN: not llvm-objdump -t %p/missing-file 2>&1 | FileCheck %s -check-prefix=NO_SUCH_FILE
NO_SUCH_FILE: '{{.*}}missing-file': No such file or directory
RUN: not llvm-objdump -t %s 2>&1 | FileCheck %s -check-prefix=UNKNOWN_FILE_TYPE
UNKNOWN_FILE_TYPE: '{{.*}}invalid-input.test': The file was not recognized as a valid object file

View File

@ -39,6 +39,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/GraphWriter.h"
@ -161,6 +162,12 @@ bool llvm::error(std::error_code EC) {
return true;
}
static void report_error(StringRef File, std::error_code EC) {
assert(EC);
errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
ReturnValue = EXIT_FAILURE;
}
static const Target *getTarget(const ObjectFile *Obj = nullptr) {
// Figure out the target triple.
llvm::Triple TheTriple("unknown-unknown-unknown");
@ -1263,15 +1270,13 @@ static void DumpArchive(const Archive *a) {
if (std::error_code EC = ChildOrErr.getError()) {
// Ignore non-object files.
if (EC != object_error::invalid_file_type)
errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
<< ".\n";
report_error(a->getFileName(), EC);
continue;
}
if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
DumpObject(o);
else
errs() << ToolName << ": '" << a->getFileName() << "': "
<< "Unrecognized file type.\n";
report_error(a->getFileName(), object_error::invalid_file_type);
}
}
@ -1279,7 +1284,7 @@ static void DumpArchive(const Archive *a) {
static void DumpInput(StringRef file) {
// If file isn't stdin, check that it exists.
if (file != "-" && !sys::fs::exists(file)) {
errs() << ToolName << ": '" << file << "': " << "No such file\n";
report_error(file, errc::no_such_file_or_directory);
return;
}
@ -1294,7 +1299,7 @@ static void DumpInput(StringRef file) {
// Attempt to open the binary.
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (std::error_code EC = BinaryOrErr.getError()) {
errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
report_error(file, EC);
return;
}
Binary &Binary = *BinaryOrErr.get().getBinary();
@ -1304,7 +1309,7 @@ static void DumpInput(StringRef file) {
else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
DumpObject(o);
else
errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
report_error(file, object_error::invalid_file_type);
}
int main(int argc, char **argv) {