#ifndef LLD_COFF_INPUT_FILES_H
#define LLD_COFF_INPUT_FILES_H
+#include "Config.h"
#include "lld/Core/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
// for details about the format.
class ImportFile : public InputFile {
public:
- explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
+ explicit ImportFile(MemoryBufferRef M)
+ : InputFile(ImportKind, M), Live(!Config->DoGC) {}
+
static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
DefinedImportData *ImpSym = nullptr;
StringRef ExternalName;
const coff_import_header *Hdr;
Chunk *Location = nullptr;
+
+ // We want to eliminate dllimported symbols if no one actually refers them.
+ // This "Live" bit is used to keep track of which import library members
+ // are actually in use.
+ //
+ // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
+ // symbols provided by this import library member.
+ bool Live;
};
// Used for LTO.
Worklist.push_back(C);
};
+ auto AddSym = [&](SymbolBody *B) {
+ if (auto *Sym = dyn_cast<DefinedRegular>(B))
+ Enqueue(Sym->getChunk());
+ else if (auto *Sym = dyn_cast<DefinedImportData>(B))
+ Sym->File->Live = true;
+ else if (auto *Sym = dyn_cast<DefinedImportThunk>(B))
+ Sym->WrappedSym->File->Live = true;
+ };
+
// Add GC root chunks.
for (SymbolBody *B : Config->GCRoot)
- if (auto *D = dyn_cast<DefinedRegular>(B))
- Enqueue(D->getChunk());
+ AddSym(B);
while (!Worklist.empty()) {
SectionChunk *SC = Worklist.pop_back_val();
assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
// Mark all symbols listed in the relocation table for this section.
- for (SymbolBody *S : SC->symbols())
- if (auto *D = dyn_cast<DefinedRegular>(S))
- Enqueue(D->getChunk());
+ for (SymbolBody *B : SC->symbols())
+ AddSym(B);
// Mark associative sections if any.
for (SectionChunk *C : SC->children())
DefinedImportThunk::DefinedImportThunk(StringRef Name, DefinedImportData *S,
uint16_t Machine)
- : Defined(DefinedImportThunkKind, Name),
+ : Defined(DefinedImportThunkKind, Name), WrappedSym(S),
Data(makeImportThunk(S, Machine)) {}
Defined *Undefined::getWeakAlias() {
void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
-private:
ImportFile *File;
};
uint64_t getRVA() { return Data->getRVA(); }
Chunk *getChunk() { return Data; }
+ DefinedImportData *WrappedSym;
+
private:
Chunk *Data;
};
// the same order as in the command line. (That affects DLL
// initialization order, and this ordering is MSVC-compatible.)
for (ImportFile *File : Symtab->ImportFiles) {
+ if (!File->Live)
+ continue;
+
std::string DLL = StringRef(File->DLLName).lower();
if (Config->DLLOrder.count(DLL) == 0)
Config->DLLOrder[DLL] = Config->DLLOrder.size();
OutputSection *Text = createSection(".text");
for (ImportFile *File : Symtab->ImportFiles) {
+ if (!File->Live)
+ continue;
+
if (DefinedImportThunk *Thunk = File->ThunkSym)
Text->addChunk(Thunk->getChunk());
+
if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
DelayIdata.add(File->ImpSym);
} else {
Idata.add(File->ImpSym);
}
}
+
if (!Idata.empty()) {
OutputSection *Sec = createSection(".idata");
for (Chunk *C : Idata.getChunks())
Sec->addChunk(C);
}
+
if (!DelayIdata.empty()) {
Defined *Helper = cast<Defined>(Config->DelayLoadHelper);
DelayIdata.create(Helper);
if (!D->getChunk()->isLive())
return None;
+ if (auto *Sym = dyn_cast<DefinedImportData>(Def))
+ if (!Sym->File->Live)
+ return None;
+
+ if (auto *Sym = dyn_cast<DefinedImportThunk>(Def))
+ if (!Sym->WrappedSym->File->Live)
+ return None;
+
coff_symbol16 Sym;
StringRef Name = Def->getName();
if (Name.size() > COFF::NameSize) {
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: 0000000000000000
+ Relocations:
+ - VirtualAddress: 0
+ SymbolName: exportfn1
+ Type: IMAGE_REL_AMD64_ADDR32NB
+ - VirtualAddress: 4
+ SymbolName: exportfn2
+ Type: IMAGE_REL_AMD64_ADDR32NB
symbols:
- Name: .text
Value: 0
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 8
- NumberOfRelocations: 0
+ NumberOfRelocations: 2
NumberOfLinenumbers: 0
CheckSum: 0
Number: 0
--- /dev/null
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_UNKNOWN
+ Characteristics: [ ]
+sections:
+ - Name: .drectve
+ Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
+ Alignment: 1
+ SectionData: ''
+symbols:
+ - Name: exportfn1
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: exportfn1_alias
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL
+ WeakExternal:
+ TagIndex: 0
+ Characteristics: IMAGE_WEAK_EXTERN_SEARCH_ALIAS
+...
--- /dev/null
+# REQUIRES: winres
+
+# RUN: yaml2obj < %p/Inputs/export.yaml > %t-lib.obj
+# RUN: lld-link /out:%t.dll /dll %t-lib.obj /implib:%t.lib /export:exportfn1
+
+# RUN: yaml2obj < %p/Inputs/oldname.yaml > %t-oldname.obj
+
+# RUN: yaml2obj < %s > %t.obj
+
+# RUN: lld-link /out:%t1.exe /entry:main %t.obj %t-oldname.obj %t.lib
+# RUN: llvm-readobj -coff-imports %t1.exe | FileCheck -check-prefix=REF %s
+# REF-NOT: Symbol: exportfn1
+
+# RUN: lld-link /out:%t2.exe /entry:main %t.obj %t-oldname.obj %t.lib /opt:noref
+# RUN: llvm-readobj -coff-imports %t2.exe | FileCheck -check-prefix=NOREF %s
+# NOREF: Symbol: exportfn1
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: []
+sections:
+ - Name: .text
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ Alignment: 4
+ SectionData: 0000000000000000
+symbols:
+ - Name: .text
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 8
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 0
+ - Name: main
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_FUNCTION
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: exportfn1
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: exportfn1_alias
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+...