xref: /OK3568_Linux_fs/buildroot/toolchain/toolchain-wrapper.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /**
2*4882a593Smuzhiyun  * Buildroot wrapper for toolchains. This simply executes the real toolchain
3*4882a593Smuzhiyun  * with a number of arguments (sysroot/arch/..) hardcoded, to ensure the
4*4882a593Smuzhiyun  * toolchain uses the correct configuration.
5*4882a593Smuzhiyun  * The hardcoded path arguments are defined relative to the actual location
6*4882a593Smuzhiyun  * of the binary.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * (C) 2011 Peter Korsgaard <jacmet@sunsite.dk>
9*4882a593Smuzhiyun  * (C) 2011 Daniel Nyström <daniel.nystrom@timeterminal.se>
10*4882a593Smuzhiyun  * (C) 2012 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
11*4882a593Smuzhiyun  * (C) 2013 Spenser Gilliland <spenser@gillilanding.com>
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This file is licensed under the terms of the GNU General Public License
14*4882a593Smuzhiyun  * version 2.  This program is licensed "as is" without any warranty of any
15*4882a593Smuzhiyun  * kind, whether express or implied.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define _GNU_SOURCE
19*4882a593Smuzhiyun #include <stdio.h>
20*4882a593Smuzhiyun #include <string.h>
21*4882a593Smuzhiyun #include <limits.h>
22*4882a593Smuzhiyun #include <unistd.h>
23*4882a593Smuzhiyun #include <stdlib.h>
24*4882a593Smuzhiyun #include <errno.h>
25*4882a593Smuzhiyun #include <time.h>
26*4882a593Smuzhiyun #include <stdbool.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #ifdef BR_CCACHE
29*4882a593Smuzhiyun static char ccache_path[PATH_MAX];
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun static char path[PATH_MAX];
32*4882a593Smuzhiyun static char sysroot[PATH_MAX];
33*4882a593Smuzhiyun /* As would be defined by gcc:
34*4882a593Smuzhiyun  *   https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
35*4882a593Smuzhiyun  * sizeof() on string literals includes the terminating \0. */
36*4882a593Smuzhiyun static char _time_[sizeof("-D__TIME__=\"HH:MM:SS\"")];
37*4882a593Smuzhiyun static char _date_[sizeof("-D__DATE__=\"MMM DD YYYY\"")];
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun  * GCC errors out with certain combinations of arguments (examples are
41*4882a593Smuzhiyun  * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
42*4882a593Smuzhiyun  * that we only pass the predefined one to the real compiler if the inverse
43*4882a593Smuzhiyun  * option isn't in the argument list.
44*4882a593Smuzhiyun  * This specifies the worst case number of extra arguments we might pass
45*4882a593Smuzhiyun  * Currently, we may have:
46*4882a593Smuzhiyun  * 	-mfloat-abi=
47*4882a593Smuzhiyun  * 	-march=
48*4882a593Smuzhiyun  * 	-mcpu=
49*4882a593Smuzhiyun  * 	-D__TIME__=
50*4882a593Smuzhiyun  * 	-D__DATE__=
51*4882a593Smuzhiyun  * 	-Wno-builtin-macro-redefined
52*4882a593Smuzhiyun  * 	-Wl,-z,now
53*4882a593Smuzhiyun  * 	-Wl,-z,relro
54*4882a593Smuzhiyun  * 	-fPIE
55*4882a593Smuzhiyun  * 	-pie
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun #define EXCLUSIVE_ARGS	10
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun static char *predef_args[] = {
60*4882a593Smuzhiyun #ifdef BR_CCACHE
61*4882a593Smuzhiyun 	ccache_path,
62*4882a593Smuzhiyun #endif
63*4882a593Smuzhiyun 	path,
64*4882a593Smuzhiyun 	"--sysroot", sysroot,
65*4882a593Smuzhiyun #ifdef BR_ABI
66*4882a593Smuzhiyun 	"-mabi=" BR_ABI,
67*4882a593Smuzhiyun #endif
68*4882a593Smuzhiyun #ifdef BR_NAN
69*4882a593Smuzhiyun 	"-mnan=" BR_NAN,
70*4882a593Smuzhiyun #endif
71*4882a593Smuzhiyun #ifdef BR_FPU
72*4882a593Smuzhiyun 	"-mfpu=" BR_FPU,
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun #ifdef BR_SOFTFLOAT
75*4882a593Smuzhiyun 	"-msoft-float",
76*4882a593Smuzhiyun #endif /* BR_SOFTFLOAT */
77*4882a593Smuzhiyun #ifdef BR_MODE
78*4882a593Smuzhiyun 	"-m" BR_MODE,
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun #ifdef BR_64
81*4882a593Smuzhiyun 	"-m64",
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun #ifdef BR_OMIT_LOCK_PREFIX
84*4882a593Smuzhiyun 	"-Wa,-momit-lock-prefix=yes",
85*4882a593Smuzhiyun #endif
86*4882a593Smuzhiyun #ifdef BR_NO_FUSED_MADD
87*4882a593Smuzhiyun 	"-mno-fused-madd",
88*4882a593Smuzhiyun #endif
89*4882a593Smuzhiyun #ifdef BR_FP_CONTRACT_OFF
90*4882a593Smuzhiyun 	"-ffp-contract=off",
91*4882a593Smuzhiyun #endif
92*4882a593Smuzhiyun #ifdef BR_BINFMT_FLAT
93*4882a593Smuzhiyun 	"-Wl,-elf2flt",
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
96*4882a593Smuzhiyun 	"-EL",
97*4882a593Smuzhiyun #endif
98*4882a593Smuzhiyun #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
99*4882a593Smuzhiyun 	"-EB",
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun #ifdef BR_ADDITIONAL_CFLAGS
102*4882a593Smuzhiyun 	BR_ADDITIONAL_CFLAGS
103*4882a593Smuzhiyun #endif
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* A {string,length} tuple, to avoid computing strlen() on constants.
107*4882a593Smuzhiyun  *  - str must be a \0-terminated string
108*4882a593Smuzhiyun  *  - len does not account for the terminating '\0'
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun struct str_len_s {
111*4882a593Smuzhiyun 	const char *str;
112*4882a593Smuzhiyun 	size_t     len;
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /* Define a {string,length} tuple. Takes an unquoted constant string as
116*4882a593Smuzhiyun  * parameter. sizeof() on a string literal includes the terminating \0,
117*4882a593Smuzhiyun  * but we don't want to count it.
118*4882a593Smuzhiyun  */
119*4882a593Smuzhiyun #define STR_LEN(s) { #s, sizeof(#s)-1 }
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /* List of paths considered unsafe for cross-compilation.
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * An unsafe path is one that points to a directory with libraries or
124*4882a593Smuzhiyun  * headers for the build machine, which are not suitable for the target.
125*4882a593Smuzhiyun  */
126*4882a593Smuzhiyun static const struct str_len_s unsafe_paths[] = {
127*4882a593Smuzhiyun 	STR_LEN(/lib),
128*4882a593Smuzhiyun 	STR_LEN(/usr/include),
129*4882a593Smuzhiyun 	STR_LEN(/usr/lib),
130*4882a593Smuzhiyun 	STR_LEN(/usr/local/include),
131*4882a593Smuzhiyun 	STR_LEN(/usr/local/lib),
132*4882a593Smuzhiyun 	STR_LEN(/usr/X11R6/include),
133*4882a593Smuzhiyun 	STR_LEN(/usr/X11R6/lib),
134*4882a593Smuzhiyun 	{ NULL, 0 },
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /* Unsafe options are options that specify a potentialy unsafe path,
138*4882a593Smuzhiyun  * that will be checked by check_unsafe_path(), below.
139*4882a593Smuzhiyun  */
140*4882a593Smuzhiyun static const struct str_len_s unsafe_opts[] = {
141*4882a593Smuzhiyun 	STR_LEN(-I),
142*4882a593Smuzhiyun 	STR_LEN(-idirafter),
143*4882a593Smuzhiyun 	STR_LEN(-iquote),
144*4882a593Smuzhiyun 	STR_LEN(-isystem),
145*4882a593Smuzhiyun 	STR_LEN(-L),
146*4882a593Smuzhiyun 	{ NULL, 0 },
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /* Check if path is unsafe for cross-compilation. Unsafe paths are those
150*4882a593Smuzhiyun  * pointing to the standard native include or library paths.
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * We print the arguments leading to the failure. For some options, gcc
153*4882a593Smuzhiyun  * accepts the path to be concatenated to the argument (e.g. -I/foo/bar)
154*4882a593Smuzhiyun  * or separated (e.g. -I /foo/bar). In the first case, we need only print
155*4882a593Smuzhiyun  * the argument as it already contains the path (arg_has_path), while in
156*4882a593Smuzhiyun  * the second case we need to print both (!arg_has_path).
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * If paranoid, exit in error instead of just printing a warning.
159*4882a593Smuzhiyun  */
check_unsafe_path(const char * arg,const char * path,int paranoid,int arg_has_path)160*4882a593Smuzhiyun static void check_unsafe_path(const char *arg,
161*4882a593Smuzhiyun 			      const char *path,
162*4882a593Smuzhiyun 			      int paranoid,
163*4882a593Smuzhiyun 			      int arg_has_path)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	const struct str_len_s *p;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	for (p=unsafe_paths; p->str; p++) {
168*4882a593Smuzhiyun 		if (strncmp(path, p->str, p->len))
169*4882a593Smuzhiyun 			continue;
170*4882a593Smuzhiyun 		fprintf(stderr,
171*4882a593Smuzhiyun 			"%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
172*4882a593Smuzhiyun 			program_invocation_short_name,
173*4882a593Smuzhiyun 			paranoid ? "ERROR" : "WARNING",
174*4882a593Smuzhiyun 			arg,
175*4882a593Smuzhiyun 			arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */
176*4882a593Smuzhiyun 			arg_has_path ? "" : path); /* so that arg and path are properly quoted. */
177*4882a593Smuzhiyun 		if (paranoid)
178*4882a593Smuzhiyun 			exit(1);
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun #ifdef BR_NEED_SOURCE_DATE_EPOCH
183*4882a593Smuzhiyun /* Returns false if SOURCE_DATE_EPOCH was not defined in the environment.
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * Returns true if SOURCE_DATE_EPOCH is in the environment and represent
186*4882a593Smuzhiyun  * a valid timestamp, in which case the timestamp is formatted into the
187*4882a593Smuzhiyun  * global variables _date_ and _time_.
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * Aborts if SOURCE_DATE_EPOCH was set in the environment but did not
190*4882a593Smuzhiyun  * contain a valid timestamp.
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * Valid values are defined in the spec:
193*4882a593Smuzhiyun  *     https://reproducible-builds.org/specs/source-date-epoch/
194*4882a593Smuzhiyun  * but we further restrict them to be positive or null.
195*4882a593Smuzhiyun  */
parse_source_date_epoch_from_env(void)196*4882a593Smuzhiyun bool parse_source_date_epoch_from_env(void)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	char *epoch_env, *endptr;
199*4882a593Smuzhiyun 	time_t epoch;
200*4882a593Smuzhiyun 	struct tm epoch_tm;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if ((epoch_env = getenv("SOURCE_DATE_EPOCH")) == NULL)
203*4882a593Smuzhiyun 		return false;
204*4882a593Smuzhiyun 	errno = 0;
205*4882a593Smuzhiyun 	epoch = (time_t) strtoll(epoch_env, &endptr, 10);
206*4882a593Smuzhiyun 	/* We just need to test if it is incorrect, but we do not
207*4882a593Smuzhiyun 	 * care why it is incorrect.
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	if ((errno != 0) || !*epoch_env || *endptr || (epoch < 0)) {
210*4882a593Smuzhiyun 		fprintf(stderr, "%s: invalid SOURCE_DATE_EPOCH='%s'\n",
211*4882a593Smuzhiyun 			program_invocation_short_name,
212*4882a593Smuzhiyun 			epoch_env);
213*4882a593Smuzhiyun 		exit(1);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 	tzset(); /* For localtime_r(), below. */
216*4882a593Smuzhiyun 	if (localtime_r(&epoch, &epoch_tm) == NULL) {
217*4882a593Smuzhiyun 		fprintf(stderr, "%s: cannot parse SOURCE_DATE_EPOCH=%s\n",
218*4882a593Smuzhiyun 				program_invocation_short_name,
219*4882a593Smuzhiyun 				getenv("SOURCE_DATE_EPOCH"));
220*4882a593Smuzhiyun 		exit(1);
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 	if (!strftime(_time_, sizeof(_time_), "-D__TIME__=\"%T\"", &epoch_tm)) {
223*4882a593Smuzhiyun 		fprintf(stderr, "%s: cannot set time from SOURCE_DATE_EPOCH=%s\n",
224*4882a593Smuzhiyun 				program_invocation_short_name,
225*4882a593Smuzhiyun 				getenv("SOURCE_DATE_EPOCH"));
226*4882a593Smuzhiyun 		exit(1);
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 	if (!strftime(_date_, sizeof(_date_), "-D__DATE__=\"%b %e %Y\"", &epoch_tm)) {
229*4882a593Smuzhiyun 		fprintf(stderr, "%s: cannot set date from SOURCE_DATE_EPOCH=%s\n",
230*4882a593Smuzhiyun 				program_invocation_short_name,
231*4882a593Smuzhiyun 				getenv("SOURCE_DATE_EPOCH"));
232*4882a593Smuzhiyun 		exit(1);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 	return true;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun #else
parse_source_date_epoch_from_env(void)237*4882a593Smuzhiyun bool parse_source_date_epoch_from_env(void)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	/* The compiler is recent enough to handle SOURCE_DATE_EPOCH itself
240*4882a593Smuzhiyun 	 * so we do not need to do anything here.
241*4882a593Smuzhiyun 	 */
242*4882a593Smuzhiyun 	return false;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun #endif
245*4882a593Smuzhiyun 
main(int argc,char ** argv)246*4882a593Smuzhiyun int main(int argc, char **argv)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	char **args, **cur, **exec_args;
249*4882a593Smuzhiyun 	char *relbasedir, *absbasedir;
250*4882a593Smuzhiyun 	char *progpath = argv[0];
251*4882a593Smuzhiyun 	char *basename;
252*4882a593Smuzhiyun 	char *env_debug;
253*4882a593Smuzhiyun 	char *paranoid_wrapper;
254*4882a593Smuzhiyun 	int paranoid;
255*4882a593Smuzhiyun 	int ret, i, count = 0, debug = 0, found_shared = 0, linker_args = 1;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/* Debug the wrapper to see arguments it was called with.
258*4882a593Smuzhiyun 	 * If environment variable BR2_DEBUG_WRAPPER is:
259*4882a593Smuzhiyun 	 * unset, empty, or 0: do not trace
260*4882a593Smuzhiyun 	 * set to 1          : trace all arguments on a single line
261*4882a593Smuzhiyun 	 * set to 2          : trace one argument per line
262*4882a593Smuzhiyun 	 */
263*4882a593Smuzhiyun 	if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
264*4882a593Smuzhiyun 		debug = atoi(env_debug);
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 	if (debug > 0) {
267*4882a593Smuzhiyun 		fprintf(stderr, "Toolchain wrapper was called with:");
268*4882a593Smuzhiyun 		for (i = 0; i < argc; i++)
269*4882a593Smuzhiyun 			fprintf(stderr, "%s'%s'",
270*4882a593Smuzhiyun 				(debug == 2) ? "\n    " : " ", argv[i]);
271*4882a593Smuzhiyun 		fprintf(stderr, "\n");
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Calculate the relative paths */
275*4882a593Smuzhiyun 	basename = strrchr(progpath, '/');
276*4882a593Smuzhiyun 	if (basename) {
277*4882a593Smuzhiyun 		*basename = '\0';
278*4882a593Smuzhiyun 		basename++;
279*4882a593Smuzhiyun 		relbasedir = malloc(strlen(progpath) + 7);
280*4882a593Smuzhiyun 		if (relbasedir == NULL) {
281*4882a593Smuzhiyun 			perror(__FILE__ ": malloc");
282*4882a593Smuzhiyun 			return 2;
283*4882a593Smuzhiyun 		}
284*4882a593Smuzhiyun 		sprintf(relbasedir, "%s/..", argv[0]);
285*4882a593Smuzhiyun 		absbasedir = realpath(relbasedir, NULL);
286*4882a593Smuzhiyun 	} else {
287*4882a593Smuzhiyun 		basename = progpath;
288*4882a593Smuzhiyun 		absbasedir = malloc(PATH_MAX + 1);
289*4882a593Smuzhiyun 		ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
290*4882a593Smuzhiyun 		if (ret < 0) {
291*4882a593Smuzhiyun 			perror(__FILE__ ": readlink");
292*4882a593Smuzhiyun 			return 2;
293*4882a593Smuzhiyun 		}
294*4882a593Smuzhiyun 		absbasedir[ret] = '\0';
295*4882a593Smuzhiyun 		for (i = ret; i > 0; i--) {
296*4882a593Smuzhiyun 			if (absbasedir[i] == '/') {
297*4882a593Smuzhiyun 				absbasedir[i] = '\0';
298*4882a593Smuzhiyun 				if (++count == 2)
299*4882a593Smuzhiyun 					break;
300*4882a593Smuzhiyun 			}
301*4882a593Smuzhiyun 		}
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 	if (absbasedir == NULL) {
304*4882a593Smuzhiyun 		perror(__FILE__ ": realpath");
305*4882a593Smuzhiyun 		return 2;
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/* clang doesn't like linker input when no linking */
309*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
310*4882a593Smuzhiyun 		if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "-E") ||
311*4882a593Smuzhiyun 		    !strcmp(argv[i], "-s") ||
312*4882a593Smuzhiyun 		    !strcmp(argv[i], "--precompile")) {
313*4882a593Smuzhiyun 			if (!strncmp(basename, "clang", strlen("clang")))
314*4882a593Smuzhiyun 				linker_args = 0;
315*4882a593Smuzhiyun 			break;
316*4882a593Smuzhiyun 		}
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	/**
320*4882a593Smuzhiyun 	 * Toolchains don't like linker input for '-v' without sources.
321*4882a593Smuzhiyun 	 * It's to hard to handle all those cases, let's focus on a single
322*4882a593Smuzhiyun 	 * '-v' here, since people might use "$CC -v" to dump spec.
323*4882a593Smuzhiyun 	 */
324*4882a593Smuzhiyun 	if (argc == 2 && !strcmp(argv[1], "-v"))
325*4882a593Smuzhiyun 		linker_args = 0;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/* Fill in the relative paths */
328*4882a593Smuzhiyun #ifdef BR_CROSS_PATH_REL
329*4882a593Smuzhiyun 	ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
330*4882a593Smuzhiyun #elif defined(BR_CROSS_PATH_ABS)
331*4882a593Smuzhiyun 	ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename);
332*4882a593Smuzhiyun #else
333*4882a593Smuzhiyun 	ret = snprintf(path, sizeof(path), "%s/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
334*4882a593Smuzhiyun #endif
335*4882a593Smuzhiyun 	if (ret >= sizeof(path)) {
336*4882a593Smuzhiyun 		perror(__FILE__ ": overflow");
337*4882a593Smuzhiyun 		return 3;
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun #ifdef BR_CCACHE
340*4882a593Smuzhiyun 	ret = snprintf(ccache_path, sizeof(ccache_path), "%s/bin/ccache", absbasedir);
341*4882a593Smuzhiyun 	if (ret >= sizeof(ccache_path)) {
342*4882a593Smuzhiyun 		perror(__FILE__ ": overflow");
343*4882a593Smuzhiyun 		return 3;
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun #endif
346*4882a593Smuzhiyun 	ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
347*4882a593Smuzhiyun 	if (ret >= sizeof(sysroot)) {
348*4882a593Smuzhiyun 		perror(__FILE__ ": overflow");
349*4882a593Smuzhiyun 		return 3;
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	cur = args = malloc(sizeof(predef_args) +
353*4882a593Smuzhiyun 			    (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
354*4882a593Smuzhiyun 	if (args == NULL) {
355*4882a593Smuzhiyun 		perror(__FILE__ ": malloc");
356*4882a593Smuzhiyun 		return 2;
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	/* start with predefined args */
360*4882a593Smuzhiyun 	memcpy(cur, predef_args, sizeof(predef_args));
361*4882a593Smuzhiyun 	cur += sizeof(predef_args) / sizeof(predef_args[0]);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun #ifdef BR_FLOAT_ABI
364*4882a593Smuzhiyun 	/* add float abi if not overridden in args */
365*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
366*4882a593Smuzhiyun 		if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
367*4882a593Smuzhiyun 		    !strcmp(argv[i], "-msoft-float") ||
368*4882a593Smuzhiyun 		    !strcmp(argv[i], "-mhard-float"))
369*4882a593Smuzhiyun 			break;
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (i == argc)
373*4882a593Smuzhiyun 		*cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
374*4882a593Smuzhiyun #endif
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun #ifdef BR_FP32_MODE
377*4882a593Smuzhiyun 	/* add fp32 mode if soft-float is not args or hard-float overrides soft-float */
378*4882a593Smuzhiyun 	int add_fp32_mode = 1;
379*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
380*4882a593Smuzhiyun 		if (!strcmp(argv[i], "-msoft-float"))
381*4882a593Smuzhiyun 			add_fp32_mode = 0;
382*4882a593Smuzhiyun 		else if (!strcmp(argv[i], "-mhard-float"))
383*4882a593Smuzhiyun 			add_fp32_mode = 1;
384*4882a593Smuzhiyun 	}
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if (add_fp32_mode == 1)
387*4882a593Smuzhiyun 		*cur++ = "-mfp" BR_FP32_MODE;
388*4882a593Smuzhiyun #endif
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun #if defined(BR_ARCH) || \
391*4882a593Smuzhiyun     defined(BR_CPU)
392*4882a593Smuzhiyun 	/* Add our -march/cpu flags, but only if none of
393*4882a593Smuzhiyun 	 * -march/mtune/mcpu are already specified on the commandline
394*4882a593Smuzhiyun 	 */
395*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
396*4882a593Smuzhiyun 		if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
397*4882a593Smuzhiyun 		    !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
398*4882a593Smuzhiyun 		    !strncmp(argv[i], "-mcpu=",  strlen("-mcpu=" )))
399*4882a593Smuzhiyun 			break;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 	if (i == argc) {
402*4882a593Smuzhiyun #ifdef BR_ARCH
403*4882a593Smuzhiyun 		*cur++ = "-march=" BR_ARCH;
404*4882a593Smuzhiyun #endif
405*4882a593Smuzhiyun #ifdef BR_CPU
406*4882a593Smuzhiyun 		*cur++ = "-mcpu=" BR_CPU;
407*4882a593Smuzhiyun #endif
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun #endif /* ARCH || CPU */
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	if (parse_source_date_epoch_from_env()) {
412*4882a593Smuzhiyun 		*cur++ = _time_;
413*4882a593Smuzhiyun 		*cur++ = _date_;
414*4882a593Smuzhiyun 		/* This has existed since gcc-4.4.0. */
415*4882a593Smuzhiyun 		*cur++ = "-Wno-builtin-macro-redefined";
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun #ifdef BR2_PIC_PIE
419*4882a593Smuzhiyun 	/* Patterned after Fedora/Gentoo hardening approaches.
420*4882a593Smuzhiyun 	 * https://fedoraproject.org/wiki/Changes/Harden_All_Packages
421*4882a593Smuzhiyun 	 * https://wiki.gentoo.org/wiki/Hardened/Toolchain#Position_Independent_Executables_.28PIEs.29
422*4882a593Smuzhiyun 	 *
423*4882a593Smuzhiyun 	 * A few checks are added to allow disabling of PIE
424*4882a593Smuzhiyun 	 * 1) -fno-pie and -no-pie are used by other distros to disable PIE in
425*4882a593Smuzhiyun 	 *    cases where the compiler enables it by default. The logic below
426*4882a593Smuzhiyun 	 *    maintains that behavior.
427*4882a593Smuzhiyun 	 *         Ref: https://wiki.ubuntu.com/SecurityTeam/PIE
428*4882a593Smuzhiyun 	 * 2) A check for -fno-PIE has been used in older Linux Kernel builds
429*4882a593Smuzhiyun 	 *    in a similar way to -fno-pie or -no-pie.
430*4882a593Smuzhiyun 	 * 3) A check is added for Kernel and U-boot defines
431*4882a593Smuzhiyun 	 *    (-D__KERNEL__ and -D__UBOOT__).
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
434*4882a593Smuzhiyun 		/* Apply all incompatible link flag and disable checks first */
435*4882a593Smuzhiyun 		if (!strcmp(argv[i], "-r") ||
436*4882a593Smuzhiyun 		    !strcmp(argv[i], "-Wl,-r") ||
437*4882a593Smuzhiyun 		    !strcmp(argv[i], "-static") ||
438*4882a593Smuzhiyun 		    !strcmp(argv[i], "-D__KERNEL__") ||
439*4882a593Smuzhiyun 		    !strcmp(argv[i], "-D__UBOOT__") ||
440*4882a593Smuzhiyun 		    !strcmp(argv[i], "-fno-pie") ||
441*4882a593Smuzhiyun 		    !strcmp(argv[i], "-fno-PIE") ||
442*4882a593Smuzhiyun 		    !strcmp(argv[i], "-no-pie"))
443*4882a593Smuzhiyun 			break;
444*4882a593Smuzhiyun 		/* Record that shared was present which disables -pie but don't
445*4882a593Smuzhiyun 		 * break out of loop as a check needs to occur that possibly
446*4882a593Smuzhiyun 		 * still allows -fPIE to be set
447*4882a593Smuzhiyun 		 */
448*4882a593Smuzhiyun 		if (!strcmp(argv[i], "-shared"))
449*4882a593Smuzhiyun 			found_shared = 1;
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (i == argc) {
453*4882a593Smuzhiyun 		/* Compile and link condition checking have been kept split
454*4882a593Smuzhiyun 		 * between these two loops, as there maybe already are valid
455*4882a593Smuzhiyun 		 * compile flags set for position independence. In that case
456*4882a593Smuzhiyun 		 * the wrapper just adds the -pie for link.
457*4882a593Smuzhiyun 		 */
458*4882a593Smuzhiyun 		for (i = 1; i < argc; i++) {
459*4882a593Smuzhiyun 			if (!strcmp(argv[i], "-fpie") ||
460*4882a593Smuzhiyun 			    !strcmp(argv[i], "-fPIE") ||
461*4882a593Smuzhiyun 			    !strcmp(argv[i], "-fpic") ||
462*4882a593Smuzhiyun 			    !strcmp(argv[i], "-fPIC"))
463*4882a593Smuzhiyun 				break;
464*4882a593Smuzhiyun 		}
465*4882a593Smuzhiyun 		/* Both args below can be set at compile/link time
466*4882a593Smuzhiyun 		 * and are ignored correctly when not used
467*4882a593Smuzhiyun 		 */
468*4882a593Smuzhiyun 		if (i == argc)
469*4882a593Smuzhiyun 			*cur++ = "-fPIE";
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 		if (!found_shared)
472*4882a593Smuzhiyun 			*cur++ = "-pie";
473*4882a593Smuzhiyun 	}
474*4882a593Smuzhiyun #endif
475*4882a593Smuzhiyun 	/* Are we building the Linux Kernel or U-Boot? */
476*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
477*4882a593Smuzhiyun 		if (!strcmp(argv[i], "-D__KERNEL__") ||
478*4882a593Smuzhiyun 		    !strcmp(argv[i], "-D__UBOOT__"))
479*4882a593Smuzhiyun 			break;
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 	if (i == argc) {
482*4882a593Smuzhiyun 		/* https://wiki.gentoo.org/wiki/Hardened/Toolchain#Mark_Read-Only_Appropriate_Sections */
483*4882a593Smuzhiyun #ifdef BR2_RELRO_PARTIAL
484*4882a593Smuzhiyun 		*cur++ = "-Wl,-z,relro";
485*4882a593Smuzhiyun #endif
486*4882a593Smuzhiyun #ifdef BR2_RELRO_FULL
487*4882a593Smuzhiyun 		*cur++ = "-Wl,-z,now";
488*4882a593Smuzhiyun 		*cur++ = "-Wl,-z,relro";
489*4882a593Smuzhiyun #endif
490*4882a593Smuzhiyun 	}
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	/* filter out linker args */
493*4882a593Smuzhiyun 	if (!linker_args) {
494*4882a593Smuzhiyun 		for (i = 0; args + i != cur;) {
495*4882a593Smuzhiyun 			if (!strncmp(args[i], "-Wl,", strlen("-Wl,")) ||
496*4882a593Smuzhiyun 			    !strcmp(args[i], "-pie") ||
497*4882a593Smuzhiyun 			    !strcmp(args[i], "-no-pie")) {
498*4882a593Smuzhiyun 				cur--;
499*4882a593Smuzhiyun 				args[i] = *cur;
500*4882a593Smuzhiyun 				continue;
501*4882a593Smuzhiyun 			}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 			i++;
504*4882a593Smuzhiyun 		}
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
508*4882a593Smuzhiyun 	if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
509*4882a593Smuzhiyun 		paranoid = 1;
510*4882a593Smuzhiyun 	else
511*4882a593Smuzhiyun 		paranoid = 0;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	/* Check for unsafe library and header paths */
514*4882a593Smuzhiyun 	for (i = 1; i < argc; i++) {
515*4882a593Smuzhiyun 		const struct str_len_s *opt;
516*4882a593Smuzhiyun 		for (opt=unsafe_opts; opt->str; opt++ ) {
517*4882a593Smuzhiyun 			/* Skip any non-unsafe option. */
518*4882a593Smuzhiyun 			if (strncmp(argv[i], opt->str, opt->len))
519*4882a593Smuzhiyun 				continue;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 			/* Handle both cases:
522*4882a593Smuzhiyun 			 *  - path is a separate argument,
523*4882a593Smuzhiyun 			 *  - path is concatenated with option.
524*4882a593Smuzhiyun 			 */
525*4882a593Smuzhiyun 			if (argv[i][opt->len] == '\0') {
526*4882a593Smuzhiyun 				i++;
527*4882a593Smuzhiyun 				if (i == argc)
528*4882a593Smuzhiyun 					break;
529*4882a593Smuzhiyun 				check_unsafe_path(argv[i-1], argv[i], paranoid, 0);
530*4882a593Smuzhiyun 			} else
531*4882a593Smuzhiyun 				check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1);
532*4882a593Smuzhiyun 		}
533*4882a593Smuzhiyun 	}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/* append forward args */
536*4882a593Smuzhiyun 	memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
537*4882a593Smuzhiyun 	cur += argc - 1;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	/* finish with NULL termination */
540*4882a593Smuzhiyun 	*cur = NULL;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	exec_args = args;
543*4882a593Smuzhiyun #ifdef BR_CCACHE
544*4882a593Smuzhiyun 	if (getenv("BR_NO_CCACHE"))
545*4882a593Smuzhiyun 		/* Skip the ccache call */
546*4882a593Smuzhiyun 		exec_args++;
547*4882a593Smuzhiyun #endif
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/* Debug the wrapper to see final arguments passed to the real compiler. */
550*4882a593Smuzhiyun 	if (debug > 0) {
551*4882a593Smuzhiyun 		fprintf(stderr, "Toolchain wrapper executing:");
552*4882a593Smuzhiyun #ifdef BR_CCACHE_HASH
553*4882a593Smuzhiyun 		fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
554*4882a593Smuzhiyun 			(debug == 2) ? "\n    " : " ");
555*4882a593Smuzhiyun #endif
556*4882a593Smuzhiyun #ifdef BR_CCACHE_BASEDIR
557*4882a593Smuzhiyun 		fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'",
558*4882a593Smuzhiyun 			(debug == 2) ? "\n    " : " ");
559*4882a593Smuzhiyun #endif
560*4882a593Smuzhiyun 		for (i = 0; exec_args[i]; i++)
561*4882a593Smuzhiyun 			fprintf(stderr, "%s'%s'",
562*4882a593Smuzhiyun 				(debug == 2) ? "\n    " : " ", exec_args[i]);
563*4882a593Smuzhiyun 		fprintf(stderr, "\n");
564*4882a593Smuzhiyun 	}
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun #ifdef BR_CCACHE_HASH
567*4882a593Smuzhiyun 	/* Allow compilercheck to be overridden through the environment */
568*4882a593Smuzhiyun 	if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
569*4882a593Smuzhiyun 		perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
570*4882a593Smuzhiyun 		return 3;
571*4882a593Smuzhiyun 	}
572*4882a593Smuzhiyun #endif
573*4882a593Smuzhiyun #ifdef BR_CCACHE_BASEDIR
574*4882a593Smuzhiyun 	/* Allow compilercheck to be overridden through the environment */
575*4882a593Smuzhiyun 	if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) {
576*4882a593Smuzhiyun 		perror(__FILE__ ": Failed to set CCACHE_BASEDIR");
577*4882a593Smuzhiyun 		return 3;
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun #endif
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	if (execv(exec_args[0], exec_args))
582*4882a593Smuzhiyun 		perror(path);
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	free(args);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	return 2;
587*4882a593Smuzhiyun }
588