From 17a01e38f2d0a34d548c7b44ef5988df10322cd8 Mon Sep 17 00:00:00 2001
From: rtoy <rtoy>
Date: Sat, 31 Jul 2010 02:45:45 +0000
Subject: [PATCH] For Linux and Darwin, we don't actually need to set the
 starting address of the core sections.  In map_core_sections, we can map them
 to the correct addresses, just like we do on Solaris.

lisp/elf.c:
o Mmap the Lisp core sections with the correct address, not using the
  one in the executable itself, just like on Solaris.

lisp/mach-o.c:
o Add the array of addresses of the dynamic, static, and read-only
  spaces.
o Mmap the Lisp core sections with the correct address, not using the
  one in the executable itself, just like on Solaris.
o Small update to print out the names of the spaces, just like for elf
  files.

tools/linker-x86.sh:
o Don't need to tell the linker the starting addresses of the sections
  anymore for Linux and Darwin.  map_core_sections handles that.
---
 lisp/elf.c          |  7 +++----
 lisp/mach-o.c       | 31 ++++++++++++++++++++++++++++---
 tools/linker-x86.sh | 14 +++++++-------
 3 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/lisp/elf.c b/lisp/elf.c
index 100934484..d742a479a 100644
--- a/lisp/elf.c
+++ b/lisp/elf.c
@@ -8,7 +8,7 @@
 
  Above changes put into main CVS branch. 05-Jul-2007.
 
- $Id: elf.c,v 1.24 2010/07/31 01:07:15 rtoy Exp $
+ $Id: elf.c,v 1.25 2010/07/31 02:45:45 rtoy Exp $
 */
 
 #include <stdio.h>
@@ -37,7 +37,7 @@ static Elf_Shdr sh;
 
 static char *section_names[] = {"CORDYN", "CORSTA", "CORRO"};
 
-#ifdef SOLARIS
+#if defined(SOLARIS) || defined(__linux__)
 /*
  * Starting address of the three ELF sections/spaces.  These must be
  * in the same order as section_names above!
@@ -482,7 +482,7 @@ map_core_sections(const char *exec_name)
 		if (!strncmp(nambuf, section_names[j], 6)) {
 		    os_vm_address_t addr;
 
-#ifdef SOLARIS
+#if defined(SOLARIS) || defined(__linux__)
 		    /*
 		     * On Solaris, the section header sets the addr
 		     * field to 0 because the linker script says the
@@ -532,4 +532,3 @@ map_core_sections(const char *exec_name)
 	exit(-1);
     }
 }
-
diff --git a/lisp/mach-o.c b/lisp/mach-o.c
index 60ddc6100..25239464b 100644
--- a/lisp/mach-o.c
+++ b/lisp/mach-o.c
@@ -19,11 +19,24 @@ typedef struct mach_header MachO_hdr;
 /* Uncomment to enable debugging prints */
 /* #define DEBUG_MACH_O */
 
-/* Names of the Lisp image ELF sections. These names must be the same as
-   the corresponding names found in the linker script.  */
+/*
+ * Names of the Lisp image sections. These names must be the same as
+ * the corresponding names found in the linker script.
+ */
 
 static char *section_names[] = {"CORDYN", "CORSTA", "CORRO"};
 
+/*
+ * Starting addresses of the various spaces.  Must be in the same
+ * order as section_names
+ */
+static os_vm_address_t section_addr[] =
+{
+    (os_vm_address_t) DYNAMIC_0_SPACE_START,
+    (os_vm_address_t) STATIC_SPACE_START,
+    (os_vm_address_t) READ_ONLY_SPACE_START
+};
+
 /* Note: write errors are not fatal. */
 static int
 ewrite(int fd, const void *buf, size_t nbytes, const char *func)
@@ -165,6 +178,7 @@ write_space_object(const char *dir, int id, os_vm_address_t start, os_vm_address
     /* The length should be a multiple of the page size. */
     size_t length = end - start + (os_vm_page_size -
 				   ((end - start) % os_vm_page_size));
+    static char *names[] = { "Dynamic", "Static", "Read-Only" };
 
     if(id < 1 || id > 3) {
 	fprintf(stderr, "Invalid space id in %s: %d\n", __func__, id);
@@ -175,6 +189,9 @@ write_space_object(const char *dir, int id, os_vm_address_t start, os_vm_address
     /* Make id be 0-based to match array. */
     id--;
 
+    printf("\t %s: %d bytes...\n", names[id], (end - start));
+    fflush(stdout);
+
     if ((write_mach_o_header(out) == -1)
         || (write_load_command(out, section_names[id], length, start) == -1)
         || (write_section(out, length, start, section_names[id]) == -1)
@@ -343,6 +360,8 @@ map_core_sections(const char *exec_name)
             /* See if the segment name matches any of our section names */
             for (j = 0; j < 3; ++j) {
                 if (strncmp(sc.segname, section_names[j], sizeof(sc.segname)) == 0) {
+		    os_vm_address_t addr;
+
                     /* Found a core segment.  Map it! */
 #ifdef DEBUG_MACH_O
                     fprintf(stderr, "Matched!\n");
@@ -350,9 +369,15 @@ map_core_sections(const char *exec_name)
                     fprintf(stderr, " vmaddr  = 0x%x\n", sc.vmaddr);
                     fprintf(stderr, " vmsize  = 0x%x\n", sc.vmsize);
 #endif
+                    /*
+                     * We don't care what address the segment has.  We
+                     * will map it where want it to go.
+                     */
+                    
+		    addr = section_addr[j];
                     
                     if ((os_vm_address_t) os_map(exec_fd, sc.fileoff,
-                                                 (os_vm_address_t) sc.vmaddr,
+                                                 (os_vm_address_t) addr,
                                                  sc.vmsize)
                         == (os_vm_address_t) -1) {
 			fprintf(stderr, "%s: Can't map section %s\n", __func__, section_names[j]);
diff --git a/tools/linker-x86.sh b/tools/linker-x86.sh
index 9149a813e..201d14cf8 100644
--- a/tools/linker-x86.sh
+++ b/tools/linker-x86.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# $Id: linker-x86.sh,v 1.4 2010/07/31 01:07:15 rtoy Exp $
+# $Id: linker-x86.sh,v 1.5 2010/07/31 02:45:45 rtoy Exp $
 
 # This file written by Raymond Toy as part of CMU Common Lisp and is
 # placed in the public domain.
@@ -33,9 +33,9 @@ OPT_CORE="CORRO.o CORSTA.o CORDYN.o"
 case `uname` in
   Linux*)
       # How to specify the starting address for each of the sections
-      RO_ADDR="-Wl,--section-start=CORRO=$4"
-      STATIC_ADDR="-Wl,--section-start=CORSTA=$5"
-      DYN_ADDR="-Wl,--section-start=CORDYN=$6"
+      #RO_ADDR="-Wl,--section-start=CORRO=$4"
+      #STATIC_ADDR="-Wl,--section-start=CORSTA=$5"
+      #DYN_ADDR="-Wl,--section-start=CORDYN=$6"
 
       #OPT_IFADDR="-Wl,--defsym -Wl,initial_function_addr=$IFADDR"
 
@@ -50,9 +50,9 @@ case `uname` in
       ;;
   Darwin*)
       # How to specify the starting address for each of the sections
-      RO_ADDR="-segaddr CORRO $4"
-      STATIC_ADDR="-segaddr CORSTA $5"
-      DYN_ADDR="-segaddr CORDYN $6"
+      #RO_ADDR="-segaddr CORRO $4"
+      #STATIC_ADDR="-segaddr CORSTA $5"
+      #DYN_ADDR="-segaddr CORDYN $6"
 
       # Specify how to link the entire lisp.a library
       OPT_ARCHIVE="-all_load $CMUCLLIB/lisp.a"
-- 
GitLab