ext-toolchain-wrapper.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Buildroot wrapper for external toolchains. This simply executes the real
  3. * toolchain with a number of arguments (sysroot/arch/..) hardcoded,
  4. * to ensure the external 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. static char path[PATH_MAX];
  25. static char sysroot[PATH_MAX];
  26. /**
  27. * GCC errors out with certain combinations of arguments (examples are
  28. * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
  29. * that we only pass the predefined one to the real compiler if the inverse
  30. * option isn't in the argument list.
  31. * This specifies the worst case number of extra arguments we might pass
  32. * Currently, we have:
  33. * -mfloat-abi=
  34. * -march=
  35. * -mcpu=
  36. */
  37. #define EXCLUSIVE_ARGS 3
  38. static char *predef_args[] = {
  39. path,
  40. "--sysroot", sysroot,
  41. #ifdef BR_ABI
  42. "-mabi=" BR_ABI,
  43. #endif
  44. #ifdef BR_FPU
  45. "-mfpu=" BR_FPU,
  46. #endif
  47. #ifdef BR_SOFTFLOAT
  48. "-msoft-float",
  49. #endif /* BR_SOFTFLOAT */
  50. #ifdef BR_MODE
  51. "-m" BR_MODE,
  52. #endif
  53. #ifdef BR_64
  54. "-m64",
  55. #endif
  56. #ifdef BR_BINFMT_FLAT
  57. "-Wl,-elf2flt",
  58. #endif
  59. #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
  60. "-EL",
  61. #endif
  62. #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
  63. "-EB",
  64. #endif
  65. #ifdef BR_ADDITIONAL_CFLAGS
  66. BR_ADDITIONAL_CFLAGS
  67. #endif
  68. };
  69. static void check_unsafe_path(const char *path, int paranoid)
  70. {
  71. char **c;
  72. static char *unsafe_paths[] = {
  73. "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
  74. };
  75. for (c = unsafe_paths; *c != NULL; c++) {
  76. if (!strncmp(path, *c, strlen(*c))) {
  77. fprintf(stderr, "%s: %s: unsafe header/library path used in cross-compilation: '%s'\n",
  78. program_invocation_short_name,
  79. paranoid ? "ERROR" : "WARNING", path);
  80. if (paranoid)
  81. exit(1);
  82. continue;
  83. }
  84. }
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. char **args, **cur;
  89. char *relbasedir, *absbasedir;
  90. char *progpath = argv[0];
  91. char *basename;
  92. char *env_debug;
  93. char *paranoid_wrapper;
  94. int paranoid;
  95. int ret, i, count = 0, debug;
  96. /* Calculate the relative paths */
  97. basename = strrchr(progpath, '/');
  98. if (basename) {
  99. *basename = '\0';
  100. basename++;
  101. relbasedir = malloc(strlen(progpath) + 7);
  102. if (relbasedir == NULL) {
  103. perror(__FILE__ ": malloc");
  104. return 2;
  105. }
  106. sprintf(relbasedir, "%s/../..", argv[0]);
  107. absbasedir = realpath(relbasedir, NULL);
  108. } else {
  109. basename = progpath;
  110. absbasedir = malloc(PATH_MAX + 1);
  111. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  112. if (ret < 0) {
  113. perror(__FILE__ ": readlink");
  114. return 2;
  115. }
  116. absbasedir[ret] = '\0';
  117. for (i = ret; i > 0; i--) {
  118. if (absbasedir[i] == '/') {
  119. absbasedir[i] = '\0';
  120. if (++count == 3)
  121. break;
  122. }
  123. }
  124. }
  125. if (absbasedir == NULL) {
  126. perror(__FILE__ ": realpath");
  127. return 2;
  128. }
  129. /* Fill in the relative paths */
  130. #ifdef BR_CROSS_PATH_REL
  131. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  132. #else /* BR_CROSS_PATH_ABS */
  133. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  134. #endif
  135. if (ret >= sizeof(path)) {
  136. perror(__FILE__ ": overflow");
  137. return 3;
  138. }
  139. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  140. if (ret >= sizeof(sysroot)) {
  141. perror(__FILE__ ": overflow");
  142. return 3;
  143. }
  144. cur = args = malloc(sizeof(predef_args) +
  145. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  146. if (args == NULL) {
  147. perror(__FILE__ ": malloc");
  148. return 2;
  149. }
  150. /* start with predefined args */
  151. memcpy(cur, predef_args, sizeof(predef_args));
  152. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  153. #ifdef BR_FLOAT_ABI
  154. /* add float abi if not overridden in args */
  155. for (i = 1; i < argc; i++) {
  156. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  157. !strcmp(argv[i], "-msoft-float") ||
  158. !strcmp(argv[i], "-mhard-float"))
  159. break;
  160. }
  161. if (i == argc)
  162. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  163. #endif
  164. #if defined(BR_ARCH) || \
  165. defined(BR_CPU)
  166. /* Add our -march/cpu flags, but only if none of
  167. * -march/mtune/mcpu are already specified on the commandline
  168. */
  169. for (i = 1; i < argc; i++) {
  170. if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
  171. !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
  172. !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
  173. break;
  174. }
  175. if (i == argc) {
  176. #ifdef BR_ARCH
  177. *cur++ = "-march=" BR_ARCH;
  178. #endif
  179. #ifdef BR_CPU
  180. *cur++ = "-mcpu=" BR_CPU;
  181. #endif
  182. }
  183. #endif /* ARCH || CPU */
  184. paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
  185. if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
  186. paranoid = 1;
  187. else
  188. paranoid = 0;
  189. /* Check for unsafe library and header paths */
  190. for (i = 1; i < argc; i++) {
  191. /* Skip options that do not start with -I and -L */
  192. if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
  193. continue;
  194. /* We handle two cases: first the case where -I/-L and
  195. * the path are separated by one space and therefore
  196. * visible as two separate options, and then the case
  197. * where they are stuck together forming one single
  198. * option.
  199. */
  200. if (argv[i][2] == '\0') {
  201. i++;
  202. if (i == argc)
  203. continue;
  204. check_unsafe_path(argv[i], paranoid);
  205. } else {
  206. check_unsafe_path(argv[i] + 2, paranoid);
  207. }
  208. }
  209. /* append forward args */
  210. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  211. cur += argc - 1;
  212. /* finish with NULL termination */
  213. *cur = NULL;
  214. /* Debug the wrapper to see actual arguments passed to
  215. * the compiler:
  216. * unset, empty, or 0: do not trace
  217. * set to 1 : trace all arguments on a single line
  218. * set to 2 : trace one argument per line
  219. */
  220. if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
  221. debug = atoi(env_debug);
  222. if (debug > 0) {
  223. fprintf(stderr, "Toolchain wrapper executing:");
  224. for (i = 0; args[i]; i++)
  225. fprintf(stderr, "%s'%s'",
  226. (debug == 2) ? "\n " : " ", args[i]);
  227. fprintf(stderr, "\n");
  228. }
  229. }
  230. if (execv(path, args))
  231. perror(path);
  232. free(args);
  233. return 2;
  234. }