[lld] r239243 - COFF: Add /opt:noref option.

Rui Ueyama ruiu at google.com
Sat Jun 6 20:17:42 PDT 2015


Author: ruiu
Date: Sat Jun  6 22:17:42 2015
New Revision: 239243

URL: http://llvm.org/viewvc/llvm-project?rev=239243&view=rev
Log:
COFF: Add /opt:noref option.

This option disables dead-stripping.

Added:
    lld/trunk/test/COFF/opt.test
Modified:
    lld/trunk/COFF/Config.h
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Writer.cpp

Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=239243&r1=239242&r2=239243&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Sat Jun  6 22:17:42 2015
@@ -30,6 +30,7 @@ public:
   WindowsSubsystem Subsystem = llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
   StringRef EntryName;
   std::string OutputFile;
+  bool DoGC = true;
 
   // Symbols in this set are considered as live by the garbage collector.
   std::set<StringRef> GCRoots;

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=239243&r1=239242&r2=239243&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Sat Jun  6 22:17:42 2015
@@ -283,6 +283,21 @@ bool LinkerDriver::link(int Argc, const
     }
   }
 
+  // Handle /opt
+  for (auto *Arg : Args->filtered(OPT_opt)) {
+    std::string S = StringRef(Arg->getValue()).lower();
+    if (S == "noref") {
+      Config->DoGC = false;
+      continue;
+    }
+    if (S != "ref" && S != "icf" && S != "noicf" &&
+        S != "lbr" && S != "nolbr" &&
+        !StringRef(S).startswith("icf=")) {
+      llvm::errs() << "/opt: unknown option: " << S << "\n";
+      return false;
+    }
+  }
+
   // Handle /failifmismatch
   if (auto EC = checkFailIfMismatch(Args.get())) {
     llvm::errs() << "/failifmismatch: " << EC.message() << "\n";

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=239243&r1=239242&r2=239243&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Sat Jun  6 22:17:42 2015
@@ -101,6 +101,8 @@ void OutputSection::writeHeaderTo(uint8_
 // COMDAT chunks will be ignored in the next step, so that they don't
 // come to the final output file.
 void Writer::markLive() {
+  if (!Config->DoGC)
+    return;
   for (StringRef Name : Config->GCRoots)
     cast<Defined>(Symtab->find(Name))->markLive();
   for (Chunk *C : Symtab->getChunks())
@@ -113,7 +115,7 @@ void Writer::createSections() {
   // First, bin chunks by name.
   std::map<StringRef, std::vector<Chunk *>> Map;
   for (Chunk *C : Symtab->getChunks()) {
-    if (!C->isLive()) {
+    if (Config->DoGC && !C->isLive()) {
       if (Config->Verbose)
         C->printDiscardedMessage();
       continue;

Added: lld/trunk/test/COFF/opt.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/opt.test?rev=239243&view=auto
==============================================================================
--- lld/trunk/test/COFF/opt.test (added)
+++ lld/trunk/test/COFF/opt.test Sat Jun  6 22:17:42 2015
@@ -0,0 +1,67 @@
+# RUN: yaml2obj < %s > %t.obj
+
+# RUN: lld -flavor link2 /out:%t.exe %t.obj /verbose >& %t.log
+### FileCheck doesn't like empty input, so write something.
+# RUN: echo dummy >> %t.log
+# RUN: FileCheck -check-prefix=CHECK1 %s < %t.log
+
+# RUN: lld -flavor link2 /out:%t.exe %t.obj /verbose /opt:noref >& %t.log
+# RUN: echo dummy >> %t.log
+# RUN: FileCheck -check-prefix=CHECK2 %s < %t.log
+
+# CHECK1:     Discarded unused
+# CHECK2-NOT: Discarded unused
+
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: []
+sections:
+  - Name:            '.text$mn'
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+  - Name:            '.text$mn'
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+symbols:
+  - Name:            '.text$mn'
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+      Selection:       IMAGE_COMDAT_SELECT_ANY
+  - Name:            '.text$mn'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+      Selection:       IMAGE_COMDAT_SELECT_ANY
+  - Name:            mainCRTStartup
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            unused
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...





More information about the llvm-commits mailing list