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
|
/*
* Copyright (c) 2002-2003 Marius Strobl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Wrapper for Intel(R) C/C++ Compiler for Linux to allow linking of native
* FreeBSD binaries.
* Based on a shell-script written by Dan Nelson <dnelson@allantgroup.com>
* with some modifications by Alexander Leidinger <netchild@FreeBSD.org>.
*
* $FreeBSD$
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined (__FreeBSD__) && __FreeBSD__ >= 2
#include <osreldate.h>
#else
#error "Won't work here."
#endif
#define PATH_LD "/usr/bin/ld"
struct arglist {
size_t argc;
const char **argv;
};
static void addarg(struct arglist *al, const char *arg);
int main(int argc, char *argv[], char *envp[]);
static void
addarg(struct arglist *al, const char *arg)
{
const char **argv2;
argv2 = realloc(al->argv, (al->argc + 1)* sizeof(al->argv[0]));
if (argv2 == NULL)
err(1, NULL);
al->argv = argv2;
al->argv[al->argc++] = arg;
}
int
main(int argc, char *argv[], char *envp[])
{
size_t i;
int bootstrap, cpp, dynamic, pic, gprof, stlinserted, threaded;
const char *libc, *libc_r, *prefix;
struct arglist al = { 0, NULL };
if (argc == 1)
errx(1, "no input files");
if ((prefix = getenv("PREFIX")) == NULL)
errx(1, "can't get PREFIX");
bootstrap = cpp = dynamic = pic = gprof = stlinserted = threaded = 0;
#ifdef DEBUG
printf("input: ");
#endif
#define ARGCMP(x) !strcmp(argv[i], (x))
/*
* XXX This doesn't deal with whitespace but a) the output of the
* compiler should be fixed and b) the real linker is also picky
* about whitespace.
*/
for (i = 0; i < argc; i++) {
#ifdef DEBUG
printf("%s ", argv[i]);
#endif
if (ARGCMP("-CPLUSPLUS")) {
cpp++;
continue;
}
if (ARGCMP("-BOOTSTRAPSTLPORT")) {
bootstrap++;
continue;
}
if (ARGCMP("-MT")) {
threaded++;
continue;
}
if (ARGCMP("-PIC")) {
pic++;
continue;
}
/*
* If the compiler was called with -static we shouldn't see
* "--dynamic-linker" here.
* Note: According to ld(1) this is "--dynamic-linker" but
* ICC passes "-dynamic-linker" to it.
*/
if (ARGCMP("--dynamic-linker") || ARGCMP("-dynamic-linker")) {
dynamic++;
continue;
}
if (ARGCMP("-shared")) {
dynamic++;
continue;
}
/*
* Just link libstlport_icc* once when compiling the stlport
* tests.
*/
if (ARGCMP("-lstlport_icc")) {
stlinserted++;
continue;
}
/*
* Link against libc_p when "-qp" or "-p" were given,
* "/usr/lib/gcrt1.o" indicates this.
*/
if (ARGCMP("/usr/lib/gcrt1.o")) {
gprof++;
continue;
}
}
/*
* Allow the user to specify an alternative threads library
* implementation, such as -lthr, or whatever
*/
if ((libc_r = getenv("PTHREAD_LIBS")) == NULL)
libc_r = "-lc_r";
/*
* Use the appropriate libs for libc and libc_r when linking static
* and "-KPIC" or "-pg" where given.
*/
if (!dynamic && (pic || gprof)) {
/*
* Let libc_p win above libc_pic when both, "-KPIC" and "-pg",
* where given, GCC does the same.
*/
if (!gprof)
libc = "-lc_pic";
else {
char *p;
libc = "-lc_p";
asprintf(&p, "%s_p", libc_r);
if (p == NULL)
err(1, NULL);
libc_r = p;
}
} else
libc = "-lc";
#ifdef DEBUG
printf("\ncpp: %s bootstrap: %s dynamic: %s gprof: %s pic: %s "
"threaded: %s\n", cpp ? "YES" : "NO", bootstrap ? "YES" : "NO",
dynamic ? "YES" : "NO", gprof ? "YES" : "NO", pic ? "YES" : "NO",
threaded ? "YES" : "NO");
#endif
if (bootstrap && !cpp)
errx(1, "-BOOTSTRAPSTLPORT is only valid in combination with "
"-CPLUSPLUS");
for (i = 0; i < argc; i++) {
if (ARGCMP("-CPLUSPLUS") || ARGCMP("-BOOTSTRAPSTLPORT") ||
ARGCMP("-MT") || ARGCMP("-PIC"))
continue;
/* prepend "-melf_i386" to the commandline */
if (i == 0) {
addarg(&al, argv[0]);
#if __FreeBSD_version < 500042
addarg(&al, "-melf_i386");
#else
addarg(&al, "-melf_i386_fbsd");
#endif
continue;
}
/* Don't add obsolete flag "-Qy". */
if (ARGCMP("-Qy"))
continue;
/* Libunwind is only needed when compiling C++ source. */
if (!cpp && ARGCMP("-lunwind"))
continue;
/*
* Replace libcprts with libstlport_icc. The Dinkumware STL
* shipping with ICC has unresolvable glibc dependencies
* in both, the static and the dynamic, versions.
*/
if (ARGCMP("-lcprts")) {
if (cpp && !bootstrap && !stlinserted) {
addarg(&al, dynamic ? "-Bdynamic" : "-Bstatic");
addarg(&al, "-lstlport_icc");
stlinserted++;
}
continue;
}
/*
* Link against libc_r when compiling multi-threaded or C++
* code (libcxa and libunwind depend on libc_r when compiling
* C++ source).
*/
if (ARGCMP("-lc")) {
if (al.argc > 0 &&
strncmp(al.argv[al.argc - 1], "-B", 2))
addarg(&al, dynamic ? "-Bdynamic" : "-Bstatic");
if (cpp || threaded) {
addarg(&al, libc_r);
#if __FreeBSD_version >= 500016
addarg(&al, dynamic ? "-Bdynamic" : "-Bstatic");
addarg(&al, libc);
#endif
} else
addarg(&al, libc);
continue;
}
/* Switch Linux stuff to FreeBSD counterparts. */
if (ARGCMP("/lib/ld-linux.so.2")) {
#if __FreeBSD_version >= 501105
addarg(&al, "/libexec/ld-elf.so.1");
#else
addarg(&al, "/usr/libexec/ld-elf.so.1");
#endif
continue;
}
if (ARGCMP("-L/usr/lib")) {
char *temp;
addarg(&al, "-L/usr/libexec/elf");
addarg(&al, "-L/usr/libexec");
addarg(&al, "-L/usr/lib");
asprintf(&temp, "-L%s/lib", prefix);
if (temp == NULL)
err(1, NULL);
addarg(&al, temp);
continue;
}
/*
* Force libcxa and libunwind to static linkage, since the
* dynamic versions have glibc dependencies.
* Don't add superfluous -Bdynamic.
*/
if (ARGCMP("-Bdynamic") && i < argc - 1) {
if (!strcmp(argv[i + 1], "-lcxa") ||
(cpp && !strcmp(argv[i + 1], "-lunwind"))) {
addarg(&al, "-Bstatic");
continue;
}
if (!strcmp(argv[i + 1], "-lcprts") ||
!strcmp(argv[i + 1], "-lunwind"))
continue;
}
/* Don't add superfluous -Bstatic. */
if (ARGCMP("-Bstatic") && i < argc - 1 &&
(!strcmp(argv[i + 1], "-lcprts") ||
(!cpp && !strcmp(argv[i + 1], "-lunwind"))))
continue;
/*
* Sanity check if every lib is prepended by a linkage option,
* add if missing.
*/
if (!strncmp(argv[i], "-l", 2) && al.argc > 0 &&
strncmp(al.argv[al.argc - 1], "-B", 2)) {
if (!strcmp(argv[i], "-lcxa") ||
!strcmp(argv[i], "-limf") ||
!strcmp(argv[i], "-lirc") ||
!strcmp(argv[i], "-lircmt") ||
!strcmp(argv[i], "-lunwind"))
addarg(&al, "-Bstatic");
else
addarg(&al, dynamic ? "-Bdynamic" : "-Bstatic");
addarg(&al, argv[i]);
continue;
}
/* default */
addarg(&al, argv[i]);
}
#undef ARGCMP
/* Still something to do ? */
if (al.argc == 1)
errx(1, "no input files");
#ifdef DEBUG
printf("output: ");
for (i = 0; i < al.argc; i++)
printf("%s ", al.argv[i]);
printf("\n");
#endif
addarg(&al, NULL);
/* Launch the real linker */
if (execve(PATH_LD, (char **)al.argv, envp) == -1)
err(1, "execing " PATH_LD);
exit (1);
}
|