Mercurial > hg > xemacs-beta
annotate src/unexaix.c @ 5127:a9c41067dd88 ben-lisp-object
more cleanups, terminology clarification, lots of doc work
-------------------- ChangeLog entries follow: --------------------
man/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* internals/internals.texi (Introduction to Allocation):
* internals/internals.texi (Integers and Characters):
* internals/internals.texi (Allocation from Frob Blocks):
* internals/internals.texi (lrecords):
* internals/internals.texi (Low-level allocation):
Rewrite section on allocation of Lisp objects to reflect the new
reality. Remove references to nonexistent XSETINT and XSETCHAR.
modules/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* postgresql/postgresql.c (allocate_pgconn):
* postgresql/postgresql.c (allocate_pgresult):
* postgresql/postgresql.h (struct Lisp_PGconn):
* postgresql/postgresql.h (struct Lisp_PGresult):
* ldap/eldap.c (allocate_ldap):
* ldap/eldap.h (struct Lisp_LDAP):
Same changes as in src/ dir. See large log there in ChangeLog,
but basically:
ALLOC_LISP_OBJECT -> ALLOC_NORMAL_LISP_OBJECT
LISP_OBJECT_HEADER -> NORMAL_LISP_OBJECT_HEADER
../hlo/src/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* alloc.c:
* alloc.c (old_alloc_sized_lcrecord):
* alloc.c (very_old_free_lcrecord):
* alloc.c (copy_lisp_object):
* alloc.c (zero_sized_lisp_object):
* alloc.c (zero_nonsized_lisp_object):
* alloc.c (lisp_object_storage_size):
* alloc.c (free_normal_lisp_object):
* alloc.c (FREE_FIXED_TYPE_WHEN_NOT_IN_GC):
* alloc.c (ALLOC_FROB_BLOCK_LISP_OBJECT):
* alloc.c (Fcons):
* alloc.c (noseeum_cons):
* alloc.c (make_float):
* alloc.c (make_bignum):
* alloc.c (make_bignum_bg):
* alloc.c (make_ratio):
* alloc.c (make_ratio_bg):
* alloc.c (make_ratio_rt):
* alloc.c (make_bigfloat):
* alloc.c (make_bigfloat_bf):
* alloc.c (size_vector):
* alloc.c (make_compiled_function):
* alloc.c (Fmake_symbol):
* alloc.c (allocate_extent):
* alloc.c (allocate_event):
* alloc.c (make_key_data):
* alloc.c (make_button_data):
* alloc.c (make_motion_data):
* alloc.c (make_process_data):
* alloc.c (make_timeout_data):
* alloc.c (make_magic_data):
* alloc.c (make_magic_eval_data):
* alloc.c (make_eval_data):
* alloc.c (make_misc_user_data):
* alloc.c (Fmake_marker):
* alloc.c (noseeum_make_marker):
* alloc.c (size_string_direct_data):
* alloc.c (make_uninit_string):
* alloc.c (make_string_nocopy):
* alloc.c (mark_lcrecord_list):
* alloc.c (alloc_managed_lcrecord):
* alloc.c (free_managed_lcrecord):
* alloc.c (sweep_lcrecords_1):
* alloc.c (malloced_storage_size):
* buffer.c (allocate_buffer):
* buffer.c (compute_buffer_usage):
* buffer.c (DEFVAR_BUFFER_LOCAL_1):
* buffer.c (nuke_all_buffer_slots):
* buffer.c (common_init_complex_vars_of_buffer):
* buffer.h (struct buffer_text):
* buffer.h (struct buffer):
* bytecode.c:
* bytecode.c (make_compiled_function_args):
* bytecode.c (size_compiled_function_args):
* bytecode.h (struct compiled_function_args):
* casetab.c (allocate_case_table):
* casetab.h (struct Lisp_Case_Table):
* charset.h (struct Lisp_Charset):
* chartab.c (fill_char_table):
* chartab.c (Fmake_char_table):
* chartab.c (make_char_table_entry):
* chartab.c (copy_char_table_entry):
* chartab.c (Fcopy_char_table):
* chartab.c (put_char_table):
* chartab.h (struct Lisp_Char_Table_Entry):
* chartab.h (struct Lisp_Char_Table):
* console-gtk-impl.h (struct gtk_device):
* console-gtk-impl.h (struct gtk_frame):
* console-impl.h (struct console):
* console-msw-impl.h (struct Lisp_Devmode):
* console-msw-impl.h (struct mswindows_device):
* console-msw-impl.h (struct msprinter_device):
* console-msw-impl.h (struct mswindows_frame):
* console-msw-impl.h (struct mswindows_dialog_id):
* console-stream-impl.h (struct stream_console):
* console-stream.c (stream_init_console):
* console-tty-impl.h (struct tty_console):
* console-tty-impl.h (struct tty_device):
* console-tty.c (allocate_tty_console_struct):
* console-x-impl.h (struct x_device):
* console-x-impl.h (struct x_frame):
* console.c (allocate_console):
* console.c (nuke_all_console_slots):
* console.c (DEFVAR_CONSOLE_LOCAL_1):
* console.c (common_init_complex_vars_of_console):
* data.c (make_weak_list):
* data.c (make_weak_box):
* data.c (make_ephemeron):
* database.c:
* database.c (struct Lisp_Database):
* database.c (allocate_database):
* database.c (finalize_database):
* device-gtk.c (allocate_gtk_device_struct):
* device-impl.h (struct device):
* device-msw.c:
* device-msw.c (mswindows_init_device):
* device-msw.c (msprinter_init_device):
* device-msw.c (finalize_devmode):
* device-msw.c (allocate_devmode):
* device-tty.c (allocate_tty_device_struct):
* device-x.c (allocate_x_device_struct):
* device.c:
* device.c (nuke_all_device_slots):
* device.c (allocate_device):
* dialog-msw.c (handle_question_dialog_box):
* elhash.c:
* elhash.c (struct Lisp_Hash_Table):
* elhash.c (finalize_hash_table):
* elhash.c (make_general_lisp_hash_table):
* elhash.c (Fcopy_hash_table):
* elhash.h (htentry):
* emacs.c (main_1):
* eval.c:
* eval.c (size_multiple_value):
* event-stream.c (finalize_command_builder):
* event-stream.c (allocate_command_builder):
* event-stream.c (free_command_builder):
* event-stream.c (event_stream_generate_wakeup):
* event-stream.c (event_stream_resignal_wakeup):
* event-stream.c (event_stream_disable_wakeup):
* event-stream.c (event_stream_wakeup_pending_p):
* events.h (struct Lisp_Timeout):
* events.h (struct command_builder):
* extents-impl.h:
* extents-impl.h (struct extent_auxiliary):
* extents-impl.h (struct extent_info):
* extents-impl.h (set_extent_no_chase_aux_field):
* extents-impl.h (set_extent_no_chase_normal_field):
* extents.c:
* extents.c (gap_array_marker):
* extents.c (gap_array):
* extents.c (extent_list_marker):
* extents.c (extent_list):
* extents.c (stack_of_extents):
* extents.c (gap_array_make_marker):
* extents.c (extent_list_make_marker):
* extents.c (allocate_extent_list):
* extents.c (SLOT):
* extents.c (mark_extent_auxiliary):
* extents.c (allocate_extent_auxiliary):
* extents.c (attach_extent_auxiliary):
* extents.c (size_gap_array):
* extents.c (finalize_extent_info):
* extents.c (allocate_extent_info):
* extents.c (uninit_buffer_extents):
* extents.c (allocate_soe):
* extents.c (copy_extent):
* extents.c (vars_of_extents):
* extents.h:
* faces.c (allocate_face):
* faces.h (struct Lisp_Face):
* faces.h (struct face_cachel):
* file-coding.c:
* file-coding.c (finalize_coding_system):
* file-coding.c (sizeof_coding_system):
* file-coding.c (Fcopy_coding_system):
* file-coding.h (struct Lisp_Coding_System):
* file-coding.h (MARKED_SLOT):
* fns.c (size_bit_vector):
* font-mgr.c:
* font-mgr.c (finalize_fc_pattern):
* font-mgr.c (print_fc_pattern):
* font-mgr.c (Ffc_pattern_p):
* font-mgr.c (Ffc_pattern_create):
* font-mgr.c (Ffc_name_parse):
* font-mgr.c (Ffc_name_unparse):
* font-mgr.c (Ffc_pattern_duplicate):
* font-mgr.c (Ffc_pattern_add):
* font-mgr.c (Ffc_pattern_del):
* font-mgr.c (Ffc_pattern_get):
* font-mgr.c (fc_config_create_using):
* font-mgr.c (fc_strlist_to_lisp_using):
* font-mgr.c (fontset_to_list):
* font-mgr.c (Ffc_config_p):
* font-mgr.c (Ffc_config_up_to_date):
* font-mgr.c (Ffc_config_build_fonts):
* font-mgr.c (Ffc_config_get_cache):
* font-mgr.c (Ffc_config_get_fonts):
* font-mgr.c (Ffc_config_set_current):
* font-mgr.c (Ffc_config_get_blanks):
* font-mgr.c (Ffc_config_get_rescan_interval):
* font-mgr.c (Ffc_config_set_rescan_interval):
* font-mgr.c (Ffc_config_app_font_add_file):
* font-mgr.c (Ffc_config_app_font_add_dir):
* font-mgr.c (Ffc_config_app_font_clear):
* font-mgr.c (size):
* font-mgr.c (Ffc_config_substitute):
* font-mgr.c (Ffc_font_render_prepare):
* font-mgr.c (Ffc_font_match):
* font-mgr.c (Ffc_font_sort):
* font-mgr.c (finalize_fc_config):
* font-mgr.c (print_fc_config):
* font-mgr.h:
* font-mgr.h (struct fc_pattern):
* font-mgr.h (XFC_PATTERN):
* font-mgr.h (struct fc_config):
* font-mgr.h (XFC_CONFIG):
* frame-gtk.c (allocate_gtk_frame_struct):
* frame-impl.h (struct frame):
* frame-msw.c (mswindows_init_frame_1):
* frame-x.c (allocate_x_frame_struct):
* frame.c (nuke_all_frame_slots):
* frame.c (allocate_frame_core):
* gc.c:
* gc.c (GC_CHECK_NOT_FREE):
* glyphs.c (finalize_image_instance):
* glyphs.c (allocate_image_instance):
* glyphs.c (Fcolorize_image_instance):
* glyphs.c (allocate_glyph):
* glyphs.c (unmap_subwindow_instance_cache_mapper):
* glyphs.c (register_ignored_expose):
* glyphs.h (struct Lisp_Image_Instance):
* glyphs.h (struct Lisp_Glyph):
* glyphs.h (struct glyph_cachel):
* glyphs.h (struct expose_ignore):
* gui.c (allocate_gui_item):
* gui.h (struct Lisp_Gui_Item):
* keymap.c (struct Lisp_Keymap):
* keymap.c (make_keymap):
* lisp.h:
* lisp.h (struct Lisp_String_Direct_Data):
* lisp.h (struct Lisp_String_Indirect_Data):
* lisp.h (struct Lisp_Vector):
* lisp.h (struct Lisp_Bit_Vector):
* lisp.h (DECLARE_INLINE_LISP_BIT_VECTOR):
* lisp.h (struct weak_box):
* lisp.h (struct ephemeron):
* lisp.h (struct weak_list):
* lrecord.h:
* lrecord.h (struct lrecord_implementation):
* lrecord.h (MC_ALLOC_CALL_FINALIZER):
* lrecord.h (struct lcrecord_list):
* lstream.c (finalize_lstream):
* lstream.c (sizeof_lstream):
* lstream.c (Lstream_new):
* lstream.c (Lstream_delete):
* lstream.h (struct lstream):
* marker.c:
* marker.c (finalize_marker):
* marker.c (compute_buffer_marker_usage):
* mule-charset.c:
* mule-charset.c (make_charset):
* mule-charset.c (compute_charset_usage):
* objects-impl.h (struct Lisp_Color_Instance):
* objects-impl.h (struct Lisp_Font_Instance):
* objects-tty-impl.h (struct tty_color_instance_data):
* objects-tty-impl.h (struct tty_font_instance_data):
* objects-tty.c (tty_initialize_color_instance):
* objects-tty.c (tty_initialize_font_instance):
* objects.c (finalize_color_instance):
* objects.c (Fmake_color_instance):
* objects.c (finalize_font_instance):
* objects.c (Fmake_font_instance):
* objects.c (reinit_vars_of_objects):
* opaque.c:
* opaque.c (sizeof_opaque):
* opaque.c (make_opaque_ptr):
* opaque.c (free_opaque_ptr):
* opaque.h:
* opaque.h (Lisp_Opaque):
* opaque.h (Lisp_Opaque_Ptr):
* print.c (printing_unreadable_lcrecord):
* print.c (external_object_printer):
* print.c (debug_p4):
* process.c (finalize_process):
* process.c (make_process_internal):
* procimpl.h (struct Lisp_Process):
* rangetab.c (Fmake_range_table):
* rangetab.c (Fcopy_range_table):
* rangetab.h (struct Lisp_Range_Table):
* scrollbar.c:
* scrollbar.c (create_scrollbar_instance):
* scrollbar.c (compute_scrollbar_instance_usage):
* scrollbar.h (struct scrollbar_instance):
* specifier.c (finalize_specifier):
* specifier.c (sizeof_specifier):
* specifier.c (set_specifier_caching):
* specifier.h (struct Lisp_Specifier):
* specifier.h (struct specifier_caching):
* symeval.h:
* symeval.h (SYMBOL_VALUE_MAGIC_P):
* symeval.h (DEFVAR_SYMVAL_FWD):
* symsinit.h:
* syntax.c (init_buffer_syntax_cache):
* syntax.h (struct syntax_cache):
* toolbar.c:
* toolbar.c (allocate_toolbar_button):
* toolbar.c (update_toolbar_button):
* toolbar.h (struct toolbar_button):
* tooltalk.c (struct Lisp_Tooltalk_Message):
* tooltalk.c (make_tooltalk_message):
* tooltalk.c (struct Lisp_Tooltalk_Pattern):
* tooltalk.c (make_tooltalk_pattern):
* ui-gtk.c:
* ui-gtk.c (allocate_ffi_data):
* ui-gtk.c (emacs_gtk_object_finalizer):
* ui-gtk.c (allocate_emacs_gtk_object_data):
* ui-gtk.c (allocate_emacs_gtk_boxed_data):
* ui-gtk.h:
* window-impl.h (struct window):
* window-impl.h (struct window_mirror):
* window.c (finalize_window):
* window.c (allocate_window):
* window.c (new_window_mirror):
* window.c (mark_window_as_deleted):
* window.c (make_dummy_parent):
* window.c (compute_window_mirror_usage):
* window.c (compute_window_usage):
Overall point of this change and previous ones in this repository:
(1) Introduce new, clearer terminology: everything other than int
or char is a "record" object, which comes in two types: "normal
objects" and "frob-block objects". Fix up all places that
referred to frob-block objects as "simple", "basic", etc.
(2) Provide an advertised interface for doing operations on Lisp
objects, including creating new types, that is clean and
consistent in its naming, uses the above-referenced terms and
avoids referencing "lrecords", "old lcrecords", etc., which should
hide under the surface.
(3) Make the size_in_bytes and finalizer methods take a
Lisp_Object rather than a void * for consistency with other methods.
(4) Separate finalizer method into finalizer and disksaver, so
that normal finalize methods don't have to worry about disksaving.
Other specifics:
(1) Renaming:
LISP_OBJECT_HEADER -> NORMAL_LISP_OBJECT_HEADER
ALLOC_LISP_OBJECT -> ALLOC_NORMAL_LISP_OBJECT
implementation->basic_p -> implementation->frob_block_p
ALLOCATE_FIXED_TYPE_AND_SET_IMPL -> ALLOC_FROB_BLOCK_LISP_OBJECT
*FCCONFIG*, wrap_fcconfig -> *FC_CONFIG*, wrap_fc_config
*FCPATTERN*, wrap_fcpattern -> *FC_PATTERN*, wrap_fc_pattern
(the last two changes make the naming of these macros consistent
with the naming of all other macros, since the objects are named
fc-config and fc-pattern with a hyphen)
(2) Lots of documentation fixes in lrecord.h.
(3) Eliminate macros for copying, freeing, zeroing objects, getting
their storage size. Instead, new functions:
zero_sized_lisp_object()
zero_nonsized_lisp_object()
lisp_object_storage_size()
free_normal_lisp_object()
(copy_lisp_object() already exists)
LISP_OBJECT_FROB_BLOCK_P() (actually a macro)
Eliminated:
free_lrecord()
zero_lrecord()
copy_lrecord()
copy_sized_lrecord()
old_copy_lcrecord()
old_copy_sized_lcrecord()
old_zero_lcrecord()
old_zero_sized_lcrecord()
LISP_OBJECT_STORAGE_SIZE()
COPY_SIZED_LISP_OBJECT()
COPY_SIZED_LCRECORD()
COPY_LISP_OBJECT()
ZERO_LISP_OBJECT()
FREE_LISP_OBJECT()
(4) Catch the remaining places where lrecord stuff was used directly
and use the advertised interface, e.g. alloc_sized_lrecord() ->
ALLOC_SIZED_LISP_OBJECT().
(5) Make certain statically-declared pseudo-objects
(buffer_local_flags, console_local_flags) have their lheader
initialized correctly, so things like copy_lisp_object() can work
on them. Make extent_auxiliary_defaults a proper heap object
Vextent_auxiliary_defaults, and make extent auxiliaries dumpable
so that this object can be dumped. allocate_extent_auxiliary()
now just creates the object, and attach_extent_auxiliary()
creates an extent auxiliary and attaches to an extent, like the
old allocate_extent_auxiliary().
(6) Create EXTENT_AUXILIARY_SLOTS macro, similar to the foo-slots.h
files but in a macro instead of a file. The purpose is to avoid
duplication when iterating over all the slots in an extent auxiliary.
Use it.
(7) In lstream.c, don't zero out object after allocation because
allocation routines take care of this.
(8) In marker.c, fix a mistake in computing marker overhead.
(9) In print.c, clean up printing_unreadable_lcrecord(),
external_object_printer() to avoid lots of ifdef NEW_GC's.
(10) Separate toolbar-button allocation into a separate
allocate_toolbar_button() function for use in the example code
in lrecord.h.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Fri, 05 Mar 2010 04:08:17 -0600 |
parents | 304aebb79cd3 |
children | 308d34e9f07d |
rev | line source |
---|---|
428 | 1 /* Dump an executable image. |
2 Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc. | |
3 | |
4 This file is part of XEmacs. | |
5 | |
6 XEmacs is free software; you can redistribute it and/or modify it | |
7 under the terms of the GNU General Public License as published by the | |
8 Free Software Foundation; either version 2, or (at your option) any | |
9 later version. | |
10 | |
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with XEmacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 Boston, MA 02111-1307, USA. */ | |
20 | |
21 /* Synched up with: FSF 20.2. */ | |
22 | |
771 | 23 /* This file has been ... uhhhhh ... Mule-ized. Yeah. |
24 | |
25 (Everything here is external format. That's potentially dangerous, | |
26 but in practice it'll be OK.) --ben */ | |
27 | |
428 | 28 /* Originally based on the COFF unexec.c by Spencer W. Thomas. |
29 * | |
30 * Subsequently hacked on by | |
31 * Bill Mann <Bill_Man@praxisint.com> | |
32 * Andrew Vignaux <Andrew.Vignaux@comp.vuw.ac.nz> | |
33 * Mike Sperber <sperber@informatik.uni-tuebingen.de> | |
34 * | |
35 * Synopsis: | |
36 * unexec (new_name, a_name, data_start, bss_start, entry_address) | |
37 * char *new_name, *a_name; | |
38 * unsigned data_start, bss_start, entry_address; | |
39 * | |
40 * Takes a snapshot of the program and makes an a.out format file in the | |
41 * file named by the string argument new_name. | |
42 * If a_name is non-NULL, the symbol table will be taken from the given file. | |
43 * On some machines, an existing a_name file is required. | |
44 * | |
45 * data_start and entry_address are ignored. | |
46 * | |
47 * bss_start indicates how much of the data segment is to be saved in the | |
48 * a.out file and restored when the program is executed. It gives the lowest | |
49 * unsaved address, and is rounded up to a page boundary. The default when 0 | |
50 * is given assumes that the entire data segment is to be stored, including | |
51 * the previous data and bss as well as any additional storage allocated with | |
52 * sbrk(2). | |
53 * | |
54 */ | |
55 | |
56 #ifndef emacs | |
57 #define PERROR(arg) perror (arg); return -1 | |
58 #else | |
59 #include <config.h> | |
3025 | 60 #define PERROR(file) report_error (file, new_) |
428 | 61 #endif |
62 | |
63 #include <a.out.h> | |
64 /* Define getpagesize () if the system does not. | |
65 Note that this may depend on symbols defined in a.out.h | |
66 */ | |
67 #include "getpagesize.h" | |
68 | |
69 #include <sys/types.h> | |
70 #include <stdio.h> | |
71 #include <sys/stat.h> | |
72 #include <errno.h> | |
73 #include <unistd.h> | |
74 #include <fcntl.h> | |
75 | |
76 extern char *start_of_text (void); /* Start of text */ | |
77 extern char *start_of_data (void); /* Start of initialized data */ | |
78 | |
79 extern int _data; | |
80 extern int _text; | |
81 | |
82 #include <filehdr.h> | |
83 #include <aouthdr.h> | |
84 #include <scnhdr.h> | |
85 #include <syms.h> | |
86 | |
87 static struct filehdr f_hdr; /* File header */ | |
88 static struct aouthdr f_ohdr; /* Optional file header (a.out) */ | |
89 static long bias; /* Bias to add for growth */ | |
90 static long lnnoptr; /* Pointer to line-number info within file */ | |
91 | |
92 static long text_scnptr; | |
93 static long data_scnptr; | |
94 #define ALIGN(val, pwr) (((val) + ((1L<<(pwr))-1)) & ~((1L<<(pwr))-1)) | |
95 static long load_scnptr; | |
96 static long orig_load_scnptr; | |
97 static long orig_data_scnptr; | |
98 static int unrelocate_symbols (int, int, char *, char *); | |
99 | |
100 #ifndef MAX_SECTIONS | |
101 #define MAX_SECTIONS 10 | |
102 #endif | |
103 | |
104 static int adjust_lnnoptrs (int, int, char *); | |
105 | |
106 static int pagemask; | |
107 | |
108 /* Correct an int which is the bit pattern of a pointer to a byte | |
109 into an int which is the number of a byte. | |
110 This is a no-op on ordinary machines, but not on all. */ | |
111 | |
112 #ifndef ADDR_CORRECT /* Let m-*.h files override this definition */ | |
113 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0) | |
114 #endif | |
115 | |
116 #ifdef emacs | |
117 #include "lisp.h" | |
118 | |
119 static void | |
120 report_error (char *file, int fd) | |
121 { | |
122 if (fd) | |
123 close (fd); | |
563 | 124 report_error_with_errno (Qio_error, "Cannot unexec", |
4953
304aebb79cd3
function renamings to track names of char typedefs
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
125 build_cistring (file)); |
428 | 126 } |
127 #endif /* emacs */ | |
128 | |
3025 | 129 #define ERROR0(msg) report_error_1 (new_, msg, 0, 0); return -1 |
130 #define ERROR1(msg,x) report_error_1 (new_, msg, x, 0); return -1 | |
131 #define ERROR2(msg,x,y) report_error_1 (new_, msg, x, y); return -1 | |
428 | 132 |
133 static void | |
134 report_error_1 (int fd, char *msg, int a1, int a2) | |
135 { | |
136 close (fd); | |
137 #ifdef emacs | |
563 | 138 signal_ferror (Qio_error, msg, a1, a2); |
428 | 139 #else |
140 fprintf (stderr, msg, a1, a2); | |
141 fprintf (stderr, "\n"); | |
142 #endif | |
143 } | |
144 | |
145 static int make_hdr (int, int, unsigned, unsigned, unsigned, char *, char *); | |
146 static void mark_x (char *); | |
147 static int copy_text_and_data (int); | |
148 static int copy_sym (int, int, char *, char *); | |
149 static void write_segment (int, char *, char *); | |
150 | |
151 /* **************************************************************** | |
152 * unexec | |
153 * | |
154 * driving logic. | |
155 */ | |
156 int unexec (char *new_name, char *a_name, | |
157 uintptr_t data_start, | |
158 uintptr_t bss_start, | |
159 uintptr_t entry_address) | |
160 { | |
3025 | 161 int new_ = -1, a_out = -1; |
428 | 162 |
163 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0) | |
164 { | |
165 PERROR (a_name); | |
166 } | |
3025 | 167 if ((new_ = creat (new_name, 0666)) < 0) |
428 | 168 { |
169 PERROR (new_name); | |
170 } | |
3025 | 171 if (make_hdr (new_, a_out, |
428 | 172 data_start, bss_start, |
173 entry_address, | |
174 a_name, new_name) < 0 | |
3025 | 175 || copy_text_and_data (new_) < 0 |
176 || copy_sym (new_, a_out, a_name, new_name) < 0 | |
177 || adjust_lnnoptrs (new_, a_out, new_name) < 0 | |
178 || unrelocate_symbols (new_, a_out, a_name, new_name) < 0) | |
428 | 179 { |
3025 | 180 close (new_); |
428 | 181 return -1; |
182 } | |
183 | |
3025 | 184 close (new_); |
428 | 185 if (a_out >= 0) |
186 close (a_out); | |
187 mark_x (new_name); | |
188 return 0; | |
189 } | |
190 | |
191 /* **************************************************************** | |
192 * make_hdr | |
193 * | |
194 * Make the header in the new a.out from the header in core. | |
195 * Modify the text and data sizes. | |
196 */ | |
197 static int | |
3025 | 198 make_hdr (int new_, int a_out, |
428 | 199 unsigned data_start, unsigned bss_start, |
2286 | 200 unsigned UNUSED (entry_address), |
428 | 201 char *a_name, char *new_name) |
202 { | |
203 int scns; | |
204 unsigned int bss_end; | |
205 | |
206 struct scnhdr section[MAX_SECTIONS]; | |
207 struct scnhdr * f_thdr; /* Text section header */ | |
208 struct scnhdr * f_dhdr; /* Data section header */ | |
209 struct scnhdr * f_bhdr; /* Bss section header */ | |
210 struct scnhdr * f_lhdr; /* Loader section header */ | |
211 struct scnhdr * f_tchdr; /* Typechk section header */ | |
212 struct scnhdr * f_dbhdr; /* Debug section header */ | |
213 struct scnhdr * f_xhdr; /* Except section header */ | |
214 | |
215 load_scnptr = orig_load_scnptr = lnnoptr = 0; | |
216 pagemask = getpagesize () - 1; | |
217 | |
218 /* Adjust text/data boundary. */ | |
219 data_start = (long) start_of_data (); | |
220 data_start = ADDR_CORRECT (data_start); | |
221 | |
222 data_start = data_start & ~pagemask; /* (Down) to page boundary. */ | |
223 | |
224 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask; | |
225 bss_end &= ~ pagemask; | |
226 /* Adjust data/bss boundary. */ | |
227 if (bss_start != 0) | |
228 { | |
229 bss_start = (ADDR_CORRECT (bss_start) + pagemask); | |
230 /* (Up) to page bdry. */ | |
231 bss_start &= ~ pagemask; | |
232 if (bss_start > bss_end) | |
233 { | |
234 ERROR1 ("unexec: Specified bss_start (%u) is past end of program", | |
235 bss_start); | |
236 } | |
237 } | |
238 else | |
239 bss_start = bss_end; | |
240 | |
241 if (data_start > bss_start) /* Can't have negative data size. */ | |
242 { | |
243 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)", | |
244 data_start, bss_start); | |
245 } | |
246 | |
247 /* Salvage as much info from the existing file as possible */ | |
248 f_thdr = NULL; f_dhdr = NULL; f_bhdr = NULL; | |
249 f_lhdr = NULL; f_tchdr = NULL; f_dbhdr = NULL; f_xhdr = NULL; | |
250 if (a_out >= 0) | |
251 { | |
252 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | |
253 { | |
254 PERROR (a_name); | |
255 } | |
256 if (f_hdr.f_opthdr > 0) | |
257 { | |
258 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
259 { | |
260 PERROR (a_name); | |
261 } | |
262 } | |
263 if (f_hdr.f_nscns > MAX_SECTIONS) | |
264 { | |
265 ERROR0 ("unexec: too many section headers -- increase MAX_SECTIONS"); | |
266 } | |
267 /* Loop through section headers */ | |
268 for (scns = 0; scns < f_hdr.f_nscns; scns++) { | |
269 struct scnhdr *s = §ion[scns]; | |
270 if (read (a_out, s, sizeof (*s)) != sizeof (*s)) | |
271 { | |
272 PERROR (a_name); | |
273 } | |
274 | |
275 #define CHECK_SCNHDR(ptr, name, flags) \ | |
276 if (strcmp(s->s_name, name) == 0) { \ | |
277 if (s->s_flags != flags) { \ | |
278 fprintf(stderr, "unexec: %lx flags where %x expected in %s section.\n", \ | |
279 (unsigned long)s->s_flags, flags, name); \ | |
280 } \ | |
281 if (ptr) { \ | |
282 fprintf(stderr, "unexec: duplicate section header for section %s.\n", \ | |
283 name); \ | |
284 } \ | |
285 ptr = s; \ | |
286 } | |
287 CHECK_SCNHDR(f_thdr, _TEXT, STYP_TEXT); | |
288 CHECK_SCNHDR(f_dhdr, _DATA, STYP_DATA); | |
289 CHECK_SCNHDR(f_bhdr, _BSS, STYP_BSS); | |
290 CHECK_SCNHDR(f_lhdr, _LOADER, STYP_LOADER); | |
291 CHECK_SCNHDR(f_dbhdr, _DEBUG, STYP_DEBUG); | |
292 CHECK_SCNHDR(f_tchdr, _TYPCHK, STYP_TYPCHK); | |
293 CHECK_SCNHDR(f_xhdr, _EXCEPT, STYP_EXCEPT); | |
294 } | |
295 | |
296 if (f_thdr == 0) | |
297 { | |
298 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _TEXT); | |
299 } | |
300 if (f_dhdr == 0) | |
301 { | |
302 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _DATA); | |
303 } | |
304 if (f_bhdr == 0) | |
305 { | |
306 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _BSS); | |
307 } | |
308 } | |
309 else | |
310 { | |
311 ERROR0 ("can't build a COFF file from scratch yet"); | |
312 } | |
313 orig_data_scnptr = f_dhdr->s_scnptr; | |
314 orig_load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0; | |
315 | |
316 /* Now we alter the contents of all the f_*hdr variables | |
317 to correspond to what we want to dump. */ | |
318 | |
319 /* Indicate that the reloc information is no longer valid for ld (bind); | |
320 we only update it enough to fake out the exec-time loader. */ | |
321 f_hdr.f_flags |= (F_RELFLG | F_EXEC); | |
322 | |
323 f_ohdr.dsize = bss_start - f_ohdr.data_start; | |
324 f_ohdr.bsize = bss_end - bss_start; | |
325 | |
326 f_dhdr->s_size = f_ohdr.dsize; | |
327 f_bhdr->s_size = f_ohdr.bsize; | |
328 f_bhdr->s_paddr = f_ohdr.data_start + f_ohdr.dsize; | |
329 f_bhdr->s_vaddr = f_ohdr.data_start + f_ohdr.dsize; | |
330 | |
331 /* fix scnptr's */ | |
332 { | |
333 ulong ptr = section[0].s_scnptr; | |
334 | |
335 bias = -1; | |
336 for (scns = 0; scns < f_hdr.f_nscns; scns++) | |
337 { | |
338 struct scnhdr *s = §ion[scns]; | |
339 | |
340 if (s->s_flags & STYP_PAD) /* .pad sections omitted in AIX 4.1 */ | |
341 { | |
342 /* | |
343 * the text_start should probably be o_algntext but that doesn't | |
344 * seem to change | |
345 */ | |
346 if (f_ohdr.text_start != 0) /* && scns != 0 */ | |
347 { | |
348 s->s_size = 512 - (ptr % 512); | |
349 if (s->s_size == 512) | |
350 s->s_size = 0; | |
351 } | |
352 s->s_scnptr = ptr; | |
353 } | |
354 else if (s->s_flags & STYP_DATA) | |
355 s->s_scnptr = ptr; | |
356 else if (!(s->s_flags & (STYP_TEXT | STYP_BSS))) | |
357 { | |
358 if (bias == -1) /* if first section after bss */ | |
359 bias = ptr - s->s_scnptr; | |
360 | |
361 s->s_scnptr += bias; | |
362 ptr = s->s_scnptr; | |
363 } | |
364 | |
365 ptr = ptr + s->s_size; | |
366 } | |
367 } | |
368 | |
369 /* fix other pointers */ | |
370 for (scns = 0; scns < f_hdr.f_nscns; scns++) | |
371 { | |
372 struct scnhdr *s = §ion[scns]; | |
373 | |
374 if (s->s_relptr != 0) | |
375 { | |
376 s->s_relptr += bias; | |
377 } | |
378 if (s->s_lnnoptr != 0) | |
379 { | |
380 if (lnnoptr == 0) lnnoptr = s->s_lnnoptr; | |
381 s->s_lnnoptr += bias; | |
382 } | |
383 } | |
384 | |
385 if (f_hdr.f_symptr > 0L) | |
386 { | |
387 f_hdr.f_symptr += bias; | |
388 } | |
389 | |
390 text_scnptr = f_thdr->s_scnptr; | |
391 data_scnptr = f_dhdr->s_scnptr; | |
392 load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0; | |
393 | |
3025 | 394 if (write (new_, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) |
428 | 395 { |
396 PERROR (new_name); | |
397 } | |
398 | |
399 if (f_hdr.f_opthdr > 0) | |
400 { | |
3025 | 401 if (write (new_, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) |
428 | 402 { |
403 PERROR (new_name); | |
404 } | |
405 } | |
406 | |
407 for (scns = 0; scns < f_hdr.f_nscns; scns++) { | |
408 struct scnhdr *s = §ion[scns]; | |
3025 | 409 if (write (new_, s, sizeof (*s)) != sizeof (*s)) |
428 | 410 { |
411 PERROR (new_name); | |
412 } | |
413 } | |
414 | |
415 return (0); | |
416 } | |
417 | |
418 /* **************************************************************** | |
419 | |
420 * | |
421 * Copy the text and data segments from memory to the new a.out | |
422 */ | |
423 static int | |
3025 | 424 copy_text_and_data (int new_) |
428 | 425 { |
426 char *end; | |
427 char *ptr; | |
428 | |
3025 | 429 lseek (new_, (long) text_scnptr, SEEK_SET); |
428 | 430 ptr = start_of_text () + text_scnptr; |
431 end = ptr + f_ohdr.tsize; | |
3025 | 432 write_segment (new_, ptr, end); |
428 | 433 |
3025 | 434 lseek (new_, (long) data_scnptr, SEEK_SET); |
428 | 435 ptr = (char *) f_ohdr.data_start; |
436 end = ptr + f_ohdr.dsize; | |
3025 | 437 write_segment (new_, ptr, end); |
428 | 438 |
439 return 0; | |
440 } | |
441 | |
442 #define UnexBlockSz (1<<12) /* read/write block size */ | |
443 static void | |
3025 | 444 write_segment (int new_, char *ptr, char *end) |
428 | 445 { |
446 int i, nwrite, ret; | |
447 char buf[80]; | |
448 char zeros[UnexBlockSz]; | |
449 | |
450 for (i = 0; ptr < end;) | |
451 { | |
452 /* distance to next block. */ | |
453 nwrite = (((int) ptr + UnexBlockSz) & -UnexBlockSz) - (int) ptr; | |
454 /* But not beyond specified end. */ | |
455 if (nwrite > end - ptr) nwrite = end - ptr; | |
3025 | 456 ret = write (new_, ptr, nwrite); |
428 | 457 /* If write gets a page fault, it means we reached |
458 a gap between the old text segment and the old data segment. | |
459 This gap has probably been remapped into part of the text segment. | |
460 So write zeros for it. */ | |
461 if (ret == -1 && errno == EFAULT) | |
462 { | |
463 memset (zeros, 0, nwrite); | |
3025 | 464 write (new_, zeros, nwrite); |
428 | 465 } |
466 else if (nwrite != ret) | |
467 { | |
468 sprintf (buf, | |
469 "unexec write failure: addr 0x%lx, fileno %d, size 0x%x, wrote 0x%x, errno %d", | |
3025 | 470 (unsigned long)ptr, new_, nwrite, ret, errno); |
428 | 471 PERROR (buf); |
472 } | |
473 i += nwrite; | |
474 ptr += nwrite; | |
475 } | |
476 } | |
477 | |
478 /* **************************************************************** | |
479 * copy_sym | |
480 * | |
481 * Copy the relocation information and symbol table from the a.out to the new | |
482 */ | |
483 static int | |
3025 | 484 copy_sym (int new_, int a_out, char *a_name, char *new_name) |
428 | 485 { |
486 char page[UnexBlockSz]; | |
487 int n; | |
488 | |
489 if (a_out < 0) | |
490 return 0; | |
491 | |
492 if (orig_load_scnptr == 0L) | |
493 return 0; | |
494 | |
495 if (lnnoptr && lnnoptr < orig_load_scnptr) /* if there is line number info */ | |
496 lseek (a_out, lnnoptr, SEEK_SET); /* start copying from there */ | |
497 else | |
498 lseek (a_out, orig_load_scnptr, SEEK_SET); /* Position a.out to symtab. */ | |
499 | |
647 | 500 while ((n = read (a_out, page, sizeof (page))) > 0) |
428 | 501 { |
3025 | 502 if (write (new_, page, n) != n) |
428 | 503 { |
504 PERROR (new_name); | |
505 } | |
506 } | |
507 if (n < 0) | |
508 { | |
509 PERROR (a_name); | |
510 } | |
511 return 0; | |
512 } | |
513 | |
514 /* **************************************************************** | |
515 * mark_x | |
516 * | |
517 * After successfully building the new a.out, mark it executable | |
518 */ | |
519 static void | |
520 mark_x (char *name) | |
521 { | |
522 struct stat sbuf; | |
523 int um; | |
3025 | 524 int new_ = 0; /* for PERROR */ |
428 | 525 |
526 um = umask (777); | |
527 umask (um); | |
528 if (stat (name, &sbuf) == -1) | |
529 { | |
530 PERROR (name); | |
531 } | |
532 sbuf.st_mode |= 0111 & ~um; | |
533 if (chmod (name, sbuf.st_mode) == -1) | |
534 PERROR (name); | |
535 } | |
536 | |
537 static int | |
2286 | 538 adjust_lnnoptrs (int UNUSED (writedesc), int UNUSED (readdesc), char *new_name) |
428 | 539 { |
540 int nsyms; | |
541 int naux; | |
3025 | 542 int new_; |
428 | 543 struct syment symentry; |
544 union auxent auxentry; | |
545 | |
546 if (!lnnoptr || !f_hdr.f_symptr) | |
547 return 0; | |
548 | |
3025 | 549 if ((new_ = open (new_name, O_RDWR)) < 0) |
428 | 550 { |
551 PERROR (new_name); | |
552 return -1; | |
553 } | |
554 | |
3025 | 555 lseek (new_, f_hdr.f_symptr, SEEK_SET); |
428 | 556 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++) |
557 { | |
3025 | 558 read (new_, &symentry, SYMESZ); |
428 | 559 if (symentry.n_sclass == C_BINCL || symentry.n_sclass == C_EINCL) |
560 { | |
561 symentry.n_value += bias; | |
3025 | 562 lseek (new_, -SYMESZ, SEEK_CUR); |
563 write (new_, &symentry, SYMESZ); | |
428 | 564 } |
565 | |
566 for (naux = symentry.n_numaux; naux-- != 0; ) | |
567 { | |
3025 | 568 read (new_, &auxentry, AUXESZ); |
428 | 569 nsyms++; |
570 if (naux != 0 /* skip csect auxentry (last entry) */ | |
571 && (symentry.n_sclass == C_EXT || symentry.n_sclass == C_HIDEXT)) | |
572 { | |
573 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias; | |
3025 | 574 lseek (new_, -AUXESZ, SEEK_CUR); |
575 write (new_, &auxentry, AUXESZ); | |
428 | 576 } |
577 } | |
578 } | |
3025 | 579 close (new_); |
428 | 580 |
581 return 0; | |
582 } | |
583 | |
584 static int | |
3025 | 585 unrelocate_symbols (int new_, int a_out, char *a_name, char *new_name) |
428 | 586 { |
587 int i; | |
588 LDHDR ldhdr; | |
589 LDREL ldrel; | |
590 ulong t_reloc = (ulong) &_text - f_ohdr.text_start; | |
591 ulong d_reloc = (ulong) &_data - f_ohdr.data_start; | |
592 int * p; | |
593 | |
594 if (load_scnptr == 0) | |
595 return 0; | |
596 | |
597 lseek (a_out, orig_load_scnptr, SEEK_SET); | |
598 if (read (a_out, &ldhdr, sizeof (ldhdr)) != sizeof (ldhdr)) | |
599 { | |
600 PERROR (new_name); | |
601 } | |
602 | |
603 #define SYMNDX_TEXT 0 | |
604 #define SYMNDX_DATA 1 | |
605 #define SYMNDX_BSS 2 | |
606 | |
607 for (i = 0; i < ldhdr.l_nreloc; i++) | |
608 { | |
609 lseek (a_out, | |
610 orig_load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i, | |
611 SEEK_SET); | |
612 | |
613 if (read (a_out, &ldrel, LDRELSZ) != LDRELSZ) | |
614 { | |
615 PERROR (a_name); | |
616 } | |
617 | |
618 /* move the BSS loader symbols to the DATA segment */ | |
619 if (ldrel.l_symndx == SYMNDX_BSS) | |
620 { | |
621 ldrel.l_symndx = SYMNDX_DATA; | |
622 | |
3025 | 623 lseek (new_, |
428 | 624 load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i, |
625 SEEK_SET); | |
626 | |
3025 | 627 if (write (new_, &ldrel, LDRELSZ) != LDRELSZ) |
428 | 628 { |
629 PERROR (new_name); | |
630 } | |
631 } | |
632 | |
633 if (ldrel.l_rsecnm == f_ohdr.o_sndata) | |
634 { | |
635 int orig_int; | |
636 | |
637 lseek (a_out, | |
638 orig_data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start), | |
639 SEEK_SET); | |
640 | |
641 if (read (a_out, (void *) &orig_int, sizeof (orig_int)) | |
642 != sizeof (orig_int)) | |
643 { | |
644 PERROR (a_name); | |
645 } | |
646 | |
647 p = (int *) (ldrel.l_vaddr + d_reloc); | |
648 | |
649 switch (ldrel.l_symndx) { | |
650 case SYMNDX_TEXT: | |
651 orig_int = * p - t_reloc; | |
652 break; | |
653 | |
654 case SYMNDX_DATA: | |
655 case SYMNDX_BSS: | |
656 orig_int = * p - d_reloc; | |
657 break; | |
658 } | |
659 | |
660 if (orig_int != * p) | |
661 { | |
3025 | 662 lseek (new_, |
428 | 663 data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start), |
664 SEEK_SET); | |
3025 | 665 if (write (new_, (void *) &orig_int, sizeof (orig_int)) |
428 | 666 != sizeof (orig_int)) |
667 { | |
668 PERROR (new_name); | |
669 } | |
670 } | |
671 } | |
672 } | |
673 return 0; | |
674 } |