annotate src/unexfreebsd.c @ 438:84b14dcb0985 r21-2-27

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