diff --git a/llvm/test/tools/llvm-objcopy/ELF/same-file-strip.test b/llvm/test/tools/llvm-objcopy/ELF/same-file-strip.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/same-file-strip.test @@ -0,0 +1,26 @@ +## Test llvm-strip using the same input file more than once. +## When using stdin ('-') more than once llvm-strip should give an error +## while a file more than once should be simply a warning. + +# RUN: yaml2obj %s -o %t + +# RUN: not llvm-strip - - < %t 2>&1 | FileCheck -check-prefix=ERR %s +# RUN: not llvm-strip - %t - < %t 2>&1 | FileCheck -check-prefix=ERR %s + +# ERR: error: cannot specify '-' as an input file more than once + +# RUN: llvm-strip %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t +# RUN: llvm-strip %t %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t + +# WARN: warning: '[[FILE]]' was already specified + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] diff --git a/llvm/tools/llvm-objcopy/CopyConfig.h b/llvm/tools/llvm-objcopy/CopyConfig.h --- a/llvm/tools/llvm-objcopy/CopyConfig.h +++ b/llvm/tools/llvm-objcopy/CopyConfig.h @@ -189,7 +189,9 @@ // ParseStripOptions returns the config and sets the input arguments. If a // help flag is set then ParseStripOptions will print the help messege and // exit. -Expected parseStripOptions(ArrayRef ArgsArr); +Expected +parseStripOptions(ArrayRef ArgsArr, + std::function RecoverableErrorCallback); } // namespace objcopy } // namespace llvm diff --git a/llvm/tools/llvm-objcopy/CopyConfig.cpp b/llvm/tools/llvm-objcopy/CopyConfig.cpp --- a/llvm/tools/llvm-objcopy/CopyConfig.cpp +++ b/llvm/tools/llvm-objcopy/CopyConfig.cpp @@ -11,6 +11,7 @@ #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/CommandLine.h" @@ -19,6 +20,7 @@ #include "llvm/Support/JamCRC.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/StringSaver.h" +#include "llvm/Support/WithColor.h" #include namespace llvm { @@ -713,7 +715,9 @@ // ParseStripOptions returns the config and sets the input arguments. If a // help flag is set then ParseStripOptions will print the help messege and // exit. -Expected parseStripOptions(ArrayRef ArgsArr) { +Expected +parseStripOptions(ArrayRef ArgsArr, + std::function RecoverableErrorCallback) { StripOptTable T; unsigned MissingArgumentIndex, MissingArgumentCount; llvm::opt::InputArgList InputArgs = @@ -735,7 +739,7 @@ exit(0); } - SmallVector Positional; + SmallVector Positional; for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) return createStringError(errc::invalid_argument, "unknown argument '%s'", Arg->getAsString(InputArgs).c_str()); @@ -800,7 +804,17 @@ InputArgs.getLastArgValue(STRIP_output, Positional[0]); DC.CopyConfigs.push_back(std::move(Config)); } else { - for (const char *Filename : Positional) { + StringMap InputFiles; + for (StringRef Filename : Positional) { + if (InputFiles[Filename]++ == 1) { + if (Filename == "-") + return createStringError( + errc::invalid_argument, + "cannot specify '-' as an input file more than once"); + RecoverableErrorCallback( + createFileError("'" + Filename + "' was already specified", + errc::invalid_argument)); + } Config.InputFilename = Filename; Config.OutputFilename = Filename; DC.CopyConfigs.push_back(Config); diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -54,7 +54,6 @@ LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { WithColor::error(errs(), ToolName) << Message << ".\n"; - errs().flush(); exit(1); } @@ -83,6 +82,11 @@ exit(1); } +void reportWarning(Error E) { + assert(E); + WithColor::warning(errs(), ToolName) << toString(std::move(E)); +} + } // end namespace objcopy } // end namespace llvm @@ -250,7 +254,7 @@ ToolName = argv[0]; bool IsStrip = sys::path::stem(ToolName).contains("strip"); Expected DriverConfig = - IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc)) + IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc), reportWarning) : parseObjcopyOptions(makeArrayRef(argv + 1, argc)); if (!DriverConfig) { logAllUnhandledErrors(DriverConfig.takeError(),