2 * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $
27 * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.10 2004/01/31 09:14:24 dillon Exp $
31 #include <objformat.h>
38 * System default compiler is still gcc2 for the IA32 architecture,
39 * unless overriden. For other architectures we have to use gcc3.
43 #define CCVER_DEFAULT "gcc2"
45 #define CCVER_DEFAULT "gcc3"
48 #ifndef BINUTILSVER_DEFAULT
49 #define BINUTILSVER_DEFAULT "binutils212"
57 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
64 static struct command commands[] = {
67 {"c++filt", COMPILER},
74 {"addr2line", BINUTILS2},
81 {"objcopy", BINUTILS2},
82 {"objdump", BINUTILS2},
83 {"ranlib", BINUTILS2},
85 {"strings", BINUTILS2},
87 {"objformat", OBJFORMAT}
91 main(int argc, char **argv)
96 char *cmd, *newcmd = NULL;
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 elemenet of the program name being executed
110 cmd = strrchr(argv[0], '/');
116 for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) {
117 if (strcmp(cmd, cmds->cmd) == 0)
121 if ((ccver = getenv("CCVER")) == NULL)
122 ccver = CCVER_DEFAULT;
123 if ((buver = getenv("BINUTILSVER")) == NULL) {
124 buver = BINUTILSVER_DEFAULT;
126 * XXX auto-select buver based on ccver if buver is
127 * not otherwise specified
132 switch (cmds->type) {
134 base_path = "/usr/bin";
143 base_path = "/usr/libexec";
148 err(1, "unknown command type");
154 * The objformat command itself doesn't need another exec
156 if (cmds->type == OBJFORMAT) {
158 fprintf(stderr, "Usage: objformat\n");
162 printf("%s\n", objformat);
167 * make buildworld glue and CCVER overrides.
169 objformat_path = getenv("OBJFORMAT_PATH");
170 if (objformat_path == NULL)
173 path = strdup(objformat_path);
175 setenv("OBJFORMAT", objformat, 1);
178 * objformat_path could be sequence of colon-separated paths.
180 while ((chunk = strsep(&path, ":")) != NULL) {
181 if (newcmd != NULL) {
186 asprintf(&newcmd, "%s%s/%s/%s/%s",
187 chunk, base_path, env_value, objformat, cmd);
189 asprintf(&newcmd, "%s%s/%s/%s",
190 chunk, base_path, env_value, cmd);
193 err(1, "cannot allocate memory");
199 err(1, "in path [%s]%s/%s/%s/%s",
200 objformat_path, base_path, env_value, objformat, cmd);
202 err(1, "in path [%s]%s/%s/%s",
203 objformat_path, base_path, env_value, cmd);