Skip to content

Commit a823ade

Browse files
committed
[RuntimeDyld][Windows] Allocate space for dllimport things.
We weren't taking account of the space we require in the stubs for things that are `dllimport`ed, and as a result we could hit the assertion failure for running out of stub space. Fix that. rdar://133473673
1 parent 3fffa6d commit a823ade

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,12 @@ unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
690690
if (!(RelSecI == Section))
691691
continue;
692692

693-
for (const RelocationRef &Reloc : SI->relocations())
693+
for (const RelocationRef &Reloc : SI->relocations()) {
694694
if (relocationNeedsStub(Reloc))
695695
StubBufSize += StubSize;
696+
if (relocationNeedsDLLImportStub(Reloc))
697+
StubBufSize = sizeAfterAddingDLLImportStub(StubBufSize);
698+
}
696699
}
697700

698701
// Get section data size and alignment

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCOFF.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,15 @@ bool RuntimeDyldCOFF::isCompatibleFile(const object::ObjectFile &Obj) const {
119119
return Obj.isCOFF();
120120
}
121121

122+
bool RuntimeDyldCOFF::relocationNeedsDLLImportStub(const RelocationRef &R) const {
123+
object::symbol_iterator Symbol = R.getSymbol();
124+
Expected<StringRef> TargetNameOrErr = Symbol->getName();
125+
if (!TargetNameOrErr)
126+
return false;
127+
128+
StringRef TargetName = *TargetNameOrErr;
129+
130+
return TargetName.startswith(getImportSymbolPrefix());
131+
}
132+
122133
} // namespace llvm

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCOFF.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_RUNTIME_DYLD_COFF_H
1515

1616
#include "RuntimeDyldImpl.h"
17+
#include "llvm/Support/MathExtras.h"
1718

1819
#define DEBUG_TYPE "dyld"
1920

@@ -49,6 +50,12 @@ class RuntimeDyldCOFF : public RuntimeDyldImpl {
4950

5051
static constexpr StringRef getImportSymbolPrefix() { return "__imp_"; }
5152

53+
bool relocationNeedsDLLImportStub(const RelocationRef &R) const;
54+
55+
unsigned sizeAfterAddingDLLImportStub(unsigned Size) const {
56+
return alignTo(Size, PointerSize) + PointerSize;
57+
}
58+
5259
private:
5360
unsigned PointerSize;
5461
uint32_t PointerReloc;

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,16 @@ class RuntimeDyldImpl {
455455
return true; // Conservative answer
456456
}
457457

458+
// Return true if the relocation R may require allocating a DLL import stub.
459+
virtual bool relocationNeedsDLLImportStub(const RelocationRef &R) const {
460+
return false;
461+
}
462+
463+
// Add the size of a DLL import stub to the buffer size
464+
virtual unsigned sizeAfterAddingDLLImportStub(unsigned Size) const {
465+
return Size;
466+
}
467+
458468
public:
459469
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
460470
JITSymbolResolver &Resolver)

0 commit comments

Comments
 (0)