2 * Copyright (c) 2004, The DragonFly Project. All rights reserved.
3 * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $
30 #include <sys/param.h>
33 #include <objformat.h>
40 #define CCVER_DEFAULT "gcc44"
43 #ifndef BINUTILSVER_DEFAULT
44 #define BINUTILSVER_DEFAULT "binutils221"
47 #ifndef OBJFORMAT_PATH_DEFAULT
48 #define OBJFORMAT_PATH_DEFAULT ""
51 enum cmd_type { OBJFORMAT, COMPILER, BINUTILS };
58 static struct command commands[] = {
61 {"c++filt", COMPILER},
67 {"addr2line", BINUTILS},
71 {"c++filt", BINUTILS},
73 {"elfedit", BINUTILS},
76 {"objcopy", BINUTILS},
77 {"objdump", BINUTILS},
79 {"readelf", BINUTILS},
81 {"strings", BINUTILS},
83 {"objformat", OBJFORMAT},
88 main(int argc, char **argv)
93 char *cmd, *newcmd = NULL;
94 const char *objformat_path;
97 const char *env_value = NULL;
98 const char *base_path = NULL;
99 int use_objformat = 0;
101 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
102 errx(1, "Invalid object format");
105 * Get the last path element of the program name being executed
107 cmd = strrchr(argv[0], '/');
113 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
114 if (strcmp(cmd, cmds->cmd) == 0)
118 if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
119 ccver = CCVER_DEFAULT;
120 if ((buver = getenv("BINUTILSVER")) == NULL) {
121 buver = BINUTILSVER_DEFAULT;
125 switch (cmds->type) {
127 base_path = "/usr/libexec";
132 base_path = "/usr/libexec";
139 errx(1, "unknown command type");
145 * The objformat command itself doesn't need another exec
147 if (cmds->type == OBJFORMAT) {
149 fprintf(stderr, "Usage: objformat\n");
153 printf("%s\n", objformat);
158 * make buildworld glue and CCVER overrides.
160 objformat_path = getenv("OBJFORMAT_PATH");
161 if (objformat_path == NULL)
162 objformat_path = OBJFORMAT_PATH_DEFAULT;
165 path = strdup(objformat_path);
167 if (setenv("OBJFORMAT", objformat, 1) == -1)
168 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
171 * objformat_path could be sequence of colon-separated paths.
173 while ((chunk = strsep(&path, ":")) != NULL) {
174 if (newcmd != NULL) {
179 asprintf(&newcmd, "%s%s/%s/%s/%s",
180 chunk, base_path, env_value, objformat, cmd);
182 asprintf(&newcmd, "%s%s/%s/%s",
183 chunk, base_path, env_value, cmd);
186 err(1, "cannot allocate memory");
193 * Fallback: if we're searching for a compiler, but didn't
194 * find any, try again using the custom compiler driver.
196 if (cmds && cmds->type == COMPILER &&
197 strcmp(env_value, "custom") != 0) {
198 env_value = "custom";
203 err(1, "in path [%s]%s/%s/%s/%s",
204 objformat_path, base_path, env_value, objformat, cmd);
206 err(1, "in path [%s]%s/%s/%s",
207 objformat_path, base_path, env_value, cmd);