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 "binutils222"
47 #define LINKER_DEFAULT "ld.bfd"
48 #define LINKER_GOLD "ld.gold"
50 #ifndef OBJFORMAT_PATH_DEFAULT
51 #define OBJFORMAT_PATH_DEFAULT ""
54 /* Macro for array size */
56 #define NELEM(ary) (sizeof(ary) / sizeof((ary)[0]))
59 enum cmd_type { OBJFORMAT, COMPILER, BINUTILS, LINKER };
66 static struct command commands[] = {
75 {"addr2line", BINUTILS},
78 {"c++filt", BINUTILS},
79 {"elfedit", BINUTILS},
82 {"objcopy", BINUTILS},
83 {"objdump", BINUTILS},
85 {"readelf", BINUTILS},
87 {"strings", BINUTILS},
89 {"incremental-dump", BINUTILS},
90 {"objformat", OBJFORMAT},
95 main(int argc, char **argv)
97 char ld_orig[] = LINKER_DEFAULT;
98 char ld_gold[] = LINKER_GOLD;
102 char *cmd, *newcmd = NULL;
103 char *ldcmd = ld_orig;
104 const char *objformat_path;
108 const char *env_value = NULL;
109 const char *base_path = NULL;
110 int use_objformat = 0;
112 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
113 errx(1, "Invalid object format");
116 * Get the last path element of the program name being executed
118 cmd = strrchr(argv[0], '/');
124 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
125 if (strcmp(cmd, cmds->cmd) == 0)
130 switch (cmds->type) {
132 ccver = getenv("CCVER");
133 if ((ccver == NULL) || ccver[0] == 0)
134 ccver = CCVER_DEFAULT;
135 base_path = "/usr/libexec";
140 buver = getenv("BINUTILSVER");
142 buver = BINUTILSVER_DEFAULT;
143 base_path = "/usr/libexec";
148 buver = getenv("BINUTILSVER");
150 buver = BINUTILSVER_DEFAULT;
151 ldver = getenv("LDVER");
152 if ((ldver != NULL) && (strcmp(ldver, ld_gold) == 0))
154 base_path = "/usr/libexec";
162 errx(1, "unknown command type");
168 * The objformat command itself doesn't need another exec
170 if (cmds->type == OBJFORMAT) {
172 fprintf(stderr, "Usage: objformat\n");
176 printf("%s\n", objformat);
181 * make buildworld glue and CCVER overrides.
183 objformat_path = getenv("OBJFORMAT_PATH");
184 if (objformat_path == NULL)
185 objformat_path = OBJFORMAT_PATH_DEFAULT;
188 path = strdup(objformat_path);
190 if (setenv("OBJFORMAT", objformat, 1) == -1)
191 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
194 * objformat_path could be sequence of colon-separated paths.
196 while ((chunk = strsep(&path, ":")) != NULL) {
197 if (newcmd != NULL) {
202 asprintf(&newcmd, "%s%s/%s/%s/%s",
203 chunk, base_path, env_value, objformat, cmd);
205 asprintf(&newcmd, "%s%s/%s/%s",
206 chunk, base_path, env_value, cmd);
209 err(1, "cannot allocate memory");
216 * Fallback: if we're searching for a compiler, but didn't
217 * find any, try again using the custom compiler driver.
219 if (cmds && cmds->type == COMPILER &&
220 strcmp(env_value, "custom") != 0) {
221 env_value = "custom";
226 err(1, "in path [%s]%s/%s/%s/%s",
227 objformat_path, base_path, env_value, objformat, cmd);
229 err(1, "in path [%s]%s/%s/%s",
230 objformat_path, base_path, env_value, cmd);