From e3f371483fa79148cc9c593971feaaa0aeff939f Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Sun, 7 May 2017 17:19:53 +0000 Subject: [PATCH] Make llvm-rtdlyd -check preserve automatic address mappings made by RuntimeDyld. Currently llvm-rtdyld in -check mode will map sections to back-to-back 4k aligned slabs starting at 0x1000. Automatically remapping sections by default is helpful because it quickly exposes relocation bugs due to use of local addresses rather than load addresses (these would silently pass if the load address was not remapped). These mappings can be explicitly overridden on a per-section basis using llvm-rtdlyd's -map-section option. This patch extends this scheme to also preserve any mappings made by RuntimeDyld itself. Preserving RuntimeDyld's automatic mappings allows us to write test cases to verify that these automatic mappings have been applied. This will allow the fix in https://reviews.llvm.org/D32899 to be tested with llvm-rtdyld -check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302372 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../llvm/ExecutionEngine/RuntimeDyldChecker.h | 6 +++++ .../RuntimeDyld/RuntimeDyldChecker.cpp | 14 +++++++++++ .../RuntimeDyld/RuntimeDyldCheckerImpl.h | 2 ++ tools/llvm-rtdyld/llvm-rtdyld.cpp | 23 ++++++++----------- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/include/llvm/ExecutionEngine/RuntimeDyldChecker.h b/include/llvm/ExecutionEngine/RuntimeDyldChecker.h index f5f52b5d2f9..de89f405af4 100644 --- a/include/llvm/ExecutionEngine/RuntimeDyldChecker.h +++ b/include/llvm/ExecutionEngine/RuntimeDyldChecker.h @@ -10,6 +10,8 @@ #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H #define LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H +#include "llvm/ADT/Optional.h" + #include #include #include @@ -97,6 +99,10 @@ public: StringRef SectionName, bool LocalAddress); + /// \brief If there is a section at the given local address, return its load + /// address, otherwise return none. + Optional getSectionLoadAddress(void *LocalAddress) const; + private: std::unique_ptr Impl; }; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp index 7bfa7944558..e45fdc7aee1 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp @@ -861,6 +861,15 @@ RuntimeDyldCheckerImpl::getSubsectionStartingAt(StringRef Name) const { SymInfo.getOffset()); } +Optional +RuntimeDyldCheckerImpl::getSectionLoadAddress(void *LocalAddress) const { + for (auto &S : getRTDyld().Sections) { + if (S.getAddress() == LocalAddress) + return S.getLoadAddress(); + } + return Optional(); +} + void RuntimeDyldCheckerImpl::registerSection( StringRef FilePath, unsigned SectionID) { StringRef FileName = sys::path::filename(FilePath); @@ -935,3 +944,8 @@ RuntimeDyldChecker::getSectionAddr(StringRef FileName, StringRef SectionName, bool LocalAddress) { return Impl->getSectionAddr(FileName, SectionName, LocalAddress); } + +Optional +RuntimeDyldChecker::getSectionLoadAddress(void *LocalAddress) const { + return Impl->getSectionLoadAddress(LocalAddress); +} diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCheckerImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCheckerImpl.h index b7263be0993..b462ef2c00c 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCheckerImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCheckerImpl.h @@ -60,6 +60,8 @@ private: bool IsInsideLoad) const; StringRef getSubsectionStartingAt(StringRef Name) const; + Optional getSectionLoadAddress(void *LocalAddr) const; + void registerSection(StringRef FilePath, unsigned SectionID); void registerStubMap(StringRef FilePath, unsigned SectionID, const RuntimeDyldImpl::StubMap &RTDyldStubs); diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index 4e1caa0400f..75345de5028 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -486,10 +486,7 @@ static int checkAllExpressions(RuntimeDyldChecker &Checker) { return 0; } -static std::map -applySpecificSectionMappings(RuntimeDyldChecker &Checker) { - - std::map SpecificMappings; +void applySpecificSectionMappings(RuntimeDyldChecker &Checker) { for (StringRef Mapping : SpecificSectionMappings) { @@ -522,10 +519,7 @@ applySpecificSectionMappings(RuntimeDyldChecker &Checker) { "'."); Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr); - SpecificMappings[OldAddr] = NewAddr; } - - return SpecificMappings; } // Scatter sections in all directions! @@ -554,8 +548,7 @@ static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple, // Apply any section-specific mappings that were requested on the command // line. - typedef std::map AppliedMappingsT; - AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker); + applySpecificSectionMappings(Checker); // Keep an "already allocated" mapping of section target addresses to sizes. // Sections whose address mappings aren't specified on the command line will @@ -563,15 +556,19 @@ static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple, // minimum separation. std::map AlreadyAllocated; - // Move the previously applied mappings into the already-allocated map. + // Move the previously applied mappings (whether explicitly specified on the + // command line, or implicitly set by RuntimeDyld) into the already-allocated + // map. for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end(); I != E;) { WorklistT::iterator Tmp = I; ++I; - AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first); + auto LoadAddr = Checker.getSectionLoadAddress(Tmp->first); - if (AI != AppliedMappings.end()) { - AlreadyAllocated[AI->second] = Tmp->second; + if (LoadAddr && + *LoadAddr != static_cast( + reinterpret_cast(Tmp->first))) { + AlreadyAllocated[*LoadAddr] = Tmp->second; Worklist.erase(Tmp); } } -- 2.50.1