annotate src/unexhp9k800.c @ 1797:cc86c049e576

*** empty log message ***
author David J. MacKenzie <djm@gnu.org>
date Thu, 14 Jan 1993 20:38:21 +0000
parents 69e337909652
children 078782db2675
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 /* Unexec for HP 9000 Series 800 machines.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2 Bob Desinger <hpsemc!bd@hplabs.hp.com>
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4 Note that the GNU project considers support for HP operation a
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 peripheral activity which should not be allowed to divert effort
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6 from development of the GNU system. Changes in this code will be
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 installed when users send them in, but aside from that we don't
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 plan to think about it, or about whether other Emacs maintenance
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 might break it.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 Unexec creates a copy of the old a.out file, and replaces the old data
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 area with the current data area. When the new file is executed, the
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 process will see the same data structures and data values that the
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 original process had when unexec was called.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 Unlike other versions of unexec, this one copies symbol table and
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 debug information to the new a.out file. Thus, the new a.out file
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 may be debugged with symbolic debuggers.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 If you fix any bugs in this, I'd like to incorporate your fixes.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 CAVEATS:
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 This routine saves the current value of all static and external
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 variables. This means that any data structure that needs to be
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 initialized must be explicitly reset. Variables will not have their
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 expected default values.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 Unfortunately, the HP-UX signal handler has internal initialization
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 flags which are not explicitly reset. Thus, for signals to work in
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 conjunction with this routine, the following code must executed when
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 the new process starts up.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 void _sigreturn();
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 ...
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 sigsetreturn(_sigreturn);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 #include <stdio.h>
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 #include <fcntl.h>
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 #include <errno.h>
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 #include <a.out.h>
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46 #define NBPG 2048
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 #define roundup(x,n) ( ( (x)+(n-1) ) & ~(n-1) ) /* n is power of 2 */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 #define min(x,y) ( ((x)<(y))?(x):(y) )
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 /* Create a new a.out file, same as old but with current data space */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 unexec(new_name, old_name, new_end_of_text, dummy1, dummy2)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 char new_name[]; /* name of the new a.out file to be created */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 char old_name[]; /* name of the old a.out file */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 char *new_end_of_text; /* ptr to new edata/etext; NOT USED YET */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 int dummy1, dummy2; /* not used by emacs */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 int old, new;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 int old_size, new_size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 struct header hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 struct som_exec_auxhdr auxhdr;
484
3165b2697c78 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 118
diff changeset
63 long i;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 /* For the greatest flexibility, should create a temporary file in
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 the same directory as the new file. When everything is complete,
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 rename the temp file to the new name.
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 This way, a program could update its own a.out file even while
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 it is still executing. If problems occur, everything is still
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 intact. NOT implemented. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 /* Open the input and output a.out files */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 old = open(old_name, O_RDONLY);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 if (old < 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 { perror(old_name); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 new = open(new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 if (new < 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 { perror(new_name); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 /* Read the old headers */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 read_header(old, &hdr, &auxhdr);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 /* Decide how large the new and old data areas are */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 old_size = auxhdr.exec_dsize;
484
3165b2697c78 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 118
diff changeset
85 /* I suspect these two statements are separate
3165b2697c78 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 118
diff changeset
86 to avoid a compiler bug in hpux version 8. */
3165b2697c78 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 118
diff changeset
87 i = sbrk (0);
3165b2697c78 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 118
diff changeset
88 new_size = i - auxhdr.exec_dmem;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 /* Copy the old file to the new, up to the data space */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 lseek(old, 0, 0);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 copy_file(old, new, auxhdr.exec_dfile);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 /* Skip the old data segment and write a new one */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 lseek(old, old_size, 1);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 save_data_space(new, &hdr, &auxhdr, new_size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 /* Copy the rest of the file */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 copy_rest(old, new);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 /* Update file pointers since we probably changed size of data area */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 update_file_ptrs(new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 /* Save the modified header */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 write_header(new, &hdr, &auxhdr);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 /* Close the binary file */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 close(old);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 close(new);
1742
69e337909652 (unexec): Don't call exit, just return.
Richard M. Stallman <rms@gnu.org>
parents: 484
diff changeset
110 return 0;
118
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 /* Save current data space in the file, update header. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 save_data_space(file, hdr, auxhdr, size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 int file;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 struct header *hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 struct som_exec_auxhdr *auxhdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 int size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 /* Write the entire data space out to the file */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 if (write(file, auxhdr->exec_dmem, size) != size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 { perror("Can't save new data space"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 /* Update the header to reflect the new data size */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 auxhdr->exec_dsize = size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 auxhdr->exec_bsize = 0;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 /* Update the values of file pointers when something is inserted. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 update_file_ptrs(file, hdr, auxhdr, location, offset)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 int file;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 struct header *hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 struct som_exec_auxhdr *auxhdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 unsigned int location;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 int offset;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 struct subspace_dictionary_record subspace;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 int i;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 /* Increase the overall size of the module */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 hdr->som_length += offset;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 /* Update the various file pointers in the header */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 #define update(ptr) if (ptr > location) ptr = ptr + offset
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 update(hdr->aux_header_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 update(hdr->space_strings_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 update(hdr->init_array_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 update(hdr->compiler_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 update(hdr->symbol_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 update(hdr->fixup_request_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 update(hdr->symbol_strings_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154 update(hdr->unloadable_sp_location);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 update(auxhdr->exec_tfile);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 update(auxhdr->exec_dfile);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 /* Do for each subspace dictionary entry */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 lseek(file, hdr->subspace_location, 0);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 for (i = 0; i < hdr->subspace_total; i++)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 if (read(file, &subspace, sizeof(subspace)) != sizeof(subspace))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 { perror("Can't read subspace record"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 /* If subspace has a file location, update it */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 if (subspace.initialization_length > 0
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 && subspace.file_loc_init_value > location)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 subspace.file_loc_init_value += offset;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 lseek(file, -sizeof(subspace), 1);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 if (write(file, &subspace, sizeof(subspace)) != sizeof(subspace))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 { perror("Can't update subspace record"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 /* Do for each initialization pointer record */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 /* (I don't think it applies to executable files, only relocatables) */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 #undef update
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 /* Read in the header records from an a.out file. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 read_header(file, hdr, auxhdr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 int file;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 struct header *hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 struct som_exec_auxhdr *auxhdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 /* Read the header in */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190 lseek(file, 0, 0);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 if (read(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 { perror("Couldn't read header from a.out file"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 && hdr->a_magic != DEMAND_MAGIC)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 fprintf(stderr, "a.out file doesn't have legal magic number\n");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 exit(1);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 lseek(file, hdr->aux_header_location, 0);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 if (read(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 perror("Couldn't read auxiliary header from a.out file");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 exit(1);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 /* Write out the header records into an a.out file. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 write_header(file, hdr, auxhdr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 int file;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 struct header *hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 struct som_exec_auxhdr *auxhdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 /* Update the checksum */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 hdr->checksum = calculate_checksum(hdr);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 /* Write the header back into the a.out file */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 lseek(file, 0, 0);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 if (write(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 { perror("Couldn't write header to a.out file"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 lseek(file, hdr->aux_header_location, 0);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 if (write(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 { perror("Couldn't write auxiliary header to a.out file"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 /* Calculate the checksum of a SOM header record. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 calculate_checksum(hdr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 struct header *hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 int checksum, i, *ptr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 checksum = 0; ptr = (int *) hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 for (i=0; i<sizeof(*hdr)/sizeof(int)-1; i++)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 checksum ^= ptr[i];
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 return(checksum);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 /* Copy size bytes from the old file to the new one. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 copy_file(old, new, size)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 int new, old;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 int size;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 int len;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250 int buffer[8196]; /* word aligned will be faster */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 for (; size > 0; size -= len)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 len = min(size, sizeof(buffer));
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 if (read(old, buffer, len) != len)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 { perror("Read failure on a.out file"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 if (write(new, buffer, len) != len)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 { perror("Write failure in a.out file"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 /* Copy the rest of the file, up to EOF. */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 copy_rest(old, new)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 int new, old;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 int buffer[4096];
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 int len;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270 /* Copy bytes until end of file or error */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271 while ( (len = read(old, buffer, sizeof(buffer))) > 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 if (write(new, buffer, len) != len) break;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274 if (len != 0)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275 { perror("Unable to copy the rest of the file"); exit(1); }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 #ifdef DEBUG
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279 display_header(hdr, auxhdr)
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 struct header *hdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 struct som_exec_auxhdr *auxhdr;
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 {
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 /* Display the header information (debug) */
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 printf("\n\nFILE HEADER\n");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 printf("magic number %d \n", hdr->a_magic);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
286 printf("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
287 printf("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
288 printf("entry %x \n", auxhdr->exec_entry);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
289 printf("Bss segment size %u\n", auxhdr->exec_bsize);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
290 printf("\n");
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
291 printf("data file loc %d size %d\n",
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
292 auxhdr->exec_dfile, auxhdr->exec_dsize);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
293 printf("som_length %d\n", hdr->som_length);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
294 printf("unloadable sploc %d size %d\n",
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
295 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
296 }
49342840ba00 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
297 #endif /* DEBUG */