[lld] r189897 - [PECOFF] Ignore options starting with -?
Rui Ueyama
ruiu at google.com
Tue Sep 3 17:51:51 PDT 2013
Author: ruiu
Date: Tue Sep 3 19:51:51 2013
New Revision: 189897
URL: http://llvm.org/viewvc/llvm-project?rev=189897&view=rev
Log:
[PECOFF] Ignore options starting with -?
The compiler is allowed to add a linker option starting with -?<name> to
.drectve section. If the linker can interpret -<name>, it's processed as if
there's no question mark there. If not, such option is silently ignored.
This is a COFF's feature to allow the compiler to emit new linker options
while keeping compatibility with older linkers.
Modified:
lld/trunk/include/lld/Driver/Driver.h
lld/trunk/lib/Driver/WinLinkDriver.cpp
lld/trunk/lib/Driver/WinLinkOptions.td
lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
lld/trunk/test/pecoff/Inputs/drectve.obj.yaml
lld/trunk/test/pecoff/drectve.test
Modified: lld/trunk/include/lld/Driver/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Driver/Driver.h?rev=189897&r1=189896&r2=189897&view=diff
==============================================================================
--- lld/trunk/include/lld/Driver/Driver.h (original)
+++ lld/trunk/include/lld/Driver/Driver.h Tue Sep 3 19:51:51 2013
@@ -108,7 +108,8 @@ public:
/// Uses Windows style link command line options to fill in options struct.
/// Returns true iff there was an error.
static bool parse(int argc, const char *argv[], PECOFFLinkingContext &info,
- raw_ostream &diagnostics = llvm::errs());
+ raw_ostream &diagnostics = llvm::errs(),
+ bool isDirective = false);
private:
WinLinkDriver() LLVM_DELETED_FUNCTION;
Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=189897&r1=189896&r2=189897&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Tue Sep 3 19:51:51 2013
@@ -183,14 +183,16 @@ void setDefaultEntrySymbolName(PECOFFLin
// Parses the given command line options and returns the result. Returns NULL if
// there's an error in the options.
-std::unique_ptr<llvm::opt::InputArgList> parseArgs(int argc, const char *argv[],
- raw_ostream &diagnostics) {
+std::unique_ptr<llvm::opt::InputArgList>
+parseArgs(int argc, const char *argv[], raw_ostream &diagnostics,
+ bool isDirective) {
// Parse command line options using WinLinkOptions.td
std::unique_ptr<llvm::opt::InputArgList> parsedArgs;
WinLinkOptTable table;
unsigned missingIndex;
unsigned missingCount;
- parsedArgs.reset(table.ParseArgs(&argv[1], &argv[argc], missingIndex, missingCount));
+ parsedArgs.reset(table.ParseArgs(&argv[1], &argv[argc],
+ missingIndex, missingCount));
if (missingCount) {
diagnostics << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
@@ -198,13 +200,16 @@ std::unique_ptr<llvm::opt::InputArgList>
return nullptr;
}
- // Show warning for unknown arguments
+ // Show warning for unknown arguments. In .drectve section, unknown options
+ // starting with "-?" are silently ignored. This is a COFF's feature to embed a
+ // new linker option to an object file while keeping backward compatibility.
for (auto it = parsedArgs->filtered_begin(OPT_UNKNOWN),
ie = parsedArgs->filtered_end(); it != ie; ++it) {
- diagnostics << "warning: ignoring unknown argument: "
- << (*it)->getAsString(*parsedArgs) << "\n";
+ StringRef arg = (*it)->getAsString(*parsedArgs);
+ if (isDirective && arg.startswith("-?"))
+ continue;
+ diagnostics << "warning: ignoring unknown argument: " << arg << "\n";
}
-
return parsedArgs;
}
@@ -244,12 +249,12 @@ bool WinLinkDriver::linkPECOFF(int argc,
return link(context, diagnostics);
}
-bool WinLinkDriver::parse(int argc, const char *argv[],
- PECOFFLinkingContext &ctx, raw_ostream &diagnostics) {
+bool WinLinkDriver::parse(int argc, const char *argv[], PECOFFLinkingContext &ctx,
+ raw_ostream &diagnostics, bool isDirective) {
std::map<StringRef, StringRef> failIfMismatchMap;
// Parse the options.
std::unique_ptr<llvm::opt::InputArgList> parsedArgs = parseArgs(
- argc, argv, diagnostics);
+ argc, argv, diagnostics, isDirective);
if (!parsedArgs)
return true;
Modified: lld/trunk/lib/Driver/WinLinkOptions.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkOptions.td?rev=189897&r1=189896&r2=189897&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkOptions.td (original)
+++ lld/trunk/lib/Driver/WinLinkOptions.td Tue Sep 3 19:51:51 2013
@@ -2,11 +2,11 @@ include "llvm/Option/OptParser.td"
// link.exe accepts options starting with either a dash or a slash.
-class F<string name> : Flag<["/", "-"], name>;
+class F<string name> : Flag<["/", "-", "-?"], name>;
multiclass P<string name, string help> {
- def "" : Joined<["/", "-"], name#":">, HelpText<help>;
- def _c : Separate<["/", "-"], name>, Alias<!cast<Option>(name)>;
+ def "" : Joined<["/", "-", "-?"], name#":">, HelpText<help>;
+ def _c : Separate<["/", "-", "-?"], name>, Alias<!cast<Option>(name)>;
}
defm base : P<"base", "Base address of the program">;
Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=189897&r1=189896&r2=189897&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Tue Sep 3 19:51:51 2013
@@ -696,7 +696,7 @@ private:
std::string errorMessage;
llvm::raw_string_ostream stream(errorMessage);
bool parseFailed = WinLinkDriver::parse(
- argc, argv, _PECOFFLinkingContext, stream);
+ argc, argv, _PECOFFLinkingContext, stream, /*isDirective*/true);
stream.flush();
// Print error message if error.
Modified: lld/trunk/test/pecoff/Inputs/drectve.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/drectve.obj.yaml?rev=189897&r1=189896&r2=189897&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/drectve.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/drectve.obj.yaml Tue Sep 3 19:51:51 2013
@@ -14,7 +14,7 @@ sections:
- Name: .drectve
Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
Alignment: 2147483648
- SectionData: 2f73756273797374656d3a636f6e736f6c652c34322e31393500
+ SectionData: 2f73756273797374656d3a636f6e736f6c652c34322e313935202d3f666f6f00
symbols:
- Name: .text
Value: 0
Modified: lld/trunk/test/pecoff/drectve.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/drectve.test?rev=189897&r1=189896&r2=189897&view=diff
==============================================================================
--- lld/trunk/test/pecoff/drectve.test (original)
+++ lld/trunk/test/pecoff/drectve.test Tue Sep 3 19:51:51 2013
@@ -1,11 +1,15 @@
# Test if the linker can properly parse the .drectve section contents.
-# "drectve.obj" contains "/subsystem:console,42.195" in its .drectve
+# "drectve.obj" contains "/subsystem:console,42.195 -?foo" in its .drectve
# section.
# RUN: yaml2obj %p/Inputs/drectve.obj.yaml > %t.obj
#
-# RUN: lld -flavor link /out:%t1 /entry:_start -- %t.obj \
-# RUN: && llvm-readobj -file-headers %t1 | FileCheck %s
+# RUN: lld -flavor link /out:%t.exe /entry:_start -- %t.obj >& %t.log
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=HEADER %s
+# RUN: echo >> %t.log
+# RUN: FileCheck -check-prefix=ERROR %s < %t.log
-CHECK: MajorOperatingSystemVersion: 42
-CHECK: MinorOperatingSystemVersion: 195
+HEADER: MajorOperatingSystemVersion: 42
+HEADER: MinorOperatingSystemVersion: 195
+
+ERROR-NOT: foo
More information about the llvm-commits
mailing list