Skip to content

Commit 1a8c687

Browse files
committed
[llvm-strip] Error when using stdin twice
Summary: Implements bug [[ https://bugs.llvm.org/show_bug.cgi?id=42204 | 42204 ]]. llvm-strip now warns when the same input file is used more than once, and errors when stdin is used more than once. Reviewers: jhenderson, rupprecht, espindola, alexshap Reviewed By: jhenderson, rupprecht Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63122 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363638 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e14caa7 commit 1a8c687

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Test llvm-strip using the same input file more than once.
2+
## When using stdin ('-') more than once llvm-strip should give an error
3+
## while a file more than once should be simply a warning.
4+
5+
# RUN: yaml2obj %s -o %t
6+
7+
# RUN: not llvm-strip - - < %t 2>&1 | FileCheck -check-prefix=ERR %s
8+
# RUN: not llvm-strip - %t - < %t 2>&1 | FileCheck -check-prefix=ERR %s
9+
10+
# ERR: error: cannot specify '-' as an input file more than once
11+
12+
# RUN: llvm-strip %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t
13+
# RUN: llvm-strip %t %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t
14+
15+
# WARN: warning: '[[FILE]]' was already specified
16+
17+
--- !ELF
18+
FileHeader:
19+
Class: ELFCLASS64
20+
Data: ELFDATA2LSB
21+
Type: ET_REL
22+
Machine: EM_X86_64
23+
Sections:
24+
- Name: .alloc
25+
Type: SHT_PROGBITS
26+
Flags: [ SHF_ALLOC ]

tools/llvm-objcopy/CopyConfig.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/ADT/Optional.h"
1212
#include "llvm/ADT/SmallVector.h"
1313
#include "llvm/ADT/StringRef.h"
14+
#include "llvm/ADT/StringSet.h"
1415
#include "llvm/Option/Arg.h"
1516
#include "llvm/Option/ArgList.h"
1617
#include "llvm/Support/CommandLine.h"
@@ -722,7 +723,9 @@ Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
722723
// ParseStripOptions returns the config and sets the input arguments. If a
723724
// help flag is set then ParseStripOptions will print the help messege and
724725
// exit.
725-
Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
726+
Expected<DriverConfig>
727+
parseStripOptions(ArrayRef<const char *> ArgsArr,
728+
std::function<Error(Error)> ErrorCallback) {
726729
StripOptTable T;
727730
unsigned MissingArgumentIndex, MissingArgumentCount;
728731
llvm::opt::InputArgList InputArgs =
@@ -809,7 +812,18 @@ Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
809812
InputArgs.getLastArgValue(STRIP_output, Positional[0]);
810813
DC.CopyConfigs.push_back(std::move(Config));
811814
} else {
815+
StringMap<unsigned> InputFiles;
812816
for (StringRef Filename : Positional) {
817+
if (InputFiles[Filename]++ == 1) {
818+
if (Filename == "-")
819+
return createStringError(
820+
errc::invalid_argument,
821+
"cannot specify '-' as an input file more than once");
822+
if (Error E = ErrorCallback(createStringError(
823+
errc::invalid_argument, "'%s' was already specified",
824+
Filename.str().c_str())))
825+
return std::move(E);
826+
}
813827
Config.InputFilename = Filename;
814828
Config.OutputFilename = Filename;
815829
DC.CopyConfigs.push_back(Config);

tools/llvm-objcopy/CopyConfig.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,11 @@ Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
188188

189189
// ParseStripOptions returns the config and sets the input arguments. If a
190190
// help flag is set then ParseStripOptions will print the help messege and
191-
// exit.
192-
Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
191+
// exit. ErrorCallback is used to handle recoverable errors. An Error returned
192+
// by the callback aborts the parsing and is then returned by this function.
193+
Expected<DriverConfig>
194+
parseStripOptions(ArrayRef<const char *> ArgsArr,
195+
std::function<Error(Error)> ErrorCallback);
193196

194197
} // namespace objcopy
195198
} // namespace llvm

tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
8282
exit(1);
8383
}
8484

85+
ErrorSuccess reportWarning(Error E) {
86+
assert(E);
87+
WithColor::warning(errs(), ToolName) << toString(std::move(E));
88+
return Error::success();
89+
}
90+
8591
} // end namespace objcopy
8692
} // end namespace llvm
8793

@@ -263,7 +269,7 @@ int main(int argc, char **argv) {
263269
ToolName = argv[0];
264270
bool IsStrip = sys::path::stem(ToolName).contains("strip");
265271
Expected<DriverConfig> DriverConfig =
266-
IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc))
272+
IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc), reportWarning)
267273
: parseObjcopyOptions(makeArrayRef(argv + 1, argc));
268274
if (!DriverConfig) {
269275
logAllUnhandledErrors(DriverConfig.takeError(),

0 commit comments

Comments
 (0)