annotate src/unexaix.c @ 611:38db05db9cb5

[xemacs-hg @ 2001-06-08 12:21:09 by ben] ------ gc-in-window-procedure fixes ------ alloc.c: Create "post-gc actions", to avoid those dreaded "GC during window procedure" problems. event-msw.c: Abort, clean and simple, when GC in window procedure. We want to flush these puppies out. glyphs-msw.c: Use a post-gc action when destroying subwindows. lisp.h: Declare register_post_gc_action(). scrollbar-msw.c: Use a post-gc action when unshowing scrollbar windows, if in gc. redisplay.c: Add comment about the utter evilness of what's going down here. ------ cygwin setitimer fixes ------ Makefile.in.in: Compile profile.c only when HAVE_SETITIMER. nt.c: Style fixes. nt.c: Move setitimer() emulation to win32.c, because Cygwin needs it too. profile.c: Make sure we don't compile if no setitimer(). Use qxe_setitimer() instead of just plain setitimer(). signal.c: Define qxe_setitimer() as an encapsulation around setitimer() -- call setitimer() directly unless Cygwin or MS Win, in which case we use our simulated version in win32.c. systime.h: Prototype mswindows_setitimer() and qxe_setitimer(). Long comment about "qxe" and the policy regarding encapsulation. win32.c: Move setitimer() emulation here, so Cygwin can use it. Rename a couple of functions and variables to be longer and more descriptive. In setitimer_helper_proc(), send the signal using either mswindows_raise() or (on Cygwin) kill(). If for some reason we are still getting lockups, we'll change the kill() to directly invoke the signal handlers. ------ windows shell fixes ------ callproc.c, ntproc.c: Comments about how these two files must die. callproc.c: On MS Windows, init shell-file-name from SHELL, then COMSPEC, not just COMSPEC. (more correct and closer to FSF.) Don't force a value for SHELL into the environment. (Comments added to explain why not.) nt.c: Don't shove a fabricated SHELL into the environment. See above. ------ misc fixes ------ glyphs-shared.c: Style correction. xemacs-faq.texi: Merge in the rest of Hrvoje's Windows FAQ. Redo section 7 to update current reality and add condensed versions of new changes for 21.1 and 21.4. (Not quite done for 21.4.) Lots more Windows updates. process.el: Need to quote a null argument, too. From Dan Holmsand. startup.el: startup.el: Call MS Windows init function. win32-native.el: Correct comments at top. Correctly handle passing arguments to Cygwin programs and to bash. Fix quoting of zero-length arguments (from Dan Holmsand). Set shell-command-switch based on shell-file-name, which in turn comes from env var SHELL.
author ben
date Fri, 08 Jun 2001 12:21:27 +0000
parents 183866b06e0b
children b39c14581166
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Dump an executable image.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 /* Synched up with: FSF 20.2. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* Originally based on the COFF unexec.c by Spencer W. Thomas.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 * Subsequently hacked on by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 * Bill Mann <Bill_Man@praxisint.com>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 * Andrew Vignaux <Andrew.Vignaux@comp.vuw.ac.nz>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 * Mike Sperber <sperber@informatik.uni-tuebingen.de>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 * Synopsis:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 * unexec (new_name, a_name, data_start, bss_start, entry_address)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 * char *new_name, *a_name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 * unsigned data_start, bss_start, entry_address;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 * Takes a snapshot of the program and makes an a.out format file in the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 * file named by the string argument new_name.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 * If a_name is non-NULL, the symbol table will be taken from the given file.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 * On some machines, an existing a_name file is required.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 * data_start and entry_address are ignored.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 * bss_start indicates how much of the data segment is to be saved in the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 * a.out file and restored when the program is executed. It gives the lowest
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 * unsaved address, and is rounded up to a page boundary. The default when 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 * is given assumes that the entire data segment is to be stored, including
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 * the previous data and bss as well as any additional storage allocated with
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 * sbrk(2).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 #ifndef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 #define PERROR(arg) perror (arg); return -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 #define PERROR(file) report_error (file, new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 #include <a.out.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 /* Define getpagesize () if the system does not.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 Note that this may depend on symbols defined in a.out.h
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 #include "getpagesize.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #include <sys/types.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 #include <sys/stat.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 #include <errno.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 #include <fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 extern char *start_of_text (void); /* Start of text */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 extern char *start_of_data (void); /* Start of initialized data */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 extern int _data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 extern int _text;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #include <filehdr.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #include <aouthdr.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 #include <scnhdr.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 #include <syms.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 static struct filehdr f_hdr; /* File header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 static long bias; /* Bias to add for growth */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 static long lnnoptr; /* Pointer to line-number info within file */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 static long text_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 static long data_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 #define ALIGN(val, pwr) (((val) + ((1L<<(pwr))-1)) & ~((1L<<(pwr))-1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 static long load_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 static long orig_load_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 static long orig_data_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 static int unrelocate_symbols (int, int, char *, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 #ifndef MAX_SECTIONS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 #define MAX_SECTIONS 10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 static int adjust_lnnoptrs (int, int, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 static int pagemask;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 /* Correct an int which is the bit pattern of a pointer to a byte
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 into an int which is the number of a byte.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 This is a no-op on ordinary machines, but not on all. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 #ifndef ADDR_CORRECT /* Let m-*.h files override this definition */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 #ifdef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 report_error (char *file, int fd)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 if (fd)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 close (fd);
563
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 438
diff changeset
119 report_error_with_errno (Qio_error, "Cannot unexec",
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 438
diff changeset
120 build_string (file));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 #endif /* emacs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 report_error_1 (int fd, char *msg, int a1, int a2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 close (fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 #ifdef emacs
563
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 438
diff changeset
133 signal_ferror (Qio_error, msg, a1, a2);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 fprintf (stderr, msg, a1, a2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 fprintf (stderr, "\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 static int make_hdr (int, int, unsigned, unsigned, unsigned, char *, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 static void mark_x (char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 static int copy_text_and_data (int);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 static int copy_sym (int, int, char *, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 static void write_segment (int, char *, char *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 /* ****************************************************************
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 * unexec
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 * driving logic.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 int unexec (char *new_name, char *a_name,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 uintptr_t data_start,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 uintptr_t bss_start,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 uintptr_t entry_address)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 int new = -1, a_out = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 if ((new = creat (new_name, 0666)) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 if (make_hdr (new, a_out,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 data_start, bss_start,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 entry_address,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 a_name, new_name) < 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 || copy_text_and_data (new) < 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 || copy_sym (new, a_out, a_name, new_name) < 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 || adjust_lnnoptrs (new, a_out, new_name) < 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 || unrelocate_symbols (new, a_out, a_name, new_name) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 close (new);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 close (new);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 if (a_out >= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 close (a_out);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 mark_x (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 /* ****************************************************************
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 * make_hdr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 * Make the header in the new a.out from the header in core.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 * Modify the text and data sizes.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 make_hdr (int new, int a_out,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 unsigned data_start, unsigned bss_start,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 unsigned entry_address,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 char *a_name, char *new_name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 int scns;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 unsigned int bss_end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 struct scnhdr section[MAX_SECTIONS];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 struct scnhdr * f_thdr; /* Text section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 struct scnhdr * f_dhdr; /* Data section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 struct scnhdr * f_bhdr; /* Bss section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 struct scnhdr * f_lhdr; /* Loader section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 struct scnhdr * f_tchdr; /* Typechk section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 struct scnhdr * f_dbhdr; /* Debug section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 struct scnhdr * f_xhdr; /* Except section header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 load_scnptr = orig_load_scnptr = lnnoptr = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 pagemask = getpagesize () - 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 /* Adjust text/data boundary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 data_start = (long) start_of_data ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 data_start = ADDR_CORRECT (data_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 bss_end &= ~ pagemask;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 /* Adjust data/bss boundary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 if (bss_start != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 bss_start = (ADDR_CORRECT (bss_start) + pagemask);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 /* (Up) to page bdry. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 bss_start &= ~ pagemask;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 if (bss_start > bss_end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 bss_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 bss_start = bss_end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 if (data_start > bss_start) /* Can't have negative data size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 data_start, bss_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 /* Salvage as much info from the existing file as possible */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 f_thdr = NULL; f_dhdr = NULL; f_bhdr = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 f_lhdr = NULL; f_tchdr = NULL; f_dbhdr = NULL; f_xhdr = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 if (a_out >= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 if (f_hdr.f_opthdr > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 if (f_hdr.f_nscns > MAX_SECTIONS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 ERROR0 ("unexec: too many section headers -- increase MAX_SECTIONS");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 /* Loop through section headers */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 struct scnhdr *s = &section[scns];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 if (read (a_out, s, sizeof (*s)) != sizeof (*s))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 #define CHECK_SCNHDR(ptr, name, flags) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 if (strcmp(s->s_name, name) == 0) { \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 if (s->s_flags != flags) { \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 fprintf(stderr, "unexec: %lx flags where %x expected in %s section.\n", \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 (unsigned long)s->s_flags, flags, name); \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 } \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 if (ptr) { \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 fprintf(stderr, "unexec: duplicate section header for section %s.\n", \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 name); \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 } \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 ptr = s; \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 CHECK_SCNHDR(f_thdr, _TEXT, STYP_TEXT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 CHECK_SCNHDR(f_dhdr, _DATA, STYP_DATA);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 CHECK_SCNHDR(f_bhdr, _BSS, STYP_BSS);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 CHECK_SCNHDR(f_lhdr, _LOADER, STYP_LOADER);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 CHECK_SCNHDR(f_dbhdr, _DEBUG, STYP_DEBUG);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 CHECK_SCNHDR(f_tchdr, _TYPCHK, STYP_TYPCHK);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 CHECK_SCNHDR(f_xhdr, _EXCEPT, STYP_EXCEPT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 if (f_thdr == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _TEXT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 if (f_dhdr == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _DATA);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 if (f_bhdr == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _BSS);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 ERROR0 ("can't build a COFF file from scratch yet");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 orig_data_scnptr = f_dhdr->s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 orig_load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 /* Now we alter the contents of all the f_*hdr variables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 to correspond to what we want to dump. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 /* Indicate that the reloc information is no longer valid for ld (bind);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 we only update it enough to fake out the exec-time loader. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 f_ohdr.dsize = bss_start - f_ohdr.data_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 f_ohdr.bsize = bss_end - bss_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 f_dhdr->s_size = f_ohdr.dsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 f_bhdr->s_size = f_ohdr.bsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 f_bhdr->s_paddr = f_ohdr.data_start + f_ohdr.dsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 f_bhdr->s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 /* fix scnptr's */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 ulong ptr = section[0].s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 bias = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 for (scns = 0; scns < f_hdr.f_nscns; scns++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 struct scnhdr *s = &section[scns];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 if (s->s_flags & STYP_PAD) /* .pad sections omitted in AIX 4.1 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 * the text_start should probably be o_algntext but that doesn't
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 * seem to change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 if (f_ohdr.text_start != 0) /* && scns != 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 s->s_size = 512 - (ptr % 512);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 if (s->s_size == 512)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 s->s_size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 s->s_scnptr = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 else if (s->s_flags & STYP_DATA)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 s->s_scnptr = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 else if (!(s->s_flags & (STYP_TEXT | STYP_BSS)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 if (bias == -1) /* if first section after bss */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 bias = ptr - s->s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 s->s_scnptr += bias;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 ptr = s->s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 ptr = ptr + s->s_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 /* fix other pointers */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 for (scns = 0; scns < f_hdr.f_nscns; scns++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 struct scnhdr *s = &section[scns];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 if (s->s_relptr != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 s->s_relptr += bias;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 if (s->s_lnnoptr != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 if (lnnoptr == 0) lnnoptr = s->s_lnnoptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 s->s_lnnoptr += bias;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 if (f_hdr.f_symptr > 0L)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 f_hdr.f_symptr += bias;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 text_scnptr = f_thdr->s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 data_scnptr = f_dhdr->s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 if (f_hdr.f_opthdr > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 struct scnhdr *s = &section[scns];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 if (write (new, s, sizeof (*s)) != sizeof (*s))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 return (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 /* ****************************************************************
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 * Copy the text and data segments from memory to the new a.out
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 copy_text_and_data (int new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 char *end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 char *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 lseek (new, (long) text_scnptr, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 ptr = start_of_text () + text_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 end = ptr + f_ohdr.tsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 write_segment (new, ptr, end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 lseek (new, (long) data_scnptr, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 ptr = (char *) f_ohdr.data_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 end = ptr + f_ohdr.dsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 write_segment (new, ptr, end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 #define UnexBlockSz (1<<12) /* read/write block size */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 write_segment (int new, char *ptr, char *end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 int i, nwrite, ret;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 char buf[80];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 char zeros[UnexBlockSz];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 for (i = 0; ptr < end;)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 /* distance to next block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 nwrite = (((int) ptr + UnexBlockSz) & -UnexBlockSz) - (int) ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 /* But not beyond specified end. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 if (nwrite > end - ptr) nwrite = end - ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 ret = write (new, ptr, nwrite);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 /* If write gets a page fault, it means we reached
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 a gap between the old text segment and the old data segment.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 This gap has probably been remapped into part of the text segment.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 So write zeros for it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 if (ret == -1 && errno == EFAULT)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 memset (zeros, 0, nwrite);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 write (new, zeros, nwrite);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 else if (nwrite != ret)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 sprintf (buf,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 "unexec write failure: addr 0x%lx, fileno %d, size 0x%x, wrote 0x%x, errno %d",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 (unsigned long)ptr, new, nwrite, ret, errno);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 PERROR (buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 i += nwrite;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 ptr += nwrite;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 /* ****************************************************************
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 * copy_sym
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 * Copy the relocation information and symbol table from the a.out to the new
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 copy_sym (int new, int a_out, char *a_name, char *new_name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 char page[UnexBlockSz];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 int n;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 if (a_out < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 if (orig_load_scnptr == 0L)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 if (lnnoptr && lnnoptr < orig_load_scnptr) /* if there is line number info */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 lseek (a_out, lnnoptr, SEEK_SET); /* start copying from there */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 lseek (a_out, orig_load_scnptr, SEEK_SET); /* Position a.out to symtab. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 while ((n = read (a_out, page, sizeof page)) > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 if (write (new, page, n) != n)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 if (n < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 /* ****************************************************************
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 * mark_x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 * After successfully building the new a.out, mark it executable
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 mark_x (char *name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 struct stat sbuf;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 int um;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 int new = 0; /* for PERROR */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 um = umask (777);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 umask (um);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 if (stat (name, &sbuf) == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 PERROR (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 sbuf.st_mode |= 0111 & ~um;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 if (chmod (name, sbuf.st_mode) == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 PERROR (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 adjust_lnnoptrs (int writedesc, int readdesc, char *new_name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 int nsyms;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 int naux;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 int new;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 struct syment symentry;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 union auxent auxentry;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 if (!lnnoptr || !f_hdr.f_symptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 if ((new = open (new_name, O_RDWR)) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 lseek (new, f_hdr.f_symptr, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 read (new, &symentry, SYMESZ);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 if (symentry.n_sclass == C_BINCL || symentry.n_sclass == C_EINCL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 symentry.n_value += bias;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 lseek (new, -SYMESZ, SEEK_CUR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 write (new, &symentry, SYMESZ);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 for (naux = symentry.n_numaux; naux-- != 0; )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 read (new, &auxentry, AUXESZ);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 nsyms++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 if (naux != 0 /* skip csect auxentry (last entry) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 && (symentry.n_sclass == C_EXT || symentry.n_sclass == C_HIDEXT))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 lseek (new, -AUXESZ, SEEK_CUR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 write (new, &auxentry, AUXESZ);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 close (new);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 unrelocate_symbols (int new, int a_out, char *a_name, char *new_name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 LDHDR ldhdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 LDREL ldrel;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 ulong t_reloc = (ulong) &_text - f_ohdr.text_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 #ifndef ALIGN_DATA_RELOC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 ulong d_reloc = (ulong) &_data - f_ohdr.data_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 /* This worked (and was needed) before AIX 4.2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 I have no idea why. -- Mike */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 ulong d_reloc = (ulong) &_data - ALIGN(f_ohdr.data_start, 2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 int * p;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 if (load_scnptr == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 lseek (a_out, orig_load_scnptr, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 if (read (a_out, &ldhdr, sizeof (ldhdr)) != sizeof (ldhdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 #define SYMNDX_TEXT 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 #define SYMNDX_DATA 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 #define SYMNDX_BSS 2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 for (i = 0; i < ldhdr.l_nreloc; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 lseek (a_out,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 orig_load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 if (read (a_out, &ldrel, LDRELSZ) != LDRELSZ)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 /* move the BSS loader symbols to the DATA segment */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 if (ldrel.l_symndx == SYMNDX_BSS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 ldrel.l_symndx = SYMNDX_DATA;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 lseek (new,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 if (write (new, &ldrel, LDRELSZ) != LDRELSZ)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 if (ldrel.l_rsecnm == f_ohdr.o_sndata)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 int orig_int;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 lseek (a_out,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 orig_data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 if (read (a_out, (void *) &orig_int, sizeof (orig_int))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 != sizeof (orig_int))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 p = (int *) (ldrel.l_vaddr + d_reloc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 switch (ldrel.l_symndx) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 case SYMNDX_TEXT:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 orig_int = * p - t_reloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 case SYMNDX_DATA:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 case SYMNDX_BSS:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 orig_int = * p - d_reloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 if (orig_int != * p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 lseek (new,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 if (write (new, (void *) &orig_int, sizeof (orig_int))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 != sizeof (orig_int))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 }