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,24 @@ +## Test strip using the same input file more than once + +# 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.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,11 +20,19 @@ #include "llvm/Support/JamCRC.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/StringSaver.h" +#include "llvm/Support/WithColor.h" #include namespace llvm { namespace objcopy { +extern StringRef ToolName; + +static void reportWarning(Twine Message) { + WithColor::warning(errs(), ToolName) << Message << "\n"; + errs().flush(); +} + namespace { enum ObjcopyID { OBJCOPY_INVALID = 0, // This is not an option ID. @@ -735,7 +744,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 +809,15 @@ 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"); + reportWarning("'" + Filename + "' was already specified"); + } Config.InputFilename = Filename; Config.OutputFilename = Filename; DC.CopyConfigs.push_back(Config);