xref: /dflybsd-src/usr.bin/objformat/objformat.c (revision fb5b37479af248e338c5ba8928faafa0b563eb9f)
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 "gcc44"
41 #endif
42 
43 #ifndef BINUTILSVER_DEFAULT
44 #define	BINUTILSVER_DEFAULT "binutils221"
45 #endif
46 
47 #ifndef LDVER_DEFAULT
48 #define	LDVER_DEFAULT "bfd"
49 #endif
50 
51 #ifndef OBJFORMAT_PATH_DEFAULT
52 #define OBJFORMAT_PATH_DEFAULT ""
53 #endif
54 
55 enum cmd_type { OBJFORMAT, COMPILER, BINUTILS };
56 
57 struct command {
58 	const char *cmd;
59 	enum cmd_type type;
60 };
61 
62 static struct command commands[] = {
63 	{"CC",		COMPILER},
64 	{"c++",		COMPILER},
65 	{"cc",		COMPILER},
66 	{"cpp",		COMPILER},
67 	{"g++",		COMPILER},
68 	{"gcc",		COMPILER},
69 	{"gcov",	COMPILER},
70 	{"addr2line",	BINUTILS},
71 	{"ar",		BINUTILS},
72 	{"as",		BINUTILS},
73 	{"c++filt",	BINUTILS},
74 	{"elfedit",	BINUTILS},
75 	{"ld",		BINUTILS},
76 	{"nm",		BINUTILS},
77 	{"objcopy",	BINUTILS},
78 	{"objdump",	BINUTILS},
79 	{"ranlib",	BINUTILS},
80 	{"readelf",	BINUTILS},
81 	{"size",	BINUTILS},
82 	{"strings",	BINUTILS},
83 	{"strip",	BINUTILS},
84 	{"objformat",	OBJFORMAT},
85 	{"",		-1}
86 };
87 
88 int
89 main(int argc, char **argv)
90 {
91 	struct command *cmds;
92 	char objformat[32];
93 	char *path, *chunk;
94 	char *cmd, *newcmd = NULL;
95 	const char *objformat_path;
96 	const char *ccver;
97 	const char *buver;
98 	const char *ldver;
99 	const char *env_value = NULL;
100 	const char *base_path = NULL;
101 	int use_objformat = 0;
102 
103 	if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
104 		errx(1, "Invalid object format");
105 
106 	/*
107 	 * Get the last path element of the program name being executed
108 	 */
109 	cmd = strrchr(argv[0], '/');
110 	if (cmd != NULL)
111 		cmd++;
112 	else
113 		cmd = argv[0];
114 
115 	for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
116 		if (strcmp(cmd, cmds->cmd) == 0)
117 			break;
118 	}
119 
120 	if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
121 		ccver = CCVER_DEFAULT;
122 	if ((buver = getenv("BINUTILSVER")) == NULL)
123 		buver = BINUTILSVER_DEFAULT;
124 	if ((ldver = getenv("LDVER")) == NULL)
125 		ldver = LDVER_DEFAULT;
126 
127 	if (cmds) {
128 		switch (cmds->type) {
129 		case COMPILER:
130 			base_path = "/usr/libexec";
131 			use_objformat = 0;
132 			env_value = ccver;
133 			break;
134 		case BINUTILS:
135 			base_path = "/usr/libexec";
136 			use_objformat = 1;
137 			env_value = buver;
138 			break;
139 		case OBJFORMAT:
140 			break;
141 		default:
142 			errx(1, "unknown command type");
143 			break;
144 		}
145 	}
146 
147 	/*
148 	 * The objformat command itself doesn't need another exec
149 	 */
150 	if (cmds->type == OBJFORMAT) {
151 		if (argc != 1) {
152 			fprintf(stderr, "Usage: objformat\n");
153 			exit(1);
154 		}
155 
156 		printf("%s\n", objformat);
157 		exit(0);
158 	}
159 
160 	/*
161 	 * make buildworld glue and CCVER overrides.
162 	 */
163 	objformat_path = getenv("OBJFORMAT_PATH");
164 	if (objformat_path == NULL)
165 		objformat_path = OBJFORMAT_PATH_DEFAULT;
166 
167 again:
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 			printf("GOLD!\n");
183 			if (strcmp(cmd, "ld") == 0) {
184 				asprintf(&newcmd, "%s%s/%s/%s/%s.%s",
185 				    chunk, base_path, env_value, objformat,
186 				    cmd, ldver);
187 			} else {
188 				asprintf(&newcmd, "%s%s/%s/%s/%s",
189 				    chunk, base_path, env_value, objformat,
190 				    cmd);
191 			}
192 		} else {
193 			asprintf(&newcmd, "%s%s/%s/%s",
194 				chunk, base_path, env_value, cmd);
195 		}
196 		if (newcmd == NULL)
197 			err(1, "cannot allocate memory");
198 
199 		argv[0] = newcmd;
200 		execv(newcmd, argv);
201 	}
202 
203 	/*
204 	 * Fallback:  if we're searching for a compiler, but didn't
205 	 * find any, try again using the custom compiler driver.
206 	 */
207 	if (cmds && cmds->type == COMPILER &&
208 	    strcmp(env_value, "custom") != 0) {
209 		env_value = "custom";
210 		goto again;
211 	}
212 
213 	if (use_objformat) {
214 		err(1, "in path [%s]%s/%s/%s/%s",
215 			objformat_path, base_path, env_value, objformat, cmd);
216 	} else {
217 		err(1, "in path [%s]%s/%s/%s",
218 			objformat_path, base_path, env_value, cmd);
219 	}
220 }
221