toolchain-wrapper.c 8.9 KB

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