1 /*- 2 * Copyright (c) 2004, The DragonFly Project. All rights reserved. 3 * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $ 28 */ 29 30 #include <sys/param.h> 31 32 #include <err.h> 33 #include <objformat.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #ifndef CCVER_DEFAULT 40 #define CCVER_DEFAULT "gcc80" 41 #endif 42 43 #ifndef BINUTILSVER_DEFAULT 44 #define BINUTILSVER_DEFAULT "binutils227" 45 #endif 46 47 #define LINKER_BFD "ld.bfd" 48 #define LINKER_GOLD "ld.gold" 49 #define LINKER_DEFAULT LINKER_GOLD 50 #define LINKER_ALT LINKER_BFD 51 52 #ifndef OBJFORMAT_PATH_DEFAULT 53 #define OBJFORMAT_PATH_DEFAULT "" 54 #endif 55 56 /* Macro for array size */ 57 #ifndef NELEM 58 #define NELEM(ary) (sizeof(ary) / sizeof((ary)[0])) 59 #endif 60 61 enum cmd_type { OBJFORMAT, COMPILER, BINUTILS, LINKER }; 62 63 struct command { 64 const char *cmd; 65 enum cmd_type type; 66 }; 67 68 static struct command commands[] = { 69 {"CC", COMPILER}, 70 {"c++", COMPILER}, 71 {"cc", COMPILER}, 72 {"cpp", COMPILER}, 73 {"g++", COMPILER}, 74 {"gcc", COMPILER}, 75 {"gcov", COMPILER}, 76 {"ld", LINKER}, 77 {"addr2line", BINUTILS}, 78 {"ar", BINUTILS}, 79 {"as", BINUTILS}, 80 {"c++filt", BINUTILS}, 81 {"elfedit", BINUTILS}, 82 {"gprof", BINUTILS}, 83 {"ld.bfd", BINUTILS}, 84 {"ld.gold", BINUTILS}, 85 {"nm", BINUTILS}, 86 {"objcopy", BINUTILS}, 87 {"objdump", BINUTILS}, 88 {"ranlib", BINUTILS}, 89 {"readelf", BINUTILS}, 90 {"size", BINUTILS}, 91 {"strings", BINUTILS}, 92 {"strip", BINUTILS}, 93 {"incremental-dump", BINUTILS}, 94 {"objformat", OBJFORMAT}, 95 {"", -1} 96 }; 97 98 int 99 main(int argc, char **argv) 100 { 101 char ld_def[] = LINKER_DEFAULT; 102 char ld_alt[] = LINKER_ALT; 103 struct command *cmds; 104 char objformat[32]; 105 char *path, *chunk; 106 char *cmd, *newcmd = NULL; 107 char *ldcmd = ld_def; 108 const char *objformat_path; 109 const char *ccver; 110 const char *buver; 111 const char *ldver; 112 const char *env_value = NULL; 113 const char *base_path = NULL; 114 int use_objformat = 0; 115 116 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) 117 errx(1, "Invalid object format"); 118 119 /* 120 * Get the last path element of the program name being executed 121 */ 122 cmd = strrchr(argv[0], '/'); 123 if (cmd != NULL) 124 cmd++; 125 else 126 cmd = argv[0]; 127 128 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) { 129 if (strcmp(cmd, cmds->cmd) == 0) 130 break; 131 } 132 133 if (cmds) { 134 switch (cmds->type) { 135 case COMPILER: 136 ccver = getenv("CCVER"); 137 if ((ccver == NULL) || ccver[0] == 0) 138 ccver = CCVER_DEFAULT; 139 base_path = "/usr/libexec"; 140 use_objformat = 0; 141 env_value = ccver; 142 break; 143 case BINUTILS: 144 buver = getenv("BINUTILSVER"); 145 if (buver == NULL) 146 buver = BINUTILSVER_DEFAULT; 147 base_path = "/usr/libexec"; 148 use_objformat = 1; 149 env_value = buver; 150 break; 151 case LINKER: 152 buver = getenv("BINUTILSVER"); 153 if (buver == NULL) 154 buver = BINUTILSVER_DEFAULT; 155 ldver = getenv("LDVER"); 156 if ((ldver != NULL) && (strcmp(ldver, ld_alt) == 0)) 157 ldcmd = ld_alt; 158 base_path = "/usr/libexec"; 159 use_objformat = 1; 160 env_value = buver; 161 cmd = ldcmd; 162 break; 163 case OBJFORMAT: 164 break; 165 default: 166 errx(1, "unknown command type"); 167 break; 168 } 169 } 170 171 /* 172 * The objformat command itself doesn't need another exec 173 */ 174 if (cmds->type == OBJFORMAT) { 175 if (argc != 1) { 176 fprintf(stderr, "Usage: objformat\n"); 177 exit(1); 178 } 179 180 printf("%s\n", objformat); 181 exit(0); 182 } 183 184 /* 185 * make buildworld glue and CCVER overrides. 186 */ 187 /* 188 * CCVER=clang check temporary; only for base clang import work 189 */ 190 if (cmds && cmds->type == COMPILER && 191 strcmp (env_value, "clang") == 0) { 192 objformat_path = ""; 193 } else { 194 objformat_path = getenv("OBJFORMAT_PATH"); 195 if (objformat_path == NULL) 196 objformat_path = OBJFORMAT_PATH_DEFAULT; 197 } 198 199 again: 200 path = strdup(objformat_path); 201 202 if (setenv("OBJFORMAT", objformat, 1) == -1) 203 err(1, "setenv: cannot set OBJFORMAT=%s", objformat); 204 205 /* 206 * objformat_path could be sequence of colon-separated paths. 207 */ 208 while ((chunk = strsep(&path, ":")) != NULL) { 209 if (newcmd != NULL) { 210 free(newcmd); 211 newcmd = NULL; 212 } 213 if (use_objformat) { 214 asprintf(&newcmd, "%s%s/%s/%s/%s", 215 chunk, base_path, env_value, objformat, cmd); 216 } else { 217 asprintf(&newcmd, "%s%s/%s/%s", 218 chunk, base_path, env_value, cmd); 219 } 220 if (newcmd == NULL) 221 err(1, "cannot allocate memory"); 222 223 argv[0] = newcmd; 224 execv(newcmd, argv); 225 } 226 227 /* 228 * Fallback: if we're searching for a compiler, but didn't 229 * find any, try again using the custom compiler driver. 230 */ 231 if (cmds && cmds->type == COMPILER && 232 strcmp(env_value, "custom") != 0) { 233 env_value = "custom"; 234 goto again; 235 } 236 237 if (use_objformat) { 238 err(1, "in path [%s]%s/%s/%s/%s", 239 objformat_path, base_path, env_value, objformat, cmd); 240 } else { 241 err(1, "in path [%s]%s/%s/%s", 242 objformat_path, base_path, env_value, cmd); 243 } 244 } 245