Expand PMF_FN_* macros.
[netbsd-mini2440.git] / distrib / utils / sysinst / aout2elf.c
blobfc3c05c0e3906d071cb3eb6b7860b31c5e1d44b5
1 /* $NetBSD: aout2elf.c,v 1.14 2009/08/16 17:12:48 pgoyette Exp $
3 * Copyright 1997 Piermont Information Systems Inc.
4 * All rights reserved.
6 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the NetBSD Project by
19 * Piermont Information Systems Inc.
20 * 4. The name of Piermont Information Systems Inc. may not be used to endorse
21 * or promote products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34 * THE POSSIBILITY OF SUCH DAMAGE.
38 /* aout2elf.c -- routines for upgrading an a.out system to ELF */
40 #include <sys/param.h>
41 #include <sys/exec.h>
42 #include <sys/exec_aout.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <dirent.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <err.h>
53 #include "defs.h"
54 #include "md.h"
55 #include "msg_defs.h"
56 #include "menu_defs.h"
58 /* Local prototypes */
59 static int is_aout_shared_lib(const char *name);
60 static void handle_aout_x_libs(const char *srcdir, const char *tgtdir);
61 static int handle_aout_libs(const char *dir, int op, const void *arg);
62 static char *target_realpath(const char *, char *);
64 #define LIB_COUNT 0
65 #define LIB_MOVE 1
67 /* XXX NAH. This probably needs moving to arch/<foo>/md.h
69 * a.out X libraries to move. These have not changed since 1.3.x
71 const char *x_libs[] = {
72 "libICE.so.6.3",
73 "libPEX5.so.6.0",
74 "libSM.so.6.0",
75 "libX11.so.6.1",
76 "libXIE.so.6.0",
77 "libXaw.so.6.1",
78 "libXext.so.6.3",
79 "libXi.so.6.0",
80 "libXmu.so.6.0",
81 "libXp.so.6.2",
82 "libXt.so.6.0",
83 "libXtst.so.6.1",
84 "liboldX.so.6.0",
87 static int
88 is_aout_shared_lib(const char *name)
90 struct exec ex;
91 struct stat st;
92 int fd;
94 if (stat(name, &st) < 0)
95 return 0;
96 if ((st.st_mode & (S_IFREG|S_IFLNK)) == 0)
97 return 0;
99 fd = open(name, O_RDONLY);
100 if (fd < 0) {
101 return 0;
103 if (read(fd, &ex, sizeof ex) - sizeof ex != 0) {
104 close(fd);
105 return 0;
107 close(fd);
108 if (N_GETMAGIC(ex) != ZMAGIC ||
109 (N_GETFLAG(ex) & EX_DYNAMIC) == 0)
110 return 0;
112 return 1;
115 static void
116 handle_aout_x_libs(const char *srcdir, const char *tgtdir)
118 char src[MAXPATHLEN];
119 unsigned int i;
121 for (i = 0; i < (sizeof x_libs / sizeof (const char *)); i++) {
122 snprintf(src, MAXPATHLEN, "%s/%s", srcdir, x_libs[i]);
123 if (!is_aout_shared_lib(src))
124 continue;
125 run_program(0, "mv -f %s %s", src, tgtdir);
129 * Don't care if it fails; X may not have been installed.
134 * Function to count or move a.out shared libraries.
136 static int
137 handle_aout_libs(const char *dir, int op, const void *arg)
139 DIR *dd;
140 struct dirent *dp;
141 char *full_name;
142 const char *destdir = NULL; /* XXX -Wuninitialized [many] */
143 int n;
145 destdir = NULL; /* XXX gcc */
147 dd = opendir(dir);
148 if (dd == NULL)
149 return -1;
151 n = 0;
153 switch (op) {
154 case LIB_COUNT:
155 break;
156 case LIB_MOVE:
157 destdir = (const char *)arg;
158 break;
159 default:
160 return -1;
163 while ((dp = readdir(dd)) != NULL) {
165 * strlen("libX.so")
167 if (dp->d_namlen < 7)
168 continue;
169 if (strncmp(dp->d_name, "lib", 3) != 0)
170 continue;
172 if (asprintf(&full_name, "%s/%s", dir, dp->d_name) == -1) {
173 warn("Out of memory");
174 continue;
177 if (!is_aout_shared_lib(full_name))
178 goto endloop;
180 switch (op) {
181 case LIB_COUNT:
182 n++;
183 break;
184 case LIB_MOVE:
185 run_program(0, "mv -f %s %s/%s",
186 full_name, destdir, dp->d_name);
187 break;
190 endloop:
191 free(full_name);
194 closedir(dd);
196 return n;
199 static void
200 abort_libupdate(void)
202 msg_display(MSG_aoutfail);
203 process_menu(MENU_ok, NULL);
204 exit(1);
208 move_aout_libs(void)
210 int n, backedup = 0;
211 char prefix[MAXPATHLEN], src[MAXPATHLEN];
212 struct stat st;
214 n = handle_aout_libs(target_expand("/usr/lib"), LIB_COUNT, NULL);
215 if (n <= 0)
216 return n;
219 * See if /emul/aout already exists, taking symlinks into
220 * account. If so, no need to create it, just use it.
222 if (target_realpath("/emul/aout", prefix) != NULL && stat(prefix, &st) == 0)
223 goto domove;
226 * See if /emul exists. If not, create it.
228 if (target_realpath("/emul", prefix) == NULL || stat(prefix, &st) < 0) {
229 strlcpy(prefix, target_expand("/emul"), sizeof(prefix));
230 if (lstat(prefix, &st) == 0) {
231 run_program(0, "mv -f %s %s", prefix,
232 target_expand("/emul.old"));
233 backedup = 1;
235 scripting_fprintf(NULL, "mkdir %s\n", prefix);
236 mkdir(prefix, 0755);
240 * Can use strcpy, target_expand has made sure it fits into
241 * MAXPATHLEN. XXX all this copying is because concat_paths
242 * returns a pointer to a static buffer.
244 * If an old aout link exists (apparently pointing to nowhere),
245 * move it out of the way.
247 strlcpy(src, concat_paths(prefix, "aout"), sizeof(src));
248 if (lstat(src, &st) == 0) {
249 run_program(0, "mv -f %s %s", src,
250 concat_paths(prefix, "aout.old"));
251 backedup = 1;
255 * We have created /emul if needed. Since no previous /emul/aout
256 * existed, we'll use a symbolic link in /emul to /usr/aout, to
257 * avoid overflowing the root partition.
259 strlcpy(prefix, target_expand("/usr/aout"), sizeof(prefix));
260 if (run_program(0, "mkdir -p %s", prefix))
261 abort_libupdate();
262 if (run_program(0, "ln -s %s %s", "/usr/aout", src))
263 abort_libupdate();
265 domove:
267 * Rename etc and usr/lib if they already existed, so that we
268 * do not overwrite old files.
270 * Then, move /etc/ld.so.conf to /emul/aout/etc/ld.so.conf,
271 * and all a.out dynamic libraries from /usr/lib to
272 * /emul/aout/usr/lib. This is where the a.out code in ldconfig
273 * and ld.so respectively will find them.
275 strlcpy(src, concat_paths(prefix, "usr/lib"), sizeof(src));
276 run_program(0, "mv -f %s %s", src, concat_paths(prefix, "usr/lib.old"));
277 strlcpy(src, concat_paths(prefix, "etc/ld.so.conf"), sizeof(src));
278 run_program(0, "mv -f %s %s",
279 src, concat_paths(prefix, "etc/ld.so.conf.old"));
280 if (run_program(0, "mkdir -p %s ", concat_paths(prefix, "usr/lib")))
281 abort_libupdate();
282 if (run_program(0, "mkdir -p %s ", concat_paths(prefix, "etc")))
283 abort_libupdate();
285 strlcpy(src, target_expand("/etc/ld.so.conf"), sizeof(src));
286 if (run_program(0, "mv -f %s %s",
287 src, concat_paths(prefix, "etc/ld.so.conf")))
288 abort_libupdate();
290 strlcpy(src, target_expand("/usr/lib"), sizeof(src));
291 n = handle_aout_libs(src, LIB_MOVE, concat_paths(prefix, "usr/lib"));
293 if (run_program(0, "mkdir -p %s ",
294 concat_paths(prefix, "usr/X11R6/lib")))
295 abort_libupdate();
297 strlcpy(src, target_expand("/usr/X11R6/lib"), sizeof(src));
298 handle_aout_x_libs(src, concat_paths(prefix, "usr/X11R6/lib"));
300 if (backedup) {
301 msg_display(MSG_emulbackup);
302 process_menu(MENU_ok, NULL);
305 return n;
309 * XXXX had to include this to deal with symlinks in some places.
310 * When the target * disk is mounted under /targetroot, absolute symlinks
311 * on it don't work right.
312 * This function will resolve them using the mountpoint as prefix.
313 * Copied verbatim from libc, with added prefix handling.
315 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
317 * Find the real name of path, by removing all ".", ".." and symlink
318 * components. Returns (resolved) on success, or (NULL) on failure,
319 * in which case the path which caused trouble is left in (resolved).
321 static char *
322 target_realpath(const char *path, char *resolved)
324 struct stat sb;
325 int fd, n, rootd, serrno, nlnk = 0;
326 char *p, *q, wbuf[MAXPATHLEN];
327 char solidus[2], empty[1];
328 solidus[0] = '/';
329 solidus[1] = '\0';
330 empty[0] = '\0';
332 /* Save the starting point. */
333 if ((fd = open(".", O_RDONLY)) < 0) {
334 (void)strlcpy(resolved, ".", MAXPATHLEN);
335 return (NULL);
339 * Find the dirname and basename from the path to be resolved.
340 * Change directory to the dirname component.
341 * lstat the basename part.
342 * if it is a symlink, read in the value and loop.
343 * if it is a directory, then change to that directory.
344 * get the current directory name and append the basename.
346 if (target_prefix() != NULL && strcmp(target_prefix(), "") != 0)
347 snprintf(resolved, MAXPATHLEN, "%s/%s", target_prefix(), path);
348 else
349 if (strlcpy(resolved, path, MAXPATHLEN) >= MAXPATHLEN) {
350 errno = ENAMETOOLONG;
351 goto err1;
353 loop:
354 q = strrchr(resolved, '/');
355 if (q != NULL) {
356 p = q + 1;
357 if (q == resolved)
358 q = solidus;
359 else {
360 do {
361 --q;
362 } while (q > resolved && *q == '/');
363 q[1] = '\0';
364 q = resolved;
366 if (chdir(q) < 0)
367 goto err1;
368 } else
369 p = resolved;
371 /* Deal with the last component. */
372 if (lstat(p, &sb) == 0) {
373 if (S_ISLNK(sb.st_mode)) {
374 if (nlnk++ >= MAXSYMLINKS) {
375 errno = ELOOP;
376 goto err1;
378 n = readlink(p, wbuf, MAXPATHLEN - 1);
379 if (n < 0)
380 goto err1;
381 wbuf[n] = '\0';
382 if (wbuf[0] == '/')
383 snprintf(resolved, MAXPATHLEN, "%s%s",
384 target_prefix(), wbuf);
385 else
386 strlcpy(resolved, wbuf, MAXPATHLEN);
387 goto loop;
389 if (S_ISDIR(sb.st_mode)) {
390 if (chdir(p) < 0)
391 goto err1;
392 p = empty;
397 * Save the last component name and get the full pathname of
398 * the current directory.
400 if (strlcpy(wbuf, p, sizeof(wbuf)) >= sizeof(wbuf)) {
401 errno = ENAMETOOLONG;
402 goto err1;
406 * Call the internal internal version of getcwd which
407 * does a physical search rather than using the $PWD short-cut
409 if (getcwd(resolved, MAXPATHLEN) == 0)
410 goto err1;
413 * Join the two strings together, ensuring that the right thing
414 * happens if the last component is empty, or the dirname is root.
416 if (resolved[0] == '/' && resolved[1] == '\0')
417 rootd = 1;
418 else
419 rootd = 0;
421 if (*wbuf) {
422 if (strlen(resolved) + strlen(wbuf) + (rootd ? 0 : 1) + 1 >
423 MAXPATHLEN) {
424 errno = ENAMETOOLONG;
425 goto err1;
427 if (rootd == 0)
428 if (strlcat(resolved, "/", MAXPATHLEN) >= MAXPATHLEN) {
429 errno = ENAMETOOLONG;
430 goto err1;
432 if (strlcat(resolved, wbuf, MAXPATHLEN) >= MAXPATHLEN) {
433 errno = ENAMETOOLONG;
434 goto err1;
438 /* Go back to where we came from. */
439 if (fchdir(fd) < 0) {
440 serrno = errno;
441 goto err2;
444 /* It's okay if the close fails, what's an fd more or less? */
445 (void)close(fd);
446 return (resolved);
448 err1: serrno = errno;
449 (void)fchdir(fd);
450 err2: (void)close(fd);
451 errno = serrno;
452 return (NULL);