Skip to content

Commit 261a651

Browse files
Cody Tapscottvchuravy
authored andcommitted
Do not patch FDE symbols in RuntimeDyld, on targets that use non-absolute symbol relocations in .eh_frame
Since processFDE adds a delta to the values in the FDE, it assumes that the relocations for the .eh_frame section have not been applied by RuntimeDyld. It expects instead that only the relocation addend has been written to the symbol locations, and that the section-to-section offset needs to be added. However, there are platform differences that interfere with this: 1) X86-64 has DwarfFDESymbolsUseAbsDiff enabled in its AsmInfo, causing an absolute symbol to be emitted for the FDE pcStart. Absolute symbols are skipped as a relocation by RuntimeDyld, so the processFDE function in RuntimeDyldMachO.cpp calculates the relocation correctly. 2) AArch64 has DwarfFDESymbolsUseAbsDiff disabled, so a relocation is emitted in the eh_frame section. Since this isn't absolute, the relocation is applied by RuntimeDyld. This means that processFDE ends up adding an additional section-to-section offset to the pcStart field, generating an incorrect FDE Differential Revision: https://reviews.llvm.org/D103052
1 parent cc1d5c9 commit 261a651

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
272272
}
273273

274274
template <typename Impl>
275-
unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(uint8_t *P,
276-
int64_t DeltaForText,
277-
int64_t DeltaForEH) {
275+
unsigned char *RuntimeDyldMachOCRTPBase<Impl>::patchFDERelocations(uint8_t *P,
276+
int64_t DeltaForText,
277+
int64_t DeltaForEH) {
278278
typedef typename Impl::TargetPtrT TargetPtrT;
279279

280280
LLVM_DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
@@ -324,19 +324,24 @@ void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
324324
continue;
325325
SectionEntry *Text = &Sections[SectionInfo.TextSID];
326326
SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
327-
SectionEntry *ExceptTab = nullptr;
328-
if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
329-
ExceptTab = &Sections[SectionInfo.ExceptTabSID];
330-
331-
int64_t DeltaForText = computeDelta(Text, EHFrame);
332-
int64_t DeltaForEH = 0;
333-
if (ExceptTab)
334-
DeltaForEH = computeDelta(ExceptTab, EHFrame);
335-
336-
uint8_t *P = EHFrame->getAddress();
337-
uint8_t *End = P + EHFrame->getSize();
338-
while (P != End) {
339-
P = processFDE(P, DeltaForText, DeltaForEH);
327+
328+
// If the FDE includes absolute symbol relocations (not supported
329+
// by RuntimeDyld), we need to manually patch-up the values
330+
if (doDwarfFDESymbolsUseAbsDiff()) {
331+
SectionEntry *ExceptTab = nullptr;
332+
if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
333+
ExceptTab = &Sections[SectionInfo.ExceptTabSID];
334+
335+
int64_t DeltaForText = computeDelta(Text, EHFrame);
336+
int64_t DeltaForEH = 0;
337+
if (ExceptTab)
338+
DeltaForEH = computeDelta(ExceptTab, EHFrame);
339+
340+
uint8_t *P = EHFrame->getAddress();
341+
uint8_t *End = P + EHFrame->getSize();
342+
while (P != End) {
343+
P = patchFDERelocations(P, DeltaForText, DeltaForEH);
344+
}
340345
}
341346

342347
MemMgr.registerEHFrames(EHFrame->getAddress(), EHFrame->getLoadAddress(),

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class RuntimeDyldMachO : public RuntimeDyldImpl {
4343
SID ExceptTabSID;
4444
};
4545

46+
// Returns true if the FDE section includes absolute symbol relocations
47+
// on this platform.
48+
virtual bool doDwarfFDESymbolsUseAbsDiff() = 0;
49+
4650
// When a module is loaded we save the SectionID of the EH frame section
4751
// in a table until we receive a request to register all unregistered
4852
// EH frame sections with the memory manager.
@@ -147,8 +151,8 @@ class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
147151
Impl &impl() { return static_cast<Impl &>(*this); }
148152
const Impl &impl() const { return static_cast<const Impl &>(*this); }
149153

150-
unsigned char *processFDE(uint8_t *P, int64_t DeltaForText,
151-
int64_t DeltaForEH);
154+
unsigned char *patchFDERelocations(uint8_t *P, int64_t DeltaForText,
155+
int64_t DeltaForEH);
152156

153157
public:
154158
RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr,

llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class RuntimeDyldMachOAArch64
3030

3131
unsigned getStubAlignment() override { return 8; }
3232

33+
bool doDwarfFDESymbolsUseAbsDiff() override { return false; }
34+
3335
/// Extract the addend encoded in the instruction / memory location.
3436
Expected<int64_t> decodeAddend(const RelocationEntry &RE) const {
3537
const SectionEntry &Section = Sections[RE.SectionID];

llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class RuntimeDyldMachOARM
3333

3434
unsigned getStubAlignment() override { return 4; }
3535

36+
bool doDwarfFDESymbolsUseAbsDiff() override { return false; }
37+
3638
Expected<JITSymbolFlags> getJITSymbolFlags(const SymbolRef &SR) override {
3739
auto Flags = RuntimeDyldImpl::getJITSymbolFlags(SR);
3840
if (!Flags)

llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class RuntimeDyldMachOI386
3030

3131
unsigned getStubAlignment() override { return 1; }
3232

33+
bool doDwarfFDESymbolsUseAbsDiff() override { return true; }
34+
3335
Expected<relocation_iterator>
3436
processRelocationRef(unsigned SectionID, relocation_iterator RelI,
3537
const ObjectFile &BaseObjT,

llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class RuntimeDyldMachOX86_64
3030

3131
unsigned getStubAlignment() override { return 8; }
3232

33+
bool doDwarfFDESymbolsUseAbsDiff() override { return true; }
34+
3335
Expected<relocation_iterator>
3436
processRelocationRef(unsigned SectionID, relocation_iterator RelI,
3537
const ObjectFile &BaseObjT,

0 commit comments

Comments
 (0)