COFF: Add /opt:noref option.

This option disables dead-stripping.

llvm-svn: 239243
This commit is contained in:
Rui Ueyama 2015-06-07 03:17:42 +00:00
parent 115d7c1036
commit e2cbfeae5c
4 changed files with 86 additions and 1 deletions

View File

@ -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;

View File

@ -283,6 +283,21 @@ bool LinkerDriver::link(int Argc, const char *Argv[]) {
}
}
// 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";

View File

@ -101,6 +101,8 @@ void OutputSection::writeHeaderTo(uint8_t *Buf) {
// 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;

67
lld/test/COFF/opt.test Normal file
View File

@ -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
...