toolchain-wrapper.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * Buildroot wrapper for toolchains. This simply executes the real toolchain
  3. * with a number of arguments (sysroot/arch/..) hardcoded, to ensure the
  4. * 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. #define _GNU_SOURCE
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #ifdef BR_CCACHE
  25. static char ccache_path[PATH_MAX];
  26. #endif
  27. static char path[PATH_MAX];
  28. static char sysroot[PATH_MAX];
  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. static char *predef_args[] = {
  42. #ifdef BR_CCACHE
  43. ccache_path,
  44. #endif
  45. path,
  46. "--sysroot", sysroot,
  47. #ifdef BR_ABI
  48. "-mabi=" BR_ABI,
  49. #endif
  50. #ifdef BR_FPU
  51. "-mfpu=" BR_FPU,
  52. #endif
  53. #ifdef BR_SOFTFLOAT
  54. "-msoft-float",
  55. #endif /* BR_SOFTFLOAT */
  56. #ifdef BR_MODE
  57. "-m" BR_MODE,
  58. #endif
  59. #ifdef BR_64
  60. "-m64",
  61. #endif
  62. #ifdef BR_OMIT_LOCK_PREFIX
  63. "-Wa,-momit-lock-prefix=yes",
  64. #endif
  65. #ifdef BR_BINFMT_FLAT
  66. "-Wl,-elf2flt",
  67. #endif
  68. #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
  69. "-EL",
  70. #endif
  71. #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
  72. "-EB",
  73. #endif
  74. #ifdef BR_ADDITIONAL_CFLAGS
  75. BR_ADDITIONAL_CFLAGS
  76. #endif
  77. };
  78. struct unsafe_opt_s {
  79. const char *arg;
  80. size_t len;
  81. };
  82. /* Unsafe options are options that specify a potentialy unsafe path,
  83. * that will be checked by check_unsafe_path(), below.
  84. *
  85. * sizeof() on a string literal includes the terminating \0.
  86. */
  87. #define UNSAFE_OPT(o) { #o, sizeof(#o)-1 }
  88. static const struct unsafe_opt_s unsafe_opts[] = {
  89. UNSAFE_OPT(-I),
  90. UNSAFE_OPT(-idirafter),
  91. UNSAFE_OPT(-iquote),
  92. UNSAFE_OPT(-isystem),
  93. UNSAFE_OPT(-L),
  94. { NULL, 0 },
  95. };
  96. /* Check if path is unsafe for cross-compilation. Unsafe paths are those
  97. * pointing to the standard native include or library paths.
  98. *
  99. * We print the arguments leading to the failure. For some options, gcc
  100. * accepts the path to be concatenated to the argument (e.g. -I/foo/bar)
  101. * or separated (e.g. -I /foo/bar). In the first case, we need only print
  102. * the argument as it already contains the path (arg_has_path), while in
  103. * the second case we need to print both (!arg_has_path).
  104. *
  105. * If paranoid, exit in error instead of just printing a warning.
  106. */
  107. static void check_unsafe_path(const char *arg,
  108. const char *path,
  109. int paranoid,
  110. int arg_has_path)
  111. {
  112. char **c;
  113. static char *unsafe_paths[] = {
  114. "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
  115. };
  116. for (c = unsafe_paths; *c != NULL; c++) {
  117. if (strncmp(path, *c, strlen(*c)))
  118. continue;
  119. fprintf(stderr,
  120. "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
  121. program_invocation_short_name,
  122. paranoid ? "ERROR" : "WARNING",
  123. arg,
  124. arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */
  125. arg_has_path ? "" : path); /* so that arg and path are properly quoted. */
  126. if (paranoid)
  127. exit(1);
  128. }
  129. }
  130. int main(int argc, char **argv)
  131. {
  132. char **args, **cur, **exec_args;
  133. char *relbasedir, *absbasedir;
  134. char *progpath = argv[0];
  135. char *basename;
  136. char *env_debug;
  137. char *paranoid_wrapper;
  138. int paranoid;
  139. int ret, i, count = 0, debug;
  140. /* Calculate the relative paths */
  141. basename = strrchr(progpath, '/');
  142. if (basename) {
  143. *basename = '\0';
  144. basename++;
  145. relbasedir = malloc(strlen(progpath) + 7);
  146. if (relbasedir == NULL) {
  147. perror(__FILE__ ": malloc");
  148. return 2;
  149. }
  150. sprintf(relbasedir, "%s/../..", argv[0]);
  151. absbasedir = realpath(relbasedir, NULL);
  152. } else {
  153. basename = progpath;
  154. absbasedir = malloc(PATH_MAX + 1);
  155. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  156. if (ret < 0) {
  157. perror(__FILE__ ": readlink");
  158. return 2;
  159. }
  160. absbasedir[ret] = '\0';
  161. for (i = ret; i > 0; i--) {
  162. if (absbasedir[i] == '/') {
  163. absbasedir[i] = '\0';
  164. if (++count == 3)
  165. break;
  166. }
  167. }
  168. }
  169. if (absbasedir == NULL) {
  170. perror(__FILE__ ": realpath");
  171. return 2;
  172. }
  173. /* Fill in the relative paths */
  174. #ifdef BR_CROSS_PATH_REL
  175. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
  176. #elif defined(BR_CROSS_PATH_ABS)
  177. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename);
  178. #else
  179. ret = snprintf(path, sizeof(path), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
  180. #endif
  181. if (ret >= sizeof(path)) {
  182. perror(__FILE__ ": overflow");
  183. return 3;
  184. }
  185. #ifdef BR_CCACHE
  186. ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
  187. if (ret >= sizeof(ccache_path)) {
  188. perror(__FILE__ ": overflow");
  189. return 3;
  190. }
  191. #endif
  192. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  193. if (ret >= sizeof(sysroot)) {
  194. perror(__FILE__ ": overflow");
  195. return 3;
  196. }
  197. cur = args = malloc(sizeof(predef_args) +
  198. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  199. if (args == NULL) {
  200. perror(__FILE__ ": malloc");
  201. return 2;
  202. }
  203. /* start with predefined args */
  204. memcpy(cur, predef_args, sizeof(predef_args));
  205. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  206. #ifdef BR_FLOAT_ABI
  207. /* add float abi if not overridden in args */
  208. for (i = 1; i < argc; i++) {
  209. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  210. !strcmp(argv[i], "-msoft-float") ||
  211. !strcmp(argv[i], "-mhard-float"))
  212. break;
  213. }
  214. if (i == argc)
  215. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  216. #endif
  217. #if defined(BR_ARCH) || \
  218. defined(BR_CPU)
  219. /* Add our -march/cpu flags, but only if none of
  220. * -march/mtune/mcpu are already specified on the commandline
  221. */
  222. for (i = 1; i < argc; i++) {
  223. if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
  224. !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
  225. !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
  226. break;
  227. }
  228. if (i == argc) {
  229. #ifdef BR_ARCH
  230. *cur++ = "-march=" BR_ARCH;
  231. #endif
  232. #ifdef BR_CPU
  233. *cur++ = "-mcpu=" BR_CPU;
  234. #endif
  235. }
  236. #endif /* ARCH || CPU */
  237. paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
  238. if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
  239. paranoid = 1;
  240. else
  241. paranoid = 0;
  242. /* Check for unsafe library and header paths */
  243. for (i = 1; i < argc; i++) {
  244. const struct unsafe_opt_s *opt;
  245. for (opt=unsafe_opts; opt->arg; opt++ ) {
  246. /* Skip any non-unsafe option. */
  247. if (strncmp(argv[i], opt->arg, opt->len))
  248. continue;
  249. /* Handle both cases:
  250. * - path is a separate argument,
  251. * - path is concatenated with option.
  252. */
  253. if (argv[i][opt->len] == '\0') {
  254. i++;
  255. if (i == argc)
  256. break;
  257. check_unsafe_path(argv[i-1], argv[i], paranoid, 0);
  258. } else
  259. check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1);
  260. }
  261. }
  262. /* append forward args */
  263. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  264. cur += argc - 1;
  265. /* finish with NULL termination */
  266. *cur = NULL;
  267. exec_args = args;
  268. #ifdef BR_CCACHE
  269. if (getenv("BR_NO_CCACHE"))
  270. /* Skip the ccache call */
  271. exec_args++;
  272. #endif
  273. /* Debug the wrapper to see actual arguments passed to
  274. * the compiler:
  275. * unset, empty, or 0: do not trace
  276. * set to 1 : trace all arguments on a single line
  277. * set to 2 : trace one argument per line
  278. */
  279. if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
  280. debug = atoi(env_debug);
  281. if (debug > 0) {
  282. fprintf(stderr, "Toolchain wrapper executing:");
  283. #ifdef BR_CCACHE_HASH
  284. fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
  285. (debug == 2) ? "\n " : " ");
  286. #endif
  287. #ifdef BR_CCACHE_BASEDIR
  288. fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'",
  289. (debug == 2) ? "\n " : " ");
  290. #endif
  291. for (i = 0; exec_args[i]; i++)
  292. fprintf(stderr, "%s'%s'",
  293. (debug == 2) ? "\n " : " ", exec_args[i]);
  294. fprintf(stderr, "\n");
  295. }
  296. }
  297. #ifdef BR_CCACHE_HASH
  298. /* Allow compilercheck to be overridden through the environment */
  299. if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
  300. perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
  301. return 3;
  302. }
  303. #endif
  304. #ifdef BR_CCACHE_BASEDIR
  305. /* Allow compilercheck to be overridden through the environment */
  306. if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) {
  307. perror(__FILE__ ": Failed to set CCACHE_BASEDIR");
  308. return 3;
  309. }
  310. #endif
  311. if (execv(exec_args[0], exec_args))
  312. perror(path);
  313. free(args);
  314. return 2;
  315. }