diff --git a/llvm/test/tools/llvm-objcopy/ELF/invalid-preserve-dates.test b/llvm/test/tools/llvm-objcopy/ELF/invalid-preserve-dates.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/invalid-preserve-dates.test @@ -0,0 +1,23 @@ +## This tests for an expected error when --preserve dates is +## specified at the same time as using stdin or stdout as input or +## output files. + +# RUN: not llvm-objcopy --preserve-dates - %t 2>&1 | FileCheck %s +# RUN: not llvm-objcopy --preserve-dates %p/Inputs/alloc-symtab.o - 2>&1 | FileCheck %s + +## Testing N args. +# RUN: not llvm-strip --preserve-dates - < %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s +# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o - < \ +# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s +# RUN: not llvm-strip --preserve-dates - %p/Inputs/alloc-symtab.o < \ +# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s +# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o - \ +# RUN: %p/Inputs/alloc-symtab.o < %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s + +## Testing -o. +# RUN: not llvm-strip --preserve-dates - -o %p/Inputs/alloc-symtab.o < \ +# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s +# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o -o - < \ +# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s + +# CHECK: error: --preserve-dates requires a file 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 @@ -667,6 +667,11 @@ Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); + if (Config.PreserveDates && + (Config.OutputFilename == "-" || Config.InputFilename == "-")) + return createStringError(errc::invalid_argument, + "--preserve-dates requires a file"); + for (auto Arg : InputArgs) if (Arg->getOption().matches(OBJCOPY_set_start)) { auto EAddr = getAsInteger(Arg->getValue()); @@ -735,7 +740,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,13 +805,18 @@ InputArgs.getLastArgValue(STRIP_output, Positional[0]); DC.CopyConfigs.push_back(std::move(Config)); } else { - for (const char *Filename : Positional) { + for (StringRef Filename : Positional) { Config.InputFilename = Filename; Config.OutputFilename = Filename; DC.CopyConfigs.push_back(Config); } } + if (Config.PreserveDates && (is_contained(Positional, "-") || + InputArgs.getLastArgValue(STRIP_output) == "-")) + return createStringError(errc::invalid_argument, + "--preserve-dates requires a file"); + return std::move(DC); }