annotate src/unexhp9k800.c @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 57709be46d1b
children 697ef44129c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Unexec for HP 9000 Series 800 machines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Bob Desinger <hpsemc!bd@hplabs.hp.com>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: Not synched with FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 Unexec creates a copy of the old a.out file, and replaces the old data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 area with the current data area. When the new file is executed, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 process will see the same data structures and data values that the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 original process had when unexec was called.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
29
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 Unlike other versions of unexec, this one copies symbol table and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 debug information to the new a.out file. Thus, the new a.out file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 may be debugged with symbolic debuggers.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
33
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 If you fix any bugs in this, I'd like to incorporate your fixes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
36
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 CAVEATS:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 This routine saves the current value of all static and external
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 variables. This means that any data structure that needs to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 initialized must be explicitly reset. Variables will not have their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 expected default values.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
42
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 Unfortunately, the HP-UX signal handler has internal initialization
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 flags which are not explicitly reset. Thus, for signals to work in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 conjunction with this routine, the following code must executed when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 the new process starts up.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
47
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 void _sigreturn();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 sigsetreturn(_sigreturn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 #include <config.h>
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 286
diff changeset
55 #include <stdlib.h>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #include <a.out.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 * Minor modification to enable dumping with shared libraries added by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 * Dipankar Gupta (dg@hplb.hpl.hp.com). I studied Oliver Laumann's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 * more elaborate dynamic loading scheme in ELK while implementing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 * this, but don't use any of his machinery.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 * Stores the BRK value at dump time, and uses the RUN_TIME_REMAP hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 * to break back to the stored value when the dumped executable is restarted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 * CAVEATS (addenda):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 * 1. Text area of the shlibs are not stored. Thus, if a shared library is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 * replaced between the time of dump and execution, all bets are off.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 * 2. Assumes that the data and bss area are adjacent, which is true of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 * current VM implementation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 * 3. Any setup that defines HPUX_USE_SHLIBS *must* also define
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
79 * RUN_TIME_REMAP.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 #ifdef HPUX_USE_SHLIBS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 #include <dl.h> /* User-space dynamic loader entry points */
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 286
diff changeset
84 void Save_Shared_Data(void);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 int run_time_remap();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 #define min(x,y) ( ((x)<(y))?(x):(y) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
90 void write_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
91 void read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
92 void save_data_space (int file, struct header *hdr,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
93 struct som_exec_auxhdr *auxhdr, int size);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
94 void copy_rest (int old, int new);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
95 void copy_file (int old, int new, int size);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
96 void update_file_ptrs(int file, struct header *hdr,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
97 struct som_exec_auxhdr *auxhdr,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
98 unsigned int location, int offset);
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 280
diff changeset
99 int calculate_checksum(struct header *hdr);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 /* Create a new a.out file, same as old but with current data space */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
102 int
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
103 unexec(char new_name[], /* name of the new a.out file to be created */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
104 char old_name[], /* name of the old a.out file */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
105 char *new_end_of_text, /* ptr to new edata/etext; NOT USED YET */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
106 int dummy1, int dummy2) /* not used by emacs */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 int old, new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 int old_size, new_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 struct header hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 struct som_exec_auxhdr auxhdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 long i;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
113
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 /* For the greatest flexibility, should create a temporary file in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 the same directory as the new file. When everything is complete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 rename the temp file to the new name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 This way, a program could update its own a.out file even while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 it is still executing. If problems occur, everything is still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 intact. NOT implemented. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
120
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 /* Open the input and output a.out files */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 old = open (old_name, O_RDONLY);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 if (old < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 { perror(old_name); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 if (new < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 { perror(new_name); exit(1); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
128
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 /* Read the old headers */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 read_header(old, &hdr, &auxhdr);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
131
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 #ifdef HPUX_USE_SHLIBS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 Save_Shared_Data(); /* Save break value (added: dg@hplb.hpl.hp.com) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 /* Decide how large the new and old data areas are */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 old_size = auxhdr.exec_dsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 /* I suspect these two statements are separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 to avoid a compiler bug in hpux version 8. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 i = (long) sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 new_size = i - auxhdr.exec_dmem;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
141
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 /* Copy the old file to the new, up to the data space */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 lseek(old, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 copy_file(old, new, auxhdr.exec_dfile);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
145
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 /* Skip the old data segment and write a new one */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 lseek(old, old_size, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 save_data_space(new, &hdr, &auxhdr, new_size);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
149
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 /* Copy the rest of the file */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 copy_rest(old, new);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
152
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 /* Update file pointers since we probably changed size of data area */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 update_file_ptrs(new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
155
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 /* Save the modified header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 write_header(new, &hdr, &auxhdr);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
158
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /* Close the binary file */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 close (old);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 close (new);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 /* Save current data space in the file, update header. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
167 void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
168 save_data_space (int file, struct header *hdr,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
169 struct som_exec_auxhdr *auxhdr, int size)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 /* Write the entire data space out to the file */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 if (write(file, (void *)auxhdr->exec_dmem, size) != size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 { perror("Can't save new data space"); exit(1); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
174
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 /* Update the header to reflect the new data size */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 auxhdr->exec_dsize = size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 auxhdr->exec_bsize = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 /* Update the values of file pointers when something is inserted. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
182 void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
183 update_file_ptrs(int file, struct header *hdr,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
184 struct som_exec_auxhdr *auxhdr,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
185 unsigned int location, int offset)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 struct subspace_dictionary_record subspace;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 int i;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
189
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 /* Increase the overall size of the module */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 hdr->som_length += offset;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
192
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 /* Update the various file pointers in the header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 #define update(ptr) if (ptr > location) ptr = ptr + offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 update(hdr->aux_header_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 update(hdr->space_strings_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 update(hdr->init_array_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 update(hdr->compiler_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 update(hdr->symbol_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 update(hdr->fixup_request_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 update(hdr->symbol_strings_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 update(hdr->unloadable_sp_location);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 update(auxhdr->exec_tfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 update(auxhdr->exec_dfile);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
205
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 /* Do for each subspace dictionary entry */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 lseek(file, hdr->subspace_location, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 for (i = 0; i < hdr->subspace_total; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 if (read(file, &subspace, sizeof(subspace)) != sizeof(subspace))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 { perror("Can't read subspace record"); exit(1); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
212
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 /* If subspace has a file location, update it */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
214 if (subspace.initialization_length > 0
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 && subspace.file_loc_init_value > location)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 subspace.file_loc_init_value += offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 lseek(file, -sizeof(subspace), 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 if (write(file, &subspace, sizeof(subspace)) != sizeof(subspace))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 { perror("Can't update subspace record"); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
222 }
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
223
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 /* Do for each initialization pointer record */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 /* (I don't think it applies to executable files, only relocatables) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 #undef update
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 /* Read in the header records from an a.out file. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
231 void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
232 read_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
234
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 /* Read the header in */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 lseek(file, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 if (read(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 { perror("Couldn't read header from a.out file"); exit(1); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
239
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 && hdr->a_magic != DEMAND_MAGIC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
243 fprintf(stderr, "a.out file doesn't have legal magic number\n");
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
244 exit(1);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
246
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 lseek(file, hdr->aux_header_location, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 if (read(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 perror("Couldn't read auxiliary header from a.out file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 exit(1);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
252 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 /* Write out the header records into an a.out file. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
256 void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
257 write_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 /* Update the checksum */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 hdr->checksum = calculate_checksum(hdr);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
261
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 /* Write the header back into the a.out file */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 lseek(file, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 if (write(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 { perror("Couldn't write header to a.out file"); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 lseek(file, hdr->aux_header_location, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 if (write(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 { perror("Couldn't write auxiliary header to a.out file"); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 /* Calculate the checksum of a SOM header record. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
272 int
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
273 calculate_checksum(struct header *hdr)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 int checksum, i, *ptr;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
276
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 checksum = 0; ptr = (int *) hdr;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
278
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 for (i=0; i<sizeof(*hdr)/sizeof(int)-1; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 checksum ^= ptr[i];
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
281
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 return(checksum);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 /* Copy size bytes from the old file to the new one. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
286 void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
287 copy_file (int old, int new, int size)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 int len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 int buffer[8192]; /* word aligned will be faster */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
291
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 for (; size > 0; size -= len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 len = min(size, sizeof(buffer));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 if (read(old, buffer, len) != len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 { perror("Read failure on a.out file"); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 if (write(new, buffer, len) != len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 { perror("Write failure in a.out file"); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 /* Copy the rest of the file, up to EOF. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
303 void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
304 copy_rest (int old, int new)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 int buffer[4096];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 int len;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
308
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 /* Copy bytes until end of file or error */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 while ( (len = read(old, buffer, sizeof(buffer))) > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 if (write(new, buffer, len) != len) break;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
312
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 if (len != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 { perror("Unable to copy the rest of the file"); exit(1); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 #ifdef DEBUG
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
318 display_header(struct header *hdr, struct som_exec_auxhdr *auxhdr)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 /* Display the header information (debug) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 printf("\n\nFILE HEADER\n");
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
322 printf("magic number %d \n", hdr->a_magic);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 printf("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 printf("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 printf("entry %x \n", auxhdr->exec_entry);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 printf("Bss segment size %u\n", auxhdr->exec_bsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 printf("\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 printf("data file loc %d size %d\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 auxhdr->exec_dfile, auxhdr->exec_dsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 printf("som_length %d\n", hdr->som_length);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 printf("unloadable sploc %d size %d\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 #endif /* DEBUG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 #ifdef HPUX_USE_SHLIBS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 /* Added machinery for shared libs... see comments at the beginning of this file. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 void *Brk_On_Dump = 0; /* Brk value to restore... stored as a global */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 void Save_Shared_Data () {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 Brk_On_Dump = sbrk( 0 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
344
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 void Restore_Shared_Data () {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 brk ( Brk_On_Dump );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 int run_time_remap (int d) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 Restore_Shared_Data();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 /* run_time_remap is the magic called by startup code in the dumped executable
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 0
diff changeset
354 * if RUN_TIME_REMAP is set.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 #endif /* HPUX_USE_SHLIBS */