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 "gcc50" 41 #endif 42 43 #ifndef BINUTILSVER_DEFAULT 44 #define BINUTILSVER_DEFAULT "binutils225" 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 {"nm", BINUTILS}, 84 {"objcopy", BINUTILS}, 85 {"objdump", BINUTILS}, 86 {"ranlib", BINUTILS}, 87 {"readelf", BINUTILS}, 88 {"size", BINUTILS}, 89 {"strings", BINUTILS}, 90 {"strip", BINUTILS}, 91 {"incremental-dump", BINUTILS}, 92 {"objformat", OBJFORMAT}, 93 {"", -1} 94 }; 95 96 int 97 main(int argc, char **argv) 98 { 99 char ld_def[] = LINKER_DEFAULT; 100 char ld_alt[] = LINKER_ALT; 101 struct command *cmds; 102 char objformat[32]; 103 char *path, *chunk; 104 char *cmd, *newcmd = NULL; 105 char *ldcmd = ld_def; 106 const char *objformat_path; 107 const char *ccver; 108 const char *buver; 109 const char *ldver; 110 const char *env_value = NULL; 111 const char *base_path = NULL; 112 int use_objformat = 0; 113 114 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) 115 errx(1, "Invalid object format"); 116 117 /* 118 * Get the last path element of the program name being executed 119 */ 120 cmd = strrchr(argv[0], '/'); 121 if (cmd != NULL) 122 cmd++; 123 else 124 cmd = argv[0]; 125 126 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) { 127 if (strcmp(cmd, cmds->cmd) == 0) 128 break; 129 } 130 131 if (cmds) { 132 switch (cmds->type) { 133 case COMPILER: 134 ccver = getenv("CCVER"); 135 if ((ccver == NULL) || ccver[0] == 0) 136 ccver = CCVER_DEFAULT; 137 base_path = "/usr/libexec"; 138 use_objformat = 0; 139 env_value = ccver; 140 break; 141 case BINUTILS: 142 buver = getenv("BINUTILSVER"); 143 if (buver == NULL) 144 buver = BINUTILSVER_DEFAULT; 145 base_path = "/usr/libexec"; 146 use_objformat = 1; 147 env_value = buver; 148 break; 149 case LINKER: 150 buver = getenv("BINUTILSVER"); 151 if (buver == NULL) 152 buver = BINUTILSVER_DEFAULT; 153 ldver = getenv("LDVER"); 154 if ((ldver != NULL) && (strcmp(ldver, ld_alt) == 0)) 155 ldcmd = ld_alt; 156 base_path = "/usr/libexec"; 157 use_objformat = 1; 158 env_value = buver; 159 cmd = ldcmd; 160 break; 161 case OBJFORMAT: 162 break; 163 default: 164 errx(1, "unknown command type"); 165 break; 166 } 167 } 168 169 /* 170 * The objformat command itself doesn't need another exec 171 */ 172 if (cmds->type == OBJFORMAT) { 173 if (argc != 1) { 174 fprintf(stderr, "Usage: objformat\n"); 175 exit(1); 176 } 177 178 printf("%s\n", objformat); 179 exit(0); 180 } 181 182 /* 183 * make buildworld glue and CCVER overrides. 184 */ 185 objformat_path = getenv("OBJFORMAT_PATH"); 186 if (objformat_path == NULL) 187 objformat_path = OBJFORMAT_PATH_DEFAULT; 188 189 again: 190 path = strdup(objformat_path); 191 192 if (setenv("OBJFORMAT", objformat, 1) == -1) 193 err(1, "setenv: cannot set OBJFORMAT=%s", objformat); 194 195 /* 196 * objformat_path could be sequence of colon-separated paths. 197 */ 198 while ((chunk = strsep(&path, ":")) != NULL) { 199 if (newcmd != NULL) { 200 free(newcmd); 201 newcmd = NULL; 202 } 203 if (use_objformat) { 204 asprintf(&newcmd, "%s%s/%s/%s/%s", 205 chunk, base_path, env_value, objformat, cmd); 206 } else { 207 asprintf(&newcmd, "%s%s/%s/%s", 208 chunk, base_path, env_value, cmd); 209 } 210 if (newcmd == NULL) 211 err(1, "cannot allocate memory"); 212 213 argv[0] = newcmd; 214 execv(newcmd, argv); 215 } 216 217 /* 218 * Fallback: if we're searching for a compiler, but didn't 219 * find any, try again using the custom compiler driver. 220 */ 221 if (cmds && cmds->type == COMPILER && 222 strcmp(env_value, "custom") != 0) { 223 env_value = "custom"; 224 goto again; 225 } 226 227 if (use_objformat) { 228 err(1, "in path [%s]%s/%s/%s/%s", 229 objformat_path, base_path, env_value, objformat, cmd); 230 } else { 231 err(1, "in path [%s]%s/%s/%s", 232 objformat_path, base_path, env_value, cmd); 233 } 234 } 235