toolchain-wrapper.c 9.8 KB

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