annotate src/unexmips.c @ 902:c81d26e85bef

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Fri, 31 Jul 1992 02:30:04 +0000
parents f4edd5fc98f6
children 861c2997afee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
486
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 /* Unexec for MIPS (including IRIS4D).
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2 Note that the GNU project considers support for MIPS operation
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3 a peripheral activity which should not be allowed to divert effort
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4 from development of the GNU system. Changes in this code will be
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 installed when users send them in, but aside from that
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6 we don't plan to think about it, or about whether other Emacs
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 maintenance might break it.
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 Copyright (C) 1988 Free Software Foundation, Inc.
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 This file is part of GNU Emacs.
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 GNU Emacs is free software; you can redistribute it and/or modify
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 it under the terms of the GNU General Public License as published by
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 the Free Software Foundation; either version 1, or (at your option)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 any later version.
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 GNU Emacs is distributed in the hope that it will be useful,
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 GNU General Public License for more details.
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 You should have received a copy of the GNU General Public License
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 along with GNU Emacs; see the file COPYING. If not, write to
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 #include "config.h"
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 #include <sys/types.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 #include <sys/file.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 #include <sys/stat.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 #include <stdio.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 #include <varargs.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34 #include <filehdr.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 #include <aouthdr.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 #include <scnhdr.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 #include <sym.h>
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 #ifdef IRIS_4D
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 #include "getpagesize.h"
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 #endif
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 static void fatal_unexec ();
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46 errno = EEOF; \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 if (read (_fd, _buffer, _size) != _size) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 fatal_unexec (_error_message, _error_arg);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 if (write (_fd, _buffer, _size) != _size) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 fatal_unexec (_error_message, _error_arg);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 #define SEEK(_fd, _position, _error_message, _error_arg) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 errno = EEOF; \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 if (lseek (_fd, _position, L_SET) != _position) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 fatal_unexec (_error_message, _error_arg);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 extern int errno;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 extern int sys_nerr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 extern char *sys_errlist[];
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 #define EEOF -1
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 static struct scnhdr *text_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 static struct scnhdr *init_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 static struct scnhdr *finit_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 static struct scnhdr *rdata_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 static struct scnhdr *data_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 static struct scnhdr *lit8_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 static struct scnhdr *lit4_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 static struct scnhdr *sdata_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 static struct scnhdr *sbss_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 static struct scnhdr *bss_section;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 struct headers {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 struct filehdr fhdr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 struct aouthdr aout;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 struct scnhdr section[10];
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 };
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 /* Define name of label for entry point for the dumped executable. */
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 #ifndef DEFAULT_ENTRY_ADDRESS
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 #define DEFAULT_ENTRY_ADDRESS __start
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 #endif
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 unexec (new_name, a_name, data_start, bss_start, entry_address)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 char *new_name, *a_name;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 unsigned data_start, bss_start, entry_address;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 int new, old;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 int pagesize, brk;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 int newsyms, symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 int nread;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 struct headers hdr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 int i;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 int vaddr, scnptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 #define BUFSIZE 8192
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 char buffer[BUFSIZE];
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 old = open (a_name, O_RDONLY, 0);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 if (old < 0) fatal_unexec ("opening %s", a_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 new = creat (new_name, 0666);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 if (new < 0) fatal_unexec ("creating %s", new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 hdr = *((struct headers *)TEXT_START);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 #ifdef MIPS2
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 if (hdr.fhdr.f_magic != MIPSELMAGIC
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 && hdr.fhdr.f_magic != MIPSEBMAGIC
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 && hdr.fhdr.f_magic != (MIPSELMAGIC | 1)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 && hdr.fhdr.f_magic != (MIPSEBMAGIC | 1))
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 fprintf(stderr,
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n",
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 hdr.fhdr.f_magic,
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 MIPSELMAGIC, MIPSEBMAGIC,
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 MIPSELMAGIC | 1, MIPSEBMAGIC | 1);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 exit(1);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 #else /* not MIPS2 */
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 if (hdr.fhdr.f_magic != MIPSELMAGIC
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 && hdr.fhdr.f_magic != MIPSEBMAGIC)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n",
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 exit (1);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 #endif /* not MIPS2 */
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 if (hdr.fhdr.f_opthdr != sizeof (hdr.aout))
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n",
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 hdr.fhdr.f_opthdr, sizeof (hdr.aout));
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 exit (1);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 if (hdr.aout.magic != ZMAGIC)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n",
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 hdr.aout.magic, ZMAGIC);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 exit (1);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 #define CHECK_SCNHDR(ptr, name, flags) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 if (strcmp (hdr.section[i].s_name, name) == 0) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 { \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 if (hdr.section[i].s_flags != flags) \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 hdr.section[i].s_flags, flags, name); \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 ptr = hdr.section + i; \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 i += 1; \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 } \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 else \
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 ptr = NULL;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 i = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 CHECK_SCNHDR (init_section, _INIT, STYP_INIT);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 CHECK_SCNHDR (data_section, _DATA, STYP_DATA);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 #ifdef _LIT8
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 #endif /* _LIT8 */
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 CHECK_SCNHDR (sbss_section, _SBSS, STYP_SBSS);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 CHECK_SCNHDR (bss_section, _BSS, STYP_BSS);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 if (i != hdr.fhdr.f_nscns)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 fprintf (stderr, "unexec: %d sections found instead of %d.\n",
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 i, hdr.fhdr.f_nscns);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 pagesize = getpagesize ();
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 brk = (sbrk (0) + pagesize - 1) & (-pagesize);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 hdr.aout.dsize = brk - DATA_START;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 hdr.aout.bsize = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 if (entry_address == 0)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 extern DEFAULT_ENTRY_ADDRESS ();
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 else
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 hdr.aout.entry = entry_address;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 rdata_section->s_size = data_start - DATA_START;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 data_section->s_vaddr = data_start;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 data_section->s_paddr = data_start;
568
f4edd5fc98f6 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 486
diff changeset
187 data_section->s_size = brk - data_start;
486
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 vaddr = data_section->s_vaddr + data_section->s_size;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190 scnptr = data_section->s_scnptr + data_section->s_size;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 if (lit8_section != NULL)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 lit8_section->s_vaddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 lit8_section->s_paddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 lit8_section->s_size = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 lit8_section->s_scnptr = scnptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 if (lit4_section != NULL)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 lit4_section->s_vaddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 lit4_section->s_paddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 lit4_section->s_size = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 lit4_section->s_scnptr = scnptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 if (sdata_section != NULL)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 sdata_section->s_vaddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 sdata_section->s_paddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 sdata_section->s_size = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 sdata_section->s_scnptr = scnptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 if (sbss_section != NULL)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 sbss_section->s_vaddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 sbss_section->s_paddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 sbss_section->s_size = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 sbss_section->s_scnptr = scnptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 if (bss_section != NULL)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 bss_section->s_vaddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 bss_section->s_paddr = vaddr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 bss_section->s_size = 0;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 bss_section->s_scnptr = scnptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 WRITE (new, TEXT_START, hdr.aout.tsize,
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 "writing text section to %s", new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 WRITE (new, DATA_START, hdr.aout.dsize,
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 "writing text section to %s", new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 errno = EEOF;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 nread = read (old, buffer, BUFSIZE);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 if (nread < sizeof (HDRR)) fatal_unexec ("reading symbols from %s", a_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 #define symhdr ((pHDRR)buffer)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 newsyms = hdr.aout.tsize + hdr.aout.dsize;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 symrel = newsyms - hdr.fhdr.f_symptr;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 hdr.fhdr.f_symptr = newsyms;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 symhdr->cbLineOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241 symhdr->cbDnOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 symhdr->cbPdOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 symhdr->cbSymOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244 symhdr->cbOptOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 symhdr->cbAuxOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 symhdr->cbSsOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 symhdr->cbSsExtOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 symhdr->cbFdOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 symhdr->cbRfdOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250 symhdr->cbExtOffset += symrel;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 #undef symhdr
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 do
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 if (write (new, buffer, nread) != nread)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 fatal_unexec ("writing symbols to %s", new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 nread = read (old, buffer, BUFSIZE);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 if (nread < 0) fatal_unexec ("reading symbols from %s", a_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 #undef BUFSIZE
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 } while (nread != 0);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 SEEK (new, 0, "seeking to start of header in %s", new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 WRITE (new, &hdr, sizeof (hdr),
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 "writing header of %s", new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 close (old);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 close (new);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 mark_x (new_name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270 /*
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271 * mark_x
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 *
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273 * After succesfully building the new a.out, mark it executable
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274 */
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276 static
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277 mark_x (name)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 char *name;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 struct stat sbuf;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 int um = umask (777);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 umask (um);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 if (stat (name, &sbuf) < 0)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 fatal_unexec ("getting protection on %s", name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 sbuf.st_mode |= 0111 & ~um;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
286 if (chmod (name, sbuf.st_mode) < 0)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
287 fatal_unexec ("setting protection on %s", name);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
288 }
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
289
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
290 static void
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
291 fatal_unexec (s, va_alist)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
292 va_dcl
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
293 {
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
294 va_list ap;
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
295 if (errno == EEOF)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
296 fputs ("unexec: unexpected end of file, ", stderr);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
297 else if (errno < sys_nerr)
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
298 fprintf (stderr, "unexec: %s, ", sys_errlist[errno]);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
299 else
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
300 fprintf (stderr, "unexec: error code %d, ", errno);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
301 va_start (ap);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
302 _doprnt (s, ap, stderr);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
303 fputs (".\n", stderr);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
304 exit (1);
80cdb2387043 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
305 }