The cirros image was rebuilt against the 3.13.0-83 kernel, drivers e1000e, igbvf...
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / toolchain / toolchain-external / ext-toolchain-wrapper.c
1 /**
2  * Buildroot wrapper for external toolchains. This simply executes the real
3  * toolchain with a number of arguments (sysroot/arch/..) hardcoded,
4  * to ensure the external toolchain uses the correct configuration.
5  * The hardcoded path arguments are defined relative to the actual location
6  * of the binary.
7  *
8  * (C) 2011 Peter Korsgaard <jacmet@sunsite.dk>
9  * (C) 2011 Daniel Nyström <daniel.nystrom@timeterminal.se>
10  * (C) 2012 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
11  * (C) 2013 Spenser Gilliland <spenser@gillilanding.com>
12  *
13  * This file is licensed under the terms of the GNU General Public License
14  * version 2.  This program is licensed "as is" without any warranty of any
15  * kind, whether express or implied.
16  */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <errno.h>
25
26 static char path[PATH_MAX];
27 static char sysroot[PATH_MAX];
28
29 /**
30  * GCC errors out with certain combinations of arguments (examples are
31  * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
32  * that we only pass the predefined one to the real compiler if the inverse
33  * option isn't in the argument list.
34  * This specifies the worst case number of extra arguments we might pass
35  * Currently, we have:
36  *      -mfloat-abi=
37  *      -march=
38  *      -mcpu=
39  */
40 #define EXCLUSIVE_ARGS  3
41
42 static char *predef_args[] = {
43         path,
44         "--sysroot", sysroot,
45 #ifdef BR_ABI
46         "-mabi=" BR_ABI,
47 #endif
48 #ifdef BR_FPU
49         "-mfpu=" BR_FPU,
50 #endif
51 #ifdef BR_SOFTFLOAT
52         "-msoft-float",
53 #endif /* BR_SOFTFLOAT */
54 #ifdef BR_MODE
55         "-m" BR_MODE,
56 #endif
57 #ifdef BR_64
58         "-m64",
59 #endif
60 #ifdef BR_BINFMT_FLAT
61         "-Wl,-elf2flt",
62 #endif
63 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
64         "-EL",
65 #endif
66 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
67         "-EB",
68 #endif
69 #ifdef BR_ADDITIONAL_CFLAGS
70         BR_ADDITIONAL_CFLAGS
71 #endif
72 };
73
74 static void check_unsafe_path(const char *path, int paranoid)
75 {
76         char **c;
77         static char *unsafe_paths[] = {
78                 "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
79         };
80
81         for (c = unsafe_paths; *c != NULL; c++) {
82                 if (!strncmp(path, *c, strlen(*c))) {
83                         fprintf(stderr, "%s: %s: unsafe header/library path used in cross-compilation: '%s'\n",
84                                 program_invocation_short_name,
85                                 paranoid ? "ERROR" : "WARNING", path);
86                         if (paranoid)
87                                 exit(1);
88                         continue;
89                 }
90         }
91 }
92
93 int main(int argc, char **argv)
94 {
95         char **args, **cur;
96         char *relbasedir, *absbasedir;
97         char *progpath = argv[0];
98         char *basename;
99         char *env_debug;
100         char *paranoid_wrapper;
101         int paranoid;
102         int ret, i, count = 0, debug;
103
104         /* Calculate the relative paths */
105         basename = strrchr(progpath, '/');
106         if (basename) {
107                 *basename = '\0';
108                 basename++;
109                 relbasedir = malloc(strlen(progpath) + 7);
110                 if (relbasedir == NULL) {
111                         perror(__FILE__ ": malloc");
112                         return 2;
113                 }
114                 sprintf(relbasedir, "%s/../..", argv[0]);
115                 absbasedir = realpath(relbasedir, NULL);
116         } else {
117                 basename = progpath;
118                 absbasedir = malloc(PATH_MAX + 1);
119                 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
120                 if (ret < 0) {
121                         perror(__FILE__ ": readlink");
122                         return 2;
123                 }
124                 absbasedir[ret] = '\0';
125                 for (i = ret; i > 0; i--) {
126                         if (absbasedir[i] == '/') {
127                                 absbasedir[i] = '\0';
128                                 if (++count == 3)
129                                         break;
130                         }
131                 }
132         }
133         if (absbasedir == NULL) {
134                 perror(__FILE__ ": realpath");
135                 return 2;
136         }
137
138         /* Fill in the relative paths */
139 #ifdef BR_CROSS_PATH_REL
140         ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
141 #else /* BR_CROSS_PATH_ABS */
142         ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
143 #endif
144         if (ret >= sizeof(path)) {
145                 perror(__FILE__ ": overflow");
146                 return 3;
147         }
148         ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
149         if (ret >= sizeof(sysroot)) {
150                 perror(__FILE__ ": overflow");
151                 return 3;
152         }
153
154         cur = args = malloc(sizeof(predef_args) +
155                             (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
156         if (args == NULL) {
157                 perror(__FILE__ ": malloc");
158                 return 2;
159         }
160
161         /* start with predefined args */
162         memcpy(cur, predef_args, sizeof(predef_args));
163         cur += sizeof(predef_args) / sizeof(predef_args[0]);
164
165 #ifdef BR_FLOAT_ABI
166         /* add float abi if not overridden in args */
167         for (i = 1; i < argc; i++) {
168                 if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
169                     !strcmp(argv[i], "-msoft-float") ||
170                     !strcmp(argv[i], "-mhard-float"))
171                         break;
172         }
173
174         if (i == argc)
175                 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
176 #endif
177
178 #if defined(BR_ARCH) || \
179     defined(BR_CPU)
180         /* Add our -march/cpu/abi flags, but only if none are
181          * already specified on the commandline
182          */
183         for (i = 1; i < argc; i++) {
184                 if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
185                     !strncmp(argv[i], "-mcpu=",  strlen("-mcpu=" )))
186                         break;
187         }
188         if (i == argc) {
189 #ifdef BR_ARCH
190                 *cur++ = "-march=" BR_ARCH;
191 #endif
192 #ifdef BR_CPU
193                 *cur++ = "-mcpu=" BR_CPU;
194 #endif
195         }
196 #endif /* ARCH || CPU */
197
198         paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
199         if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
200                 paranoid = 1;
201         else
202                 paranoid = 0;
203
204         /* Check for unsafe library and header paths */
205         for (i = 1; i < argc; i++) {
206
207                 /* Skip options that do not start with -I and -L */
208                 if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
209                         continue;
210
211                 /* We handle two cases: first the case where -I/-L and
212                  * the path are separated by one space and therefore
213                  * visible as two separate options, and then the case
214                  * where they are stuck together forming one single
215                  * option.
216                  */
217                 if (argv[i][2] == '\0') {
218                         i++;
219                         if (i == argc)
220                                 continue;
221                         check_unsafe_path(argv[i], paranoid);
222                 } else {
223                         check_unsafe_path(argv[i] + 2, paranoid);
224                 }
225         }
226
227         /* append forward args */
228         memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
229         cur += argc - 1;
230
231         /* finish with NULL termination */
232         *cur = NULL;
233
234         /* Debug the wrapper to see actual arguments passed to
235          * the compiler:
236          * unset, empty, or 0: do not trace
237          * set to 1          : trace all arguments on a single line
238          * set to 2          : trace one argument per line
239          */
240         if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
241                 debug = atoi(env_debug);
242                 if (debug > 0) {
243                         fprintf(stderr, "Toolchain wrapper executing:");
244                         for (i = 0; args[i]; i++)
245                                 fprintf(stderr, "%s'%s'",
246                                         (debug == 2) ? "\n    " : " ", args[i]);
247                         fprintf(stderr, "\n");
248                 }
249         }
250
251         if (execv(path, args))
252                 perror(path);
253
254         free(args);
255
256         return 2;
257 }