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 "gcc50"
43 #ifndef BINUTILSVER_DEFAULT
44 #define BINUTILSVER_DEFAULT "binutils227"
47 #define LINKER_BFD "ld.bfd"
48 #define LINKER_GOLD "ld.gold"
49 #define LINKER_DEFAULT LINKER_GOLD
50 #define LINKER_ALT LINKER_BFD
52 #ifndef OBJFORMAT_PATH_DEFAULT
53 #define OBJFORMAT_PATH_DEFAULT ""
56 /* Macro for array size */
58 #define NELEM(ary) (sizeof(ary) / sizeof((ary)[0]))
61 enum cmd_type
{ OBJFORMAT
, COMPILER
, BINUTILS
, LINKER
};
68 static struct command commands
[] = {
77 {"addr2line", BINUTILS
},
80 {"c++filt", BINUTILS
},
81 {"elfedit", BINUTILS
},
84 {"ld.gold", BINUTILS
},
86 {"objcopy", BINUTILS
},
87 {"objdump", BINUTILS
},
89 {"readelf", BINUTILS
},
91 {"strings", BINUTILS
},
93 {"incremental-dump", BINUTILS
},
94 {"objformat", OBJFORMAT
},
99 main(int argc
, char **argv
)
101 char ld_def
[] = LINKER_DEFAULT
;
102 char ld_alt
[] = LINKER_ALT
;
103 struct command
*cmds
;
106 char *cmd
, *newcmd
= NULL
;
107 char *ldcmd
= ld_def
;
108 const char *objformat_path
;
112 const char *env_value
= NULL
;
113 const char *base_path
= NULL
;
114 int use_objformat
= 0;
116 if (getobjformat(objformat
, sizeof objformat
, &argc
, argv
) == -1)
117 errx(1, "Invalid object format");
120 * Get the last path element of the program name being executed
122 cmd
= strrchr(argv
[0], '/');
128 for (cmds
= commands
; cmds
< &commands
[NELEM(commands
) - 1]; ++cmds
) {
129 if (strcmp(cmd
, cmds
->cmd
) == 0)
134 switch (cmds
->type
) {
136 ccver
= getenv("CCVER");
137 if ((ccver
== NULL
) || ccver
[0] == 0)
138 ccver
= CCVER_DEFAULT
;
139 base_path
= "/usr/libexec";
144 buver
= getenv("BINUTILSVER");
146 buver
= BINUTILSVER_DEFAULT
;
147 base_path
= "/usr/libexec";
152 buver
= getenv("BINUTILSVER");
154 buver
= BINUTILSVER_DEFAULT
;
155 ldver
= getenv("LDVER");
156 if ((ldver
!= NULL
) && (strcmp(ldver
, ld_alt
) == 0))
158 base_path
= "/usr/libexec";
166 errx(1, "unknown command type");
172 * The objformat command itself doesn't need another exec
174 if (cmds
->type
== OBJFORMAT
) {
176 fprintf(stderr
, "Usage: objformat\n");
180 printf("%s\n", objformat
);
185 * make buildworld glue and CCVER overrides.
188 * CCVER=clang check temporary; only for base clang import work
190 if (cmds
&& cmds
->type
== COMPILER
&&
191 strcmp (env_value
, "clang") == 0) {
194 objformat_path
= getenv("OBJFORMAT_PATH");
195 if (objformat_path
== NULL
)
196 objformat_path
= OBJFORMAT_PATH_DEFAULT
;
200 path
= strdup(objformat_path
);
202 if (setenv("OBJFORMAT", objformat
, 1) == -1)
203 err(1, "setenv: cannot set OBJFORMAT=%s", objformat
);
206 * objformat_path could be sequence of colon-separated paths.
208 while ((chunk
= strsep(&path
, ":")) != NULL
) {
209 if (newcmd
!= NULL
) {
214 asprintf(&newcmd
, "%s%s/%s/%s/%s",
215 chunk
, base_path
, env_value
, objformat
, cmd
);
217 asprintf(&newcmd
, "%s%s/%s/%s",
218 chunk
, base_path
, env_value
, cmd
);
221 err(1, "cannot allocate memory");
228 * Fallback: if we're searching for a compiler, but didn't
229 * find any, try again using the custom compiler driver.
231 if (cmds
&& cmds
->type
== COMPILER
&&
232 strcmp(env_value
, "custom") != 0) {
233 env_value
= "custom";
238 err(1, "in path [%s]%s/%s/%s/%s",
239 objformat_path
, base_path
, env_value
, objformat
, cmd
);
241 err(1, "in path [%s]%s/%s/%s",
242 objformat_path
, base_path
, env_value
, cmd
);