annotate src/unexfreebsd.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 ca9a9ec9c1c1
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 /* Code to do an unexec for FreeBSD-1.1 for a temacs linked -Bdynamic.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Derived from unexnetbsd.c, which was derived from unexsunos4.c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 /* Synched up with: Not in FSF? */
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 Created 29-Oct-92 by Harlan Sexton
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 Tweaked 06-Aug-93 by Dean Michaels to work with sun3.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 Converted 01-Dec-93 by Paul Mackerras to work with NetBSD shared libraries.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 Tweaked 26-Feb-94 by Shawn Carey for use with FreeBSD-1.1 shared libraries.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 /********************** Included .h Files **************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #include <stdarg.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #include <sys/param.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #include <sys/mman.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 #include <sys/file.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #include <stdio.h>
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 274
diff changeset
43 #include <errno.h>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #include <a.out.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 #include <ctype.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #include <stab.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #include <sys/dir.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #include <link.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 /********************** Macros *************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #define SYS_ERR strerror(errno)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 #define MASK_UP(x,p_of_two) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 #define MASK_DOWN(x,p_of_two) (((unsigned long) (x)) & (~((p_of_two) - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 /********************** Typedefs and Structs ***********************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 struct translation_struct
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 long txtaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 long txtoff;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 long dataddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 long datoff;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 long bssaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 long endaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 /********************** Function Prototypes/Declarations ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 274
diff changeset
74 static void unexec_error (const char *m, int use_errno, ...);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 static int unexec_open (char *filename, int flag, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 static caddr_t unexec_mmap (int fd, size_t len, int prot, int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 static long unexec_seek (int fd, long position);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 static void unexec_read (int fd, long position, char *buf, int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 static void unexec_write (int fd, long position, char *buf, int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 static void unexec_pad (int fd, int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 static void unexec_fstat (int fd, struct stat *statptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 static void unexec_fchmod (int fd, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 static long unexec_addr_to_offset (long addr, struct translation_struct *ts);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 static void copy_relocation_site (struct relocation_info *ri,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 caddr_t from_base_addr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 caddr_t to_base_addr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 struct translation_struct *ts);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 static void reset_symtab (struct nlist *start, struct nlist *end,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 char *strtab, long edata_value, long end_value,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 int shlib_image);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 static void reset_ldso_symtab (struct nzlist *start, struct nzlist *end,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 char *strtab, long edata_value, long end_value,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 int shlib_image);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 int run_time_remap (char *dummy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 /********************** Variables **********************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 /* for reporting error messages from system calls */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 extern int _DYNAMIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 extern char **environ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 static unsigned long sbrk_of_0_at_unexec;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 /*******************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 static void
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 274
diff changeset
107 unexec_error (const char *fmt, int use_errno, ...)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 274
diff changeset
109 const char *err_msg = SYS_ERR;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 va_list args;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 fprintf (stderr, "unexec - ");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 va_start (args, use_errno);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 vfprintf (stderr, fmt, args);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 va_end (args);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 if (use_errno)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 fprintf (stderr, ": %s", err_msg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 fprintf (stderr, "\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 unexec_open (char *filename, int flag, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 fd = open (filename, flag, mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 unexec_error ("Failure opening file %s", 1, filename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 return fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 static caddr_t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 unexec_mmap (int fd, size_t len, int prot, int flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 caddr_t return_val;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 unexec_seek (fd, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 return_val = mmap (0, len, prot, flags, fd, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 if (return_val == (caddr_t) -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 unexec_error ("Failure mmap'ing file", 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 return return_val;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 static long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 unexec_seek (int fd, long position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 long seek_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 if (fd <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 unexec_error ("No file open in which to seek", 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 if (position < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 seek_value = (long) lseek (fd, 0, L_INCR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 seek_value = (long) lseek (fd, position, L_SET);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 if (seek_value < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 unexec_error ("Failed to do a seek to 0x%x in %s", 1,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 position, "unexec() output file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 return seek_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 unexec_read (int fd, long position, char *buf, int bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 int n_read;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 int remains = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 position = unexec_seek (fd, position);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 if (bytes < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 unexec_error ("Attempted read of %d bytes", 0, bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 errno = 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 while (remains > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 n_read = read (fd, buf, remains);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 if (n_read <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 unexec_error ("Read failed for 0x%x bytes at offset 0x%x in %s",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 1, bytes, position, "unexec() output file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 buf += n_read;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 remains -= n_read;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 unexec_write (int fd, long position, char *buf, int bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 int n_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 int remains = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 position = unexec_seek (fd, position);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 if (bytes < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 unexec_error ("Attempted write of %d bytes in %s",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 0, bytes, "unexec() output file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 while (remains > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 n_written = write (fd, buf, remains);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 if (n_written <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 unexec_error ("Write failed for 0x%x bytes at offset 0x%x in %s",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 1, bytes, position, "unexec() output file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 buf += n_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 remains -= n_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 unexec_pad (int fd, int bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 if (bytes > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 char buf[1024];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 int remaining = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 memset (buf, 0, sizeof (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 while (remaining > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 unexec_write (fd, -1, buf, this_write);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 remaining -= this_write;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 unexec_fstat (int fd, struct stat *statptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 if (-1 == fstat (fd, statptr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 unexec_error ("fstat() failed for descriptor %d", 1, fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 unexec_fchmod (int fd, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 if (-1 == fchmod (fd, mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 unexec_error ("fchmod() failed for descriptor %d", 1, fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 static long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 unexec_addr_to_offset (long addr, struct translation_struct *ts)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 if ((addr < ts->txtaddr) || (addr >= ts->bssaddr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 else if (addr >= ts->dataddr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 return ((long) ((addr - ts->dataddr) + ts->datoff));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 return ((long) ((addr - ts->txtaddr) + ts->txtoff));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 * "LD.SO" DATA AND SYMBOL TABLE OPERATIONS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 copy_relocation_site (struct relocation_info *ri,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 caddr_t from_base_addr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 caddr_t to_base_addr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 struct translation_struct *ts)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 long offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 caddr_t from, to;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 /* We can get relocation sites in the bss region, for objects whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 contents are copied from a shared library. We don't need or want
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 to restore these at present. */
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
292 #ifndef sparc
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 if (ri->r_copy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 return;
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
295 #else
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
296 /* Struct relocation_info_sparc doesn't have member r_copy.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
297 Instead, we use the address to check if this is run-time-copied. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
298 if (ri->r_address >= ts->bssaddr && ri->r_address < ts->endaddr)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
299 return;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
300 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 offset = unexec_addr_to_offset (ri->r_address, ts);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 if (offset == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 unexec_error ("bad relocation address 0x%x (0x%x)", 0, ri->r_address,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 ((long *)ri)[1]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 from = from_base_addr + offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 to = to_base_addr + offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 /* This stuff should be in a md_ file somewhere... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 #ifndef sparc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 switch (ri->r_length)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 *((char *) to) = *((char *) from);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 case 1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 *((short *) to) = *((short *) from);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 case 2:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 *((long *) to) = *((long *) from);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 unexec_error ("unknown reloc length %d seen during unexec()",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 0, ri->r_length);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 #else /* sparc */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 switch (ri->r_type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 case RELOC_8:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 case RELOC_DISP8:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 *((char *) to) = *((char *) from);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 case RELOC_16:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 case RELOC_DISP16:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 *((short *) to) = *((short *) from);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 case RELOC_LO10:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 case RELOC_13:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 case RELOC_22:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 case RELOC_HI22:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 case RELOC_WDISP22:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 case RELOC_WDISP30:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 case RELOC_32:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 case RELOC_DISP32:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 case RELOC_GLOB_DAT:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 *((long *) to) = *((long *) from);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 case RELOC_JMP_SLOT:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 long *target = (long *) to;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 long *source = (long *) from;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 *target = *source;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 target++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 source++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 *target = *source;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 target++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 source++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 *target = *source;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 }
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
361 break;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 unexec_error ("unknown reloc type %d seen during unexec()",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 0, ri->r_type);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 #endif /* sparc */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 reset_symtab (struct nlist *start, struct nlist *end, char *strtab,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 long edata_value, long end_value, int shlib_image)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 struct nlist *tmp = start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 int found_edata = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 int found_end = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 while (tmp < end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 int type = tmp->n_type;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 if ((type == (N_UNDF | N_EXT)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (tmp->n_value != 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 unexec_error ("unexec'ing image has COMMON symbols in it -- we quit!",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 if (!(type & N_STAB))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 if (!found_edata &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (type == (N_EXT | N_DATA)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 tmp->n_un.n_strx &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 !strcmp ("_edata", strtab + tmp->n_un.n_strx))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 tmp->n_value = edata_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 found_edata = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 if ((type & N_TYPE) == N_BSS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 if (!found_end &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (type == (N_EXT | N_BSS)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 tmp->n_un.n_strx &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 !strcmp ("_end", strtab + tmp->n_un.n_strx))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 tmp->n_value = end_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 found_end = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 else if (type & N_EXT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 tmp->n_type = N_DATA | N_EXT;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 tmp->n_type = N_DATA;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 /* the way things are being handled here, having sbrk() in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 image is fatal for an image linked with shared lib's (although
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 the code could be modified to support it), but this should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 never happen anyway */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 if (shlib_image &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 (type == (N_EXT | N_TEXT)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 tmp->n_un.n_strx &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 !strcmp ("_sbrk", strtab + tmp->n_un.n_strx))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 unexec_error ("unexec'd shlib image has sbrk() in it -- we quit!",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 tmp++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 reset_ldso_symtab (struct nzlist *start, struct nzlist *end, char *strtab,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 long edata_value, long end_value, int shlib_image)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 struct nzlist *tmp = start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 int found_edata = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 int found_end = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 while (tmp < end) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 int type = tmp->nz_type;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 * the following code breaks under FreeBSD-1.1-BETA, but everything
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 * seems to work perfectly if it's commented out. This did not break
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 * anything until the changes to ld.so were made.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 if ((type == (N_UNDF | N_EXT)) && (tmp->nz_value != 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 unexec_error("unexec'ing image has COMMON symbols in rel -- we quit!",0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 if (!(type & N_STAB)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 if (!found_edata &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (type == (N_EXT | N_DATA)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 !strcmp ("_edata", strtab + tmp->nz_strx)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 tmp->nz_value = edata_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 found_edata = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 if ((type & N_TYPE) == N_BSS) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 if (!found_end &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 (type == (N_EXT | N_BSS)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 !strcmp ("_end", strtab + tmp->nz_strx)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 tmp->nz_value = end_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 found_end = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 } else if (type & N_EXT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 tmp->nz_type = N_DATA | N_EXT;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 tmp->nz_type = N_DATA;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 /* the way things are being handled here, having sbrk() in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 image is fatal for an image linked with shared lib's (although
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 the code could be modified to support it), but this should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 never happen anyway */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 if (shlib_image &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (type == (N_EXT | N_TEXT)) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 !strcmp ("_sbrk", strtab + tmp->nz_strx))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 unexec_error("unexec'd shlib image has sbrk() ref -- we quit!", 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 tmp++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 extern int getpagesize (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 * EXPORTED FUNCTIONS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 /* this has to be a global variable to prevent the optimizers from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 * assuming that it can not be 0.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 static void *dynamic_addr = (void *) &_DYNAMIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 unexec (char *new_name, char *old_name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 unsigned int emacs_edata, unsigned int dummy1, unsigned int dummy2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 /* ld.so data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 struct _dynamic *ld = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 struct section_dispatch_table *ld2 = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 /* old and new state */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 int old_fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 int new_fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 caddr_t old_base_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 caddr_t new_base_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 struct exec old_hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 struct exec new_hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 struct stat old_buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 struct stat new_buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 /* some process specific "constants" */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 unsigned long n_pagsiz, new_edata;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 long page_size = getpagesize ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 caddr_t plt_end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 caddr_t current_break = (caddr_t) sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 if (!page_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 unexec_error ("unexec() failed because we can't get the size of a page!",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 /* see if this is a -Bdynamic image -- if so, find ld.so structures */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 if (dynamic_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 ld = (struct _dynamic *) dynamic_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 ld2 = ld->d_un.d_sdt;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 if (ld->d_version < LD_VERSION_BSD)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 unexec_error ("%s linked with obsolete version of ld -- we quit!",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 0, old_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 /* open the old and new files, figuring out how big the old one is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 so that we can map it in */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 old_fd = unexec_open (old_name, O_RDONLY, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 new_fd = unexec_open (new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 /* setup the header and the statbuf for old_fd */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 unexec_fstat (old_fd, &old_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 /* set up some important constants */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 n_pagsiz = __LDPGSZ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 if (dynamic_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 plt_end = (caddr_t) MASK_UP (ld2->sdt_plt + ld2->sdt_plt_sz, sizeof (double));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 plt_end = (caddr_t) N_DATADDR (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 /* never write protect the variable "environ", defined in /lib/crt0.o, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 set in process.c and callproc.c */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 mprotect_bottom_addr = ((unsigned long) &environ) + sizeof (char **);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 /* never protect ABOVE the end of data emacs_edata specified */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 mprotect_top_addr = MIN (emacs_edata, N_DATADDR (old_hdr) + old_hdr.a_data);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 /* Set up the image of the old file */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 old_base_addr = unexec_mmap (old_fd, old_buf.st_size, PROT_READ,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 MAP_FILE | MAP_PRIVATE);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 close (old_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 /* set up the new exec */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 new_hdr = old_hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 new_edata = (unsigned long) MASK_UP (current_break, n_pagsiz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 new_hdr.a_data = new_edata - ((unsigned long) N_DATADDR (old_hdr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 new_hdr.a_bss = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 /* set up this variable, in case we want to reset "the break"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 when restarting */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 sbrk_of_0_at_unexec = ((unsigned long) MASK_UP (current_break, n_pagsiz));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 /* Write out the first approximation to the new file. The sizes of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 each section will be correct, but there will be a number of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 corrections that will need to be made. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 long old_datoff = N_DATOFF (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 long old_dataddr = N_DATADDR (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 long new_treloff = N_RELOFF (new_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 long old_treloff = N_RELOFF (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 long ld_so_size = ((unsigned long) plt_end) - old_dataddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 long real_data_size = current_break - plt_end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 long pad_size =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 /* First, write the text segment with new header -- copy everything until
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 the start of the data segment from the old file, and then go back and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 write the new header. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 unexec_write (new_fd, 0, old_base_addr, old_datoff + ld_so_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 /* Copy the rest of the data segment from the running image. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 unexec_write (new_fd, old_datoff + ld_so_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 plt_end, real_data_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 /* pad out the data segment */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 unexec_pad (new_fd, pad_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 /* Finally, copy the symbol table information from the old file. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 unexec_write (new_fd, new_treloff,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 old_base_addr + old_treloff,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 old_buf.st_size - old_treloff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 /* Next, map in the output file so that we can jump around fixing it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 up. We retain the old file so that we can refer to it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 unexec_fstat (new_fd, &new_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 new_base_addr = unexec_mmap (new_fd,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 MASK_UP (new_buf.st_size, page_size),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 PROT_READ | PROT_WRITE,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 MAP_FILE | MAP_SHARED);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 /* We need to do 2 things. First, make sure that _edata and _end (and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 hence, curbrk) are set to the correct values. At the same time, for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 neatness and to help with debugging, mark all the types of all ld.so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 and nm BSS symbols in the new file to be DATA, and make sure that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 there are no COMMON symbols in the output file, as any references to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 these can lose really big. Second, reset all of the ld.so "relocation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 sites" in the new file to have the values that appear in the old file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 -- the failure to do this was the biggest loser in the old version of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 this code. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 /* STEP 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 /* Reset the regular symbol table first. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 reset_symtab ((struct nlist *) (new_base_addr + N_SYMOFF(new_hdr)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (struct nlist *) (new_base_addr + N_SYMOFF(new_hdr) +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 new_hdr.a_syms),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 (char *) (new_base_addr + N_STROFF(new_hdr)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 new_edata, new_edata,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 !!dynamic_addr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 /* Now reset the ld.so symbol table. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 if (dynamic_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 struct translation_struct ts;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 struct relocation_info *tmp, *end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 caddr_t syms, strings;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 /* set up the structure that we use to translate addresses in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 old file into file offsets */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 ts.txtaddr = N_TXTADDR (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 ts.txtoff = N_TXTOFF (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 ts.dataddr = N_DATADDR (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 ts.datoff = N_DATOFF (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 ts.bssaddr = N_DATADDR (old_hdr) + old_hdr.a_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 ts.endaddr = ts.bssaddr + old_hdr.a_bss;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 syms = new_base_addr + unexec_addr_to_offset(ld2->sdt_nzlist, &ts);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 strings = new_base_addr + unexec_addr_to_offset(ld2->sdt_strings, &ts);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 reset_ldso_symtab ((struct nzlist *) syms, (struct nzlist *) strings,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 (char *) strings,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 new_edata, new_edata,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 !!dynamic_addr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 /* STEP 2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 tmp = (struct relocation_info *)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (old_base_addr + unexec_addr_to_offset(ld2->sdt_rel, &ts));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 end = (struct relocation_info *)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 (old_base_addr + unexec_addr_to_offset(ld2->sdt_hash, &ts));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 while (tmp < end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 copy_relocation_site (tmp, old_base_addr, new_base_addr, &ts);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 tmp++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 /* get rid of the mmap-ed file space and make the output file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 executable -- then quit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 munmap (new_base_addr, MASK_UP (new_buf.st_size, page_size));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 munmap (old_base_addr, MASK_UP (old_buf.st_size, page_size));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 unexec_fchmod (new_fd, 0755);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 close (new_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 run_time_remap (char *dummy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 unsigned long current_sbrk = (unsigned long) sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 255
diff changeset
683 #if __FreeBSD_version < 300000 /* 2.x can work with this code */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 if (sbrk_of_0_at_unexec < current_sbrk)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 if (sbrk_of_0_at_unexec != 0)
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 203
diff changeset
687 fprintf (stderr, "Absurd new brk addr = %lx (current = %lx)\n",
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 sbrk_of_0_at_unexec, current_sbrk);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 else
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 255
diff changeset
691 #endif
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 255
diff changeset
692 if (sbrk_of_0_at_unexec > current_sbrk)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 if (brk ((caddr_t) sbrk_of_0_at_unexec))
255
084402c475ba Import from CVS: tag r20-5b26
cvs
parents: 203
diff changeset
696 fprintf (stderr, "failed to change brk addr to %lx: %s\n",
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 sbrk_of_0_at_unexec, SYS_ERR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 /* with proper COW, i don't think we really need to do this... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 long page_size = getpagesize();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 unsigned long base_addr = MASK_UP (mprotect_bottom_addr, page_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 unsigned long top_addr = MASK_DOWN (mprotect_top_addr, page_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 long len = top_addr - base_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 if (len > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 if (mprotect ((caddr_t) base_addr, len, PROT_READ | PROT_EXEC))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 fprintf (stderr, "failed to change protection on data pages: %s\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 SYS_ERR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 }