mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-02-19 03:17:36 +00:00
* src/alloc.c, src/bidi.c, src/emacs-module.c, src/pdumper.c: * src/regex-emacs.c, src/unexhp9k800.c, src/unexmacosx.c: * src/widget.c, src/xdisp.c, src/xselect.c, src/xterm.c: Include sysstdio.h instead of stdio.h, to avoid locking stdio streams in many cases. * src/alloc.c (test_setjmp): * src/bidi.c (bidi_dump_cached_states): * src/cm.c (calccost): * src/dispnew.c (init_display_interactive): * src/emacs.c (main): * src/image.c (convert_mono_to_color_image): * src/minibuf.c (read_minibuf_noninteractive): * src/nsfont.m (ns_descriptor_to_entity) (ns_dump_glyphstring): * src/nsterm.h (NSTRACE_MSG_NO_DASHES): * src/nsterm.m (ns_mouse_position) (sendEvent:, keyDown:, performDragOperation:): * src/pdumper.c (dump_fingerprint, print_paths_to_root_1): * src/print.c (debug_print): * src/regex-emacs.c (debug_putchar, print_fastmap) (print_partial_compiled_pattern, print_compiled_pattern) (print_double_string, regex_compile): * src/term.c (vfatal): * src/unexhp9k800.c (read_header): * src/unexmacosx.c (unexec_error): * src/widget.c (EmacsFrameInitialize): * src/xdisp.c (message_to_stderr, vmessage, dump_glyph_row) (Fdump_glyph_matrix, Fdump_frame_glyph_matrix, dump_glyph_string): * src/xfaces.c (Fdump_colors, Fdump_face): * src/xselect.c (x_clipboard_manager_error_2): * src/xterm.c (x_initialize): * src/xwidget.c (WEBKIT_FN_INIT): Prefer unlocked calls like fputs to locked calls like fprintf. * src/charset.c (read_hex): * src/cm.c (cmputc, cmcheckmagic): * src/dispnew.c (update_frame, update_frame_with_menu) (update_frame_1, Fsend_string_to_terminal, Fding) (bitch_at_user): * src/emacs.c (main, Fdump_emacs): * src/emacs-module.c (module_abort): * src/fileio.c (Fdo_auto_save): * src/image.c (slurp_file) (png_read_from_file, png_load_body, our_stdio_fill_input_buffer): * src/keyboard.c (record_char, kbd_buffer_get_event) (handle_interrupt): * src/lread.c (readbyte_from_stdio, read1): * src/minibuf.c (read_minibuf_noninteractive): * src/print.c (printchar_to_stream, strout) (Fredirect_debugging_output): * src/sysdep.c (reset_sys_modes, close_output_streams) (procfs_ttyname, procfs_get_total_memory): * src/term.c (tty_ring_bell, tty_send_additional_strings) (tty_set_terminal_modes, tty_reset_terminal_modes) (tty_update_end, tty_clear_end_of_line, tty_write_glyphs) (tty_write_glyphs_with_face, tty_insert_glyphs) (tty_menu_activate): * src/xfaces.c (Fx_load_color_file): Simplify by using ordinary calls like putc to explicitly-unlocked calls like putc_unlocked, since the ordinary calls are now unlocked anyway. * src/emacs.c (main, Fdump_emacs): * src/pdumper.c (Fdump_emacs_portable): Coalesce adjacent printfs. * src/nsterm.h: Include sysstdio.h as this file’s macros rely on it. * src/regex-emacs.c (print_compiled_pattern): Omit redundant fflush. * src/sysstdio.h: Include unlocked-io.h. (clearerr_unlocked, feof_unlocked, ferror_unlocked) (fflush_unlocked, fgets_unlocked, fputc_unlocked) (fputs_unlocked, fread_unlocked, fwrite_unlocked) (getc_unlocked, getchar_unlocked, putc_unlocked) (putchar_unlocked): Remove these macros; now done by unlocked-io.h. * src/xwidget.c: Include sysstdio.h.
324 lines
9.6 KiB
C
324 lines
9.6 KiB
C
/* Unexec for HP 9000 Series 800 machines.
|
||
|
||
This file is in the public domain.
|
||
|
||
Author: John V. Morris
|
||
|
||
This file was written by John V. Morris at Hewlett Packard.
|
||
Both the author and Hewlett Packard Co. have disclaimed the
|
||
copyright on this file, and it is therefore in the public domain.
|
||
(Search for "hp9k800" in copyright.list.)
|
||
*/
|
||
|
||
/*
|
||
Bob Desinger <hpsemc!bd@hplabs.hp.com>
|
||
|
||
Note that the GNU project considers support for HP operation a
|
||
peripheral activity which should not be allowed to divert effort
|
||
from development of the GNU system. Changes in this code will be
|
||
installed when users send them in, but aside from that we don't
|
||
plan to think about it, or about whether other Emacs maintenance
|
||
might break it.
|
||
|
||
|
||
Unexec creates a copy of the old a.out file, and replaces the old data
|
||
area with the current data area. When the new file is executed, the
|
||
process will see the same data structures and data values that the
|
||
original process had when unexec was called.
|
||
|
||
Unlike other versions of unexec, this one copies symbol table and
|
||
debug information to the new a.out file. Thus, the new a.out file
|
||
may be debugged with symbolic debuggers.
|
||
|
||
If you fix any bugs in this, I'd like to incorporate your fixes.
|
||
Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
|
||
|
||
CAVEATS:
|
||
This routine saves the current value of all static and external
|
||
variables. This means that any data structure that needs to be
|
||
initialized must be explicitly reset. Variables will not have their
|
||
expected default values.
|
||
|
||
Unfortunately, the HP-UX signal handler has internal initialization
|
||
flags which are not explicitly reset. Thus, for signals to work in
|
||
conjunction with this routine, the following code must executed when
|
||
the new process starts up.
|
||
|
||
void _sigreturn ();
|
||
...
|
||
sigsetreturn (_sigreturn);
|
||
*/
|
||
|
||
#include <config.h>
|
||
#include "unexec.h"
|
||
#include "lisp.h"
|
||
#include "sysstdio.h"
|
||
|
||
#include <fcntl.h>
|
||
#include <errno.h>
|
||
#include <a.out.h>
|
||
#include <dl.h>
|
||
|
||
/* brk value to restore, stored as a global.
|
||
This is really used only if we used shared libraries. */
|
||
static long brk_on_dump = 0;
|
||
|
||
/* Called from main, if we use shared libraries. */
|
||
int
|
||
run_time_remap (char *ignored)
|
||
{
|
||
brk ((char *) brk_on_dump);
|
||
}
|
||
|
||
#undef roundup
|
||
#define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */
|
||
|
||
/* Report a fatal error and exit. */
|
||
static _Noreturn void
|
||
unexec_error (char const *msg)
|
||
{
|
||
perror (msg);
|
||
exit (1);
|
||
}
|
||
|
||
/* Do an lseek and check the result. */
|
||
static void
|
||
check_lseek (int fd, off_t offset, int whence)
|
||
{
|
||
if (lseek (fd, offset, whence) < 0)
|
||
unexec_error ("Cannot lseek");
|
||
}
|
||
|
||
/* Save current data space in the file, update header. */
|
||
|
||
static void
|
||
save_data_space (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
|
||
int size)
|
||
{
|
||
/* Write the entire data space out to the file */
|
||
if (write (file, auxhdr->exec_dmem, size) != size)
|
||
unexec_error ("Can't save new data space");
|
||
|
||
/* Update the header to reflect the new data size */
|
||
auxhdr->exec_dsize = size;
|
||
auxhdr->exec_bsize = 0;
|
||
}
|
||
|
||
/* Update the values of file pointers when something is inserted. */
|
||
|
||
static void
|
||
update_file_ptrs (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
|
||
unsigned int location, int offset)
|
||
{
|
||
struct subspace_dictionary_record subspace;
|
||
int i;
|
||
|
||
/* Increase the overall size of the module */
|
||
hdr->som_length += offset;
|
||
|
||
/* Update the various file pointers in the header */
|
||
#define update(ptr) if (ptr > location) ptr = ptr + offset
|
||
update (hdr->aux_header_location);
|
||
update (hdr->space_strings_location);
|
||
update (hdr->init_array_location);
|
||
update (hdr->compiler_location);
|
||
update (hdr->symbol_location);
|
||
update (hdr->fixup_request_location);
|
||
update (hdr->symbol_strings_location);
|
||
update (hdr->unloadable_sp_location);
|
||
update (auxhdr->exec_tfile);
|
||
update (auxhdr->exec_dfile);
|
||
|
||
/* Do for each subspace dictionary entry */
|
||
check_lseek (file, hdr->subspace_location, 0);
|
||
for (i = 0; i < hdr->subspace_total; i++)
|
||
{
|
||
ptrdiff_t subspace_size = sizeof subspace;
|
||
if (read (file, &subspace, subspace_size) != subspace_size)
|
||
unexec_error ("Can't read subspace record");
|
||
|
||
/* If subspace has a file location, update it */
|
||
if (subspace.initialization_length > 0
|
||
&& subspace.file_loc_init_value > location)
|
||
{
|
||
subspace.file_loc_init_value += offset;
|
||
check_lseek (file, -subspace_size, 1);
|
||
if (write (file, &subspace, subspace_size) != subspace_size)
|
||
unexec_error ("Can't update subspace record");
|
||
}
|
||
}
|
||
|
||
/* Do for each initialization pointer record */
|
||
/* (I don't think it applies to executable files, only relocatables) */
|
||
#undef update
|
||
}
|
||
|
||
/* Read in the header records from an a.out file. */
|
||
|
||
static void
|
||
read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
|
||
{
|
||
|
||
/* Read the header in */
|
||
check_lseek (file, 0, 0);
|
||
if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
|
||
unexec_error ("Couldn't read header from a.out file");
|
||
|
||
if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
|
||
&& hdr->a_magic != DEMAND_MAGIC)
|
||
{
|
||
fputs ("a.out file doesn't have valid magic number\n", stderr);
|
||
exit (1);
|
||
}
|
||
|
||
check_lseek (file, hdr->aux_header_location, 0);
|
||
if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
|
||
unexec_error ("Couldn't read auxiliary header from a.out file");
|
||
}
|
||
|
||
/* Write out the header records into an a.out file. */
|
||
|
||
static void
|
||
write_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
|
||
{
|
||
/* Update the checksum */
|
||
hdr->checksum = calculate_checksum (hdr);
|
||
|
||
/* Write the header back into the a.out file */
|
||
check_lseek (file, 0, 0);
|
||
if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
|
||
unexec_error ("Couldn't write header to a.out file");
|
||
check_lseek (file, hdr->aux_header_location, 0);
|
||
if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
|
||
unexec_error ("Couldn't write auxiliary header to a.out file");
|
||
}
|
||
|
||
/* Calculate the checksum of a SOM header record. */
|
||
|
||
static int
|
||
calculate_checksum (struct header *hdr)
|
||
{
|
||
int checksum, i, *ptr;
|
||
|
||
checksum = 0; ptr = (int *) hdr;
|
||
|
||
for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
|
||
checksum ^= ptr[i];
|
||
|
||
return (checksum);
|
||
}
|
||
|
||
/* Copy size bytes from the old file to the new one. */
|
||
|
||
static void
|
||
copy_file (int old, int new, int size)
|
||
{
|
||
int len;
|
||
int buffer[8192]; /* word aligned will be faster */
|
||
|
||
for (; size > 0; size -= len)
|
||
{
|
||
len = min (size, sizeof (buffer));
|
||
if (read (old, buffer, len) != len)
|
||
unexec_error ("Read failure on a.out file");
|
||
if (write (new, buffer, len) != len)
|
||
unexec_error ("Write failure in a.out file");
|
||
}
|
||
}
|
||
|
||
/* Copy the rest of the file, up to EOF. */
|
||
|
||
static void
|
||
copy_rest (int old, int new)
|
||
{
|
||
int buffer[4096];
|
||
int len;
|
||
|
||
/* Copy bytes until end of file or error */
|
||
while ((len = read (old, buffer, sizeof (buffer))) > 0)
|
||
if (write (new, buffer, len) != len) break;
|
||
|
||
if (len != 0)
|
||
unexec_error ("Unable to copy the rest of the file");
|
||
}
|
||
|
||
#ifdef DEBUG
|
||
static void
|
||
display_header (struct header *hdr, struct som_exec_auxhdr *auxhdr)
|
||
{
|
||
/* Display the header information (debug) */
|
||
printf ("\n\nFILE HEADER\n");
|
||
printf ("magic number %d \n", hdr->a_magic);
|
||
printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
|
||
printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
|
||
printf ("entry %x \n", auxhdr->exec_entry);
|
||
printf ("Bss segment size %u\n", auxhdr->exec_bsize);
|
||
printf ("\n");
|
||
printf ("data file loc %d size %d\n",
|
||
auxhdr->exec_dfile, auxhdr->exec_dsize);
|
||
printf ("som_length %d\n", hdr->som_length);
|
||
printf ("unloadable sploc %d size %d\n",
|
||
hdr->unloadable_sp_location, hdr->unloadable_sp_size);
|
||
}
|
||
#endif /* DEBUG */
|
||
|
||
|
||
/* Create a new a.out file, same as old but with current data space */
|
||
void
|
||
unexec (const char *new_name, /* name of the new a.out file to be created */
|
||
const char *old_name) /* name of the old a.out file */
|
||
{
|
||
int old, new;
|
||
int old_size, new_size;
|
||
struct header hdr;
|
||
struct som_exec_auxhdr auxhdr;
|
||
long i;
|
||
|
||
/* For the greatest flexibility, should create a temporary file in
|
||
the same directory as the new file. When everything is complete,
|
||
rename the temp file to the new name.
|
||
This way, a program could update its own a.out file even while
|
||
it is still executing. If problems occur, everything is still
|
||
intact. NOT implemented. */
|
||
|
||
/* Open the input and output a.out files. */
|
||
old = emacs_open (old_name, O_RDONLY, 0);
|
||
if (old < 0)
|
||
unexec_error (old_name);
|
||
new = emacs_open (new_name, O_CREAT | O_RDWR | O_TRUNC, 0777);
|
||
if (new < 0)
|
||
unexec_error (new_name);
|
||
|
||
/* Read the old headers. */
|
||
read_header (old, &hdr, &auxhdr);
|
||
|
||
brk_on_dump = (long) sbrk (0);
|
||
|
||
/* Decide how large the new and old data areas are. */
|
||
old_size = auxhdr.exec_dsize;
|
||
/* I suspect these two statements are separate
|
||
to avoid a compiler bug in hpux version 8. */
|
||
i = (long) sbrk (0);
|
||
new_size = i - auxhdr.exec_dmem;
|
||
|
||
/* Copy the old file to the new, up to the data space. */
|
||
check_lseek (old, 0, 0);
|
||
copy_file (old, new, auxhdr.exec_dfile);
|
||
|
||
/* Skip the old data segment and write a new one. */
|
||
check_lseek (old, old_size, 1);
|
||
save_data_space (new, &hdr, &auxhdr, new_size);
|
||
|
||
/* Copy the rest of the file. */
|
||
copy_rest (old, new);
|
||
|
||
/* Update file pointers since we probably changed size of data area. */
|
||
update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
|
||
|
||
/* Save the modified header. */
|
||
write_header (new, &hdr, &auxhdr);
|
||
|
||
/* Close the binary file. */
|
||
emacs_close (old);
|
||
emacs_close (new);
|
||
}
|