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 $
31 #include <objformat.h>
38 #define CCVER_DEFAULT "gcc44"
41 #ifndef BINUTILSVER_DEFAULT
42 #define BINUTILSVER_DEFAULT "binutils221"
45 #ifndef OBJFORMAT_PATH_DEFAULT
46 #define OBJFORMAT_PATH_DEFAULT ""
54 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
61 static struct command commands[] = {
64 {"c++filt", COMPILER},
70 {"addr2line", BINUTILS2},
74 {"c++filt", BINUTILS2},
76 {"elfedit", BINUTILS2},
79 {"objcopy", BINUTILS2},
80 {"objdump", BINUTILS2},
81 {"ranlib", BINUTILS2},
82 {"readelf", BINUTILS2},
84 {"strings", BINUTILS2},
86 {"objformat", OBJFORMAT},
91 main(int argc, char **argv)
96 char *cmd, *newcmd = NULL;
97 const char *objformat_path;
100 const char *env_value = NULL;
101 const char *base_path = NULL;
102 int use_objformat = 0;
104 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
105 errx(1, "Invalid object format");
108 * Get the last path element of the program name being executed
110 cmd = strrchr(argv[0], '/');
116 for (cmds = commands; cmds < &commands[arysize(commands) - 1]; ++cmds) {
117 if (strcmp(cmd, cmds->cmd) == 0)
121 if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
122 ccver = CCVER_DEFAULT;
123 if ((buver = getenv("BINUTILSVER")) == NULL) {
124 buver = BINUTILSVER_DEFAULT;
128 switch (cmds->type) {
130 base_path = "/usr/libexec";
139 base_path = "/usr/libexec";
144 errx(1, "unknown command type");
150 * The objformat command itself doesn't need another exec
152 if (cmds->type == OBJFORMAT) {
154 fprintf(stderr, "Usage: objformat\n");
158 printf("%s\n", objformat);
163 * make buildworld glue and CCVER overrides.
165 objformat_path = getenv("OBJFORMAT_PATH");
166 if (objformat_path == NULL)
167 objformat_path = OBJFORMAT_PATH_DEFAULT;
170 path = strdup(objformat_path);
172 if (setenv("OBJFORMAT", objformat, 1) == -1)
173 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
176 * objformat_path could be sequence of colon-separated paths.
178 while ((chunk = strsep(&path, ":")) != NULL) {
179 if (newcmd != NULL) {
184 asprintf(&newcmd, "%s%s/%s/%s/%s",
185 chunk, base_path, env_value, objformat, cmd);
187 asprintf(&newcmd, "%s%s/%s/%s",
188 chunk, base_path, env_value, cmd);
191 err(1, "cannot allocate memory");
198 * Fallback: if we're searching for a compiler, but didn't
199 * find any, try again using the custom compiler driver.
201 if (cmds && cmds->type == COMPILER &&
202 strcmp(env_value, "custom") != 0) {
203 env_value = "custom";
208 err(1, "in path [%s]%s/%s/%s/%s",
209 objformat_path, base_path, env_value, objformat, cmd);
211 err(1, "in path [%s]%s/%s/%s",
212 objformat_path, base_path, env_value, cmd);