toolchain-wrapper.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_BINFMT_FLAT
  63. "-Wl,-elf2flt",
  64. #endif
  65. #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
  66. "-EL",
  67. #endif
  68. #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
  69. "-EB",
  70. #endif
  71. #ifdef BR_ADDITIONAL_CFLAGS
  72. BR_ADDITIONAL_CFLAGS
  73. #endif
  74. };
  75. static void check_unsafe_path(const char *path, int paranoid)
  76. {
  77. char **c;
  78. static char *unsafe_paths[] = {
  79. "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
  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. int main(int argc, char **argv)
  93. {
  94. char **args, **cur, **exec_args;
  95. char *relbasedir, *absbasedir;
  96. char *progpath = argv[0];
  97. char *basename;
  98. char *env_debug;
  99. char *paranoid_wrapper;
  100. int paranoid;
  101. int ret, i, count = 0, debug;
  102. /* Calculate the relative paths */
  103. basename = strrchr(progpath, '/');
  104. if (basename) {
  105. *basename = '\0';
  106. basename++;
  107. relbasedir = malloc(strlen(progpath) + 7);
  108. if (relbasedir == NULL) {
  109. perror(__FILE__ ": malloc");
  110. return 2;
  111. }
  112. sprintf(relbasedir, "%s/../..", argv[0]);
  113. absbasedir = realpath(relbasedir, NULL);
  114. } else {
  115. basename = progpath;
  116. absbasedir = malloc(PATH_MAX + 1);
  117. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  118. if (ret < 0) {
  119. perror(__FILE__ ": readlink");
  120. return 2;
  121. }
  122. absbasedir[ret] = '\0';
  123. for (i = ret; i > 0; i--) {
  124. if (absbasedir[i] == '/') {
  125. absbasedir[i] = '\0';
  126. if (++count == 3)
  127. break;
  128. }
  129. }
  130. }
  131. if (absbasedir == NULL) {
  132. perror(__FILE__ ": realpath");
  133. return 2;
  134. }
  135. /* Fill in the relative paths */
  136. #ifdef BR_CROSS_PATH_REL
  137. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  138. #elif BR_CROSS_PATH_ABS
  139. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  140. #else /* BR_CROSS_PATH_SUFFIX */
  141. ret = snprintf(path, sizeof(path), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
  142. #endif
  143. if (ret >= sizeof(path)) {
  144. perror(__FILE__ ": overflow");
  145. return 3;
  146. }
  147. #ifdef BR_CCACHE
  148. ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
  149. if (ret >= sizeof(ccache_path)) {
  150. perror(__FILE__ ": overflow");
  151. return 3;
  152. }
  153. #endif
  154. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  155. if (ret >= sizeof(sysroot)) {
  156. perror(__FILE__ ": overflow");
  157. return 3;
  158. }
  159. cur = args = malloc(sizeof(predef_args) +
  160. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  161. if (args == NULL) {
  162. perror(__FILE__ ": malloc");
  163. return 2;
  164. }
  165. /* start with predefined args */
  166. memcpy(cur, predef_args, sizeof(predef_args));
  167. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  168. #ifdef BR_FLOAT_ABI
  169. /* add float abi if not overridden in args */
  170. for (i = 1; i < argc; i++) {
  171. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  172. !strcmp(argv[i], "-msoft-float") ||
  173. !strcmp(argv[i], "-mhard-float"))
  174. break;
  175. }
  176. if (i == argc)
  177. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  178. #endif
  179. #if defined(BR_ARCH) || \
  180. defined(BR_CPU)
  181. /* Add our -march/cpu flags, but only if none of
  182. * -march/mtune/mcpu are already specified on the commandline
  183. */
  184. for (i = 1; i < argc; i++) {
  185. if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
  186. !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
  187. !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
  188. break;
  189. }
  190. if (i == argc) {
  191. #ifdef BR_ARCH
  192. *cur++ = "-march=" BR_ARCH;
  193. #endif
  194. #ifdef BR_CPU
  195. *cur++ = "-mcpu=" BR_CPU;
  196. #endif
  197. }
  198. #endif /* ARCH || CPU */
  199. paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
  200. if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
  201. paranoid = 1;
  202. else
  203. paranoid = 0;
  204. /* Check for unsafe library and header paths */
  205. for (i = 1; i < argc; i++) {
  206. /* Skip options that do not start with -I and -L */
  207. if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
  208. continue;
  209. /* We handle two cases: first the case where -I/-L and
  210. * the path are separated by one space and therefore
  211. * visible as two separate options, and then the case
  212. * where they are stuck together forming one single
  213. * option.
  214. */
  215. if (argv[i][2] == '\0') {
  216. i++;
  217. if (i == argc)
  218. continue;
  219. check_unsafe_path(argv[i], paranoid);
  220. } else {
  221. check_unsafe_path(argv[i] + 2, paranoid);
  222. }
  223. }
  224. /* append forward args */
  225. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  226. cur += argc - 1;
  227. /* finish with NULL termination */
  228. *cur = NULL;
  229. exec_args = args;
  230. #ifdef BR_CCACHE
  231. if (getenv("BR_NO_CCACHE"))
  232. /* Skip the ccache call */
  233. exec_args++;
  234. #endif
  235. /* Debug the wrapper to see actual arguments passed to
  236. * the compiler:
  237. * unset, empty, or 0: do not trace
  238. * set to 1 : trace all arguments on a single line
  239. * set to 2 : trace one argument per line
  240. */
  241. if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
  242. debug = atoi(env_debug);
  243. if (debug > 0) {
  244. fprintf(stderr, "Toolchain wrapper executing:");
  245. for (i = 0; exec_args[i]; i++)
  246. fprintf(stderr, "%s'%s'",
  247. (debug == 2) ? "\n " : " ", exec_args[i]);
  248. fprintf(stderr, "\n");
  249. }
  250. }
  251. if (execv(exec_args[0], exec_args))
  252. perror(path);
  253. free(args);
  254. return 2;
  255. }