From 943f06b288ac45ec29016b4126f2018d9c9e228a Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Mon, 23 Nov 2020 15:15:09 -0500
Subject: [PATCH] rts/m32: Introduce NEEDS_M32 macro

Instead of relying on RTS_LINKER_USE_MMAP

(cherry picked from commit c35d0e03514ce111ff8265426a7b911456984f50)
---
 rts/Linker.c          | 10 ++++++----
 rts/LinkerInternals.h | 10 +++++++++-
 rts/linker/Elf.c      |  1 +
 rts/linker/M32Alloc.c | 22 +++++++++-------------
 rts/linker/M32Alloc.h | 16 +++++++---------
 5 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/rts/Linker.c b/rts/Linker.c
index 9b38f3a57853..f1c0db7c929f 100644
--- a/rts/Linker.c
+++ b/rts/Linker.c
@@ -45,6 +45,8 @@
 #include <sys/types.h>
 #endif
 
+#include <fcntl.h>
+#include <unistd.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1256,13 +1258,13 @@ void freeObjectCode (ObjectCode *oc)
                     munmap(oc->sections[i].mapped_start,
                            oc->sections[i].mapped_size);
                     break;
+#endif
                 case SECTION_M32:
                     IF_DEBUG(zero_on_gc,
                         memset(oc->sections[i].start,
                             0x00, oc->sections[i].size));
                     // Freed by m32_allocator_free
                     break;
-#endif
                 case SECTION_MALLOC:
                     IF_DEBUG(zero_on_gc,
                         memset(oc->sections[i].start,
@@ -1305,7 +1307,7 @@ void freeObjectCode (ObjectCode *oc)
     ocDeinit_ELF(oc);
 #endif
 
-#if RTS_LINKER_USE_MMAP == 1
+#if defined(NEED_M32)
     m32_allocator_free(oc->rx_m32);
     m32_allocator_free(oc->rw_m32);
 #endif
@@ -1381,7 +1383,7 @@ mkOc( pathchar *path, char *image, int imageSize,
    oc->mark              = object_code_mark_bit;
    oc->dependencies      = allocHashSet();
 
-#if RTS_LINKER_USE_MMAP
+#if defined(NEED_M32)
    oc->rw_m32 = m32_allocator_new(false);
    oc->rx_m32 = m32_allocator_new(true);
 #endif
@@ -1716,7 +1718,7 @@ int ocTryLoad (ObjectCode* oc) {
 
     // We have finished loading and relocating; flush the m32 allocators to
     // setup page protections.
-#if RTS_LINKER_USE_MMAP
+#if defined(NEED_M32)
     m32_allocator_flush(oc->rx_m32);
     m32_allocator_flush(oc->rw_m32);
 #endif
diff --git a/rts/LinkerInternals.h b/rts/LinkerInternals.h
index 3852c656161c..f326a840b588 100644
--- a/rts/LinkerInternals.h
+++ b/rts/LinkerInternals.h
@@ -138,6 +138,14 @@ typedef struct _Segment {
 #define NEED_SYMBOL_EXTRAS 1
 #endif
 
+/*
+ * We use the m32 allocator for symbol extras on Windows and other mmap-using
+ * platforms.
+ */
+#if RTS_LINKER_USE_MMAP
+#define NEED_M32 1
+#endif
+
 /* Jump Islands are sniplets of machine code required for relative
  * address relocations on the PowerPC, x86_64 and ARM.
  */
@@ -261,7 +269,7 @@ typedef struct _ObjectCode {
        require extra information.*/
     HashTable *extraInfos;
 
-#if RTS_LINKER_USE_MMAP == 1
+#if defined(NEED_M32)
     /* The m32 allocators used for allocating small sections and symbol extras
      * during loading. We have two: one for (writeable) data and one for
      * (read-only/executable) code. */
diff --git a/rts/linker/Elf.c b/rts/linker/Elf.c
index 642beb1d5f61..fdfe87a4e597 100644
--- a/rts/linker/Elf.c
+++ b/rts/linker/Elf.c
@@ -25,6 +25,7 @@
 #include "linker/elf_util.h"
 
 #include <stdlib.h>
+#include <unistd.h>
 #include <string.h>
 #if defined(HAVE_SYS_STAT_H)
 #include <sys/stat.h>
diff --git a/rts/linker/M32Alloc.c b/rts/linker/M32Alloc.c
index 9f745c260851..d2b91140c0e4 100644
--- a/rts/linker/M32Alloc.c
+++ b/rts/linker/M32Alloc.c
@@ -24,25 +24,25 @@ Note [Compile Time Trickery]
 
 This file implements two versions of each of the `m32_*` functions. At the top
 of the file there is the real implementation (compiled in when
-`RTS_LINKER_USE_MMAP` is true) and a dummy implementation that exists only to
+`NEED_M32` is true) and a dummy implementation that exists only to
 satisfy the compiler and which should never be called. If any of these dummy
 implementations are called the program will abort.
 
 The rationale for this is to allow the calling code to be written without using
-the C pre-processor (CPP) `#if` hackery. The value of `RTS_LINKER_USE_MMAP` is
-known at compile time, code like:
+the C pre-processor (CPP) `#if` hackery. The value of `NEED_M32` is
+known at compile time, allowing code like:
 
-    if (RTS_LINKER_USE_MMAP)
+    if (NEED_M32)
         m32_allocator_init();
 
-will be compiled to call to `m32_allocator_init` if  `RTS_LINKER_USE_MMAP` is
-true and will be optimised awat to nothing if `RTS_LINKER_USE_MMAP` is false.
-However, regardless of the value of `RTS_LINKER_USE_MMAP` the compiler will
+will be compiled to call to `m32_allocator_init` if  `NEED_M32` is
+true and will be optimised away to nothing if `NEED_M32` is false.
+However, regardless of the value of `NEED_M32` the compiler will
 still check the call for syntax and correct function parameter types.
 
 */
 
-#if RTS_LINKER_USE_MMAP == 1
+#if defined(NEED_M32)
 
 /*
 
@@ -468,7 +468,7 @@ m32_alloc(struct m32_allocator_t *alloc, size_t size, size_t alignment)
    return (char*)page + ROUND_UP(sizeof(struct m32_page_t),alignment);
 }
 
-#elif RTS_LINKER_USE_MMAP == 0
+#else
 
 // The following implementations of these functions should never be called. If
 // they are, there is a bug at the call site.
@@ -499,8 +499,4 @@ m32_alloc(m32_allocator *alloc STG_UNUSED,
     barf("%s: RTS_LINKER_USE_MMAP is %d", __func__, RTS_LINKER_USE_MMAP);
 }
 
-#else
-
-#error RTS_LINKER_USE_MMAP should be either `0` or `1`.
-
 #endif
diff --git a/rts/linker/M32Alloc.h b/rts/linker/M32Alloc.h
index 892588082fdf..8a349a3b3ee3 100644
--- a/rts/linker/M32Alloc.h
+++ b/rts/linker/M32Alloc.h
@@ -8,19 +8,17 @@
 
 #pragma once
 
-#if RTS_LINKER_USE_MMAP == 1
-#include <fcntl.h>
-#include <sys/mman.h>
-
-#if defined(HAVE_UNISTD_H)
-#include <unistd.h>
-#endif
-
+/*
+ * We use the m32 allocator for symbol extras on Windows and other mmap-using
+ * platforms.
+ */
+#if RTS_LINKER_USE_MMAP
+#define NEED_M32 1
 #endif
 
 #include "BeginPrivate.h"
 
-#if RTS_LINKER_USE_MMAP
+#if defined(NEED_M32)
 #define M32_NO_RETURN    /* Nothing */
 #else
 #define M32_NO_RETURN    GNUC3_ATTRIBUTE(__noreturn__)
-- 
GitLab