[lld] r239005 - COFF: /include'd symbols must be preserved.

Rui Ueyama ruiu at google.com
Wed Jun 3 21:10:27 PDT 2015


It doesn't support DLL yet.

On Wed, Jun 3, 2015 at 8:40 PM, David Majnemer <david.majnemer at gmail.com>
wrote:

> Do you already do the same for exported symbols?
>
>
> On Wednesday, June 3, 2015, Rui Ueyama <ruiu at google.com> wrote:
>
>> Author: ruiu
>> Date: Wed Jun  3 21:12:16 2015
>> New Revision: 239005
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=239005&view=rev
>> Log:
>> COFF: /include'd symbols must be preserved.
>>
>> Not only entry point symbol but also symbols specified by /include
>> option must be preserved, as they will never be dead-stripped.
>>
>> http://reviews.llvm.org/D10220
>>
>> Modified:
>>     lld/trunk/COFF/Config.h
>>     lld/trunk/COFF/Driver.cpp
>>     lld/trunk/COFF/SymbolTable.cpp
>>     lld/trunk/COFF/Writer.cpp
>>     lld/trunk/COFF/Writer.h
>>     lld/trunk/test/COFF/lto.ll
>>
>> Modified: lld/trunk/COFF/Config.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=239005&r1=239004&r2=239005&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/COFF/Config.h (original)
>> +++ lld/trunk/COFF/Config.h Wed Jun  3 21:12:16 2015
>> @@ -27,7 +27,10 @@ public:
>>    llvm::COFF::MachineTypes MachineType =
>> llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
>>    bool Verbose = false;
>>    WindowsSubsystem Subsystem = llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
>> -  std::string EntryName;
>> +  StringRef EntryName;
>> +
>> +  // Symbols in this set are considered as live by the garbage collector.
>> +  std::set<StringRef> GCRoots;
>>
>>    std::set<StringRef> NoDefaultLibs;
>>    bool NoDefaultLibAll = false;
>>
>> Modified: lld/trunk/COFF/Driver.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=239005&r1=239004&r2=239005&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/COFF/Driver.cpp (original)
>> +++ lld/trunk/COFF/Driver.cpp Wed Jun  3 21:12:16 2015
>> @@ -312,8 +312,11 @@ bool LinkerDriver::link(int Argc, const
>>
>>    // Add undefined symbols given via the command line.
>>    // (/include is equivalent to Unix linker's -u option.)
>> -  for (auto *Arg : Args->filtered(OPT_incl))
>> -    Symtab.addUndefined(Arg->getValue());
>> +  for (auto *Arg : Args->filtered(OPT_incl)) {
>> +    StringRef Sym = Arg->getValue();
>> +    Symtab.addUndefined(Sym);
>> +    Config->GCRoots.insert(Sym);
>> +  }
>>
>>    // Parse all input files and put all symbols to the symbol table.
>>    // The symbol table will take care of name resolution.
>> @@ -362,11 +365,14 @@ bool LinkerDriver::link(int Argc, const
>>      }
>>      Config->EntryName = EntryOrErr.get();
>>    }
>> +  Config->GCRoots.insert(Config->EntryName);
>>
>>    // Make sure we have resolved all symbols.
>>    if (Symtab.reportRemainingUndefines())
>>      return false;
>>
>> +  // Do LTO by compiling bitcode input files to a native COFF file
>> +  // then link that file.
>>    if (auto EC = Symtab.addCombinedLTOObject()) {
>>      llvm::errs() << EC.message() << "\n";
>>      return false;
>>
>> Modified: lld/trunk/COFF/SymbolTable.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/SymbolTable.cpp?rev=239005&r1=239004&r2=239005&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/COFF/SymbolTable.cpp (original)
>> +++ lld/trunk/COFF/SymbolTable.cpp Wed Jun  3 21:12:16 2015
>> @@ -227,20 +227,17 @@ std::error_code SymbolTable::addCombined
>>      return std::error_code();
>>
>>    llvm::LTOCodeGenerator CG;
>> -  std::set<DefinedBitcode *> PreservedBitcodeSymbols;
>>
>>    // All symbols referenced by non-bitcode objects must be preserved.
>>    for (std::unique_ptr<ObjectFile> &File : ObjectFiles)
>>      for (SymbolBody *Body : File->getSymbols())
>>        if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
>> -        PreservedBitcodeSymbols.insert(S);
>> +        CG.addMustPreserveSymbol(S->getName());
>>
>> -  // Likewise for the linker-generated reference to the entry point.
>> -  if (auto *S =
>> dyn_cast<DefinedBitcode>(Symtab[Config->EntryName]->Body))
>> -    PreservedBitcodeSymbols.insert(S);
>> -
>> -  for (DefinedBitcode *S : PreservedBitcodeSymbols)
>> -    CG.addMustPreserveSymbol(S->getName());
>> +  // Likewise for other symbols that must be preserved.
>> +  for (StringRef Name : Config->GCRoots)
>> +    if (isa<DefinedBitcode>(Symtab[Name]->Body))
>> +      CG.addMustPreserveSymbol(Name);
>>
>>    CG.setModule(BitcodeFiles[0]->releaseModule());
>>    for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
>>
>> Modified: lld/trunk/COFF/Writer.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=239005&r1=239004&r2=239005&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/COFF/Writer.cpp (original)
>> +++ lld/trunk/COFF/Writer.cpp Wed Jun  3 21:12:16 2015
>> @@ -87,8 +87,8 @@ void OutputSection::writeHeader(uint8_t
>>  }
>>
>>  void Writer::markLive() {
>> -  Entry = cast<Defined>(Symtab->find(Config->EntryName));
>> -  Entry->markLive();
>> +  for (StringRef Name : Config->GCRoots)
>> +    cast<Defined>(Symtab->find(Name))->markLive();
>>    for (Chunk *C : Symtab->getChunks())
>>      if (C->isRoot())
>>        C->markLive();
>> @@ -291,6 +291,7 @@ void Writer::writeHeader() {
>>    PE->Subsystem = Config->Subsystem;
>>    PE->SizeOfImage = SizeOfImage;
>>    PE->SizeOfHeaders = SizeOfHeaders;
>> +  Defined *Entry = cast<Defined>(Symtab->find(Config->EntryName));
>>    PE->AddressOfEntryPoint = Entry->getRVA();
>>    PE->SizeOfStackReserve = Config->StackReserve;
>>    PE->SizeOfStackCommit = Config->StackCommit;
>>
>> Modified: lld/trunk/COFF/Writer.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.h?rev=239005&r1=239004&r2=239005&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/COFF/Writer.h (original)
>> +++ lld/trunk/COFF/Writer.h Wed Jun  3 21:12:16 2015
>> @@ -98,7 +98,6 @@ private:
>>    uint32_t ImportDirectoryTableSize = 0;
>>    uint32_t ImportAddressTableSize = 0;
>>
>> -  Defined *Entry;
>>    uint64_t FileSize;
>>    uint64_t SizeOfImage;
>>    uint64_t SizeOfHeaders;
>>
>> Modified: lld/trunk/test/COFF/lto.ll
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/lto.ll?rev=239005&r1=239004&r2=239005&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/test/COFF/lto.ll (original)
>> +++ lld/trunk/test/COFF/lto.ll Wed Jun  3 21:12:16 2015
>> @@ -8,10 +8,10 @@
>>  ; RUN: rm -f %T/foo.lib
>>  ; RUN: llvm-ar cru %T/foo.lib %T/foo.obj
>>
>> -; RUN: lld -flavor link2 /out:%T/main.exe /entry:main /subsystem:console
>> %T/main.lto.obj %T/foo.lto.obj
>> +; RUN: lld -flavor link2 /out:%T/main.exe /entry:main /include:f2
>> /subsystem:console %T/main.lto.obj %T/foo.lto.obj
>>  ; RUN: llvm-readobj -file-headers %T/main.exe | FileCheck
>> -check-prefix=HEADERS-11 %s
>>  ; RUN: llvm-objdump -d %T/main.exe | FileCheck -check-prefix=TEXT-11 %s
>> -; RUN: lld -flavor link2 /out:%T/main.exe /entry:main /subsystem:console
>> %T/main.lto.obj %T/foo.lto.lib
>> +; RUN: lld -flavor link2 /out:%T/main.exe /entry:main /include:f2
>> /subsystem:console %T/main.lto.obj %T/foo.lto.lib
>>  ; RUN: llvm-readobj -file-headers %T/main.exe | FileCheck
>> -check-prefix=HEADERS-11 %s
>>  ; RUN: llvm-objdump -d %T/main.exe | FileCheck -check-prefix=TEXT-11 %s
>>
>> @@ -34,6 +34,21 @@
>>  ; TEXT-11-NEXT: .text:
>>  ; TEXT-11-NEXT: xorl   %eax, %eax
>>  ; TEXT-11-NEXT: retq
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: int3
>> +; TEXT-11-NEXT: movl   $2, %eax
>> +; TEXT-11-NEXT: retq
>>
>>  ; HEADERS-01: AddressOfEntryPoint: 0x1000
>>  ; TEXT-01: Disassembly of section .text:
>> @@ -79,3 +94,13 @@ define i32 @main() {
>>  }
>>
>>  declare void @foo()
>> +
>> +$f1 = comdat any
>> +define i32 @f1() comdat($f1) {
>> +  ret i32 1
>> +}
>> +
>> +$f2 = comdat any
>> +define i32 @f2() comdat($f2) {
>> +  ret i32 2
>> +}
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150603/8f7de1da/attachment.html>


More information about the llvm-commits mailing list