2
1

toolchain-wrapper.c 9.5 KB

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