pkgsrc-WIP-changes archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

llvm-netbsd: Scratch of AllocateJIT



Module Name:	pkgsrc-wip
Committed By:	Kamil Rytarowski <n54%gmx.com@localhost>
Pushed By:	kamil
Date:		Sat Jul 29 00:46:15 2017 +0200
Changeset:	23e4a484e2ca5f6da36e21d3f9a1f40c2b8ca90f

Modified Files:
	llvm-netbsd/distinfo
	llvm-netbsd/patches/patch-include_llvm_Support_Memory.h
	llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
Added Files:
	llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_IndirectionUtils.h
	llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_OrcRemoteTargetServer.h
	llvm-netbsd/patches/patch-lib_ExecutionEngine_Orc_OrcABISupport.cpp
	llvm-netbsd/patches/patch-lib_ExecutionEngine_SectionMemoryManager.cpp
	llvm-netbsd/patches/patch-tools_llvm-rtdyld_llvm-rtdyld.cpp

Log Message:
llvm-netbsd: Scratch of AllocateJIT

Sponsored by <The NetBSD Foundation>

To see a diff of this commit:
https://wip.pkgsrc.org/cgi-bin/gitweb.cgi?p=pkgsrc-wip.git;a=commitdiff;h=23e4a484e2ca5f6da36e21d3f9a1f40c2b8ca90f

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

diffstat:
 llvm-netbsd/distinfo                               |   9 +-
 ...ude_llvm_ExecutionEngine_Orc_IndirectionUtils.h |  48 +++++++
 ...lvm_ExecutionEngine_Orc_OrcRemoteTargetServer.h |  59 +++++++++
 .../patches/patch-include_llvm_Support_Memory.h    |  70 +++++++++++
 ...patch-lib_ExecutionEngine_Orc_OrcABISupport.cpp |  70 +++++++++++
 ...ch-lib_ExecutionEngine_SectionMemoryManager.cpp |  19 +++
 .../patches/patch-lib_Support_Unix_Memory.inc      | 140 +++++++++++++++++++++
 .../patch-tools_llvm-rtdyld_llvm-rtdyld.cpp        |  58 +++++++++
 8 files changed, 471 insertions(+), 2 deletions(-)

diffs:
diff --git a/llvm-netbsd/distinfo b/llvm-netbsd/distinfo
index 57bf92733e..057b574eda 100644
--- a/llvm-netbsd/distinfo
+++ b/llvm-netbsd/distinfo
@@ -12,6 +12,11 @@ Size (libcxx-3.6.2.src.tar.xz) = 944020 bytes
 SHA1 (llvm-3.6.2.src.tar.xz) = 7a00257eb2bc9431e4c77c3a36b033072c54bc7e
 RMD160 (llvm-3.6.2.src.tar.xz) = 521cbc5fe2925ea3c6e90c7a31f752a04045c972
 Size (llvm-3.6.2.src.tar.xz) = 12802380 bytes
-SHA1 (patch-include_llvm_Support_Memory.h) = ba6eec99b1126ffbe27c18b63b3340697b192635
-SHA1 (patch-lib_Support_Unix_Memory.inc) = 69fc875b16e954e5fbf6fe2767d98c6f901713d4
+SHA1 (patch-include_llvm_ExecutionEngine_Orc_IndirectionUtils.h) = f106d7e0b95c498c34dc087d72027e19c7342224
+SHA1 (patch-include_llvm_ExecutionEngine_Orc_OrcRemoteTargetServer.h) = 6bec3a8920d3d73363d353c05ce40ff33b0a4193
+SHA1 (patch-include_llvm_Support_Memory.h) = b432032e91f76dc3061d918714d1bc788c872d03
+SHA1 (patch-lib_ExecutionEngine_Orc_OrcABISupport.cpp) = 3fc562d8e3c3dd7ca28121bf1bd03d1275d3d1c2
+SHA1 (patch-lib_ExecutionEngine_SectionMemoryManager.cpp) = fe15fc03578dad2af283ba389381ebb5ff793e57
+SHA1 (patch-lib_Support_Unix_Memory.inc) = cf14509c8d562a0101615d7a8c4148aa02b06bc9
+SHA1 (patch-tools_llvm-rtdyld_llvm-rtdyld.cpp) = 98c44df7b016f311e2553a75458175a7d5216279
 SHA1 (patch-unittests_Support_MemoryTest.cpp) = cd7bfc8053ceca839993e70e147cf803bac7eaae
diff --git a/llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_IndirectionUtils.h b/llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_IndirectionUtils.h
new file mode 100644
index 0000000000..e40820b007
--- /dev/null
+++ b/llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_IndirectionUtils.h
@@ -0,0 +1,48 @@
+$NetBSD$
+
+--- include/llvm/ExecutionEngine/Orc/IndirectionUtils.h.orig	2017-07-05 18:41:32.000000000 +0000
++++ include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
+@@ -165,17 +165,14 @@ public:
+       : JITCompileCallbackManager(ErrorHandlerAddress) {
+     /// Set up the resolver block.
+     std::error_code EC;
+-    ResolverBlock = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-        TargetT::ResolverCodeSize, nullptr,
+-        sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++    ResolverBlock = sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++        TargetT::ResolverCodeSize, nullptr, EC));
+     assert(!EC && "Failed to allocate resolver block");
+ 
+     TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.base()),
+                                &reenter, this);
+ 
+-    EC = sys::Memory::protectMappedMemory(ResolverBlock.getMemoryBlock(),
+-                                          sys::Memory::MF_READ |
+-                                              sys::Memory::MF_EXEC);
++    EC = sys::Memory::setExecutable(ResolverBlock.getMemoryBlock());
+     assert(!EC && "Failed to mprotect resolver block");
+   }
+ 
+@@ -193,9 +190,8 @@ private:
+ 
+     std::error_code EC;
+     auto TrampolineBlock =
+-        sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-            sys::Process::getPageSize(), nullptr,
+-            sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++        sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++            sys::Process::getPageSize(), nullptr, EC));
+     assert(!EC && "Failed to allocate trampoline block");
+ 
+     unsigned NumTrampolines =
+@@ -211,9 +207,7 @@ private:
+           static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(
+               TrampolineMem + (I * TargetT::TrampolineSize))));
+ 
+-    EC = sys::Memory::protectMappedMemory(TrampolineBlock.getMemoryBlock(),
+-                                          sys::Memory::MF_READ |
+-                                              sys::Memory::MF_EXEC);
++    EC = sys::Memory::setExecutable(TrampolineBlock.getMemoryBlock());
+     assert(!EC && "Failed to mprotect trampoline block");
+ 
+     TrampolineBlocks.push_back(std::move(TrampolineBlock));
diff --git a/llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_OrcRemoteTargetServer.h b/llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_OrcRemoteTargetServer.h
new file mode 100644
index 0000000000..9f83381164
--- /dev/null
+++ b/llvm-netbsd/patches/patch-include_llvm_ExecutionEngine_Orc_OrcRemoteTargetServer.h
@@ -0,0 +1,59 @@
+$NetBSD$
+
+--- include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h.orig	2017-07-05 18:41:32.000000000 +0000
++++ include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h
+@@ -116,8 +116,8 @@ private:
+ 
+     Error allocate(void *&Addr, size_t Size, uint32_t Align) {
+       std::error_code EC;
+-      sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(
+-          Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
++      sys::MemoryBlock MB = sys::Memory::AllocateJIT(
++          Size, nullptr, EC);
+       if (EC)
+         return errorCodeToError(EC);
+ 
+@@ -278,26 +278,23 @@ private:
+ 
+   Error handleEmitResolverBlock() {
+     std::error_code EC;
+-    ResolverBlock = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-        TargetT::ResolverCodeSize, nullptr,
+-        sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++    ResolverBlock = sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++        TargetT::ResolverCodeSize, nullptr, EC));
+     if (EC)
+       return errorCodeToError(EC);
+ 
+     TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.base()),
+                                &reenter, this);
+ 
+-    return errorCodeToError(sys::Memory::protectMappedMemory(
+-        ResolverBlock.getMemoryBlock(),
+-        sys::Memory::MF_READ | sys::Memory::MF_EXEC));
++    return errorCodeToError(sys::Memory::setExecutable(
++        ResolverBlock.getMemoryBlock()));
+   }
+ 
+   Expected<std::tuple<JITTargetAddress, uint32_t>> handleEmitTrampolineBlock() {
+     std::error_code EC;
+     auto TrampolineBlock =
+-        sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-            sys::Process::getPageSize(), nullptr,
+-            sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++        sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++            sys::Process::getPageSize(), nullptr, EC));
+     if (EC)
+       return errorCodeToError(EC);
+ 
+@@ -309,9 +306,7 @@ private:
+     TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(),
+                               NumTrampolines);
+ 
+-    EC = sys::Memory::protectMappedMemory(TrampolineBlock.getMemoryBlock(),
+-                                          sys::Memory::MF_READ |
+-                                              sys::Memory::MF_EXEC);
++    EC = sys::Memory::setExecutable(TrampolineBlock.getMemoryBlock());
+ 
+     TrampolineBlocks.push_back(std::move(TrampolineBlock));
+ 
diff --git a/llvm-netbsd/patches/patch-include_llvm_Support_Memory.h b/llvm-netbsd/patches/patch-include_llvm_Support_Memory.h
index 1357346c9d..108a45ec55 100644
--- a/llvm-netbsd/patches/patch-include_llvm_Support_Memory.h
+++ b/llvm-netbsd/patches/patch-include_llvm_Support_Memory.h
@@ -53,3 +53,73 @@ $NetBSD$
      /// \r error_success if the function was successful, or an error_code
      /// describing the failure if an error occurred.
      ///
+@@ -109,51 +119,38 @@ namespace sys {
+     static std::error_code protectMappedMemory(const MemoryBlock &Block,
+                                                unsigned Flags);
+ 
+-    /// This method allocates a block of Read/Write/Execute memory that is
++    /// This method allocates a block of memory that is
+     /// suitable for executing dynamically generated code (e.g. JIT). An
+     /// attempt to allocate \p NumBytes bytes of virtual memory is made.
+     /// \p NearBlock may point to an existing allocation in which case
+     /// an attempt is made to allocate more memory near the existing block.
+     ///
+-    /// On success, this returns a non-null memory block, otherwise it returns
+-    /// a null memory block and fills in *ErrMsg.
++    /// The reserved memory block is Writable (MF_READ | MF_WRITE). In
++    /// order to toggle it between writable and executable, one has to use
++    /// setWritable and setExecutable methods.
+     ///
+-    /// @brief Allocate Read/Write/Execute memory.
+-    static MemoryBlock AllocateRWX(size_t NumBytes,
++    /// This function is a utility wrapper for allocateMappedMemory.
++    ///
++    /// \r a non-null MemoryBlock if the function was successful,
++    /// otherwise a null MemoryBlock is with \p EC describing the error.
++    ///
++    /// @brief Allocate mapped memory suitable for JIT code.
++    static MemoryBlock AllocateJIT(size_t NumBytes,
+                                    const MemoryBlock *NearBlock,
+-                                   std::string *ErrMsg = nullptr);
+-
+-    /// This method releases a block of Read/Write/Execute memory that was
+-    /// allocated with the AllocateRWX method. It should not be used to
+-    /// release any memory block allocated any other way.
+-    ///
+-    /// On success, this returns false, otherwise it returns true and fills
+-    /// in *ErrMsg.
+-    /// @brief Release Read/Write/Execute memory.
+-    static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = nullptr);
++                                   std::error_code &EC);
+ 
+     /// InvalidateInstructionCache - Before the JIT can run a block of code
+     /// that has been emitted it must invalidate the instruction cache on some
+     /// platforms.
+     static void InvalidateInstructionCache(const void *Addr, size_t Len);
+ 
++    /// setExecutable - Before the JIT can write a block of code, it has to be
++    /// given read and write privilege.
++    static std::error_code setWritable(const MemoryBlock &M);
++
+     /// setExecutable - Before the JIT can run a block of code, it has to be
+-    /// given read and executable privilege. Return true if it is already r-x
+-    /// or the system is able to change its previlege.
+-    static bool setExecutable(MemoryBlock &M, std::string *ErrMsg = nullptr);
+-
+-    /// setWritable - When adding to a block of code, the JIT may need
+-    /// to mark a block of code as RW since the protections are on page
+-    /// boundaries, and the JIT internal allocations are not page aligned.
+-    static bool setWritable(MemoryBlock &M, std::string *ErrMsg = nullptr);
+-
+-    /// setRangeExecutable - Mark the page containing a range of addresses
+-    /// as executable.
+-    static bool setRangeExecutable(const void *Addr, size_t Size);
+-
+-    /// setRangeWritable - Mark the page containing a range of addresses
+-    /// as writable.
+-    static bool setRangeWritable(const void *Addr, size_t Size);
++    /// given read and executable privilege.
++    static std::error_code setExecutable(const MemoryBlock &M);
+   };
+ 
+   /// Owning version of MemoryBlock.
diff --git a/llvm-netbsd/patches/patch-lib_ExecutionEngine_Orc_OrcABISupport.cpp b/llvm-netbsd/patches/patch-lib_ExecutionEngine_Orc_OrcABISupport.cpp
new file mode 100644
index 0000000000..0a71fe416f
--- /dev/null
+++ b/llvm-netbsd/patches/patch-lib_ExecutionEngine_Orc_OrcABISupport.cpp
@@ -0,0 +1,70 @@
+$NetBSD$
+
+--- lib/ExecutionEngine/Orc/OrcABISupport.cpp.orig	2017-07-05 18:38:00.000000000 +0000
++++ lib/ExecutionEngine/Orc/OrcABISupport.cpp
+@@ -155,9 +155,8 @@ Error OrcAArch64::emitIndirectStubsBlock
+ 
+   // Allocate memory for stubs and pointers in one call.
+   std::error_code EC;
+-  auto StubsMem = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-      2 * NumPages * PageSize, nullptr,
+-      sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++  auto StubsMem = sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++      2 * NumPages * PageSize, nullptr, EC));
+ 
+   if (EC)
+     return errorCodeToError(EC);
+@@ -176,8 +175,7 @@ Error OrcAArch64::emitIndirectStubsBlock
+   for (unsigned I = 0; I < NumStubs; ++I)
+     Stub[I] = 0xd61f020058000010 | PtrOffsetField;
+ 
+-  if (auto EC = sys::Memory::protectMappedMemory(
+-          StubsBlock, sys::Memory::MF_READ | sys::Memory::MF_EXEC))
++  if (auto EC = sys::Memory::setExecutable(StubsBlock))
+     return errorCodeToError(EC);
+ 
+   // Initialize all pointers to point at FailureAddress.
+@@ -237,9 +235,8 @@ Error OrcX86_64_Base::emitIndirectStubsB
+ 
+   // Allocate memory for stubs and pointers in one call.
+   std::error_code EC;
+-  auto StubsMem = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-      2 * NumPages * PageSize, nullptr,
+-      sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++  auto StubsMem = sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++      2 * NumPages * PageSize, nullptr, EC));
+ 
+   if (EC)
+     return errorCodeToError(EC);
+@@ -257,8 +254,7 @@ Error OrcX86_64_Base::emitIndirectStubsB
+   for (unsigned I = 0; I < NumStubs; ++I)
+     Stub[I] = 0xF1C40000000025ff | PtrOffsetField;
+ 
+-  if (auto EC = sys::Memory::protectMappedMemory(
+-          StubsBlock, sys::Memory::MF_READ | sys::Memory::MF_EXEC))
++  if (auto EC = sys::Memory::setExecutable(StubsBlock))
+     return errorCodeToError(EC);
+ 
+   // Initialize all pointers to point at FailureAddress.
+@@ -505,9 +501,8 @@ Error OrcI386::emitIndirectStubsBlock(In
+ 
+   // Allocate memory for stubs and pointers in one call.
+   std::error_code EC;
+-  auto StubsMem = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+-      2 * NumPages * PageSize, nullptr,
+-      sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
++  auto StubsMem = sys::OwningMemoryBlock(sys::Memory::AllocateJIT(
++      2 * NumPages * PageSize, nullptr, EC));
+ 
+   if (EC)
+     return errorCodeToError(EC);
+@@ -524,8 +519,7 @@ Error OrcI386::emitIndirectStubsBlock(In
+   for (unsigned I = 0; I < NumStubs; ++I, PtrAddr += 4)
+     Stub[I] = 0xF1C40000000025ff | (PtrAddr << 16);
+ 
+-  if (auto EC = sys::Memory::protectMappedMemory(
+-          StubsBlock, sys::Memory::MF_READ | sys::Memory::MF_EXEC))
++  if (auto EC = sys::Memory::setExecutable(StubsBlock))
+     return errorCodeToError(EC);
+ 
+   // Initialize all pointers to point at FailureAddress.
diff --git a/llvm-netbsd/patches/patch-lib_ExecutionEngine_SectionMemoryManager.cpp b/llvm-netbsd/patches/patch-lib_ExecutionEngine_SectionMemoryManager.cpp
new file mode 100644
index 0000000000..4d36258f12
--- /dev/null
+++ b/llvm-netbsd/patches/patch-lib_ExecutionEngine_SectionMemoryManager.cpp
@@ -0,0 +1,19 @@
+$NetBSD$
+
+--- lib/ExecutionEngine/SectionMemoryManager.cpp.orig	2017-07-05 18:38:00.000000000 +0000
++++ lib/ExecutionEngine/SectionMemoryManager.cpp
+@@ -84,11 +84,9 @@ uint8_t *SectionMemoryManager::allocateS
+   // FIXME: Initialize the Near member for each memory group to avoid
+   // interleaving.
+   std::error_code ec;
+-  sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
+-                                                          &MemGroup.Near,
+-                                                          sys::Memory::MF_READ |
+-                                                            sys::Memory::MF_WRITE,
+-                                                          ec);
++  sys::MemoryBlock MB = sys::Memory::AllocateJIT(RequiredSize,
++                                                 &MemGroup.Near,
++                                                 ec);
+   if (ec) {
+     // FIXME: Add error propagation to the interface.
+     return nullptr;
diff --git a/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc b/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
index a49693e9ea..993a40a7c8 100644
--- a/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
+++ b/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
@@ -66,3 +66,143 @@ $NetBSD$
    static const size_t PageSize = Process::getPageSize();
    if (M.Address == nullptr || M.Size == 0)
      return std::error_code();
+@@ -166,128 +190,24 @@ Memory::protectMappedMemory(const Memory
+   return std::error_code();
+ }
+ 
+-/// AllocateRWX - Allocate a slab of memory with read/write/execute
+-/// permissions.  This is typically used for JIT applications where we want
+-/// to emit code to the memory then jump to it.  Getting this type of memory
+-/// is very OS specific.
+-///
+ MemoryBlock
+-Memory::AllocateRWX(size_t NumBytes, const MemoryBlock* NearBlock,
+-                    std::string *ErrMsg) {
+-  if (NumBytes == 0) return MemoryBlock();
+-
+-  static const size_t PageSize = Process::getPageSize();
+-  size_t NumPages = (NumBytes+PageSize-1)/PageSize;
+-
+-  int fd = -1;
+-
+-  int flags = MAP_PRIVATE |
+-#ifdef MAP_ANONYMOUS
+-  MAP_ANONYMOUS
+-#else
+-  MAP_ANON
+-#endif
+-  ;
+-
+-  void* start = NearBlock ? (unsigned char*)NearBlock->base() +
+-                            NearBlock->size() : nullptr;
+-
+-#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+-  void *pa = ::mmap(start, PageSize*NumPages, PROT_READ|PROT_EXEC,
+-                    flags, fd, 0);
+-#elif defined(__NetBSD__) && defined(PROT_MPROTECT)
+-  void *pa =
+-      ::mmap(start, PageSize * NumPages,
+-             PROT_READ | PROT_WRITE | PROT_MPROTECT(PROT_EXEC), flags, fd, 0);
+-#else
+-  void *pa = ::mmap(start, PageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
+-                    flags, fd, 0);
+-#endif
+-  if (pa == MAP_FAILED) {
+-    if (NearBlock) //Try again without a near hint
+-      return AllocateRWX(NumBytes, nullptr);
+-
+-    MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
+-    return MemoryBlock();
+-  }
+-
+-#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+-  kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
+-                                (vm_size_t)(PageSize*NumPages), 0,
+-                                VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
+-  if (KERN_SUCCESS != kr) {
+-    MakeErrMsg(ErrMsg, "vm_protect max RX failed");
+-    return MemoryBlock();
+-  }
+-
+-  kr = vm_protect(mach_task_self(), (vm_address_t)pa,
+-                  (vm_size_t)(PageSize*NumPages), 0,
+-                  VM_PROT_READ | VM_PROT_WRITE);
+-  if (KERN_SUCCESS != kr) {
+-    MakeErrMsg(ErrMsg, "vm_protect RW failed");
+-    return MemoryBlock();
+-  }
+-#endif
+-
+-  MemoryBlock result;
+-  result.Address = pa;
+-  result.Size = NumPages*PageSize;
+-
+-  return result;
+-}
+-
+-bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
+-  if (M.Address == nullptr || M.Size == 0) return false;
+-  if (0 != ::munmap(M.Address, M.Size))
+-    return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
+-  return false;
+-}
+-
+-bool Memory::setWritable (MemoryBlock &M, std::string *ErrMsg) {
+-#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+-  if (M.Address == 0 || M.Size == 0) return false;
+-  Memory::InvalidateInstructionCache(M.Address, M.Size);
+-  kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
+-    (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_WRITE);
+-  return KERN_SUCCESS == kr;
+-#else
+-  return true;
+-#endif
++Memory::AllocateJIT(size_t NumBytes, const MemoryBlock* NearBlock,
++                    std::error_code &EC) {
++  return allocateMappedMemory(NumBytes, NearBlock, MF_READ | MF_WRITE, EC, MF_EXEC);
+ }
+ 
+-bool Memory::setExecutable (MemoryBlock &M, std::string *ErrMsg) {
+-  if (M.Address == nullptr || M.Size == 0) return false;
+-  Memory::InvalidateInstructionCache(M.Address, M.Size);
+-#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+-  kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
+-    (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
+-  return KERN_SUCCESS == kr;
+-#else
+-  return true;
+-#endif
++std::error_code
++Memory::setWritable (const MemoryBlock &M) {
++  return protectMappedMemory(M, MF_READ | MF_WRITE);
+ }
+ 
+-bool Memory::setRangeWritable(const void *Addr, size_t Size) {
+-#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+-  kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
+-                                (vm_size_t)Size, 0,
+-                                VM_PROT_READ | VM_PROT_WRITE);
+-  return KERN_SUCCESS == kr;
+-#else
+-  return true;
+-#endif
++std::error_code
++Memory::setExecutable (const MemoryBlock &M) {
++  if (M.Address != nullptr && M.Size != 0)
++    Memory::InvalidateInstructionCache(M.Address, M.Size);
++  return protectMappedMemory(M, MF_READ | MF_EXEC);
+ }
+ 
+-bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
+-#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+-  kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
+-                                (vm_size_t)Size, 0,
+-                                VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
+-  return KERN_SUCCESS == kr;
+-#else
+-  return true;
+-#endif
+-}
+ 
+ /// InvalidateInstructionCache - Before the JIT can run a block of code
+ /// that has been emitted it must invalidate the instruction cache on some
diff --git a/llvm-netbsd/patches/patch-tools_llvm-rtdyld_llvm-rtdyld.cpp b/llvm-netbsd/patches/patch-tools_llvm-rtdyld_llvm-rtdyld.cpp
new file mode 100644
index 0000000000..750bb72330
--- /dev/null
+++ b/llvm-netbsd/patches/patch-tools_llvm-rtdyld_llvm-rtdyld.cpp
@@ -0,0 +1,58 @@
+$NetBSD$
+
+--- tools/llvm-rtdyld/llvm-rtdyld.cpp.orig	2017-07-20 19:13:57.000000000 +0000
++++ tools/llvm-rtdyld/llvm-rtdyld.cpp
+@@ -178,10 +178,10 @@ public:
+   void deregisterEHFrames() override {}
+ 
+   void preallocateSlab(uint64_t Size) {
+-    std::string Err;
+-    sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
++    std::error_code EC;
++    sys::MemoryBlock MB = sys::Memory::AllocateJIT(Size, nullptr, EC);
+     if (!MB.base())
+-      report_fatal_error("Can't allocate enough memory: " + Err);
++      report_fatal_error("Can't allocate enough memory");
+ 
+     PreallocSlab = MB;
+     UsePreallocation = true;
+@@ -222,10 +222,10 @@ uint8_t *TrivialMemoryManager::allocateC
+   if (UsePreallocation)
+     return allocateFromSlab(Size, Alignment, true /* isCode */);
+ 
+-  std::string Err;
+-  sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
++  std::error_code EC;
++  sys::MemoryBlock MB = sys::Memory::AllocateJIT(Size, nullptr, EC);
+   if (!MB.base())
+-    report_fatal_error("MemoryManager allocation failed: " + Err);
++    report_fatal_error("MemoryManager allocation failed");
+   FunctionMemory.push_back(MB);
+   return (uint8_t*)MB.base();
+ }
+@@ -242,10 +242,10 @@ uint8_t *TrivialMemoryManager::allocateD
+   if (UsePreallocation)
+     return allocateFromSlab(Size, Alignment, false /* isCode */);
+ 
+-  std::string Err;
+-  sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
++  std::error_code EC;
++  sys::MemoryBlock MB = sys::Memory::AllocateJIT(Size, nullptr, EC);
+   if (!MB.base())
+-    report_fatal_error("MemoryManager allocation failed: " + Err);
++    report_fatal_error("MemoryManager allocation failed");
+   DataMemory.push_back(MB);
+   return (uint8_t*)MB.base();
+ }
+@@ -453,9 +453,8 @@ static int executeInput() {
+ 
+     // Make sure the memory is executable.
+     // setExecutable will call InvalidateInstructionCache.
+-    std::string ErrorStr;
+-    if (!sys::Memory::setExecutable(FM, &ErrorStr))
+-      ErrorAndExit("unable to mark function executable: '" + ErrorStr + "'");
++    if (sys::Memory::setExecutable(FM))
++      ErrorAndExit("unable to mark function executable");
+   }
+ 
+   // Dispatch to _main().


Home | Main Index | Thread Index | Old Index