Skip to content

Commit 899a307

Browse files
committed
[objcopy] Error when --preserve-dates is specified with standard streams
Summary: llvm-objcopy/strip now error when -p is specified when reading from stdin or writing to stdout Reviewers: jhenderson, rupprecht, espindola, alexshap Reviewed By: jhenderson, rupprecht Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63090 llvm-svn: 363485
1 parent ad6bb86 commit 899a307

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## This tests for an expected error when --preserve dates is
2+
## specified at the same time as using stdin or stdout as input or
3+
## output files.
4+
5+
# RUN: not llvm-objcopy --preserve-dates - %t 2>&1 | FileCheck %s
6+
# RUN: not llvm-objcopy --preserve-dates %p/Inputs/alloc-symtab.o - 2>&1 | FileCheck %s
7+
8+
## Testing N args.
9+
# RUN: not llvm-strip --preserve-dates - < %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
10+
# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o - < \
11+
# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
12+
# RUN: not llvm-strip --preserve-dates - %p/Inputs/alloc-symtab.o < \
13+
# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
14+
# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o - \
15+
# RUN: %p/Inputs/alloc-symtab.o < %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
16+
17+
## Testing -o.
18+
# RUN: not llvm-strip --preserve-dates - -o %p/Inputs/alloc-symtab.o < \
19+
# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
20+
# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o -o - < \
21+
# RUN: %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
22+
23+
# CHECK: error: --preserve-dates requires a file

llvm/tools/llvm-objcopy/CopyConfig.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@ Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
668668

669669
Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
670670

671+
if (Config.PreserveDates &&
672+
(Config.OutputFilename == "-" || Config.InputFilename == "-"))
673+
return createStringError(errc::invalid_argument,
674+
"--preserve-dates requires a file");
675+
671676
for (auto Arg : InputArgs)
672677
if (Arg->getOption().matches(OBJCOPY_set_start)) {
673678
auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
@@ -736,7 +741,7 @@ Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
736741
exit(0);
737742
}
738743

739-
SmallVector<const char *, 2> Positional;
744+
SmallVector<StringRef, 2> Positional;
740745
for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
741746
return createStringError(errc::invalid_argument, "unknown argument '%s'",
742747
Arg->getAsString(InputArgs).c_str());
@@ -801,13 +806,18 @@ Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
801806
InputArgs.getLastArgValue(STRIP_output, Positional[0]);
802807
DC.CopyConfigs.push_back(std::move(Config));
803808
} else {
804-
for (const char *Filename : Positional) {
809+
for (StringRef Filename : Positional) {
805810
Config.InputFilename = Filename;
806811
Config.OutputFilename = Filename;
807812
DC.CopyConfigs.push_back(Config);
808813
}
809814
}
810815

816+
if (Config.PreserveDates && (is_contained(Positional, "-") ||
817+
InputArgs.getLastArgValue(STRIP_output) == "-"))
818+
return createStringError(errc::invalid_argument,
819+
"--preserve-dates requires a file");
820+
811821
return std::move(DC);
812822
}
813823

0 commit comments

Comments
 (0)