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