2 * Copyright (c) 1994 University of Maryland
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of U.M. not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. U.M. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
15 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Author: James da Silva, Systems Design and Analysis Group
23 * Computer Science Department
24 * University of Maryland at College Park
27 * SPDX-License-Identifier: BSD-2-Clause
29 * Copyright 2020 Alex Richardson <arichardson@FreeBSD.org>
31 * This software was developed by SRI International and the University of
32 * Cambridge Computer Laboratory (Department of Computer Science and
33 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
34 * DARPA SSITH research programme.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * crunched_main.c - main program for crunched binaries, it branches to a
60 * particular subprogram based on the value of argv[0]. Also included
61 * is a little program invoked when the crunched binary is called via
62 * its EXECNAME. This one prints out the list of compiled-in binaries,
63 * or calls one of them based on argv[1]. This allows the testing of
64 * the crunched binary without creating all the links.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
72 #include <sys/sysctl.h>
79 typedef int crunched_stub_t(int, char **, char **);
86 extern const char *__progname;
87 extern struct stub entry_points[];
89 static void crunched_usage(void);
91 crunched_stub_t crunched_main;
94 find_entry_point(const char *basename)
96 struct stub *ep = NULL;
98 for (ep = entry_points; ep->name != NULL; ep++)
99 if (!strcmp(basename, ep->name))
106 get_basename(const char *exe_path)
108 const char *slash = strrchr(exe_path, '/');
109 return (slash ? slash + 1 : exe_path);
113 main(int argc, char **argv, char **envp)
115 struct stub *ep = NULL;
116 const char *basename = NULL;
117 char buf[MAXPATHLEN];
120 * Look at __progname first (this will be set if the crunched binary is
124 basename = get_basename(__progname);
125 ep = find_entry_point(basename);
129 * Otherwise try to find entry point based on argv[0] (this works for
130 * both symlinks as well as hardlinks). However, it does not work when
131 * su invokes a crunched shell because it sets argv[0] to _su when
132 * invoking the shell. In that case we look at AT_EXECPATH as a
136 basename = get_basename(argv[0]);
137 ep = find_entry_point(basename);
141 * If we didn't find the entry point based on __progname or argv[0],
142 * try AT_EXECPATH to get the actual binary that was executed.
145 int error = elf_aux_info(AT_EXECPATH, &buf, sizeof(buf));
148 const char *exe_name = get_basename(buf);
150 * Keep using argv[0] if AT_EXECPATH is the crunched
151 * binary so that symlinks to the crunched binary report
152 * "not compiled in" instead of invoking
155 if (strcmp(exe_name, EXECNAME) != 0) {
157 ep = find_entry_point(basename);
160 warnc(error, "elf_aux_info(AT_EXECPATH) failed");
164 if (basename == NULL || *basename == '\0')
168 return ep->f(argc, argv, envp);
170 fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename);
176 crunched_main(int argc, char **argv, char **envp)
181 __progname = get_basename(argv[1]);
182 return main(--argc, ++argv, envp);
192 "usage: %s <prog> <args> ..., where <prog> is one of:\n", EXECNAME);
194 for (ep = entry_points; ep->name != NULL; ep++) {
195 len = strlen(ep->name) + 1;
196 if (columns + len < 80)
199 fprintf(stderr, "\n");
202 fprintf(stderr, " %s", ep->name);
204 fprintf(stderr, "\n");
208 /* end of crunched_main.c */