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 * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.21 2007/05/08 20:59:37 swildner Exp $ 29 */ 30 31 #include <err.h> 32 #include <objformat.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #ifndef CCVER_DEFAULT 39 #define CCVER_DEFAULT "gcc34" 40 #endif 41 42 #ifndef BINUTILSVER_DEFAULT 43 #define BINUTILSVER_DEFAULT "binutils217" 44 #endif 45 #define BINUTILSVER_GCC34 "binutils217" 46 47 #define OBJFORMAT 0 48 #define COMPILER 1 49 #define BINUTILS1 2 50 #define BINUTILS2 3 51 52 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0])) 53 54 struct command { 55 const char *cmd; 56 int type; 57 }; 58 59 static struct command commands[] = { 60 {"CC", COMPILER}, 61 {"c++", COMPILER}, 62 {"c++filt", COMPILER}, 63 {"cc", COMPILER}, 64 {"cpp", COMPILER}, 65 {"f77", COMPILER}, 66 {"g77", COMPILER}, 67 {"g++", COMPILER}, 68 {"gcc", COMPILER}, 69 {"gcov", COMPILER}, 70 {"addr2line", BINUTILS2}, 71 {"ar", BINUTILS2}, 72 {"as", BINUTILS2}, 73 {"gasp", BINUTILS2}, 74 {"gdb", BINUTILS2}, 75 {"ld", BINUTILS2}, 76 {"nm", BINUTILS2}, 77 {"objcopy", BINUTILS2}, 78 {"objdump", BINUTILS2}, 79 {"ranlib", BINUTILS2}, 80 {"size", BINUTILS2}, 81 {"strings", BINUTILS2}, 82 {"strip", BINUTILS2}, 83 {"objformat", OBJFORMAT} 84 }; 85 86 int 87 main(int argc, char **argv) 88 { 89 struct command *cmds; 90 char objformat[32]; 91 char *path, *chunk; 92 char *cmd, *newcmd = NULL; 93 const char *objformat_path; 94 const char *ccver; 95 const char *buver; 96 const char *env_value = NULL; 97 const char *base_path = NULL; 98 int use_objformat = 0; 99 100 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) 101 errx(1, "Invalid object format"); 102 103 /* 104 * Get the last path elemenet of the program name being executed 105 */ 106 cmd = strrchr(argv[0], '/'); 107 if (cmd != NULL) 108 cmd++; 109 else 110 cmd = argv[0]; 111 112 for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) { 113 if (strcmp(cmd, cmds->cmd) == 0) 114 break; 115 } 116 117 if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0) 118 ccver = CCVER_DEFAULT; 119 if ((buver = getenv("BINUTILSVER")) == NULL) { 120 if (strcmp(ccver, "gcc34") == 0) 121 buver = BINUTILSVER_GCC34; 122 else 123 buver = BINUTILSVER_DEFAULT; /* fall through */ 124 } 125 126 if (cmds) { 127 switch (cmds->type) { 128 case COMPILER: 129 base_path = "/usr/libexec"; 130 use_objformat = 0; 131 env_value = ccver; 132 break; 133 case BINUTILS2: 134 use_objformat = 1; 135 /* fall through */ 136 case BINUTILS1: 137 env_value = buver; 138 base_path = "/usr/libexec"; 139 break; 140 case OBJFORMAT: 141 break; 142 default: 143 err(1, "unknown command type"); 144 break; 145 } 146 } 147 148 /* 149 * The objformat command itself doesn't need another exec 150 */ 151 if (cmds->type == OBJFORMAT) { 152 if (argc != 1) { 153 fprintf(stderr, "Usage: objformat\n"); 154 exit(1); 155 } 156 157 printf("%s\n", objformat); 158 exit(0); 159 } 160 161 /* 162 * make buildworld glue and CCVER overrides. 163 */ 164 objformat_path = getenv("OBJFORMAT_PATH"); 165 if (objformat_path == NULL) 166 objformat_path = ""; 167 168 path = strdup(objformat_path); 169 170 if (setenv("OBJFORMAT", objformat, 1) == -1) 171 err(1, "setenv: cannot set OBJFORMAT=%s", objformat); 172 173 /* 174 * objformat_path could be sequence of colon-separated paths. 175 */ 176 while ((chunk = strsep(&path, ":")) != NULL) { 177 if (newcmd != NULL) { 178 free(newcmd); 179 newcmd = NULL; 180 } 181 if (use_objformat) { 182 asprintf(&newcmd, "%s%s/%s/%s/%s", 183 chunk, base_path, env_value, objformat, cmd); 184 } else { 185 asprintf(&newcmd, "%s%s/%s/%s", 186 chunk, base_path, env_value, cmd); 187 } 188 if (newcmd == NULL) 189 err(1, "cannot allocate memory"); 190 191 argv[0] = newcmd; 192 execv(newcmd, argv); 193 } 194 if (use_objformat) { 195 err(1, "in path [%s]%s/%s/%s/%s", 196 objformat_path, base_path, env_value, objformat, cmd); 197 } else { 198 err(1, "in path [%s]%s/%s/%s", 199 objformat_path, base_path, env_value, cmd); 200 } 201 } 202 203