2003-08-30 12:47:54 -04:00
|
|
|
/* dump.c
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@zoy.org>, Paris
|
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "config.h"
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
#ifdef CAN_DUMP
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "kernel.h"
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
extern void Check_If_Dump_Works ();
|
|
|
|
extern void Flush_Output (Object);
|
|
|
|
extern void Close_All_Files ();
|
|
|
|
extern void Generational_GC_Finalize ();
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
extern int errno;
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Set_File_Executable (int, char *);
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
Object Dump_Control_Point;
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Init_Dump () {
|
2003-08-19 15:19:38 -04:00
|
|
|
Dump_Control_Point = Null;
|
|
|
|
Global_GC_Link (Dump_Control_Point);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GENERATIONAL_GC
|
|
|
|
# define GC_FINALIZE Generational_GC_Finalize()
|
|
|
|
#else
|
|
|
|
# define GC_FINALIZE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define Dump_Prolog \
|
|
|
|
Object ret;\
|
|
|
|
int ofd, afd;\
|
|
|
|
char *ofn;\
|
|
|
|
GC_Node;\
|
|
|
|
\
|
|
|
|
Check_If_Dump_Works ();\
|
|
|
|
if (!EQ (Curr_Input_Port, Standard_Input_Port) ||\
|
2003-09-02 04:12:11 -04:00
|
|
|
!EQ (Curr_Output_Port, Standard_Output_Port))\
|
|
|
|
Primitive_Error ("cannot dump with current ports redirected");\
|
2003-08-19 15:19:38 -04:00
|
|
|
Flush_Output (Curr_Output_Port);\
|
|
|
|
Close_All_Files ();\
|
|
|
|
GC_FINALIZE;\
|
|
|
|
\
|
|
|
|
GC_Link (ofile);\
|
|
|
|
ret = Internal_Call_CC (1, Null);\
|
|
|
|
if (Truep (ret))\
|
2003-09-02 04:12:11 -04:00
|
|
|
return ret;\
|
2003-08-19 15:19:38 -04:00
|
|
|
GC_Unlink;\
|
|
|
|
\
|
|
|
|
Disable_Interrupts;\
|
|
|
|
\
|
|
|
|
ofn = Get_Strsym (ofile);\
|
|
|
|
if ((ofd = open (ofn, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666)) == -1) {\
|
2003-09-02 04:12:11 -04:00
|
|
|
Saved_Errno = errno;\
|
|
|
|
Primitive_Error ("cannot open ~s: ~E", ofile);\
|
2003-08-19 15:19:38 -04:00
|
|
|
}\
|
|
|
|
if ((afd = open (A_Out_Name, O_RDONLY|O_BINARY)) == -1) {\
|
2003-09-02 04:12:11 -04:00
|
|
|
Saved_Errno = errno;\
|
|
|
|
close (ofd);\
|
|
|
|
Primitive_Error ("cannot open a.out file: ~E");\
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define Dump_Finalize Saved_Errno = errno; close (afd); close (ofd)
|
2003-08-19 15:24:23 -04:00
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
#define Dump_Epilog {\
|
|
|
|
close (afd);\
|
|
|
|
Set_File_Executable (ofd, ofn);\
|
|
|
|
close (ofd);\
|
|
|
|
Enable_Interrupts;\
|
|
|
|
return False;\
|
|
|
|
}
|
|
|
|
|
2003-09-06 11:30:43 -04:00
|
|
|
#if defined(HAVE_ELF)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "dump-elf.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(ECOFF)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "dump-ecoff.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(HP9K)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "dump-hp9k.c"
|
|
|
|
#else
|
|
|
|
# include "dump-vanilla.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*ARGSUSED1*/
|
2003-08-19 15:24:23 -04:00
|
|
|
void Set_File_Executable (int fd, char *fn) {
|
2003-08-19 15:19:38 -04:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat (fd, &st) != -1) {
|
2003-09-02 04:12:11 -04:00
|
|
|
int omask = umask (0);
|
|
|
|
(void)umask (omask);
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifdef FCHMOD_BROKEN
|
2003-09-02 04:12:11 -04:00
|
|
|
(void)chmod (fn, (st.st_mode & 0777) | (0111 & ~omask));
|
2003-08-19 15:19:38 -04:00
|
|
|
#else
|
2003-09-02 04:12:11 -04:00
|
|
|
(void)fchmod (fd, (st.st_mode & 0777) | (0111 & ~omask));
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CAN_DUMP */
|