| 1 | /* cmd.c -- Handle commands
|
|---|
| 2 | Copyright (c) 1991-1995 Eberhard Mattes
|
|---|
| 3 |
|
|---|
| 4 | This file is part of emxbind.
|
|---|
| 5 |
|
|---|
| 6 | emxbind is free software; you can redistribute it and/or modify it
|
|---|
| 7 | under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | emxbind is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with emxbind; see the file COPYING. If not, write to
|
|---|
| 18 | the Free Software Foundation, 59 Temple Place - Suite 330,
|
|---|
| 19 | Boston, MA 02111-1307, USA. */
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <stdlib.h>
|
|---|
| 24 | #include <string.h>
|
|---|
| 25 | #include "defs.h"
|
|---|
| 26 | #include "emxbind.h"
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | /* Alter the emx options of a bound executable. */
|
|---|
| 30 |
|
|---|
| 31 | static void alter (void)
|
|---|
| 32 | {
|
|---|
| 33 | set_options ();
|
|---|
| 34 | my_seek (&inp_file, patch_pos);
|
|---|
| 35 | my_write (&dos_bind_h, sizeof (dos_bind_h), &inp_file);
|
|---|
| 36 | my_seek (&inp_file, a_in_pos + a_in_data);
|
|---|
| 37 | my_write (&os2_bind_h, sizeof (os2_bind_h), &inp_file);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | /* Perform initializations for the bind operation. */
|
|---|
| 42 |
|
|---|
| 43 | static void init_bind (void)
|
|---|
| 44 | {
|
|---|
| 45 | if (dll_flag)
|
|---|
| 46 | {
|
|---|
| 47 | relocatable = TRUE;
|
|---|
| 48 | stack_size = 0;
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | /* Change the application type. */
|
|---|
| 54 |
|
|---|
| 55 | static void exe_type (void)
|
|---|
| 56 | {
|
|---|
| 57 | read_os2_header ();
|
|---|
| 58 | exe_flags ();
|
|---|
| 59 | my_change (&out_file, &inp_file);
|
|---|
| 60 | my_seek (&out_file, inp_os2_pos);
|
|---|
| 61 | my_write (&os2_h, sizeof (os2_h), &out_file);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | /* Show the emx options of a bound executable. */
|
|---|
| 66 |
|
|---|
| 67 | static void show (void)
|
|---|
| 68 | {
|
|---|
| 69 | if (dos_bind_h.options[0] != 0)
|
|---|
| 70 | printf ("emx options for MS-DOS: %s\n", (char *)dos_bind_h.options);
|
|---|
| 71 | if (os2_bind_h.options[0] != 0)
|
|---|
| 72 | printf ("emx options for OS/2: %s\n", (char *)os2_bind_h.options);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | /* Strip the symbols from a bound executable. */
|
|---|
| 77 |
|
|---|
| 78 | static void strip_symbols (void)
|
|---|
| 79 | {
|
|---|
| 80 | long size, pos, str_len;
|
|---|
| 81 |
|
|---|
| 82 | pos = a_in_pos + a_in_sym;
|
|---|
| 83 | size = my_size (&inp_file);
|
|---|
| 84 | if (pos + sizeof (str_len) <= size)
|
|---|
| 85 | {
|
|---|
| 86 | my_seek (&inp_file, pos);
|
|---|
| 87 | str_len = 4;
|
|---|
| 88 | my_write (&str_len, sizeof (str_len), &inp_file);
|
|---|
| 89 | my_trunc (&inp_file);
|
|---|
| 90 | a_in_h.sym_size = 0;
|
|---|
| 91 | my_seek (&inp_file, a_in_pos);
|
|---|
| 92 | my_write (&a_in_h, sizeof (a_in_h), &inp_file);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | /* Update emxl.exe or emx.exe in a bound executable. */
|
|---|
| 98 |
|
|---|
| 99 | static void update (void)
|
|---|
| 100 | {
|
|---|
| 101 | long size, i;
|
|---|
| 102 | byte *buf;
|
|---|
| 103 |
|
|---|
| 104 | read_os2_header ();
|
|---|
| 105 | set_exe_header ();
|
|---|
| 106 | i = (inp_os2_pos & 0xfff) - (os2_hdr_pos & 0xfff);
|
|---|
| 107 | if (i != 0)
|
|---|
| 108 | {
|
|---|
| 109 | if (i < 0)
|
|---|
| 110 | i += 0x1000;
|
|---|
| 111 | os2_hdr_pos += i; fill2 += i;
|
|---|
| 112 | }
|
|---|
| 113 | out_h2.new_lo = LOWORD (os2_hdr_pos);
|
|---|
| 114 | out_h2.new_hi = HIWORD (os2_hdr_pos);
|
|---|
| 115 | size = my_size (&inp_file) - inp_os2_pos - sizeof (os2_h);
|
|---|
| 116 | a_out_pos = a_in_pos - inp_os2_pos + os2_hdr_pos;
|
|---|
| 117 | os2_h.enum_offset += os2_hdr_pos - inp_os2_pos;
|
|---|
| 118 | if (os2_h.nonresname_size != 0)
|
|---|
| 119 | os2_h.nonresname_offset += os2_hdr_pos - inp_os2_pos;
|
|---|
| 120 | dos_bind_h.hdr_loc_lo = LOWORD (a_out_pos);
|
|---|
| 121 | dos_bind_h.hdr_loc_hi = HIWORD (a_out_pos);
|
|---|
| 122 | os2_bind_h.heap_off += os2_hdr_pos - inp_os2_pos;
|
|---|
| 123 | memmove (dos_bind_h.hdr, emx_bind_h.hdr, sizeof (dos_bind_h.hdr));
|
|---|
| 124 | buf = xmalloc (size);
|
|---|
| 125 | my_seek (&inp_file, inp_os2_pos + sizeof (os2_h));
|
|---|
| 126 | my_read (buf, size, &inp_file);
|
|---|
| 127 | my_change (&out_file, &inp_file);
|
|---|
| 128 | write_header ();
|
|---|
| 129 | my_write (&os2_h, sizeof (os2_h), &out_file);
|
|---|
| 130 | my_write (buf, size, &out_file);
|
|---|
| 131 | my_trunc (&out_file);
|
|---|
| 132 | write_bind_header ();
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | /* Extract the a.out subfile of a bound executable. */
|
|---|
| 137 |
|
|---|
| 138 | static void extract (void)
|
|---|
| 139 | {
|
|---|
| 140 | long hdr_loc, size;
|
|---|
| 141 |
|
|---|
| 142 | hdr_loc = COMBINE (dos_bind_h.hdr_loc_lo, dos_bind_h.hdr_loc_hi);
|
|---|
| 143 | size = my_size (&inp_file) - hdr_loc;
|
|---|
| 144 | my_seek (&inp_file, hdr_loc);
|
|---|
| 145 | copy (&inp_file, size);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | void cmd (int mode)
|
|---|
| 150 | {
|
|---|
| 151 | switch (mode)
|
|---|
| 152 | {
|
|---|
| 153 | case 'a':
|
|---|
| 154 |
|
|---|
| 155 | /* Alter emxbind options. */
|
|---|
| 156 |
|
|---|
| 157 | check_bound ();
|
|---|
| 158 | alter ();
|
|---|
| 159 | break;
|
|---|
| 160 |
|
|---|
| 161 | case 'b':
|
|---|
| 162 |
|
|---|
| 163 | /* Bind an a.out file into an .exe file. */
|
|---|
| 164 |
|
|---|
| 165 | init_bind ();
|
|---|
| 166 | init_os2_header ();
|
|---|
| 167 | read_emx ();
|
|---|
| 168 | read_a_out_header ();
|
|---|
| 169 | if (opt_c != NULL)
|
|---|
| 170 | read_core ();
|
|---|
| 171 | read_res ();
|
|---|
| 172 | os2_fixup ();
|
|---|
| 173 | relocations ();
|
|---|
| 174 | sort_fixup ();
|
|---|
| 175 | set_exe_header ();
|
|---|
| 176 | set_os2_header ();
|
|---|
| 177 | set_dos_bind_header ();
|
|---|
| 178 | write_header ();
|
|---|
| 179 | write_res ();
|
|---|
| 180 | copy_a_out ();
|
|---|
| 181 | write_nonres ();
|
|---|
| 182 | write_bind_header ();
|
|---|
| 183 | if (opt_m != NULL)
|
|---|
| 184 | write_map (opt_m);
|
|---|
| 185 | break;
|
|---|
| 186 |
|
|---|
| 187 | case 'e':
|
|---|
| 188 |
|
|---|
| 189 | /* Set the OS/2 application type. */
|
|---|
| 190 |
|
|---|
| 191 | check_bound ();
|
|---|
| 192 | exe_type ();
|
|---|
| 193 | break;
|
|---|
| 194 |
|
|---|
| 195 | #if LIST_OPT
|
|---|
| 196 | case 'L':
|
|---|
| 197 |
|
|---|
| 198 | /* List the headers. */
|
|---|
| 199 |
|
|---|
| 200 | check_bound ();
|
|---|
| 201 | list ();
|
|---|
| 202 | break;
|
|---|
| 203 | #endif
|
|---|
| 204 | case 'i':
|
|---|
| 205 |
|
|---|
| 206 | /* Show the emxbind options. */
|
|---|
| 207 |
|
|---|
| 208 | check_bound ();
|
|---|
| 209 | show ();
|
|---|
| 210 | break;
|
|---|
| 211 |
|
|---|
| 212 | case 's':
|
|---|
| 213 |
|
|---|
| 214 | /* Strip symbols. */
|
|---|
| 215 |
|
|---|
| 216 | check_bound ();
|
|---|
| 217 | strip_symbols ();
|
|---|
| 218 | break;
|
|---|
| 219 |
|
|---|
| 220 | case 'u':
|
|---|
| 221 |
|
|---|
| 222 | /* Update emxl.exe or emx.exe in a bound executable. */
|
|---|
| 223 |
|
|---|
| 224 | read_emx ();
|
|---|
| 225 |
|
|---|
| 226 | /* Call check_bound() *after* read_emx() as it needs dos_bind_h! */
|
|---|
| 227 |
|
|---|
| 228 | check_bound ();
|
|---|
| 229 | update ();
|
|---|
| 230 | break;
|
|---|
| 231 |
|
|---|
| 232 | case 'x':
|
|---|
| 233 |
|
|---|
| 234 | /* Extract the a.out file of a bound executable. */
|
|---|
| 235 |
|
|---|
| 236 | check_bound ();
|
|---|
| 237 | extract ();
|
|---|
| 238 | break;
|
|---|
| 239 |
|
|---|
| 240 | default:
|
|---|
| 241 | abort ();
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|