00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "config.h"
00030 #include "system.h"
00031 #include "coretypes.h"
00032 #include "tm.h"
00033 #include <signal.h>
00034 #if ! defined( SIGCHLD ) && defined( SIGCLD )
00035 # define SIGCHLD SIGCLD
00036 #endif
00037
00038 #ifndef LIBRARY_PATH_ENV
00039 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
00040 #endif
00041
00042 #define COLLECT
00043
00044 #include "collect2.h"
00045 #include "demangle.h"
00046 #include "obstack.h"
00047 #include "intl.h"
00048 #include "version.h"
00049
00050
00051
00052
00053
00054
00055
00056 #ifdef CROSS_COMPILE
00057 #undef OBJECT_FORMAT_COFF
00058 #undef MD_EXEC_PREFIX
00059 #undef REAL_LD_FILE_NAME
00060 #undef REAL_NM_FILE_NAME
00061 #undef REAL_STRIP_FILE_NAME
00062 #endif
00063
00064
00065
00066
00067
00068
00069 #if !defined (OBJECT_FORMAT_COFF)
00070 #define OBJECT_FORMAT_NONE
00071 #endif
00072
00073 #ifdef OBJECT_FORMAT_COFF
00074
00075 #include <a.out.h>
00076 #include <ar.h>
00077
00078 #ifdef UMAX
00079 #include <sgs.h>
00080 #endif
00081
00082
00083 #ifdef FREAD
00084 #undef FREAD
00085 #undef FWRITE
00086 #endif
00087
00088 #include <ldfcn.h>
00089
00090
00091
00092
00093
00094
00095 #ifndef MY_ISCOFF
00096 #define MY_ISCOFF(X) ISCOFF (X)
00097 #endif
00098
00099 #endif
00100
00101 #ifdef OBJECT_FORMAT_NONE
00102
00103
00104 #ifndef NM_FLAGS
00105 #define NM_FLAGS "-n"
00106 #endif
00107
00108 #endif
00109
00110
00111
00112
00113 #ifndef NAME__MAIN
00114 #define NAME__MAIN "__main"
00115 #endif
00116
00117
00118 #define DEFAULT_INIT_PRIORITY 65535
00119
00120 #ifndef COLLECT_SHARED_INIT_FUNC
00121 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
00122 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
00123 #endif
00124 #ifndef COLLECT_SHARED_FINI_FUNC
00125 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
00126 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
00127 #endif
00128
00129 #ifdef LDD_SUFFIX
00130 #define SCAN_LIBRARIES
00131 #endif
00132
00133 #ifdef USE_COLLECT2
00134 int do_collecting = 1;
00135 #else
00136 int do_collecting = 0;
00137 #endif
00138
00139
00140
00141 int no_demangle;
00142
00143
00144
00145 struct id
00146 {
00147 struct id *next;
00148 int sequence;
00149 char name[1];
00150 };
00151
00152 struct head
00153 {
00154 struct id *first;
00155 struct id *last;
00156 int number;
00157 };
00158
00159
00160
00161 enum pass {
00162 PASS_FIRST,
00163 PASS_OBJ,
00164 PASS_LIB,
00165 PASS_SECOND
00166 };
00167
00168 int vflag;
00169 static int rflag;
00170 static int strip_flag;
00171 static const char *demangle_flag;
00172 #ifdef COLLECT_EXPORT_LIST
00173 static int export_flag;
00174 static int aix64_flag;
00175 static int aixrtl_flag;
00176 #endif
00177
00178 int debug;
00179
00180 static int shared_obj;
00181
00182 static const char *c_file;
00183 static const char *o_file;
00184 #ifdef COLLECT_EXPORT_LIST
00185 static const char *export_file;
00186 #endif
00187 const char *ldout;
00188 const char *lderrout;
00189 static const char *output_file;
00190 static const char *nm_file_name;
00191 #ifdef LDD_SUFFIX
00192 static const char *ldd_file_name;
00193 #endif
00194 static const char *strip_file_name;
00195 const char *c_file_name;
00196 static char *initname, *fininame;
00197
00198 static struct head constructors;
00199 static struct head destructors;
00200 #ifdef COLLECT_EXPORT_LIST
00201 static struct head exports;
00202 #endif
00203 static struct head frame_tables;
00204
00205 struct obstack temporary_obstack;
00206 char * temporary_firstobj;
00207
00208
00209
00210
00211 struct prefix_list
00212 {
00213 const char *prefix;
00214 struct prefix_list *next;
00215 };
00216
00217 struct path_prefix
00218 {
00219 struct prefix_list *plist;
00220 int max_len;
00221 const char *name;
00222 };
00223
00224 #ifdef COLLECT_EXPORT_LIST
00225
00226 static struct head libs;
00227 static struct path_prefix cmdline_lib_dirs;
00228 static struct path_prefix libpath_lib_dirs;
00229 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
00230 &libpath_lib_dirs, NULL};
00231 #endif
00232
00233 static void handler (int);
00234 static int is_ctor_dtor (const char *);
00235 static char *find_a_file (struct path_prefix *, const char *);
00236 static void add_prefix (struct path_prefix *, const char *);
00237 static void prefix_from_env (const char *, struct path_prefix *);
00238 static void prefix_from_string (const char *, struct path_prefix *);
00239 static void do_wait (const char *, struct pex_obj *);
00240 static void fork_execute (const char *, char **);
00241 static void maybe_unlink (const char *);
00242 static void add_to_list (struct head *, const char *);
00243 static int extract_init_priority (const char *);
00244 static void sort_ids (struct head *);
00245 static void write_list (FILE *, const char *, struct id *);
00246 #ifdef COLLECT_EXPORT_LIST
00247 static void dump_list (FILE *, const char *, struct id *);
00248 #endif
00249 #if 0
00250 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
00251 #endif
00252 static void write_list_with_asm (FILE *, const char *, struct id *);
00253 static void write_c_file (FILE *, const char *);
00254 static void write_c_file_stat (FILE *, const char *);
00255 #ifndef LD_INIT_SWITCH
00256 static void write_c_file_glob (FILE *, const char *);
00257 #endif
00258 static void scan_prog_file (const char *, enum pass);
00259 #ifdef SCAN_LIBRARIES
00260 static void scan_libraries (const char *);
00261 #endif
00262 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
00263 static int is_in_args (const char *, const char **, const char **);
00264 #endif
00265 #ifdef COLLECT_EXPORT_LIST
00266 #if 0
00267 static int is_in_list (const char *, struct id *);
00268 #endif
00269 static void write_aix_file (FILE *, struct id *);
00270 static char *resolve_lib_name (const char *);
00271 #endif
00272 static char *extract_string (const char **);
00273
00274
00275
00276 void
00277 collect_exit (int status)
00278 {
00279 if (c_file != 0 && c_file[0])
00280 maybe_unlink (c_file);
00281
00282 if (o_file != 0 && o_file[0])
00283 maybe_unlink (o_file);
00284
00285 #ifdef COLLECT_EXPORT_LIST
00286 if (export_file != 0 && export_file[0])
00287 maybe_unlink (export_file);
00288 #endif
00289
00290 if (ldout != 0 && ldout[0])
00291 {
00292 dump_file (ldout, stdout);
00293 maybe_unlink (ldout);
00294 }
00295
00296 if (lderrout != 0 && lderrout[0])
00297 {
00298 dump_file (lderrout, stderr);
00299 maybe_unlink (lderrout);
00300 }
00301
00302 if (status != 0 && output_file != 0 && output_file[0])
00303 maybe_unlink (output_file);
00304
00305 exit (status);
00306 }
00307
00308
00309
00310 void
00311 notice (const char *cmsgid, ...)
00312 {
00313 va_list ap;
00314
00315 va_start (ap, cmsgid);
00316 vfprintf (stderr, _(cmsgid), ap);
00317 va_end (ap);
00318 }
00319
00320
00321
00322 void
00323 fatal_perror (const char * cmsgid, ...)
00324 {
00325 int e = errno;
00326 va_list ap;
00327
00328 va_start (ap, cmsgid);
00329 fprintf (stderr, "collect2: ");
00330 vfprintf (stderr, _(cmsgid), ap);
00331 fprintf (stderr, ": %s\n", xstrerror (e));
00332 va_end (ap);
00333
00334 collect_exit (FATAL_EXIT_CODE);
00335 }
00336
00337
00338
00339 void
00340 fatal (const char * cmsgid, ...)
00341 {
00342 va_list ap;
00343
00344 va_start (ap, cmsgid);
00345 fprintf (stderr, "collect2: ");
00346 vfprintf (stderr, _(cmsgid), ap);
00347 fprintf (stderr, "\n");
00348 va_end (ap);
00349
00350 collect_exit (FATAL_EXIT_CODE);
00351 }
00352
00353
00354
00355 void
00356 error (const char * gmsgid, ...)
00357 {
00358 va_list ap;
00359
00360 va_start (ap, gmsgid);
00361 fprintf (stderr, "collect2: ");
00362 vfprintf (stderr, _(gmsgid), ap);
00363 fprintf (stderr, "\n");
00364 va_end(ap);
00365 }
00366
00367
00368
00369
00370 void
00371 fancy_abort (const char *file, int line, const char *func)
00372 {
00373 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
00374 }
00375
00376 static void
00377 handler (int signo)
00378 {
00379 if (c_file != 0 && c_file[0])
00380 maybe_unlink (c_file);
00381
00382 if (o_file != 0 && o_file[0])
00383 maybe_unlink (o_file);
00384
00385 if (ldout != 0 && ldout[0])
00386 maybe_unlink (ldout);
00387
00388 if (lderrout != 0 && lderrout[0])
00389 maybe_unlink (lderrout);
00390
00391 #ifdef COLLECT_EXPORT_LIST
00392 if (export_file != 0 && export_file[0])
00393 maybe_unlink (export_file);
00394 #endif
00395
00396 signal (signo, SIG_DFL);
00397 raise (signo);
00398 }
00399
00400
00401 int
00402 file_exists (const char *name)
00403 {
00404 return access (name, R_OK) == 0;
00405 }
00406
00407
00408
00409 static char *
00410 extract_string (const char **pp)
00411 {
00412 const char *p = *pp;
00413 int backquote = 0;
00414 int inside = 0;
00415
00416 for (;;)
00417 {
00418 char c = *p;
00419 if (c == '\0')
00420 break;
00421 ++p;
00422 if (backquote)
00423 obstack_1grow (&temporary_obstack, c);
00424 else if (! inside && c == ' ')
00425 break;
00426 else if (! inside && c == '\\')
00427 backquote = 1;
00428 else if (c == '\'')
00429 inside = !inside;
00430 else
00431 obstack_1grow (&temporary_obstack, c);
00432 }
00433
00434 obstack_1grow (&temporary_obstack, '\0');
00435 *pp = p;
00436 return XOBFINISH (&temporary_obstack, char *);
00437 }
00438
00439 void
00440 dump_file (const char *name, FILE *to)
00441 {
00442 FILE *stream = fopen (name, "r");
00443
00444 if (stream == 0)
00445 return;
00446 while (1)
00447 {
00448 int c;
00449 while (c = getc (stream),
00450 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
00451 obstack_1grow (&temporary_obstack, c);
00452 if (obstack_object_size (&temporary_obstack) > 0)
00453 {
00454 const char *word, *p;
00455 char *result;
00456 obstack_1grow (&temporary_obstack, '\0');
00457 word = XOBFINISH (&temporary_obstack, const char *);
00458
00459 if (*word == '.')
00460 ++word, putc ('.', to);
00461 p = word;
00462 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
00463 p += strlen (USER_LABEL_PREFIX);
00464
00465 #ifdef HAVE_LD_DEMANGLE
00466 result = 0;
00467 #else
00468 if (no_demangle)
00469 result = 0;
00470 else
00471 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
00472 #endif
00473
00474 if (result)
00475 {
00476 int diff;
00477 fputs (result, to);
00478
00479 diff = strlen (word) - strlen (result);
00480 while (diff > 0 && c == ' ')
00481 --diff, putc (' ', to);
00482 while (diff < 0 && c == ' ')
00483 ++diff, c = getc (stream);
00484
00485 free (result);
00486 }
00487 else
00488 fputs (word, to);
00489
00490 fflush (to);
00491 obstack_free (&temporary_obstack, temporary_firstobj);
00492 }
00493 if (c == EOF)
00494 break;
00495 putc (c, to);
00496 }
00497 fclose (stream);
00498 }
00499
00500
00501
00502
00503
00504
00505 static int
00506 is_ctor_dtor (const char *s)
00507 {
00508 struct names { const char *const name; const int len; const int ret;
00509 const int two_underscores; };
00510
00511 const struct names *p;
00512 int ch;
00513 const char *orig_s = s;
00514
00515 static const struct names special[] = {
00516 #ifndef NO_DOLLAR_IN_LABEL
00517 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
00518 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
00519 #else
00520 #ifndef NO_DOT_IN_LABEL
00521 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
00522 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
00523 #endif
00524 #endif
00525 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
00526 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
00527 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
00528 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
00529 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
00530 { NULL, 0, 0, 0 }
00531 };
00532
00533 while ((ch = *s) == '_')
00534 ++s;
00535
00536 if (s == orig_s)
00537 return 0;
00538
00539 for (p = &special[0]; p->len > 0; p++)
00540 {
00541 if (ch == p->name[0]
00542 && (!p->two_underscores || ((s - orig_s) >= 2))
00543 && strncmp(s, p->name, p->len) == 0)
00544 {
00545 return p->ret;
00546 }
00547 }
00548 return 0;
00549 }
00550
00551
00552
00553
00554 static struct path_prefix cpath, path;
00555
00556 #ifdef CROSS_COMPILE
00557
00558
00559
00560 static const char *const target_machine = TARGET_MACHINE;
00561 #endif
00562
00563
00564
00565
00566
00567
00568 static char *
00569 find_a_file (struct path_prefix *pprefix, const char *name)
00570 {
00571 char *temp;
00572 struct prefix_list *pl;
00573 int len = pprefix->max_len + strlen (name) + 1;
00574
00575 if (debug)
00576 fprintf (stderr, "Looking for '%s'\n", name);
00577
00578 #ifdef HOST_EXECUTABLE_SUFFIX
00579 len += strlen (HOST_EXECUTABLE_SUFFIX);
00580 #endif
00581
00582 temp = XNEWVEC (char, len);
00583
00584
00585
00586 if (*name == '/'
00587 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00588 || (*name && name[1] == ':')
00589 #endif
00590 )
00591 {
00592 if (access (name, X_OK) == 0)
00593 {
00594 strcpy (temp, name);
00595
00596 if (debug)
00597 fprintf (stderr, " - found: absolute path\n");
00598
00599 return temp;
00600 }
00601
00602 #ifdef HOST_EXECUTABLE_SUFFIX
00603
00604
00605 strcpy (temp, name);
00606 strcat (temp, HOST_EXECUTABLE_SUFFIX);
00607
00608 if (access (temp, X_OK) == 0)
00609 return temp;
00610 #endif
00611
00612 if (debug)
00613 fprintf (stderr, " - failed to locate using absolute path\n");
00614 }
00615 else
00616 for (pl = pprefix->plist; pl; pl = pl->next)
00617 {
00618 struct stat st;
00619
00620 strcpy (temp, pl->prefix);
00621 strcat (temp, name);
00622
00623 if (stat (temp, &st) >= 0
00624 && ! S_ISDIR (st.st_mode)
00625 && access (temp, X_OK) == 0)
00626 return temp;
00627
00628 #ifdef HOST_EXECUTABLE_SUFFIX
00629
00630
00631 strcat (temp, HOST_EXECUTABLE_SUFFIX);
00632
00633 if (stat (temp, &st) >= 0
00634 && ! S_ISDIR (st.st_mode)
00635 && access (temp, X_OK) == 0)
00636 return temp;
00637 #endif
00638 }
00639
00640 if (debug && pprefix->plist == NULL)
00641 fprintf (stderr, " - failed: no entries in prefix list\n");
00642
00643 free (temp);
00644 return 0;
00645 }
00646
00647
00648
00649 static void
00650 add_prefix (struct path_prefix *pprefix, const char *prefix)
00651 {
00652 struct prefix_list *pl, **prev;
00653 int len;
00654
00655 if (pprefix->plist)
00656 {
00657 for (pl = pprefix->plist; pl->next; pl = pl->next)
00658 ;
00659 prev = &pl->next;
00660 }
00661 else
00662 prev = &pprefix->plist;
00663
00664
00665
00666 len = strlen (prefix);
00667 if (len > pprefix->max_len)
00668 pprefix->max_len = len;
00669
00670 pl = XNEW (struct prefix_list);
00671 pl->prefix = xstrdup (prefix);
00672
00673 if (*prev)
00674 pl->next = *prev;
00675 else
00676 pl->next = (struct prefix_list *) 0;
00677 *prev = pl;
00678 }
00679
00680
00681
00682
00683 static void
00684 prefix_from_env (const char *env, struct path_prefix *pprefix)
00685 {
00686 const char *p;
00687 GET_ENVIRONMENT (p, env);
00688
00689 if (p)
00690 prefix_from_string (p, pprefix);
00691 }
00692
00693 static void
00694 prefix_from_string (const char *p, struct path_prefix *pprefix)
00695 {
00696 const char *startp, *endp;
00697 char *nstore = XNEWVEC (char, strlen (p) + 3);
00698
00699 if (debug)
00700 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
00701
00702 startp = endp = p;
00703 while (1)
00704 {
00705 if (*endp == PATH_SEPARATOR || *endp == 0)
00706 {
00707 strncpy (nstore, startp, endp-startp);
00708 if (endp == startp)
00709 {
00710 strcpy (nstore, "./");
00711 }
00712 else if (! IS_DIR_SEPARATOR (endp[-1]))
00713 {
00714 nstore[endp-startp] = DIR_SEPARATOR;
00715 nstore[endp-startp+1] = 0;
00716 }
00717 else
00718 nstore[endp-startp] = 0;
00719
00720 if (debug)
00721 fprintf (stderr, " - add prefix: %s\n", nstore);
00722
00723 add_prefix (pprefix, nstore);
00724 if (*endp == 0)
00725 break;
00726 endp = startp = endp + 1;
00727 }
00728 else
00729 endp++;
00730 }
00731 }
00732
00733
00734
00735 int
00736 main (int argc, char **argv)
00737 {
00738 static const char *const ld_suffix = "ld";
00739 static const char *const real_ld_suffix = "real-ld";
00740 static const char *const collect_ld_suffix = "collect-ld";
00741 static const char *const nm_suffix = "nm";
00742 static const char *const gnm_suffix = "gnm";
00743 #ifdef LDD_SUFFIX
00744 static const char *const ldd_suffix = LDD_SUFFIX;
00745 #endif
00746 static const char *const strip_suffix = "strip";
00747 static const char *const gstrip_suffix = "gstrip";
00748
00749 #ifdef CROSS_COMPILE
00750
00751
00752
00753
00754
00755 const char *const full_ld_suffix =
00756 concat(target_machine, "-", ld_suffix, NULL);
00757 const char *const full_nm_suffix =
00758 concat (target_machine, "-", nm_suffix, NULL);
00759 const char *const full_gnm_suffix =
00760 concat (target_machine, "-", gnm_suffix, NULL);
00761 #ifdef LDD_SUFFIX
00762 const char *const full_ldd_suffix =
00763 concat (target_machine, "-", ldd_suffix, NULL);
00764 #endif
00765 const char *const full_strip_suffix =
00766 concat (target_machine, "-", strip_suffix, NULL);
00767 const char *const full_gstrip_suffix =
00768 concat (target_machine, "-", gstrip_suffix, NULL);
00769 #else
00770 const char *const full_ld_suffix = ld_suffix;
00771 const char *const full_nm_suffix = nm_suffix;
00772 const char *const full_gnm_suffix = gnm_suffix;
00773 #ifdef LDD_SUFFIX
00774 const char *const full_ldd_suffix = ldd_suffix;
00775 #endif
00776 const char *const full_strip_suffix = strip_suffix;
00777 const char *const full_gstrip_suffix = gstrip_suffix;
00778 #endif
00779
00780 const char *arg;
00781 FILE *outf;
00782 #ifdef COLLECT_EXPORT_LIST
00783 FILE *exportf;
00784 #endif
00785 const char *ld_file_name;
00786 const char *p;
00787 char **c_argv;
00788 const char **c_ptr;
00789 char **ld1_argv;
00790 const char **ld1;
00791 char **ld2_argv;
00792 const char **ld2;
00793 char **object_lst;
00794 const char **object;
00795 int first_file;
00796 int num_c_args = argc+9;
00797
00798 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
00799
00800
00801 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
00802
00803 #if defined (COLLECT2_HOST_INITIALIZATION)
00804
00805 COLLECT2_HOST_INITIALIZATION;
00806 #endif
00807
00808 #ifdef SIGCHLD
00809
00810
00811 signal (SIGCHLD, SIG_DFL);
00812 #endif
00813
00814
00815 unlock_std_streams ();
00816
00817 gcc_init_libintl ();
00818
00819
00820
00821
00822 ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
00823 ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
00824 object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
00825
00826 #ifdef DEBUG
00827 debug = 1;
00828 #endif
00829
00830
00831
00832
00833 {
00834 int i;
00835
00836 for (i = 1; argv[i] != NULL; i ++)
00837 {
00838 if (! strcmp (argv[i], "-debug"))
00839 debug = 1;
00840 }
00841 vflag = debug;
00842 }
00843
00844 #ifndef DEFAULT_A_OUT_NAME
00845 output_file = "a.out";
00846 #else
00847 output_file = DEFAULT_A_OUT_NAME;
00848 #endif
00849
00850 obstack_begin (&temporary_obstack, 0);
00851 temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
00852
00853 #ifndef HAVE_LD_DEMANGLE
00854 current_demangling_style = auto_demangling;
00855 #endif
00856 p = getenv ("COLLECT_GCC_OPTIONS");
00857 while (p && *p)
00858 {
00859 const char *q = extract_string (&p);
00860 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
00861 num_c_args++;
00862 }
00863 obstack_free (&temporary_obstack, temporary_firstobj);
00864
00865
00866
00867 num_c_args += 5;
00868
00869 c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
00870
00871 if (argc < 2)
00872 fatal ("no arguments");
00873
00874 #ifdef SIGQUIT
00875 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
00876 signal (SIGQUIT, handler);
00877 #endif
00878 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
00879 signal (SIGINT, handler);
00880 #ifdef SIGALRM
00881 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
00882 signal (SIGALRM, handler);
00883 #endif
00884 #ifdef SIGHUP
00885 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
00886 signal (SIGHUP, handler);
00887 #endif
00888 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
00889 signal (SIGSEGV, handler);
00890 #ifdef SIGBUS
00891 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
00892 signal (SIGBUS, handler);
00893 #endif
00894
00895
00896 prefix_from_env ("COMPILER_PATH", &cpath);
00897 prefix_from_env ("PATH", &path);
00898
00899
00900
00901
00902 ld_file_name = 0;
00903 #ifdef DEFAULT_LINKER
00904 if (access (DEFAULT_LINKER, X_OK) == 0)
00905 ld_file_name = DEFAULT_LINKER;
00906 if (ld_file_name == 0)
00907 #endif
00908 #ifdef REAL_LD_FILE_NAME
00909 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
00910 if (ld_file_name == 0)
00911 #endif
00912
00913 ld_file_name = find_a_file (&cpath, real_ld_suffix);
00914
00915 if (ld_file_name == 0)
00916 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
00917
00918
00919 if (ld_file_name == 0)
00920 ld_file_name = find_a_file (&cpath, ld_suffix);
00921
00922
00923 if (ld_file_name == 0)
00924 ld_file_name = find_a_file (&path, full_ld_suffix);
00925
00926 #ifdef REAL_NM_FILE_NAME
00927 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
00928 if (nm_file_name == 0)
00929 #endif
00930 nm_file_name = find_a_file (&cpath, gnm_suffix);
00931 if (nm_file_name == 0)
00932 nm_file_name = find_a_file (&path, full_gnm_suffix);
00933 if (nm_file_name == 0)
00934 nm_file_name = find_a_file (&cpath, nm_suffix);
00935 if (nm_file_name == 0)
00936 nm_file_name = find_a_file (&path, full_nm_suffix);
00937
00938 #ifdef LDD_SUFFIX
00939 ldd_file_name = find_a_file (&cpath, ldd_suffix);
00940 if (ldd_file_name == 0)
00941 ldd_file_name = find_a_file (&path, full_ldd_suffix);
00942 #endif
00943
00944 #ifdef REAL_STRIP_FILE_NAME
00945 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
00946 if (strip_file_name == 0)
00947 #endif
00948 strip_file_name = find_a_file (&cpath, gstrip_suffix);
00949 if (strip_file_name == 0)
00950 strip_file_name = find_a_file (&path, full_gstrip_suffix);
00951 if (strip_file_name == 0)
00952 strip_file_name = find_a_file (&cpath, strip_suffix);
00953 if (strip_file_name == 0)
00954 strip_file_name = find_a_file (&path, full_strip_suffix);
00955
00956
00957 c_file_name = getenv ("COLLECT_GCC");
00958 if (c_file_name == 0)
00959 {
00960 #ifdef CROSS_COMPILE
00961 c_file_name = concat (target_machine, "-gcc", NULL);
00962 #else
00963 c_file_name = "gcc";
00964 #endif
00965 }
00966
00967 p = find_a_file (&cpath, c_file_name);
00968
00969
00970
00971 if (p == 0)
00972 p = find_a_file (&path, c_file_name);
00973
00974 if (p)
00975 c_file_name = p;
00976
00977 *ld1++ = *ld2++ = ld_file_name;
00978
00979
00980 c_file = make_temp_file (".c");
00981 o_file = make_temp_file (".o");
00982 #ifdef COLLECT_EXPORT_LIST
00983 export_file = make_temp_file (".x");
00984 #endif
00985 ldout = make_temp_file (".ld");
00986 lderrout = make_temp_file (".le");
00987 *c_ptr++ = c_file_name;
00988 *c_ptr++ = "-x";
00989 *c_ptr++ = "c";
00990 *c_ptr++ = "-c";
00991 *c_ptr++ = "-o";
00992 *c_ptr++ = o_file;
00993
00994 #ifdef COLLECT_EXPORT_LIST
00995
00996 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
00997
00998
00999 add_prefix (&libpath_lib_dirs, "/lib");
01000 add_prefix (&libpath_lib_dirs, "/usr/lib");
01001 #endif
01002
01003
01004
01005
01006
01007
01008 p = getenv ("COLLECT_GCC_OPTIONS");
01009 while (p && *p)
01010 {
01011 const char *q = extract_string (&p);
01012 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
01013 *c_ptr++ = xstrdup (q);
01014 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
01015 *c_ptr++ = xstrdup (q);
01016 if (strcmp (q, "-shared") == 0)
01017 shared_obj = 1;
01018 if (*q == '-' && q[1] == 'B')
01019 {
01020 *c_ptr++ = xstrdup (q);
01021 if (q[2] == 0)
01022 {
01023 q = extract_string (&p);
01024 *c_ptr++ = xstrdup (q);
01025 }
01026 }
01027 }
01028 obstack_free (&temporary_obstack, temporary_firstobj);
01029 *c_ptr++ = "-fno-profile-arcs";
01030 *c_ptr++ = "-fno-test-coverage";
01031 *c_ptr++ = "-fno-branch-probabilities";
01032 *c_ptr++ = "-fno-exceptions";
01033 *c_ptr++ = "-w";
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045 first_file = 1;
01046 #ifdef HAVE_LD_DEMANGLE
01047 if (!demangle_flag && !no_demangle)
01048 demangle_flag = "--demangle";
01049 if (demangle_flag)
01050 *ld1++ = *ld2++ = demangle_flag;
01051 #endif
01052 while ((arg = *++argv) != (char *) 0)
01053 {
01054 *ld1++ = *ld2++ = arg;
01055
01056 if (arg[0] == '-')
01057 {
01058 switch (arg[1])
01059 {
01060 #ifdef COLLECT_EXPORT_LIST
01061
01062
01063 case 'b':
01064 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
01065 export_flag = 1;
01066 else if (arg[2] == '6' && arg[3] == '4')
01067 aix64_flag = 1;
01068 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
01069 aixrtl_flag = 1;
01070 break;
01071 #endif
01072
01073 case 'd':
01074 if (!strcmp (arg, "-debug"))
01075 {
01076
01077 ld1--;
01078 ld2--;
01079 }
01080 if (!strcmp (arg, "-dynamic-linker") && argv[1])
01081 {
01082 ++argv;
01083 *ld1++ = *ld2++ = *argv;
01084 }
01085 break;
01086
01087 case 'l':
01088 if (first_file)
01089 {
01090
01091 first_file = 0;
01092 ld2--;
01093 *ld2++ = o_file;
01094 *ld2++ = arg;
01095 }
01096 #ifdef COLLECT_EXPORT_LIST
01097 {
01098
01099 const char *s = resolve_lib_name (arg+2);
01100
01101
01102 add_to_list (&libs, s);
01103 }
01104 #endif
01105 break;
01106
01107 #ifdef COLLECT_EXPORT_LIST
01108
01109 case 'L':
01110 add_prefix (&cmdline_lib_dirs, arg+2);
01111 break;
01112 #else
01113 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
01114 case 'L':
01115 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
01116 --ld1;
01117 break;
01118 #endif
01119 #endif
01120
01121 case 'o':
01122 if (arg[2] == '\0')
01123 output_file = *ld1++ = *ld2++ = *++argv;
01124 else if (1
01125 #ifdef SWITCHES_NEED_SPACES
01126 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
01127 #endif
01128 )
01129
01130 output_file = &arg[2];
01131 break;
01132
01133 case 'r':
01134 if (arg[2] == '\0')
01135 rflag = 1;
01136 break;
01137
01138 case 's':
01139 if (arg[2] == '\0' && do_collecting)
01140 {
01141
01142
01143
01144 strip_flag = 1;
01145 ld1--;
01146 }
01147 break;
01148
01149 case 'v':
01150 if (arg[2] == '\0')
01151 vflag = 1;
01152 break;
01153
01154 case '-':
01155 if (strcmp (arg, "--no-demangle") == 0)
01156 {
01157 demangle_flag = arg;
01158 no_demangle = 1;
01159 ld1--;
01160 ld2--;
01161 }
01162 else if (strncmp (arg, "--demangle", 10) == 0)
01163 {
01164 demangle_flag = arg;
01165 no_demangle = 0;
01166 #ifndef HAVE_LD_DEMANGLE
01167 if (arg[10] == '=')
01168 {
01169 enum demangling_styles style
01170 = cplus_demangle_name_to_style (arg+11);
01171 if (style == unknown_demangling)
01172 error ("unknown demangling style '%s'", arg+11);
01173 else
01174 current_demangling_style = style;
01175 }
01176 #endif
01177 ld1--;
01178 ld2--;
01179 }
01180 break;
01181 }
01182 }
01183 else if ((p = strrchr (arg, '.')) != (char *) 0
01184 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
01185 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
01186 || strcmp (p, ".obj") == 0))
01187 {
01188 if (first_file)
01189 {
01190 first_file = 0;
01191 if (p[1] == 'o')
01192 *ld2++ = o_file;
01193 else
01194 {
01195
01196 ld2--;
01197 *ld2++ = o_file;
01198 *ld2++ = arg;
01199 }
01200 }
01201 if (p[1] == 'o' || p[1] == 'l')
01202 *object++ = arg;
01203 #ifdef COLLECT_EXPORT_LIST
01204
01205 else
01206 {
01207
01208 add_to_list (&libs, arg);
01209 }
01210 #endif
01211 }
01212 }
01213
01214 #ifdef COLLECT_EXPORT_LIST
01215
01216 if (debug)
01217 {
01218 fprintf (stderr, "List of libraries:\n");
01219 dump_list (stderr, "\t", libs.first);
01220 }
01221
01222
01223
01224 {
01225 const char **export_object_lst = (const char **)object_lst;
01226
01227 while (export_object_lst < object)
01228 scan_prog_file (*export_object_lst++, PASS_OBJ);
01229 }
01230 {
01231 struct id *list = libs.first;
01232
01233 for (; list; list = list->next)
01234 scan_prog_file (list->name, PASS_FIRST);
01235 }
01236
01237 if (exports.first)
01238 {
01239 char *buf = concat ("-bE:", export_file, NULL);
01240
01241 *ld1++ = buf;
01242 *ld2++ = buf;
01243
01244 exportf = fopen (export_file, "w");
01245 if (exportf == (FILE *) 0)
01246 fatal_perror ("fopen %s", export_file);
01247 write_aix_file (exportf, exports.first);
01248 if (fclose (exportf))
01249 fatal_perror ("fclose %s", export_file);
01250 }
01251 #endif
01252
01253 *c_ptr++ = c_file;
01254 *c_ptr = *ld1 = *object = (char *) 0;
01255
01256 if (vflag)
01257 {
01258 notice ("collect2 version %s", version_string);
01259 #ifdef TARGET_VERSION
01260 TARGET_VERSION;
01261 #endif
01262 fprintf (stderr, "\n");
01263 }
01264
01265 if (debug)
01266 {
01267 const char *ptr;
01268 fprintf (stderr, "ld_file_name = %s\n",
01269 (ld_file_name ? ld_file_name : "not found"));
01270 fprintf (stderr, "c_file_name = %s\n",
01271 (c_file_name ? c_file_name : "not found"));
01272 fprintf (stderr, "nm_file_name = %s\n",
01273 (nm_file_name ? nm_file_name : "not found"));
01274 #ifdef LDD_SUFFIX
01275 fprintf (stderr, "ldd_file_name = %s\n",
01276 (ldd_file_name ? ldd_file_name : "not found"));
01277 #endif
01278 fprintf (stderr, "strip_file_name = %s\n",
01279 (strip_file_name ? strip_file_name : "not found"));
01280 fprintf (stderr, "c_file = %s\n",
01281 (c_file ? c_file : "not found"));
01282 fprintf (stderr, "o_file = %s\n",
01283 (o_file ? o_file : "not found"));
01284
01285 ptr = getenv ("COLLECT_GCC_OPTIONS");
01286 if (ptr)
01287 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
01288
01289 ptr = getenv ("COLLECT_GCC");
01290 if (ptr)
01291 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
01292
01293 ptr = getenv ("COMPILER_PATH");
01294 if (ptr)
01295 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
01296
01297 ptr = getenv (LIBRARY_PATH_ENV);
01298 if (ptr)
01299 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
01300
01301 fprintf (stderr, "\n");
01302 }
01303
01304
01305
01306
01307
01308 #ifndef COLLECT_EXPORT_LIST
01309 do_tlink (ld1_argv, object_lst);
01310 #endif
01311
01312
01313
01314 if (rflag
01315 #ifndef COLLECT_EXPORT_LIST
01316 || ! do_collecting
01317 #endif
01318 )
01319 {
01320 #ifdef COLLECT_EXPORT_LIST
01321
01322 do_tlink (ld1_argv, object_lst);
01323
01324
01325 if (export_file != 0 && export_file[0])
01326 maybe_unlink (export_file);
01327 #endif
01328 maybe_unlink (c_file);
01329 maybe_unlink (o_file);
01330 return 0;
01331 }
01332
01333
01334
01335
01336
01337
01338 #ifndef COLLECT_EXPORT_LIST
01339 scan_prog_file (output_file, PASS_FIRST);
01340 #endif
01341
01342 #ifdef SCAN_LIBRARIES
01343 scan_libraries (output_file);
01344 #endif
01345
01346 if (debug)
01347 {
01348 notice ("%d constructor(s) found\n", constructors.number);
01349 notice ("%d destructor(s) found\n", destructors.number);
01350 notice ("%d frame table(s) found\n", frame_tables.number);
01351 }
01352
01353 if (constructors.number == 0 && destructors.number == 0
01354 && frame_tables.number == 0
01355 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
01356
01357
01358
01359 && ! shared_obj
01360 #endif
01361 )
01362 {
01363 #ifdef COLLECT_EXPORT_LIST
01364
01365 do_tlink (ld1_argv, object_lst);
01366 #endif
01367
01368 if (strip_flag)
01369 {
01370 char **real_strip_argv = XCNEWVEC (char *, 3);
01371 const char ** strip_argv = (const char **) real_strip_argv;
01372
01373 strip_argv[0] = strip_file_name;
01374 strip_argv[1] = output_file;
01375 strip_argv[2] = (char *) 0;
01376 fork_execute ("strip", real_strip_argv);
01377 }
01378
01379 #ifdef COLLECT_EXPORT_LIST
01380 maybe_unlink (export_file);
01381 #endif
01382 maybe_unlink (c_file);
01383 maybe_unlink (o_file);
01384 return 0;
01385 }
01386
01387
01388 sort_ids (&constructors);
01389 sort_ids (&destructors);
01390
01391 maybe_unlink(output_file);
01392 outf = fopen (c_file, "w");
01393 if (outf == (FILE *) 0)
01394 fatal_perror ("fopen %s", c_file);
01395
01396 write_c_file (outf, c_file);
01397
01398 if (fclose (outf))
01399 fatal_perror ("fclose %s", c_file);
01400
01401
01402 #ifdef LD_INIT_SWITCH
01403 #ifdef COLLECT_EXPORT_LIST
01404 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
01405 #else
01406 *ld2++ = LD_INIT_SWITCH;
01407 *ld2++ = initname;
01408 *ld2++ = LD_FINI_SWITCH;
01409 *ld2++ = fininame;
01410 #endif
01411 #endif
01412
01413 #ifdef COLLECT_EXPORT_LIST
01414 if (shared_obj)
01415 {
01416
01417
01418 if (! exports.first)
01419 *ld2++ = concat ("-bE:", export_file, NULL);
01420
01421 #ifndef LD_INIT_SWITCH
01422 add_to_list (&exports, initname);
01423 add_to_list (&exports, fininame);
01424 add_to_list (&exports, "_GLOBAL__DI");
01425 add_to_list (&exports, "_GLOBAL__DD");
01426 #endif
01427 exportf = fopen (export_file, "w");
01428 if (exportf == (FILE *) 0)
01429 fatal_perror ("fopen %s", export_file);
01430 write_aix_file (exportf, exports.first);
01431 if (fclose (exportf))
01432 fatal_perror ("fclose %s", export_file);
01433 }
01434 #endif
01435
01436
01437 *ld2 = (char*) 0;
01438
01439 if (debug)
01440 {
01441 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
01442 output_file, c_file);
01443 write_c_file (stderr, "stderr");
01444 fprintf (stderr, "========== end of c_file\n\n");
01445 #ifdef COLLECT_EXPORT_LIST
01446 fprintf (stderr, "\n========== export_file = %s\n", export_file);
01447 write_aix_file (stderr, exports.first);
01448 fprintf (stderr, "========== end of export_file\n\n");
01449 #endif
01450 }
01451
01452
01453
01454
01455 fork_execute ("gcc", c_argv);
01456 #ifdef COLLECT_EXPORT_LIST
01457
01458 do_tlink (ld2_argv, object_lst);
01459 #else
01460
01461 fork_execute ("ld", ld2_argv);
01462
01463
01464
01465 scan_prog_file (output_file, PASS_SECOND);
01466 #endif
01467
01468 maybe_unlink (c_file);
01469 maybe_unlink (o_file);
01470
01471 #ifdef COLLECT_EXPORT_LIST
01472 maybe_unlink (export_file);
01473 #endif
01474
01475 return 0;
01476 }
01477
01478
01479
01480
01481 int
01482 collect_wait (const char *prog, struct pex_obj *pex)
01483 {
01484 int status;
01485
01486 if (!pex_get_status (pex, 1, &status))
01487 fatal_perror ("can't get program status");
01488 pex_free (pex);
01489
01490 if (status)
01491 {
01492 if (WIFSIGNALED (status))
01493 {
01494 int sig = WTERMSIG (status);
01495 error ("%s terminated with signal %d [%s]%s",
01496 prog, sig, strsignal(sig),
01497 WCOREDUMP(status) ? ", core dumped" : "");
01498 collect_exit (FATAL_EXIT_CODE);
01499 }
01500
01501 if (WIFEXITED (status))
01502 return WEXITSTATUS (status);
01503 }
01504 return 0;
01505 }
01506
01507 static void
01508 do_wait (const char *prog, struct pex_obj *pex)
01509 {
01510 int ret = collect_wait (prog, pex);
01511 if (ret != 0)
01512 {
01513 error ("%s returned %d exit status", prog, ret);
01514 collect_exit (ret);
01515 }
01516 }
01517
01518
01519
01520
01521 struct pex_obj *
01522 collect_execute (const char *prog, char **argv, const char *outname,
01523 const char *errname)
01524 {
01525 struct pex_obj *pex;
01526 const char *errmsg;
01527 int err;
01528
01529 if (vflag || debug)
01530 {
01531 char **p_argv;
01532 const char *str;
01533
01534 if (argv[0])
01535 fprintf (stderr, "%s", argv[0]);
01536 else
01537 notice ("[cannot find %s]", prog);
01538
01539 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
01540 fprintf (stderr, " %s", str);
01541
01542 fprintf (stderr, "\n");
01543 }
01544
01545 fflush (stdout);
01546 fflush (stderr);
01547
01548
01549
01550
01551 if (argv[0] == 0)
01552 fatal ("cannot find '%s'", prog);
01553
01554 pex = pex_init (0, "collect2", NULL);
01555 if (pex == NULL)
01556 fatal_perror ("pex_init failed");
01557
01558 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
01559 errname, &err);
01560 if (errmsg != NULL)
01561 {
01562 if (err != 0)
01563 {
01564 errno = err;
01565 fatal_perror (errmsg);
01566 }
01567 else
01568 fatal (errmsg);
01569 }
01570
01571 return pex;
01572 }
01573
01574 static void
01575 fork_execute (const char *prog, char **argv)
01576 {
01577 struct pex_obj *pex;
01578
01579 pex = collect_execute (prog, argv, NULL, NULL);
01580 do_wait (prog, pex);
01581 }
01582
01583
01584
01585 static void
01586 maybe_unlink (const char *file)
01587 {
01588 if (!debug)
01589 unlink_if_ordinary (file);
01590 else
01591 notice ("[Leaving %s]\n", file);
01592 }
01593
01594
01595 static long sequence_number = 0;
01596
01597
01598
01599 static void
01600 add_to_list (struct head *head_ptr, const char *name)
01601 {
01602 struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
01603 struct id *p;
01604 strcpy (newid->name, name);
01605
01606 if (head_ptr->first)
01607 head_ptr->last->next = newid;
01608 else
01609 head_ptr->first = newid;
01610
01611
01612 for (p = head_ptr->first;
01613 strcmp (name, p->name) != 0;
01614 p = p->next)
01615 ;
01616 if (p != newid)
01617 {
01618 head_ptr->last->next = 0;
01619 free (newid);
01620 return;
01621 }
01622
01623 newid->sequence = ++sequence_number;
01624 head_ptr->last = newid;
01625 head_ptr->number++;
01626 }
01627
01628
01629
01630
01631 static int
01632 extract_init_priority (const char *name)
01633 {
01634 int pos = 0, pri;
01635
01636 while (name[pos] == '_')
01637 ++pos;
01638 pos += 10;
01639
01640
01641 pri = atoi (name + pos);
01642 return pri ? pri : DEFAULT_INIT_PRIORITY;
01643 }
01644
01645
01646
01647
01648 static void
01649 sort_ids (struct head *head_ptr)
01650 {
01651
01652
01653
01654 struct id *id, *id_next, **id_ptr;
01655
01656 id = head_ptr->first;
01657
01658
01659 head_ptr->first = NULL;
01660
01661 for (; id; id = id_next)
01662 {
01663 id_next = id->next;
01664 id->sequence = extract_init_priority (id->name);
01665
01666 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
01667 if (*id_ptr == NULL
01668
01669
01670 || id->sequence > (*id_ptr)->sequence
01671
01672
01673
01674 )
01675 {
01676 id->next = *id_ptr;
01677 *id_ptr = id;
01678 break;
01679 }
01680 }
01681
01682
01683 for (id = head_ptr->first; id; id = id->next)
01684 id->sequence = ++sequence_number;
01685 }
01686
01687
01688
01689 static void
01690 write_list (FILE *stream, const char *prefix, struct id *list)
01691 {
01692 while (list)
01693 {
01694 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
01695 list = list->next;
01696 }
01697 }
01698
01699 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
01700
01701
01702
01703 static int
01704 is_in_args (const char *string, const char **args_begin,
01705 const char **args_end)
01706 {
01707 const char **args_pointer;
01708 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
01709 if (strcmp (string, *args_pointer) == 0)
01710 return 1;
01711 return 0;
01712 }
01713 #endif
01714
01715 #ifdef COLLECT_EXPORT_LIST
01716
01717 #if 0
01718 static int
01719 is_in_list (const char *prefix, struct id *list)
01720 {
01721 while (list)
01722 {
01723 if (!strcmp (prefix, list->name)) return 1;
01724 list = list->next;
01725 }
01726 return 0;
01727 }
01728 #endif
01729 #endif
01730
01731
01732 #ifdef COLLECT_EXPORT_LIST
01733 static void
01734 dump_list (FILE *stream, const char *prefix, struct id *list)
01735 {
01736 while (list)
01737 {
01738 fprintf (stream, "%s%s,\n", prefix, list->name);
01739 list = list->next;
01740 }
01741 }
01742 #endif
01743
01744 #if 0
01745 static void
01746 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
01747 {
01748 while (list)
01749 {
01750 fprintf (stream, "%s%s,\n", prefix, list->prefix);
01751 list = list->next;
01752 }
01753 }
01754 #endif
01755
01756 static void
01757 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
01758 {
01759 while (list)
01760 {
01761 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
01762 prefix, list->sequence, list->name);
01763 list = list->next;
01764 }
01765 }
01766
01767
01768
01769
01770 static void
01771 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
01772 {
01773 const char *p, *q;
01774 char *prefix, *r;
01775 int frames = (frame_tables.number > 0);
01776
01777
01778 p = strrchr (output_file, '/');
01779 if (p == 0)
01780 p = output_file;
01781 else
01782 p++;
01783 q = p;
01784 while (q)
01785 {
01786 q = strchr (q,'.');
01787 if (q == 0)
01788 {
01789 q = p + strlen (p);
01790 break;
01791 }
01792 else
01793 {
01794 if (strncmp (q, ".so", 3) == 0)
01795 {
01796 q += 3;
01797 break;
01798 }
01799 else
01800 q++;
01801 }
01802 }
01803
01804 prefix = XNEWVEC (char, q - p + 1);
01805 strncpy (prefix, p, q - p);
01806 prefix[q - p] = 0;
01807 for (r = prefix; *r; r++)
01808 if (!ISALNUM ((unsigned char)*r))
01809 *r = '_';
01810 if (debug)
01811 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
01812 output_file, prefix);
01813
01814 initname = concat ("_GLOBAL__FI_", prefix, NULL);
01815 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
01816
01817 free (prefix);
01818
01819
01820
01821 fprintf (stream, "static int count;\n");
01822 fprintf (stream, "typedef void entry_pt();\n");
01823 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
01824
01825 if (frames)
01826 {
01827 write_list_with_asm (stream, "extern void *", frame_tables.first);
01828
01829 fprintf (stream, "\tstatic void *frame_table[] = {\n");
01830 write_list (stream, "\t\t&", frame_tables.first);
01831 fprintf (stream, "\t0\n};\n");
01832
01833
01834 fprintf (stream, "struct object {\n");
01835 fprintf (stream, " void *pc_begin;\n");
01836 fprintf (stream, " void *pc_end;\n");
01837 fprintf (stream, " void *fde_begin;\n");
01838 fprintf (stream, " void *fde_array;\n");
01839 fprintf (stream, " __SIZE_TYPE__ count;\n");
01840 fprintf (stream, " struct object *next;\n");
01841 fprintf (stream, "};\n");
01842
01843 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
01844 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
01845
01846 fprintf (stream, "static void reg_frame () {\n");
01847 fprintf (stream, "\tstatic struct object ob;\n");
01848 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
01849 fprintf (stream, "\t}\n");
01850
01851 fprintf (stream, "static void dereg_frame () {\n");
01852 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
01853 fprintf (stream, "\t}\n");
01854 }
01855
01856 fprintf (stream, "void %s() {\n", initname);
01857 if (constructors.number > 0 || frames)
01858 {
01859 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
01860 write_list (stream, "\t\t", constructors.first);
01861 if (frames)
01862 fprintf (stream, "\treg_frame,\n");
01863 fprintf (stream, "\t};\n");
01864 fprintf (stream, "\tentry_pt **p;\n");
01865 fprintf (stream, "\tif (count++ != 0) return;\n");
01866 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
01867 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
01868 }
01869 else
01870 fprintf (stream, "\t++count;\n");
01871 fprintf (stream, "}\n");
01872 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
01873 fprintf (stream, "void %s() {\n", fininame);
01874 if (destructors.number > 0 || frames)
01875 {
01876 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
01877 write_list (stream, "\t\t", destructors.first);
01878 if (frames)
01879 fprintf (stream, "\tdereg_frame,\n");
01880 fprintf (stream, "\t};\n");
01881 fprintf (stream, "\tentry_pt **p;\n");
01882 fprintf (stream, "\tif (--count != 0) return;\n");
01883 fprintf (stream, "\tp = dtors;\n");
01884 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
01885 destructors.number + frames);
01886 }
01887 fprintf (stream, "}\n");
01888
01889 if (shared_obj)
01890 {
01891 COLLECT_SHARED_INIT_FUNC(stream, initname);
01892 COLLECT_SHARED_FINI_FUNC(stream, fininame);
01893 }
01894 }
01895
01896
01897
01898 #ifndef LD_INIT_SWITCH
01899 static void
01900 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
01901 {
01902
01903
01904 int frames = (frame_tables.number > 0);
01905
01906 fprintf (stream, "typedef void entry_pt();\n\n");
01907
01908 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
01909
01910 if (frames)
01911 {
01912 write_list_with_asm (stream, "extern void *", frame_tables.first);
01913
01914 fprintf (stream, "\tstatic void *frame_table[] = {\n");
01915 write_list (stream, "\t\t&", frame_tables.first);
01916 fprintf (stream, "\t0\n};\n");
01917
01918
01919 fprintf (stream, "struct object {\n");
01920 fprintf (stream, " void *pc_begin;\n");
01921 fprintf (stream, " void *pc_end;\n");
01922 fprintf (stream, " void *fde_begin;\n");
01923 fprintf (stream, " void *fde_array;\n");
01924 fprintf (stream, " __SIZE_TYPE__ count;\n");
01925 fprintf (stream, " struct object *next;\n");
01926 fprintf (stream, "};\n");
01927
01928 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
01929 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
01930
01931 fprintf (stream, "static void reg_frame () {\n");
01932 fprintf (stream, "\tstatic struct object ob;\n");
01933 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
01934 fprintf (stream, "\t}\n");
01935
01936 fprintf (stream, "static void dereg_frame () {\n");
01937 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
01938 fprintf (stream, "\t}\n");
01939 }
01940
01941 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
01942 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
01943 write_list (stream, "\t", constructors.first);
01944 if (frames)
01945 fprintf (stream, "\treg_frame,\n");
01946 fprintf (stream, "\t0\n};\n\n");
01947
01948 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
01949
01950 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
01951 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
01952 write_list (stream, "\t", destructors.first);
01953 if (frames)
01954 fprintf (stream, "\tdereg_frame,\n");
01955 fprintf (stream, "\t0\n};\n\n");
01956
01957 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
01958 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
01959 }
01960 #endif
01961
01962 static void
01963 write_c_file (FILE *stream, const char *name)
01964 {
01965 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
01966 #ifndef LD_INIT_SWITCH
01967 if (! shared_obj)
01968 write_c_file_glob (stream, name);
01969 else
01970 #endif
01971 write_c_file_stat (stream, name);
01972 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
01973 }
01974
01975 #ifdef COLLECT_EXPORT_LIST
01976 static void
01977 write_aix_file (FILE *stream, struct id *list)
01978 {
01979 for (; list; list = list->next)
01980 {
01981 fputs (list->name, stream);
01982 putc ('\n', stream);
01983 }
01984 }
01985 #endif
01986
01987 #ifdef OBJECT_FORMAT_NONE
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998 static void
01999 scan_prog_file (const char *prog_name, enum pass which_pass)
02000 {
02001 void (*int_handler) (int);
02002 #ifdef SIGQUIT
02003 void (*quit_handler) (int);
02004 #endif
02005 char *real_nm_argv[4];
02006 const char **nm_argv = (const char **) real_nm_argv;
02007 int argc = 0;
02008 struct pex_obj *pex;
02009 const char *errmsg;
02010 int err;
02011 char *p, buf[1024];
02012 FILE *inf;
02013
02014 if (which_pass == PASS_SECOND)
02015 return;
02016
02017
02018 if (nm_file_name == 0)
02019 fatal ("cannot find 'nm'");
02020
02021 nm_argv[argc++] = nm_file_name;
02022 if (NM_FLAGS[0] != '\0')
02023 nm_argv[argc++] = NM_FLAGS;
02024
02025 nm_argv[argc++] = prog_name;
02026 nm_argv[argc++] = (char *) 0;
02027
02028
02029 if (vflag)
02030 {
02031 const char **p_argv;
02032 const char *str;
02033
02034 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
02035 fprintf (stderr, " %s", str);
02036
02037 fprintf (stderr, "\n");
02038 }
02039
02040 fflush (stdout);
02041 fflush (stderr);
02042
02043 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
02044 if (pex == NULL)
02045 fatal_perror ("pex_init failed");
02046
02047 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
02048 if (errmsg != NULL)
02049 {
02050 if (err != 0)
02051 {
02052 errno = err;
02053 fatal_perror (errmsg);
02054 }
02055 else
02056 fatal (errmsg);
02057 }
02058
02059 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
02060 #ifdef SIGQUIT
02061 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
02062 #endif
02063
02064 inf = pex_read_output (pex, 0);
02065 if (inf == NULL)
02066 fatal_perror ("can't open nm output");
02067
02068 if (debug)
02069 fprintf (stderr, "\nnm output with constructors/destructors.\n");
02070
02071
02072 while (fgets (buf, sizeof buf, inf) != (char *) 0)
02073 {
02074 int ch, ch2;
02075 char *name, *end;
02076
02077
02078
02079
02080 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
02081 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
02082 break;
02083
02084 if (ch != '_')
02085 continue;
02086
02087 name = p;
02088
02089
02090 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
02091 end++)
02092 continue;
02093
02094
02095 *end = '\0';
02096 switch (is_ctor_dtor (name))
02097 {
02098 case 1:
02099 if (which_pass != PASS_LIB)
02100 add_to_list (&constructors, name);
02101 break;
02102
02103 case 2:
02104 if (which_pass != PASS_LIB)
02105 add_to_list (&destructors, name);
02106 break;
02107
02108 case 3:
02109 if (which_pass != PASS_LIB)
02110 fatal ("init function found in object %s", prog_name);
02111 #ifndef LD_INIT_SWITCH
02112 add_to_list (&constructors, name);
02113 #endif
02114 break;
02115
02116 case 4:
02117 if (which_pass != PASS_LIB)
02118 fatal ("fini function found in object %s", prog_name);
02119 #ifndef LD_FINI_SWITCH
02120 add_to_list (&destructors, name);
02121 #endif
02122 break;
02123
02124 case 5:
02125 if (which_pass != PASS_LIB)
02126 add_to_list (&frame_tables, name);
02127 break;
02128
02129 default:
02130 continue;
02131 }
02132
02133 if (debug)
02134 fprintf (stderr, "\t%s\n", buf);
02135 }
02136
02137 if (debug)
02138 fprintf (stderr, "\n");
02139
02140 do_wait (nm_file_name, pex);
02141
02142 signal (SIGINT, int_handler);
02143 #ifdef SIGQUIT
02144 signal (SIGQUIT, quit_handler);
02145 #endif
02146 }
02147
02148 #ifdef LDD_SUFFIX
02149
02150
02151
02152
02153
02154 static void
02155 scan_libraries (const char *prog_name)
02156 {
02157 static struct head libraries;
02158 struct id *list;
02159 void (*int_handler) (int);
02160 #ifdef SIGQUIT
02161 void (*quit_handler) (int);
02162 #endif
02163 char *real_ldd_argv[4];
02164 const char **ldd_argv = (const char **) real_ldd_argv;
02165 int argc = 0;
02166 struct pex_obj *pex;
02167 const char *errmsg;
02168 int err;
02169 char buf[1024];
02170 FILE *inf;
02171
02172
02173 if (ldd_file_name == 0)
02174 {
02175 error ("cannot find 'ldd'");
02176 return;
02177 }
02178
02179 ldd_argv[argc++] = ldd_file_name;
02180 ldd_argv[argc++] = prog_name;
02181 ldd_argv[argc++] = (char *) 0;
02182
02183
02184 if (vflag)
02185 {
02186 const char **p_argv;
02187 const char *str;
02188
02189 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
02190 fprintf (stderr, " %s", str);
02191
02192 fprintf (stderr, "\n");
02193 }
02194
02195 fflush (stdout);
02196 fflush (stderr);
02197
02198 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
02199 if (pex == NULL)
02200 fatal_perror ("pex_init failed");
02201
02202 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
02203 if (errmsg != NULL)
02204 {
02205 if (err != 0)
02206 {
02207 errno = err;
02208 fatal_perror (errmsg);
02209 }
02210 else
02211 fatal (errmsg);
02212 }
02213
02214 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
02215 #ifdef SIGQUIT
02216 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
02217 #endif
02218
02219 inf = pex_read_output (pex, 0);
02220 if (inf == NULL)
02221 fatal_perror ("can't open ldd output");
02222
02223 if (debug)
02224 notice ("\nldd output with constructors/destructors.\n");
02225
02226
02227 while (fgets (buf, sizeof buf, inf) != (char *) 0)
02228 {
02229 int ch2;
02230 char *name, *end, *p = buf;
02231
02232
02233 PARSE_LDD_OUTPUT (p);
02234 if (p == 0)
02235 continue;
02236
02237 name = p;
02238 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
02239 fatal ("dynamic dependency %s not found", buf);
02240
02241
02242 for (end = p;
02243 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
02244 end++)
02245 continue;
02246 *end = '\0';
02247
02248 if (access (name, R_OK) == 0)
02249 add_to_list (&libraries, name);
02250 else
02251 fatal ("unable to open dynamic dependency '%s'", buf);
02252
02253 if (debug)
02254 fprintf (stderr, "\t%s\n", buf);
02255 }
02256 if (debug)
02257 fprintf (stderr, "\n");
02258
02259 do_wait (ldd_file_name, pex);
02260
02261 signal (SIGINT, int_handler);
02262 #ifdef SIGQUIT
02263 signal (SIGQUIT, quit_handler);
02264 #endif
02265
02266
02267
02268 for (list = libraries.first; list; list = list->next)
02269 scan_prog_file (list->name, PASS_LIB);
02270 }
02271
02272 #endif
02273
02274 #endif
02275
02276
02277
02278
02279
02280
02281 #ifdef OBJECT_FORMAT_COFF
02282
02283 #if defined (EXTENDED_COFF)
02284
02285 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
02286 # define GCC_SYMENT SYMR
02287 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
02288 # define GCC_SYMINC(X) (1)
02289 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
02290 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
02291
02292 #else
02293
02294 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
02295 # define GCC_SYMENT SYMENT
02296 # if defined (C_WEAKEXT)
02297 # define GCC_OK_SYMBOL(X) \
02298 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
02299 ((X).n_scnum > N_UNDEF) && \
02300 (aix64_flag \
02301 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
02302 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
02303 # define GCC_UNDEF_SYMBOL(X) \
02304 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
02305 ((X).n_scnum == N_UNDEF))
02306 # else
02307 # define GCC_OK_SYMBOL(X) \
02308 (((X).n_sclass == C_EXT) && \
02309 ((X).n_scnum > N_UNDEF) && \
02310 (aix64_flag \
02311 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
02312 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
02313 # define GCC_UNDEF_SYMBOL(X) \
02314 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
02315 # endif
02316 # define GCC_SYMINC(X) ((X).n_numaux+1)
02317 # define GCC_SYMZERO(X) 0
02318
02319
02320 #ifdef _AIX51
02321 # define GCC_CHECK_HDR(X) \
02322 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
02323 || (HEADER (X).f_magic == 0767 && aix64_flag))
02324 #else
02325 # define GCC_CHECK_HDR(X) \
02326 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
02327 || (HEADER (X).f_magic == 0757 && aix64_flag))
02328 #endif
02329
02330 #endif
02331
02332 #ifdef COLLECT_EXPORT_LIST
02333
02334
02335 static const char *const aix_std_libs[] = {
02336 "/unix",
02337 "/lib/libc.a",
02338 "/lib/libm.a",
02339 "/lib/libc_r.a",
02340 "/lib/libm_r.a",
02341 "/usr/lib/libc.a",
02342 "/usr/lib/libm.a",
02343 "/usr/lib/libc_r.a",
02344 "/usr/lib/libm_r.a",
02345 "/usr/lib/threads/libc.a",
02346 "/usr/ccs/lib/libc.a",
02347 "/usr/ccs/lib/libm.a",
02348 "/usr/ccs/lib/libc_r.a",
02349 "/usr/ccs/lib/libm_r.a",
02350 NULL
02351 };
02352
02353
02354
02355 static int ignore_library (const char *);
02356 static int
02357 ignore_library (const char *name)
02358 {
02359 const char *const *p = &aix_std_libs[0];
02360 while (*p++ != NULL)
02361 if (! strcmp (name, *p)) return 1;
02362 return 0;
02363 }
02364 #endif
02365
02366 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
02367 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
02368 #endif
02369
02370
02371
02372
02373
02374
02375
02376
02377
02378
02379 static void
02380 scan_prog_file (const char *prog_name, enum pass which_pass)
02381 {
02382 LDFILE *ldptr = NULL;
02383 int sym_index, sym_count;
02384 int is_shared = 0;
02385
02386 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
02387 return;
02388
02389 #ifdef COLLECT_EXPORT_LIST
02390
02391 if (which_pass == PASS_FIRST && ignore_library (prog_name))
02392 return;
02393
02394
02395
02396
02397 do
02398 {
02399 #endif
02400
02401
02402
02403
02404 if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
02405 {
02406 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
02407 fatal ("%s: not a COFF file", prog_name);
02408
02409 if (GCC_CHECK_HDR (ldptr))
02410 {
02411 sym_count = GCC_SYMBOLS (ldptr);
02412 sym_index = GCC_SYMZERO (ldptr);
02413
02414 #ifdef COLLECT_EXPORT_LIST
02415
02416 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
02417 #endif
02418
02419 while (sym_index < sym_count)
02420 {
02421 GCC_SYMENT symbol;
02422
02423 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
02424 break;
02425 sym_index += GCC_SYMINC (symbol);
02426
02427 if (GCC_OK_SYMBOL (symbol))
02428 {
02429 char *name;
02430
02431 if ((name = ldgetname (ldptr, &symbol)) == NULL)
02432 continue;
02433
02434 #ifdef XCOFF_DEBUGGING_INFO
02435
02436
02437 if (*name == '.')
02438 ++name;
02439 #endif
02440
02441 switch (is_ctor_dtor (name))
02442 {
02443 case 1:
02444 if (! is_shared)
02445 add_to_list (&constructors, name);
02446 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
02447 if (which_pass == PASS_OBJ)
02448 add_to_list (&exports, name);
02449 #endif
02450 break;
02451
02452 case 2:
02453 if (! is_shared)
02454 add_to_list (&destructors, name);
02455 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
02456 if (which_pass == PASS_OBJ)
02457 add_to_list (&exports, name);
02458 #endif
02459 break;
02460
02461 #ifdef COLLECT_EXPORT_LIST
02462 case 3:
02463 #ifndef LD_INIT_SWITCH
02464 if (is_shared)
02465 add_to_list (&constructors, name);
02466 #endif
02467 break;
02468
02469 case 4:
02470 #ifndef LD_INIT_SWITCH
02471 if (is_shared)
02472 add_to_list (&destructors, name);
02473 #endif
02474 break;
02475 #endif
02476
02477 case 5:
02478 if (! is_shared)
02479 add_to_list (&frame_tables, name);
02480 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
02481 if (which_pass == PASS_OBJ)
02482 add_to_list (&exports, name);
02483 #endif
02484 break;
02485
02486 default:
02487 #ifdef COLLECT_EXPORT_LIST
02488
02489
02490
02491
02492
02493 if (shared_obj && !is_shared
02494 && which_pass == PASS_OBJ && !export_flag)
02495 add_to_list (&exports, name);
02496 #endif
02497 continue;
02498 }
02499
02500 if (debug)
02501 #if !defined(EXTENDED_COFF)
02502 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
02503 symbol.n_scnum, symbol.n_sclass,
02504 (symbol.n_type ? "0" : ""), symbol.n_type,
02505 name);
02506 #else
02507 fprintf (stderr,
02508 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
02509 symbol.iss, (long) symbol.value, symbol.index, name);
02510 #endif
02511 }
02512 }
02513 }
02514 #ifdef COLLECT_EXPORT_LIST
02515 else
02516 {
02517
02518
02519 if (debug)
02520 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
02521 prog_name, HEADER (ldptr).f_magic, aix64_flag);
02522 }
02523 #endif
02524 }
02525 else
02526 {
02527 fatal ("%s: cannot open as COFF file", prog_name);
02528 }
02529 #ifdef COLLECT_EXPORT_LIST
02530
02531 }
02532 while (ldclose (ldptr) == FAILURE);
02533 #else
02534
02535 (void) ldclose(ldptr);
02536 #endif
02537 }
02538 #endif
02539
02540 #ifdef COLLECT_EXPORT_LIST
02541
02542
02543 static char *
02544 resolve_lib_name (const char *name)
02545 {
02546 char *lib_buf;
02547 int i, j, l = 0;
02548
02549 const char * const libexts[2] = {"a", "so"};
02550
02551 for (i = 0; libpaths[i]; i++)
02552 if (libpaths[i]->max_len > l)
02553 l = libpaths[i]->max_len;
02554
02555 lib_buf = xmalloc (l + strlen(name) + 10);
02556
02557 for (i = 0; libpaths[i]; i++)
02558 {
02559 struct prefix_list *list = libpaths[i]->plist;
02560 for (; list; list = list->next)
02561 {
02562
02563
02564
02565 const char *p = "";
02566 if (list->prefix[strlen(list->prefix)-1] != '/')
02567 p = "/";
02568 for (j = 0; j < 2; j++)
02569 {
02570 sprintf (lib_buf, "%s%slib%s.%s",
02571 list->prefix, p, name,
02572 libexts[(j + aixrtl_flag) % 2]);
02573 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
02574 if (file_exists (lib_buf))
02575 {
02576 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
02577 return (lib_buf);
02578 }
02579 }
02580 }
02581 }
02582 if (debug)
02583 fprintf (stderr, "not found\n");
02584 else
02585 fatal ("library lib%s not found", name);
02586 return (NULL);
02587 }
02588 #endif