From 14a22a442d8aa6bee8c828872b82b7dd722dc7b8 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 9 Aug 2017 20:19:27 +0000 Subject: [RuntimeDyld][ORC] Add support for Thumb mode to RuntimeDyldMachOARM. This patch adds support for thumb relocations to RuntimeDyldMachOARM, and adds a target-specific flags field to JITSymbolFlags (so that on ARM we can record whether each symbol is Thumb-mode code). RuntimeDyldImpl::emitSection is modified to ensure that stubs memory is correctly aligned based on the size returned by getStubAlignment(). llvm-svn: 310517 --- llvm/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp | 8 ++ .../ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp | 40 ++++-- .../ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h | 38 ++++-- .../RuntimeDyld/RuntimeDyldMachO.cpp | 4 +- .../ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h | 3 +- .../RuntimeDyld/Targets/RuntimeDyldMachOARM.h | 142 ++++++++++++++++++--- 6 files changed, 200 insertions(+), 35 deletions(-) (limited to 'llvm/lib/ExecutionEngine/RuntimeDyld') diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp index 8769dcf7374..87059ef2b88 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/JITSymbol.cpp @@ -39,3 +39,11 @@ llvm::JITSymbolFlags::fromObjectSymbol(const object::BasicSymbolRef &Symbol) { Flags |= JITSymbolFlags::Exported; return Flags; } + +ARMJITSymbolFlags llvm::ARMJITSymbolFlags::fromObjectSymbol( + const object::BasicSymbolRef &Symbol) { + ARMJITSymbolFlags Flags; + if (Symbol.getFlags() & object::BasicSymbolRef::SF_Thumb) + Flags |= ARMJITSymbolFlags::Thumb; + return Flags; +} diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 8198836f7a0..4d1d74cf34a 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -233,7 +233,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { return NameOrErr.takeError(); // Compute JIT symbol flags. - JITSymbolFlags JITSymFlags = JITSymbolFlags::fromObjectSymbol(*I); + JITSymbolFlags JITSymFlags = getJITSymbolFlags(*I); // If this is a weak definition, check to see if there's a strong one. // If there is, skip this symbol (we won't be providing it: the strong @@ -616,6 +616,10 @@ void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst, } } +JITSymbolFlags RuntimeDyldImpl::getJITSymbolFlags(const BasicSymbolRef &SR) { + return JITSymbolFlags::fromObjectSymbol(SR); +} + Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols) { if (CommonSymbols.empty()) @@ -685,7 +689,7 @@ Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj, Addr += AlignOffset; Offset += AlignOffset; } - JITSymbolFlags JITSymFlags = JITSymbolFlags::fromObjectSymbol(Sym); + JITSymbolFlags JITSymFlags = getJITSymbolFlags(Sym); DEBUG(dbgs() << "Allocating common symbol " << Name << " address " << format("%p", Addr) << "\n"); GlobalSymbolTable[Name] = @@ -746,8 +750,11 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj, // Code section alignment needs to be at least as high as stub alignment or // padding calculations may by incorrect when the section is remapped to a // higher alignment. - if (IsCode) + if (IsCode) { Alignment = std::max(Alignment, getStubAlignment()); + if (StubBufSize > 0) + PaddingSize += getStubAlignment() - 1; + } // Some sections, such as debug info, don't need to be loaded for execution. // Process those only if explicitly requested. @@ -771,8 +778,13 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj, // Fill in any extra bytes we allocated for padding if (PaddingSize != 0) { memset(Addr + DataSize, 0, PaddingSize); - // Update the DataSize variable so that the stub offset is set correctly. + // Update the DataSize variable to include padding. DataSize += PaddingSize; + + // Align DataSize to stub alignment if we have any stubs (PaddingSize will + // have been increased above to account for this). + if (StubBufSize > 0) + DataSize &= ~(getStubAlignment() - 1); } DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name @@ -864,7 +876,7 @@ uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr, } else if (Arch == Triple::arm || Arch == Triple::armeb) { // TODO: There is only ARM far stub now. We should add the Thumb stub, // and stubs for branches Thumb - ARM and ARM - Thumb. - writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,