gcc-uClibc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (C) 2000 Manuel Novoa III
  3. * Copyright (C) 2002 Erik Andersen
  4. *
  5. * This is a crude wrapper to use uClibc with gcc.
  6. * It was originally written to work around ./configure for ext2fs-utils.
  7. * It certainly can be improved, but it works for me in the normal cases.
  8. *
  9. * April 7, 2001
  10. *
  11. * A bug was fixed in building the gcc command line when dynamic linking.
  12. * The functions dlopen, etc. now work. At this time, you must make sure
  13. * the correct libdl.so is included however. It is safest to, for example,
  14. * add /lib/libdl.so.1 if using ld-linux.so.1 rather than adding -ldl to the
  15. * command line.
  16. *
  17. * Note: This is only a problem if devel and target archs are the same. To
  18. * avoid the problem, you can use a customized dynamic linker.
  19. *
  20. *
  21. * April 18, 2001
  22. *
  23. * The wrapper now works with either installed and uninstalled uClibc versions.
  24. * If you want to use the uninstalled header files and libs, either include
  25. * the string "build" in the invocation name such as
  26. * 'ln -s <ARCH>-uclibc-gcc <ARCH>-uclibc-gcc-build'
  27. * or include it in the environment variable setting of UCLIBC_GCC.
  28. * Note: This automatically enables the "rpath" behavior described below.
  29. *
  30. * The wrapper will now pass the location of the uClibc shared libs used to
  31. * the linker with the "-rpath" option if the invocation name includes the
  32. * string "rpath" or if the environment variable UCLIBC_GCC include it (as
  33. * with "build" above). This is primarily intended to be used on devel
  34. * platforms of the same arch as the target. A good place to use this feature
  35. * would be in the uClibc/test directory.
  36. *
  37. * The wrapper now displays the command line passed to gcc when '-v' is used.
  38. *
  39. * May 31, 2001
  40. *
  41. * "rpath" and "build" behavior are now decoupled. You can of course get
  42. * the old "build" behavior by setting UCLIBC_GCC="rpath-build". Order
  43. * isn't important here, as only the substrings are searched for.
  44. *
  45. * Added environment variable check for UCLIBC_GCC_DLOPT to let user specify
  46. * an alternative dynamic linker at runtime without using command line args.
  47. * Since this wouldn't commonly be used, I made it easy on myself. You have
  48. * to match the option you would have passed to the gcc wrapper. As an
  49. * example,
  50. *
  51. * export UCLIBC_GCC_DLOPT="-Wl,--dynamic-linker,/lib/ld-alt-linker.so.3"
  52. *
  53. * This is really only useful if target arch == devel arch and DEVEL_PREFIX
  54. * isn't empty. It involves a recompile, but you can at least test apps
  55. * on your devel system if combined with the "rpath" behavor if by using
  56. * LD_LIBRARY_PATH, etc.
  57. *
  58. * Also added check for "-Wl,--dynamic-linker" on the command line. The
  59. * use default dynamic linker or the envirnment-specified dynamic linker
  60. * is disabled in that case.
  61. *
  62. * Added options --uclibc-use-build-dir and --uclibc-use-rpath so that those
  63. * behaviors can be invoked from the command line.
  64. *
  65. */
  66. /*
  67. *
  68. * TODO:
  69. * Check/modify gcc-specific environment variables?
  70. */
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <stdarg.h>
  74. #include <string.h>
  75. #include <unistd.h>
  76. #include <libgen.h>
  77. #include "gcc-uClibc.h"
  78. static char *our_usr_lib_path = "-L"UCLIBC_DEVEL_PREFIX"/lib";
  79. static char static_linking[] = "-static";
  80. static char nostdinc[] = "-nostdinc";
  81. static char nostartfiles[] = "-nostartfiles";
  82. static char nodefaultlibs[] = "-nodefaultlibs";
  83. static char nostdlib[] = "-nostdlib";
  84. #ifdef __UCLIBC_CTOR_DTOR__
  85. static char nostdinc_plus[] = "-nostdinc++";
  86. #endif
  87. extern void *xmalloc(size_t size)
  88. {
  89. void *ptr = malloc(size);
  90. if (!ptr) {
  91. fprintf(stderr, "memory exhausted");
  92. exit(EXIT_FAILURE);
  93. }
  94. return ptr;
  95. }
  96. void xstrcat(char **string, ...)
  97. {
  98. const char *c;
  99. va_list p;
  100. /* Don't bother to calculate how big exerything
  101. * will be, just be careful to not overflow... */
  102. va_start(p, string);
  103. *string = xmalloc(BUFSIZ);
  104. **string = '\0';
  105. while(1) {
  106. if (!(c = va_arg(p, const char *)))
  107. break;
  108. strcat(*string, c);
  109. }
  110. va_end(p);
  111. }
  112. int main(int argc, char **argv)
  113. {
  114. int use_build_dir = 0, linking = 1, use_static_linking = 0;
  115. int use_stdinc = 1, use_start = 1, use_stdlib = 1, use_pic = 0;
  116. int source_count = 0, use_rpath = 0, verbose = 0;
  117. int i, j, k, l, m, n;
  118. char ** gcc_argv;
  119. char ** gcc_argument;
  120. char ** libraries;
  121. char ** libpath;
  122. char *dlstr;
  123. char *incstr;
  124. char *devprefix;
  125. char *builddir;
  126. char *libstr;
  127. char *build_dlstr = 0;
  128. char *ep;
  129. char *rpath_link[2];
  130. char *rpath[2];
  131. char *uClibc_inc[2];
  132. char *our_lib_path[2];
  133. char *crt0_path[2];
  134. const char *application_name;
  135. #ifdef __UCLIBC_CTOR_DTOR__
  136. char *crti_path[2];
  137. char *crtn_path[2];
  138. int len;
  139. int ctor_dtor = 1, cplusplus = 0, use_nostdinc_plus = 0;
  140. char *GPLUSPLUS_BIN = NULL;
  141. #endif
  142. #ifdef __UCLIBC_PROFILING__
  143. int profile = 0;
  144. char *gcrt1_path[2];
  145. #endif
  146. application_name = basename(argv[0]);
  147. if (application_name[0] == '-')
  148. application_name++;
  149. #ifdef __UCLIBC_CTOR_DTOR__
  150. /* We must use strstr since g++ might be named like a
  151. * cross compiler (i.e. arm-linux-g++). We must also
  152. * search carefully, in case we are searching something
  153. * like /opt/c++/gcc-3.1/bin/arm-linux-g++ or some similar
  154. * perversion... */
  155. len = strlen(application_name);
  156. if ((strcmp(application_name+len-3, "g++")==0) ||
  157. (strcmp(application_name+len-3, "c++")==0)) {
  158. char *gcc_bin = GCC_BIN;
  159. len = strlen(gcc_bin);
  160. if (strcmp(gcc_bin+len-3, "gcc")==0) {
  161. GPLUSPLUS_BIN = strdup(gcc_bin);
  162. GPLUSPLUS_BIN[len-1]='+';
  163. GPLUSPLUS_BIN[len-2]='+';
  164. }
  165. cplusplus = 1;
  166. use_nostdinc_plus = 1;
  167. }
  168. #endif
  169. devprefix = getenv("UCLIBC_DEVEL_PREFIX");
  170. if (!devprefix) {
  171. devprefix = UCLIBC_DEVEL_PREFIX;
  172. }
  173. builddir = getenv("UCLIBC_BUILD_DIR");
  174. if (!builddir) {
  175. builddir = UCLIBC_BUILD_DIR;
  176. }
  177. incstr = getenv("UCLIBC_GCC_INC");
  178. libstr = getenv("UCLIBC_GCC_LIB");
  179. xstrcat(&(rpath_link[0]), "-Wl,-rpath-link,", devprefix, "/lib", NULL);
  180. xstrcat(&(rpath_link[1]), "-Wl,-rpath-link,", builddir, "/lib", NULL);
  181. xstrcat(&(rpath[0]), "-Wl,-rpath,", devprefix, "/lib", NULL);
  182. xstrcat(&(rpath[1]), "-Wl,-rpath,", builddir, "/lib", NULL);
  183. xstrcat(&(uClibc_inc[0]), devprefix, "/include/", NULL);
  184. xstrcat(&(uClibc_inc[1]), builddir, "/include/", NULL);
  185. #ifdef __UCLIBC_CTOR_DTOR__
  186. xstrcat(&(crt0_path[0]), devprefix, "/lib/crt1.o", NULL);
  187. xstrcat(&(crt0_path[1]), builddir, "/lib/crt1.o", NULL);
  188. xstrcat(&(crti_path[0]), devprefix, "/lib/crti.o", NULL);
  189. xstrcat(&(crti_path[1]), builddir, "/lib/crti.o", NULL);
  190. xstrcat(&(crtn_path[0]), devprefix, "/lib/crtn.o", NULL);
  191. xstrcat(&(crtn_path[1]), builddir, "/lib/crtn.o", NULL);
  192. #else
  193. xstrcat(&(crt0_path[0]), devprefix, "/lib/crt0.o", NULL);
  194. xstrcat(&(crt0_path[1]), builddir, "/lib/crt0.o", NULL);
  195. #endif
  196. #ifdef __UCLIBC_PROFILING__
  197. xstrcat(&(gcrt1_path[0]), devprefix, "/lib/gcrt1.o", NULL);
  198. xstrcat(&(gcrt1_path[1]), builddir, "/lib/gcrt1.o", NULL);
  199. #endif
  200. xstrcat(&(our_lib_path[0]), "-L", devprefix, "/lib", NULL);
  201. xstrcat(&(our_lib_path[1]), "-L", builddir, "/lib", NULL);
  202. #ifdef __UCLIBC_HAS_SHARED__
  203. build_dlstr = "-Wl,--dynamic-linker," BUILD_DYNAMIC_LINKER;
  204. dlstr = getenv("UCLIBC_GCC_DLOPT");
  205. if (!dlstr) {
  206. dlstr = "-Wl,--dynamic-linker," DYNAMIC_LINKER;
  207. }
  208. #endif
  209. ep = getenv("UCLIBC_GCC");
  210. if (!ep) {
  211. ep = "";
  212. }
  213. if (strstr(ep,"build") != 0) {
  214. use_build_dir = 1;
  215. }
  216. if (strstr(ep,"rpath") != 0) {
  217. use_rpath = 1;
  218. }
  219. m = 0;
  220. libraries = __builtin_alloca(sizeof(char*) * (argc));
  221. libraries[m] = '\0';
  222. n = 0;
  223. libpath = __builtin_alloca(sizeof(char*) * (argc));
  224. libpath[n] = '\0';
  225. for ( i = 1 ; i < argc ; i++ ) {
  226. if (argv[i][0] == '-') { /* option */
  227. switch (argv[i][1]) {
  228. case 'c': /* compile or assemble */
  229. case 'S': /* generate assembler code */
  230. case 'E': /* preprocess only */
  231. case 'M': /* generate dependencies */
  232. linking = 0;
  233. break;
  234. case 'L': /* library */
  235. libpath[n++] = argv[i];
  236. libpath[n] = '\0';
  237. if (argv[i][2] == 0) {
  238. argv[i] = '\0';
  239. libpath[n++] = argv[++i];
  240. libpath[n] = '\0';
  241. }
  242. argv[i] = '\0';
  243. break;
  244. case 'l': /* library */
  245. libraries[m++] = argv[i];
  246. libraries[m] = '\0';
  247. argv[i] = '\0';
  248. break;
  249. case 'v': /* verbose */
  250. if (argv[i][2] == 0) verbose = 1;
  251. printf("Invoked as %s\n", argv[0]);
  252. break;
  253. case 'n':
  254. if (strcmp(nostdinc,argv[i]) == 0) {
  255. use_stdinc = 0;
  256. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  257. #ifdef __UCLIBC_CTOR_DTOR__
  258. ctor_dtor = 0;
  259. #endif
  260. use_start = 0;
  261. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  262. use_stdlib = 0;
  263. argv[i] = '\0';
  264. } else if (strcmp(nostdlib,argv[i]) == 0) {
  265. #ifdef __UCLIBC_CTOR_DTOR__
  266. ctor_dtor = 0;
  267. #endif
  268. use_start = 0;
  269. use_stdlib = 0;
  270. }
  271. #ifdef __UCLIBC_CTOR_DTOR__
  272. else if (strcmp(nostdinc_plus,argv[i]) == 0) {
  273. if (cplusplus==1) {
  274. use_nostdinc_plus = 0;
  275. }
  276. }
  277. #endif
  278. break;
  279. case 's':
  280. if (strstr(argv[i],static_linking) != NULL) {
  281. use_static_linking = 1;
  282. }
  283. if (strcmp("-shared",argv[i]) == 0) {
  284. use_start = 0;
  285. use_pic = 1;
  286. }
  287. break;
  288. case 'W': /* -static could be passed directly to ld */
  289. if (strncmp("-Wl,",argv[i],4) == 0) {
  290. if (strstr(argv[i],static_linking) != 0) {
  291. use_static_linking = 1;
  292. }
  293. if (strstr(argv[i],"--dynamic-linker") != 0) {
  294. dlstr = 0;
  295. }
  296. }
  297. break;
  298. #ifdef __UCLIBC_PROFILING__
  299. case 'p':
  300. if (strcmp("-pg",argv[i]) == 0) {
  301. profile = 1;
  302. }
  303. break;
  304. #endif
  305. case 'f':
  306. /* Check if we are doing PIC */
  307. if (strcmp("-fPIC",argv[i]) == 0) {
  308. use_pic = 1;
  309. } else if (strcmp("-fpic",argv[i]) == 0) {
  310. use_pic = 1;
  311. }
  312. #ifdef __UCLIBC_PROFILING__
  313. else if (strcmp("-fprofile-arcs",argv[i]) == 0) {
  314. profile = 1;
  315. }
  316. #endif
  317. break;
  318. case '-':
  319. if (strstr(argv[i]+1,static_linking) != NULL) {
  320. use_static_linking = 1;
  321. argv[i]='\0';
  322. } else if (strcmp("--uclibc-use-build-dir",argv[i]) == 0) {
  323. use_build_dir = 1;
  324. argv[i]='\0';
  325. } else if (strcmp("--uclibc-use-rpath",argv[i]) == 0) {
  326. use_rpath = 1;
  327. argv[i]='\0';
  328. }
  329. #ifdef __UCLIBC_CTOR_DTOR__
  330. else if (strcmp("--uclibc-no-ctors",argv[i]) == 0) {
  331. ctor_dtor = 0;
  332. argv[i]='\0';
  333. }
  334. #endif
  335. break;
  336. }
  337. } else { /* assume it is an existing source file */
  338. ++source_count;
  339. }
  340. }
  341. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 64));
  342. gcc_argument = __builtin_alloca(sizeof(char*) * (argc + 20));
  343. i = 0; k = 0;
  344. #ifdef __UCLIBC_CTOR_DTOR__
  345. if (cplusplus && GPLUSPLUS_BIN)
  346. gcc_argv[i++] = GPLUSPLUS_BIN;
  347. else
  348. #endif
  349. gcc_argv[i++] = GCC_BIN;
  350. for ( j = 1 ; j < argc ; j++ ) {
  351. if (argv[j]=='\0') {
  352. continue;
  353. } else {
  354. gcc_argument[k++] = argv[j];
  355. gcc_argument[k] = '\0';
  356. }
  357. }
  358. if (linking && source_count) {
  359. #if defined __HAVE_ELF__ && ! defined __UCLIBC_HAS_MMU__
  360. gcc_argv[i++] = "-Wl,-elf2flt";
  361. #endif
  362. gcc_argv[i++] = nostdlib;
  363. if (use_static_linking) {
  364. gcc_argv[i++] = static_linking;
  365. }
  366. if (!use_static_linking) {
  367. if (dlstr && use_build_dir) {
  368. gcc_argv[i++] = build_dlstr;
  369. } else if (dlstr) {
  370. gcc_argv[i++] = dlstr;
  371. }
  372. if (use_rpath) {
  373. gcc_argv[i++] = rpath[use_build_dir];
  374. }
  375. }
  376. for ( l = 0 ; l < n ; l++ ) {
  377. if (libpath[l]) gcc_argv[i++] = libpath[l];
  378. }
  379. gcc_argv[i++] = rpath_link[use_build_dir]; /* just to be safe */
  380. if( libstr )
  381. gcc_argv[i++] = libstr;
  382. gcc_argv[i++] = our_lib_path[use_build_dir];
  383. if (!use_build_dir) {
  384. gcc_argv[i++] = our_usr_lib_path;
  385. }
  386. }
  387. if (use_stdinc && source_count) {
  388. gcc_argv[i++] = nostdinc;
  389. #ifdef __UCLIBC_CTOR_DTOR__
  390. if (cplusplus) {
  391. char *cppinc;
  392. if (use_nostdinc_plus) {
  393. gcc_argv[i++] = nostdinc_plus;
  394. }
  395. xstrcat(&cppinc, uClibc_inc[use_build_dir], "g++/", NULL);
  396. gcc_argv[i++] = "-isystem";
  397. gcc_argv[i++] = cppinc;
  398. xstrcat(&cppinc, uClibc_inc[use_build_dir], "g++-v3/", NULL);
  399. gcc_argv[i++] = "-isystem";
  400. gcc_argv[i++] = cppinc;
  401. }
  402. #endif
  403. gcc_argv[i++] = "-isystem";
  404. gcc_argv[i++] = uClibc_inc[use_build_dir];
  405. gcc_argv[i++] = "-iwithprefix";
  406. gcc_argv[i++] = "include";
  407. if( incstr )
  408. gcc_argv[i++] = incstr;
  409. }
  410. if (linking && source_count) {
  411. #ifdef __UCLIBC_PROFILING__
  412. if (profile) {
  413. gcc_argv[i++] = gcrt1_path[use_build_dir];
  414. }
  415. #endif
  416. #ifdef __UCLIBC_CTOR_DTOR__
  417. if (ctor_dtor) {
  418. gcc_argv[i++] = crti_path[use_build_dir];
  419. if (use_pic) {
  420. gcc_argv[i++] = LIBGCC_DIR "crtbeginS.o" ;
  421. } else {
  422. gcc_argv[i++] = LIBGCC_DIR "crtbegin.o" ;
  423. }
  424. }
  425. #endif
  426. if (use_start) {
  427. #ifdef __UCLIBC_PROFILING__
  428. if (!profile)
  429. #endif
  430. {
  431. gcc_argv[i++] = crt0_path[use_build_dir];
  432. }
  433. }
  434. for ( l = 0 ; l < k ; l++ ) {
  435. if (gcc_argument[l]) gcc_argv[i++] = gcc_argument[l];
  436. }
  437. if (use_stdlib) {
  438. //gcc_argv[i++] = "-Wl,--start-group";
  439. gcc_argv[i++] = "-lgcc";
  440. }
  441. for ( l = 0 ; l < m ; l++ ) {
  442. if (libraries[l]) gcc_argv[i++] = libraries[l];
  443. }
  444. if (use_stdlib) {
  445. #ifdef __UCLIBC_CTOR_DTOR__
  446. if (cplusplus) {
  447. gcc_argv[ i++ ] = "-lstdc++";
  448. gcc_argv[ i++ ] = "-lm";
  449. }
  450. #endif
  451. gcc_argv[i++] = "-lc";
  452. gcc_argv[i++] = "-lgcc";
  453. //gcc_argv[i++] = "-Wl,--end-group";
  454. }
  455. #ifdef __UCLIBC_CTOR_DTOR__
  456. if (ctor_dtor) {
  457. if (use_pic) {
  458. gcc_argv[i++] = LIBGCC_DIR "crtendS.o" ;
  459. } else {
  460. gcc_argv[i++] = LIBGCC_DIR "crtend.o" ;
  461. }
  462. gcc_argv[i++] = crtn_path[use_build_dir];
  463. }
  464. #endif
  465. } else {
  466. for ( l = 0 ; l < k ; l++ ) {
  467. if (gcc_argument[l]) gcc_argv[i++] = gcc_argument[l];
  468. }
  469. }
  470. gcc_argv[i++] = NULL;
  471. if (verbose) {
  472. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  473. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  474. }
  475. fflush(stdout);
  476. }
  477. //no need to free memory from xstrcat because we never return...
  478. #ifdef __UCLIBC_CTOR_DTOR__
  479. if (cplusplus && GPLUSPLUS_BIN)
  480. return execvp(GPLUSPLUS_BIN, gcc_argv);
  481. else
  482. #endif
  483. return execvp(GCC_BIN, gcc_argv);
  484. }