[lld] r196743 - [PECOFF] Add /alternatename option parser.

Rui Ueyama ruiu at google.com
Sun Dec 8 17:47:32 PST 2013


Author: ruiu
Date: Sun Dec  8 19:47:32 2013
New Revision: 196743

URL: http://llvm.org/viewvc/llvm-project?rev=196743&view=rev
Log:
[PECOFF] Add /alternatename option parser.

/ALTERNATENAME is a rarely-used, undocumented command line option that is
needed to link LLD for release build. It seems that the option is for defining
an weak alias; /alternatename:foo=bar defines weak symbol "foo" for "bar".
If "foo" is defined in an input file, it'll be linked normally and the command
line option will have no effect. If it's not defined, "foo" will be handled
as an alias for "bar".

This patch implements the parser for the option. The actual weak alias handling
will be implemented in a separate patch.

Modified:
    lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
    lld/trunk/lib/Driver/WinLinkDriver.cpp
    lld/trunk/lib/Driver/WinLinkOptions.td
    lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
    lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp

Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=196743&r1=196742&r2=196743&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Sun Dec  8 19:47:32 2013
@@ -198,6 +198,9 @@ public:
   bool addSectionRenaming(raw_ostream &diagnostics,
                           StringRef from, StringRef to);
 
+  StringRef getAlternateName(StringRef def) const;
+  void setAlternateName(StringRef def, StringRef weak);
+
   void addNoDefaultLib(StringRef path) { _noDefaultLibs.insert(path); }
   bool hasNoDefaultLib(StringRef path) const {
     return _noDefaultLibs.count(path) == 1;
@@ -282,6 +285,9 @@ private:
   std::unique_ptr<Reader> _reader;
   std::unique_ptr<Writer> _writer;
 
+  // A map for weak aliases.
+  std::map<std::string, std::string> _alternateNames;
+
   // A map for section renaming. For example, if there is an entry in the map
   // whose value is .rdata -> .text, the section contens of .rdata will be
   // merged to .text in the resulting executable.

Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=196743&r1=196742&r2=196743&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Sun Dec  8 19:47:32 2013
@@ -99,6 +99,18 @@ std::vector<StringRef> splitPathList(Str
   return ret;
 }
 
+// Parse an argument for /alternatename. The expected string is
+// "<string>=<string>".
+bool parseAlternateName(StringRef arg, StringRef &weak, StringRef &def,
+                        raw_ostream &diagnostics) {
+  llvm::tie(weak, def) = arg.split('=');
+  if (weak.empty() || def.empty()) {
+    diagnostics << "Error: malformed /alternatename option: " << arg << "\n";
+    return false;
+  }
+  return true;
+}
+
 // Parse an argument for /base, /stack or /heap. The expected string
 // is "<integer>[,<integer>]".
 bool parseMemoryOption(StringRef arg, uint64_t &reserve, uint64_t &commit) {
@@ -646,6 +658,14 @@ WinLinkDriver::parse(int argc, const cha
       ctx.appendLLVMOption(inputArg->getValue());
       break;
 
+    case OPT_alternatename: {
+      StringRef weak, def;
+      if (!parseAlternateName(inputArg->getValue(), weak, def, diagnostics))
+        return false;
+      ctx.setAlternateName(weak, def);
+      break;
+    }
+
     case OPT_base:
       // Parse /base command line option. The argument for the parameter is in
       // the form of "<address>[:<size>]".

Modified: lld/trunk/lib/Driver/WinLinkOptions.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkOptions.td?rev=196743&r1=196742&r2=196743&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkOptions.td (original)
+++ lld/trunk/lib/Driver/WinLinkOptions.td Sun Dec  8 19:47:32 2013
@@ -15,6 +15,7 @@ multiclass B<string name, string help> {
   def _no : F<name#":no">, HelpText<help>;
 }
 
+def alternatename : P<"alternatename", "Define weak alias">;
 def base    : P<"base", "Base address of the program">;
 def defaultlib : P<"defaultlib", "Add the library to the list of input files">;
 def nodefaultlib : P<"nodefaultlib", "Remove a default library">;

Modified: lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=196743&r1=196742&r2=196743&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp Sun Dec  8 19:47:32 2013
@@ -155,6 +155,17 @@ bool PECOFFLinkingContext::addSectionRen
   return true;
 }
 
+StringRef PECOFFLinkingContext::getAlternateName(StringRef def) const {
+  auto it = _alternateNames.find(def);
+  if (it == _alternateNames.end())
+    return "";
+  return it->second;
+}
+
+void PECOFFLinkingContext::setAlternateName(StringRef weak, StringRef def) {
+  _alternateNames[def] = weak;
+}
+
 /// Try to find the input library file from the search paths and append it to
 /// the input file list. Returns true if the library file is found.
 StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {

Modified: lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp?rev=196743&r1=196742&r2=196743&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp Sun Dec  8 19:47:32 2013
@@ -149,6 +149,12 @@ TEST_F(WinLinkParserTest, InputOrder) {
 // Tests for command line options that take values.
 //
 
+TEST_F(WinLinkParserTest, AlternateName) {
+  EXPECT_TRUE(parse("link.exe", "/alternatename:sym1=sym2", "a.out", nullptr));
+  EXPECT_EQ("sym1", _context.getAlternateName("sym2"));
+  EXPECT_EQ("", _context.getAlternateName("foo"));
+}
+
 TEST_F(WinLinkParserTest, MachineX86) {
   EXPECT_TRUE(parse("link.exe", "/machine:x86", "a.obj", nullptr));
   EXPECT_EQ(llvm::COFF::IMAGE_FILE_MACHINE_I386, _context.getMachineType());





More information about the llvm-commits mailing list