-
Notifications
You must be signed in to change notification settings - Fork 4
/
collect2.c
3484 lines (2905 loc) · 88.3 KB
/
collect2.c
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Collect static initialization info into data structures that can be
traversed by C++ initialization and finalization routines.
Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
Contributed by Chris Smith ([email protected]).
Heavily modified by Michael Meissner ([email protected]),
Per Bothner ([email protected]), and John Gilmore ([email protected]).
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* Build tables of static constructors and destructors and run ld. */
#include "config.h"
#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/stat.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#define COLLECT
#include "demangle.h"
#include "obstack.h"
#include "gansidecl.h"
#ifndef errno
extern int errno;
#endif
#ifndef HAVE_STRERROR
extern char *sys_errlist[];
extern int sys_nerr;
#else
char *strerror();
#endif
/* Obstack allocation and deallocation routines. */
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
#ifdef USG
#define vfork fork
#endif
#ifndef R_OK
#define R_OK 4
#define W_OK 2
#define X_OK 1
#endif
#ifndef WIFSIGNALED
#define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
#endif
#ifndef WTERMSIG
#define WTERMSIG(S) ((S) & 0x7f)
#endif
#ifndef WIFEXITED
#define WIFEXITED(S) (((S) & 0xff) == 0)
#endif
#ifndef WEXITSTATUS
#define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
#endif
extern char *choose_temp_base ();
/* On certain systems, we have code that works by scanning the object file
directly. But this code uses system-specific header files and library
functions, so turn it off in a cross-compiler. Likewise, the names of
the utilities aren't correct for a cross-compiler; we have to hope that
cross-versions are in the proper directories. */
#ifdef CROSS_COMPILE
#undef SUNOS4_SHARED_LIBRARIES
#undef OBJECT_FORMAT_COFF
#undef OBJECT_FORMAT_ROSE
#undef MD_EXEC_PREFIX
#undef REAL_LD_FILE_NAME
#undef REAL_NM_FILE_NAME
#undef REAL_STRIP_FILE_NAME
#endif
/* If we can't use a special method, use the ordinary one:
run nm to find what symbols are present.
In a cross-compiler, this means you need a cross nm,
but that isn't quite as unpleasant as special headers. */
#if !defined (OBJECT_FORMAT_COFF) && !defined (OBJECT_FORMAT_ROSE)
#define OBJECT_FORMAT_NONE
#endif
#ifdef OBJECT_FORMAT_COFF
#include <a.out.h>
#include <ar.h>
#ifdef UMAX
#include <sgs.h>
#endif
/* Many versions of ldfcn.h define these. */
#ifdef FREAD
#undef FREAD
#undef FWRITE
#endif
#include <ldfcn.h>
/* Some systems have an ISCOFF macro, but others do not. In some cases
the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
that either do not have an ISCOFF macro in /usr/include or for those
where it is wrong. */
#ifndef MY_ISCOFF
#define MY_ISCOFF(X) ISCOFF (X)
#endif
#ifdef XCOFF_DEBUGGING_INFO
#define XCOFF_SCAN_LIBS
#endif
#endif /* OBJECT_FORMAT_COFF */
#ifdef OBJECT_FORMAT_ROSE
#ifdef _OSF_SOURCE
#define USE_MMAP
#endif
#ifdef USE_MMAP
#include <sys/mman.h>
#endif
#include <unistd.h>
#include <mach_o_format.h>
#include <mach_o_header.h>
#include <mach_o_vals.h>
#include <mach_o_types.h>
#endif /* OBJECT_FORMAT_ROSE */
#ifdef OBJECT_FORMAT_NONE
/* Default flags to pass to nm. */
#ifndef NM_FLAGS
#define NM_FLAGS "-p"
#endif
#endif /* OBJECT_FORMAT_NONE */
/* Some systems use __main in a way incompatible with its use in gcc, in these
cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
give the same symbol without quotes for an alternative entry point. You
must define both, or neither. */
#ifndef NAME__MAIN
#define NAME__MAIN "__main"
#define SYMBOL__MAIN __main
#endif
#if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES || defined(XCOFF_SCAN_LIBS)
#define SCAN_LIBRARIES
#endif
#ifdef USE_COLLECT2
int do_collecting = 1;
#else
int do_collecting = 0;
#endif
/* Linked lists of constructor and destructor names. */
struct id
{
struct id *next;
int sequence;
char name[1];
};
struct head
{
struct id *first;
struct id *last;
int number;
};
/* Enumeration giving which pass this is for scanning the program file. */
enum pass {
PASS_FIRST, /* without constructors */
PASS_OBJ, /* individual objects */
PASS_LIB, /* looking for shared libraries */
PASS_SECOND /* with constructors linked in */
};
#ifndef NO_SYS_SIGLIST
#ifndef SYS_SIGLIST_DECLARED
extern char *sys_siglist[];
#endif
#endif
extern char *version_string;
int vflag; /* true if -v */
static int rflag; /* true if -r */
static int strip_flag; /* true if -s */
int debug; /* true if -debug */
static int shared_obj; /* true if -shared */
static int temp_filename_length; /* Length of temp_filename */
static char *temp_filename; /* Base of temp filenames */
static char *c_file; /* <xxx>.c for constructor/destructor list. */
static char *o_file; /* <xxx>.o for constructor/destructor list. */
static char *export_file; /* <xxx>.x for AIX export list. */
char *ldout; /* File for ld errors. */
static char *output_file; /* Output file for ld. */
static char *nm_file_name; /* pathname of nm */
static char *ldd_file_name; /* pathname of ldd (or equivalent) */
static char *strip_file_name; /* pathname of strip */
char *c_file_name; /* pathname of gcc */
static char *initname, *fininame; /* names of init and fini funcs */
static struct head constructors; /* list of constructors found */
static struct head destructors; /* list of destructors found */
static struct head exports; /* list of exported symbols */
static struct head frame_tables; /* list of frame unwind info tables */
struct obstack temporary_obstack;
struct obstack permanent_obstack;
char * temporary_firstobj;
/* Defined in the automatically-generated underscore.c. */
extern int prepends_underscore;
extern char *getenv ();
extern char *mktemp ();
extern FILE *fdopen ();
/* Structure to hold all the directories in which to search for files to
execute. */
struct prefix_list
{
char *prefix; /* String to prepend to the path. */
struct prefix_list *next; /* Next in linked list. */
};
struct path_prefix
{
struct prefix_list *plist; /* List of prefixes to try */
int max_len; /* Max length of a prefix in PLIST */
char *name; /* Name of this list (used in config stuff) */
};
void collect_exit PROTO((int));
void collect_execute PROTO((char *, char **, char *));
void dump_file PROTO((char *));
static void handler PROTO((int));
static int is_ctor_dtor PROTO((char *));
static int is_in_prefix_list PROTO((struct path_prefix *, char *, int));
static char *find_a_file PROTO((struct path_prefix *, char *));
static void add_prefix PROTO((struct path_prefix *, char *));
static void prefix_from_env PROTO((char *, struct path_prefix *));
static void prefix_from_string PROTO((char *, struct path_prefix *));
static void do_wait PROTO((char *));
static void fork_execute PROTO((char *, char **));
static void maybe_unlink PROTO((char *));
static void add_to_list PROTO((struct head *, char *));
static void write_list PROTO((FILE *, char *, struct id *));
static void write_list_with_asm PROTO((FILE *, char *, struct id *));
static void write_c_file PROTO((FILE *, char *));
static void write_export_file PROTO((FILE *));
static void scan_prog_file PROTO((char *, enum pass));
static void scan_libraries PROTO((char *));
char *xcalloc ();
char *xmalloc ();
#ifdef NEED_DECLARATION_INDEX
extern char *index ();
#endif
#ifdef NEED_DECLARATION_RINDEX
extern char *rindex ();
#endif
#ifdef NEED_DECLARATION_FREE
extern void free ();
#endif
#ifdef NO_DUP2
int
dup2 (oldfd, newfd)
int oldfd;
int newfd;
{
int fdtmp[256];
int fdx = 0;
int fd;
if (oldfd == newfd)
return oldfd;
close (newfd);
while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
fdtmp[fdx++] = fd;
while (fdx > 0)
close (fdtmp[--fdx]);
return fd;
}
#endif
char *
my_strerror (e)
int e;
{
#ifdef HAVE_STRERROR
return strerror (e);
#else
static char buffer[30];
if (!e)
return "";
if (e > 0 && e < sys_nerr)
return sys_errlist[e];
sprintf (buffer, "Unknown error %d", e);
return buffer;
#endif
}
/* Delete tempfiles and exit function. */
void
collect_exit (status)
int status;
{
if (c_file != 0 && c_file[0])
maybe_unlink (c_file);
if (o_file != 0 && o_file[0])
maybe_unlink (o_file);
if (export_file != 0 && export_file[0])
maybe_unlink (export_file);
if (ldout != 0 && ldout[0])
{
dump_file (ldout);
maybe_unlink (ldout);
}
if (status != 0 && output_file != 0 && output_file[0])
maybe_unlink (output_file);
exit (status);
}
/* Die when sys call fails. */
void
fatal_perror (string, arg1, arg2, arg3)
char *string, *arg1, *arg2, *arg3;
{
int e = errno;
fprintf (stderr, "collect2: ");
fprintf (stderr, string, arg1, arg2, arg3);
fprintf (stderr, ": %s\n", my_strerror (e));
collect_exit (FATAL_EXIT_CODE);
}
/* Just die. */
void
fatal (string, arg1, arg2, arg3)
char *string, *arg1, *arg2, *arg3;
{
fprintf (stderr, "collect2: ");
fprintf (stderr, string, arg1, arg2, arg3);
fprintf (stderr, "\n");
collect_exit (FATAL_EXIT_CODE);
}
/* Write error message. */
void
error (string, arg1, arg2, arg3, arg4)
char *string, *arg1, *arg2, *arg3, *arg4;
{
fprintf (stderr, "collect2: ");
fprintf (stderr, string, arg1, arg2, arg3, arg4);
fprintf (stderr, "\n");
}
/* In case obstack is linked in, and abort is defined to fancy_abort,
provide a default entry. */
void
fancy_abort ()
{
fatal ("internal error");
}
static void
handler (signo)
int signo;
{
if (c_file != 0 && c_file[0])
maybe_unlink (c_file);
if (o_file != 0 && o_file[0])
maybe_unlink (o_file);
if (ldout != 0 && ldout[0])
maybe_unlink (ldout);
if (export_file != 0 && export_file[0])
maybe_unlink (export_file);
signal (signo, SIG_DFL);
kill (getpid (), signo);
}
char *
xcalloc (size1, size2)
int size1, size2;
{
char *ptr = (char *) calloc (size1, size2);
if (ptr)
return ptr;
fatal ("out of memory");
return (char *) 0;
}
char *
xmalloc (size)
unsigned size;
{
char *ptr = (char *) malloc (size);
if (ptr)
return ptr;
fatal ("out of memory");
return (char *) 0;
}
char *
xrealloc (ptr, size)
char *ptr;
unsigned size;
{
register char *value = (char *) realloc (ptr, size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
int
file_exists (name)
char *name;
{
return access (name, R_OK) == 0;
}
/* Make a copy of a string INPUT with size SIZE. */
char *
savestring (input, size)
char *input;
int size;
{
char *output = (char *) xmalloc (size + 1);
bcopy (input, output, size);
output[size] = 0;
return output;
}
/* Parse a reasonable subset of shell quoting syntax. */
static char *
extract_string (pp)
char **pp;
{
char *p = *pp;
int backquote = 0;
int inside = 0;
for (;;)
{
char c = *p;
if (c == '\0')
break;
++p;
if (backquote)
obstack_1grow (&temporary_obstack, c);
else if (! inside && c == ' ')
break;
else if (! inside && c == '\\')
backquote = 1;
else if (c == '\'')
inside = !inside;
else
obstack_1grow (&temporary_obstack, c);
}
obstack_1grow (&temporary_obstack, '\0');
*pp = p;
return obstack_finish (&temporary_obstack);
}
void
dump_file (name)
char *name;
{
FILE *stream = fopen (name, "r");
int no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
if (stream == 0)
return;
while (1)
{
int c;
while (c = getc (stream),
c != EOF && (isalnum (c) || c == '_' || c == '$' || c == '.'))
obstack_1grow (&temporary_obstack, c);
if (obstack_object_size (&temporary_obstack) > 0)
{
char *word, *p, *result;
obstack_1grow (&temporary_obstack, '\0');
word = obstack_finish (&temporary_obstack);
if (*word == '.')
++word, putc ('.', stderr);
p = word;
if (*p == '_' && prepends_underscore)
++p;
if (no_demangle)
result = 0;
else
result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI);
if (result)
{
int diff;
fputs (result, stderr);
diff = strlen (word) - strlen (result);
while (diff > 0)
--diff, putc (' ', stderr);
while (diff < 0 && c == ' ')
++diff, c = getc (stream);
free (result);
}
else
fputs (word, stderr);
fflush (stderr);
obstack_free (&temporary_obstack, temporary_firstobj);
}
if (c == EOF)
break;
putc (c, stderr);
}
fclose (stream);
}
/* Decide whether the given symbol is:
a constructor (1), a destructor (2), or neither (0). */
static int
is_ctor_dtor (s)
char *s;
{
struct names { char *name; int len; int ret; int two_underscores; };
register struct names *p;
register int ch;
register char *orig_s = s;
static struct names special[] = {
#ifdef NO_DOLLAR_IN_LABEL
#ifdef NO_DOT_IN_LABEL
{ "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
{ "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
{ "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
#else
{ "GLOBAL_.I.", sizeof ("GLOBAL_.I.")-1, 1, 0 },
{ "GLOBAL_.D.", sizeof ("GLOBAL_.D.")-1, 2, 0 },
{ "GLOBAL_.F.", sizeof ("GLOBAL_.F.")-1, 5, 0 },
#endif
#else
{ "GLOBAL_$I$", sizeof ("GLOBAL_$I$")-1, 1, 0 },
{ "GLOBAL_$D$", sizeof ("GLOBAL_$D$")-1, 2, 0 },
{ "GLOBAL_$F$", sizeof ("GLOBAL_$F$")-1, 5, 0 },
#endif
{ "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
{ "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
#ifdef CFRONT_LOSSAGE /* Don't collect cfront initialization functions.
cfront has its own linker procedure to collect them;
if collect2 gets them too, they get collected twice
when the cfront procedure is run and the compiler used
for linking happens to be GCC. */
{ "sti__", sizeof ("sti__")-1, 1, 1 },
{ "std__", sizeof ("std__")-1, 2, 1 },
#endif /* CFRONT_LOSSAGE */
{ NULL, 0, 0, 0 }
};
while ((ch = *s) == '_')
++s;
if (s == orig_s)
return 0;
for (p = &special[0]; p->len > 0; p++)
{
if (ch == p->name[0]
&& (!p->two_underscores || ((s - orig_s) >= 2))
&& strncmp(s, p->name, p->len) == 0)
{
return p->ret;
}
}
return 0;
}
/* Routine to add variables to the environment. */
#ifndef HAVE_PUTENV
int
putenv (str)
char *str;
{
#ifndef VMS /* nor about VMS */
extern char **environ;
char **old_environ = environ;
char **envp;
int num_envs = 0;
int name_len = 1;
char *p = str;
int ch;
while ((ch = *p++) != '\0' && ch != '=')
name_len++;
if (!ch)
abort ();
/* Search for replacing an existing environment variable, and
count the number of total environment variables. */
for (envp = old_environ; *envp; envp++)
{
num_envs++;
if (!strncmp (str, *envp, name_len))
{
*envp = str;
return 0;
}
}
/* Add a new environment variable */
environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
*environ = str;
bcopy ((char *) old_environ, (char *) (environ + 1),
sizeof (char *) * (num_envs+1));
return 0;
#endif /* VMS */
}
#endif /* HAVE_PUTENV */
/* By default, colon separates directories in a path. */
#ifndef PATH_SEPARATOR
#define PATH_SEPARATOR ':'
#endif
/* We maintain two prefix lists: one from COMPILER_PATH environment variable
and one from the PATH variable. */
static struct path_prefix cpath, path;
#ifdef CROSS_COMPILE
/* This is the name of the target machine. We use it to form the name
of the files to execute. */
static char *target_machine = TARGET_MACHINE;
#endif
/* Names under which we were executed. Never return one of those files in our
searches. */
static struct path_prefix our_file_names;
/* Determine if STRING is in PPREFIX.
This utility is currently only used to look up file names. Prefix lists
record directory names. This matters to us because the latter has a
trailing slash, so I've added a flag to handle both. */
static int
is_in_prefix_list (pprefix, string, filep)
struct path_prefix *pprefix;
char *string;
int filep;
{
struct prefix_list *pl;
if (filep)
{
int len = strlen (string);
for (pl = pprefix->plist; pl; pl = pl->next)
{
if (strncmp (pl->prefix, string, len) == 0
&& strcmp (pl->prefix + len, "/") == 0)
return 1;
}
}
else
{
for (pl = pprefix->plist; pl; pl = pl->next)
{
if (strcmp (pl->prefix, string) == 0)
return 1;
}
}
return 0;
}
/* Search for NAME using prefix list PPREFIX. We only look for executable
files.
Return 0 if not found, otherwise return its name, allocated with malloc. */
static char *
find_a_file (pprefix, name)
struct path_prefix *pprefix;
char *name;
{
char *temp;
struct prefix_list *pl;
int len = pprefix->max_len + strlen (name) + 1;
#ifdef EXECUTABLE_SUFFIX
len += strlen (EXECUTABLE_SUFFIX);
#endif
temp = xmalloc (len);
/* Determine the filename to execute (special case for absolute paths). */
if (*name == '/')
{
if (access (name, X_OK) == 0)
{
strcpy (temp, name);
return temp;
}
}
else
for (pl = pprefix->plist; pl; pl = pl->next)
{
strcpy (temp, pl->prefix);
strcat (temp, name);
if (! is_in_prefix_list (&our_file_names, temp, 1)
/* This is a kludge, but there seems no way around it. */
&& strcmp (temp, "./ld") != 0
&& access (temp, X_OK) == 0)
return temp;
#ifdef EXECUTABLE_SUFFIX
/* Some systems have a suffix for executable files.
So try appending that. */
strcat (temp, EXECUTABLE_SUFFIX);
if (! is_in_prefix_list (&our_file_names, temp, 1)
&& access (temp, X_OK) == 0)
return temp;
#endif
}
free (temp);
return 0;
}
/* Add an entry for PREFIX to prefix list PPREFIX. */
static void
add_prefix (pprefix, prefix)
struct path_prefix *pprefix;
char *prefix;
{
struct prefix_list *pl, **prev;
int len;
if (pprefix->plist)
{
for (pl = pprefix->plist; pl->next; pl = pl->next)
;
prev = &pl->next;
}
else
prev = &pprefix->plist;
/* Keep track of the longest prefix */
len = strlen (prefix);
if (len > pprefix->max_len)
pprefix->max_len = len;
pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
pl->prefix = savestring (prefix, len);
if (*prev)
pl->next = *prev;
else
pl->next = (struct prefix_list *) 0;
*prev = pl;
}
/* Take the value of the environment variable ENV, break it into a path, and
add of the entries to PPREFIX. */
static void
prefix_from_env (env, pprefix)
char *env;
struct path_prefix *pprefix;
{
char *p = getenv (env);
if (p)
prefix_from_string (p, pprefix);
}
static void
prefix_from_string (p, pprefix)
char *p;
struct path_prefix *pprefix;
{
char *startp, *endp;
char *nstore = (char *) xmalloc (strlen (p) + 3);
startp = endp = p;
while (1)
{
if (*endp == PATH_SEPARATOR || *endp == 0)
{
strncpy (nstore, startp, endp-startp);
if (endp == startp)
{
strcpy (nstore, "./");
}
else if (endp[-1] != '/')
{
nstore[endp-startp] = '/';
nstore[endp-startp+1] = 0;
}
else
nstore[endp-startp] = 0;
add_prefix (pprefix, nstore);
if (*endp == 0)
break;
endp = startp = endp + 1;
}
else
endp++;
}
}
/* Main program. */
int
main (argc, argv)
int argc;
char *argv[];
{
char *ld_suffix = "ld";
char *full_ld_suffix = ld_suffix;
char *real_ld_suffix = "real-ld";
char *full_real_ld_suffix = real_ld_suffix;
char *collect_ld_suffix = "collect-ld";
char *nm_suffix = "nm";
char *full_nm_suffix = nm_suffix;
char *gnm_suffix = "gnm";
char *full_gnm_suffix = gnm_suffix;
#ifdef LDD_SUFFIX
char *ldd_suffix = LDD_SUFFIX;
char *full_ldd_suffix = ldd_suffix;
#endif
char *strip_suffix = "strip";
char *full_strip_suffix = strip_suffix;
char *gstrip_suffix = "gstrip";
char *full_gstrip_suffix = gstrip_suffix;
char *arg;
FILE *outf, *exportf;
char *ld_file_name;
char *collect_name;
char *collect_names;
char *p;
char **c_argv;
char **c_ptr;
char **ld1_argv = (char **) xcalloc (sizeof (char *), argc+3);
char **ld1 = ld1_argv;
char **ld2_argv = (char **) xcalloc (sizeof (char *), argc+6);
char **ld2 = ld2_argv;
char **object_lst = (char **) xcalloc (sizeof (char *), argc);
char **object = object_lst;
int first_file;
int num_c_args = argc+7;
#ifdef DEBUG
debug = 1;
vflag = 1;
#endif
#ifndef DEFAULT_A_OUT_NAME
output_file = "a.out";
#else
output_file = DEFAULT_A_OUT_NAME;
#endif
obstack_begin (&temporary_obstack, 0);
obstack_begin (&permanent_obstack, 0);
temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
current_demangling_style = gnu_demangling;
/* We must check that we do not call ourselves in an infinite
recursion loop. We append the name used for us to the COLLECT_NAMES
environment variable.
In practice, collect will rarely invoke itself. This can happen now
that we are no longer called gld. A perfect example is when running
gcc in a build directory that has been installed. When looking for
ld's, we'll find our installed version and believe that's the real ld. */
/* We must also append COLLECT_NAME to COLLECT_NAMES to watch for the
previous version of collect (the one that used COLLECT_NAME and only
handled two levels of recursion). If we don't we may mutually recurse
forever. This can happen (I think) when bootstrapping the old version
and a new one is installed (rare, but we should handle it).
??? Hopefully references to COLLECT_NAME can be removed at some point. */
collect_name = (char *) getenv ("COLLECT_NAME");
collect_names = (char *) getenv ("COLLECT_NAMES");
p = (char *) xmalloc (strlen ("COLLECT_NAMES=")
+ (collect_name ? strlen (collect_name) + 1 : 0)
+ (collect_names ? strlen (collect_names) + 1 : 0)
+ strlen (argv[0]) + 1);
strcpy (p, "COLLECT_NAMES=");
if (collect_name != 0)
sprintf (p + strlen (p), "%s%c", collect_name, PATH_SEPARATOR);
if (collect_names != 0)
sprintf (p + strlen (p), "%s%c", collect_names, PATH_SEPARATOR);
strcat (p, argv[0]);
putenv (p);
prefix_from_env ("COLLECT_NAMES", &our_file_names);