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 $
28 * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.17 2005/04/20 21:39:00 joerg Exp $
32 #include <objformat.h>
39 #define CCVER_DEFAULT "gcc34"
42 #ifndef BINUTILSVER_DEFAULT
43 #define BINUTILSVER_DEFAULT "binutils215"
45 #define BINUTILSVER_GCC2 "binutils212"
46 #define BINUTILSVER_GCC34 "binutils215"
53 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
60 static struct command commands[] = {
63 {"c++filt", COMPILER},
70 {"addr2line", BINUTILS2},
77 {"objcopy", BINUTILS2},
78 {"objdump", BINUTILS2},
79 {"ranlib", BINUTILS2},
81 {"strings", BINUTILS2},
83 {"objformat", OBJFORMAT}
87 main(int argc, char **argv)
92 char *cmd, *newcmd = NULL;
96 const char *env_value = NULL;
97 const char *base_path = NULL;
98 int use_objformat = 0;
100 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
101 errx(1, "Invalid object format");
104 * Get the last path elemenet of the program name being executed
106 cmd = strrchr(argv[0], '/');
112 for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) {
113 if (strcmp(cmd, cmds->cmd) == 0)
117 if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
118 ccver = CCVER_DEFAULT;
119 if ((buver = getenv("BINUTILSVER")) == NULL) {
120 if (strcmp(ccver, "gcc2") == 0)
121 buver = BINUTILSVER_GCC2;
122 else if (strcmp(ccver, "gcc34") == 0)
123 buver = BINUTILSVER_GCC34;
125 buver = BINUTILSVER_DEFAULT; /* fall through */
129 switch (cmds->type) {
131 base_path = "/usr/libexec";
140 base_path = "/usr/libexec";
145 err(1, "unknown command type");
151 * The objformat command itself doesn't need another exec
153 if (cmds->type == OBJFORMAT) {
155 fprintf(stderr, "Usage: objformat\n");
159 printf("%s\n", objformat);
164 * make buildworld glue and CCVER overrides.
166 objformat_path = getenv("OBJFORMAT_PATH");
167 if (objformat_path == NULL)
170 path = strdup(objformat_path);
172 setenv("OBJFORMAT", objformat, 1);
175 * objformat_path could be sequence of colon-separated paths.
177 while ((chunk = strsep(&path, ":")) != NULL) {
178 if (newcmd != NULL) {
183 asprintf(&newcmd, "%s%s/%s/%s/%s",
184 chunk, base_path, env_value, objformat, cmd);
186 asprintf(&newcmd, "%s%s/%s/%s",
187 chunk, base_path, env_value, cmd);
190 err(1, "cannot allocate memory");
196 err(1, "in path [%s]%s/%s/%s/%s",
197 objformat_path, base_path, env_value, objformat, cmd);
199 err(1, "in path [%s]%s/%s/%s",
200 objformat_path, base_path, env_value, cmd);