]> code.delx.au - gnu-emacs/blobdiff - src/unexec.c
*** empty log message ***
[gnu-emacs] / src / unexec.c
index 7e2760159432063bf40d14e6a2ccb1ee752edb23..dadea560e0c75ca97008a4f589b6c17be315e12b 100644 (file)
@@ -134,7 +134,7 @@ before writing it (above and beyond the number of bytes of actual
 program text).  HDR's standard fields are already correct, except that
 this adjustment to the `a_text' field has not yet been made;
 thus, the amount of offset can depend on the data in the file.
-  
+
 * A_TEXT_SEEK(HDR)
 
 If defined, this macro specifies the number of bytes to seek into the
@@ -171,15 +171,14 @@ pointer looks like an int) but not on all machines.
 
 #ifndef CANNOT_DUMP  /* all rest of file!  */
 
-#ifdef COFF_ENCAPSULATE
-int need_coff_header = 1;
-#include <coff-encap/a.out.encap.h> /* The location might be a poor assumption */
-#else
+#if defined(COFF) && defined(HAVE_COFF_H)
+#include <coff.h>
 #ifdef MSDOS
 #if __DJGPP__ > 1
 #include <fcntl.h>  /* for O_RDONLY, O_RDWR */
-#endif
-#include <coff.h>
+#include <crt0.h>   /* for _crt0_startup_flags and its bits */
+static int save_djgpp_startup_flags;
+#endif /* __DJGPP__ > 1 */
 #define filehdr external_filehdr
 #define scnhdr external_scnhdr
 #define syment external_syment
@@ -197,12 +196,15 @@ struct aouthdr
   unsigned long                text_start;/* base of text used for this file */
   unsigned long                data_start;/* base of data used for this file */
 };
-
-
-#else /* not MSDOS */
-#include <a.out.h>
 #endif /* not MSDOS */
-#endif
+#else  /* not COFF */
+#ifdef COFF_ENCAPSULATE
+int need_coff_header = 1;
+#include <coff-encap/a.out.encap.h> /* The location might be a poor assumption */
+#else  /* not COFF_ENCAPSULATE */
+#include <a.out.h>
+#endif /* not COFF_ENCAPSULATE */
+#endif /* not COFF */
 
 /* Define getpagesize if the system does not.
    Note that this may depend on symbols defined in a.out.h.  */
@@ -243,6 +245,8 @@ long lnnoptr;                       /* Pointer to line-number info within file */
 static long text_scnptr;
 static long data_scnptr;
 
+static long coff_offset;
+
 #else /* not COFF */
 
 #ifdef HPUX
@@ -284,7 +288,7 @@ static EXEC_HDR_TYPE hdr, ohdr;
 
 #else /* not HPUX */
 
-#if defined (USG) && !defined (IBMAIX) && !defined (IRIS) && !defined (COFF_ENCAPSULATE) && !defined (LINUX)
+#if defined (USG) && !defined (IBMAIX) && !defined (IRIS) && !defined (COFF_ENCAPSULATE) && !defined (GNU_LINUX)
 static struct bhdr hdr, ohdr;
 #define a_magic fmagic
 #define a_text tsize
@@ -395,7 +399,7 @@ unexec (new_name, a_name, data_start, bss_start, entry_address)
     {
       close (new);
       /* unlink (new_name);            /* Failed, unlink new a.out */
-      return -1;       
+      return -1;
     }
 
   close (new);
@@ -474,9 +478,32 @@ make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
     }
 
 #ifdef COFF
+  coff_offset = 0L;            /* stays zero, except in DJGPP */
+
   /* Salvage as much info from the existing file as possible */
   if (a_out >= 0)
     {
+#ifdef MSDOS
+#if __DJGPP__ > 1
+      /* Support the coff-go32-exe format with a prepended stub, since
+        this is what GCC 2.8.0 and later generates by default in DJGPP.  */
+      unsigned short mz_header[3];
+
+      if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
+       {
+         PERROR (a_name);
+       }
+      if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
+       {
+         coff_offset = (long)mz_header[2] * 512L;
+         if (mz_header[1])
+           coff_offset += (long)mz_header[1] - 512L;
+         lseek (a_out, coff_offset, 0);
+       }
+      else
+       lseek (a_out, 0L, 0);
+#endif /* __DJGPP__ > 1 */
+#endif /* MSDOS */
       if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
        {
          PERROR (a_name);
@@ -491,7 +518,7 @@ make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
          block_copy_start += sizeof (f_ohdr);
        }
       /* Loop through section headers, copying them in */
-      lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
+      lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
       for (scns = f_hdr.f_nscns; scns > 0; scns--) {
        if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
          {
@@ -794,7 +821,7 @@ make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
       PERROR (new_name);
     }
 
-#if 0 /* This #ifndef caused a bug on Linux when using QMAGIC.  */
+#if 0 /* This #ifndef caused a bug on GNU/Linux when using QMAGIC.  */
   /* This adjustment was done above only #ifndef NO_REMAP,
      so only undo it now #ifndef NO_REMAP.  */
   /* #ifndef NO_REMAP  */
@@ -884,6 +911,11 @@ copy_text_and_data (new, a_out)
   /* Dump the original table of exception handlers, not the one
      where our exception hooks are registered.  */
   __djgpp_exception_toggle ();
+
+  /* Switch off startup flags that might have been set at runtime
+     and which might change the way that dumped Emacs works.  */
+  save_djgpp_startup_flags = _crt0_startup_flags;
+  _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR);
 #endif
 #endif
 
@@ -905,6 +937,9 @@ copy_text_and_data (new, a_out)
 #if __DJGPP__ >= 2
   /* Restore our exception hooks.  */
   __djgpp_exception_toggle ();
+
+  /* Restore the startup flags.  */
+  _crt0_startup_flags = save_djgpp_startup_flags;
 #endif
 #endif
 
@@ -957,7 +992,7 @@ copy_text_and_data (new, a_out)
     char c;
     int mcount_address, mcount_offset, count;
     extern char *_execname;
-   
+
 
     /* The use of _execname is incompatible with RISCiX 1.1 */
     sprintf (command, "nm %s | fgrep mcount", _execname);
@@ -977,7 +1012,7 @@ copy_text_and_data (new, a_out)
     {
       sprintf (errbuf, "Failed to execute the command '%s'\n", command);
       PERROR (errbuf);
-    }  
+    }
 
     sscanf(address_text, "%x", &mcount_address);
     ptr = (char *) unexec_text_start;
@@ -1032,7 +1067,9 @@ write_segment (new, ptr, end)
 {
   register int i, nwrite, ret;
   char buf[80];
+#ifndef USE_CRT_DLL
   extern int errno;
+#endif
   /* This is the normal amount to write at once.
      It is the size of block that NFS uses.  */
   int writesize = 1 << 13;
@@ -1103,10 +1140,12 @@ copy_sym (new, a_out, a_name, new_name)
 
 #ifdef COFF
   if (lnnoptr)                 /* if there is line number info */
-    lseek (a_out, lnnoptr, 0); /* start copying from there */
+    lseek (a_out, coff_offset + lnnoptr, 0);   /* start copying from there */
   else
-#endif /* COFF */
-    lseek (a_out, SYMS_START, 0);      /* Position a.out to symtab. */
+    lseek (a_out, coff_offset + SYMS_START, 0);        /* Position a.out to symtab. */
+#else  /* not COFF */
+  lseek (a_out, SYMS_START, 0);        /* Position a.out to symtab. */
+#endif /* not COFF */
 
   while ((n = read (a_out, page, sizeof page)) > 0)
     {
@@ -1226,3 +1265,6 @@ adjust_lnnoptrs (writedesc, readdesc, new_name)
 #endif /* COFF */
 
 #endif /* not CANNOT_DUMP */
+
+/* arch-tag: 62409b69-e27a-4a7c-9413-0210d6b54e7f
+   (do not change this comment) */