emacs-devel
[Top][All Lists]
Advanced

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

[PATCHES] 21.3.50 for Cygwin: patch 7


From: Joe Buehler
Subject: [PATCHES] 21.3.50 for Cygwin: patch 7
Date: Tue, 10 Dec 2002 16:48:46 -0500
User-agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.2) Gecko/20021126

This patch set adds unexec() capability to the Cygwin port, and changes
a couple documentation files, as requested on this list.

Many thanks to Andy Piper, who did the coding for Cygwin XEmacs, which
I have just adapted here for GNU emacs.  The XEmacs unexec() solution
for Cygwin is pretty elegant.  It uses an array in the data segment
for malloc() before unexec(), thereby avoiding all sorts of
heap startup nastiness in a dumped emacs.  Dumping consists of
rewriting the .exe file with the data segment filled in,
and the data/bss boundary moved up to truncate bss down to a bare
minimum.

Note that these patches are mostly cvs-generated diffs, but there are
a couple new files at the end that are in raw diff format.

Joe Buehler

Index: lisp/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/ChangeLog,v
retrieving revision 1.4583
diff -u -r1.4583 ChangeLog
--- lisp/ChangeLog      4 Dec 2002 17:20:06 -0000       1.4583
+++ lisp/ChangeLog      10 Dec 2002 21:11:53 -0000
@@ -1,3 +1,7 @@
+2002-12-10  Joe Buehler  <address@hidden>
+
+       * loadup.el: Do not create versioned copy of dumped emacs under Cygwin.
+
 2002-12-04  Stefan Monnier  <address@hidden>

        * gdb-ui.el (gdb-inferior-io-mode-map): Remove (unused).
Index: lispref/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/lispref/ChangeLog,v
retrieving revision 1.55
diff -u -r1.55 ChangeLog
--- lispref/ChangeLog   4 Dec 2002 04:00:48 -0000       1.55
+++ lispref/ChangeLog   10 Dec 2002 21:11:53 -0000
@@ -1,3 +1,7 @@
+2002-12-10  Joe Buehler  <address@hidden>
+
+       * os.texi (System Environment): Added cygwin system-type.
+
 2002-12-04   Markus Rost  <address@hidden>

        * variables.texi (File Local Variables): Fix typo.
Index: src/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/src/ChangeLog,v
retrieving revision 1.2979
diff -u -r1.2979 ChangeLog
--- src/ChangeLog       4 Dec 2002 11:44:42 -0000       1.2979
+++ src/ChangeLog       10 Dec 2002 21:11:57 -0000
@@ -1,3 +1,20 @@
+2002-12-10  Joe Buehler  <address@hidden>
+
+       * unexcw.c: Copied file in from XEmacs 21.5.9 for Cygwin unexec().
+
+       * sheap.c: Copied file in from XEmacs 21.5.9 for Cygwin unexec().
+
+       * s/cygwin.h: Use unexcw.c for unexec().
+
+       * Makefile.in: Added sheap.c for Cygwin, moved allocators inside dumped
+       portion of address space.
+
+       * puresize.h: Check pure[] array bounds when deciding whether PURE_P().
+
+       * lastfile.c: Pad my_endbss[] to pagesize under Cygwin, so dumped bss 
meets minimum size.
+
+       * gmalloc.c (__default_morecore): Use more_static_core() under Cygwin 
before unexec().
+
 2002-12-04  Richard M. Stallman  <address@hidden>

        * sysdep.c (fcntl.h): Test only HAVE_FCNTL_H.
Index: lispref/os.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/lispref/os.texi,v
retrieving revision 1.50
diff -u -r1.50 os.texi
--- lispref/os.texi     14 Oct 2002 01:34:48 -0000      1.50
+++ lispref/os.texi     10 Dec 2002 20:57:24 -0000
@@ -613,6 +613,9 @@
 @item berkeley-unix
 Berkeley BSD.

address@hidden cygwin
+Cygwin.
+
 @item dgux
 Data General DGUX operating system.

Index: etc/NEWS
===================================================================
RCS file: /cvsroot/emacs/emacs/etc/NEWS,v
retrieving revision 1.756
diff -u -r1.756 NEWS
--- etc/NEWS    4 Dec 2002 11:05:53 -0000       1.756
+++ etc/NEWS    10 Dec 2002 20:56:41 -0000
@@ -55,6 +55,8 @@
 item was added to the menu bar that makes it easy accessible
 (Help->More Manuals->Introduction to Emacs Lisp).

+** Support for Cygwin was added.
+
 ---
 ** Support for AIX 5.1 was added.

Index: lisp/loadup.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/loadup.el,v
retrieving revision 1.126
diff -u -r1.126 loadup.el
--- lisp/loadup.el      30 Sep 2002 06:37:42 -0000      1.126
+++ lisp/loadup.el      10 Dec 2002 20:57:11 -0000
@@ -323,7 +323,7 @@
       (dump-emacs "emacs" "temacs")
       (message "%d pure bytes used" pure-bytes-used)
       ;; Recompute NAME now, so that it isn't set when we dump.
-      (if (not (memq system-type '(ms-dos windows-nt)))
+      (if (not (memq system-type '(ms-dos windows-nt cygwin)))
          (let ((name (concat "emacs-" emacs-version)))
            (while (string-match "[^-+_.a-zA-Z0-9]+" name)
              (setq name (concat (downcase (substring name 0 (match-beginning 
0)))
Index: src/gmalloc.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/gmalloc.c,v
retrieving revision 1.12
diff -u -r1.12 gmalloc.c
--- src/gmalloc.c       3 Nov 2001 15:08:36 -0000       1.12
+++ src/gmalloc.c       10 Dec 2002 20:57:31 -0000
@@ -352,6 +352,10 @@
 #include <errno.h>

 /* How to really get more memory.  */
+#if defined(CYGWIN)
+extern __ptr_t more_static_core PP ((ptrdiff_t __size));
+extern int static_heap_dumped;
+#endif
 __ptr_t (*__morecore) PP ((ptrdiff_t __size)) = __default_morecore;

 /* Debugging hook for `malloc'.  */
@@ -1572,7 +1576,13 @@
 __default_morecore (increment)
      __malloc_ptrdiff_t increment;
 {
-  __ptr_t result = (__ptr_t) __sbrk (increment);
+  __ptr_t result;
+#if defined(CYGWIN)
+  if (!static_heap_dumped) {
+    return more_static_core(increment);
+  }
+#endif
+  result = (__ptr_t) __sbrk (increment);
   if (result == (__ptr_t) -1)
     return NULL;
   return result;
Index: src/lastfile.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/lastfile.c,v
retrieving revision 1.7
diff -u -r1.7 lastfile.c
--- src/lastfile.c      17 Jan 1999 19:16:08 -0000      1.7
+++ src/lastfile.c      10 Dec 2002 20:57:36 -0000
@@ -40,10 +40,16 @@

 char my_edata[] = "End of Emacs initialized data";

-#ifdef WINDOWSNT
+#if defined(WINDOWSNT) || defined(CYGWIN)
 /* Help unexec locate the end of the .bss area used by Emacs (which
    isn't always a separate section in NT executables).  */
+#if defined(CYGWIN)
+/* Ensure there is enough slack in the .bss to pad with. */
+#define BSS_PADDING 0x1000
+char my_endbss[BSS_PADDING];
+#else
 char my_endbss[1];
+#endif

 /* The Alpha MSVC linker globally segregates all static and public bss
    data, so we must take both into account to determine the true extent
Index: src/puresize.h
===================================================================
RCS file: /cvsroot/emacs/emacs/src/puresize.h,v
retrieving revision 1.66
diff -u -r1.66 puresize.h
--- src/puresize.h      9 Aug 2002 05:08:00 -0000       1.66
+++ src/puresize.h      10 Dec 2002 20:57:37 -0000
@@ -68,7 +68,7 @@
 
 /* Define PURE_P.  */

-#ifdef VIRT_ADDR_VARIES
+#if defined(VIRT_ADDR_VARIES) || defined(CYGWIN)
 /* For machines like APOLLO where text and data can go anywhere
    in virtual memory.  */

Index: src/Makefile.in
===================================================================
RCS file: /cvsroot/emacs/emacs/src/Makefile.in,v
retrieving revision 1.262
diff -u -r1.262 Makefile.in
--- src/Makefile.in     25 Nov 2002 19:08:14 -0000      1.262
+++ src/Makefile.in     10 Dec 2002 20:57:26 -0000
@@ -539,6 +539,10 @@
 #endif
 #endif

+#ifdef CYGWIN
+CYGWIN_OBJ = sheap.o
+#endif
+
 #ifdef HAVE_CARBON
 XMENU_OBJ =
 MAC_OBJ = mac.o macterm.o macfns.o macmenu.o fontset.o
@@ -562,7 +566,7 @@
        process.o callproc.o \
        region-cache.o sound.o atimer.o \
        doprnt.o strftime.o intervals.o textprop.o composite.o md5.o \
-       $(MSDOS_OBJ) $(MAC_OBJ)
+       $(MSDOS_OBJ) $(MAC_OBJ) $(CYGWIN_OBJ)

 /* Object files used on some machine or other.
    These go in the DOC file on all machines
@@ -599,8 +603,6 @@
 rallocobj = ralloc.o
 #endif

-mallocobj = $(gmallocobj) $(rallocobj) vm-limit.o
-
 #endif /* SYSTEM_MALLOC */


@@ -619,7 +621,11 @@

 /* define otherobj as list of object files that make-docfile
    should not be told about.  */
-otherobj= $(termcapobj) lastfile.o $(mallocobj) $(allocaobj) $(widgetobj) 
$(LIBOBJS)
+#ifdef CYGWIN
+otherobj= $(termcapobj) $(gmallocobj) $(rallocobj) lastfile.o vm-limit.o 
$(allocaobj) $(widgetobj) $(LIBOBJS)
+#else
+otherobj= $(termcapobj) lastfile.o $(gmallocobj) $(rallocobj) vm-limit.o 
$(allocaobj) $(widgetobj) $(LIBOBJS)
+#endif

 #ifdef HAVE_MOUSE
 #define MOUSE_SUPPORT ${lispsource}mouse.elc \
--- src/s/cygwin.h      2002-12-05 16:47:00.000000000 -0500
+++ src/s/cygwin.h      2002-12-09 19:47:26.000000000 -0500
@@ -118,7 +118,7 @@
 /* -lutil comes from inetutils and has pty functions in it */
 #define LIBS_SYSTEM -lutil
 /* undumping is not implemented yet */
-#define CANNOT_DUMP 1
+#define UNEXEC unexcw.o
 #define POSIX_SIGNALS 1
 /* force the emacs image to start high in memory, so dll relocation
    can put things in low memory without causing all sorts of grief for
--- src/sheap.c 2002-12-09 19:43:01.000000000 -0500
+++ src/sheap.c 2002-12-09 17:41:32.000000000 -0500
@@ -0,0 +1,156 @@
+/* Static Heap management routines, copied from XEmacs 21.5.9.
+   Copyright (C) 1994, 1998 Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#include <config.h>
+#include <stdio.h>
+#include "lisp.h"
+
+#include <unistd.h>
+#define SHEAP_ADJUSTMENT 0 /* Xemacs build dynamically adjusts this... */
+
+#define STATIC_HEAP_BASE       0x800000
+#define STATIC_HEAP_SLOP       0xf0000
+#define STATIC_HEAP_SIZE \
+(STATIC_HEAP_BASE + SHEAP_ADJUSTMENT + STATIC_HEAP_SLOP)
+#define BLOCKSIZE      (1<<12)
+#define ALLOC_UNIT (BLOCKSIZE-1)
+#define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
+#define ALIGN_ALLOC(addr) ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
+
+char   static_heap_buffer[STATIC_HEAP_SIZE]={0};
+char*  static_heap_base=static_heap_buffer;
+char*  static_heap_ptr=static_heap_buffer;
+unsigned long  static_heap_size=STATIC_HEAP_SIZE;
+int    static_heap_initialized=0;
+int    static_heap_dumped=0;
+
+void *more_static_core ( ptrdiff_t increment );
+void *more_static_core ( ptrdiff_t increment )
+{
+  int size = (int) increment;
+  void *result;
+
+  if (!static_heap_initialized)
+    {
+#if 0
+#ifdef VALMASK
+      if (((unsigned long) static_heap_base & ~VALMASK) != 0)
+       {
+         printf ("error: The heap was allocated in upper memory.\n");
+         exit (-1);
+       }
+#endif
+#endif
+      static_heap_base=(char*)ALIGN_ALLOC(static_heap_buffer);
+      static_heap_ptr=static_heap_base;
+      static_heap_size=STATIC_HEAP_SIZE -
+       (static_heap_base-static_heap_buffer);
+#ifdef CYGWIN
+      sbrk(BLOCKSIZE);         /* force space for fork to work */
+#endif
+      static_heap_initialized=1;
+    }
+
+  result = static_heap_ptr;
+
+  /* we don't need to align - handled by gmalloc.  */
+
+  if (size < 0)
+    {
+      if (static_heap_ptr + size < static_heap_base)
+       {
+         return 0;
+       }
+    }
+  else
+    {
+      if (static_heap_ptr + size >= static_heap_base + static_heap_size)
+       {
+         printf(
+
+"\nRequested %d bytes, static heap exhausted!  base is %p, current ptr
+is %p. You have exhausted the static heap.
+
+If you are simply trying to compile, remove sheap-adjust.h
+and recompile from the top level. If this doesn't
+work then STATIC_HEAP_SLOP (defined in this file) is too small.
+
+If you want to run temacs, change SHEAP_ADJUSTMENT in sheap-adjust.h
+to 0 or a +ve number. Generally you should *not* try to run temacs
+with a static heap, you should dump first.\n", size,
+static_heap_base, static_heap_ptr);
+
+         exit(-1);
+         return 0;
+       }
+    }
+  static_heap_ptr += size;
+
+  return result;
+}
+
+static void
+sheap_adjust_h (long adjust)
+{
+  FILE *stream = fopen ("sheap-adjust.h", "w");
+
+  if (stream == NULL)
+    report_file_error ("Opening sheap adjustment file",
+                      build_string ("sheap-adjust.h"));
+
+  fprintf (stream,
+          "/*\tDo not edit this file!\n"
+          "\tAutomatically generated by XEmacs */\n"
+          "# define SHEAP_ADJUSTMENT (%ld)\n", adjust);
+  fclose (stream);
+}
+
+void report_sheap_usage (int die_if_pure_storage_exceeded);
+void
+report_sheap_usage (int die_if_pure_storage_exceeded)
+{
+  int rc = 0;
+
+  int lost = STATIC_HEAP_SIZE
+    - (static_heap_ptr - static_heap_buffer);
+  char buf[200];
+  sprintf (buf, "Static heap usage: %ld of %ld, slop is %ld",
+               (long) (static_heap_ptr - static_heap_buffer),
+          (long) (STATIC_HEAP_SIZE),
+          (long) STATIC_HEAP_SLOP);
+
+  if (lost > STATIC_HEAP_SLOP) {
+    sprintf (buf + strlen (buf), " -- %ldk wasted", (long)(lost/1024));
+    if (die_if_pure_storage_exceeded) {
+      sheap_adjust_h(STATIC_HEAP_SLOP - lost);
+      sprintf (buf + strlen (buf), " -- reset to %ldk",
+              (long) (STATIC_HEAP_SIZE + STATIC_HEAP_SLOP - lost));
+      rc = -1;
+    }
+    message ("%s", buf);
+  }
+
+  if (rc < 0) {
+    unlink("SATISFIED");
+    fatal ("Static heap size adjusted, Don't Panic!  I will restart the 
`make'");
+  }
+}
+
+
--- src/unexcw.c        2002-12-09 19:44:50.000000000 -0500
+++ src/unexcw.c        2002-12-09 17:17:34.000000000 -0500
@@ -0,0 +1,480 @@
+/* unexec for GNU Emacs on Cygwin, copied from XEmacs 21.5.9.
+   Copyright (C) 1994, 1998 Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+/* This is a complete rewrite, some code snarfed from unexnt.c and
+   unexec.c, Andy Piper (address@hidden) 13-1-98 */
+
+#include <config.h>
+#include "lisp.h"
+#include "stdio.h"
+#include "fcntl.h"
+
+#define PERROR(arg)                            \
+do {                                           \
+  perror (arg);                                        \
+  exit (-1);                                   \
+} while (0)
+
+#ifdef MINGW
+#include <../../include/a.out.h>
+#else
+#include <a.out.h>
+#endif
+
+#define ALLOC_UNIT 0xFFFF
+#define ALLOC_MASK ~((unsigned long) (ALLOC_UNIT))
+#define ALIGN_ALLOC(addr) \
+((((unsigned long) addr) + ALLOC_UNIT) & ALLOC_MASK)
+/* Note that all sections must be aligned on a 0x1000 boundary so
+   this is the minimum size that our dummy bss can be. */
+#define BSS_PAD_SIZE   0x1000
+
+/* To prevent zero-initialized variables from being placed into the bss
+   section, use non-zero values to represent an uninitialized state.  */
+#define UNINIT_PTR ((void *) 0xF0A0F0A0)
+#define UNINIT_LONG (0xF0A0F0A0L)
+
+static void get_section_info (int a_out, char* a_name);
+static void copy_executable_and_dump_data_section (int a_out, int a_new);
+static void dup_file_area (int a_out, int a_new, long size);
+#if 0
+static void write_int_to_bss (int a_out, int a_new, void* va, void* newval);
+#endif
+
+/* Cached info about the .data section in the executable.  */
+void *data_start_va = UNINIT_PTR;
+long data_size = UNINIT_LONG;
+
+/* Cached info about the .bss section in the executable.  */
+void *bss_start = UNINIT_PTR;
+long bss_size = UNINIT_LONG;
+int sections_reversed = 0;
+FILHDR f_hdr;
+PEAOUTHDR f_ohdr;
+SCNHDR f_data, f_bss, f_text, f_nextdata;
+
+#define CHECK_AOUT_POS(a)                              \
+do {                                                   \
+  if (lseek (a_out, 0, SEEK_CUR) != a)                 \
+    {                                                  \
+      printf ("we are at %lx, should be at %lx\n",   \
+             lseek (a_out, 0, SEEK_CUR), a);           \
+      exit (-1);                                       \
+    }                                                  \
+} while (0)
+
+/* Dump out .data and .bss sections into a new executable.  */
+int
+unexec (char *out_name, char *in_name, unsigned start_data,
+       unsigned d1, unsigned d2)
+{
+  /* ugly nt hack - should be in lisp */
+  int a_new, a_out = -1;
+  char new_name[FILENAME_MAX], a_name[FILENAME_MAX];
+  char *ptr;
+
+  /* Make sure that the input and output filenames have the
+     ".exe" extension...patch them up if they don't.  */
+  strcpy (a_name, in_name);
+  ptr = a_name + strlen (a_name) - 4;
+  if (strcmp (ptr, ".exe"))
+    strcat (a_name, ".exe");
+
+  strcpy (new_name, out_name);
+  ptr = new_name + strlen (new_name) - 4;
+  if (strcmp (ptr, ".exe"))
+    strcat (new_name, ".exe");
+
+  /* We need to round off our heap to NT's allocation unit (64KB).  */
+  /* round_heap (get_allocation_unit ()); */
+
+  if (a_name && (a_out = open (a_name, O_RDONLY | O_BINARY)) < 0)
+    PERROR (a_name);
+
+  if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
+                    0755)) < 0)
+    PERROR (new_name);
+
+  /* Get the interesting section info, like start and size of .bss...  */
+  get_section_info (a_out, a_name);
+
+  copy_executable_and_dump_data_section (a_out, a_new);
+
+  close (a_out);
+  close (a_new);
+  return 0;
+}
+
+/* Flip through the executable and cache the info necessary for dumping.  */
+static void
+get_section_info (int a_out, char* a_name)
+{
+  extern char my_endbss[];
+  /* From lastfile.c  */
+  extern char my_edata[];
+
+  if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
+    PERROR (a_name);
+
+  if (f_hdr.e_magic != DOSMAGIC)
+    PERROR ("unknown exe header");
+
+  /* Check the NT header signature ...  */
+  if (f_hdr.nt_signature != NT_SIGNATURE)
+    PERROR ("invalid nt header");
+
+  /* Flip through the sections for .data and .bss ...  */
+  if (f_hdr.f_opthdr > 0)
+    {
+      if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ)
+       PERROR (a_name);
+    }
+  /* Loop through .data & .bss section headers, copying them in.
+     With newer lds these are reversed so we have to cope with both */
+  lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
+
+  if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text)
+      || strcmp (f_text.s_name, ".text"))
+    PERROR ("no .text section");
+
+  /* The .bss section.  */
+  if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
+      || (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data")))
+    PERROR ("no .bss / .data section");
+
+  /* check for reversed .bss and .data */
+  if (!strcmp (f_bss.s_name, ".data"))
+    {
+      printf (".data and .bss reversed\n");
+      sections_reversed = 1;
+      memcpy (&f_data, &f_bss, sizeof (f_bss));
+    }
+
+  /* The .data section.  */
+  if (!sections_reversed)
+    {
+      if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data)
+         || strcmp (f_data.s_name, ".data"))
+       PERROR ("no .data section");
+    }
+  else
+    {
+      if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
+         || strcmp (f_bss.s_name, ".bss"))
+       PERROR ("no .bss section");
+    }
+
+  bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr);
+  bss_size = (unsigned long)((char*)&my_endbss-(char*)bss_start);
+
+  /* must keep bss data that we want to be blank as blank */
+  printf ("found bss - keeping %lx of %lx bytes\n", bss_size, f_ohdr.bsize);
+
+  /* The .data section.  */
+  data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr);
+
+  /* We want to only write Emacs data back to the executable,
+     not any of the library data (if library data is included,
+     then a dumped Emacs won't run on system versions other
+     than the one Emacs was dumped on).  */
+  data_size = (unsigned long)my_edata - (unsigned long)data_start_va;
+  printf ("found data - keeping %lx of %lx bytes\n", data_size, f_ohdr.dsize);
+
+  /* The following data section - often .idata */
+  if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata)
+      && strcmp (&f_nextdata.s_name[2], "data"))
+    PERROR ("no other data section");
+}
+
+/* The dump routines.  */
+
+static void
+copy_executable_and_dump_data_section (int a_out, int a_new)
+{
+  long size = 0;
+  /* NOTE: Some of these were previously declared as unsigned long,
+     but the ones changed to long represent file sizes or pointers,
+     which can't reasonably get above 2G. (A 2G executable???)
+     Furthermore, some were even being compared as in if (x < 0) ... */
+  long new_data_size, new_bss_size, bss_padding, file_sz_change;
+  long data_padding = 0;
+  long f_data_s_scnptr = f_data.s_scnptr;
+  long f_nextdata_s_scnptr = f_nextdata.s_scnptr;
+  unsigned long f_data_s_vaddr = f_data.s_vaddr;
+  unsigned long f_bss_s_vaddr = f_bss.s_vaddr;
+
+  int i;
+  void* empty_space;
+  extern int static_heap_dumped;
+  SCNHDR section;
+  /* calculate new sizes:
+
+     f_ohdr.dsize is the total initialized data size on disk which is
+     f_data.s_size + f_idata.s_size.
+
+     f_ohdr.data_start is the base addres of all data and so should
+     not be changed.
+
+     *.s_vaddr is the virtual address of the start of the section
+     *normalized from f_ohdr.ImageBase.
+
+     *.s_paddr appears to be the number of bytes in the section
+     *actually used (whereas *.s_size is aligned).
+
+     bsize is now 0 since subsumed into .data
+     dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr)
+     f_data.s_vaddr is f_bss.s_vaddr
+     f_data.s_size is new dsize maybe.
+     what about s_paddr & s_scnptr?  */
+
+  /* this is the amount the file increases in size */
+  if (!sections_reversed)
+    {
+      new_bss_size = f_data.s_vaddr - f_bss.s_vaddr;
+      data_padding = 0;
+    }
+  else
+    {
+      new_bss_size = f_nextdata.s_vaddr - f_bss.s_vaddr;
+      data_padding = (f_bss.s_vaddr - f_data.s_vaddr) - f_data.s_size;
+    }
+
+  if ((new_bss_size - bss_size) < BSS_PAD_SIZE)
+    PERROR (".bss free space too small");
+
+  file_sz_change = (new_bss_size + data_padding) - BSS_PAD_SIZE;
+  new_data_size = f_ohdr.dsize + file_sz_change;
+
+  if (!sections_reversed)
+    f_data.s_vaddr = f_bss.s_vaddr;
+  f_data.s_paddr += file_sz_change;
+#if 0
+  if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize)
+    printf ("section size doesn't tally with dsize %lx != %lx\n",
+          f_data.s_size + f_nextdata.s_size, f_ohdr.dsize);
+#endif
+  f_data.s_size += file_sz_change;
+  lseek (a_new, 0, SEEK_SET);
+  /* write file header */
+  f_hdr.f_symptr += file_sz_change;
+#ifdef NO_DEBUG
+  f_hdr.f_nscns--;
+#endif
+
+  printf ("writing file header\n");
+  if (write (a_new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
+    PERROR ("failed to write file header");
+  /* write optional header fixing dsize & bsize*/
+  printf ("writing optional header\n");
+  printf ("new data size is %lx, >= %lx\n", new_data_size,
+        f_ohdr.dsize + f_ohdr.bsize);
+  if (new_data_size < (long) (f_ohdr.dsize + f_ohdr.bsize))
+    printf ("warning: new data size is < approx\n");
+  f_ohdr.dsize=new_data_size;
+  f_ohdr.bsize=BSS_PAD_SIZE;
+  if (write (a_new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
+    PERROR ("failed to write optional header");
+  /* write text as is */
+  printf ("writing text header (unchanged)\n");
+
+  if (write (a_new, &f_text, sizeof (f_text)) != sizeof (f_text))
+    PERROR ("failed to write text header");
+#ifndef NO_DEBUG
+  /* Write small bss section. */
+  if (!sections_reversed)
+    {
+      f_bss.s_size = BSS_PAD_SIZE;
+      f_bss.s_paddr = BSS_PAD_SIZE;
+      f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE;
+      if (write (a_new, &f_bss, sizeof (f_bss)) != sizeof (f_bss))
+       PERROR ("failed to write bss header");
+    }
+#endif
+  /* write new data header */
+  printf ("writing .data header\n");
+
+  if (write (a_new, &f_data, sizeof (f_data)) != sizeof (f_data))
+    PERROR ("failed to write data header");
+#ifndef NO_DEBUG
+  /* Write small bss section. */
+  if (sections_reversed)
+    {
+      f_bss.s_size = BSS_PAD_SIZE;
+      f_bss.s_paddr = BSS_PAD_SIZE;
+      f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE;
+      if (write (a_new, &f_bss, sizeof (f_bss)) != sizeof (f_bss))
+       PERROR ("failed to write bss header");
+    }
+#endif
+  printf ("writing following data header\n");
+  f_nextdata.s_scnptr += file_sz_change;
+  if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change;
+  if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change;
+  if (write (a_new, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata))
+    PERROR ("failed to write nextdata header");
+
+  /* copy other section headers adjusting the file offset */
+  for (i=0; i<(f_hdr.f_nscns-3); i++)
+    {
+      if (read (a_out, &section, sizeof (section)) != sizeof (section))
+       PERROR ("no .data section");
+
+      section.s_scnptr += file_sz_change;
+      if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
+      if (section.s_relptr != 0) section.s_relptr += file_sz_change;
+
+      if (write (a_new, &section, sizeof (section)) != sizeof (section))
+       PERROR ("failed to write data header");
+    }
+#ifdef NO_DEBUG
+  /* dump bss to maintain offsets */
+  memset (&f_bss, 0, sizeof (f_bss));
+  if (write (a_new, &f_bss, sizeof (f_bss)) != sizeof (f_bss))
+    PERROR ("failed to write bss header");
+#endif
+  size = lseek (a_new, 0, SEEK_CUR);
+  CHECK_AOUT_POS (size);
+
+  /* copy eveything else until start of data */
+  size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR);
+
+  printf ("copying executable up to data section ... %lx bytes\n",
+         size);
+  dup_file_area (a_out, a_new, size);
+
+  CHECK_AOUT_POS (f_data_s_scnptr);
+
+  if (!sections_reversed)
+    {
+      /* dump bss + padding between sections, sans small bss pad */
+      printf ("dumping .bss into executable... %lx bytes\n", bss_size);
+      if (write (a_new, bss_start, bss_size) != bss_size)
+       {
+         PERROR ("failed to write bss section");
+       }
+
+      /* pad, needs to be zero */
+      bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE;
+      if (bss_padding < 0)
+       PERROR ("padded .bss too small");
+      printf ("padding .bss ... %lx bytes\n", bss_padding);
+      empty_space = malloc (bss_padding);
+      memset (empty_space, 0, bss_padding);
+      if (write (a_new, empty_space, bss_padding) != bss_padding)
+       PERROR ("failed to write bss section");
+      free (empty_space);
+    }
+
+  /* tell dumped version not to free pure heap */
+  static_heap_dumped = 1;
+  /* Get a pointer to the raw data in our address space.  */
+  printf ("dumping .data section... %lx bytes\n", data_size);
+  if (write (a_new, data_start_va, data_size) != data_size)
+    PERROR ("failed to write data section");
+  /* were going to use free again ... */
+  static_heap_dumped = 0;
+
+  size = lseek (a_out, f_data_s_scnptr + data_size, SEEK_SET);
+
+  if (!sections_reversed)
+    {
+      size = f_nextdata_s_scnptr - size;
+      dup_file_area (a_out, a_new, size);
+    }
+  else
+    {
+      /* need to pad to bss with data in file */
+      printf ("padding .data ... %lx bytes\n", data_padding);
+      size = (f_bss_s_vaddr - f_data_s_vaddr) - data_size;
+      dup_file_area (a_out, a_new, size);
+
+      /* dump bss + padding between sections */
+      printf ("dumping .bss into executable... %lx bytes\n", bss_size);
+      if (write (a_new, bss_start, bss_size) != bss_size)
+       PERROR ("failed to write bss section");
+
+      /* pad, needs to be zero */
+      bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE;
+      if (bss_padding < 0)
+       PERROR ("padded .bss too small");
+      printf ("padding .bss ... %lx bytes\n", bss_padding);
+      empty_space = malloc (bss_padding);
+      memset (empty_space, 0, bss_padding);
+      if (write (a_new, empty_space, bss_padding) != bss_padding)
+       PERROR ("failed to write bss section");
+      free (empty_space);
+      if (lseek (a_new, 0, SEEK_CUR) != (long) f_nextdata.s_scnptr)
+       {
+         printf ("at %lx should be at %lx\n",
+                lseek (a_new, 0, SEEK_CUR),
+                f_nextdata.s_scnptr);
+         PERROR ("file positioning error\n");
+       }
+      lseek (a_out, f_nextdata_s_scnptr, SEEK_SET);
+    }
+
+  CHECK_AOUT_POS (f_nextdata_s_scnptr);
+
+  /* now dump - nextdata don't need to do this cygwin ds is in .data! */
+  printf ("dumping following data section... %lx bytes\n", f_nextdata.s_size);
+
+  dup_file_area (a_out,a_new,f_nextdata.s_size);
+
+  /* write rest of file */
+  printf ("writing rest of file\n");
+  size = lseek (a_out, 0, SEEK_END);
+  size = size - (f_nextdata_s_scnptr + f_nextdata.s_size); /* length remaining 
in a_out */
+  lseek (a_out, f_nextdata_s_scnptr + f_nextdata.s_size, SEEK_SET);
+
+  dup_file_area (a_out, a_new, size);
+}
+
+/*
+ * copy from aout to anew
+ */
+static void
+dup_file_area (int a_out, int a_new, long size)
+{
+  char page[BUFSIZ];
+  long n;
+  for (; size > 0; size -= sizeof (page))
+    {
+      n = size > (long) sizeof (page) ? sizeof (page) : size;
+      if (read (a_out, page, n) != n || write (a_new, page, n) != n)
+       PERROR ("dump_out()");
+    }
+}
+
+#if 0
+static void
+write_int_to_bss (int a_out, int a_new, void* va, void* newval)
+{
+  int cpos;
+
+  cpos = lseek (a_new, 0, SEEK_CUR);
+  if (va < bss_start || va > bss_start + f_data.s_size)
+    PERROR ("address not in data space\n");
+  lseek (a_new, f_data.s_scnptr + ((unsigned long)va -
+                                 (unsigned long)bss_start), SEEK_SET);
+  if (write (a_new, newval, sizeof (int)) != (int) sizeof (int))
+    PERROR ("failed to write int value");
+  lseek (a_new, cpos, SEEK_SET);
+}
+#endif






reply via email to

[Prev in Thread] Current Thread [Next in Thread]